{"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting an element s properties", "response": "def _get_element_properties(name, element_type, server=None):\n '''\n Get an element's properties\n '''\n properties = {}\n data = _api_get('{0}/{1}/property'.format(element_type, name), server)\n\n # Get properties into a dict\n if any(data['extraProperties']['properties']):\n for element in data['extraProperties']['properties']:\n properties[element['name']] = element['value']\n return properties\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_element(name, element_type, server=None, with_properties=True):\n '''\n Get an element with or without properties\n '''\n element = {}\n name = quote(name, safe='')\n data = _api_get('{0}/{1}'.format(element_type, name), server)\n\n # Format data, get properties if asked, and return the whole thing\n if any(data['extraProperties']['entity']):\n for key, value in data['extraProperties']['entity'].items():\n element[key] = value\n if with_properties:\n element['properties'] = _get_element_properties(name, element_type)\n return element\n return None", "response": "Get an element with or without properties"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _create_element(name, element_type, data, server=None):\n '''\n Create a new element\n '''\n # Define property and id from name and properties + remove SaltStack parameters\n if 'properties' in data:\n data['property'] = ''\n for key, value in data['properties'].items():\n if not data['property']:\n data['property'] += '{0}={1}'.format(key, value.replace(':', '\\\\:'))\n else:\n data['property'] += ':{0}={1}'.format(key, value.replace(':', '\\\\:'))\n del data['properties']\n\n # Send request\n _api_post(element_type, _clean_data(data), server)\n return unquote(name)", "response": "Create a new element in the order they appear in the element list."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _update_element(name, element_type, data, server=None):\n '''\n Update an element, including it's properties\n '''\n # Urlencode the name (names may have slashes)\n name = quote(name, safe='')\n\n # Update properties first\n if 'properties' in data:\n properties = []\n for key, value in data['properties'].items():\n properties.append({'name': key, 'value': value})\n _api_post('{0}/{1}/property'.format(element_type, name), properties, server)\n del data['properties']\n\n # If the element only contained properties\n if not data:\n return unquote(name)\n\n # Get the current data then merge updated data into it\n update_data = _get_element(name, element_type, server, with_properties=False)\n if update_data:\n update_data.update(data)\n else:\n __context__['retcode'] = salt.defaults.exitcodes.SALT_BUILD_FAIL\n raise CommandExecutionError('Cannot update {0}'.format(name))\n\n # Finally, update the element\n _api_post('{0}/{1}'.format(element_type, name), _clean_data(update_data), server)\n return unquote(name)", "response": "Update an element including it s properties and properties"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _delete_element(name, element_type, data, server=None):\n '''\n Delete an element\n '''\n _api_delete('{0}/{1}'.format(element_type, quote(name, safe='')), data, server)\n return name", "response": "Delete an element from the sequence."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a connection pool", "response": "def create_connector_c_pool(name, server=None, **kwargs):\n '''\n Create a connection pool\n '''\n defaults = {\n 'connectionDefinitionName': 'javax.jms.ConnectionFactory',\n 'resourceAdapterName': 'jmsra',\n 'associateWithThread': False,\n 'connectionCreationRetryAttempts': 0,\n 'connectionCreationRetryIntervalInSeconds': 0,\n 'connectionLeakReclaim': False,\n 'connectionLeakTimeoutInSeconds': 0,\n 'description': '',\n 'failAllConnections': False,\n 'id': name,\n 'idleTimeoutInSeconds': 300,\n 'isConnectionValidationRequired': False,\n 'lazyConnectionAssociation': False,\n 'lazyConnectionEnlistment': False,\n 'matchConnections': True,\n 'maxConnectionUsageCount': 0,\n 'maxPoolSize': 32,\n 'maxWaitTimeInMillis': 60000,\n 'ping': False,\n 'poolResizeQuantity': 2,\n 'pooling': True,\n 'steadyPoolSize': 8,\n 'target': 'server',\n 'transactionSupport': '',\n 'validateAtmostOncePeriodInSeconds': 0\n }\n\n # Data = defaults + merge kwargs + remove salt\n data = defaults\n data.update(kwargs)\n\n # Check TransactionSupport against acceptable values\n if data['transactionSupport'] and data['transactionSupport'] not in (\n 'XATransaction',\n 'LocalTransaction',\n 'NoTransaction'\n ):\n raise CommandExecutionError('Invalid transaction support')\n\n return _create_element(name, 'resources/connector-connection-pool', data, server)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nupdates a connector connection pool", "response": "def update_connector_c_pool(name, server=None, **kwargs):\n '''\n Update a connection pool\n '''\n if 'transactionSupport' in kwargs and kwargs['transactionSupport'] not in (\n 'XATransaction',\n 'LocalTransaction',\n 'NoTransaction'\n ):\n raise CommandExecutionError('Invalid transaction support')\n return _update_element(name, 'resources/connector-connection-pool', kwargs, server)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef delete_connector_c_pool(name, target='server', cascade=True, server=None):\n '''\n Delete a connection pool\n '''\n data = {'target': target, 'cascade': cascade}\n return _delete_element(name, 'resources/connector-connection-pool', data, server)", "response": "Delete a connector connection pool."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a connection resource", "response": "def create_connector_resource(name, server=None, **kwargs):\n '''\n Create a connection resource\n '''\n defaults = {\n 'description': '',\n 'enabled': True,\n 'id': name,\n 'poolName': '',\n 'objectType': 'user',\n 'target': 'server'\n }\n\n # Data = defaults + merge kwargs + poolname\n data = defaults\n data.update(kwargs)\n\n if not data['poolName']:\n raise CommandExecutionError('No pool name!')\n\n # Fix for lowercase vs camelCase naming differences\n for key, value in list(data.items()):\n del data[key]\n data[key.lower()] = value\n\n return _create_element(name, 'resources/connector-resource', data, server)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating a connection resource", "response": "def update_connector_resource(name, server=None, **kwargs):\n '''\n Update a connection resource\n '''\n # You're not supposed to update jndiName, if you do so, it will crash, silently\n if 'jndiName' in kwargs:\n del kwargs['jndiName']\n return _update_element(name, 'resources/connector-resource', kwargs, server)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a JMS destination resource", "response": "def create_admin_object_resource(name, server=None, **kwargs):\n '''\n Create a JMS destination\n '''\n defaults = {\n 'description': '',\n 'className': 'com.sun.messaging.Queue',\n 'enabled': True,\n 'id': name,\n 'resAdapter': 'jmsra',\n 'resType': 'javax.jms.Queue',\n 'target': 'server'\n }\n\n # Data = defaults + merge kwargs + poolname\n data = defaults\n data.update(kwargs)\n\n # ClassName isn't optional, even if the API says so\n if data['resType'] == 'javax.jms.Queue':\n data['className'] = 'com.sun.messaging.Queue'\n elif data['resType'] == 'javax.jms.Topic':\n data['className'] = 'com.sun.messaging.Topic'\n else:\n raise CommandExecutionError('resType should be \"javax.jms.Queue\" or \"javax.jms.Topic\"!')\n\n if data['resAdapter'] != 'jmsra':\n raise CommandExecutionError('resAdapter should be \"jmsra\"!')\n\n # Fix for lowercase vs camelCase naming differences\n if 'resType' in data:\n data['restype'] = data['resType']\n del data['resType']\n if 'className' in data:\n data['classname'] = data['className']\n del data['className']\n\n return _create_element(name, 'resources/admin-object-resource', data, server)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef update_admin_object_resource(name, server=None, **kwargs):\n '''\n Update a JMS destination\n '''\n if 'jndiName' in kwargs:\n del kwargs['jndiName']\n return _update_element(name, 'resources/admin-object-resource', kwargs, server)", "response": "Update an admin object resource"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a new JDBC connection pool.", "response": "def create_jdbc_connection_pool(name, server=None, **kwargs):\n '''\n Create a connection resource\n '''\n defaults = {\n 'allowNonComponentCallers': False,\n 'associateWithThread': False,\n 'connectionCreationRetryAttempts': '0',\n 'connectionCreationRetryIntervalInSeconds': '10',\n 'connectionLeakReclaim': False,\n 'connectionLeakTimeoutInSeconds': '0',\n 'connectionValidationMethod': 'table',\n 'datasourceClassname': '',\n 'description': '',\n 'driverClassname': '',\n 'failAllConnections': False,\n 'idleTimeoutInSeconds': '300',\n 'initSql': '',\n 'isConnectionValidationRequired': False,\n 'isIsolationLevelGuaranteed': True,\n 'lazyConnectionAssociation': False,\n 'lazyConnectionEnlistment': False,\n 'matchConnections': False,\n 'maxConnectionUsageCount': '0',\n 'maxPoolSize': '32',\n 'maxWaitTimeInMillis': 60000,\n 'name': name,\n 'nonTransactionalConnections': False,\n 'ping': False,\n 'poolResizeQuantity': '2',\n 'pooling': True,\n 'resType': '',\n 'sqlTraceListeners': '',\n 'statementCacheSize': '0',\n 'statementLeakReclaim': False,\n 'statementLeakTimeoutInSeconds': '0',\n 'statementTimeoutInSeconds': '-1',\n 'steadyPoolSize': '8',\n 'target': 'server',\n 'transactionIsolationLevel': '',\n 'validateAtmostOncePeriodInSeconds': '0',\n 'validationClassname': '',\n 'validationTableName': '',\n 'wrapJdbcObjects': True\n }\n\n # Data = defaults + merge kwargs + poolname\n data = defaults\n data.update(kwargs)\n\n # Check resType against acceptable values\n if data['resType'] not in (\n 'javax.sql.DataSource',\n 'javax.sql.XADataSource',\n 'javax.sql.ConnectionPoolDataSource',\n 'java.sql.Driver'\n ):\n raise CommandExecutionError('Invalid resource type')\n\n # Check connectionValidationMethod against acceptable velues\n if data['connectionValidationMethod'] not in (\n 'auto-commit',\n 'meta-data',\n 'table',\n 'custom-validation'\n ):\n raise CommandExecutionError('Invalid connection validation method')\n\n if data['transactionIsolationLevel'] \\\n and data['transactionIsolationLevel'] not in (\n 'read-uncommitted',\n 'read-committed',\n 'repeatable-read',\n 'serializable'\n ):\n raise CommandExecutionError('Invalid transaction isolation level')\n\n if not data['datasourceClassname'] \\\n and data['resType'] in (\n 'javax.sql.DataSource',\n 'javax.sql.ConnectionPoolDataSource',\n 'javax.sql.XADataSource'\n ):\n raise CommandExecutionError('No datasource class name while using datasource resType')\n if not data['driverClassname'] and data['resType'] == 'java.sql.Driver':\n raise CommandExecutionError('No driver class nime while using driver resType')\n\n return _create_element(name, 'resources/jdbc-connection-pool', data, server)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete_jdbc_connection_pool(name, target='server', cascade=False, server=None):\n '''\n Delete a JDBC pool\n '''\n data = {'target': target, 'cascade': cascade}\n return _delete_element(name, 'resources/jdbc-connection-pool', data, server)", "response": "Delete a JDBC connection pool."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a JDBC resource", "response": "def create_jdbc_resource(name, server=None, **kwargs):\n '''\n Create a JDBC resource\n '''\n defaults = {\n 'description': '',\n 'enabled': True,\n 'id': name,\n 'poolName': '',\n 'target': 'server'\n }\n\n # Data = defaults + merge kwargs + poolname\n data = defaults\n data.update(kwargs)\n\n if not data['poolName']:\n raise CommandExecutionError('No pool name!')\n\n return _create_element(name, 'resources/jdbc-resource', data, server)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nupdate a JDBC resource", "response": "def update_jdbc_resource(name, server=None, **kwargs):\n '''\n Update a JDBC resource\n '''\n # You're not supposed to update jndiName, if you do so, it will crash, silently\n if 'jndiName' in kwargs:\n del kwargs['jndiName']\n return _update_element(name, 'resources/jdbc-resource', kwargs, server)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting system properties from the server", "response": "def get_system_properties(server=None):\n '''\n Get system properties\n '''\n properties = {}\n data = _api_get('system-properties', server)\n\n # Get properties into a dict\n if any(data['extraProperties']['systemProperties']):\n for element in data['extraProperties']['systemProperties']:\n properties[element['name']] = element['value']\n return properties\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef present(name, auth=None, **kwargs):\n '''\n Ensure a network exists and is up-to-date\n\n name\n Name of the network\n\n provider\n A dict of network provider options.\n\n shared\n Set the network as shared.\n\n external\n Whether this network is externally accessible.\n\n admin_state_up\n Set the network administrative state to up.\n\n vlan\n Vlan ID. Alias for provider\n\n - physical_network: provider\n - network_type: vlan\n - segmentation_id: (vlan id)\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n kwargs = __utils__['args.clean_kwargs'](**kwargs)\n\n __salt__['neutronng.setup_clouds'](auth)\n\n kwargs['name'] = name\n network = __salt__['neutronng.network_get'](name=name)\n\n if network is None:\n if __opts__['test'] is True:\n ret['result'] = None\n ret['changes'] = kwargs\n ret['comment'] = 'Network will be created.'\n return ret\n\n if 'vlan' in kwargs:\n kwargs['provider'] = {\"physical_network\": \"provider\",\n \"network_type\": \"vlan\",\n \"segmentation_id\": kwargs['vlan']}\n del kwargs['vlan']\n\n if 'project' in kwargs:\n projectname = kwargs['project']\n project = __salt__['keystoneng.project_get'](name=projectname)\n if project:\n kwargs['project_id'] = project.id\n del kwargs['project']\n else:\n ret['result'] = False\n ret['comment'] = \"Project:{} not found.\".format(projectname)\n return ret\n\n network = __salt__['neutronng.network_create'](**kwargs)\n ret['changes'] = network\n ret['comment'] = 'Created network'\n return ret\n\n changes = __salt__['neutronng.compare_changes'](network, **kwargs)\n\n # there's no method for network update in shade right now;\n # can only delete and recreate\n if changes:\n if __opts__['test'] is True:\n ret['result'] = None\n ret['changes'] = changes\n ret['comment'] = 'Project will be updated.'\n return ret\n\n __salt__['neutronng.network_delete'](name=network)\n __salt__['neutronng.network_create'](**kwargs)\n ret['changes'].update(changes)\n ret['comment'] = 'Updated network'\n\n return ret", "response": "Ensure a network exists and is up - to - date and is up - to - date."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef absent(name, auth=None, **kwargs):\n '''\n Ensure a network does not exists\n\n name\n Name of the network\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n kwargs = __utils__['args.clean_kwargs'](**kwargs)\n\n __salt__['neutronng.setup_clouds'](auth)\n\n kwargs['name'] = name\n network = __salt__['neutronng.network_get'](name=name)\n\n if network:\n if __opts__['test'] is True:\n ret['result'] = None\n ret['changes'] = {'id': network.id}\n ret['comment'] = 'Network will be deleted.'\n return ret\n\n __salt__['neutronng.network_delete'](name=network)\n ret['changes']['id'] = network.id\n ret['comment'] = 'Deleted network'\n\n return ret", "response": "Ensure a network does not exists"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsplitting a smartos docker uuid into repo and tag", "response": "def _split_docker_uuid(uuid):\n '''\n Split a smartos docker uuid into repo and tag\n '''\n if uuid:\n uuid = uuid.split(':')\n if len(uuid) == 2:\n tag = uuid[1]\n repo = uuid[0]\n return repo, tag\n return None, None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _is_uuid(uuid):\n '''\n Check if uuid is a valid smartos uuid\n\n Example: e69a0918-055d-11e5-8912-e3ceb6df4cf8\n '''\n if uuid and list((len(x) for x in uuid.split('-'))) == [8, 4, 4, 4, 12]:\n return True\n return False", "response": "Check if uuid is a valid smartos uuid"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef version():\n '''\n Return imgadm version\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' imgadm.version\n '''\n ret = {}\n cmd = 'imgadm --version'\n res = __salt__['cmd.run'](cmd).splitlines()\n ret = res[0].split()\n return ret[-1]", "response": "Return imgadm version\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' imgadm.version"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting the image uuid from an imported docker image .. versionadded:: 2019. 2. 0", "response": "def docker_to_uuid(uuid):\n '''\n Get the image uuid from an imported docker image\n\n .. versionadded:: 2019.2.0\n '''\n if _is_uuid(uuid):\n return uuid\n if _is_docker_uuid(uuid):\n images = list_installed(verbose=True)\n for image_uuid in images:\n if 'name' not in images[image_uuid]:\n continue\n if images[image_uuid]['name'] == uuid:\n return image_uuid\n return None"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef avail(search=None, verbose=False):\n '''\n Return a list of available images\n\n search : string\n search keyword\n verbose : boolean (False)\n toggle verbose output\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' imgadm.avail [percona]\n salt '*' imgadm.avail verbose=True\n '''\n ret = {}\n cmd = 'imgadm avail -j'\n res = __salt__['cmd.run_all'](cmd)\n retcode = res['retcode']\n if retcode != 0:\n ret['Error'] = _exit_status(retcode)\n return ret\n\n for image in salt.utils.json.loads(res['stdout']):\n if image['manifest']['disabled'] or not image['manifest']['public']:\n continue\n if search and search not in image['manifest']['name']:\n # we skip if we are searching but don't have a match\n continue\n uuid = image['manifest']['uuid']\n data = _parse_image_meta(image, verbose)\n if data:\n ret[uuid] = data\n\n return ret", "response": "Return a list of available images"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef show(uuid):\n '''\n Show manifest of a given image\n\n uuid : string\n uuid of image\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' imgadm.show e42f8c84-bbea-11e2-b920-078fab2aab1f\n salt '*' imgadm.show plexinc/pms-docker:plexpass\n '''\n ret = {}\n\n if _is_uuid(uuid) or _is_docker_uuid(uuid):\n cmd = 'imgadm show {0}'.format(uuid)\n res = __salt__['cmd.run_all'](cmd, python_shell=False)\n retcode = res['retcode']\n if retcode != 0:\n ret['Error'] = _exit_status(retcode, res['stderr'])\n else:\n ret = salt.utils.json.loads(res['stdout'])\n else:\n ret['Error'] = \"{} is not a valid uuid.\".format(uuid)\n\n return ret", "response": "Show manifest of a given image\nTaxonomy"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nimport an image from the repository", "response": "def import_image(uuid, verbose=False):\n '''\n Import an image from the repository\n\n uuid : string\n uuid to import\n verbose : boolean (False)\n toggle verbose output\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' imgadm.import e42f8c84-bbea-11e2-b920-078fab2aab1f [verbose=True]\n '''\n ret = {}\n cmd = 'imgadm import {0}'.format(uuid)\n res = __salt__['cmd.run_all'](cmd, python_shell=False)\n retcode = res['retcode']\n if retcode != 0:\n ret['Error'] = _exit_status(retcode)\n return ret\n\n uuid = docker_to_uuid(uuid)\n data = _parse_image_meta(get(uuid), verbose)\n return {uuid: data}"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nremoving an installed image", "response": "def delete(uuid):\n '''\n Remove an installed image\n\n uuid : string\n Specifies uuid to import\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' imgadm.delete e42f8c84-bbea-11e2-b920-078fab2aab1f\n '''\n ret = {}\n cmd = 'imgadm delete {0}'.format(uuid)\n res = __salt__['cmd.run_all'](cmd, python_shell=False)\n retcode = res['retcode']\n if retcode != 0:\n ret['Error'] = _exit_status(retcode)\n return ret\n # output: Deleted image d5b3865c-0804-11e5-be21-dbc4ce844ddc\n result = []\n for image in res['stdout'].splitlines():\n image = [var for var in image.split(\" \") if var]\n result.append(image[2])\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nremoving unused images from the vacuum image list", "response": "def vacuum(verbose=False):\n '''\n Remove unused images\n\n verbose : boolean (False)\n toggle verbose output\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' imgadm.vacuum [verbose=True]\n '''\n ret = {}\n cmd = 'imgadm vacuum -f'\n res = __salt__['cmd.run_all'](cmd)\n retcode = res['retcode']\n if retcode != 0:\n ret['Error'] = _exit_status(retcode)\n return ret\n # output: Deleted image d5b3865c-0804-11e5-be21-dbc4ce844ddc (lx-centos-6@20150601)\n result = {}\n for image in res['stdout'].splitlines():\n image = [var for var in image.split(\" \") if var]\n result[image[2]] = {\n 'name': image[3][1:image[3].index('@')],\n 'version': image[3][image[3].index('@')+1:-1]\n }\n if verbose:\n return result\n else:\n return list(result.keys())"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a list of available sources", "response": "def sources(verbose=False):\n '''\n Return a list of available sources\n\n verbose : boolean (False)\n toggle verbose output\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' imgadm.sources\n '''\n ret = {}\n cmd = 'imgadm sources -j'\n res = __salt__['cmd.run_all'](cmd)\n retcode = res['retcode']\n if retcode != 0:\n ret['Error'] = _exit_status(retcode)\n return ret\n\n for src in salt.utils.json.loads(res['stdout']):\n ret[src['url']] = src\n del src['url']\n\n if not verbose:\n ret = list(ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndeleting a source from the", "response": "def source_delete(source):\n '''\n Delete a source\n\n source : string\n source url to delete\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' imgadm.source_delete https://updates.joyent.com\n '''\n ret = {}\n cmd = 'imgadm sources -d {0}'.format(source)\n res = __salt__['cmd.run_all'](cmd)\n retcode = res['retcode']\n if retcode != 0:\n ret['Error'] = _exit_status(retcode, res['stderr'])\n return ret\n\n return sources(False)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef source_add(source, source_type='imgapi'):\n '''\n Add a new source\n\n source : string\n source url to add\n source_trype : string (imgapi)\n source type, either imgapi or docker\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' imgadm.source_add https://updates.joyent.com\n salt '*' imgadm.source_add https://docker.io docker\n '''\n ret = {}\n\n # NOTE: there are some undocumented deprecated source types\n # so we just warn instead of error on those\n if source_type not in ['imgapi', 'docker']:\n log.warning('Possible unsupported imgage source type specified!')\n\n cmd = 'imgadm sources -a {0} -t {1}'.format(source, source_type)\n res = __salt__['cmd.run_all'](cmd)\n retcode = res['retcode']\n if retcode != 0:\n ret['Error'] = _exit_status(retcode, res['stderr'])\n return ret\n\n return sources(False)", "response": "Add a new source to the\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning an object containing all the flags for dealing with Windows permissions and permissions.", "response": "def flags(instantiated=True):\n '''\n Helper function for instantiating a Flags object\n\n Args:\n\n instantiated (bool):\n True to return an instantiated object, False to return the object\n definition. Use False if inherited by another class. Default is\n True.\n\n Returns:\n object: An instance of the Flags object or its definition\n '''\n if not HAS_WIN32:\n return\n\n class Flags(object):\n '''\n Object containing all the flags for dealing with Windows permissions\n '''\n # Flag Dicts\n ace_perms = {\n 'file': {\n 'basic': {\n 0x1f01ff: 'Full control',\n 0x1301bf: 'Modify',\n 0x1201bf: 'Read & execute with write',\n 0x1200a9: 'Read & execute',\n 0x120089: 'Read',\n 0x100116: 'Write',\n 'full_control': 0x1f01ff,\n 'modify': 0x1301bf,\n 'read_execute': 0x1200a9,\n 'read': 0x120089,\n 'write': 0x100116,\n },\n 'advanced': {\n # Advanced\n 0x0001: 'List folder / read data',\n 0x0002: 'Create files / write data',\n 0x0004: 'Create folders / append data',\n 0x0008: 'Read extended attributes',\n 0x0010: 'Write extended attributes',\n 0x0020: 'Traverse folder / execute file',\n 0x0040: 'Delete subfolders and files',\n 0x0080: 'Read attributes',\n 0x0100: 'Write attributes',\n 0x10000: 'Delete',\n 0x20000: 'Read permissions',\n 0x40000: 'Change permissions',\n 0x80000: 'Take ownership',\n # 0x100000: 'SYNCHRONIZE', # This is in all of them\n 'list_folder': 0x0001,\n 'read_data': 0x0001,\n 'create_files': 0x0002,\n 'write_data': 0x0002,\n 'create_folders': 0x0004,\n 'append_data': 0x0004,\n 'read_ea': 0x0008,\n 'write_ea': 0x0010,\n 'traverse_folder': 0x0020,\n 'execute_file': 0x0020,\n 'delete_subfolders_files': 0x0040,\n 'read_attributes': 0x0080,\n 'write_attributes': 0x0100,\n 'delete': 0x10000,\n 'read_permissions': 0x20000,\n 'change_permissions': 0x40000,\n 'take_ownership': 0x80000,\n },\n },\n 'registry': {\n 'basic': {\n 0xf003f: 'Full Control',\n 0x20019: 'Read',\n 0x20006: 'Write',\n # Generic Values (These sometimes get hit)\n 0x10000000: 'Full Control',\n 0x20000000: 'Execute',\n 0x40000000: 'Write',\n 0xffffffff80000000: 'Read',\n 'full_control': 0xf003f,\n 'read': 0x20019,\n 'write': 0x20006,\n },\n 'advanced': {\n # Advanced\n 0x0001: 'Query Value',\n 0x0002: 'Set Value',\n 0x0004: 'Create Subkey',\n 0x0008: 'Enumerate Subkeys',\n 0x0010: 'Notify',\n 0x0020: 'Create Link',\n 0x10000: 'Delete',\n 0x20000: 'Read Control',\n 0x40000: 'Write DAC',\n 0x80000: 'Write Owner',\n 'query_value': 0x0001,\n 'set_value': 0x0002,\n 'create_subkey': 0x0004,\n 'enum_subkeys': 0x0008,\n 'notify': 0x0010,\n 'create_link': 0x0020,\n 'delete': 0x10000,\n 'read_control': 0x20000,\n 'write_dac': 0x40000,\n 'write_owner': 0x80000,\n },\n },\n 'share': {\n 'basic': {\n 0x1f01ff: 'Full control',\n 0x1301bf: 'Change',\n 0x1200a9: 'Read',\n 'full_control': 0x1f01ff,\n 'change': 0x1301bf,\n 'read': 0x1200a9,\n },\n 'advanced': {}, # No 'advanced' for shares, needed for lookup\n },\n 'printer': {\n 'basic': {\n 0x20008: 'Print',\n 0xf000c: 'Manage this printer',\n 0xf0030: 'Manage documents',\n 'print': 0x20008,\n 'manage_printer': 0xf000c,\n 'manage_documents': 0xf0030,\n },\n 'advanced': {\n # Advanced\n 0x10004: 'Manage this printer',\n 0x0008: 'Print',\n 0x20000: 'Read permissions',\n 0x40000: 'Change permissions',\n 0x80000: 'Take ownership',\n 'manage_printer': 0x10004,\n 'print': 0x0008,\n 'read_permissions': 0x20000,\n 'change_permissions': 0x40000,\n 'take_ownership': 0x80000,\n },\n },\n 'service': {\n 'basic': {\n 0xf01ff: 'Full Control',\n 0x2008f: 'Read & Write',\n 0x2018d: 'Read',\n 0x20002: 'Write',\n 'full_control': 0xf01ff,\n 'read_write': 0x2008f,\n 'read': 0x2018d,\n 'write': 0x20002,\n },\n 'advanced': {\n 0x0001: 'Query Config',\n 0x0002: 'Change Config',\n 0x0004: 'Query Status',\n 0x0008: 'Enumerate Dependents',\n 0x0010: 'Start',\n 0x0020: 'Stop',\n 0x0040: 'Pause/Resume',\n 0x0080: 'Interrogate',\n 0x0100: 'User-Defined Control',\n # 0x10000: 'Delete', # Not visible in the GUI\n 0x20000: 'Read Permissions',\n 0x40000: 'Change Permissions',\n 0x80000: 'Change Owner',\n 'query_config': 0x0001,\n 'change_config': 0x0002,\n 'query_status': 0x0004,\n 'enum_dependents': 0x0008,\n 'start': 0x0010,\n 'stop': 0x0020,\n 'pause_resume': 0x0040,\n 'interrogate': 0x0080,\n 'user_defined': 0x0100,\n 'read_permissions': 0x20000,\n 'change_permissions': 0x40000,\n 'change_owner': 0x80000,\n },\n }\n }\n\n # These denote inheritance\n # 0x0000 : Not inherited, I don't know the enumeration for this\n # 0x0010 : win32security.INHERITED_ACE\n\n # All the values in the dict below are combinations of the following\n # enumerations or'ed together\n # 0x0001 : win32security.OBJECT_INHERIT_ACE\n # 0x0002 : win32security.CONTAINER_INHERIT_ACE\n # 0x0004 : win32security.NO_PROPAGATE_INHERIT_ACE\n # 0x0008 : win32security.INHERIT_ONLY_ACE\n ace_prop = {\n 'file': {\n # for report\n 0x0000: 'Not Inherited (file)',\n 0x0001: 'This folder and files',\n 0x0002: 'This folder and subfolders',\n 0x0003: 'This folder, subfolders and files',\n 0x0006: 'This folder only',\n 0x0009: 'Files only',\n 0x000a: 'Subfolders only',\n 0x000b: 'Subfolders and files only',\n 0x0010: 'Inherited (file)',\n # for setting\n 'this_folder_only': 0x0006,\n 'this_folder_subfolders_files': 0x0003,\n 'this_folder_subfolders': 0x0002,\n 'this_folder_files': 0x0001,\n 'subfolders_files': 0x000b,\n 'subfolders_only': 0x000a,\n 'files_only': 0x0009,\n },\n 'registry': {\n 0x0000: 'Not Inherited',\n 0x0002: 'This key and subkeys',\n 0x0006: 'This key only',\n 0x000a: 'Subkeys only',\n 0x0010: 'Inherited',\n 'this_key_only': 0x0006,\n 'this_key_subkeys': 0x0002,\n 'subkeys_only': 0x000a,\n },\n 'registry32': {\n 0x0000: 'Not Inherited',\n 0x0002: 'This key and subkeys',\n 0x0006: 'This key only',\n 0x000a: 'Subkeys only',\n 0x0010: 'Inherited',\n 'this_key_only': 0x0006,\n 'this_key_subkeys': 0x0002,\n 'subkeys_only': 0x000a,\n },\n }\n\n ace_type = {\n 'grant': win32security.ACCESS_ALLOWED_ACE_TYPE,\n 'deny': win32security.ACCESS_DENIED_ACE_TYPE,\n win32security.ACCESS_ALLOWED_ACE_TYPE: 'grant',\n win32security.ACCESS_DENIED_ACE_TYPE: 'deny',\n }\n\n element = {\n 'dacl': win32security.DACL_SECURITY_INFORMATION,\n 'group': win32security.GROUP_SECURITY_INFORMATION,\n 'owner': win32security.OWNER_SECURITY_INFORMATION,\n }\n\n inheritance = {\n 'protected': win32security.PROTECTED_DACL_SECURITY_INFORMATION,\n 'unprotected': win32security.UNPROTECTED_DACL_SECURITY_INFORMATION,\n }\n\n obj_type = {\n 'file': win32security.SE_FILE_OBJECT,\n 'service': win32security.SE_SERVICE,\n 'printer': win32security.SE_PRINTER,\n 'registry': win32security.SE_REGISTRY_KEY,\n 'registry32': win32security.SE_REGISTRY_WOW64_32KEY,\n 'share': win32security.SE_LMSHARE,\n }\n\n return Flags() if instantiated else Flags"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a new DACL object.", "response": "def dacl(obj_name=None, obj_type='file'):\n '''\n Helper function for instantiating a Dacl class.\n\n Args:\n\n obj_name (str):\n The full path to the object. If None, a blank DACL will be created.\n Default is None.\n\n obj_type (str):\n The type of object. Default is 'File'\n\n Returns:\n object: An instantiated Dacl object\n '''\n\n if not HAS_WIN32:\n return\n\n class Dacl(flags(False)):\n '''\n DACL Object\n '''\n def __init__(self, obj_name=None, obj_type='file'):\n '''\n Either load the DACL from the passed object or create an empty DACL.\n If `obj_name` is not passed, an empty DACL is created.\n\n Args:\n\n obj_name (str):\n The full path to the object. If None, a blank DACL will be\n created\n\n obj_type (Optional[str]):\n The type of object.\n\n Returns:\n obj: A DACL object\n\n Usage:\n\n .. code-block:: python\n\n # Create an Empty DACL\n dacl = Dacl(obj_type=obj_type)\n\n # Load the DACL of the named object\n dacl = Dacl(obj_name, obj_type)\n '''\n # Validate obj_type\n if obj_type.lower() not in self.obj_type:\n raise SaltInvocationError(\n 'Invalid \"obj_type\" passed: {0}'.format(obj_type))\n\n self.dacl_type = obj_type.lower()\n\n if obj_name is None:\n self.dacl = win32security.ACL()\n else:\n if 'registry' in self.dacl_type:\n obj_name = self.get_reg_name(obj_name)\n\n try:\n sd = win32security.GetNamedSecurityInfo(\n obj_name, self.obj_type[self.dacl_type], self.element['dacl'])\n except pywintypes.error as exc:\n if 'The system cannot find' in exc.strerror:\n msg = 'System cannot find {0}'.format(obj_name)\n log.exception(msg)\n raise CommandExecutionError(msg)\n raise\n\n self.dacl = sd.GetSecurityDescriptorDacl()\n if self.dacl is None:\n self.dacl = win32security.ACL()\n\n def get_reg_name(self, obj_name):\n '''\n Take the obj_name and convert the hive to a valid registry hive.\n\n Args:\n\n obj_name (str):\n The full path to the registry key including the hive, eg:\n ``HKLM\\\\SOFTWARE\\\\salt``. Valid options for the hive are:\n\n - HKEY_LOCAL_MACHINE\n - MACHINE\n - HKLM\n - HKEY_USERS\n - USERS\n - HKU\n - HKEY_CURRENT_USER\n - CURRENT_USER\n - HKCU\n - HKEY_CLASSES_ROOT\n - CLASSES_ROOT\n - HKCR\n\n Returns:\n str:\n The full path to the registry key in the format expected by\n the Windows API\n\n Usage:\n\n .. code-block:: python\n\n import salt.utils.win_dacl\n dacl = salt.utils.win_dacl.Dacl()\n valid_key = dacl.get_reg_name('HKLM\\\\SOFTWARE\\\\salt')\n\n # Returns: MACHINE\\\\SOFTWARE\\\\salt\n '''\n # Make sure the hive is correct\n # Should be MACHINE, USERS, CURRENT_USER, or CLASSES_ROOT\n hives = {\n # MACHINE\n 'HKEY_LOCAL_MACHINE': 'MACHINE',\n 'MACHINE': 'MACHINE',\n 'HKLM': 'MACHINE',\n # USERS\n 'HKEY_USERS': 'USERS',\n 'USERS': 'USERS',\n 'HKU': 'USERS',\n # CURRENT_USER\n 'HKEY_CURRENT_USER': 'CURRENT_USER',\n 'CURRENT_USER': 'CURRENT_USER',\n 'HKCU': 'CURRENT_USER',\n # CLASSES ROOT\n 'HKEY_CLASSES_ROOT': 'CLASSES_ROOT',\n 'CLASSES_ROOT': 'CLASSES_ROOT',\n 'HKCR': 'CLASSES_ROOT',\n }\n reg = obj_name.split('\\\\')\n passed_hive = reg.pop(0)\n\n try:\n valid_hive = hives[passed_hive.upper()]\n except KeyError:\n log.exception('Invalid Registry Hive: %s', passed_hive)\n raise CommandExecutionError(\n 'Invalid Registry Hive: {0}'.format(passed_hive))\n\n reg.insert(0, valid_hive)\n\n return r'\\\\'.join(reg)\n\n def add_ace(self, principal, access_mode, permissions, applies_to):\n '''\n Add an ACE to the DACL\n\n Args:\n\n principal (str):\n The sid of the user/group to for the ACE\n\n access_mode (str):\n Determines the type of ACE to add. Must be either ``grant``\n or ``deny``.\n\n permissions (str, list):\n The type of permissions to grant/deny the user. Can be one\n of the basic permissions, or a list of advanced permissions.\n\n applies_to (str):\n The objects to which these permissions will apply. Not all\n these options apply to all object types.\n\n Returns:\n bool: True if successful, otherwise False\n\n Usage:\n\n .. code-block:: python\n\n dacl = Dacl(obj_type=obj_type)\n dacl.add_ace(sid, access_mode, permission, applies_to)\n dacl.save(obj_name, protected)\n '''\n sid = get_sid(principal)\n\n if self.dacl is None:\n raise SaltInvocationError(\n 'You must load the DACL before adding an ACE')\n\n # Get the permission flag\n perm_flag = 0\n if isinstance(permissions, six.string_types):\n try:\n perm_flag = self.ace_perms[self.dacl_type]['basic'][permissions]\n except KeyError as exc:\n msg = 'Invalid permission specified: {0}'.format(permissions)\n log.exception(msg)\n raise CommandExecutionError(msg, exc)\n else:\n try:\n for perm in permissions:\n perm_flag |= self.ace_perms[self.dacl_type]['advanced'][perm]\n except KeyError as exc:\n msg = 'Invalid permission specified: {0}'.format(perm)\n log.exception(msg)\n raise CommandExecutionError(msg, exc)\n\n if access_mode.lower() not in ['grant', 'deny']:\n raise SaltInvocationError('Invalid Access Mode: {0}'.format(access_mode))\n\n # Add ACE to the DACL\n # Grant or Deny\n try:\n if access_mode.lower() == 'grant':\n self.dacl.AddAccessAllowedAceEx(\n win32security.ACL_REVISION_DS,\n # Some types don't support propagation\n # May need to use 0x0000 instead of None\n self.ace_prop.get(self.dacl_type, {}).get(applies_to),\n perm_flag,\n sid)\n elif access_mode.lower() == 'deny':\n self.dacl.AddAccessDeniedAceEx(\n win32security.ACL_REVISION_DS,\n self.ace_prop.get(self.dacl_type, {}).get(applies_to),\n perm_flag,\n sid)\n else:\n log.exception('Invalid access mode: %s', access_mode)\n raise SaltInvocationError(\n 'Invalid access mode: {0}'.format(access_mode))\n except Exception as exc:\n return False, 'Error: {0}'.format(exc)\n\n return True\n\n def order_acl(self):\n '''\n Put the ACEs in the ACL in the proper order. This is necessary\n because the add_ace function puts ACEs at the end of the list\n without regard for order. This will cause the following Windows\n Security dialog to appear when viewing the security for the object:\n\n ``The permissions on Directory are incorrectly ordered, which may\n cause some entries to be ineffective.``\n\n .. note:: Run this function after adding all your ACEs.\n\n Proper Orders is as follows:\n\n 1. Implicit Deny\n 2. Inherited Deny\n 3. Implicit Deny Object\n 4. Inherited Deny Object\n 5. Implicit Allow\n 6. Inherited Allow\n 7. Implicit Allow Object\n 8. Inherited Allow Object\n\n Usage:\n\n .. code-block:: python\n\n dacl = Dacl(obj_type=obj_type)\n dacl.add_ace(sid, access_mode, applies_to, permission)\n dacl.order_acl()\n dacl.save(obj_name, protected)\n '''\n new_dacl = Dacl()\n deny_dacl = Dacl()\n deny_obj_dacl = Dacl()\n allow_dacl = Dacl()\n allow_obj_dacl = Dacl()\n\n # Load Non-Inherited ACEs first\n for i in range(0, self.dacl.GetAceCount()):\n ace = self.dacl.GetAce(i)\n if ace[0][1] & win32security.INHERITED_ACE == 0:\n if ace[0][0] == win32security.ACCESS_DENIED_ACE_TYPE:\n deny_dacl.dacl.AddAccessDeniedAceEx(\n win32security.ACL_REVISION_DS,\n ace[0][1],\n ace[1],\n ace[2])\n elif ace[0][0] == win32security.ACCESS_DENIED_OBJECT_ACE_TYPE:\n deny_obj_dacl.dacl.AddAccessDeniedAceEx(\n win32security.ACL_REVISION_DS,\n ace[0][1],\n ace[1],\n ace[2])\n elif ace[0][0] == win32security.ACCESS_ALLOWED_ACE_TYPE:\n allow_dacl.dacl.AddAccessAllowedAceEx(\n win32security.ACL_REVISION_DS,\n ace[0][1],\n ace[1],\n ace[2])\n elif ace[0][0] == win32security.ACCESS_ALLOWED_OBJECT_ACE_TYPE:\n allow_obj_dacl.dacl.AddAccessAllowedAceEx(\n win32security.ACL_REVISION_DS,\n ace[0][1],\n ace[1],\n ace[2])\n\n # Load Inherited ACEs last\n for i in range(0, self.dacl.GetAceCount()):\n ace = self.dacl.GetAce(i)\n if ace[0][1] & win32security.INHERITED_ACE == \\\n win32security.INHERITED_ACE:\n ace_prop = ace[0][1] ^ win32security.INHERITED_ACE\n if ace[0][0] == win32security.ACCESS_DENIED_ACE_TYPE:\n deny_dacl.dacl.AddAccessDeniedAceEx(\n win32security.ACL_REVISION_DS,\n ace_prop,\n ace[1],\n ace[2])\n elif ace[0][0] == win32security.ACCESS_DENIED_OBJECT_ACE_TYPE:\n deny_obj_dacl.dacl.AddAccessDeniedAceEx(\n win32security.ACL_REVISION_DS,\n ace_prop,\n ace[1],\n ace[2])\n elif ace[0][0] == win32security.ACCESS_ALLOWED_ACE_TYPE:\n allow_dacl.dacl.AddAccessAllowedAceEx(\n win32security.ACL_REVISION_DS,\n ace_prop,\n ace[1],\n ace[2])\n elif ace[0][0] == win32security.ACCESS_ALLOWED_OBJECT_ACE_TYPE:\n allow_obj_dacl.dacl.AddAccessAllowedAceEx(\n win32security.ACL_REVISION_DS,\n ace_prop,\n ace[1],\n ace[2])\n\n # Combine ACEs in the proper order\n # Deny, Deny Object, Allow, Allow Object\n # Deny\n for i in range(0, deny_dacl.dacl.GetAceCount()):\n ace = deny_dacl.dacl.GetAce(i)\n new_dacl.dacl.AddAccessDeniedAceEx(\n win32security.ACL_REVISION_DS,\n ace[0][1],\n ace[1],\n ace[2])\n\n # Deny Object\n for i in range(0, deny_obj_dacl.dacl.GetAceCount()):\n ace = deny_obj_dacl.dacl.GetAce(i)\n new_dacl.dacl.AddAccessDeniedAceEx(\n win32security.ACL_REVISION_DS,\n ace[0][1] ^ win32security.INHERITED_ACE,\n ace[1],\n ace[2])\n\n # Allow\n for i in range(0, allow_dacl.dacl.GetAceCount()):\n ace = allow_dacl.dacl.GetAce(i)\n new_dacl.dacl.AddAccessAllowedAceEx(\n win32security.ACL_REVISION_DS,\n ace[0][1],\n ace[1],\n ace[2])\n\n # Allow Object\n for i in range(0, allow_obj_dacl.dacl.GetAceCount()):\n ace = allow_obj_dacl.dacl.GetAce(i)\n new_dacl.dacl.AddAccessAllowedAceEx(\n win32security.ACL_REVISION_DS,\n ace[0][1] ^ win32security.INHERITED_ACE,\n ace[1],\n ace[2])\n\n # Set the new dacl\n self.dacl = new_dacl.dacl\n\n def get_ace(self, principal):\n '''\n Get the ACE for a specific principal.\n\n Args:\n\n principal (str):\n The name of the user or group for which to get the ace. Can\n also be a SID.\n\n Returns:\n dict: A dictionary containing the ACEs found for the principal\n\n Usage:\n\n .. code-block:: python\n\n dacl = Dacl(obj_type=obj_type)\n dacl.get_ace()\n '''\n principal = get_name(principal)\n aces = self.list_aces()\n\n # Filter for the principal\n ret = {}\n for inheritance in aces:\n if principal in aces[inheritance]:\n ret[inheritance] = {principal: aces[inheritance][principal]}\n\n return ret\n\n def list_aces(self):\n '''\n List all Entries in the dacl.\n\n Returns:\n dict: A dictionary containing the ACEs for the object\n\n Usage:\n\n .. code-block:: python\n\n dacl = Dacl('C:\\\\Temp')\n dacl.list_aces()\n '''\n ret = {'Inherited': {},\n 'Not Inherited': {}}\n\n # loop through each ACE in the DACL\n for i in range(0, self.dacl.GetAceCount()):\n ace = self.dacl.GetAce(i)\n\n # Get ACE Elements\n user, a_type, a_prop, a_perms, inheritance = self._ace_to_dict(ace)\n\n if user in ret[inheritance]:\n ret[inheritance][user][a_type] = {\n 'applies to': a_prop,\n 'permissions': a_perms,\n }\n else:\n ret[inheritance][user] = {\n a_type: {\n 'applies to': a_prop,\n 'permissions': a_perms,\n }}\n\n return ret\n\n def _ace_to_dict(self, ace):\n '''\n Helper function for creating the ACE return dictionary\n '''\n # Get the principal from the sid (object sid)\n sid = win32security.ConvertSidToStringSid(ace[2])\n try:\n principal = get_name(sid)\n except CommandExecutionError:\n principal = sid\n\n # Get the ace type\n ace_type = self.ace_type[ace[0][0]]\n\n # Is the inherited ace flag present\n inherited = ace[0][1] & win32security.INHERITED_ACE == 16\n\n # Ace Propagation\n ace_prop = 'NA'\n\n # Get the ace propagation properties\n if self.dacl_type in ['file', 'registry', 'registry32']:\n\n ace_prop = ace[0][1]\n\n # Remove the inherited ace flag and get propagation\n if inherited:\n ace_prop = ace[0][1] ^ win32security.INHERITED_ACE\n\n # Lookup the propagation\n try:\n ace_prop = self.ace_prop[self.dacl_type][ace_prop]\n except KeyError:\n ace_prop = 'Unknown propagation'\n\n # Get the object type\n obj_type = 'registry' if self.dacl_type == 'registry32' \\\n else self.dacl_type\n\n # Get the ace permissions\n # Check basic permissions first\n ace_perms = self.ace_perms[obj_type]['basic'].get(ace[1], [])\n\n # If it didn't find basic perms, check advanced permissions\n if not ace_perms:\n ace_perms = []\n for perm in self.ace_perms[obj_type]['advanced']:\n # Don't match against the string perms\n if isinstance(perm, six.string_types):\n continue\n if ace[1] & perm == perm:\n ace_perms.append(\n self.ace_perms[obj_type]['advanced'][perm])\n\n # If still nothing, it must be undefined\n if not ace_perms:\n ace_perms = ['Undefined Permission: {0}'.format(ace[1])]\n\n return principal, ace_type, ace_prop, ace_perms, \\\n 'Inherited' if inherited else 'Not Inherited'\n\n def rm_ace(self, principal, ace_type='all'):\n '''\n Remove a specific ACE from the DACL.\n\n Args:\n\n principal (str):\n The user whose ACE to remove. Can be the user name or a SID.\n\n ace_type (str):\n The type of ACE to remove. If not specified, all ACEs will\n be removed. Default is 'all'. Valid options are:\n\n - 'grant'\n - 'deny'\n - 'all'\n\n Returns:\n list: List of removed aces\n\n Usage:\n\n .. code-block:: python\n\n dacl = Dacl(obj_name='C:\\\\temp', obj_type='file')\n dacl.rm_ace('Users')\n dacl.save(obj_name='C:\\\\temp')\n '''\n sid = get_sid(principal)\n offset = 0\n ret = []\n\n for i in range(0, self.dacl.GetAceCount()):\n ace = self.dacl.GetAce(i - offset)\n\n # Is the inherited ace flag present\n inherited = ace[0][1] & win32security.INHERITED_ACE == 16\n\n if ace[2] == sid and not inherited:\n if self.ace_type[ace[0][0]] == ace_type.lower() or \\\n ace_type == 'all':\n self.dacl.DeleteAce(i - offset)\n ret.append(self._ace_to_dict(ace))\n offset += 1\n\n if not ret:\n ret = ['ACE not found for {0}'.format(principal)]\n\n return ret\n\n def save(self, obj_name, protected=None):\n '''\n Save the DACL\n\n Args:\n\n obj_name (str):\n The object for which to set permissions. This can be the\n path to a file or folder, a registry key, printer, etc. For\n more information about how to format the name see:\n\n https://msdn.microsoft.com/en-us/library/windows/desktop/aa379593(v=vs.85).aspx\n\n protected (Optional[bool]):\n True will disable inheritance for the object. False will\n enable inheritance. None will make no change. Default is\n ``None``.\n\n Returns:\n bool: True if successful, Otherwise raises an exception\n\n Usage:\n\n .. code-block:: python\n\n dacl = Dacl(obj_type='file')\n dacl.save('C:\\\\Temp', True)\n '''\n sec_info = self.element['dacl']\n\n if protected is not None:\n if protected:\n sec_info = sec_info | self.inheritance['protected']\n else:\n sec_info = sec_info | self.inheritance['unprotected']\n\n if self.dacl_type in ['registry', 'registry32']:\n obj_name = self.get_reg_name(obj_name)\n\n try:\n win32security.SetNamedSecurityInfo(\n obj_name,\n self.obj_type[self.dacl_type],\n sec_info,\n None, None, self.dacl, None)\n except pywintypes.error as exc:\n raise CommandExecutionError(\n 'Failed to set permissions: {0}'.format(obj_name),\n exc.strerror)\n\n return True\n\n return Dacl(obj_name, obj_type)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a sid or a sid from a user s name or a sid.", "response": "def get_sid(principal):\n '''\n Converts a username to a sid, or verifies a sid. Required for working with\n the DACL.\n\n Args:\n\n principal(str):\n The principal to lookup the sid. Can be a sid or a username.\n\n Returns:\n PySID Object: A sid\n\n Usage:\n\n .. code-block:: python\n\n # Get a user's sid\n salt.utils.win_dacl.get_sid('jsnuffy')\n\n # Verify that the sid is valid\n salt.utils.win_dacl.get_sid('S-1-5-32-544')\n '''\n # If None is passed, use the Universal Well-known SID \"Null SID\"\n if principal is None:\n principal = 'NULL SID'\n\n # Test if the user passed a sid or a name\n try:\n sid = salt.utils.win_functions.get_sid_from_name(principal)\n except CommandExecutionError:\n sid = principal\n\n # Test if the SID is valid\n try:\n sid = win32security.ConvertStringSidToSid(sid)\n except pywintypes.error:\n log.exception('Invalid user/group or sid: %s', principal)\n raise CommandExecutionError(\n 'Invalid user/group or sid: {0}'.format(principal))\n except TypeError:\n raise CommandExecutionError\n\n return sid"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconverts a PySID object to a string SID.", "response": "def get_sid_string(principal):\n '''\n Converts a PySID object to a string SID.\n\n Args:\n\n principal(str):\n The principal to lookup the sid. Must be a PySID object.\n\n Returns:\n str: A string sid\n\n Usage:\n\n .. code-block:: python\n\n # Get a PySID object\n py_sid = salt.utils.win_dacl.get_sid('jsnuffy')\n\n # Get the string version of the SID\n salt.utils.win_dacl.get_sid_string(py_sid)\n '''\n # If None is passed, use the Universal Well-known SID \"Null SID\"\n if principal is None:\n principal = 'NULL SID'\n\n try:\n return win32security.ConvertSidToStringSid(principal)\n except TypeError:\n # Not a PySID object\n principal = get_sid(principal)\n\n try:\n return win32security.ConvertSidToStringSid(principal)\n except pywintypes.error:\n log.exception('Invalid principal %s', principal)\n raise CommandExecutionError('Invalid principal {0}'.format(principal))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget the name from the specified principal.", "response": "def get_name(principal):\n '''\n Gets the name from the specified principal.\n\n Args:\n\n principal (str):\n Find the Normalized name based on this. Can be a PySID object, a SID\n string, or a user name in any capitalization.\n\n .. note::\n Searching based on the user name can be slow on hosts connected\n to large Active Directory domains.\n\n Returns:\n str: The name that corresponds to the passed principal\n\n Usage:\n\n .. code-block:: python\n\n salt.utils.win_dacl.get_name('S-1-5-32-544')\n salt.utils.win_dacl.get_name('adminisTrators')\n '''\n # If this is a PySID object, use it\n if isinstance(principal, pywintypes.SIDType):\n sid_obj = principal\n else:\n # If None is passed, use the Universal Well-known SID for \"Null SID\"\n if principal is None:\n principal = 'S-1-0-0'\n # Try Converting String SID to SID Object first as it's least expensive\n try:\n sid_obj = win32security.ConvertStringSidToSid(principal)\n except pywintypes.error:\n # Try Getting the SID Object by Name Lookup last\n # This is expensive, especially on large AD Domains\n try:\n sid_obj = win32security.LookupAccountName(None, principal)[0]\n except pywintypes.error:\n # This is not a PySID object, a SID String, or a valid Account\n # Name. Just pass it and let the LookupAccountSid function try\n # to resolve it\n sid_obj = principal\n\n # By now we should have a valid PySID object\n try:\n return win32security.LookupAccountSid(None, sid_obj)[0]\n except (pywintypes.error, TypeError) as exc:\n message = 'Error resolving \"{0}\"'.format(principal)\n if type(exc) == pywintypes.error:\n win_error = win32api.FormatMessage(exc.winerror).rstrip('\\n')\n message = '{0}: {1}'.format(message, win_error)\n log.exception(message)\n raise CommandExecutionError(message, exc)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_primary_group(obj_name, obj_type='file'):\n r'''\n Gets the primary group of the passed object\n\n Args:\n\n obj_name (str):\n The path for which to obtain primary group information\n\n obj_type (str):\n The type of object to query. This value changes the format of the\n ``obj_name`` parameter as follows:\n\n - file: indicates a file or directory\n - a relative path, such as ``FileName.txt`` or ``..\\FileName``\n - an absolute path, such as ``C:\\DirName\\FileName.txt``\n - A UNC name, such as ``\\\\ServerName\\ShareName\\FileName.txt``\n - service: indicates the name of a Windows service\n - printer: indicates the name of a printer\n - registry: indicates a registry key\n - Uses the following literal strings to denote the hive:\n - HKEY_LOCAL_MACHINE\n - MACHINE\n - HKLM\n - HKEY_USERS\n - USERS\n - HKU\n - HKEY_CURRENT_USER\n - CURRENT_USER\n - HKCU\n - HKEY_CLASSES_ROOT\n - CLASSES_ROOT\n - HKCR\n - Should be in the format of ``HIVE\\Path\\To\\Key``. For example,\n ``HKLM\\SOFTWARE\\Windows``\n - registry32: indicates a registry key under WOW64. Formatting is\n the same as it is for ``registry``\n - share: indicates a network share\n\n Returns:\n str: The primary group for the object\n\n Usage:\n\n .. code-block:: python\n\n salt.utils.win_dacl.get_primary_group('c:\\\\file')\n '''\n # Not all filesystems mountable within windows have SecurityDescriptors.\n # For instance, some mounted SAMBA shares, or VirtualBox shared folders. If\n # we can't load a file descriptor for the file, we default to \"Everyone\"\n # http://support.microsoft.com/kb/243330\n\n # Validate obj_type\n try:\n obj_type_flag = flags().obj_type[obj_type.lower()]\n except KeyError:\n raise SaltInvocationError(\n 'Invalid \"obj_type\" passed: {0}'.format(obj_type))\n\n if 'registry' in obj_type.lower():\n obj_name = dacl().get_reg_name(obj_name)\n log.debug('Name converted to: %s', obj_name)\n\n try:\n security_descriptor = win32security.GetNamedSecurityInfo(\n obj_name, obj_type_flag, win32security.GROUP_SECURITY_INFORMATION)\n primary_group_gid = security_descriptor.GetSecurityDescriptorGroup()\n\n except MemoryError:\n # Generic Memory Error (Windows Server 2003+)\n primary_group_gid = 'S-1-0-0'\n\n except pywintypes.error as exc:\n # Incorrect function error (Windows Server 2008+)\n if exc.winerror == 1 or exc.winerror == 50:\n primary_group_gid = 'S-1-0-0'\n else:\n log.exception('Failed to get the primary group: %s', obj_name)\n raise CommandExecutionError(\n 'Failed to get primary group: {0}'.format(obj_name),\n exc.strerror)\n\n return get_name(win32security.ConvertSidToStringSid(primary_group_gid))", "response": "r Returns the primary group of the passed object."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_owner(obj_name, principal, obj_type='file'):\n '''\n Set the owner of an object. This can be a file, folder, registry key,\n printer, service, etc...\n\n Args:\n\n obj_name (str):\n The object for which to set owner. This can be the path to a file or\n folder, a registry key, printer, etc. For more information about how\n to format the name see:\n\n https://msdn.microsoft.com/en-us/library/windows/desktop/aa379593(v=vs.85).aspx\n\n principal (str):\n The name of the user or group to make owner of the object. Can also\n pass a SID.\n\n obj_type (Optional[str]):\n The type of object for which to set the owner. Default is ``file``\n\n Returns:\n bool: True if successful, raises an error otherwise\n\n Usage:\n\n .. code-block:: python\n\n salt.utils.win_dacl.set_owner('C:\\\\MyDirectory', 'jsnuffy', 'file')\n '''\n sid = get_sid(principal)\n\n obj_flags = flags()\n\n # Validate obj_type\n if obj_type.lower() not in obj_flags.obj_type:\n raise SaltInvocationError(\n 'Invalid \"obj_type\" passed: {0}'.format(obj_type))\n\n if 'registry' in obj_type.lower():\n obj_name = dacl().get_reg_name(obj_name)\n\n # To set the owner to something other than the logged in user requires\n # SE_TAKE_OWNERSHIP_NAME and SE_RESTORE_NAME privileges\n # Enable them for the logged in user\n # Setup the privilege set\n new_privs = set()\n luid = win32security.LookupPrivilegeValue('', 'SeTakeOwnershipPrivilege')\n new_privs.add((luid, win32con.SE_PRIVILEGE_ENABLED))\n luid = win32security.LookupPrivilegeValue('', 'SeRestorePrivilege')\n new_privs.add((luid, win32con.SE_PRIVILEGE_ENABLED))\n\n # Get the current token\n p_handle = win32api.GetCurrentProcess()\n t_handle = win32security.OpenProcessToken(\n p_handle,\n win32security.TOKEN_ALL_ACCESS | win32con.TOKEN_ADJUST_PRIVILEGES)\n\n # Enable the privileges\n win32security.AdjustTokenPrivileges(t_handle, 0, new_privs)\n\n # Set the user\n try:\n win32security.SetNamedSecurityInfo(\n obj_name,\n obj_flags.obj_type[obj_type.lower()],\n obj_flags.element['owner'],\n sid,\n None, None, None)\n except pywintypes.error as exc:\n log.exception('Failed to make %s the owner: %s', principal, exc)\n raise CommandExecutionError(\n 'Failed to set owner: {0}'.format(obj_name), exc.strerror)\n\n return True", "response": "Set the owner of an object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_permissions(obj_name,\n principal,\n permissions,\n access_mode='grant',\n applies_to=None,\n obj_type='file',\n reset_perms=False,\n protected=None):\n '''\n Set the permissions of an object. This can be a file, folder, registry key,\n printer, service, etc...\n\n Args:\n\n obj_name (str):\n The object for which to set permissions. This can be the path to a\n file or folder, a registry key, printer, etc. For more information\n about how to format the name see:\n\n https://msdn.microsoft.com/en-us/library/windows/desktop/aa379593(v=vs.85).aspx\n\n principal (str):\n The name of the user or group for which to set permissions. Can also\n pass a SID.\n\n permissions (str, list):\n The type of permissions to grant/deny the user. Can be one of the\n basic permissions, or a list of advanced permissions.\n\n access_mode (Optional[str]):\n Whether to grant or deny user the access. Valid options are:\n\n - grant (default): Grants the user access\n - deny: Denies the user access\n\n applies_to (Optional[str]):\n The objects to which these permissions will apply. Not all these\n options apply to all object types. Defaults to\n 'this_folder_subfolders_files'\n\n obj_type (Optional[str]):\n The type of object for which to set permissions. Default is 'file'\n\n reset_perms (Optional[bool]):\n True will overwrite the permissions on the specified object. False\n will append the permissions. Default is False\n\n protected (Optional[bool]):\n True will disable inheritance for the object. False will enable\n inheritance. None will make no change. Default is None.\n\n Returns:\n bool: True if successful, raises an error otherwise\n\n Usage:\n\n .. code-block:: python\n\n salt.utils.win_dacl.set_permissions(\n 'C:\\\\Temp', 'jsnuffy', 'full_control', 'grant')\n '''\n # Set up applies_to defaults used by registry and file types\n if applies_to is None:\n if 'registry' in obj_type.lower():\n applies_to = 'this_key_subkeys'\n elif obj_type.lower() == 'file':\n applies_to = 'this_folder_subfolders_files'\n\n # If you don't pass `obj_name` it will create a blank DACL\n # Otherwise, it will grab the existing DACL and add to it\n if reset_perms:\n obj_dacl = dacl(obj_type=obj_type)\n else:\n obj_dacl = dacl(obj_name, obj_type)\n obj_dacl.rm_ace(principal, access_mode)\n\n obj_dacl.add_ace(principal, access_mode, permissions, applies_to)\n\n obj_dacl.order_acl()\n\n obj_dacl.save(obj_name, protected)\n\n return True", "response": "Set the permissions of an object in the specified order."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_permissions(obj_name, principal=None, obj_type='file'):\n '''\n Get the permissions for the passed object\n\n Args:\n\n obj_name (str):\n The name of or path to the object.\n\n principal (Optional[str]):\n The name of the user or group for which to get permissions. Can also\n pass a SID. If None, all ACEs defined on the object will be\n returned. Default is None\n\n obj_type (Optional[str]):\n The type of object for which to get permissions.\n\n Returns:\n dict: A dictionary representing the object permissions\n\n Usage:\n\n .. code-block:: python\n\n salt.utils.win_dacl.get_permissions('C:\\\\Temp')\n '''\n obj_dacl = dacl(obj_name, obj_type)\n\n if principal is None:\n return obj_dacl.list_aces()\n\n return obj_dacl.get_ace(principal)", "response": "Get the permissions for the passed object"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef has_permission(obj_name,\n principal,\n permission,\n access_mode='grant',\n obj_type='file',\n exact=True):\n r'''\n Check if the object has a permission\n\n Args:\n\n obj_name (str):\n The name of or path to the object.\n\n principal (str):\n The name of the user or group for which to get permissions. Can also\n pass a SID.\n\n permission (str):\n The permission to verify. Valid options depend on the obj_type.\n\n access_mode (Optional[str]):\n The access mode to check. Is the user granted or denied the\n permission. Default is 'grant'. Valid options are:\n\n - grant\n - deny\n\n obj_type (Optional[str]):\n The type of object for which to check permissions. Default is 'file'\n\n exact (Optional[bool]):\n True for an exact match, otherwise check to see if the permission is\n included in the ACE. Default is True\n\n Returns:\n bool: True if the object has the permission, otherwise False\n\n Usage:\n\n .. code-block:: python\n\n # Does Joe have read permissions to C:\\Temp\n salt.utils.win_dacl.has_permission(\n 'C:\\\\Temp', 'joe', 'read', 'grant', False)\n\n # Does Joe have Full Control of C:\\Temp\n salt.utils.win_dacl.has_permission(\n 'C:\\\\Temp', 'joe', 'full_control', 'grant')\n '''\n # Validate access_mode\n if access_mode.lower() not in ['grant', 'deny']:\n raise SaltInvocationError(\n 'Invalid \"access_mode\" passed: {0}'.format(access_mode))\n access_mode = access_mode.lower()\n\n # Get the DACL\n obj_dacl = dacl(obj_name, obj_type)\n\n obj_type = obj_type.lower()\n\n # Get a PySID object\n sid = get_sid(principal)\n\n # Get the passed permission flag, check basic first\n chk_flag = obj_dacl.ace_perms[obj_type]['basic'].get(\n permission.lower(),\n obj_dacl.ace_perms[obj_type]['advanced'].get(permission.lower(), False))\n if not chk_flag:\n raise SaltInvocationError(\n 'Invalid \"permission\" passed: {0}'.format(permission))\n\n # Check each ace for sid and type\n cur_flag = None\n for i in range(0, obj_dacl.dacl.GetAceCount()):\n ace = obj_dacl.dacl.GetAce(i)\n if ace[2] == sid and obj_dacl.ace_type[ace[0][0]] == access_mode:\n cur_flag = ace[1]\n\n # If the ace is empty, return false\n if not cur_flag:\n return False\n\n # Check if the ACE contains the exact flag\n if exact:\n return cur_flag == chk_flag\n\n # Check if the ACE contains the permission\n return cur_flag & chk_flag == chk_flag", "response": "r Check if the object has the permission"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_inheritance(obj_name, enabled, obj_type='file', clear=False):\n '''\n Enable or disable an objects inheritance.\n\n Args:\n\n obj_name (str):\n The name of the object\n\n enabled (bool):\n True to enable inheritance, False to disable\n\n obj_type (Optional[str]):\n The type of object. Only three objects allow inheritance. Valid\n objects are:\n\n - file (default): This is a file or directory\n - registry\n - registry32 (for WOW64)\n\n clear (Optional[bool]):\n True to clear existing ACEs, False to keep existing ACEs.\n Default is False\n\n Returns:\n bool: True if successful, otherwise an Error\n\n Usage:\n\n .. code-block:: python\n\n salt.utils.win_dacl.set_inheritance('C:\\\\Temp', False)\n '''\n if obj_type not in ['file', 'registry', 'registry32']:\n raise SaltInvocationError(\n 'obj_type called with incorrect parameter: {0}'.format(obj_name))\n\n if clear:\n obj_dacl = dacl(obj_type=obj_type)\n else:\n obj_dacl = dacl(obj_name, obj_type)\n\n return obj_dacl.save(obj_name, not enabled)", "response": "Enable or disable inheritance of an object in the ACEs."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_inheritance(obj_name, obj_type='file'):\n '''\n Get an object's inheritance.\n\n Args:\n\n obj_name (str):\n The name of the object\n\n obj_type (Optional[str]):\n The type of object. Only three object types allow inheritance. Valid\n objects are:\n\n - file (default): This is a file or directory\n - registry\n - registry32 (for WOW64)\n\n The following should return False as there is no inheritance:\n\n - service\n - printer\n - share\n\n Returns:\n bool: True if enabled, otherwise False\n\n Usage:\n\n .. code-block:: python\n\n salt.utils.win_dacl.get_inheritance('HKLM\\\\SOFTWARE\\\\salt', 'registry')\n '''\n obj_dacl = dacl(obj_name=obj_name, obj_type=obj_type)\n inherited = win32security.INHERITED_ACE\n\n for i in range(0, obj_dacl.dacl.GetAceCount()):\n ace = obj_dacl.dacl.GetAce(i)\n if ace[0][1] & inherited == inherited:\n return True\n\n return False", "response": "Get an object s inheritance."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef copy_security(source,\n target,\n obj_type='file',\n copy_owner=True,\n copy_group=True,\n copy_dacl=True,\n copy_sacl=True):\n r'''\n Copy the security descriptor of the Source to the Target. You can specify a\n specific portion of the security descriptor to copy using one of the\n `copy_*` parameters.\n\n .. note::\n At least one `copy_*` parameter must be ``True``\n\n .. note::\n The user account running this command must have the following\n privileges:\n\n - SeTakeOwnershipPrivilege\n - SeRestorePrivilege\n - SeSecurityPrivilege\n\n Args:\n\n source (str):\n The full path to the source. This is where the security info will be\n copied from\n\n target (str):\n The full path to the target. This is where the security info will be\n applied\n\n obj_type (str): file\n The type of object to query. This value changes the format of the\n ``obj_name`` parameter as follows:\n - file: indicates a file or directory\n - a relative path, such as ``FileName.txt`` or ``..\\FileName``\n - an absolute path, such as ``C:\\DirName\\FileName.txt``\n - A UNC name, such as ``\\\\ServerName\\ShareName\\FileName.txt``\n - service: indicates the name of a Windows service\n - printer: indicates the name of a printer\n - registry: indicates a registry key\n - Uses the following literal strings to denote the hive:\n - HKEY_LOCAL_MACHINE\n - MACHINE\n - HKLM\n - HKEY_USERS\n - USERS\n - HKU\n - HKEY_CURRENT_USER\n - CURRENT_USER\n - HKCU\n - HKEY_CLASSES_ROOT\n - CLASSES_ROOT\n - HKCR\n - Should be in the format of ``HIVE\\Path\\To\\Key``. For example,\n ``HKLM\\SOFTWARE\\Windows``\n - registry32: indicates a registry key under WOW64. Formatting is\n the same as it is for ``registry``\n - share: indicates a network share\n\n copy_owner (bool): True\n ``True`` copies owner information. Default is ``True``\n\n copy_group (bool): True\n ``True`` copies group information. Default is ``True``\n\n copy_dacl (bool): True\n ``True`` copies the DACL. Default is ``True``\n\n copy_sacl (bool): True\n ``True`` copies the SACL. Default is ``True``\n\n Returns:\n bool: ``True`` if successful\n\n Raises:\n SaltInvocationError: When parameters are invalid\n CommandExecutionError: On failure to set security\n\n Usage:\n\n .. code-block:: python\n\n salt.utils.win_dacl.copy_security(\n source='C:\\\\temp\\\\source_file.txt',\n target='C:\\\\temp\\\\target_file.txt',\n obj_type='file')\n\n salt.utils.win_dacl.copy_security(\n source='HKLM\\\\SOFTWARE\\\\salt\\\\test_source',\n target='HKLM\\\\SOFTWARE\\\\salt\\\\test_target',\n obj_type='registry',\n copy_owner=False)\n '''\n obj_dacl = dacl(obj_type=obj_type)\n if 'registry' in obj_type.lower():\n source = obj_dacl.get_reg_name(source)\n log.info('Source converted to: %s', source)\n target = obj_dacl.get_reg_name(target)\n log.info('Target converted to: %s', target)\n\n # Set flags\n try:\n obj_type_flag = flags().obj_type[obj_type.lower()]\n except KeyError:\n raise SaltInvocationError(\n 'Invalid \"obj_type\" passed: {0}'.format(obj_type))\n\n security_flags = 0\n if copy_owner:\n security_flags |= win32security.OWNER_SECURITY_INFORMATION\n if copy_group:\n security_flags |= win32security.GROUP_SECURITY_INFORMATION\n if copy_dacl:\n security_flags |= win32security.DACL_SECURITY_INFORMATION\n if copy_sacl:\n security_flags |= win32security.SACL_SECURITY_INFORMATION\n\n if not security_flags:\n raise SaltInvocationError(\n 'One of copy_owner, copy_group, copy_dacl, or copy_sacl must be '\n 'True')\n\n # To set the owner to something other than the logged in user requires\n # SE_TAKE_OWNERSHIP_NAME and SE_RESTORE_NAME privileges\n # Enable them for the logged in user\n # Setup the privilege set\n new_privs = set()\n luid = win32security.LookupPrivilegeValue('', 'SeTakeOwnershipPrivilege')\n new_privs.add((luid, win32con.SE_PRIVILEGE_ENABLED))\n luid = win32security.LookupPrivilegeValue('', 'SeRestorePrivilege')\n new_privs.add((luid, win32con.SE_PRIVILEGE_ENABLED))\n luid = win32security.LookupPrivilegeValue('', 'SeSecurityPrivilege')\n new_privs.add((luid, win32con.SE_PRIVILEGE_ENABLED))\n\n # Get the current token\n p_handle = win32api.GetCurrentProcess()\n t_handle = win32security.OpenProcessToken(\n p_handle,\n win32security.TOKEN_ALL_ACCESS | win32con.TOKEN_ADJUST_PRIVILEGES)\n\n # Enable the privileges\n win32security.AdjustTokenPrivileges(t_handle, 0, new_privs)\n\n # Load object Security Info from the Source\n sec = win32security.GetNamedSecurityInfo(\n source, obj_type_flag, security_flags)\n\n # The following return None if the corresponding flag is not set\n sd_sid = sec.GetSecurityDescriptorOwner()\n sd_gid = sec.GetSecurityDescriptorGroup()\n sd_dacl = sec.GetSecurityDescriptorDacl()\n sd_sacl = sec.GetSecurityDescriptorSacl()\n\n # Set Security info on the target\n try:\n win32security.SetNamedSecurityInfo(\n target, obj_type_flag, security_flags, sd_sid, sd_gid, sd_dacl,\n sd_sacl)\n except pywintypes.error as exc:\n raise CommandExecutionError(\n 'Failed to set security info: {0}'.format(exc.strerror))\n\n return True", "response": "r Copy the security info from one source to another."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _check_perms(obj_name, obj_type, new_perms, cur_perms, access_mode, ret):\n '''\n Helper function used by ``check_perms`` for checking and setting Grant and\n Deny permissions.\n\n Args:\n\n obj_name (str):\n The name or full path to the object\n\n obj_type (Optional[str]):\n The type of object for which to check permissions. Default is 'file'\n\n new_perms (dict):\n A dictionary containing the user/group and the basic permissions to\n check/grant, ie: ``{'user': {'perms': 'basic_permission'}}``.\n\n cur_perms (dict):\n A dictionary containing the user/group permissions as they currently\n exists on the target object.\n\n access_mode (str):\n The access mode to set. Either ``grant`` or ``deny``\n\n ret (dict):\n A dictionary to append changes to and return. If not passed, will\n create a new dictionary to return.\n\n Returns:\n dict: A dictionary of return data as expected by the state system\n '''\n access_mode = access_mode.lower()\n changes = {}\n for user in new_perms:\n applies_to_text = ''\n # Check that user exists:\n try:\n user_name = get_name(principal=user)\n except CommandExecutionError:\n ret['comment'].append(\n '{0} Perms: User \"{1}\" missing from Target System'\n ''.format(access_mode.capitalize(), user))\n continue\n\n # Get the proper applies_to text\n if 'applies_to' in new_perms[user]:\n applies_to = new_perms[user]['applies_to']\n at_flag = flags().ace_prop['file'][applies_to]\n applies_to_text = flags().ace_prop['file'][at_flag]\n\n else:\n applies_to = None\n\n if user_name not in cur_perms['Not Inherited']:\n if user not in changes:\n changes[user] = {}\n changes[user][access_mode] = new_perms[user]['perms']\n if applies_to:\n changes[user]['applies_to'] = applies_to\n else:\n # Check Perms for basic perms\n if isinstance(new_perms[user]['perms'], six.string_types):\n if not has_permission(obj_name=obj_name,\n principal=user_name,\n permission=new_perms[user]['perms'],\n access_mode=access_mode,\n obj_type=obj_type,\n exact=False):\n\n if user not in changes:\n changes[user] = {}\n changes[user][access_mode] = new_perms[user]['perms']\n # Check Perms for advanced perms\n else:\n for perm in new_perms[user]['perms']:\n if not has_permission(obj_name=obj_name,\n principal=user_name,\n permission=perm,\n access_mode=access_mode,\n obj_type=obj_type,\n exact=False):\n if user not in changes:\n changes[user] = {access_mode: []}\n changes[user][access_mode].append(perm)\n\n # Check if applies_to was passed\n if applies_to:\n # Is there a deny/grant permission set\n if access_mode in cur_perms['Not Inherited'][user_name]:\n # If the applies to settings are different, use the new one\n if not cur_perms['Not Inherited'][user_name][access_mode]['applies to'] == applies_to_text:\n if user not in changes:\n changes[user] = {}\n changes[user]['applies_to'] = applies_to\n\n if changes:\n if 'perms' not in ret['changes']:\n ret['changes']['perms'] = {}\n for user in changes:\n user_name = get_name(principal=user)\n\n if __opts__['test'] is True:\n if user not in ret['changes']['perms']:\n ret['changes']['perms'][user] = {}\n ret['changes']['perms'][user][access_mode] = changes[user][access_mode]\n else:\n # Get applies_to\n applies_to = None\n if 'applies_to' not in changes[user]:\n # Get current \"applies to\" settings from the file\n if user_name in cur_perms['Not Inherited'] and \\\n access_mode in cur_perms['Not Inherited'][user_name]:\n for flag in flags().ace_prop[obj_type]:\n if flags().ace_prop[obj_type][flag] == cur_perms['Not Inherited'][user_name][access_mode]['applies to']:\n at_flag = flag\n for flag1 in flags().ace_prop[obj_type]:\n if salt.utils.win_dacl.flags().ace_prop[obj_type][flag1] == at_flag:\n applies_to = flag1\n if not applies_to:\n if obj_type.lower() in ['registry', 'registry32']:\n applies_to = 'this_key_subkeys'\n else:\n applies_to = 'this_folder_subfolders_files'\n else:\n applies_to = changes[user]['applies_to']\n\n perms = []\n if access_mode not in changes[user]:\n # Get current perms\n # Check for basic perms\n for perm in cur_perms['Not Inherited'][user_name][access_mode]['permissions']:\n for flag in flags().ace_perms[obj_type]['basic']:\n if flags().ace_perms[obj_type]['basic'][flag] == perm:\n perm_flag = flag\n for flag1 in flags().ace_perms[obj_type]['basic']:\n if flags().ace_perms[obj_type]['basic'][flag1] == perm_flag:\n perms = flag1\n # Make a list of advanced perms\n if not perms:\n for perm in cur_perms['Not Inherited'][user_name][access_mode]['permissions']:\n for flag in flags().ace_perms[obj_type]['advanced']:\n if flags().ace_perms[obj_type]['advanced'][flag] == perm:\n perm_flag = flag\n for flag1 in flags().ace_perms[obj_type]['advanced']:\n if flags().ace_perms[obj_type]['advanced'][flag1] == perm_flag:\n perms.append(flag1)\n else:\n perms = changes[user][access_mode]\n\n try:\n set_permissions(\n obj_name=obj_name,\n principal=user_name,\n permissions=perms,\n access_mode=access_mode,\n applies_to=applies_to,\n obj_type=obj_type)\n if user not in ret['changes']['perms']:\n ret['changes']['perms'][user] = {}\n ret['changes']['perms'][user][access_mode] = changes[user][access_mode]\n except CommandExecutionError as exc:\n ret['result'] = False\n ret['comment'].append(\n 'Failed to change {0} permissions for \"{1}\" to {2}\\n'\n 'Error: {3}'.format(access_mode, user, changes[user], exc.strerror))\n\n return ret", "response": "Helper function to check permissions for the object and return a dictionary of changes to the state system."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck owner and permissions for the passed directory. This function checks the permissions and sets them, returning the changes made. .. versionadded:: 2019.2.0 Args: obj_name (str): The name or full path to the object obj_type (Optional[str]): The type of object for which to check permissions. Default is 'file' ret (dict): A dictionary to append changes to and return. If not passed, will create a new dictionary to return. owner (str): The owner to set for the directory. grant_perms (dict): A dictionary containing the user/group and the basic permissions to check/grant, ie: ``{'user': {'perms': 'basic_permission'}}``. Default is ``None``. deny_perms (dict): A dictionary containing the user/group and permissions to check/deny. Default is ``None``. inheritance (bool): ``True`` will enable inheritance from the parent object. ``False`` will disable inheritance. Default is ``True``. reset (bool): ``True`` will clear the DACL and set only the permissions defined in ``grant_perms`` and ``deny_perms``. ``False`` append permissions to the existing DACL. Default is ``False``. This does NOT affect inherited permissions. Returns: dict: A dictionary of changes that have been made Usage: .. code-block:: bash # You have to use __utils__ in order for __opts__ to be available # To see changes to ``C:\\\\Temp`` if the 'Users' group is given 'read & execute' permissions. __utils__['dacl.check_perms'](obj_name='C:\\\\Temp', obj_type='file', owner='Administrators', grant_perms={ 'Users': { 'perms': 'read_execute' } }) # Specify advanced attributes with a list __utils__['dacl.check_perms'](obj_name='C:\\\\Temp', obj_type='file', owner='Administrators', grant_perms={ 'jsnuffy': { 'perms': [ 'read_attributes', 'read_ea' ], 'applies_to': 'files_only' } })", "response": "def check_perms(obj_name,\n obj_type='file',\n ret=None,\n owner=None,\n grant_perms=None,\n deny_perms=None,\n inheritance=True,\n reset=False):\n '''\n Check owner and permissions for the passed directory. This function checks\n the permissions and sets them, returning the changes made.\n\n .. versionadded:: 2019.2.0\n\n Args:\n\n obj_name (str):\n The name or full path to the object\n\n obj_type (Optional[str]):\n The type of object for which to check permissions. Default is 'file'\n\n ret (dict):\n A dictionary to append changes to and return. If not passed, will\n create a new dictionary to return.\n\n owner (str):\n The owner to set for the directory.\n\n grant_perms (dict):\n A dictionary containing the user/group and the basic permissions to\n check/grant, ie: ``{'user': {'perms': 'basic_permission'}}``.\n Default is ``None``.\n\n deny_perms (dict):\n A dictionary containing the user/group and permissions to\n check/deny. Default is ``None``.\n\n inheritance (bool):\n ``True`` will enable inheritance from the parent object. ``False``\n will disable inheritance. Default is ``True``.\n\n reset (bool):\n ``True`` will clear the DACL and set only the permissions defined\n in ``grant_perms`` and ``deny_perms``. ``False`` append permissions\n to the existing DACL. Default is ``False``. This does NOT affect\n inherited permissions.\n\n Returns:\n dict: A dictionary of changes that have been made\n\n Usage:\n\n .. code-block:: bash\n\n # You have to use __utils__ in order for __opts__ to be available\n\n # To see changes to ``C:\\\\Temp`` if the 'Users' group is given 'read & execute' permissions.\n __utils__['dacl.check_perms'](obj_name='C:\\\\Temp',\n obj_type='file',\n owner='Administrators',\n grant_perms={\n 'Users': {\n 'perms': 'read_execute'\n }\n })\n\n # Specify advanced attributes with a list\n __utils__['dacl.check_perms'](obj_name='C:\\\\Temp',\n obj_type='file',\n owner='Administrators',\n grant_perms={\n 'jsnuffy': {\n 'perms': [\n 'read_attributes',\n 'read_ea'\n ],\n 'applies_to': 'files_only'\n }\n })\n '''\n # Validate obj_type\n if obj_type.lower() not in flags().obj_type:\n raise SaltInvocationError(\n 'Invalid \"obj_type\" passed: {0}'.format(obj_type))\n\n obj_type = obj_type.lower()\n\n if not ret:\n ret = {'name': obj_name,\n 'changes': {},\n 'comment': [],\n 'result': True}\n orig_comment = ''\n else:\n orig_comment = ret['comment']\n ret['comment'] = []\n\n # Check owner\n if owner:\n owner = get_name(principal=owner)\n current_owner = get_owner(obj_name=obj_name, obj_type=obj_type)\n if owner != current_owner:\n if __opts__['test'] is True:\n ret['changes']['owner'] = owner\n else:\n try:\n set_owner(obj_name=obj_name,\n principal=owner,\n obj_type=obj_type)\n log.debug('Owner set to %s', owner)\n ret['changes']['owner'] = owner\n except CommandExecutionError:\n ret['result'] = False\n ret['comment'].append(\n 'Failed to change owner to \"{0}\"'.format(owner))\n\n # Check inheritance\n if inheritance is not None:\n if not inheritance == get_inheritance(obj_name=obj_name,\n obj_type=obj_type):\n if __opts__['test'] is True:\n ret['changes']['inheritance'] = inheritance\n else:\n try:\n set_inheritance(\n obj_name=obj_name,\n enabled=inheritance,\n obj_type=obj_type)\n log.debug('%s inheritance', 'Enabling' if inheritance else 'Disabling')\n ret['changes']['inheritance'] = inheritance\n except CommandExecutionError:\n ret['result'] = False\n ret['comment'].append(\n 'Failed to set inheritance for \"{0}\" to {1}'\n ''.format(obj_name, inheritance))\n\n # Check permissions\n log.debug('Getting current permissions for %s', obj_name)\n cur_perms = get_permissions(obj_name=obj_name, obj_type=obj_type)\n\n # Verify Deny Permissions\n if deny_perms is not None:\n ret = _check_perms(obj_name=obj_name,\n obj_type=obj_type,\n new_perms=deny_perms,\n cur_perms=cur_perms,\n access_mode='deny',\n ret=ret)\n\n # Verify Grant Permissions\n if grant_perms is not None:\n ret = _check_perms(obj_name=obj_name,\n obj_type=obj_type,\n new_perms=grant_perms,\n cur_perms=cur_perms,\n access_mode='grant',\n ret=ret)\n\n # Check reset\n # If reset=True, which users will be removed as a result\n if reset:\n log.debug('Resetting permissions for %s', obj_name)\n cur_perms = get_permissions(obj_name=obj_name, obj_type=obj_type)\n for user_name in cur_perms['Not Inherited']:\n # case insensitive dictionary search\n if grant_perms is not None and \\\n user_name.lower() not in set(k.lower() for k in grant_perms):\n if 'grant' in cur_perms['Not Inherited'][user_name]:\n if __opts__['test'] is True:\n if 'remove_perms' not in ret['changes']:\n ret['changes']['remove_perms'] = {}\n ret['changes']['remove_perms'].update(\n {user_name: cur_perms['Not Inherited'][user_name]})\n else:\n if 'remove_perms' not in ret['changes']:\n ret['changes']['remove_perms'] = {}\n rm_permissions(\n obj_name=obj_name,\n principal=user_name,\n ace_type='grant',\n obj_type=obj_type)\n ret['changes']['remove_perms'].update(\n {user_name: cur_perms['Not Inherited'][user_name]})\n # case insensitive dictionary search\n if deny_perms is not None and \\\n user_name.lower() not in set(k.lower() for k in deny_perms):\n if 'deny' in cur_perms['Not Inherited'][user_name]:\n if __opts__['test'] is True:\n if 'remove_perms' not in ret['changes']:\n ret['changes']['remove_perms'] = {}\n ret['changes']['remove_perms'].update(\n {user_name: cur_perms['Not Inherited'][user_name]})\n else:\n if 'remove_perms' not in ret['changes']:\n ret['changes']['remove_perms'] = {}\n rm_permissions(\n obj_name=obj_name,\n principal=user_name,\n ace_type='deny',\n obj_type=obj_type)\n ret['changes']['remove_perms'].update(\n {user_name: cur_perms['Not Inherited'][user_name]})\n\n # Re-add the Original Comment if defined\n if isinstance(orig_comment, six.string_types):\n if orig_comment:\n ret['comment'].insert(0, orig_comment)\n else:\n if orig_comment:\n ret['comment'] = orig_comment.extend(ret['comment'])\n\n ret['comment'] = '\\n'.join(ret['comment'])\n\n # Set result for test = True\n if __opts__['test'] and ret['changes']:\n ret['result'] = None\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef set_perms(obj_name,\n obj_type='file',\n grant_perms=None,\n deny_perms=None,\n inheritance=True,\n reset=False):\n '''\n Set permissions for the given path\n\n .. versionadded:: 2019.2.0\n\n Args:\n\n obj_name (str):\n The name or full path to the object\n\n obj_type (Optional[str]):\n The type of object for which to check permissions. Default is 'file'\n\n grant_perms (dict):\n A dictionary containing the user/group and the basic permissions to\n grant, ie: ``{'user': {'perms': 'basic_permission'}}``. You can also\n set the ``applies_to`` setting here. The default for ``applise_to``\n is ``this_folder_subfolders_files``. Specify another ``applies_to``\n setting like this:\n\n .. code-block:: yaml\n\n {'user': {'perms': 'full_control', 'applies_to': 'this_folder'}}\n\n To set advanced permissions use a list for the ``perms`` parameter,\n ie:\n\n .. code-block:: yaml\n\n {'user': {'perms': ['read_attributes', 'read_ea'], 'applies_to': 'this_folder'}}\n\n To see a list of available attributes and applies to settings see\n the documentation for salt.utils.win_dacl.\n\n A value of ``None`` will make no changes to the ``grant`` portion of\n the DACL. Default is ``None``.\n\n deny_perms (dict):\n A dictionary containing the user/group and permissions to deny along\n with the ``applies_to`` setting. Use the same format used for the\n ``grant_perms`` parameter. Remember, deny permissions supersede\n grant permissions.\n\n A value of ``None`` will make no changes to the ``deny`` portion of\n the DACL. Default is ``None``.\n\n inheritance (bool):\n If ``True`` the object will inherit permissions from the parent, if\n ``False``, inheritance will be disabled. Inheritance setting will\n not apply to parent directories if they must be created. Default is\n ``False``.\n\n reset (bool):\n If ``True`` the existing DCL will be cleared and replaced with the\n settings defined in this function. If ``False``, new entries will be\n appended to the existing DACL. Default is ``False``.\n\n Returns:\n bool: True if successful\n\n Raises:\n CommandExecutionError: If unsuccessful\n\n Usage:\n\n .. code-block:: bash\n\n import salt.utils.win_dacl\n\n # To grant the 'Users' group 'read & execute' permissions.\n salt.utils.win_dacl.set_perms(obj_name='C:\\\\Temp',\n obj_type='file',\n grant_perms={\n 'Users': {\n 'perms': 'read_execute'\n }\n })\n\n # Specify advanced attributes with a list\n salt.utils.win_dacl.set_perms(obj_name='C:\\\\Temp',\n obj_type='file',\n grant_perms={\n 'jsnuffy': {\n 'perms': [\n 'read_attributes',\n 'read_ea'\n ],\n 'applies_to': 'this_folder_only'\n }\n }\"\n '''\n ret = {}\n\n if reset:\n # Get an empty DACL\n obj_dacl = dacl(obj_type=obj_type)\n\n # Get an empty perms dict\n cur_perms = {}\n\n else:\n # Get the DACL for the directory\n obj_dacl = dacl(obj_name, obj_type=obj_type)\n\n # Get current file/folder permissions\n cur_perms = get_permissions(obj_name=obj_name, obj_type=obj_type)\n\n # Set 'deny' perms if any\n if deny_perms is not None:\n ret['deny'] = _set_perms(obj_dacl=obj_dacl,\n obj_type=obj_type,\n new_perms=deny_perms,\n cur_perms=cur_perms,\n access_mode='deny')\n\n # Set 'grant' perms if any\n if grant_perms is not None:\n ret['grant'] = _set_perms(obj_dacl=obj_dacl,\n obj_type=obj_type,\n new_perms=grant_perms,\n cur_perms=cur_perms,\n access_mode='grant')\n\n # Order the ACL\n obj_dacl.order_acl()\n\n # Save the DACL, setting the inheritance\n # you have to invert inheritance because dacl.save is looking for\n # protected. protected True means Inherited False...\n\n if obj_dacl.save(obj_name, not inheritance):\n return ret\n\n return {}", "response": "Sets the permissions for the given object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nretrieving a set of public keys from GitHub for the specified list of users.", "response": "def get_user_pubkeys(users):\n '''\n Retrieve a set of public keys from GitHub for the specified list of users.\n Expects input in list format. Optionally, a value in the list may be a dict\n whose value is a list of key IDs to be returned. If this is not done, then\n all keys will be returned.\n\n Some example data structures that coupld be passed in would look like:\n\n .. code_block:: yaml\n\n ['user1', 'user2', 'user3']\n\n [\n 'user1': [\n '12345',\n '67890',\n ],\n 'user2',\n 'user3',\n ]\n '''\n if not isinstance(users, list):\n return {'Error': 'A list of users is expected'}\n\n ret = {}\n for user in users:\n key_ids = []\n if isinstance(user, dict):\n tmp_user = next(six.iterkeys(user))\n key_ids = user[tmp_user]\n user = tmp_user\n\n url = 'https://api.github.com/users/{0}/keys'.format(user)\n result = salt.utils.http.query(\n url,\n 'GET',\n decode=False,\n text=True,\n )\n\n keys = salt.utils.json.loads(result['text'])\n\n ret[user] = {}\n for key in keys:\n if key_ids:\n if six.text_type(key['id']) in key_ids:\n ret[user][key['id']] = key['key']\n else:\n ret[user][key['id']] = key['key']\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_hosts(sld, tld):\n '''\n Retrieves DNS host record settings for the requested domain.\n\n returns a dictionary of information about the requested domain\n\n sld\n SLD of the domain name\n\n tld\n TLD of the domain name\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'my-minion' namecheap_domains_dns.get_hosts sld tld\n '''\n opts = salt.utils.namecheap.get_opts('namecheap.domains.dns.gethosts')\n opts['TLD'] = tld\n opts['SLD'] = sld\n\n response_xml = salt.utils.namecheap.get_request(opts)\n if response_xml is None:\n return {}\n\n domaindnsgethostsresult = response_xml.getElementsByTagName('DomainDNSGetHostsResult')[0]\n\n return salt.utils.namecheap.xml_to_dict(domaindnsgethostsresult)", "response": "Retrieves DNS host record settings for the requested domain."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting a list of DNS servers associated with the requested domain.", "response": "def get_list(sld, tld):\n '''\n Gets a list of DNS servers associated with the requested domain.\n\n returns a dictionary of information about requested domain\n\n sld\n SLD of the domain name\n\n tld\n TLD of the domain name\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'my-minion' namecheap_domains_dns.get_list sld tld\n '''\n opts = salt.utils.namecheap.get_opts('namecheap.domains.dns.getlist')\n opts['TLD'] = tld\n opts['SLD'] = sld\n\n response_xml = salt.utils.namecheap.get_request(opts)\n if response_xml is None:\n return {}\n\n domaindnsgetlistresult = response_xml.getElementsByTagName('DomainDNSGetListResult')[0]\n\n return salt.utils.namecheap.xml_to_dict(domaindnsgetlistresult)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsets the DNS host records settings for the requested domain. Returns True if the host records were set successfully.", "response": "def set_hosts(sld, tld, hosts):\n '''\n Sets DNS host records settings for the requested domain.\n\n returns True if the host records were set successfully\n\n sld\n SLD of the domain name\n\n tld\n TLD of the domain name\n\n hosts\n Must be passed as a list of Python dictionaries, with each dictionary\n containing the following keys:\n\n - **hostname**\n - **recordtype** - One of ``A``, ``AAAA``, ``CNAME``, ``MX``, ``MXE``,\n ``TXT``, ``URL``, ``URL301``, or ``FRAME``\n - **address** - URL or IP address\n - **ttl** - An integer between 60 and 60000 (default: ``1800``)\n\n Additonally, the ``mxpref`` key can be present, but must be accompanied\n by an ``emailtype`` key.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'my-minion' namecheap_domains_dns.set_hosts sld tld hosts\n '''\n opts = salt.utils.namecheap.get_opts('namecheap.domains.dns.setHosts')\n opts['SLD'] = sld\n opts['TLD'] = tld\n i = 1\n for hostrecord in hosts:\n str_i = six.text_type(i)\n opts['HostName' + str_i] = hostrecord['hostname']\n opts['RecordType' + str_i] = hostrecord['recordtype']\n opts['Address' + str_i] = hostrecord['address']\n if 'ttl' in hostrecord:\n opts['TTL' + str_i] = hostrecord['ttl']\n if 'mxpref' in hostrecord:\n opts['MXPref' + str_i] = hostrecord['mxpref']\n opts['EmailType'] = hostrecord['emailtype']\n i += 1\n\n response_xml = salt.utils.namecheap.post_request(opts)\n if response_xml is None:\n return False\n\n dnsresult = response_xml.getElementsByTagName('DomainDNSSetHostsResult')[0]\n return salt.utils.namecheap.string_to_value(dnsresult.getAttribute('IsSuccess'))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting the domain to use custom DNS servers. Returns True if the custom nameservers were set successfully", "response": "def set_custom(sld, tld, nameservers):\n '''\n Sets domain to use custom DNS servers.\n\n returns True if the custom nameservers were set successfully\n\n sld\n SLD of the domain name\n\n tld\n TLD of the domain name\n\n nameservers\n array of strings List of nameservers to be associated with this domain\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'my-minion' namecheap_domains_dns.set_custom sld tld nameserver\n '''\n opts = salt.utils.namecheap.get_opts('namecheap.domains.dns.setCustom')\n opts['SLD'] = sld\n opts['TLD'] = tld\n opts['Nameservers'] = ','.join(nameservers)\n response_xml = salt.utils.namecheap.post_request(opts)\n if response_xml is None:\n return False\n\n dnsresult = response_xml.getElementsByTagName('DomainDNSSetCustomResult')[0]\n return salt.utils.namecheap.string_to_value(dnsresult.getAttribute('Update'))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_default(sld, tld):\n '''\n Sets domain to use namecheap default DNS servers. Required for free\n services like Host record management, URL forwarding, email forwarding,\n dynamic DNS and other value added services.\n\n sld\n SLD of the domain name\n\n tld\n TLD of the domain name\n\n Returns ``True`` if the domain was successfully pointed at the default DNS\n servers.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'my-minion' namecheap_domains_dns.set_default sld tld\n '''\n opts = salt.utils.namecheap.get_opts('namecheap.domains.dns.setDefault')\n opts['SLD'] = sld\n opts['TLD'] = tld\n response_xml = salt.utils.namecheap.post_request(opts)\n if response_xml is None:\n return False\n\n dnsresult = response_xml.getElementsByTagName('DomainDNSSetDefaultResult')[0]\n return salt.utils.namecheap.string_to_value(dnsresult.getAttribute('Updated'))", "response": "Sets the domain to use namecheap default DNS servers."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndisplaying the profiling data in a table format.", "response": "def output(data, **kwargs):\n '''\n Display the profiling data in a table format.\n '''\n\n rows = _find_durations(data)\n\n kwargs['opts'] = __opts__\n kwargs['rows_key'] = 'rows'\n kwargs['labels_key'] = 'labels'\n\n to_show = {'labels': ['name', 'mod.fun', 'duration (ms)'],\n 'rows': rows}\n\n return table_out.output(to_show, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nensuring an image exists copy it else from source and create it if it does not exist.", "response": "def present(name,\n source,\n aliases=None,\n public=None,\n auto_update=None,\n remote_addr=None,\n cert=None,\n key=None,\n verify_cert=True):\n '''\n Ensure an image exists, copy it else from source\n\n name :\n An alias of the image, this is used to check if the image exists and\n it will be added as alias to the image on copy/create.\n\n source :\n Source dict.\n\n For an LXD to LXD copy:\n\n .. code-block: yaml\n\n source:\n type: lxd\n name: ubuntu/xenial/amd64 # This can also be a fingerprint.\n remote_addr: https://images.linuxcontainers.org:8443\n cert: ~/.config/lxd/client.crt\n key: ~/.config/lxd/client.key\n verify_cert: False\n\n .. attention:\n\n For this kind of remote you also need to provide:\n - a https:// remote_addr\n - a cert and key\n - verify_cert\n\n From file:\n\n .. code-block: yaml\n\n source:\n type: file\n filename: salt://lxd/files/busybox.tar.xz\n saltenv: base\n\n From simplestreams:\n\n .. code-block: yaml\n\n source:\n type: simplestreams\n server: https://cloud-images.ubuntu.com/releases\n name: xenial/amd64\n\n From an URL:\n\n .. code-block: yaml\n\n source:\n type: url\n url: https://dl.stgraber.org/lxd\n\n aliases :\n List of aliases to append, can be empty.\n\n public :\n Make this image public available on this instance?\n None on source_type LXD means copy source\n None on source_type file means False\n\n auto_update :\n Try to auto-update from the original source?\n None on source_type LXD means copy source\n source_type file does not have auto-update.\n\n remote_addr :\n An URL to a remote Server, you also have to give cert and key if you\n provide remote_addr!\n\n Examples:\n https://myserver.lan:8443\n /var/lib/mysocket.sock\n\n cert :\n PEM Formatted SSL Zertifikate.\n\n Examples:\n ~/.config/lxc/client.crt\n\n key :\n PEM Formatted SSL Key.\n\n Examples:\n ~/.config/lxc/client.key\n\n verify_cert : True\n Wherever to verify the cert, this is by default True\n but in the most cases you want to set it off as LXD\n normaly uses self-signed certificates.\n '''\n if aliases is None:\n aliases = []\n\n # Create a copy of aliases, since we're modifying it here\n aliases = aliases[:]\n ret = {\n 'name': name,\n 'source': source,\n 'aliases': aliases,\n 'public': public,\n 'auto_update': auto_update,\n\n 'remote_addr': remote_addr,\n 'cert': cert,\n 'key': key,\n 'verify_cert': verify_cert,\n\n 'changes': {}\n }\n\n image = None\n try:\n image = __salt__['lxd.image_get_by_alias'](\n name, remote_addr, cert, key, verify_cert, _raw=True\n )\n except CommandExecutionError as e:\n return _error(ret, six.text_type(e))\n except SaltInvocationError as e:\n # Image not found\n pass\n\n if image is None:\n if __opts__['test']:\n # Test is on, just return that we would create the image\n msg = 'Would create the image \"{0}\"'.format(name)\n ret['changes'] = {'created': msg}\n return _unchanged(ret, msg)\n\n try:\n if source['type'] == 'lxd':\n image = __salt__['lxd.image_copy_lxd'](\n source['name'],\n src_remote_addr=source['remote_addr'],\n src_cert=source['cert'],\n src_key=source['key'],\n src_verify_cert=source.get('verify_cert', True),\n remote_addr=remote_addr,\n cert=cert,\n key=key,\n verify_cert=verify_cert,\n aliases=aliases,\n public=public,\n auto_update=auto_update,\n _raw=True\n )\n\n if source['type'] == 'file':\n if 'saltenv' not in source:\n source['saltenv'] = __env__\n image = __salt__['lxd.image_from_file'](\n source['filename'],\n remote_addr=remote_addr,\n cert=cert,\n key=key,\n verify_cert=verify_cert,\n aliases=aliases,\n public=False if public is None else public,\n saltenv=source['saltenv'],\n _raw=True\n )\n\n if source['type'] == 'simplestreams':\n image = __salt__['lxd.image_from_simplestreams'](\n source['server'],\n source['name'],\n remote_addr=remote_addr,\n cert=cert,\n key=key,\n verify_cert=verify_cert,\n aliases=aliases,\n public=False if public is None else public,\n auto_update=False if auto_update is None else auto_update,\n _raw=True\n )\n\n if source['type'] == 'url':\n image = __salt__['lxd.image_from_url'](\n source['url'],\n remote_addr=remote_addr,\n cert=cert,\n key=key,\n verify_cert=verify_cert,\n aliases=aliases,\n public=False if public is None else public,\n auto_update=False if auto_update is None else auto_update,\n _raw=True\n )\n except CommandExecutionError as e:\n return _error(ret, six.text_type(e))\n\n # Sync aliases\n if name not in aliases:\n aliases.append(name)\n\n old_aliases = set([six.text_type(a['name']) for a in image.aliases])\n new_aliases = set(map(six.text_type, aliases))\n\n alias_changes = []\n # Removed aliases\n for k in old_aliases.difference(new_aliases):\n if not __opts__['test']:\n __salt__['lxd.image_alias_delete'](image, k)\n alias_changes.append('Removed alias \"{0}\"'.format(k))\n else:\n alias_changes.append('Would remove alias \"{0}\"'.format(k))\n\n # New aliases\n for k in new_aliases.difference(old_aliases):\n if not __opts__['test']:\n __salt__['lxd.image_alias_add'](image, k, '')\n alias_changes.append('Added alias \"{0}\"'.format(k))\n else:\n alias_changes.append('Would add alias \"{0}\"'.format(k))\n\n if alias_changes:\n ret['changes']['aliases'] = alias_changes\n\n # Set public\n if public is not None and image.public != public:\n if not __opts__['test']:\n ret['changes']['public'] = \\\n 'Setting the image public to {0!s}'.format(public)\n image.public = public\n __salt__['lxd.pylxd_save_object'](image)\n else:\n ret['changes']['public'] = \\\n 'Would set public to {0!s}'.format(public)\n\n if __opts__['test'] and ret['changes']:\n return _unchanged(\n ret,\n 'Would do {0} changes'.format(len(ret['changes'].keys()))\n )\n\n return _success(ret, '{0} changes'.format(len(ret['changes'].keys())))"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef hosted_zone_present(name, Name=None, PrivateZone=False,\n CallerReference=None, Comment=None, VPCs=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure a hosted zone exists with the given attributes.\n\n name\n The name of the state definition.\n\n Name\n The name of the domain. This should be a fully-specified domain, and should terminate with a\n period. This is the name you have registered with your DNS registrar. It is also the name\n you will delegate from your registrar to the Amazon Route 53 delegation servers returned in\n response to this request. If not provided, the value of name will be used.\n\n PrivateZone\n Set True if creating a private hosted zone. If true, then 'VPCs' is also required.\n\n Comment\n Any comments you want to include about the hosted zone.\n\n CallerReference\n A unique string that identifies the request and that allows create_hosted_zone() calls to be\n retried without the risk of executing the operation twice. This helps ensure idempotency\n across state calls, but can cause issues if a zone is deleted and then an attempt is made\n to recreate it with the same CallerReference. If not provided, a unique UUID will be\n generated at each state run, which can potentially lead to duplicate zones being created if\n the state is run again while the previous zone creation is still in PENDING status (which\n can occasionally take several minutes to clear). Maximum length of 128.\n\n VPCs\n A list of dicts, each dict composed of a VPCRegion, and either a VPCId or a VPCName.\n Note that this param is ONLY used if PrivateZone == True\n\n VPCId\n When creating a private hosted zone, either the VPC ID or VPC Name to associate with is\n required. Exclusive with VPCName.\n\n VPCName\n When creating a private hosted zone, either the VPC ID or VPC Name to associate with is\n required. Exclusive with VPCId.\n\n VPCRegion\n When creating a private hosted zone, the region of the associated VPC is required. If\n not provided, an effort will be made to determine it from VPCId or VPCName, if\n possible. This will fail if a given VPCName exists in multiple regions visible to the\n bound account, in which case you'll need to provide an explicit value for VPCRegion.\n '''\n Name = Name if Name else name\n\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n if not PrivateZone and VPCs:\n raise SaltInvocationError(\"Parameter 'VPCs' is invalid when creating a public zone.\")\n if PrivateZone and not VPCs:\n raise SaltInvocationError(\"Parameter 'VPCs' is required when creating a private zone.\")\n if VPCs:\n if not isinstance(VPCs, list):\n raise SaltInvocationError(\"Parameter 'VPCs' must be a list of dicts.\")\n for v in VPCs:\n if not isinstance(v, dict) or not salt.utils.data.exactly_one((v.get('VPCId'), v.get('VPCName'))):\n raise SaltInvocationError(\"Parameter 'VPCs' must be a list of dicts, each composed \"\n \"of either a 'VPCId' or a 'VPCName', and optionally a \"\n \"'VPCRegion', to help distinguish between multitple matches.\")\n # Massage VPCs into something AWS will accept...\n fixed_vpcs = []\n if PrivateZone:\n for v in VPCs:\n VPCId = v.get('VPCId')\n VPCName = v.get('VPCName')\n VPCRegion = v.get('VPCRegion')\n VPCs = __salt__['boto_vpc.describe_vpcs'](vpc_id=VPCId, name=VPCName, region=region,\n key=key, keyid=keyid, profile=profile).get('vpcs', [])\n if VPCRegion and VPCs:\n VPCs = [v for v in VPCs if v['region'] == VPCRegion]\n if not VPCs:\n ret['comment'] = ('A VPC matching given criteria (vpc: {0} / vpc_region: {1}) not '\n 'found.'.format(VPCName or VPCId, VPCRegion))\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n if len(VPCs) > 1:\n ret['comment'] = ('Multiple VPCs matching given criteria (vpc: {0} / vpc_region: '\n '{1}) found: {2}.'.format(VPCName or VPCId, VPCRegion,\n ', '.join([v['id'] for v in VPCs])))\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n vpc = VPCs[0]\n if VPCName:\n VPCId = vpc['id']\n if not VPCRegion:\n VPCRegion = vpc['region']\n fixed_vpcs += [{'VPCId': VPCId, 'VPCRegion': VPCRegion}]\n\n create = False\n update_comment = False\n add_vpcs = []\n del_vpcs = []\n args = {'Name': Name, 'PrivateZone': PrivateZone,\n 'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n zone = __salt__['boto3_route53.find_hosted_zone'](**args)\n if not zone:\n create = True\n # Grrrr - can only pass one VPC when initially creating a private zone...\n # The rest have to be added (one-by-one) later in a separate step.\n if len(fixed_vpcs) > 1:\n add_vpcs = fixed_vpcs[1:]\n fixed_vpcs = fixed_vpcs[:1]\n CallerReference = CallerReference if CallerReference else str(uuid.uuid4()) # future lint: disable=blacklisted-function\n else:\n # Currently the only modifiable traits about a zone are associated VPCs and the comment.\n zone = zone[0]\n if PrivateZone:\n for z in zone.get('VPCs'):\n if z not in fixed_vpcs:\n del_vpcs += [z]\n for z in fixed_vpcs:\n if z not in zone.get('VPCs'):\n add_vpcs += [z]\n if zone['HostedZone']['Config'].get('Comment') != Comment:\n update_comment = True\n\n if not (create or add_vpcs or del_vpcs or update_comment):\n ret['comment'] = 'Hostd Zone {0} already in desired state'.format(Name)\n return ret\n\n if create:\n if __opts__['test']:\n ret['comment'] = 'Route 53 {} hosted zone {} would be created.'.format('private' if\n PrivateZone else 'public', Name)\n ret['result'] = None\n return ret\n vpc_id = fixed_vpcs[0].get('VPCId') if fixed_vpcs else None\n vpc_region = fixed_vpcs[0].get('VPCRegion') if fixed_vpcs else None\n newzone = __salt__['boto3_route53.create_hosted_zone'](Name=Name,\n CallerReference=CallerReference, Comment=Comment,\n PrivateZone=PrivateZone, VPCId=vpc_id, VPCRegion=vpc_region,\n region=region, key=key, keyid=keyid, profile=profile)\n if newzone:\n newzone = newzone[0]\n ret['comment'] = 'Route 53 {} hosted zone {} successfully created'.format('private' if\n PrivateZone else 'public', Name)\n log.info(ret['comment'])\n ret['changes']['new'] = newzone\n else:\n ret['comment'] = 'Creation of Route 53 {} hosted zone {} failed'.format('private' if\n PrivateZone else 'public', Name)\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n\n if update_comment:\n if __opts__['test']:\n ret['comment'] = 'Route 53 {} hosted zone {} comment would be updated.'.format('private'\n if PrivateZone else 'public', Name)\n ret['result'] = None\n return ret\n r = __salt__['boto3_route53.update_hosted_zone_comment'](Name=Name,\n Comment=Comment, PrivateZone=PrivateZone, region=region, key=key, keyid=keyid,\n profile=profile)\n if r:\n r = r[0]\n msg = 'Route 53 {} hosted zone {} comment successfully updated'.format('private' if\n PrivateZone else 'public', Name)\n log.info(msg)\n ret['comment'] = ' '.join([ret['comment'], msg])\n ret['changes']['old'] = zone\n ret['changes']['new'] = salt.utils.dictupdate.update(ret['changes'].get('new', {}), r)\n else:\n ret['comment'] = 'Update of Route 53 {} hosted zone {} comment failed'.format('private'\n if PrivateZone else 'public', Name)\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n\n if add_vpcs or del_vpcs:\n if __opts__['test']:\n ret['comment'] = 'Route 53 {} hosted zone {} associated VPCs would be updated.'.format(\n 'private' if PrivateZone else 'public', Name)\n ret['result'] = None\n return ret\n all_added = True\n all_deled = True\n for vpc in add_vpcs: # Add any new first to avoid the \"can't delete last VPC\" errors.\n r = __salt__['boto3_route53.associate_vpc_with_hosted_zone'](Name=Name,\n VPCId=vpc['VPCId'], VPCRegion=vpc['VPCRegion'], region=region, key=key,\n keyid=keyid, profile=profile)\n if not r:\n all_added = False\n for vpc in del_vpcs:\n r = __salt__['boto3_route53.disassociate_vpc_from_hosted_zone'](Name=Name,\n VPCId=vpc['VPCId'], VPCRegion=vpc['VPCRegion'], region=region, key=key,\n keyid=keyid, profile=profile)\n if not r:\n all_deled = False\n\n ret['changes']['old'] = zone\n ret['changes']['new'] = __salt__['boto3_route53.find_hosted_zone'](**args)\n if all_added and all_deled:\n msg = 'Route 53 {} hosted zone {} associated VPCs successfully updated'.format('private'\n if PrivateZone else 'public', Name)\n log.info(msg)\n ret['comment'] = ' '.join([ret['comment'], msg])\n else:\n ret['comment'] = 'Update of Route 53 {} hosted zone {} associated VPCs failed'.format(\n 'private' if PrivateZone else 'public', Name)\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n\n return ret", "response": "Ensure a hosted zone with the given attributes exists."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nensures the Route 53 Hostes Zone described is absent.", "response": "def hosted_zone_absent(name, Name=None, PrivateZone=False,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure the Route53 Hostes Zone described is absent\n\n name\n The name of the state definition.\n\n Name\n The name of the domain. This should be a fully-specified domain, and should terminate with a\n period. If not provided, the value of name will be used.\n\n PrivateZone\n Set True if deleting a private hosted zone.\n\n '''\n Name = Name if Name else name\n\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n args = {'Name': Name, 'PrivateZone': PrivateZone, 'region': region, 'key': key,\n 'keyid': keyid, 'profile': profile}\n zone = __salt__['boto3_route53.find_hosted_zone'](**args)\n if not zone:\n ret['comment'] = 'Route 53 {} hosted zone {} already absent'.format('private' if\n PrivateZone else 'public', Name)\n log.info(ret['comment'])\n return ret\n if __opts__['test']:\n ret['comment'] = 'Route 53 {} hosted zone {} would be deleted'.format('private' if\n PrivateZone else 'public', Name)\n ret['result'] = None\n return ret\n zone = zone[0]\n Id = zone['HostedZone']['Id']\n if __salt__['boto3_route53.delete_hosted_zone'](Id=Id, region=region, key=key,\n keyid=keyid, profile=profile):\n ret['comment'] = 'Route 53 {} hosted zone {} deleted'.format('private' if PrivateZone else\n 'public', Name)\n log.info(ret['comment'])\n ret['changes']['old'] = zone\n ret['changes']['new'] = None\n else:\n ret['comment'] = 'Failed to delete Route 53 {} hosted zone {}'.format('private' if\n PrivateZone else 'public', Name)\n log.info(ret['comment'])\n ret['result'] = False\n return ret\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nensures a Route 53 record is present in the Route 53 record store.", "response": "def rr_present(name, HostedZoneId=None, DomainName=None, PrivateZone=False, Name=None, Type=None,\n SetIdentifier=None, Weight=None, Region=None, GeoLocation=None, Failover=None,\n TTL=None, ResourceRecords=None, AliasTarget=None, HealthCheckId=None,\n TrafficPolicyInstanceId=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure the Route53 record is present.\n\n name\n The name of the state definition. This will be used for Name if the latter is\n not provided.\n\n HostedZoneId\n The ID of a zone to create the record in. Exclusive with DomainName.\n\n DomainName\n The domain name of a zone to create the record in. Exclusive with HostedZoneId.\n\n PrivateZone\n Set to True if the resource record should be in a private zone, False if public.\n\n Name\n Name of the Route 53 resource record being managed.\n\n Type\n The record type (A, NS, MX, TXT, etc.)\n\n SetIdentifier\n Valid for Weighted, Latency, Geolocation, and Failover resource record sets only.\n An identifier that differentiates among multiple resource record sets that have the same\n combination of DNS name and type. The value of SetIdentifier must be unique for each\n resource record set that has the same combination of DNS name and type. Omit SetIdentifier\n for any other types of record sets.\n\n Weight\n Valid for Weighted resource record sets only. Among resource record sets that have the\n same combination of DNS name and type, a value that determines the proportion of DNS\n queries that Amazon Route 53 responds to using the current resource record set. Amazon Route\n 53 calculates the sum of the weights for the resource record sets that have the same\n combination of DNS name and type. Amazon Route 53 then responds to queries based on the\n ratio of a resource's weight to the total.\n\n Note the following:\n\n - You must specify a value for the Weight element for every weighted resource record set.\n - You can only specify one ResourceRecord per weighted resource record set.\n - You can't create latency, failover, or geolocation resource record sets that have the\n same values for the Name and Type elements as weighted resource record sets.\n - You can create a maximum of 100 weighted resource record sets that have the same values\n for the Name and Type elements.\n - For weighted (but not weighted alias) resource record sets, if you set Weight to 0 for a\n resource record set, Amazon Route 53 never responds to queries with the applicable value\n for that resource record set. However, if you set Weight to 0 for all resource record\n sets that have the same combination of DNS name and type, traffic is routed to all\n resources with equal probability. The effect of setting Weight to 0 is different when\n you associate health checks with weighted resource record sets. For more information,\n see `Options for Configuring Amazon Route 53 Active-Active and Active-Passive Failover`__\n in the Amazon Route 53 Developer Guide.\n\n .. __: http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/dns-failover-configuring-options.html\n\n Region\n Valid for Latency-based resource record sets only. The Amazon EC2 Region where the resource\n that is specified in this resource record set resides. The resource typically is an AWS\n resource, such as an EC2 instance or an ELB load balancer, and is referred to by an IP\n address or a DNS domain name, depending on the record type.\n\n GeoLocation\n Geo location resource record sets only. A dict that lets you control how Route 53 responds\n to DNS queries based on the geographic origin of the query. For example, if you want all\n queries from Africa to be routed to a web server with an IP address of 192.0.2.111, create a\n resource record set with a Type of A and a ContinentCode of AF.\n\n .. code-block:: text\n\n ContinentCode\n The two-letter code for the continent.\n Valid values: AF | AN | AS | EU | OC | NA | SA\n Constraint: Specifying ContinentCode with either CountryCode or SubdivisionCode\n returns an InvalidInput error.\n CountryCode\n The two-letter code for the country.\n SubdivisionCode\n The code for the subdivision, for example, a state in the United States or a\n province in Canada.\n\n Notes\n\n - Creating geolocation and geolocation alias resource record sets in private hosted zones\n is not supported.\n - If you create separate resource record sets for overlapping geographic regions (for\n example, one resource record set for a continent and one for a country on the same\n continent), priority goes to the smallest geographic region. This allows you to route\n most queries for a continent to one resource and to route queries for a country on that\n continent to a different resource.\n - You can't create two geolocation resource record sets that specify the same geographic\n location.\n - The value ``*`` in the CountryCode element matches all geographic locations that aren't\n specified in other geolocation resource record sets that have the same values for the\n Name and Type elements.\n - Geolocation works by mapping IP addresses to locations. However, some IP addresses\n aren't mapped to geographic locations, so even if you create geolocation resource\n record sets that cover all seven continents, Amazon Route 53 will receive some DNS\n queries from locations that it can't identify. We recommend that you\n create a resource record set for which the value of CountryCode is\n ``*``, which handles both queries that come from locations for which you\n haven't created geolocation resource record sets and queries from IP\n addresses that aren't mapped to a location. If you don't create a ``*``\n resource record set, Amazon Route 53 returns a \"no answer\" response\n for queries from those locations.\n - You can't create non-geolocation resource record sets that have the same values for the\n Name and Type elements as geolocation resource record sets.\n\n TTL\n The resource record cache time to live (TTL), in seconds.\n Note the following:\n\n - If you're creating an alias resource record set, omit TTL. Amazon Route 53 uses the\n value of TTL for the alias target.\n - If you're associating this resource record set with a health check (if you're adding\n a HealthCheckId element), we recommend that you specify a TTL of 60 seconds or less so\n clients respond quickly to changes in health status.\n - All of the resource record sets in a group of weighted, latency, geolocation, or\n failover resource record sets must have the same value for TTL.\n - If a group of weighted resource record sets includes one or more weighted alias\n resource record sets for which the alias target is an ELB load balancer, we recommend\n that you specify a TTL of 60 seconds for all of the non-alias weighted resource record\n sets that have the same name and type. Values other than 60 seconds (the TTL for load\n balancers) will change the effect of the values that you specify for Weight.\n\n ResourceRecords\n A list, containing one or more values for the resource record. No single value can exceed\n 4,000 characters. For details on how to format values for different record types, see\n `Supported DNS Resource Record Types`__ in the Amazon Route 53 Developer Guide.\n\n .. __: http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html\n\n Note: You can specify more than one value for all record types except CNAME and SOA.\n\n It is also possible to pass \"magic\" strings as resource record values. This functionality\n can easily be extended, but for the moment supports the following:\n\n 'magic:ec2_instance_tag:some_tag_name:some_string:some_instance_attr'\n\n This tells salt to lookup an EC2 instance with a tag 'some_tag_name' which has the value\n 'some_string' and substitute the 'some_instance_attr' attribute of that instance as the\n resource record value being evaluated.\n\n This should work generally for any EC2 instance tags, as long as the instance attribute\n being fetched is available to getattr(instance, 'attribute') as seen in the code below.\n Anything else will most likely require this function to be extended to handle it.\n\n The canonical use-case for this (at least at our site) is to query the Name tag (which\n we always populate with the host's FQDN) to lookup the public or private IPs bound to the\n instance, so we can then automgically create Route 53 records for them.\n\n AliasTarget\n The rules governing how to define an AliasTarget for the various supported use-cases are\n obtuse beyond reason and attempting to paraphrase them (or even worse, cut-and-paste them\n in their entirety) would be silly and counterproductive. If you need this feature, then\n Read The Fine Materials at the `Boto 3 Route 53 page`__ and/or the `AWS Route 53 docs`__\n and suss them for yourself - I sure won't claim to understand them partcularly well.\n\n .. __: http://boto3.readthedocs.io/en/latest/reference/services/route53.html#Route53.Client.change_resource_record_sets\n .. __: http://docs.aws.amazon.com/Route53/latest/APIReference/API_AliasTarget.html\n\n region\n The region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n '''\n Name = Name if Name else name\n\n if Type is None:\n raise SaltInvocationError(\"'Type' is a required parameter when adding or updating\"\n \"resource records.\")\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n args = {'Id': HostedZoneId, 'Name': DomainName, 'PrivateZone': PrivateZone,\n 'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n zone = __salt__['boto3_route53.find_hosted_zone'](**args)\n if not zone:\n ret['comment'] = 'Route 53 {} hosted zone {} not found'.format('private' if PrivateZone\n else 'public', DomainName)\n log.info(ret['comment'])\n return ret\n zone = zone[0]\n HostedZoneId = zone['HostedZone']['Id']\n\n # Convert any magic RR values to something AWS will understand, and otherwise clean them up.\n fixed_rrs = []\n if ResourceRecords:\n for rr in ResourceRecords:\n if rr.startswith('magic:'):\n fields = rr.split(':')\n if fields[1] == 'ec2_instance_tag':\n if len(fields) != 5:\n log.warning(\"Invalid magic RR value seen: '%s'. Passing as-is.\", rr)\n fixed_rrs += [rr]\n continue\n tag_name = fields[2]\n tag_value = fields[3]\n instance_attr = fields[4]\n good_states = ('pending', 'rebooting', 'running', 'stopping', 'stopped')\n r = __salt__['boto_ec2.find_instances'](\n tags={tag_name: tag_value}, return_objs=True, in_states=good_states,\n region=region, key=key, keyid=keyid, profile=profile)\n if not r:\n ret['comment'] = 'No EC2 instance with tag {} == {} found'.format(tag_name,\n tag_value)\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n if len(r) > 1:\n ret['comment'] = 'Multiple EC2 instances with tag {} == {} found'.format(\n tag_name, tag_value)\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n instance = r[0]\n res = getattr(instance, instance_attr, None)\n if res:\n log.debug('Found %s %s for instance %s', instance_attr, res, instance.id)\n fixed_rrs += [__salt__['boto3_route53.aws_encode'](res)]\n else:\n ret['comment'] = 'Attribute {} not found on instance {}'.format(instance_attr,\n instance.id)\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n else:\n ret['comment'] = ('Unknown RR magic value seen: {}. Please extend the '\n 'boto3_route53 state module to add support for your preferred '\n 'incantation.'.format(fields[1]))\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n else:\n # for TXT records the entry must be encapsulated in quotes as required by the API\n # this appears to be incredibly difficult with the jinja templating engine\n # so inject the quotations here to make a viable ChangeBatch\n if Type == 'TXT':\n rr = '\"{}\"'.format(rr)\n fixed_rrs += [rr]\n ResourceRecords = [{'Value': rr} for rr in sorted(fixed_rrs)]\n\n recordsets = __salt__['boto3_route53.get_resource_records'](HostedZoneId=HostedZoneId,\n StartRecordName=Name, StartRecordType=Type, region=region, key=key, keyid=keyid,\n profile=profile)\n\n if SetIdentifier and recordsets:\n log.debug('Filter recordsets %s by SetIdentifier %s.', recordsets, SetIdentifier)\n recordsets = [r for r in recordsets if r.get('SetIdentifier') == SetIdentifier]\n log.debug('Resulted in recordsets %s.', recordsets)\n\n create = False\n update = False\n updatable = ['SetIdentifier', 'Weight', 'Region', 'GeoLocation', 'Failover', 'TTL',\n 'AliasTarget', 'HealthCheckId', 'TrafficPolicyInstanceId']\n if not recordsets:\n create = True\n if __opts__['test']:\n ret['comment'] = 'Route 53 resource record {} with type {} would be added.'.format(\n Name, Type)\n ret['result'] = None\n return ret\n elif len(recordsets) > 1:\n ret['comment'] = 'Given criteria matched more than one ResourceRecordSet.'\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n else:\n rrset = recordsets[0]\n for u in updatable:\n if locals().get(u) != rrset.get(u):\n update = True\n break\n if rrset.get('ResourceRecords') is not None:\n if ResourceRecords != sorted(rrset.get('ResourceRecords', {}), key=lambda x: x['Value']):\n update = True\n elif (AliasTarget is not None) and (rrset.get('AliasTarget') is not None):\n if sorted(AliasTarget) != sorted(rrset.get('AliasTarget')):\n update = True\n\n if not create and not update:\n ret['comment'] = ('Route 53 resource record {} with type {} is already in the desired state.'\n ''.format(Name, Type))\n log.info(ret['comment'])\n return ret\n else:\n if __opts__['test']:\n ret['comment'] = 'Route 53 resource record {} with type {} would be updated.'.format(\n Name, Type)\n ret['result'] = None\n return ret\n ResourceRecordSet = {\n 'Name': Name,\n 'Type': Type\n }\n if ResourceRecords:\n ResourceRecordSet['ResourceRecords'] = ResourceRecords\n for u in updatable:\n if locals().get(u) or (locals().get(u) == 0):\n ResourceRecordSet.update({u: locals().get(u)})\n else:\n log.debug('Not updating ResourceRecordSet with local value: %s', locals().get(u))\n\n ChangeBatch = {\n 'Changes': [\n {\n 'Action': 'UPSERT',\n 'ResourceRecordSet': ResourceRecordSet,\n }\n ]\n }\n\n if __salt__['boto3_route53.change_resource_record_sets'](HostedZoneId=HostedZoneId,\n ChangeBatch=ChangeBatch, region=region, key=key, keyid=keyid, profile=profile):\n ret['comment'] = 'Route 53 resource record {} with type {} {}.'.format(Name,\n Type, 'created' if create else 'updated')\n log.info(ret['comment'])\n if create:\n ret['changes']['old'] = None\n else:\n ret['changes']['old'] = rrset\n ret['changes']['new'] = ResourceRecordSet\n else:\n ret['comment'] = 'Failed to {} Route 53 resource record {} with type {}.'.format(\n 'create' if create else 'update', Name, Type)\n log.error(ret['comment'])\n ret['result'] = False\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef rr_absent(name, HostedZoneId=None, DomainName=None, PrivateZone=False,\n Name=None, Type=None, SetIdentifier=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure the Route53 record is deleted.\n\n name\n The name of the state definition. This will be used for Name if the latter is\n not provided.\n\n HostedZoneId\n The ID of the zone to delete the record from. Exclusive with DomainName.\n\n DomainName\n The domain name of the zone to delete the record from. Exclusive with HostedZoneId.\n\n PrivateZone\n Set to True if the RR to be removed is in a private zone, False if public.\n\n Name\n Name of the resource record.\n\n Type\n The record type (A, NS, MX, TXT, etc.)\n\n SetIdentifier\n Valid for Weighted, Latency, Geolocation, and Failover resource record sets only.\n An identifier that differentiates among multiple resource record sets that have the same\n combination of DNS name and type. The value of SetIdentifier must be unique for each\n resource record set that has the same combination of DNS name and type. Omit SetIdentifier\n for any other types of record sets.\n\n region\n The region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n '''\n Name = Name if Name else name\n\n if Type is None:\n raise SaltInvocationError(\"'Type' is a required parameter when deleting resource records.\")\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n args = {'Id': HostedZoneId, 'Name': DomainName, 'PrivateZone': PrivateZone,\n 'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n zone = __salt__['boto3_route53.find_hosted_zone'](**args)\n if not zone:\n ret['comment'] = 'Route 53 {} hosted zone {} not found'.format('private' if PrivateZone\n else 'public', DomainName)\n log.info(ret['comment'])\n return ret\n zone = zone[0]\n HostedZoneId = zone['HostedZone']['Id']\n\n recordsets = __salt__['boto3_route53.get_resource_records'](HostedZoneId=HostedZoneId,\n StartRecordName=Name, StartRecordType=Type, region=region, key=key, keyid=keyid,\n profile=profile)\n if SetIdentifier and recordsets:\n log.debug('Filter recordsets %s by SetIdentifier %s.', recordsets, SetIdentifier)\n recordsets = [r for r in recordsets if r.get('SetIdentifier') == SetIdentifier]\n log.debug('Resulted in recordsets %s.', recordsets)\n if not recordsets:\n ret['comment'] = 'Route 53 resource record {} with type {} already absent.'.format(\n Name, Type)\n return ret\n elif len(recordsets) > 1:\n ret['comment'] = 'Given criteria matched more than one ResourceRecordSet.'\n log.error(ret['comment'])\n ret['result'] = False\n return ret\n ResourceRecordSet = recordsets[0]\n if __opts__['test']:\n ret['comment'] = 'Route 53 resource record {} with type {} would be deleted.'.format(\n Name, Type)\n ret['result'] = None\n return ret\n\n ChangeBatch = {\n 'Changes': [\n {\n 'Action': 'DELETE',\n 'ResourceRecordSet': ResourceRecordSet,\n }\n ]\n }\n\n if __salt__['boto3_route53.change_resource_record_sets'](HostedZoneId=HostedZoneId,\n ChangeBatch=ChangeBatch, region=region, key=key, keyid=keyid, profile=profile):\n ret['comment'] = 'Route 53 resource record {} with type {} deleted.'.format(Name, Type)\n log.info(ret['comment'])\n ret['changes']['old'] = ResourceRecordSet\n ret['changes']['new'] = None\n else:\n ret['comment'] = 'Failed to delete Route 53 resource record {} with type {}.'.format(Name,\n Type)\n log.error(ret['comment'])\n ret['result'] = False\n\n return ret", "response": "Ensure a Route53 record is absent in a Route53 state definition."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_module_environment(env=None, function=None):\n '''\n Get module optional environment.\n\n To setup an environment option for a particular module,\n add either pillar or config at the minion as follows:\n\n system-environment:\n modules:\n pkg:\n _:\n LC_ALL: en_GB.UTF-8\n FOO: bar\n install:\n HELLO: world\n states:\n pkg:\n _:\n LC_ALL: en_US.Latin-1\n NAME: Fred\n\n So this will export the environment to all the modules,\n states, returnes etc. And calling this function with the globals()\n in that context will fetch the environment for further reuse.\n\n Underscore '_' exports environment for all functions within the module.\n If you want to specifially export environment only for one function,\n specify it as in the example above \"install\".\n\n First will be fetched configuration, where virtual name goes first,\n then the physical name of the module overrides the virtual settings.\n Then pillar settings will override the configuration in the same order.\n\n :param env:\n :param function: name of a particular function\n :return: dict\n '''\n result = {}\n if not env:\n env = {}\n for env_src in [env.get('__opts__', {}), env.get('__pillar__', {})]:\n fname = env.get('__file__', '')\n physical_name = os.path.basename(fname).split('.')[0]\n section = os.path.basename(os.path.dirname(fname))\n m_names = [env.get('__virtualname__')]\n if physical_name not in m_names:\n m_names.append(physical_name)\n for m_name in m_names:\n if not m_name:\n continue\n result.update(env_src.get('system-environment', {}).get(\n section, {}).get(m_name, {}).get('_', {}).copy())\n if function is not None:\n result.update(env_src.get('system-environment', {}).get(\n section, {}).get(m_name, {}).get(function, {}).copy())\n\n return result", "response": "Get the environment for a particular module."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _cmd_run(cmd):\n '''\n Run the aptly command.\n\n :return: The string output of the command.\n :rtype: str\n '''\n cmd.insert(0, 'aptly')\n cmd_ret = __salt__['cmd.run_all'](cmd, ignore_retcode=True)\n\n if cmd_ret['retcode'] != 0:\n log.debug('Unable to execute command: %s\\nError: %s', cmd,\n cmd_ret['stderr'])\n\n return cmd_ret['stdout']", "response": "Run the aptly command and return the output of the command."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _convert_to_closest_type(value):\n '''\n Convert string value to it's closest non-string analog, if possible.\n\n :param dict value: The string value to attempt conversion on.\n\n :return: The converted value.\n :rtype: Union[bool, int, None, str]\n '''\n value = salt.utils.stringutils.to_bool(value.strip())\n\n if isinstance(value, bool):\n return value\n\n return salt.utils.stringutils.to_none(salt.utils.stringutils.to_num(value))", "response": "Convert string value to it s closest non - string analog."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a list of arguments to be passed to the aptly command line", "response": "def _format_repo_args(comment=None, component=None, distribution=None,\n uploaders_file=None, saltenv='base'):\n '''\n Format the common arguments for creating or editing a repository.\n\n :param str comment: The description of the repository.\n :param str component: The default component to use when publishing.\n :param str distribution: The default distribution to use when publishing.\n :param str uploaders_file: The repository upload restrictions config.\n :param str saltenv: The environment the file resides in.\n\n :return: A list of the arguments formatted as aptly arguments.\n :rtype: list\n '''\n ret = list()\n cached_uploaders_path = None\n settings = {'comment': comment, 'component': component,\n 'distribution': distribution}\n\n if uploaders_file:\n cached_uploaders_path = __salt__['cp.cache_file'](uploaders_file, saltenv)\n\n if not cached_uploaders_path:\n log.error('Unable to get cached copy of file: %s', uploaders_file)\n return False\n\n for setting in settings:\n if settings[setting] is not None:\n ret.append('-{}={}'.format(setting, settings[setting]))\n\n if cached_uploaders_path:\n ret.append('-uploaders-file={}'.format(cached_uploaders_path))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _parse_show_output(cmd_ret):\n '''\n Parse the output of an aptly show command.\n\n :param str cmd_ret: The text of the command output that needs to be parsed.\n\n :return: A dictionary containing the configuration data.\n :rtype: dict\n '''\n parsed_data = dict()\n list_key = None\n\n for line in cmd_ret.splitlines():\n # Skip empty lines.\n if not line.strip():\n continue\n\n # If there are indented lines below an existing key, assign them as a list value\n # for that key.\n if not salt.utils.stringutils.contains_whitespace(line[0]):\n list_key = None\n\n if list_key:\n list_value = _convert_to_closest_type(line)\n parsed_data.setdefault(list_key, []).append(list_value)\n continue\n\n items = [item.strip() for item in line.split(':', 1)]\n\n key = items[0].lower()\n key = ' '.join(key.split()).replace(' ', '_')\n\n # Track the current key so that we can use it in instances where the values\n # appear on subsequent lines of the output.\n list_key = key\n\n try:\n value = items[1]\n except IndexError:\n # If the line doesn't have the separator or is otherwise invalid, skip it.\n log.debug('Skipping line: %s', line)\n continue\n\n if value:\n parsed_data[key] = _convert_to_closest_type(value)\n\n return _convert_parsed_show_output(parsed_data=parsed_data)", "response": "Parse the output of an aptly show command."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconverts matching string values to lists and dictionaries.", "response": "def _convert_parsed_show_output(parsed_data):\n '''\n Convert matching string values to lists/dictionaries.\n\n :param dict parsed_data: The text of the command output that needs to be parsed.\n\n :return: A dictionary containing the modified configuration data.\n :rtype: dict\n '''\n # Match lines like \"main: xenial [snapshot]\" or \"test [local]\".\n source_pattern = re.compile(r'(?:(?P\\S+):)?\\s*(?P\\S+)\\s+\\[(?P\\S+)\\]')\n sources = list()\n\n if 'architectures' in parsed_data:\n parsed_data['architectures'] = [item.strip() for item in parsed_data['architectures'].split()]\n parsed_data['architectures'] = sorted(parsed_data['architectures'])\n\n for source in parsed_data.get('sources', []):\n # Retain the key/value of only the matching named groups.\n matches = source_pattern.search(source)\n\n if matches:\n groups = matches.groupdict()\n sources.append({key: groups[key] for key in groups if groups[key]})\n if sources:\n parsed_data['sources'] = sources\n\n return parsed_data"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _validate_config(config_path):\n '''\n Validate that the configuration file exists and is readable.\n\n :param str config_path: The path to the configuration file for the aptly instance.\n\n :return: None\n :rtype: None\n '''\n log.debug('Checking configuration file: %s', config_path)\n\n if not os.path.isfile(config_path):\n message = 'Unable to get configuration file: {}'.format(config_path)\n log.error(message)\n raise SaltInvocationError(message)\n log.debug('Found configuration file: %s', config_path)", "response": "Validate that the configuration file exists and is readable."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_config(config_path=_DEFAULT_CONFIG_PATH):\n '''\n Get the configuration data.\n\n :param str config_path: The path to the configuration file for the aptly instance.\n\n :return: A dictionary containing the configuration data.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.get_config\n '''\n _validate_config(config_path)\n\n cmd = ['config', 'show', '-config={}'.format(config_path)]\n\n cmd_ret = _cmd_run(cmd)\n\n return salt.utils.json.loads(cmd_ret)", "response": "Get the configuration data for the Aptly instance."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nlists all of the local package repositories.", "response": "def list_repos(config_path=_DEFAULT_CONFIG_PATH, with_packages=False):\n '''\n List all of the local package repositories.\n\n :param str config_path: The path to the configuration file for the aptly instance.\n :param bool with_packages: Return a list of packages in the repo.\n\n :return: A dictionary of the repositories.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.list_repos\n '''\n _validate_config(config_path)\n\n ret = dict()\n cmd = ['repo', 'list', '-config={}'.format(config_path), '-raw=true']\n\n cmd_ret = _cmd_run(cmd)\n repos = [line.strip() for line in cmd_ret.splitlines()]\n\n log.debug('Found repositories: %s', len(repos))\n\n for name in repos:\n ret[name] = get_repo(name=name, config_path=config_path,\n with_packages=with_packages)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting detailed information about a local package repository.", "response": "def get_repo(name, config_path=_DEFAULT_CONFIG_PATH, with_packages=False):\n '''\n Get detailed information about a local package repository.\n\n :param str name: The name of the local repository.\n :param str config_path: The path to the configuration file for the aptly instance.\n :param bool with_packages: Return a list of packages in the repo.\n\n :return: A dictionary containing information about the repository.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.get_repo name=\"test-repo\"\n '''\n _validate_config(config_path)\n with_packages = six.text_type(bool(with_packages)).lower()\n\n ret = dict()\n cmd = ['repo', 'show', '-config={}'.format(config_path),\n '-with-packages={}'.format(with_packages), name]\n\n cmd_ret = _cmd_run(cmd)\n\n ret = _parse_show_output(cmd_ret=cmd_ret)\n\n if ret:\n log.debug('Found repository: %s', name)\n else:\n log.debug('Unable to find repository: %s', name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef new_repo(name, config_path=_DEFAULT_CONFIG_PATH, comment=None, component=None,\n distribution=None, uploaders_file=None, from_snapshot=None,\n saltenv='base'):\n '''\n Create a new local package repository.\n\n :param str name: The name of the local repository.\n :param str config_path: The path to the configuration file for the aptly instance.\n :param str comment: The description of the repository.\n :param str component: The default component to use when publishing.\n :param str distribution: The default distribution to use when publishing.\n :param str uploaders_file: The repository upload restrictions config.\n :param str from_snapshot: The snapshot to initialize the repository contents from.\n :param str saltenv: The environment the file resides in.\n\n :return: A boolean representing whether all changes succeeded.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.new_repo name=\"test-repo\" comment=\"Test main repo\" component=\"main\" distribution=\"trusty\"\n '''\n _validate_config(config_path)\n\n current_repo = __salt__['aptly.get_repo'](name=name, config_path=config_path)\n\n if current_repo:\n log.debug('Repository already exists: %s', name)\n return True\n\n cmd = ['repo', 'create', '-config={}'.format(config_path)]\n repo_params = _format_repo_args(comment=comment, component=component,\n distribution=distribution,\n uploaders_file=uploaders_file, saltenv=saltenv)\n cmd.extend(repo_params)\n cmd.append(name)\n\n if from_snapshot:\n cmd.extend(['from', 'snapshot', from_snapshot])\n\n _cmd_run(cmd)\n repo = __salt__['aptly.get_repo'](name=name, config_path=config_path)\n\n if repo:\n log.debug('Created repo: %s', name)\n return True\n log.error('Unable to create repo: %s', name)\n return False", "response": "Create a new local package repository."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconfiguring the settings for a local package repository.", "response": "def set_repo(name, config_path=_DEFAULT_CONFIG_PATH, comment=None, component=None,\n distribution=None, uploaders_file=None, saltenv='base'):\n '''\n Configure the settings for a local package repository.\n\n :param str name: The name of the local repository.\n :param str config_path: The path to the configuration file for the aptly instance.\n :param str comment: The description of the repository.\n :param str component: The default component to use when publishing.\n :param str distribution: The default distribution to use when publishing.\n :param str uploaders_file: The repository upload restrictions config.\n :param str from_snapshot: The snapshot to initialize the repository contents from.\n :param str saltenv: The environment the file resides in.\n\n :return: A boolean representing whether all changes succeeded.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.set_repo name=\"test-repo\" comment=\"Test universe repo\" component=\"universe\" distribution=\"xenial\"\n '''\n _validate_config(config_path)\n\n failed_settings = dict()\n\n # Only check for settings that were passed in and skip the rest.\n settings = {'comment': comment, 'component': component,\n 'distribution': distribution}\n\n for setting in list(settings):\n if settings[setting] is None:\n settings.pop(setting, None)\n\n current_settings = __salt__['aptly.get_repo'](name=name, config_path=config_path)\n\n if not current_settings:\n log.error('Unable to get repo: %s', name)\n return False\n\n # Discard any additional settings that get_repo gives\n # us that are not present in the provided arguments.\n for current_setting in list(current_settings):\n if current_setting not in settings:\n current_settings.pop(current_setting, None)\n\n # Check the existing repo settings to see if they already have the desired values.\n if settings == current_settings:\n log.debug('Settings already have the desired values for repository: %s', name)\n return True\n\n cmd = ['repo', 'edit', '-config={}'.format(config_path)]\n\n repo_params = _format_repo_args(comment=comment, component=component,\n distribution=distribution,\n uploaders_file=uploaders_file, saltenv=saltenv)\n cmd.extend(repo_params)\n cmd.append(name)\n\n _cmd_run(cmd)\n new_settings = __salt__['aptly.get_repo'](name=name, config_path=config_path)\n\n # Check the new repo settings to see if they have the desired values.\n for setting in settings:\n if settings[setting] != new_settings[setting]:\n failed_settings.update({setting: settings[setting]})\n\n if failed_settings:\n log.error('Unable to change settings for the repository: %s', name)\n return False\n log.debug('Settings successfully changed to the desired values for repository: %s', name)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete_repo(name, config_path=_DEFAULT_CONFIG_PATH, force=False):\n '''\n Remove a local package repository.\n\n :param str name: The name of the local repository.\n :param str config_path: The path to the configuration file for the aptly instance.\n :param bool force: Whether to remove the repository even if it is used as the source\n of an existing snapshot.\n\n :return: A boolean representing whether all changes succeeded.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.delete_repo name=\"test-repo\"\n '''\n _validate_config(config_path)\n force = six.text_type(bool(force)).lower()\n\n current_repo = __salt__['aptly.get_repo'](name=name, config_path=config_path)\n\n if not current_repo:\n log.debug('Repository already absent: %s', name)\n return True\n\n cmd = ['repo', 'drop', '-config={}'.format(config_path),\n '-force={}'.format(force), name]\n\n _cmd_run(cmd)\n repo = __salt__['aptly.get_repo'](name=name, config_path=config_path)\n\n if repo:\n log.error('Unable to remove repo: %s', name)\n return False\n log.debug('Removed repo: %s', name)\n return True", "response": "Remove a local package repository."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nlist all the mirrored remote repositories.", "response": "def list_mirrors(config_path=_DEFAULT_CONFIG_PATH):\n '''\n Get a list of all the mirrored remote repositories.\n\n :param str config_path: The path to the configuration file for the aptly instance.\n\n :return: A list of the mirror names.\n :rtype: list\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.list_mirrors\n '''\n _validate_config(config_path)\n\n cmd = ['mirror', 'list', '-config={}'.format(config_path), '-raw=true']\n\n cmd_ret = _cmd_run(cmd)\n ret = [line.strip() for line in cmd_ret.splitlines()]\n\n log.debug('Found mirrors: %s', len(ret))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget detailed information about a mirrored remote repository.", "response": "def get_mirror(name, config_path=_DEFAULT_CONFIG_PATH, with_packages=False):\n '''\n Get detailed information about a mirrored remote repository.\n\n :param str name: The name of the remote repository mirror.\n :param str config_path: The path to the configuration file for the aptly instance.\n :param bool with_packages: Return a list of packages in the repo.\n\n :return: A dictionary containing information about the mirror.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.get_mirror name=\"test-mirror\"\n '''\n _validate_config(config_path)\n\n ret = dict()\n cmd = ['mirror', 'show', '-config={}'.format(config_path),\n '-with-packages={}'.format(str(with_packages).lower()),\n name]\n\n cmd_ret = _cmd_run(cmd)\n\n ret = _parse_show_output(cmd_ret=cmd_ret)\n\n if ret:\n log.debug('Found mirror: %s', name)\n else:\n log.debug('Unable to find mirror: %s', name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete_mirror(name, config_path=_DEFAULT_CONFIG_PATH, force=False):\n '''\n Remove a mirrored remote repository. By default, Package data is not removed.\n\n :param str name: The name of the remote repository mirror.\n :param str config_path: The path to the configuration file for the aptly instance.\n :param bool force: Whether to remove the mirror even if it is used as the source\n of an existing snapshot.\n\n :return: A boolean representing whether all changes succeeded.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.delete_mirror name=\"test-mirror\"\n '''\n _validate_config(config_path)\n force = six.text_type(bool(force)).lower()\n\n current_mirror = __salt__['aptly.get_mirror'](name=name, config_path=config_path)\n\n if not current_mirror:\n log.debug('Mirror already absent: %s', name)\n return True\n\n cmd = ['mirror', 'drop', '-config={}'.format(config_path),\n '-force={}'.format(force), name]\n\n _cmd_run(cmd)\n mirror = __salt__['aptly.get_mirror'](name=name, config_path=config_path)\n\n if mirror:\n log.error('Unable to remove mirror: %s', name)\n return False\n log.debug('Removed mirror: %s', name)\n return True", "response": "Remove a mirrored remote repository. By default Package data is not removed."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nlists all the published repositories.", "response": "def list_published(config_path=_DEFAULT_CONFIG_PATH):\n '''\n Get a list of all the published repositories.\n\n :param str config_path: The path to the configuration file for the aptly instance.\n\n :return: A list of the published repositories.\n :rtype: list\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.list_published\n '''\n _validate_config(config_path)\n\n ret = list()\n cmd = ['publish', 'list', '-config={}'.format(config_path), '-raw=true']\n\n cmd_ret = _cmd_run(cmd)\n\n for line in cmd_ret.splitlines():\n items = [item.strip() for item in line.split(' ', 1)]\n ret.append({\n 'distribution': items[1],\n 'prefix': items[0]\n })\n\n log.debug('Found published repositories: %s', len(ret))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting the details of a published repository.", "response": "def get_published(name, config_path=_DEFAULT_CONFIG_PATH, endpoint='', prefix=None):\n '''\n Get the details of a published repository.\n\n :param str name: The distribution name of the published repository.\n :param str config_path: The path to the configuration file for the aptly instance.\n :param str endpoint: The publishing endpoint.\n :param str prefix: The prefix for publishing.\n\n :return: A dictionary containing information about the published repository.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.get_published name=\"test-dist\"\n '''\n _validate_config(config_path)\n\n ret = dict()\n sources = list()\n cmd = ['publish', 'show', '-config={}'.format(config_path), name]\n\n if prefix:\n cmd.append('{}:{}'.format(endpoint, prefix))\n\n cmd_ret = _cmd_run(cmd)\n\n ret = _parse_show_output(cmd_ret=cmd_ret)\n\n if ret:\n log.debug('Found published repository: %s', name)\n else:\n log.debug('Unable to find published repository: %s', name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndelete a published repository.", "response": "def delete_published(name, config_path=_DEFAULT_CONFIG_PATH, endpoint='', prefix=None,\n skip_cleanup=False, force=False):\n '''\n Remove files belonging to a published repository. Aptly tries to remove as many files\n belonging to this repository as possible.\n\n :param str name: The distribution name of the published repository.\n :param str config_path: The path to the configuration file for the aptly instance.\n :param str endpoint: The publishing endpoint.\n :param str prefix: The prefix for publishing.\n :param bool skip_cleanup: Whether to remove unreferenced files.\n :param bool force: Whether to remove the published repository even if component\n cleanup fails.\n\n :return: A boolean representing whether all changes succeeded.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.delete_published name=\"test-dist\"\n '''\n _validate_config(config_path)\n force = six.text_type(bool(force)).lower()\n skip_cleanup = six.text_type(bool(skip_cleanup)).lower()\n\n current_published = __salt__['aptly.get_published'](name=name, config_path=config_path,\n endpoint=endpoint, prefix=prefix)\n\n if not current_published:\n log.debug('Published repository already absent: %s', name)\n return True\n\n cmd = ['publish', 'drop', '-config={}'.format(config_path),\n '-force-drop={}'.format(force), '-skip-cleanup={}'.format(skip_cleanup),\n name]\n\n if prefix:\n cmd.append('{}:{}'.format(endpoint, prefix))\n\n _cmd_run(cmd)\n published = __salt__['aptly.get_published'](name=name, config_path=config_path,\n endpoint=endpoint, prefix=prefix)\n\n if published:\n log.error('Unable to remove published snapshot: %s', name)\n return False\n log.debug('Removed published snapshot: %s', name)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list_snapshots(config_path=_DEFAULT_CONFIG_PATH, sort_by_time=False):\n '''\n Get a list of all the existing snapshots.\n\n :param str config_path: The path to the configuration file for the aptly instance.\n :param bool sort_by_time: Whether to sort by creation time instead of by name.\n\n :return: A list of the snapshot names.\n :rtype: list\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.list_snapshots\n '''\n _validate_config(config_path)\n\n cmd = ['snapshot', 'list', '-config={}'.format(config_path), '-raw=true']\n\n if sort_by_time:\n cmd.append('-sort=time')\n else:\n cmd.append('-sort=name')\n\n cmd_ret = _cmd_run(cmd)\n ret = [line.strip() for line in cmd_ret.splitlines()]\n\n log.debug('Found snapshots: %s', len(ret))\n return ret", "response": "Get a list of all the existing snapshots."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets detailed information about a snapshot.", "response": "def get_snapshot(name, config_path=_DEFAULT_CONFIG_PATH, with_packages=False):\n '''\n Get detailed information about a snapshot.\n\n :param str name: The name of the snapshot given during snapshot creation.\n :param str config_path: The path to the configuration file for the aptly instance.\n :param bool with_packages: Return a list of packages in the snapshot.\n\n :return: A dictionary containing information about the snapshot.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.get_snapshot name=\"test-repo\"\n '''\n _validate_config(config_path)\n\n sources = list()\n\n cmd = ['snapshot', 'show', '-config={}'.format(config_path),\n '-with-packages={}'.format(str(with_packages).lower()),\n name]\n\n cmd_ret = _cmd_run(cmd)\n\n ret = _parse_show_output(cmd_ret=cmd_ret)\n\n if ret:\n log.debug('Found shapshot: %s', name)\n else:\n log.debug('Unable to find snapshot: %s', name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nremoving information about a snapshot.", "response": "def delete_snapshot(name, config_path=_DEFAULT_CONFIG_PATH, force=False):\n '''\n Remove information about a snapshot. If a snapshot is published, it can not be\n dropped without first removing publishing for that snapshot. If a snapshot is\n used as the source for other snapshots, Aptly will refuse to remove it unless\n forced.\n\n :param str name: The name of the snapshot given during snapshot creation.\n :param str config_path: The path to the configuration file for the aptly instance.\n :param bool force: Whether to remove the snapshot even if it is used as the source\n of another snapshot.\n\n :return: A boolean representing whether all changes succeeded.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.delete_snapshot name=\"test-snapshot\"\n '''\n _validate_config(config_path)\n force = six.text_type(bool(force)).lower()\n\n current_snapshot = __salt__['aptly.get_snapshot'](name=name, config_path=config_path)\n\n if not current_snapshot:\n log.debug('Snapshot already absent: %s', name)\n return True\n\n cmd = ['snapshot', 'drop', '-config={}'.format(config_path),\n '-force={}'.format(force), name]\n\n _cmd_run(cmd)\n snapshot = __salt__['aptly.get_snapshot'](name=name, config_path=config_path)\n\n if snapshot:\n log.error('Unable to remove snapshot: %s', name)\n return False\n log.debug('Removed snapshot: %s', name)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef cleanup_db(config_path=_DEFAULT_CONFIG_PATH, dry_run=False):\n '''\n Remove data regarding unreferenced packages and delete files in the package pool that\n are no longer being used by packages.\n\n :param bool dry_run: Report potential changes without making any changes.\n\n :return: A dictionary of the package keys and files that were removed.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aptly.cleanup_db\n '''\n _validate_config(config_path)\n dry_run = six.text_type(bool(dry_run)).lower()\n\n ret = {'deleted_keys': list(),\n 'deleted_files': list()}\n\n cmd = ['db', 'cleanup', '-config={}'.format(config_path),\n '-dry-run={}'.format(dry_run), '-verbose=true']\n\n cmd_ret = _cmd_run(cmd)\n\n type_pattern = r'^List\\s+[\\w\\s]+(?P(file|key)s)[\\w\\s]+:$'\n list_pattern = r'^\\s+-\\s+(?P.*)$'\n current_block = None\n\n for line in cmd_ret.splitlines():\n if current_block:\n match = re.search(list_pattern, line)\n if match:\n package_type = 'deleted_{}'.format(current_block)\n ret[package_type].append(match.group('package'))\n else:\n current_block = None\n # Intentionally not using an else here, in case of a situation where\n # the next list header might be bordered by the previous list.\n if not current_block:\n match = re.search(type_pattern, line)\n if match:\n current_block = match.group('package_type')\n\n log.debug('Package keys identified for deletion: %s', len(ret['deleted_keys']))\n log.debug('Package files identified for deletion: %s', len(ret['deleted_files']))\n return ret", "response": "Remove data regarding unreferenced packages and delete files in the package pool."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef publish_minions(self):\n '''\n Publishes minions as a list of dicts.\n '''\n minions = []\n\n for minion, minion_info in six.iteritems(self.minions):\n curr_minion = {}\n curr_minion.update(minion_info)\n curr_minion.update({'id': minion})\n minions.append(curr_minion)\n\n ret = {'minions': minions}\n self.handler.send(salt.utils.json.dumps(ret), False)", "response": "Publishes the minions as a list of dicts."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\npublish the data to the event stream.", "response": "def publish(self, key, data):\n '''\n Publishes the data to the event stream.\n '''\n publish_data = {key: data}\n self.handler.send(salt.utils.json.dumps(publish_data), False)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nprocesses presence events from the master", "response": "def process_presence_events(self, event_data, token, opts):\n '''\n Check if any minions have connected or dropped.\n Send a message to the client if they have.\n '''\n tag = event_data['tag']\n event_info = event_data['data']\n\n minions_detected = event_info['present']\n curr_minions = six.iterkeys(self.minions)\n\n changed = False\n\n # check if any connections were dropped\n dropped_minions = set(curr_minions) - set(minions_detected)\n\n for minion in dropped_minions:\n changed = True\n self.minions.pop(minion, None)\n\n # check if any new connections were made\n new_minions = set(minions_detected) - set(curr_minions)\n\n tgt = ','.join(new_minions)\n\n if tgt:\n changed = True\n client = salt.netapi.NetapiClient(opts)\n client.run(\n {\n 'fun': 'grains.items',\n 'tgt': tgt,\n 'expr_type': 'list',\n 'mode': 'client',\n 'client': 'local',\n 'asynchronous': 'local_async',\n 'token': token,\n })\n\n if changed:\n self.publish_minions()"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nprocess events and publish data", "response": "def process(self, salt_data, token, opts):\n '''\n Process events and publish data\n '''\n parts = salt_data['tag'].split('/')\n if len(parts) < 2:\n return\n\n # TBD: Simplify these conditional expressions\n if parts[1] == 'job':\n if parts[3] == 'new':\n self.process_new_job_event(salt_data)\n if salt_data['data']['fun'] == 'grains.items':\n self.minions = {}\n elif parts[3] == 'ret':\n self.process_ret_job_event(salt_data)\n if salt_data['data']['fun'] == 'grains.items':\n self.process_minion_update(salt_data)\n if parts[1] == 'key':\n self.process_key_event(salt_data)\n if parts[1] == 'presence':\n self.process_presence_events(salt_data, token, opts)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nensuring that a distinguished name list exists with the items provided.", "response": "def distinguished_name_list_exists(name, items):\n '''\n Ensures that a distinguished name list exists with the items provided.\n\n name: The name of the module function to execute.\n\n name(str): The name of the distinguished names list.\n\n items(list): A list of items to ensure exist on the distinguished names list.\n\n SLS Example:\n\n .. code-block:: yaml\n\n MyDistinguishedNameList:\n bluecoat_sslv.distinguished_name_list_exists:\n items:\n - cn=test.com\n - cn=othersite.com\n\n '''\n ret = _default_ret(name)\n req_change = False\n try:\n existing_lists = __salt__['bluecoat_sslv.get_distinguished_name_lists']()\n if name not in existing_lists:\n __salt__['bluecoat_sslv.add_distinguished_name_list'](name)\n req_change = True\n list_members = __salt__['bluecoat_sslv.get_distinguished_name_list'](name)\n for item in items:\n if item not in list_members:\n __salt__['bluecoat_sslv.add_distinguished_name'](name, item)\n req_change = True\n if req_change:\n ret['changes']['before'] = list_members\n ret['changes']['after'] = __salt__['bluecoat_sslv.get_distinguished_name_list'](name)\n ret['comment'] = \"Updated distinguished name list.\"\n else:\n ret['comment'] = \"No changes required.\"\n except salt.exceptions.CommandExecutionError as err:\n ret['result'] = False\n ret['comment'] = err\n log.error(err)\n return ret\n ret['result'] = True\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting details about an alarm.", "response": "def get_alarm(name, region=None, key=None, keyid=None, profile=None):\n '''\n Get alarm details. Also can be used to check to see if an alarm exists.\n\n CLI example::\n\n salt myminion boto_cloudwatch.get_alarm myalarm region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n alarms = conn.describe_alarms(alarm_names=[name])\n if not alarms:\n return None\n if len(alarms) > 1:\n log.error(\"multiple alarms matched name '%s'\", name)\n return _metric_alarm_to_dict(alarms[0])"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _safe_dump(data):\n '''\n this presenter magic makes yaml.safe_dump\n work with the objects returned from\n boto.describe_alarms()\n '''\n custom_dumper = __utils__['yaml.get_dumper']('SafeOrderedDumper')\n\n def boto_listelement_presenter(dumper, data):\n return dumper.represent_list(list(data))\n\n yaml.add_representer(boto.ec2.cloudwatch.listelement.ListElement,\n boto_listelement_presenter,\n Dumper=custom_dumper)\n\n def dimension_presenter(dumper, data):\n return dumper.represent_dict(dict(data))\n\n yaml.add_representer(boto.ec2.cloudwatch.dimension.Dimension,\n dimension_presenter, Dumper=custom_dumper)\n\n return __utils__['yaml.dump'](data, Dumper=custom_dumper)", "response": "This presenter magic makes yaml. safe_dump"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_all_alarms(region=None, prefix=None, key=None, keyid=None,\n profile=None):\n '''\n Get all alarm details. Produces results that can be used to create an sls\n file.\n\n If prefix parameter is given, alarm names in the output will be prepended\n with the prefix; alarms that have the prefix will be skipped. This can be\n used to convert existing alarms to be managed by salt, as follows:\n\n 1. Make a \"backup\" of all existing alarms\n $ salt-call boto_cloudwatch.get_all_alarms --out=txt | sed \"s/local: //\" > legacy_alarms.sls\n\n 2. Get all alarms with new prefixed names\n $ salt-call boto_cloudwatch.get_all_alarms \"prefix=**MANAGED BY SALT** \" --out=txt | sed \"s/local: //\" > managed_alarms.sls\n\n 3. Insert the managed alarms into cloudwatch\n $ salt-call state.template managed_alarms.sls\n\n 4. Manually verify that the new alarms look right\n\n 5. Delete the original alarms\n $ sed s/present/absent/ legacy_alarms.sls > remove_legacy_alarms.sls\n $ salt-call state.template remove_legacy_alarms.sls\n\n 6. Get all alarms again, verify no changes\n $ salt-call boto_cloudwatch.get_all_alarms --out=txt | sed \"s/local: //\" > final_alarms.sls\n $ diff final_alarms.sls managed_alarms.sls\n\n CLI example::\n\n salt myminion boto_cloudwatch.get_all_alarms region=us-east-1 --out=txt\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n alarms = conn.describe_alarms()\n results = odict.OrderedDict()\n for alarm in alarms:\n alarm = _metric_alarm_to_dict(alarm)\n name = alarm[\"name\"]\n if prefix:\n if name.startswith(prefix):\n continue\n name = prefix + alarm[\"name\"]\n del alarm[\"name\"]\n alarm_sls = [{\"name\": name}, {\"attributes\": alarm}]\n results[\"manage alarm \" + name] = {\"boto_cloudwatch_alarm.present\":\n alarm_sls}\n return _safe_dump(results)", "response": "Get all alarms in a given region optionally with a specific prefix."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating or update a CloudWatch alarm.", "response": "def create_or_update_alarm(\n connection=None, name=None, metric=None, namespace=None,\n statistic=None, comparison=None, threshold=None, period=None,\n evaluation_periods=None, unit=None, description='',\n dimensions=None, alarm_actions=None,\n insufficient_data_actions=None, ok_actions=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Create or update a cloudwatch alarm.\n\n Params are the same as:\n https://boto.readthedocs.io/en/latest/ref/cloudwatch.html#boto.ec2.cloudwatch.alarm.MetricAlarm.\n\n Dimensions must be a dict. If the value of Dimensions is a string, it will\n be json decoded to produce a dict. alarm_actions, insufficient_data_actions,\n and ok_actions must be lists of string. If the passed-in value is a string,\n it will be split on \",\" to produce a list. The strings themselves for\n alarm_actions, insufficient_data_actions, and ok_actions must be Amazon\n resource names (ARN's); however, this method also supports an arn lookup\n notation, as follows:\n\n arn:aws:.... ARN as per http://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html\n scaling_policy:: The named autoscale group scaling policy, for the named group (e.g. scaling_policy:my-asg:ScaleDown)\n\n This is convenient for setting up autoscaling as follows. First specify a\n boto_asg.present state for an ASG with scaling_policies, and then set up\n boto_cloudwatch_alarm.present states which have alarm_actions that\n reference the scaling_policy.\n\n CLI example:\n\n salt myminion boto_cloudwatch.create_alarm name=myalarm ... region=us-east-1\n '''\n # clean up argument types, so that CLI works\n if threshold:\n threshold = float(threshold)\n if period:\n period = int(period)\n if evaluation_periods:\n evaluation_periods = int(evaluation_periods)\n if isinstance(dimensions, six.string_types):\n dimensions = salt.utils.json.loads(dimensions)\n if not isinstance(dimensions, dict):\n log.error(\"could not parse dimensions argument: must be json encoding of a dict: '%s'\", dimensions)\n return False\n if isinstance(alarm_actions, six.string_types):\n alarm_actions = alarm_actions.split(\",\")\n if isinstance(insufficient_data_actions, six.string_types):\n insufficient_data_actions = insufficient_data_actions.split(\",\")\n if isinstance(ok_actions, six.string_types):\n ok_actions = ok_actions.split(\",\")\n\n # convert provided action names into ARN's\n if alarm_actions:\n alarm_actions = convert_to_arn(alarm_actions,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile)\n if insufficient_data_actions:\n insufficient_data_actions = convert_to_arn(insufficient_data_actions,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile)\n if ok_actions:\n ok_actions = convert_to_arn(ok_actions,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile)\n\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n alarm = boto.ec2.cloudwatch.alarm.MetricAlarm(\n connection=connection,\n name=name,\n metric=metric,\n namespace=namespace,\n statistic=statistic,\n comparison=comparison,\n threshold=threshold,\n period=period,\n evaluation_periods=evaluation_periods,\n unit=unit,\n description=description,\n dimensions=dimensions,\n alarm_actions=alarm_actions,\n insufficient_data_actions=insufficient_data_actions,\n ok_actions=ok_actions\n )\n conn.create_alarm(alarm)\n log.info('Created/updated alarm %s', name)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting a list of strings into actual arns.", "response": "def convert_to_arn(arns, region=None, key=None, keyid=None, profile=None):\n '''\n Convert a list of strings into actual arns. Converts convenience names such\n as 'scaling_policy:...'\n\n CLI Example::\n\n salt '*' convert_to_arn 'scaling_policy:'\n '''\n results = []\n for arn in arns:\n if arn.startswith(\"scaling_policy:\"):\n _, as_group, scaling_policy_name = arn.split(\":\")\n policy_arn = __salt__[\"boto_asg.get_scaling_policy_arn\"](\n as_group, scaling_policy_name, region, key, keyid, profile\n )\n if policy_arn:\n results.append(policy_arn)\n else:\n log.error('Could not convert: %s', arn)\n else:\n results.append(arn)\n return results"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef delete_alarm(name, region=None, key=None, keyid=None, profile=None):\n '''\n Delete a cloudwatch alarm\n\n CLI example to delete a queue::\n\n salt myminion boto_cloudwatch.delete_alarm myalarm region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n conn.delete_alarms([name])\n log.info('Deleted alarm %s', name)\n return True", "response": "Delete a cloudwatch alarm\n\n CLI example to delete a queue::\n\n salt myminion boto_cloudwatch.delete_alarm myalarm region=us-east-1"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting a boto. ec2. cloudwatch. alarm. MetricAlarm into a dict. Convenience for pretty printing.", "response": "def _metric_alarm_to_dict(alarm):\n '''\n Convert a boto.ec2.cloudwatch.alarm.MetricAlarm into a dict. Convenience\n for pretty printing.\n '''\n d = odict.OrderedDict()\n fields = ['name', 'metric', 'namespace', 'statistic', 'comparison',\n 'threshold', 'period', 'evaluation_periods', 'unit',\n 'description', 'dimensions', 'alarm_actions',\n 'insufficient_data_actions', 'ok_actions']\n for f in fields:\n if hasattr(alarm, f):\n d[f] = getattr(alarm, f)\n return d"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nensuring GPG public key is present in keychain.", "response": "def present(name,\n keys=None,\n user=None,\n keyserver=None,\n gnupghome=None,\n trust=None,\n **kwargs):\n '''\n Ensure GPG public key is present in keychain\n\n name\n The unique name or keyid for the GPG public key.\n\n keys\n The keyId or keyIds to add to the GPG keychain.\n\n user\n Add GPG keys to the specified user's keychain\n\n keyserver\n The keyserver to retrieve the keys from.\n\n gnupghome\n Override GNUPG Home directory\n\n trust\n Trust level for the key in the keychain,\n ignored by default. Valid trust levels:\n expired, unknown, not_trusted, marginally,\n fully, ultimately\n\n\n '''\n\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': []}\n\n _current_keys = __salt__['gpg.list_keys'](user=user, gnupghome=gnupghome)\n\n current_keys = {}\n for key in _current_keys:\n keyid = key['keyid']\n current_keys[keyid] = {}\n current_keys[keyid]['trust'] = key['trust']\n\n if not keys:\n keys = name\n\n if isinstance(keys, six.string_types):\n keys = [keys]\n\n for key in keys:\n if key in current_keys.keys():\n if trust:\n if trust in _VALID_TRUST_VALUES:\n if current_keys[key]['trust'] != TRUST_MAP[trust]:\n # update trust level\n result = __salt__['gpg.trust_key'](keyid=key,\n trust_level=trust,\n user=user,\n )\n if 'result' in result and not result['result']:\n ret['result'] = result['result']\n ret['comment'].append(result['comment'])\n else:\n ret['comment'].append('Set trust level for {0} to {1}'.format(key, trust))\n else:\n ret['comment'].append('GPG Public Key {0} already in correct trust state'.format(key))\n else:\n ret['comment'].append('Invalid trust level {0}'.format(trust))\n\n ret['comment'].append('GPG Public Key {0} already in keychain '.format(key))\n\n else:\n result = __salt__['gpg.receive_keys'](keyserver,\n key,\n user,\n gnupghome,\n )\n if 'result' in result and not result['result']:\n ret['result'] = result['result']\n ret['comment'].append(result['comment'])\n else:\n ret['comment'].append('Adding {0} to GPG keychain'.format(name))\n\n if trust:\n if trust in _VALID_TRUST_VALUES:\n result = __salt__['gpg.trust_key'](keyid=key,\n trust_level=trust,\n user=user,\n )\n if 'result' in result and not result['result']:\n ret['result'] = result['result']\n ret['comment'].append(result['comment'])\n else:\n ret['comment'].append('Set trust level for {0} to {1}'.format(key, trust))\n else:\n ret['comment'].append('Invalid trust level {0}'.format(trust))\n\n ret['comment'] = '\\n'.join(ret['comment'])\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef absent(name,\n keys=None,\n user=None,\n gnupghome=None,\n **kwargs):\n '''\n Ensure GPG public key is absent in keychain\n\n name\n The unique name or keyid for the GPG public key.\n\n keys\n The keyId or keyIds to add to the GPG keychain.\n\n user\n Remove GPG keys from the specified user's keychain\n\n gnupghome\n Override GNUPG Home directory\n\n '''\n\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': []}\n\n _current_keys = __salt__['gpg.list_keys']()\n\n current_keys = []\n for key in _current_keys:\n current_keys.append(key['keyid'])\n\n if not keys:\n keys = name\n\n if isinstance(keys, six.string_types):\n keys = [keys]\n\n for key in keys:\n if key in current_keys:\n result = __salt__['gpg.delete_key'](key,\n user,\n gnupghome,\n )\n if 'result' in result and not result['result']:\n ret['result'] = result['result']\n ret['comment'].append(result['comment'])\n else:\n ret['comment'].append('Deleting {0} from GPG keychain'.format(name))\n else:\n ret['comment'].append('{0} not found in GPG keychain'.format(name))\n ret['comment'] = '\\n'.join(ret['comment'])\n return ret", "response": "Ensure GPG public key is absent in keychain."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef ext_pillar(minion_id, pillar, repo, branch='default', root=None):\n '''\n Extract pillar from an hg repository\n '''\n with Repo(repo) as repo:\n repo.update(branch)\n envname = 'base' if branch == 'default' else branch\n if root:\n path = os.path.normpath(os.path.join(repo.working_dir, root))\n else:\n path = repo.working_dir\n\n opts = copy.deepcopy(__opts__)\n opts['pillar_roots'][envname] = [path]\n pil = salt.pillar.Pillar(opts, __grains__, minion_id, envname)\n return pil.compile_pillar(ext=False)", "response": "Extract pillar from an hg repository"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nupdates the latest revision in the hg repository", "response": "def update(self, branch='default'):\n '''\n Ensure we are using the latest revision in the hg repository\n '''\n log.debug('Updating hg repo from hg_pillar module (pull)')\n self.repo.pull()\n log.debug('Updating hg repo from hg_pillar module (update)')\n self.repo.update(branch, clean=True)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef init(opts):\n '''\n Open the connection to the network device\n managed through netmiko.\n '''\n proxy_dict = opts.get('proxy', {})\n opts['multiprocessing'] = proxy_dict.get('multiprocessing', False)\n netmiko_connection_args = proxy_dict.copy()\n netmiko_connection_args.pop('proxytype', None)\n netmiko_device['always_alive'] = netmiko_connection_args.pop('always_alive',\n opts.get('proxy_always_alive', True))\n try:\n connection = ConnectHandler(**netmiko_connection_args)\n netmiko_device['connection'] = connection\n netmiko_device['initialized'] = True\n netmiko_device['args'] = netmiko_connection_args\n netmiko_device['up'] = True\n if not netmiko_device['always_alive']:\n netmiko_device['connection'].disconnect()\n except NetMikoTimeoutException as t_err:\n log.error('Unable to setup the netmiko connection', exc_info=True)\n except NetMikoAuthenticationException as au_err:\n log.error('Unable to setup the netmiko connection', exc_info=True)\n return True", "response": "Open the connection to the network device and set up the necessary attributes."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef alive(opts):\n '''\n Return the connection status with the network device.\n '''\n log.debug('Checking if %s is still alive', opts.get('id', ''))\n if not netmiko_device['always_alive']:\n return True\n if ping() and initialized():\n return netmiko_device['connection'].remote_conn.transport.is_alive()\n return False", "response": "Return the connection status with the network device."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncalls an arbitrary netmiko method.", "response": "def call(method, *args, **kwargs):\n '''\n Calls an arbitrary netmiko method.\n '''\n kwargs = clean_kwargs(**kwargs)\n if not netmiko_device['always_alive']:\n connection = ConnectHandler(**netmiko_device['args'])\n ret = getattr(connection, method)(*args, **kwargs)\n connection.disconnect()\n return ret\n return getattr(netmiko_device['connection'], method)(*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef exists(Name,\n region=None, key=None, keyid=None, profile=None):\n '''\n Given a trail name, check to see if the given trail exists.\n\n Returns True if the given trail exists and returns False if the given\n trail does not exist.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudtrail.exists mytrail\n\n '''\n\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n conn.get_trail_status(Name=Name)\n return {'exists': True}\n except ClientError as e:\n err = __utils__['boto3.get_error'](e)\n if e.response.get('Error', {}).get('Code') == 'TrailNotFoundException':\n return {'exists': False}\n return {'error': err}", "response": "Check to see if the given trail exists."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngives a trail name describe its properties.", "response": "def describe(Name,\n region=None, key=None, keyid=None, profile=None):\n '''\n Given a trail name describe its properties.\n\n Returns a dictionary of interesting properties.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudtrail.describe mytrail\n\n '''\n\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n trails = conn.describe_trails(trailNameList=[Name])\n if trails and trails.get('trailList'):\n keys = ('Name', 'S3BucketName', 'S3KeyPrefix',\n 'SnsTopicName', 'IncludeGlobalServiceEvents',\n 'IsMultiRegionTrail',\n 'HomeRegion', 'TrailARN',\n 'LogFileValidationEnabled', 'CloudWatchLogsLogGroupArn',\n 'CloudWatchLogsRoleArn', 'KmsKeyId')\n trail = trails['trailList'].pop()\n return {'trail': dict([(k, trail.get(k)) for k in keys])}\n else:\n return {'trail': None}\n except ClientError as e:\n err = __utils__['boto3.get_error'](e)\n if e.response.get('Error', {}).get('Code') == 'TrailNotFoundException':\n return {'trail': None}\n return {'error': __utils__['boto3.get_error'](e)}"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngives a trail name describe its properties.", "response": "def status(Name,\n region=None, key=None, keyid=None, profile=None):\n '''\n Given a trail name describe its properties.\n\n Returns a dictionary of interesting properties.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudtrail.describe mytrail\n\n '''\n\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n trail = conn.get_trail_status(Name=Name)\n if trail:\n keys = ('IsLogging', 'LatestDeliveryError', 'LatestNotificationError',\n 'LatestDeliveryTime', 'LatestNotificationTime',\n 'StartLoggingTime', 'StopLoggingTime',\n 'LatestCloudWatchLogsDeliveryError',\n 'LatestCloudWatchLogsDeliveryTime',\n 'LatestDigestDeliveryTime', 'LatestDigestDeliveryError',\n 'LatestDeliveryAttemptTime',\n 'LatestNotificationAttemptTime',\n 'LatestNotificationAttemptSucceeded',\n 'LatestDeliveryAttemptSucceeded',\n 'TimeLoggingStarted',\n 'TimeLoggingStopped')\n return {'trail': dict([(k, trail.get(k)) for k in keys])}\n else:\n return {'trail': None}\n except ClientError as e:\n err = __utils__['boto3.get_error'](e)\n if e.response.get('Error', {}).get('Code') == 'TrailNotFoundException':\n return {'trail': None}\n return {'error': __utils__['boto3.get_error'](e)}"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nlisting all trails in a region", "response": "def list(region=None, key=None, keyid=None, profile=None):\n '''\n List all trails\n\n Returns list of trails\n\n CLI Example:\n\n .. code-block:: yaml\n\n policies:\n - {...}\n - {...}\n\n '''\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n trails = conn.describe_trails()\n if not bool(trails.get('trailList')):\n log.warning('No trails found')\n return {'trails': trails.get('trailList', [])}\n except ClientError as e:\n return {'error': __utils__['boto3.get_error'](e)}"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngives a valid config update a trail.", "response": "def update(Name,\n S3BucketName, S3KeyPrefix=None,\n SnsTopicName=None,\n IncludeGlobalServiceEvents=None,\n IsMultiRegionTrail=None,\n EnableLogFileValidation=None,\n CloudWatchLogsLogGroupArn=None,\n CloudWatchLogsRoleArn=None,\n KmsKeyId=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Given a valid config, update a trail.\n\n Returns {created: true} if the trail was created and returns\n {created: False} if the trail was not created.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudtrail.update my_trail my_bucket\n\n '''\n\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n kwargs = {}\n for arg in ('S3KeyPrefix', 'SnsTopicName', 'IncludeGlobalServiceEvents',\n 'IsMultiRegionTrail',\n 'EnableLogFileValidation', 'CloudWatchLogsLogGroupArn',\n 'CloudWatchLogsRoleArn', 'KmsKeyId'):\n if locals()[arg] is not None:\n kwargs[arg] = locals()[arg]\n trail = conn.update_trail(Name=Name,\n S3BucketName=S3BucketName,\n **kwargs)\n if trail:\n log.info('The updated trail name is %s', trail['Name'])\n\n return {'updated': True, 'name': trail['Name']}\n else:\n log.warning('Trail was not created')\n return {'updated': False}\n except ClientError as e:\n return {'updated': False, 'error': __utils__['boto3.get_error'](e)}"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstart logging for a trail", "response": "def start_logging(Name,\n region=None, key=None, keyid=None, profile=None):\n '''\n Start logging for a trail\n\n Returns {started: true} if the trail was started and returns\n {started: False} if the trail was not started.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudtrail.start_logging my_trail\n\n '''\n\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n conn.start_logging(Name=Name)\n return {'started': True}\n except ClientError as e:\n return {'started': False, 'error': __utils__['boto3.get_error'](e)}"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef add_tags(Name,\n region=None, key=None, keyid=None, profile=None, **kwargs):\n '''\n Add tags to a trail\n\n Returns {tagged: true} if the trail was tagged and returns\n {tagged: False} if the trail was not tagged.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudtrail.add_tags my_trail tag_a=tag_value tag_b=tag_value\n\n '''\n\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n tagslist = []\n for k, v in six.iteritems(kwargs):\n if six.text_type(k).startswith('__'):\n continue\n tagslist.append({'Key': six.text_type(k), 'Value': six.text_type(v)})\n conn.add_tags(ResourceId=_get_trail_arn(Name,\n region=region, key=key, keyid=keyid,\n profile=profile), TagsList=tagslist)\n return {'tagged': True}\n except ClientError as e:\n return {'tagged': False, 'error': __utils__['boto3.get_error'](e)}", "response": "Add tags to a trail\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nlists tags of a trail", "response": "def list_tags(Name,\n region=None, key=None, keyid=None, profile=None):\n '''\n List tags of a trail\n\n Returns:\n tags:\n - {...}\n - {...}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudtrail.list_tags my_trail\n\n '''\n\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n rid = _get_trail_arn(Name,\n region=region, key=key, keyid=keyid,\n profile=profile)\n ret = conn.list_tags(ResourceIdList=[rid])\n tlist = ret.get('ResourceTagList', []).pop().get('TagsList')\n tagdict = {}\n for tag in tlist:\n tagdict[tag.get('Key')] = tag.get('Value')\n return {'tags': tagdict}\n except ClientError as e:\n return {'error': __utils__['boto3.get_error'](e)}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _list_distributions(\n conn,\n name=None,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n):\n '''\n Private function that returns an iterator over all CloudFront distributions.\n The caller is responsible for all boto-related error handling.\n\n name\n (Optional) Only yield the distribution with the given name\n '''\n for dl_ in conn.get_paginator('list_distributions').paginate():\n distribution_list = dl_['DistributionList']\n if 'Items' not in distribution_list:\n # If there are no items, AWS omits the `Items` key for some reason\n continue\n for partial_dist in distribution_list['Items']:\n tags = conn.list_tags_for_resource(Resource=partial_dist['ARN'])\n tags = dict(\n (kv['Key'], kv['Value']) for kv in tags['Tags']['Items']\n )\n\n id_ = partial_dist['Id']\n if 'Name' not in tags:\n log.warning('CloudFront distribution %s has no Name tag.', id_)\n continue\n distribution_name = tags.pop('Name', None)\n if name is not None and distribution_name != name:\n continue\n\n # NOTE: list_distributions() returns a DistributionList,\n # which nominally contains a list of Distribution objects.\n # However, they are mangled in that they are missing values\n # (`Logging`, `ActiveTrustedSigners`, and `ETag` keys)\n # and moreover flatten the normally nested DistributionConfig\n # attributes to the top level.\n # Hence, we must call get_distribution() to get the full object,\n # and we cache these objects to help lessen API calls.\n distribution = _cache_id(\n 'cloudfront',\n sub_resource=distribution_name,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if distribution:\n yield (distribution_name, distribution)\n continue\n\n dist_with_etag = conn.get_distribution(Id=id_)\n distribution = {\n 'distribution': dist_with_etag['Distribution'],\n 'etag': dist_with_etag['ETag'],\n 'tags': tags,\n }\n _cache_id(\n 'cloudfront',\n sub_resource=distribution_name,\n resource_id=distribution,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n yield (distribution_name, distribution)", "response": "Private function that returns an iterator over all CloudFront distributions."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_distribution(name, region=None, key=None, keyid=None, profile=None):\n '''\n Get information about a CloudFront distribution (configuration, tags) with a given name.\n\n name\n Name of the CloudFront distribution\n\n region\n Region to connect to\n\n key\n Secret key to use\n\n keyid\n Access key to use\n\n profile\n A dict with region, key, and keyid,\n or a pillar key (string) that contains such a dict.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.get_distribution name=mydistribution profile=awsprofile\n\n '''\n distribution = _cache_id(\n 'cloudfront',\n sub_resource=name,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if distribution:\n return {'result': distribution}\n\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n for _, dist in _list_distributions(\n conn,\n name=name,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n ):\n # _list_distributions should only return the one distribution\n # that we want (with the given name).\n # In case of multiple distributions with the same name tag,\n # our use of caching means list_distributions will just\n # return the first one over and over again,\n # so only the first result is useful.\n if distribution is not None:\n msg = 'More than one distribution found with name {0}'\n return {'error': msg.format(name)}\n distribution = dist\n except botocore.exceptions.ClientError as err:\n return {'error': __utils__['boto3.get_error'](err)}\n if not distribution:\n return {'result': None}\n\n _cache_id(\n 'cloudfront',\n sub_resource=name,\n resource_id=distribution,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n return {'result': distribution}", "response": "Get information about a CloudFront distribution with a given name."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef export_distributions(region=None, key=None, keyid=None, profile=None):\n '''\n Get details of all CloudFront distributions.\n Produces results that can be used to create an SLS file.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call boto_cloudfront.export_distributions --out=txt |\\\n sed \"s/local: //\" > cloudfront_distributions.sls\n\n '''\n results = OrderedDict()\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n for name, distribution in _list_distributions(\n conn,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n ):\n config = distribution['distribution']['DistributionConfig']\n tags = distribution['tags']\n\n distribution_sls_data = [\n {'name': name},\n {'config': config},\n {'tags': tags},\n ]\n results['Manage CloudFront distribution {0}'.format(name)] = {\n 'boto_cloudfront.present': distribution_sls_data,\n }\n except botocore.exceptions.ClientError as err:\n # Raise an exception, as this is meant to be user-invoked at the CLI\n # as opposed to being called from execution or state modules\n raise err\n\n dumper = __utils__['yaml.get_dumper']('IndentedSafeOrderedDumper')\n return __utils__['yaml.dump'](\n results,\n default_flow_style=False,\n Dumper=dumper,\n )", "response": "Get details of all CloudFront distributions and export them to a single SLS file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_distribution(\n name,\n config,\n tags=None,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n):\n '''\n Create a CloudFront distribution with the given name, config, and (optionally) tags.\n\n name\n Name for the CloudFront distribution\n\n config\n Configuration for the distribution\n\n tags\n Tags to associate with the distribution\n\n region\n Region to connect to\n\n key\n Secret key to use\n\n keyid\n Access key to use\n\n profile\n A dict with region, key, and keyid,\n or a pillar key (string) that contains such a dict.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.create_distribution name=mydistribution profile=awsprofile \\\n config='{\"Comment\":\"partial configuration\",\"Enabled\":true}'\n '''\n if tags is None:\n tags = {}\n if 'Name' in tags:\n # Be lenient and silently accept if names match, else error\n if tags['Name'] != name:\n return {'error': 'Must not pass `Name` in `tags` but as `name`'}\n tags['Name'] = name\n tags = {\n 'Items': [{'Key': k, 'Value': v} for k, v in six.iteritems(tags)]\n }\n\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n conn.create_distribution_with_tags(\n DistributionConfigWithTags={\n 'DistributionConfig': config,\n 'Tags': tags,\n },\n )\n _cache_id(\n 'cloudfront',\n sub_resource=name,\n invalidate=True,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n except botocore.exceptions.ClientError as err:\n return {'error': __utils__['boto3.get_error'](err)}\n\n return {'result': True}", "response": "Create a CloudFront distribution with the given name config and tags."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate the CloudFront distribution with the given name config and optionally tags.", "response": "def update_distribution(\n name,\n config,\n tags=None,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n):\n '''\n Update the config (and optionally tags) for the CloudFront distribution with the given name.\n\n name\n Name of the CloudFront distribution\n\n config\n Configuration for the distribution\n\n tags\n Tags to associate with the distribution\n\n region\n Region to connect to\n\n key\n Secret key to use\n\n keyid\n Access key to use\n\n profile\n A dict with region, key, and keyid,\n or a pillar key (string) that contains such a dict.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.update_distribution name=mydistribution profile=awsprofile \\\n config='{\"Comment\":\"partial configuration\",\"Enabled\":true}'\n '''\n ### FIXME - BUG. This function can NEVER work as written...\n ### Obviously it was never actually tested.\n distribution_ret = get_distribution(\n name,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile\n )\n if 'error' in distribution_ret:\n return distribution_ret\n dist_with_tags = distribution_ret['result']\n\n current_distribution = dist_with_tags['distribution']\n current_config = current_distribution['DistributionConfig']\n current_tags = dist_with_tags['tags']\n etag = dist_with_tags['etag']\n\n config_diff = __utils__['dictdiffer.deep_diff'](current_config, config)\n if tags:\n tags_diff = __utils__['dictdiffer.deep_diff'](current_tags, tags)\n\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n if 'old' in config_diff or 'new' in config_diff:\n conn.update_distribution(\n DistributionConfig=config,\n Id=current_distribution['Id'],\n IfMatch=etag,\n )\n if tags:\n arn = current_distribution['ARN']\n if 'new' in tags_diff:\n tags_to_add = {\n 'Items': [\n {'Key': k, 'Value': v}\n for k, v in six.iteritems(tags_diff['new'])\n ],\n }\n conn.tag_resource(\n Resource=arn,\n Tags=tags_to_add,\n )\n if 'old' in tags_diff:\n tags_to_remove = {\n 'Items': list(tags_diff['old'].keys()),\n }\n conn.untag_resource(\n Resource=arn,\n TagKeys=tags_to_remove,\n )\n except botocore.exceptions.ClientError as err:\n return {'error': __utils__['boto3.get_error'](err)}\n finally:\n _cache_id(\n 'cloudfront',\n sub_resource=name,\n invalidate=True,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n\n return {'result': True}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlist CloudFront distributions in the specified region with moderate information.", "response": "def list_distributions(region=None, key=None, keyid=None, profile=None):\n '''\n List, with moderate information, all CloudFront distributions in the bound account.\n\n region\n Region to connect to.\n\n key\n Secret key to use.\n\n keyid\n Access key to use.\n\n profile\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.list_distributions\n\n '''\n retries = 10\n sleep = 6\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n Items = []\n while retries:\n try:\n log.debug('Garnering list of CloudFront distributions')\n Marker = ''\n while Marker is not None:\n ret = conn.list_distributions(Marker=Marker)\n Items += ret.get('DistributionList', {}).get('Items', [])\n Marker = ret.get('DistributionList', {}).get('NextMarker')\n return Items\n except botocore.exceptions.ParamValidationError as err:\n raise SaltInvocationError(str(err))\n except botocore.exceptions.ClientError as err:\n if retries and err.response.get('Error', {}).get('Code') == 'Throttling':\n retries -= 1\n log.debug('Throttled by AWS API, retrying in %s seconds...', sleep)\n time.sleep(sleep)\n continue\n log.error('Failed to list CloudFront distributions: %s', err.message)\n return None"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef distribution_exists(Id, region=None, key=None, keyid=None, profile=None):\n '''\n Return True if a CloudFront distribution exists with the given Resource ID or False otherwise.\n\n Id\n Resource ID of the CloudFront distribution.\n\n region\n Region to connect to.\n\n key\n Secret key to use.\n\n keyid\n Access key to use.\n\n profile\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.distribution_exists Id=E24RBTSABCDEF0\n\n '''\n authargs = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n dists = list_distributions(**authargs) or []\n return bool([i['Id'] for i in dists if i['Id'] == Id])", "response": "Returns True if a CloudFront distribution exists with the given Resource ID or False otherwise."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_distributions_by_comment(Comment, region=None, key=None, keyid=None, profile=None):\n '''\n Find and return any CloudFront distributions which happen to have a Comment sub-field\n either exactly matching the given Comment, or beginning with it AND with the remainder\n separated by a colon.\n\n Comment\n The string to be matched when searching for the given Distribution. Note that this\n will be matched against both the exact value of the Comment sub-field, AND as a\n colon-separated initial value for the same Comment sub-field. E.g. given a passed\n `Comment` value of `foobar`, this would match a distribution with EITHER a\n Comment sub-field of exactly `foobar`, OR a Comment sub-field beginning with\n `foobar:`. The intention here is to permit using the Comment field for storing\n actual comments, in addition to overloading it to store Salt's `Name` attribute.\n\n region\n Region to connect to.\n\n key\n Secret key to use.\n\n keyid\n Access key to use.\n\n profile\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.get_distributions_by_comment 'Comment=foobar'\n salt myminion boto_cloudfront.get_distributions_by_comment 'Comment=foobar:Plus a real comment'\n\n '''\n log.debug('Dereferincing CloudFront distribution(s) by Comment `%s`.', Comment)\n ret = list_distributions(region=region, key=key, keyid=keyid, profile=profile)\n if ret is None:\n return ret\n items = []\n for item in ret:\n comment = item.get('Comment')\n # Comment field is never None, so it can only match if both exist...\n if comment == Comment or comment.startswith('{0}:'.format(Comment)):\n items += [item]\n return items", "response": "Return a list of CloudFront distributions that have a Comment sub - field matching the given Comment."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef disable_distribution(region=None, key=None, keyid=None, profile=None, **kwargs):\n '''\n Set a CloudFront distribution to be disabled.\n\n Id\n Id of the distribution to update.\n\n region\n Region to connect to.\n\n key\n Secret key to use.\n\n keyid\n Access key to use.\n\n profile\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.disable_distribution Id=E24RBTSABCDEF0\n\n '''\n retries = 10\n sleep = 6\n kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}\n authargs = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n Id = kwargs.get('Id')\n current = get_distribution_v2(Id=Id, **authargs)\n if current is None:\n log.error('Failed to get current config of CloudFront distribution `%s`.', Id)\n return None\n if not current['Distribution']['DistributionConfig']['Enabled']:\n return current\n\n ETag = current['ETag']\n DistributionConfig = current['Distribution']['DistributionConfig']\n DistributionConfig['Enabled'] = False\n kwargs = {'DistributionConfig': DistributionConfig, 'Id': Id, 'IfMatch': ETag}\n kwargs.update(authargs)\n while retries:\n try:\n log.debug('Disabling CloudFront distribution `%s`.', Id)\n ret = conn.update_distribution(**kwargs)\n return ret\n except botocore.exceptions.ParamValidationError as err:\n raise SaltInvocationError(str(err))\n except botocore.exceptions.ClientError as err:\n if retries and err.response.get('Error', {}).get('Code') == 'Throttling':\n retries -= 1\n log.debug('Throttled by AWS API, retrying in %s seconds...', sleep)\n time.sleep(sleep)\n continue\n log.error('Failed to disable CloudFront distribution `%s`: %s', Comment, err.message)\n return None", "response": "Disable a CloudFront distribution."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_cloud_front_origin_access_identities_by_comment(Comment, region=None, key=None, keyid=None,\n profile=None):\n '''\n Find and return any CloudFront Origin Access Identities which happen to have a Comment\n sub-field either exactly matching the given Comment, or beginning with it AND with the\n remainder separate by a colon.\n\n Comment\n The string to be matched when searching for the given Origin Access Identity. Note\n that this will be matched against both the exact value of the Comment sub-field, AND as\n a colon-separated initial value for the same Comment sub-field. E.g. given a passed\n `Comment` value of `foobar`, this would match a Origin Access Identity with EITHER a\n Comment sub-field of exactly `foobar`, OR a Comment sub-field beginning with\n `foobar:`. The intention here is to permit using the Comment field for storing\n actual comments, in addition to overloading it to store Salt's `Name` attribute.\n\n region\n Region to connect to.\n\n key\n Secret key to use.\n\n keyid\n Access key to use.\n\n profile\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.get_cloud_front_origin_access_identities_by_comment 'Comment=foobar'\n salt myminion boto_cloudfront.get_cloud_front_origin_access_identities_by_comment 'Comment=foobar:Plus a real comment'\n\n '''\n log.debug('Dereferincing CloudFront origin access identity `%s` by Comment.', Comment)\n ret = list_cloud_front_origin_access_identities(region=region, key=key, keyid=keyid,\n profile=profile)\n if ret is None:\n return ret\n items = []\n for item in ret:\n comment = item.get('Comment', '')\n if comment == Comment or comment.startswith('{0}:'.format(Comment)):\n items += [item]\n return items", "response": "Return a list of CloudFront Origin Access Identities that have a Comment sub - field matching the given Comment."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning True if a CloudFront origin access identity exists with the given Resource ID or False otherwise.", "response": "def cloud_front_origin_access_identity_exists(Id, region=None, key=None, keyid=None, profile=None):\n '''\n Return True if a CloudFront origin access identity exists with the given Resource ID or False\n otherwise.\n\n Id\n Resource ID of the CloudFront origin access identity.\n\n region\n Region to connect to.\n\n key\n Secret key to use.\n\n keyid\n Access key to use.\n\n profile\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.cloud_front_origin_access_identity_exists Id=E30RBTSABCDEF0\n\n '''\n authargs = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n oais = list_cloud_front_origin_access_identities(**authargs) or []\n return bool([i['Id'] for i in oais if i['Id'] == Id])"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nadds tags to a CloudFront resource.", "response": "def tag_resource(region=None, key=None, keyid=None, profile=None, **kwargs):\n '''\n Add tags to a CloudFront resource.\n\n Resource\n The ARN of the affected CloudFront resource.\n\n Tags\n Dict of {'Tag': 'Value', ...} providing the tags to be set.\n\n region\n Region to connect to.\n\n key\n Secret key to use.\n\n keyid\n Access key to use.\n\n profile\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.tag_resource Tags='{Owner: Infra, Role: salt_master}' \\\\\n Resource='arn:aws:cloudfront::012345678012:distribution/ETLNABCDEF123'\n\n '''\n retries = 10\n sleep = 6\n kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n kwargs['Tags'] = {'Items': [{'Key': k, 'Value': v} for k, v in kwargs.get('Tags', {}).items()]}\n while retries:\n try:\n log.debug('Adding tags (%s) to CloudFront resource `%s`.', kwargs['Tags'],\n kwargs.get('Resource'))\n conn.tag_resource(**kwargs)\n return True\n except botocore.exceptions.ParamValidationError as err:\n raise SaltInvocationError(str(err))\n except botocore.exceptions.ClientError as err:\n if retries and err.response.get('Error', {}).get('Code') == 'Throttling':\n retries -= 1\n log.debug('Throttled by AWS API, retrying in %s seconds...', sleep)\n time.sleep(sleep)\n continue\n log.error('Failed to add tags to resource `%s`: %s', kwargs.get('Resource'),\n err.message)\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef enforce_tags(Resource, Tags, region=None, key=None, keyid=None, profile=None):\n '''\n Enforce a given set of tags on a CloudFront resource: adding, removing, or changing them\n as necessary to ensure the resource's tags are exactly and only those specified.\n\n Resource\n The ARN of the affected CloudFront resource.\n\n Tags\n Dict of {'Tag': 'Value', ...} providing the tags to be enforced.\n\n region\n Region to connect to.\n\n key\n Secret key to use.\n\n keyid\n Access key to use.\n\n profile\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudfront.enforce_tags Tags='{Owner: Infra, Role: salt_master}' \\\\\n Resource='arn:aws:cloudfront::012345678012:distribution/ETLNABCDEF123'\n\n '''\n authargs = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n current = list_tags_for_resource(Resource=Resource, **authargs)\n if current is None:\n log.error('Failed to list tags for CloudFront resource `%s`.', Resource)\n return False\n if current == Tags: # Short-ciruits save cycles!\n return True\n remove = [k for k in current if k not in Tags]\n removed = untag_resource(Resource=Resource, TagKeys=remove, **authargs)\n if removed is False:\n log.error('Failed to remove tags (%s) from CloudFront resource `%s`.', remove, Resource)\n return False\n add = {k: v for k, v in Tags.items() if current.get(k) != v}\n added = tag_resource(Resource=Resource, Tags=add, **authargs)\n if added is False:\n log.error('Failed to add tags (%s) to CloudFront resource `%s`.', add, Resource)\n return False\n return True", "response": "Enforces a given set of tags on a CloudFront resource."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreading a file and return content", "response": "def _file_read(path):\n '''\n Read a file and return content\n '''\n content = False\n if os.path.exists(path):\n with salt.utils.files.fopen(path, 'r+') as fp_:\n content = salt.utils.stringutils.to_unicode(fp_.read())\n fp_.close()\n return content"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _file_write(path, content):\n '''\n Write content to a file\n '''\n with salt.utils.files.fopen(path, 'w+') as fp_:\n fp_.write(salt.utils.stringutils.to_str(content))\n fp_.close()", "response": "Write content to a file"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_cibpath():\n '''\n Get the path to the directory on the minion where CIB's are saved\n '''\n cibpath = os.path.join(__opts__['cachedir'], 'pcs', __env__)\n log.trace('cibpath: %s', cibpath)\n return cibpath", "response": "Get the path to the directory on the minion where CIBs are saved"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_cibfile(cibname):\n '''\n Get the full path of a cached CIB-file with the name of the CIB\n '''\n cibfile = os.path.join(_get_cibpath(), '{0}.{1}'.format(cibname, 'cib'))\n log.trace('cibfile: %s', cibfile)\n return cibfile", "response": "Get the full path of a cached CIB - file with the name of the CIB\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_cibfile_tmp(cibname):\n '''\n Get the full path of a temporary CIB-file with the name of the CIB\n '''\n cibfile_tmp = '{0}.tmp'.format(_get_cibfile(cibname))\n log.trace('cibfile_tmp: %s', cibfile_tmp)\n return cibfile_tmp", "response": "Get the full path of a temporary CIB - file with the name of the CIB\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_cibfile_cksum(cibname):\n '''\n Get the full path of the file containing a checksum of a CIB-file with the name of the CIB\n '''\n cibfile_cksum = '{0}.cksum'.format(_get_cibfile(cibname))\n log.trace('cibfile_cksum: %s', cibfile_cksum)\n return cibfile_cksum", "response": "Get the full path of the file containing a checksum of a CIB - file with the name of the CIBName"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nensure that an item is present in the cib file", "response": "def _item_present(name, item, item_id, item_type, show='show', create='create', extra_args=None, cibname=None):\n '''\n Ensure that an item is created\n\n name\n Irrelevant, not used\n item\n config, property, resource, constraint etc.\n item_id\n id of the item\n item_type\n item type\n show\n show command (probably None, default: show)\n create\n create command (create or set f.e., default: create)\n extra_args\n additional options for the pcs command\n cibname\n use a cached CIB-file named like cibname instead of the live CIB\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n item_create_required = True\n\n cibfile = None\n if isinstance(cibname, six.string_types):\n cibfile = _get_cibfile(cibname)\n\n if not isinstance(extra_args, (list, tuple)):\n extra_args = []\n\n # split off key and value (item_id contains =)\n item_id_key = item_id\n item_id_value = None\n if '=' in item_id:\n item_id_key = item_id.split('=')[0].strip()\n item_id_value = item_id.replace(item_id.split('=')[0] + '=', '').strip()\n log.trace('item_id_key=%s item_id_value=%s', item_id_key, item_id_value)\n\n # constraints, properties, resource defaults or resource op defaults\n # do not support specifying an id on 'show' command\n item_id_show = item_id\n if item in ['constraint'] or '=' in item_id:\n item_id_show = None\n\n is_existing = __salt__['pcs.item_show'](item=item,\n item_id=item_id_show,\n item_type=item_type,\n show=show,\n cibfile=cibfile)\n log.trace(\n 'Output of pcs.item_show item=%s item_id=%s item_type=%s cibfile=%s: %s',\n item, item_id_show, item_type, cibfile, is_existing\n )\n\n # key,value pairs (item_id contains =) - match key and value\n if item_id_value is not None:\n for line in is_existing['stdout'].splitlines():\n if len(line.split(':')) in [2]:\n key = line.split(':')[0].strip()\n value = line.split(':')[1].strip()\n if item_id_key in [key]:\n if item_id_value in [value]:\n item_create_required = False\n\n # constraints match on '(id:)'\n elif item in ['constraint']:\n for line in is_existing['stdout'].splitlines():\n if '(id:{0})'.format(item_id) in line:\n item_create_required = False\n\n # item_id was provided,\n # return code 0 indicates, that resource already exists\n else:\n if is_existing['retcode'] in [0]:\n item_create_required = False\n\n if not item_create_required:\n ret['comment'] += '{0} {1} ({2}) is already existing\\n'.format(\n six.text_type(item), six.text_type(item_id), six.text_type(item_type)\n )\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] += '{0} {1} ({2}) is set to be created\\n'.format(\n six.text_type(item), six.text_type(item_id), six.text_type(item_type)\n )\n return ret\n\n item_create = __salt__['pcs.item_create'](\n item=item,\n item_id=item_id,\n item_type=item_type,\n create=create,\n extra_args=extra_args,\n cibfile=cibfile)\n\n log.trace('Output of pcs.item_create: %s', item_create)\n\n if item_create['retcode'] in [0]:\n ret['comment'] += 'Created {0} {1} ({2})\\n'.format(item, item_id, item_type)\n ret['changes'].update({item_id: {'old': '', 'new': six.text_type(item_id)}})\n else:\n ret['result'] = False\n ret['comment'] += 'Failed to create {0} {1} ({2})\\n'.format(item, item_id, item_type)\n\n log.trace('ret: %s', ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures all nodes are authorized to the cluster", "response": "def auth(name, nodes, pcsuser='hacluster', pcspasswd='hacluster', extra_args=None):\n '''\n Ensure all nodes are authorized to the cluster\n\n name\n Irrelevant, not used (recommended: pcs_auth__auth)\n nodes\n a list of nodes which should be authorized to the cluster\n pcsuser\n user for communication with pcs (default: hacluster)\n pcspasswd\n password for pcsuser (default: hacluster)\n extra_args\n list of extra args for the \\'pcs cluster auth\\' command\n\n Example:\n\n .. code-block:: yaml\n\n pcs_auth__auth:\n pcs.auth:\n - nodes:\n - node1.example.com\n - node2.example.com\n - pcsuser: hacluster\n - pcspasswd: hoonetorg\n - extra_args: []\n '''\n\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n auth_required = False\n\n authorized = __salt__['pcs.is_auth'](nodes=nodes)\n log.trace('Output of pcs.is_auth: %s', authorized)\n\n authorized_dict = {}\n for line in authorized['stdout'].splitlines():\n node = line.split(':')[0].strip()\n auth_state = line.split(':')[1].strip()\n if node in nodes:\n authorized_dict.update({node: auth_state})\n log.trace('authorized_dict: %s', authorized_dict)\n\n for node in nodes:\n if node in authorized_dict and authorized_dict[node] == 'Already authorized':\n ret['comment'] += 'Node {0} is already authorized\\n'.format(node)\n else:\n auth_required = True\n if __opts__['test']:\n ret['comment'] += 'Node is set to authorize: {0}\\n'.format(node)\n\n if not auth_required:\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n return ret\n if not isinstance(extra_args, (list, tuple)):\n extra_args = []\n if '--force' not in extra_args:\n extra_args += ['--force']\n\n authorize = __salt__['pcs.auth'](nodes=nodes, pcsuser=pcsuser, pcspasswd=pcspasswd, extra_args=extra_args)\n log.trace('Output of pcs.auth: %s', authorize)\n\n authorize_dict = {}\n for line in authorize['stdout'].splitlines():\n node = line.split(':')[0].strip()\n auth_state = line.split(':')[1].strip()\n if node in nodes:\n authorize_dict.update({node: auth_state})\n log.trace('authorize_dict: %s', authorize_dict)\n\n for node in nodes:\n if node in authorize_dict and authorize_dict[node] == 'Authorized':\n ret['comment'] += 'Authorized {0}\\n'.format(node)\n ret['changes'].update({node: {'old': '', 'new': 'Authorized'}})\n else:\n ret['result'] = False\n if node in authorized_dict:\n ret['comment'] += 'Authorization check for node {0} returned: {1}\\n'.format(node, authorized_dict[node])\n if node in authorize_dict:\n ret['comment'] += 'Failed to authorize {0} with error {1}\\n'.format(node, authorize_dict[node])\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nensure that a node is present in the Pacemaker cluster", "response": "def cluster_node_present(name, node, extra_args=None):\n '''\n Add a node to the Pacemaker cluster via PCS\n Should be run on one cluster node only\n (there may be races)\n Can only be run on a already setup/added node\n\n name\n Irrelevant, not used (recommended: pcs_setup__node_add_{{node}})\n node\n node that should be added\n extra_args\n list of extra args for the \\'pcs cluster node add\\' command\n\n Example:\n\n .. code-block:: yaml\n\n pcs_setup__node_add_node1.example.com:\n pcs.cluster_node_present:\n - node: node1.example.com\n - extra_args:\n - '--start'\n - '--enable'\n '''\n\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n node_add_required = True\n current_nodes = []\n\n is_member_cmd = ['pcs', 'status', 'nodes', 'corosync']\n is_member = __salt__['cmd.run_all'](is_member_cmd, output_loglevel='trace', python_shell=False)\n log.trace('Output of pcs status nodes corosync: %s', is_member)\n\n for line in is_member['stdout'].splitlines():\n try:\n key, value = [x.strip() for x in line.split(':')]\n except ValueError:\n continue\n else:\n if not value or key not in ('Offline', 'Online'):\n continue\n values = value.split(':')\n if node in values:\n node_add_required = False\n ret['comment'] += 'Node {0} is already member of the cluster\\n'.format(node)\n else:\n current_nodes += values\n\n if not node_add_required:\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] += 'Node {0} is set to be added to the cluster\\n'.format(node)\n return ret\n\n if not isinstance(extra_args, (list, tuple)):\n extra_args = []\n\n node_add = __salt__['pcs.cluster_node_add'](node=node, extra_args=extra_args)\n log.trace('Output of pcs.cluster_node_add: %s', node_add)\n\n node_add_dict = {}\n for line in node_add['stdout'].splitlines():\n log.trace('line: %s', line)\n log.trace('line.split(:).len: %s', len(line.split(':')))\n if len(line.split(':')) in [2]:\n current_node = line.split(':')[0].strip()\n current_node_add_state = line.split(':')[1].strip()\n if current_node in current_nodes + [node]:\n node_add_dict.update({current_node: current_node_add_state})\n log.trace('node_add_dict: %s', node_add_dict)\n\n for current_node in current_nodes:\n if current_node in node_add_dict:\n if node_add_dict[current_node] not in ['Corosync updated']:\n ret['result'] = False\n ret['comment'] += 'Failed to update corosync.conf on node {0}\\n'.format(current_node)\n ret['comment'] += '{0}: node_add_dict: {1}\\n'.format(current_node, node_add_dict[current_node])\n else:\n ret['result'] = False\n ret['comment'] += 'Failed to update corosync.conf on node {0}\\n'.format(current_node)\n\n if node in node_add_dict and node_add_dict[node] in ['Succeeded', 'Success']:\n ret['comment'] += 'Added node {0}\\n'.format(node)\n ret['changes'].update({node: {'old': '', 'new': 'Added'}})\n else:\n ret['result'] = False\n ret['comment'] += 'Failed to add node{0}\\n'.format(node)\n if node in node_add_dict:\n ret['comment'] += '{0}: node_add_dict: {1}\\n'.format(node, node_add_dict[node])\n ret['comment'] += six.text_type(node_add)\n\n log.trace('ret: %s', ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring that a CIB - file with the content of the current live CIB is created", "response": "def cib_present(name, cibname, scope=None, extra_args=None):\n '''\n Ensure that a CIB-file with the content of the current live CIB is created\n\n Should be run on one cluster node only\n (there may be races)\n\n name\n Irrelevant, not used (recommended: {{formulaname}}__cib_present_{{cibname}})\n cibname\n name/path of the file containing the CIB\n scope\n specific section of the CIB (default:\n extra_args\n additional options for creating the CIB-file\n\n Example:\n\n .. code-block:: yaml\n\n mysql_pcs__cib_present_cib_for_galera:\n pcs.cib_present:\n - cibname: cib_for_galera\n - scope: None\n - extra_args: None\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n cib_hash_form = 'sha256'\n\n cib_create_required = False\n cib_cksum_required = False\n cib_required = False\n\n cibpath = _get_cibpath()\n cibfile = _get_cibfile(cibname)\n cibfile_tmp = _get_cibfile_tmp(cibname)\n cibfile_cksum = _get_cibfile_cksum(cibname)\n\n if not os.path.exists(cibpath):\n os.makedirs(cibpath)\n\n if not isinstance(extra_args, (list, tuple)):\n extra_args = []\n\n if os.path.exists(cibfile_tmp):\n __salt__['file.remove'](cibfile_tmp)\n\n cib_create = __salt__['pcs.cib_create'](cibfile=cibfile_tmp, scope=scope, extra_args=extra_args)\n log.trace('Output of pcs.cib_create: %s', cib_create)\n\n if cib_create['retcode'] not in [0] or not os.path.exists(cibfile_tmp):\n ret['result'] = False\n ret['comment'] += 'Failed to get live CIB\\n'\n return ret\n\n cib_hash_live = '{0}:{1}'.format(cib_hash_form, __salt__['file.get_hash'](path=cibfile_tmp, form=cib_hash_form))\n log.trace('cib_hash_live: %s', cib_hash_live)\n\n cib_hash_cur = _file_read(path=cibfile_cksum)\n\n if cib_hash_cur not in [cib_hash_live]:\n cib_cksum_required = True\n\n log.trace('cib_hash_cur: %s', cib_hash_cur)\n\n if not os.path.exists(cibfile) or not __salt__['file.check_hash'](path=cibfile, file_hash=cib_hash_live):\n cib_create_required = True\n\n if cib_cksum_required or cib_create_required:\n cib_required = True\n\n if not cib_create_required:\n __salt__['file.remove'](cibfile_tmp)\n ret['comment'] += 'CIB {0} is already equal to the live CIB\\n'.format(cibname)\n\n if not cib_cksum_required:\n ret['comment'] += 'CIB {0} checksum is correct\\n'.format(cibname)\n\n if not cib_required:\n return ret\n\n if __opts__['test']:\n __salt__['file.remove'](cibfile_tmp)\n ret['result'] = None\n if cib_create_required:\n ret['comment'] += 'CIB {0} is set to be created/updated\\n'.format(cibname)\n if cib_cksum_required:\n ret['comment'] += 'CIB {0} checksum is set to be created/updated\\n'.format(cibname)\n return ret\n\n if cib_create_required:\n __salt__['file.move'](cibfile_tmp, cibfile)\n\n if __salt__['file.check_hash'](path=cibfile, file_hash=cib_hash_live):\n ret['comment'] += 'Created/updated CIB {0}\\n'.format(cibname)\n ret['changes'].update({'cibfile': cibfile})\n else:\n ret['result'] = False\n ret['comment'] += 'Failed to create/update CIB {0}\\n'.format(cibname)\n\n if cib_cksum_required:\n _file_write(cibfile_cksum, cib_hash_live)\n\n if _file_read(cibfile_cksum) in [cib_hash_live]:\n ret['comment'] += 'Created/updated checksum {0} of CIB {1}\\n'.format(cib_hash_live, cibname)\n ret['changes'].update({'cibcksum': cib_hash_live})\n else:\n ret['result'] = False\n ret['comment'] += 'Failed to create/update checksum {0} CIB {1}\\n'.format(cib_hash_live, cibname)\n\n log.trace('ret: %s', ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nensure that a CIB - file is pushed if it is not already pushed", "response": "def cib_pushed(name, cibname, scope=None, extra_args=None):\n '''\n Ensure that a CIB-file is pushed if it is changed since the creation of it with pcs.cib_present\n\n Should be run on one cluster node only\n (there may be races)\n\n name\n Irrelevant, not used (recommended: {{formulaname}}__cib_pushed_{{cibname}})\n cibname\n name/path of the file containing the CIB\n scope\n specific section of the CIB\n extra_args\n additional options for creating the CIB-file\n\n Example:\n\n .. code-block:: yaml\n\n mysql_pcs__cib_pushed_cib_for_galera:\n pcs.cib_pushed:\n - cibname: cib_for_galera\n - scope: None\n - extra_args: None\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n cib_hash_form = 'sha256'\n\n cib_push_required = False\n\n cibfile = _get_cibfile(cibname)\n cibfile_cksum = _get_cibfile_cksum(cibname)\n\n if not isinstance(extra_args, (list, tuple)):\n extra_args = []\n\n if not os.path.exists(cibfile):\n ret['result'] = False\n ret['comment'] += 'CIB-file {0} does not exist\\n'.format(cibfile)\n return ret\n\n cib_hash_cibfile = '{0}:{1}'.format(cib_hash_form, __salt__['file.get_hash'](path=cibfile, form=cib_hash_form))\n log.trace('cib_hash_cibfile: %s', cib_hash_cibfile)\n\n if _file_read(cibfile_cksum) not in [cib_hash_cibfile]:\n cib_push_required = True\n\n if not cib_push_required:\n ret['comment'] += 'CIB {0} is not changed since creation through pcs.cib_present\\n'.format(cibname)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] += 'CIB {0} is set to be pushed as the new live CIB\\n'.format(cibname)\n return ret\n\n cib_push = __salt__['pcs.cib_push'](cibfile=cibfile, scope=scope, extra_args=extra_args)\n log.trace('Output of pcs.cib_push: %s', cib_push)\n\n if cib_push['retcode'] in [0]:\n ret['comment'] += 'Pushed CIB {0}\\n'.format(cibname)\n ret['changes'].update({'cibfile_pushed': cibfile})\n else:\n ret['result'] = False\n ret['comment'] += 'Failed to push CIB {0}\\n'.format(cibname)\n\n log.trace('ret: %s', ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nensure that a property in the cluster has a given value", "response": "def prop_has_value(name, prop, value, extra_args=None, cibname=None):\n '''\n Ensure that a property in the cluster is set to a given value\n\n Should be run on one cluster node only\n (there may be races)\n\n name\n Irrelevant, not used (recommended: pcs_properties__prop_has_value_{{prop}})\n prop\n name of the property\n value\n value of the property\n extra_args\n additional options for the pcs property command\n cibname\n use a cached CIB-file named like cibname instead of the live CIB\n\n Example:\n\n .. code-block:: yaml\n\n pcs_properties__prop_has_value_no-quorum-policy:\n pcs.prop_has_value:\n - prop: no-quorum-policy\n - value: ignore\n - cibname: cib_for_cluster_settings\n '''\n return _item_present(name=name,\n item='property',\n item_id='{0}={1}'.format(prop, value),\n item_type=None,\n create='set',\n extra_args=extra_args,\n cibname=cibname)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef resource_defaults_to(name, default, value, extra_args=None, cibname=None):\n '''\n Ensure a resource default in the cluster is set to a given value\n\n Should be run on one cluster node only\n (there may be races)\n Can only be run on a node with a functional pacemaker/corosync\n\n name\n Irrelevant, not used (recommended: pcs_properties__resource_defaults_to_{{default}})\n default\n name of the default resource property\n value\n value of the default resource property\n extra_args\n additional options for the pcs command\n cibname\n use a cached CIB-file named like cibname instead of the live CIB\n\n Example:\n\n .. code-block:: yaml\n\n pcs_properties__resource_defaults_to_resource-stickiness:\n pcs.resource_defaults_to:\n - default: resource-stickiness\n - value: 100\n - cibname: cib_for_cluster_settings\n '''\n return _item_present(name=name,\n item='resource',\n item_id='{0}={1}'.format(default, value),\n item_type=None,\n show='defaults',\n create='defaults',\n extra_args=extra_args,\n cibname=cibname)", "response": "Ensure a resource default in the cluster is set to a given value"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef resource_op_defaults_to(name, op_default, value, extra_args=None, cibname=None):\n '''\n Ensure a resource operation default in the cluster is set to a given value\n\n Should be run on one cluster node only\n (there may be races)\n Can only be run on a node with a functional pacemaker/corosync\n\n name\n Irrelevant, not used (recommended: pcs_properties__resource_op_defaults_to_{{op_default}})\n op_default\n name of the operation default resource property\n value\n value of the operation default resource property\n extra_args\n additional options for the pcs command\n cibname\n use a cached CIB-file named like cibname instead of the live CIB\n\n Example:\n\n .. code-block:: yaml\n\n pcs_properties__resource_op_defaults_to_monitor-interval:\n pcs.resource_op_defaults_to:\n - op_default: monitor-interval\n - value: 60s\n - cibname: cib_for_cluster_settings\n '''\n return _item_present(name=name,\n item='resource',\n item_id='{0}={1}'.format(op_default, value),\n item_type=None,\n show=['op', 'defaults'],\n create=['op', 'defaults'],\n extra_args=extra_args,\n cibname=cibname)", "response": "Ensure a resource operation default in the cluster is set to a given value"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring that a stonith resource is created", "response": "def stonith_present(name, stonith_id, stonith_device_type, stonith_device_options=None, cibname=None):\n '''\n Ensure that a fencing resource is created\n\n Should be run on one cluster node only\n (there may be races)\n Can only be run on a node with a functional pacemaker/corosync\n\n name\n Irrelevant, not used (recommended: pcs_stonith__created_{{stonith_id}})\n stonith_id\n name for the stonith resource\n stonith_device_type\n name of the stonith agent fence_eps, fence_xvm f.e.\n stonith_device_options\n additional options for creating the stonith resource\n cibname\n use a cached CIB-file named like cibname instead of the live CIB\n\n Example:\n\n .. code-block:: yaml\n\n pcs_stonith__created_eps_fence:\n pcs.stonith_present:\n - stonith_id: eps_fence\n - stonith_device_type: fence_eps\n - stonith_device_options:\n - 'pcmk_host_map=node1.example.org:01;node2.example.org:02'\n - 'ipaddr=myepsdevice.example.org'\n - 'power_wait=5'\n - 'verbose=1'\n - 'debug=/var/log/pcsd/eps_fence.log'\n - 'login=hidden'\n - 'passwd=hoonetorg'\n - cibname: cib_for_stonith\n '''\n return _item_present(name=name,\n item='stonith',\n item_id=stonith_id,\n item_type=stonith_device_type,\n extra_args=stonith_device_options,\n cibname=cibname)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nensures that a resource is created", "response": "def resource_present(name, resource_id, resource_type, resource_options=None, cibname=None):\n '''\n Ensure that a resource is created\n\n Should be run on one cluster node only\n (there may be races)\n Can only be run on a node with a functional pacemaker/corosync\n\n name\n Irrelevant, not used (recommended: {{formulaname}}__resource_present_{{resource_id}})\n resource_id\n name for the resource\n resource_type\n resource type (f.e. ocf:heartbeat:IPaddr2 or VirtualIP)\n resource_options\n additional options for creating the resource\n cibname\n use a cached CIB-file named like cibname instead of the live CIB\n\n Example:\n\n .. code-block:: yaml\n\n mysql_pcs__resource_present_galera:\n pcs.resource_present:\n - resource_id: galera\n - resource_type: \"ocf:heartbeat:galera\"\n - resource_options:\n - 'wsrep_cluster_address=gcomm://node1.example.org,node2.example.org,node3.example.org'\n - '--master'\n - cibname: cib_for_galera\n '''\n return _item_present(name=name,\n item='resource',\n item_id=resource_id,\n item_type=resource_type,\n extra_args=resource_options,\n cibname=cibname)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef constraint_present(name, constraint_id, constraint_type, constraint_options=None, cibname=None):\n '''\n Ensure that a constraint is created\n\n Should be run on one cluster node only\n (there may be races)\n Can only be run on a node with a functional pacemaker/corosync\n\n name\n Irrelevant, not used (recommended: {{formulaname}}__constraint_present_{{constraint_id}})\n constraint_id\n name for the constraint (try first to create manually to find out the autocreated name)\n constraint_type\n constraint type (location, colocation, order)\n constraint_options\n options for creating the constraint\n cibname\n use a cached CIB-file named like cibname instead of the live CIB\n\n Example:\n\n .. code-block:: yaml\n\n haproxy_pcs__constraint_present_colocation-vip_galera-haproxy-clone-INFINITY:\n pcs.constraint_present:\n - constraint_id: colocation-vip_galera-haproxy-clone-INFINITY\n - constraint_type: colocation\n - constraint_options:\n - 'add'\n - 'vip_galera'\n - 'with'\n - 'haproxy-clone'\n - cibname: cib_for_haproxy\n '''\n return _item_present(name=name,\n item='constraint',\n item_id=constraint_id,\n item_type=constraint_type,\n create=None,\n extra_args=constraint_options,\n cibname=cibname)", "response": "Ensure that a constraint is created and present in the cluster node."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _parse_top_cfg(content, filename):\n '''\n Allow top_cfg to be YAML\n '''\n try:\n obj = salt.utils.yaml.safe_load(content)\n if isinstance(obj, list):\n log.debug('MakoStack cfg `%s` parsed as YAML', filename)\n return obj\n except Exception as err:\n pass\n log.debug('MakoStack cfg `%s` parsed as plain text', filename)\n return content.splitlines()", "response": "Parse the top_cfg file into a list of lines"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks if jail service is actually enabled on boot CLI Example", "response": "def is_enabled():\n '''\n See if jail service is actually enabled on boot\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jail.is_enabled \n '''\n cmd = 'service -e'\n services = __salt__['cmd.run'](cmd, python_shell=False)\n for service in services.split('\\\\n'):\n if re.search('jail', service):\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns which jails are set to be run CLI Example : bash salt '*' jail. get_enabled", "response": "def get_enabled():\n '''\n Return which jails are set to be run\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jail.get_enabled\n '''\n ret = []\n for rconf in ('/etc/rc.conf', '/etc/rc.conf.local'):\n if os.access(rconf, os.R_OK):\n with salt.utils.files.fopen(rconf, 'r') as _fp:\n for line in _fp:\n line = salt.utils.stringutils.to_unicode(line)\n if not line.strip():\n continue\n if not line.startswith('jail_list='):\n continue\n jails = line.split('\"')[1].split()\n for j in jails:\n ret.append(j)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndisplay the specified jail s configuration", "response": "def show_config(jail):\n '''\n Display specified jail's configuration\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jail.show_config \n '''\n ret = {}\n if subprocess.call([\"jls\", \"-nq\", \"-j\", jail]) == 0:\n jls = subprocess.check_output([\"jls\", \"-nq\", \"-j\", jail]) # pylint: disable=minimum-python-version\n jailopts = salt.utils.args.shlex_split(salt.utils.stringutils.to_unicode(jls))\n for jailopt in jailopts:\n if '=' not in jailopt:\n ret[jailopt.strip().rstrip(\";\")] = '1'\n else:\n key = jailopt.split('=')[0].strip()\n value = jailopt.split('=')[-1].strip().strip(\"\\\"\")\n ret[key] = value\n else:\n for rconf in ('/etc/rc.conf', '/etc/rc.conf.local'):\n if os.access(rconf, os.R_OK):\n with salt.utils.files.fopen(rconf, 'r') as _fp:\n for line in _fp:\n line = salt.utils.stringutils.to_unicode(line)\n if not line.strip():\n continue\n if not line.startswith('jail_{0}_'.format(jail)):\n continue\n key, value = line.split('=')\n ret[key.split('_', 2)[2]] = value.split('\"')[1]\n for jconf in ('/etc/jail.conf', '/usr/local/etc/jail.conf'):\n if os.access(jconf, os.R_OK):\n with salt.utils.files.fopen(jconf, 'r') as _fp:\n for line in _fp:\n line = salt.utils.stringutils.to_unicode(line)\n line = line.partition('#')[0].strip()\n if line:\n if line.split()[-1] == '{':\n if line.split()[0] != jail and line.split()[0] != '*':\n while line.split()[-1] != '}':\n line = next(_fp)\n line = line.partition('#')[0].strip()\n else:\n continue\n if line.split()[-1] == '}':\n continue\n if '=' not in line:\n ret[line.strip().rstrip(\";\")] = '1'\n else:\n key = line.split('=')[0].strip()\n value = line.split('=')[-1].strip().strip(\";'\\\"\")\n ret[key] = value\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndisplay contents of a fstab file defined in specified jail s configuration.", "response": "def fstab(jail):\n '''\n Display contents of a fstab(5) file defined in specified\n jail's configuration. If no file is defined, return False.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jail.fstab \n '''\n ret = []\n config = show_config(jail)\n if 'fstab' in config:\n c_fstab = config['fstab']\n elif 'mount.fstab' in config:\n c_fstab = config['mount.fstab']\n if 'fstab' in config or 'mount.fstab' in config:\n if os.access(c_fstab, os.R_OK):\n with salt.utils.files.fopen(c_fstab, 'r') as _fp:\n for line in _fp:\n line = salt.utils.stringutils.to_unicode(line)\n line = line.strip()\n if not line:\n continue\n if line.startswith('#'):\n continue\n try:\n device, mpoint, fstype, opts, dump, pas_ = line.split()\n except ValueError:\n # Gracefully continue on invalid lines\n continue\n ret.append({\n 'device': device,\n 'mountpoint': mpoint,\n 'fstype': fstype,\n 'options': opts,\n 'dump': dump,\n 'pass': pas_\n })\n if not ret:\n ret = False\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking if specified jail is currently running", "response": "def status(jail):\n '''\n See if specified jail is currently running\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jail.status \n '''\n cmd = 'jls'\n found_jails = __salt__['cmd.run'](cmd, python_shell=False)\n for found_jail in found_jails.split('\\\\n'):\n if re.search(jail, found_jail):\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn all kernel states in the systemctl jail", "response": "def sysctl():\n '''\n Dump all jail related kernel states (sysctl)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jail.sysctl\n '''\n ret = {}\n sysctl_jail = __salt__['cmd.run']('sysctl security.jail')\n for line in sysctl_jail.splitlines():\n key, value = line.split(':', 1)\n ret[key.strip()] = value.strip()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nexecute the command passed with pecl", "response": "def _pecl(command, defaults=False):\n '''\n Execute the command passed with pecl\n '''\n cmdline = 'pecl {0}'.format(command)\n if salt.utils.data.is_true(defaults):\n cmdline = 'yes ' \"''\" + ' | ' + cmdline\n\n ret = __salt__['cmd.run_all'](cmdline, python_shell=True)\n\n if ret['retcode'] == 0:\n return ret['stdout']\n else:\n log.error('Problem running pecl. Is php-pear installed?')\n return ''"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef install(pecls, defaults=False, force=False, preferred_state='stable'):\n '''\n .. versionadded:: 0.17.0\n\n Installs one or several pecl extensions.\n\n pecls\n The pecl extensions to install.\n\n defaults\n Use default answers for extensions such as pecl_http which ask\n questions before installation. Without this option, the pecl.installed\n state will hang indefinitely when trying to install these extensions.\n\n force\n Whether to force the installed version or not\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pecl.install fuse\n '''\n if isinstance(pecls, six.string_types):\n pecls = [pecls]\n preferred_state = '-d preferred_state={0}'.format(_cmd_quote(preferred_state))\n if force:\n return _pecl('{0} install -f {1}'.format(preferred_state, _cmd_quote(' '.join(pecls))),\n defaults=defaults)\n else:\n _pecl('{0} install {1}'.format(preferred_state, _cmd_quote(' '.join(pecls))),\n defaults=defaults)\n if not isinstance(pecls, list):\n pecls = [pecls]\n for pecl in pecls:\n found = False\n if '/' in pecl:\n channel, pecl = pecl.split('/')\n else:\n channel = None\n installed_pecls = list_(channel)\n for pecl in installed_pecls:\n installed_pecl_with_version = '{0}-{1}'.format(\n pecl,\n installed_pecls.get(pecl)[0]\n )\n if pecl in installed_pecl_with_version:\n found = True\n if not found:\n return False\n return True", "response": "Installs one or several pecl extensions and returns True if successful."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef uninstall(pecls):\n '''\n Uninstall one or several pecl extensions.\n\n pecls\n The pecl extensions to uninstall.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pecl.uninstall fuse\n '''\n if isinstance(pecls, six.string_types):\n pecls = [pecls]\n return _pecl('uninstall {0}'.format(_cmd_quote(' '.join(pecls))))", "response": "Uninstall one or several pecl extensions."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlisting installed pecl extensions.", "response": "def list_(channel=None):\n '''\n List installed pecl extensions.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pecl.list\n '''\n pecl_channel_pat = re.compile('^([^ ]+)[ ]+([^ ]+)[ ]+([^ ]+)')\n pecls = {}\n command = 'list'\n if channel:\n command = '{0} -c {1}'.format(command, _cmd_quote(channel))\n lines = _pecl(command).splitlines()\n lines = (l for l in lines if pecl_channel_pat.match(l))\n\n for line in lines:\n match = pecl_channel_pat.match(line)\n if match:\n pecls[match.group(1)] = [match.group(2), match.group(3)]\n\n return pecls"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef profile(prof=None, instances=None, opts=None, **kwargs):\n '''\n Create a cloud vm with the given profile and instances, instances can be a\n list or comma-delimited string\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run cloud.profile prof=my-ec2 instances=node1,node2,node3\n '''\n if prof is None and 'profile' in kwargs:\n prof = kwargs['profile']\n\n if prof is None:\n return {'Error': 'A profile (or prof) must be defined'}\n\n if instances is None and 'names' in kwargs:\n instances = kwargs['names']\n\n if instances is None:\n return {'Error': 'One or more instances (comma-delimited) must be set'}\n\n client = _get_client()\n if isinstance(opts, dict):\n client.opts.update(opts)\n info = client.profile(prof, instances, **salt.utils.args.clean_kwargs(**kwargs))\n return info", "response": "Create a cloud vm with the given profile and instances"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef map_run(path=None, opts=None, **kwargs):\n '''\n Execute a salt cloud map file\n '''\n client = _get_client()\n if isinstance(opts, dict):\n client.opts.update(opts)\n info = client.map_run(path, **salt.utils.args.clean_kwargs(**kwargs))\n return info", "response": "Execute a salt cloud map file\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndestroys the named vm", "response": "def destroy(instances, opts=None):\n '''\n Destroy the named vm(s)\n '''\n client = _get_client()\n if isinstance(opts, dict):\n client.opts.update(opts)\n info = client.destroy(instances)\n return info"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nexecutes a single action on the given map provider instance", "response": "def action(func=None,\n cloudmap=None,\n instances=None,\n provider=None,\n instance=None,\n opts=None,\n **kwargs):\n '''\n Execute a single action on the given map/provider/instance\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run cloud.action start my-salt-vm\n '''\n info = {}\n client = _get_client()\n if isinstance(opts, dict):\n client.opts.update(opts)\n try:\n info = client.action(\n func,\n cloudmap,\n instances,\n provider,\n instance,\n salt.utils.args.clean_kwargs(**kwargs)\n )\n except SaltCloudConfigError as err:\n log.error(err)\n return info"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates an instance using Salt Cloud Taxonomy CLI Example", "response": "def create(provider, instances, opts=None, **kwargs):\n '''\n Create an instance using Salt Cloud\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run cloud.create my-ec2-config myinstance \\\n image=ami-1624987f size='t1.micro' ssh_username=ec2-user \\\n securitygroup=default delvol_on_destroy=True\n '''\n client = _get_client()\n if isinstance(opts, dict):\n client.opts.update(opts)\n info = client.create(provider, instances, **salt.utils.args.clean_kwargs(**kwargs))\n return info"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget splunk search properties from an object", "response": "def _get_splunk_search_props(search):\n '''\n Get splunk search properties from an object\n '''\n props = search.content\n props[\"app\"] = search.access.app\n props[\"sharing\"] = search.access.sharing\n return props"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get(name, profile=\"splunk\"):\n '''\n Get a splunk search\n\n CLI Example:\n\n splunk_search.get 'my search name'\n '''\n client = _get_splunk(profile)\n search = None\n # uglyness of splunk lib\n try:\n search = client.saved_searches[name]\n except KeyError:\n pass\n return search", "response": "Get a splunk search by name CLI Example : splunk_search. get my search name"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate a splunk search object with new properties", "response": "def update(name, profile=\"splunk\", **kwargs):\n '''\n Update a splunk search\n\n CLI Example:\n\n splunk_search.update 'my search name' sharing=app\n '''\n client = _get_splunk(profile)\n search = client.saved_searches[name]\n props = _get_splunk_search_props(search)\n updates = kwargs\n update_needed = False\n update_set = dict()\n diffs = []\n for key in sorted(kwargs):\n old_value = props.get(key, None)\n new_value = updates.get(key, None)\n if isinstance(old_value, six.string_types):\n old_value = old_value.strip()\n if isinstance(new_value, six.string_types):\n new_value = new_value.strip()\n if old_value != new_value:\n update_set[key] = new_value\n update_needed = True\n diffs.append(\"{0}: '{1}' => '{2}'\".format(\n key, old_value, new_value\n ))\n if update_needed:\n search.update(**update_set).refresh()\n return update_set, diffs\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create(name, profile=\"splunk\", **kwargs):\n '''\n Create a splunk search\n\n CLI Example:\n\n splunk_search.create 'my search name' search='error msg'\n '''\n client = _get_splunk(profile)\n search = client.saved_searches.create(name, **kwargs)\n\n # use the REST API to set owner and permissions\n # this is hard-coded for now; all managed searches are app scope and\n # readable by all\n config = __salt__['config.option'](profile)\n url = \"https://{0}:{1}\".format(config.get('host'), config.get('port'))\n auth = (config.get('username'), config.get('password'))\n data = {\n \"owner\": config.get(\"username\"),\n \"sharing\": \"app\",\n \"perms.read\": \"*\",\n }\n _req_url = \"{0}/servicesNS/{1}/search/saved/searches/{2}/acl\".format(\n url, config.get(\"username\"), urllib.quote(name)\n )\n requests.post(_req_url, auth=auth, verify=True, data=data)\n return _get_splunk_search_props(search)", "response": "Create a splunk search\n CLI Example : splunk_search. create my search name search = error msg"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delete(name, profile=\"splunk\"):\n '''\n Delete a splunk search\n\n CLI Example:\n\n splunk_search.delete 'my search name'\n '''\n client = _get_splunk(profile)\n try:\n client.saved_searches.delete(name)\n return True\n except KeyError:\n return None", "response": "Delete a splunk search by name"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nlisting all splunk searches", "response": "def list_(profile=\"splunk\"):\n '''\n List splunk searches (names only)\n\n CLI Example:\n splunk_search.list\n '''\n client = _get_splunk(profile)\n searches = [x['name'] for x in client.saved_searches]\n return searches"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_all(prefix=None, app=None, owner=None, description_contains=None,\n name_not_contains=None, profile=\"splunk\"):\n '''\n Get all splunk search details. Produces results that can be used to create\n an sls file.\n\n if app or owner are specified, results will be limited to matching saved\n searches.\n\n if description_contains is specified, results will be limited to those\n where \"description_contains in description\" is true if name_not_contains is\n specified, results will be limited to those where \"name_not_contains not in\n name\" is true.\n\n If prefix parameter is given, alarm names in the output will be prepended\n with the prefix; alarms that have the prefix will be skipped. This can be\n used to convert existing alarms to be managed by salt, as follows:\n\n CLI example:\n\n 1. Make a \"backup\" of all existing searches\n $ salt-call splunk_search.list_all --out=txt | sed \"s/local: //\" > legacy_searches.sls\n\n 2. Get all searches with new prefixed names\n $ salt-call splunk_search.list_all \"prefix=**MANAGED BY SALT** \" --out=txt | sed \"s/local: //\" > managed_searches.sls\n\n 3. Insert the managed searches into splunk\n $ salt-call state.sls managed_searches.sls\n\n 4. Manually verify that the new searches look right\n\n 5. Delete the original searches\n $ sed s/present/absent/ legacy_searches.sls > remove_legacy_searches.sls\n $ salt-call state.sls remove_legacy_searches.sls\n\n 6. Get all searches again, verify no changes\n $ salt-call splunk_search.list_all --out=txt | sed \"s/local: //\" > final_searches.sls\n $ diff final_searches.sls managed_searches.sls\n '''\n client = _get_splunk(profile)\n\n # splunklib doesn't provide the default settings for saved searches.\n # so, in order to get the defaults, we create a search with no\n # configuration, get that search, and then delete it. We use its contents\n # as the default settings\n name = \"splunk_search.list_all get defaults\"\n try:\n client.saved_searches.delete(name)\n except Exception:\n pass\n search = client.saved_searches.create(name, search=\"nothing\")\n defaults = dict(search.content)\n client.saved_searches.delete(name)\n\n # stuff that splunk returns but that you should not attempt to set.\n # cf http://dev.splunk.com/view/python-sdk/SP-CAAAEK2\n readonly_keys = (\"triggered_alert_count\",\n \"action.email\",\n \"action.populate_lookup\",\n \"action.rss\",\n \"action.script\",\n \"action.summary_index\",\n \"qualifiedSearch\",\n \"next_scheduled_time\")\n\n results = OrderedDict()\n # sort the splunk searches by name, so we get consistent output\n searches = sorted([(s.name, s) for s in client.saved_searches])\n for name, search in searches:\n if app and search.access.app != app:\n continue\n if owner and search.access.owner != owner:\n continue\n if name_not_contains and name_not_contains in name:\n continue\n if prefix:\n if name.startswith(prefix):\n continue\n name = prefix + name\n # put name in the OrderedDict first\n d = [{\"name\": name}]\n # add the rest of the splunk settings, ignoring any defaults\n description = ''\n for (k, v) in sorted(search.content.items()):\n if k in readonly_keys:\n continue\n if k.startswith(\"display.\"):\n continue\n if not v:\n continue\n if k in defaults and defaults[k] == v:\n continue\n d.append({k: v})\n if k == 'description':\n description = v\n if description_contains and description_contains not in description:\n continue\n results[\"manage splunk search \" + name] = {\"splunk_search.present\": d}\n\n return salt.utils.yaml.safe_dump(results, default_flow_style=False, width=120)", "response": "List all alarms in the current project."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nensuring the set is present for the specified user - defined set.", "response": "def set_present(name, set_type, family='ipv4', **kwargs):\n '''\n .. versionadded:: 2014.7.0\n\n Verify the set exists.\n\n name\n A user-defined set name.\n\n set_type\n The type for the set.\n\n family\n Networking family, either ipv4 or ipv6\n '''\n\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n set_check = __salt__['ipset.check_set'](name)\n if set_check is True:\n ret['result'] = True\n ret['comment'] = ('ipset set {0} already exists for {1}'\n .format(name, family))\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'ipset set {0} would be added for {1}'.format(\n name,\n family)\n return ret\n command = __salt__['ipset.new_set'](name, set_type, family, **kwargs)\n if command is True:\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = ('ipset set {0} created successfully for {1}'\n .format(name, family))\n return ret\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to create set {0} for {2}: {1}'.format(\n name,\n command.strip(),\n family\n )\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nensures the specified set is absent.", "response": "def set_absent(name, family='ipv4', **kwargs):\n '''\n .. versionadded:: 2014.7.0\n\n Verify the set is absent.\n\n family\n Networking family, either ipv4 or ipv6\n '''\n\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n set_check = __salt__['ipset.check_set'](name, family)\n if not set_check:\n ret['result'] = True\n ret['comment'] = ('ipset set {0} for {1} is already absent'\n .format(name, family))\n return ret\n if __opts__['test']:\n ret['comment'] = 'ipset set {0} for {1} would be removed'.format(\n name,\n family)\n return ret\n flush_set = __salt__['ipset.flush'](name, family)\n if flush_set:\n command = __salt__['ipset.delete_set'](name, family)\n if command is True:\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = ('ipset set {0} deleted successfully for family {1}'\n .format(name, family))\n else:\n ret['result'] = False\n ret['comment'] = ('Failed to delete set {0} for {2}: {1}'\n .format(name, command.strip(), family))\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to flush set {0} for {2}: {1}'.format(\n name,\n flush_set.strip(),\n family\n )\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nensure that the named entry or entries are not present in the chain.", "response": "def absent(name, entry=None, entries=None, family='ipv4', **kwargs):\n '''\n .. versionadded:: 2014.7.0\n\n Remove a entry or entries from a chain\n\n name\n A user-defined name to call this entry by in another part of a state or\n formula. This should not be an actual entry.\n\n family\n Network family, ipv4 or ipv6.\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n if not entry:\n ret['result'] = False\n ret['comment'] = ('ipset entry must be specified')\n return ret\n\n entries = []\n if isinstance(entry, list):\n entries = entry\n else:\n entries.append(entry)\n\n for entry in entries:\n entry_opts = ''\n if ' ' in entry:\n entry, entry_opts = entry.split(' ', 1)\n if 'timeout' in kwargs and 'timeout' not in entry_opts:\n entry_opts = 'timeout {0} {1}'.format(kwargs['timeout'], entry_opts)\n if 'comment' in kwargs and 'comment' not in entry_opts:\n entry_opts = '{0} comment \"{1}\"'.format(entry_opts, kwargs['comment'])\n _entry = ' '.join([entry, entry_opts]).strip()\n\n log.debug('_entry %s', _entry)\n if not __salt__['ipset.check'](kwargs['set_name'],\n _entry,\n family) is True:\n ret['result'] = True\n ret['comment'] += 'ipset entry for {0} not present in set {1} for {2}\\n'.format(\n _entry,\n kwargs['set_name'],\n family)\n else:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] += 'ipset entry {0} would be removed from set {1} for {2}\\n'.format(\n entry,\n kwargs['set_name'],\n family)\n else:\n command = __salt__['ipset.delete'](kwargs['set_name'], entry, family, **kwargs)\n if 'Error' not in command:\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] += 'ipset entry {1} removed from set {0} for {2}\\n'.format(\n kwargs['set_name'],\n _entry,\n family)\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to delete ipset entry from set {0} for {2}. ' \\\n 'Attempted entry was {1}.\\n' \\\n '{3}\\n'.format(kwargs['set_name'], _entry, family, command)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nflushes the current set of entries for the specified networking family.", "response": "def flush(name, family='ipv4', **kwargs):\n '''\n .. versionadded:: 2014.7.0\n\n Flush current ipset set\n\n family\n Networking family, either ipv4 or ipv6\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n set_check = __salt__['ipset.check_set'](name)\n if set_check is False:\n ret['result'] = False\n ret['comment'] = ('ipset set {0} does not exist for {1}'\n .format(name, family))\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'ipset entries in set {0} for {1} would be flushed'.format(\n name,\n family)\n return ret\n if __salt__['ipset.flush'](name, family):\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = 'Flushed ipset entries from set {0} for {1}'.format(\n name,\n family\n )\n return ret\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to flush ipset entries from set {0} for {1}' \\\n ''.format(name, family)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef installed(name, # pylint: disable=C0103\n ruby=None,\n gem_bin=None,\n user=None,\n version=None,\n rdoc=False,\n ri=False,\n pre_releases=False,\n proxy=None,\n source=None): # pylint: disable=C0103\n '''\n Make sure that a gem is installed.\n\n name\n The name of the gem to install\n\n ruby: None\n Only for RVM or rbenv installations: the ruby version and gemset to\n target.\n\n gem_bin: None\n Custom ``gem`` command to run instead of the default.\n Use this to install gems to a non-default ruby install. If you are\n using rvm or rbenv use the ruby argument instead.\n\n user: None\n The user under which to run the ``gem`` command\n\n .. versionadded:: 0.17.0\n\n version : None\n Specify the version to install for the gem.\n Doesn't play nice with multiple gems at once\n\n rdoc : False\n Generate RDoc documentation for the gem(s).\n\n ri : False\n Generate RI documentation for the gem(s).\n\n pre_releases : False\n Install pre-release version of gem(s) if available.\n\n proxy : None\n Use the specified HTTP proxy server for all outgoing traffic.\n Format: http://hostname[:port]\n\n source : None\n Use the specified HTTP gem source server to download gem.\n Format: http://hostname[:port]\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n if ruby is not None and not(__salt__['rvm.is_installed'](runas=user) or __salt__['rbenv.is_installed'](runas=user)):\n log.warning(\n 'Use of argument ruby found, but neither rvm or rbenv is installed'\n )\n gems = __salt__['gem.list'](name, ruby, gem_bin=gem_bin, runas=user)\n if name in gems and version is not None:\n versions = list([x.replace('default: ', '') for x in gems[name]])\n match = re.match(r'(>=|>|<|<=)', version)\n if match:\n # Grab the comparison\n cmpr = match.group()\n\n # Clear out 'default:' and any whitespace\n installed_version = re.sub('default: ', '', gems[name][0]).strip()\n\n # Clear out comparison from version and whitespace\n desired_version = re.sub(cmpr, '', version).strip()\n\n if salt.utils.versions.compare(installed_version,\n cmpr,\n desired_version):\n ret['result'] = True\n ret['comment'] = 'Installed Gem meets version requirements.'\n return ret\n elif str(version) in versions:\n ret['result'] = True\n ret['comment'] = 'Gem is already installed.'\n return ret\n else:\n if str(version) in gems[name]:\n ret['result'] = True\n ret['comment'] = 'Gem is already installed.'\n return ret\n elif name in gems and version is None:\n ret['result'] = True\n ret['comment'] = 'Gem is already installed.'\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'The gem {0} would have been installed'.format(name)\n return ret\n if __salt__['gem.install'](name,\n ruby=ruby,\n gem_bin=gem_bin,\n runas=user,\n version=version,\n rdoc=rdoc,\n ri=ri,\n pre_releases=pre_releases,\n proxy=proxy,\n source=source):\n ret['result'] = True\n ret['changes'][name] = 'Installed'\n ret['comment'] = 'Gem was successfully installed'\n else:\n ret['result'] = False\n ret['comment'] = 'Could not install gem.'\n\n return ret", "response": "Ensure that a gem is installed."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef removed(name, ruby=None, user=None, gem_bin=None):\n '''\n Make sure that a gem is not installed.\n\n name\n The name of the gem to uninstall\n\n gem_bin : None\n Full path to ``gem`` binary to use.\n\n ruby : None\n If RVM or rbenv are installed, the ruby version and gemset to use.\n Ignored if ``gem_bin`` is specified.\n\n user: None\n The user under which to run the ``gem`` command\n\n .. versionadded:: 0.17.0\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n\n if name not in __salt__['gem.list'](name, ruby, gem_bin=gem_bin, runas=user):\n ret['result'] = True\n ret['comment'] = 'Gem is not installed.'\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'The gem {0} would have been removed'.format(name)\n return ret\n if __salt__['gem.uninstall'](name, ruby, gem_bin=gem_bin, runas=user):\n ret['result'] = True\n ret['changes'][name] = 'Removed'\n ret['comment'] = 'Gem was successfully removed.'\n else:\n ret['result'] = False\n ret['comment'] = 'Could not remove gem.'\n return ret", "response": "Ensure that a gem is not installed."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nadds a gem source to the base set.", "response": "def sources_add(name, ruby=None, user=None):\n '''\n Make sure that a gem source is added.\n\n name\n The URL of the gem source to be added\n\n ruby: None\n For RVM or rbenv installations: the ruby version and gemset to target.\n\n user: None\n The user under which to run the ``gem`` command\n\n .. versionadded:: 0.17.0\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n\n if name in __salt__['gem.sources_list'](ruby, runas=user):\n ret['result'] = True\n ret['comment'] = 'Gem source is already added.'\n return ret\n if __opts__['test']:\n ret['comment'] = 'The gem source {0} would have been added.'.format(name)\n return ret\n if __salt__['gem.sources_add'](source_uri=name, ruby=ruby, runas=user):\n ret['result'] = True\n ret['changes'][name] = 'Installed'\n ret['comment'] = 'Gem source was successfully added.'\n else:\n ret['result'] = False\n ret['comment'] = 'Could not add gem source.'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef sources_remove(name, ruby=None, user=None):\n '''\n Make sure that a gem source is removed.\n\n name\n The URL of the gem source to be removed\n\n ruby: None\n For RVM or rbenv installations: the ruby version and gemset to target.\n\n user: None\n The user under which to run the ``gem`` command\n\n .. versionadded:: 0.17.0\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n\n if name not in __salt__['gem.sources_list'](ruby, runas=user):\n ret['result'] = True\n ret['comment'] = 'Gem source is already removed.'\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'The gem source would have been removed.'\n return ret\n\n if __salt__['gem.sources_remove'](source_uri=name, ruby=ruby, runas=user):\n ret['result'] = True\n ret['changes'][name] = 'Removed'\n ret['comment'] = 'Gem source was successfully removed.'\n else:\n ret['result'] = False\n ret['comment'] = 'Could not remove gem source.'\n return ret", "response": "Remove a gem source from the current set."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nensure the current node is joined to a rabbit cluster with node user@host", "response": "def joined(name, host, user='rabbit', ram_node=None, runas='root'):\n '''\n Ensure the current node joined to a cluster with node user@host\n\n name\n Irrelevant, not used (recommended: user@host)\n user\n The user of node to join to (default: rabbit)\n host\n The host of node to join to\n ram_node\n Join node as a RAM node\n runas\n The user to run the rabbitmq command as\n '''\n\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n status = __salt__['rabbitmq.cluster_status']()\n if '{0}@{1}'.format(user, host) in status:\n ret['comment'] = 'Already in cluster'\n return ret\n\n if not __opts__['test']:\n result = __salt__['rabbitmq.join_cluster'](host,\n user,\n ram_node,\n runas=runas)\n if 'Error' in result:\n ret['result'] = False\n ret['comment'] = result['Error']\n return ret\n elif 'Join' in result:\n ret['comment'] = result['Join']\n\n # If we've reached this far before returning, we have changes.\n ret['changes'] = {'old': '', 'new': '{0}@{1}'.format(user, host)}\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Node is set to join cluster {0}@{1}'.format(\n user, host)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef image_create(auth=None, **kwargs):\n '''\n Create an image\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' glanceng.image_create name=cirros file=cirros.raw disk_format=raw\n salt '*' glanceng.image_create name=cirros file=cirros.raw disk_format=raw hw_scsi_model=virtio-scsi hw_disk_bus=scsi\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(keep_name=True, **kwargs)\n return cloud.create_image(**kwargs)", "response": "Create an image\n CLI Example :\n Create an image\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef image_delete(auth=None, **kwargs):\n '''\n Delete an image\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' glanceng.image_delete name=image1\n salt '*' glanceng.image_delete name=0e4febc2a5ab4f2c8f374b054162506d\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.delete_image(**kwargs)", "response": "Delete an image\n CLI Example :\n Delete an image\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef image_list(auth=None, **kwargs):\n '''\n List images\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' glanceng.image_list\n salt '*' glanceng.image_list\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.list_images(**kwargs)", "response": "List images in the nagios tree"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsearch for images CLI Example : Search for images", "response": "def image_search(auth=None, **kwargs):\n '''\n Search for images\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' glanceng.image_search name=image1\n salt '*' glanceng.image_search\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.search_images(**kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef image_get(auth=None, **kwargs):\n '''\n Get a single image\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' glanceng.image_get name=image1\n salt '*' glanceng.image_get name=0e4febc2a5ab4f2c8f374b054162506d\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.get_image(**kwargs)", "response": "Get a single image\n CLI Example :\n Get a single image\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef update_image_properties(auth=None, **kwargs):\n '''\n Update properties for an image\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' glanceng.update_image_properties name=image1 hw_scsi_model=virtio-scsi hw_disk_bus=scsi\n salt '*' glanceng.update_image_properties name=0e4febc2a5ab4f2c8f374b054162506d min_ram=1024\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.update_image_properties(**kwargs)", "response": "Update properties for an image\n CLI Example : bash\n\n name = image1 hw_scsi_model = virtio - scsi hw_disk_bus = virtio - scsi hw_disk_bus = virtio - scsi hw_disk_model = virtio - scsi hw_disk_bus = virtio - scsi hw_disk_bus = virtio - scsi hw_disk_bus = virtio - scsi"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndeletes a device from Vistara based on DNS name or partial name.", "response": "def delete_device(name, safety_on=True):\n '''\n Deletes a device from Vistara based on DNS name or partial name. By default,\n delete_device will only perform the delete if a single host is returned. Set\n safety_on=False to delete all matches (up to default API search page size)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run vistara.delete_device 'hostname-101.mycompany.com'\n salt-run vistara.delete_device 'hostname-101'\n salt-run vistara.delete_device 'hostname-1' safety_on=False\n\n '''\n\n config = _get_vistara_configuration()\n if not config:\n return False\n\n access_token = _get_oath2_access_token(config['client_key'], config['client_secret'])\n\n if not access_token:\n return 'Vistara access token not available'\n\n query_string = 'dnsName:{0}'.format(name)\n\n devices = _search_devices(query_string, config['client_id'], access_token)\n\n if not devices:\n return \"No devices found\"\n\n device_count = len(devices)\n\n if safety_on and device_count != 1:\n return \"Expected to delete 1 device and found {0}. \"\\\n \"Set safety_on=False to override.\".format(device_count)\n\n delete_responses = []\n for device in devices:\n device_id = device['id']\n log.debug(device_id)\n delete_response = _delete_resource(device_id, config['client_id'], access_token)\n if not delete_response:\n return False\n delete_responses.append(delete_response)\n\n return delete_responses"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget an access_token from the vistara API and return it", "response": "def _get_oath2_access_token(client_key, client_secret):\n '''\n Query the vistara API and get an access_token\n\n '''\n if not client_key and not client_secret:\n log.error(\n \"client_key and client_secret have not been specified \"\n \"and are required parameters.\"\n )\n return False\n\n method = 'POST'\n url = 'https://api.vistara.io/auth/oauth/token'\n headers = {\n 'Content-Type': 'application/x-www-form-urlencoded',\n 'Accept': 'application/json'\n }\n\n params = {\n 'grant_type': 'client_credentials',\n 'client_id': client_key,\n 'client_secret': client_secret\n }\n\n resp = salt.utils.http.query(\n url=url,\n method=method,\n header_dict=headers,\n params=params,\n opts=__opts__\n )\n\n respbody = resp.get('body', None)\n\n if not respbody:\n return False\n\n access_token = salt.utils.json.loads(respbody)['access_token']\n return access_token"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef version():\n '''\n Return server version (``apachectl -v``)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' apache.version\n '''\n cmd = '{0} -v'.format(_detect_os())\n out = __salt__['cmd.run'](cmd).splitlines()\n ret = out[0].split(': ')\n return ret[1]", "response": "Return server version (``apachectl -v``)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' apache.version"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn server version (``apachectl -V``) CLI Example: .. code-block:: bash salt '*' apache.fullversion", "response": "def fullversion():\n '''\n Return server version (``apachectl -V``)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' apache.fullversion\n '''\n cmd = '{0} -V'.format(_detect_os())\n ret = {}\n ret['compiled_with'] = []\n out = __salt__['cmd.run'](cmd).splitlines()\n # Example\n # -D APR_HAS_MMAP\n define_re = re.compile(r'^\\s+-D\\s+')\n for line in out:\n if ': ' in line:\n comps = line.split(': ')\n if not comps:\n continue\n ret[comps[0].strip().lower().replace(' ', '_')] = comps[1].strip()\n elif ' -D' in line:\n cwith = define_re.sub('', line)\n ret['compiled_with'].append(cwith)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef modules():\n '''\n Return list of static and shared modules (``apachectl -M``)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' apache.modules\n '''\n cmd = '{0} -M'.format(_detect_os())\n ret = {}\n ret['static'] = []\n ret['shared'] = []\n out = __salt__['cmd.run'](cmd).splitlines()\n for line in out:\n comps = line.split()\n if not comps:\n continue\n if '(static)' in line:\n ret['static'].append(comps[0])\n if '(shared)' in line:\n ret['shared'].append(comps[0])\n return ret", "response": "Return list of static and shared modules"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn list of modules compiled into the server", "response": "def servermods():\n '''\n Return list of modules compiled into the server (``apachectl -l``)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' apache.servermods\n '''\n cmd = '{0} -l'.format(_detect_os())\n ret = []\n out = __salt__['cmd.run'](cmd).splitlines()\n for line in out:\n if not line:\n continue\n if '.c' in line:\n ret.append(line.strip())\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns list of directives together with expected arguments and places where the directive is valid", "response": "def directives():\n '''\n Return list of directives together with expected arguments\n and places where the directive is valid (``apachectl -L``)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' apache.directives\n '''\n cmd = '{0} -L'.format(_detect_os())\n ret = {}\n out = __salt__['cmd.run'](cmd)\n out = out.replace('\\n\\t', '\\t')\n for line in out.splitlines():\n if not line:\n continue\n comps = line.split('\\t')\n desc = '\\n'.join(comps[1:])\n ret[comps[0]] = desc\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nshows the settings as parsed from the config file.", "response": "def vhosts():\n '''\n Show the settings as parsed from the config file (currently\n only shows the virtualhost settings) (``apachectl -S``).\n Because each additional virtual host adds to the execution\n time, this command may require a long timeout be specified\n by using ``-t 10``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt -t 10 '*' apache.vhosts\n '''\n cmd = '{0} -S'.format(_detect_os())\n ret = {}\n namevhost = ''\n out = __salt__['cmd.run'](cmd)\n for line in out.splitlines():\n if not line:\n continue\n comps = line.split()\n if 'is a NameVirtualHost' in line:\n namevhost = comps[0]\n ret[namevhost] = {}\n else:\n if comps[0] == 'default':\n ret[namevhost]['default'] = {}\n ret[namevhost]['default']['vhost'] = comps[2]\n ret[namevhost]['default']['conf'] = re.sub(\n r'\\(|\\)',\n '',\n comps[3]\n )\n if comps[0] == 'port':\n ret[namevhost][comps[3]] = {}\n ret[namevhost][comps[3]]['vhost'] = comps[3]\n ret[namevhost][comps[3]]['conf'] = re.sub(\n r'\\(|\\)',\n '',\n comps[4]\n )\n ret[namevhost][comps[3]]['port'] = comps[1]\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef signal(signal=None):\n '''\n Signals httpd to start, restart, or stop.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' apache.signal restart\n '''\n no_extra_args = ('configtest', 'status', 'fullstatus')\n valid_signals = ('start', 'stop', 'restart', 'graceful', 'graceful-stop')\n\n if signal not in valid_signals and signal not in no_extra_args:\n return\n # Make sure you use the right arguments\n if signal in valid_signals:\n arguments = ' -k {0}'.format(signal)\n else:\n arguments = ' {0}'.format(signal)\n cmd = _detect_os() + arguments\n out = __salt__['cmd.run_all'](cmd)\n\n # A non-zero return code means fail\n if out['retcode'] and out['stderr']:\n ret = out['stderr'].strip()\n # 'apachectl configtest' returns 'Syntax OK' to stderr\n elif out['stderr']:\n ret = out['stderr'].strip()\n elif out['stdout']:\n ret = out['stdout'].strip()\n # No output for something like: apachectl graceful\n else:\n ret = 'Command: \"{0}\" completed successfully!'.format(cmd)\n return ret", "response": "Signal httpd to start restart or stop."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef useradd(pwfile, user, password, opts=''):\n '''\n Add HTTP user using the ``htpasswd`` command. If the ``htpasswd`` file does not\n exist, it will be created. Valid options that can be passed are:\n\n .. code-block:: text\n\n n Don't update file; display results on stdout.\n m Force MD5 hashing of the password (default).\n d Force CRYPT(3) hashing of the password.\n p Do not hash the password (plaintext).\n s Force SHA1 hashing of the password.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' apache.useradd /etc/httpd/htpasswd larry badpassword\n salt '*' apache.useradd /etc/httpd/htpasswd larry badpass opts=ns\n '''\n return __salt__['webutil.useradd'](pwfile, user, password, opts)", "response": "Add a user using the apache useradd command"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef server_status(profile='default'):\n '''\n Get Information from the Apache server-status handler\n\n .. note::\n\n The server-status handler is disabled by default.\n In order for this function to work it needs to be enabled.\n See http://httpd.apache.org/docs/2.2/mod/mod_status.html\n\n The following configuration needs to exists in pillar/grains.\n Each entry nested in ``apache.server-status`` is a profile of a vhost/server.\n This would give support for multiple apache servers/vhosts.\n\n .. code-block:: yaml\n\n apache.server-status:\n default:\n url: http://localhost/server-status\n user: someuser\n pass: password\n realm: 'authentication realm for digest passwords'\n timeout: 5\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' apache.server_status\n salt '*' apache.server_status other-profile\n '''\n ret = {\n 'Scoreboard': {\n '_': 0,\n 'S': 0,\n 'R': 0,\n 'W': 0,\n 'K': 0,\n 'D': 0,\n 'C': 0,\n 'L': 0,\n 'G': 0,\n 'I': 0,\n '.': 0,\n },\n }\n\n # Get configuration from pillar\n url = __salt__['config.get'](\n 'apache.server-status:{0}:url'.format(profile),\n 'http://localhost/server-status'\n )\n user = __salt__['config.get'](\n 'apache.server-status:{0}:user'.format(profile),\n ''\n )\n passwd = __salt__['config.get'](\n 'apache.server-status:{0}:pass'.format(profile),\n ''\n )\n realm = __salt__['config.get'](\n 'apache.server-status:{0}:realm'.format(profile),\n ''\n )\n timeout = __salt__['config.get'](\n 'apache.server-status:{0}:timeout'.format(profile),\n 5\n )\n\n # create authentication handler if configuration exists\n if user and passwd:\n basic = _HTTPBasicAuthHandler()\n basic.add_password(realm=realm, uri=url, user=user, passwd=passwd)\n digest = _HTTPDigestAuthHandler()\n digest.add_password(realm=realm, uri=url, user=user, passwd=passwd)\n _install_opener(_build_opener(basic, digest))\n\n # get http data\n url += '?auto'\n try:\n response = _urlopen(url, timeout=timeout).read().splitlines()\n except URLError:\n return 'error'\n\n # parse the data\n for line in response:\n splt = line.split(':', 1)\n splt[0] = splt[0].strip()\n splt[1] = splt[1].strip()\n\n if splt[0] == 'Scoreboard':\n for c in splt[1]:\n ret['Scoreboard'][c] += 1\n else:\n if splt[1].isdigit():\n ret[splt[0]] = int(splt[1])\n else:\n ret[splt[0]] = float(splt[1])\n\n # return the good stuff\n return ret", "response": "Return the server - status information from the Apache server - status handler."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _parse_config(conf, slot=None):\n '''\n Recursively goes through config structure and builds final Apache configuration\n\n :param conf: defined config structure\n :param slot: name of section container if needed\n '''\n ret = cStringIO()\n if isinstance(conf, six.string_types):\n if slot:\n print('{0} {1}'.format(slot, conf), file=ret, end='')\n else:\n print('{0}'.format(conf), file=ret, end='')\n elif isinstance(conf, list):\n is_section = False\n for item in conf:\n if 'this' in item:\n is_section = True\n slot_this = six.text_type(item['this'])\n if is_section:\n print('<{0} {1}>'.format(slot, slot_this), file=ret)\n for item in conf:\n for key, val in item.items():\n if key != 'this':\n print(_parse_config(val, six.text_type(key)), file=ret)\n print(''.format(slot), file=ret)\n else:\n for value in conf:\n print(_parse_config(value, six.text_type(slot)), file=ret)\n elif isinstance(conf, dict):\n try:\n print('<{0} {1}>'.format(slot, conf['this']), file=ret)\n except KeyError:\n raise SaltException('Apache section container \"<{0}>\" expects attribute. '\n 'Specify it using key \"this\".'.format(slot))\n for key, value in six.iteritems(conf):\n if key != 'this':\n if isinstance(value, six.string_types):\n print('{0} {1}'.format(key, value), file=ret)\n elif isinstance(value, list):\n print(_parse_config(value, key), file=ret)\n elif isinstance(value, dict):\n print(_parse_config(value, key), file=ret)\n print(''.format(slot), file=ret)\n\n ret.seek(0)\n return ret.read()", "response": "Recursively parses the Apache configuration structure and returns the contents of the Apache configuration structure."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a virtual host configuration file for the given name and configuration dict.", "response": "def config(name, config, edit=True):\n '''\n Create VirtualHost configuration files\n\n name\n File for the virtual host\n config\n VirtualHost configurations\n\n .. note::\n\n This function is not meant to be used from the command line.\n Config is meant to be an ordered dict of all of the apache configs.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' apache.config /etc/httpd/conf.d/ports.conf config=\"[{'Listen': '22'}]\"\n '''\n\n configs = []\n for entry in config:\n key = next(six.iterkeys(entry))\n configs.append(_parse_config(entry[key], key))\n\n # Python auto-correct line endings\n configstext = '\\n'.join(salt.utils.data.decode(configs))\n if edit:\n with salt.utils.files.fopen(name, 'w') as configfile:\n configfile.write('# This file is managed by Salt.\\n')\n configfile.write(salt.utils.stringutils.to_str(configstext))\n return configstext"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef present(\n name,\n attributes=None,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n):\n '''\n Ensure the SQS queue exists.\n\n name\n Name of the SQS queue.\n\n attributes\n A dict of key/value SQS attributes.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n '''\n ret = {\n 'name': name,\n 'result': True,\n 'comment': [],\n 'changes': {},\n }\n\n r = __salt__['boto_sqs.exists'](\n name,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if 'error' in r:\n ret['result'] = False\n ret['comment'].append(r['error'])\n return ret\n\n if r['result']:\n ret['comment'].append('SQS queue {0} present.'.format(name))\n else:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'].append(\n 'SQS queue {0} is set to be created.'.format(name),\n )\n ret['changes'] = {'old': None, 'new': name}\n return ret\n\n r = __salt__['boto_sqs.create'](\n name,\n attributes=attributes,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if 'error' in r:\n ret['result'] = False\n ret['comment'].append(\n 'Failed to create SQS queue {0}: {1}'.format(name, r['error']),\n )\n return ret\n\n ret['comment'].append('SQS queue {0} created.'.format(name))\n ret['changes']['old'] = None\n ret['changes']['new'] = name\n # Return immediately, as the create call also set all attributes\n return ret\n\n if not attributes:\n return ret\n\n r = __salt__['boto_sqs.get_attributes'](\n name,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if 'error' in r:\n ret['result'] = False\n ret['comment'].append(\n 'Failed to get queue attributes: {0}'.format(r['error']),\n )\n return ret\n current_attributes = r['result']\n\n attrs_to_set = {}\n for attr, val in six.iteritems(attributes):\n _val = current_attributes.get(attr, None)\n if attr == 'Policy':\n # Normalize by brute force\n if isinstance(_val, six.string_types):\n _val = salt.utils.json.loads(_val)\n if isinstance(val, six.string_types):\n val = salt.utils.json.loads(val)\n if _val != val:\n log.debug('Policies differ:\\n%s\\n%s', _val, val)\n attrs_to_set[attr] = salt.utils.json.dumps(val, sort_keys=True)\n elif six.text_type(_val) != six.text_type(val):\n log.debug('Attributes differ:\\n%s\\n%s', _val, val)\n attrs_to_set[attr] = val\n attr_names = ', '.join(attrs_to_set)\n\n if not attrs_to_set:\n ret['comment'].append('Queue attributes already set correctly.')\n return ret\n\n final_attributes = current_attributes.copy()\n final_attributes.update(attrs_to_set)\n\n def _yaml_safe_dump(attrs):\n '''\n Safely dump YAML using a readable flow style\n '''\n dumper = __utils__['yaml.get_dumper']('IndentedSafeOrderedDumper')\n return __utils__['yaml.dump'](\n attrs,\n default_flow_style=False,\n Dumper=dumper)\n\n attributes_diff = ''.join(difflib.unified_diff(\n _yaml_safe_dump(current_attributes).splitlines(True),\n _yaml_safe_dump(final_attributes).splitlines(True),\n ))\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'].append(\n 'Attribute(s) {0} set to be updated:\\n{1}'.format(\n attr_names,\n attributes_diff,\n )\n )\n ret['changes'] = {'attributes': {'diff': attributes_diff}}\n return ret\n\n r = __salt__['boto_sqs.set_attributes'](\n name,\n attrs_to_set,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if 'error' in r:\n ret['result'] = False\n ret['comment'].append(\n 'Failed to set queue attributes: {0}'.format(r['error']),\n )\n return ret\n\n ret['comment'].append(\n 'Updated SQS queue attribute(s) {0}.'.format(attr_names),\n )\n ret['changes']['attributes'] = {'diff': attributes_diff}\n return ret", "response": "Ensure the SQS queue with the given attributes is present."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef absent(\n name,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n):\n '''\n Ensure the named sqs queue is deleted.\n\n name\n Name of the SQS queue.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n r = __salt__['boto_sqs.exists'](\n name,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = six.text_type(r['error'])\n return ret\n\n if not r['result']:\n ret['comment'] = 'SQS queue {0} does not exist in {1}.'.format(\n name,\n region,\n )\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'SQS queue {0} is set to be removed.'.format(name)\n ret['changes'] = {'old': name, 'new': None}\n return ret\n\n r = __salt__['boto_sqs.delete'](\n name,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = six.text_type(r['error'])\n return ret\n\n ret['comment'] = 'SQS queue {0} was deleted.'.format(name)\n ret['changes']['old'] = name\n ret['changes']['new'] = None\n return ret", "response": "Ensure the named sqs queue is absent in the named region."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndetermining if the group changes in the current set.", "response": "def _group_changes(cur, wanted, remove=False):\n '''\n Determine if the groups need to be changed\n '''\n old = set(cur)\n new = set(wanted)\n if (remove and old != new) or (not remove and not new.issubset(old)):\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _changes(name,\n uid=None,\n gid=None,\n groups=None,\n optional_groups=None,\n remove_groups=True,\n home=None,\n createhome=True,\n password=None,\n enforce_password=True,\n empty_password=False,\n shell=None,\n fullname='',\n roomnumber='',\n workphone='',\n homephone='',\n other='',\n loginclass=None,\n date=None,\n mindays=0,\n maxdays=999999,\n inactdays=0,\n warndays=7,\n expire=None,\n win_homedrive=None,\n win_profile=None,\n win_logonscript=None,\n win_description=None,\n allow_uid_change=False,\n allow_gid_change=False):\n '''\n Return a dict of the changes required for a user if the user is present,\n otherwise return False.\n\n Updated in 2015.8.0 to include support for windows homedrive, profile,\n logonscript, and description fields.\n\n Updated in 2014.7.0 to include support for shadow attributes, all\n attributes supported as integers only.\n '''\n\n if 'shadow.info' in __salt__:\n lshad = __salt__['shadow.info'](name)\n\n lusr = __salt__['user.info'](name)\n if not lusr:\n return False\n\n change = {}\n if groups is None:\n groups = lusr['groups']\n wanted_groups = sorted(set((groups or []) + (optional_groups or [])))\n if uid and lusr['uid'] != uid:\n change['uid'] = uid\n if gid is not None and lusr['gid'] not in (gid, __salt__['file.group_to_gid'](gid)):\n change['gid'] = gid\n default_grp = __salt__['file.gid_to_group'](\n gid if gid is not None else lusr['gid']\n )\n # remove the default group from the list for comparison purposes\n if default_grp in lusr['groups']:\n lusr['groups'].remove(default_grp)\n if name in lusr['groups'] and name not in wanted_groups:\n lusr['groups'].remove(name)\n # remove default group from wanted_groups, as this requirement is\n # already met\n if default_grp in wanted_groups:\n wanted_groups.remove(default_grp)\n if _group_changes(lusr['groups'], wanted_groups, remove_groups):\n change['groups'] = wanted_groups\n if home and lusr['home'] != home:\n change['home'] = home\n if createhome:\n newhome = home if home else lusr['home']\n if newhome is not None and not os.path.isdir(newhome):\n change['homeDoesNotExist'] = newhome\n if shell and lusr['shell'] != shell:\n change['shell'] = shell\n if 'shadow.info' in __salt__ and 'shadow.default_hash' in __salt__:\n if password and not empty_password:\n default_hash = __salt__['shadow.default_hash']()\n if lshad['passwd'] == default_hash \\\n or lshad['passwd'] != default_hash and enforce_password:\n if lshad['passwd'] != password:\n change['passwd'] = password\n if empty_password and lshad['passwd'] != '':\n change['empty_password'] = True\n if date is not None and lshad['lstchg'] != date:\n change['date'] = date\n if mindays is not None and lshad['min'] != mindays:\n change['mindays'] = mindays\n if maxdays is not None and lshad['max'] != maxdays:\n change['maxdays'] = maxdays\n if inactdays is not None and lshad['inact'] != inactdays:\n change['inactdays'] = inactdays\n if warndays is not None and lshad['warn'] != warndays:\n change['warndays'] = warndays\n if expire and lshad['expire'] != expire:\n change['expire'] = expire\n elif 'shadow.info' in __salt__ and salt.utils.platform.is_windows():\n if expire and expire is not -1 and salt.utils.dateutils.strftime(lshad['expire']) != salt.utils.dateutils.strftime(expire):\n change['expire'] = expire\n\n # GECOS fields\n fullname = salt.utils.data.decode(fullname)\n lusr['fullname'] = salt.utils.data.decode(lusr['fullname'])\n if fullname is not None and lusr['fullname'] != fullname:\n change['fullname'] = fullname\n if win_homedrive and lusr['homedrive'] != win_homedrive:\n change['homedrive'] = win_homedrive\n if win_profile and lusr['profile'] != win_profile:\n change['profile'] = win_profile\n if win_logonscript and lusr['logonscript'] != win_logonscript:\n change['logonscript'] = win_logonscript\n if win_description and lusr['description'] != win_description:\n change['description'] = win_description\n\n # MacOS doesn't have full GECOS support, so check for the \"ch\" functions\n # and ignore these parameters if these functions do not exist.\n if 'user.chroomnumber' in __salt__ \\\n and roomnumber is not None:\n roomnumber = salt.utils.data.decode(roomnumber)\n lusr['roomnumber'] = salt.utils.data.decode(lusr['roomnumber'])\n if lusr['roomnumber'] != roomnumber:\n change['roomnumber'] = roomnumber\n if 'user.chworkphone' in __salt__ \\\n and workphone is not None:\n workphone = salt.utils.data.decode(workphone)\n lusr['workphone'] = salt.utils.data.decode(lusr['workphone'])\n if lusr['workphone'] != workphone:\n change['workphone'] = workphone\n if 'user.chhomephone' in __salt__ \\\n and homephone is not None:\n homephone = salt.utils.data.decode(homephone)\n lusr['homephone'] = salt.utils.data.decode(lusr['homephone'])\n if lusr['homephone'] != homephone:\n change['homephone'] = homephone\n if 'user.chother' in __salt__ and other is not None:\n other = salt.utils.data.decode(other)\n lusr['other'] = salt.utils.data.decode(lusr['other'])\n if lusr['other'] != other:\n change['other'] = other\n # OpenBSD/FreeBSD login class\n if __grains__['kernel'] in ('OpenBSD', 'FreeBSD'):\n if loginclass:\n if __salt__['user.get_loginclass'](name) != loginclass:\n change['loginclass'] = loginclass\n\n errors = []\n if not allow_uid_change and 'uid' in change:\n errors.append(\n 'Changing uid ({0} -> {1}) not permitted, set allow_uid_change to '\n 'True to force this change. Note that this will not change file '\n 'ownership.'.format(lusr['uid'], uid)\n )\n if not allow_gid_change and 'gid' in change:\n errors.append(\n 'Changing gid ({0} -> {1}) not permitted, set allow_gid_change to '\n 'True to force this change. Note that this will not change file '\n 'ownership.'.format(lusr['gid'], gid)\n )\n if errors:\n raise CommandExecutionError(\n 'Encountered error checking for needed changes',\n info=errors\n )\n\n return change", "response": "Return a dict of the changes required for a user."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nensure that the named user is present with the specified properties.", "response": "def present(name,\n uid=None,\n gid=None,\n usergroup=None,\n groups=None,\n optional_groups=None,\n remove_groups=True,\n home=None,\n createhome=True,\n password=None,\n hash_password=False,\n enforce_password=True,\n empty_password=False,\n shell=None,\n unique=True,\n system=False,\n fullname=None,\n roomnumber=None,\n workphone=None,\n homephone=None,\n other=None,\n loginclass=None,\n date=None,\n mindays=None,\n maxdays=None,\n inactdays=None,\n warndays=None,\n expire=None,\n win_homedrive=None,\n win_profile=None,\n win_logonscript=None,\n win_description=None,\n nologinit=False,\n allow_uid_change=False,\n allow_gid_change=False):\n '''\n Ensure that the named user is present with the specified properties\n\n name\n The name of the user to manage\n\n uid\n The user id to assign. If not specified, and the user does not exist,\n then the next available uid will be assigned.\n\n gid\n The id of the default group to assign to the user. Either a group name\n or gid can be used. If not specified, and the user does not exist, then\n the next available gid will be assigned.\n\n allow_uid_change : False\n Set to ``True`` to allow the state to update the uid.\n\n .. versionadded:: 2018.3.1\n\n allow_gid_change : False\n Set to ``True`` to allow the state to update the gid.\n\n .. versionadded:: 2018.3.1\n\n usergroup\n If True, a group with the same name as the user will be created. If\n False, a group with the same name as the user will not be created. The\n default is distribution-specific. See the USERGROUPS_ENAB section of\n the login.defs(5) man page.\n\n .. note::\n Only supported on GNU/Linux distributions\n\n .. versionadded:: Fluorine\n\n groups\n A list of groups to assign the user to, pass a list object. If a group\n specified here does not exist on the minion, the state will fail.\n If set to the empty list, the user will be removed from all groups\n except the default group. If unset, salt will assume current groups\n are still wanted (see issue #28706).\n\n optional_groups\n A list of groups to assign the user to, pass a list object. If a group\n specified here does not exist on the minion, the state will silently\n ignore it.\n\n NOTE: If the same group is specified in both \"groups\" and\n \"optional_groups\", then it will be assumed to be required and not optional.\n\n remove_groups\n Remove groups that the user is a member of that weren't specified in\n the state, Default is ``True``.\n\n home\n The custom login directory of user. Uses default value of underlying\n system if not set. Notice that this directory does not have to exist.\n This also the location of the home directory to create if createhome is\n set to True.\n\n createhome : True\n If set to ``False``, the home directory will not be created if it\n doesn't already exist.\n\n .. warning::\n Not supported on Windows or Mac OS.\n\n Additionally, parent directories will *not* be created. The parent\n directory for ``home`` must already exist.\n\n nologinit : False\n If set to ``True``, it will not add the user to lastlog and faillog\n databases.\n\n .. note::\n Not supported on Windows or Mac OS.\n\n password\n A password hash to set for the user. This field is only supported on\n Linux, FreeBSD, NetBSD, OpenBSD, and Solaris. If the ``empty_password``\n argument is set to ``True`` then ``password`` is ignored.\n For Windows this is the plain text password.\n For Linux, the hash can be generated with ``mkpasswd -m sha-256``.\n\n .. versionchanged:: 0.16.0\n BSD support added.\n\n hash_password\n Set to True to hash the clear text password. Default is ``False``.\n\n\n enforce_password\n Set to False to keep the password from being changed if it has already\n been set and the password hash differs from what is specified in the\n \"password\" field. This option will be ignored if \"password\" is not\n specified, Default is ``True``.\n\n empty_password\n Set to True to enable password-less login for user, Default is ``False``.\n\n shell\n The login shell, defaults to the system default shell\n\n unique\n Require a unique UID, Default is ``True``.\n\n system\n Choose UID in the range of FIRST_SYSTEM_UID and LAST_SYSTEM_UID, Default is\n ``False``.\n\n loginclass\n The login class, defaults to empty\n (BSD only)\n\n User comment field (GECOS) support (currently Linux, BSD, and MacOS\n only):\n\n The below values should be specified as strings to avoid ambiguities when\n the values are loaded. (Especially the phone and room number fields which\n are likely to contain numeric data)\n\n fullname\n The user's full name\n\n roomnumber\n The user's room number (not supported in MacOS)\n\n workphone\n The user's work phone number (not supported in MacOS)\n\n homephone\n The user's home phone number (not supported in MacOS)\n\n other\n The user's other attribute (not supported in MacOS)\n If GECOS field contains more than 4 commas, this field will have the rest of 'em\n\n .. versionchanged:: 2014.7.0\n Shadow attribute support added.\n\n Shadow attributes support (currently Linux only):\n\n The below values should be specified as integers.\n\n date\n Date of last change of password, represented in days since epoch\n (January 1, 1970).\n\n mindays\n The minimum number of days between password changes.\n\n maxdays\n The maximum number of days between password changes.\n\n inactdays\n The number of days after a password expires before an account is\n locked.\n\n warndays\n Number of days prior to maxdays to warn users.\n\n expire\n Date that account expires, represented in days since epoch (January 1,\n 1970).\n\n The below parameters apply to windows only:\n\n win_homedrive (Windows Only)\n The drive letter to use for the home directory. If not specified the\n home directory will be a unc path. Otherwise the home directory will be\n mapped to the specified drive. Must be a letter followed by a colon.\n Because of the colon, the value must be surrounded by single quotes. ie:\n - win_homedrive: 'U:\n\n .. versionchanged:: 2015.8.0\n\n win_profile (Windows Only)\n The custom profile directory of the user. Uses default value of\n underlying system if not set.\n\n .. versionchanged:: 2015.8.0\n\n win_logonscript (Windows Only)\n The full path to the logon script to run when the user logs in.\n\n .. versionchanged:: 2015.8.0\n\n win_description (Windows Only)\n A brief description of the purpose of the users account.\n\n .. versionchanged:: 2015.8.0\n '''\n # First check if a password is set. If password is set, check if\n # hash_password is True, then hash it.\n if password and hash_password:\n log.debug('Hashing a clear text password')\n # in case a password is already set, it will contain a Salt\n # which should be re-used to generate the new hash, other-\n # wise the Salt will be generated randomly, causing the\n # hash to change each time and thereby making the\n # user.present state non-idempotent.\n algorithms = {\n '1': 'md5',\n '2a': 'blowfish',\n '5': 'sha256',\n '6': 'sha512',\n }\n try:\n _, algo, shadow_salt, shadow_hash = __salt__['shadow.info'](name)['passwd'].split('$', 4)\n if algo == '1':\n log.warning('Using MD5 for hashing passwords is considered insecure!')\n log.debug('Re-using existing shadow salt for hashing password using %s', algorithms.get(algo))\n password = __salt__['shadow.gen_password'](password, crypt_salt=shadow_salt, algorithm=algorithms.get(algo))\n except ValueError:\n log.info('No existing shadow salt found, defaulting to a randomly generated new one')\n password = __salt__['shadow.gen_password'](password)\n\n if fullname is not None:\n fullname = salt.utils.data.decode(fullname)\n if roomnumber is not None:\n roomnumber = salt.utils.data.decode(roomnumber)\n if workphone is not None:\n workphone = salt.utils.data.decode(workphone)\n if homephone is not None:\n homephone = salt.utils.data.decode(homephone)\n if other is not None:\n other = salt.utils.data.decode(other)\n\n # createhome not supported on Windows or Mac\n if __grains__['kernel'] in ('Darwin', 'Windows'):\n createhome = False\n\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'User {0} is present and up to date'.format(name)}\n\n # the comma is used to separate field in GECOS, thus resulting into\n # salt adding the end of fullname each time this function is called\n for gecos_field in [fullname, roomnumber, workphone]:\n if isinstance(gecos_field, string_types) and ',' in gecos_field:\n ret['comment'] = \"Unsupported char ',' in {0}\".format(gecos_field)\n ret['result'] = False\n return ret\n\n if groups:\n missing_groups = [x for x in groups if not __salt__['group.info'](x)]\n if missing_groups:\n ret['comment'] = 'The following group(s) are not present: ' \\\n '{0}'.format(','.join(missing_groups))\n ret['result'] = False\n return ret\n\n if optional_groups:\n present_optgroups = [x for x in optional_groups\n if __salt__['group.info'](x)]\n for missing_optgroup in [x for x in optional_groups\n if x not in present_optgroups]:\n log.debug(\n 'Optional group \"%s\" for user \"%s\" is not present',\n missing_optgroup, name\n )\n else:\n present_optgroups = None\n\n # Log a warning for all groups specified in both \"groups\" and\n # \"optional_groups\" lists.\n if groups and optional_groups:\n for isected in set(groups).intersection(optional_groups):\n log.warning(\n 'Group \"%s\" specified in both groups and optional_groups '\n 'for user %s', isected, name\n )\n\n # If usergroup was specified, we'll also be creating a new\n # group. We should report this change without setting the gid\n # variable.\n if usergroup and __salt__['file.group_to_gid'](name) != '':\n changes_gid = name\n else:\n changes_gid = gid\n\n try:\n changes = _changes(name,\n uid,\n changes_gid,\n groups,\n present_optgroups,\n remove_groups,\n home,\n createhome,\n password,\n enforce_password,\n empty_password,\n shell,\n fullname,\n roomnumber,\n workphone,\n homephone,\n other,\n loginclass,\n date,\n mindays,\n maxdays,\n inactdays,\n warndays,\n expire,\n win_homedrive,\n win_profile,\n win_logonscript,\n win_description,\n allow_uid_change,\n allow_gid_change)\n except CommandExecutionError as exc:\n ret['result'] = False\n ret['comment'] = exc.strerror\n return ret\n\n if changes:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = ('The following user attributes are set to be '\n 'changed:\\n')\n for key, val in iteritems(changes):\n if key == 'passwd':\n val = 'XXX-REDACTED-XXX'\n elif key == 'group' and not remove_groups:\n key = 'ensure groups'\n ret['comment'] += '{0}: {1}\\n'.format(key, val)\n return ret\n # The user is present\n if 'shadow.info' in __salt__:\n lshad = __salt__['shadow.info'](name)\n if __grains__['kernel'] in ('OpenBSD', 'FreeBSD'):\n lcpre = __salt__['user.get_loginclass'](name)\n pre = __salt__['user.info'](name)\n for key, val in iteritems(changes):\n if key == 'passwd' and not empty_password:\n __salt__['shadow.set_password'](name, password)\n continue\n if key == 'passwd' and empty_password:\n log.warning(\"No password will be set when empty_password=True\")\n continue\n if key == 'empty_password' and val:\n __salt__['shadow.del_password'](name)\n continue\n if key == 'date':\n __salt__['shadow.set_date'](name, date)\n continue\n # run chhome once to avoid any possible bad side-effect\n if key == 'home' and 'homeDoesNotExist' not in changes:\n if __grains__['kernel'] in ('Darwin', 'Windows'):\n __salt__['user.chhome'](name, val)\n else:\n __salt__['user.chhome'](name, val, persist=False)\n continue\n if key == 'homeDoesNotExist':\n if __grains__['kernel'] in ('Darwin', 'Windows'):\n __salt__['user.chhome'](name, val)\n else:\n __salt__['user.chhome'](name, val, persist=True)\n if not os.path.isdir(val):\n __salt__['file.mkdir'](val, pre['uid'], pre['gid'], 0o755)\n continue\n if key == 'mindays':\n __salt__['shadow.set_mindays'](name, mindays)\n continue\n if key == 'maxdays':\n __salt__['shadow.set_maxdays'](name, maxdays)\n continue\n if key == 'inactdays':\n __salt__['shadow.set_inactdays'](name, inactdays)\n continue\n if key == 'warndays':\n __salt__['shadow.set_warndays'](name, warndays)\n continue\n if key == 'expire':\n __salt__['shadow.set_expire'](name, expire)\n continue\n if key == 'win_homedrive':\n __salt__['user.update'](name=name, homedrive=val)\n continue\n if key == 'win_profile':\n __salt__['user.update'](name=name, profile=val)\n continue\n if key == 'win_logonscript':\n __salt__['user.update'](name=name, logonscript=val)\n continue\n if key == 'win_description':\n __salt__['user.update'](name=name, description=val)\n continue\n if key == 'groups':\n __salt__['user.ch{0}'.format(key)](\n name, val, not remove_groups\n )\n else:\n __salt__['user.ch{0}'.format(key)](name, val)\n\n post = __salt__['user.info'](name)\n spost = {}\n if 'shadow.info' in __salt__ and lshad['passwd'] != password:\n spost = __salt__['shadow.info'](name)\n if __grains__['kernel'] in ('OpenBSD', 'FreeBSD'):\n lcpost = __salt__['user.get_loginclass'](name)\n # See if anything changed\n for key in post:\n if post[key] != pre[key]:\n ret['changes'][key] = post[key]\n if 'shadow.info' in __salt__:\n for key in spost:\n if lshad[key] != spost[key]:\n if key == 'passwd':\n ret['changes'][key] = 'XXX-REDACTED-XXX'\n else:\n ret['changes'][key] = spost[key]\n if __grains__['kernel'] in ('OpenBSD', 'FreeBSD') and lcpost != lcpre:\n ret['changes']['loginclass'] = lcpost\n if ret['changes']:\n ret['comment'] = 'Updated user {0}'.format(name)\n changes = _changes(name,\n uid,\n gid,\n groups,\n present_optgroups,\n remove_groups,\n home,\n createhome,\n password,\n enforce_password,\n empty_password,\n shell,\n fullname,\n roomnumber,\n workphone,\n homephone,\n other,\n loginclass,\n date,\n mindays,\n maxdays,\n inactdays,\n warndays,\n expire,\n win_homedrive,\n win_profile,\n win_logonscript,\n win_description,\n allow_uid_change=True,\n allow_gid_change=True)\n # allow_uid_change and allow_gid_change passed as True to avoid race\n # conditions where a uid/gid is modified outside of Salt. If an\n # unauthorized change was requested, it would have been caught the\n # first time we ran _changes().\n\n if changes:\n ret['comment'] = 'These values could not be changed: {0}'.format(\n changes\n )\n ret['result'] = False\n return ret\n\n if changes is False:\n # The user is not present, make it!\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'User {0} set to be added'.format(name)\n return ret\n if groups and present_optgroups:\n groups.extend(present_optgroups)\n elif present_optgroups:\n groups = present_optgroups[:]\n\n # Setup params specific to Linux and Windows to be passed to the\n # add.user function\n if not salt.utils.platform.is_windows():\n params = {'name': name,\n 'uid': uid,\n 'gid': gid,\n 'groups': groups,\n 'home': home,\n 'shell': shell,\n 'unique': unique,\n 'system': system,\n 'fullname': fullname,\n 'roomnumber': roomnumber,\n 'workphone': workphone,\n 'homephone': homephone,\n 'other': other,\n 'createhome': createhome,\n 'nologinit': nologinit,\n 'loginclass': loginclass,\n 'usergroup': usergroup}\n else:\n params = ({'name': name,\n 'password': password,\n 'fullname': fullname,\n 'description': win_description,\n 'groups': groups,\n 'home': home,\n 'homedrive': win_homedrive,\n 'profile': win_profile,\n 'logonscript': win_logonscript})\n\n if __salt__['user.add'](**params):\n ret['comment'] = 'New user {0} created'.format(name)\n ret['changes'] = __salt__['user.info'](name)\n if not createhome:\n # pwd incorrectly reports presence of home\n ret['changes']['home'] = ''\n if 'shadow.info' in __salt__ \\\n and not salt.utils.platform.is_windows() \\\n and not salt.utils.platform.is_darwin():\n if password and not empty_password:\n __salt__['shadow.set_password'](name, password)\n spost = __salt__['shadow.info'](name)\n if spost['passwd'] != password:\n ret['comment'] = 'User {0} created but failed to set' \\\n ' password to' \\\n ' {1}'.format(name, 'XXX-REDACTED-XXX')\n ret['result'] = False\n ret['changes']['password'] = 'XXX-REDACTED-XXX'\n if empty_password and not password:\n __salt__['shadow.del_password'](name)\n spost = __salt__['shadow.info'](name)\n if spost['passwd'] != '':\n ret['comment'] = 'User {0} created but failed to ' \\\n 'empty password'.format(name)\n ret['result'] = False\n ret['changes']['password'] = ''\n if date is not None:\n __salt__['shadow.set_date'](name, date)\n spost = __salt__['shadow.info'](name)\n if spost['lstchg'] != date:\n ret['comment'] = 'User {0} created but failed to set' \\\n ' last change date to' \\\n ' {1}'.format(name, date)\n ret['result'] = False\n ret['changes']['date'] = date\n if mindays:\n __salt__['shadow.set_mindays'](name, mindays)\n spost = __salt__['shadow.info'](name)\n if spost['min'] != mindays:\n ret['comment'] = 'User {0} created but failed to set' \\\n ' minimum days to' \\\n ' {1}'.format(name, mindays)\n ret['result'] = False\n ret['changes']['mindays'] = mindays\n if maxdays:\n __salt__['shadow.set_maxdays'](name, maxdays)\n spost = __salt__['shadow.info'](name)\n if spost['max'] != maxdays:\n ret['comment'] = 'User {0} created but failed to set' \\\n ' maximum days to' \\\n ' {1}'.format(name, maxdays)\n ret['result'] = False\n ret['changes']['maxdays'] = maxdays\n if inactdays:\n __salt__['shadow.set_inactdays'](name, inactdays)\n spost = __salt__['shadow.info'](name)\n if spost['inact'] != inactdays:\n ret['comment'] = 'User {0} created but failed to set' \\\n ' inactive days to' \\\n ' {1}'.format(name, inactdays)\n ret['result'] = False\n ret['changes']['inactdays'] = inactdays\n if warndays:\n __salt__['shadow.set_warndays'](name, warndays)\n spost = __salt__['shadow.info'](name)\n if spost['warn'] != warndays:\n ret['comment'] = 'User {0} created but failed to set' \\\n ' warn days to' \\\n ' {1}'.format(name, warndays)\n ret['result'] = False\n ret['changes']['warndays'] = warndays\n if expire:\n __salt__['shadow.set_expire'](name, expire)\n spost = __salt__['shadow.info'](name)\n if spost['expire'] != expire:\n ret['comment'] = 'User {0} created but failed to set' \\\n ' expire days to' \\\n ' {1}'.format(name, expire)\n ret['result'] = False\n ret['changes']['expire'] = expire\n elif salt.utils.platform.is_windows():\n if password and not empty_password:\n if not __salt__['user.setpassword'](name, password):\n ret['comment'] = 'User {0} created but failed to set' \\\n ' password to' \\\n ' {1}'.format(name, 'XXX-REDACTED-XXX')\n ret['result'] = False\n ret['changes']['passwd'] = 'XXX-REDACTED-XXX'\n if expire:\n __salt__['shadow.set_expire'](name, expire)\n spost = __salt__['shadow.info'](name)\n if salt.utils.dateutils.strftime(spost['expire']) != salt.utils.dateutils.strftime(expire):\n ret['comment'] = 'User {0} created but failed to set' \\\n ' expire days to' \\\n ' {1}'.format(name, expire)\n ret['result'] = False\n ret['changes']['expiration_date'] = spost['expire']\n elif salt.utils.platform.is_darwin() and password and not empty_password:\n if not __salt__['shadow.set_password'](name, password):\n ret['comment'] = 'User {0} created but failed to set' \\\n ' password to' \\\n ' {1}'.format(name, 'XXX-REDACTED-XXX')\n ret['result'] = False\n ret['changes']['passwd'] = 'XXX-REDACTED-XXX'\n else:\n ret['comment'] = 'Failed to create new user {0}'.format(name)\n ret['result'] = False\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef absent(name, purge=False, force=False):\n '''\n Ensure that the named user is absent\n\n name\n The name of the user to remove\n\n purge\n Set purge to True to delete all of the user's files as well as the user,\n Default is ``False``.\n\n force\n If the user is logged in, the absent state will fail. Set the force\n option to True to remove the user even if they are logged in. Not\n supported in FreeBSD and Solaris, Default is ``False``.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n lusr = __salt__['user.info'](name)\n if lusr:\n # The user is present, make it not present\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'User {0} set for removal'.format(name)\n return ret\n beforegroups = set(salt.utils.user.get_group_list(name))\n ret['result'] = __salt__['user.delete'](name, purge, force)\n aftergroups = set([g for g in beforegroups if __salt__['group.info'](g)])\n if ret['result']:\n ret['changes'] = {}\n for g in beforegroups - aftergroups:\n ret['changes']['{0} group'.format(g)] = 'removed'\n ret['changes'][name] = 'removed'\n ret['comment'] = 'Removed user {0}'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to remove user {0}'.format(name)\n return ret\n\n ret['comment'] = 'User {0} is not present'.format(name)\n\n return ret", "response": "Ensure that the named user is absent."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing a salt. url string and return the path and a possible saltenv query.", "response": "def parse(url):\n '''\n Parse a salt:// URL; return the path and a possible saltenv query.\n '''\n if not url.startswith('salt://'):\n return url, None\n\n # urlparse will split on valid filename chars such as '?' and '&'\n resource = url.split('salt://', 1)[-1]\n\n if '?env=' in resource:\n # \"env\" is not supported; Use \"saltenv\".\n path, saltenv = resource.split('?env=', 1)[0], None\n elif '?saltenv=' in resource:\n path, saltenv = resource.split('?saltenv=', 1)\n else:\n path, saltenv = resource, None\n\n if salt.utils.platform.is_windows():\n path = salt.utils.path.sanitize_win_path(path)\n\n return path, saltenv"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a new URL from a path and saltenv.", "response": "def create(path, saltenv=None):\n '''\n join `path` and `saltenv` into a 'salt://' URL.\n '''\n if salt.utils.platform.is_windows():\n path = salt.utils.path.sanitize_win_path(path)\n path = salt.utils.data.decode(path)\n\n query = 'saltenv={0}'.format(saltenv) if saltenv else ''\n url = salt.utils.data.decode(urlunparse(('file', '', path, '', query, '')))\n return 'salt://{0}'.format(url[len('file:///'):])"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntesting whether url is escaped with |", "response": "def is_escaped(url):\n '''\n test whether `url` is escaped with `|`\n '''\n scheme = urlparse(url).scheme\n if not scheme:\n return url.startswith('|')\n elif scheme == 'salt':\n path, saltenv = parse(url)\n if salt.utils.platform.is_windows() and '|' in url:\n return path.startswith('_')\n else:\n return path.startswith('|')\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd escape character | to url", "response": "def escape(url):\n '''\n add escape character `|` to `url`\n '''\n if salt.utils.platform.is_windows():\n return url\n\n scheme = urlparse(url).scheme\n if not scheme:\n if url.startswith('|'):\n return url\n else:\n return '|{0}'.format(url)\n elif scheme == 'salt':\n path, saltenv = parse(url)\n if path.startswith('|'):\n return create(path, saltenv)\n else:\n return create('|{0}'.format(path), saltenv)\n else:\n return url"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nremove escape character | from url", "response": "def unescape(url):\n '''\n remove escape character `|` from `url`\n '''\n scheme = urlparse(url).scheme\n if not scheme:\n return url.lstrip('|')\n elif scheme == 'salt':\n path, saltenv = parse(url)\n if salt.utils.platform.is_windows() and '|' in url:\n return create(path.lstrip('_'), saltenv)\n else:\n return create(path.lstrip('|'), saltenv)\n else:\n return url"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add_env(url, saltenv):\n '''\n append `saltenv` to `url` as a query parameter to a 'salt://' url\n '''\n if not url.startswith('salt://'):\n return url\n\n path, senv = parse(url)\n return create(path, saltenv)", "response": "add_env - add a saltenv to a url"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef split_env(url):\n '''\n remove the saltenv query parameter from a 'salt://' url\n '''\n if not url.startswith('salt://'):\n return url, None\n\n path, senv = parse(url)\n return create(path), senv", "response": "split the saltenv query parameter into a path and the saltenv query parameter"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadds http basic auth to a url", "response": "def add_http_basic_auth(url,\n user=None,\n password=None,\n https_only=False):\n '''\n Return a string with http basic auth incorporated into it\n '''\n if user is None and password is None:\n return url\n else:\n urltuple = urlparse(url)\n if https_only and urltuple.scheme != 'https':\n raise ValueError('Basic Auth only supported for HTTPS')\n if password is None:\n netloc = '{0}@{1}'.format(\n user,\n urltuple.netloc\n )\n urltuple = urltuple._replace(netloc=netloc)\n return urlunparse(urltuple)\n else:\n netloc = '{0}:{1}@{2}'.format(\n user,\n password,\n urltuple.netloc\n )\n urltuple = urltuple._replace(netloc=netloc)\n return urlunparse(urltuple)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef redact_http_basic_auth(output):\n '''\n Remove HTTP user and password\n '''\n # We can't use re.compile because re.compile(someregex).sub() doesn't\n # support flags even in Python 2.7.\n url_re = '(https?)://.*@'\n redacted = r'\\1://@'\n if sys.version_info >= (2, 7):\n # re.sub() supports flags as of 2.7, use this to do a case-insensitive\n # match.\n return re.sub(url_re, redacted, output, flags=re.IGNORECASE)\n else:\n # We're on python 2.6, test if a lowercased version of the output\n # string matches the regex...\n if re.search(url_re, output.lower()):\n # ... and if it does, perform the regex substitution.\n return re.sub(url_re, redacted, output.lower())\n # No match, just return the original string\n return output", "response": "Redacts the HTTP basic auth string."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_hold(line, pattern=__HOLD_PATTERN, full=True):\n '''\n Resolve a package name from a line containing the hold expression. If the\n regex is not matched, None is returned.\n\n yum ==> 2:vim-enhanced-7.4.629-5.el6.*\n dnf ==> vim-enhanced-2:7.4.827-1.fc22.*\n '''\n if full:\n if _yum() == 'dnf':\n lock_re = r'({0}-\\S+)'.format(pattern)\n else:\n lock_re = r'(\\d+:{0}-\\S+)'.format(pattern)\n else:\n if _yum() == 'dnf':\n lock_re = r'({0}-\\S+)'.format(pattern)\n else:\n lock_re = r'\\d+:({0}-\\S+)'.format(pattern)\n\n match = re.search(lock_re, line)\n if match:\n if not full:\n woarch = match.group(1).rsplit('.', 1)[0]\n worel = woarch.rsplit('-', 1)[0]\n return worel.rsplit('-', 1)[0]\n else:\n return match.group(1)\n return None", "response": "Resolve a package name from a line containing the hold expression."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _yum():\n '''\n Determine package manager name (yum or dnf),\n depending on the system version.\n '''\n contextkey = 'yum_bin'\n if contextkey not in __context__:\n if ('fedora' in __grains__['os'].lower()\n and int(__grains__['osrelease']) >= 22):\n __context__[contextkey] = 'dnf'\n elif 'photon' in __grains__['os'].lower():\n __context__[contextkey] = 'tdnf'\n else:\n __context__[contextkey] = 'yum'\n return __context__[contextkey]", "response": "Determine package manager name based on system version."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncalls yum on the available modules", "response": "def _call_yum(args, **kwargs):\n '''\n Call yum/dnf.\n '''\n params = {'output_loglevel': 'trace',\n 'python_shell': False,\n 'env': salt.utils.environment.get_module_environment(globals())}\n params.update(kwargs)\n cmd = []\n if salt.utils.systemd.has_scope(__context__) and __salt__['config.get']('systemd.scope', True):\n cmd.extend(['systemd-run', '--scope'])\n cmd.append(_yum())\n cmd.extend(args)\n\n return __salt__['cmd.run_all'](cmd, **params)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _yum_pkginfo(output):\n '''\n Parse yum/dnf output (which could contain irregular line breaks if package\n names are long) retrieving the name, version, etc., and return a list of\n pkginfo namedtuples.\n '''\n cur = {}\n keys = itertools.cycle(('name', 'version', 'repoid'))\n values = salt.utils.itertools.split(_strip_headers(output))\n osarch = __grains__['osarch']\n for (key, value) in zip(keys, values):\n if key == 'name':\n try:\n cur['name'], cur['arch'] = value.rsplit('.', 1)\n except ValueError:\n cur['name'] = value\n cur['arch'] = osarch\n cur['name'] = salt.utils.pkg.rpm.resolve_name(cur['name'],\n cur['arch'],\n osarch)\n else:\n if key == 'version':\n # Suppport packages with no 'Release' parameter\n value = value.rstrip('-')\n elif key == 'repoid':\n # Installed packages show a '@' at the beginning\n value = value.lstrip('@')\n cur[key] = value\n if key == 'repoid':\n # We're done with this package, create the pkginfo namedtuple\n pkginfo = salt.utils.pkg.rpm.pkginfo(**cur)\n # Clear the dict for the next package\n cur = {}\n # Yield the namedtuple\n if pkginfo is not None:\n yield pkginfo", "response": "Parse the output of yum - pkginfo and yield a list of namedtuples."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _check_versionlock():\n '''\n Ensure that the appropriate versionlock plugin is present\n '''\n if _yum() == 'dnf':\n if int(__grains__.get('osmajorrelease')) >= 26:\n if six.PY3:\n vl_plugin = 'python3-dnf-plugin-versionlock'\n else:\n vl_plugin = 'python2-dnf-plugin-versionlock'\n else:\n if six.PY3:\n vl_plugin = 'python3-dnf-plugins-extras-versionlock'\n else:\n vl_plugin = 'python-dnf-plugins-extras-versionlock'\n else:\n vl_plugin = 'yum-versionlock' \\\n if __grains__.get('osmajorrelease') == '5' \\\n else 'yum-plugin-versionlock'\n\n if vl_plugin not in list_pkgs():\n raise SaltInvocationError(\n 'Cannot proceed, {0} is not installed.'.format(vl_plugin)\n )", "response": "Ensure that the appropriate versionlock plugin is present."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_options(**kwargs):\n '''\n Returns a list of options to be used in the yum/dnf command, based on the\n kwargs passed.\n '''\n # Get repo options from the kwargs\n fromrepo = kwargs.pop('fromrepo', '')\n repo = kwargs.pop('repo', '')\n disablerepo = kwargs.pop('disablerepo', '')\n enablerepo = kwargs.pop('enablerepo', '')\n disableexcludes = kwargs.pop('disableexcludes', '')\n branch = kwargs.pop('branch', '')\n setopt = kwargs.pop('setopt', None)\n if setopt is None:\n setopt = []\n else:\n setopt = salt.utils.args.split_input(setopt)\n get_extra_options = kwargs.pop('get_extra_options', False)\n\n # Support old 'repo' argument\n if repo and not fromrepo:\n fromrepo = repo\n\n ret = []\n\n if fromrepo:\n log.info('Restricting to repo \\'%s\\'', fromrepo)\n ret.extend(['--disablerepo=*', '--enablerepo={0}'.format(fromrepo)])\n else:\n if disablerepo:\n targets = [disablerepo] \\\n if not isinstance(disablerepo, list) \\\n else disablerepo\n log.info('Disabling repo(s): %s', ', '.join(targets))\n ret.extend(\n ['--disablerepo={0}'.format(x) for x in targets]\n )\n if enablerepo:\n targets = [enablerepo] \\\n if not isinstance(enablerepo, list) \\\n else enablerepo\n log.info('Enabling repo(s): %s', ', '.join(targets))\n ret.extend(['--enablerepo={0}'.format(x) for x in targets])\n\n if disableexcludes:\n log.info('Disabling excludes for \\'%s\\'', disableexcludes)\n ret.append('--disableexcludes={0}'.format(disableexcludes))\n\n if branch:\n log.info('Adding branch \\'%s\\'', branch)\n ret.append('--branch={0}'.format(branch))\n\n for item in setopt:\n ret.extend(['--setopt', six.text_type(item)])\n\n if get_extra_options:\n # sorting here to make order uniform, makes unit testing more reliable\n for key in sorted(kwargs):\n if key.startswith('__'):\n continue\n value = kwargs[key]\n if isinstance(value, six.string_types):\n log.info('Found extra option --%s=%s', key, value)\n ret.append('--{0}={1}'.format(key, value))\n elif value is True:\n log.info('Found extra option --%s', key)\n ret.append('--{0}'.format(key))\n if ret:\n log.info('Adding extra options: %s', ret)\n\n return ret", "response": "Returns a list of options to be used in the yum - dnf command based on the passed arguments."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget the yum config options and values from the yum. conf file.", "response": "def _get_yum_config():\n '''\n Returns a dict representing the yum config options and values.\n\n We try to pull all of the yum config options into a standard dict object.\n This is currently only used to get the reposdir settings, but could be used\n for other things if needed.\n\n If the yum python library is available, use that, which will give us all of\n the options, including all of the defaults not specified in the yum config.\n Additionally, they will all be of the correct object type.\n\n If the yum library is not available, we try to read the yum.conf\n directly ourselves with a minimal set of \"defaults\".\n '''\n # in case of any non-fatal failures, these defaults will be used\n conf = {\n 'reposdir': ['/etc/yum/repos.d', '/etc/yum.repos.d'],\n }\n\n if HAS_YUM:\n try:\n yb = yum.YumBase()\n yb.preconf.init_plugins = False\n for name, value in six.iteritems(yb.conf):\n conf[name] = value\n except (AttributeError, yum.Errors.ConfigError) as exc:\n raise CommandExecutionError(\n 'Could not query yum config: {0}'.format(exc)\n )\n except yum.Errors.YumBaseError as yum_base_error:\n raise CommandExecutionError(\n 'Error accessing yum or rpmdb: {0}'.format(yum_base_error)\n )\n else:\n # fall back to parsing the config ourselves\n # Look for the config the same order yum does\n fn = None\n paths = ('/etc/yum/yum.conf', '/etc/yum.conf', '/etc/dnf/dnf.conf', '/etc/tdnf/tdnf.conf')\n for path in paths:\n if os.path.exists(path):\n fn = path\n break\n\n if not fn:\n raise CommandExecutionError(\n 'No suitable yum config file found in: {0}'.format(paths)\n )\n\n cp = configparser.ConfigParser()\n try:\n cp.read(fn)\n except (IOError, OSError) as exc:\n raise CommandExecutionError(\n 'Unable to read from {0}: {1}'.format(fn, exc)\n )\n\n if cp.has_section('main'):\n for opt in cp.options('main'):\n if opt in ('reposdir', 'commands', 'excludes'):\n # these options are expected to be lists\n conf[opt] = [x.strip()\n for x in cp.get('main', opt).split(',')]\n else:\n conf[opt] = cp.get('main', opt)\n else:\n log.warning(\n 'Could not find [main] section in %s, using internal '\n 'defaults',\n fn\n )\n\n return conf"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nlooks for a specific config variable and return its value", "response": "def _get_yum_config_value(name):\n '''\n Look for a specific config variable and return its value\n '''\n conf = _get_yum_config()\n if name in conf.keys():\n return conf.get(name)\n return None"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _normalize_basedir(basedir=None):\n '''\n Takes a basedir argument as a string or a list. If the string or list is\n empty, then look up the default from the 'reposdir' option in the yum\n config.\n\n Returns a list of directories.\n '''\n # if we are passed a string (for backward compatibility), convert to a list\n if isinstance(basedir, six.string_types):\n basedir = [x.strip() for x in basedir.split(',')]\n\n if basedir is None:\n basedir = []\n\n # nothing specified, so use the reposdir option as the default\n if not basedir:\n basedir = _get_yum_config_value('reposdir')\n\n if not isinstance(basedir, list) or not basedir:\n raise SaltInvocationError('Could not determine any repo directories')\n\n return basedir", "response": "Normalizes the basedir argument to a list of directories."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nnormalizing the package name to include architectures and architectures.", "response": "def normalize_name(name):\n '''\n Strips the architecture from the specified package name, if necessary.\n Circumstances where this would be done include:\n\n * If the arch is 32 bit and the package name ends in a 32-bit arch.\n * If the arch matches the OS arch, or is ``noarch``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.normalize_name zsh.x86_64\n '''\n try:\n arch = name.rsplit('.', 1)[-1]\n if arch not in salt.utils.pkg.rpm.ARCHES + ('noarch',):\n return name\n except ValueError:\n return name\n if arch in (__grains__['osarch'], 'noarch') \\\n or salt.utils.pkg.rpm.check_32(arch, osarch=__grains__['osarch']):\n return name[:-(len(arch) + 1)]\n return name"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncompares two packages and return the version of the first one.", "response": "def version_cmp(pkg1, pkg2, ignore_epoch=False, **kwargs):\n '''\n .. versionadded:: 2015.5.4\n\n Do a cmp-style comparison on two packages. Return -1 if pkg1 < pkg2, 0 if\n pkg1 == pkg2, and 1 if pkg1 > pkg2. Return None if there was a problem\n making the comparison.\n\n ignore_epoch : False\n Set to ``True`` to ignore the epoch when comparing versions\n\n .. versionadded:: 2015.8.10,2016.3.2\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.version_cmp '0.2-001' '0.2.0.1-002'\n '''\n\n return __salt__['lowpkg.version_cmp'](pkg1, pkg2, ignore_epoch=ignore_epoch)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_pkgs(versions_as_list=False, **kwargs):\n '''\n List the packages currently installed as a dict. By default, the dict\n contains versions as a comma separated string::\n\n {'': '[,...]'}\n\n versions_as_list:\n If set to true, the versions are provided as a list\n\n {'': ['', '']}\n\n attr:\n If a list of package attributes is specified, returned value will\n contain them in addition to version, eg.::\n\n {'': [{'version' : 'version', 'arch' : 'arch'}]}\n\n Valid attributes are: ``epoch``, ``version``, ``release``, ``arch``,\n ``install_date``, ``install_date_time_t``.\n\n If ``all`` is specified, all valid attributes will be returned.\n\n .. versionadded:: 2018.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_pkgs\n salt '*' pkg.list_pkgs attr=version,arch\n salt '*' pkg.list_pkgs attr='[\"version\", \"arch\"]'\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 attr = kwargs.get('attr')\n if attr is not None:\n attr = salt.utils.args.split_input(attr)\n\n contextkey = 'pkg.list_pkgs'\n\n if contextkey not in __context__:\n ret = {}\n cmd = ['rpm', '-qa', '--queryformat',\n salt.utils.pkg.rpm.QUERYFORMAT.replace('%{REPOID}', '(none)') + '\\n']\n output = __salt__['cmd.run'](cmd,\n python_shell=False,\n output_loglevel='trace')\n for line in output.splitlines():\n pkginfo = salt.utils.pkg.rpm.parse_pkginfo(\n line,\n osarch=__grains__['osarch']\n )\n if pkginfo is not None:\n # see rpm version string rules available at https://goo.gl/UGKPNd\n pkgver = pkginfo.version\n epoch = ''\n release = ''\n if ':' in pkgver:\n epoch, pkgver = pkgver.split(\":\", 1)\n if '-' in pkgver:\n pkgver, release = pkgver.split(\"-\", 1)\n all_attr = {\n 'epoch': epoch,\n 'version': pkgver,\n 'release': release,\n 'arch': pkginfo.arch,\n 'install_date': pkginfo.install_date,\n 'install_date_time_t': pkginfo.install_date_time_t\n }\n __salt__['pkg_resource.add_pkg'](ret, pkginfo.name, all_attr)\n\n for pkgname in ret:\n ret[pkgname] = sorted(ret[pkgname], key=lambda d: d['version'])\n\n __context__[contextkey] = ret\n\n return __salt__['pkg_resource.format_pkg_list'](\n __context__[contextkey],\n versions_as_list,\n attr)", "response": "Return a dict containing the currently installed packages."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_repo_pkgs(*args, **kwargs):\n '''\n .. versionadded:: 2014.1.0\n .. versionchanged:: 2014.7.0\n All available versions of each package are now returned. This required\n a slight modification to the structure of the return dict. The return\n data shown below reflects the updated return dict structure. Note that\n packages which are version-locked using :py:mod:`pkg.hold\n ` will only show the currently-installed\n version, as locking a package will make other versions appear\n unavailable to yum/dnf.\n .. versionchanged:: 2017.7.0\n By default, the versions for each package are no longer organized by\n repository. To get results organized by repository, use\n ``byrepo=True``.\n\n Returns all available packages. Optionally, package names (and name globs)\n can be passed and the results will be filtered to packages matching those\n names. This is recommended as it speeds up the function considerably.\n\n .. warning::\n Running this function on RHEL/CentOS 6 and earlier will be more\n resource-intensive, as the version of yum that ships with older\n RHEL/CentOS has no yum subcommand for listing packages from a\n repository. Thus, a ``yum list installed`` and ``yum list available``\n are run, which generates a lot of output, which must then be analyzed\n to determine which package information to include in the return data.\n\n This function can be helpful in discovering the version or repo to specify\n in a :mod:`pkg.installed ` state.\n\n The return data will be a dictionary mapping package names to a list of\n version numbers, ordered from newest to oldest. If ``byrepo`` is set to\n ``True``, then the return dictionary will contain repository names at the\n top level, and each repository will map packages to lists of version\n numbers. For example:\n\n .. code-block:: python\n\n # With byrepo=False (default)\n {\n 'bash': ['4.1.2-15.el6_5.2',\n '4.1.2-15.el6_5.1',\n '4.1.2-15.el6_4'],\n 'kernel': ['2.6.32-431.29.2.el6',\n '2.6.32-431.23.3.el6',\n '2.6.32-431.20.5.el6',\n '2.6.32-431.20.3.el6',\n '2.6.32-431.17.1.el6',\n '2.6.32-431.11.2.el6',\n '2.6.32-431.5.1.el6',\n '2.6.32-431.3.1.el6',\n '2.6.32-431.1.2.0.1.el6',\n '2.6.32-431.el6']\n }\n # With byrepo=True\n {\n 'base': {\n 'bash': ['4.1.2-15.el6_4'],\n 'kernel': ['2.6.32-431.el6']\n },\n 'updates': {\n 'bash': ['4.1.2-15.el6_5.2', '4.1.2-15.el6_5.1'],\n 'kernel': ['2.6.32-431.29.2.el6',\n '2.6.32-431.23.3.el6',\n '2.6.32-431.20.5.el6',\n '2.6.32-431.20.3.el6',\n '2.6.32-431.17.1.el6',\n '2.6.32-431.11.2.el6',\n '2.6.32-431.5.1.el6',\n '2.6.32-431.3.1.el6',\n '2.6.32-431.1.2.0.1.el6']\n }\n }\n\n fromrepo : None\n Only include results from the specified repo(s). Multiple repos can be\n specified, comma-separated.\n\n enablerepo (ignored if ``fromrepo`` is specified)\n Specify a disabled package repository (or repositories) to enable.\n (e.g., ``yum --enablerepo='somerepo'``)\n\n .. versionadded:: 2017.7.0\n\n disablerepo (ignored if ``fromrepo`` is specified)\n Specify an enabled package repository (or repositories) to disable.\n (e.g., ``yum --disablerepo='somerepo'``)\n\n .. versionadded:: 2017.7.0\n\n byrepo : False\n When ``True``, the return data for each package will be organized by\n repository.\n\n .. versionadded:: 2017.7.0\n\n cacheonly : False\n When ``True``, the repo information will be retrieved from the cached\n repo metadata. This is equivalent to passing the ``-C`` option to\n yum/dnf.\n\n .. versionadded:: 2017.7.0\n\n setopt\n A comma-separated or Python list of key=value options. This list will\n be expanded and ``--setopt`` prepended to each in the yum/dnf command\n that is run.\n\n .. versionadded:: 2019.2.0\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.list_repo_pkgs\n salt '*' pkg.list_repo_pkgs foo bar baz\n salt '*' pkg.list_repo_pkgs 'samba4*' fromrepo=base,updates\n salt '*' pkg.list_repo_pkgs 'python2-*' byrepo=True\n '''\n byrepo = kwargs.pop('byrepo', False)\n cacheonly = kwargs.pop('cacheonly', False)\n fromrepo = kwargs.pop('fromrepo', '') or ''\n disablerepo = kwargs.pop('disablerepo', '') or ''\n enablerepo = kwargs.pop('enablerepo', '') or ''\n\n repo_arg = _get_options(fromrepo=fromrepo, **kwargs)\n\n if fromrepo and not isinstance(fromrepo, list):\n try:\n fromrepo = [x.strip() for x in fromrepo.split(',')]\n except AttributeError:\n fromrepo = [x.strip() for x in six.text_type(fromrepo).split(',')]\n\n if disablerepo and not isinstance(disablerepo, list):\n try:\n disablerepo = [x.strip() for x in disablerepo.split(',')\n if x != '*']\n except AttributeError:\n disablerepo = [x.strip() for x in six.text_type(disablerepo).split(',')\n if x != '*']\n\n if enablerepo and not isinstance(enablerepo, list):\n try:\n enablerepo = [x.strip() for x in enablerepo.split(',')\n if x != '*']\n except AttributeError:\n enablerepo = [x.strip() for x in six.text_type(enablerepo).split(',')\n if x != '*']\n\n if fromrepo:\n repos = fromrepo\n else:\n repos = [\n repo_name for repo_name, repo_info in six.iteritems(list_repos())\n if repo_name in enablerepo\n or (repo_name not in disablerepo\n and six.text_type(repo_info.get('enabled', '1')) == '1')\n ]\n\n ret = {}\n\n def _check_args(args, name):\n '''\n Do glob matching on args and return True if a match was found.\n Otherwise, return False\n '''\n for arg in args:\n if fnmatch.fnmatch(name, arg):\n return True\n return False\n\n def _parse_output(output, strict=False):\n for pkg in _yum_pkginfo(output):\n if strict and (pkg.repoid not in repos\n or not _check_args(args, pkg.name)):\n continue\n repo_dict = ret.setdefault(pkg.repoid, {})\n version_list = repo_dict.setdefault(pkg.name, set())\n version_list.add(pkg.version)\n\n yum_version = None if _yum() != 'yum' else _LooseVersion(\n __salt__['cmd.run'](\n ['yum', '--version'],\n python_shell=False\n ).splitlines()[0].strip()\n )\n # Really old version of yum; does not even have --showduplicates option\n if yum_version and yum_version < _LooseVersion('3.2.13'):\n cmd_prefix = ['--quiet']\n if cacheonly:\n cmd_prefix.append('-C')\n cmd_prefix.append('list')\n for pkg_src in ('installed', 'available'):\n # Check installed packages first\n out = _call_yum(cmd_prefix + [pkg_src], ignore_retcode=True)\n if out['retcode'] == 0:\n _parse_output(out['stdout'], strict=True)\n # The --showduplicates option is added in 3.2.13, but the\n # repository-packages subcommand is only in 3.4.3 and newer\n elif yum_version and yum_version < _LooseVersion('3.4.3'):\n cmd_prefix = ['--quiet', '--showduplicates']\n if cacheonly:\n cmd_prefix.append('-C')\n cmd_prefix.append('list')\n for pkg_src in ('installed', 'available'):\n # Check installed packages first\n out = _call_yum(cmd_prefix + [pkg_src], ignore_retcode=True)\n if out['retcode'] == 0:\n _parse_output(out['stdout'], strict=True)\n else:\n for repo in repos:\n cmd = ['--quiet', '--showduplicates', 'repository-packages', repo, 'list']\n if cacheonly:\n cmd.append('-C')\n # Can't concatenate because args is a tuple, using list.extend()\n cmd.extend(args)\n out = _call_yum(cmd, ignore_retcode=True)\n if out['retcode'] != 0 and 'Error:' in out['stdout']:\n continue\n _parse_output(out['stdout'])\n\n if byrepo:\n for reponame in ret:\n # Sort versions newest to oldest\n for pkgname in ret[reponame]:\n sorted_versions = sorted(\n [_LooseVersion(x) for x in ret[reponame][pkgname]],\n reverse=True\n )\n ret[reponame][pkgname] = [x.vstring for x in sorted_versions]\n return ret\n else:\n byrepo_ret = {}\n for reponame in ret:\n for pkgname in ret[reponame]:\n byrepo_ret.setdefault(pkgname, []).extend(ret[reponame][pkgname])\n for pkgname in byrepo_ret:\n sorted_versions = sorted(\n [_LooseVersion(x) for x in byrepo_ret[pkgname]],\n reverse=True\n )\n byrepo_ret[pkgname] = [x.vstring for x in sorted_versions]\n return byrepo_ret", "response": "List the packages in the repository."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list_upgrades(refresh=True, **kwargs):\n '''\n Check whether or not an upgrade is available for all packages\n\n The ``fromrepo``, ``enablerepo``, and ``disablerepo`` arguments are\n supported, as used in pkg states, and the ``disableexcludes`` option is\n also supported.\n\n .. versionadded:: 2014.7.0\n Support for the ``disableexcludes`` option\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_upgrades\n '''\n options = _get_options(**kwargs)\n\n if salt.utils.data.is_true(refresh):\n refresh_db(check_update=False, **kwargs)\n\n cmd = ['--quiet']\n cmd.extend(options)\n cmd.extend(['list', 'upgrades' if _yum() == 'dnf' else 'updates'])\n out = _call_yum(cmd, ignore_retcode=True)\n if out['retcode'] != 0 and 'Error:' in out:\n return {}\n\n return dict([(x.name, x.version) for x in _yum_pkginfo(out['stdout'])])", "response": "List the available upgrades for all packages in the current repository."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list_downloaded(**kwargs):\n '''\n .. versionadded:: 2017.7.0\n\n List prefetched packages downloaded by Yum in the local disk.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_downloaded\n '''\n CACHE_DIR = os.path.join('/var/cache/', _yum())\n\n ret = {}\n for root, dirnames, filenames in salt.utils.path.os_walk(CACHE_DIR):\n for filename in fnmatch.filter(filenames, '*.rpm'):\n package_path = os.path.join(root, filename)\n pkg_info = __salt__['lowpkg.bin_pkg_info'](package_path)\n pkg_timestamp = int(os.path.getctime(package_path))\n ret.setdefault(pkg_info['name'], {})[pkg_info['version']] = {\n 'path': package_path,\n 'size': os.path.getsize(package_path),\n 'creation_date_time_t': pkg_timestamp,\n 'creation_date_time': datetime.datetime.fromtimestamp(pkg_timestamp).isoformat(),\n }\n return ret", "response": "List prefetched packages downloaded by Yum in the local disk."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef info_installed(*names, **kwargs):\n '''\n .. versionadded:: 2015.8.1\n\n Return the information of the named package(s), installed on the system.\n\n :param all_versions:\n Include information for all versions of the packages installed on the minion.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pkg.info_installed \n salt '*' pkg.info_installed ...\n salt '*' pkg.info_installed all_versions=True\n '''\n all_versions = kwargs.get('all_versions', False)\n ret = dict()\n for pkg_name, pkgs_nfo in __salt__['lowpkg.info'](*names, **kwargs).items():\n pkg_nfo = pkgs_nfo if all_versions else [pkgs_nfo]\n for _nfo in pkg_nfo:\n t_nfo = dict()\n # Translate dpkg-specific keys to a common structure\n for key, value in _nfo.items():\n if key == 'source_rpm':\n t_nfo['source'] = value\n else:\n t_nfo[key] = value\n if not all_versions:\n ret[pkg_name] = t_nfo\n else:\n ret.setdefault(pkg_name, []).append(t_nfo)\n return ret", "response": "Return the information of the named package on the system."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrefreshing the yum repos for updated packages", "response": "def refresh_db(**kwargs):\n '''\n Check the yum repos for updated packages\n\n Returns:\n\n - ``True``: Updates are available\n - ``False``: An error occurred\n - ``None``: No updates are available\n\n repo\n Refresh just the specified repo\n\n disablerepo\n Do not refresh the specified repo\n\n enablerepo\n Refresh a disabled repo using this option\n\n branch\n Add the specified branch when refreshing\n\n disableexcludes\n Disable the excludes defined in your config files. Takes one of three\n options:\n - ``all`` - disable all excludes\n - ``main`` - disable excludes defined in [main] in yum.conf\n - ``repoid`` - disable excludes defined for that repo\n\n setopt\n A comma-separated or Python list of key=value options. This list will\n be expanded and ``--setopt`` prepended to each in the yum/dnf command\n that is run.\n\n .. versionadded:: 2019.2.0\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 retcodes = {\n 100: True,\n 0: None,\n 1: False,\n }\n\n ret = True\n check_update_ = kwargs.pop('check_update', True)\n options = _get_options(**kwargs)\n\n clean_cmd = ['--quiet', '--assumeyes', 'clean', 'expire-cache']\n clean_cmd.extend(options)\n _call_yum(clean_cmd, ignore_retcode=True)\n\n if check_update_:\n update_cmd = ['--quiet', '--assumeyes', 'check-update']\n if (__grains__.get('os_family') == 'RedHat'\n and __grains__.get('osmajorrelease') == 7):\n # This feature is disabled because it is not used by Salt and adds a\n # lot of extra time to the command with large repos like EPEL\n update_cmd.append('--setopt=autocheck_running_kernel=false')\n update_cmd.extend(options)\n ret = retcodes.get(_call_yum(update_cmd, ignore_retcode=True)['retcode'], False)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef install(name=None,\n refresh=False,\n skip_verify=False,\n pkgs=None,\n sources=None,\n downloadonly=False,\n reinstall=False,\n normalize=True,\n update_holds=False,\n saltenv='base',\n ignore_epoch=False,\n **kwargs):\n '''\n .. versionchanged:: 2015.8.12,2016.3.3,2016.11.0\n On minions running systemd>=205, `systemd-run(1)`_ is now used to\n isolate commands which modify installed packages from the\n ``salt-minion`` daemon's control group. This is done to keep systemd\n from killing any yum/dnf commands spawned by Salt when the\n ``salt-minion`` service is restarted. (see ``KillMode`` in the\n `systemd.kill(5)`_ manpage for more information). If desired, usage of\n `systemd-run(1)`_ can be suppressed by setting a :mod:`config option\n ` called ``systemd.scope``, with a value of\n ``False`` (no quotes).\n\n .. _`systemd-run(1)`: https://www.freedesktop.org/software/systemd/man/systemd-run.html\n .. _`systemd.kill(5)`: https://www.freedesktop.org/software/systemd/man/systemd.kill.html\n\n Install the passed package(s), add refresh=True to clean the yum database\n before package is installed.\n\n name\n The name of the package to be installed. Note that this parameter is\n ignored if either \"pkgs\" or \"sources\" is passed. Additionally, please\n note that this option can only be used to install packages from a\n software repository. To install a package file manually, use the\n \"sources\" option.\n\n 32-bit packages can be installed on 64-bit systems by appending the\n architecture designation (``.i686``, ``.i586``, etc.) to the end of the\n package name.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install \n\n refresh\n Whether or not to update the yum database before executing.\n\n reinstall\n Specifying reinstall=True will use ``yum reinstall`` rather than\n ``yum install`` for requested packages that are already installed.\n\n If a version is specified with the requested package, then\n ``yum reinstall`` will only be used if the installed version\n matches the requested version.\n\n Works with ``sources`` when the package header of the source can be\n matched to the name and version of an installed package.\n\n .. versionadded:: 2014.7.0\n\n skip_verify\n Skip the GPG verification check (e.g., ``--nogpgcheck``)\n\n downloadonly\n Only download the packages, do not install.\n\n version\n Install a specific version of the package, e.g. 1.2.3-4.el5. Ignored\n if \"pkgs\" or \"sources\" is passed.\n\n .. versionchanged:: 2018.3.0\n version can now contain comparison operators (e.g. ``>1.2.3``,\n ``<=2.0``, etc.)\n\n update_holds : False\n If ``True``, and this function would update the package version, any\n packages held using the yum/dnf \"versionlock\" plugin will be unheld so\n that they can be updated. Otherwise, if this function attempts to\n update a held package, the held package(s) will be skipped and an\n error will be raised.\n\n .. versionadded:: 2016.11.0\n\n setopt\n A comma-separated or Python list of key=value options. This list will\n be expanded and ``--setopt`` prepended to each in the yum/dnf command\n that is run.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install foo setopt='obsoletes=0,plugins=0'\n\n .. versionadded:: 2019.2.0\n\n Repository Options:\n\n fromrepo\n Specify a package repository (or repositories) from which to install.\n (e.g., ``yum --disablerepo='*' --enablerepo='somerepo'``)\n\n enablerepo (ignored if ``fromrepo`` is specified)\n Specify a disabled package repository (or repositories) to enable.\n (e.g., ``yum --enablerepo='somerepo'``)\n\n disablerepo (ignored if ``fromrepo`` is specified)\n Specify an enabled package repository (or repositories) to disable.\n (e.g., ``yum --disablerepo='somerepo'``)\n\n disableexcludes\n Disable exclude from main, for a repo or for everything.\n (e.g., ``yum --disableexcludes='main'``)\n\n .. versionadded:: 2014.7.0\n\n ignore_epoch : False\n Only used when the version of a package is specified using a comparison\n operator (e.g. ``>4.1``). If set to ``True``, then the epoch will be\n ignored when comparing the currently-installed version to the desired\n version.\n\n .. versionadded:: 2018.3.0\n\n\n Multiple Package Installation Options:\n\n pkgs\n A list of packages to install from a software repository. Must be\n passed as a python list. A specific version number can be specified\n by using a single-element dict representing the package and its\n version.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.install pkgs='[\"foo\", \"bar\"]'\n salt '*' pkg.install pkgs='[\"foo\", {\"bar\": \"1.2.3-4.el5\"}]'\n\n sources\n A list of RPM packages to install. Must be passed as a list of dicts,\n with the keys being package names, and the values being the source URI\n or local path to the package.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install sources='[{\"foo\": \"salt://foo.rpm\"}, {\"bar\": \"salt://bar.rpm\"}]'\n\n normalize : True\n Normalize the package name by removing the architecture. This is useful\n for poorly created packages which might include the architecture as an\n actual part of the name such as kernel modules which match a specific\n kernel version.\n\n .. code-block:: bash\n\n salt -G role:nsd pkg.install gpfs.gplbin-2.6.32-279.31.1.el6.x86_64 normalize=False\n\n .. versionadded:: 2014.7.0\n\n diff_attr:\n If a list of package attributes is specified, returned value will\n contain them, eg.::\n\n {'': {\n 'old': {\n 'version': '',\n 'arch': ''},\n\n 'new': {\n 'version': '',\n 'arch': ''}}}\n\n Valid attributes are: ``epoch``, ``version``, ``release``, ``arch``,\n ``install_date``, ``install_date_time_t``.\n\n If ``all`` is specified, all valid attributes will be returned.\n\n .. versionadded:: 2018.3.0\n\n Returns a dict containing the new package names and versions::\n\n {'': {'old': '',\n 'new': ''}}\n\n If an attribute list in diff_attr is specified, the dict will also contain\n any specified attribute, eg.::\n\n {'': {\n 'old': {\n 'version': '',\n 'arch': ''},\n\n 'new': {\n 'version': '',\n 'arch': ''}}}\n '''\n options = _get_options(**kwargs)\n\n if salt.utils.data.is_true(refresh):\n refresh_db(**kwargs)\n reinstall = salt.utils.data.is_true(reinstall)\n\n try:\n pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](\n name, pkgs, sources, saltenv=saltenv, normalize=normalize, **kwargs\n )\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n if not pkg_params:\n return {}\n\n version_num = kwargs.get('version')\n\n diff_attr = kwargs.get('diff_attr')\n old = list_pkgs(versions_as_list=False, attr=diff_attr) if not downloadonly else list_downloaded()\n # Use of __context__ means no duplicate work here, just accessing\n # information already in __context__ from the previous call to list_pkgs()\n old_as_list = list_pkgs(versions_as_list=True) if not downloadonly else list_downloaded()\n\n to_install = []\n to_downgrade = []\n to_reinstall = []\n _available = {}\n # The above three lists will be populated with tuples containing the\n # package name and the string being used for this particular package\n # modification. The reason for this method is that the string we use for\n # installation, downgrading, or reinstallation will be different than the\n # package name in a couple cases:\n #\n # 1) A specific version is being targeted. In this case the string being\n # passed to install/downgrade/reinstall will contain the version\n # information after the package name.\n # 2) A binary package is being installed via the \"sources\" param. In this\n # case the string being passed will be the path to the local copy of\n # the package in the minion cachedir.\n #\n # The reason that we need both items is to be able to modify the installed\n # version of held packages.\n if pkg_type == 'repository':\n has_wildcards = []\n has_comparison = []\n for pkgname, pkgver in six.iteritems(pkg_params):\n try:\n if '*' in pkgver:\n has_wildcards.append(pkgname)\n elif pkgver.startswith('<') or pkgver.startswith('>'):\n has_comparison.append(pkgname)\n except (TypeError, ValueError):\n continue\n _available = AvailablePackages(\n *has_wildcards + has_comparison,\n byrepo=False,\n **kwargs)\n pkg_params_items = six.iteritems(pkg_params)\n elif pkg_type == 'advisory':\n pkg_params_items = []\n cur_patches = list_patches()\n for advisory_id in pkg_params:\n if advisory_id not in cur_patches:\n raise CommandExecutionError(\n 'Advisory id \"{0}\" not found'.format(advisory_id)\n )\n else:\n pkg_params_items.append(advisory_id)\n else:\n pkg_params_items = []\n for pkg_source in pkg_params:\n if 'lowpkg.bin_pkg_info' in __salt__:\n rpm_info = __salt__['lowpkg.bin_pkg_info'](pkg_source)\n else:\n rpm_info = None\n if rpm_info is None:\n log.error(\n 'pkg.install: Unable to get rpm information for %s. '\n 'Version comparisons will be unavailable, and return '\n 'data may be inaccurate if reinstall=True.', pkg_source\n )\n pkg_params_items.append([pkg_source])\n else:\n pkg_params_items.append(\n [rpm_info['name'], pkg_source, rpm_info['version']]\n )\n\n errors = []\n for pkg_item_list in pkg_params_items:\n if pkg_type == 'repository':\n pkgname, version_num = pkg_item_list\n elif pkg_type == 'advisory':\n pkgname = pkg_item_list\n version_num = None\n else:\n try:\n pkgname, pkgpath, version_num = pkg_item_list\n except ValueError:\n pkgname = None\n pkgpath = pkg_item_list[0]\n version_num = None\n\n if version_num is None:\n if pkg_type == 'repository':\n if reinstall and pkgname in old:\n to_reinstall.append((pkgname, pkgname))\n else:\n to_install.append((pkgname, pkgname))\n elif pkg_type == 'advisory':\n to_install.append((pkgname, pkgname))\n else:\n to_install.append((pkgname, pkgpath))\n else:\n # If we are installing a package file and not one from the repo,\n # and version_num is not None, then we can assume that pkgname is\n # not None, since the only way version_num is not None is if RPM\n # metadata parsing was successful.\n if pkg_type == 'repository':\n # yum/dnf does not support comparison operators. If the version\n # starts with an equals sign, ignore it.\n version_num = version_num.lstrip('=')\n if pkgname in has_comparison:\n candidates = _available.get(pkgname, [])\n target = salt.utils.pkg.match_version(\n version_num,\n candidates,\n cmp_func=version_cmp,\n ignore_epoch=ignore_epoch,\n )\n if target is None:\n errors.append(\n 'No version matching \\'{0}{1}\\' could be found '\n '(available: {2})'.format(\n pkgname,\n version_num,\n ', '.join(candidates) if candidates else None\n )\n )\n continue\n else:\n version_num = target\n if _yum() == 'yum':\n # yum install does not support epoch without the arch, and\n # we won't know what the arch will be when it's not\n # provided. It could either be the OS architecture, or\n # 'noarch', and we don't make that distinction in the\n # pkg.list_pkgs return data.\n if ignore_epoch is True:\n version_num = version_num.split(':', 1)[-1]\n arch = ''\n try:\n namepart, archpart = pkgname.rsplit('.', 1)\n except ValueError:\n pass\n else:\n if archpart in salt.utils.pkg.rpm.ARCHES:\n arch = '.' + archpart\n pkgname = namepart\n\n if '*' in version_num:\n # Resolve wildcard matches\n candidates = _available.get(pkgname, [])\n match = salt.utils.itertools.fnmatch_multiple(candidates, version_num)\n if match is not None:\n version_num = match\n else:\n errors.append(\n 'No version matching \\'{0}\\' found for package '\n '\\'{1}\\' (available: {2})'.format(\n version_num,\n pkgname,\n ', '.join(candidates) if candidates else 'none'\n )\n )\n continue\n\n if ignore_epoch is True:\n pkgstr = '{0}-{1}{2}'.format(pkgname, version_num, arch)\n else:\n pkgstr = '{0}-{1}{2}'.format(pkgname, version_num.split(':', 1)[-1], arch)\n\n else:\n pkgstr = pkgpath\n\n # Lambda to trim the epoch from the currently-installed version if\n # no epoch is specified in the specified version\n cver = old_as_list.get(pkgname, [])\n if reinstall and cver:\n for ver in cver:\n if salt.utils.versions.compare(ver1=version_num,\n oper='==',\n ver2=ver,\n cmp_func=version_cmp,\n ignore_epoch=ignore_epoch):\n # This version is already installed, so we need to\n # reinstall.\n to_reinstall.append((pkgname, pkgstr))\n break\n else:\n if not cver:\n to_install.append((pkgname, pkgstr))\n else:\n for ver in cver:\n if salt.utils.versions.compare(ver1=version_num,\n oper='>=',\n ver2=ver,\n cmp_func=version_cmp,\n ignore_epoch=ignore_epoch):\n to_install.append((pkgname, pkgstr))\n break\n else:\n if pkgname is not None:\n if re.match('^kernel(|-devel)$', pkgname):\n # kernel and kernel-devel support multiple\n # installs as their paths do not conflict.\n # Performing a yum/dnf downgrade will be a\n # no-op so just do an install instead. It will\n # fail if there are other interdependencies\n # that have conflicts, and that's OK. We don't\n # want to force anything, we just want to\n # properly handle it if someone tries to\n # install a kernel/kernel-devel of a lower\n # version than the currently-installed one.\n # TODO: find a better way to determine if a\n # package supports multiple installs.\n to_install.append((pkgname, pkgstr))\n else:\n # None of the currently-installed versions are\n # greater than the specified version, so this\n # is a downgrade.\n to_downgrade.append((pkgname, pkgstr))\n\n def _add_common_args(cmd):\n '''\n DRY function to add args common to all yum/dnf commands\n '''\n cmd.extend(options)\n if skip_verify:\n cmd.append('--nogpgcheck')\n if downloadonly:\n cmd.append('--downloadonly')\n\n try:\n holds = list_holds(full=False)\n except SaltInvocationError:\n holds = []\n log.debug(\n 'Failed to get holds, versionlock plugin is probably not '\n 'installed'\n )\n unhold_prevented = []\n\n @contextlib.contextmanager\n def _temporarily_unhold(pkgs, targets):\n '''\n Temporarily unhold packages that need to be updated. Add any\n successfully-removed ones (and any packages not in the list of current\n holds) to the list of targets.\n '''\n to_unhold = {}\n for pkgname, pkgstr in pkgs:\n if pkgname in holds:\n if update_holds:\n to_unhold[pkgname] = pkgstr\n else:\n unhold_prevented.append(pkgname)\n else:\n targets.append(pkgstr)\n\n if not to_unhold:\n yield\n else:\n log.debug('Unholding packages: %s', ', '.join(to_unhold))\n try:\n # Using list() here for python3 compatibility, dict.keys() no\n # longer returns a list in python3.\n unhold_names = list(to_unhold.keys())\n for unheld_pkg, outcome in \\\n six.iteritems(unhold(pkgs=unhold_names)):\n if outcome['result']:\n # Package was successfully unheld, add to targets\n targets.append(to_unhold[unheld_pkg])\n else:\n # Failed to unhold package\n errors.append(unheld_pkg)\n yield\n except Exception as exc:\n errors.append(\n 'Error encountered unholding packages {0}: {1}'\n .format(', '.join(to_unhold), exc)\n )\n finally:\n hold(pkgs=unhold_names)\n\n targets = []\n with _temporarily_unhold(to_install, targets):\n if targets:\n if pkg_type == 'advisory':\n targets = [\"--advisory={0}\".format(t) for t in targets]\n cmd = ['-y']\n if _yum() == 'dnf':\n cmd.extend(['--best', '--allowerasing'])\n _add_common_args(cmd)\n cmd.append('install' if pkg_type != 'advisory' else 'update')\n cmd.extend(targets)\n out = _call_yum(cmd, ignore_retcode=False, redirect_stderr=True)\n if out['retcode'] != 0:\n errors.append(out['stdout'])\n\n targets = []\n with _temporarily_unhold(to_downgrade, targets):\n if targets:\n cmd = ['-y']\n _add_common_args(cmd)\n cmd.append('downgrade')\n cmd.extend(targets)\n out = _call_yum(cmd)\n if out['retcode'] != 0:\n errors.append(out['stdout'])\n\n targets = []\n with _temporarily_unhold(to_reinstall, targets):\n if targets:\n cmd = ['-y']\n _add_common_args(cmd)\n cmd.append('reinstall')\n cmd.extend(targets)\n out = _call_yum(cmd)\n if out['retcode'] != 0:\n errors.append(out['stdout'])\n\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs(versions_as_list=False, attr=diff_attr) if not downloadonly else list_downloaded()\n\n ret = salt.utils.data.compare_dicts(old, new)\n\n for pkgname, _ in to_reinstall:\n if pkgname not in ret or pkgname in old:\n ret.update({pkgname: {'old': old.get(pkgname, ''),\n 'new': new.get(pkgname, '')}})\n\n if unhold_prevented:\n errors.append(\n 'The following package(s) could not be updated because they are '\n 'being held: {0}. Set \\'update_holds\\' to True to temporarily '\n 'unhold these packages so that they can be updated.'.format(\n ', '.join(unhold_prevented)\n )\n )\n\n if errors:\n raise CommandExecutionError(\n 'Error occurred installing{0} package(s)'.format(\n '/reinstalling' if to_reinstall else ''\n ),\n info={'errors': errors, 'changes': ret}\n )\n\n return ret", "response": "Install a package from the yum database."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef upgrade(name=None,\n pkgs=None,\n refresh=True,\n skip_verify=False,\n normalize=True,\n minimal=False,\n obsoletes=True,\n **kwargs):\n '''\n Run a full system upgrade (a ``yum upgrade`` or ``dnf upgrade``), or\n upgrade specified packages. If the packages aren't installed, they will\n not be installed.\n\n .. versionchanged:: 2014.7.0\n .. versionchanged:: 2015.8.12,2016.3.3,2016.11.0\n On minions running systemd>=205, `systemd-run(1)`_ is now used to\n isolate commands which modify installed packages from the\n ``salt-minion`` daemon's control group. This is done to keep systemd\n from killing any yum/dnf commands spawned by Salt when the\n ``salt-minion`` service is restarted. (see ``KillMode`` in the\n `systemd.kill(5)`_ manpage for more information). If desired, usage of\n `systemd-run(1)`_ can be suppressed by setting a :mod:`config option\n ` called ``systemd.scope``, with a value of\n ``False`` (no quotes).\n\n .. _`systemd-run(1)`: https://www.freedesktop.org/software/systemd/man/systemd-run.html\n .. _`systemd.kill(5)`: https://www.freedesktop.org/software/systemd/man/systemd.kill.html\n\n .. versionchanged:: 2019.2.0\n Added ``obsoletes`` and ``minimal`` arguments\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 salt '*' pkg.upgrade name=openssl\n\n Repository Options:\n\n fromrepo\n Specify a package repository (or repositories) from which to install.\n (e.g., ``yum --disablerepo='*' --enablerepo='somerepo'``)\n\n enablerepo (ignored if ``fromrepo`` is specified)\n Specify a disabled package repository (or repositories) to enable.\n (e.g., ``yum --enablerepo='somerepo'``)\n\n disablerepo (ignored if ``fromrepo`` is specified)\n Specify an enabled package repository (or repositories) to disable.\n (e.g., ``yum --disablerepo='somerepo'``)\n\n disableexcludes\n Disable exclude from main, for a repo or for everything.\n (e.g., ``yum --disableexcludes='main'``)\n\n .. versionadded:: 2014.7\n\n name\n The name of the package to be upgraded. Note that this parameter is\n ignored if \"pkgs\" is passed.\n\n 32-bit packages can be upgraded on 64-bit systems by appending the\n architecture designation (``.i686``, ``.i586``, etc.) to the end of the\n package name.\n\n Warning: if you forget 'name=' and run pkg.upgrade openssl, ALL packages\n are upgraded. This will be addressed in next releases.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade name=openssl\n\n .. versionadded:: 2016.3.0\n\n pkgs\n A list of packages to upgrade from a software repository. Must be\n passed as a python list. A specific version number can be specified\n by using a single-element dict representing the package and its\n version. If the package was not already installed on the system,\n it will not be installed.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade pkgs='[\"foo\", \"bar\"]'\n salt '*' pkg.upgrade pkgs='[\"foo\", {\"bar\": \"1.2.3-4.el5\"}]'\n\n .. versionadded:: 2016.3.0\n\n normalize : True\n Normalize the package name by removing the architecture. This is useful\n for poorly created packages which might include the architecture as an\n actual part of the name such as kernel modules which match a specific\n kernel version.\n\n .. code-block:: bash\n\n salt -G role:nsd pkg.upgrade gpfs.gplbin-2.6.32-279.31.1.el6.x86_64 normalize=False\n\n .. versionadded:: 2016.3.0\n\n minimal : False\n Use upgrade-minimal instead of upgrade (e.g., ``yum upgrade-minimal``)\n Goes to the 'newest' package match which fixes a problem that affects your system.\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade minimal=True\n\n .. versionadded:: 2019.2.0\n\n obsoletes : True\n Controls wether yum/dnf should take obsoletes into account and remove them.\n If set to ``False`` yum will use ``update`` instead of ``upgrade``\n and dnf will be run with ``--obsoletes=False``\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade obsoletes=False\n\n .. versionadded:: 2019.2.0\n\n setopt\n A comma-separated or Python list of key=value options. This list will\n be expanded and ``--setopt`` prepended to each in the yum/dnf command\n that is run.\n\n .. versionadded:: 2019.2.0\n\n .. note::\n To add extra arguments to the ``yum upgrade`` command, pass them as key\n word arguments. For arguments without assignments, pass ``True``\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade security=True exclude='kernel*'\n '''\n options = _get_options(get_extra_options=True, **kwargs)\n\n if salt.utils.data.is_true(refresh):\n refresh_db(**kwargs)\n\n old = list_pkgs()\n\n targets = []\n if name or pkgs:\n try:\n pkg_params = __salt__['pkg_resource.parse_targets'](\n name=name,\n pkgs=pkgs,\n sources=None,\n normalize=normalize,\n **kwargs)[0]\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n if pkg_params:\n # Calling list.extend() on a dict will extend it using the\n # dictionary's keys.\n targets.extend(pkg_params)\n\n cmd = ['--quiet', '-y']\n cmd.extend(options)\n if skip_verify:\n cmd.append('--nogpgcheck')\n if obsoletes:\n cmd.append('upgrade' if not minimal else 'upgrade-minimal')\n else:\n # do not force the removal of obsolete packages\n if _yum() == 'dnf':\n # for dnf we can just disable obsoletes\n cmd.append('--obsoletes=False')\n cmd.append('upgrade' if not minimal else 'upgrade-minimal')\n else:\n # for yum we have to use update instead of upgrade\n cmd.append('update' if not minimal else 'update-minimal')\n cmd.extend(targets)\n result = _call_yum(cmd)\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": "A full system upgrade of a set of packages."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef remove(name=None, pkgs=None, **kwargs): # pylint: disable=W0613\n '''\n .. versionchanged:: 2015.8.12,2016.3.3,2016.11.0\n On minions running systemd>=205, `systemd-run(1)`_ is now used to\n isolate commands which modify installed packages from the\n ``salt-minion`` daemon's control group. This is done to keep systemd\n from killing any yum/dnf commands spawned by Salt when the\n ``salt-minion`` service is restarted. (see ``KillMode`` in the\n `systemd.kill(5)`_ manpage for more information). If desired, usage of\n `systemd-run(1)`_ can be suppressed by setting a :mod:`config option\n ` called ``systemd.scope``, with a value of\n ``False`` (no quotes).\n\n .. _`systemd-run(1)`: https://www.freedesktop.org/software/systemd/man/systemd-run.html\n .. _`systemd.kill(5)`: https://www.freedesktop.org/software/systemd/man/systemd.kill.html\n\n Remove packages\n\n name\n The name of the package to be removed\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'](name, pkgs)[0]\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n old = list_pkgs()\n targets = []\n for target in pkg_params:\n # Check if package version set to be removed is actually installed:\n # old[target] contains a comma-separated list of installed versions\n if target in old and not pkg_params[target]:\n targets.append(target)\n elif target in old and pkg_params[target] in old[target].split(','):\n arch = ''\n pkgname = target\n try:\n namepart, archpart = target.rsplit('.', 1)\n except ValueError:\n pass\n else:\n if archpart in salt.utils.pkg.rpm.ARCHES:\n arch = '.' + archpart\n pkgname = namepart\n targets.append('{0}-{1}{2}'.format(pkgname, pkg_params[target], arch))\n if not targets:\n return {}\n\n out = _call_yum(['-y', 'remove'] + targets)\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 'Error occurred removing package(s)',\n info={'errors': errors, 'changes': ret}\n )\n\n return ret", "response": "Remove packages from a named package."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef hold(name=None, pkgs=None, sources=None, normalize=True, **kwargs): # pylint: disable=W0613\n '''\n .. versionadded:: 2014.7.0\n\n Version-lock packages\n\n .. note::\n Requires the appropriate ``versionlock`` plugin package to be installed:\n\n - On RHEL 5: ``yum-versionlock``\n - On RHEL 6 & 7: ``yum-plugin-versionlock``\n - On Fedora: ``python-dnf-plugins-extras-versionlock``\n\n\n name\n The name of the package to be held.\n\n Multiple Package Options:\n\n pkgs\n A list of packages to hold. Must be passed as a python list. The\n ``name`` parameter will be ignored if this option is passed.\n\n Returns a dict containing the changes.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.hold \n salt '*' pkg.hold pkgs='[\"foo\", \"bar\"]'\n '''\n _check_versionlock()\n\n if not name and not pkgs and not sources:\n raise SaltInvocationError(\n 'One of name, pkgs, or sources must be specified.'\n )\n if pkgs and sources:\n raise SaltInvocationError(\n 'Only one of pkgs or sources can be specified.'\n )\n\n targets = []\n if pkgs:\n targets.extend(pkgs)\n elif sources:\n for source in sources:\n targets.append(next(six.iterkeys(source)))\n else:\n targets.append(name)\n\n current_locks = list_holds(full=False)\n ret = {}\n for target in targets:\n if isinstance(target, dict):\n target = next(six.iterkeys(target))\n\n ret[target] = {'name': target,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n if target not in current_locks:\n if 'test' in __opts__ and __opts__['test']:\n ret[target].update(result=None)\n ret[target]['comment'] = ('Package {0} is set to be held.'\n .format(target))\n else:\n out = _call_yum(['versionlock', target])\n if out['retcode'] == 0:\n ret[target].update(result=True)\n ret[target]['comment'] = ('Package {0} is now being held.'\n .format(target))\n ret[target]['changes']['new'] = 'hold'\n ret[target]['changes']['old'] = ''\n else:\n ret[target]['comment'] = ('Package {0} was unable to be held.'\n .format(target))\n else:\n ret[target].update(result=True)\n ret[target]['comment'] = ('Package {0} is already set to be held.'\n .format(target))\n return ret", "response": "Hold a versionlock package."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef unhold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W0613\n '''\n .. versionadded:: 2014.7.0\n\n Remove version locks\n\n .. note::\n Requires the appropriate ``versionlock`` plugin package to be installed:\n\n - On RHEL 5: ``yum-versionlock``\n - On RHEL 6 & 7: ``yum-plugin-versionlock``\n - On Fedora: ``python-dnf-plugins-extras-versionlock``\n\n\n name\n The name of the package to be unheld\n\n Multiple Package Options:\n\n pkgs\n A list of packages to unhold. Must be passed as a python list. The\n ``name`` parameter will be ignored if this option is passed.\n\n Returns a dict containing the changes.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.unhold \n salt '*' pkg.unhold pkgs='[\"foo\", \"bar\"]'\n '''\n _check_versionlock()\n\n if not name and not pkgs and not sources:\n raise SaltInvocationError(\n 'One of name, pkgs, or sources must be specified.'\n )\n if pkgs and sources:\n raise SaltInvocationError(\n 'Only one of pkgs or sources can be specified.'\n )\n\n targets = []\n if pkgs:\n for pkg in salt.utils.data.repack_dictlist(pkgs):\n targets.append(pkg)\n elif sources:\n for source in sources:\n targets.append(next(iter(source)))\n else:\n targets.append(name)\n\n # Yum's versionlock plugin doesn't support passing just the package name\n # when removing a lock, so we need to get the full list and then use\n # fnmatch below to find the match.\n current_locks = list_holds(full=_yum() == 'yum')\n\n ret = {}\n for target in targets:\n if isinstance(target, dict):\n target = next(six.iterkeys(target))\n\n ret[target] = {'name': target,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n if _yum() == 'dnf':\n search_locks = [x for x in current_locks if x == target]\n else:\n # To accommodate yum versionlock's lack of support for removing\n # locks using just the package name, we have to use fnmatch to do\n # glob matching on the target name, and then for each matching\n # expression double-check that the package name (obtained via\n # _get_hold()) matches the targeted package.\n search_locks = [\n x for x in current_locks\n if fnmatch.fnmatch(x, '*{0}*'.format(target))\n and target == _get_hold(x, full=False)\n ]\n\n if search_locks:\n if __opts__['test']:\n ret[target].update(result=None)\n ret[target]['comment'] = ('Package {0} is set to be unheld.'\n .format(target))\n else:\n out = _call_yum(['versionlock', 'delete'] + search_locks)\n if out['retcode'] == 0:\n ret[target].update(result=True)\n ret[target]['comment'] = ('Package {0} is no longer held.'\n .format(target))\n ret[target]['changes']['new'] = ''\n ret[target]['changes']['old'] = 'hold'\n else:\n ret[target]['comment'] = ('Package {0} was unable to be '\n 'unheld.'.format(target))\n else:\n ret[target].update(result=True)\n ret[target]['comment'] = ('Package {0} is not being held.'\n .format(target))\n return ret", "response": "Remove version locks from a package."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_holds(pattern=__HOLD_PATTERN, full=True):\n r'''\n .. versionchanged:: 2016.3.0,2015.8.4,2015.5.10\n Function renamed from ``pkg.get_locked_pkgs`` to ``pkg.list_holds``.\n\n List information on locked packages\n\n .. note::\n Requires the appropriate ``versionlock`` plugin package to be installed:\n\n - On RHEL 5: ``yum-versionlock``\n - On RHEL 6 & 7: ``yum-plugin-versionlock``\n - On Fedora: ``python-dnf-plugins-extras-versionlock``\n\n pattern : \\w+(?:[.-][^-]+)*\n Regular expression used to match the package name\n\n full : True\n Show the full hold definition including version and epoch. Set to\n ``False`` to return just the name of the package(s) being held.\n\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_holds\n salt '*' pkg.list_holds full=False\n '''\n _check_versionlock()\n\n out = __salt__['cmd.run']([_yum(), 'versionlock', 'list'],\n python_shell=False)\n ret = []\n for line in salt.utils.itertools.split(out, '\\n'):\n match = _get_hold(line, pattern=pattern, full=full)\n if match is not None:\n ret.append(match)\n return ret", "response": "r List the packages that are currently held."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nlists all available group names and languages on this system", "response": "def group_list():\n '''\n .. versionadded:: 2014.1.0\n\n Lists all groups known by yum on this system\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.group_list\n '''\n ret = {'installed': [],\n 'available': [],\n 'installed environments': [],\n 'available environments': [],\n 'available languages': {}}\n\n section_map = {\n 'installed groups:': 'installed',\n 'available groups:': 'available',\n 'installed environment groups:': 'installed environments',\n 'available environment groups:': 'available environments',\n 'available language groups:': 'available languages',\n }\n\n out = __salt__['cmd.run_stdout'](\n [_yum(), 'grouplist', 'hidden'],\n output_loglevel='trace',\n python_shell=False\n )\n key = None\n for line in salt.utils.itertools.split(out, '\\n'):\n line_lc = line.lower()\n if line_lc == 'done':\n break\n\n section_lookup = section_map.get(line_lc)\n if section_lookup is not None and section_lookup != key:\n key = section_lookup\n continue\n\n # Ignore any administrative comments (plugin info, repo info, etc.)\n if key is None:\n continue\n\n line = line.strip()\n if key != 'available languages':\n ret[key].append(line)\n else:\n match = re.match(r'(.+) \\[(.+)\\]', line)\n if match:\n name, lang = match.groups()\n ret[key][line] = {'name': name, 'language': lang}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef group_info(name, expand=False):\n '''\n .. versionadded:: 2014.1.0\n .. versionchanged:: 2016.3.0,2015.8.4,2015.5.10\n The return data has changed. A new key ``type`` has been added to\n distinguish environment groups from package groups. Also, keys for the\n group name and group ID have been added. The ``mandatory packages``,\n ``optional packages``, and ``default packages`` keys have been renamed\n to ``mandatory``, ``optional``, and ``default`` for accuracy, as\n environment groups include other groups, and not packages. Finally,\n this function now properly identifies conditional packages.\n\n Lists packages belonging to a certain group\n\n name\n Name of the group to query\n\n expand : False\n If the specified group is an environment group, then the group will be\n expanded and the return data will include package names instead of\n group names.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.group_info 'Perl Support'\n '''\n pkgtypes = ('mandatory', 'optional', 'default', 'conditional')\n ret = {}\n for pkgtype in pkgtypes:\n ret[pkgtype] = set()\n\n cmd = [_yum(), '--quiet', 'groupinfo', name]\n out = __salt__['cmd.run_stdout'](\n cmd,\n output_loglevel='trace',\n python_shell=False\n )\n\n g_info = {}\n for line in salt.utils.itertools.split(out, '\\n'):\n try:\n key, value = [x.strip() for x in line.split(':')]\n g_info[key.lower()] = value\n except ValueError:\n continue\n\n if 'environment group' in g_info:\n ret['type'] = 'environment group'\n elif 'group' in g_info:\n ret['type'] = 'package group'\n\n ret['group'] = g_info.get('environment group') or g_info.get('group')\n ret['id'] = g_info.get('environment-id') or g_info.get('group-id')\n if not ret['group'] and not ret['id']:\n raise CommandExecutionError('Group \\'{0}\\' not found'.format(name))\n\n ret['description'] = g_info.get('description', '')\n\n pkgtypes_capturegroup = '(' + '|'.join(pkgtypes) + ')'\n for pkgtype in pkgtypes:\n target_found = False\n for line in salt.utils.itertools.split(out, '\\n'):\n line = line.strip().lstrip(string.punctuation)\n match = re.match(\n pkgtypes_capturegroup + r' (?:groups|packages):\\s*$',\n line.lower()\n )\n if match:\n if target_found:\n # We've reached a new section, break from loop\n break\n else:\n if match.group(1) == pkgtype:\n # We've reached the targeted section\n target_found = True\n continue\n if target_found:\n if expand and ret['type'] == 'environment group':\n expanded = group_info(line, expand=True)\n # Don't shadow the pkgtype variable from the outer loop\n for p_type in pkgtypes:\n ret[p_type].update(set(expanded[p_type]))\n else:\n ret[pkgtype].add(line)\n\n for pkgtype in pkgtypes:\n ret[pkgtype] = sorted(ret[pkgtype])\n\n return ret", "response": "Return information about a specific group."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef group_diff(name):\n '''\n .. versionadded:: 2014.1.0\n .. versionchanged:: 2016.3.0,2015.8.4,2015.5.10\n Environment groups are now supported. The key names have been renamed,\n similar to the changes made in :py:func:`pkg.group_info\n `.\n\n Lists which of a group's packages are installed and which are not\n installed\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.group_diff 'Perl Support'\n '''\n pkgtypes = ('mandatory', 'optional', 'default', 'conditional')\n ret = {}\n for pkgtype in pkgtypes:\n ret[pkgtype] = {'installed': [], 'not installed': []}\n\n pkgs = list_pkgs()\n group_pkgs = group_info(name, expand=True)\n for pkgtype in pkgtypes:\n for member in group_pkgs.get(pkgtype, []):\n if member in pkgs:\n ret[pkgtype]['installed'].append(member)\n else:\n ret[pkgtype]['not installed'].append(member)\n return ret", "response": "Return a dictionary of the keys that are not installed and which are not installed."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist all repos in a basedir.", "response": "def list_repos(basedir=None, **kwargs):\n '''\n Lists all repos in (default: all dirs in `reposdir` yum option).\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_repos\n salt '*' pkg.list_repos basedir=/path/to/dir\n salt '*' pkg.list_repos basedir=/path/to/dir,/path/to/another/dir\n '''\n\n basedirs = _normalize_basedir(basedir)\n repos = {}\n log.debug('Searching for repos in %s', basedirs)\n for bdir in basedirs:\n if not os.path.exists(bdir):\n continue\n for repofile in os.listdir(bdir):\n repopath = '{0}/{1}'.format(bdir, repofile)\n if not repofile.endswith('.repo'):\n continue\n filerepos = _parse_repo_file(repopath)[1]\n for reponame in filerepos:\n repo = filerepos[reponame]\n repo['file'] = repopath\n repos[reponame] = repo\n return repos"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_repo(name, basedir=None, **kwargs): # pylint: disable=W0613\n '''\n Display a repo from (default basedir: all dirs in ``reposdir``\n yum option).\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.get_repo myrepo\n salt '*' pkg.get_repo myrepo basedir=/path/to/dir\n salt '*' pkg.get_repo myrepo basedir=/path/to/dir,/path/to/another/dir\n '''\n repos = list_repos(basedir)\n\n # Find out what file the repo lives in\n repofile = ''\n for repo in repos:\n if repo == name:\n repofile = repos[repo]['file']\n\n if repofile:\n # Return just one repo\n filerepos = _parse_repo_file(repofile)[1]\n return filerepos[name]\n return {}", "response": "Get a single repo from a given basedir"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndelete a repo from the specified base directory.", "response": "def del_repo(repo, basedir=None, **kwargs): # pylint: disable=W0613\n '''\n Delete a repo from (default basedir: all dirs in `reposdir` yum\n option).\n\n If the .repo file in which the repo exists does not contain any other repo\n configuration, the file itself will be deleted.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.del_repo myrepo\n salt '*' pkg.del_repo myrepo basedir=/path/to/dir\n salt '*' pkg.del_repo myrepo basedir=/path/to/dir,/path/to/another/dir\n '''\n # this is so we know which dirs are searched for our error messages below\n basedirs = _normalize_basedir(basedir)\n repos = list_repos(basedirs)\n\n if repo not in repos:\n return 'Error: the {0} repo does not exist in {1}'.format(\n repo, basedirs)\n\n # Find out what file the repo lives in\n repofile = ''\n for arepo in repos:\n if arepo == repo:\n repofile = repos[arepo]['file']\n\n # See if the repo is the only one in the file\n onlyrepo = True\n for arepo in six.iterkeys(repos):\n if arepo == repo:\n continue\n if repos[arepo]['file'] == repofile:\n onlyrepo = False\n\n # If this is the only repo in the file, delete the file itself\n if onlyrepo:\n os.remove(repofile)\n return 'File {0} containing repo {1} has been removed'.format(\n repofile, repo)\n\n # There must be other repos in this file, write the file with them\n header, filerepos = _parse_repo_file(repofile)\n content = header\n for stanza in six.iterkeys(filerepos):\n if stanza == repo:\n continue\n comments = ''\n if 'comments' in six.iterkeys(filerepos[stanza]):\n comments = salt.utils.pkg.rpm.combine_comments(\n filerepos[stanza]['comments'])\n del filerepos[stanza]['comments']\n content += '\\n[{0}]'.format(stanza)\n for line in filerepos[stanza]:\n content += '\\n{0}={1}'.format(line, filerepos[stanza][line])\n content += '\\n{0}\\n'.format(comments)\n\n with salt.utils.files.fopen(repofile, 'w') as fileout:\n fileout.write(salt.utils.stringutils.to_str(content))\n\n return 'Repo {0} has been removed from {1}'.format(repo, repofile)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef mod_repo(repo, basedir=None, **kwargs):\n '''\n Modify one or more values for a repo. If the repo does not exist, it will\n be created, so long as the following values are specified:\n\n repo\n name by which the yum refers to the repo\n name\n a human-readable name for the repo\n baseurl\n the URL for yum to reference\n mirrorlist\n the URL for yum to reference\n key_url\n the URL to gather the repo key from (salt:// or any other scheme supported by cp.cache_file)\n\n Key/Value pairs may also be removed from a repo's configuration by setting\n a key to a blank value. Bear in mind that a name cannot be deleted, and a\n baseurl can only be deleted if a mirrorlist is specified (or vice versa).\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.mod_repo reponame enabled=1 gpgcheck=1\n salt '*' pkg.mod_repo reponame basedir=/path/to/dir enabled=1\n salt '*' pkg.mod_repo reponame baseurl= mirrorlist=http://host.com/\n '''\n # Filter out '__pub' arguments, as well as saltenv\n repo_opts = dict(\n (x, kwargs[x]) for x in kwargs\n if not x.startswith('__') and x not in ('saltenv',)\n )\n\n if all(x in repo_opts for x in ('mirrorlist', 'baseurl')):\n raise SaltInvocationError(\n 'Only one of \\'mirrorlist\\' and \\'baseurl\\' can be specified'\n )\n\n # Build a list of keys to be deleted\n todelete = []\n # list() of keys because the dict could be shrinking in the for loop.\n for key in list(repo_opts):\n if repo_opts[key] != 0 and not repo_opts[key]:\n del repo_opts[key]\n todelete.append(key)\n\n # Add baseurl or mirrorlist to the 'todelete' list if the other was\n # specified in the repo_opts\n if 'mirrorlist' in repo_opts:\n todelete.append('baseurl')\n elif 'baseurl' in repo_opts:\n todelete.append('mirrorlist')\n\n # Fail if the user tried to delete the name\n if 'name' in todelete:\n raise SaltInvocationError('The repo name cannot be deleted')\n\n # Give the user the ability to change the basedir\n repos = {}\n basedirs = _normalize_basedir(basedir)\n repos = list_repos(basedirs)\n\n repofile = ''\n header = ''\n filerepos = {}\n if repo not in repos:\n # If the repo doesn't exist, create it in a new file in the first\n # repo directory that exists\n newdir = None\n for d in basedirs:\n if os.path.exists(d):\n newdir = d\n break\n if not newdir:\n raise SaltInvocationError(\n 'The repo does not exist and needs to be created, but none '\n 'of the following basedir directories exist: {0}'.format(basedirs)\n )\n\n repofile = '{0}/{1}.repo'.format(newdir, repo)\n\n if 'name' not in repo_opts:\n raise SaltInvocationError(\n 'The repo does not exist and needs to be created, but a name '\n 'was not given'\n )\n\n if 'baseurl' not in repo_opts and 'mirrorlist' not in repo_opts:\n raise SaltInvocationError(\n 'The repo does not exist and needs to be created, but either '\n 'a baseurl or a mirrorlist needs to be given'\n )\n filerepos[repo] = {}\n else:\n # The repo does exist, open its file\n repofile = repos[repo]['file']\n header, filerepos = _parse_repo_file(repofile)\n\n # Error out if they tried to delete baseurl or mirrorlist improperly\n if 'baseurl' in todelete:\n if 'mirrorlist' not in repo_opts and 'mirrorlist' \\\n not in filerepos[repo]:\n raise SaltInvocationError(\n 'Cannot delete baseurl without specifying mirrorlist'\n )\n if 'mirrorlist' in todelete:\n if 'baseurl' not in repo_opts and 'baseurl' \\\n not in filerepos[repo]:\n raise SaltInvocationError(\n 'Cannot delete mirrorlist without specifying baseurl'\n )\n\n # Import repository gpg key\n if 'key_url' in repo_opts:\n key_url = kwargs['key_url']\n fn_ = __salt__['cp.cache_file'](key_url, saltenv=(kwargs['saltenv'] if 'saltenv' in kwargs else 'base'))\n if not fn_:\n raise CommandExecutionError(\n 'Error: Unable to copy key from URL {0} for repository {1}'.format(key_url, repo_opts['name'])\n )\n cmd = ['rpm', '--import', fn_]\n out = __salt__['cmd.retcode'](cmd, python_shell=False, **kwargs)\n if out != salt.defaults.exitcodes.EX_OK:\n raise CommandExecutionError(\n 'Error: Unable to import key from URL {0} for repository {1}'.format(key_url, repo_opts['name'])\n )\n del repo_opts['key_url']\n\n # Delete anything in the todelete list\n for key in todelete:\n if key in six.iterkeys(filerepos[repo].copy()):\n del filerepos[repo][key]\n\n _bool_to_str = lambda x: '1' if x else '0'\n # Old file or new, write out the repos(s)\n filerepos[repo].update(repo_opts)\n content = header\n for stanza in six.iterkeys(filerepos):\n comments = salt.utils.pkg.rpm.combine_comments(\n filerepos[stanza].pop('comments', [])\n )\n content += '[{0}]\\n'.format(stanza)\n for line in six.iterkeys(filerepos[stanza]):\n content += '{0}={1}\\n'.format(\n line,\n filerepos[stanza][line]\n if not isinstance(filerepos[stanza][line], bool)\n else _bool_to_str(filerepos[stanza][line])\n )\n content += comments + '\\n'\n\n with salt.utils.files.fopen(repofile, 'w') as fileout:\n fileout.write(salt.utils.stringutils.to_str(content))\n\n return {repofile: filerepos}", "response": "Modify one or more values for a repo."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _parse_repo_file(filename):\n '''\n Turn a single repo file into a dict\n '''\n parsed = configparser.ConfigParser()\n config = {}\n\n try:\n parsed.read(filename)\n except configparser.MissingSectionHeaderError as err:\n log.error(\n 'Failed to parse file %s, error: %s',\n filename, err.message\n )\n return ('', {})\n\n for section in parsed._sections:\n section_dict = dict(parsed._sections[section])\n section_dict.pop('__name__', None)\n config[section] = section_dict\n\n # Try to extract header comments, as well as comments for each repo. Read\n # from the beginning of the file and assume any leading comments are\n # header comments. Continue to read each section header and then find the\n # comments for each repo.\n headers = ''\n section = None\n with salt.utils.files.fopen(filename, 'r') as repofile:\n for line in repofile:\n line = salt.utils.stringutils.to_unicode(line)\n line = line.strip()\n if line.startswith('#'):\n if section is None:\n headers += line + '\\n'\n else:\n try:\n comments = config[section].setdefault('comments', [])\n comments.append(line[1:].lstrip())\n except KeyError:\n log.debug(\n 'Found comment in %s which does not appear to '\n 'belong to any repo section: %s', filename, line\n )\n elif line.startswith('[') and line.endswith(']'):\n section = line[1:-1]\n\n return (headers, salt.utils.data.decode(config))", "response": "Parse a single repo file into a dict containing the headers and comments for each repo section."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef owner(*paths, **kwargs):\n '''\n .. versionadded:: 2014.7.0\n\n Return the name of the package that owns the file. Multiple file paths can\n be passed. Like :mod:`pkg.version `, if a\n single path is passed, a string will be returned, and if multiple paths are\n passed, a dictionary of file/package name pairs will be returned.\n\n If the file is not owned by a package, or is not present on the minion,\n then an empty string will be returned for that path.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.owner /usr/bin/apachectl\n salt '*' pkg.owner /usr/bin/apachectl /etc/httpd/conf/httpd.conf\n '''\n if not paths:\n return ''\n ret = {}\n cmd_prefix = ['rpm', '-qf', '--queryformat', '%{name}']\n for path in paths:\n ret[path] = __salt__['cmd.run_stdout'](\n cmd_prefix + [path],\n output_loglevel='trace',\n python_shell=False\n )\n if 'not owned' in ret[path].lower():\n ret[path] = ''\n if len(ret) == 1:\n return next(six.itervalues(ret))\n return ret", "response": "Return the name of the package that owns the file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef download(*packages, **kwargs):\n '''\n .. versionadded:: 2015.5.0\n\n Download packages to the local disk. Requires ``yumdownloader`` from\n ``yum-utils`` package.\n\n .. note::\n\n ``yum-utils`` will already be installed on the minion if the package\n was installed from the Fedora / EPEL repositories.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pkg.download httpd\n salt '*' pkg.download httpd postfix\n '''\n if not packages:\n raise SaltInvocationError('No packages were specified')\n\n CACHE_DIR = '/var/cache/yum/packages'\n if not os.path.exists(CACHE_DIR):\n os.makedirs(CACHE_DIR)\n cached_pkgs = os.listdir(CACHE_DIR)\n to_purge = []\n for pkg in packages:\n to_purge.extend([os.path.join(CACHE_DIR, x)\n for x in cached_pkgs\n if x.startswith('{0}-'.format(pkg))])\n for purge_target in set(to_purge):\n log.debug('Removing cached package %s', purge_target)\n try:\n os.unlink(purge_target)\n except OSError as exc:\n log.error('Unable to remove %s: %s', purge_target, exc)\n\n cmd = ['yumdownloader', '-q', '--destdir={0}'.format(CACHE_DIR)]\n cmd.extend(packages)\n __salt__['cmd.run'](\n cmd,\n output_loglevel='trace',\n python_shell=False\n )\n ret = {}\n for dld_result in os.listdir(CACHE_DIR):\n if not dld_result.endswith('.rpm'):\n continue\n pkg_name = None\n pkg_file = None\n for query_pkg in packages:\n if dld_result.startswith('{0}-'.format(query_pkg)):\n pkg_name = query_pkg\n pkg_file = dld_result\n break\n if pkg_file is not None:\n ret[pkg_name] = os.path.join(CACHE_DIR, pkg_file)\n\n if not ret:\n raise CommandExecutionError(\n 'Unable to download any of the following packages: {0}'\n .format(', '.join(packages))\n )\n\n failed = [x for x in packages if x not in ret]\n if failed:\n ret['_error'] = ('The following package(s) failed to download: {0}'\n .format(', '.join(failed)))\n return ret", "response": "Download a list of packages to the local disk."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef diff(*paths, **kwargs):\n '''\n Return a formatted diff between current files and original in a package.\n NOTE: this function includes all files (configuration and not), but does\n not work on binary content.\n\n :param path: Full path to the installed file\n :return: Difference string or raises and exception if examined file is binary.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pkg.diff /etc/apache2/httpd.conf /etc/sudoers\n '''\n ret = {}\n\n pkg_to_paths = {}\n for pth in paths:\n pth_pkg = __salt__['lowpkg.owner'](pth)\n if not pth_pkg:\n ret[pth] = os.path.exists(pth) and 'Not managed' or 'N/A'\n else:\n if pkg_to_paths.get(pth_pkg) is None:\n pkg_to_paths[pth_pkg] = []\n pkg_to_paths[pth_pkg].append(pth)\n\n if pkg_to_paths:\n local_pkgs = __salt__['pkg.download'](*pkg_to_paths.keys())\n for pkg, files in pkg_to_paths.items():\n for path in files:\n ret[path] = __salt__['lowpkg.diff'](\n local_pkgs[pkg]['path'], path) or 'Unchanged'\n\n return ret", "response": "Return a formatted diff between current files and original files in a package."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_patches(installed_only=False):\n '''\n List all known patches in repos.\n '''\n patches = {}\n\n cmd = [_yum(), '--quiet', 'updateinfo', 'list', 'all']\n ret = __salt__['cmd.run_stdout'](\n cmd,\n python_shell=False\n )\n for line in salt.utils.itertools.split(ret, os.linesep):\n inst, advisory_id, sev, pkg = re.match(r'([i|\\s]) ([^\\s]+) +([^\\s]+) +([^\\s]+)',\n line).groups()\n if inst != 'i' and installed_only:\n continue\n patches[advisory_id] = {\n 'installed': True if inst == 'i' else False,\n 'summary': pkg\n }\n return patches", "response": "Return a dict of all known patches in repos."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef complete_transaction(cleanup_only=False, recursive=False, max_attempts=3):\n '''\n .. versionadded:: Fluorine\n\n Execute ``yum-complete-transaction``, which is provided by the ``yum-utils`` package.\n\n cleanup_only\n Specify if the ``--cleanup-only`` option should be supplied.\n\n recursive\n Specify if ``yum-complete-transaction`` should be called recursively\n (it only completes one transaction at a time).\n\n max_attempts\n If ``recursive`` is ``True``, the maximum times ``yum-complete-transaction`` should be called.\n\n .. note::\n\n Recursive calls will stop once ``No unfinished transactions left.`` is in the returned output.\n\n .. note::\n\n ``yum-utils`` will already be installed on the minion if the package\n was installed from the Fedora / EPEL repositories.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pkg.complete_transaction\n salt '*' pkg.complete_transaction cleanup_only=True\n salt '*' pkg.complete_transaction recursive=True max_attempts=5\n '''\n\n return _complete_transaction(cleanup_only, recursive, max_attempts, 1, [])", "response": "Returns a new version of the base base"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _complete_transaction(cleanup_only, recursive, max_attempts, run_count, cmd_ret_list):\n '''\n .. versionadded:: Fluorine\n\n Called from ``complete_transaction`` to protect the arguments\n used for tail recursion, ``run_count`` and ``cmd_ret_list``.\n '''\n\n cmd = ['yum-complete-transaction']\n if cleanup_only:\n cmd.append('--cleanup-only')\n\n cmd_ret_list.append(__salt__['cmd.run_all'](\n cmd,\n output_loglevel='trace',\n python_shell=False\n ))\n\n if (cmd_ret_list[-1]['retcode'] == salt.defaults.exitcodes.EX_OK and\n recursive and\n 'No unfinished transactions left.' not in cmd_ret_list[-1]['stdout']):\n if run_count >= max_attempts:\n cmd_ret_list[-1]['retcode'] = salt.defaults.exitcodes.EX_GENERIC\n log.error('Attempt %s/%s exceeded `max_attempts` for command: `%s`',\n run_count, max_attempts, ' '.join(cmd))\n raise CommandExecutionError('The `max_attempts` limit was reached and unfinished transactions remain.'\n ' You may wish to increase `max_attempts` or re-execute this module.',\n info={'results': cmd_ret_list})\n else:\n return _complete_transaction(cleanup_only, recursive, max_attempts, run_count + 1, cmd_ret_list)\n\n return cmd_ret_list", "response": "Helper function to run the yum - complete - transaction command and return the list of output"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef present(name, value, zone, record_type, ttl=None, identifier=None, region=None, key=None,\n keyid=None, profile=None, wait_for_sync=True, split_dns=False, private_zone=False):\n '''\n Ensure the Route53 record is present.\n\n name\n Name of the record.\n\n value\n Value of the record. As a special case, you can pass in:\n `private:` to have the function autodetermine the private IP\n `public:` to have the function autodetermine the public IP\n\n zone\n The zone to create the record in.\n\n record_type\n The record type (A, NS, MX, TXT, etc.)\n\n ttl\n The time to live for the record.\n\n identifier\n The unique identifier to use for this record.\n\n region\n The region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that contains a dict\n with region, key and keyid.\n\n wait_for_sync\n Wait for an INSYNC change status from Route53 before returning success.\n\n split_dns\n Route53 supports parallel public and private DNS zones with the same name.\n\n private_zone\n If using split_dns, specify if this is the private zone.\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n # If a list is passed in for value, change it to a comma-separated string\n # So it will work with subsequent boto module calls and string functions\n if isinstance(value, list):\n value = ','.join(value)\n elif value.startswith('private:') or value.startswith('public:'):\n name_tag = value.split(':', 1)[1]\n in_states = ('pending', 'rebooting', 'running', 'stopping', 'stopped')\n r = __salt__['boto_ec2.find_instances'](name=name_tag,\n return_objs=True,\n in_states=in_states,\n profile=profile)\n if not r:\n ret['comment'] = 'Error: instance with Name tag {0} not found'.format(name_tag)\n ret['result'] = False\n return ret\n if len(r) > 1:\n ret['comment'] = 'Error: Name tag {0} matched more than one instance'.format(name_tag)\n ret['result'] = False\n return ret\n instance = r[0]\n private_ip = getattr(instance, 'private_ip_address', None)\n public_ip = getattr(instance, 'ip_address', None)\n if value.startswith('private:'):\n value = private_ip\n log.info('Found private IP %s for instance %s', private_ip, name_tag)\n else:\n if public_ip is None:\n ret['comment'] = 'Error: No Public IP assigned to instance with Name {0}'.format(name_tag)\n ret['result'] = False\n return ret\n value = public_ip\n log.info('Found public IP %s for instance %s', public_ip, name_tag)\n\n try:\n record = __salt__['boto_route53.get_record'](name, zone, record_type,\n False, region, key, keyid,\n profile, split_dns,\n private_zone, identifier)\n except SaltInvocationError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n ret['result'] = False\n return ret\n\n if isinstance(record, dict) and not record:\n if __opts__['test']:\n ret['comment'] = 'Route53 record {0} set to be added.'.format(name)\n ret['result'] = None\n return ret\n added = __salt__['boto_route53.add_record'](name, value, zone,\n record_type, identifier,\n ttl, region, key, keyid,\n profile, wait_for_sync,\n split_dns, private_zone)\n if added:\n ret['changes']['old'] = None\n ret['changes']['new'] = {'name': name,\n 'value': value,\n 'record_type': record_type,\n 'ttl': ttl,\n 'identifier': identifier}\n ret['comment'] = 'Added {0} Route53 record.'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to add {0} Route53 record.'.format(name)\n return ret\n elif record:\n need_to_update = False\n # Values can be a comma separated list and some values will end with a\n # period (even if we set it without one). To easily check this we need\n # to split and check with the period stripped from the input and what's\n # in route53.\n # TODO: figure out if this will cause us problems with some records.\n _values = [x.rstrip('.') for x in value.split(',')]\n _r_values = [x.rstrip('.') for x in record['value'].split(',')]\n _values.sort()\n _r_values.sort()\n if _values != _r_values:\n need_to_update = True\n if identifier and identifier != record['identifier']:\n need_to_update = True\n if ttl and six.text_type(ttl) != six.text_type(record['ttl']):\n need_to_update = True\n if need_to_update:\n if __opts__['test']:\n ret['comment'] = 'Route53 record {0} set to be updated.'.format(name)\n ret['result'] = None\n return ret\n updated = __salt__['boto_route53.update_record'](name, value, zone,\n record_type,\n identifier, ttl,\n region, key,\n keyid, profile,\n wait_for_sync,\n split_dns,\n private_zone)\n if updated:\n ret['changes']['old'] = record\n ret['changes']['new'] = {'name': name,\n 'value': value,\n 'record_type': record_type,\n 'ttl': ttl,\n 'identifier': identifier}\n ret['comment'] = 'Updated {0} Route53 record.'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to update {0} Route53 record.'.format(name)\n else:\n ret['comment'] = '{0} exists.'.format(name)\n return ret", "response": "Ensure a Route53 record is present."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures a Route53 record is absent.", "response": "def absent(\n name,\n zone,\n record_type,\n identifier=None,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n wait_for_sync=True,\n split_dns=False,\n private_zone=False):\n '''\n Ensure the Route53 record is deleted.\n\n name\n Name of the record.\n\n zone\n The zone to delete the record from.\n\n record_type\n The record type (A, NS, MX, TXT, etc.)\n\n identifier\n An identifier to match for deletion.\n\n region\n The region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n\n wait_for_sync\n Wait for an INSYNC change status from Route53.\n\n split_dns\n Route53 supports a public and private DNS zone with the same\n names.\n\n private_zone\n If using split_dns, specify if this is the private zone.\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n record = __salt__['boto_route53.get_record'](name, zone, record_type,\n False, region, key, keyid,\n profile, split_dns,\n private_zone, identifier)\n if record:\n if __opts__['test']:\n ret['comment'] = 'Route53 record {0} set to be deleted.'.format(name)\n ret['result'] = None\n return ret\n deleted = __salt__['boto_route53.delete_record'](name, zone,\n record_type,\n identifier, False,\n region, key, keyid,\n profile,\n wait_for_sync,\n split_dns,\n private_zone)\n if deleted:\n ret['changes']['old'] = record\n ret['changes']['new'] = None\n ret['comment'] = 'Deleted {0} Route53 record.'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to delete {0} Route53 record.'.format(name)\n else:\n ret['comment'] = '{0} does not exist.'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef hosted_zone_present(name, domain_name=None, private_zone=False, caller_ref=None, comment='',\n vpc_id=None, vpc_name=None, vpc_region=None, region=None, key=None,\n keyid=None, profile=None):\n '''\n Ensure a hosted zone exists with the given attributes. Note that most\n things cannot be modified once a zone is created - it must be deleted and\n re-spun to update these attributes:\n\n - private_zone (AWS API limitation).\n - comment (the appropriate call exists in the AWS API and in boto3, but has\n not, as of this writing, been added to boto2).\n - vpc_id (same story - we really need to rewrite this module with boto3)\n - vpc_name (really just a pointer to vpc_id anyway).\n - vpc_region (again, supported in boto3 but not boto2).\n\n If you need the ability to update these attributes, please use the newer\n boto3_route53 module instead.\n\n name\n The name of the state definition.\n\n domain_name\n The name of the domain. This must be fully-qualified, terminating with a period. This is\n the name you have registered with your domain registrar. It is also the name you will\n delegate from your registrar to the Amazon Route 53 delegation servers returned in response\n to this request. Defaults to the value of name if not provided.\n\n private_zone\n Set True if creating a private hosted zone.\n\n caller_ref\n A unique string that identifies the request and that allows create_hosted_zone() calls to be\n retried without the risk of executing the operation twice. This helps ensure idempotency\n across state calls, but can cause issues if a zone is deleted and then an attempt is made\n to recreate it with the same caller_ref. If not provided, a unique UUID will be generated\n at each state run, which avoids the risk of the above (transient) error. This option is\n generally not needed. Maximum length of 128.\n\n comment\n Any comments you want to include about the hosted zone.\n\n vpc_id\n When creating a private hosted zone, either the VPC ID or VPC Name to associate with is\n required. Exclusive with vpe_name. Ignored when creating a non-private zone.\n\n vpc_name\n When creating a private hosted zone, either the VPC ID or VPC Name to associate with is\n required. Exclusive with vpe_id. Ignored when creating a non-private zone.\n\n vpc_region\n When creating a private hosted zone, the region of the associated VPC is required. If not\n provided, an effort will be made to determine it from vpc_id or vpc_name, where possible.\n If this fails, you'll need to provide an explicit value for this option. Ignored when\n creating a non-private zone.\n '''\n domain_name = domain_name if domain_name else name\n\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n # First translaste vpc_name into a vpc_id if possible\n if private_zone:\n if not salt.utils.data.exactly_one((vpc_name, vpc_id)):\n raise SaltInvocationError('Either vpc_name or vpc_id is required when creating a '\n 'private zone.')\n vpcs = __salt__['boto_vpc.describe_vpcs'](\n vpc_id=vpc_id, name=vpc_name, region=region, key=key,\n keyid=keyid, profile=profile).get('vpcs', [])\n if vpc_region and vpcs:\n vpcs = [v for v in vpcs if v['region'] == vpc_region]\n if not vpcs:\n msg = 'Private zone requested but a VPC matching given criteria not found.'\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n if len(vpcs) > 1:\n log.error(\n 'Private zone requested but multiple VPCs matching given '\n 'criteria found: %s', [v['id'] for v in vpcs]\n )\n return None\n vpc = vpcs[0]\n if vpc_name:\n vpc_id = vpc['id']\n if not vpc_region:\n vpc_region = vpc['region']\n\n # Next, see if it (or they) exist at all, anywhere?\n deets = __salt__['boto_route53.describe_hosted_zones'](\n domain_name=domain_name, region=region, key=key, keyid=keyid,\n profile=profile)\n\n create = False\n if not deets:\n create = True\n else: # Something exists - now does it match our criteria?\n if (salt.utils.json.loads(deets['HostedZone']['Config']['PrivateZone']) !=\n private_zone):\n create = True\n else:\n if private_zone:\n for d in deets.get('VPCs', {}):\n if (d['VPCId'] == vpc_id\n and d['VPCRegion'] == vpc_region):\n create = False\n break\n else:\n create = True\n if not create:\n ret['comment'] = 'Hostd Zone {0} already in desired state'.format(\n domain_name)\n else:\n # Until we get modifies in place with boto3, the best option is to\n # attempt creation and let route53 tell us if we're stepping on\n # toes. We can't just fail, because some scenarios (think split\n # horizon DNS) require zones with identical names but different\n # settings...\n log.info('A Hosted Zone with name %s already exists, but with '\n 'different settings. Will attempt to create the one '\n 'requested on the assumption this is what is desired. '\n 'This may fail...', domain_name)\n\n if create:\n if caller_ref is None:\n caller_ref = six.text_type(uuid.uuid4())\n if __opts__['test']:\n ret['comment'] = 'Route53 Hosted Zone {0} set to be added.'.format(\n domain_name)\n ret['result'] = None\n return ret\n res = __salt__['boto_route53.create_hosted_zone'](domain_name=domain_name,\n caller_ref=caller_ref, comment=comment, private_zone=private_zone,\n vpc_id=vpc_id, vpc_region=vpc_region, region=region, key=key,\n keyid=keyid, profile=profile)\n if res:\n msg = 'Hosted Zone {0} successfully created'.format(domain_name)\n log.info(msg)\n ret['comment'] = msg\n ret['changes']['old'] = None\n ret['changes']['new'] = res\n else:\n ret['comment'] = 'Creating Hosted Zone {0} failed'.format(\n domain_name)\n ret['result'] = False\n\n return ret", "response": "Creates a new hosted zone with the given attributes."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef hosted_zone_absent(name, domain_name=None, region=None, key=None,\n keyid=None, profile=None):\n '''\n Ensure the Route53 Hostes Zone described is absent\n\n name\n The name of the state definition.\n\n domain_name\n The FQDN (including final period) of the zone you wish absent. If not\n provided, the value of name will be used.\n\n '''\n domain_name = domain_name if domain_name else name\n\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n deets = __salt__['boto_route53.describe_hosted_zones'](\n domain_name=domain_name, region=region, key=key, keyid=keyid,\n profile=profile)\n if not deets:\n ret['comment'] = 'Hosted Zone {0} already absent'.format(domain_name)\n log.info(ret['comment'])\n return ret\n if __opts__['test']:\n ret['comment'] = 'Route53 Hosted Zone {0} set to be deleted.'.format(\n domain_name)\n ret['result'] = None\n return ret\n # Not entirely comfortable with this - no safety checks around pub/priv, VPCs\n # or anything else. But this is all the module function exposes, so hmph.\n # Inclined to put it on the \"wait 'til we port to boto3\" pile in any case :)\n if __salt__['boto_route53.delete_zone'](\n zone=domain_name, region=region, key=key, keyid=keyid,\n profile=profile):\n ret['comment'] = 'Route53 Hosted Zone {0} deleted'.format(domain_name)\n log.info(ret['comment'])\n ret['changes']['old'] = deets\n ret['changes']['new'] = None\n\n return ret", "response": "Ensure that the Route53 Hosted Zone described by name is absent."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_self_ip():\n '''\n Find out localhost outside IP.\n\n :return:\n '''\n sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n try:\n sck.connect(('1.255.255.255', 1)) # Does not needs to be reachable\n ip_addr = sck.getsockname()[0]\n except Exception:\n ip_addr = socket.gethostbyname(socket.gethostname())\n finally:\n sck.close()\n return ip_addr", "response": "Find out localhost outside IP."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsend data to the destination addr.", "response": "def _sendto(self, data, addr=None, attempts=10):\n '''\n On multi-master environments, running on the same machine,\n transport sending to the destination can be allowed only at once.\n Since every machine will immediately respond, high chance to\n get sending fired at the same time, which will result to a PermissionError\n at socket level. We are attempting to send it in a different time.\n\n :param data:\n :param addr:\n :return:\n '''\n tries = 0\n slp_time = lambda: 0.5 / random.randint(10, 30)\n slp = slp_time()\n while tries < attempts:\n try:\n self.transport.sendto(data, addr=addr)\n self.log.debug('Sent successfully')\n return\n except AttributeError as ex:\n self.log.debug('Permission error: %s', ex)\n time.sleep(slp)\n tries += 1\n slp += slp_time()"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates datagram connection. Based on code from Python 3.5 version, this method is used only in Python 2.7+ versions, since Trollius library did not ported UDP packets broadcast.", "response": "def create_datagram_endpoint(loop, protocol_factory, local_addr=None, remote_addr=None, family=0, proto=0, flags=0):\n '''\n Create datagram connection.\n\n Based on code from Python 3.5 version, this method is used\n only in Python 2.7+ versions, since Trollius library did not\n ported UDP packets broadcast.\n '''\n if not (local_addr or remote_addr):\n if not family:\n raise ValueError('unexpected address family')\n addr_pairs_info = (((family, proto), (None, None)),)\n else:\n addr_infos = OrderedDict()\n for idx, addr in ((0, local_addr), (1, remote_addr)):\n if addr is not None:\n assert isinstance(addr, tuple) and len(addr) == 2, '2-tuple is expected'\n infos = yield asyncio.coroutines.From(loop.getaddrinfo(\n *addr, family=family, type=socket.SOCK_DGRAM, proto=proto, flags=flags))\n if not infos:\n raise socket.error('getaddrinfo() returned empty list')\n for fam, _, pro, _, address in infos:\n key = (fam, pro)\n if key not in addr_infos:\n addr_infos[key] = [None, None]\n addr_infos[key][idx] = address\n addr_pairs_info = [\n (key, addr_pair) for key, addr_pair in addr_infos.items()\n if not ((local_addr and addr_pair[0] is None) or\n (remote_addr and addr_pair[1] is None))]\n if not addr_pairs_info:\n raise ValueError('can not get address information')\n exceptions = []\n for ((family, proto),\n (local_address, remote_address)) in addr_pairs_info:\n sock = r_addr = None\n try:\n sock = socket.socket(family=family, type=socket.SOCK_DGRAM, proto=proto)\n for opt in [socket.SO_REUSEADDR, socket.SO_BROADCAST]:\n sock.setsockopt(socket.SOL_SOCKET, opt, 1)\n sock.setblocking(False)\n if local_addr:\n sock.bind(local_address)\n if remote_addr:\n yield asyncio.coroutines.From(loop.sock_connect(sock, remote_address))\n r_addr = remote_address\n except socket.error as exc:\n if sock is not None:\n sock.close()\n exceptions.append(exc)\n except Exception:\n if sock is not None:\n sock.close()\n raise\n else:\n break\n else:\n raise exceptions[0]\n\n protocol = protocol_factory()\n waiter = asyncio.futures.Future(loop=loop)\n transport = loop._make_datagram_transport(sock, protocol, r_addr, waiter)\n try:\n yield asyncio.coroutines.From(waiter)\n except Exception:\n transport.close()\n raise\n raise asyncio.coroutines.Return(transport, protocol)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _query(self):\n '''\n Query the broadcast for defined services.\n :return:\n '''\n query = salt.utils.stringutils.to_bytes(\n \"{}{}\".format(self.signature, time.time()))\n self._socket.sendto(query, ('', self.port))\n\n return query", "response": "Query the broadcast for defined services."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncollects masters map from the network.", "response": "def _collect_masters_map(self, response):\n '''\n Collect masters map from the network.\n :return:\n '''\n while True:\n try:\n data, addr = self._socket.recvfrom(0x400)\n if data:\n if addr not in response:\n response[addr] = []\n response[addr].append(data)\n else:\n break\n except Exception as err:\n if not response:\n self.log.error('Discovery master collection failure: %s', err)\n break"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngather the information of currently declared servers.", "response": "def discover(self):\n '''\n Gather the information of currently declared servers.\n\n :return:\n '''\n response = {}\n masters = {}\n self.log.info(\"Looking for a server discovery\")\n self._query()\n self._collect_masters_map(response)\n if not response:\n msg = 'No master has been discovered.'\n self.log.info(msg)\n else:\n for addr, descriptions in response.items():\n for data in descriptions: # Several masters can run at the same machine.\n msg = salt.utils.stringutils.to_unicode(data)\n if msg.startswith(self.signature):\n msg = msg.split(self.signature)[-1]\n self.log.debug(\n \"Service announcement at '%s:%s'. Response: '%s'\",\n addr[0], addr[1], msg\n )\n if ':E:' in msg:\n err = msg.split(':E:')[-1]\n self.log.error(\n 'Error response from the service publisher at %s: %s',\n addr, err\n )\n if \"timestamp\" in err:\n self.log.error('Publisher sent shifted timestamp from %s', addr)\n else:\n if addr not in masters:\n masters[addr] = []\n masters[addr].append(\n salt.utils.json.loads(msg.split(':@:')[-1], _json_module=_json)\n )\n return masters"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef admin_password_present(name, password=None, **kwargs):\n '''\n Initial change of Zabbix Admin password to password taken from one of the sources (only the most prioritized one):\n 1. 'password' parameter\n 2. '_connection_password' parameter\n 3. pillar 'zabbix.password' setting\n\n 1) Tries to log in as Admin with password found in state password parameter or _connection_password\n or pillar or default zabbix password in this precise order, if any of them is present.\n 2) If one of above passwords matches, it tries to change the password to the most prioritized one.\n 3) If not able to connect with any password then it fails.\n\n :param name: Just a name of state\n :param password: Optional - desired password for Admin to be set\n :param _connection_user: Optional - Ignored in this state (always assumed 'Admin')\n :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)\n :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)\n\n .. code-block:: yaml\n\n # password taken from pillar or _connection_password\n zabbix-admin-password:\n zabbix_user.admin_password_present\n\n # directly set password\n zabbix-admin-password:\n zabbix_user.admin_password_present:\n - password: SECRET_PASS\n '''\n dry_run = __opts__['test']\n default_zabbix_user = 'Admin'\n default_zabbix_password = 'zabbix'\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n passwords = []\n connection_args = {}\n connection_args['_connection_user'] = default_zabbix_user\n if '_connection_url' in kwargs:\n connection_args['_connection_url'] = kwargs['_connection_url']\n\n config_password = __salt__['config.option']('zabbix.password', None)\n if config_password:\n passwords.append(config_password)\n if '_connection_password' in kwargs:\n passwords.append(kwargs['_connection_password'])\n if password:\n passwords.append(password)\n\n # get unique list in preserved order and reverse it\n seen = set()\n unique_passwords = [six.text_type(x) for x in passwords if x not in seen and not seen.add(x)]\n unique_passwords.reverse()\n\n if not unique_passwords:\n ret['comment'] = 'Could not find any Zabbix Admin password setting! See documentation.'\n return ret\n else:\n desired_password = unique_passwords[0]\n\n unique_passwords.append(default_zabbix_password)\n\n for pwd in unique_passwords:\n connection_args['_connection_password'] = pwd\n try:\n user_get = __salt__['zabbix.user_get'](default_zabbix_user, **connection_args)\n except SaltException as err:\n if 'Login name or password is incorrect' in six.text_type(err):\n user_get = False\n else:\n raise\n if user_get:\n if pwd == desired_password:\n ret['result'] = True\n ret['comment'] = 'Admin password is correct.'\n return ret\n else:\n break\n\n if user_get:\n if not dry_run:\n user_update = __salt__['zabbix.user_update'](user_get[0]['userid'],\n passwd=desired_password,\n **connection_args)\n if user_update:\n ret['result'] = True\n ret['changes']['passwd'] = 'changed to \\'' + six.text_type(desired_password) + '\\''\n else:\n ret['result'] = None\n ret['comment'] = 'Password for user ' + six.text_type(default_zabbix_user) \\\n + ' updated to \\'' + six.text_type(desired_password) + '\\''\n\n return ret", "response": "Ensures that the admin password is present in the Zabbix admin."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef present(alias, passwd, usrgrps, medias=None, password_reset=False, **kwargs):\n '''\n Ensures that the user exists, eventually creates new user.\n NOTE: use argument firstname instead of name to not mess values with name from salt sls.\n\n .. versionadded:: 2016.3.0\n\n :param alias: user alias\n :param passwd: user's password\n :param usrgrps: user groups to add the user to\n :param medias: Optional - user's medias to create\n :param password_reset: whether or not to reset password at update\n :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)\n :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)\n :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)\n :param firstname: string with firstname of the user, use 'firstname' instead of 'name' parameter to not mess \\\n with value supplied from Salt sls file.\n\n .. code-block:: yaml\n\n make_user:\n zabbix_user.present:\n - alias: George\n - passwd: donottellanyonE@456x\n - password_reset: True\n - usrgrps:\n - 13\n - 7\n - medias:\n - me@example.com:\n - mediatype: mail\n - period: '1-7,00:00-24:00'\n - severity: NIWAHD\n - make_jabber:\n - active: true\n - mediatype: jabber\n - period: '1-5,08:00-19:00'\n - sendto: jabbera@example.com\n - text_me_morning_disabled:\n - active: false\n - mediatype: sms\n - period: '1-5,09:30-10:00'\n - severity: D\n - sendto: '+42032132588568'\n\n '''\n if medias is None:\n medias = []\n connection_args = {}\n if '_connection_user' in kwargs:\n connection_args['_connection_user'] = kwargs['_connection_user']\n if '_connection_password' in kwargs:\n connection_args['_connection_password'] = kwargs['_connection_password']\n if '_connection_url' in kwargs:\n connection_args['_connection_url'] = kwargs['_connection_url']\n\n ret = {'name': alias, 'changes': {}, 'result': False, 'comment': ''}\n\n # Comment and change messages\n comment_user_created = 'User {0} created.'.format(alias)\n comment_user_updated = 'User {0} updated.'.format(alias)\n comment_user_notcreated = 'Unable to create user: {0}. '.format(alias)\n comment_user_exists = 'User {0} already exists.'.format(alias)\n changes_user_created = {alias: {'old': 'User {0} does not exist.'.format(alias),\n 'new': 'User {0} created.'.format(alias),\n }\n }\n\n def _media_format(medias_data):\n '''\n Formats medias from SLS file into valid JSON usable for zabbix API.\n Completes JSON with default values.\n\n :param medias_data: list of media data from SLS file\n\n '''\n if not medias_data:\n return list()\n medias_json = loads(dumps(medias_data))\n medias_attr = ('active', 'mediatype', 'period', 'severity', 'sendto')\n media_type = {'mail': 1, 'jabber': 2, 'sms': 3}\n media_severities = ('D', 'H', 'A', 'W', 'I', 'N')\n\n medias_dict = dict()\n for media in medias_json:\n for med in media:\n medias_dict[med] = dict()\n for medattr in media[med]:\n for key, value in medattr.items():\n if key in medias_attr:\n medias_dict[med][key] = value\n\n medias_list = list()\n for key, value in medias_dict.items():\n # Load media values or default values\n active = '0' if six.text_type(value.get('active', 'true')).lower() == 'true' else '1'\n mediatype_sls = six.text_type(value.get('mediatype', 'mail')).lower()\n mediatypeid = six.text_type(media_type.get(mediatype_sls, 1))\n period = value.get('period', '1-7,00:00-24:00')\n sendto = value.get('sendto', key)\n\n severity_sls = value.get('severity', 'HD')\n severity_bin = six.text_type()\n for sev in media_severities:\n if sev in severity_sls:\n severity_bin += '1'\n else:\n severity_bin += '0'\n severity = six.text_type(int(severity_bin, 2))\n\n medias_list.append({'active': active,\n 'mediatypeid': mediatypeid,\n 'period': period,\n 'sendto': sendto,\n 'severity': severity})\n return medias_list\n\n user_exists = __salt__['zabbix.user_exists'](alias, **connection_args)\n\n if user_exists:\n user = __salt__['zabbix.user_get'](alias, **connection_args)[0]\n userid = user['userid']\n\n update_usrgrps = False\n update_medias = False\n\n usergroups = __salt__['zabbix.usergroup_get'](userids=userid, **connection_args)\n cur_usrgrps = list()\n\n for usergroup in usergroups:\n cur_usrgrps.append(int(usergroup['usrgrpid']))\n\n if set(cur_usrgrps) != set(usrgrps):\n update_usrgrps = True\n\n user_medias = __salt__['zabbix.user_getmedia'](userid, **connection_args)\n medias_formated = _media_format(medias)\n\n if user_medias:\n user_medias_copy = deepcopy(user_medias)\n for user_med in user_medias_copy:\n user_med.pop('userid')\n user_med.pop('mediaid')\n media_diff = [x for x in medias_formated if x not in user_medias_copy] + \\\n [y for y in user_medias_copy if y not in medias_formated]\n if media_diff:\n update_medias = True\n elif not user_medias and medias:\n update_medias = True\n\n # Dry run, test=true mode\n if __opts__['test']:\n if user_exists:\n if update_usrgrps or password_reset or update_medias:\n ret['result'] = None\n ret['comment'] = comment_user_updated\n else:\n ret['result'] = True\n ret['comment'] = comment_user_exists\n else:\n ret['result'] = None\n ret['comment'] = comment_user_created\n\n error = []\n\n if user_exists:\n ret['result'] = True\n if update_usrgrps or password_reset or update_medias:\n ret['comment'] = comment_user_updated\n\n if update_usrgrps:\n __salt__['zabbix.user_update'](userid, usrgrps=usrgrps, **connection_args)\n updated_groups = __salt__['zabbix.usergroup_get'](userids=userid, **connection_args)\n\n cur_usrgrps = list()\n for usergroup in updated_groups:\n cur_usrgrps.append(int(usergroup['usrgrpid']))\n\n usrgrp_diff = list(set(usrgrps) - set(cur_usrgrps))\n\n if usrgrp_diff:\n error.append('Unable to update grpup(s): {0}'.format(usrgrp_diff))\n\n ret['changes']['usrgrps'] = six.text_type(updated_groups)\n\n if password_reset:\n updated_password = __salt__['zabbix.user_update'](userid, passwd=passwd, **connection_args)\n if 'error' in updated_password:\n error.append(updated_groups['error'])\n else:\n ret['changes']['passwd'] = 'updated'\n\n if update_medias:\n for user_med in user_medias:\n deletedmed = __salt__['zabbix.user_deletemedia'](user_med['mediaid'], **connection_args)\n if 'error' in deletedmed:\n error.append(deletedmed['error'])\n\n for media in medias_formated:\n updatemed = __salt__['zabbix.user_addmedia'](userids=userid,\n active=media['active'],\n mediatypeid=media['mediatypeid'],\n period=media['period'],\n sendto=media['sendto'],\n severity=media['severity'],\n **connection_args)\n\n if 'error' in updatemed:\n error.append(updatemed['error'])\n\n ret['changes']['medias'] = six.text_type(medias_formated)\n\n else:\n ret['comment'] = comment_user_exists\n else:\n user_create = __salt__['zabbix.user_create'](alias, passwd, usrgrps, **kwargs)\n\n if 'error' not in user_create:\n ret['result'] = True\n ret['comment'] = comment_user_created\n ret['changes'] = changes_user_created\n else:\n ret['result'] = False\n ret['comment'] = comment_user_notcreated + six.text_type(user_create['error'])\n\n # error detected\n if error:\n ret['changes'] = {}\n ret['result'] = False\n ret['comment'] = six.text_type(error)\n\n return ret", "response": "Ensures that the user exists eventually creates new user."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nensure that the user with the given name is absent.", "response": "def absent(name, **kwargs):\n '''\n Ensures that the user does not exist, eventually delete user.\n\n .. versionadded:: 2016.3.0\n\n :param name: user alias\n :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)\n :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)\n :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)\n\n .. code-block:: yaml\n\n George:\n zabbix_user.absent\n\n '''\n connection_args = {}\n if '_connection_user' in kwargs:\n connection_args['_connection_user'] = kwargs['_connection_user']\n if '_connection_password' in kwargs:\n connection_args['_connection_password'] = kwargs['_connection_password']\n if '_connection_url' in kwargs:\n connection_args['_connection_url'] = kwargs['_connection_url']\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n # Comment and change messages\n comment_user_deleted = 'USer {0} deleted.'.format(name)\n comment_user_notdeleted = 'Unable to delete user: {0}. '.format(name)\n comment_user_notexists = 'User {0} does not exist.'.format(name)\n changes_user_deleted = {name: {'old': 'User {0} exists.'.format(name),\n 'new': 'User {0} deleted.'.format(name),\n }\n }\n\n user_get = __salt__['zabbix.user_get'](name, **connection_args)\n\n # Dry run, test=true mode\n if __opts__['test']:\n if not user_get:\n ret['result'] = True\n ret['comment'] = comment_user_notexists\n else:\n ret['result'] = None\n ret['comment'] = comment_user_deleted\n ret['changes'] = changes_user_deleted\n\n if not user_get:\n ret['result'] = True\n ret['comment'] = comment_user_notexists\n else:\n try:\n userid = user_get[0]['userid']\n user_delete = __salt__['zabbix.user_delete'](userid, **connection_args)\n except KeyError:\n user_delete = False\n\n if user_delete and 'error' not in user_delete:\n ret['result'] = True\n ret['comment'] = comment_user_deleted\n ret['changes'] = changes_user_deleted\n else:\n ret['result'] = False\n ret['comment'] = comment_user_notdeleted + six.text_type(user_delete['error'])\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef query(function,\n token=None,\n api_version='1',\n method='POST',\n header_dict=None,\n data=None,\n query_params=None,\n opts=None):\n '''\n PushOver object method function to construct and execute on the API URL.\n\n :param token: The PushOver api key.\n :param api_version: The PushOver API version to use, defaults to version 1.\n :param function: The PushOver api function to perform.\n :param method: The HTTP method, e.g. GET or POST.\n :param data: The data to be sent for POST method.\n :return: The json response from the API call or False.\n '''\n\n ret = {'message': '',\n 'res': True}\n\n pushover_functions = {\n 'message': {\n 'request': 'messages.json',\n 'response': 'status',\n },\n 'validate_user': {\n 'request': 'users/validate.json',\n 'response': 'status',\n },\n 'validate_sound': {\n 'request': 'sounds.json',\n 'response': 'status',\n },\n }\n\n api_url = 'https://api.pushover.net'\n base_url = _urljoin(api_url, api_version + '/')\n path = pushover_functions.get(function).get('request')\n url = _urljoin(base_url, path, False)\n\n if not query_params:\n query_params = {}\n\n decode = True\n if method == 'DELETE':\n decode = False\n\n result = salt.utils.http.query(\n url,\n method,\n params=query_params,\n data=data,\n header_dict=header_dict,\n decode=decode,\n decode_type='json',\n text=True,\n status=True,\n cookies=True,\n persist_session=True,\n opts=opts,\n )\n\n if result.get('status', None) == salt.ext.six.moves.http_client.OK:\n response = pushover_functions.get(function).get('response')\n if response in result and result[response] == 0:\n ret['res'] = False\n ret['message'] = result\n return ret\n else:\n try:\n if 'response' in result and result[response] == 0:\n ret['res'] = False\n ret['message'] = result\n except ValueError:\n ret['res'] = False\n ret['message'] = result\n return ret", "response": "Query the PushOver API for a single object."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsends a message to a Pushover user or group. :param sound: The sound that we want to verify :param token: The PushOver token.", "response": "def validate_sound(sound,\n token):\n '''\n Send a message to a Pushover user or group.\n :param sound: The sound that we want to verify\n :param token: The PushOver token.\n '''\n ret = {\n 'message': 'Sound is invalid',\n 'res': False\n }\n parameters = dict()\n parameters['token'] = token\n\n response = query(function='validate_sound',\n method='GET',\n query_params=parameters)\n\n if response['res']:\n if 'message' in response:\n _message = response.get('message', '')\n if 'status' in _message:\n if _message.get('dict', {}).get('status', '') == 1:\n sounds = _message.get('dict', {}).get('sounds', '')\n if sound in sounds:\n ret['message'] = 'Valid sound {0}.'.format(sound)\n ret['res'] = True\n else:\n ret['message'] = 'Warning: {0} not a valid sound.'.format(sound)\n ret['res'] = False\n else:\n ret['message'] = ''.join(_message.get('dict', {}).get('errors'))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nvalidates a PushOver user or group.", "response": "def validate_user(user,\n device,\n token):\n '''\n Send a message to a Pushover user or group.\n :param user: The user or group name, either will work.\n :param device: The device for the user.\n :param token: The PushOver token.\n '''\n res = {\n 'message': 'User key is invalid',\n 'result': False\n }\n\n parameters = dict()\n parameters['user'] = user\n parameters['token'] = token\n if device:\n parameters['device'] = device\n\n response = query(function='validate_user',\n method='POST',\n header_dict={'Content-Type': 'application/x-www-form-urlencoded'},\n data=_urlencode(parameters))\n\n if response['res']:\n if 'message' in response:\n _message = response.get('message', '')\n if 'status' in _message:\n if _message.get('dict', {}).get('status', None) == 1:\n res['result'] = True\n res['message'] = 'User key is valid.'\n else:\n res['result'] = False\n res['message'] = ''.join(_message.get('dict', {}).get('errors'))\n return res"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef is_alive(pidfile):\n '''\n Check if PID is still alive.\n '''\n try:\n with salt.utils.files.fopen(pidfile) as fp_:\n os.kill(int(fp_.read().strip()), 0)\n return True\n except Exception as ex:\n if os.access(pidfile, os.W_OK) and os.path.isfile(pidfile):\n os.unlink(pidfile)\n return False", "response": "Check if the PID is still alive."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncalling an external system command.", "response": "def _syscall(self, command, input=None, env=None, *params):\n '''\n Call an external system command.\n '''\n return Popen([command] + list(params), stdout=PIPE, stdin=PIPE, stderr=STDOUT,\n env=env or os.environ).communicate(input=input)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a dict of cfg packages based on OS family.", "response": "def _get_cfg_pkgs(self):\n '''\n Package scanner switcher between the platforms.\n\n :return:\n '''\n if self.grains_core.os_data().get('os_family') == 'Debian':\n return self.__get_cfg_pkgs_dpkg()\n elif self.grains_core.os_data().get('os_family') in ['Suse', 'redhat']:\n return self.__get_cfg_pkgs_rpm()\n else:\n return dict()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting packages with configuration files on Dpkg systems.", "response": "def __get_cfg_pkgs_dpkg(self):\n '''\n Get packages with configuration files on Dpkg systems.\n :return:\n '''\n # Get list of all available packages\n data = dict()\n\n for pkg_name in salt.utils.stringutils.to_str(self._syscall('dpkg-query', None, None,\n '-Wf', \"${binary:Package}\\\\n\")[0]).split(os.linesep):\n pkg_name = pkg_name.strip()\n if not pkg_name:\n continue\n data[pkg_name] = list()\n for pkg_cfg_item in salt.utils.stringutils.to_str(self._syscall('dpkg-query', None, None, '-Wf', \"${Conffiles}\\\\n\",\n pkg_name)[0]).split(os.linesep):\n pkg_cfg_item = pkg_cfg_item.strip()\n if not pkg_cfg_item:\n continue\n pkg_cfg_file, pkg_cfg_sum = pkg_cfg_item.strip().split(\" \", 1)\n data[pkg_name].append(pkg_cfg_file)\n\n # Dpkg meta data is unreliable. Check every package\n # and remove which actually does not have config files.\n if not data[pkg_name]:\n data.pop(pkg_name)\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget packages with configuration files on RPM systems.", "response": "def __get_cfg_pkgs_rpm(self):\n '''\n Get packages with configuration files on RPM systems.\n '''\n out, err = self._syscall('rpm', None, None, '-qa', '--configfiles',\n '--queryformat', '%{name}-%{version}-%{release}\\\\n')\n data = dict()\n pkg_name = None\n pkg_configs = []\n\n out = salt.utils.stringutils.to_str(out)\n for line in out.split(os.linesep):\n line = line.strip()\n if not line:\n continue\n if not line.startswith(\"/\"):\n if pkg_name and pkg_configs:\n data[pkg_name] = pkg_configs\n pkg_name = line\n pkg_configs = []\n else:\n pkg_configs.append(line)\n\n if pkg_name and pkg_configs:\n data[pkg_name] = pkg_configs\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a dict of all cfg packages that have changed since the last time.", "response": "def _get_changed_cfg_pkgs(self, data):\n '''\n Filter out unchanged packages on the Debian or RPM systems.\n\n :param data: Structure {package-name -> [ file .. file1 ]}\n :return: Same structure as data, except only files that were changed.\n '''\n f_data = dict()\n for pkg_name, pkg_files in data.items():\n cfgs = list()\n cfg_data = list()\n if self.grains_core.os_data().get('os_family') == 'Debian':\n cfg_data = salt.utils.stringutils.to_str(self._syscall(\"dpkg\", None, None, '--verify',\n pkg_name)[0]).split(os.linesep)\n elif self.grains_core.os_data().get('os_family') in ['Suse', 'redhat']:\n cfg_data = salt.utils.stringutils.to_str(self._syscall(\"rpm\", None, None, '-V', '--nodeps', '--nodigest',\n '--nosignature', '--nomtime', '--nolinkto',\n pkg_name)[0]).split(os.linesep)\n for line in cfg_data:\n line = line.strip()\n if not line or line.find(\" c \") < 0 or line.split(\" \")[0].find(\"5\") < 0:\n continue\n cfg_file = line.split(\" \")[-1]\n if cfg_file in pkg_files:\n cfgs.append(cfg_file)\n if cfgs:\n f_data[pkg_name] = cfgs\n\n return f_data"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _save_cfg_packages(self, data):\n '''\n Save configuration packages. (NG)\n\n :param data:\n :return:\n '''\n pkg_id = 0\n pkg_cfg_id = 0\n for pkg_name, pkg_configs in data.items():\n pkg = Package()\n pkg.id = pkg_id\n pkg.name = pkg_name\n self.db.store(pkg)\n\n for pkg_config in pkg_configs:\n cfg = PackageCfgFile()\n cfg.id = pkg_cfg_id\n cfg.pkgid = pkg_id\n cfg.path = pkg_config\n self.db.store(cfg)\n pkg_cfg_id += 1\n\n pkg_id += 1", "response": "Save configuration packages. ( NG"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _save_payload(self, files, directories, links):\n '''\n Save payload (unmanaged files)\n\n :param files:\n :param directories:\n :param links:\n :return:\n '''\n\n idx = 0\n for p_type, p_list in (('f', files), ('d', directories), ('l', links,),):\n for p_obj in p_list:\n stats = os.stat(p_obj)\n\n payload = PayloadFile()\n payload.id = idx\n payload.path = p_obj\n payload.p_type = p_type\n payload.mode = stats.st_mode\n payload.uid = stats.st_uid\n payload.gid = stats.st_gid\n payload.p_size = stats.st_size\n payload.atime = stats.st_atime\n payload.mtime = stats.st_mtime\n payload.ctime = stats.st_ctime\n\n idx += 1\n self.db.store(payload)", "response": "Save the unmanaged files directories and links to the database."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nbuilding a in - memory data of all managed files.", "response": "def _get_managed_files(self):\n '''\n Build a in-memory data of all managed files.\n '''\n if self.grains_core.os_data().get('os_family') == 'Debian':\n return self.__get_managed_files_dpkg()\n elif self.grains_core.os_data().get('os_family') in ['Suse', 'redhat']:\n return self.__get_managed_files_rpm()\n\n return list(), list(), list()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef __get_managed_files_dpkg(self):\n '''\n Get a list of all system files, belonging to the Debian package manager.\n '''\n dirs = set()\n links = set()\n files = set()\n\n for pkg_name in salt.utils.stringutils.to_str(self._syscall(\"dpkg-query\", None, None,\n '-Wf', '${binary:Package}\\\\n')[0]).split(os.linesep):\n pkg_name = pkg_name.strip()\n if not pkg_name:\n continue\n for resource in salt.utils.stringutils.to_str(self._syscall(\"dpkg\", None, None, '-L', pkg_name)[0]).split(os.linesep):\n resource = resource.strip()\n if not resource or resource in ['/', './', '.']:\n continue\n if os.path.isdir(resource):\n dirs.add(resource)\n elif os.path.islink(resource):\n links.add(resource)\n elif os.path.isfile(resource):\n files.add(resource)\n\n return sorted(files), sorted(dirs), sorted(links)", "response": "Get a list of all system files belonging to the Debian package manager."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget a list of all system files belonging to the RedHat package manager.", "response": "def __get_managed_files_rpm(self):\n '''\n Get a list of all system files, belonging to the RedHat package manager.\n '''\n dirs = set()\n links = set()\n files = set()\n\n for line in salt.utils.stringutils.to_str(self._syscall(\"rpm\", None, None, '-qlav')[0]).split(os.linesep):\n line = line.strip()\n if not line:\n continue\n line = line.replace(\"\\t\", \" \").split(\" \")\n if line[0][0] == \"d\":\n dirs.add(line[-1])\n elif line[0][0] == \"l\":\n links.add(line[-1])\n elif line[0][0] == \"-\":\n files.add(line[-1])\n\n return sorted(files), sorted(dirs), sorted(links)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nwalks implementation. Version in python 2. x and 3. x works differently.", "response": "def _get_all_files(self, path, *exclude):\n '''\n Walk implementation. Version in python 2.x and 3.x works differently.\n '''\n files = list()\n dirs = list()\n links = list()\n\n if os.access(path, os.R_OK):\n for obj in os.listdir(path):\n obj = os.path.join(path, obj)\n valid = True\n for ex_obj in exclude:\n if obj.startswith(str(ex_obj)):\n valid = False\n continue\n if not valid or not os.path.exists(obj) or not os.access(obj, os.R_OK):\n continue\n if salt.utils.path.islink(obj):\n links.append(obj)\n elif os.path.isdir(obj):\n dirs.append(obj)\n f_obj, d_obj, l_obj = self._get_all_files(obj, *exclude)\n files.extend(f_obj)\n dirs.extend(d_obj)\n links.extend(l_obj)\n elif os.path.isfile(obj):\n files.append(obj)\n\n return sorted(files), sorted(dirs), sorted(links)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_unmanaged_files(self, managed, system_all):\n '''\n Get the intersection between all files and managed files.\n '''\n m_files, m_dirs, m_links = managed\n s_files, s_dirs, s_links = system_all\n\n return (sorted(list(set(s_files).difference(m_files))),\n sorted(list(set(s_dirs).difference(m_dirs))),\n sorted(list(set(s_links).difference(m_links))))", "response": "Get the intersection between all files and managed files."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\npreparing full system scan by setting up the database etc.", "response": "def _prepare_full_scan(self, **kwargs):\n '''\n Prepare full system scan by setting up the database etc.\n '''\n self.db.open(new=True)\n # Add ignored filesystems\n ignored_fs = set()\n ignored_fs |= set(self.IGNORE_PATHS)\n mounts = salt.utils.fsutils._get_mounts()\n for device, data in mounts.items():\n if device in self.IGNORE_MOUNTS:\n for mpt in data:\n ignored_fs.add(mpt['mount_point'])\n continue\n for mpt in data:\n if mpt['type'] in self.IGNORE_FS_TYPES:\n ignored_fs.add(mpt['mount_point'])\n\n # Remove leafs of ignored filesystems\n ignored_all = list()\n for entry in sorted(list(ignored_fs)):\n valid = True\n for e_entry in ignored_all:\n if entry.startswith(e_entry):\n valid = False\n break\n if valid:\n ignored_all.append(entry)\n # Save to the database for further scan\n for ignored_dir in ignored_all:\n dir_obj = IgnoredDir()\n dir_obj.path = ignored_dir\n self.db.store(dir_obj)\n\n # Add allowed filesystems (overrides all above at full scan)\n allowed = [elm for elm in kwargs.get(\"filter\", \"\").split(\",\") if elm]\n for allowed_dir in allowed:\n dir_obj = AllowedDir()\n dir_obj.path = allowed_dir\n self.db.store(dir_obj)\n\n return ignored_all"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _init_env(self):\n '''\n Initialize some Salt environment.\n '''\n from salt.config import minion_config\n from salt.grains import core as g_core\n g_core.__opts__ = minion_config(self.DEFAULT_MINION_CONFIG_PATH)\n self.grains_core = g_core", "response": "Initialize some Salt environment."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef snapshot(self, mode):\n '''\n Take a snapshot of the system.\n '''\n self._init_env()\n\n self._save_cfg_packages(self._get_changed_cfg_pkgs(self._get_cfg_pkgs()))\n self._save_payload(*self._scan_payload())", "response": "Take a snapshot of the system."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nrequest a snapshot of the system.", "response": "def request_snapshot(self, mode, priority=19, **kwargs):\n '''\n Take a snapshot of the system.\n '''\n if mode not in self.MODE:\n raise InspectorSnapshotException(\"Unknown mode: '{0}'\".format(mode))\n\n if is_alive(self.pidfile):\n raise CommandExecutionError('Inspection already in progress.')\n\n self._prepare_full_scan(**kwargs)\n\n os.system(\"nice -{0} python {1} {2} {3} {4} & > /dev/null\".format(\n priority, __file__, os.path.dirname(self.pidfile), os.path.dirname(self.dbfile), mode))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nexports the Kiwi data for the given description.", "response": "def export(self, description, local=False, path='/tmp', format='qcow2'):\n '''\n Export description for Kiwi.\n\n :param local:\n :param path:\n :return:\n '''\n kiwiproc.__salt__ = __salt__\n return kiwiproc.KiwiExporter(grains=__grains__,\n format=format).load(**description).export('something')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nbuilding an image using Kiwi.", "response": "def build(self, format='qcow2', path='/tmp'):\n '''\n Build an image using Kiwi.\n\n :param format:\n :param path:\n :return:\n '''\n if kiwi is None:\n msg = 'Unable to build the image due to the missing dependencies: Kiwi module is not available.'\n log.error(msg)\n raise CommandExecutionError(msg)\n\n raise CommandExecutionError(\"Build is not yet implemented\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring that the given value is present in the given XML file.", "response": "def value_present(name, xpath, value, **kwargs):\n '''\n .. versionadded:: Neon\n\n Manages a given XML file\n\n name : string\n The location of the XML file to manage, as an absolute path.\n\n xpath : string\n xpath location to manage\n\n value : string\n value to ensure present\n\n .. code-block:: yaml\n\n ensure_value_true:\n xml.value_present:\n - name: /tmp/test.xml\n - xpath: .//playwright[@id='1']\n - value: William Shakespeare\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n if 'test' not in kwargs:\n kwargs['test'] = __opts__.get('test', False)\n\n current_value = __salt__['xml.get_value'](name, xpath)\n if not current_value:\n ret['result'] = False\n ret['comment'] = 'xpath query {0} not found in {1}'.format(xpath, name)\n return ret\n\n if current_value != value:\n if kwargs['test']:\n ret['result'] = None\n ret['comment'] = '{0} will be updated'.format(name)\n ret['changes'] = {name: {'old': current_value, 'new': value}}\n else:\n results = __salt__['xml.set_value'](name, xpath, value)\n ret['result'] = results\n ret['comment'] = '{0} updated'.format(name)\n ret['changes'] = {name: {'old': current_value, 'new': value}}\n else:\n ret['comment'] = '{0} is already present'.format(value)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring that the thing type with the given name is present.", "response": "def thing_type_present(name, thingTypeName, thingTypeDescription,\n searchableAttributesList,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure thing type exists.\n\n .. versionadded:: 2016.11.0\n\n name\n The name of the state definition\n\n thingTypeName\n Name of the thing type\n\n thingTypeDescription\n Description of the thing type\n\n searchableAttributesList\n List of string attributes that are searchable for\n the thing type\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used\n\n profile\n A dict with region, key, keyid, or a pillar key (string) that\n contains a dict with region, key, and keyid\n\n '''\n ret = {\n 'name': thingTypeName,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n r = __salt__['boto_iot.thing_type_exists'](\n thingTypeName=thingTypeName,\n region=region, key=key, keyid=keyid, profile=profile\n )\n\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to create thing type: {0}.'.format(r['error']['message'])\n return ret\n\n if r.get('exists'):\n ret['result'] = True\n ret['comment'] = 'Thing type with given name {0} already exists'.format(thingTypeName)\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'Thing type {0} is set to be created.'.format(thingTypeName)\n ret['result'] = None\n return ret\n\n r = __salt__['boto_iot.create_thing_type'](\n thingTypeName=thingTypeName,\n thingTypeDescription=thingTypeDescription,\n searchableAttributesList=searchableAttributesList,\n region=region, key=key, keyid=keyid, profile=profile\n )\n\n if not r.get('created'):\n ret['result'] = False\n ret['comment'] = 'Failed to create thing type: {0}.'.format(r['error']['message'])\n return ret\n\n _describe = __salt__['boto_iot.describe_thing_type'](\n thingTypeName=thingTypeName,\n region=region, key=key, keyid=keyid, profile=profile\n )\n ret['changes']['old'] = {'thing_type': None}\n ret['changes']['new'] = _describe\n ret['comment'] = 'Thing Type {0} created.'.format(thingTypeName)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef thing_type_absent(name, thingTypeName,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure thing type with passed properties is absent.\n\n .. versionadded:: 2016.11.0\n\n name\n The name of the state definition.\n\n thingTypeName\n Name of the thing type.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n\n '''\n\n ret = {'name': thingTypeName,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n _describe = __salt__['boto_iot.describe_thing_type'](\n thingTypeName=thingTypeName,\n region=region, key=key, keyid=keyid, profile=profile\n )\n if 'error' in _describe:\n ret['result'] = False\n ret['comment'] = 'Failed to delete thing type: {0}.'.format(_describe['error']['message'])\n return ret\n\n if _describe and not _describe['thing_type']:\n ret['comment'] = 'Thing Type {0} does not exist.'.format(thingTypeName)\n return ret\n\n _existing_thing_type = _describe['thing_type']\n _thing_type_metadata = _existing_thing_type.get('thingTypeMetadata')\n _deprecated = _thing_type_metadata.get('deprecated', False)\n\n if __opts__['test']:\n if _deprecated:\n _change_desc = 'removed'\n else:\n _change_desc = 'deprecated and removed'\n ret['comment'] = 'Thing Type {0} is set to be {1}.'.format(thingTypeName, _change_desc)\n ret['result'] = None\n return ret\n\n # initialize a delete_wait_timer to be 5 minutes\n # AWS does not allow delete thing type until 5 minutes\n # after a thing type is marked deprecated.\n _delete_wait_timer = 300\n\n if _deprecated is False:\n _deprecate = __salt__['boto_iot.deprecate_thing_type'](\n thingTypeName=thingTypeName,\n undoDeprecate=False,\n region=region, key=key, keyid=keyid, profile=profile\n )\n if 'error' in _deprecate:\n ret['result'] = False\n ret['comment'] = 'Failed to deprecate thing type: {0}.'.format(_deprecate['error']['message'])\n return ret\n else:\n # grab the deprecation date string from _thing_type_metadata\n _deprecation_date_str = _thing_type_metadata.get('deprecationDate')\n if _deprecation_date_str:\n # see if we can wait less than 5 minutes\n _tz_index = _deprecation_date_str.find('+')\n if _tz_index != -1:\n _deprecation_date_str = _deprecation_date_str[:_tz_index]\n\n _deprecation_date = datetime.datetime.strptime(\n _deprecation_date_str,\n \"%Y-%m-%d %H:%M:%S.%f\"\n )\n\n _elapsed_time_delta = datetime.datetime.utcnow() - _deprecation_date\n if _elapsed_time_delta.seconds >= 300:\n _delete_wait_timer = 0\n else:\n _delete_wait_timer = 300 - _elapsed_time_delta.seconds\n\n # wait required 5 minutes since deprecation time\n if _delete_wait_timer:\n log.warning(\n 'wait for %s seconds per AWS (5 minutes after deprecation time) '\n 'before we can delete iot thing type', _delete_wait_timer\n )\n time.sleep(_delete_wait_timer)\n\n # delete thing type\n r = __salt__['boto_iot.delete_thing_type'](\n thingTypeName=thingTypeName,\n region=region, key=key, keyid=keyid, profile=profile\n )\n if not r['deleted']:\n ret['result'] = False\n ret['comment'] = 'Failed to delete thing type: {0}.'.format(r['error']['message'])\n return ret\n ret['changes']['old'] = _describe\n ret['changes']['new'] = {'thing_type': None}\n ret['comment'] = 'Thing Type {0} deleted.'.format(thingTypeName)\n return ret", "response": "Ensure thing type with passed properties is absent."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nensure a policy is present in the state definition.", "response": "def policy_present(name, policyName, policyDocument,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure policy exists.\n\n name\n The name of the state definition\n\n policyName\n Name of the policy.\n\n policyDocument\n The JSON document that describes the policy. The length of the\n policyDocument must be a minimum length of 1, with a maximum length of\n 2048, excluding whitespace.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n '''\n ret = {'name': policyName,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n r = __salt__['boto_iot.policy_exists'](policyName=policyName,\n region=region, key=key, keyid=keyid, profile=profile)\n\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to create policy: {0}.'.format(r['error']['message'])\n return ret\n\n if not r.get('exists'):\n if __opts__['test']:\n ret['comment'] = 'Policy {0} is set to be created.'.format(policyName)\n ret['result'] = None\n return ret\n r = __salt__['boto_iot.create_policy'](policyName=policyName,\n policyDocument=policyDocument,\n region=region, key=key,\n keyid=keyid, profile=profile)\n if not r.get('created'):\n ret['result'] = False\n ret['comment'] = 'Failed to create policy: {0}.'.format(r['error']['message'])\n return ret\n _describe = __salt__['boto_iot.describe_policy'](policyName,\n region=region, key=key, keyid=keyid, profile=profile)\n ret['changes']['old'] = {'policy': None}\n ret['changes']['new'] = _describe\n ret['comment'] = 'Policy {0} created.'.format(policyName)\n return ret\n\n ret['comment'] = os.linesep.join([ret['comment'], 'Policy {0} is present.'.format(policyName)])\n ret['changes'] = {}\n # policy exists, ensure config matches\n _describe = __salt__['boto_iot.describe_policy'](policyName=policyName,\n region=region, key=key, keyid=keyid, profile=profile)['policy']\n\n if isinstance(_describe['policyDocument'], six.string_types):\n describeDict = salt.utils.json.loads(_describe['policyDocument'])\n else:\n describeDict = _describe['policyDocument']\n\n if isinstance(policyDocument, six.string_types):\n policyDocument = salt.utils.json.loads(policyDocument)\n\n r = salt.utils.data.compare_dicts(describeDict, policyDocument)\n if bool(r):\n if __opts__['test']:\n msg = 'Policy {0} set to be modified.'.format(policyName)\n ret['comment'] = msg\n ret['result'] = None\n return ret\n\n ret['comment'] = os.linesep.join([ret['comment'], 'Policy to be modified'])\n policyDocument = salt.utils.json.dumps(policyDocument)\n\n r = __salt__['boto_iot.create_policy_version'](policyName=policyName,\n policyDocument=policyDocument,\n setAsDefault=True,\n region=region, key=key,\n keyid=keyid, profile=profile)\n if not r.get('created'):\n ret['result'] = False\n ret['comment'] = 'Failed to update policy: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n\n __salt__['boto_iot.delete_policy_version'](policyName=policyName,\n policyVersionId=_describe['defaultVersionId'],\n region=region, key=key,\n keyid=keyid, profile=profile)\n\n ret['changes'].setdefault('new', {})['policyDocument'] = policyDocument\n ret['changes'].setdefault('old', {})['policyDocument'] = _describe['policyDocument']\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef policy_attached(name, policyName, principal,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure policy is attached to the given principal.\n\n name\n The name of the state definition\n\n policyName\n Name of the policy.\n\n principal\n The principal which can be a certificate ARN or a Cognito ID.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n '''\n ret = {'name': policyName,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n r = __salt__['boto_iot.list_principal_policies'](principal=principal,\n region=region, key=key, keyid=keyid, profile=profile)\n\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to attach policy: {0}.'.format(r['error']['message'])\n return ret\n\n attached = False\n for policy in r.get('policies', []):\n if policy.get('policyName') == policyName:\n attached = True\n break\n if not attached:\n if __opts__['test']:\n ret['comment'] = 'Policy {0} is set to be attached to {1}.'.format(policyName, principal)\n ret['result'] = None\n return ret\n r = __salt__['boto_iot.attach_principal_policy'](policyName=policyName,\n principal=principal,\n region=region, key=key,\n keyid=keyid, profile=profile)\n if not r.get('attached'):\n ret['result'] = False\n ret['comment'] = 'Failed to attach policy: {0}.'.format(r['error']['message'])\n return ret\n ret['changes']['old'] = {'attached': False}\n ret['changes']['new'] = {'attached': True}\n ret['comment'] = 'Policy {0} attached to {1}.'.format(policyName, principal)\n return ret\n\n ret['comment'] = os.linesep.join([ret['comment'], 'Policy {0} is attached.'.format(policyName)])\n ret['changes'] = {}\n\n return ret", "response": "Ensure that the given policy is attached to the given principal."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef topic_rule_present(name, ruleName, sql, actions, description='',\n ruleDisabled=False,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure topic rule exists.\n\n name\n The name of the state definition\n\n ruleName\n Name of the rule.\n\n sql\n The SQL statement used to query the topic.\n\n actions\n The actions associated with the rule.\n\n description\n The description of the rule.\n\n ruleDisable\n Specifies whether the rule is disabled.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n '''\n ret = {'name': ruleName,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n r = __salt__['boto_iot.topic_rule_exists'](ruleName=ruleName,\n region=region, key=key, keyid=keyid, profile=profile)\n\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to create rule: {0}.'.format(r['error']['message'])\n return ret\n\n if not r.get('exists'):\n if __opts__['test']:\n ret['comment'] = 'Rule {0} is set to be created.'.format(ruleName)\n ret['result'] = None\n return ret\n r = __salt__['boto_iot.create_topic_rule'](ruleName=ruleName,\n sql=sql,\n actions=actions,\n description=description,\n ruleDisabled=ruleDisabled,\n region=region, key=key,\n keyid=keyid, profile=profile)\n if not r.get('created'):\n ret['result'] = False\n ret['comment'] = 'Failed to create rule: {0}.'.format(r['error']['message'])\n return ret\n _describe = __salt__['boto_iot.describe_topic_rule'](ruleName,\n region=region, key=key, keyid=keyid, profile=profile)\n ret['changes']['old'] = {'rule': None}\n ret['changes']['new'] = _describe\n ret['comment'] = 'Rule {0} created.'.format(ruleName)\n return ret\n\n ret['comment'] = os.linesep.join([ret['comment'], 'Rule {0} is present.'.format(ruleName)])\n ret['changes'] = {}\n # policy exists, ensure config matches\n _describe = __salt__['boto_iot.describe_topic_rule'](ruleName=ruleName,\n region=region, key=key, keyid=keyid, profile=profile)['rule']\n\n if isinstance(actions, six.string_types):\n actions = salt.utils.json.loads(actions)\n\n need_update = False\n # cmp() function is deprecated in Python 3: use the following as a substitute for 'r'.\n r = (_describe['actions'] > actions) - (_describe['actions'] < actions)\n if bool(r):\n need_update = True\n ret['changes'].setdefault('new', {})['actions'] = actions\n ret['changes'].setdefault('old', {})['actions'] = _describe['actions']\n\n for var in ('sql', 'description', 'ruleDisabled'):\n if _describe[var] != locals()[var]:\n need_update = True\n ret['changes'].setdefault('new', {})[var] = locals()[var]\n ret['changes'].setdefault('old', {})[var] = _describe[var]\n if need_update:\n if __opts__['test']:\n msg = 'Rule {0} set to be modified.'.format(ruleName)\n ret['changes'] = {}\n ret['comment'] = msg\n ret['result'] = None\n return ret\n ret['comment'] = os.linesep.join([ret['comment'], 'Rule to be modified'])\n r = __salt__['boto_iot.replace_topic_rule'](ruleName=ruleName,\n sql=sql,\n actions=actions,\n description=description,\n ruleDisabled=ruleDisabled,\n region=region, key=key,\n keyid=keyid, profile=profile)\n if not r.get('replaced'):\n ret['result'] = False\n ret['comment'] = 'Failed to update rule: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret", "response": "Ensure a topic rule exists and is created."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef topic_rule_absent(name, ruleName,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure topic rule with passed properties is absent.\n\n name\n The name of the state definition.\n\n ruleName\n Name of the policy.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n '''\n\n ret = {'name': ruleName,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n r = __salt__['boto_iot.topic_rule_exists'](ruleName,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to delete rule: {0}.'.format(r['error']['message'])\n return ret\n\n if r and not r['exists']:\n ret['comment'] = 'Rule {0} does not exist.'.format(ruleName)\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'Rule {0} is set to be removed.'.format(ruleName)\n ret['result'] = None\n return ret\n r = __salt__['boto_iot.delete_topic_rule'](ruleName,\n region=region, key=key,\n keyid=keyid, profile=profile)\n if not r['deleted']:\n ret['result'] = False\n ret['comment'] = 'Failed to delete rule: {0}.'.format(r['error']['message'])\n return ret\n ret['changes']['old'] = {'rule': ruleName}\n ret['changes']['new'] = {'rule': None}\n ret['comment'] = 'Rule {0} deleted.'.format(ruleName)\n return ret", "response": "Ensure the named topic rule is absent."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _quote_username(name):\n '''\n Usernames can only contain ascii chars, so make sure we return a str type\n '''\n if not isinstance(name, six.string_types):\n return str(name) # future lint: disable=blacklisted-function\n else:\n return salt.utils.stringutils.to_str(name)", "response": "Quote a username into a str type\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_gecos(name, root=None):\n '''\n Retrieve GECOS field info and return it in dictionary form\n '''\n if root is not None and __grains__['kernel'] != 'AIX':\n getpwnam = functools.partial(_getpwnam, root=root)\n else:\n getpwnam = functools.partial(pwd.getpwnam)\n gecos_field = salt.utils.stringutils.to_unicode(\n getpwnam(_quote_username(name)).pw_gecos).split(',', 4)\n\n if not gecos_field:\n return {}\n else:\n # Assign empty strings for any unspecified trailing GECOS fields\n while len(gecos_field) < 5:\n gecos_field.append('')\n return {'fullname': salt.utils.data.decode(gecos_field[0]),\n 'roomnumber': salt.utils.data.decode(gecos_field[1]),\n 'workphone': salt.utils.data.decode(gecos_field[2]),\n 'homephone': salt.utils.data.decode(gecos_field[3]),\n 'other': salt.utils.data.decode(gecos_field[4])}", "response": "Retrieve the GECOS field info and return it in dictionary form\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding a user to the minion", "response": "def add(name,\n uid=None,\n gid=None,\n groups=None,\n home=None,\n shell=None,\n unique=True,\n system=False,\n fullname='',\n roomnumber='',\n workphone='',\n homephone='',\n other='',\n createhome=True,\n loginclass=None,\n nologinit=False,\n root=None,\n usergroup=None):\n '''\n Add a user to the minion\n\n name\n Username LOGIN to add\n\n uid\n User ID of the new account\n\n gid\n Name or ID of the primary group of the new account\n\n groups\n List of supplementary groups of the new account\n\n home\n Home directory of the new account\n\n shell\n Login shell of the new account\n\n unique\n If not True, the user account can have a non-unique UID\n\n system\n Create a system account\n\n fullname\n GECOS field for the full name\n\n roomnumber\n GECOS field for the room number\n\n workphone\n GECOS field for the work phone\n\n homephone\n GECOS field for the home phone\n\n other\n GECOS field for other information\n\n createhome\n Create the user's home directory\n\n loginclass\n Login class for the new account (OpenBSD)\n\n nologinit\n Do not add the user to the lastlog and faillog databases\n\n root\n Directory to chroot into\n\n usergroup\n Create and add the user to a new primary group of the same name\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' user.add name \n '''\n cmd = ['useradd']\n if shell:\n cmd.extend(['-s', shell])\n if uid not in (None, ''):\n cmd.extend(['-u', uid])\n if gid not in (None, ''):\n cmd.extend(['-g', gid])\n elif usergroup:\n cmd.append('-U')\n if __grains__['kernel'] != 'Linux':\n log.warning(\"'usergroup' is only supported on GNU/Linux hosts.\")\n elif groups is not None and name in groups:\n defs_file = '/etc/login.defs'\n if __grains__['kernel'] != 'OpenBSD':\n try:\n with salt.utils.files.fopen(defs_file) as fp_:\n for line in fp_:\n line = salt.utils.stringutils.to_unicode(line)\n if 'USERGROUPS_ENAB' not in line[:15]:\n continue\n\n if 'yes' in line:\n cmd.extend([\n '-g', __salt__['file.group_to_gid'](name)\n ])\n\n # We found what we wanted, let's break out of the loop\n break\n except OSError:\n log.debug('Error reading %s', defs_file,\n exc_info_on_loglevel=logging.DEBUG)\n else:\n usermgmt_file = '/etc/usermgmt.conf'\n try:\n with salt.utils.files.fopen(usermgmt_file) as fp_:\n for line in fp_:\n line = salt.utils.stringutils.to_unicode(line)\n if 'group' not in line[:5]:\n continue\n\n cmd.extend([\n '-g', line.split()[-1]\n ])\n\n # We found what we wanted, let's break out of the loop\n break\n except OSError:\n # /etc/usermgmt.conf not present: defaults will be used\n pass\n # Setting usergroup to False adds the -N command argument. If\n # usergroup is None, no arguments are added to allow useradd to go\n # with the defaults defined for the OS.\n if usergroup is False:\n cmd.append('-N')\n\n if createhome:\n cmd.append('-m')\n elif (__grains__['kernel'] != 'NetBSD'\n and __grains__['kernel'] != 'OpenBSD'):\n cmd.append('-M')\n\n if nologinit:\n cmd.append('-l')\n\n if home is not None:\n cmd.extend(['-d', home])\n\n if not unique and __grains__['kernel'] != 'AIX':\n cmd.append('-o')\n\n if (system\n and __grains__['kernel'] != 'NetBSD'\n and __grains__['kernel'] != 'OpenBSD'):\n cmd.append('-r')\n\n if __grains__['kernel'] == 'OpenBSD':\n if loginclass is not None:\n cmd.extend(['-L', loginclass])\n\n cmd.append(name)\n\n if root is not None and __grains__['kernel'] != 'AIX':\n cmd.extend(('-R', root))\n\n ret = __salt__['cmd.run_all'](cmd, python_shell=False)\n\n if ret['retcode'] != 0:\n return False\n\n # At this point, the user was successfully created, so return true\n # regardless of the outcome of the below functions. If there is a\n # problem wth changing any of the user's info below, it will be raised\n # in a future highstate call. If anyone has a better idea on how to do\n # this, feel free to change it, but I didn't think it was a good idea\n # to return False when the user was successfully created since A) the\n # user does exist, and B) running useradd again would result in a\n # nonzero exit status and be interpreted as a False result.\n if groups:\n chgroups(name, groups, root=root)\n if fullname:\n chfullname(name, fullname, root=root)\n if roomnumber:\n chroomnumber(name, roomnumber, root=root)\n if workphone:\n chworkphone(name, workphone, root=root)\n if homephone:\n chhomephone(name, homephone, root=root)\n if other:\n chother(name, other, root=root)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndeletes a user from the minion", "response": "def delete(name, remove=False, force=False, root=None):\n '''\n Remove a user from the minion\n\n name\n Username to delete\n\n remove\n Remove home directory and mail spool\n\n force\n Force some actions that would fail otherwise\n\n root\n Directory to chroot into\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' user.delete name remove=True force=True\n '''\n cmd = ['userdel']\n\n if remove:\n cmd.append('-r')\n\n if force and __grains__['kernel'] != 'OpenBSD' and __grains__['kernel'] != 'AIX':\n cmd.append('-f')\n\n cmd.append(name)\n\n if root is not None and __grains__['kernel'] != 'AIX':\n cmd.extend(('-R', root))\n\n ret = __salt__['cmd.run_all'](cmd, python_shell=False)\n\n if ret['retcode'] == 0:\n # Command executed with no errors\n return True\n\n if ret['retcode'] == 12:\n # There's a known bug in Debian based distributions, at least, that\n # makes the command exit with 12, see:\n # https://bugs.launchpad.net/ubuntu/+source/shadow/+bug/1023509\n if __grains__['os_family'] not in ('Debian',):\n return False\n\n if 'var/mail' in ret['stderr'] or 'var/spool/mail' in ret['stderr']:\n # We've hit the bug, let's log it and not fail\n log.debug(\n 'While the userdel exited with code 12, this is a known bug on '\n 'debian based distributions. See http://goo.gl/HH3FzT'\n )\n return True\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the list of all info for all users", "response": "def getent(refresh=False, root=None):\n '''\n Return the list of all info for all users\n\n refresh\n Force a refresh of user information\n\n root\n Directory to chroot into\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' user.getent\n '''\n if 'user.getent' in __context__ and not refresh:\n return __context__['user.getent']\n\n ret = []\n if root is not None and __grains__['kernel'] != 'AIX':\n getpwall = functools.partial(_getpwall, root=root)\n else:\n getpwall = functools.partial(pwd.getpwall)\n\n for data in getpwall():\n ret.append(_format_info(data))\n __context__['user.getent'] = ret\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _chattrib(name, key, value, param, persist=False, root=None):\n '''\n Change an attribute for a named user\n '''\n pre_info = info(name, root=root)\n if not pre_info:\n raise CommandExecutionError('User \\'{0}\\' does not exist'.format(name))\n\n if value == pre_info[key]:\n return True\n\n cmd = ['usermod']\n\n if root is not None and __grains__['kernel'] != 'AIX':\n cmd.extend(('-R', root))\n\n if persist and __grains__['kernel'] != 'OpenBSD':\n cmd.append('-m')\n\n cmd.extend((param, value, name))\n\n __salt__['cmd.run'](cmd, python_shell=False)\n return info(name, root=root).get(key) == value", "response": "Change an attribute for a named user\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef chhome(name, home, persist=False, root=None):\n '''\n Change the home directory of the user, pass True for persist to move files\n to the new home directory if the old home directory exist.\n\n name\n User to modify\n\n home\n New home directory for the user account\n\n presist\n Move contents of the home directory to the new location\n\n root\n Directory to chroot into\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' user.chhome foo /home/users/foo True\n '''\n return _chattrib(name, 'home', home, '-d', persist=persist, root=root)", "response": "Change the home directory of the user"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nchanging the groups of a user into a new user.", "response": "def chgroups(name, groups, append=False, root=None):\n '''\n Change the groups to which this user belongs\n\n name\n User to modify\n\n groups\n Groups to set for the user\n\n append : False\n If ``True``, append the specified group(s). Otherwise, this function\n will replace the user's groups with the specified group(s).\n\n root\n Directory to chroot into\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' user.chgroups foo wheel,root\n salt '*' user.chgroups foo wheel,root append=True\n '''\n if isinstance(groups, six.string_types):\n groups = groups.split(',')\n ugrps = set(list_groups(name))\n if ugrps == set(groups):\n return True\n cmd = ['usermod']\n\n if __grains__['kernel'] != 'OpenBSD':\n if append and __grains__['kernel'] != 'AIX':\n cmd.append('-a')\n cmd.append('-G')\n else:\n if append:\n cmd.append('-G')\n else:\n cmd.append('-S')\n\n if append and __grains__['kernel'] == 'AIX':\n cmd.extend([','.join(ugrps) + ',' + ','.join(groups), name])\n else:\n cmd.extend([','.join(groups), name])\n\n if root is not None and __grains__['kernel'] != 'AIX':\n cmd.extend(('-R', root))\n\n result = __salt__['cmd.run_all'](cmd, python_shell=False)\n # try to fallback on gpasswd to add user to localgroups\n # for old lib-pamldap support\n if __grains__['kernel'] != 'OpenBSD' and __grains__['kernel'] != 'AIX':\n if result['retcode'] != 0 and 'not found in' in result['stderr']:\n ret = True\n for group in groups:\n cmd = ['gpasswd', '-a', name, group]\n if __salt__['cmd.retcode'](cmd, python_shell=False) != 0:\n ret = False\n return ret\n return result['retcode'] == 0"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef chloginclass(name, loginclass, root=None):\n '''\n Change the default login class of the user\n\n name\n User to modify\n\n loginclass\n Login class for the new account\n\n root\n Directory to chroot into\n\n .. note::\n This function only applies to OpenBSD systems.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' user.chloginclass foo staff\n '''\n if __grains__['kernel'] != 'OpenBSD':\n return False\n\n if loginclass == get_loginclass(name):\n return True\n\n cmd = ['usermod', '-L', loginclass, name]\n\n if root is not None and __grains__['kernel'] != 'AIX':\n cmd.extend(('-R', root))\n\n __salt__['cmd.run'](cmd, python_shell=False)\n return get_loginclass(name) == loginclass", "response": "Change the default login class of the user"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning user information for a user", "response": "def info(name, root=None):\n '''\n Return user information\n\n name\n User to get the information\n\n root\n Directory to chroot into\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' user.info root\n '''\n # If root is provided, we use a less portable solution that\n # depends on analyzing /etc/passwd manually. Of course we cannot\n # find users from NIS nor LDAP, but in those cases do not makes\n # sense to provide a root parameter.\n #\n # Please, note that if the non-root /etc/passwd file is long the\n # iteration can be slow.\n if root is not None and __grains__['kernel'] != 'AIX':\n getpwnam = functools.partial(_getpwnam, root=root)\n else:\n getpwnam = functools.partial(pwd.getpwnam)\n\n try:\n data = getpwnam(_quote_username(name))\n except KeyError:\n return {}\n else:\n return _format_info(data)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the login class of the user", "response": "def get_loginclass(name):\n '''\n Get the login class of the user\n\n name\n User to get the information\n\n .. note::\n This function only applies to OpenBSD systems.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' user.get_loginclass foo\n '''\n if __grains__['kernel'] != 'OpenBSD':\n return False\n userinfo = __salt__['cmd.run_stdout'](\n ['userinfo', name],\n python_shell=False)\n for line in userinfo.splitlines():\n if line.startswith('class'):\n try:\n ret = line.split(None, 1)[1]\n break\n except (ValueError, IndexError):\n continue\n else:\n ret = ''\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn user information in a pretty way", "response": "def _format_info(data):\n '''\n Return user information in a pretty way\n '''\n # Put GECOS info into a list\n gecos_field = salt.utils.stringutils.to_unicode(data.pw_gecos).split(',', 4)\n # Make sure our list has at least five elements\n while len(gecos_field) < 5:\n gecos_field.append('')\n\n return {'gid': data.pw_gid,\n 'groups': list_groups(data.pw_name),\n 'home': data.pw_dir,\n 'name': data.pw_name,\n 'passwd': data.pw_passwd,\n 'shell': data.pw_shell,\n 'uid': data.pw_uid,\n 'fullname': gecos_field[0],\n 'roomnumber': gecos_field[1],\n 'workphone': gecos_field[2],\n 'homephone': gecos_field[3],\n 'other': gecos_field[4]}"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrename a user in the specified user s login directory", "response": "def rename(name, new_name, root=None):\n '''\n Change the username for a named user\n\n name\n User to modify\n\n new_name\n New value of the login name\n\n root\n Directory to chroot into\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' user.rename name new_name\n '''\n if info(new_name, root=root):\n raise CommandExecutionError('User \\'{0}\\' already exists'.format(new_name))\n\n return _chattrib(name, 'name', new_name, '-l', root=root)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_subgids(name, first=100000, last=110000):\n '''\n Add a range of subordinate gids to the user\n\n name\n User to modify\n\n first\n Begin of the range\n\n last\n End of the range\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' user.add_subgids foo\n salt '*' user.add_subgids foo first=105000\n salt '*' user.add_subgids foo first=600000000 last=600100000\n '''\n if __grains__['kernel'] != 'Linux':\n log.warning(\"'subgids' are only supported on GNU/Linux hosts.\")\n\n return __salt__['cmd.run'](['usermod', '-w', '-'.join(str(x) for x in (first, last)), name])", "response": "Add a range of subordinate gids to the user\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef capability_installed(name,\n source=None,\n limit_access=False,\n image=None,\n restart=False):\n '''\n Install a DISM capability\n\n Args:\n name (str): The capability to install\n source (str): The optional source of the capability\n limit_access (bool): Prevent DISM from contacting Windows Update for\n online images\n image (Optional[str]): The path to the root directory of an offline\n Windows image. If `None` is passed, the running operating system is\n targeted. Default is None.\n restart (Optional[bool]): Reboot the machine if required by the install\n\n Example:\n Run ``dism.available_capabilities`` to get a list of available\n capabilities. This will help you get the proper name to use.\n\n .. code-block:: yaml\n\n install_dotnet35:\n dism.capability_installed:\n - name: NetFX3~~~~\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}}\n\n old = __salt__['dism.installed_capabilities']()\n\n if name in old:\n ret['comment'] = 'The capability {0} is already installed'.format(name)\n return ret\n\n if __opts__['test']:\n ret['changes']['capability'] = '{0} will be installed'.format(name)\n ret['result'] = None\n return ret\n\n # Install the capability\n status = __salt__['dism.add_capability'](\n name, source, limit_access, image, restart)\n\n if status['retcode'] not in [0, 1641, 3010]:\n ret['comment'] = 'Failed to install {0}: {1}'\\\n .format(name, status['stdout'])\n ret['result'] = False\n\n new = __salt__['dism.installed_capabilities']()\n changes = salt.utils.data.compare_lists(old, new)\n\n if changes:\n ret['comment'] = 'Installed {0}'.format(name)\n ret['changes'] = status\n ret['changes']['capability'] = changes\n\n return ret", "response": "Install a DISM capability."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nremoves a package from the list of installed packages.", "response": "def package_removed(name, image=None, restart=False):\n '''\n Uninstall a package\n\n Args:\n name (str): The full path to the package. Can be either a .cab file or a\n folder. Should point to the original source of the package, not to\n where the file is installed. This can also be the name of a package as listed in\n ``dism.installed_packages``\n image (Optional[str]): The path to the root directory of an offline\n Windows image. If `None` is passed, the running operating system is\n targeted. Default is None.\n restart (Optional[bool]): Reboot the machine if required by the install\n\n Example:\n\n .. code-block:: yaml\n\n # Example using source\n remove_KB1231231:\n dism.package_installed:\n - name: C:\\\\Packages\\\\KB1231231.cab\n\n # Example using name from ``dism.installed_packages``\n remove_KB1231231:\n dism.package_installed:\n - name: Package_for_KB1231231~31bf3856ad364e35~amd64~~10.0.1.3\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}}\n\n # Fail if using a non-existent package path\n if '~' not in name and not os.path.exists(name):\n if __opts__['test']:\n ret['result'] = None\n else:\n ret['result'] = False\n ret['comment'] = 'Package path {0} does not exist'.format(name)\n return ret\n\n old = __salt__['dism.installed_packages']()\n\n # Get package info so we can see if it's already removed\n package_info = __salt__['dism.package_info'](name)\n\n # If `Package Identity` isn't returned or if they passed a cab file, if\n # `Package Identity` isn't in the list of installed packages\n if 'Package Identity' not in package_info or \\\n package_info['Package Identity'] not in old:\n ret['comment'] = 'The package {0} is already removed'.format(name)\n return ret\n\n if __opts__['test']:\n ret['changes']['package'] = '{0} will be removed'.format(name)\n ret['result'] = None\n return ret\n\n # Remove the package\n status = __salt__['dism.remove_package'](name, image, restart)\n\n if status['retcode'] not in [0, 1641, 3010]:\n ret['comment'] = 'Failed to remove {0}: {1}' \\\n .format(name, status['stdout'])\n ret['result'] = False\n\n new = __salt__['dism.installed_packages']()\n changes = salt.utils.data.compare_lists(old, new)\n\n if changes:\n ret['comment'] = 'Removed {0}'.format(name)\n ret['changes'] = status\n ret['changes']['package'] = changes\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef ping(host, timeout=False, return_boolean=False):\n '''\n Performs an ICMP ping to a host\n\n .. versionchanged:: 2015.8.0\n Added support for SunOS\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.ping archlinux.org\n\n .. versionadded:: 2015.5.0\n\n Return a True or False instead of ping output.\n\n .. code-block:: bash\n\n salt '*' network.ping archlinux.org return_boolean=True\n\n Set the time to wait for a response in seconds.\n\n .. code-block:: bash\n\n salt '*' network.ping archlinux.org timeout=3\n '''\n if timeout:\n if __grains__['kernel'] == 'SunOS':\n cmd = 'ping -c 4 {1} {0}'.format(timeout, salt.utils.network.sanitize_host(host))\n else:\n cmd = 'ping -W {0} -c 4 {1}'.format(timeout, salt.utils.network.sanitize_host(host))\n else:\n cmd = 'ping -c 4 {0}'.format(salt.utils.network.sanitize_host(host))\n if return_boolean:\n ret = __salt__['cmd.run_all'](cmd)\n if ret['retcode'] != 0:\n return False\n else:\n return True\n else:\n return __salt__['cmd.run'](cmd)", "response": "Ping ICMP to a host"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _netstat_linux():\n '''\n Return netstat information for Linux distros\n '''\n ret = []\n cmd = 'netstat -tulpnea'\n out = __salt__['cmd.run'](cmd)\n for line in out.splitlines():\n comps = line.split()\n if line.startswith('tcp'):\n ret.append({\n 'proto': comps[0],\n 'recv-q': comps[1],\n 'send-q': comps[2],\n 'local-address': comps[3],\n 'remote-address': comps[4],\n 'state': comps[5],\n 'user': comps[6],\n 'inode': comps[7],\n 'program': comps[8]})\n if line.startswith('udp'):\n ret.append({\n 'proto': comps[0],\n 'recv-q': comps[1],\n 'send-q': comps[2],\n 'local-address': comps[3],\n 'remote-address': comps[4],\n 'user': comps[5],\n 'inode': comps[6],\n 'program': comps[7]})\n return ret", "response": "Return netstat information for Linux distros\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _ss_linux():\n '''\n Return ss information for Linux distros\n (netstat is deprecated and may not be available)\n '''\n ret = []\n cmd = 'ss -tulpnea'\n out = __salt__['cmd.run'](cmd)\n for line in out.splitlines():\n comps = line.split()\n ss_user = 0\n ss_inode = 0\n ss_program = ''\n length = len(comps)\n if line.startswith('tcp') or line.startswith('udp'):\n i = 6\n while i < (length - 1):\n fields = comps[i].split(\":\")\n if fields[0] == \"users\":\n users = fields[1].split(\",\")\n ss_program = users[0].split(\"\\\"\")[1]\n\n if fields[0] == \"uid\":\n ss_user = fields[1]\n\n if fields[0] == \"ino\":\n ss_inode = fields[1]\n\n i += 1\n\n if line.startswith('tcp'):\n ss_state = comps[1]\n if ss_state == \"ESTAB\":\n ss_state = \"ESTABLISHED\"\n ret.append({\n 'proto': comps[0],\n 'recv-q': comps[2],\n 'send-q': comps[3],\n 'local-address': comps[4],\n 'remote-address': comps[5],\n 'state': ss_state,\n 'user': ss_user,\n 'inode': ss_inode,\n 'program': ss_program})\n if line.startswith('udp'):\n ret.append({\n 'proto': comps[0],\n 'recv-q': comps[2],\n 'send-q': comps[3],\n 'local-address': comps[4],\n 'remote-address': comps[5],\n 'user': ss_user,\n 'inode': ss_inode,\n 'program': ss_program})\n return ret", "response": "Return ss information for Linux distros\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the process information for network connections using fstat", "response": "def _netinfo_openbsd():\n '''\n Get process information for network connections using fstat\n '''\n ret = {}\n _fstat_re = re.compile(\n r'internet(6)? (?:stream tcp 0x\\S+ (\\S+)|dgram udp (\\S+))'\n r'(?: [<>=-]+ (\\S+))?$'\n )\n out = __salt__['cmd.run']('fstat')\n for line in out.splitlines():\n try:\n user, cmd, pid, _, details = line.split(None, 4)\n ipv6, tcp, udp, remote_addr = _fstat_re.match(details).groups()\n except (ValueError, AttributeError):\n # Line either doesn't have the right number of columns, or the\n # regex which looks for address information did not match. Either\n # way, ignore this line and continue on to the next one.\n continue\n if tcp:\n local_addr = tcp\n proto = 'tcp{0}'.format('' if ipv6 is None else ipv6)\n else:\n local_addr = udp\n proto = 'udp{0}'.format('' if ipv6 is None else ipv6)\n if ipv6:\n # IPv6 addresses have the address part enclosed in brackets (if the\n # address part is not a wildcard) to distinguish the address from\n # the port number. Remove them.\n local_addr = ''.join(x for x in local_addr if x not in '[]')\n\n # Normalize to match netstat output\n local_addr = '.'.join(local_addr.rsplit(':', 1))\n if remote_addr is None:\n remote_addr = '*.*'\n else:\n remote_addr = '.'.join(remote_addr.rsplit(':', 1))\n\n ret.setdefault(\n local_addr, {}).setdefault(\n remote_addr, {}).setdefault(\n proto, {}).setdefault(\n pid, {})['user'] = user\n ret[local_addr][remote_addr][proto][pid]['cmd'] = cmd\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _netinfo_freebsd_netbsd():\n '''\n Get process information for network connections using sockstat\n '''\n ret = {}\n # NetBSD requires '-n' to disable port-to-service resolution\n out = __salt__['cmd.run'](\n 'sockstat -46 {0} | tail -n+2'.format(\n '-n' if __grains__['kernel'] == 'NetBSD' else ''\n ), python_shell=True\n )\n for line in out.splitlines():\n user, cmd, pid, _, proto, local_addr, remote_addr = line.split()\n local_addr = '.'.join(local_addr.rsplit(':', 1))\n remote_addr = '.'.join(remote_addr.rsplit(':', 1))\n ret.setdefault(\n local_addr, {}).setdefault(\n remote_addr, {}).setdefault(\n proto, {}).setdefault(\n pid, {})['user'] = user\n ret[local_addr][remote_addr][proto][pid]['cmd'] = cmd\n return ret", "response": "Return process information for freebsd network connections using sockstat\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _ppid():\n '''\n Return a dict of pid to ppid mappings\n '''\n ret = {}\n if __grains__['kernel'] == 'SunOS':\n cmd = 'ps -a -o pid,ppid | tail +2'\n else:\n cmd = 'ps -ax -o pid,ppid | tail -n+2'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n pid, ppid = line.split()\n ret[pid] = ppid\n return ret", "response": "Return a dict of pid to ppid mappings"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn netstat information for BSD flavors", "response": "def _netstat_bsd():\n '''\n Return netstat information for BSD flavors\n '''\n ret = []\n if __grains__['kernel'] == 'NetBSD':\n for addr_family in ('inet', 'inet6'):\n cmd = 'netstat -f {0} -an | tail -n+3'.format(addr_family)\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n entry = {\n 'proto': comps[0],\n 'recv-q': comps[1],\n 'send-q': comps[2],\n 'local-address': comps[3],\n 'remote-address': comps[4]\n }\n if entry['proto'].startswith('tcp'):\n entry['state'] = comps[5]\n ret.append(entry)\n else:\n # Lookup TCP connections\n cmd = 'netstat -p tcp -an | tail -n+3'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n ret.append({\n 'proto': comps[0],\n 'recv-q': comps[1],\n 'send-q': comps[2],\n 'local-address': comps[3],\n 'remote-address': comps[4],\n 'state': comps[5]})\n # Lookup UDP connections\n cmd = 'netstat -p udp -an | tail -n+3'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n ret.append({\n 'proto': comps[0],\n 'recv-q': comps[1],\n 'send-q': comps[2],\n 'local-address': comps[3],\n 'remote-address': comps[4]})\n\n # Add in user and program info\n ppid = _ppid()\n if __grains__['kernel'] == 'OpenBSD':\n netinfo = _netinfo_openbsd()\n elif __grains__['kernel'] in ('FreeBSD', 'NetBSD'):\n netinfo = _netinfo_freebsd_netbsd()\n for idx in range(len(ret)):\n local = ret[idx]['local-address']\n remote = ret[idx]['remote-address']\n proto = ret[idx]['proto']\n try:\n # Make a pointer to the info for this connection for easier\n # reference below\n ptr = netinfo[local][remote][proto]\n except KeyError:\n continue\n # Get the pid-to-ppid mappings for this connection\n conn_ppid = dict((x, y) for x, y in six.iteritems(ppid) if x in ptr)\n try:\n # Master pid for this connection will be the pid whose ppid isn't\n # in the subset dict we created above\n master_pid = next(iter(\n x for x, y in six.iteritems(conn_ppid) if y not in ptr\n ))\n except StopIteration:\n continue\n ret[idx]['user'] = ptr[master_pid]['user']\n ret[idx]['program'] = '/'.join((master_pid, ptr[master_pid]['cmd']))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _netstat_sunos():\n '''\n Return netstat information for SunOS flavors\n '''\n log.warning('User and program not (yet) supported on SunOS')\n\n ret = []\n for addr_family in ('inet', 'inet6'):\n # Lookup TCP connections\n cmd = 'netstat -f {0} -P tcp -an | tail +5'.format(addr_family)\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n ret.append({\n 'proto': 'tcp6' if addr_family == 'inet6' else 'tcp',\n 'recv-q': comps[5],\n 'send-q': comps[4],\n 'local-address': comps[0],\n 'remote-address': comps[1],\n 'state': comps[6]})\n # Lookup UDP connections\n cmd = 'netstat -f {0} -P udp -an | tail +5'.format(addr_family)\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n ret.append({\n 'proto': 'udp6' if addr_family == 'inet6' else 'udp',\n 'local-address': comps[0],\n 'remote-address': comps[1] if len(comps) > 2 else ''})\n\n return ret", "response": "Return netstat information for SunOS flavors"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _netstat_aix():\n '''\n Return netstat information for SunOS flavors\n '''\n ret = []\n ## AIX 6.1 - 7.2, appears to ignore addr_family field contents\n ## for addr_family in ('inet', 'inet6'):\n for addr_family in ('inet',):\n # Lookup connections\n cmd = 'netstat -n -a -f {0} | tail -n +3'.format(addr_family)\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n if len(comps) < 5:\n continue\n\n proto_seen = None\n tcp_flag = True\n if 'tcp' == comps[0] or 'tcp4' == comps[0]:\n proto_seen = 'tcp'\n elif 'tcp6' == comps[0]:\n proto_seen = 'tcp6'\n elif 'udp' == comps[0] or 'udp4' == comps[0]:\n proto_seen = 'udp'\n tcp_flag = False\n elif 'udp6' == comps[0]:\n proto_seen = 'udp6'\n tcp_flag = False\n\n if tcp_flag:\n if len(comps) >= 6:\n ret.append({\n 'proto': proto_seen,\n 'recv-q': comps[1],\n 'send-q': comps[2],\n 'local-address': comps[3],\n 'remote-address': comps[4],\n 'state': comps[5]})\n else:\n if len(comps) >= 5:\n ret.append({\n 'proto': proto_seen,\n 'local-address': comps[3],\n 'remote-address': comps[4]})\n return ret", "response": "Return netstat information for SunOS flavors"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _ip_route_linux():\n '''\n Return ip routing information for Linux distros\n (netstat is deprecated and may not be available)\n '''\n # table main closest to old netstat inet output\n ret = []\n cmd = 'ip -4 route show table main'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n\n # need to fake similar output to that provided by netstat\n # to maintain output format\n if comps[0] == \"unreachable\":\n continue\n\n if comps[0] == \"default\":\n ip_interface = ''\n if comps[3] == \"dev\":\n ip_interface = comps[4]\n\n ret.append({\n 'addr_family': 'inet',\n 'destination': '0.0.0.0',\n 'gateway': comps[2],\n 'netmask': '0.0.0.0',\n 'flags': 'UG',\n 'interface': ip_interface})\n else:\n address_mask = convert_cidr(comps[0])\n ip_interface = ''\n if comps[1] == \"dev\":\n ip_interface = comps[2]\n\n ret.append({\n 'addr_family': 'inet',\n 'destination': address_mask['network'],\n 'gateway': '0.0.0.0',\n 'netmask': address_mask['netmask'],\n 'flags': 'U',\n 'interface': ip_interface})\n\n # table all closest to old netstat inet6 output\n cmd = 'ip -6 route show table all'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n\n # need to fake similar output to that provided by netstat\n # to maintain output format\n if comps[0] == \"unreachable\":\n continue\n\n if comps[0] == \"default\":\n ip_interface = ''\n if comps[3] == \"dev\":\n ip_interface = comps[4]\n\n ret.append({\n 'addr_family': 'inet6',\n 'destination': '::/0',\n 'gateway': comps[2],\n 'netmask': '',\n 'flags': 'UG',\n 'interface': ip_interface})\n\n elif comps[0] == \"local\":\n ip_interface = ''\n if comps[2] == \"dev\":\n ip_interface = comps[3]\n\n local_address = comps[1] + \"/128\"\n ret.append({\n 'addr_family': 'inet6',\n 'destination': local_address,\n 'gateway': '::',\n 'netmask': '',\n 'flags': 'U',\n 'interface': ip_interface})\n else:\n address_mask = convert_cidr(comps[0])\n ip_interface = ''\n if comps[1] == \"dev\":\n ip_interface = comps[2]\n\n ret.append({\n 'addr_family': 'inet6',\n 'destination': comps[0],\n 'gateway': '::',\n 'netmask': '',\n 'flags': 'U',\n 'interface': ip_interface})\n return ret", "response": "Return ip routing information for Linux distros"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _netstat_route_freebsd():\n '''\n Return netstat routing information for FreeBSD and macOS\n '''\n ret = []\n cmd = 'netstat -f inet -rn | tail -n+5'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n if __grains__['os'] == 'FreeBSD' and int(__grains__.get('osmajorrelease', 0)) < 10:\n ret.append({\n 'addr_family': 'inet',\n 'destination': comps[0],\n 'gateway': comps[1],\n 'netmask': comps[2],\n 'flags': comps[3],\n 'interface': comps[5]})\n else:\n ret.append({\n 'addr_family': 'inet',\n 'destination': comps[0],\n 'gateway': comps[1],\n 'netmask': '',\n 'flags': comps[2],\n 'interface': comps[3]})\n cmd = 'netstat -f inet6 -rn | tail -n+5'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n ret.append({\n 'addr_family': 'inet6',\n 'destination': comps[0],\n 'gateway': comps[1],\n 'netmask': '',\n 'flags': comps[2],\n 'interface': comps[3]})\n return ret", "response": "Return netstat routing information for FreeBSD and MacOS"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning netstat routing information for NetBSD", "response": "def _netstat_route_netbsd():\n '''\n Return netstat routing information for NetBSD\n '''\n ret = []\n cmd = 'netstat -f inet -rn | tail -n+5'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n ret.append({\n 'addr_family': 'inet',\n 'destination': comps[0],\n 'gateway': comps[1],\n 'netmask': '',\n 'flags': comps[3],\n 'interface': comps[6]})\n cmd = 'netstat -f inet6 -rn | tail -n+5'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n ret.append({\n 'addr_family': 'inet6',\n 'destination': comps[0],\n 'gateway': comps[1],\n 'netmask': '',\n 'flags': comps[3],\n 'interface': comps[6]})\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _netstat_route_sunos():\n '''\n Return netstat routing information for SunOS\n '''\n ret = []\n cmd = 'netstat -f inet -rn | tail +5'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n ret.append({\n 'addr_family': 'inet',\n 'destination': comps[0],\n 'gateway': comps[1],\n 'netmask': '',\n 'flags': comps[2],\n 'interface': comps[5] if len(comps) >= 6 else ''})\n cmd = 'netstat -f inet6 -rn | tail +5'\n out = __salt__['cmd.run'](cmd, python_shell=True)\n for line in out.splitlines():\n comps = line.split()\n ret.append({\n 'addr_family': 'inet6',\n 'destination': comps[0],\n 'gateway': comps[1],\n 'netmask': '',\n 'flags': comps[2],\n 'interface': comps[5] if len(comps) >= 6 else ''})\n return ret", "response": "Return netstat routing information for SunOS"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning information on open ports and states of each available network entry.", "response": "def netstat():\n '''\n Return information on open ports and states\n\n .. note::\n On BSD minions, the output contains PID info (where available) for each\n netstat entry, fetched from sockstat/fstat output.\n\n .. versionchanged:: 2014.1.4\n Added support for OpenBSD, FreeBSD, and NetBSD\n\n .. versionchanged:: 2015.8.0\n Added support for SunOS\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.netstat\n '''\n if __grains__['kernel'] == 'Linux':\n if not salt.utils.path.which('netstat'):\n return _ss_linux()\n else:\n return _netstat_linux()\n elif __grains__['kernel'] in ('OpenBSD', 'FreeBSD', 'NetBSD'):\n return _netstat_bsd()\n elif __grains__['kernel'] == 'SunOS':\n return _netstat_sunos()\n elif __grains__['kernel'] == 'AIX':\n return _netstat_aix()\n raise CommandExecutionError('Not yet supported on this platform')"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef active_tcp():\n '''\n Return a dict containing information on all of the running TCP connections (currently linux and solaris only)\n\n .. versionchanged:: 2015.8.4\n\n Added support for SunOS\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.active_tcp\n '''\n if __grains__['kernel'] == 'Linux':\n return salt.utils.network.active_tcp()\n elif __grains__['kernel'] == 'SunOS':\n # lets use netstat to mimic linux as close as possible\n ret = {}\n for connection in _netstat_sunos():\n if not connection['proto'].startswith('tcp'):\n continue\n if connection['state'] != 'ESTABLISHED':\n continue\n ret[len(ret)+1] = {\n 'local_addr': '.'.join(connection['local-address'].split('.')[:-1]),\n 'local_port': '.'.join(connection['local-address'].split('.')[-1:]),\n 'remote_addr': '.'.join(connection['remote-address'].split('.')[:-1]),\n 'remote_port': '.'.join(connection['remote-address'].split('.')[-1:])\n }\n return ret\n elif __grains__['kernel'] == 'AIX':\n # lets use netstat to mimic linux as close as possible\n ret = {}\n for connection in _netstat_aix():\n if not connection['proto'].startswith('tcp'):\n continue\n if connection['state'] != 'ESTABLISHED':\n continue\n ret[len(ret)+1] = {\n 'local_addr': '.'.join(connection['local-address'].split('.')[:-1]),\n 'local_port': '.'.join(connection['local-address'].split('.')[-1:]),\n 'remote_addr': '.'.join(connection['remote-address'].split('.')[:-1]),\n 'remote_port': '.'.join(connection['remote-address'].split('.')[-1:])\n }\n return ret\n else:\n return {}", "response": "Return a dict containing information on all of the running TCP connections"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nperforming a traceroute to a 3rd party host", "response": "def traceroute(host):\n '''\n Performs a traceroute to a 3rd party host\n\n .. versionchanged:: 2015.8.0\n Added support for SunOS\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.traceroute archlinux.org\n '''\n ret = []\n if not salt.utils.path.which('traceroute'):\n log.info('This minion does not have traceroute installed')\n return ret\n\n cmd = 'traceroute {0}'.format(salt.utils.network.sanitize_host(host))\n\n out = __salt__['cmd.run'](cmd)\n\n # Parse version of traceroute\n if salt.utils.platform.is_sunos() or salt.utils.platform.is_aix():\n traceroute_version = [0, 0, 0]\n else:\n cmd2 = 'traceroute --version'\n out2 = __salt__['cmd.run'](cmd2)\n try:\n # Linux traceroute version looks like:\n # Modern traceroute for Linux, version 2.0.19, Dec 10 2012\n # Darwin and FreeBSD traceroute version looks like: Version 1.4a12+[FreeBSD|Darwin]\n\n traceroute_version_raw = re.findall(r'.*[Vv]ersion (\\d+)\\.([\\w\\+]+)\\.*(\\w*)', out2)[0]\n log.debug('traceroute_version_raw: %s', traceroute_version_raw)\n traceroute_version = []\n for t in traceroute_version_raw:\n try:\n traceroute_version.append(int(t))\n except ValueError:\n traceroute_version.append(t)\n\n if len(traceroute_version) < 3:\n traceroute_version.append(0)\n\n log.debug('traceroute_version: %s', traceroute_version)\n\n except IndexError:\n traceroute_version = [0, 0, 0]\n\n for line in out.splitlines():\n if ' ' not in line:\n continue\n if line.startswith('traceroute'):\n continue\n\n if salt.utils.platform.is_aix():\n if line.startswith('trying to get source for'):\n continue\n\n if line.startswith('source should be'):\n continue\n\n if line.startswith('outgoing MTU'):\n continue\n\n if line.startswith('fragmentation required'):\n continue\n\n if 'Darwin' in six.text_type(traceroute_version[1]) or \\\n 'FreeBSD' in six.text_type(traceroute_version[1]) or \\\n __grains__['kernel'] in ('SunOS', 'AIX'):\n try:\n traceline = re.findall(r'\\s*(\\d*)\\s+(.*)\\s+\\((.*)\\)\\s+(.*)$', line)[0]\n except IndexError:\n traceline = re.findall(r'\\s*(\\d*)\\s+(\\*\\s+\\*\\s+\\*)', line)[0]\n\n log.debug('traceline: %s', traceline)\n delays = re.findall(r'(\\d+\\.\\d+)\\s*ms', six.text_type(traceline))\n\n try:\n if traceline[1] == '* * *':\n result = {\n 'count': traceline[0],\n 'hostname': '*'\n }\n else:\n result = {\n 'count': traceline[0],\n 'hostname': traceline[1],\n 'ip': traceline[2],\n }\n for idx in range(0, len(delays)):\n result['ms{0}'.format(idx + 1)] = delays[idx]\n except IndexError:\n result = {}\n\n elif (traceroute_version[0] >= 2 and traceroute_version[2] >= 14\n or traceroute_version[0] >= 2 and traceroute_version[1] > 0):\n comps = line.split(' ')\n if comps[1] == '* * *':\n result = {\n 'count': int(comps[0]),\n 'hostname': '*'}\n else:\n result = {\n 'count': int(comps[0]),\n 'hostname': comps[1].split()[0],\n 'ip': comps[1].split()[1].strip('()'),\n 'ms1': float(comps[2].split()[0]),\n 'ms2': float(comps[3].split()[0]),\n 'ms3': float(comps[4].split()[0])}\n else:\n comps = line.split()\n result = {\n 'count': comps[0],\n 'hostname': comps[1],\n 'ip': comps[2],\n 'ms1': comps[4],\n 'ms2': comps[6],\n 'ms3': comps[8],\n 'ping1': comps[3],\n 'ping2': comps[5],\n 'ping3': comps[7]}\n\n ret.append(result)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nperform a DNS lookup with dig CLI Example", "response": "def dig(host):\n '''\n Performs a DNS lookup with dig\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.dig archlinux.org\n '''\n cmd = 'dig {0}'.format(salt.utils.network.sanitize_host(host))\n return __salt__['cmd.run'](cmd)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the arp table from the minion", "response": "def arp():\n '''\n Return the arp table from the minion\n\n .. versionchanged:: 2015.8.0\n Added support for SunOS\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.arp\n '''\n ret = {}\n out = __salt__['cmd.run']('arp -an')\n for line in out.splitlines():\n comps = line.split()\n if len(comps) < 4:\n continue\n if __grains__['kernel'] == 'SunOS':\n if ':' not in comps[-1]:\n continue\n ret[comps[-1]] = comps[1]\n elif __grains__['kernel'] == 'OpenBSD':\n if comps[0] == 'Host' or comps[1] == '(incomplete)':\n continue\n ret[comps[1]] = comps[0]\n elif __grains__['kernel'] == 'AIX':\n if comps[0] in ('bucket', 'There'):\n continue\n ret[comps[3]] = comps[1].strip('(').strip(')')\n else:\n ret[comps[3]] = comps[1].strip('(').strip(')')\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconverts a CIDR address to a network address subnet mask and broadcast address.", "response": "def convert_cidr(cidr):\n '''\n returns the network address, subnet mask and broadcast address of a cidr address\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.convert_cidr 172.31.0.0/16\n '''\n ret = {'network': None,\n 'netmask': None,\n 'broadcast': None}\n cidr = calc_net(cidr)\n network_info = ipaddress.ip_network(cidr)\n ret['network'] = six.text_type(network_info.network_address)\n ret['netmask'] = six.text_type(network_info.netmask)\n ret['broadcast'] = six.text_type(network_info.broadcast_address)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmodify the hostname of the current node and the hostname of the current node.", "response": "def mod_hostname(hostname):\n '''\n Modify hostname\n\n .. versionchanged:: 2015.8.0\n Added support for SunOS (Solaris 10, Illumos, SmartOS)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.mod_hostname master.saltstack.com\n '''\n #\n # SunOS tested on SmartOS and OmniOS (Solaris 10 compatible)\n # Oracle Solaris 11 uses smf, currently not supported\n #\n # /etc/nodename is the hostname only, not fqdn\n # /etc/defaultdomain is the domain\n # /etc/hosts should have both fqdn and hostname entries\n #\n\n if hostname is None:\n return False\n\n hostname_cmd = salt.utils.path.which('hostnamectl') or salt.utils.path.which('hostname')\n if salt.utils.platform.is_sunos():\n uname_cmd = '/usr/bin/uname' if salt.utils.platform.is_smartos() else salt.utils.path.which('uname')\n check_hostname_cmd = salt.utils.path.which('check-hostname')\n\n # Grab the old hostname so we know which hostname to change and then\n # change the hostname using the hostname command\n if hostname_cmd.endswith('hostnamectl'):\n result = __salt__['cmd.run_all']('{0} status'.format(hostname_cmd))\n if 0 == result['retcode']:\n out = result['stdout']\n for line in out.splitlines():\n line = line.split(':')\n if 'Static hostname' in line[0]:\n o_hostname = line[1].strip()\n else:\n log.debug('%s was unable to get hostname', hostname_cmd)\n o_hostname = __salt__['network.get_hostname']()\n elif not salt.utils.platform.is_sunos():\n # don't run hostname -f because -f is not supported on all platforms\n o_hostname = socket.getfqdn()\n else:\n # output: Hostname core OK: fully qualified as core.acheron.be\n o_hostname = __salt__['cmd.run'](check_hostname_cmd).split(' ')[-1]\n\n if hostname_cmd.endswith('hostnamectl'):\n result = __salt__['cmd.run_all']('{0} set-hostname {1}'.format(\n hostname_cmd,\n hostname,\n ))\n if result['retcode'] != 0:\n log.debug('%s was unable to set hostname. Error: %s',\n hostname_cmd, result['stderr'])\n return False\n elif not salt.utils.platform.is_sunos():\n __salt__['cmd.run']('{0} {1}'.format(hostname_cmd, hostname))\n else:\n __salt__['cmd.run']('{0} -S {1}'.format(uname_cmd, hostname.split('.')[0]))\n\n # Modify the /etc/hosts file to replace the old hostname with the\n # new hostname\n with salt.utils.files.fopen('/etc/hosts', 'r') as fp_:\n host_c = [salt.utils.stringutils.to_unicode(_l)\n for _l in fp_.readlines()]\n\n with salt.utils.files.fopen('/etc/hosts', 'w') as fh_:\n for host in host_c:\n host = host.split()\n\n try:\n host[host.index(o_hostname)] = hostname\n if salt.utils.platform.is_sunos():\n # also set a copy of the hostname\n host[host.index(o_hostname.split('.')[0])] = hostname.split('.')[0]\n except ValueError:\n pass\n\n fh_.write(salt.utils.stringutils.to_str('\\t'.join(host) + '\\n'))\n\n # Modify the /etc/sysconfig/network configuration file to set the\n # new hostname\n if __grains__['os_family'] == 'RedHat':\n with salt.utils.files.fopen('/etc/sysconfig/network', 'r') as fp_:\n network_c = [salt.utils.stringutils.to_unicode(_l)\n for _l in fp_.readlines()]\n\n with salt.utils.files.fopen('/etc/sysconfig/network', 'w') as fh_:\n for net in network_c:\n if net.startswith('HOSTNAME'):\n old_hostname = net.split('=', 1)[1].rstrip()\n quote_type = salt.utils.stringutils.is_quoted(old_hostname)\n fh_.write(salt.utils.stringutils.to_str(\n 'HOSTNAME={1}{0}{1}\\n'.format(\n salt.utils.stringutils.dequote(hostname),\n quote_type)))\n else:\n fh_.write(salt.utils.stringutils.to_str(net))\n elif __grains__['os_family'] in ('Debian', 'NILinuxRT'):\n with salt.utils.files.fopen('/etc/hostname', 'w') as fh_:\n fh_.write(salt.utils.stringutils.to_str(hostname + '\\n'))\n if __grains__['lsb_distrib_id'] == 'nilrt':\n str_hostname = salt.utils.stringutils.to_str(hostname)\n nirtcfg_cmd = '/usr/local/natinst/bin/nirtcfg'\n nirtcfg_cmd += ' --set section=SystemSettings,token=\\'Host_Name\\',value=\\'{0}\\''.format(str_hostname)\n if __salt__['cmd.run_all'](nirtcfg_cmd)['retcode'] != 0:\n raise CommandExecutionError('Couldn\\'t set hostname to: {0}\\n'.format(str_hostname))\n elif __grains__['os_family'] == 'OpenBSD':\n with salt.utils.files.fopen('/etc/myname', 'w') as fh_:\n fh_.write(salt.utils.stringutils.to_str(hostname + '\\n'))\n\n # Update /etc/nodename and /etc/defaultdomain on SunOS\n if salt.utils.platform.is_sunos():\n with salt.utils.files.fopen('/etc/nodename', 'w') as fh_:\n fh_.write(salt.utils.stringutils.to_str(\n hostname.split('.')[0] + '\\n')\n )\n with salt.utils.files.fopen('/etc/defaultdomain', 'w') as fh_:\n fh_.write(salt.utils.stringutils.to_str(\n \".\".join(hostname.split('.')[1:]) + '\\n')\n )\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_bufsize_linux(iface):\n '''\n Return network interface buffer information using ethtool\n '''\n ret = {'result': False}\n\n cmd = '/sbin/ethtool -g {0}'.format(iface)\n out = __salt__['cmd.run'](cmd)\n pat = re.compile(r'^(.+):\\s+(\\d+)$')\n suffix = 'max-'\n for line in out.splitlines():\n res = pat.match(line)\n if res:\n ret[res.group(1).lower().replace(' ', '-') + suffix] = int(res.group(2))\n ret['result'] = True\n elif line.endswith('maximums:'):\n suffix = '-max'\n elif line.endswith('settings:'):\n suffix = ''\n if not ret['result']:\n parts = out.split()\n # remove shell cmd prefix from msg\n if parts[0].endswith('sh:'):\n out = ' '.join(parts[1:])\n ret['comment'] = out\n return ret", "response": "Return network interface buffer information using ethtool\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nmodifying network interface buffer sizes using ethtool", "response": "def _mod_bufsize_linux(iface, *args, **kwargs):\n '''\n Modify network interface buffer sizes using ethtool\n '''\n ret = {'result': False,\n 'comment': 'Requires rx= tx== rx-mini= and/or rx-jumbo='}\n cmd = '/sbin/ethtool -G ' + iface\n if not kwargs:\n return ret\n if args:\n ret['comment'] = 'Unknown arguments: ' + ' '.join([six.text_type(item)\n for item in args])\n return ret\n eargs = ''\n for kw in ['rx', 'tx', 'rx-mini', 'rx-jumbo']:\n value = kwargs.get(kw)\n if value is not None:\n eargs += ' ' + kw + ' ' + six.text_type(value)\n if not eargs:\n return ret\n cmd += eargs\n out = __salt__['cmd.run'](cmd)\n if out:\n ret['comment'] = out\n else:\n ret['comment'] = eargs.strip()\n ret['result'] = True\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef mod_bufsize(iface, *args, **kwargs):\n '''\n Modify network interface buffers (currently linux only)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.mod_bufsize tx= rx= rx-mini= rx-jumbo=\n '''\n if __grains__['kernel'] == 'Linux':\n if os.path.exists('/sbin/ethtool'):\n return _mod_bufsize_linux(iface, *args, **kwargs)\n\n return False", "response": "Modify network interface buffers"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef routes(family=None):\n '''\n Return currently configured routes from routing table\n\n .. versionchanged:: 2015.8.0\n Added support for SunOS (Solaris 10, Illumos, SmartOS)\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.routes\n '''\n if family != 'inet' and family != 'inet6' and family is not None:\n raise CommandExecutionError('Invalid address family {0}'.format(family))\n\n if __grains__['kernel'] == 'Linux':\n if not salt.utils.path.which('netstat'):\n routes_ = _ip_route_linux()\n else:\n routes_ = _netstat_route_linux()\n elif __grains__['kernel'] == 'SunOS':\n routes_ = _netstat_route_sunos()\n elif __grains__['os'] in ['FreeBSD', 'MacOS', 'Darwin']:\n routes_ = _netstat_route_freebsd()\n elif __grains__['os'] in ['NetBSD']:\n routes_ = _netstat_route_netbsd()\n elif __grains__['os'] in ['OpenBSD']:\n routes_ = _netstat_route_openbsd()\n elif __grains__['os'] in ['AIX']:\n routes_ = _netstat_route_aix()\n else:\n raise CommandExecutionError('Not yet supported on this platform')\n\n if not family:\n return routes_\n else:\n ret = [route for route in routes_ if route['addr_family'] == family]\n return ret", "response": "Return currently configured routes from routing table"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef default_route(family=None):\n '''\n Return default route(s) from routing table\n\n .. versionchanged:: 2015.8.0\n Added support for SunOS (Solaris 10, Illumos, SmartOS)\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.default_route\n '''\n\n if family != 'inet' and family != 'inet6' and family is not None:\n raise CommandExecutionError('Invalid address family {0}'.format(family))\n\n _routes = routes()\n default_route = {}\n if __grains__['kernel'] == 'Linux':\n default_route['inet'] = ['0.0.0.0', 'default']\n default_route['inet6'] = ['::/0', 'default']\n elif __grains__['os'] in ['FreeBSD', 'NetBSD', 'OpenBSD', 'MacOS', 'Darwin'] or \\\n __grains__['kernel'] in ('SunOS', 'AIX'):\n default_route['inet'] = ['default']\n default_route['inet6'] = ['default']\n else:\n raise CommandExecutionError('Not yet supported on this platform')\n\n ret = []\n for route in _routes:\n if family:\n if route['destination'] in default_route[family]:\n if __grains__['kernel'] == 'SunOS' and route['addr_family'] != family:\n continue\n ret.append(route)\n else:\n if route['destination'] in default_route['inet'] or \\\n route['destination'] in default_route['inet6']:\n ret.append(route)\n\n return ret", "response": "Return default route from routing table"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns routing information for given ip", "response": "def get_route(ip):\n '''\n Return routing information for given destination ip\n\n .. versionadded:: 2015.5.3\n\n .. versionchanged:: 2015.8.0\n Added support for SunOS (Solaris 10, Illumos, SmartOS)\n Added support for OpenBSD\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example::\n\n salt '*' network.get_route 10.10.10.10\n '''\n\n if __grains__['kernel'] == 'Linux':\n cmd = 'ip route get {0}'.format(ip)\n out = __salt__['cmd.run'](cmd, python_shell=True)\n regexp = re.compile(r'(via\\s+(?P[\\w\\.:]+))?\\s+dev\\s+(?P[\\w\\.\\:\\-]+)\\s+.*src\\s+(?P[\\w\\.:]+)')\n m = regexp.search(out.splitlines()[0])\n ret = {\n 'destination': ip,\n 'gateway': m.group('gateway'),\n 'interface': m.group('interface'),\n 'source': m.group('source')\n }\n\n return ret\n\n if __grains__['kernel'] == 'SunOS':\n # [root@nacl ~]# route -n get 172.16.10.123\n # route to: 172.16.10.123\n #destination: 172.16.10.0\n # mask: 255.255.255.0\n # interface: net0\n # flags: \n # recvpipe sendpipe ssthresh rtt,ms rttvar,ms hopcount mtu expire\n # 0 0 0 0 0 0 1500 0\n cmd = '/usr/sbin/route -n get {0}'.format(ip)\n out = __salt__['cmd.run'](cmd, python_shell=False)\n\n ret = {\n 'destination': ip,\n 'gateway': None,\n 'interface': None,\n 'source': None\n }\n\n for line in out.splitlines():\n line = line.split(':')\n if 'route to' in line[0]:\n ret['destination'] = line[1].strip()\n if 'gateway' in line[0]:\n ret['gateway'] = line[1].strip()\n if 'interface' in line[0]:\n ret['interface'] = line[1].strip()\n ret['source'] = salt.utils.network.interface_ip(line[1].strip())\n\n return ret\n\n if __grains__['kernel'] == 'OpenBSD':\n # [root@exosphere] route -n get blackdot.be\n # route to: 5.135.127.100\n #destination: default\n # mask: default\n # gateway: 192.168.0.1\n # interface: vio0\n # if address: 192.168.0.2\n # priority: 8 (static)\n # flags: \n # use mtu expire\n # 8352657 0 0\n cmd = 'route -n get {0}'.format(ip)\n out = __salt__['cmd.run'](cmd, python_shell=False)\n\n ret = {\n 'destination': ip,\n 'gateway': None,\n 'interface': None,\n 'source': None\n }\n\n for line in out.splitlines():\n line = line.split(':')\n if 'route to' in line[0]:\n ret['destination'] = line[1].strip()\n if 'gateway' in line[0]:\n ret['gateway'] = line[1].strip()\n if 'interface' in line[0]:\n ret['interface'] = line[1].strip()\n if 'if address' in line[0]:\n ret['source'] = line[1].strip()\n\n return ret\n\n if __grains__['kernel'] == 'AIX':\n # root@la68pp002_pub:~# route -n get 172.29.149.95\n # route to: 172.29.149.95\n #destination: 172.29.149.95\n # gateway: 127.0.0.1\n # interface: lo0\n #interf addr: 127.0.0.1\n # flags: \n #recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire\n # 0 0 0 0 0 0 0 -68642\n cmd = 'route -n get {0}'.format(ip)\n out = __salt__['cmd.run'](cmd, python_shell=False)\n\n ret = {\n 'destination': ip,\n 'gateway': None,\n 'interface': None,\n 'source': None\n }\n\n for line in out.splitlines():\n line = line.split(':')\n if 'route to' in line[0]:\n ret['destination'] = line[1].strip()\n if 'gateway' in line[0]:\n ret['gateway'] = line[1].strip()\n if 'interface' in line[0]:\n ret['interface'] = line[1].strip()\n if 'interf addr' in line[0]:\n ret['source'] = line[1].strip()\n\n return ret\n\n else:\n raise CommandExecutionError('Not yet supported on this platform')"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef ifacestartswith(cidr):\n '''\n Retrieve the interface name from a specific CIDR\n\n .. versionadded:: 2016.11.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.ifacestartswith 10.0\n '''\n net_list = interfaces()\n intfnames = []\n pattern = six.text_type(cidr)\n size = len(pattern)\n for ifname, ifval in six.iteritems(net_list):\n if 'inet' in ifval:\n for inet in ifval['inet']:\n if inet['address'][0:size] == pattern:\n if 'label' in inet:\n intfnames.append(inet['label'])\n else:\n intfnames.append(ifname)\n return intfnames", "response": "Retrieve the interface name from a specific CIDR."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef iphexval(ip):\n '''\n Retrieve the hexadecimal representation of an IP address\n\n .. versionadded:: 2016.11.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' network.iphexval 10.0.0.1\n '''\n a = ip.split('.')\n hexval = ['%02X' % int(x) for x in a] # pylint: disable=E1321\n return ''.join(hexval)", "response": "Return the hexadecimal representation of an IP address"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure CloudWatch event rules exist.", "response": "def present(name, Name=None,\n ScheduleExpression=None,\n EventPattern=None,\n Description=None,\n RoleArn=None,\n State=None,\n Targets=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure trail exists.\n\n name\n The name of the state definition\n\n Name\n Name of the event rule. Defaults to the value of the 'name' param if\n not provided.\n\n ScheduleExpression\n The scheduling expression. For example, ``cron(0 20 * * ? *)``,\n \"rate(5 minutes)\"\n\n EventPattern\n The event pattern.\n\n Description\n A description of the rule\n\n State\n Indicates whether the rule is ENABLED or DISABLED.\n\n RoleArn\n The Amazon Resource Name (ARN) of the IAM role associated with the\n rule.\n\n Targets\n A list of rresources to be invoked when the rule is triggered.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n '''\n ret = {'name': Name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n Name = Name if Name else name\n\n if isinstance(Targets, six.string_types):\n Targets = salt.utils.json.loads(Targets)\n if Targets is None:\n Targets = []\n\n r = __salt__['boto_cloudwatch_event.exists'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile)\n\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to create event rule: {0}.'.format(r['error']['message'])\n return ret\n\n if not r.get('exists'):\n if __opts__['test']:\n ret['comment'] = 'CloudWatch event rule {0} is set to be created.'.format(Name)\n ret['result'] = None\n return ret\n r = __salt__['boto_cloudwatch_event.create_or_update'](Name=Name,\n ScheduleExpression=ScheduleExpression,\n EventPattern=EventPattern,\n Description=Description,\n RoleArn=RoleArn,\n State=State,\n region=region, key=key, keyid=keyid, profile=profile)\n if not r.get('created'):\n ret['result'] = False\n ret['comment'] = 'Failed to create event rule: {0}.'.format(r['error']['message'])\n return ret\n _describe = __salt__['boto_cloudwatch_event.describe'](Name,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in _describe:\n ret['result'] = False\n ret['comment'] = 'Failed to create event rule: {0}.'.format(_describe['error']['message'])\n ret['changes'] = {}\n return ret\n ret['changes']['old'] = {'rule': None}\n ret['changes']['new'] = _describe\n ret['comment'] = 'CloudTrail {0} created.'.format(Name)\n\n if bool(Targets):\n r = __salt__['boto_cloudwatch_event.put_targets'](Rule=Name,\n Targets=Targets,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to create event rule: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n ret['changes']['new']['rule']['Targets'] = Targets\n return ret\n\n ret['comment'] = os.linesep.join([ret['comment'], 'CloudWatch event rule {0} is present.'.format(Name)])\n ret['changes'] = {}\n # trail exists, ensure config matches\n _describe = __salt__['boto_cloudwatch_event.describe'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in _describe:\n ret['result'] = False\n ret['comment'] = 'Failed to update event rule: {0}.'.format(_describe['error']['message'])\n ret['changes'] = {}\n return ret\n _describe = _describe.get('rule')\n\n r = __salt__['boto_cloudwatch_event.list_targets'](Rule=Name,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to update event rule: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n _describe['Targets'] = r.get('targets', [])\n\n need_update = False\n rule_vars = {'ScheduleExpression': 'ScheduleExpression',\n 'EventPattern': 'EventPattern',\n 'Description': 'Description',\n 'RoleArn': 'RoleArn',\n 'State': 'State',\n 'Targets': 'Targets'}\n for invar, outvar in six.iteritems(rule_vars):\n if _describe[outvar] != locals()[invar]:\n need_update = True\n ret['changes'].setdefault('new', {})[invar] = locals()[invar]\n ret['changes'].setdefault('old', {})[invar] = _describe[outvar]\n\n if need_update:\n if __opts__['test']:\n msg = 'CloudWatch event rule {0} set to be modified.'.format(Name)\n ret['comment'] = msg\n ret['result'] = None\n return ret\n\n ret['comment'] = os.linesep.join([ret['comment'], 'CloudWatch event rule to be modified'])\n r = __salt__['boto_cloudwatch_event.create_or_update'](Name=Name,\n ScheduleExpression=ScheduleExpression,\n EventPattern=EventPattern,\n Description=Description,\n RoleArn=RoleArn,\n State=State,\n region=region, key=key, keyid=keyid, profile=profile)\n if not r.get('created'):\n ret['result'] = False\n ret['comment'] = 'Failed to update event rule: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n\n if _describe['Targets'] != Targets:\n removes = [i.get('Id') for i in _describe['Targets']]\n log.error(Targets)\n if bool(Targets):\n for target in Targets:\n tid = target.get('Id', None)\n if tid is not None and tid in removes:\n ix = removes.index(tid)\n removes.pop(ix)\n r = __salt__['boto_cloudwatch_event.put_targets'](Rule=Name,\n Targets=Targets,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to update event rule: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n if bool(removes):\n r = __salt__['boto_cloudwatch_event.remove_targets'](Rule=Name,\n Ids=removes,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to update event rule: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure CloudWatch event rule with passed properties is absent.", "response": "def absent(name, Name=None, region=None, key=None, keyid=None, profile=None):\n '''\n Ensure CloudWatch event rule with passed properties is absent.\n\n name\n The name of the state definition.\n\n Name\n Name of the event rule. Defaults to the value of the 'name' param if\n not provided.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n '''\n\n ret = {'name': Name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n Name = Name if Name else name\n\n r = __salt__['boto_cloudwatch_event.exists'](Name,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to delete event rule: {0}.'.format(r['error']['message'])\n return ret\n\n if r and not r['exists']:\n ret['comment'] = 'CloudWatch event rule {0} does not exist.'.format(Name)\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'CloudWatch event rule {0} is set to be removed.'.format(Name)\n ret['result'] = None\n return ret\n\n # must remove all targets first\n r = __salt__['boto_cloudwatch_event.list_targets'](Rule=Name,\n region=region, key=key,\n keyid=keyid, profile=profile)\n if not r.get('targets'):\n ret['result'] = False\n ret['comment'] = 'Failed to delete event rule: {0}.'.format(r['error']['message'])\n return ret\n ids = [t.get('Id') for t in r['targets']]\n if bool(ids):\n r = __salt__['boto_cloudwatch_event.remove_targets'](Rule=Name, Ids=ids,\n region=region, key=key,\n keyid=keyid, profile=profile)\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to delete event rule: {0}.'.format(r['error']['message'])\n return ret\n if r.get('failures'):\n ret['result'] = False\n ret['comment'] = 'Failed to delete event rule: {0}.'.format(r['failures'])\n return ret\n\n r = __salt__['boto_cloudwatch_event.delete'](Name,\n region=region, key=key,\n keyid=keyid, profile=profile)\n if not r['deleted']:\n ret['result'] = False\n ret['comment'] = 'Failed to delete event rule: {0}.'.format(r['error']['message'])\n return ret\n ret['changes']['old'] = {'rule': Name}\n ret['changes']['new'] = {'rule': None}\n ret['comment'] = 'CloudWatch event rule {0} deleted.'.format(Name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list_nodes(profile, **libcloud_kwargs):\n '''\n Return a list of nodes\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's list_nodes method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.list_nodes profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n nodes = conn.list_nodes(**libcloud_kwargs)\n ret = []\n for node in nodes:\n ret.append(_simple_node(node))\n return ret", "response": "Return a list of nodes in a profile"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a list of node sizes in a given profile", "response": "def list_sizes(profile, location_id=None, **libcloud_kwargs):\n '''\n Return a list of node sizes\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param location_id: The location key, from list_locations\n :type location_id: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's list_sizes method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.list_sizes profile1\n salt myminion libcloud_compute.list_sizes profile1 us-east1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n if location_id is not None:\n locations = [loc for loc in conn.list_locations() if loc.id == location_id]\n if not locations:\n raise ValueError(\"Location not found\")\n else:\n sizes = conn.list_sizes(location=locations[0], **libcloud_kwargs)\n else:\n sizes = conn.list_sizes(**libcloud_kwargs)\n\n ret = []\n for size in sizes:\n ret.append(_simple_size(size))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a list of locations for this cloud", "response": "def list_locations(profile, **libcloud_kwargs):\n '''\n Return a list of locations for this cloud\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's list_locations method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.list_locations profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n locations = conn.list_locations(**libcloud_kwargs)\n\n ret = []\n for loc in locations:\n ret.append(_simple_location(loc))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list_volumes(profile, **libcloud_kwargs):\n '''\n Return a list of storage volumes for this cloud\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's list_volumes method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.list_volumes profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n volumes = conn.list_volumes(**libcloud_kwargs)\n\n ret = []\n for volume in volumes:\n ret.append(_simple_volume(volume))\n return ret", "response": "Return a list of storage volumes for this cloud"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of storage volumes snapshots for this cloud", "response": "def list_volume_snapshots(volume_id, profile, **libcloud_kwargs):\n '''\n Return a list of storage volumes snapshots for this cloud\n\n :param volume_id: The volume identifier\n :type volume_id: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's list_volume_snapshots method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.list_volume_snapshots vol1 profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n volume = _get_by_id(conn.list_volumes(), volume_id)\n snapshots = conn.list_volume_snapshots(volume, **libcloud_kwargs)\n\n ret = []\n for snapshot in snapshots:\n ret.append(_simple_volume_snapshot(snapshot))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_volume(size, name, profile, location_id=None, **libcloud_kwargs):\n '''\n Create a storage volume\n\n :param size: Size of volume in gigabytes (required)\n :type size: ``int``\n\n :param name: Name of the volume to be created\n :type name: ``str``\n\n :param location_id: Which data center to create a volume in. If\n empty, undefined behavior will be selected.\n (optional)\n :type location_id: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's list_volumes method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.create_volume 1000 vol1 profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n if location_id is not None:\n location = _get_by_id(conn.list_locations(), location_id)\n else:\n location = None\n # TODO : Support creating from volume snapshot\n\n volume = conn.create_volume(size, name, location, snapshot=None, **libcloud_kwargs)\n return _simple_volume(volume)", "response": "Create a storage volume in a specific location"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_volume_snapshot(volume_id, profile, name=None, **libcloud_kwargs):\n '''\n Create a storage volume snapshot\n\n :param volume_id: Volume ID from which to create the new\n snapshot.\n :type volume_id: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param name: Name of the snapshot to be created (optional)\n :type name: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's create_volume_snapshot method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.create_volume_snapshot vol1 profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n volume = _get_by_id(conn.list_volumes(), volume_id)\n\n snapshot = conn.create_volume_snapshot(volume, name=name, **libcloud_kwargs)\n return _simple_volume_snapshot(snapshot)", "response": "Create a storage volume snapshot"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef attach_volume(node_id, volume_id, profile, device=None, **libcloud_kwargs):\n '''\n Attaches volume to node.\n\n :param node_id: Node ID to target\n :type node_id: ``str``\n\n :param volume_id: Volume ID from which to attach\n :type volume_id: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param device: Where the device is exposed, e.g. '/dev/sdb'\n :type device: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's attach_volume method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.detach_volume vol1 profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n volume = _get_by_id(conn.list_volumes(), volume_id)\n node = _get_by_id(conn.list_nodes(), node_id)\n return conn.attach_volume(node, volume, device=device, **libcloud_kwargs)", "response": "Attaches a volume to a node."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndetach a volume from a node", "response": "def detach_volume(volume_id, profile, **libcloud_kwargs):\n '''\n Detaches a volume from a node.\n\n :param volume_id: Volume ID from which to detach\n :type volume_id: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's detach_volume method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.detach_volume vol1 profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n volume = _get_by_id(conn.list_volumes(), volume_id)\n return conn.detach_volume(volume, **libcloud_kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef destroy_volume_snapshot(volume_id, snapshot_id, profile, **libcloud_kwargs):\n '''\n Destroy a volume snapshot.\n\n :param volume_id: Volume ID from which the snapshot belongs\n :type volume_id: ``str``\n\n :param snapshot_id: Volume Snapshot ID from which to destroy\n :type snapshot_id: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's destroy_volume_snapshot method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.destroy_volume_snapshot snap1 profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n volume = _get_by_id(conn.list_volumes(), volume_id)\n snapshot = _get_by_id(conn.list_volume_snapshots(volume), snapshot_id)\n return conn.destroy_volume_snapshot(snapshot, **libcloud_kwargs)", "response": "Destroy a volume snapshot"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list_images(profile, location_id=None, **libcloud_kwargs):\n '''\n Return a list of images for this cloud\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param location_id: The location key, from list_locations\n :type location_id: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's list_images method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.list_images profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n if location_id is not None:\n location = _get_by_id(conn.list_locations(), location_id)\n else:\n location = None\n images = conn.list_images(location=location, **libcloud_kwargs)\n\n ret = []\n for image in images:\n ret.append(_simple_image(image))\n return ret", "response": "Return a list of images for this cloud"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_image(node_id, name, profile, description=None, **libcloud_kwargs):\n '''\n Create an image from a node\n\n :param node_id: Node to run the task on.\n :type node_id: ``str``\n\n :param name: name for new image.\n :type name: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param description: description for new image.\n :type description: ``description``\n\n :param libcloud_kwargs: Extra arguments for the driver's create_image method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.create_image server1 my_image profile1\n salt myminion libcloud_compute.create_image server1 my_image profile1 description='test image'\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n node = _get_by_id(conn.list_nodes(), node_id)\n return _simple_image(conn.create_image(node, name, description=description, **libcloud_kwargs))", "response": "Create an image from a node"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndeleting an image of a node", "response": "def delete_image(image_id, profile, **libcloud_kwargs):\n '''\n Delete an image of a node\n\n :param image_id: Image to delete\n :type image_id: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's delete_image method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.delete_image image1 profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n image = _get_by_id(conn.list_images(), image_id)\n return conn.delete_image(image, **libcloud_kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_image(image_id, profile, **libcloud_kwargs):\n '''\n Get an image of a node\n\n :param image_id: Image to fetch\n :type image_id: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's delete_image method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.get_image image1 profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n image = conn.get_image(image_id, **libcloud_kwargs)\n return _simple_image(image)", "response": "Get an image of a node"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef copy_image(source_region, image_id, name, profile, description=None, **libcloud_kwargs):\n '''\n Copies an image from a source region to the current region.\n\n :param source_region: Region to copy the node from.\n :type source_region: ``str``\n\n :param image_id: Image to copy.\n :type image_id: ``str``\n\n :param name: name for new image.\n :type name: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param description: description for new image.\n :type name: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's copy_image method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.copy_image us-east1 image1 'new image' profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n image = conn.get_image(image_id, **libcloud_kwargs)\n new_image = conn.copy_image(source_region, image, name,\n description=description, **libcloud_kwargs)\n return _simple_image(new_image)", "response": "Copy an image from a source region to the current region."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_key_pairs(profile, **libcloud_kwargs):\n '''\n List all the available key pair objects.\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's list_key_pairs method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.list_key_pairs profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n keys = conn.list_key_pairs(**libcloud_kwargs)\n\n ret = []\n for key in keys:\n ret.append(_simple_key_pair(key))\n return ret", "response": "List all the available key pairs for a given profile"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget a single key pair by name", "response": "def get_key_pair(name, profile, **libcloud_kwargs):\n '''\n Get a single key pair by name\n\n :param name: Name of the key pair to retrieve.\n :type name: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's get_key_pair method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.get_key_pair pair1 profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n return _simple_key_pair(conn.get_key_pair(name, **libcloud_kwargs))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef import_key_pair(name, key, profile, key_type=None, **libcloud_kwargs):\n '''\n Import a new public key from string or a file path\n\n :param name: Key pair name.\n :type name: ``str``\n\n :param key: Public key material, the string or a path to a file\n :type key: ``str`` or path ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param key_type: The key pair type, either `FILE` or `STRING`. Will detect if not provided\n and assume that if the string is a path to an existing path it is a FILE, else STRING.\n :type key_type: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's import_key_pair_from_xxx method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.import_key_pair pair1 key_value_data123 profile1\n salt myminion libcloud_compute.import_key_pair pair1 /path/to/key profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n if os.path.exists(key) or key_type == 'FILE':\n return _simple_key_pair(conn.import_key_pair_from_file(name,\n key,\n **libcloud_kwargs))\n else:\n return _simple_key_pair(conn.import_key_pair_from_string(name,\n key,\n **libcloud_kwargs))", "response": "Import a new public key from string or a file path"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndeleting a key pair", "response": "def delete_key_pair(name, profile, **libcloud_kwargs):\n '''\n Delete a key pair\n\n :param name: Key pair name.\n :type name: ``str``\n\n :param profile: The profile key\n :type profile: ``str``\n\n :param libcloud_kwargs: Extra arguments for the driver's import_key_pair_from_xxx method\n :type libcloud_kwargs: ``dict``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion libcloud_compute.delete_key_pair pair1 profile1\n '''\n conn = _get_driver(profile=profile)\n libcloud_kwargs = salt.utils.args.clean_kwargs(**libcloud_kwargs)\n key = conn.get_key_pair(name)\n return conn.delete_key_pair(key, **libcloud_kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_by_id(collection, id):\n '''\n Get item from a list by the id field\n '''\n matches = [item for item in collection if item.id == id]\n if not matches:\n raise ValueError('Could not find a matching item')\n elif len(matches) > 1:\n raise ValueError('The id matched {0} items, not 1'.format(len(matches)))\n return matches[0]", "response": "Get item from a list by the id field\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef monitor(name):\n '''\n Get the summary from module monit and try to see if service is\n being monitored. If not then monitor the service.\n '''\n ret = {'result': None,\n 'name': name,\n 'comment': '',\n 'changes': {}\n }\n result = __salt__['monit.summary'](name)\n\n try:\n for key, value in result.items():\n if 'Running' in value[name]:\n ret['comment'] = ('{0} is being being monitored.').format(name)\n ret['result'] = True\n else:\n if __opts__['test']:\n ret['comment'] = 'Service {0} is set to be monitored.'.format(name)\n ret['result'] = None\n return ret\n __salt__['monit.monitor'](name)\n ret['comment'] = ('{0} started to be monitored.').format(name)\n ret['changes'][name] = 'Running'\n ret['result'] = True\n break\n except KeyError:\n ret['comment'] = ('{0} not found in configuration.').format(name)\n ret['result'] = False\n\n return ret", "response": "Monitor the status of a single resource item in module monit."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef status(jboss_config, host=None, server_config=None):\n '''\n Get status of running jboss instance.\n\n jboss_config\n Configuration dictionary with properties specified above.\n host\n The name of the host. JBoss domain mode only - and required if running in domain mode.\n The host name is the \"name\" attribute of the \"host\" element in host.xml\n server_config\n The name of the Server Configuration. JBoss Domain mode only - and required\n if running in domain mode.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.status '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}'\n\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.status\")\n if host is None and server_config is None:\n operation = ':read-attribute(name=server-state)'\n elif host is not None and server_config is not None:\n operation = '/host=\"{host}\"/server-config=\"{server_config}\"/:read-attribute(name=status)'.format(\n host=host,\n server_config=server_config)\n else:\n raise SaltInvocationError('Invalid parameters. Must either pass both host and server_config or neither')\n return __salt__['jboss7_cli.run_operation'](jboss_config, operation, fail_on_error=False, retries=0)", "response": "Get the status of running JBoss instance."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef stop_server(jboss_config, host=None):\n '''\n Stop running jboss instance\n\n jboss_config\n Configuration dictionary with properties specified above.\n host\n The name of the host. JBoss domain mode only - and required if running in domain mode.\n The host name is the \"name\" attribute of the \"host\" element in host.xml\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.stop_server '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}'\n\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.stop_server\")\n if host is None:\n operation = ':shutdown'\n else:\n operation = '/host=\"{host}\"/:shutdown'.format(host=host)\n shutdown_result = __salt__['jboss7_cli.run_operation'](jboss_config, operation, fail_on_error=False)\n # JBoss seems to occasionaly close the channel immediately when :shutdown is sent\n if shutdown_result['success'] or (not shutdown_result['success'] and 'Operation failed: Channel closed' in shutdown_result['stdout']):\n return shutdown_result\n else:\n raise Exception('''Cannot handle error, return code={retcode}, stdout='{stdout}', stderr='{stderr}' '''.format(**shutdown_result))", "response": "Stop running JBoss instance and return the shutdown result"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreloads running JBoss instance with properties specified above.", "response": "def reload_(jboss_config, host=None):\n '''\n Reload running jboss instance\n\n jboss_config\n Configuration dictionary with properties specified above.\n host\n The name of the host. JBoss domain mode only - and required if running in domain mode.\n The host name is the \"name\" attribute of the \"host\" element in host.xml\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.reload '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}'\n\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.reload\")\n if host is None:\n operation = ':reload'\n else:\n operation = '/host=\"{host}\"/:reload'.format(host=host)\n reload_result = __salt__['jboss7_cli.run_operation'](jboss_config, operation, fail_on_error=False)\n # JBoss seems to occasionaly close the channel immediately when :reload is sent\n if reload_result['success'] or (not reload_result['success'] and\n ('Operation failed: Channel closed' in reload_result['stdout'] or\n 'Communication error: java.util.concurrent.ExecutionException: Operation failed' in reload_result['stdout'])):\n return reload_result\n else:\n raise Exception('''Cannot handle error, return code={retcode}, stdout='{stdout}', stderr='{stderr}' '''.format(**reload_result))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a new datasource in running jboss instance", "response": "def create_datasource(jboss_config, name, datasource_properties, profile=None):\n '''\n Create datasource in running jboss instance\n\n jboss_config\n Configuration dictionary with properties specified above.\n name\n Datasource name\n datasource_properties\n A dictionary of datasource properties to be created:\n - driver-name: mysql\n - connection-url: 'jdbc:mysql://localhost:3306/sampleDatabase'\n - jndi-name: 'java:jboss/datasources/sampleDS'\n - user-name: sampleuser\n - password: secret\n - min-pool-size: 3\n - use-java-context: True\n profile\n The profile name (JBoss domain mode only)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.create_datasource '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}' 'my_datasource' '{\"driver-name\": \"mysql\", \"connection-url\": \"jdbc:mysql://localhost:3306/sampleDatabase\", \"jndi-name\": \"java:jboss/datasources/sampleDS\", \"user-name\": \"sampleuser\", \"password\": \"secret\", \"min-pool-size\": 3, \"use-java-context\": True}'\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.create_datasource, name=%s, profile=%s\", name, profile)\n ds_resource_description = __get_datasource_resource_description(jboss_config, name, profile)\n\n operation = '/subsystem=datasources/data-source=\"{name}\":add({properties})'.format(\n name=name,\n properties=__get_properties_assignment_string(datasource_properties, ds_resource_description)\n )\n if profile is not None:\n operation = '/profile=\"{profile}\"'.format(profile=profile) + operation\n\n return __salt__['jboss7_cli.run_operation'](jboss_config, operation, fail_on_error=False)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nupdates an existing datasource in running jboss instance.", "response": "def update_datasource(jboss_config, name, new_properties, profile=None):\n '''\n Update an existing datasource in running jboss instance.\n If the property doesn't exist if will be created, if it does, it will be updated with the new value\n\n jboss_config\n Configuration dictionary with properties specified above.\n name\n Datasource name\n new_properties\n A dictionary of datasource properties to be updated. For example:\n - driver-name: mysql\n - connection-url: 'jdbc:mysql://localhost:3306/sampleDatabase'\n - jndi-name: 'java:jboss/datasources/sampleDS'\n - user-name: sampleuser\n - password: secret\n - min-pool-size: 3\n - use-java-context: True\n profile\n The profile name (JBoss domain mode only)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.update_datasource '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}' 'my_datasource' '{\"driver-name\": \"mysql\", \"connection-url\": \"jdbc:mysql://localhost:3306/sampleDatabase\", \"jndi-name\": \"java:jboss/datasources/sampleDS\", \"user-name\": \"sampleuser\", \"password\": \"secret\", \"min-pool-size\": 3, \"use-java-context\": True}'\n\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.update_datasource, name=%s, profile=%s\", name, profile)\n ds_result = __read_datasource(jboss_config, name, profile)\n current_properties = ds_result['result']\n diff = dictdiffer.DictDiffer(new_properties, current_properties)\n changed_properties = diff.changed()\n\n ret = {\n 'success': True,\n 'comment': ''\n }\n if changed_properties:\n ds_resource_description = __get_datasource_resource_description(jboss_config, name, profile)\n ds_attributes = ds_resource_description['attributes']\n for key in changed_properties:\n update_result = __update_datasource_property(jboss_config, name, key, new_properties[key], ds_attributes, profile)\n if not update_result['success']:\n ret['result'] = False\n ret['comment'] = ret['comment'] + ('Could not update datasource property {0} with value {1},\\n stdout: {2}\\n'.format(key, new_properties[key], update_result['stdout']))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nread datasource properties in running jboss instance.", "response": "def read_datasource(jboss_config, name, profile=None):\n '''\n Read datasource properties in the running jboss instance.\n\n jboss_config\n Configuration dictionary with properties specified above.\n name\n Datasource name\n profile\n Profile name (JBoss domain mode only)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.read_datasource '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}'\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.read_datasource, name=%s\", name)\n return __read_datasource(jboss_config, name, profile)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a simple jndi binding in the running jboss instance", "response": "def create_simple_binding(jboss_config, binding_name, value, profile=None):\n '''\n Create a simple jndi binding in the running jboss instance\n\n jboss_config\n Configuration dictionary with properties specified above.\n binding_name\n Binding name to be created\n value\n Binding value\n profile\n The profile name (JBoss domain mode only)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.create_simple_binding \\\\\n '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \\\\\n \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}' \\\\\n my_binding_name my_binding_value\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.create_simple_binding, binding_name=%s, value=%s, profile=%s\", binding_name, value, profile)\n operation = '/subsystem=naming/binding=\"{binding_name}\":add(binding-type=simple, value=\"{value}\")'.format(\n binding_name=binding_name,\n value=__escape_binding_value(value)\n )\n if profile is not None:\n operation = '/profile=\"{profile}\"'.format(profile=profile) + operation\n return __salt__['jboss7_cli.run_operation'](jboss_config, operation)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef read_simple_binding(jboss_config, binding_name, profile=None):\n '''\n Read jndi binding in the running jboss instance\n\n jboss_config\n Configuration dictionary with properties specified above.\n binding_name\n Binding name to be created\n profile\n The profile name (JBoss domain mode only)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.read_simple_binding '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}' my_binding_name\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.read_simple_binding, %s\", binding_name)\n return __read_simple_binding(jboss_config, binding_name, profile=profile)", "response": "Read jndi binding in running jboss instance"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef remove_datasource(jboss_config, name, profile=None):\n '''\n Remove an existing datasource from the running jboss instance.\n\n jboss_config\n Configuration dictionary with properties specified above.\n name\n Datasource name\n profile\n The profile (JBoss domain mode only)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.remove_datasource '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}' my_datasource_name\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.remove_datasource, name=%s, profile=%s\", name, profile)\n\n operation = '/subsystem=datasources/data-source={name}:remove'.format(name=name)\n if profile is not None:\n operation = '/profile=\"{profile}\"'.format(profile=profile) + operation\n\n return __salt__['jboss7_cli.run_operation'](jboss_config, operation, fail_on_error=False)", "response": "Remove an existing datasource from the running jboss instance."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndeploy the application on the jboss instance from the local file system", "response": "def deploy(jboss_config, source_file):\n '''\n Deploy the application on the jboss instance from the local file system where minion is running.\n\n jboss_config\n Configuration dictionary with properties specified above.\n source_file\n Source file to deploy from\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.deploy '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}' /opt/deploy_files/my_deploy\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.deploy, source_file=%s\", source_file)\n command = 'deploy {source_file} --force '.format(source_file=source_file)\n return __salt__['jboss7_cli.run_command'](jboss_config, command, fail_on_error=False)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlist all deployments on the instance", "response": "def list_deployments(jboss_config):\n '''\n List all deployments on the jboss instance\n\n jboss_config\n Configuration dictionary with properties specified above.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.list_deployments '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}'\n\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.list_deployments\")\n command_result = __salt__['jboss7_cli.run_command'](jboss_config, 'deploy')\n deployments = []\n if command_result['stdout']:\n deployments = re.split('\\\\s*', command_result['stdout'])\n log.debug('deployments=%s', deployments)\n return deployments"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef undeploy(jboss_config, deployment):\n '''\n Undeploy the application from jboss instance\n\n jboss_config\n Configuration dictionary with properties specified above.\n deployment\n Deployment name to undeploy\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7.undeploy '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}' my_deployment\n '''\n log.debug(\"======================== MODULE FUNCTION: jboss7.undeploy, deployment=%s\", deployment)\n command = 'undeploy {deployment} '.format(deployment=deployment)\n return __salt__['jboss7_cli.run_command'](jboss_config, command)", "response": "Undeploy the application from jboss instance"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the timestamp for a future event that may occur in time_in or at time_at.", "response": "def get_timestamp_at(time_in=None, time_at=None):\n '''\n Computes the timestamp for a future event that may occur in ``time_in`` time\n or at ``time_at``.\n '''\n if time_in:\n if isinstance(time_in, int):\n hours = 0\n minutes = time_in\n else:\n time_in = time_in.replace('h', ':')\n time_in = time_in.replace('m', '')\n try:\n hours, minutes = time_in.split(':')\n except ValueError:\n hours = 0\n minutes = time_in\n if not minutes:\n minutes = 0\n hours, minutes = int(hours), int(minutes)\n dt = timedelta(hours=hours, minutes=minutes)\n time_now = datetime.utcnow()\n time_at = time_now + dt\n return time.mktime(time_at.timetuple())\n elif time_at:\n log.debug('Predicted at specified as %s', time_at)\n if isinstance(time_at, (six.integer_types, float)):\n # then it's a timestamp\n return time_at\n else:\n fmts = ('%H%M', '%Hh%M', '%I%p', '%I:%M%p', '%I:%M %p')\n # Support different formats for the timestamp\n # The current formats accepted are the following:\n #\n # - 18:30 (and 18h30)\n # - 1pm (no minutes, fixed hour)\n # - 1:20am (and 1:20am - with or without space)\n for fmt in fmts:\n try:\n log.debug('Trying to match %s', fmt)\n dt = datetime.strptime(time_at, fmt)\n return time.mktime(dt.timetuple())\n except ValueError:\n log.debug('Did not match %s, continue searching', fmt)\n continue\n msg = '{pat} does not match any of the accepted formats: {fmts}'.format(pat=time_at,\n fmts=', '.join(fmts))\n log.error(msg)\n raise ValueError(msg)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the time in human readable format for a future event that may occur in time_in time or at time_at.", "response": "def get_time_at(time_in=None, time_at=None, out_fmt='%Y-%m-%dT%H:%M:%S'):\n '''\n Return the time in human readable format for a future event that may occur\n in ``time_in`` time, or at ``time_at``.\n '''\n dt = get_timestamp_at(time_in=time_in, time_at=time_at)\n return time.strftime(out_fmt, time.localtime(dt))"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a bool telling whether or path is absolute.", "response": "def _path_is_abs(path):\n '''\n Return a bool telling whether or ``path`` is absolute. If ``path`` is None,\n return ``True``. This function is designed to validate variables which\n optionally contain a file path.\n '''\n if path is None:\n return True\n try:\n return os.path.isabs(path)\n except AttributeError:\n # Non-string data passed\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef extracted(name,\n source,\n source_hash=None,\n source_hash_name=None,\n source_hash_update=False,\n skip_verify=False,\n password=None,\n options=None,\n list_options=None,\n force=False,\n overwrite=False,\n clean=False,\n user=None,\n group=None,\n if_missing=None,\n trim_output=False,\n use_cmd_unzip=None,\n extract_perms=True,\n enforce_toplevel=True,\n enforce_ownership_on=None,\n archive_format=None,\n **kwargs):\n '''\n .. versionadded:: 2014.1.0\n .. versionchanged:: 2016.11.0\n This state has been rewritten. Some arguments are new to this release\n and will not be available in the 2016.3 release cycle (and earlier).\n Additionally, the **ZIP Archive Handling** section below applies\n specifically to the 2016.11.0 release (and newer).\n\n Ensure that an archive is extracted to a specific directory.\n\n .. important::\n **Changes for 2016.11.0**\n\n In earlier releases, this state would rely on the ``if_missing``\n argument to determine whether or not the archive needed to be\n extracted. When this argument was not passed, then the state would just\n assume ``if_missing`` is the same as the ``name`` argument (i.e. the\n parent directory into which the archive would be extracted).\n\n This caused a number of annoyances. One such annoyance was the need to\n know beforehand a path that would result from the extraction of the\n archive, and setting ``if_missing`` to that directory, like so:\n\n .. code-block:: yaml\n\n extract_myapp:\n archive.extracted:\n - name: /var/www\n - source: salt://apps/src/myapp-16.2.4.tar.gz\n - user: www\n - group: www\n - if_missing: /var/www/myapp-16.2.4\n\n If ``/var/www`` already existed, this would effectively make\n ``if_missing`` a required argument, just to get Salt to extract the\n archive.\n\n Some users worked around this by adding the top-level directory of the\n archive to the end of the ``name`` argument, and then used ``--strip``\n or ``--strip-components`` to remove that top-level dir when extracting:\n\n .. code-block:: yaml\n\n extract_myapp:\n archive.extracted:\n - name: /var/www/myapp-16.2.4\n - source: salt://apps/src/myapp-16.2.4.tar.gz\n - user: www\n - group: www\n\n With the rewrite for 2016.11.0, these workarounds are no longer\n necessary. ``if_missing`` is still a supported argument, but it is no\n longer required. The equivalent SLS in 2016.11.0 would be:\n\n .. code-block:: yaml\n\n extract_myapp:\n archive.extracted:\n - name: /var/www\n - source: salt://apps/src/myapp-16.2.4.tar.gz\n - user: www\n - group: www\n\n Salt now uses a function called :py:func:`archive.list\n ` to get a list of files/directories in the\n archive. Using this information, the state can now check the minion to\n see if any paths are missing, and know whether or not the archive needs\n to be extracted. This makes the ``if_missing`` argument unnecessary in\n most use cases.\n\n .. important::\n **ZIP Archive Handling**\n\n *Note: this information applies to 2016.11.0 and later.*\n\n Salt has two different functions for extracting ZIP archives:\n\n 1. :py:func:`archive.unzip `, which uses\n Python's zipfile_ module to extract ZIP files.\n 2. :py:func:`archive.cmd_unzip `, which\n uses the ``unzip`` CLI command to extract ZIP files.\n\n Salt will prefer the use of :py:func:`archive.cmd_unzip\n ` when CLI options are specified (via\n the ``options`` argument), and will otherwise prefer the\n :py:func:`archive.unzip ` function. Use\n of :py:func:`archive.cmd_unzip ` can be\n forced however by setting the ``use_cmd_unzip`` argument to ``True``.\n By contrast, setting this argument to ``False`` will force usage of\n :py:func:`archive.unzip `. For example:\n\n .. code-block:: yaml\n\n /var/www:\n archive.extracted:\n - source: salt://foo/bar/myapp.zip\n - use_cmd_unzip: True\n\n When ``use_cmd_unzip`` is omitted, Salt will choose which extraction\n function to use based on the source archive and the arguments passed to\n the state. When in doubt, simply do not set this argument; it is\n provided as a means of overriding the logic Salt uses to decide which\n function to use.\n\n There are differences in the features available in both extraction\n functions. These are detailed below.\n\n - *Command-line options* (only supported by :py:func:`archive.cmd_unzip\n `) - When the ``options`` argument is\n used, :py:func:`archive.cmd_unzip `\n is the only function that can be used to extract the archive.\n Therefore, if ``use_cmd_unzip`` is specified and set to ``False``,\n and ``options`` is also set, the state will not proceed.\n\n - *Permissions* - Due to an `upstream bug in Python`_, permissions are\n not preserved when the zipfile_ module is used to extract an archive.\n As of the 2016.11.0 release, :py:func:`archive.unzip\n ` (as well as this state) has an\n ``extract_perms`` argument which, when set to ``True`` (the default),\n will attempt to match the permissions of the extracted\n files/directories to those defined within the archive. To disable\n this functionality and have the state not attempt to preserve the\n permissions from the ZIP archive, set ``extract_perms`` to ``False``:\n\n .. code-block:: yaml\n\n /var/www:\n archive.extracted:\n - source: salt://foo/bar/myapp.zip\n - extract_perms: False\n\n .. _`upstream bug in Python`: https://bugs.python.org/issue15795\n\n name\n Directory into which the archive should be extracted\n\n source\n Archive to be extracted\n\n .. note::\n This argument uses the same syntax as its counterpart in the\n :py:func:`file.managed ` state.\n\n source_hash\n Hash of source file, or file with list of hash-to-file mappings\n\n .. note::\n This argument uses the same syntax as its counterpart in the\n :py:func:`file.managed ` state.\n\n .. versionchanged:: 2016.11.0\n If this argument specifies the hash itself, instead of a URI to a\n file containing hashes, the hash type can now be omitted and Salt\n will determine the hash type based on the length of the hash. For\n example, both of the below states are now valid, while before only\n the second one would be:\n\n .. code-block:: yaml\n\n foo_app:\n archive.extracted:\n - name: /var/www\n - source: https://mydomain.tld/foo.tar.gz\n - source_hash: 3360db35e682f1c5f9c58aa307de16d41361618c\n\n bar_app:\n archive.extracted:\n - name: /var/www\n - source: https://mydomain.tld/bar.tar.gz\n - source_hash: sha1=5edb7d584b82ddcbf76e311601f5d4442974aaa5\n\n source_hash_name\n When ``source_hash`` refers to a hash file, Salt will try to find the\n correct hash by matching the filename part of the ``source`` URI. When\n managing a file with a ``source`` of ``salt://files/foo.tar.gz``, then\n the following line in a hash file would match:\n\n .. code-block:: text\n\n acbd18db4cc2f85cedef654fccc4a4d8 foo.tar.gz\n\n This line would also match:\n\n .. code-block:: text\n\n acbd18db4cc2f85cedef654fccc4a4d8 ./dir1/foo.tar.gz\n\n However, sometimes a hash file will include multiple similar paths:\n\n .. code-block:: text\n\n 37b51d194a7513e45b56f6524f2d51f2 ./dir1/foo.txt\n acbd18db4cc2f85cedef654fccc4a4d8 ./dir2/foo.txt\n 73feffa4b7f6bb68e44cf984c85f6e88 ./dir3/foo.txt\n\n In cases like this, Salt may match the incorrect hash. This argument\n can be used to tell Salt which filename to match, to ensure that the\n correct hash is identified. For example:\n\n .. code-block:: yaml\n\n /var/www:\n archive.extracted:\n - source: https://mydomain.tld/dir2/foo.tar.gz\n - source_hash: https://mydomain.tld/hashes\n - source_hash_name: ./dir2/foo.tar.gz\n\n .. note::\n This argument must contain the full filename entry from the\n checksum file, as this argument is meant to disambiguate matches\n for multiple files that have the same basename. So, in the\n example above, simply using ``foo.txt`` would not match.\n\n .. versionadded:: 2016.11.0\n\n source_hash_update : False\n Set this to ``True`` if archive should be extracted if source_hash has\n changed. This would extract regardless of the ``if_missing`` parameter.\n\n Note that this is only checked if the ``source`` value has not changed.\n If it has (e.g. to increment a version number in the path) then the\n archive will not be extracted even if the hash has changed.\n\n .. versionadded:: 2016.3.0\n\n skip_verify : False\n If ``True``, hash verification of remote file sources (``http://``,\n ``https://``, ``ftp://``) will be skipped, and the ``source_hash``\n argument will be ignored.\n\n .. versionadded:: 2016.3.4\n\n keep_source : True\n For ``source`` archives not local to the minion (i.e. from the Salt\n fileserver or a remote source such as ``http(s)`` or ``ftp``), Salt\n will need to download the archive to the minion cache before they can\n be extracted. To remove the downloaded archive after extraction, set\n this argument to ``False``.\n\n .. versionadded:: 2017.7.3\n\n keep : True\n Same as ``keep_source``, kept for backward-compatibility.\n\n .. note::\n If both ``keep_source`` and ``keep`` are used, ``keep`` will be\n ignored.\n\n password\n **For ZIP archives only.** Password used for extraction.\n\n .. versionadded:: 2016.3.0\n .. versionchanged:: 2016.11.0\n The newly-added :py:func:`archive.is_encrypted\n ` function will be used to\n determine if the archive is password-protected. If it is, then the\n ``password`` argument will be required for the state to proceed.\n\n options\n **For tar and zip archives only.** This option can be used to specify\n a string of additional arguments to pass to the tar/zip command.\n\n If this argument is not used, then the minion will attempt to use\n Python's native tarfile_/zipfile_ support to extract it. For zip\n archives, this argument is mostly used to overwrite existing files with\n ``o``.\n\n Using this argument means that the ``tar`` or ``unzip`` command will be\n used, which is less platform-independent, so keep this in mind when\n using this option; the CLI options must be valid options for the\n ``tar``/``unzip`` implementation on the minion's OS.\n\n .. versionadded:: 2016.11.0\n .. versionchanged:: 2015.8.11,2016.3.2\n XZ-compressed tar archives no longer require ``J`` to manually be\n set in the ``options``, they are now detected automatically and\n decompressed using the xz_ CLI command and extracted using ``tar\n xvf``. This is a more platform-independent solution, as not all tar\n implementations support the ``J`` argument for extracting archives.\n\n .. note::\n For tar archives, main operators like ``-x``, ``--extract``,\n ``--get``, ``-c`` and ``-f``/``--file`` should *not* be used here.\n\n list_options\n **For tar archives only.** This state uses :py:func:`archive.list\n ` to discover the contents of the source\n archive so that it knows which file paths should exist on the minion if\n the archive has already been extracted. For the vast majority of tar\n archives, :py:func:`archive.list ` \"just\n works\". Archives compressed using gzip, bzip2, and xz/lzma (with the\n help of the xz_ CLI command) are supported automatically. However, for\n archives compressed using other compression types, CLI options must be\n passed to :py:func:`archive.list `.\n\n This argument will be passed through to :py:func:`archive.list\n ` as its ``options`` argument, to allow it\n to successfully list the archive's contents. For the vast majority of\n archives, this argument should not need to be used, it should only be\n needed in cases where the state fails with an error stating that the\n archive's contents could not be listed.\n\n .. versionadded:: 2016.11.0\n\n force : False\n If a path that should be occupied by a file in the extracted result is\n instead a directory (or vice-versa), the state will fail. Set this\n argument to ``True`` to force these paths to be removed in order to\n allow the archive to be extracted.\n\n .. warning::\n Use this option *very* carefully.\n\n .. versionadded:: 2016.11.0\n\n overwrite : False\n Set this to ``True`` to force the archive to be extracted. This is\n useful for cases where the filenames/directories have not changed, but\n the content of the files have.\n\n .. versionadded:: 2016.11.1\n\n clean : False\n Set this to ``True`` to remove any top-level files and recursively\n remove any top-level directory paths before extracting.\n\n .. note::\n Files will only be cleaned first if extracting the archive is\n deemed necessary, either by paths missing on the minion, or if\n ``overwrite`` is set to ``True``.\n\n .. versionadded:: 2016.11.1\n\n user\n The user to own each extracted file. Not available on Windows.\n\n .. versionadded:: 2015.8.0\n .. versionchanged:: 2016.3.0\n When used in combination with ``if_missing``, ownership will only\n be enforced if ``if_missing`` is a directory.\n .. versionchanged:: 2016.11.0\n Ownership will be enforced only on the file/directory paths found\n by running :py:func:`archive.list ` on\n the source archive. An alternative root directory on which to\n enforce ownership can be specified using the\n ``enforce_ownership_on`` argument.\n\n group\n The group to own each extracted file. Not available on Windows.\n\n .. versionadded:: 2015.8.0\n .. versionchanged:: 2016.3.0\n When used in combination with ``if_missing``, ownership will only\n be enforced if ``if_missing`` is a directory.\n .. versionchanged:: 2016.11.0\n Ownership will be enforced only on the file/directory paths found\n by running :py:func:`archive.list ` on\n the source archive. An alternative root directory on which to\n enforce ownership can be specified using the\n ``enforce_ownership_on`` argument.\n\n if_missing\n If specified, this path will be checked, and if it exists then the\n archive will not be extracted. This path can be either a directory or a\n file, so this option can also be used to check for a semaphore file and\n conditionally skip extraction.\n\n .. versionchanged:: 2016.3.0\n When used in combination with either ``user`` or ``group``,\n ownership will only be enforced when ``if_missing`` is a directory.\n .. versionchanged:: 2016.11.0\n Ownership enforcement is no longer tied to this argument, it is\n simply checked for existence and extraction will be skipped if\n if is present.\n\n trim_output : False\n Useful for archives with many files in them. This can either be set to\n ``True`` (in which case only the first 100 files extracted will be\n in the state results), or it can be set to an integer for more exact\n control over the max number of files to include in the state results.\n\n .. versionadded:: 2016.3.0\n\n use_cmd_unzip : False\n Set to ``True`` for zip files to force usage of the\n :py:func:`archive.cmd_unzip ` function\n to extract.\n\n .. versionadded:: 2016.11.0\n\n extract_perms : True\n **For ZIP archives only.** When using :py:func:`archive.unzip\n ` to extract ZIP archives, Salt works\n around an `upstream bug in Python`_ to set the permissions on extracted\n files/directories to match those encoded into the ZIP archive. Set this\n argument to ``False`` to skip this workaround.\n\n .. versionadded:: 2016.11.0\n\n enforce_toplevel : True\n This option will enforce a single directory at the top level of the\n source archive, to prevent extracting a 'tar-bomb'. Set this argument\n to ``False`` to allow archives with files (or multiple directories) at\n the top level to be extracted.\n\n .. versionadded:: 2016.11.0\n\n enforce_ownership_on\n When ``user`` or ``group`` is specified, Salt will default to enforcing\n permissions on the file/directory paths detected by running\n :py:func:`archive.list ` on the source\n archive. Use this argument to specify an alternate directory on which\n ownership should be enforced.\n\n .. note::\n This path must be within the path specified by the ``name``\n argument.\n\n .. versionadded:: 2016.11.0\n\n archive_format\n One of ``tar``, ``zip``, or ``rar``.\n\n .. versionchanged:: 2016.11.0\n If omitted, the archive format will be guessed based on the value\n of the ``source`` argument. If the minion is running a release\n older than 2016.11.0, this option is required.\n\n .. _tarfile: https://docs.python.org/2/library/tarfile.html\n .. _zipfile: https://docs.python.org/2/library/zipfile.html\n .. _xz: http://tukaani.org/xz/\n\n **Examples**\n\n 1. tar with lmza (i.e. xz) compression:\n\n .. code-block:: yaml\n\n graylog2-server:\n archive.extracted:\n - name: /opt/\n - source: https://github.com/downloads/Graylog2/graylog2-server/graylog2-server-0.9.6p1.tar.lzma\n - source_hash: md5=499ae16dcae71eeb7c3a30c75ea7a1a6\n\n 2. tar archive with flag for verbose output, and enforcement of user/group\n ownership:\n\n .. code-block:: yaml\n\n graylog2-server:\n archive.extracted:\n - name: /opt/\n - source: https://github.com/downloads/Graylog2/graylog2-server/graylog2-server-0.9.6p1.tar.gz\n - source_hash: md5=499ae16dcae71eeb7c3a30c75ea7a1a6\n - options: v\n - user: foo\n - group: foo\n\n 3. tar archive, with ``source_hash_update`` set to ``True`` to prevent\n state from attempting extraction unless the ``source_hash`` differs\n from the previous time the archive was extracted:\n\n .. code-block:: yaml\n\n graylog2-server:\n archive.extracted:\n - name: /opt/\n - source: https://github.com/downloads/Graylog2/graylog2-server/graylog2-server-0.9.6p1.tar.lzma\n - source_hash: md5=499ae16dcae71eeb7c3a30c75ea7a1a6\n - source_hash_update: True\n '''\n ret = {'name': name, 'result': False, 'changes': {}, 'comment': ''}\n\n # Remove pub kwargs as they're irrelevant here.\n kwargs = salt.utils.args.clean_kwargs(**kwargs)\n\n if 'keep_source' in kwargs and 'keep' in kwargs:\n ret.setdefault('warnings', []).append(\n 'Both \\'keep_source\\' and \\'keep\\' were used. Since these both '\n 'do the same thing, \\'keep\\' was ignored.'\n )\n keep_source = bool(kwargs.pop('keep_source'))\n kwargs.pop('keep')\n elif 'keep_source' in kwargs:\n keep_source = bool(kwargs.pop('keep_source'))\n elif 'keep' in kwargs:\n keep_source = bool(kwargs.pop('keep'))\n else:\n # Neither was passed, default is True\n keep_source = True\n\n if not _path_is_abs(name):\n ret['comment'] = '{0} is not an absolute path'.format(name)\n return ret\n else:\n if not name:\n # Empty name, like None, '' etc.\n ret['comment'] = 'Name of the directory path needs to be specified'\n return ret\n # os.path.isfile() returns False when there is a trailing slash, hence\n # our need for first stripping the slash and then adding it back later.\n # Otherwise, we can't properly check if the extraction location both a)\n # exists and b) is a file.\n #\n # >>> os.path.isfile('/tmp/foo.txt')\n # True\n # >>> os.path.isfile('/tmp/foo.txt/')\n # False\n name = name.rstrip(os.sep)\n if os.path.isfile(name):\n ret['comment'] = '{0} exists and is not a directory'.format(name)\n return ret\n # Add back the slash so that file.makedirs properly creates the\n # destdir if it needs to be created. file.makedirs expects a trailing\n # slash in the directory path.\n name += os.sep\n if not _path_is_abs(if_missing):\n ret['comment'] = 'Value for \\'if_missing\\' is not an absolute path'\n return ret\n if not _path_is_abs(enforce_ownership_on):\n ret['comment'] = ('Value for \\'enforce_ownership_on\\' is not an '\n 'absolute path')\n return ret\n else:\n if enforce_ownership_on is not None:\n try:\n not_rel = os.path.relpath(enforce_ownership_on,\n name).startswith('..' + os.sep)\n except Exception:\n # A ValueError is raised on Windows when the paths passed to\n # os.path.relpath are not on the same drive letter. Using a\n # generic Exception here to keep other possible exception types\n # from making this state blow up with a traceback.\n not_rel = True\n if not_rel:\n ret['comment'] = (\n 'Value for \\'enforce_ownership_on\\' must be within {0}'\n .format(name)\n )\n return ret\n\n if if_missing is not None and os.path.exists(if_missing):\n ret['result'] = True\n ret['comment'] = 'Path {0} exists'.format(if_missing)\n return ret\n\n if user or group:\n if salt.utils.platform.is_windows():\n ret['comment'] = \\\n 'User/group ownership cannot be enforced on Windows minions'\n return ret\n\n if user:\n uid = __salt__['file.user_to_uid'](user)\n if uid == '':\n ret['comment'] = 'User {0} does not exist'.format(user)\n return ret\n else:\n uid = -1\n\n if group:\n gid = __salt__['file.group_to_gid'](group)\n if gid == '':\n ret['comment'] = 'Group {0} does not exist'.format(group)\n return ret\n else:\n gid = -1\n else:\n # We should never hit the ownership enforcement code unless user or\n # group was specified, but just in case, set uid/gid to -1 to make the\n # os.chown() a no-op and avoid a NameError.\n uid = gid = -1\n\n if source_hash_update and not source_hash:\n ret.setdefault('warnings', []).append(\n 'The \\'source_hash_update\\' argument is ignored when '\n '\\'source_hash\\' is not also specified.'\n )\n\n try:\n source_match = __salt__['file.source_list'](source,\n source_hash,\n __env__)[0]\n except CommandExecutionError as exc:\n ret['result'] = False\n ret['comment'] = exc.strerror\n return ret\n\n urlparsed_source = _urlparse(source_match)\n urlparsed_scheme = urlparsed_source.scheme\n urlparsed_path = os.path.join(\n urlparsed_source.netloc,\n urlparsed_source.path).rstrip(os.sep)\n\n # urlparsed_scheme will be the drive letter if this is a Windows file path\n # This checks for a drive letter as the scheme and changes it to file\n if urlparsed_scheme and \\\n urlparsed_scheme.lower() in string.ascii_lowercase:\n urlparsed_path = ':'.join([urlparsed_scheme, urlparsed_path])\n urlparsed_scheme = 'file'\n\n source_hash_basename = urlparsed_path or urlparsed_source.netloc\n\n source_is_local = urlparsed_scheme in salt.utils.files.LOCAL_PROTOS\n if source_is_local:\n # Get rid of \"file://\" from start of source_match\n source_match = os.path.realpath(os.path.expanduser(urlparsed_path))\n if not os.path.isfile(source_match):\n ret['comment'] = 'Source file \\'{0}\\' does not exist'.format(\n salt.utils.url.redact_http_basic_auth(source_match))\n return ret\n\n valid_archive_formats = ('tar', 'rar', 'zip')\n if not archive_format:\n archive_format = salt.utils.files.guess_archive_type(source_hash_basename)\n if archive_format is None:\n ret['comment'] = (\n 'Could not guess archive_format from the value of the '\n '\\'source\\' argument. Please set this archive_format to one '\n 'of the following: {0}'.format(', '.join(valid_archive_formats))\n )\n return ret\n try:\n archive_format = archive_format.lower()\n except AttributeError:\n pass\n if archive_format not in valid_archive_formats:\n ret['comment'] = (\n 'Invalid archive_format \\'{0}\\'. Either set it to a supported '\n 'value ({1}) or remove this argument and the archive format will '\n 'be guesseed based on file extension.'.format(\n archive_format,\n ', '.join(valid_archive_formats),\n )\n )\n return ret\n\n if options is not None and not isinstance(options, six.string_types):\n options = six.text_type(options)\n\n strip_components = None\n if options and archive_format == 'tar':\n try:\n strip_components = int(\n re.search(\n r'''--strip(?:-components)?(?:\\s+|=)[\"']?(\\d+)[\"']?''',\n options\n ).group(1)\n )\n except (AttributeError, ValueError):\n pass\n\n if archive_format == 'zip':\n if options:\n if use_cmd_unzip is None:\n log.info(\n 'Presence of CLI options in archive.extracted state for '\n '\\'%s\\' implies that use_cmd_unzip is set to True.', name\n )\n use_cmd_unzip = True\n elif not use_cmd_unzip:\n # use_cmd_unzip explicitly disabled\n ret['comment'] = (\n '\\'use_cmd_unzip\\' cannot be set to False if CLI options '\n 'are being specified (via the \\'options\\' argument). '\n 'Either remove \\'use_cmd_unzip\\', or set it to True.'\n )\n return ret\n if use_cmd_unzip:\n if 'archive.cmd_unzip' not in __salt__:\n ret['comment'] = (\n 'archive.cmd_unzip function not available, unzip might '\n 'not be installed on minion'\n )\n return ret\n if password:\n if use_cmd_unzip is None:\n log.info(\n 'Presence of a password in archive.extracted state for '\n '\\'%s\\' implies that use_cmd_unzip is set to False.', name\n )\n use_cmd_unzip = False\n elif use_cmd_unzip:\n ret.setdefault('warnings', []).append(\n 'Using a password in combination with setting '\n '\\'use_cmd_unzip\\' to True is considered insecure. It is '\n 'recommended to remove the \\'use_cmd_unzip\\' argument (or '\n 'set it to False) and allow Salt to extract the archive '\n 'using Python\\'s built-in ZIP file support.'\n )\n else:\n if password:\n ret['comment'] = \\\n 'The \\'password\\' argument is only supported for zip archives'\n return ret\n\n if archive_format == 'rar':\n if 'archive.unrar' not in __salt__:\n ret['comment'] = (\n 'archive.unrar function not available, rar/unrar might '\n 'not be installed on minion'\n )\n return ret\n\n supports_options = ('tar', 'zip')\n if options and archive_format not in supports_options:\n ret['comment'] = (\n 'The \\'options\\' argument is only compatible with the following '\n 'archive formats: {0}'.format(', '.join(supports_options))\n )\n return ret\n\n if trim_output:\n if trim_output is True:\n trim_output = 100\n elif not isinstance(trim_output, (bool, six.integer_types)):\n try:\n # Try to handle cases where trim_output was passed as a\n # string-ified integer.\n trim_output = int(trim_output)\n except TypeError:\n ret['comment'] = (\n 'Invalid value for trim_output, must be True/False or an '\n 'integer'\n )\n return ret\n\n if source_hash:\n try:\n source_sum = __salt__['file.get_source_sum'](\n source=source_match,\n source_hash=source_hash,\n source_hash_name=source_hash_name,\n saltenv=__env__)\n except CommandExecutionError as exc:\n ret['comment'] = exc.strerror\n return ret\n else:\n source_sum = {}\n\n if source_is_local:\n cached = source_match\n else:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = (\n 'Archive {0} would be cached (if necessary) and checked to '\n 'discover if extraction is needed'.format(\n salt.utils.url.redact_http_basic_auth(source_match)\n )\n )\n return ret\n\n if 'file.cached' not in __states__:\n # Shouldn't happen unless there is a traceback keeping\n # salt/states/file.py from being processed through the loader. If\n # that is the case, we have much more important problems as _all_\n # file states would be unavailable.\n ret['comment'] = (\n 'Unable to cache {0}, file.cached state not available'.format(\n salt.utils.url.redact_http_basic_auth(source_match)\n )\n )\n return ret\n\n try:\n result = __states__['file.cached'](source_match,\n source_hash=source_hash,\n source_hash_name=source_hash_name,\n skip_verify=skip_verify,\n saltenv=__env__)\n except Exception as exc:\n msg = 'Failed to cache {0}: {1}'.format(\n salt.utils.url.redact_http_basic_auth(source_match),\n exc.__str__())\n log.exception(msg)\n ret['comment'] = msg\n return ret\n else:\n log.debug('file.cached: %s', result)\n\n if result['result']:\n # Get the path of the file in the minion cache\n cached = __salt__['cp.is_cached'](source_match, saltenv=__env__)\n else:\n log.debug(\n 'failed to download %s',\n salt.utils.url.redact_http_basic_auth(source_match)\n )\n return result\n\n existing_cached_source_sum = _read_cached_checksum(cached)\n\n if source_hash and source_hash_update and not skip_verify:\n # Create local hash sum file if we're going to track sum update\n _update_checksum(cached)\n\n if archive_format == 'zip' and not password:\n log.debug('Checking %s to see if it is password-protected',\n source_match)\n # Either use_cmd_unzip was explicitly set to True, or was\n # implicitly enabled by setting the \"options\" argument.\n try:\n encrypted_zip = __salt__['archive.is_encrypted'](\n cached,\n clean=False,\n saltenv=__env__)\n except CommandExecutionError:\n # This would happen if archive_format=zip and the source archive is\n # not actually a zip file.\n pass\n else:\n if encrypted_zip:\n ret['comment'] = (\n 'Archive {0} is password-protected, but no password was '\n 'specified. Please set the \\'password\\' argument.'.format(\n salt.utils.url.redact_http_basic_auth(source_match)\n )\n )\n return ret\n\n try:\n contents = __salt__['archive.list'](cached,\n archive_format=archive_format,\n options=list_options,\n strip_components=strip_components,\n clean=False,\n verbose=True)\n except CommandExecutionError as exc:\n contents = None\n errors = []\n if not if_missing:\n errors.append('\\'if_missing\\' must be set')\n if not enforce_ownership_on and (user or group):\n errors.append(\n 'Ownership cannot be managed without setting '\n '\\'enforce_ownership_on\\'.'\n )\n msg = exc.strerror\n if errors:\n msg += '\\n\\n'\n if archive_format == 'tar':\n msg += (\n 'If the source archive is a tar archive compressed using '\n 'a compression type not natively supported by the tar '\n 'command, then setting the \\'list_options\\' argument may '\n 'allow the contents to be listed. Otherwise, if Salt is '\n 'unable to determine the files/directories in the '\n 'archive, the following workaround(s) would need to be '\n 'used for this state to proceed'\n )\n else:\n msg += (\n 'The following workarounds must be used for this state to '\n 'proceed'\n )\n msg += (\n ' (assuming the source file is a valid {0} archive):\\n'\n .format(archive_format)\n )\n\n for error in errors:\n msg += '\\n- {0}'.format(error)\n ret['comment'] = msg\n return ret\n\n if enforce_toplevel and contents is not None \\\n and (len(contents['top_level_dirs']) > 1\n or contents['top_level_files']):\n ret['comment'] = ('Archive does not have a single top-level directory. '\n 'To allow this archive to be extracted, set '\n '\\'enforce_toplevel\\' to False. To avoid a '\n '\\'{0}-bomb\\' it may also be advisable to set a '\n 'top-level directory by adding it to the \\'name\\' '\n 'value (for example, setting \\'name\\' to {1} '\n 'instead of {2}).'.format(\n archive_format,\n os.path.join(name, 'some_dir'),\n name,\n ))\n return ret\n\n extraction_needed = overwrite\n contents_missing = False\n\n # Check to see if we need to extract the archive. Using os.lstat() in a\n # try/except is considerably faster than using os.path.exists(), and we\n # already need to catch an OSError to cover edge cases where the minion is\n # running as a non-privileged user and is trying to check for the existence\n # of a path to which it does not have permission.\n try:\n if_missing_path_exists = os.path.exists(if_missing)\n except TypeError:\n if_missing_path_exists = False\n\n if not if_missing_path_exists:\n if contents is None:\n try:\n os.lstat(if_missing)\n extraction_needed = False\n except OSError as exc:\n if exc.errno == errno.ENOENT:\n extraction_needed = True\n else:\n ret['comment'] = (\n 'Failed to check for existence of if_missing path '\n '({0}): {1}'.format(if_missing, exc.__str__())\n )\n return ret\n else:\n incorrect_type = []\n for path_list, func in \\\n ((contents['dirs'], stat.S_ISDIR),\n (contents['files'], lambda x: not stat.S_ISLNK(x)\n and not stat.S_ISDIR(x)),\n (contents['links'], stat.S_ISLNK)):\n for path in path_list:\n full_path = salt.utils.path.join(name, path)\n try:\n path_mode = os.lstat(full_path.rstrip(os.sep)).st_mode\n if not func(path_mode):\n incorrect_type.append(path)\n except OSError as exc:\n if exc.errno == errno.ENOENT:\n extraction_needed = True\n contents_missing = True\n elif exc.errno != errno.ENOTDIR:\n # In cases where a directory path was occupied by a\n # file instead, all os.lstat() calls to files within\n # that dir will raise an ENOTDIR OSError. So we\n # expect these and will only abort here if the\n # error code is something else.\n ret['comment'] = exc.__str__()\n return ret\n\n if incorrect_type:\n incorrect_paths = '\\n\\n' + '\\n'.join(\n ['- {0}'.format(x) for x in incorrect_type]\n )\n ret['comment'] = (\n 'The below paths (relative to {0}) exist, but are the '\n 'incorrect type (file instead of directory, symlink '\n 'instead of file, etc.).'.format(name)\n )\n if __opts__['test'] and clean and contents is not None:\n ret['result'] = None\n ret['comment'] += (\n ' Since the \\'clean\\' option is enabled, the '\n 'destination paths would be cleared and the '\n 'archive would be extracted.{0}'.format(\n incorrect_paths\n )\n )\n return ret\n\n # Skip notices of incorrect types if we're cleaning\n if not (clean and contents is not None):\n if not force:\n ret['comment'] += (\n ' To proceed with extraction, set \\'force\\' to '\n 'True. Note that this will remove these paths '\n 'before extracting.{0}'.format(incorrect_paths)\n )\n return ret\n else:\n errors = []\n for path in incorrect_type:\n full_path = os.path.join(name, path)\n try:\n salt.utils.files.rm_rf(full_path.rstrip(os.sep))\n ret['changes'].setdefault(\n 'removed', []).append(full_path)\n extraction_needed = True\n except OSError as exc:\n if exc.errno != errno.ENOENT:\n errors.append(exc.__str__())\n if errors:\n msg = (\n 'One or more paths existed by were the incorrect '\n 'type (i.e. file instead of directory or '\n 'vice-versa), but could not be removed. The '\n 'following errors were observed:\\n'\n )\n for error in errors:\n msg += '\\n- {0}'.format(error)\n ret['comment'] = msg\n return ret\n\n if not extraction_needed \\\n and source_hash_update \\\n and existing_cached_source_sum is not None \\\n and not _compare_checksum(cached, existing_cached_source_sum):\n extraction_needed = True\n source_hash_trigger = True\n else:\n source_hash_trigger = False\n\n created_destdir = False\n\n if extraction_needed:\n if source_is_local and source_hash and not skip_verify:\n ret['result'] = __salt__['file.check_hash'](source_match, source_sum['hsum'])\n if not ret['result']:\n ret['comment'] = \\\n '{0} does not match the desired source_hash {1}'.format(\n salt.utils.url.redact_http_basic_auth(source_match),\n source_sum['hsum']\n )\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = \\\n 'Archive {0} would be extracted to {1}'.format(\n salt.utils.url.redact_http_basic_auth(source_match),\n name\n )\n if clean and contents is not None:\n ret['comment'] += ', after cleaning destination path(s)'\n _add_explanation(ret, source_hash_trigger, contents_missing)\n return ret\n\n if clean and contents is not None:\n errors = []\n log.debug('Cleaning archive paths from within %s', name)\n for path in contents['top_level_dirs'] + contents['top_level_files']:\n full_path = os.path.join(name, path)\n try:\n log.debug('Removing %s', full_path)\n salt.utils.files.rm_rf(full_path.rstrip(os.sep))\n ret['changes'].setdefault(\n 'removed', []).append(full_path)\n except OSError as exc:\n if exc.errno != errno.ENOENT:\n errors.append(exc.__str__())\n\n if errors:\n msg = (\n 'One or more paths could not be cleaned. The following '\n 'errors were observed:\\n'\n )\n for error in errors:\n msg += '\\n- {0}'.format(error)\n ret['comment'] = msg\n return ret\n\n if not os.path.isdir(name):\n __states__['file.directory'](name, user=user, makedirs=True)\n created_destdir = True\n\n log.debug('Extracting %s to %s', cached, name)\n try:\n if archive_format == 'zip':\n if use_cmd_unzip:\n try:\n files = __salt__['archive.cmd_unzip'](\n cached,\n name,\n options=options,\n trim_output=trim_output,\n password=password,\n **kwargs)\n except (CommandExecutionError, CommandNotFoundError) as exc:\n ret['comment'] = exc.strerror\n return ret\n else:\n files = __salt__['archive.unzip'](cached,\n name,\n options=options,\n trim_output=trim_output,\n password=password,\n extract_perms=extract_perms,\n **kwargs)\n elif archive_format == 'rar':\n try:\n files = __salt__['archive.unrar'](cached,\n name,\n trim_output=trim_output,\n **kwargs)\n except (CommandExecutionError, CommandNotFoundError) as exc:\n ret['comment'] = exc.strerror\n return ret\n else:\n if options is None:\n try:\n with closing(tarfile.open(cached, 'r')) as tar:\n tar.extractall(salt.utils.stringutils.to_str(name))\n files = tar.getnames()\n if trim_output:\n files = files[:trim_output]\n except tarfile.ReadError:\n if salt.utils.path.which('xz'):\n if __salt__['cmd.retcode'](\n ['xz', '-t', cached],\n python_shell=False,\n ignore_retcode=True) == 0:\n # XZ-compressed data\n log.debug(\n 'Tar file is XZ-compressed, attempting '\n 'decompression and extraction using XZ Utils '\n 'and the tar command'\n )\n # Must use python_shell=True here because not\n # all tar implementations support the -J flag\n # for decompressing XZ-compressed data. We need\n # to dump the decompressed data to stdout and\n # pipe it to tar for extraction.\n cmd = 'xz --decompress --stdout {0} | tar xvf -'\n results = __salt__['cmd.run_all'](\n cmd.format(_cmd_quote(cached)),\n cwd=name,\n python_shell=True)\n if results['retcode'] != 0:\n if created_destdir:\n _cleanup_destdir(name)\n ret['result'] = False\n ret['changes'] = results\n return ret\n if _is_bsdtar():\n files = results['stderr']\n else:\n files = results['stdout']\n else:\n # Failed to open tar archive and it is not\n # XZ-compressed, gracefully fail the state\n if created_destdir:\n _cleanup_destdir(name)\n ret['result'] = False\n ret['comment'] = (\n 'Failed to read from tar archive using '\n 'Python\\'s native tar file support. If '\n 'archive is compressed using something '\n 'other than gzip or bzip2, the '\n '\\'options\\' argument may be required to '\n 'pass the correct options to the tar '\n 'command in order to extract the archive.'\n )\n return ret\n else:\n if created_destdir:\n _cleanup_destdir(name)\n ret['result'] = False\n ret['comment'] = (\n 'Failed to read from tar archive. If it is '\n 'XZ-compressed, install xz-utils to attempt '\n 'extraction.'\n )\n return ret\n else:\n if not salt.utils.path.which('tar'):\n ret['comment'] = (\n 'tar command not available, it might not be '\n 'installed on minion'\n )\n return ret\n\n tar_opts = shlex.split(options)\n\n tar_cmd = ['tar']\n tar_shortopts = 'x'\n tar_longopts = []\n\n for position, opt in enumerate(tar_opts):\n if opt.startswith('-'):\n tar_longopts.append(opt)\n else:\n if position > 0:\n tar_longopts.append(opt)\n else:\n append_opt = opt\n append_opt = append_opt.replace('x', '')\n append_opt = append_opt.replace('f', '')\n tar_shortopts = tar_shortopts + append_opt\n\n if __grains__['os'].lower() == 'openbsd':\n tar_shortopts = '-' + tar_shortopts\n\n tar_cmd.append(tar_shortopts)\n tar_cmd.extend(tar_longopts)\n tar_cmd.extend(['-f', cached])\n\n results = __salt__['cmd.run_all'](tar_cmd,\n cwd=name,\n python_shell=False)\n if results['retcode'] != 0:\n ret['result'] = False\n ret['changes'] = results\n return ret\n if _is_bsdtar():\n files = results['stderr']\n else:\n files = results['stdout']\n if not files:\n files = 'no tar output so far'\n except CommandExecutionError as exc:\n ret['comment'] = exc.strerror\n return ret\n\n # Recursively set user and group ownership of files\n enforce_missing = []\n enforce_failed = []\n if user or group:\n if enforce_ownership_on:\n if os.path.isdir(enforce_ownership_on):\n enforce_dirs = [enforce_ownership_on]\n enforce_files = []\n enforce_links = []\n else:\n enforce_dirs = []\n enforce_files = [enforce_ownership_on]\n enforce_links = []\n else:\n if contents is not None:\n enforce_dirs = contents['top_level_dirs']\n enforce_files = contents['top_level_files']\n enforce_links = contents['top_level_links']\n\n recurse = []\n if user:\n recurse.append('user')\n if group:\n recurse.append('group')\n recurse_str = ', '.join(recurse)\n\n owner_changes = dict([\n (x, y) for x, y in (('user', user), ('group', group)) if y\n ])\n for dirname in enforce_dirs:\n full_path = os.path.join(name, dirname)\n if not os.path.isdir(full_path):\n if not __opts__['test']:\n enforce_missing.append(full_path)\n else:\n log.debug(\n 'Enforcing %s ownership on %s using a file.directory state%s',\n recurse_str,\n dirname,\n ' (dry-run only)' if __opts__['test'] else ''\n )\n dir_result = __states__['file.directory'](full_path,\n user=user,\n group=group,\n recurse=recurse)\n log.debug('file.directory: %s', dir_result)\n\n if dir_result.get('changes'):\n ret['changes']['updated ownership'] = True\n try:\n if not dir_result['result']:\n enforce_failed.append(full_path)\n except (KeyError, TypeError):\n log.warning(\n 'Bad state return %s for file.directory state on %s',\n dir_result, dirname\n )\n\n for filename in enforce_files + enforce_links:\n full_path = os.path.join(name, filename)\n try:\n # Using os.lstat instead of calling out to\n # __salt__['file.stats'], since we may be doing this for a lot\n # of files, and simply calling os.lstat directly will speed\n # things up a bit.\n file_stat = os.lstat(full_path)\n except OSError as exc:\n if not __opts__['test']:\n if exc.errno == errno.ENOENT:\n enforce_missing.append(full_path)\n enforce_failed.append(full_path)\n else:\n # Earlier we set uid, gid to -1 if we're not enforcing\n # ownership on user, group, as passing -1 to os.chown will tell\n # it not to change that ownership. Since we've done that, we\n # can selectively compare the uid/gid from the values in\n # file_stat, _only if_ the \"desired\" uid/gid is something other\n # than -1.\n if (uid != -1 and uid != file_stat.st_uid) \\\n or (gid != -1 and gid != file_stat.st_gid):\n if __opts__['test']:\n ret['changes']['updated ownership'] = True\n else:\n try:\n os.lchown(full_path, uid, gid)\n ret['changes']['updated ownership'] = True\n except OSError:\n enforce_failed.append(filename)\n\n if extraction_needed:\n if files:\n if created_destdir:\n ret['changes']['directories_created'] = [name]\n ret['changes']['extracted_files'] = files\n ret['comment'] = '{0} extracted to {1}'.format(\n salt.utils.url.redact_http_basic_auth(source_match),\n name,\n )\n _add_explanation(ret, source_hash_trigger, contents_missing)\n ret['result'] = True\n\n else:\n ret['result'] = False\n ret['comment'] = 'No files were extracted from {0}'.format(\n salt.utils.url.redact_http_basic_auth(source_match)\n )\n else:\n ret['result'] = True\n if if_missing_path_exists:\n ret['comment'] = '{0} exists'.format(if_missing)\n else:\n ret['comment'] = 'All files in archive are already present'\n if __opts__['test']:\n if ret['changes'].get('updated ownership'):\n ret['result'] = None\n ret['comment'] += (\n '. Ownership would be updated on one or more '\n 'files/directories.'\n )\n\n if enforce_missing:\n if not if_missing:\n # If is_missing was used, and both a) the archive had never been\n # extracted, and b) the path referred to by if_missing exists, then\n # enforce_missing would contain paths of top_level dirs/files that\n # _would_ have been extracted. Since if_missing can be used as a\n # semaphore to conditionally extract, we don't want to make this a\n # case where the state fails, so we only fail the state if\n # is_missing is not used.\n ret['result'] = False\n ret['comment'] += (\n '\\n\\nWhile trying to enforce user/group ownership, the following '\n 'paths were missing:\\n'\n )\n for item in enforce_missing:\n ret['comment'] += '\\n- {0}'.format(item)\n\n if enforce_failed:\n ret['result'] = False\n ret['comment'] += (\n '\\n\\nWhile trying to enforce user/group ownership, Salt was '\n 'unable to change ownership on the following paths:\\n'\n )\n for item in enforce_failed:\n ret['comment'] += '\\n- {0}'.format(item)\n\n if not source_is_local:\n if keep_source:\n log.debug('Keeping cached source file %s', cached)\n else:\n log.debug('Cleaning cached source file %s', cached)\n result = __states__['file.not_cached'](source_match, saltenv=__env__)\n if not result['result']:\n # Don't let failure to delete cached file cause the state\n # itself to fail, just drop it in the warnings.\n ret.setdefault('warnings', []).append(result['comment'])\n\n return ret", "response": "A state machine that extracts a new archive into a specific directory."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the status of all the firewall profiles on the system", "response": "def get_config():\n '''\n Get the status of all the firewall profiles\n\n Returns:\n dict: A dictionary of all profiles on the system\n\n Raises:\n CommandExecutionError: If the command fails\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' firewall.get_config\n '''\n profiles = {}\n curr = None\n\n cmd = ['netsh', 'advfirewall', 'show', 'allprofiles']\n ret = __salt__['cmd.run_all'](cmd, python_shell=False, ignore_retcode=True)\n if ret['retcode'] != 0:\n raise CommandExecutionError(ret['stdout'])\n\n # There may be some problems with this depending on how `netsh` is localized\n # It's looking for lines that contain `Profile Settings` or start with\n # `State` which may be different in different localizations\n for line in ret['stdout'].splitlines():\n if not curr:\n tmp = re.search('(.*) Profile Settings:', line)\n if tmp:\n curr = tmp.group(1)\n elif line.startswith('State'):\n profiles[curr] = line.split()[1] == 'ON'\n curr = None\n\n return profiles"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nenable the is firewall profile isn t enabled", "response": "def enable(profile='allprofiles'):\n '''\n .. versionadded:: 2015.5.0\n\n Enable firewall profile\n\n Args:\n profile (Optional[str]): The name of the profile to enable. Default is\n ``allprofiles``. Valid options are:\n\n - allprofiles\n - domainprofile\n - privateprofile\n - publicprofile\n\n Returns:\n bool: True if successful\n\n Raises:\n CommandExecutionError: If the command fails\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' firewall.enable\n '''\n cmd = ['netsh', 'advfirewall', 'set', profile, 'state', 'on']\n ret = __salt__['cmd.run_all'](cmd, python_shell=False, ignore_retcode=True)\n if ret['retcode'] != 0:\n raise CommandExecutionError(ret['stdout'])\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndisplaying all matching rules as specified by name", "response": "def get_rule(name='all'):\n '''\n .. versionadded:: 2015.5.0\n\n Display all matching rules as specified by name\n\n Args:\n name (Optional[str]): The full name of the rule. ``all`` will return all\n rules. Default is ``all``\n\n Returns:\n dict: A dictionary of all rules or rules that match the name exactly\n\n Raises:\n CommandExecutionError: If the command fails\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' firewall.get_rule 'MyAppPort'\n '''\n cmd = ['netsh', 'advfirewall', 'firewall', 'show', 'rule',\n 'name={0}'.format(name)]\n ret = __salt__['cmd.run_all'](cmd, python_shell=False, ignore_retcode=True)\n if ret['retcode'] != 0:\n raise CommandExecutionError(ret['stdout'])\n\n return {name: ret['stdout']}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef add_rule(name, localport, protocol='tcp', action='allow', dir='in',\n remoteip='any'):\n '''\n .. versionadded:: 2015.5.0\n\n Add a new inbound or outbound rule to the firewall policy\n\n Args:\n\n name (str): The name of the rule. Must be unique and cannot be \"all\".\n Required.\n\n localport (int): The port the rule applies to. Must be a number between\n 0 and 65535. Can be a range. Can specify multiple ports separated by\n commas. Required.\n\n protocol (Optional[str]): The protocol. Can be any of the following:\n\n - A number between 0 and 255\n - icmpv4\n - icmpv6\n - tcp\n - udp\n - any\n\n action (Optional[str]): The action the rule performs. Can be any of the\n following:\n\n - allow\n - block\n - bypass\n\n dir (Optional[str]): The direction. Can be ``in`` or ``out``.\n\n remoteip (Optional [str]): The remote IP. Can be any of the following:\n\n - any\n - localsubnet\n - dns\n - dhcp\n - wins\n - defaultgateway\n - Any valid IPv4 address (192.168.0.12)\n - Any valid IPv6 address (2002:9b3b:1a31:4:208:74ff:fe39:6c43)\n - Any valid subnet (192.168.1.0/24)\n - Any valid range of IP addresses (192.168.0.1-192.168.0.12)\n - A list of valid IP addresses\n\n Can be combinations of the above separated by commas.\n\n Returns:\n bool: True if successful\n\n Raises:\n CommandExecutionError: If the command fails\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' firewall.add_rule 'test' '8080' 'tcp'\n salt '*' firewall.add_rule 'test' '1' 'icmpv4'\n salt '*' firewall.add_rule 'test_remote_ip' '8000' 'tcp' 'allow' 'in' '192.168.0.1'\n '''\n cmd = ['netsh', 'advfirewall', 'firewall', 'add', 'rule',\n 'name={0}'.format(name),\n 'protocol={0}'.format(protocol),\n 'dir={0}'.format(dir),\n 'action={0}'.format(action),\n 'remoteip={0}'.format(remoteip)]\n\n if protocol is None \\\n or ('icmpv4' not in protocol and 'icmpv6' not in protocol):\n cmd.append('localport={0}'.format(localport))\n\n ret = __salt__['cmd.run_all'](cmd, python_shell=False, ignore_retcode=True)\n if ret['retcode'] != 0:\n raise CommandExecutionError(ret['stdout'])\n\n return True", "response": "Adds a new rule to the firewall policy."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete_rule(name=None,\n localport=None,\n protocol=None,\n dir=None,\n remoteip=None):\n '''\n .. versionadded:: 2015.8.0\n\n Delete an existing firewall rule identified by name and optionally by ports,\n protocols, direction, and remote IP.\n\n Args:\n\n name (str): The name of the rule to delete. If the name ``all`` is used\n you must specify additional parameters.\n\n localport (Optional[str]): The port of the rule. If protocol is not\n specified, protocol will be set to ``tcp``\n\n protocol (Optional[str]): The protocol of the rule. Default is ``tcp``\n when ``localport`` is specified\n\n dir (Optional[str]): The direction of the rule.\n\n remoteip (Optional[str]): The remote IP of the rule.\n\n Returns:\n bool: True if successful\n\n Raises:\n CommandExecutionError: If the command fails\n\n CLI Example:\n\n .. code-block:: bash\n\n # Delete incoming tcp port 8080 in the rule named 'test'\n salt '*' firewall.delete_rule 'test' '8080' 'tcp' 'in'\n\n # Delete the incoming tcp port 8000 from 192.168.0.1 in the rule named\n # 'test_remote_ip'\n salt '*' firewall.delete_rule 'test_remote_ip' '8000' 'tcp' 'in' '192.168.0.1'\n\n # Delete all rules for local port 80:\n salt '*' firewall.delete_rule all 80 tcp\n\n # Delete a rule called 'allow80':\n salt '*' firewall.delete_rule allow80\n '''\n cmd = ['netsh', 'advfirewall', 'firewall', 'delete', 'rule']\n if name:\n cmd.append('name={0}'.format(name))\n if protocol:\n cmd.append('protocol={0}'.format(protocol))\n if dir:\n cmd.append('dir={0}'.format(dir))\n if remoteip:\n cmd.append('remoteip={0}'.format(remoteip))\n\n if protocol is None \\\n or ('icmpv4' not in protocol and 'icmpv6' not in protocol):\n if localport:\n if not protocol:\n cmd.append('protocol=tcp')\n cmd.append('localport={0}'.format(localport))\n\n ret = __salt__['cmd.run_all'](cmd, python_shell=False, ignore_retcode=True)\n if ret['retcode'] != 0:\n raise CommandExecutionError(ret['stdout'])\n\n return True", "response": "Delete an existing firewall rule identified by name ports protocol direction and remote IP."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the firewall properties for the specified profile section and store.", "response": "def get_settings(profile, section, store='local'):\n '''\n Get the firewall property from the specified profile in the specified store\n as returned by ``netsh advfirewall``.\n\n .. versionadded:: 2018.3.4\n .. versionadded:: 2019.2.0\n\n Args:\n\n profile (str):\n The firewall profile to query. Valid options are:\n\n - domain\n - public\n - private\n\n section (str):\n The property to query within the selected profile. Valid options\n are:\n\n - firewallpolicy : inbound/outbound behavior\n - logging : firewall logging settings\n - settings : firewall properties\n - state : firewalls state (on | off)\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n dict: A dictionary containing the properties for the specified profile\n\n Raises:\n CommandExecutionError: If an error occurs\n ValueError: If the parameters are incorrect\n\n CLI Example:\n\n .. code-block:: bash\n\n # Get the inbound/outbound firewall settings for connections on the\n # local domain profile\n salt * win_firewall.get_settings domain firewallpolicy\n\n # Get the inbound/outbound firewall settings for connections on the\n # domain profile as defined by local group policy\n salt * win_firewall.get_settings domain firewallpolicy lgpo\n '''\n return salt.utils.win_lgpo_netsh.get_settings(profile=profile,\n section=section,\n store=store)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget all the properties for the specified firewall profile in the specified store", "response": "def get_all_settings(domain, store='local'):\n '''\n Gets all the properties for the specified profile in the specified store\n\n .. versionadded:: 2018.3.4\n .. versionadded:: 2019.2.0\n\n Args:\n\n profile (str):\n The firewall profile to query. Valid options are:\n\n - domain\n - public\n - private\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n dict: A dictionary containing the specified settings\n\n CLI Example:\n\n .. code-block:: bash\n\n # Get all firewall settings for connections on the domain profile\n salt * win_firewall.get_all_settings domain\n\n # Get all firewall settings for connections on the domain profile as\n # defined by local group policy\n salt * win_firewall.get_all_settings domain lgpo\n '''\n return salt.utils.win_lgpo_netsh.get_all_settings(profile=domain,\n store=store)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the firewall inbound and outbound settings for the specified profile and store.", "response": "def set_firewall_settings(profile, inbound=None, outbound=None, store='local'):\n '''\n Set the firewall inbound/outbound settings for the specified profile and\n store\n\n .. versionadded:: 2018.3.4\n .. versionadded:: 2019.2.0\n\n Args:\n\n profile (str):\n The firewall profile to query. Valid options are:\n\n - domain\n - public\n - private\n\n inbound (str):\n The inbound setting. If ``None`` is passed, the setting will remain\n unchanged. Valid values are:\n\n - blockinbound\n - blockinboundalways\n - allowinbound\n - notconfigured\n\n Default is ``None``\n\n outbound (str):\n The outbound setting. If ``None`` is passed, the setting will remain\n unchanged. Valid values are:\n\n - allowoutbound\n - blockoutbound\n - notconfigured\n\n Default is ``None``\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n bool: ``True`` if successful\n\n Raises:\n CommandExecutionError: If an error occurs\n ValueError: If the parameters are incorrect\n\n CLI Example:\n\n .. code-block:: bash\n\n # Set the inbound setting for the domain profile to block inbound\n # connections\n salt * firewall.set_firewall_settings domain='domain' inbound='blockinbound'\n\n # Set the outbound setting for the domain profile to allow outbound\n # connections\n salt * firewall.set_firewall_settings domain='domain' outbound='allowoutbound'\n\n # Set inbound/outbound settings for the domain profile in the group\n # policy to block inbound and allow outbound\n salt * firewall.set_firewall_settings domain='domain' inbound='blockinbound' outbound='allowoutbound' store='lgpo'\n '''\n return salt.utils.win_lgpo_netsh.set_firewall_settings(profile=profile,\n inbound=inbound,\n outbound=outbound,\n store=store)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_settings(profile, setting, value, store='local'):\n '''\n Configure firewall settings.\n\n .. versionadded:: 2018.3.4\n .. versionadded:: 2019.2.0\n\n Args:\n\n profile (str):\n The firewall profile to configure. Valid options are:\n\n - domain\n - public\n - private\n\n setting (str):\n The firewall setting to configure. Valid options are:\n\n - localfirewallrules\n - localconsecrules\n - inboundusernotification\n - remotemanagement\n - unicastresponsetomulticast\n\n value (str):\n The value to apply to the setting. Valid options are\n\n - enable\n - disable\n - notconfigured\n\n .. note::\n ``notconfigured`` can only be used when using the lgpo store\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n bool: ``True`` if successful\n\n Raises:\n CommandExecutionError: If an error occurs\n ValueError: If the parameters are incorrect\n\n CLI Example:\n\n .. code-block:: bash\n\n # Merge local rules with those distributed through group policy\n salt * firewall.set_settings domain localfirewallrules enable\n\n # Allow remote management of Windows Firewall\n salt * firewall.set_settings domain remotemanagement enable\n '''\n return salt.utils.win_lgpo_netsh.set_settings(profile=profile,\n setting=setting,\n value=value,\n store=store)", "response": "Configure the firewall settings for the specified profile and store."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_state(profile, state, store='local'):\n '''\n Configure the firewall state.\n\n .. versionadded:: 2018.3.4\n .. versionadded:: 2019.2.0\n\n Args:\n\n profile (str):\n The firewall profile to configure. Valid options are:\n\n - domain\n - public\n - private\n\n state (str):\n The firewall state. Valid options are:\n\n - on\n - off\n - notconfigured\n\n .. note::\n ``notconfigured`` can only be used when using the lgpo store\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n bool: ``True`` if successful\n\n Raises:\n CommandExecutionError: If an error occurs\n ValueError: If the parameters are incorrect\n\n CLI Example:\n\n .. code-block:: bash\n\n # Turn the firewall off when the domain profile is active\n salt * firewall.set_state domain off\n\n # Turn the firewall on when the public profile is active and set that in\n # the local group policy\n salt * firewall.set_state public on lgpo\n '''\n return salt.utils.win_lgpo_netsh.set_state(profile=profile,\n state=state,\n store=store)", "response": "Configure the firewall state."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the error line and error message output from the error message a stacktrace.", "response": "def _get_jinja_error(trace, context=None):\n '''\n Return the error line and error message output from\n a stacktrace.\n If we are in a macro, also output inside the message the\n exact location of the error in the macro\n '''\n if not context:\n context = {}\n out = ''\n error = _get_jinja_error_slug(trace)\n line = _get_jinja_error_line(trace)\n msg = _get_jinja_error_message(trace)\n # if we failed on a nested macro, output a little more info\n # to help debugging\n # if sls is not found in context, add output only if we can\n # resolve the filename\n add_log = False\n template_path = None\n if 'sls' not in context:\n if (\n (error[0] != '')\n and os.path.exists(error[0])\n ):\n template_path = error[0]\n add_log = True\n else:\n # the offender error is not from the called sls\n filen = context['sls'].replace('.', '/')\n if (\n not error[0].endswith(filen)\n and os.path.exists(error[0])\n ):\n add_log = True\n template_path = error[0]\n # if we add a log, format explicitly the exception here\n # by telling to output the macro context after the macro\n # error log place at the beginning\n if add_log:\n if template_path:\n out = '\\n{0}\\n'.format(msg.splitlines()[0])\n with salt.utils.files.fopen(template_path) as fp_:\n template_contents = salt.utils.stringutils.to_unicode(fp_.read())\n out += salt.utils.stringutils.get_context(\n template_contents,\n line,\n marker=' <======================')\n else:\n out = '\\n{0}\\n'.format(msg)\n line = 0\n return line, out"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef render_genshi_tmpl(tmplstr, context, tmplpath=None):\n '''\n Render a Genshi template. A method should be passed in as part of the\n context. If no method is passed in, xml is assumed. Valid methods are:\n\n .. code-block:\n\n - xml\n - xhtml\n - html\n - text\n - newtext\n - oldtext\n\n Note that the ``text`` method will call ``NewTextTemplate``. If ``oldtext``\n is desired, it must be called explicitly\n '''\n method = context.get('method', 'xml')\n if method == 'text' or method == 'newtext':\n from genshi.template import NewTextTemplate\n tmpl = NewTextTemplate(tmplstr)\n elif method == 'oldtext':\n from genshi.template import OldTextTemplate\n tmpl = OldTextTemplate(tmplstr)\n else:\n from genshi.template import MarkupTemplate\n tmpl = MarkupTemplate(tmplstr)\n\n return tmpl.generate(**context).render(method)", "response": "Render a Genshi template."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nrenders a Cheetah template.", "response": "def render_cheetah_tmpl(tmplstr, context, tmplpath=None):\n '''\n Render a Cheetah template.\n '''\n from Cheetah.Template import Template\n return salt.utils.data.decode(Template(tmplstr, searchList=[context]))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nrender a template from a python source file", "response": "def py(sfn, string=False, **kwargs): # pylint: disable=C0103\n '''\n Render a template from a python source file\n\n Returns::\n\n {'result': bool,\n 'data': }\n '''\n if not os.path.isfile(sfn):\n return {}\n\n base_fname = os.path.basename(sfn)\n name = base_fname.split('.')[0]\n\n if USE_IMPORTLIB:\n # pylint: disable=no-member\n loader = importlib.machinery.SourceFileLoader(name, sfn)\n spec = importlib.util.spec_from_file_location(name, sfn, loader=loader)\n if spec is None:\n raise ImportError()\n mod = importlib.util.module_from_spec(spec)\n spec.loader.exec_module(mod)\n # pylint: enable=no-member\n sys.modules[name] = mod\n else:\n mod = imp.load_source(name, sfn)\n\n # File templates need these set as __var__\n if '__env__' not in kwargs and 'saltenv' in kwargs:\n setattr(mod, '__env__', kwargs['saltenv'])\n builtins = ['salt', 'grains', 'pillar', 'opts']\n for builtin in builtins:\n arg = '__{0}__'.format(builtin)\n setattr(mod, arg, kwargs[builtin])\n\n for kwarg in kwargs:\n setattr(mod, kwarg, kwargs[kwarg])\n\n try:\n data = mod.run()\n if string:\n return {'result': True,\n 'data': data}\n tgt = salt.utils.files.mkstemp()\n with salt.utils.files.fopen(tgt, 'w+') as target:\n target.write(salt.utils.stringutils.to_str(data))\n return {'result': True,\n 'data': tgt}\n except Exception:\n trb = traceback.format_exc()\n return {'result': False,\n 'data': trb}"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting information about the fund in the Namecheap account.", "response": "def get_balances():\n '''\n Gets information about fund in the user's account. This method returns the\n following information: Available Balance, Account Balance, Earned Amount,\n Withdrawable Amount and Funds Required for AutoRenew.\n\n .. note::\n If a domain setup with automatic renewal is expiring within the next 90\n days, the FundsRequiredForAutoRenew attribute shows the amount needed\n in your Namecheap account to complete auto renewal.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'my-minion' namecheap_users.get_balances\n '''\n opts = salt.utils.namecheap.get_opts('namecheap.users.getBalances')\n\n response_xml = salt.utils.namecheap.get_request(opts)\n\n if response_xml is None:\n return {}\n\n balance_response = response_xml.getElementsByTagName(\"UserGetBalancesResult\")[0]\n return salt.utils.namecheap.atts_to_dict(balance_response)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef check_balances(minimum=100):\n '''\n Checks if the provided minimum value is present in the user's account.\n\n Returns a boolean. Returns ``False`` if the user's account balance is less\n than the provided minimum or ``True`` if greater than the minimum.\n\n minimum : 100\n The value to check\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'my-minion' namecheap_users.check_balances\n salt 'my-minion' namecheap_users.check_balances minimum=150\n\n '''\n min_float = float(minimum)\n result = get_balances()\n if result['accountbalance'] <= min_float:\n return False\n return True", "response": "Checks if the user s account balance is present in the user s account. Returns a boolean if the user s account balance is less than the provided minimum."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_master_uri(master_ip,\n master_port,\n source_ip=None,\n source_port=None):\n '''\n Return the ZeroMQ URI to connect the Minion to the Master.\n It supports different source IP / port, given the ZeroMQ syntax:\n // Connecting using a IP address and bind to an IP address\n rc = zmq_connect(socket, \"tcp://192.168.1.17:5555;192.168.1.1:5555\"); assert (rc == 0);\n Source: http://api.zeromq.org/4-1:zmq-tcp\n '''\n from salt.utils.zeromq import ip_bracket\n\n master_uri = 'tcp://{master_ip}:{master_port}'.format(\n master_ip=ip_bracket(master_ip), master_port=master_port)\n\n if source_ip or source_port:\n if LIBZMQ_VERSION_INFO >= (4, 1, 6) and ZMQ_VERSION_INFO >= (16, 0, 1):\n # The source:port syntax for ZeroMQ has been added in libzmq 4.1.6\n # which is included in the pyzmq wheels starting with 16.0.1.\n if source_ip and source_port:\n master_uri = 'tcp://{source_ip}:{source_port};{master_ip}:{master_port}'.format(\n source_ip=ip_bracket(source_ip), source_port=source_port,\n master_ip=ip_bracket(master_ip), master_port=master_port)\n elif source_ip and not source_port:\n master_uri = 'tcp://{source_ip}:0;{master_ip}:{master_port}'.format(\n source_ip=ip_bracket(source_ip),\n master_ip=ip_bracket(master_ip), master_port=master_port)\n elif source_port and not source_ip:\n ip_any = '0.0.0.0' if ipaddress.ip_address(master_ip).version == 4 else ip_bracket('::')\n master_uri = 'tcp://{ip_any}:{source_port};{master_ip}:{master_port}'.format(\n ip_any=ip_any, source_port=source_port,\n master_ip=ip_bracket(master_ip), master_port=master_port)\n else:\n log.warning('Unable to connect to the Master using a specific source IP / port')\n log.warning('Consider upgrading to pyzmq >= 16.0.1 and libzmq >= 4.1.6')\n log.warning('Specific source IP / port for connecting to master returner port: configuraion ignored')\n\n return master_uri", "response": "Returns the ZeroMQ URI to connect the Minion to the Master."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _set_tcp_keepalive(zmq_socket, opts):\n '''\n Ensure that TCP keepalives are set as specified in \"opts\".\n\n Warning: Failure to set TCP keepalives on the salt-master can result in\n not detecting the loss of a minion when the connection is lost or when\n it's host has been terminated without first closing the socket.\n Salt's Presence System depends on this connection status to know if a minion\n is \"present\".\n\n Warning: Failure to set TCP keepalives on minions can result in frequent or\n unexpected disconnects!\n '''\n if hasattr(zmq, 'TCP_KEEPALIVE') and opts:\n if 'tcp_keepalive' in opts:\n zmq_socket.setsockopt(\n zmq.TCP_KEEPALIVE, opts['tcp_keepalive']\n )\n if 'tcp_keepalive_idle' in opts:\n zmq_socket.setsockopt(\n zmq.TCP_KEEPALIVE_IDLE, opts['tcp_keepalive_idle']\n )\n if 'tcp_keepalive_cnt' in opts:\n zmq_socket.setsockopt(\n zmq.TCP_KEEPALIVE_CNT, opts['tcp_keepalive_cnt']\n )\n if 'tcp_keepalive_intvl' in opts:\n zmq_socket.setsockopt(\n zmq.TCP_KEEPALIVE_INTVL, opts['tcp_keepalive_intvl']\n )", "response": "Sets the TCP keepalive on the given zmq socket."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _uncrypted_transfer(self, load, tries=3, timeout=60):\n '''\n Send a load across the wire in cleartext\n\n :param dict load: A load to send across the wire\n :param int tries: The number of times to make before failure\n :param int timeout: The number of seconds on a response before failing\n '''\n ret = yield self.message_client.send(\n self._package_load(load),\n timeout=timeout,\n tries=tries,\n )\n\n raise tornado.gen.Return(ret)", "response": "Send a load across the wire in cleartext\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsending a request to the broker.", "response": "def send(self, load, tries=3, timeout=60, raw=False):\n '''\n Send a request, return a future which will complete when we send the message\n '''\n if self.crypt == 'clear':\n ret = yield self._uncrypted_transfer(load, tries=tries, timeout=timeout)\n else:\n ret = yield self._crypted_transfer(load, tries=tries, timeout=timeout, raw=raw)\n raise tornado.gen.Return(ret)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef master_pub(self):\n '''\n Return the master publish port\n '''\n return _get_master_uri(self.opts['master_ip'],\n self.publish_port,\n source_ip=self.opts.get('source_ip'),\n source_port=self.opts.get('source_publish_port'))", "response": "Return the master publish port"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ntakes the zmq messages decrypt and decode them into a payload", "response": "def _decode_messages(self, messages):\n '''\n Take the zmq messages, decrypt/decode them into a payload\n\n :param list messages: A list of messages to be decoded\n '''\n messages_len = len(messages)\n # if it was one message, then its old style\n if messages_len == 1:\n payload = self.serial.loads(messages[0])\n # 2 includes a header which says who should do it\n elif messages_len == 2:\n if (self.opts.get('__role') != 'syndic' and messages[0] not in ('broadcast', self.hexid)) or \\\n (self.opts.get('__role') == 'syndic' and messages[0] not in ('broadcast', 'syndic')):\n log.debug('Publish received for not this minion: %s', messages[0])\n raise tornado.gen.Return(None)\n payload = self.serial.loads(messages[1])\n else:\n raise Exception(('Invalid number of messages ({0}) in zeromq pub'\n 'message from master').format(len(messages_len)))\n # Yield control back to the caller. When the payload has been decoded, assign\n # the decoded payload to 'ret' and resume operation\n ret = yield self._decode_payload(payload)\n raise tornado.gen.Return(ret)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef stream(self):\n '''\n Return the current zmqstream, creating one if necessary\n '''\n if not hasattr(self, '_stream'):\n self._stream = zmq.eventloop.zmqstream.ZMQStream(self._socket, io_loop=self.io_loop)\n return self._stream", "response": "Return the current zmqstream creating one if necessary"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nregisters a callback for received messages", "response": "def on_recv(self, callback):\n '''\n Register a callback for received messages (that we didn't initiate)\n\n :param func callback: A function which should be called when data is received\n '''\n if callback is None:\n return self.stream.on_recv(None)\n\n @tornado.gen.coroutine\n def wrap_callback(messages):\n payload = yield self._decode_messages(messages)\n if payload is not None:\n callback(payload)\n return self.stream.on_recv(wrap_callback)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef close(self):\n '''\n Cleanly shutdown the router socket\n '''\n if self._closing:\n return\n log.info('MWorkerQueue under PID %s is closing', os.getpid())\n self._closing = True\n # pylint: disable=E0203\n if getattr(self, '_monitor', None) is not None:\n self._monitor.stop()\n self._monitor = None\n if getattr(self, '_w_monitor', None) is not None:\n self._w_monitor.stop()\n self._w_monitor = None\n if hasattr(self, 'clients') and self.clients.closed is False:\n self.clients.close()\n if hasattr(self, 'workers') and self.workers.closed is False:\n self.workers.close()\n if hasattr(self, 'stream'):\n self.stream.close()\n if hasattr(self, '_socket') and self._socket.closed is False:\n self._socket.close()\n if hasattr(self, 'context') and self.context.closed is False:\n self.context.term()", "response": "Cleanly shutdown the router socket\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _start_zmq_monitor(self):\n '''\n Starts ZMQ monitor for debugging purposes.\n :return:\n '''\n # Socket monitor shall be used the only for debug\n # purposes so using threading doesn't look too bad here\n\n if HAS_ZMQ_MONITOR and self.opts['zmq_monitor']:\n log.debug('Starting ZMQ monitor')\n import threading\n self._w_monitor = ZeroMQSocketMonitor(self._socket)\n threading.Thread(target=self._w_monitor.start_poll).start()\n log.debug('ZMQ monitor has been started started')", "response": "Starts the ZMQ monitor for debugging purposes."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\npost - forking the master process.", "response": "def post_fork(self, payload_handler, io_loop):\n '''\n After forking we need to create all of the local sockets to listen to the\n router\n\n :param func payload_handler: A function to called to handle incoming payloads as\n they are picked up off the wire\n :param IOLoop io_loop: An instance of a Tornado IOLoop, to handle event scheduling\n '''\n self.payload_handler = payload_handler\n self.io_loop = io_loop\n\n self.context = zmq.Context(1)\n self._socket = self.context.socket(zmq.REP)\n self._start_zmq_monitor()\n\n if self.opts.get('ipc_mode', '') == 'tcp':\n self.w_uri = 'tcp://127.0.0.1:{0}'.format(\n self.opts.get('tcp_master_workers', 4515)\n )\n else:\n self.w_uri = 'ipc://{0}'.format(\n os.path.join(self.opts['sock_dir'], 'workers.ipc')\n )\n log.info('Worker binding to socket %s', self.w_uri)\n self._socket.connect(self.w_uri)\n\n salt.transport.mixins.auth.AESReqServerMixin.post_fork(self, payload_handler, io_loop)\n\n self.stream = zmq.eventloop.zmqstream.ZMQStream(self._socket, io_loop=self.io_loop)\n self.stream.on_recv_stream(self.handle_message)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nhandling incoming messages from the ZeroMQ stream.", "response": "def handle_message(self, stream, payload):\n '''\n Handle incoming messages from underlying TCP streams\n\n :stream ZMQStream stream: A ZeroMQ stream.\n See http://zeromq.github.io/pyzmq/api/generated/zmq.eventloop.zmqstream.html\n\n :param dict payload: A payload to process\n '''\n try:\n payload = self.serial.loads(payload[0])\n payload = self._decode_payload(payload)\n except Exception as exc:\n exc_type = type(exc).__name__\n if exc_type == 'AuthenticationError':\n log.debug(\n 'Minion failed to auth to master. Since the payload is '\n 'encrypted, it is not known which minion failed to '\n 'authenticate. It is likely that this is a transient '\n 'failure due to the master rotating its public key.'\n )\n else:\n log.error('Bad load from minion: %s: %s', exc_type, exc)\n stream.send(self.serial.dumps('bad load'))\n raise tornado.gen.Return()\n\n # TODO helper functions to normalize payload?\n if not isinstance(payload, dict) or not isinstance(payload.get('load'), dict):\n log.error('payload and load must be a dict. Payload was: %s and load was %s', payload, payload.get('load'))\n stream.send(self.serial.dumps('payload and load must be a dict'))\n raise tornado.gen.Return()\n\n try:\n id_ = payload['load'].get('id', '')\n if str('\\0') in id_:\n log.error('Payload contains an id with a null byte: %s', payload)\n stream.send(self.serial.dumps('bad load: id contains a null byte'))\n raise tornado.gen.Return()\n except TypeError:\n log.error('Payload contains non-string id: %s', payload)\n stream.send(self.serial.dumps('bad load: id {0} is not a string'.format(id_)))\n raise tornado.gen.Return()\n\n # intercept the \"_auth\" commands, since the main daemon shouldn't know\n # anything about our key auth\n if payload['enc'] == 'clear' and payload.get('load', {}).get('cmd') == '_auth':\n stream.send(self.serial.dumps(self._auth(payload['load'])))\n raise tornado.gen.Return()\n\n # TODO: test\n try:\n # Take the payload_handler function that was registered when we created the channel\n # and call it, returning control to the caller until it completes\n ret, req_opts = yield self.payload_handler(payload)\n except Exception as e:\n # always attempt to return an error to the minion\n stream.send(self.serial.dumps('Some exception handling minion payload'))\n log.error('Some exception handling a payload from minion', exc_info=True)\n raise tornado.gen.Return()\n\n req_fun = req_opts.get('fun', 'send')\n if req_fun == 'send_clear':\n stream.send(self.serial.dumps(ret))\n elif req_fun == 'send':\n stream.send(self.serial.dumps(self.crypticle.dumps(ret)))\n elif req_fun == 'send_private':\n stream.send(self.serial.dumps(self._encrypt_private(ret,\n req_opts['key'],\n req_opts['tgt'],\n )))\n else:\n log.error('Unknown req_fun %s', req_fun)\n # always attempt to return an error to the minion\n stream.send(self.serial.dumps('Server-side exception handling payload'))\n raise tornado.gen.Return()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _publish_daemon(self, log_queue=None):\n '''\n Bind to the interface specified in the configuration file\n '''\n salt.utils.process.appendproctitle(self.__class__.__name__)\n if log_queue:\n salt.log.setup.set_multiprocessing_logging_queue(log_queue)\n salt.log.setup.setup_multiprocessing_logging(log_queue)\n\n # Set up the context\n context = zmq.Context(1)\n # Prepare minion publish socket\n pub_sock = context.socket(zmq.PUB)\n _set_tcp_keepalive(pub_sock, self.opts)\n # if 2.1 >= zmq < 3.0, we only have one HWM setting\n try:\n pub_sock.setsockopt(zmq.HWM, self.opts.get('pub_hwm', 1000))\n # in zmq >= 3.0, there are separate send and receive HWM settings\n except AttributeError:\n # Set the High Water Marks. For more information on HWM, see:\n # http://api.zeromq.org/4-1:zmq-setsockopt\n pub_sock.setsockopt(zmq.SNDHWM, self.opts.get('pub_hwm', 1000))\n pub_sock.setsockopt(zmq.RCVHWM, self.opts.get('pub_hwm', 1000))\n if self.opts['ipv6'] is True and hasattr(zmq, 'IPV4ONLY'):\n # IPv6 sockets work for both IPv6 and IPv4 addresses\n pub_sock.setsockopt(zmq.IPV4ONLY, 0)\n pub_sock.setsockopt(zmq.BACKLOG, self.opts.get('zmq_backlog', 1000))\n pub_sock.setsockopt(zmq.LINGER, -1)\n pub_uri = 'tcp://{interface}:{publish_port}'.format(**self.opts)\n # Prepare minion pull socket\n pull_sock = context.socket(zmq.PULL)\n pull_sock.setsockopt(zmq.LINGER, -1)\n\n if self.opts.get('ipc_mode', '') == 'tcp':\n pull_uri = 'tcp://127.0.0.1:{0}'.format(\n self.opts.get('tcp_master_publish_pull', 4514)\n )\n else:\n pull_uri = 'ipc://{0}'.format(\n os.path.join(self.opts['sock_dir'], 'publish_pull.ipc')\n )\n salt.utils.zeromq.check_ipc_path_max_len(pull_uri)\n\n # Start the minion command publisher\n log.info('Starting the Salt Publisher on %s', pub_uri)\n pub_sock.bind(pub_uri)\n\n # Securely create socket\n log.info('Starting the Salt Puller on %s', pull_uri)\n with salt.utils.files.set_umask(0o177):\n pull_sock.bind(pull_uri)\n\n try:\n while True:\n # Catch and handle EINTR from when this process is sent\n # SIGUSR1 gracefully so we don't choke and die horribly\n try:\n log.debug('Publish daemon getting data from puller %s', pull_uri)\n package = pull_sock.recv()\n log.debug('Publish daemon received payload. size=%d', len(package))\n\n unpacked_package = salt.payload.unpackage(package)\n if six.PY3:\n unpacked_package = salt.transport.frame.decode_embedded_strs(unpacked_package)\n payload = unpacked_package['payload']\n log.trace('Accepted unpacked package from puller')\n if self.opts['zmq_filtering']:\n # if you have a specific topic list, use that\n if 'topic_lst' in unpacked_package:\n for topic in unpacked_package['topic_lst']:\n log.trace('Sending filtered data over publisher %s', pub_uri)\n # zmq filters are substring match, hash the topic\n # to avoid collisions\n htopic = salt.utils.stringutils.to_bytes(hashlib.sha1(topic).hexdigest())\n pub_sock.send(htopic, flags=zmq.SNDMORE)\n pub_sock.send(payload)\n log.trace('Filtered data has been sent')\n\n # Syndic broadcast\n if self.opts.get('order_masters'):\n log.trace('Sending filtered data to syndic')\n pub_sock.send(b'syndic', flags=zmq.SNDMORE)\n pub_sock.send(payload)\n log.trace('Filtered data has been sent to syndic')\n # otherwise its a broadcast\n else:\n # TODO: constants file for \"broadcast\"\n log.trace('Sending broadcasted data over publisher %s', pub_uri)\n pub_sock.send(b'broadcast', flags=zmq.SNDMORE)\n pub_sock.send(payload)\n log.trace('Broadcasted data has been sent')\n else:\n log.trace('Sending ZMQ-unfiltered data over publisher %s', pub_uri)\n pub_sock.send(payload)\n log.trace('Unfiltered data has been sent')\n except zmq.ZMQError as exc:\n if exc.errno == errno.EINTR:\n continue\n raise exc\n\n except KeyboardInterrupt:\n log.trace('Publish daemon caught Keyboard interupt, tearing down')\n # Cleanly close the sockets if we're shutting down\n if pub_sock.closed is False:\n pub_sock.close()\n if pull_sock.closed is False:\n pull_sock.close()\n if context.closed is False:\n context.term()", "response": "Create a publish daemon socket and publish_port."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef pub_connect(self):\n '''\n Create and connect this thread's zmq socket. If a publisher socket\n already exists \"pub_close\" is called before creating and connecting a\n new socket.\n '''\n if self.pub_sock:\n self.pub_close()\n ctx = zmq.Context.instance()\n self._sock_data.sock = ctx.socket(zmq.PUSH)\n self.pub_sock.setsockopt(zmq.LINGER, -1)\n if self.opts.get('ipc_mode', '') == 'tcp':\n pull_uri = 'tcp://127.0.0.1:{0}'.format(\n self.opts.get('tcp_master_publish_pull', 4514)\n )\n else:\n pull_uri = 'ipc://{0}'.format(\n os.path.join(self.opts['sock_dir'], 'publish_pull.ipc')\n )\n log.debug(\"Connecting to pub server: %s\", pull_uri)\n self.pub_sock.connect(pull_uri)\n return self._sock_data.sock", "response": "Create and connect this thread s zmq socket. If a publisher socket already exists pub_close is called before connecting a\n new socket is created."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef pub_close(self):\n '''\n Disconnect an existing publisher socket and remove it from the local\n thread's cache.\n '''\n if hasattr(self._sock_data, 'sock'):\n self._sock_data.sock.close()\n delattr(self._sock_data, 'sock')", "response": "Disconnect an existing publisher socket and remove it from the local\n thread s cache."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\npublishes a load to the daemon", "response": "def publish(self, load):\n '''\n Publish \"load\" to minions. This send the load to the publisher daemon\n process with does the actual sending to minions.\n\n :param dict load: A load to be sent across the wire to minions\n '''\n payload = {'enc': 'aes'}\n crypticle = salt.crypt.Crypticle(self.opts, salt.master.SMaster.secrets['aes']['secret'].value)\n payload['load'] = crypticle.dumps(load)\n if self.opts['sign_pub_messages']:\n master_pem_path = os.path.join(self.opts['pki_dir'], 'master.pem')\n log.debug(\"Signing data packet\")\n payload['sig'] = salt.crypt.sign_message(master_pem_path, payload['load'])\n int_payload = {'payload': self.serial.dumps(payload)}\n\n # add some targeting stuff for lists only (for now)\n if load['tgt_type'] == 'list':\n int_payload['topic_lst'] = load['tgt']\n\n # If zmq_filtering is enabled, target matching has to happen master side\n match_targets = [\"pcre\", \"glob\", \"list\"]\n if self.opts['zmq_filtering'] and load['tgt_type'] in match_targets:\n # Fetch a list of minions that match\n _res = self.ckminions.check_minions(load['tgt'],\n tgt_type=load['tgt_type'])\n match_ids = _res['minions']\n\n log.debug(\"Publish Side Match: %s\", match_ids)\n # Send list of miions thru so zmq can target them\n int_payload['topic_lst'] = match_ids\n payload = self.serial.dumps(int_payload)\n log.debug(\n 'Sending payload to publish daemon. jid=%s size=%d',\n load.get('jid', None), len(payload),\n )\n if not self.pub_sock:\n self.pub_connect()\n self.pub_sock.send(payload)\n log.debug('Sent payload to publish daemon.')"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nhandle a message timeout by sending it to the caller.", "response": "def timeout_message(self, message):\n '''\n Handle a message timeout by removing it from the sending queue\n and informing the caller\n\n :raises: SaltReqTimeoutError\n '''\n future = self.send_future_map.pop(message, None)\n # In a race condition the message might have been sent by the time\n # we're timing it out. Make sure the future is not None\n if future is not None:\n del self.send_timeout_map[message]\n if future.attempts < future.tries:\n future.attempts += 1\n log.debug('SaltReqTimeoutError, retrying. (%s/%s)', future.attempts, future.tries)\n self.send(\n message,\n timeout=future.timeout,\n tries=future.tries,\n future=future,\n )\n\n else:\n future.set_exception(SaltReqTimeoutError('Message timed out'))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef send(self, message, timeout=None, tries=3, future=None, callback=None, raw=False):\n '''\n Return a future which will be completed when the message has a response\n '''\n if future is None:\n future = tornado.concurrent.Future()\n future.tries = tries\n future.attempts = 0\n future.timeout = timeout\n # if a future wasn't passed in, we need to serialize the message\n message = self.serial.dumps(message)\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 # Add this future to the mapping\n self.send_future_map[message] = future\n\n if self.opts.get('detect_mode') is True:\n timeout = 1\n\n if timeout is not None:\n send_timeout = self.io_loop.call_later(timeout, self.timeout_message, message)\n self.send_timeout_map[message] = send_timeout\n\n if not self.send_queue:\n self.io_loop.spawn_callback(self._internal_send_recv)\n\n self.send_queue.append(message)\n\n return future", "response": "Send a message to the server."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the elasticsearch instance of the elastic elastic", "response": "def _get_instance(hosts=None, profile=None):\n '''\n Return the elasticsearch instance\n '''\n es = None\n proxies = None\n use_ssl = False\n ca_certs = None\n verify_certs = True\n http_auth = None\n timeout = 10\n\n if profile is None:\n profile = 'elasticsearch'\n\n if isinstance(profile, six.string_types):\n _profile = __salt__['config.option'](profile, None)\n elif isinstance(profile, dict):\n _profile = profile\n if _profile:\n hosts = _profile.get('host', hosts)\n if not hosts:\n hosts = _profile.get('hosts', hosts)\n proxies = _profile.get('proxies', None)\n use_ssl = _profile.get('use_ssl', False)\n ca_certs = _profile.get('ca_certs', None)\n verify_certs = _profile.get('verify_certs', True)\n username = _profile.get('username', None)\n password = _profile.get('password', None)\n timeout = _profile.get('timeout', 10)\n\n if username and password:\n http_auth = (username, password)\n\n if not hosts:\n hosts = ['127.0.0.1:9200']\n if isinstance(hosts, six.string_types):\n hosts = [hosts]\n try:\n if proxies:\n # Custom connection class to use requests module with proxies\n class ProxyConnection(RequestsHttpConnection):\n def __init__(self, *args, **kwargs):\n proxies = kwargs.pop('proxies', {})\n super(ProxyConnection, self).__init__(*args, **kwargs)\n self.session.proxies = proxies\n\n es = elasticsearch.Elasticsearch(\n hosts,\n connection_class=ProxyConnection,\n proxies=proxies,\n use_ssl=use_ssl,\n ca_certs=ca_certs,\n verify_certs=verify_certs,\n http_auth=http_auth,\n timeout=timeout,\n )\n else:\n es = elasticsearch.Elasticsearch(\n hosts,\n use_ssl=use_ssl,\n ca_certs=ca_certs,\n verify_certs=verify_certs,\n http_auth=http_auth,\n timeout=timeout,\n )\n\n # Try the connection\n es.info()\n except elasticsearch.exceptions.TransportError as err:\n raise CommandExecutionError(\n 'Could not connect to Elasticsearch host/ cluster {0} due to {1}'.format(hosts, err))\n return es"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef ping(allow_failure=False, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Test connection to Elasticsearch instance. This method does not fail if not explicitly specified.\n\n allow_failure\n Throw exception if ping fails\n\n CLI example::\n\n salt myminion elasticsearch.ping allow_failure=True\n salt myminion elasticsearch.ping profile=elasticsearch-extra\n '''\n try:\n _get_instance(hosts, profile)\n except CommandExecutionError as e:\n if allow_failure:\n raise e\n return False\n return True", "response": "Ping an elasticsearch instance"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef info(hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Return Elasticsearch information.\n\n CLI example::\n\n salt myminion elasticsearch.info\n salt myminion elasticsearch.info profile=elasticsearch-extra\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.info()\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot retrieve server information, server returned code {0} with message {1}\".format(e.status_code, e.error))", "response": "Return Elasticsearch information about the current node."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns node information for a given node id or name", "response": "def node_info(nodes=None, flat_settings=False, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Return Elasticsearch node information.\n\n nodes\n List of cluster nodes (id or name) to display stats for. Use _local for connected node, empty for all\n flat_settings\n Flatten settings keys\n\n CLI example::\n\n salt myminion elasticsearch.node_info flat_settings=True\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.nodes.info(node_id=nodes, flat_settings=flat_settings)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot retrieve node information, server returned code {0} with message {1}\".format(e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the cluster health information", "response": "def cluster_health(index=None, level='cluster', local=False, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Return Elasticsearch cluster health.\n\n index\n Limit the information returned to a specific index\n level\n Specify the level of detail for returned information, default 'cluster', valid choices are: 'cluster', 'indices', 'shards'\n local\n Return local information, do not retrieve the state from master node\n\n CLI example::\n\n salt myminion elasticsearch.cluster_health\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.cluster.health(index=index, level=level, local=local)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot retrieve health information, server returned code {0} with message {1}\".format(e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns elasticsearch cluster stats for a list of nodes or names", "response": "def cluster_stats(nodes=None, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Return Elasticsearch cluster stats.\n\n nodes\n List of cluster nodes (id or name) to display stats for. Use _local for connected node, empty for all\n\n CLI example::\n\n salt myminion elasticsearch.cluster_stats\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.cluster.stats(node_id=nodes)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot retrieve cluster stats, server returned code {0} with message {1}\".format(e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef alias_create(indices, alias, hosts=None, body=None, profile=None, source=None):\n '''\n Create an alias for a specific index/indices\n\n indices\n Single or multiple indices separated by comma, use _all to perform the operation on all indices.\n alias\n Alias name\n body\n Optional definition such as routing or filter as defined in https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html\n source\n URL of file specifying optional definition such as routing or filter. Cannot be used in combination with ``body``.\n\n CLI example::\n\n salt myminion elasticsearch.alias_create testindex_v1 testindex\n '''\n es = _get_instance(hosts, profile)\n if source and body:\n message = 'Either body or source should be specified but not both.'\n raise SaltInvocationError(message)\n if source:\n body = __salt__['cp.get_file_str'](\n source,\n saltenv=__opts__.get('saltenv', 'base'))\n try:\n result = es.indices.put_alias(index=indices, name=alias, body=body)\n return result.get('acknowledged', False)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot create alias {0} in index {1}, server returned code {2} with message {3}\".format(alias, indices, e.status_code, e.error))", "response": "Create an alias for a specific index"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef alias_delete(indices, aliases, hosts=None, body=None, profile=None, source=None):\n '''\n Delete an alias of an index\n\n indices\n Single or multiple indices separated by comma, use _all to perform the operation on all indices.\n aliases\n Alias names separated by comma\n\n CLI example::\n\n salt myminion elasticsearch.alias_delete testindex_v1 testindex\n '''\n es = _get_instance(hosts, profile)\n if source and body:\n message = 'Either body or source should be specified but not both.'\n raise SaltInvocationError(message)\n if source:\n body = __salt__['cp.get_file_str'](\n source,\n saltenv=__opts__.get('saltenv', 'base'))\n try:\n result = es.indices.delete_alias(index=indices, name=aliases)\n\n return result.get('acknowledged', False)\n except elasticsearch.exceptions.NotFoundError:\n return True\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot delete alias {0} in index {1}, server returned code {2} with message {3}\".format(aliases, indices, e.status_code, e.error))", "response": "Delete an alias of an index"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef alias_exists(aliases, indices=None, hosts=None, profile=None):\n '''\n Return a boolean indicating whether given alias exists\n\n indices\n Single or multiple indices separated by comma, use _all to perform the operation on all indices.\n aliases\n Alias names separated by comma\n\n CLI example::\n\n salt myminion elasticsearch.alias_exists None testindex\n '''\n es = _get_instance(hosts, profile)\n try:\n return es.indices.exists_alias(name=aliases, index=indices)\n except elasticsearch.exceptions.NotFoundError:\n return False\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot get alias {0} in index {1}, server returned code {2} with message {3}\".format(aliases, indices, e.status_code, e.error))", "response": "Return a boolean indicating whether given alias exists in indices"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget an alias from an index", "response": "def alias_get(indices=None, aliases=None, hosts=None, profile=None):\n '''\n Check for the existence of an alias and if it exists, return it\n\n indices\n Single or multiple indices separated by comma, use _all to perform the operation on all indices.\n aliases\n Alias names separated by comma\n\n CLI example::\n\n salt myminion elasticsearch.alias_get testindex\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.indices.get_alias(index=indices, name=aliases)\n except elasticsearch.exceptions.NotFoundError:\n return None\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot get alias {0} in index {1}, server returned code {2} with message {3}\".format(aliases, indices, e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef document_create(index, doc_type, body=None, id=None, hosts=None, profile=None, source=None):\n '''\n Create a document in a specified index\n\n index\n Index name where the document should reside\n doc_type\n Type of the document\n body\n Document to store\n source\n URL of file specifying document to store. Cannot be used in combination with ``body``.\n id\n Optional unique document identifier for specified doc_type (empty for random)\n\n CLI example::\n\n salt myminion elasticsearch.document_create testindex doctype1 '{}'\n '''\n es = _get_instance(hosts, profile)\n if source and body:\n message = 'Either body or source should be specified but not both.'\n raise SaltInvocationError(message)\n if source:\n body = __salt__['cp.get_file_str'](\n source,\n saltenv=__opts__.get('saltenv', 'base'))\n try:\n return es.index(index=index, doc_type=doc_type, body=body, id=id)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot create document in index {0}, server returned code {1} with message {2}\".format(index, e.status_code, e.error))", "response": "Create a document in an index"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef document_delete(index, doc_type, id, hosts=None, profile=None):\n '''\n Delete a document from an index\n\n index\n Index name where the document resides\n doc_type\n Type of the document\n id\n Document identifier\n\n CLI example::\n\n salt myminion elasticsearch.document_delete testindex doctype1 AUx-384m0Bug_8U80wQZ\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.delete(index=index, doc_type=doc_type, id=id)\n except elasticsearch.exceptions.NotFoundError:\n return None\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot delete document {0} in index {1}, server returned code {2} with message {3}\".format(id, index, e.status_code, e.error))", "response": "Delete a document from an index"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a boolean indicating whether given document exists in the given index", "response": "def document_exists(index, id, doc_type='_all', hosts=None, profile=None):\n '''\n Return a boolean indicating whether given document exists\n\n index\n Index name where the document resides\n id\n Document identifier\n doc_type\n Type of the document, use _all to fetch the first document matching the ID across all types\n\n CLI example::\n\n salt myminion elasticsearch.document_exists testindex AUx-384m0Bug_8U80wQZ\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.exists(index=index, id=id, doc_type=doc_type)\n except elasticsearch.exceptions.NotFoundError:\n return False\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot retrieve document {0} from index {1}, server returned code {2} with message {3}\".format(id, index, e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef index_create(index, body=None, hosts=None, profile=None, source=None):\n '''\n Create an index\n\n index\n Index name\n body\n Index definition, such as settings and mappings as defined in https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html\n source\n URL to file specifying index definition. Cannot be used in combination with ``body``.\n\n CLI example::\n\n salt myminion elasticsearch.index_create testindex\n salt myminion elasticsearch.index_create testindex2 '{\"settings\" : {\"index\" : {\"number_of_shards\" : 3, \"number_of_replicas\" : 2}}}'\n '''\n es = _get_instance(hosts, profile)\n if source and body:\n message = 'Either body or source should be specified but not both.'\n raise SaltInvocationError(message)\n if source:\n body = __salt__['cp.get_file_str'](\n source,\n saltenv=__opts__.get('saltenv', 'base'))\n try:\n result = es.indices.create(index=index, body=body)\n return result.get('acknowledged', False) and result.get(\"shards_acknowledged\", True)\n except elasticsearch.TransportError as e:\n if \"index_already_exists_exception\" == e.error:\n return True\n\n raise CommandExecutionError(\"Cannot create index {0}, server returned code {1} with message {2}\".format(index, e.status_code, e.error))", "response": "Create an index and return it"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef index_delete(index, hosts=None, profile=None):\n '''\n Delete an index\n\n index\n Index name\n\n CLI example::\n\n salt myminion elasticsearch.index_delete testindex\n '''\n es = _get_instance(hosts, profile)\n\n try:\n result = es.indices.delete(index=index)\n\n return result.get('acknowledged', False)\n except elasticsearch.exceptions.NotFoundError:\n return True\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot delete index {0}, server returned code {1} with message {2}\".format(index, e.status_code, e.error))", "response": "Delete an index\n\n index\n Index name\n\n CLI example::\n\n salt myminion elasticsearch.index_delete testindex"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef index_exists(index, hosts=None, profile=None):\n '''\n Return a boolean indicating whether given index exists\n\n index\n Index name\n\n CLI example::\n\n salt myminion elasticsearch.index_exists testindex\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.indices.exists(index=index)\n except elasticsearch.exceptions.NotFoundError:\n return False\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot retrieve index {0}, server returned code {1} with message {2}\".format(index, e.status_code, e.error))", "response": "Return a boolean indicating whether given index exists"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the index and its contents", "response": "def index_get(index, hosts=None, profile=None):\n '''\n Check for the existence of an index and if it exists, return it\n\n index\n Index name\n\n CLI example::\n\n salt myminion elasticsearch.index_get testindex\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.indices.get(index=index)\n except elasticsearch.exceptions.NotFoundError:\n return None\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot retrieve index {0}, server returned code {1} with message {2}\".format(index, e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nopen an index and return a new index object.", "response": "def index_open(index, allow_no_indices=True, expand_wildcards='closed', ignore_unavailable=True, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Open specified index.\n\n index\n Index to be opened\n allow_no_indices\n Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes _all string or when no indices have been specified)\n expand_wildcards\n Whether to expand wildcard expression to concrete indices that are open, closed or both., default \u2018closed\u2019, valid choices are: \u2018open\u2019, \u2018closed\u2019, \u2018none\u2019, \u2018all\u2019\n ignore_unavailable\n Whether specified concrete indices should be ignored when unavailable (missing or closed)\n\n CLI example::\n\n salt myminion elasticsearch.index_open testindex\n '''\n es = _get_instance(hosts, profile)\n\n try:\n result = es.indices.open(index=index, allow_no_indices=allow_no_indices, expand_wildcards=expand_wildcards, ignore_unavailable=ignore_unavailable)\n\n return result.get('acknowledged', False)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot open index {0}, server returned code {1} with message {2}\".format(index, e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef mapping_create(index, doc_type, body=None, hosts=None, profile=None, source=None):\n '''\n Create a mapping in a given index\n\n index\n Index for the mapping\n doc_type\n Name of the document type\n body\n Mapping definition as specified in https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html\n source\n URL to file specifying mapping definition. Cannot be used in combination with ``body``.\n\n CLI example::\n\n salt myminion elasticsearch.mapping_create testindex user '{ \"user\" : { \"properties\" : { \"message\" : {\"type\" : \"string\", \"store\" : true } } } }'\n '''\n es = _get_instance(hosts, profile)\n if source and body:\n message = 'Either body or source should be specified but not both.'\n raise SaltInvocationError(message)\n if source:\n body = __salt__['cp.get_file_str'](\n source,\n saltenv=__opts__.get('saltenv', 'base'))\n try:\n result = es.indices.put_mapping(index=index, doc_type=doc_type, body=body)\n\n return result.get('acknowledged', False)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot create mapping {0}, server returned code {1} with message {2}\".format(index, e.status_code, e.error))", "response": "Create a mapping in a given index"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ndelete a mapping of a given type along with its data.", "response": "def mapping_delete(index, doc_type, hosts=None, profile=None):\n '''\n Delete a mapping (type) along with its data. As of Elasticsearch 5.0 this is no longer available.\n\n index\n Index for the mapping\n doc_type\n Name of the document type\n\n CLI example::\n\n salt myminion elasticsearch.mapping_delete testindex user\n '''\n es = _get_instance(hosts, profile)\n try:\n result = es.indices.delete_mapping(index=index, doc_type=doc_type)\n\n return result.get('acknowledged', False)\n except elasticsearch.exceptions.NotFoundError:\n return True\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot delete mapping {0}, server returned code {1} with message {2}\".format(index, e.status_code, e.error))\n except AttributeError:\n raise CommandExecutionError(\"Method is not applicable for Elasticsearch 5.0+\")"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef mapping_get(index, doc_type, hosts=None, profile=None):\n '''\n Retrieve mapping definition of index or index/type\n\n index\n Index for the mapping\n doc_type\n Name of the document type\n\n CLI example::\n\n salt myminion elasticsearch.mapping_get testindex user\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.indices.get_mapping(index=index, doc_type=doc_type)\n except elasticsearch.exceptions.NotFoundError:\n return None\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot retrieve mapping {0}, server returned code {1} with message {2}\".format(index, e.status_code, e.error))", "response": "Retrieve mapping definition of index or index type"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef index_template_create(name, body=None, hosts=None, profile=None, source=None):\n '''\n Create an index template\n\n name\n Index template name\n\n body\n Template definition as specified in http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html\n\n source\n URL to file specifying template definition. Cannot be used in combination with ``body``.\n\n CLI example::\n\n salt myminion elasticsearch.index_template_create testindex_templ '{ \"template\": \"logstash-*\", \"order\": 1, \"settings\": { \"number_of_shards\": 1 } }'\n '''\n es = _get_instance(hosts, profile)\n if source and body:\n message = 'Either body or source should be specified but not both.'\n raise SaltInvocationError(message)\n if source:\n body = __salt__['cp.get_file_str'](\n source,\n saltenv=__opts__.get('saltenv', 'base'))\n try:\n result = es.indices.put_template(name=name, body=body)\n return result.get('acknowledged', False)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot create template {0}, server returned code {1} with message {2}\".format(name, e.status_code, e.error))", "response": "Create an index template with the specified body"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef index_template_delete(name, hosts=None, profile=None):\n '''\n Delete an index template (type) along with its data\n\n name\n Index template name\n\n CLI example::\n\n salt myminion elasticsearch.index_template_delete testindex_templ user\n '''\n es = _get_instance(hosts, profile)\n try:\n result = es.indices.delete_template(name=name)\n\n return result.get('acknowledged', False)\n except elasticsearch.exceptions.NotFoundError:\n return True\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot delete template {0}, server returned code {1} with message {2}\".format(name, e.status_code, e.error))", "response": "Delete an index template"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef index_template_exists(name, hosts=None, profile=None):\n '''\n Return a boolean indicating whether given index template exists\n\n name\n Index template name\n\n CLI example::\n\n salt myminion elasticsearch.index_template_exists testindex_templ\n '''\n es = _get_instance(hosts, profile)\n try:\n return es.indices.exists_template(name=name)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot retrieve template {0}, server returned code {1} with message {2}\".format(name, e.status_code, e.error))", "response": "Return a boolean indicating whether given index template exists"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef index_template_get(name, hosts=None, profile=None):\n '''\n Retrieve template definition of index or index/type\n\n name\n Index template name\n\n CLI example::\n\n salt myminion elasticsearch.index_template_get testindex_templ\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.indices.get_template(name=name)\n except elasticsearch.exceptions.NotFoundError:\n return None\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot retrieve template {0}, server returned code {1} with message {2}\".format(name, e.status_code, e.error))", "response": "Retrieve a specific index template"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nretrieve an Ingest pipeline definition.", "response": "def pipeline_get(id, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Retrieve Ingest pipeline definition. Available since Elasticsearch 5.0.\n\n id\n Pipeline id\n\n CLI example::\n\n salt myminion elasticsearch.pipeline_get mypipeline\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.ingest.get_pipeline(id=id)\n except elasticsearch.NotFoundError:\n return None\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot create pipeline {0}, server returned code {1} with message {2}\".format(id, e.status_code, e.error))\n except AttributeError:\n raise CommandExecutionError(\"Method is applicable only for Elasticsearch 5.0+\")"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndeletes an Ingest pipeline", "response": "def pipeline_delete(id, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Delete Ingest pipeline. Available since Elasticsearch 5.0.\n\n id\n Pipeline id\n\n CLI example::\n\n salt myminion elasticsearch.pipeline_delete mypipeline\n '''\n es = _get_instance(hosts, profile)\n\n try:\n ret = es.ingest.delete_pipeline(id=id)\n return ret.get('acknowledged', False)\n except elasticsearch.NotFoundError:\n return True\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot delete pipeline {0}, server returned code {1} with message {2}\".format(id, e.status_code, e.error))\n except AttributeError:\n raise CommandExecutionError(\"Method is applicable only for Elasticsearch 5.0+\")"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate an Ingest pipeline by supplied definition.", "response": "def pipeline_create(id, body, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Create Ingest pipeline by supplied definition. Available since Elasticsearch 5.0.\n\n id\n Pipeline id\n body\n Pipeline definition as specified in https://www.elastic.co/guide/en/elasticsearch/reference/master/pipeline.html\n\n CLI example::\n\n salt myminion elasticsearch.pipeline_create mypipeline '{\"description\": \"my custom pipeline\", \"processors\": [{\"set\" : {\"field\": \"collector_timestamp_millis\", \"value\": \"{{_ingest.timestamp}}\"}}]}'\n '''\n es = _get_instance(hosts, profile)\n try:\n out = es.ingest.put_pipeline(id=id, body=body)\n return out.get('acknowledged', False)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot create pipeline {0}, server returned code {1} with message {2}\".format(id, e.status_code, e.error))\n except AttributeError:\n raise CommandExecutionError(\"Method is applicable only for Elasticsearch 5.0+\")"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsimulates an existing Ingest pipeline on provided data.", "response": "def pipeline_simulate(id, body, verbose=False, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Simulate existing Ingest pipeline on provided data. Available since Elasticsearch 5.0.\n\n id\n Pipeline id\n body\n Pipeline definition as specified in https://www.elastic.co/guide/en/elasticsearch/reference/master/pipeline.html\n verbose\n Specify if the output should be more verbose\n\n CLI example::\n\n salt myminion elasticsearch.pipeline_simulate mypipeline '{\"docs\":[{\"_index\":\"index\",\"_type\":\"type\",\"_id\":\"id\",\"_source\":{\"foo\":\"bar\"}},{\"_index\":\"index\",\"_type\":\"type\",\"_id\":\"id\",\"_source\":{\"foo\":\"rab\"}}]}' verbose=True\n '''\n es = _get_instance(hosts, profile)\n try:\n return es.ingest.simulate(id=id, body=body, verbose=verbose)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot simulate pipeline {0}, server returned code {1} with message {2}\".format(id, e.status_code, e.error))\n except AttributeError:\n raise CommandExecutionError(\"Method is applicable only for Elasticsearch 5.0+\")"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef search_template_get(id, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Obtain existing search template definition.\n\n id\n Template ID\n\n CLI example::\n\n salt myminion elasticsearch.search_template_get mytemplate\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.get_template(id=id)\n except elasticsearch.NotFoundError:\n return None\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot obtain search template {0}, server returned code {1} with message {2}\".format(id, e.status_code, e.error))", "response": "Get an existing search template definition."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef search_template_create(id, body, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Create search template by supplied definition\n\n id\n Template ID\n body\n Search template definition\n\n CLI example::\n\n salt myminion elasticsearch.search_template_create mytemplate '{\"template\":{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}}'\n '''\n es = _get_instance(hosts, profile)\n\n try:\n result = es.put_template(id=id, body=body)\n\n return result.get('acknowledged', False)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot create search template {0}, server returned code {1} with message {2}\".format(id, e.status_code, e.error))", "response": "Create a search template by supplied id"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete an existing search template definition.", "response": "def search_template_delete(id, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Delete existing search template definition.\n\n id\n Template ID\n\n CLI example::\n\n salt myminion elasticsearch.search_template_delete mytemplate\n '''\n es = _get_instance(hosts, profile)\n\n try:\n result = es.delete_template(id=id)\n\n return result.get('acknowledged', False)\n except elasticsearch.NotFoundError:\n return True\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot delete search template {0}, server returned code {1} with message {2}\".format(id, e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef repository_get(name, local=False, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Get existing repository details.\n\n name\n Repository name\n local\n Retrieve only local information, default is false\n\n CLI example::\n\n salt myminion elasticsearch.repository_get testrepo\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.snapshot.get_repository(repository=name, local=local)\n except elasticsearch.NotFoundError:\n return None\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot obtain repository {0}, server returned code {1} with message {2}\".format(name, e.status_code, e.error))", "response": "Get information about a repository"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a new repository", "response": "def repository_create(name, body, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Create repository for storing snapshots. Note that shared repository paths have to be specified in path.repo Elasticsearch configuration option.\n\n name\n Repository name\n body\n Repository definition as in https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html\n\n CLI example::\n\n salt myminion elasticsearch.repository_create testrepo '{\"type\":\"fs\",\"settings\":{\"location\":\"/tmp/test\",\"compress\":true}}'\n '''\n es = _get_instance(hosts, profile)\n\n try:\n result = es.snapshot.create_repository(repository=name, body=body)\n\n return result.get('acknowledged', False)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot create repository {0}, server returned code {1} with message {2}\".format(name, e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndeleting an existing repository", "response": "def repository_delete(name, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Delete existing repository.\n\n name\n Repository name\n\n CLI example::\n\n salt myminion elasticsearch.repository_delete testrepo\n '''\n es = _get_instance(hosts, profile)\n\n try:\n result = es.snapshot.delete_repository(repository=name)\n\n return result.get('acknowledged', False)\n except elasticsearch.NotFoundError:\n return True\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot delete repository {0}, server returned code {1} with message {2}\".format(name, e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef repository_verify(name, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Obtain list of cluster nodes which successfully verified this repository.\n\n name\n Repository name\n\n CLI example::\n\n salt myminion elasticsearch.repository_verify testrepo\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.snapshot.verify_repository(repository=name)\n except elasticsearch.NotFoundError:\n return None\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot verify repository {0}, server returned code {1} with message {2}\".format(name, e.status_code, e.error))", "response": "Verify that a repository is up to date"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef snapshot_status(repository=None, snapshot=None, ignore_unavailable=False, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Obtain status of all currently running snapshots.\n\n repository\n Particular repository to look for snapshots\n snapshot\n Snapshot name\n ignore_unavailable\n Ignore unavailable snapshots\n\n CLI example::\n\n salt myminion elasticsearch.snapshot_status ignore_unavailable=True\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.snapshot.status(repository=repository, snapshot=snapshot, ignore_unavailable=ignore_unavailable)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot obtain snapshot status, server returned code {0} with message {1}\".format(e.status_code, e.error))", "response": "Return the status of a snapshot"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting details of a snapshot in a repository", "response": "def snapshot_get(repository, snapshot, ignore_unavailable=False, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Obtain snapshot residing in specified repository.\n\n repository\n Repository name\n snapshot\n Snapshot name, use _all to obtain all snapshots in specified repository\n ignore_unavailable\n Ignore unavailable snapshots\n\n CLI example::\n\n salt myminion elasticsearch.snapshot_get testrepo testsnapshot\n '''\n es = _get_instance(hosts, profile)\n\n try:\n return es.snapshot.get(repository=repository, snapshot=snapshot, ignore_unavailable=ignore_unavailable)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot obtain details of snapshot {0} in repository {1}, server returned code {2} with message {3}\".format(snapshot, repository, e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a new snapshot in specified repository", "response": "def snapshot_create(repository, snapshot, body=None, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Create snapshot in specified repository by supplied definition.\n\n repository\n Repository name\n snapshot\n Snapshot name\n body\n Snapshot definition as in https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html\n\n CLI example::\n\n salt myminion elasticsearch.snapshot_create testrepo testsnapshot '{\"indices\":\"index_1,index_2\",\"ignore_unavailable\":true,\"include_global_state\":false}'\n '''\n es = _get_instance(hosts, profile)\n\n try:\n response = es.snapshot.create(repository=repository, snapshot=snapshot, body=body)\n\n return response.get('accepted', False)\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot create snapshot {0} in repository {1}, server returned code {2} with message {3}\".format(snapshot, repository, e.status_code, e.error))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef snapshot_delete(repository, snapshot, hosts=None, profile=None):\n '''\n .. versionadded:: 2017.7.0\n\n Delete snapshot from specified repository.\n\n repository\n Repository name\n snapshot\n Snapshot name\n\n CLI example::\n\n salt myminion elasticsearch.snapshot_delete testrepo testsnapshot\n '''\n es = _get_instance(hosts, profile)\n\n try:\n result = es.snapshot.delete(repository=repository, snapshot=snapshot)\n\n return result.get('acknowledged', False)\n except elasticsearch.NotFoundError:\n return True\n except elasticsearch.TransportError as e:\n raise CommandExecutionError(\"Cannot delete snapshot {0} from repository {1}, server returned code {2} with message {3}\".format(snapshot, repository, e.status_code, e.error))", "response": "Delete a snapshot from a repository"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nensuring that the named snap package is installed and installed.", "response": "def installed(name, channel=None):\n '''\n Ensure that the named snap package is installed\n\n name\n The snap package\n\n channel\n Optional. The channel to install the package from.\n '''\n ret = {'name': name,\n 'changes': {},\n 'pchanges': {},\n 'result': None,\n 'comment': ''}\n\n old = __salt__['snap.versions_installed'](name)\n if not old:\n if __opts__['test']:\n ret['comment'] = 'Package \"{0}\" would have been installed'.format(name)\n ret['pchanges']['new'] = name\n ret['pchanges']['old'] = None\n ret['result'] = None\n return ret\n\n install = __salt__['snap.install'](name, channel=channel)\n if install['result']:\n ret['comment'] = 'Package \"{0}\" was installed'.format(name)\n ret['changes']['new'] = name\n ret['changes']['old'] = None\n ret['result'] = True\n return ret\n\n ret['comment'] = 'Package \"{0}\" failed to install'.format(name)\n ret['comment'] += '\\noutput:\\n' + install['output']\n ret['result'] = False\n return ret\n\n # Currently snap always returns only one line?\n old_channel = old[0]['tracking']\n if old_channel != channel and channel is not None:\n if __opts__['test']:\n ret['comment'] = 'Package \"{0}\" would have been switched to channel {1}'.format(name, channel)\n ret['pchanges']['old_channel'] = old_channel\n ret['pchanges']['new_channel'] = channel\n ret['result'] = None\n return ret\n\n refresh = __salt__['snap.install'](name, channel=channel, refresh=True)\n if refresh['result']:\n ret['comment'] = 'Package \"{0}\" was switched to channel {1}'.format(name, channel)\n ret['pchanges']['old_channel'] = old_channel\n ret['pchanges']['new_channel'] = channel\n ret['result'] = True\n return ret\n\n ret['comment'] = 'Failed to switch Package \"{0}\" to channel {1}'.format(name, channel)\n ret['comment'] += '\\noutput:\\n' + install['output']\n ret['result'] = False\n return ret\n\n ret['comment'] = 'Package \"{0}\" is already installed'.format(name)\n if __opts__['test']:\n ret['result'] = None\n return ret\n\n ret['result'] = True\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nensuring that the named snap package is not installed and removed", "response": "def removed(name):\n '''\n Ensure that the named snap package is not installed\n\n name\n The snap package\n '''\n\n ret = {'name': name,\n 'changes': {},\n 'pchanges': {},\n 'result': None,\n 'comment': ''}\n\n old = __salt__['snap.versions_installed'](name)\n if not old:\n ret['comment'] = 'Package {0} is not installed'.format(name)\n ret['result'] = True\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'Package {0} would have been removed'.format(name)\n ret['result'] = None\n ret['pchanges']['old'] = old[0]['version']\n ret['pchanges']['new'] = None\n return ret\n\n remove = __salt__['snap.remove'](name)\n ret['comment'] = 'Package {0} removed'.format(name)\n ret['result'] = True\n ret['changes']['old'] = old[0]['version']\n ret['changes']['new'] = None\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_queue(config):\n '''\n Check the context for the notifier and construct it if not present\n '''\n\n if 'watchdog.observer' not in __context__:\n queue = collections.deque()\n observer = Observer()\n for path in config.get('directories', {}):\n path_params = config.get('directories').get(path)\n masks = path_params.get('mask', DEFAULT_MASK)\n event_handler = Handler(queue, masks)\n observer.schedule(event_handler, path)\n\n observer.start()\n\n __context__['watchdog.observer'] = observer\n __context__['watchdog.queue'] = queue\n\n return __context__['watchdog.queue']", "response": "Get the queue for the notifier if not present"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef validate(config):\n '''\n Validate the beacon configuration\n '''\n\n try:\n _validate(config)\n return True, 'Valid beacon configuration'\n except ValidationError as error:\n return False, str(error)", "response": "Validate the beacon configuration"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef beacon(config):\n '''\n Watch the configured directories\n\n Example Config\n\n .. code-block:: yaml\n\n beacons:\n watchdog:\n - directories:\n /path/to/dir:\n mask:\n - create\n - modify\n - delete\n - move\n\n The mask list can contain the following events (the default mask is create,\n modify delete, and move):\n * create - File or directory is created in watched directory\n * modify - The watched directory is modified\n * delete - File or directory is deleted from watched directory\n * move - File or directory is moved or renamed in the watched directory\n '''\n\n _config = {}\n list(map(_config.update, config))\n\n queue = _get_queue(_config)\n\n ret = []\n while queue:\n ret.append(to_salt_event(queue.popleft()))\n\n return ret", "response": "Watch the configured directories\nAttributeNames and return a list of events that can be sent to the master"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nbounce all Traffic Server nodes in the specified cluster.", "response": "def bounce_cluster(name):\n '''\n Bounce all Traffic Server nodes in the cluster. Bouncing Traffic Server\n shuts down and immediately restarts Traffic Server, node-by-node.\n\n .. code-block:: yaml\n\n bounce_ats_cluster:\n trafficserver.bounce_cluster\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Bouncing cluster'\n return ret\n\n __salt__['trafficserver.bounce_cluster']()\n\n ret['result'] = True\n ret['comment'] = 'Bounced cluster'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef bounce_local(name, drain=False):\n '''\n Bounce Traffic Server on the local node. Bouncing Traffic Server shuts down\n and immediately restarts the Traffic Server node.\n\n This option modifies the behavior of traffic_line -b and traffic_line -L\n such that traffic_server is not shut down until the number of active client\n connections drops to the number given by the\n proxy.config.restart.active_client_threshold configuration variable.\n\n .. code-block:: yaml\n\n bounce_ats_local:\n trafficserver.bounce_local\n\n bounce_ats_local:\n trafficserver.bounce_local\n - drain: True\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Bouncing local node'\n return ret\n\n if drain:\n __salt__['trafficserver.bounce_local'](drain=True)\n ret['result'] = True\n ret['comment'] = 'Bounced local node with drain option'\n return ret\n else:\n __salt__['trafficserver.bounce_local']()\n ret['result'] = True\n ret['comment'] = 'Bounced local node'\n return ret", "response": "Bounces Traffic Server on the local node."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nclearing accumulated statistics on all nodes in the cluster.", "response": "def clear_cluster(name):\n '''\n Clears accumulated statistics on all nodes in the cluster.\n\n .. code-block:: yaml\n\n clear_ats_cluster:\n trafficserver.clear_cluster\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Clearing cluster statistics'\n return ret\n\n __salt__['trafficserver.clear_cluster']()\n\n ret['result'] = True\n ret['comment'] = 'Cleared cluster statistics'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef clear_node(name):\n '''\n Clears accumulated statistics on the local node.\n\n .. code-block:: yaml\n\n clear_ats_node:\n trafficserver.clear_node\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Clearing local node statistics'\n return ret\n\n __salt__['trafficserver.clear_node']()\n\n ret['result'] = True\n ret['comment'] = 'Cleared local node statistics'\n return ret", "response": "Clears accumulated statistics on the local node."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef restart_cluster(name):\n '''\n Restart the traffic_manager process and the traffic_server process on all\n the nodes in a cluster.\n\n .. code-block:: bash\n\n restart_ats_cluster:\n trafficserver.restart_cluster\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Restarting cluster'\n return ret\n\n __salt__['trafficserver.restart_cluster']()\n\n ret['result'] = True\n ret['comment'] = 'Restarted cluster'\n return ret", "response": "Restart the traffic manager process and traffic server process on all the nodes in a cluster."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nrestarting the local node", "response": "def restart_local(name, drain=False):\n '''\n Restart the traffic_manager and traffic_server processes on the local node.\n\n This option modifies the behavior of traffic_line -b and traffic_line -L\n such that traffic_server is not shut down until the number of active client\n connections drops to the number given by the\n proxy.config.restart.active_client_threshold configuration variable.\n\n .. code-block:: yaml\n\n restart_ats_local:\n trafficserver.restart_local\n\n restart_ats_local_drain:\n trafficserver.restart_local\n - drain: True\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Restarting local node'\n return ret\n\n if drain:\n __salt__['trafficserver.restart_local'](drain=True)\n ret['result'] = True\n ret['comment'] = 'Restarted local node with drain option'\n return ret\n else:\n __salt__['trafficserver.restart_local']()\n ret['result'] = True\n ret['comment'] = 'Restarted local node'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef config(name, value):\n '''\n Set Traffic Server configuration variable values.\n\n .. code-block:: yaml\n\n proxy.config.proxy_name:\n trafficserver.config:\n - value: cdn.site.domain.tld\n\n OR\n\n traffic_server_setting:\n trafficserver.config:\n - name: proxy.config.proxy_name\n - value: cdn.site.domain.tld\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Configuring {0} to {1}'.format(\n name,\n value,\n )\n return ret\n\n __salt__['trafficserver.set_config'](name, value)\n\n ret['result'] = True\n ret['comment'] = 'Configured {0} to {1}'.format(name, value)\n return ret", "response": "Set Traffic Server configuration variable values."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef shutdown(name):\n '''\n Shut down Traffic Server on the local node.\n\n .. code-block:: yaml\n\n shutdown_ats:\n trafficserver.shutdown\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Shutting down local node'\n return ret\n\n __salt__['trafficserver.shutdown']()\n\n ret['result'] = True\n ret['comment'] = 'Shutdown local node'\n return ret", "response": "Shut down Traffic Server on the local node."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef startup(name):\n '''\n Start Traffic Server on the local node.\n\n .. code-block:: yaml\n\n startup_ats:\n trafficserver.startup\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Starting up local node'\n return ret\n\n __salt__['trafficserver.startup']()\n\n ret['result'] = True\n ret['comment'] = 'Starting up local node'\n return ret", "response": "Start Traffic Server on the local node."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef refresh(name):\n '''\n Initiate a Traffic Server configuration file reread. Use this command to\n update the running configuration after any configuration file modification.\n\n The timestamp of the last reconfiguration event (in seconds since epoch) is\n published in the proxy.node.config.reconfigure_time metric.\n\n .. code-block:: yaml\n\n refresh_ats:\n trafficserver.refresh\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Refreshing local node configuration'\n return ret\n\n __salt__['trafficserver.refresh']()\n\n ret['result'] = True\n ret['comment'] = 'Refreshed local node configuration'\n return ret", "response": "Initiate a Traffic Server configuration file reread. Use this command to update the running configuration after any configuration file modification."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nresetting performance statistics to zero across the cluster.", "response": "def zero_cluster(name):\n '''\n Reset performance statistics to zero across the cluster.\n\n .. code-block:: yaml\n\n zero_ats_cluster:\n trafficserver.zero_cluster\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Zeroing cluster statistics'\n return ret\n\n __salt__['trafficserver.zero_cluster']()\n\n ret['result'] = True\n ret['comment'] = 'Zeroed cluster statistics'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreset performance statistics to zero on the local node.", "response": "def zero_node(name):\n '''\n Reset performance statistics to zero on the local node.\n\n .. code-block:: yaml\n\n zero_ats_node:\n trafficserver.zero_node\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Zeroing local node statistics'\n return ret\n\n __salt__['trafficserver.zero_node']()\n\n ret['result'] = True\n ret['comment'] = 'Zeroed local node statistics'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef offline(name, path):\n '''\n Mark a cache storage device as offline. The storage is identified by a path\n which must match exactly a path specified in storage.config. This removes\n the storage from the cache and redirects requests that would have used this\n storage to other storage. This has exactly the same effect as a disk\n failure for that storage. This does not persist across restarts of the\n traffic_server process.\n\n .. code-block:: yaml\n\n offline_ats_path:\n trafficserver.offline:\n - path: /path/to/cache\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'Setting {0} to offline'.format(path)\n return ret\n\n __salt__['trafficserver.offline'](path)\n\n ret['result'] = True\n ret['comment'] = 'Set {0} as offline'.format(path)\n return ret", "response": "Mark a cache storage device as offline"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef present(name,\n type,\n url,\n access='proxy',\n user='',\n password='',\n database='',\n basic_auth=False,\n basic_auth_user='',\n basic_auth_password='',\n is_default=False,\n json_data=None,\n profile='grafana'):\n '''\n Ensure that a data source is present.\n\n name\n Name of the data source.\n\n type\n Which type of data source it is ('graphite', 'influxdb' etc.).\n\n url\n The URL to the data source API.\n\n user\n Optional - user to authenticate with the data source\n\n password\n Optional - password to authenticate with the data source\n\n basic_auth\n Optional - set to True to use HTTP basic auth to authenticate with the\n data source.\n\n basic_auth_user\n Optional - HTTP basic auth username.\n\n basic_auth_password\n Optional - HTTP basic auth password.\n\n is_default\n Default: False\n '''\n if isinstance(profile, string_types):\n profile = __salt__['config.option'](profile)\n\n ret = {'name': name, 'result': None, 'comment': None, 'changes': {}}\n datasource = _get_datasource(profile, name)\n data = _get_json_data(name, type, url, access, user, password, database,\n basic_auth, basic_auth_user, basic_auth_password, is_default, json_data)\n\n if datasource:\n requests.put(\n _get_url(profile, datasource['id']),\n data,\n headers=_get_headers(profile),\n timeout=profile.get('grafana_timeout', 3),\n )\n ret['result'] = True\n ret['changes'] = _diff(datasource, data)\n if ret['changes']['new'] or ret['changes']['old']:\n ret['comment'] = 'Data source {0} updated'.format(name)\n else:\n ret['changes'] = {}\n ret['comment'] = 'Data source {0} already up-to-date'.format(name)\n else:\n requests.post(\n '{0}/api/datasources'.format(profile['grafana_url']),\n data,\n headers=_get_headers(profile),\n timeout=profile.get('grafana_timeout', 3),\n )\n ret['result'] = True\n ret['comment'] = 'New data source {0} added'.format(name)\n ret['changes'] = data\n\n return ret", "response": "Ensure that a data source is present."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef absent(name, profile='grafana'):\n '''\n Ensure that a data source is present.\n\n name\n Name of the data source to remove.\n '''\n if isinstance(profile, string_types):\n profile = __salt__['config.option'](profile)\n\n ret = {'result': None, 'comment': None, 'changes': {}}\n datasource = _get_datasource(profile, name)\n\n if not datasource:\n ret['result'] = True\n ret['comment'] = 'Data source {0} already absent'.format(name)\n return ret\n\n requests.delete(\n _get_url(profile, datasource['id']),\n headers=_get_headers(profile),\n timeout=profile.get('grafana_timeout', 3),\n )\n\n ret['result'] = True\n ret['comment'] = 'Data source {0} was deleted'.format(name)\n\n return ret", "response": "Ensure that a data source is absent."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncompares two dicts returning only keys that exist in the first dict and are WorkItem different in the second dict", "response": "def compare_changes(obj, **kwargs):\n '''\n Compare two dicts returning only keys that exist in the first dict and are\n different in the second one\n '''\n changes = {}\n for key, value in obj.items():\n if key in kwargs:\n if value != kwargs[key]:\n changes[key] = kwargs[key]\n return changes"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a network in the Neutron Cloud", "response": "def network_create(auth=None, **kwargs):\n '''\n Create a network\n\n name\n Name of the network being created\n\n shared : False\n If ``True``, set the network as shared\n\n admin_state_up : True\n If ``True``, Set the network administrative state to \"up\"\n\n external : False\n Control whether or not this network is externally accessible\n\n provider\n An optional Python dictionary of network provider options\n\n project_id\n The project ID on which this network will be created\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.network_create name=network2 \\\n shared=True admin_state_up=True external=True\n\n salt '*' neutronng.network_create name=network3 \\\n provider='{\"network_type\": \"vlan\",\\\n \"segmentation_id\": \"4010\",\\\n \"physical_network\": \"provider\"}' \\\n project_id=1dcac318a83b4610b7a7f7ba01465548\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(keep_name=True, **kwargs)\n return cloud.create_network(**kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndelete a network in the neutron server", "response": "def network_delete(auth=None, **kwargs):\n '''\n Delete a network\n\n name_or_id\n Name or ID of the network being deleted\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.network_delete name_or_id=network1\n salt '*' neutronng.network_delete name_or_id=1dcac318a83b4610b7a7f7ba01465548\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.delete_network(**kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_networks(auth=None, **kwargs):\n '''\n List networks\n\n filters\n A Python dictionary of filter conditions to push down\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.list_networks\n salt '*' neutronng.list_networks \\\n filters='{\"tenant_id\": \"1dcac318a83b4610b7a7f7ba01465548\"}'\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.list_networks(**kwargs)", "response": "List networks in the nova network store"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef network_get(auth=None, **kwargs):\n '''\n Get a single network\n\n filters\n A Python dictionary of filter conditions to push down\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.network_get name=XLB4\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.get_network(**kwargs)", "response": "Get a single network"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a subnet network_name_or_id The unique name or ID of the attached network. If a non-unique name is supplied, an exception is raised. cidr The CIDR ip_version The IP version, which is 4 or 6. enable_dhcp : False Set to ``True`` if DHCP is enabled and ``False`` if disabled subnet_name The name of the subnet tenant_id The ID of the tenant who owns the network. Only administrative users can specify a tenant ID other than their own. allocation_pools A list of dictionaries of the start and end addresses for the allocation pools. gateway_ip The gateway IP address. When you specify both ``allocation_pools`` and ``gateway_ip``, you must ensure that the gateway IP does not overlap with the specified allocation pools. disable_gateway_ip : False Set to ``True`` if gateway IP address is disabled and ``False`` if enabled. It is not allowed with ``gateway_ip``. dns_nameservers A list of DNS name servers for the subnet host_routes A list of host route dictionaries for the subnet ipv6_ra_mode IPv6 Router Advertisement mode. Valid values are ``dhcpv6-stateful``, ``dhcpv6-stateless``, or ``slaac``. ipv6_address_mode IPv6 address mode. Valid values are ``dhcpv6-stateful``, ``dhcpv6-stateless``, or ``slaac``. use_default_subnetpool If ``True``, use the default subnetpool for ``ip_version`` to obtain a CIDR. It is required to pass ``None`` to the ``cidr`` argument when enabling this option. CLI Example: .. code-block:: bash salt '*' neutronng.subnet_create network_name_or_id=network1 subnet_name=subnet1 salt '*' neutronng.subnet_create subnet_name=subnet2\\ network_name_or_id=network2 enable_dhcp=True \\ allocation_pools='[{\"start\": \"192.168.199.2\",\\ \"end\": \"192.168.199.254\"}]'\\ gateway_ip='192.168.199.1' cidr=192.168.199.0/24 salt '*' neutronng.subnet_create network_name_or_id=network1 \\ subnet_name=subnet1 dns_nameservers='[\"8.8.8.8\", \"8.8.8.7\"]'", "response": "def subnet_create(auth=None, **kwargs):\n '''\n Create a subnet\n\n network_name_or_id\n The unique name or ID of the attached network. If a non-unique name is\n supplied, an exception is raised.\n\n cidr\n The CIDR\n\n ip_version\n The IP version, which is 4 or 6.\n\n enable_dhcp : False\n Set to ``True`` if DHCP is enabled and ``False`` if disabled\n\n subnet_name\n The name of the subnet\n\n tenant_id\n The ID of the tenant who owns the network. Only administrative users\n can specify a tenant ID other than their own.\n\n allocation_pools\n A list of dictionaries of the start and end addresses for the\n allocation pools.\n\n gateway_ip\n The gateway IP address. When you specify both ``allocation_pools`` and\n ``gateway_ip``, you must ensure that the gateway IP does not overlap\n with the specified allocation pools.\n\n disable_gateway_ip : False\n Set to ``True`` if gateway IP address is disabled and ``False`` if\n enabled. It is not allowed with ``gateway_ip``.\n\n dns_nameservers\n A list of DNS name servers for the subnet\n\n host_routes\n A list of host route dictionaries for the subnet\n\n ipv6_ra_mode\n IPv6 Router Advertisement mode. Valid values are ``dhcpv6-stateful``,\n ``dhcpv6-stateless``, or ``slaac``.\n\n ipv6_address_mode\n IPv6 address mode. Valid values are ``dhcpv6-stateful``,\n ``dhcpv6-stateless``, or ``slaac``.\n\n use_default_subnetpool\n If ``True``, use the default subnetpool for ``ip_version`` to obtain a\n CIDR. It is required to pass ``None`` to the ``cidr`` argument when\n enabling this option.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.subnet_create network_name_or_id=network1\n subnet_name=subnet1\n\n salt '*' neutronng.subnet_create subnet_name=subnet2\\\n network_name_or_id=network2 enable_dhcp=True \\\n allocation_pools='[{\"start\": \"192.168.199.2\",\\\n \"end\": \"192.168.199.254\"}]'\\\n gateway_ip='192.168.199.1' cidr=192.168.199.0/24\n\n salt '*' neutronng.subnet_create network_name_or_id=network1 \\\n subnet_name=subnet1 dns_nameservers='[\"8.8.8.8\", \"8.8.8.7\"]'\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.create_subnet(**kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef subnet_update(auth=None, **kwargs):\n '''\n Update a subnet\n\n name_or_id\n Name or ID of the subnet to update\n\n subnet_name\n The new name of the subnet\n\n enable_dhcp\n Set to ``True`` if DHCP is enabled and ``False`` if disabled\n\n gateway_ip\n The gateway IP address. When you specify both allocation_pools and\n gateway_ip, you must ensure that the gateway IP does not overlap with\n the specified allocation pools.\n\n disable_gateway_ip : False\n Set to ``True`` if gateway IP address is disabled and False if enabled.\n It is not allowed with ``gateway_ip``.\n\n allocation_pools\n A list of dictionaries of the start and end addresses for the\n allocation pools.\n\n dns_nameservers\n A list of DNS name servers for the subnet\n\n host_routes\n A list of host route dictionaries for the subnet\n\n .. code-block:: bash\n\n salt '*' neutronng.subnet_update name=subnet1 subnet_name=subnet2\n salt '*' neutronng.subnet_update name=subnet1 dns_nameservers='[\"8.8.8.8\", \"8.8.8.7\"]'\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.update_subnet(**kwargs)", "response": "A wrapper for the Neutron cloud. update_subnet method that returns a new subnet object."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef subnet_delete(auth=None, **kwargs):\n '''\n Delete a subnet\n\n name\n Name or ID of the subnet to update\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.subnet_delete name=subnet1\n salt '*' neutronng.subnet_delete \\\n name=1dcac318a83b4610b7a7f7ba01465548\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.delete_subnet(**kwargs)", "response": "A wrapper for the cloud. delete_subnet function that deletes a subnet from the nationale."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists subnets in the cloud", "response": "def list_subnets(auth=None, **kwargs):\n '''\n List subnets\n\n filters\n A Python dictionary of filter conditions to push down\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.list_subnets\n salt '*' neutronng.list_subnets \\\n filters='{\"tenant_id\": \"1dcac318a83b4610b7a7f7ba01465548\"}'\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.list_subnets(**kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a single subnet", "response": "def subnet_get(auth=None, **kwargs):\n '''\n Get a single subnet\n\n filters\n A Python dictionary of filter conditions to push down\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.subnet_get name=subnet1\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.get_subnet(**kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef security_group_create(auth=None, **kwargs):\n '''\n Create a security group. Use security_group_get to create default.\n\n project_id\n The project ID on which this security group will be created\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.security_group_create name=secgroup1 \\\n description=\"Very secure security group\"\n salt '*' neutronng.security_group_create name=secgroup1 \\\n description=\"Very secure security group\" \\\n project_id=1dcac318a83b4610b7a7f7ba01465548\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(keep_name=True, **kwargs)\n return cloud.create_security_group(**kwargs)", "response": "Create a security group. Use security_group_get to create default."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef security_group_update(secgroup=None, auth=None, **kwargs):\n '''\n Update a security group\n\n secgroup\n Name, ID or Raw Object of the security group to update\n\n name\n New name for the security group\n\n description\n New description for the security group\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.security_group_update secgroup=secgroup1 \\\n description=\"Very secure security group\"\n salt '*' neutronng.security_group_update secgroup=secgroup1 \\\n description=\"Very secure security group\" \\\n project_id=1dcac318a83b4610b7a7f7ba01465548\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(keep_name=True, **kwargs)\n return cloud.update_security_group(secgroup, **kwargs)", "response": "Update a security group"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef security_group_delete(auth=None, **kwargs):\n '''\n Delete a security group\n\n name_or_id\n The name or unique ID of the security group\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.security_group_delete name_or_id=secgroup1\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.delete_security_group(**kwargs)", "response": "Delete a security group"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets a single security group.", "response": "def security_group_get(auth=None, **kwargs):\n '''\n Get a single security group. This will create a default security group\n if one does not exist yet for a particular project id.\n\n filters\n A Python dictionary of filter conditions to push down\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.security_group_get \\\n name=1dcac318a83b4610b7a7f7ba01465548\n\n salt '*' neutronng.security_group_get \\\n name=default\\\n filters='{\"tenant_id\":\"2e778bb64ca64a199eb526b5958d8710\"}'\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.get_security_group(**kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef security_group_rule_create(auth=None, **kwargs):\n '''\n Create a rule in a security group\n\n secgroup_name_or_id\n The security group name or ID to associate with this security group\n rule. If a non-unique group name is given, an exception is raised.\n\n port_range_min\n The minimum port number in the range that is matched by the security\n group rule. If the protocol is TCP or UDP, this value must be less than\n or equal to the port_range_max attribute value. If nova is used by the\n cloud provider for security groups, then a value of None will be\n transformed to -1.\n\n port_range_max\n The maximum port number in the range that is matched by the security\n group rule. The port_range_min attribute constrains the port_range_max\n attribute. If nova is used by the cloud provider for security groups,\n then a value of None will be transformed to -1.\n\n protocol\n The protocol that is matched by the security group rule. Valid values\n are ``None``, ``tcp``, ``udp``, and ``icmp``.\n\n remote_ip_prefix\n The remote IP prefix to be associated with this security group rule.\n This attribute matches the specified IP prefix as the source IP address\n of the IP packet.\n\n remote_group_id\n The remote group ID to be associated with this security group rule\n\n direction\n Either ``ingress`` or ``egress``; the direction in which the security\n group rule is applied. For a compute instance, an ingress security\n group rule is applied to incoming (ingress) traffic for that instance.\n An egress rule is applied to traffic leaving the instance\n\n ethertype\n Must be IPv4 or IPv6, and addresses represented in CIDR must match the\n ingress or egress rules\n\n project_id\n Specify the project ID this security group will be created on\n (admin-only)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.security_group_rule_create\\\n secgroup_name_or_id=secgroup1\n\n salt '*' neutronng.security_group_rule_create\\\n secgroup_name_or_id=secgroup2 port_range_min=8080\\\n port_range_max=8080 direction='egress'\n\n salt '*' neutronng.security_group_rule_create\\\n secgroup_name_or_id=c0e1d1ce-7296-405e-919d-1c08217be529\\\n protocol=icmp project_id=1dcac318a83b4610b7a7f7ba01465548\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.create_security_group_rule(**kwargs)", "response": "This function creates a security group rule in the neutronng project."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndeletes a security group rule", "response": "def security_group_rule_delete(auth=None, **kwargs):\n '''\n Delete a security group\n\n name_or_id\n The unique ID of the security group rule\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' neutronng.security_group_rule_delete name_or_id=1dcac318a83b4610b7a7f7ba01465548\n\n '''\n cloud = get_operator_cloud(auth)\n kwargs = _clean_kwargs(**kwargs)\n return cloud.delete_security_group_rule(**kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef avail_locations(call=None):\n '''\n Return a dict of all available VM locations on the cloud provider with\n relevant data\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 params = {'Action': 'DescribeRegions'}\n items = query(params=params)\n\n ret = {}\n for region in items['Regions']['Region']:\n ret[region['RegionId']] = {}\n for item in region:\n ret[region['RegionId']][item] = six.text_type(region[item])\n\n return ret", "response": "Return a dict of all available VM locations on the cloud provider with avail_locations relevant data"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef avail_images(kwargs=None, call=None):\n '''\n Return a list of the images that are on the provider\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_images function must be called with '\n '-f or --function, or with the --list-images option'\n )\n\n if not isinstance(kwargs, dict):\n kwargs = {}\n\n provider = get_configured_provider()\n location = provider.get('location', DEFAULT_LOCATION)\n\n if 'location' in kwargs:\n location = kwargs['location']\n\n params = {\n 'Action': 'DescribeImages',\n 'RegionId': location,\n 'PageSize': '100',\n }\n items = query(params=params)\n\n ret = {}\n for image in items['Images']['Image']:\n ret[image['ImageId']] = {}\n for item in image:\n ret[image['ImageId']][item] = six.text_type(image[item])\n\n return ret", "response": "Return a list of the images that are on the provider\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a list of the image sizes that are on the provider", "response": "def avail_sizes(call=None):\n '''\n Return a list of the image sizes that are on the provider\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_sizes function must be called with '\n '-f or --function, or with the --list-sizes option'\n )\n\n params = {'Action': 'DescribeInstanceTypes'}\n items = query(params=params)\n\n ret = {}\n for image in items['InstanceTypes']['InstanceType']:\n ret[image['InstanceTypeId']] = {}\n for item in image:\n ret[image['InstanceTypeId']][item] = six.text_type(image[item])\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlisting all availability zones in the current region", "response": "def list_availability_zones(call=None):\n '''\n List all availability zones in the current region\n '''\n ret = {}\n\n params = {'Action': 'DescribeZones',\n 'RegionId': get_location()}\n items = query(params)\n\n for zone in items['Zones']['Zone']:\n ret[zone['ZoneId']] = {}\n for item in zone:\n ret[zone['ZoneId']][item] = six.text_type(zone[item])\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_nodes_min(call=None):\n '''\n Return a list of the VMs that are on the provider. Only a list of VM names,\n and their state, is returned. This is the minimum amount of information\n needed to check for existing VMs.\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes_min function must be called with -f or --function.'\n )\n\n ret = {}\n location = get_location()\n params = {\n 'Action': 'DescribeInstanceStatus',\n 'RegionId': location,\n }\n nodes = query(params)\n\n log.debug(\n 'Total %s instance found in Region %s',\n nodes['TotalCount'], location\n )\n if 'Code' in nodes or nodes['TotalCount'] == 0:\n return ret\n\n for node in nodes['InstanceStatuses']['InstanceStatus']:\n ret[node['InstanceId']] = {}\n for item in node:\n ret[node['InstanceId']][item] = node[item]\n\n return ret", "response": "Return a list of the VMs that are on the provider that are not in the state of the virtual machine."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_nodes(call=None):\n '''\n Return a list of the VMs that are on the provider\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes function must be called with -f or --function.'\n )\n\n nodes = list_nodes_full()\n ret = {}\n for instanceId in nodes:\n node = nodes[instanceId]\n ret[node['name']] = {\n 'id': node['id'],\n 'name': node['name'],\n 'public_ips': node['public_ips'],\n 'private_ips': node['private_ips'],\n 'size': node['size'],\n 'state': six.text_type(node['state']),\n }\n return ret", "response": "Return a list of the VMs that are on the provider\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list_nodes_full(call=None):\n '''\n Return a list of the VMs that are on the provider\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes_full function must be called with -f '\n 'or --function.'\n )\n\n ret = {}\n location = get_location()\n params = {\n 'Action': 'DescribeInstanceStatus',\n 'RegionId': location,\n 'PageSize': '50'\n }\n result = query(params=params)\n\n log.debug(\n 'Total %s instance found in Region %s',\n result['TotalCount'], location\n )\n if 'Code' in result or result['TotalCount'] == 0:\n return ret\n\n # aliyun max 100 top instance in api\n result_instancestatus = result['InstanceStatuses']['InstanceStatus']\n if result['TotalCount'] > 50:\n params['PageNumber'] = '2'\n result = query(params=params)\n result_instancestatus.update(result['InstanceStatuses']['InstanceStatus'])\n\n for node in result_instancestatus:\n\n instanceId = node.get('InstanceId', '')\n\n params = {\n 'Action': 'DescribeInstanceAttribute',\n 'InstanceId': instanceId\n }\n items = query(params=params)\n if 'Code' in items:\n log.warning('Query instance:%s attribute failed', instanceId)\n continue\n\n name = items['InstanceName']\n ret[name] = {\n 'id': items['InstanceId'],\n 'name': name,\n 'image': items['ImageId'],\n 'size': 'TODO',\n 'state': items['Status']\n }\n for item in items:\n value = items[item]\n if value is not None:\n value = six.text_type(value)\n if item == \"PublicIpAddress\":\n ret[name]['public_ips'] = items[item]['IpAddress']\n if item == \"InnerIpAddress\" and 'private_ips' not in ret[name]:\n ret[name]['private_ips'] = items[item]['IpAddress']\n if item == 'VpcAttributes':\n vpc_ips = items[item]['PrivateIpAddress']['IpAddress']\n if vpc_ips:\n ret[name]['private_ips'] = vpc_ips\n ret[name][item] = value\n\n provider = __active_provider_name__ or 'aliyun'\n if ':' in provider:\n comps = provider.split(':')\n provider = comps[0]\n\n __opts__['update_cachedir'] = True\n __utils__['cloud.cache_node_list'](ret, provider, __opts__)\n\n return ret", "response": "Return a list of the VMs that are on the provider"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of security groups in the VMware environment", "response": "def list_securitygroup(call=None):\n '''\n Return a list of security group\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes function must be called with -f or --function.'\n )\n\n params = {\n 'Action': 'DescribeSecurityGroups',\n 'RegionId': get_location(),\n 'PageSize': '50',\n }\n\n result = query(params)\n if 'Code' in result:\n return {}\n\n ret = {}\n for sg in result['SecurityGroups']['SecurityGroup']:\n ret[sg['SecurityGroupId']] = {}\n for item in sg:\n ret[sg['SecurityGroupId']][item] = sg[item]\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the image object to use for this VM", "response": "def get_image(vm_):\n '''\n Return the image object to use\n '''\n images = avail_images()\n vm_image = six.text_type(config.get_cloud_config_value(\n 'image', vm_, __opts__, search_global=False\n ))\n\n if not vm_image:\n raise SaltCloudNotFound('No image specified for this VM.')\n\n if vm_image and six.text_type(vm_image) in images:\n return images[vm_image]['ImageId']\n raise SaltCloudNotFound(\n 'The specified image, \\'{0}\\', could not be found.'.format(vm_image)\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the security group for this VM", "response": "def get_securitygroup(vm_):\n '''\n Return the security group\n '''\n sgs = list_securitygroup()\n securitygroup = config.get_cloud_config_value(\n 'securitygroup', vm_, __opts__, search_global=False\n )\n\n if not securitygroup:\n raise SaltCloudNotFound('No securitygroup ID specified for this VM.')\n\n if securitygroup and six.text_type(securitygroup) in sgs:\n return sgs[securitygroup]['SecurityGroupId']\n raise SaltCloudNotFound(\n 'The specified security group, \\'{0}\\', could not be found.'.format(\n securitygroup)\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the VM s size. Used by create_node.", "response": "def get_size(vm_):\n '''\n Return the VM's size. Used by create_node().\n '''\n sizes = avail_sizes()\n vm_size = six.text_type(config.get_cloud_config_value(\n 'size', vm_, __opts__, search_global=False\n ))\n\n if not vm_size:\n raise SaltCloudNotFound('No size specified for this VM.')\n\n if vm_size and six.text_type(vm_size) in sizes:\n return sizes[vm_size]['InstanceTypeId']\n\n raise SaltCloudNotFound(\n 'The specified size, \\'{0}\\', could not be found.'.format(vm_size)\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the VM s location", "response": "def __get_location(vm_):\n '''\n Return the VM's location\n '''\n locations = avail_locations()\n vm_location = six.text_type(config.get_cloud_config_value(\n 'location', vm_, __opts__, search_global=False\n ))\n\n if not vm_location:\n raise SaltCloudNotFound('No location specified for this VM.')\n\n if vm_location and six.text_type(vm_location) in locations:\n return locations[vm_location]['RegionId']\n raise SaltCloudNotFound(\n 'The specified location, \\'{0}\\', could not be found.'.format(\n vm_location\n )\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef start(name, call=None):\n '''\n Start a node\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -a start myinstance\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The stop action must be called with -a or --action.'\n )\n\n log.info('Starting node %s', name)\n\n instanceId = _get_node(name)['InstanceId']\n\n params = {'Action': 'StartInstance',\n 'InstanceId': instanceId}\n result = query(params)\n\n return result", "response": "Start a node\nattery"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef stop(name, force=False, call=None):\n '''\n Stop a node\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -a stop myinstance\n salt-cloud -a stop myinstance force=True\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The stop action must be called with -a or --action.'\n )\n\n log.info('Stopping node %s', name)\n\n instanceId = _get_node(name)['InstanceId']\n\n params = {\n 'Action': 'StopInstance',\n 'InstanceId': instanceId,\n 'ForceStop': six.text_type(force).lower()\n }\n result = query(params)\n\n return result", "response": "Stop a node in the cluster"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\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 'aliyun',\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 log.info('Creating Cloud VM %s', vm_['name'])\n kwargs = {\n 'name': vm_['name'],\n 'size_id': get_size(vm_),\n 'image_id': get_image(vm_),\n 'region_id': __get_location(vm_),\n 'securitygroup_id': get_securitygroup(vm_),\n }\n if 'vswitch_id' in vm_:\n kwargs['VSwitchId'] = vm_['vswitch_id']\n if 'internet_chargetype' in vm_:\n kwargs['InternetChargeType'] = vm_['internet_chargetype']\n if 'internet_maxbandwidthin' in vm_:\n kwargs['InternetMaxBandwidthIn'] = six.text_type(vm_['internet_maxbandwidthin'])\n if 'internet_maxbandwidthout' in vm_:\n kwargs['InternetMaxBandwidthOut'] = six.text_type(vm_['internet_maxbandwidthOut'])\n if 'hostname' in vm_:\n kwargs['HostName'] = vm_['hostname']\n if 'password' in vm_:\n kwargs['Password'] = vm_['password']\n if 'instance_name' in vm_:\n kwargs['InstanceName'] = vm_['instance_name']\n if 'systemdisk_category' in vm_:\n kwargs['SystemDisk.Category'] = vm_['systemdisk_category']\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(vm_['name']),\n args=__utils__['cloud.filter_event']('requesting', kwargs, list(kwargs)),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n try:\n ret = create_node(kwargs)\n except Exception as exc:\n log.error(\n 'Error creating %s on Aliyun ECS\\n\\n'\n 'The following exception was thrown when trying to '\n 'run the initial deployment: %s',\n vm_['name'], six.text_type(exc),\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n return False\n # repair ip address error and start vm\n time.sleep(8)\n params = {'Action': 'StartInstance',\n 'InstanceId': ret}\n query(params)\n\n def __query_node_data(vm_name):\n data = show_instance(vm_name, call='action')\n if not data:\n # Trigger an error in the wait_for_ip function\n return False\n if data.get('PublicIpAddress', None) is not None:\n return data\n\n try:\n data = salt.utils.cloud.wait_for_ip(\n __query_node_data,\n update_args=(vm_['name'],),\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 if data['public_ips']:\n ssh_ip = data['public_ips'][0]\n elif data['private_ips']:\n ssh_ip = data['private_ips'][0]\n else:\n log.info('No available ip:cant connect to salt')\n return False\n log.debug('VM %s is now running', ssh_ip)\n vm_['ssh_host'] = ssh_ip\n\n # The instance is booted and accessible, let's Salt it!\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": "Implement a function in Python 3 to\ncompute signature for the aliyun request.", "response": "def _compute_signature(parameters, access_key_secret):\n '''\n Generate aliyun request signature\n '''\n\n def percent_encode(line):\n if not isinstance(line, six.string_types):\n return line\n\n s = line\n if sys.stdin.encoding is None:\n s = line.decode().encode('utf8')\n else:\n s = line.decode(sys.stdin.encoding).encode('utf8')\n res = _quote(s, '')\n res = res.replace('+', '%20')\n res = res.replace('*', '%2A')\n res = res.replace('%7E', '~')\n return res\n\n sortedParameters = sorted(list(parameters.items()), key=lambda items: items[0])\n\n canonicalizedQueryString = ''\n for k, v in sortedParameters:\n canonicalizedQueryString += '&' + percent_encode(k) \\\n + '=' + percent_encode(v)\n\n # All aliyun API only support GET method\n stringToSign = 'GET&%2F&' + percent_encode(canonicalizedQueryString[1:])\n\n h = hmac.new(to_bytes(access_key_secret + \"&\"), stringToSign, sha1)\n signature = base64.encodestring(h.digest()).strip()\n return signature"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef query(params=None):\n '''\n Make a web call to aliyun ECS REST API\n '''\n path = 'https://ecs-cn-hangzhou.aliyuncs.com'\n\n access_key_id = config.get_cloud_config_value(\n 'id', get_configured_provider(), __opts__, search_global=False\n )\n access_key_secret = config.get_cloud_config_value(\n 'key', get_configured_provider(), __opts__, search_global=False\n )\n\n timestamp = time.strftime(\"%Y-%m-%dT%H:%M:%SZ\", time.gmtime())\n\n # public interface parameters\n parameters = {\n 'Format': 'JSON',\n 'Version': DEFAULT_ALIYUN_API_VERSION,\n 'AccessKeyId': access_key_id,\n 'SignatureVersion': '1.0',\n 'SignatureMethod': 'HMAC-SHA1',\n 'SignatureNonce': six.text_type(uuid.uuid1()),\n 'TimeStamp': timestamp,\n }\n\n # include action or function parameters\n if params:\n parameters.update(params)\n\n # Calculate the string for Signature\n signature = _compute_signature(parameters, access_key_secret)\n parameters['Signature'] = signature\n\n request = requests.get(path, params=parameters, verify=True)\n if request.status_code != 200:\n raise SaltCloudSystemExit(\n 'An error occurred while querying aliyun ECS. HTTP Code: {0} '\n 'Error: \\'{1}\\''.format(\n request.status_code,\n request.text\n )\n )\n\n log.debug(request.url)\n\n content = request.text\n\n result = salt.utils.json.loads(content)\n if 'Code' in result:\n raise SaltCloudSystemExit(\n pprint.pformat(result.get('Message', {}))\n )\n return result", "response": "Query the ECS API for the current resource."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef show_disk(name, call=None):\n '''\n Show the disk details of the instance\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -a show_disk aliyun myinstance\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The show_disks action must be called with -a or --action.'\n )\n\n ret = {}\n params = {\n 'Action': 'DescribeInstanceDisks',\n 'InstanceId': name\n }\n items = query(params=params)\n\n for disk in items['Disks']['Disk']:\n ret[disk['DiskId']] = {}\n for item in disk:\n ret[disk['DiskId']][item] = six.text_type(disk[item])\n\n return ret", "response": "Show the details of the instance with the specified name."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist the monitor data of an instance.", "response": "def list_monitor_data(kwargs=None, call=None):\n '''\n Get monitor data of the instance. If instance name is\n missing, will show all the instance monitor data on the region.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f list_monitor_data aliyun\n salt-cloud -f list_monitor_data aliyun name=AY14051311071990225bd\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_monitor_data must be called with -f or --function.'\n )\n\n if not isinstance(kwargs, dict):\n kwargs = {}\n\n ret = {}\n params = {\n 'Action': 'GetMonitorData',\n 'RegionId': get_location()\n }\n if 'name' in kwargs:\n params['InstanceId'] = kwargs['name']\n\n items = query(params=params)\n\n monitorData = items['MonitorData']\n\n for data in monitorData['InstanceMonitorData']:\n ret[data['InstanceId']] = {}\n for item in data:\n ret[data['InstanceId']][item] = six.text_type(data[item])\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef show_image(kwargs, call=None):\n '''\n Show the details from aliyun image\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_images function must be called with '\n '-f or --function'\n )\n\n if not isinstance(kwargs, dict):\n kwargs = {}\n\n location = get_location()\n if 'location' in kwargs:\n location = kwargs['location']\n\n params = {\n 'Action': 'DescribeImages',\n 'RegionId': location,\n 'ImageId': kwargs['image']\n }\n\n ret = {}\n items = query(params=params)\n # DescribeImages so far support input multi-image. And\n # if not found certain image, the response will include\n # blank image list other than 'not found' error message\n if 'Code' in items or not items['Images']['Image']:\n raise SaltCloudNotFound('The specified image could not be found.')\n\n log.debug(\n 'Total %s image found in Region %s',\n items['TotalCount'], location\n )\n\n for image in items['Images']['Image']:\n ret[image['ImageId']] = {}\n for item in image:\n ret[image['ImageId']][item] = six.text_type(image[item])\n\n return ret", "response": "Show the details from aliyun image"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef destroy(name, call=None):\n '''\n Destroy a node.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -a destroy myinstance\n salt-cloud -d myinstance\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 instanceId = _get_node(name)['InstanceId']\n\n # have to stop instance before del it\n stop_params = {\n 'Action': 'StopInstance',\n 'InstanceId': instanceId\n }\n query(stop_params)\n\n params = {\n 'Action': 'DeleteInstance',\n 'InstanceId': instanceId\n }\n\n node = query(params)\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 node", "response": "Destroy a node.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -a destroy myinstance\n salt-cloud -d myinstance"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a temporary adminfile based on the keyword arguments passed to pkg. install.", "response": "def _write_adminfile(kwargs):\n '''\n Create a temporary adminfile based on the keyword arguments passed to\n pkg.install.\n '''\n # Set the adminfile default variables\n email = kwargs.get('email', '')\n instance = kwargs.get('instance', 'quit')\n partial = kwargs.get('partial', 'nocheck')\n runlevel = kwargs.get('runlevel', 'nocheck')\n idepend = kwargs.get('idepend', 'nocheck')\n rdepend = kwargs.get('rdepend', 'nocheck')\n space = kwargs.get('space', 'nocheck')\n setuid = kwargs.get('setuid', 'nocheck')\n conflict = kwargs.get('conflict', 'nocheck')\n action = kwargs.get('action', 'nocheck')\n basedir = kwargs.get('basedir', 'default')\n\n # Make tempfile to hold the adminfile contents.\n adminfile = salt.utils.files.mkstemp(prefix=\"salt-\")\n\n def _write_line(fp_, line):\n fp_.write(salt.utils.stringutils.to_str(line))\n\n with salt.utils.files.fopen(adminfile, 'w') as fp_:\n _write_line(fp_, 'email={0}\\n'.format(email))\n _write_line(fp_, 'instance={0}\\n'.format(instance))\n _write_line(fp_, 'partial={0}\\n'.format(partial))\n _write_line(fp_, 'runlevel={0}\\n'.format(runlevel))\n _write_line(fp_, 'idepend={0}\\n'.format(idepend))\n _write_line(fp_, 'rdepend={0}\\n'.format(rdepend))\n _write_line(fp_, 'space={0}\\n'.format(space))\n _write_line(fp_, 'setuid={0}\\n'.format(setuid))\n _write_line(fp_, 'conflict={0}\\n'.format(conflict))\n _write_line(fp_, 'action={0}\\n'.format(action))\n _write_line(fp_, 'basedir={0}\\n'.format(basedir))\n\n return adminfile"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list_pkgs(versions_as_list=False, **kwargs):\n '''\n List the packages currently installed as a dict:\n\n .. code-block:: python\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 = '/usr/bin/pkginfo -x'\n\n # Package information returned two lines per package. On even-offset\n # lines, the package name is in the first column. On odd-offset lines, the\n # package version is in the second column.\n lines = __salt__['cmd.run'](\n cmd,\n output_loglevel='trace',\n python_shell=False).splitlines()\n for index, line in enumerate(lines):\n if index % 2 == 0:\n name = line.split()[0].strip()\n if index % 2 == 1:\n version_num = line.split()[1].strip()\n __salt__['pkg_resource.add_pkg'](ret, name, version_num)\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 as a dict"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef install(name=None, sources=None, saltenv='base', **kwargs):\n '''\n Install the passed package. Can install packages from the following\n sources:\n\n * Locally (package already exists on the minion\n * HTTP/HTTPS server\n * FTP server\n * Salt master\n\n Returns a dict containing the new package names and versions:\n\n .. code-block:: python\n\n {'': {'old': '',\n 'new': ''}}\n\n CLI Examples:\n\n .. code-block:: bash\n\n # Installing a data stream pkg that already exists on the minion\n\n salt '*' pkg.install sources='[{\"\": \"/dir/on/minion/\"}]'\n salt '*' pkg.install sources='[{\"SMClgcc346\": \"/var/spool/pkg/gcc-3.4.6-sol10-sparc-local.pkg\"}]'\n\n # Installing a data stream pkg that exists on the salt master\n\n salt '*' pkg.install sources='[{\"\": \"salt://pkgs/\"}]'\n salt '*' pkg.install sources='[{\"SMClgcc346\": \"salt://pkgs/gcc-3.4.6-sol10-sparc-local.pkg\"}]'\n\n CLI Example:\n\n .. code-block:: bash\n\n # Installing a data stream pkg that exists on a HTTP server\n salt '*' pkg.install sources='[{\"\": \"http://packages.server.com/\"}]'\n salt '*' pkg.install sources='[{\"SMClgcc346\": \"http://packages.server.com/gcc-3.4.6-sol10-sparc-local.pkg\"}]'\n\n If working with solaris zones and you want to install a package only in the\n global zone you can pass 'current_zone_only=True' to salt to have the\n package only installed in the global zone. (Behind the scenes this is\n passing '-G' to the pkgadd command.) Solaris default when installing a\n package in the global zone is to install it in all zones. This overrides\n that and installs the package only in the global.\n\n CLI Example:\n\n .. code-block:: bash\n\n # Installing a data stream package only in the global zone:\n salt 'global_zone' pkg.install sources='[{\"SMClgcc346\": \"/var/spool/pkg/gcc-3.4.6-sol10-sparc-local.pkg\"}]' current_zone_only=True\n\n By default salt automatically provides an adminfile, to automate package\n installation, with these options set::\n\n email=\n instance=quit\n partial=nocheck\n runlevel=nocheck\n idepend=nocheck\n rdepend=nocheck\n space=nocheck\n setuid=nocheck\n conflict=nocheck\n action=nocheck\n basedir=default\n\n You can override any of these options in two ways. First you can optionally\n pass any of the options as a kwarg to the module/state to override the\n default value or you can optionally pass the 'admin_source' option\n providing your own adminfile to the minions.\n\n Note: You can find all of the possible options to provide to the adminfile\n by reading the admin man page:\n\n .. code-block:: bash\n\n man -s 4 admin\n\n CLI Example:\n\n .. code-block:: bash\n\n # Overriding the 'instance' adminfile option when calling the module directly\n salt '*' pkg.install sources='[{\"\": \"salt://pkgs/\"}]' instance=\"overwrite\"\n\n SLS Example:\n\n .. code-block:: yaml\n\n # Overriding the 'instance' adminfile option when used in a state\n\n SMClgcc346:\n pkg.installed:\n - sources:\n - SMClgcc346: salt://srv/salt/pkgs/gcc-3.4.6-sol10-sparc-local.pkg\n - instance: overwrite\n\n .. note::\n The ID declaration is ignored, as the package name is read from the\n ``sources`` parameter.\n\n CLI Example:\n\n .. code-block:: bash\n\n # Providing your own adminfile when calling the module directly\n\n salt '*' pkg.install sources='[{\"\": \"salt://pkgs/\"}]' admin_source='salt://pkgs/'\n\n # Providing your own adminfile when using states\n\n :\n pkg.installed:\n - sources:\n - : salt://pkgs/\n - admin_source: salt://pkgs/\n\n .. note::\n The ID declaration is ignored, as the package name is read from the\n ``sources`` parameter.\n '''\n if salt.utils.data.is_true(kwargs.get('refresh')):\n log.warning('\\'refresh\\' argument not implemented for solarispkg '\n 'module')\n\n # pkgs is not supported, but must be passed here for API compatibility\n pkgs = kwargs.pop('pkgs', None)\n try:\n pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](\n name, pkgs, sources, **kwargs\n )\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n if not pkg_params:\n return {}\n\n if not sources:\n log.error('\"sources\" param required for solaris pkg_add installs')\n return {}\n\n try:\n if 'admin_source' in kwargs:\n adminfile = __salt__['cp.cache_file'](kwargs['admin_source'], saltenv)\n else:\n adminfile = _write_adminfile(kwargs)\n\n old = list_pkgs()\n cmd_prefix = ['/usr/sbin/pkgadd', '-n', '-a', adminfile]\n\n # Only makes sense in a global zone but works fine in non-globals.\n if kwargs.get('current_zone_only') == 'True':\n cmd_prefix += '-G '\n\n errors = []\n for pkg in pkg_params:\n cmd = cmd_prefix + ['-d', pkg, 'all']\n # Install the package{s}\n out = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n\n if out['retcode'] != 0 and out['stderr']:\n errors.append(out['stderr'])\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 finally:\n # Remove the temp adminfile\n if 'admin_source' not in kwargs:\n try:\n os.remove(adminfile)\n except (NameError, OSError):\n pass\n\n return ret", "response": "Installs the passed package from the specified sources."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nremoving packages with pkgrm", "response": "def remove(name=None, pkgs=None, saltenv='base', **kwargs):\n '''\n Remove packages with pkgrm\n\n name\n The name of the package to be deleted\n\n By default salt automatically provides an adminfile, to automate package\n removal, with these options set::\n\n email=\n instance=quit\n partial=nocheck\n runlevel=nocheck\n idepend=nocheck\n rdepend=nocheck\n space=nocheck\n setuid=nocheck\n conflict=nocheck\n action=nocheck\n basedir=default\n\n You can override any of these options in two ways. First you can optionally\n pass any of the options as a kwarg to the module/state to override the\n default value or you can optionally pass the 'admin_source' option\n providing your own adminfile to the minions.\n\n Note: You can find all of the possible options to provide to the adminfile\n by reading the admin man page:\n\n .. code-block:: bash\n\n man -s 4 admin\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 SUNWgit\n salt '*' pkg.remove ,,\n salt '*' pkg.remove pkgs='[\"foo\", \"bar\"]'\n '''\n try:\n pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[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\n try:\n if 'admin_source' in kwargs:\n adminfile = __salt__['cp.cache_file'](kwargs['admin_source'], saltenv)\n else:\n # Make tempfile to hold the adminfile contents.\n adminfile = _write_adminfile(kwargs)\n\n # Remove the package\n cmd = ['/usr/sbin/pkgrm', '-n', '-a', adminfile] + targets\n out = __salt__['cmd.run_all'](cmd,\n python_shell=False,\n output_loglevel='trace')\n\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 finally:\n # Remove the temp adminfile\n if 'admin_source' not in kwargs:\n try:\n os.remove(adminfile)\n except (NameError, OSError):\n pass\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nensure that a specific state definition for a specific log file is present in the given trail.", "response": "def present(name, Name,\n S3BucketName, S3KeyPrefix=None,\n SnsTopicName=None,\n IncludeGlobalServiceEvents=True,\n IsMultiRegionTrail=None,\n EnableLogFileValidation=False,\n CloudWatchLogsLogGroupArn=None,\n CloudWatchLogsRoleArn=None,\n KmsKeyId=None,\n LoggingEnabled=True,\n Tags=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure trail exists.\n\n name\n The name of the state definition\n\n Name\n Name of the trail.\n\n S3BucketName\n Specifies the name of the Amazon S3 bucket designated for publishing log\n files.\n\n S3KeyPrefix\n Specifies the Amazon S3 key prefix that comes after the name of the\n bucket you have designated for log file delivery.\n\n SnsTopicName\n Specifies the name of the Amazon SNS topic defined for notification of\n log file delivery. The maximum length is 256 characters.\n\n IncludeGlobalServiceEvents\n Specifies whether the trail is publishing events from global services\n such as IAM to the log files.\n\n EnableLogFileValidation\n Specifies whether log file integrity validation is enabled. The default\n is false.\n\n CloudWatchLogsLogGroupArn\n Specifies a log group name using an Amazon Resource Name (ARN), a unique\n identifier that represents the log group to which CloudTrail logs will\n be delivered. Not required unless you specify CloudWatchLogsRoleArn.\n\n CloudWatchLogsRoleArn\n Specifies the role for the CloudWatch Logs endpoint to assume to write\n to a user's log group.\n\n KmsKeyId\n Specifies the KMS key ID to use to encrypt the logs delivered by\n CloudTrail. The value can be a an alias name prefixed by \"alias/\", a\n fully specified ARN to an alias, a fully specified ARN to a key, or a\n globally unique identifier.\n\n LoggingEnabled\n Whether logging should be enabled for the trail\n\n Tags\n A dictionary of tags that should be set on the trail\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n '''\n ret = {'name': Name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n r = __salt__['boto_cloudtrail.exists'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile)\n\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to create trail: {0}.'.format(r['error']['message'])\n return ret\n\n if not r.get('exists'):\n if __opts__['test']:\n ret['comment'] = 'CloudTrail {0} is set to be created.'.format(Name)\n ret['result'] = None\n return ret\n r = __salt__['boto_cloudtrail.create'](Name=Name,\n S3BucketName=S3BucketName,\n S3KeyPrefix=S3KeyPrefix,\n SnsTopicName=SnsTopicName,\n IncludeGlobalServiceEvents=IncludeGlobalServiceEvents,\n IsMultiRegionTrail=IsMultiRegionTrail,\n EnableLogFileValidation=EnableLogFileValidation,\n CloudWatchLogsLogGroupArn=CloudWatchLogsLogGroupArn,\n CloudWatchLogsRoleArn=CloudWatchLogsRoleArn,\n KmsKeyId=KmsKeyId,\n region=region, key=key, keyid=keyid, profile=profile)\n if not r.get('created'):\n ret['result'] = False\n ret['comment'] = 'Failed to create trail: {0}.'.format(r['error']['message'])\n return ret\n _describe = __salt__['boto_cloudtrail.describe'](Name,\n region=region, key=key, keyid=keyid, profile=profile)\n ret['changes']['old'] = {'trail': None}\n ret['changes']['new'] = _describe\n ret['comment'] = 'CloudTrail {0} created.'.format(Name)\n\n if LoggingEnabled:\n r = __salt__['boto_cloudtrail.start_logging'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to create trail: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n ret['changes']['new']['trail']['LoggingEnabled'] = True\n else:\n ret['changes']['new']['trail']['LoggingEnabled'] = False\n\n if bool(Tags):\n r = __salt__['boto_cloudtrail.add_tags'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile, **Tags)\n if not r.get('tagged'):\n ret['result'] = False\n ret['comment'] = 'Failed to create trail: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n ret['changes']['new']['trail']['Tags'] = Tags\n return ret\n\n ret['comment'] = os.linesep.join([ret['comment'], 'CloudTrail {0} is present.'.format(Name)])\n ret['changes'] = {}\n # trail exists, ensure config matches\n _describe = __salt__['boto_cloudtrail.describe'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in _describe:\n ret['result'] = False\n ret['comment'] = 'Failed to update trail: {0}.'.format(_describe['error']['message'])\n ret['changes'] = {}\n return ret\n _describe = _describe.get('trail')\n\n r = __salt__['boto_cloudtrail.status'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile)\n _describe['LoggingEnabled'] = r.get('trail', {}).get('IsLogging', False)\n\n need_update = False\n bucket_vars = {'S3BucketName': 'S3BucketName',\n 'S3KeyPrefix': 'S3KeyPrefix',\n 'SnsTopicName': 'SnsTopicName',\n 'IncludeGlobalServiceEvents': 'IncludeGlobalServiceEvents',\n 'IsMultiRegionTrail': 'IsMultiRegionTrail',\n 'EnableLogFileValidation': 'LogFileValidationEnabled',\n 'CloudWatchLogsLogGroupArn': 'CloudWatchLogsLogGroupArn',\n 'CloudWatchLogsRoleArn': 'CloudWatchLogsRoleArn',\n 'KmsKeyId': 'KmsKeyId',\n 'LoggingEnabled': 'LoggingEnabled'}\n\n for invar, outvar in six.iteritems(bucket_vars):\n if _describe[outvar] != locals()[invar]:\n need_update = True\n ret['changes'].setdefault('new', {})[invar] = locals()[invar]\n ret['changes'].setdefault('old', {})[invar] = _describe[outvar]\n\n r = __salt__['boto_cloudtrail.list_tags'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile)\n _describe['Tags'] = r.get('tags', {})\n tagchange = salt.utils.data.compare_dicts(_describe['Tags'], Tags)\n if bool(tagchange):\n need_update = True\n ret['changes'].setdefault('new', {})['Tags'] = Tags\n ret['changes'].setdefault('old', {})['Tags'] = _describe['Tags']\n\n if need_update:\n if __opts__['test']:\n msg = 'CloudTrail {0} set to be modified.'.format(Name)\n ret['comment'] = msg\n ret['result'] = None\n return ret\n\n ret['comment'] = os.linesep.join([ret['comment'], 'CloudTrail to be modified'])\n r = __salt__['boto_cloudtrail.update'](Name=Name,\n S3BucketName=S3BucketName,\n S3KeyPrefix=S3KeyPrefix,\n SnsTopicName=SnsTopicName,\n IncludeGlobalServiceEvents=IncludeGlobalServiceEvents,\n IsMultiRegionTrail=IsMultiRegionTrail,\n EnableLogFileValidation=EnableLogFileValidation,\n CloudWatchLogsLogGroupArn=CloudWatchLogsLogGroupArn,\n CloudWatchLogsRoleArn=CloudWatchLogsRoleArn,\n KmsKeyId=KmsKeyId,\n region=region, key=key, keyid=keyid, profile=profile)\n if not r.get('updated'):\n ret['result'] = False\n ret['comment'] = 'Failed to update trail: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n\n if LoggingEnabled:\n r = __salt__['boto_cloudtrail.start_logging'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile)\n if not r.get('started'):\n ret['result'] = False\n ret['comment'] = 'Failed to update trail: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n else:\n r = __salt__['boto_cloudtrail.stop_logging'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile)\n if not r.get('stopped'):\n ret['result'] = False\n ret['comment'] = 'Failed to update trail: {0}.'.format(r['error']['message'])\n ret['changes'] = {}\n return ret\n\n if bool(tagchange):\n adds = {}\n removes = {}\n for k, diff in six.iteritems(tagchange):\n if diff.get('new', '') != '':\n # there's an update for this key\n adds[k] = Tags[k]\n elif diff.get('old', '') != '':\n removes[k] = _describe['Tags'][k]\n if bool(adds):\n r = __salt__['boto_cloudtrail.add_tags'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile, **adds)\n if bool(removes):\n r = __salt__['boto_cloudtrail.remove_tags'](Name=Name,\n region=region, key=key, keyid=keyid, profile=profile,\n **removes)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures that the CloudTrail trail with passed properties is absent.", "response": "def absent(name, Name,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure trail with passed properties is absent.\n\n name\n The name of the state definition.\n\n Name\n Name of the trail.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n '''\n\n ret = {'name': Name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n r = __salt__['boto_cloudtrail.exists'](Name,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to delete trail: {0}.'.format(r['error']['message'])\n return ret\n\n if r and not r['exists']:\n ret['comment'] = 'CloudTrail {0} does not exist.'.format(Name)\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'CloudTrail {0} is set to be removed.'.format(Name)\n ret['result'] = None\n return ret\n r = __salt__['boto_cloudtrail.delete'](Name,\n region=region, key=key,\n keyid=keyid, profile=profile)\n if not r['deleted']:\n ret['result'] = False\n ret['comment'] = 'Failed to delete trail: {0}.'.format(r['error']['message'])\n return ret\n ret['changes']['old'] = {'trail': Name}\n ret['changes']['new'] = {'trail': None}\n ret['comment'] = 'CloudTrail {0} deleted.'.format(Name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef iter_transport_opts(opts):\n '''\n Yield transport, opts for all master configured transports\n '''\n transports = set()\n\n for transport, opts_overrides in six.iteritems(opts.get('transport_opts', {})):\n t_opts = dict(opts)\n t_opts.update(opts_overrides)\n t_opts['transport'] = transport\n transports.add(transport)\n yield transport, t_opts\n\n if opts['transport'] not in transports:\n yield opts['transport'], opts", "response": "Iterate over all master configured transports and opts."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck if the value in the given register location is greater than the given value.", "response": "def gt(name, value):\n '''\n Only succeed if the value in the given register location is greater than\n the given value\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n check.gt:\n - value: 42\n\n run_remote_ex:\n local.cmd:\n - tgt: '*'\n - func: test.ping\n - require:\n - check: foo\n '''\n ret = {'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}}\n if name not in __reg__:\n ret['result'] = False\n ret['comment'] = 'Value {0} not in register'.format(name)\n return ret\n if __reg__[name]['val'] > value:\n ret['result'] = True\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef gte(name, value):\n '''\n Only succeed if the value in the given register location is greater or equal\n than the given value\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n check.gte:\n - value: 42\n\n run_remote_ex:\n local.cmd:\n - tgt: '*'\n - func: test.ping\n - require:\n - check: foo\n '''\n ret = {'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}}\n if name not in __reg__:\n ret['result'] = False\n ret['comment'] = 'Value {0} not in register'.format(name)\n return ret\n if __reg__[name]['val'] >= value:\n ret['result'] = True\n return ret", "response": "Check if the value in the given register location is greater or equal to the given value."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef lt(name, value):\n '''\n Only succeed if the value in the given register location is less than\n the given value\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n check.lt:\n - value: 42\n\n run_remote_ex:\n local.cmd:\n - tgt: '*'\n - func: test.ping\n - require:\n - check: foo\n '''\n ret = {'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}}\n if name not in __reg__:\n ret['result'] = False\n ret['comment'] = 'Value {0} not in register'.format(name)\n return ret\n if __reg__[name]['val'] < value:\n ret['result'] = True\n return ret", "response": "Check if the value in the given register location is less than the given value."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef lte(name, value):\n '''\n Only succeed if the value in the given register location is less than\n or equal the given value\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n check.lte:\n - value: 42\n\n run_remote_ex:\n local.cmd:\n - tgt: '*'\n - func: test.ping\n - require:\n - check: foo\n '''\n ret = {'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}}\n if name not in __reg__:\n ret['result'] = False\n ret['comment'] = 'Value {0} not in register'.format(name)\n return ret\n if __reg__[name]['val'] <= value:\n ret['result'] = True\n return ret", "response": "Only succeed if the value in the given register location is less than the given value"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck if the value in the given register location is equal to the given value.", "response": "def eq(name, value):\n '''\n Only succeed if the value in the given register location is equal to\n the given value\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n check.eq:\n - value: 42\n\n run_remote_ex:\n local.cmd:\n - tgt: '*'\n - func: test.ping\n - require:\n - check: foo\n '''\n ret = {'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}}\n if name not in __reg__:\n ret['result'] = False\n ret['comment'] = 'Value {0} not in register'.format(name)\n return ret\n if __reg__[name]['val'] == value:\n ret['result'] = True\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef ne(name, value):\n '''\n Only succeed if the value in the given register location is not equal to\n the given value\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n check.ne:\n - value: 42\n\n run_remote_ex:\n local.cmd:\n - tgt: '*'\n - func: test.ping\n - require:\n - check: foo\n '''\n ret = {'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}}\n if name not in __reg__:\n ret['result'] = False\n ret['comment'] = 'Value {0} not in register'.format(name)\n return ret\n if __reg__[name]['val'] != value:\n ret['result'] = True\n return ret", "response": "Only succeed if the value in the given register location is not equal to the given value"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef contains(name,\n value,\n count_lt=None,\n count_lte=None,\n count_eq=None,\n count_gte=None,\n count_gt=None,\n count_ne=None):\n '''\n Only succeed if the value in the given register location contains\n the given value\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n check.contains:\n - value: itni\n\n run_remote_ex:\n local.cmd:\n - tgt: '*'\n - func: test.ping\n - require:\n - check: foo\n '''\n ret = {'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}}\n if name not in __reg__:\n ret['result'] = False\n ret['comment'] = 'Value {0} not in register'.format(name)\n return ret\n try:\n count_compare = count_lt or count_lte or count_eq or\\\n count_gte or count_gt or count_ne\n if count_compare:\n occurrences = __reg__[name]['val'].count(value)\n log.debug('%s appears %s times', value, occurrences)\n ret['result'] = True\n if count_lt:\n ret['result'] &= occurrences < count_lt\n if count_lte:\n ret['result'] &= occurrences <= count_lte\n if count_eq:\n ret['result'] &= occurrences == count_eq\n if count_gte:\n ret['result'] &= occurrences >= count_gte\n if count_gt:\n ret['result'] &= occurrences > count_gt\n if count_ne:\n ret['result'] &= occurrences != count_ne\n else:\n if value in __reg__[name]['val']:\n ret['result'] = True\n except TypeError:\n pass\n return ret", "response": "Check if the given value is in the given register location."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncheck if a specific event is in a Chekcs list and returns result True if the match is happens ador", "response": "def event(name):\n '''\n Chekcs for a specific event match and returns result True if the match\n happens\n\n USAGE:\n\n .. code-block:: yaml\n\n salt/foo/*/bar:\n check.event\n\n run_remote_ex:\n local.cmd:\n - tgt: '*'\n - func: test.ping\n - require:\n - check: salt/foo/*/bar\n '''\n ret = {'name': name,\n 'changes': {},\n 'comment': '',\n 'result': False}\n\n for event in __events__:\n if salt.utils.stringutils.expr_match(event['tag'], name):\n ret['result'] = True\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn True if the length of the given register location is greater than the given value.", "response": "def len_gt(name, value):\n '''\n Only succeed if length of the given register location is greater than\n the given value.\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n check.len_gt:\n - value: 42\n\n run_remote_ex:\n local.cmd:\n - tgt: '*'\n - func: test.ping\n - require:\n - check: foo\n '''\n ret = {'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {}}\n if name not in __reg__:\n ret['result'] = False\n ret['comment'] = 'Value {0} not in register'.format(name)\n return ret\n if len(__reg__[name]['val']) > value:\n ret['result'] = True\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_users(users, test=False, commit=True, **kwargs): # pylint: disable=unused-argument\n\n '''\n Configures users on network devices.\n\n :param users: Dictionary formatted as the output of the function config()\n\n :param test: Dry run? If set as True, will apply the config, discard and\n return the changes. Default: False\n\n :param commit: Commit? (default: True) Sometimes it is not needed to commit\n the config immediately after loading the changes. E.g.: a state loads a\n couple of parts (add / remove / update) and would not be optimal to\n commit after each operation. Also, from the CLI when the user needs to\n apply the similar changes before committing, can specify commit=False\n and will not discard the config.\n\n :raise MergeConfigException: If there is an error on the configuration sent.\n :return a dictionary having the following keys:\n\n - result (bool): if the config was applied successfully. It is `False` only\n in case of failure. In case there are no changes to be applied and\n successfully performs all operations it is still `True` and so will be\n the `already_configured` flag (example below)\n - comment (str): a message for the user\n - already_configured (bool): flag to check if there were no changes applied\n - diff (str): returns the config changes applied\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' users.set_users \"{'mircea': {}}\"\n '''\n\n return __salt__['net.load_template']('set_users',\n users=users,\n test=test,\n commit=commit,\n inherit_napalm_device=napalm_device)", "response": "Configure the users on network devices."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef map_clonemode(vm_info):\n mode_map = {\n 'state': 0,\n 'child': 1,\n 'all': 2\n }\n\n if not vm_info:\n return DEFAULT_CLONE_MODE\n\n if 'clonemode' not in vm_info:\n return DEFAULT_CLONE_MODE\n\n if vm_info['clonemode'] in mode_map:\n return mode_map[vm_info['clonemode']]\n else:\n raise SaltCloudSystemExit(\n \"Illegal clonemode for virtualbox profile. Legal values are: {}\".format(','.join(mode_map.keys()))\n )", "response": "Map the virtualbox config file values for clone_mode into the integers the API requires\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create(vm_info):\n '''\n Creates a virtual machine from the given VM information\n\n This is what is used to request a virtual machine to be created by the\n cloud provider, wait for it to become available, and then (optionally) log\n in and install Salt on it.\n\n Events fired:\n\n This function fires the event ``salt/cloud/vm_name/creating``, with the\n payload containing the names of the VM, profile, and provider.\n\n @param vm_info\n\n .. code-block:: text\n\n {\n name: \n profile: \n driver: :\n clonefrom: \n clonemode: (default: state, choices: state, child, all)\n }\n\n @type vm_info dict\n @return dict of resulting vm. !!!Passwords can and should be included!!!\n '''\n try:\n # Check for required profile parameters before sending any API calls.\n if vm_info['profile'] and config.is_profile_configured(\n __opts__,\n __active_provider_name__ or 'virtualbox',\n vm_info['profile']\n ) is False:\n return False\n except AttributeError:\n pass\n\n vm_name = vm_info[\"name\"]\n deploy = config.get_cloud_config_value(\n 'deploy', vm_info, __opts__, search_global=False, default=True\n )\n wait_for_ip_timeout = config.get_cloud_config_value(\n 'wait_for_ip_timeout', vm_info, __opts__, default=60\n )\n boot_timeout = config.get_cloud_config_value(\n 'boot_timeout', vm_info, __opts__, default=60 * 1000\n )\n power = config.get_cloud_config_value(\n 'power_on', vm_info, __opts__, default=False\n )\n key_filename = config.get_cloud_config_value(\n 'private_key', vm_info, __opts__, search_global=False, default=None\n )\n clone_mode = map_clonemode(vm_info)\n wait_for_pattern = vm_info['waitforpattern'] if 'waitforpattern' in vm_info.keys() else None\n interface_index = vm_info['interfaceindex'] if 'interfaceindex' in vm_info.keys() else 0\n\n log.debug(\"Going to fire event: starting create\")\n __utils__['cloud.fire_event'](\n 'event',\n 'starting create',\n 'salt/cloud/{0}/creating'.format(vm_info['name']),\n args=__utils__['cloud.filter_event']('creating', vm_info, ['name', 'profile', 'provider', 'driver']),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n # to create the virtual machine.\n request_kwargs = {\n 'name': vm_info['name'],\n 'clone_from': vm_info['clonefrom'],\n 'clone_mode': clone_mode\n }\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(vm_info['name']),\n args=__utils__['cloud.filter_event']('requesting', request_kwargs, list(request_kwargs)),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n vm_result = vb_clone_vm(**request_kwargs)\n\n # Booting and deploying if needed\n if power:\n vb_start_vm(vm_name, timeout=boot_timeout)\n ips = vb_wait_for_network_address(wait_for_ip_timeout, machine_name=vm_name, wait_for_pattern=wait_for_pattern)\n\n if ips:\n ip = ips[interface_index]\n log.info(\"[ %s ] IPv4 is: %s\", vm_name, ip)\n # ssh or smb using ip and install salt only if deploy is True\n if deploy:\n vm_info['key_filename'] = key_filename\n vm_info['ssh_host'] = ip\n\n res = __utils__['cloud.bootstrap'](vm_info, __opts__)\n vm_result.update(res)\n\n __utils__['cloud.fire_event'](\n 'event',\n 'created machine',\n 'salt/cloud/{0}/created'.format(vm_info['name']),\n args=__utils__['cloud.filter_event']('created', vm_result, list(vm_result)),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n # Passwords should be included in this object!!\n return vm_result", "response": "Create a new virtual machine from the given VM information."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_nodes_full(kwargs=None, call=None):\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes_full function must be called '\n 'with -f or --function.'\n )\n\n machines = {}\n\n # TODO ask for the correct attributes e.g state and private_ips\n for machine in vb_list_machines():\n name = machine.get(\"name\")\n if name:\n machines[name] = treat_machine_dict(machine)\n del machine[\"name\"]\n\n return machines", "response": "Return a list of all available nodes in the cloud provider"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_nodes(kwargs=None, call=None):\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes function must be called '\n 'with -f or --function.'\n )\n\n attributes = [\n \"id\",\n \"image\",\n \"size\",\n \"state\",\n \"private_ips\",\n \"public_ips\",\n ]\n return __utils__['cloud.list_nodes_select'](\n list_nodes_full('function'), attributes, call,\n )", "response": "This function returns a list of nodes available on this cloud provider"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndestroy a virtual machine on the cloud provider.", "response": "def destroy(name, call=None):\n \"\"\"\n This function irreversibly destroys a virtual machine on the cloud provider.\n Before doing so, it should fire an event on the Salt event bus.\n\n The tag for this event is `salt/cloud//destroying`.\n Once the virtual machine has been destroyed, another event is fired.\n The tag for that event is `salt/cloud//destroyed`.\n\n Dependencies:\n list_nodes\n\n @param name:\n @type name: str\n @param call:\n @type call:\n @return: True if all went well, otherwise an error message\n @rtype: bool|str\n \"\"\"\n log.info(\"Attempting to delete instance %s\", name)\n if not vb_machine_exists(name):\n return \"{0} doesn't exist and can't be deleted\".format(name)\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 vb_destroy_machine(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 )"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef start(name, call=None):\n '''\n Start a machine.\n @param name: Machine to start\n @type name: str\n @param call: Must be \"action\"\n @type call: str\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The instance action must be called with -a or --action.'\n )\n\n log.info(\"Starting machine: %s\", name)\n vb_start_vm(name)\n machine = vb_get_machine(name)\n del machine[\"name\"]\n return treat_machine_dict(machine)", "response": "Start a new virtual machine."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef stop(name, call=None):\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The instance action must be called with -a or --action.'\n )\n\n log.info(\"Stopping machine: %s\", name)\n vb_stop_vm(name)\n machine = vb_get_machine(name)\n del machine[\"name\"]\n return treat_machine_dict(machine)", "response": "Stop a running node in the cluster."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nshows the details of an image", "response": "def show_image(kwargs, call=None):\n \"\"\"\n Show the details of an image\n \"\"\"\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_image action must be called with -f or --function.'\n )\n\n name = kwargs['image']\n log.info(\"Showing image %s\", name)\n machine = vb_get_machine(name)\n\n ret = {\n machine[\"name\"]: treat_machine_dict(machine)\n }\n del machine[\"name\"]\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef mapped(name,\n device,\n keyfile=None,\n opts=None,\n config='/etc/crypttab',\n persist=True,\n immediate=False,\n match_on='name'):\n '''\n Verify that a device is mapped\n\n name\n The name under which the device is to be mapped\n\n device\n The device name, typically the device node, such as ``/dev/sdb1``\n or ``UUID=066e0200-2867-4ebe-b9e6-f30026ca2314``.\n\n keyfile\n Either ``None`` if the password is to be entered manually on boot, or\n an absolute path to a keyfile. If the password is to be asked\n interactively, the mapping cannot be performed with ``immediate=True``.\n\n opts\n A list object of options or a comma delimited list\n\n config\n Set an alternative location for the crypttab, if the map is persistent,\n Default is ``/etc/crypttab``\n\n persist\n Set if the map should be saved in the crypttab, Default is ``True``\n\n immediate\n Set if the device mapping should be executed immediately. Requires that\n the keyfile not be ``None``, because the password cannot be asked\n interactively. Note that options are not passed through on the initial\n mapping. Default is ``False``.\n\n match_on\n A name or list of crypttab properties on which this state should be applied.\n Default is ``name``, meaning that the line is matched only by the name\n parameter. If the desired configuration requires two devices mapped to\n the same name, supply a list of parameters to match on.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n # If neither option is set, we've been asked to do nothing.\n if not immediate and not persist:\n ret['result'] = False\n ret['comment'] = 'Either persist or immediate must be set, otherwise this state does nothing'\n return ret\n\n if immediate and (keyfile is None or keyfile == 'none' or keyfile == '-'):\n ret['result'] = False\n ret['changes']['cryptsetup'] = 'Device cannot be mapped immediately without a keyfile'\n\n elif immediate:\n # Get the active crypt mounts. If ours is listed already, no action is necessary.\n active = __salt__['cryptdev.active']()\n if name not in active.keys():\n # Open the map using cryptsetup. This does not pass any options.\n if opts:\n log.warning('Ignore cryptdev configuration when mapping immediately')\n\n if __opts__['test']:\n ret['result'] = None\n ret['commment'] = 'Device would be mapped immediately'\n else:\n cryptsetup_result = __salt__['cryptdev.open'](name, device, keyfile)\n if cryptsetup_result:\n ret['changes']['cryptsetup'] = 'Device mapped using cryptsetup'\n else:\n ret['changes']['cryptsetup'] = 'Device failed to map using cryptsetup'\n ret['result'] = False\n\n if persist and not __opts__['test']:\n crypttab_result = __salt__['cryptdev.set_crypttab'](name,\n device,\n password=keyfile,\n options=opts,\n config=config,\n match_on=match_on)\n if crypttab_result:\n if crypttab_result == 'new':\n ret['changes']['crypttab'] = 'Entry added in {0}'.format(config)\n\n if crypttab_result == 'change':\n ret['changes']['crypttab'] = 'Existing entry in {0} changed'.format(config)\n\n else:\n ret['changes']['crypttab'] = 'Unable to set entry in {0}'.format(config)\n ret['result'] = False\n\n return ret", "response": "Returns a new node that is mapped to the given device node."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nensure that a device is unmapped using cryptsetup.", "response": "def unmapped(name,\n config='/etc/crypttab',\n persist=True,\n immediate=False):\n '''\n Ensure that a device is unmapped\n\n name\n The name to ensure is not mapped\n\n config\n Set an alternative location for the crypttab, if the map is persistent,\n Default is ``/etc/crypttab``\n\n persist\n Set if the map should be removed from the crypttab. Default is ``True``\n\n immediate\n Set if the device should be unmapped immediately. Default is ``False``.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n if immediate:\n # Get the active crypt mounts. If ours is not listed already, no action is necessary.\n active = __salt__['cryptdev.active']()\n if name in active.keys():\n # Close the map using cryptsetup.\n if __opts__['test']:\n ret['result'] = None\n ret['commment'] = 'Device would be unmapped immediately'\n else:\n cryptsetup_result = __salt__['cryptdev.close'](name)\n if cryptsetup_result:\n ret['changes']['cryptsetup'] = 'Device unmapped using cryptsetup'\n else:\n ret['changes']['cryptsetup'] = 'Device failed to unmap using cryptsetup'\n ret['result'] = False\n\n if persist and not __opts__['test']:\n crypttab_result = __salt__['cryptdev.rm_crypttab'](name, config=config)\n if crypttab_result:\n if crypttab_result == 'change':\n ret['changes']['crypttab'] = 'Entry removed from {0}'.format(config)\n\n else:\n ret['changes']['crypttab'] = 'Unable to remove entry in {0}'.format(config)\n ret['result'] = False\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the compiled top data for a specific minion", "response": "def show_top(minion=None, saltenv='base'):\n '''\n Returns the compiled top data for pillar for a specific minion. If no\n minion is specified, we use the first minion we find.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run pillar.show_top\n '''\n id_, grains, _ = salt.utils.minions.get_minion_data(minion, __opts__)\n if not grains and minion == __opts__['id']:\n grains = salt.loader.grains(__opts__)\n pillar = salt.pillar.Pillar(\n __opts__,\n grains,\n id_,\n saltenv)\n\n top, errors = pillar.get_top()\n\n if errors:\n __jid_event__.fire_event({'data': errors, 'outputter': 'nested'}, 'progress')\n return errors\n\n # needed because pillar compilation clobbers grains etc via lazyLoader\n # this resets the masterminion back to known state\n __salt__['salt.cmd']('sys.reload_modules')\n\n return top"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef show_pillar(minion='*', **kwargs):\n '''\n Returns the compiled pillar either of a specific minion\n or just the global available pillars. This function assumes\n that no minion has the id ``*``.\n Function also accepts pillarenv as attribute in order to limit to a specific pillar branch of git\n\n CLI Example:\n\n shows minion specific pillar:\n\n .. code-block:: bash\n\n salt-run pillar.show_pillar 'www.example.com'\n\n shows global pillar:\n\n .. code-block:: bash\n\n salt-run pillar.show_pillar\n\n shows global pillar for 'dev' pillar environment:\n (note that not specifying pillarenv will merge all pillar environments\n using the master config option pillar_source_merging_strategy.)\n\n .. code-block:: bash\n\n salt-run pillar.show_pillar 'pillarenv=dev'\n\n shows global pillar for 'dev' pillar environment and specific pillarenv = dev:\n\n .. code-block:: bash\n\n salt-run pillar.show_pillar 'saltenv=dev' 'pillarenv=dev'\n\n API Example:\n\n .. code-block:: python\n\n import salt.config\n import salt.runner\n opts = salt.config.master_config('/etc/salt/master')\n runner = salt.runner.RunnerClient(opts)\n pillar = runner.cmd('pillar.show_pillar', [])\n print(pillar)\n '''\n pillarenv = None\n saltenv = 'base'\n id_, grains, _ = salt.utils.minions.get_minion_data(minion, __opts__)\n if not grains and minion == __opts__['id']:\n grains = salt.loader.grains(__opts__)\n if grains is None:\n grains = {'fqdn': minion}\n\n for key in kwargs:\n if key == 'saltenv':\n saltenv = kwargs[key]\n elif key == 'pillarenv':\n # pillarenv overridden on CLI\n pillarenv = kwargs[key]\n else:\n grains[key] = kwargs[key]\n\n pillar = salt.pillar.Pillar(\n __opts__,\n grains,\n id_,\n saltenv,\n pillarenv=pillarenv)\n\n compiled_pillar = pillar.compile_pillar()\n\n # needed because pillar compilation clobbers grains etc via lazyLoader\n # this resets the masterminion back to known state\n __salt__['salt.cmd']('sys.reload_modules')\n\n return compiled_pillar", "response": "Show pillar for a specific minion or just the global available pillars."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ninstalling the RVM system - wide RVM and returns True if successful", "response": "def install(runas=None):\n '''\n Install RVM system-wide\n\n runas\n The user under which to run the rvm installer script. If not specified,\n then it be run as the user under which Salt is running.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' rvm.install\n '''\n # RVM dependencies on Ubuntu 10.04:\n # bash coreutils gzip bzip2 gawk sed curl git-core subversion\n installer = 'https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer'\n ret = __salt__['cmd.run_all'](\n # the RVM installer automatically does a multi-user install when it is\n # invoked with root privileges\n 'curl -Ls {installer} | bash -s stable'.format(installer=installer),\n runas=runas,\n python_shell=True\n )\n if ret['retcode'] > 0:\n msg = 'Error encountered while downloading the RVM installer'\n if ret['stderr']:\n msg += '. stderr follows:\\n\\n' + ret['stderr']\n raise CommandExecutionError(msg)\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ninstalls a ruby implementation.", "response": "def install_ruby(ruby, runas=None, opts=None, env=None):\n '''\n Install a ruby implementation.\n\n ruby\n The version of ruby to install\n\n runas\n The user under which to run rvm. If not specified, then rvm will be run\n as the user under which Salt is running.\n\n env\n Environment to set for the install command. Useful for exporting compilation\n flags such as RUBY_CONFIGURE_OPTS\n\n opts\n List of options to pass to the RVM installer (ie -C, --patch, etc)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' rvm.install_ruby 1.9.3-p385\n '''\n # MRI/RBX/REE dependencies for Ubuntu 10.04:\n # build-essential openssl libreadline6 libreadline6-dev curl\n # git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0\n # libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev autoconf libc6-dev\n # libncurses5-dev automake libtool bison subversion ruby\n if opts is None:\n opts = []\n\n if runas and runas != 'root':\n _rvm(['autolibs', 'disable', ruby] + opts, runas=runas)\n opts.append('--disable-binary')\n return _rvm(['install', ruby] + opts, runas=runas, env=env)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef reinstall_ruby(ruby, runas=None, env=None):\n '''\n Reinstall a ruby implementation\n\n ruby\n The version of ruby to reinstall\n\n runas\n The user under which to run rvm. If not specified, then rvm will be run\n as the user under which Salt is running.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' rvm.reinstall_ruby 1.9.3-p385\n '''\n return _rvm(['reinstall', ruby], runas=runas, env=env)", "response": "Reinstall a ruby implementation"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_(runas=None):\n '''\n List all rvm-installed rubies\n\n runas\n The user under which to run rvm. If not specified, then rvm will be run\n as the user under which Salt is running.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' rvm.list\n '''\n rubies = []\n output = _rvm(['list'], runas=runas)\n if output:\n regex = re.compile(r'^[= ]([*> ]) ([^- ]+)-([^ ]+) \\[ (.*) \\]')\n for line in output.splitlines():\n match = regex.match(line)\n if match:\n rubies.append([\n match.group(2), match.group(3), match.group(1) == '*'\n ])\n return rubies", "response": "List all rvm - installed rubies"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef wrapper(ruby_string, wrapper_prefix, runas=None, *binaries):\n '''\n Install RVM wrapper scripts\n\n ruby_string\n Ruby/gemset to install wrappers for\n\n wrapper_prefix\n What to prepend to the name of the generated wrapper binaries\n\n runas\n The user under which to run rvm. If not specified, then rvm will be run\n as the user under which Salt is running.\n\n binaries : None\n The names of the binaries to create wrappers for. When nothing is\n given, wrappers for ruby, gem, rake, irb, rdoc, ri and testrb are\n generated.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' rvm.wrapper \n '''\n cmd = ['wrapper', ruby_string, wrapper_prefix]\n cmd.extend(binaries)\n return _rvm(cmd, runas=runas)", "response": "Installs RVM wrapper scripts for the specified ruby_string and adds wrappers to the specified binaries."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef gemset_list(ruby='default', runas=None):\n '''\n List all gemsets for the given ruby.\n\n ruby : default\n The ruby version for which to list the gemsets\n\n runas\n The user under which to run rvm. If not specified, then rvm will be run\n as the user under which Salt is running.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' rvm.gemset_list\n '''\n gemsets = []\n output = _rvm_do(ruby, ['rvm', 'gemset', 'list'], runas=runas)\n if output:\n regex = re.compile('^ ([^ ]+)')\n for line in output.splitlines():\n match = regex.match(line)\n if match:\n gemsets.append(match.group(1))\n return gemsets", "response": "List all gemsets for the given ruby"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns all gemsets for all installed rubies.", "response": "def gemset_list_all(runas=None):\n '''\n List all gemsets for all installed rubies.\n\n Note that you must have set a default ruby before this can work.\n\n runas\n The user under which to run rvm. If not specified, then rvm will be run\n as the user under which Salt is running.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' rvm.gemset_list_all\n '''\n gemsets = {}\n current_ruby = None\n output = _rvm_do('default', ['rvm', 'gemset', 'list_all'], runas=runas)\n if output:\n gems_regex = re.compile('^ ([^ ]+)')\n gemset_regex = re.compile('^gemsets for ([^ ]+)')\n for line in output.splitlines():\n match = gemset_regex.match(line)\n if match:\n current_ruby = match.group(1)\n gemsets[current_ruby] = []\n match = gems_regex.match(line)\n if match:\n gemsets[current_ruby].append(match.group(1))\n return gemsets"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef do(ruby, command, runas=None, cwd=None, env=None): # pylint: disable=C0103\n '''\n Execute a command in an RVM controlled environment.\n\n ruby\n Which ruby to use\n\n command\n The rvm command to execute\n\n runas\n The user under which to run rvm. If not specified, then rvm will be run\n as the user under which Salt is running.\n\n cwd\n The directory from which to run the rvm command. Defaults to the user's\n home directory.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' rvm.do 2.0.0 \n '''\n try:\n command = salt.utils.args.shlex_split(command)\n except AttributeError:\n command = salt.utils.args.shlex_split(six.text_type(command))\n return _rvm_do(ruby, command, runas=runas, cwd=cwd, env=env)", "response": "Execute a command in an RVM controlled environment."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parse_file(self, fpath):\n '''\n Read a file on the file system (relative to salt's base project dir)\n\n :returns: A file-like object.\n :raises IOError: If the file cannot be found or read.\n '''\n sdir = os.path.abspath(os.path.join(os.path.dirname(salt.__file__),\n os.pardir))\n with open(os.path.join(sdir, fpath), 'rb') as f:\n return f.readlines()", "response": "Read a file on the file system and return a list of lines."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef parse_lit(self, lines):\n '''\n Parse a string line-by-line delineating comments and code\n\n :returns: An tuple of boolean/list-of-string pairs. True designates a\n comment; False designates code.\n '''\n comment_char = '#' # TODO: move this into a directive option\n comment = re.compile(r'^\\s*{0}[ \\n]'.format(comment_char))\n section_test = lambda val: bool(comment.match(val))\n\n sections = []\n for is_doc, group in itertools.groupby(lines, section_test):\n if is_doc:\n text = [comment.sub('', i).rstrip('\\r\\n') for i in group]\n else:\n text = [i.rstrip('\\r\\n') for i in group]\n\n sections.append((is_doc, text))\n\n return sections", "response": "Parse a string line - by - line delineating comments and code\n ."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngives a typical Salt SLS path find the the file on the file system and parse it", "response": "def parse_file(self, sls_path):\n '''\n Given a typical Salt SLS path (e.g.: apache.vhosts.standard), find the\n file on the file system and parse it\n '''\n config = self.state.document.settings.env.config\n formulas_dirs = config.formulas_dirs\n fpath = sls_path.replace('.', '/')\n\n name_options = (\n '{0}.sls'.format(fpath),\n os.path.join(fpath, 'init.sls')\n )\n\n paths = [os.path.join(fdir, fname)\n for fname in name_options\n for fdir in formulas_dirs]\n\n for i in paths:\n try:\n with open(i, 'rb') as f:\n return f.readlines()\n except IOError:\n pass\n\n raise IOError(\"Could not find sls file '{0}'\".format(sls_path))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the minions found by looking via pillar", "response": "def mmatch(expr, delimiter, greedy, opts=None):\n '''\n Return the minions found by looking via pillar\n '''\n if not opts:\n opts = __opts__\n\n ckminions = salt.utils.minions.CkMinions(opts)\n return ckminions._check_compound_minions(expr, delimiter, greedy,\n pillar_exact=True)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef attr(key, value=None):\n '''\n Access/write a SysFS attribute.\n If the attribute is a symlink, it's destination is returned\n\n :return: value or bool\n\n CLI example:\n .. code-block:: bash\n\n salt '*' sysfs.attr block/sda/queue/logical_block_size\n '''\n key = target(key)\n\n if key is False:\n return False\n elif os.path.isdir(key):\n return key\n elif value is not None:\n return write(key, value)\n else:\n return read(key)", "response": "Access a SysFS attribute."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nwrites a SysFS attribute or action to a SysFS attribute.", "response": "def write(key, value):\n '''\n Write a SysFS attribute/action\n\n CLI example:\n .. code-block:: bash\n\n salt '*' sysfs.write devices/system/cpu/cpu0/cpufreq/scaling_governor 'performance'\n '''\n try:\n key = target(key)\n log.trace('Writing %s to %s', value, key)\n with salt.utils.files.fopen(key, 'w') as twriter:\n twriter.write(\n salt.utils.stringutils.to_str('{0}\\n'.format(value))\n )\n return True\n except Exception:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nread SysFS attributes under the specified key.", "response": "def read(key, root=''):\n '''\n Read from SysFS\n\n :param key: file or path in SysFS; if key is a list then root will be prefixed on each key\n\n :return: the full (tree of) SysFS attributes under key\n\n CLI example:\n .. code-block:: bash\n\n salt '*' sysfs.read class/net/em1/statistics\n '''\n\n if not isinstance(key, six.string_types):\n res = {}\n for akey in key:\n ares = read(os.path.join(root, akey))\n if ares is not False:\n res[akey] = ares\n return res\n\n key = target(os.path.join(root, key))\n if key is False:\n return False\n elif os.path.isdir(key):\n keys = interfaces(key)\n result = {}\n for subkey in keys['r'] + keys['rw']:\n subval = read(os.path.join(key, subkey))\n if subval is not False:\n subkeys = subkey.split('/')\n subkey = subkeys.pop()\n subresult = result\n if subkeys:\n for skey in subkeys:\n if skey not in subresult:\n subresult[skey] = {}\n subresult = subresult[skey]\n subresult[subkey] = subval\n return result\n else:\n try:\n log.trace('Reading %s...', key)\n\n # Certain things in SysFS are pipes 'n such.\n # This opens it non-blocking, which prevents indefinite blocking\n with os.fdopen(os.open(key, os.O_RDONLY | os.O_NONBLOCK)) as treader:\n # alternative method for the same idea, but only works for completely empty pipes\n # treader = select.select([treader], [], [], 1)[0][0]\n val = treader.read().strip()\n if not val:\n return False\n try:\n val = int(val)\n except Exception:\n try:\n val = float(val)\n except Exception:\n pass\n return val\n except Exception:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the basename of a SysFS key path", "response": "def target(key, full=True):\n '''\n Return the basename of a SysFS key path\n\n :param key: the location to resolve within SysFS\n :param full: full path instead of basename\n\n :return: fullpath or basename of path\n\n CLI example:\n .. code-block:: bash\n\n salt '*' sysfs.read class/ttyS0\n\n '''\n if not key.startswith('/sys'):\n key = os.path.join('/sys', key)\n key = os.path.realpath(key)\n\n if not os.path.exists(key):\n log.debug('Unkown SysFS key %s', key)\n return False\n elif full:\n return key\n else:\n return os.path.basename(key)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef interfaces(root):\n '''\n Generate a dictionary with all available interfaces relative to root.\n Symlinks are not followed.\n\n CLI example:\n .. code-block:: bash\n\n salt '*' sysfs.interfaces block/bcache0/bcache\n\n Output example:\n .. code-block:: json\n\n {\n \"r\": [\n \"state\",\n \"partial_stripes_expensive\",\n \"writeback_rate_debug\",\n \"stripe_size\",\n \"dirty_data\",\n \"stats_total/cache_hits\",\n \"stats_total/cache_bypass_misses\",\n \"stats_total/bypassed\",\n \"stats_total/cache_readaheads\",\n \"stats_total/cache_hit_ratio\",\n \"stats_total/cache_miss_collisions\",\n \"stats_total/cache_misses\",\n \"stats_total/cache_bypass_hits\",\n ],\n \"rw\": [\n \"writeback_rate\",\n \"writeback_rate_update_seconds\",\n \"cache_mode\",\n \"writeback_delay\",\n \"label\",\n \"writeback_running\",\n \"writeback_metadata\",\n \"running\",\n \"writeback_rate_p_term_inverse\",\n \"sequential_cutoff\",\n \"writeback_percent\",\n \"writeback_rate_d_term\",\n \"readahead\"\n ],\n \"w\": [\n \"stop\",\n \"clear_stats\",\n \"attach\",\n \"detach\"\n ]\n }\n\n .. note::\n * 'r' interfaces are read-only\n * 'w' interfaces are write-only (e.g. actions)\n * 'rw' are interfaces that can both be read or written\n '''\n\n root = target(root)\n if root is False or not os.path.isdir(root):\n log.error('SysFS %s not a dir', root)\n return False\n\n readwrites = []\n reads = []\n writes = []\n\n for path, _, files in salt.utils.path.os_walk(root, followlinks=False):\n for afile in files:\n canpath = os.path.join(path, afile)\n\n if not os.path.isfile(canpath):\n continue\n\n stat_mode = os.stat(canpath).st_mode\n is_r = bool(stat.S_IRUSR & stat_mode)\n is_w = bool(stat.S_IWUSR & stat_mode)\n\n relpath = os.path.relpath(canpath, root)\n if is_w:\n if is_r:\n readwrites.append(relpath)\n else:\n writes.append(relpath)\n elif is_r:\n reads.append(relpath)\n else:\n log.warning('Unable to find any interfaces in %s', canpath)\n\n return {\n 'r': reads,\n 'w': writes,\n 'rw': readwrites\n }", "response": "Generate a dictionary with all available interfaces relative to root."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_librato(ret=None):\n '''\n Return a Librato connection object.\n '''\n _options = _get_options(ret)\n\n conn = librato.connect(\n _options.get('email'),\n _options.get('api_token'),\n sanitizer=librato.sanitize_metric_name,\n hostname=_options.get('api_url'))\n log.info(\"Connected to librato.\")\n return conn", "response": "Return a Librato connection object."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nparsing the return data and return metrics to Librato.", "response": "def returner(ret):\n '''\n Parse the return data and return metrics to Librato.\n '''\n librato_conn = _get_librato(ret)\n\n q = librato_conn.new_queue()\n\n if ret['fun'] == 'state.highstate':\n log.debug('Found returned Highstate data.')\n # Calculate the runtimes and number of failed states.\n stats = _calculate_runtimes(ret['return'])\n log.debug('Batching Metric retcode with %s', ret['retcode'])\n q.add('saltstack.highstate.retcode',\n ret['retcode'], tags={'Name': ret['id']})\n\n log.debug(\n 'Batching Metric num_failed_jobs with %s',\n stats['num_failed_states']\n )\n q.add('saltstack.highstate.failed_states',\n stats['num_failed_states'], tags={'Name': ret['id']})\n\n log.debug(\n 'Batching Metric num_passed_states with %s',\n stats['num_passed_states']\n )\n q.add('saltstack.highstate.passed_states',\n stats['num_passed_states'], tags={'Name': ret['id']})\n\n log.debug('Batching Metric runtime with %s', stats['runtime'])\n q.add('saltstack.highstate.runtime',\n stats['runtime'], tags={'Name': ret['id']})\n\n log.debug(\n 'Batching Metric runtime with %s',\n stats['num_failed_states'] + stats['num_passed_states']\n )\n q.add('saltstack.highstate.total_states', stats[\n 'num_failed_states'] + stats['num_passed_states'], tags={'Name': ret['id']})\n\n log.info('Sending metrics to Librato.')\n q.submit()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _common(ret, name, service_name, kwargs):\n '''\n Returns: tuple whose first element is a bool indicating success or failure\n and the second element is either a ret dict for salt or an object\n '''\n if 'interface' not in kwargs and 'public_url' not in kwargs:\n kwargs['interface'] = name\n service = __salt__['keystoneng.service_get'](name_or_id=service_name)\n\n if not service:\n ret['comment'] = 'Cannot find service'\n ret['result'] = False\n return (False, ret)\n\n filters = kwargs.copy()\n filters.pop('enabled', None)\n filters.pop('url', None)\n filters['service_id'] = service.id\n kwargs['service_name_or_id'] = service.id\n endpoints = __salt__['keystoneng.endpoint_search'](filters=filters)\n\n if len(endpoints) > 1:\n ret['comment'] = \"Multiple endpoints match criteria\"\n ret['result'] = False\n return ret\n endpoint = endpoints[0] if endpoints else None\n return (True, endpoint)", "response": "Common logic for the keystone service search"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nensuring an endpoint exists and is up - to - date and contains the specified service.", "response": "def present(name, service_name, auth=None, **kwargs):\n '''\n Ensure an endpoint exists and is up-to-date\n\n name\n Interface name\n\n url\n URL of the endpoint\n\n service_name\n Service name or ID\n\n region\n The region name to assign the endpoint\n\n enabled\n Boolean to control if endpoint is enabled\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n kwargs = __utils__['args.clean_kwargs'](**kwargs)\n\n __salt__['keystoneng.setup_clouds'](auth)\n\n success, val = _, endpoint = _common(ret, name, service_name, kwargs)\n if not success:\n return val\n\n if not endpoint:\n if __opts__['test'] is True:\n ret['result'] = None\n ret['changes'] = kwargs\n ret['comment'] = 'Endpoint will be created.'\n return ret\n\n # NOTE(SamYaple): Endpoints are returned as a list which can contain\n # several items depending on the options passed\n endpoints = __salt__['keystoneng.endpoint_create'](**kwargs)\n if len(endpoints) == 1:\n ret['changes'] = endpoints[0]\n else:\n for i, endpoint in enumerate(endpoints):\n ret['changes'][i] = endpoint\n ret['comment'] = 'Created endpoint'\n return ret\n\n changes = __salt__['keystoneng.compare_changes'](endpoint, **kwargs)\n if changes:\n if __opts__['test'] is True:\n ret['result'] = None\n ret['changes'] = changes\n ret['comment'] = 'Endpoint will be updated.'\n return ret\n\n kwargs['endpoint_id'] = endpoint.id\n __salt__['keystoneng.endpoint_update'](**kwargs)\n ret['changes'].update(changes)\n ret['comment'] = 'Updated endpoint'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure an endpoint does not exists", "response": "def absent(name, service_name, auth=None, **kwargs):\n '''\n Ensure an endpoint does not exists\n\n name\n Interface name\n\n url\n URL of the endpoint\n\n service_name\n Service name or ID\n\n region\n The region name to assign the endpoint\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n __salt__['keystoneng.setup_clouds'](auth)\n\n success, val = _, endpoint = _common(ret, name, service_name, kwargs)\n if not success:\n return val\n\n if endpoint:\n if __opts__['test'] is True:\n ret['result'] = None\n ret['changes'] = {'id': endpoint.id}\n ret['comment'] = 'Endpoint will be deleted.'\n return ret\n\n __salt__['keystoneng.endpoint_delete'](id=endpoint.id)\n ret['changes']['id'] = endpoint.id\n ret['comment'] = 'Deleted endpoint'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the bgp runner options.", "response": "def _get_bgp_runner_opts():\n '''\n Return the bgp runner options.\n '''\n runner_opts = __opts__.get('runners', {}).get('bgp', {})\n return {\n 'tgt': runner_opts.get('tgt', _DEFAULT_TARGET),\n 'tgt_type': runner_opts.get('tgt_type', _DEFAULT_EXPR_FORM),\n 'display': runner_opts.get('display', _DEFAULT_DISPLAY),\n 'return_fields': _DEFAULT_INCLUDED_FIELDS + runner_opts.get('return_fields', _DEFAULT_RETURN_FIELDS),\n 'outputter': runner_opts.get('outputter', _DEFAULT_OUTPUTTER),\n }"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _compare_match(dict1, dict2):\n '''\n Compare two dictionaries and return a boolean value if their values match.\n '''\n for karg, warg in six.iteritems(dict1):\n if karg in dict2 and dict2[karg] != warg:\n return False\n return True", "response": "Compare two dictionaries and return a boolean value if they match."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _display_runner(rows,\n labels,\n title,\n display=_DEFAULT_DISPLAY,\n outputter=_DEFAULT_OUTPUTTER):\n '''\n Display or return the rows.\n '''\n if display:\n if outputter == 'table':\n ret = salt.output.out_format({'rows': rows, 'labels': labels},\n 'table',\n __opts__,\n title=title,\n rows_key='rows',\n labels_key='labels')\n else:\n ret = salt.output.out_format(rows,\n outputter,\n __opts__)\n print(ret)\n else:\n return rows", "response": "Display or return the rows."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of BGP neighbors details in the BGP routing table.", "response": "def neighbors(*asns, **kwargs):\n '''\n Search for BGP neighbors details in the mines of the ``bgp.neighbors`` function.\n\n Arguments:\n\n asns\n A list of AS numbers to search for.\n The runner will return only the neighbors of these AS numbers.\n\n device\n Filter by device name (minion ID).\n\n ip\n Search BGP neighbor using the IP address.\n In multi-VRF environments, the same IP address could be used by\n more than one neighbors, in different routing tables.\n\n network\n Search neighbors within a certain IP network.\n\n title\n Custom title.\n\n display: ``True``\n Display on the screen or return structured object? Default: ``True`` (return on the CLI).\n\n outputter: ``table``\n Specify the outputter name when displaying on the CLI. Default: :mod:`table `.\n\n In addition, any field from the output of the ``neighbors`` function\n from the :mod:`NAPALM BGP module ` can be used as a filter.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run bgp.neighbors 13335 15169\n salt-run bgp.neighbors 13335 ip=172.17.19.1\n salt-run bgp.neighbors multipath=True\n salt-run bgp.neighbors up=False export_policy=my-export-policy multihop=False\n salt-run bgp.neighbors network=192.168.0.0/16\n\n Output example:\n\n .. code-block:: text\n\n BGP Neighbors for 13335, 15169\n ________________________________________________________________________________________________________________________________________________________________\n | Device | AS Number | Neighbor Address | State|#Active/Received/Accepted/Damped | Policy IN | Policy OUT |\n ________________________________________________________________________________________________________________________________________________________________\n | edge01.bjm01 | 13335 | 172.17.109.11 | Established 0/398/398/0 | import-policy | export-policy |\n ________________________________________________________________________________________________________________________________________________________________\n | edge01.bjm01 | 13335 | 172.17.109.12 | Established 397/398/398/0 | import-policy | export-policy |\n ________________________________________________________________________________________________________________________________________________________________\n | edge01.flw01 | 13335 | 192.168.172.11 | Established 1/398/398/0 | import-policy | export-policy |\n ________________________________________________________________________________________________________________________________________________________________\n | edge01.oua01 | 13335 | 172.17.109.17 | Established 0/0/0/0 | import-policy | export-policy |\n ________________________________________________________________________________________________________________________________________________________________\n | edge01.bjm01 | 15169 | 2001::1 | Established 102/102/102/0 | import-policy | export-policy |\n ________________________________________________________________________________________________________________________________________________________________\n | edge01.bjm01 | 15169 | 2001::2 | Established 102/102/102/0 | import-policy | export-policy |\n ________________________________________________________________________________________________________________________________________________________________\n | edge01.tbg01 | 13335 | 192.168.172.17 | Established 0/1/1/0 | import-policy | export-policy |\n ________________________________________________________________________________________________________________________________________________________________\n '''\n opts = _get_bgp_runner_opts()\n title = kwargs.pop('title', None)\n display = kwargs.pop('display', opts['display'])\n outputter = kwargs.pop('outputter', opts['outputter'])\n\n # cleaning up the kwargs\n # __pub args not used in this runner (yet)\n kwargs_copy = {}\n kwargs_copy.update(kwargs)\n for karg, _ in six.iteritems(kwargs_copy):\n if karg.startswith('__pub'):\n kwargs.pop(karg)\n if not asns and not kwargs:\n if display:\n print('Please specify at least an AS Number or an output filter')\n return []\n device = kwargs.pop('device', None)\n neighbor_ip = kwargs.pop('ip', None)\n ipnet = kwargs.pop('network', None)\n ipnet_obj = IPNetwork(ipnet) if ipnet else None\n # any other key passed on the CLI can be used as a filter\n\n rows = []\n # building the labels\n labels = {}\n for field in opts['return_fields']:\n if field in _DEFAULT_LABELS_MAPPING:\n labels[field] = _DEFAULT_LABELS_MAPPING[field]\n else:\n # transform from 'previous_connection_state' to 'Previous Connection State'\n labels[field] = ' '.join(map(lambda word: word.title(), field.split('_')))\n display_fields = list(set(opts['return_fields']) - set(_DEFAULT_INCLUDED_FIELDS))\n get_bgp_neighbors_all = _get_mine(opts=opts)\n\n if not title:\n title_parts = []\n if asns:\n title_parts.append('BGP Neighbors for {asns}'.format(\n asns=', '.join([six.text_type(asn) for asn in asns])\n ))\n if neighbor_ip:\n title_parts.append('Selecting neighbors having the remote IP address: {ipaddr}'.format(ipaddr=neighbor_ip))\n if ipnet:\n title_parts.append('Selecting neighbors within the IP network: {ipnet}'.format(ipnet=ipnet))\n if kwargs:\n title_parts.append('Searching for BGP neighbors having the attributes: {attrmap}'.format(\n attrmap=', '.join(map(lambda key: '{key}={value}'.format(key=key, value=kwargs[key]), kwargs))\n ))\n title = '\\n'.join(title_parts)\n for minion, get_bgp_neighbors_minion in six.iteritems(get_bgp_neighbors_all): # pylint: disable=too-many-nested-blocks\n if not get_bgp_neighbors_minion.get('result'):\n continue # ignore empty or failed mines\n if device and minion != device:\n # when requested to display only the neighbors on a certain device\n continue\n get_bgp_neighbors_minion_out = get_bgp_neighbors_minion.get('out', {})\n for vrf, vrf_bgp_neighbors in six.iteritems(get_bgp_neighbors_minion_out): # pylint: disable=unused-variable\n for asn, get_bgp_neighbors_minion_asn in six.iteritems(vrf_bgp_neighbors):\n if asns and asn not in asns:\n # if filtering by AS number(s),\n # will ignore if this AS number key not in that list\n # and continue the search\n continue\n for neighbor in get_bgp_neighbors_minion_asn:\n if kwargs and not _compare_match(kwargs, neighbor):\n # requested filtering by neighbors stats\n # but this one does not correspond\n continue\n if neighbor_ip and neighbor_ip != neighbor.get('remote_address'):\n # requested filtering by neighbors IP addr\n continue\n if ipnet_obj and neighbor.get('remote_address'):\n neighbor_ip_obj = IPAddress(neighbor.get('remote_address'))\n if neighbor_ip_obj not in ipnet_obj:\n # Neighbor not in this network\n continue\n row = {\n 'device': minion,\n 'neighbor_address': neighbor.get('remote_address'),\n 'as_number': asn\n }\n if 'vrf' in display_fields:\n row['vrf'] = vrf\n if 'connection_stats' in display_fields:\n connection_stats = '{state} {active}/{received}/{accepted}/{damped}'.format(\n state=neighbor.get('connection_state', -1),\n active=neighbor.get('active_prefix_count', -1),\n received=neighbor.get('received_prefix_count', -1),\n accepted=neighbor.get('accepted_prefix_count', -1),\n damped=neighbor.get('suppressed_prefix_count', -1),\n )\n row['connection_stats'] = connection_stats\n if 'interface_description' in display_fields or 'interface_name' in display_fields:\n net_find = __salt__['net.interfaces'](device=minion,\n ipnet=neighbor.get('remote_address'),\n display=False)\n if net_find:\n if 'interface_description' in display_fields:\n row['interface_description'] = net_find[0]['interface_description']\n if 'interface_name' in display_fields:\n row['interface_name'] = net_find[0]['interface']\n else:\n # if unable to find anything, leave blank\n if 'interface_description' in display_fields:\n row['interface_description'] = ''\n if 'interface_name' in display_fields:\n row['interface_name'] = ''\n for field in display_fields:\n if field in neighbor:\n row[field] = neighbor[field]\n rows.append(row)\n return _display_runner(rows, labels, title, display=display, outputter=outputter)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_conn(ret):\n '''\n Return a mongodb connection object\n '''\n _options = _get_options(ret)\n\n host = _options.get('host')\n port = _options.get('port')\n uri = _options.get('uri')\n db_ = _options.get('db')\n user = _options.get('user')\n password = _options.get('password')\n indexes = _options.get('indexes', False)\n\n # at some point we should remove support for\n # pymongo versions < 2.3 until then there are\n # a bunch of these sections that need to be supported\n if uri and PYMONGO_VERSION > _LooseVersion('2.3'):\n if uri and host:\n raise salt.exceptions.SaltConfigurationError(\n \"Mongo returner expects either uri or host configuration. Both were provided\")\n pymongo.uri_parser.parse_uri(uri)\n conn = pymongo.MongoClient(uri)\n mdb = conn.get_database()\n else:\n if PYMONGO_VERSION > _LooseVersion('2.3'):\n conn = pymongo.MongoClient(host, port)\n else:\n if uri:\n raise salt.exceptions.SaltConfigurationError(\n \"pymongo <= 2.3 does not support uri format\")\n conn = pymongo.Connection(host, port)\n\n mdb = conn[db_]\n if user and password:\n mdb.authenticate(user, password)\n\n if indexes:\n if PYMONGO_VERSION > _LooseVersion('2.3'):\n mdb.saltReturns.create_index('minion')\n mdb.saltReturns.create_index('jid')\n mdb.jobs.create_index('jid')\n mdb.events.create_index('tag')\n else:\n mdb.saltReturns.ensure_index('minion')\n mdb.saltReturns.ensure_index('jid')\n mdb.jobs.ensure_index('jid')\n mdb.events.ensure_index('tag')\n\n return conn, mdb", "response": "Return a mongodb connection object"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef returner(ret):\n '''\n Return data to a mongodb server\n '''\n conn, mdb = _get_conn(ret)\n\n if isinstance(ret['return'], dict):\n back = _remove_dots(ret['return'])\n else:\n back = ret['return']\n\n if isinstance(ret, dict):\n full_ret = _remove_dots(ret)\n else:\n full_ret = ret\n\n log.debug(back)\n sdata = {'minion': ret['id'], 'jid': ret['jid'], 'return': back, 'fun': ret['fun'], 'full_ret': full_ret}\n if 'out' in ret:\n sdata['out'] = ret['out']\n\n # save returns in the saltReturns collection in the json format:\n # { 'minion': , 'jid': , 'return': ,\n # 'fun': , 'full_ret': }\n #\n # again we run into the issue with deprecated code from previous versions\n\n if PYMONGO_VERSION > _LooseVersion('2.3'):\n #using .copy() to ensure that the original data is not changed, raising issue with pymongo team\n mdb.saltReturns.insert_one(sdata.copy())\n else:\n mdb.saltReturns.insert(sdata.copy())", "response": "Return data to a mongodb server\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nsafe copy of a dict to a single object", "response": "def _safe_copy(dat):\n ''' mongodb doesn't allow '.' in keys, but does allow unicode equivs.\n Apparently the docs suggest using escaped unicode full-width\n encodings. *sigh*\n\n \\\\ --> \\\\\\\\\n $ --> \\\\\\\\u0024\n . --> \\\\\\\\u002e\n\n Personally, I prefer URL encodings,\n\n \\\\ --> %5c\n $ --> %24\n . --> %2e\n\n\n Which means also escaping '%':\n\n % -> %25\n '''\n\n if isinstance(dat, dict):\n ret = {}\n for k in dat:\n r = k.replace('%', '%25').replace('\\\\', '%5c').replace('$', '%24').replace('.', '%2e')\n if r != k:\n log.debug('converting dict key from %s to %s for mongodb', k, r)\n ret[r] = _safe_copy(dat[k])\n return ret\n\n if isinstance(dat, (list, tuple)):\n return [_safe_copy(i) for i in dat]\n\n return dat"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef save_load(jid, load, minions=None):\n '''\n Save the load for a given job id\n '''\n conn, mdb = _get_conn(ret=None)\n to_save = _safe_copy(load)\n\n if PYMONGO_VERSION > _LooseVersion('2.3'):\n #using .copy() to ensure original data for load is unchanged\n mdb.jobs.insert_one(to_save)\n else:\n mdb.jobs.insert(to_save)", "response": "Save the load for a given job id\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the load associated with a given job id", "response": "def get_load(jid):\n '''\n Return the load associated with a given job id\n '''\n conn, mdb = _get_conn(ret=None)\n return mdb.jobs.find_one({'jid': jid}, {'_id': 0})"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_minions():\n '''\n Return a list of minions\n '''\n conn, mdb = _get_conn(ret=None)\n ret = []\n name = mdb.saltReturns.distinct('minion')\n ret.append(name)\n return ret", "response": "Return a list of minions"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_jids():\n '''\n Return a list of job ids\n '''\n conn, mdb = _get_conn(ret=None)\n map = \"function() { emit(this.jid, this); }\"\n reduce = \"function (key, values) { return values[0]; }\"\n result = mdb.jobs.inline_map_reduce(map, reduce)\n ret = {}\n for r in result:\n jid = r['_id']\n ret[jid] = salt.utils.jid.format_jid_instance(jid, r['value'])\n return ret", "response": "Return a list of job ids"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef event_return(events):\n '''\n Return events to Mongodb server\n '''\n conn, mdb = _get_conn(ret=None)\n\n if isinstance(events, list):\n events = events[0]\n\n if isinstance(events, dict):\n log.debug(events)\n\n if PYMONGO_VERSION > _LooseVersion('2.3'):\n mdb.events.insert_one(events.copy())\n else:\n mdb.events.insert(events.copy())", "response": "Return events to Mongodb server\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef lowstate_file_refs(chunks):\n '''\n Create a list of file ref objects to reconcile\n '''\n refs = {}\n for chunk in chunks:\n saltenv = 'base'\n crefs = []\n for state in chunk:\n if state == '__env__':\n saltenv = chunk[state]\n elif state == 'saltenv':\n saltenv = chunk[state]\n elif state.startswith('__'):\n continue\n crefs.extend(salt_refs(chunk[state]))\n if crefs:\n if saltenv not in refs:\n refs[saltenv] = []\n refs[saltenv].append(crefs)\n return refs", "response": "Create a list of file ref objects to reconcile\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\npull salt file references out of the states", "response": "def salt_refs(data):\n '''\n Pull salt file references out of the states\n '''\n proto = 'salt://'\n ret = []\n if isinstance(data, six.string_types):\n if data.startswith(proto):\n return [data]\n if isinstance(data, list):\n for comp in data:\n if isinstance(comp, six.string_types):\n if comp.startswith(proto):\n ret.append(comp)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngenerating the arguments for the shim data", "response": "def mod_data(fsclient):\n '''\n Generate the module arguments for the shim data\n '''\n # TODO, change out for a fileserver backend\n sync_refs = [\n 'modules',\n 'states',\n 'grains',\n 'renderers',\n 'returners',\n ]\n ret = {}\n envs = fsclient.envs()\n ver_base = ''\n for env in envs:\n files = fsclient.file_list(env)\n for ref in sync_refs:\n mods_data = {}\n pref = '_{0}'.format(ref)\n for fn_ in sorted(files):\n if fn_.startswith(pref):\n if fn_.endswith(('.py', '.so', '.pyx')):\n full = salt.utils.url.create(fn_)\n mod_path = fsclient.cache_file(full, env)\n if not os.path.isfile(mod_path):\n continue\n mods_data[os.path.basename(fn_)] = mod_path\n chunk = salt.utils.hashutils.get_hash(mod_path)\n ver_base += chunk\n if mods_data:\n if ref in ret:\n ret[ref].update(mods_data)\n else:\n ret[ref] = mods_data\n if not ret:\n return {}\n\n if six.PY3:\n ver_base = salt.utils.stringutils.to_bytes(ver_base)\n\n ver = hashlib.sha1(ver_base).hexdigest()\n ext_tar_path = os.path.join(\n fsclient.opts['cachedir'],\n 'ext_mods.{0}.tgz'.format(ver))\n mods = {'version': ver,\n 'file': ext_tar_path}\n if os.path.isfile(ext_tar_path):\n return mods\n tfp = tarfile.open(ext_tar_path, 'w:gz')\n verfile = os.path.join(fsclient.opts['cachedir'], 'ext_mods.ver')\n with salt.utils.files.fopen(verfile, 'w+') as fp_:\n fp_.write(ver)\n tfp.add(verfile, 'ext_version')\n for ref in ret:\n for fn_ in ret[ref]:\n tfp.add(ret[ref][fn_], os.path.join(ref, fn_))\n tfp.close()\n return mods"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the version of the installed ssh command", "response": "def ssh_version():\n '''\n Returns the version of the installed ssh command\n '''\n # This function needs more granular checks and to be validated against\n # older versions of ssh\n ret = subprocess.Popen(\n ['ssh', '-V'],\n stdout=subprocess.PIPE,\n stderr=subprocess.PIPE).communicate()\n try:\n version_parts = ret[1].split(b',')[0].split(b'_')[1]\n parts = []\n for part in version_parts:\n try:\n parts.append(int(part))\n except ValueError:\n return tuple(parts)\n return tuple(parts)\n except IndexError:\n return (2, 0)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _convert_args(args):\n '''\n Take a list of args, and convert any dicts inside the list to keyword\n args in the form of `key=value`, ready to be passed to salt-ssh\n '''\n converted = []\n for arg in args:\n if isinstance(arg, dict):\n for key in list(arg.keys()):\n if key == '__kwarg__':\n continue\n converted.append('{0}={1}'.format(key, arg[key]))\n else:\n converted.append(arg)\n return converted", "response": "Takes a list of args and converts any dicts inside the list to keyword\n args in the form of key = value ready to be passed to salt - ssh\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading the roster file as a key to the data.", "response": "def _get_roster(self):\n '''\n Read roster filename as a key to the data.\n :return:\n '''\n roster_file = salt.roster.get_roster_file(self.opts)\n if roster_file not in self.__parsed_rosters:\n roster_data = compile_template(roster_file, salt.loader.render(self.opts, {}),\n self.opts['renderer'], self.opts['renderer_blacklist'],\n self.opts['renderer_whitelist'])\n self.__parsed_rosters[roster_file] = roster_data\n return roster_file"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _expand_target(self):\n '''\n Figures out if the target is a reachable host without wildcards, expands if any.\n :return:\n '''\n # TODO: Support -L\n target = self.opts['tgt']\n if isinstance(target, list):\n return\n\n hostname = self.opts['tgt'].split('@')[-1]\n needs_expansion = '*' not in hostname and \\\n salt.utils.network.is_reachable_host(hostname) and \\\n salt.utils.network.is_ip(hostname)\n if needs_expansion:\n hostname = salt.utils.network.ip_to_host(hostname)\n if hostname is None:\n # Reverse lookup failed\n return\n self._get_roster()\n for roster_filename in self.__parsed_rosters:\n roster_data = self.__parsed_rosters[roster_filename]\n if not isinstance(roster_data, bool):\n for host_id in roster_data:\n if hostname in [host_id, roster_data.get('host')]:\n if hostname != self.opts['tgt']:\n self.opts['tgt'] = hostname\n self.__parsed_rosters[self.ROSTER_UPDATE_FLAG] = False\n return", "response": "Expand the target with wildcards and return a list of the unique identifiers."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nupdates the flat roster with the passed in information.", "response": "def _update_roster(self):\n '''\n Update default flat roster with the passed in information.\n :return:\n '''\n roster_file = self._get_roster()\n if os.access(roster_file, os.W_OK):\n if self.__parsed_rosters[self.ROSTER_UPDATE_FLAG]:\n with salt.utils.files.fopen(roster_file, 'a') as roster_fp:\n roster_fp.write('# Automatically added by \"{s_user}\" at {s_time}\\n{hostname}:\\n host: '\n '{hostname}\\n user: {user}'\n '\\n passwd: {passwd}\\n'.format(s_user=getpass.getuser(),\n s_time=datetime.datetime.utcnow().isoformat(),\n hostname=self.opts.get('tgt', ''),\n user=self.opts.get('ssh_user', ''),\n passwd=self.opts.get('ssh_passwd', '')))\n log.info('The host {0} has been added to the roster {1}'.format(self.opts.get('tgt', ''),\n roster_file))\n else:\n log.error('Unable to update roster {0}: access denied'.format(roster_file))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nupdate the target dict with the hostname and user", "response": "def _update_targets(self):\n '''\n Uptade targets in case hostname was directly passed without the roster.\n :return:\n '''\n\n hostname = self.opts.get('tgt', '')\n if '@' in hostname:\n user, hostname = hostname.split('@', 1)\n else:\n user = self.opts.get('ssh_user')\n if hostname == '*':\n hostname = ''\n\n if salt.utils.network.is_reachable_host(hostname):\n hostname = salt.utils.network.ip_to_host(hostname)\n self.opts['tgt'] = hostname\n self.targets[hostname] = {\n 'passwd': self.opts.get('ssh_passwd', ''),\n 'host': hostname,\n 'user': user,\n }\n if self.opts.get('ssh_update_roster'):\n self._update_roster()"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the key string for the SSH public key", "response": "def get_pubkey(self):\n '''\n Return the key string for the SSH public key\n '''\n if '__master_opts__' in self.opts and \\\n self.opts['__master_opts__'].get('ssh_use_home_key') and \\\n os.path.isfile(os.path.expanduser('~/.ssh/id_rsa')):\n priv = os.path.expanduser('~/.ssh/id_rsa')\n else:\n priv = self.opts.get(\n 'ssh_priv',\n os.path.join(\n self.opts['pki_dir'],\n 'ssh',\n 'salt-ssh.rsa'\n )\n )\n pub = '{0}.pub'.format(priv)\n with salt.utils.files.fopen(pub, 'r') as fp_:\n return '{0} rsa root@master'.format(fp_.read().split()[1])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndeploy the SSH key if the minions don t auth", "response": "def key_deploy(self, host, ret):\n '''\n Deploy the SSH key if the minions don't auth\n '''\n if not isinstance(ret[host], dict) or self.opts.get('ssh_key_deploy'):\n target = self.targets[host]\n if target.get('passwd', False) or self.opts['ssh_passwd']:\n self._key_deploy_run(host, target, False)\n return ret\n if ret[host].get('stderr', '').count('Permission denied'):\n target = self.targets[host]\n # permission denied, attempt to auto deploy ssh key\n print(('Permission denied for host {0}, do you want to deploy '\n 'the salt-ssh key? (password required):').format(host))\n deploy = input('[Y/n] ')\n if deploy.startswith(('n', 'N')):\n return ret\n target['passwd'] = getpass.getpass(\n 'Password for {0}@{1}: '.format(target['user'], host)\n )\n return self._key_deploy_run(host, target, True)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nrun the routine in a \"Thread\", put a dict on the queue", "response": "def handle_routine(self, que, opts, host, target, mine=False):\n '''\n Run the routine in a \"Thread\", put a dict on the queue\n '''\n opts = copy.deepcopy(opts)\n single = Single(\n opts,\n opts['argv'],\n host,\n mods=self.mods,\n fsclient=self.fsclient,\n thin=self.thin,\n mine=mine,\n **target)\n ret = {'id': single.id}\n stdout, stderr, retcode = single.run()\n # This job is done, yield\n try:\n data = salt.utils.json.find_json(stdout)\n if len(data) < 2 and 'local' in data:\n ret['ret'] = data['local']\n else:\n ret['ret'] = {\n 'stdout': stdout,\n 'stderr': stderr,\n 'retcode': retcode,\n }\n except Exception:\n ret['ret'] = {\n 'stdout': stdout,\n 'stderr': stderr,\n 'retcode': retcode,\n }\n que.put(ret)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nyields the set of hosts that are available on the roster.", "response": "def handle_ssh(self, mine=False):\n '''\n Spin up the needed threads or processes and execute the subsequent\n routines\n '''\n que = multiprocessing.Queue()\n running = {}\n target_iter = self.targets.__iter__()\n returned = set()\n rets = set()\n init = False\n while True:\n if not self.targets:\n log.error('No matching targets found in roster.')\n break\n if len(running) < self.opts.get('ssh_max_procs', 25) and not init:\n try:\n host = next(target_iter)\n except StopIteration:\n init = True\n continue\n for default in self.defaults:\n if default not in self.targets[host]:\n self.targets[host][default] = self.defaults[default]\n if 'host' not in self.targets[host]:\n self.targets[host]['host'] = host\n if self.targets[host].get('winrm') and not HAS_WINSHELL:\n returned.add(host)\n rets.add(host)\n log_msg = 'Please contact sales@saltstack.com for access to the enterprise saltwinshell module.'\n log.debug(log_msg)\n no_ret = {'fun_args': [],\n 'jid': None,\n 'return': log_msg,\n 'retcode': 1,\n 'fun': '',\n 'id': host}\n yield {host: no_ret}\n continue\n args = (\n que,\n self.opts,\n host,\n self.targets[host],\n mine,\n )\n routine = MultiprocessingProcess(\n target=self.handle_routine,\n args=args)\n routine.start()\n running[host] = {'thread': routine}\n continue\n ret = {}\n try:\n ret = que.get(False)\n if 'id' in ret:\n returned.add(ret['id'])\n yield {ret['id']: ret['ret']}\n except Exception:\n # This bare exception is here to catch spurious exceptions\n # thrown by que.get during healthy operation. Please do not\n # worry about this bare exception, it is entirely here to\n # control program flow.\n pass\n for host in running:\n if not running[host]['thread'].is_alive():\n if host not in returned:\n # Try to get any returns that came through since we\n # last checked\n try:\n while True:\n ret = que.get(False)\n if 'id' in ret:\n returned.add(ret['id'])\n yield {ret['id']: ret['ret']}\n except Exception:\n pass\n\n if host not in returned:\n error = ('Target \\'{0}\\' did not return any data, '\n 'probably due to an error.').format(host)\n ret = {'id': host,\n 'ret': error}\n log.error(error)\n yield {ret['id']: ret['ret']}\n running[host]['thread'].join()\n rets.add(host)\n for host in rets:\n if host in running:\n running.pop(host)\n if len(rets) >= len(self.targets):\n break\n # Sleep when limit or all threads started\n if len(running) >= self.opts.get('ssh_max_procs', 25) or len(self.targets) >= len(running):\n time.sleep(0.1)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef run_iter(self, mine=False, jid=None):\n '''\n Execute and yield returns as they come in, do not print to the display\n\n mine\n The Single objects will use mine_functions defined in the roster,\n pillar, or master config (they will be checked in that order) and\n will modify the argv with the arguments from mine_functions\n '''\n fstr = '{0}.prep_jid'.format(self.opts['master_job_cache'])\n jid = self.returners[fstr](passed_jid=jid or self.opts.get('jid', None))\n\n # Save the invocation information\n argv = self.opts['argv']\n\n if self.opts.get('raw_shell', False):\n fun = 'ssh._raw'\n args = argv\n else:\n fun = argv[0] if argv else ''\n args = argv[1:]\n\n job_load = {\n 'jid': jid,\n 'tgt_type': self.tgt_type,\n 'tgt': self.opts['tgt'],\n 'user': self.opts['user'],\n 'fun': fun,\n 'arg': args,\n }\n\n # save load to the master job cache\n if self.opts['master_job_cache'] == 'local_cache':\n self.returners['{0}.save_load'.format(self.opts['master_job_cache'])](jid, job_load, minions=self.targets.keys())\n else:\n self.returners['{0}.save_load'.format(self.opts['master_job_cache'])](jid, job_load)\n\n for ret in self.handle_ssh(mine=mine):\n host = next(six.iterkeys(ret))\n self.cache_job(jid, host, ret[host], fun)\n if self.event:\n id_, data = next(six.iteritems(ret))\n if isinstance(data, six.text_type):\n data = {'return': data}\n if 'id' not in data:\n data['id'] = id_\n data['jid'] = jid # make the jid in the payload the same as the jid in the tag\n self.event.fire_event(\n data,\n salt.utils.event.tagify(\n [jid, 'ret', host],\n 'job'))\n yield ret", "response": "Execute and yield return as they come in"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef cache_job(self, jid, id_, ret, fun):\n '''\n Cache the job information\n '''\n self.returners['{0}.returner'.format(self.opts['master_job_cache'])]({'jid': jid,\n 'id': id_,\n 'return': ret,\n 'fun': fun})", "response": "Cache the job information\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef run(self, jid=None):\n '''\n Execute the overall routine, print results via outputters\n '''\n if self.opts.get('list_hosts'):\n self._get_roster()\n ret = {}\n for roster_file in self.__parsed_rosters:\n if roster_file.startswith('#'):\n continue\n ret[roster_file] = {}\n for host_id in self.__parsed_rosters[roster_file]:\n hostname = self.__parsed_rosters[roster_file][host_id]['host']\n ret[roster_file][host_id] = hostname\n salt.output.display_output(ret, 'nested', self.opts)\n sys.exit()\n\n fstr = '{0}.prep_jid'.format(self.opts['master_job_cache'])\n jid = self.returners[fstr](passed_jid=jid or self.opts.get('jid', None))\n\n # Save the invocation information\n argv = self.opts['argv']\n\n if self.opts.get('raw_shell', False):\n fun = 'ssh._raw'\n args = argv\n else:\n fun = argv[0] if argv else ''\n args = argv[1:]\n\n job_load = {\n 'jid': jid,\n 'tgt_type': self.tgt_type,\n 'tgt': self.opts['tgt'],\n 'user': self.opts['user'],\n 'fun': fun,\n 'arg': args,\n }\n\n # save load to the master job cache\n try:\n if isinstance(jid, bytes):\n jid = jid.decode('utf-8')\n if self.opts['master_job_cache'] == 'local_cache':\n self.returners['{0}.save_load'.format(self.opts['master_job_cache'])](jid, job_load, minions=self.targets.keys())\n else:\n self.returners['{0}.save_load'.format(self.opts['master_job_cache'])](jid, job_load)\n except Exception as exc:\n log.exception(exc)\n log.error(\n 'Could not save load with returner %s: %s',\n self.opts['master_job_cache'], exc\n )\n\n if self.opts.get('verbose'):\n msg = 'Executing job with jid {0}'.format(jid)\n print(msg)\n print('-' * len(msg) + '\\n')\n print('')\n sret = {}\n outputter = self.opts.get('output', 'nested')\n final_exit = 0\n for ret in self.handle_ssh():\n host = next(six.iterkeys(ret))\n if isinstance(ret[host], dict):\n host_ret = ret[host].get('retcode', 0)\n if host_ret != 0:\n final_exit = 1\n else:\n # Error on host\n final_exit = 1\n\n self.cache_job(jid, host, ret[host], fun)\n ret = self.key_deploy(host, ret)\n\n if isinstance(ret[host], dict) and (ret[host].get('stderr') or '').startswith('ssh:'):\n ret[host] = ret[host]['stderr']\n\n if not isinstance(ret[host], dict):\n p_data = {host: ret[host]}\n elif 'return' not in ret[host]:\n p_data = ret\n else:\n outputter = ret[host].get('out', self.opts.get('output', 'nested'))\n p_data = {host: ret[host].get('return', {})}\n if self.opts.get('static'):\n sret.update(p_data)\n else:\n salt.output.display_output(\n p_data,\n outputter,\n self.opts)\n if self.event:\n id_, data = next(six.iteritems(ret))\n if isinstance(data, six.text_type):\n data = {'return': data}\n if 'id' not in data:\n data['id'] = id_\n data['jid'] = jid # make the jid in the payload the same as the jid in the tag\n self.event.fire_event(\n data,\n salt.utils.event.tagify(\n [jid, 'ret', host],\n 'job'))\n if self.opts.get('static'):\n salt.output.display_output(\n sret,\n outputter,\n self.opts)\n if final_exit:\n sys.exit(salt.defaults.exitcodes.EX_AGGREGATE)", "response": "Execute the overall routine and print the results via outputters\nAttributeNames"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the function name and the arg list", "response": "def __arg_comps(self):\n '''\n Return the function name and the arg list\n '''\n fun = self.argv[0] if self.argv else ''\n parsed = salt.utils.args.parse_input(\n self.argv[1:],\n condition=False,\n no_parse=self.opts.get('no_parse', []))\n args = parsed[0]\n kws = parsed[1]\n return fun, args, kws"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nescapes the argument to protect special characters from shell interpretation.", "response": "def _escape_arg(self, arg):\n '''\n Properly escape argument to protect special characters from shell\n interpretation. This avoids having to do tricky argument quoting.\n\n Effectively just escape all characters in the argument that are not\n alphanumeric!\n '''\n if self.winrm:\n return arg\n return ''.join(['\\\\' + char if re.match(r'\\W', char) else char for char in arg])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndeploying salt - thin", "response": "def deploy(self):\n '''\n Deploy salt-thin\n '''\n self.shell.send(\n self.thin,\n os.path.join(self.thin_dir, 'salt-thin.tgz'),\n )\n self.deploy_ext()\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndeploying the ext_mods tarball", "response": "def deploy_ext(self):\n '''\n Deploy the ext_mods tarball\n '''\n if self.mods.get('file'):\n self.shell.send(\n self.mods['file'],\n os.path.join(self.thin_dir, 'salt-ext_mods.tgz'),\n )\n return True"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef run(self, deploy_attempted=False):\n '''\n Execute the routine, the routine can be either:\n 1. Execute a raw shell command\n 2. Execute a wrapper func\n 3. Execute a remote Salt command\n\n If a (re)deploy is needed, then retry the operation after a deploy\n attempt\n\n Returns tuple of (stdout, stderr, retcode)\n '''\n stdout = stderr = retcode = None\n\n if self.opts.get('raw_shell', False):\n cmd_str = ' '.join([self._escape_arg(arg) for arg in self.argv])\n stdout, stderr, retcode = self.shell.exec_cmd(cmd_str)\n\n elif self.fun in self.wfuncs or self.mine:\n stdout, retcode = self.run_wfunc()\n\n else:\n stdout, stderr, retcode = self.cmd_block()\n\n return stdout, stderr, retcode", "response": "Execute the routine, the routine can be either:\n 1. Execute a raw shell command\n 2. Execute a wrapper func\n 3. Execute a remote Salt command\n\n If a (re)deploy is needed, then retry the operation after a deploy\n attempt\n\n Returns tuple of (stdout, stderr, retcode)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nexecuting a wrapper function Execute a wrapper function Returns tuple of json_data retcode", "response": "def run_wfunc(self):\n '''\n Execute a wrapper function\n\n Returns tuple of (json_data, '')\n '''\n # Ensure that opts/grains are up to date\n # Execute routine\n data_cache = False\n data = None\n cdir = os.path.join(self.opts['cachedir'], 'minions', self.id)\n if not os.path.isdir(cdir):\n os.makedirs(cdir)\n datap = os.path.join(cdir, 'ssh_data.p')\n refresh = False\n if not os.path.isfile(datap):\n refresh = True\n else:\n passed_time = (time.time() - os.stat(datap).st_mtime) / 60\n if passed_time > self.opts.get('cache_life', 60):\n refresh = True\n\n if self.opts.get('refresh_cache'):\n refresh = True\n conf_grains = {}\n # Save conf file grains before they get clobbered\n if 'ssh_grains' in self.opts:\n conf_grains = self.opts['ssh_grains']\n if not data_cache:\n refresh = True\n if refresh:\n # Make the datap\n # TODO: Auto expire the datap\n pre_wrapper = salt.client.ssh.wrapper.FunctionWrapper(\n self.opts,\n self.id,\n fsclient=self.fsclient,\n minion_opts=self.minion_opts,\n **self.target)\n opts_pkg = pre_wrapper['test.opts_pkg']() # pylint: disable=E1102\n if '_error' in opts_pkg:\n #Refresh failed\n retcode = opts_pkg['retcode']\n ret = salt.utils.json.dumps({'local': opts_pkg})\n return ret, retcode\n\n opts_pkg['file_roots'] = self.opts['file_roots']\n opts_pkg['pillar_roots'] = self.opts['pillar_roots']\n opts_pkg['ext_pillar'] = self.opts['ext_pillar']\n opts_pkg['extension_modules'] = self.opts['extension_modules']\n opts_pkg['module_dirs'] = self.opts['module_dirs']\n opts_pkg['_ssh_version'] = self.opts['_ssh_version']\n opts_pkg['__master_opts__'] = self.context['master_opts']\n if 'known_hosts_file' in self.opts:\n opts_pkg['known_hosts_file'] = self.opts['known_hosts_file']\n if '_caller_cachedir' in self.opts:\n opts_pkg['_caller_cachedir'] = self.opts['_caller_cachedir']\n else:\n opts_pkg['_caller_cachedir'] = self.opts['cachedir']\n # Use the ID defined in the roster file\n opts_pkg['id'] = self.id\n\n retcode = 0\n\n # Restore master grains\n for grain in conf_grains:\n opts_pkg['grains'][grain] = conf_grains[grain]\n # Enable roster grains support\n if 'grains' in self.target:\n for grain in self.target['grains']:\n opts_pkg['grains'][grain] = self.target['grains'][grain]\n\n popts = {}\n popts.update(opts_pkg)\n\n # Master centric operations such as mine.get must have master option loaded.\n # The pillar must then be compiled by passing master opts found in opts_pkg['__master_opts__']\n # which causes the pillar renderer to loose track of salt master options\n #\n # Depending on popts merge order, it will overwrite some options found in opts_pkg['__master_opts__']\n master_centric_funcs = [\n \"pillar.items\",\n \"mine.get\"\n ]\n\n # Pillar compilation is a master centric operation.\n # Master options take precedence during Pillar compilation\n popts.update(opts_pkg['__master_opts__'])\n\n pillar = salt.pillar.Pillar(\n popts,\n opts_pkg['grains'],\n opts_pkg['id'],\n opts_pkg.get('saltenv', 'base')\n )\n pillar_data = pillar.compile_pillar()\n\n # Once pillar has been compiled, restore priority of minion opts\n if self.fun not in master_centric_funcs:\n log.debug('%s is a minion function', self.fun)\n popts.update(opts_pkg)\n else:\n log.debug('%s is a master function', self.fun)\n\n # TODO: cache minion opts in datap in master.py\n data = {'opts': opts_pkg,\n 'grains': opts_pkg['grains'],\n 'pillar': pillar_data}\n if data_cache:\n with salt.utils.files.fopen(datap, 'w+b') as fp_:\n fp_.write(\n self.serial.dumps(data)\n )\n if not data and data_cache:\n with salt.utils.files.fopen(datap, 'rb') as fp_:\n data = self.serial.load(fp_)\n opts = data.get('opts', {})\n opts['grains'] = data.get('grains')\n\n # Restore master grains\n for grain in conf_grains:\n opts['grains'][grain] = conf_grains[grain]\n # Enable roster grains support\n if 'grains' in self.target:\n for grain in self.target['grains']:\n opts['grains'][grain] = self.target['grains'][grain]\n\n opts['pillar'] = data.get('pillar')\n wrapper = salt.client.ssh.wrapper.FunctionWrapper(\n opts,\n self.id,\n fsclient=self.fsclient,\n minion_opts=self.minion_opts,\n **self.target)\n self.wfuncs = salt.loader.ssh_wrapper(opts, wrapper, self.context)\n wrapper.wfuncs = self.wfuncs\n\n # We're running in the mine, need to fetch the arguments from the\n # roster, pillar, master config (in that order)\n if self.mine:\n mine_args = None\n mine_fun_data = None\n mine_fun = self.fun\n\n if self.mine_functions and self.fun in self.mine_functions:\n mine_fun_data = self.mine_functions[self.fun]\n elif opts['pillar'] and self.fun in opts['pillar'].get('mine_functions', {}):\n mine_fun_data = opts['pillar']['mine_functions'][self.fun]\n elif self.fun in self.context['master_opts'].get('mine_functions', {}):\n mine_fun_data = self.context['master_opts']['mine_functions'][self.fun]\n\n if isinstance(mine_fun_data, dict):\n mine_fun = mine_fun_data.pop('mine_function', mine_fun)\n mine_args = mine_fun_data\n elif isinstance(mine_fun_data, list):\n for item in mine_fun_data[:]:\n if isinstance(item, dict) and 'mine_function' in item:\n mine_fun = item['mine_function']\n mine_fun_data.pop(mine_fun_data.index(item))\n mine_args = mine_fun_data\n else:\n mine_args = mine_fun_data\n\n # If we found mine_args, replace our command's args\n if isinstance(mine_args, dict):\n self.args = []\n self.kwargs = mine_args\n elif isinstance(mine_args, list):\n self.args = mine_args\n self.kwargs = {}\n\n try:\n if self.mine:\n result = wrapper[mine_fun](*self.args, **self.kwargs)\n else:\n result = self.wfuncs[self.fun](*self.args, **self.kwargs)\n except TypeError as exc:\n result = 'TypeError encountered executing {0}: {1}'.format(self.fun, exc)\n log.error(result, exc_info_on_loglevel=logging.DEBUG)\n retcode = 1\n except Exception as exc:\n result = 'An Exception occurred while executing {0}: {1}'.format(self.fun, exc)\n log.error(result, exc_info_on_loglevel=logging.DEBUG)\n retcode = 1\n # Mimic the json data-structure that \"salt-call --local\" will\n # emit (as seen in ssh_py_shim.py)\n if isinstance(result, dict) and 'local' in result:\n ret = salt.utils.json.dumps({'local': result['local']})\n else:\n ret = salt.utils.json.dumps({'local': {'return': result}})\n return ret, retcode"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _cmd_str(self):\n '''\n Prepare the command string\n '''\n sudo = 'sudo' if self.target['sudo'] else ''\n sudo_user = self.target['sudo_user']\n if '_caller_cachedir' in self.opts:\n cachedir = self.opts['_caller_cachedir']\n else:\n cachedir = self.opts['cachedir']\n thin_code_digest, thin_sum = salt.utils.thin.thin_sum(cachedir, 'sha1')\n debug = ''\n if not self.opts.get('log_level'):\n self.opts['log_level'] = 'info'\n if salt.log.LOG_LEVELS['debug'] >= salt.log.LOG_LEVELS[self.opts.get('log_level', 'info')]:\n debug = '1'\n arg_str = '''\nOPTIONS.config = \\\n\"\"\"\n{config}\n\"\"\"\nOPTIONS.delimiter = '{delimeter}'\nOPTIONS.saltdir = '{saltdir}'\nOPTIONS.checksum = '{checksum}'\nOPTIONS.hashfunc = '{hashfunc}'\nOPTIONS.version = '{version}'\nOPTIONS.ext_mods = '{ext_mods}'\nOPTIONS.wipe = {wipe}\nOPTIONS.tty = {tty}\nOPTIONS.cmd_umask = {cmd_umask}\nOPTIONS.code_checksum = {code_checksum}\nARGS = {arguments}\\n'''.format(config=self.minion_config,\n delimeter=RSTR,\n saltdir=self.thin_dir,\n checksum=thin_sum,\n hashfunc='sha1',\n version=salt.version.__version__,\n ext_mods=self.mods.get('version', ''),\n wipe=self.wipe,\n tty=self.tty,\n cmd_umask=self.cmd_umask,\n code_checksum=thin_code_digest,\n arguments=self.argv)\n py_code = SSH_PY_SHIM.replace('#%%OPTS', arg_str)\n if six.PY2:\n py_code_enc = py_code.encode('base64')\n else:\n py_code_enc = base64.encodebytes(py_code.encode('utf-8')).decode('utf-8')\n if not self.winrm:\n cmd = SSH_SH_SHIM.format(\n DEBUG=debug,\n SUDO=sudo,\n SUDO_USER=sudo_user,\n SSH_PY_CODE=py_code_enc,\n HOST_PY_MAJOR=sys.version_info[0],\n )\n else:\n cmd = saltwinshell.gen_shim(py_code_enc)\n\n return cmd", "response": "Prepare the command string for the command line"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef shim_cmd(self, cmd_str, extension='py'):\n '''\n Run a shim command.\n\n If tty is enabled, we must scp the shim to the target system and\n execute it there\n '''\n if not self.tty and not self.winrm:\n return self.shell.exec_cmd(cmd_str)\n\n # Write the shim to a temporary file in the default temp directory\n with tempfile.NamedTemporaryFile(mode='w+b',\n prefix='shim_',\n delete=False) as shim_tmp_file:\n shim_tmp_file.write(salt.utils.stringutils.to_bytes(cmd_str))\n\n # Copy shim to target system, under $HOME/.\n target_shim_file = '.{0}.{1}'.format(\n binascii.hexlify(os.urandom(6)).decode('ascii'),\n extension\n )\n if self.winrm:\n target_shim_file = saltwinshell.get_target_shim_file(self, target_shim_file)\n self.shell.send(shim_tmp_file.name, target_shim_file, makedirs=True)\n\n # Remove our shim file\n try:\n os.remove(shim_tmp_file.name)\n except IOError:\n pass\n\n # Execute shim\n if extension == 'ps1':\n ret = self.shell.exec_cmd('\"powershell {0}\"'.format(target_shim_file))\n else:\n if not self.winrm:\n ret = self.shell.exec_cmd('/bin/sh \\'$HOME/{0}\\''.format(target_shim_file))\n else:\n ret = saltwinshell.call_python(self, target_shim_file)\n\n # Remove shim from target system\n if not self.winrm:\n self.shell.exec_cmd('rm \\'$HOME/{0}\\''.format(target_shim_file))\n else:\n self.shell.exec_cmd('del {0}'.format(target_shim_file))\n\n return ret", "response": "Run a shim command."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nexecutes a command and return the result of the command.", "response": "def cmd_block(self, is_retry=False):\n '''\n Prepare the pre-check command to send to the subsystem\n\n 1. execute SHIM + command\n 2. check if SHIM returns a master request or if it completed\n 3. handle any master request\n 4. re-execute SHIM + command\n 5. split SHIM results from command results\n 6. return command results\n '''\n self.argv = _convert_args(self.argv)\n log.debug(\n 'Performing shimmed, blocking command as follows:\\n%s',\n ' '.join([six.text_type(arg) for arg in self.argv])\n )\n cmd_str = self._cmd_str()\n stdout, stderr, retcode = self.shim_cmd(cmd_str)\n\n log.trace('STDOUT %s\\n%s', self.target['host'], stdout)\n log.trace('STDERR %s\\n%s', self.target['host'], stderr)\n log.debug('RETCODE %s: %s', self.target['host'], retcode)\n\n error = self.categorize_shim_errors(stdout, stderr, retcode)\n if error:\n if error == 'Python environment not found on Windows system':\n saltwinshell.deploy_python(self)\n stdout, stderr, retcode = self.shim_cmd(cmd_str)\n while re.search(RSTR_RE, stdout):\n stdout = re.split(RSTR_RE, stdout, 1)[1].strip()\n while re.search(RSTR_RE, stderr):\n stderr = re.split(RSTR_RE, stderr, 1)[1].strip()\n elif error == 'Undefined SHIM state':\n self.deploy()\n stdout, stderr, retcode = self.shim_cmd(cmd_str)\n if not re.search(RSTR_RE, stdout) or not re.search(RSTR_RE, stderr):\n # If RSTR is not seen in both stdout and stderr then there\n # was a thin deployment problem.\n return 'ERROR: Failure deploying thin, undefined state: {0}'.format(stdout), stderr, retcode\n while re.search(RSTR_RE, stdout):\n stdout = re.split(RSTR_RE, stdout, 1)[1].strip()\n while re.search(RSTR_RE, stderr):\n stderr = re.split(RSTR_RE, stderr, 1)[1].strip()\n else:\n return 'ERROR: {0}'.format(error), stderr, retcode\n\n # FIXME: this discards output from ssh_shim if the shim succeeds. It should\n # always save the shim output regardless of shim success or failure.\n while re.search(RSTR_RE, stdout):\n stdout = re.split(RSTR_RE, stdout, 1)[1].strip()\n\n if re.search(RSTR_RE, stderr):\n # Found RSTR in stderr which means SHIM completed and only\n # and remaining output is only from salt.\n while re.search(RSTR_RE, stderr):\n stderr = re.split(RSTR_RE, stderr, 1)[1].strip()\n\n else:\n # RSTR was found in stdout but not stderr - which means there\n # is a SHIM command for the master.\n shim_command = re.split(r'\\r?\\n', stdout, 1)[0].strip()\n log.debug('SHIM retcode(%s) and command: %s', retcode, shim_command)\n if 'deploy' == shim_command and retcode == salt.defaults.exitcodes.EX_THIN_DEPLOY:\n self.deploy()\n stdout, stderr, retcode = self.shim_cmd(cmd_str)\n if not re.search(RSTR_RE, stdout) or not re.search(RSTR_RE, stderr):\n if not self.tty:\n # If RSTR is not seen in both stdout and stderr then there\n # was a thin deployment problem.\n log.error(\n 'ERROR: Failure deploying thin, retrying:\\n'\n 'STDOUT:\\n%s\\nSTDERR:\\n%s\\nRETCODE: %s',\n stdout, stderr, retcode\n )\n return self.cmd_block()\n elif not re.search(RSTR_RE, stdout):\n # If RSTR is not seen in stdout with tty, then there\n # was a thin deployment problem.\n log.error(\n 'ERROR: Failure deploying thin, retrying:\\n'\n 'STDOUT:\\n%s\\nSTDERR:\\n%s\\nRETCODE: %s',\n stdout, stderr, retcode\n )\n while re.search(RSTR_RE, stdout):\n stdout = re.split(RSTR_RE, stdout, 1)[1].strip()\n if self.tty:\n stderr = ''\n else:\n while re.search(RSTR_RE, stderr):\n stderr = re.split(RSTR_RE, stderr, 1)[1].strip()\n elif 'ext_mods' == shim_command:\n self.deploy_ext()\n stdout, stderr, retcode = self.shim_cmd(cmd_str)\n if not re.search(RSTR_RE, stdout) or not re.search(RSTR_RE, stderr):\n # If RSTR is not seen in both stdout and stderr then there\n # was a thin deployment problem.\n return 'ERROR: Failure deploying ext_mods: {0}'.format(stdout), stderr, retcode\n while re.search(RSTR_RE, stdout):\n stdout = re.split(RSTR_RE, stdout, 1)[1].strip()\n while re.search(RSTR_RE, stderr):\n stderr = re.split(RSTR_RE, stderr, 1)[1].strip()\n\n return stdout, stderr, retcode"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _lookup_style(element, names):\n '''\n Lookup style by either element name or the list of classes\n '''\n return _STYLES.get('_'+element, '') + \\\n ''.join([_STYLES.get(name, '') for name in names])", "response": "Lookup style by either element name or the list of classes\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngenerating a single table of data", "response": "def _generate_html_table(data, out, level=0, extra_style=''):\n '''\n Generate a single table of data\n '''\n print(''.format(\n _lookup_style('table', ['table' + six.text_type(level)])), file=out)\n\n firstone = True\n\n row_style = 'row' + six.text_type(level)\n cell_style = 'cell' + six.text_type(level)\n\n for subdata in data:\n first_style = 'first_first' if firstone else 'notfirst_first'\n second_style = 'first' if firstone else 'other'\n\n if isinstance(subdata, dict):\n if '__style__' in subdata:\n new_extra_style = subdata['__style__']\n del subdata['__style__']\n else:\n new_extra_style = extra_style\n if len(subdata) == 1:\n name, value = next(six.iteritems(subdata))\n print(''.format(\n _lookup_style('tr', [row_style])\n ), file=out)\n print(''.format(\n _lookup_style(\n 'td',\n [cell_style, first_style, 'name', new_extra_style]\n ),\n name\n ), file=out)\n if isinstance(value, list):\n print('', file=out)\n else:\n print(''.format(\n _lookup_style(\n 'td',\n [\n cell_style,\n second_style,\n 'value',\n new_extra_style\n ]\n ),\n cgi.escape(six.text_type(value))\n ), file=out)\n print('', file=out)\n elif isinstance(subdata, list):\n print(''.format(\n _lookup_style('tr', [row_style])\n ), file=out)\n print('', file=out)\n print('', file=out)\n else:\n print(''.format(\n _lookup_style('tr', [row_style])\n ), file=out)\n print(''.format(\n _lookup_style(\n 'td',\n [cell_style, first_style, 'value', extra_style]\n ),\n cgi.escape(six.text_type(subdata))\n ), file=out)\n print('', file=out)\n firstone = False\n print('
{1}'.format(\n _lookup_style(\n 'td',\n [\n cell_style,\n second_style,\n 'container',\n new_extra_style\n ]\n )\n ), file=out)\n _generate_html_table(\n value,\n out,\n level + 1,\n new_extra_style\n )\n print('{1}
'.format(\n _lookup_style(\n 'td',\n [cell_style, first_style, 'container', extra_style]\n )\n ), file=out)\n _generate_html_table(subdata, out, level + 1, extra_style)\n print('
{1}
', file=out)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngenerating report data as HTML", "response": "def _generate_html(data, out):\n '''\n Generate report data as HTML\n '''\n print('', file=out)\n print('', file=out)\n _generate_html_table(data, out, 0)\n print('', file=out)\n print('', file=out)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nconvert a dictionary to a list of dictionaries to facilitate ordering", "response": "def _dict_to_name_value(data):\n '''\n Convert a dictionary to a list of dictionaries to facilitate ordering\n '''\n if isinstance(data, dict):\n sorted_data = sorted(data.items(), key=lambda s: s[0])\n result = []\n for name, value in sorted_data:\n if isinstance(value, dict):\n result.append({name: _dict_to_name_value(value)})\n else:\n result.append({name: value})\n else:\n result = data\n return result"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngenerating states report from sorted_data", "response": "def _generate_states_report(sorted_data):\n '''\n Generate states report\n '''\n states = []\n for state, data in sorted_data:\n module, stateid, name, function = state.split('_|-')\n module_function = '.'.join((module, function))\n result = data.get('result', '')\n single = [\n {'function': module_function},\n {'name': name},\n {'result': result},\n {'duration': data.get('duration', 0.0)},\n {'comment': data.get('comment', '')}\n ]\n\n if not result:\n style = 'failed'\n else:\n changes = data.get('changes', {})\n if changes and isinstance(changes, dict):\n single.append({'changes': _dict_to_name_value(changes)})\n style = 'changed'\n else:\n style = 'unchanged'\n\n started = data.get('start_time', '')\n if started:\n single.append({'started': started})\n\n states.append({stateid: single, '__style__': style})\n return states"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ngenerate report dictionary for highstate", "response": "def _generate_report(ret, setup):\n '''\n Generate report dictionary\n '''\n\n retdata = ret.get('return', {})\n\n sorted_data = sorted(\n retdata.items(),\n key=lambda s: s[1].get('__run_num__', 0)\n )\n\n total = 0\n failed = 0\n changed = 0\n duration = 0.0\n\n # gather stats\n for _, data in sorted_data:\n if not data.get('result', True):\n failed += 1\n total += 1\n\n try:\n duration += float(data.get('duration', 0.0))\n except ValueError:\n pass\n\n if data.get('changes', {}):\n changed += 1\n\n unchanged = total - failed - changed\n\n log.debug('highstate total: %s', total)\n log.debug('highstate failed: %s', failed)\n log.debug('highstate unchanged: %s', unchanged)\n log.debug('highstate changed: %s', changed)\n\n # generate report if required\n if setup.get('report_everything', False) or \\\n (setup.get('report_changes', True) and changed != 0) or \\\n (setup.get('report_failures', True) and failed != 0):\n\n report = [\n {'stats': [\n {'total': total},\n {'failed': failed, '__style__': 'failed'},\n {'unchanged': unchanged, '__style__': 'unchanged'},\n {'changed': changed, '__style__': 'changed'},\n {'duration': duration}\n ]},\n {'job': [\n {'function': ret.get('fun', '')},\n {'arguments': ret.get('fun_args', '')},\n {'jid': ret.get('jid', '')},\n {'success': ret.get('success', True)},\n {'retcode': ret.get('retcode', 0)}\n ]},\n {'states': _generate_states_report(sorted_data)}\n ]\n\n if failed:\n function = setup.get('failure_function', None)\n else:\n function = setup.get('success_function', None)\n\n if function:\n func_result = __salt__[function]()\n report.insert(\n 0,\n {'extra': [{function: _dict_to_name_value(func_result)}]}\n )\n\n else:\n report = []\n\n return report, failed"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _produce_output(report, failed, setup):\n '''\n Produce output from the report dictionary generated by _generate_report\n '''\n report_format = setup.get('report_format', 'yaml')\n\n log.debug('highstate output format: %s', report_format)\n\n if report_format == 'json':\n report_text = salt.utils.json.dumps(report)\n elif report_format == 'yaml':\n string_file = StringIO()\n salt.utils.yaml.safe_dump(report, string_file, default_flow_style=False)\n string_file.seek(0)\n report_text = string_file.read()\n else:\n string_file = StringIO()\n _generate_html(report, string_file)\n string_file.seek(0)\n report_text = string_file.read()\n\n report_delivery = setup.get('report_delivery', 'file')\n\n log.debug('highstate report_delivery: %s', report_delivery)\n\n if report_delivery == 'file':\n output_file = _sprinkle(setup.get('file_output', '/tmp/test.rpt'))\n with salt.utils.files.fopen(output_file, 'w') as out:\n out.write(salt.utils.stringutils.to_str(report_text))\n else:\n msg = MIMEText(report_text, report_format)\n\n sender = setup.get('smtp_sender', '')\n recipients = setup.get('smtp_recipients', '')\n\n host = setup.get('smtp_server', '')\n port = int(setup.get('smtp_port', 25))\n tls = setup.get('smtp_tls')\n username = setup.get('smtp_username')\n password = setup.get('smtp_password')\n\n if failed:\n subject = setup.get('smtp_failure_subject', 'Installation failure')\n else:\n subject = setup.get('smtp_success_subject', 'Installation success')\n\n subject = _sprinkle(subject)\n\n msg['Subject'] = subject\n msg['From'] = sender\n msg['To'] = recipients\n\n log.debug('highstate smtp port: %d', port)\n smtp = smtplib.SMTP(host=host, port=port)\n\n if tls is True:\n smtp.starttls()\n log.debug('highstate smtp tls enabled')\n\n if username and password:\n smtp.login(username, password)\n log.debug('highstate smtp authenticated')\n\n smtp.sendmail(\n sender,\n [x.strip() for x in recipients.split(',')], msg.as_string())\n log.debug('highstate message sent.')\n\n smtp.quit()", "response": "Produce output from the report dictionary generated by _generate_report\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef returner(ret):\n '''\n Check highstate return information and possibly fire off an email\n or save a file.\n '''\n setup = _get_options(ret)\n\n log.debug('highstate setup %s', setup)\n\n report, failed = _generate_report(ret, setup)\n if report:\n _produce_output(report, failed, setup)", "response": "Check highstate return information and possibly fire off an email\n or save a file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the formatted outputter string for the Python object.", "response": "def out_format(data, out='nested', opts=None, **kwargs):\n '''\n Return the formatted outputter string for the Python object.\n\n data\n The JSON serializable object.\n\n out: ``nested``\n The name of the output to use to transform the data. Default: ``nested``.\n\n opts\n Dictionary of configuration options. Default: ``__opts__``.\n\n kwargs\n Arguments to sent to the outputter module.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' out.out_format \"{'key': 'value'}\"\n '''\n if not opts:\n opts = __opts__\n return salt.output.out_format(data, out, opts=opts, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the outputter formatted string for the passed data.", "response": "def string_format(data, out='nested', opts=None, **kwargs):\n '''\n Return the outputter formatted string, removing the ANSI escape sequences.\n\n data\n The JSON serializable object.\n\n out: ``nested``\n The name of the output to use to transform the data. Default: ``nested``.\n\n opts\n Dictionary of configuration options. Default: ``__opts__``.\n\n kwargs\n Arguments to sent to the outputter module.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' out.string_format \"{'key': 'value'}\" out=table\n '''\n if not opts:\n opts = __opts__\n return salt.output.string_format(data, out, opts=opts, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the formatted string as HTML.", "response": "def html_format(data, out='nested', opts=None, **kwargs):\n '''\n Return the formatted string as HTML.\n\n data\n The JSON serializable object.\n\n out: ``nested``\n The name of the output to use to transform the data. Default: ``nested``.\n\n opts\n Dictionary of configuration options. Default: ``__opts__``.\n\n kwargs\n Arguments to sent to the outputter module.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' out.html_format \"{'key': 'value'}\" out=yaml\n '''\n if not opts:\n opts = __opts__\n return salt.output.html_format(data, out, opts=opts, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a thin wrapper to modules. buildout. buildout_file.", "response": "def installed(name,\n config='buildout.cfg',\n quiet=False,\n parts=None,\n user=None,\n env=(),\n buildout_ver=None,\n test_release=False,\n distribute=None,\n new_st=None,\n offline=False,\n newest=False,\n python=sys.executable,\n debug=False,\n verbose=False,\n unless=None,\n onlyif=None,\n use_vt=False,\n loglevel='debug',\n **kwargs):\n '''\n Install buildout in a specific directory\n\n It is a thin wrapper to modules.buildout.buildout\n\n name\n directory to execute in\n\n quiet\n\n do not output console & logs\n\n config\n buildout config to use (default: buildout.cfg)\n\n parts\n specific buildout parts to run\n\n user\n user used to run buildout as\n\n .. versionadded:: 2014.1.4\n\n env\n environment variables to set when running\n\n buildout_ver\n force a specific buildout version (1 | 2)\n\n test_release\n buildout accept test release\n\n new_st\n Forcing use of setuptools >= 0.7\n\n distribute\n use distribute over setuptools if possible\n\n offline\n does buildout run offline\n\n python\n python to use\n\n debug\n run buildout with -D debug flag\n\n onlyif\n Only execute cmd if statement on the host return 0\n\n unless\n Do not execute cmd if statement on the host return 0\n\n newest\n run buildout in newest mode\n\n verbose\n run buildout in verbose mode (-vvvvv)\n\n use_vt\n Use the new salt VT to stream output [experimental]\n\n loglevel\n loglevel for buildout commands\n '''\n ret = {}\n\n if 'group' in kwargs:\n log.warning(\"Passing 'group' is deprecated, just remove it\")\n output_loglevel = kwargs.get('output_loglevel', None)\n if output_loglevel and not loglevel:\n log.warning(\"Passing 'output_loglevel' is deprecated,\"\n ' please use loglevel instead')\n try:\n test_release = int(test_release)\n except ValueError:\n test_release = None\n\n func = __salt('buildout.buildout')\n kwargs = dict(\n directory=name,\n config=config,\n parts=parts,\n runas=user,\n env=env,\n buildout_ver=buildout_ver,\n test_release=test_release,\n distribute=distribute,\n new_st=new_st,\n offline=offline,\n newest=newest,\n python=python,\n debug=debug,\n verbose=verbose,\n onlyif=onlyif,\n unless=unless,\n use_vt=use_vt,\n loglevel=loglevel\n )\n ret.update(_ret_status(func(**kwargs), name, quiet=quiet))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a string representing the package version.", "response": "def version(*names, **kwargs):\n '''\n Returns a string representing the package version or an empty string if not\n installed. If more than one package name is specified, a dict of\n name/version pairs is returned.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.version \n salt '*' pkg.version ...\n '''\n if len(names) == 1:\n vers = __proxy__['dummy.package_status'](names[0])\n return vers[names[0]]\n else:\n results = {}\n for n in names:\n vers = __proxy__['dummy.package_status'](n)\n results.update(vers)\n return results"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_user_perm(obj, perm, sid):\n '''\n Set an object permission for the given user sid\n '''\n info = (\n win32security.OWNER_SECURITY_INFORMATION |\n win32security.GROUP_SECURITY_INFORMATION |\n win32security.DACL_SECURITY_INFORMATION\n )\n sd = win32security.GetUserObjectSecurity(obj, info)\n dacl = sd.GetSecurityDescriptorDacl()\n ace_cnt = dacl.GetAceCount()\n found = False\n for idx in range(0, ace_cnt):\n (aceType, aceFlags), ace_mask, ace_sid = dacl.GetAce(idx)\n ace_exists = (\n aceType == ntsecuritycon.ACCESS_ALLOWED_ACE_TYPE and\n ace_mask == perm and\n ace_sid == sid\n )\n if ace_exists:\n # If the ace already exists, do nothing\n break\n else:\n dacl.AddAccessAllowedAce(dacl.GetAclRevision(), perm, sid)\n sd.SetSecurityDescriptorDacl(1, dacl, 0)\n win32security.SetUserObjectSecurity(obj, info, sd)", "response": "Sets an object permission for the given user sid\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef grant_winsta_and_desktop(th):\n '''\n Grant the token's user access to the current process's window station and\n desktop.\n '''\n current_sid = win32security.GetTokenInformation(th, win32security.TokenUser)[0]\n # Add permissions for the sid to the current windows station and thread id.\n # This prevents windows error 0xC0000142.\n winsta = win32process.GetProcessWindowStation()\n set_user_perm(winsta, WINSTA_ALL, current_sid)\n desktop = win32service.GetThreadDesktop(win32api.GetCurrentThreadId())\n set_user_perm(desktop, DESKTOP_ALL, current_sid)", "response": "Grant the token s user access to the current process s window station and thread id."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef enumerate_tokens(sid=None, session_id=None, privs=None):\n '''\n Enumerate tokens from any existing processes that can be accessed.\n Optionally filter by sid.\n '''\n for p in psutil.process_iter():\n if p.pid == 0:\n continue\n try:\n ph = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, 0, p.pid)\n except win32api.error as exc:\n if exc.winerror == 5:\n log.debug(\"Unable to OpenProcess pid=%d name=%s\", p.pid, p.name())\n continue\n raise exc\n try:\n access = (\n win32security.TOKEN_DUPLICATE |\n win32security.TOKEN_QUERY |\n win32security.TOKEN_IMPERSONATE |\n win32security.TOKEN_ASSIGN_PRIMARY\n )\n th = win32security.OpenProcessToken(ph, access)\n except Exception as exc:\n log.debug(\"OpenProcessToken failed pid=%d name=%s user%s\", p.pid, p.name(), p.username())\n continue\n try:\n process_sid = win32security.GetTokenInformation(th, win32security.TokenUser)[0]\n except Exception as exc:\n log.exception(\"GetTokenInformation pid=%d name=%s user%s\", p.pid, p.name(), p.username())\n continue\n\n proc_sid = win32security.ConvertSidToStringSid(process_sid)\n if sid and sid != proc_sid:\n log.debug(\"Token for pid does not match user sid: %s\", sid)\n continue\n\n if session_id and win32security.GetTokenInformation(th, win32security.TokenSessionId) != session_id:\n continue\n\n def has_priv(tok, priv):\n luid = win32security.LookupPrivilegeValue(None, priv)\n for priv_luid, flags in win32security.GetTokenInformation(tok, win32security.TokenPrivileges):\n if priv_luid == luid:\n return True\n return False\n if privs:\n has_all = True\n for name in privs:\n if not has_priv(th, name):\n has_all = False\n if not has_all:\n continue\n yield dup_token(th)", "response": "Enumerate tokens from any processes that can be accessed by sid."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfinding an existing process token for the given sid and impersonate it.", "response": "def impersonate_sid(sid, session_id=None, privs=None):\n '''\n Find an existing process token for the given sid and impersonate the token.\n '''\n for tok in enumerate_tokens(sid, session_id, privs):\n tok = dup_token(tok)\n elevate_token(tok)\n if win32security.ImpersonateLoggedOnUser(tok) == 0:\n raise WindowsError(\"Impersonation failure\") # pylint: disable=undefined-variable\n return tok\n raise WindowsError(\"Impersonation failure\")"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nduplicating the access token", "response": "def dup_token(th):\n '''\n duplicate the access token\n '''\n # TODO: is `duplicate_token` the same?\n sec_attr = win32security.SECURITY_ATTRIBUTES()\n sec_attr.bInheritHandle = True\n return win32security.DuplicateTokenEx(\n th,\n win32security.SecurityImpersonation,\n win32con.MAXIMUM_ALLOWED,\n win32security.TokenPrimary,\n sec_attr,\n )"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nelevating all token priviledges to enabled", "response": "def elevate_token(th):\n '''\n Set all token priviledges to enabled\n '''\n # Get list of privileges this token contains\n privileges = win32security.GetTokenInformation(\n th, win32security.TokenPrivileges)\n\n # Create a set of all privileges to be enabled\n enable_privs = set()\n for luid, flags in privileges:\n enable_privs.add((luid, win32con.SE_PRIVILEGE_ENABLED))\n\n # Enable the privileges\n if win32security.AdjustTokenPrivileges(th, 0, enable_privs) == 0:\n raise WindowsError(win32api.FormatMessage(win32api.GetLastError()))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef make_inheritable(token):\n '''Create an inheritable handle'''\n return win32api.DuplicateHandle(\n win32api.GetCurrentProcess(),\n token,\n win32api.GetCurrentProcess(),\n 0,\n 1,\n win32con.DUPLICATE_SAME_ACCESS\n )", "response": "Create an inheritable handle."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nensuring CloudFront distribution is present.", "response": "def present(\n name,\n config,\n tags,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n):\n '''\n Ensure the CloudFront distribution is present.\n\n name (string)\n Name of the CloudFront distribution\n\n config (dict)\n Configuration for the distribution\n\n tags (dict)\n Tags to associate with the distribution\n\n region (string)\n Region to connect to\n\n key (string)\n Secret key to use\n\n keyid (string)\n Access key to use\n\n profile (dict or string)\n A dict with region, key, and keyid,\n or a pillar key (string) that contains such a dict.\n\n Example:\n\n .. code-block:: yaml\n\n Manage my_distribution CloudFront distribution:\n boto_cloudfront.present:\n - name: my_distribution\n - config:\n Comment: 'partial config shown, most parameters elided'\n Enabled: True\n - tags:\n testing_key: testing_value\n '''\n ret = {\n 'name': name,\n 'comment': '',\n 'changes': {},\n }\n\n res = __salt__['boto_cloudfront.get_distribution'](\n name,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if 'error' in res:\n ret['result'] = False\n ret['comment'] = 'Error checking distribution {0}: {1}'.format(\n name,\n res['error'],\n )\n return ret\n\n old = res['result']\n if old is None:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Distribution {0} set for creation.'.format(name)\n ret['changes'] = {'old': None, 'new': name}\n return ret\n\n res = __salt__['boto_cloudfront.create_distribution'](\n name,\n config,\n tags,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if 'error' in res:\n ret['result'] = False\n ret['comment'] = 'Error creating distribution {0}: {1}'.format(\n name,\n res['error'],\n )\n return ret\n\n ret['result'] = True\n ret['comment'] = 'Created distribution {0}.'.format(name)\n ret['changes'] = {'old': None, 'new': name}\n return ret\n else:\n full_config_old = {\n 'config': old['distribution']['DistributionConfig'],\n 'tags': old['tags'],\n }\n full_config_new = {\n 'config': config,\n 'tags': tags,\n }\n diffed_config = __utils__['dictdiffer.deep_diff'](\n full_config_old,\n full_config_new,\n )\n\n def _yaml_safe_dump(attrs):\n '''\n Safely dump YAML using a readable flow style\n '''\n dumper_name = 'IndentedSafeOrderedDumper'\n dumper = __utils__['yaml.get_dumper'](dumper_name)\n return __utils__['yaml.dump'](\n attrs,\n default_flow_style=False,\n Dumper=dumper)\n\n changes_diff = ''.join(difflib.unified_diff(\n _yaml_safe_dump(full_config_old).splitlines(True),\n _yaml_safe_dump(full_config_new).splitlines(True),\n ))\n\n any_changes = bool('old' in diffed_config or 'new' in diffed_config)\n if not any_changes:\n ret['result'] = True\n ret['comment'] = 'Distribution {0} has correct config.'.format(\n name,\n )\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = '\\n'.join([\n 'Distribution {0} set for new config:'.format(name),\n changes_diff,\n ])\n ret['changes'] = {'diff': changes_diff}\n return ret\n\n res = __salt__['boto_cloudfront.update_distribution'](\n name,\n config,\n tags,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile,\n )\n if 'error' in res:\n ret['result'] = False\n ret['comment'] = 'Error updating distribution {0}: {1}'.format(\n name,\n res['error'],\n )\n return ret\n\n ret['result'] = True\n ret['comment'] = 'Updated distribution {0}.'.format(name)\n ret['changes'] = {'diff': changes_diff}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _fix_quantities(tree):\n '''\n Stupidly simple function to fix any Items/Quantity disparities inside a\n DistributionConfig block before use. Since AWS only accepts JSON-encodable\n data types, this implementation is \"good enough\" for our purposes.\n '''\n if isinstance(tree, dict):\n tree = {k: _fix_quantities(v) for k, v in tree.items()}\n if isinstance(tree.get('Items'), list):\n tree['Quantity'] = len(tree['Items'])\n if not tree['Items']:\n tree.pop('Items') # Silly, but AWS requires it....\n return tree\n elif isinstance(tree, list):\n return [_fix_quantities(t) for t in tree]\n else:\n return tree", "response": "This function fixes any Items and Quantity disparities inside a\n DistributionConfig block before use."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef distribution_present(name, region=None, key=None, keyid=None, profile=None, **kwargs):\n '''\n Ensure the given CloudFront distribution exists in the described state.\n\n The implementation of this function, and all those following, is orthagonal\n to that of :py:mod:`boto_cloudfront.present\n `. Resources created with\n :py:mod:`boto_cloudfront.present `\n will not be correctly managed by this function, as a different method is\n used to store Salt's state signifier. This function and those following are\n a suite, designed to work together. As an extra bonus, they correctly\n process updates of the managed resources, so it is recommended to use them\n in preference to :py:mod:`boto_cloudfront.present\n ` above.\n\n Note that the semantics of DistributionConfig (below) are rather arcane,\n and vary wildly depending on whether the distribution already exists or not\n (e.g. is being initially created, or being updated in place). Many more\n details can be found here__.\n\n .. __: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-overview-required-fields.html\n\n name (string)\n Name of the state definition.\n\n Name (string)\n Name of the resource (for purposes of Salt's idempotency). If not\n provided, the value of ``name`` will be used.\n\n DistributionConfig (dict)\n Configuration for the distribution.\n\n Notes:\n\n - The CallerReference field should NOT be provided - it will be\n autopopulated by Salt.\n\n - A large number of sub- (and sub-sub-) fields require a ``Quantity``\n element, which simply COUNTS the number of items in the ``Items``\n element. This is bluntly stupid, so as a convenience, Salt will\n traverse the provided configuration, and add (or fix) a ``Quantity``\n element for any ``Items`` elements of list-type it encounters. This\n adds a bit of sanity to an otherwise error-prone situation. Note\n that for this to work, zero-length lists must be inlined as ``[]``.\n\n - Due to the unavailibity of a better way to store stateful idempotency\n information about Distributions, the Comment sub-element (as the only\n user-settable attribute without weird self-blocking semantics, and\n which is available from the core ``get_distribution()`` API call) is\n utilized to store the Salt state signifier, which is used to\n determine resource existence and state. That said, to enable **some**\n usability of this field, only the value up to the first colon\n character is taken as the signifier, with everything afterward\n free-form, and ignored (but preserved) by Salt.\n\n Tags (dict)\n Tags to associate with the distribution.\n\n region (string)\n Region to connect to.\n\n key (string)\n Secret key to use.\n\n keyid (string)\n Access key to use.\n\n profile (dict or string)\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n Example:\n\n .. code-block:: yaml\n\n plt-dev-spaapi-cf-dist-cf_dist-present:\n boto_cloudfront.distribution_present:\n - Name: plt-dev-spaapi-cf-dist\n - DistributionConfig:\n Comment: SPA\n Logging:\n Enabled: false\n Prefix: ''\n Bucket: ''\n IncludeCookies: false\n WebACLId: ''\n Origins:\n Items:\n - S3OriginConfig:\n OriginAccessIdentity: the-SPA-OAI\n OriginPath: ''\n CustomHeaders:\n Items: []\n Id: S3-hs-backend-srpms\n DomainName: hs-backend-srpms.s3.amazonaws.com\n PriceClass: PriceClass_All\n DefaultRootObject: ''\n Enabled: true\n DefaultCacheBehavior:\n ViewerProtocolPolicy: allow-all\n TrustedSigners:\n Items: []\n Enabled: false\n SmoothStreaming: false\n TargetOriginId: S3-hs-backend-srpms\n FieldLevelEncryptionId: ''\n ForwardedValues:\n Headers:\n Items: []\n Cookies:\n Forward: none\n QueryStringCacheKeys:\n Items: []\n QueryString: false\n MaxTTL: 31536000\n LambdaFunctionAssociations:\n Items: []\n DefaultTTL: 86400\n AllowedMethods:\n CachedMethods:\n Items:\n - HEAD\n - GET\n Items:\n - HEAD\n - GET\n MinTTL: 0\n Compress: false\n IsIPV6Enabled: true\n ViewerCertificate:\n CloudFrontDefaultCertificate: true\n MinimumProtocolVersion: TLSv1\n CertificateSource: cloudfront\n Aliases:\n Items:\n - bubba-hotep.bodhi-dev.io\n HttpVersion: http2\n - Tags:\n Owner: dev_engrs\n\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}\n authargs = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n Name = kwargs.pop('Name', name)\n Tags = kwargs.pop('Tags', None)\n DistributionConfig = kwargs.get('DistributionConfig', {})\n\n ## Sub-element munging on config data should go in here, before we proceed:\n # For instance, origin access identities must be of the form\n # `origin-access-identity/cloudfront/ID-of-origin-access-identity`, but we can't really\n # know that ID apriori, so any OAI state names inside the config data must be resolved\n # and converted into that format before submission. Be aware that the `state names` of\n # salt managed OAIs are stored in their Comment fields for lack of any better place...\n for item in range(len(DistributionConfig.get('Origins', {}).get('Items', []))):\n oai = DistributionConfig['Origins']['Items'][item].get('S3OriginConfig',\n {}).get('OriginAccessIdentity', '')\n if oai and not oai.startswith('origin-access-identity/cloudfront/'):\n res = __salt__['boto_cloudfront.get_cloud_front_origin_access_identities_by_comment'](\n Comment=oai, region=region, key=key, keyid=keyid, profile=profile)\n if res is None: # An error occurred, bubble it up...\n log.warning('Error encountered while trying to determine the Resource ID of'\n ' CloudFront origin access identity `%s`. Passing as-is.', oai)\n elif not res:\n log.warning('Failed to determine the Resource ID of CloudFront origin access'\n ' identity `%s`. Passing as-is.', oai)\n elif len(res) > 1:\n log.warning('Failed to find unique Resource ID for CloudFront origin access'\n ' identity `%s`. Passing as-is.', oai)\n else:\n # One unique OAI resource found -- deref and replace it...\n new = 'origin-access-identity/cloudfront/{}'.format(res[0]['Id'])\n DistributionConfig['Origins']['Items'][item]['S3OriginConfig']['OriginAccessIdentity'] = new\n # Munge Name into the Comment field...\n DistributionConfig['Comment'] = '{}:{}'.format(Name, DistributionConfig['Comment']) \\\n if DistributionConfig.get('Comment') else Name\n\n # Fix up any missing (or wrong) Quantity sub-elements...\n DistributionConfig = _fix_quantities(DistributionConfig)\n kwargs['DistributionConfig'] = DistributionConfig\n\n # Current state of the thing?\n res = __salt__['boto_cloudfront.get_distributions_by_comment'](Comment=Name, region=region,\n key=key, keyid=keyid, profile=profile)\n if res is None:\n msg = 'Error determining current state of distribution `{}`.'.format(Name)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n if len(res) > 1:\n msg = 'Multiple CloudFront distibutions matched `{}`.'.format(Name)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n\n # Luckily, the `DistributionConfig` structure returned by `get_distribution()` (as a sub-\n # element of `Distribution`) is identical to that returned by `get_distribution_config(),\n # and as a bonus, the ETag's are ALSO compatible...\n # Since \"updates\" are actually \"replace everything from scratch\" events, this implies that\n # it's enough to simply determine SOME update is necessary to trigger one, rather than\n # exhaustively calculating all changes needed - this makes life MUCH EASIER :)\n # Thus our workflow here is:\n # - check if the distribution exists\n # - if it doesn't, create it fresh with the requested DistributionConfig, and Tag it if needed\n # - if it does, grab its ETag, and TWO copies of the current DistributionConfig\n # - merge the requested DistributionConfig on top of one of them\n # - compare the copy we just merged against the one we didn't\n # - if they differ, send the merged copy, along with the ETag we got, back as an update\n # - lastly, verify and set/unset any Tags which may need changing...\n exists = bool(res)\n if not exists:\n if 'CallerReference' not in kwargs['DistributionConfig']:\n kwargs['DistributionConfig']['CallerReference'] = str(uuid.uuid4())\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'CloudFront distribution `{}` would be created.'.format(Name)\n new = {'DistributionConfig': kwargs['DistributionConfig']}\n new.update({'Tags': Tags}) if Tags else None\n ret['pchanges'] = {'old': None, 'new': new}\n return ret\n kwargs.update(authargs)\n comments = []\n res = __salt__['boto_cloudfront.create_distribution_v2'](**kwargs)\n if res is None:\n ret['result'] = False\n msg = 'Error occurred while creating distribution `{}`.'.format(Name)\n log.error(msg)\n ret['comment'] = msg\n return ret\n new = {'DistributionConfig': res['Distribution']['DistributionConfig']}\n comments += ['Created distribution `{}`.'.format(Name)]\n newARN = res.get('Distribution', {}).get('ARN')\n tagged = __salt__['boto_cloudfront.tag_resource'](Tags=Tags, **authargs)\n if tagged is False:\n ret['result'] = False\n msg = 'Error occurred while tagging distribution `{}`.'.format(Name)\n log.error(msg)\n comments += [msg]\n ret['comment'] = ' '.join(comments)\n return ret\n comments += ['Tagged distribution `{}`.'.format(Name)]\n new['Tags'] = Tags\n ret['comment'] = ' '.join(comments)\n ret['changes'] = {'old': None, 'new': new}\n return ret\n else:\n currentId = res[0]['Id']\n current = __salt__['boto_cloudfront.get_distribution_v2'](Id=currentId, **authargs)\n # Insanely unlikely given that we JUST got back this Id from the previous search, but....\n if not current:\n msg = 'Failed to lookup CloudFront distribution with Id `{}`.'.format(currentId)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n currentDC = current['Distribution']['DistributionConfig']\n currentARN = current['Distribution']['ARN']\n currentETag = current['ETag']\n currentTags = __salt__['boto_cloudfront.list_tags_for_resource'](Resource=currentARN,\n **authargs)\n copyOne = copy.deepcopy(currentDC)\n copyTwo = copy.deepcopy(currentDC)\n copyTwo.update(kwargs['DistributionConfig'])\n correct = __utils__['boto3.json_objs_equal'](copyOne, copyTwo)\n tags_correct = (currentTags == Tags)\n comments = []\n old = {}\n new = {}\n\n if correct and tags_correct:\n ret['comment'] = 'CloudFront distribution `{}` is in the correct state.'.format(Name)\n return ret\n if __opts__['test']:\n ret['result'] = None\n if not correct:\n comments += ['CloudFront distribution `{}` config would be updated.'.format(Name)]\n old['DistributionConfig'] = copyOne\n new['DistributionConfig'] = copyTwo\n if not tags_correct:\n comments += ['CloudFront distribution `{}` Tags would be updated.'.format(Name)]\n old['Tags'] = currentTags\n new['Tags'] = Tags\n ret['comment'] = ' '.join(comments)\n ret['pchanges'] = {'old': old, 'new': new}\n return ret\n if not correct:\n kwargs = {'DistributionConfig': copyTwo, 'Id': currentId, 'IfMatch': currentETag}\n kwargs.update(authargs)\n log.debug('Calling `boto_cloudfront.update_distribution_v2()` with **kwargs =='\n ' %s', kwargs)\n res = __salt__['boto_cloudfront.update_distribution_v2'](**kwargs)\n if res is None:\n ret['result'] = False\n msg = 'Error occurred while updating distribution `{}`.'.format(Name)\n log.error(msg)\n ret['comment'] = msg\n return ret\n old['DistributionConfig'] = copyOne\n new['DistributionConfig'] = res['Distribution']['DistributionConfig']\n comments += ['CloudFront distribution `{}` config updated.'.format(Name)]\n if not tags_correct:\n tagged = __salt__['boto_cloudfront.enforce_tags'](Resource=currentARN, Tags=Tags,\n **authargs)\n if tagged is False:\n ret['result'] = False\n msg = 'Error occurred while updating Tags on distribution `{}`.'.format(Name)\n log.error(msg)\n comments += [msg]\n ret['comment'] = ' '.join(comments)\n return ret\n comments += ['CloudFront distribution `{}` Tags updated.'.format(Name)]\n old['Tags'] = currentTags\n new['Tags'] = Tags\n ret['comment'] = ' '.join(comments)\n ret['changes'] = {'old': old, 'new': new}\n return ret", "response": "Ensures that the given CloudFront distribution exists in the described state."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef oai_bucket_policy_present(name, Bucket, OAI, Policy,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure the given policy exists on an S3 bucket, granting access for the given origin access\n identity to do the things specified in the policy.\n\n name\n The name of the state definition\n\n Bucket\n The S3 bucket which CloudFront needs access to. Note that this policy\n is exclusive - it will be the only policy definition on the bucket (and\n objects inside the bucket if you specify such permissions in the\n policy). Note that this likely SHOULD reflect the bucket mentioned in\n the Resource section of the Policy, but this is not enforced...\n\n OAI\n The value of `Name` passed to the state definition for the origin\n access identity which will be accessing the bucket.\n\n Policy\n The full policy document which should be set on the S3 bucket. If a\n ``Principal`` clause is not provided in the policy, one will be\n automatically added, and pointed at the correct value as dereferenced\n from the OAI provided above. If one IS provided, then this is not\n done, and you are responsible for providing the correct values.\n\n region (string)\n Region to connect to.\n\n key (string)\n Secret key to use.\n\n keyid (string)\n Access key to use.\n\n profile (dict or string)\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n Example:\n\n .. code-block:: yaml\n\n my_oai_s3_policy:\n boto_cloudfront.oai_bucket_policy_present:\n - Bucket: the_bucket_for_my_distribution\n - OAI: the_OAI_I_just_created_and_attached_to_my_distribution\n - Policy:\n Version: 2012-10-17\n Statement:\n - Effect: Allow\n Action: s3:GetObject\n Resource: arn:aws:s3:::the_bucket_for_my_distribution/*\n\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n oais = __salt__['boto_cloudfront.get_cloud_front_origin_access_identities_by_comment'](\n Comment=OAI, region=region, key=key, keyid=keyid, profile=profile)\n if len(oais) > 1:\n msg = 'Multiple origin access identities matched `{}`.'.format(OAI)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n if not oais:\n msg = 'No origin access identities matched `{}`.'.format(OAI)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n canonical_user = oais[0].get('S3CanonicalUserId')\n oai_id = oais[0].get('Id')\n if isinstance(Policy, six.string_types):\n Policy = json.loads(Policy)\n for stanza in range(len(Policy.get('Statement', []))):\n if 'Principal' not in Policy['Statement'][stanza]:\n Policy['Statement'][stanza]['Principal'] = {\"CanonicalUser\": canonical_user}\n bucket = __salt__['boto_s3_bucket.describe'](Bucket=Bucket, region=region, key=key,\n keyid=keyid, profile=profile)\n if not bucket or 'bucket' not in bucket:\n msg = 'S3 bucket `{}` not found.'.format(Bucket)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n curr_policy = bucket['bucket'].get('Policy', {}).get('Policy', {}) # ?!? dunno, that's just how it gets returned...\n curr_policy = json.loads(curr_policy) if isinstance(curr_policy,\n six.string_types) else curr_policy\n # Sooooo, you have to SUBMIT Principals of the form\n # Principal: {'S3CanonicalUserId': someCrazyLongMagicValueAsDerivedAbove}\n # BUT, they RETURN the Principal as something WILDLY different\n # Principal: {'AWS': arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity E30ABCDEF12345}\n # which obviously compare different on every run... So we fake it thusly.\n fake_Policy = copy.deepcopy(Policy)\n for stanza in range(len(fake_Policy.get('Statement', []))):\n # Warning: unavoidable hardcoded magic values HO!\n fake_Policy['Statement'][stanza].update({'Principal': {'AWS':\n 'arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity {}'.format(oai_id)}})\n if __utils__['boto3.json_objs_equal'](curr_policy, fake_Policy):\n msg = 'Policy of S3 bucket `{}` is in the correct state.'.format(Bucket)\n log.info(msg)\n ret['comment'] = msg\n return ret\n if __opts__['test']:\n ret['comment'] = 'Policy on S3 bucket `{}` would be updated.'.format(Bucket)\n ret['result'] = None\n ret['changes'] = {'old': curr_policy, 'new': fake_Policy}\n return ret\n res = __salt__['boto_s3_bucket.put_policy'](Bucket=Bucket, Policy=Policy,\n region=region, key=key, keyid=keyid, profile=profile)\n if 'error' in res:\n ret['comment'] = 'Failed to update policy on S3 bucket `{}`: {}'.format(Bucket,\n res['error'])\n ret['return'] = False\n return ret\n ret['comment'] = 'Policy on S3 bucket `{}` updated.'.format(Bucket)\n ret['changes'] = {'old': curr_policy, 'new': fake_Policy}\n return ret", "response": "Ensure the given policy exists on an S3 bucket and OAI is present on the given bucket."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef route53_alias_present(name, region=None, key=None, keyid=None, profile=None, **kwargs):\n '''\n Ensure a Route53 Alias exists and is pointing at the given CloudFront\n distribution. An ``A`` record is always created, and if IPV6 is enabled on\n the given distribution, an ``AAAA`` record will be created as well. Also be\n aware that Alias records for CloudFront distributions are only permitted in\n non-private zones.\n\n name\n The name of the state definition.\n\n Distribution\n The name of the CloudFront distribution. Defaults to the value of\n ``name`` if not provided.\n\n HostedZoneId\n Id of the Route53 hosted zone within which the records should be created.\n\n DomainName\n The domain name associated with the Hosted Zone. Exclusive with HostedZoneId.\n\n ResourceRecordSet\n A Route53 Record Set (with AliasTarget section, suitable for use as an\n ``Alias`` record, if non-default settings are needed on the Alias)\n which should be pointed at the provided CloudFront distribution. Note\n that this MUST correlate with the Aliases set within the\n DistributionConfig section of the distribution.\n\n Some notes *specifically* about the ``AliasTarget`` subsection of the\n ResourceRecordSet:\n\n - If not specified, the ``DNSName`` sub-field will be populated by\n dereferencing ``Distribution`` above to the value of its\n ``DomainName`` attribute.\n\n - The HostedZoneId sub-field should not be provided -- it will be\n automatically populated with a ``magic`` AWS value.\n\n - The EvaluateTargetHealth can only be False on a CloudFront Alias.\n\n - The above items taken all together imply that, for most use-cases,\n the AliasTarget sub-section can be entirely omitted, as seen in the\n first code sample below.\n\n Lastly, note that if you set ``name`` to the desired ResourceRecordSet\n Name, you can entirely omit this parameter, as shown in the second\n example below.\n\n .. code-block:: yaml\n\n Add a Route53 Alias for my_distribution:\n boto_cloudfront.present:\n - Distribution: my_distribution\n - DomainName: saltstack.org.\n - ResourceRecordSet:\n Name: the-alias.saltstack.org.\n # This is even simpler - it uses the value of `name` for ResourceRecordSet.Name\n another-alias.saltstack.org.:\n boto_cloudfront.present:\n - Distribution: my_distribution\n - DomainName: saltstack.org.\n\n '''\n MAGIC_CLOUDFRONT_HOSTED_ZONEID = 'Z2FDTNDATAQYW2'\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n Distribution = kwargs['Distribution'] if 'Distribution' in kwargs else name\n ResourceRecordSet = kwargs.get('ResourceRecordSet', {})\n Name = ResourceRecordSet.get('Name', name)\n ResourceRecordSet['Name'] = Name\n\n res = __salt__['boto_cloudfront.get_distributions_by_comment'](Comment=Distribution,\n region=region, key=key, keyid=keyid, profile=profile)\n if res is None:\n msg = 'Error resolving CloudFront distribution `{}` to a Resource ID.'.format(Distribution)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n if len(res) > 1:\n msg = 'Multiple CloudFront distibutions matched `{}`.'.format(Distribution)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n if not res:\n msg = 'No CloudFront distibutions matching `{}` found.'.format(Distribution)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n dist = res[0]\n\n Types = ('A', 'AAAA') if dist.get('IsIPV6Enabled', False) else ('A',)\n DNSName = dist.get('DomainName', '')\n Aliases = dist.get('Aliases', {}).get('Items', [])\n # AWS annoyance #437:\n # Route53 \"FQDNs\" (correctly!) REQUIRE trailing periods...\n # while CloudFront \"FQDNs\" don't PERMIT trailing periods...\n Aliases += [(a if a.endswith('.') else '{}.'.format(a)) for a in Aliases]\n if Name not in Aliases:\n msg = ('Route53 alias `{}` requested which is not mirrored in the `Aliases`'\n ' sub-section of the DistributionConfig.'.format(Name))\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n\n changes = {'old': [], 'new': []}\n comments = []\n # Now mock out a route53 state def, and use the route53 rr_exists state to enforce it...\n AliasTarget = ResourceRecordSet.get('AliasTarget', {})\n AliasTarget['DNSName'] = AliasTarget['DNSName'] if 'DNSName' in AliasTarget else DNSName\n AliasTarget['DNSName'] += '' if AliasTarget['DNSName'].endswith('.') else '.' # GRRRR!\n AliasTarget['HostedZoneId'] = MAGIC_CLOUDFRONT_HOSTED_ZONEID\n AliasTarget['EvaluateTargetHealth'] = False # Route53 limitation\n ResourceRecordSet['name'] = Name\n ResourceRecordSet['AliasTarget'] = AliasTarget\n ResourceRecordSet['PrivateZone'] = False # Route53 limitation\n ResourceRecordSet['DomainName'] = kwargs.get('DomainName')\n ResourceRecordSet['HostedZoneId'] = kwargs.get('HostedZoneId')\n ResourceRecordSet.update({'region': region, 'key': key, 'keyid': keyid, 'profile': profile})\n for Type in Types:\n ResourceRecordSet['Type'] = Type\n # Checking for `test=True` will occur in the called state....\n log.debug('Calling state function `boto3_route53.rr_present()` with args: `%s`',\n ResourceRecordSet)\n res = __states__['boto3_route53.rr_present'](**ResourceRecordSet)\n ret['result'] = res['result']\n comments += [res['comment']]\n if ret['result'] not in (True, None):\n break\n changes['old'] += [res['changes']['old']] if res['changes'].get('old') else []\n changes['new'] += [res['changes']['new']] if res['changes'].get('new') else []\n ret['changes'].update({'old': changes['old']}) if changes.get('old') else None\n ret['changes'].update({'new': changes['new']}) if changes.get('new') else None\n ret['comment'] = ' '.join(comments)\n return ret", "response": "Ensure a Route53 Alias exists and is pointing at the given CloudFront Distribution."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nensures a CloudFront distribution with the given Name tag does not exist.", "response": "def distribution_absent(name, region=None, key=None, keyid=None, profile=None, **kwargs):\n '''\n Ensure a distribution with the given Name tag does not exist.\n\n Note that CloudFront does not allow directly deleting an enabled\n Distribution. If such is requested, Salt will attempt to first update the\n distribution's status to Disabled, and once that returns success, to then\n delete the resource. THIS CAN TAKE SOME TIME, so be patient :)\n\n name (string)\n Name of the state definition.\n\n Name (string)\n Name of the CloudFront distribution to be managed. If not provided, the\n value of ``name`` will be used as a default. The purpose of this\n parameter is only to resolve it to a Resource ID, so be aware that an\n explicit value for ``Id`` below will override any value provided, or\n defaulted, here.\n\n Id (string)\n The Resource ID of a CloudFront distribution to be managed.\n\n region (string)\n Region to connect to\n\n key (string)\n Secret key to use\n\n keyid (string)\n Access key to use\n\n profile (dict or string)\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n Example:\n\n .. code-block:: yaml\n\n Ensure a distribution named my_distribution is gone:\n boto_cloudfront.distribution_absent:\n - Name: my_distribution\n\n '''\n Name = kwargs['Name'] if 'Name' in kwargs else name\n Id = kwargs.get('Id')\n ref = kwargs['Id'] if 'Id' in kwargs else Name\n ret = {'name': Id if Id else Name, 'comment': '', 'changes': {}, 'result': True}\n authargs = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n if not Id:\n res = __salt__['boto_cloudfront.get_distributions_by_comment'](Comment=Name, **authargs)\n if res is None:\n msg = 'Error dereferencing CloudFront distribution `{}` to a Resource ID.'.format(Name)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n if len(res) > 1:\n msg = ('Multiple CloudFront distibutions matched `{}`, no way to know which to'\n ' delete.`.'.format(Name))\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n if not res:\n msg = 'CloudFront Distribution `{}` already absent.'.format(Name)\n log.info(msg)\n ret['comment'] = msg\n ret['result'] = True\n return ret\n Id = res[0]['Id']\n\n if not __salt__['boto_cloudfront.distribution_exists'](Id=Id, **authargs):\n msg = 'CloudFront distribution `{}` already absent.'.format(ref)\n log.info(msg)\n ret['comment'] = msg\n return ret\n\n old = __salt__['boto_cloudfront.get_distribution_v2'](Id=Id, **authargs)\n if old is None:\n ret['result'] = False\n msg = 'Error getting state of CloudFront distribution `{}`.'.format(ref)\n log.error(msg)\n ret['comment'] = msg\n return ret\n currETag = old['ETag']\n\n Enabled = old['DistributionConfig']['Enabled']\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'CloudFront distribution `{}` would be {}deleted.'.format(ref,\n ('disabled and ' if Enabled else ''))\n ret['pchanges'] = {'old': old, 'new': None}\n return ret\n\n comments = []\n if Enabled:\n disabled = __salt__['boto_cloudfront.disable_distribution'](Id=Id, **authargs)\n if disabled is None:\n ret['result'] = False\n msg = 'Error disabling CloudFront distribution `{}`'.format(ref)\n log.error(msg)\n ret['comment'] = msg\n return ret\n comments += ['CloudFront distribution `{}` disabled.'.format(ref)]\n currETag = disabled['ETag']\n deleted = __salt__['boto_cloudfront.delete_distribution'](Id=Id, IfMatch=currETag, **authargs)\n if deleted is False:\n ret['result'] = False\n msg = 'Error deleting CloudFront distribution `{}`'.format(ref)\n comments += [msg]\n log.error(msg)\n ret['comment'] = ' '.join(comments)\n return ret\n msg = 'CloudFront distribution `{}` deleted.'.format(ref)\n comments += [msg]\n log.info(msg)\n ret['comment'] = ' '.join(comments)\n ret['changes'] = {'old': old, 'new': None}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nensure a CloudFront Origin Access Identity exists.", "response": "def origin_access_identity_present(name, region=None, key=None, keyid=None, profile=None, **kwargs):\n '''\n Ensure a given CloudFront Origin Access Identity exists.\n\n .. note::\n Due to the unavailibity of ANY other way to store stateful idempotency\n information about Origin Access Identities (including resource tags),\n the Comment attribute (as the only user-settable attribute without\n weird self-blocking semantics) is necessarily utilized to store the\n Salt state signifier, which is used to determine resource existence and\n state. That said, to enable SOME usability of this field, only the\n value up to the first colon character is taken as the signifier, while\n anything afterward is free-form and ignored by Salt.\n\n name (string)\n Name of the state definition.\n\n Name (string)\n Name of the resource (for purposes of Salt's idempotency). If not provided, the value of\n `name` will be used.\n\n Comment\n Free-form text description of the origin access identity.\n\n region (string)\n Region to connect to\n\n key (string)\n Secret key to use\n\n keyid (string)\n Access key to use\n\n profile (dict or string)\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n Example:\n\n .. code-block:: yaml\n\n my_OAI:\n boto_cloudfront.origin_access_identity_present:\n - Comment: Simply ensures an OAI named my_OAI exists\n\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}\n authargs = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n Name = kwargs.get('Name', name)\n # Munge Name into the Comment field...\n Comment = '{}:{}'.format(Name, kwargs['Comment']) if kwargs.get('Comment') else Name\n\n # Current state of the thing?\n res = __salt__['boto_cloudfront.get_cloud_front_origin_access_identities_by_comment'](\n Comment=Name, region=region, key=key, keyid=keyid, profile=profile)\n if res is None:\n msg = 'Error determining current state of origin access identity `{}`.'.format(Name)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n if len(res) > 1:\n msg = 'Multiple CloudFront origin access identities matched `{}`.'.format(Name)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n\n exists = bool(res)\n if not exists:\n CloudFrontOriginAccessIdentityConfig = {'Comment': Comment,\n 'CallerReference': str(uuid.uuid4())}\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'CloudFront origin access identity `{}` would be created.'.format(Name)\n new = {'CloudFrontOriginAccessIdentityConfig': CloudFrontOriginAccessIdentityConfig}\n ret['pchanges'] = {'old': None, 'new': new}\n return ret\n kwargs = {'CloudFrontOriginAccessIdentityConfig': CloudFrontOriginAccessIdentityConfig}\n kwargs.update(authargs)\n res = __salt__['boto_cloudfront.create_cloud_front_origin_access_identity'](**kwargs)\n if res is None:\n ret['result'] = False\n msg = 'Failed to create CloudFront origin access identity `{}`.'.format(Name)\n log.error(msg)\n ret['comment'] = msg\n return ret\n ret['comment'] = 'Created CloudFrong origin access identity`{}`.'.format(Name)\n ret['changes'] = {'old': None, 'new': res}\n return ret\n else:\n currentId = res[0]['Id']\n current = __salt__['boto_cloudfront.get_cloud_front_origin_access_identity'](Id=currentId,\n **authargs)\n currentETag = current['ETag']\n currentOAIC = current['CloudFrontOriginAccessIdentity']['CloudFrontOriginAccessIdentityConfig']\n new = copy.deepcopy(currentOAIC)\n new.update({'Comment': Comment}) # Currently the only updatable element :-/\n if currentOAIC == new:\n msg = 'CloudFront origin access identity `{}` is in the correct state.'.format(Name)\n log.info(msg)\n ret['comment'] = msg\n return ret\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'CloudFront origin access identity `{}` would be updated.'.format(Name)\n ret['pchanges'] = {'old': currentOAIC, 'new': new}\n return ret\n kwargs = {'CloudFrontOriginAccessIdentityConfig': new,\n 'Id': currentId, 'IfMatch': currentETag}\n kwargs.update(authargs)\n res = __salt__['boto_cloudfront.update_cloud_front_origin_access_identity'](**kwargs)\n if res is None:\n ret['result'] = False\n msg = 'Error occurred while updating origin access identity `{}`.'.format(Name)\n log.error(msg)\n ret['comment'] = msg\n return ret\n ret['comment'] = 'CloudFront origin access identity `{}` config updated.'.format(Name)\n ret['changes'] = {'old': currentOAIC, 'new': new}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nensure a CloudFront Origin Access Identity is absent.", "response": "def origin_access_identity_absent(name, region=None, key=None, keyid=None, profile=None, **kwargs):\n '''\n Ensure a given CloudFront Origin Access Identity is absent.\n\n name\n The name of the state definition.\n\n Name (string)\n Name of the resource (for purposes of Salt's idempotency). If not\n provided, the value of ``name`` will be used.\n\n Id (string)\n The Resource ID of a CloudFront origin access identity to be managed.\n\n region (string)\n Region to connect to\n\n key (string)\n Secret key to use\n\n keyid (string)\n Access key to use\n\n profile (dict or string)\n Dict, or pillar key pointing to a dict, containing AWS region/key/keyid.\n\n Example:\n\n .. code-block:: yaml\n\n Ensure an origin access identity named my_OAI is gone:\n boto_cloudfront.origin_access_identity_absent:\n - Name: my_distribution\n\n '''\n Name = kwargs['Name'] if 'Name' in kwargs else name\n Id = kwargs.get('Id')\n ref = kwargs['Id'] if 'Id' in kwargs else Name\n ret = {'name': Id if Id else Name, 'comment': '', 'changes': {}, 'result': True}\n authargs = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n current = None\n if not Id:\n current = __salt__['boto_cloudfront.get_cloud_front_origin_access_identities_by_comment'](\n Comment=Name, **authargs)\n if current is None:\n msg = 'Error dereferencing origin access identity `{}` to a Resource ID.'.format(Name)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n if len(current) > 1:\n msg = ('Multiple CloudFront origin access identities matched `{}`, no way to know'\n ' which to delete.`.'.format(Name))\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n if not current:\n msg = 'CloudFront origin access identity `{}` already absent.'.format(Name)\n log.info(msg)\n ret['comment'] = msg\n ret['result'] = True\n return ret\n Id = current[0]['Id']\n\n if not __salt__['boto_cloudfront.cloud_front_origin_access_identity_exists'](Id=Id, **authargs):\n msg = 'CloudFront origin access identity `{}` already absent.'.format(ref)\n log.info(msg)\n ret['comment'] = msg\n return ret\n\n old = __salt__['boto_cloudfront.get_cloud_front_origin_access_identity'](Id=Id, **authargs)\n if old is None:\n ret['result'] = False\n msg = 'Error getting state of CloudFront origin access identity `{}`.'.format(ref)\n log.error(msg)\n ret['comment'] = msg\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'CloudFront origin access identity `{}` would be deleted.'.format(ref)\n ret['pchanges'] = {'old': old, 'new': None}\n return ret\n\n deleted = __salt__['boto_cloudfront.delete_cloud_front_origin_access_identity'](Id=Id,\n IfMatch=old['ETag'], **authargs)\n if deleted is False:\n ret['result'] = False\n msg = 'Error deleting CloudFront origin access identity `{}`'.format(ref)\n log.error(msg)\n ret['comment'] = msg\n return ret\n msg = 'CloudFront origin access identity `{}` deleted.'.format(ref)\n log.info(msg)\n ret['comment'] = msg\n ret['changes'] = {'old': old, 'new': None}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef install(pkg, channel=None, refresh=False):\n '''\n Install the specified snap package from the specified channel.\n Returns a dictionary of \"result\" and \"output\".\n\n pkg\n The snap package name\n\n channel\n Optional. The snap channel to install from, eg \"beta\"\n\n refresh : False\n If True, use \"snap refresh\" instead of \"snap install\".\n This allows changing the channel of a previously installed package.\n '''\n args = []\n ret = {'result': None, 'output': \"\"}\n\n if refresh:\n cmd = 'refresh'\n else:\n cmd = 'install'\n\n if channel:\n args.append('--channel=' + channel)\n\n try:\n # Try to run it, merging stderr into output\n ret['output'] = subprocess.check_output([SNAP_BINARY_NAME, cmd, pkg] + args, stderr=subprocess.STDOUT)\n ret['result'] = True\n except subprocess.CalledProcessError as e:\n ret['output'] = e.output\n ret['result'] = False\n\n return ret", "response": "Install the specified snap package from the specified channel."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nremove the specified snap package. Returns a dictionary of result and output.", "response": "def remove(pkg):\n '''\n Remove the specified snap package. Returns a dictionary of \"result\" and \"output\".\n\n pkg\n The package name\n '''\n ret = {'result': None, 'output': \"\"}\n try:\n ret['output'] = subprocess.check_output([SNAP_BINARY_NAME, 'remove', pkg])\n ret['result'] = True\n except subprocess.CalledProcessError as e:\n ret['output'] = e.output\n ret['result'] = False"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of 0 or more dictionaries where the keys are name version tracking and publisher are versions of the specified snap package.", "response": "def versions_installed(pkg):\n '''\n Query which version(s) of the specified snap package are installed.\n Returns a list of 0 or more dictionaries.\n\n pkg\n The package name\n '''\n\n try:\n # Try to run it, merging stderr into output\n output = subprocess.check_output([SNAP_BINARY_NAME, 'list', pkg], stderr=subprocess.STDOUT)\n except subprocess.CalledProcessError:\n return []\n\n lines = output.splitlines()[1:]\n ret = []\n for item in lines:\n # If fields contain spaces this will break.\n i = item.split()\n # Ignore 'Notes' field\n ret.append({\n 'name': i[0],\n 'version': i[1],\n 'rev': i[2],\n 'tracking': i[3],\n 'publisher': i[4]\n })\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates a virtual environment and return a new virtual environment.", "response": "def create(path,\n venv_bin=None,\n system_site_packages=False,\n distribute=False,\n clear=False,\n python=None,\n extra_search_dir=None,\n never_download=None,\n prompt=None,\n pip=False,\n symlinks=None,\n upgrade=None,\n user=None,\n use_vt=False,\n saltenv='base',\n **kwargs):\n '''\n Create a virtualenv\n\n path\n The path to the virtualenv to be created\n\n venv_bin\n The name (and optionally path) of the virtualenv command. This can also\n be set globally in the pillar data as ``venv_bin``.\n Defaults to ``pyvenv`` or ``virtualenv``, depending on what is installed.\n\n system_site_packages : False\n Passthrough argument given to virtualenv or pyvenv\n\n distribute : False\n Passthrough argument given to virtualenv\n\n pip : False\n Install pip after creating a virtual environment. Implies\n ``distribute=True``\n\n clear : False\n Passthrough argument given to virtualenv or pyvenv\n\n python : None (default)\n Passthrough argument given to virtualenv\n\n extra_search_dir : None (default)\n Passthrough argument given to virtualenv\n\n never_download : None (default)\n Passthrough argument given to virtualenv if True\n\n prompt : None (default)\n Passthrough argument given to virtualenv if not None\n\n symlinks : None\n Passthrough argument given to pyvenv if True\n\n upgrade : None\n Passthrough argument given to pyvenv if True\n\n user : None\n Set ownership for the virtualenv\n\n .. note::\n On Windows you must also pass a ``password`` parameter. Additionally,\n the user must have permissions to the location where the virtual\n environment is being created\n\n runas : None\n Set ownership for the virtualenv\n\n .. deprecated:: 2014.1.0\n ``user`` should be used instead\n\n use_vt : False\n Use VT terminal emulation (see output while installing)\n\n .. versionadded:: 2015.5.0\n\n saltenv : 'base'\n Specify a different environment. The default environment is ``base``.\n\n .. versionadded:: 2014.1.0\n\n .. note::\n The ``runas`` argument is deprecated as of 2014.1.0. ``user`` should be\n used instead.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virtualenv.create /path/to/new/virtualenv\n '''\n if venv_bin is None:\n # Beginning in 3.6, pyvenv has been deprecated\n # in favor of \"python3 -m venv\"\n if sys.version_info >= (3, 6):\n venv_bin = ['python3', '-m', 'venv']\n else:\n venv_bin = __pillar__.get('venv_bin') or __opts__.get('venv_bin')\n\n if not isinstance(venv_bin, list):\n cmd = [venv_bin]\n else:\n cmd = venv_bin\n\n if 'pyvenv' not in venv_bin:\n # ----- Stop the user if pyvenv only options are used --------------->\n # If any of the following values are not None, it means that the user\n # is actually passing a True or False value. Stop Him!\n if upgrade is not None:\n raise CommandExecutionError(\n 'The `upgrade`(`--upgrade`) option is not supported '\n 'by \\'{0}\\''.format(venv_bin)\n )\n elif symlinks is not None:\n raise CommandExecutionError(\n 'The `symlinks`(`--symlinks`) option is not supported '\n 'by \\'{0}\\''.format(venv_bin)\n )\n # <---- Stop the user if pyvenv only options are used ----------------\n\n # Virtualenv package\n try:\n import virtualenv\n version = getattr(virtualenv, '__version__',\n virtualenv.virtualenv_version)\n virtualenv_version_info = tuple(\n [int(i) for i in version.split('rc')[0].split('.')]\n )\n except ImportError:\n # Unable to import?? Let's parse the version from the console\n version_cmd = [venv_bin, '--version']\n ret = __salt__['cmd.run_all'](\n version_cmd, runas=user, python_shell=False, **kwargs\n )\n if ret['retcode'] > 0 or not ret['stdout'].strip():\n raise CommandExecutionError(\n 'Unable to get the virtualenv version output using \\'{0}\\'. '\n 'Returned data: {1}'.format(version_cmd, ret)\n )\n virtualenv_version_info = tuple(\n [int(i) for i in\n ret['stdout'].strip().split('rc')[0].split('.')]\n )\n\n if distribute:\n if virtualenv_version_info >= (1, 10):\n log.info(\n 'The virtualenv \\'--distribute\\' option has been '\n 'deprecated in virtualenv(>=1.10), as such, the '\n '\\'distribute\\' option to `virtualenv.create()` has '\n 'also been deprecated and it\\'s not necessary anymore.'\n )\n else:\n cmd.append('--distribute')\n\n if python is not None and python.strip() != '':\n if not salt.utils.path.which(python):\n raise CommandExecutionError(\n 'Cannot find requested python ({0}).'.format(python)\n )\n cmd.append('--python={0}'.format(python))\n if extra_search_dir is not None:\n if isinstance(extra_search_dir, string_types) and \\\n extra_search_dir.strip() != '':\n extra_search_dir = [\n e.strip() for e in extra_search_dir.split(',')\n ]\n for entry in extra_search_dir:\n cmd.append('--extra-search-dir={0}'.format(entry))\n if never_download is True:\n if (1, 10) <= virtualenv_version_info < (14, 0, 0):\n log.info(\n '--never-download was deprecated in 1.10.0, but reimplemented in 14.0.0. '\n 'If this feature is needed, please install a supported virtualenv version.'\n )\n else:\n cmd.append('--never-download')\n if prompt is not None and prompt.strip() != '':\n cmd.append('--prompt=\\'{0}\\''.format(prompt))\n else:\n # venv module from the Python >= 3.3 standard library\n\n # ----- Stop the user if virtualenv only options are being used ----->\n # If any of the following values are not None, it means that the user\n # is actually passing a True or False value. Stop Him!\n if python is not None and python.strip() != '':\n raise CommandExecutionError(\n 'The `python`(`--python`) option is not supported '\n 'by \\'{0}\\''.format(venv_bin)\n )\n elif extra_search_dir is not None and extra_search_dir.strip() != '':\n raise CommandExecutionError(\n 'The `extra_search_dir`(`--extra-search-dir`) option is not '\n 'supported by \\'{0}\\''.format(venv_bin)\n )\n elif never_download is not None:\n raise CommandExecutionError(\n 'The `never_download`(`--never-download`) option is not '\n 'supported by \\'{0}\\''.format(venv_bin)\n )\n elif prompt is not None and prompt.strip() != '':\n raise CommandExecutionError(\n 'The `prompt`(`--prompt`) option is not supported '\n 'by \\'{0}\\''.format(venv_bin)\n )\n # <---- Stop the user if virtualenv only options are being used ------\n\n if upgrade is True:\n cmd.append('--upgrade')\n if symlinks is True:\n cmd.append('--symlinks')\n\n # Common options to virtualenv and pyvenv\n if clear is True:\n cmd.append('--clear')\n if system_site_packages is True:\n cmd.append('--system-site-packages')\n\n # Finally the virtualenv path\n cmd.append(path)\n\n # Let's create the virtualenv\n ret = __salt__['cmd.run_all'](cmd, runas=user, python_shell=False, **kwargs)\n if ret['retcode'] != 0:\n # Something went wrong. Let's bail out now!\n return ret\n\n # Check if distribute and pip are already installed\n if salt.utils.platform.is_windows():\n venv_python = os.path.join(path, 'Scripts', 'python.exe')\n venv_pip = os.path.join(path, 'Scripts', 'pip.exe')\n venv_setuptools = os.path.join(path, 'Scripts', 'easy_install.exe')\n else:\n venv_python = os.path.join(path, 'bin', 'python')\n venv_pip = os.path.join(path, 'bin', 'pip')\n venv_setuptools = os.path.join(path, 'bin', 'easy_install')\n\n # Install setuptools\n if (pip or distribute) and not os.path.exists(venv_setuptools):\n _install_script(\n 'https://bitbucket.org/pypa/setuptools/raw/default/ez_setup.py',\n path, venv_python, user, saltenv=saltenv, use_vt=use_vt\n )\n\n # clear up the distribute archive which gets downloaded\n for fpath in glob.glob(os.path.join(path, 'distribute-*.tar.gz*')):\n os.unlink(fpath)\n\n if ret['retcode'] != 0:\n # Something went wrong. Let's bail out now!\n return ret\n\n # Install pip\n if pip and not os.path.exists(venv_pip):\n _ret = _install_script(\n 'https://bootstrap.pypa.io/get-pip.py',\n path, venv_python, user, saltenv=saltenv, use_vt=use_vt\n )\n # Let's update the return dictionary with the details from the pip\n # installation\n ret.update(\n retcode=_ret['retcode'],\n stdout='{0}\\n{1}'.format(ret['stdout'], _ret['stdout']).strip(),\n stderr='{0}\\n{1}'.format(ret['stderr'], _ret['stderr']).strip(),\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the path to the site - packages directory of a virtualenv", "response": "def get_site_packages(venv):\n '''\n Return the path to the site-packages directory of a virtualenv\n\n venv\n Path to the virtualenv.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virtualenv.get_site_packages /path/to/my/venv\n '''\n bin_path = _verify_virtualenv(venv)\n\n ret = __salt__['cmd.exec_code_all'](\n bin_path,\n 'from distutils import sysconfig; '\n 'print(sysconfig.get_python_lib())'\n )\n\n if ret['retcode'] != 0:\n raise CommandExecutionError('{stdout}\\n{stderr}'.format(**ret))\n\n return ret['stdout']"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the path to a distribution installed inside a virtualenv", "response": "def get_distribution_path(venv, distribution):\n '''\n Return the path to a distribution installed inside a virtualenv\n\n .. versionadded:: 2016.3.0\n\n venv\n Path to the virtualenv.\n distribution\n Name of the distribution. Note, all non-alphanumeric characters\n will be converted to dashes.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virtualenv.get_distribution_path /path/to/my/venv my_distribution\n '''\n _verify_safe_py_code(distribution)\n bin_path = _verify_virtualenv(venv)\n\n ret = __salt__['cmd.exec_code_all'](\n bin_path,\n 'import pkg_resources; '\n \"print(pkg_resources.get_distribution('{0}').location)\".format(\n distribution\n )\n )\n\n if ret['retcode'] != 0:\n raise CommandExecutionError('{stdout}\\n{stderr}'.format(**ret))\n\n return ret['stdout']"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the path to a resource installed inside a virtualenv", "response": "def get_resource_path(venv,\n package=None,\n resource=None):\n '''\n Return the path to a package resource installed inside a virtualenv\n\n .. versionadded:: 2015.5.0\n\n venv\n Path to the virtualenv\n\n package\n Name of the package in which the resource resides\n\n .. versionadded:: 2016.3.0\n\n resource\n Name of the resource of which the path is to be returned\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virtualenv.get_resource_path /path/to/my/venv my_package my/resource.xml\n '''\n _verify_safe_py_code(package, resource)\n bin_path = _verify_virtualenv(venv)\n\n ret = __salt__['cmd.exec_code_all'](\n bin_path,\n 'import pkg_resources; '\n \"print(pkg_resources.resource_filename('{0}', '{1}'))\".format(\n package,\n resource\n )\n )\n\n if ret['retcode'] != 0:\n raise CommandExecutionError('{stdout}\\n{stderr}'.format(**ret))\n\n return ret['stdout']"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a list of the VMs that are connected to the cloud provider.", "response": "def list_nodes(call=None):\n '''\n Return a list of the VMs\n\n id (str)\n image (str)\n size (str)\n state (str)\n private_ips (list)\n public_ips (list)\n\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes function must be called '\n 'with -f or --function.'\n )\n\n providers = __opts__.get('providers', {})\n\n ret = {}\n providers_to_check = [_f for _f in [cfg.get('libvirt') for cfg in six.itervalues(providers)] if _f]\n for provider in providers_to_check:\n conn = __get_conn(provider['url'])\n domains = conn.listAllDomains()\n for domain in domains:\n data = {\n 'id': domain.UUIDString(),\n 'image': '',\n 'size': '',\n 'state': VIRT_STATE_NAME_MAP[domain.state()[0]],\n 'private_ips': [],\n 'public_ips': get_domain_ips(domain, libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE)}\n # TODO: Annoyingly name is not guaranteed to be unique, but the id will not work in other places\n ret[domain.name()] = data\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_nodes_select(call=None):\n '''\n Return a list of the VMs that are on the provider, with select fields\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes_select function must be called '\n 'with -f or --function.'\n )\n\n selection = __opts__.get('query.selection')\n\n if not selection:\n raise SaltCloudSystemExit(\n 'query.selection not found in /etc/salt/cloud'\n )\n\n # TODO: somewhat doubt the implementation of cloud.list_nodes_select\n return salt.utils.cloud.list_nodes_select(\n list_nodes_full(), selection, call,\n )", "response": "Return a list of the VMs that are on the provider with select fields\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a single machine with the specified parameters.", "response": "def create(vm_):\n '''\n Provision a single machine\n '''\n clone_strategy = vm_.get('clone_strategy') or 'full'\n\n if clone_strategy not in ('quick', 'full'):\n raise SaltCloudSystemExit(\"'clone_strategy' must be one of quick or full. Got '{0}'\".format(clone_strategy))\n\n ip_source = vm_.get('ip_source') or 'ip-learning'\n\n if ip_source not in ('ip-learning', 'qemu-agent'):\n raise SaltCloudSystemExit(\"'ip_source' must be one of qemu-agent or ip-learning. Got '{0}'\".format(ip_source))\n\n validate_xml = vm_.get('validate_xml') if vm_.get('validate_xml') is not None else True\n\n log.info(\"Cloning '%s' with strategy '%s' validate_xml='%s'\", vm_['name'], clone_strategy, validate_xml)\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 'libvirt',\n vm_['profile']) is False:\n return False\n except AttributeError:\n pass\n\n # TODO: check name qemu/libvirt will choke on some characters (like '/')?\n name = vm_['name']\n\n __utils__['cloud.fire_event'](\n 'event',\n 'starting create',\n 'salt/cloud/{0}/creating'.format(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 key_filename = config.get_cloud_config_value(\n 'private_key', 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 key_filename \\'{0}\\' does not exist'.format(\n key_filename\n )\n )\n vm_['key_filename'] = key_filename\n # wait_for_instance requires private_key\n vm_['private_key'] = key_filename\n\n cleanup = []\n try:\n # clone the vm\n base = vm_['base_domain']\n conn = __get_conn(vm_['url'])\n\n try:\n # for idempotency the salt-bootstrap needs -F argument\n # script_args: -F\n clone_domain = conn.lookupByName(name)\n except libvirtError as e:\n domain = conn.lookupByName(base)\n # TODO: ensure base is shut down before cloning\n xml = domain.XMLDesc(0)\n\n kwargs = {\n 'name': name,\n 'base_domain': base,\n }\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(name),\n args={\n 'kwargs': __utils__['cloud.filter_event']('requesting', kwargs, list(kwargs)),\n },\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n log.debug(\"Source machine XML '%s'\", xml)\n\n domain_xml = ElementTree.fromstring(xml)\n domain_xml.find('./name').text = name\n if domain_xml.find('./description') is None:\n description_elem = ElementTree.Element('description')\n domain_xml.insert(0, description_elem)\n description = domain_xml.find('./description')\n description.text = \"Cloned from {0}\".format(base)\n domain_xml.remove(domain_xml.find('./uuid'))\n\n for iface_xml in domain_xml.findall('./devices/interface'):\n iface_xml.remove(iface_xml.find('./mac'))\n # enable IP learning, this might be a default behaviour...\n # Don't always enable since it can cause problems through libvirt-4.5\n if ip_source == 'ip-learning' and iface_xml.find(\"./filterref/parameter[@name='CTRL_IP_LEARNING']\") is None:\n iface_xml.append(ElementTree.fromstring(IP_LEARNING_XML))\n\n # If a qemu agent is defined we need to fix the path to its socket\n # \n # \n # \n #
\n # \n for agent_xml in domain_xml.findall(\"\"\"./devices/channel[@type='unix']\"\"\"):\n # is org.qemu.guest_agent.0 an option?\n if agent_xml.find(\"\"\"./target[@type='virtio'][@name='org.qemu.guest_agent.0']\"\"\") is not None:\n source_element = agent_xml.find(\"\"\"./source[@mode='bind']\"\"\")\n # see if there is a path element that needs rewriting\n if source_element and 'path' in source_element.attrib:\n path = source_element.attrib['path']\n new_path = path.replace('/domain-{0}/'.format(base), '/domain-{0}/'.format(name))\n log.debug(\"Rewriting agent socket path to %s\", new_path)\n source_element.attrib['path'] = new_path\n\n for disk in domain_xml.findall(\"\"\"./devices/disk[@device='disk'][@type='file']\"\"\"):\n # print \"Disk: \", ElementTree.tostring(disk)\n # check if we can clone\n driver = disk.find(\"./driver[@name='qemu']\")\n if driver is None:\n # Err on the safe side\n raise SaltCloudExecutionFailure(\"Non qemu driver disk encountered bailing out.\")\n disk_type = driver.attrib.get('type')\n log.info(\"disk attributes %s\", disk.attrib)\n if disk_type == 'qcow2':\n source = disk.find(\"./source\").attrib['file']\n pool, volume = find_pool_and_volume(conn, source)\n if clone_strategy == 'quick':\n new_volume = pool.createXML(create_volume_with_backing_store_xml(volume), 0)\n else:\n new_volume = pool.createXMLFrom(create_volume_xml(volume), volume, 0)\n cleanup.append({'what': 'volume', 'item': new_volume})\n\n disk.find(\"./source\").attrib['file'] = new_volume.path()\n elif disk_type == 'raw':\n source = disk.find(\"./source\").attrib['file']\n pool, volume = find_pool_and_volume(conn, source)\n # TODO: more control on the cloned disk type\n new_volume = pool.createXMLFrom(create_volume_xml(volume), volume, 0)\n cleanup.append({'what': 'volume', 'item': new_volume})\n\n disk.find(\"./source\").attrib['file'] = new_volume.path()\n else:\n raise SaltCloudExecutionFailure(\"Disk type '{0}' not supported\".format(disk_type))\n\n clone_xml = salt.utils.stringutils.to_str(ElementTree.tostring(domain_xml))\n log.debug(\"Clone XML '%s'\", clone_xml)\n\n validate_flags = libvirt.VIR_DOMAIN_DEFINE_VALIDATE if validate_xml else 0\n clone_domain = conn.defineXMLFlags(clone_xml, validate_flags)\n\n cleanup.append({'what': 'domain', 'item': clone_domain})\n clone_domain.createWithFlags(libvirt.VIR_DOMAIN_START_FORCE_BOOT)\n\n log.debug(\"VM '%s'\", vm_)\n\n if ip_source == 'qemu-agent':\n ip_source = libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT\n elif ip_source == 'ip-learning':\n ip_source = libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE\n\n address = salt.utils.cloud.wait_for_ip(\n get_domain_ip,\n update_args=(clone_domain, 0, ip_source),\n timeout=config.get_cloud_config_value('wait_for_ip_timeout', vm_, __opts__, default=10 * 60),\n interval=config.get_cloud_config_value('wait_for_ip_interval', vm_, __opts__, default=10),\n interval_multiplier=config.get_cloud_config_value('wait_for_ip_interval_multiplier', vm_, __opts__, default=1),\n )\n\n log.info('Address = %s', address)\n\n vm_['ssh_host'] = address\n\n # the bootstrap script needs to be installed first in /etc/salt/cloud.deploy.d/\n # salt-cloud -u is your friend\n ret = __utils__['cloud.bootstrap'](vm_, __opts__)\n\n __utils__['cloud.fire_event'](\n 'event',\n 'created instance',\n 'salt/cloud/{0}/created'.format(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\n except Exception as e: # pylint: disable=broad-except\n do_cleanup(cleanup)\n # throw the root cause after cleanup\n raise e"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncleans up clone domain leftovers as much as possible.", "response": "def do_cleanup(cleanup):\n '''\n Clean up clone domain leftovers as much as possible.\n\n Extra robust clean up in order to deal with some small changes in libvirt\n behavior over time. Passed in volumes and domains are deleted, any errors\n are ignored. Used when cloning/provisioning a domain fails.\n\n :param cleanup: list containing dictonaries with two keys: 'what' and 'item'.\n If 'what' is domain the 'item' is a libvirt domain object.\n If 'what' is volume then the item is a libvirt volume object.\n\n Returns:\n none\n\n .. versionadded: 2017.7.3\n '''\n log.info('Cleaning up after exception')\n for leftover in cleanup:\n what = leftover['what']\n item = leftover['item']\n if what == 'domain':\n log.info('Cleaning up %s %s', what, item.name())\n try:\n item.destroy()\n log.debug('%s %s forced off', what, item.name())\n except libvirtError:\n pass\n try:\n item.undefineFlags(libvirt.VIR_DOMAIN_UNDEFINE_MANAGED_SAVE+\n libvirt.VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA+\n libvirt.VIR_DOMAIN_UNDEFINE_NVRAM)\n log.debug('%s %s undefined', what, item.name())\n except libvirtError:\n pass\n if what == 'volume':\n try:\n item.delete()\n log.debug('%s %s cleaned up', what, item.name())\n except libvirtError:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndestroy a virtual machine on the cloud provider.", "response": "def destroy(name, call=None):\n \"\"\"\n This function irreversibly destroys a virtual machine on the cloud provider.\n Before doing so, it should fire an event on the Salt event bus.\n\n The tag for this event is `salt/cloud//destroying`.\n Once the virtual machine has been destroyed, another event is fired.\n The tag for that event is `salt/cloud//destroyed`.\n\n Dependencies:\n list_nodes\n\n @param name:\n @type name: str\n @param call:\n @type call:\n @return: True if all went well, otherwise an error message\n @rtype: bool|str\n \"\"\"\n log.info(\"Attempting to delete instance %s\", name)\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 found = []\n\n providers = __opts__.get('providers', {})\n providers_to_check = [_f for _f in [cfg.get('libvirt') for cfg in six.itervalues(providers)] if _f]\n for provider in providers_to_check:\n conn = __get_conn(provider['url'])\n log.info(\"looking at %s\", provider['url'])\n try:\n domain = conn.lookupByName(name)\n found.append({'domain': domain, 'conn': conn})\n except libvirtError:\n pass\n\n if not found:\n return \"{0} doesn't exist and can't be deleted\".format(name)\n\n if len(found) > 1:\n return \"{0} doesn't identify a unique machine leaving things\".format(name)\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 destroy_domain(found[0]['conn'], found[0]['domain'])\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 )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef output(data, **kwargs): # pylint: disable=unused-argument\n '''\n Print out via pretty print\n '''\n if isinstance(data, Exception):\n data = six.text_type(data)\n if 'output_indent' in __opts__ and __opts__['output_indent'] >= 0:\n return pprint.pformat(data, indent=__opts__['output_indent'])\n return pprint.pformat(data)", "response": "Print out via pretty print\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the VM s size object", "response": "def get_size(vm_):\n '''\n Return the VM's size object\n '''\n vm_size = config.get_cloud_config_value(\n 'fixed_instance_size', vm_, __opts__, default=None,\n search_global=False\n )\n sizes = avail_sizes()\n\n if not vm_size:\n size = next((item for item in sizes if item['name'] == 'S'), None)\n return size\n\n size = next((item for item in sizes if item['name'] == vm_size or item['id'] == vm_size), None)\n if size:\n return size\n\n raise SaltCloudNotFound(\n 'The specified size, \\'{0}\\', could not be found.'.format(vm_size)\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_image(vm_):\n '''\n Return the image object to use\n '''\n vm_image = config.get_cloud_config_value('image', vm_, __opts__).encode(\n 'ascii', 'salt-cloud-force-ascii'\n )\n\n images = avail_images()\n for key, value in six.iteritems(images):\n if vm_image and vm_image in (images[key]['id'], images[key]['name']):\n return images[key]\n\n raise SaltCloudNotFound(\n 'The specified image, \\'{0}\\', could not be found.'.format(vm_image)\n )", "response": "Return the image object to use"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a dict of available locations for 1 & 1", "response": "def avail_locations(conn=None, call=None):\n '''\n List available locations/datacenters for 1&1\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 datacenters = []\n\n if not conn:\n conn = get_conn()\n\n for datacenter in conn.list_datacenters():\n datacenters.append({datacenter['country_code']: datacenter})\n\n return {'Locations': datacenters}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_block_storage(kwargs=None, call=None):\n '''\n Create a block storage\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The create_block_storage function must be called with '\n '-f or --function'\n )\n\n conn = get_conn()\n\n # Assemble the composite block storage object.\n block_storage = _get_block_storage(kwargs)\n\n data = conn.create_block_storage(block_storage=block_storage)\n\n return {'BlockStorage': data}", "response": "Create a composite block storage object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconstructing a block storage instance from passed arguments", "response": "def _get_block_storage(kwargs):\n '''\n Construct a block storage instance from passed arguments\n '''\n if kwargs is None:\n kwargs = {}\n\n block_storage_name = kwargs.get('name', None)\n block_storage_size = kwargs.get('size', None)\n block_storage_description = kwargs.get('description', None)\n datacenter_id = kwargs.get('datacenter_id', None)\n server_id = kwargs.get('server_id', None)\n\n block_storage = BlockStorage(\n name=block_storage_name,\n size=block_storage_size)\n\n if block_storage_description:\n block_storage.description = block_storage_description\n\n if datacenter_id:\n block_storage.datacenter_id = datacenter_id\n\n if server_id:\n block_storage.server_id = server_id\n\n return block_storage"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_ssh_key(kwargs):\n '''\n Construct an SshKey instance from passed arguments\n '''\n ssh_key_name = kwargs.get('name', None)\n ssh_key_description = kwargs.get('description', None)\n public_key = kwargs.get('public_key', None)\n\n return SshKey(\n name=ssh_key_name,\n description=ssh_key_description,\n public_key=public_key\n )", "response": "Construct an SshKey instance from passed arguments\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_ssh_key(kwargs=None, call=None):\n '''\n Create an ssh key\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The create_ssh_key function must be called with '\n '-f or --function'\n )\n\n conn = get_conn()\n\n # Assemble the composite SshKey object.\n ssh_key = _get_ssh_key(kwargs)\n\n data = conn.create_ssh_key(ssh_key=ssh_key)\n\n return {'SshKey': data}", "response": "Create an ssh key in the Azure VMware environment"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_firewall_policy(kwargs):\n '''\n Construct FirewallPolicy and FirewallPolicy instances from passed arguments\n '''\n fp_name = kwargs.get('name', None)\n fp_description = kwargs.get('description', None)\n firewallPolicy = FirewallPolicy(\n name=fp_name,\n description=fp_description\n )\n\n fpr_json = kwargs.get('rules', None)\n jdata = json.loads(fpr_json)\n rules = []\n for fwpr in jdata:\n firewallPolicyRule = FirewallPolicyRule()\n if 'protocol' in fwpr:\n firewallPolicyRule.rule_set['protocol'] = fwpr['protocol']\n if 'port_from' in fwpr:\n firewallPolicyRule.rule_set['port_from'] = fwpr['port_from']\n if 'port_to' in fwpr:\n firewallPolicyRule.rule_set['port_to'] = fwpr['port_to']\n if 'source' in fwpr:\n firewallPolicyRule.rule_set['source'] = fwpr['source']\n if 'action' in fwpr:\n firewallPolicyRule.rule_set['action'] = fwpr['action']\n if 'description' in fwpr:\n firewallPolicyRule.rule_set['description'] = fwpr['description']\n if 'port' in fwpr:\n firewallPolicyRule.rule_set['port'] = fwpr['port']\n rules.append(firewallPolicyRule)\n\n return {'firewall_policy': firewallPolicy, 'firewall_policy_rules': rules}", "response": "Construct FirewallPolicy and FirewallPolicy instances from passed arguments\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a firewall policy in Azure Cloud", "response": "def create_firewall_policy(kwargs=None, call=None):\n '''\n Create a firewall policy\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The create_firewall_policy function must be called with '\n '-f or --function'\n )\n\n conn = get_conn()\n\n # Assemble the composite FirewallPolicy and FirewallPolicyRule[] objects.\n getFwpResult = _get_firewall_policy(kwargs)\n\n data = conn.create_firewall_policy(\n firewall_policy=getFwpResult['firewall_policy'],\n firewall_policy_rules=getFwpResult['firewall_policy_rules']\n )\n\n return {'FirewallPolicy': data}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a list of the server appliances that are on the provider", "response": "def avail_images(conn=None, call=None):\n '''\n Return a list of the server appliances that are on the provider\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_images function must be called with '\n '-f or --function, or with the --list-images option'\n )\n\n if not conn:\n conn = get_conn()\n\n ret = {}\n\n for appliance in conn.list_appliances():\n ret[appliance['name']] = appliance\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef avail_baremetal_images(conn=None, call=None):\n '''\n Return a list of the baremetal server appliances that are on the provider\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_baremetal_images function must be called with '\n '-f or --function'\n )\n\n if not conn:\n conn = get_conn()\n\n ret = {}\n\n for appliance in conn.list_appliances(q='BAREMETAL'):\n ret[appliance['name']] = appliance\n\n return ret", "response": "Return a list of the baremetal server appliances that are on the provider"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a dict of all available VM sizes on the cloud provider with avail_sizes relevant data.", "response": "def avail_sizes(call=None):\n '''\n Return a dict of all available VM sizes on the cloud provider with\n relevant data.\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_sizes function must be called with '\n '-f or --function, or with the --list-sizes option'\n )\n\n conn = get_conn()\n\n sizes = conn.fixed_server_flavors()\n\n return sizes"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef baremetal_models(call=None):\n '''\n Return a dict of all available baremetal models with relevant data.\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The baremetal_models function must be called with '\n '-f or --function'\n )\n\n conn = get_conn()\n\n bmodels = conn.list_baremetal_models()\n\n return bmodels", "response": "Return a dict of all available baremetal models with relevant data."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a list of VMs that are on the provider", "response": "def list_nodes(conn=None, call=None):\n '''\n Return a list of VMs that are on the provider\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n ret = {}\n nodes = conn.list_servers()\n\n for node in nodes:\n public_ips = []\n private_ips = []\n ret = {}\n\n size = node.get('hardware').get('fixed_instance_size_id', 'Custom size')\n\n if node.get('private_networks'):\n for private_ip in node['private_networks']:\n private_ips.append(private_ip)\n\n if node.get('ips'):\n for public_ip in node['ips']:\n public_ips.append(public_ip['ip'])\n\n server = {\n 'id': node['id'],\n 'image': node['image']['id'],\n 'size': size,\n 'state': node['status']['state'],\n 'private_ips': private_ips,\n 'public_ips': public_ips\n }\n ret[node['name']] = server\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a list of the VMs that are on the provider with all fields available", "response": "def list_nodes_full(conn=None, call=None):\n '''\n Return a list of the VMs that are on the provider, with all fields\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes_full function must be called with -f or '\n '--function.'\n )\n\n if not conn:\n conn = get_conn()\n\n ret = {}\n nodes = conn.list_servers()\n\n for node in nodes:\n ret[node['name']] = node\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a server instance from the cloud profile config", "response": "def _get_server(vm_):\n '''\n Construct server instance from cloud profile config\n '''\n description = config.get_cloud_config_value(\n 'description', vm_, __opts__, default=None,\n search_global=False\n )\n\n ssh_key = load_public_key(vm_)\n\n server_type = config.get_cloud_config_value(\n 'server_type', vm_, __opts__, default='cloud',\n search_global=False\n )\n vcore = None\n cores_per_processor = None\n ram = None\n fixed_instance_size_id = None\n baremetal_model_id = None\n\n if 'fixed_instance_size' in vm_:\n fixed_instance_size = get_size(vm_)\n fixed_instance_size_id = fixed_instance_size['id']\n elif 'vm_core' in vm_ and 'cores_per_processor' in vm_ and 'ram' in vm_ and 'hdds' in vm_:\n vcore = config.get_cloud_config_value(\n 'vcore', vm_, __opts__, default=None,\n search_global=False\n )\n cores_per_processor = config.get_cloud_config_value(\n 'cores_per_processor', vm_, __opts__, default=None,\n search_global=False\n )\n ram = config.get_cloud_config_value(\n 'ram', vm_, __opts__, default=None,\n search_global=False\n )\n elif 'baremetal_model_id' in vm_ and server_type == 'baremetal':\n baremetal_model_id = config.get_cloud_config_value(\n 'baremetal_model_id', vm_, __opts__, default=None,\n search_global=False\n )\n else:\n raise SaltCloudConfigError(\"'fixed_instance_size' or 'vcore', \"\n \"'cores_per_processor', 'ram', and 'hdds' \"\n \"must be provided for 'cloud' server. \"\n \"For 'baremetal' server, 'baremetal_model_id'\"\n \"must be provided.\")\n\n appliance_id = config.get_cloud_config_value(\n 'appliance_id', vm_, __opts__, default=None,\n search_global=False\n )\n\n password = config.get_cloud_config_value(\n 'password', vm_, __opts__, default=None,\n search_global=False\n )\n\n firewall_policy_id = config.get_cloud_config_value(\n 'firewall_policy_id', vm_, __opts__, default=None,\n search_global=False\n )\n\n ip_id = config.get_cloud_config_value(\n 'ip_id', vm_, __opts__, default=None,\n search_global=False\n )\n\n load_balancer_id = config.get_cloud_config_value(\n 'load_balancer_id', vm_, __opts__, default=None,\n search_global=False\n )\n\n monitoring_policy_id = config.get_cloud_config_value(\n 'monitoring_policy_id', vm_, __opts__, default=None,\n search_global=False\n )\n\n datacenter_id = config.get_cloud_config_value(\n 'datacenter_id', vm_, __opts__, default=None,\n search_global=False\n )\n\n private_network_id = config.get_cloud_config_value(\n 'private_network_id', vm_, __opts__, default=None,\n search_global=False\n )\n\n power_on = config.get_cloud_config_value(\n 'power_on', vm_, __opts__, default=True,\n search_global=False\n )\n\n public_key = config.get_cloud_config_value(\n 'public_key_ids', vm_, __opts__, default=None,\n search_global=False\n )\n\n # Contruct server object\n return Server(\n name=vm_['name'],\n description=description,\n fixed_instance_size_id=fixed_instance_size_id,\n vcore=vcore,\n cores_per_processor=cores_per_processor,\n ram=ram,\n appliance_id=appliance_id,\n password=password,\n power_on=power_on,\n firewall_policy_id=firewall_policy_id,\n ip_id=ip_id,\n load_balancer_id=load_balancer_id,\n monitoring_policy_id=monitoring_policy_id,\n datacenter_id=datacenter_id,\n rsa_key=ssh_key,\n private_network_id=private_network_id,\n public_key=public_key,\n server_type=server_type,\n baremetal_model_id=baremetal_model_id\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_hdds(vm_):\n '''\n Construct VM hdds from cloud profile config\n '''\n _hdds = config.get_cloud_config_value(\n 'hdds', vm_, __opts__, default=None,\n search_global=False\n )\n\n hdds = []\n\n for hdd in _hdds:\n hdds.append(\n Hdd(\n size=hdd['size'],\n is_main=hdd['is_main']\n )\n )\n\n return hdds", "response": "Return a list of VM hdds from cloud profile config"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\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\n config.is_profile_configured(__opts__,\n (__active_provider_name__ or\n 'oneandone'),\n vm_['profile']) is False):\n return False\n except AttributeError:\n pass\n\n data = None\n conn = get_conn()\n hdds = []\n\n # Assemble the composite server object.\n server = _get_server(vm_)\n\n if not bool(server.specs['hardware']['fixed_instance_size_id'])\\\n and not bool(server.specs['server_type'] == 'baremetal'):\n # Assemble the hdds object.\n hdds = _get_hdds(vm_)\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(vm_['name']),\n args={'name': vm_['name']},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n try:\n data = conn.create_server(server=server, hdds=hdds)\n\n _wait_for_completion(conn,\n get_wait_timeout(vm_),\n data['id'])\n except Exception as exc: # pylint: disable=W0703\n log.error(\n 'Error creating %s on 1and1\\n\\n'\n 'The following exception was thrown by the 1and1 library '\n 'when trying to run the initial deployment: \\n%s',\n vm_['name'], exc, exc_info_on_loglevel=logging.DEBUG\n )\n return False\n\n vm_['server_id'] = data['id']\n password = data['first_password']\n\n def __query_node_data(vm_, data):\n '''\n Query node data until node becomes available.\n '''\n running = False\n try:\n data = show_instance(vm_['name'], 'action')\n if not data:\n return False\n log.debug(\n 'Loaded node data for %s:\\nname: %s\\nstate: %s',\n vm_['name'],\n pprint.pformat(data['name']),\n data['status']['state']\n )\n except Exception as err:\n log.error(\n 'Failed to get nodes list: %s', err,\n # Show the trackback 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\n running = data['status']['state'].lower() == 'powered_on'\n if not running:\n # Still not running, trigger another iteration\n return\n\n vm_['ssh_host'] = data['ips'][0]['ip']\n\n return data\n\n try:\n data = salt.utils.cloud.wait_for_ip(\n __query_node_data,\n update_args=(vm_, data),\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.message))\n\n log.debug('VM is now running')\n log.info('Created Cloud VM %s', vm_)\n log.debug('%s VM creation details:\\n%s', vm_, pprint.pformat(data))\n\n __utils__['cloud.fire_event'](\n 'event',\n 'created instance',\n 'salt/cloud/{0}/created'.format(vm_['name']),\n args={\n 'name': vm_['name'],\n 'profile': vm_['profile'],\n 'provider': vm_['driver'],\n },\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n if 'ssh_host' in vm_:\n vm_['password'] = password\n vm_['key_filename'] = get_key_filename(vm_)\n ret = __utils__['cloud.bootstrap'](vm_, __opts__)\n ret.update(data)\n return ret\n else:\n raise SaltCloudSystemExit('A valid IP address was not found.')", "response": "Create a single VM from a data dict."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndestroying a server by name", "response": "def destroy(name, call=None):\n '''\n destroy a server by name\n\n :param name: name given to the server\n :param call: call value in this case is 'action'\n :return: array of booleans , true if successfully stopped and true if\n successfully removed\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -d vm_name\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 __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 conn = get_conn()\n node = get_node(conn, name)\n\n conn.delete_server(server_id=node['id'])\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 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\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a node for the named VM", "response": "def get_node(conn, name):\n '''\n Return a node for the named VM\n '''\n for node in conn.list_servers(per_page=1000):\n if node['name'] == name:\n return node"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nload the public key file if exists.", "response": "def load_public_key(vm_):\n '''\n Load the public key file if exists.\n '''\n public_key_filename = config.get_cloud_config_value(\n 'ssh_public_key', vm_, __opts__, search_global=False, default=None\n )\n if public_key_filename is not None:\n public_key_filename = os.path.expanduser(public_key_filename)\n if not os.path.isfile(public_key_filename):\n raise SaltCloudConfigError(\n 'The defined ssh_public_key \\'{0}\\' does not exist'.format(\n public_key_filename\n )\n )\n\n with salt.utils.files.fopen(public_key_filename, 'r') as public_key:\n key = salt.utils.stringutils.to_unicode(public_key.read().replace('\\n', ''))\n\n return key"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nwaits until the server is provisioned.", "response": "def _wait_for_completion(conn, wait_timeout, server_id):\n '''\n Poll request status until resource is provisioned.\n '''\n wait_timeout = time.time() + wait_timeout\n while wait_timeout > time.time():\n time.sleep(5)\n\n server = conn.get_server(server_id)\n server_state = server['status']['state'].lower()\n\n if server_state == \"powered_on\":\n return\n elif server_state == 'failed':\n raise Exception('Server creation failed for {0}'.format(server_id))\n elif server_state in ('active',\n 'enabled',\n 'deploying',\n 'configuring'):\n continue\n else:\n raise Exception(\n 'Unknown server state {0}'.format(server_state))\n raise Exception(\n 'Timed out waiting for server create completion for {0}'.format(server_id)\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nformat the results of listing functions", "response": "def _load_result(response, ret):\n '''\n format the results of listing functions\n '''\n\n #were we able to connect?\n if response['code'] is None:\n ret['comment'] = response['content']\n #forbidden?\n elif response['code'] == 401:\n ret['comment'] = '401 Forbidden: Authentication required!'\n #Not found?\n elif response['code'] == 404:\n ret['comment'] = response['content']['message']\n #200?\n elif response['code'] == 200:\n ret['result'] = True\n ret['comment'] = 'Listing Current Configuration Only. ' \\\n 'Not action or changes occurred during the execution of this state.'\n ret['changes'] = response['content']\n #something bad\n else:\n ret['comment'] = response['content']['message']\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _strip_key(dictionary, keyword):\n '''\n look for a certain key within a dictionary and nullify ti's contents, check within nested\n dictionaries and lists as well. Certain attributes such as \"generation\" will change even\n when there were no changes made to the entity.\n '''\n\n for key, value in six.iteritems(dictionary):\n if key == keyword:\n dictionary[key] = None\n elif isinstance(value, dict):\n _strip_key(value, keyword)\n elif isinstance(value, list):\n for item in value:\n if isinstance(item, dict):\n _strip_key(item, keyword)\n\n return dictionary", "response": "Recursively strips a certain key from a nested dictionary and sets it to None if the key is not found."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck if the entity_type is enforced to the desired state.", "response": "def _check_for_changes(entity_type, ret, existing, modified):\n '''\n take an existing entity and a modified entity and check for changes.\n '''\n\n ret['result'] = True\n\n #were there any changes? generation always changes, remove it.\n\n if isinstance(existing, dict) and isinstance(modified, dict):\n if 'generation' in modified['content'].keys():\n del modified['content']['generation']\n\n if 'generation' in existing['content'].keys():\n del existing['content']['generation']\n\n if modified['content'] == existing['content']:\n ret['comment'] = '{entity_type} is currently enforced to the desired state. No changes made.'.format(entity_type=entity_type)\n else:\n ret['comment'] = '{entity_type} was enforced to the desired state. Note: Only parameters specified ' \\\n 'were enforced. See changes for details.'.format(entity_type=entity_type)\n ret['changes']['old'] = existing['content']\n ret['changes']['new'] = modified['content']\n\n else:\n if modified == existing:\n ret['comment'] = '{entity_type} is currently enforced to the desired state. No changes made.'.format(entity_type=entity_type)\n else:\n ret['comment'] = '{entity_type} was enforced to the desired state. Note: Only parameters specified ' \\\n 'were enforced. See changes for details.'.format(entity_type=entity_type)\n ret['changes']['old'] = existing\n ret['changes']['new'] = modified\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a new node in the node list if it does not already exist.", "response": "def create_node(hostname, username, password, name, address):\n '''\n Create a new node if it does not already exist.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the node to create\n address\n The address of the node\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'create', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'address': address\n }\n )\n\n #is this node currently configured?\n existing = __salt__['bigip.list_node'](hostname, username, password, name)\n\n # if it exists\n if existing['code'] == 200:\n\n ret['result'] = True\n ret['comment'] = 'A node by this name currently exists. No change made.'\n\n # if it doesn't exist\n elif existing['code'] == 404:\n response = __salt__['bigip.create_node'](hostname, username, password, name, address)\n\n ret['result'] = True\n ret['changes']['old'] = {}\n ret['changes']['new'] = response['content']\n ret['comment'] = 'Node was successfully created.'\n\n # else something else was returned\n else:\n ret = _load_result(existing, ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nmanage a node of a given bigip device.", "response": "def manage_node(hostname, username, password, name, address,\n connection_limit=None,\n description=None,\n dynamic_ratio=None,\n logging=None,\n monitor=None,\n rate_limit=None,\n ratio=None,\n session=None,\n node_state=None):\n '''\n Manages a node of a given bigip device. If the node does not exist it will be created, otherwise,\n only the properties which are different than the existing will be updated.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the node to manage.\n address\n The address of the node\n connection_limit\n [integer]\n description\n [string]\n dynam\n c_ratio: [integer]\n logging\n [enabled | disabled]\n monitor\n [[name] | none | default]\n rate_limit\n [integer]\n ratio\n [integer]\n session\n [user-enabled | user-disabled]\n node_state (state)\n [user-down | user-up ]\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'manage', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'address': address,\n 'connection_limit': connection_limit,\n 'description': description,\n 'dynamic_ratio': dynamic_ratio,\n 'logging': logging,\n 'monitor': monitor,\n 'rate_limit': rate_limit,\n 'ratio': ratio,\n 'session': session,\n 'state:': node_state\n }\n )\n\n #is this node currently configured?\n existing = __salt__['bigip.list_node'](hostname, username, password, name)\n\n # if it exists by name\n if existing['code'] == 200:\n\n # ensure the address is the same, we don't want to modify a different node than what\n # we think we are managing\n if existing['content']['address'] != address:\n ret['result'] = False\n ret['comment'] = 'A node with this name exists but the address does not match.'\n\n modified = __salt__['bigip.modify_node'](hostname=hostname,\n username=username,\n password=password,\n name=name,\n connection_limit=connection_limit,\n description=description,\n dynamic_ratio=dynamic_ratio,\n logging=logging,\n monitor=monitor,\n rate_limit=rate_limit,\n ratio=ratio,\n session=session,\n state=node_state)\n\n #was the modification successful?\n if modified['code'] == 200:\n ret = _check_for_changes('Node', ret, existing, modified)\n else:\n ret = _load_result(modified, ret)\n\n # not found, attempt to create it\n elif existing['code'] == 404:\n\n new = __salt__['bigip.create_node'](hostname, username, password, name, address)\n\n # were we able to create it?\n if new['code'] == 200:\n # try modification\n\n modified = __salt__['bigip.modify_node'](hostname=hostname,\n username=username,\n password=password,\n name=name,\n connection_limit=connection_limit,\n description=description,\n dynamic_ratio=dynamic_ratio,\n logging=logging,\n monitor=monitor,\n rate_limit=rate_limit,\n ratio=ratio,\n session=session,\n state=node_state)\n #was the modification successful?\n if modified['code'] == 200:\n\n ret['result'] = True\n ret['comment'] = 'Node was created and enforced to the desired state. Note: Only parameters specified ' \\\n 'were enforced. See changes for details.'\n ret['changes']['old'] = {}\n ret['changes']['new'] = modified['content']\n\n # roll it back\n else:\n\n deleted = __salt__['bigip.delete_node'](hostname, username, password, name)\n # did we get rid of it?\n if deleted['code'] == 200:\n ret['comment'] = 'Node was successfully created but an error occurred during modification. ' \\\n 'The creation of the node has been rolled back. Message is as follows:\\n' \\\n '{message}'.format(message=modified['content']['message'])\n # something bad happened\n else:\n ret['comment'] = 'Node was successfully created but an error occurred during modification. ' \\\n 'The creation of the node was not able to be rolled back. Message is as follows:' \\\n '\\n {message}\\n{message_two}'.format(message=modified['content']['message'],\n message_two=deleted['content']['message'])\n\n # unable to create it\n else:\n ret = _load_result(new, ret)\n # an error occurred\n else:\n ret = _load_result(existing, ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmodify an existing node.", "response": "def modify_node(hostname, username, password, name,\n connection_limit=None,\n description=None,\n dynamic_ratio=None,\n logging=None,\n monitor=None,\n rate_limit=None,\n ratio=None,\n session=None,\n node_state=None):\n '''\n Modify an existing node. Only a node which already exists will be modified and\n only the parameters specified will be enforced.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the node to modify\n connection_limit\n [integer]\n description\n [string]\n dynamic_ratio\n [integer]\n logging\n [enabled | disabled]\n monitor\n [[name] | none | default]\n rate_limit\n [integer]\n ratio\n [integer]\n session\n [user-enabled | user-disabled]\n node_state (state)\n [user-down | user-up ]\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'modify', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'connection_limit': connection_limit,\n 'description': description,\n 'dynamic_ratio': dynamic_ratio,\n 'logging': logging,\n 'monitor': monitor,\n 'rate_limit': rate_limit,\n 'ratio': ratio,\n 'session': session,\n 'state:': node_state\n }\n )\n\n #is this node currently configured?\n existing = __salt__['bigip.list_node'](hostname, username, password, name)\n\n # if it exists by name\n if existing['code'] == 200:\n\n modified = __salt__['bigip.modify_node'](hostname=hostname,\n username=username,\n password=password,\n name=name,\n connection_limit=connection_limit,\n description=description,\n dynamic_ratio=dynamic_ratio,\n logging=logging,\n monitor=monitor,\n rate_limit=rate_limit,\n ratio=ratio,\n session=session,\n state=node_state)\n\n #was the modification successful?\n if modified['code'] == 200:\n ret = _check_for_changes('Node', ret, existing, modified)\n else:\n ret = _load_result(modified, ret)\n\n # not found, attempt to create it\n elif existing['code'] == 404:\n ret['comment'] = 'A node with this name was not found.'\n # an error occurred\n else:\n ret = _load_result(existing, ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_pool(hostname, username, password, name, members=None,\n allow_nat=None,\n allow_snat=None,\n description=None,\n gateway_failsafe_device=None,\n ignore_persisted_weight=None,\n ip_tos_to_client=None,\n ip_tos_to_server=None,\n link_qos_to_client=None,\n link_qos_to_server=None,\n load_balancing_mode=None,\n min_active_members=None,\n min_up_members=None,\n min_up_members_action=None,\n min_up_members_checking=None,\n monitor=None,\n profiles=None,\n queue_depth_limit=None,\n queue_on_connection_limit=None,\n queue_time_limit=None,\n reselect_tries=None,\n service_down_action=None,\n slow_ramp_time=None):\n '''\n Create a new node if it does not already exist.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the pool to create\n members\n List of members to be added to the pool\n allow_nat\n [yes | no]\n allow_snat\n [yes | no]\n description\n [string]\n gateway_failsafe_device\n [string]\n ignore_persisted_weight\n [enabled | disabled]\n ip_tos_to_client\n [pass-through | [integer]]\n ip_tos_to_server\n [pass-through | [integer]]\n link_qos_to_client\n [pass-through | [integer]]\n link_qos_to_server\n [pass-through | [integer]]\n load_balancing_mode\n [dynamic-ratio-member | dynamic-ratio-node |\n fastest-app-response | fastest-node |\n least-connections-members |\n least-connections-node |\n least-sessions |\n observed-member | observed-node |\n predictive-member | predictive-node |\n ratio-least-connections-member |\n ratio-least-connections-node |\n ratio-member | ratio-node | ratio-session |\n round-robin | weighted-least-connections-member |\n weighted-least-connections-node]\n min_active_members\n [integer]\n min_up_members\n [integer]\n min_up_members_action\n [failover | reboot | restart-all]\n min_up_members_checking\n [enabled | disabled]\n monitor\n [name]\n profiles\n [none | profile_name]\n queue_depth_limit\n [integer]\n queue_on_connection_limit\n [enabled | disabled]\n queue_time_limit\n [integer]\n reselect_tries\n [integer]\n service_down_action\n [drop | none | reselect | reset]\n slow_ramp_time\n [integer]\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'create', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'members': members,\n 'allow_nat': allow_nat,\n 'allow_snat': allow_snat,\n 'description': description,\n 'gateway_failsafe_device': gateway_failsafe_device,\n 'ignore_persisted_weight': ignore_persisted_weight,\n 'ip_tos_client:': ip_tos_to_client,\n 'ip_tos_server': ip_tos_to_server,\n 'link_qos_to_client': link_qos_to_client,\n 'link_qos_to_server': link_qos_to_server,\n 'load_balancing_mode': load_balancing_mode,\n 'min_active_members': min_active_members,\n 'min_up_members': min_up_members,\n 'min_up_members_checking': min_up_members_checking,\n 'monitor': monitor,\n 'profiles': profiles,\n 'queue_depth_limit': queue_depth_limit,\n 'queue_on_connection_limit': queue_on_connection_limit,\n 'queue_time_limit': queue_time_limit,\n 'reselect_tries': reselect_tries,\n 'service_down_action': service_down_action,\n 'slow_ramp_time': slow_ramp_time\n }\n )\n\n #is this pool currently configured?\n existing = __salt__['bigip.list_pool'](hostname, username, password, name)\n\n # if it exists\n if existing['code'] == 200:\n\n ret['result'] = True\n ret['comment'] = 'A pool by this name currently exists. No change made.'\n\n # if it doesn't exist\n elif existing['code'] == 404:\n\n response = __salt__['bigip.create_pool'](hostname=hostname,\n username=username,\n password=password,\n name=name,\n members=members,\n allow_nat=allow_nat,\n allow_snat=allow_snat,\n description=description,\n gateway_failsafe_device=gateway_failsafe_device,\n ignore_persisted_weight=ignore_persisted_weight,\n ip_tos_to_client=ip_tos_to_client,\n ip_tos_to_server=ip_tos_to_server,\n link_qos_to_client=link_qos_to_client,\n link_qos_to_server=link_qos_to_server,\n load_balancing_mode=load_balancing_mode,\n min_active_members=min_active_members,\n min_up_members=min_up_members,\n min_up_members_action=min_up_members_action,\n min_up_members_checking=min_up_members_checking,\n monitor=monitor,\n profiles=profiles,\n queue_depth_limit=queue_depth_limit,\n queue_on_connection_limit=queue_on_connection_limit,\n queue_time_limit=queue_time_limit,\n reselect_tries=reselect_tries,\n service_down_action=service_down_action,\n slow_ramp_time=slow_ramp_time)\n if response['code'] == 200:\n ret['result'] = True\n ret['changes']['old'] = {}\n ret['changes']['new'] = response['content']\n ret['comment'] = 'Pool was successfully created.'\n else:\n ret = _load_result(existing, ret)\n\n # else something else was returned\n else:\n ret = _load_result(existing, ret)\n\n return ret", "response": "Creates a new node in the iControl REST network."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a new pool if it does not already exist. Pool members are managed separately. Only the parameters specified are enforced. hostname The host/address of the bigip device username The iControl REST username password The iControl REST password name The name of the pool to create allow_nat [yes | no] allow_snat [yes | no] description [string] gateway_failsafe_device [string] ignore_persisted_weight [enabled | disabled] ip_tos_to_client [pass-through | [integer]] ip_tos_to_server [pass-through | [integer]] link_qos_to_client [pass-through | [integer]] link_qos_to_server [pass-through | [integer]] load_balancing_mode [dynamic-ratio-member | dynamic-ratio-node | fastest-app-response | fastest-node | least-connections-members | least-connections-node | least-sessions | observed-member | observed-node | predictive-member | predictive-node | ratio-least-connections-member | ratio-least-connections-node | ratio-member | ratio-node | ratio-session | round-robin | weighted-least-connections-member | weighted-least-connections-node] min_active_members [integer] min_up_members [integer] min_up_members_action [failover | reboot | restart-all] min_up_members_checking [enabled | disabled] monitor [name] profiles [none | profile_name] queue_depth_limit [integer] queue_on_connection_limit [enabled | disabled] queue_time_limit [integer] reselect_tries [integer] service_down_action [drop | none | reselect | reset] slow_ramp_time [integer]", "response": "def manage_pool(hostname, username, password, name,\n allow_nat=None,\n allow_snat=None,\n description=None,\n gateway_failsafe_device=None,\n ignore_persisted_weight=None,\n ip_tos_to_client=None,\n ip_tos_to_server=None,\n link_qos_to_client=None,\n link_qos_to_server=None,\n load_balancing_mode=None,\n min_active_members=None,\n min_up_members=None,\n min_up_members_action=None,\n min_up_members_checking=None,\n monitor=None,\n profiles=None,\n queue_depth_limit=None,\n queue_on_connection_limit=None,\n queue_time_limit=None,\n reselect_tries=None,\n service_down_action=None,\n slow_ramp_time=None):\n '''\n Create a new pool if it does not already exist. Pool members are managed separately. Only the\n parameters specified are enforced.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the pool to create\n allow_nat\n [yes | no]\n allow_snat\n [yes | no]\n description\n [string]\n gateway_failsafe_device\n [string]\n ignore_persisted_weight\n [enabled | disabled]\n ip_tos_to_client\n [pass-through | [integer]]\n ip_tos_to_server\n [pass-through | [integer]]\n link_qos_to_client\n [pass-through | [integer]]\n link_qos_to_server\n [pass-through | [integer]]\n load_balancing_mode\n [dynamic-ratio-member | dynamic-ratio-node |\n fastest-app-response | fastest-node |\n least-connections-members |\n least-connections-node |\n least-sessions |\n observed-member | observed-node |\n predictive-member | predictive-node |\n ratio-least-connections-member |\n ratio-least-connections-node |\n ratio-member | ratio-node | ratio-session |\n round-robin | weighted-least-connections-member |\n weighted-least-connections-node]\n min_active_members\n [integer]\n min_up_members\n [integer]\n min_up_members_action\n [failover | reboot | restart-all]\n min_up_members_checking\n [enabled | disabled]\n monitor\n [name]\n profiles\n [none | profile_name]\n queue_depth_limit\n [integer]\n queue_on_connection_limit\n [enabled | disabled]\n queue_time_limit\n [integer]\n reselect_tries\n [integer]\n service_down_action\n [drop | none | reselect | reset]\n slow_ramp_time\n [integer]\n\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'manage', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'allow_nat': allow_nat,\n 'allow_snat': allow_snat,\n 'description': description,\n 'gateway_failsafe_device': gateway_failsafe_device,\n 'ignore_persisted_weight': ignore_persisted_weight,\n 'ip_tos_client:': ip_tos_to_client,\n 'ip_tos_server': ip_tos_to_server,\n 'link_qos_to_client': link_qos_to_client,\n 'link_qos_to_server': link_qos_to_server,\n 'load_balancing_mode': load_balancing_mode,\n 'min_active_members': min_active_members,\n 'min_up_members': min_up_members,\n 'min_up_members_checking': min_up_members_checking,\n 'monitor': monitor,\n 'profiles': profiles,\n 'queue_depth_limit': queue_depth_limit,\n 'queue_on_connection_limit': queue_on_connection_limit,\n 'queue_time_limit': queue_time_limit,\n 'reselect_tries': reselect_tries,\n 'service_down_action': service_down_action,\n 'slow_ramp_time': slow_ramp_time\n }\n )\n\n #is this pool currently configured?\n existing = __salt__['bigip.list_pool'](hostname, username, password, name)\n\n # if it exists\n if existing['code'] == 200:\n\n modified = __salt__['bigip.modify_pool'](hostname=hostname,\n username=username,\n password=password,\n name=name,\n allow_nat=allow_nat,\n allow_snat=allow_snat,\n description=description,\n gateway_failsafe_device=gateway_failsafe_device,\n ignore_persisted_weight=ignore_persisted_weight,\n ip_tos_to_client=ip_tos_to_client,\n ip_tos_to_server=ip_tos_to_server,\n link_qos_to_client=link_qos_to_client,\n link_qos_to_server=link_qos_to_server,\n load_balancing_mode=load_balancing_mode,\n min_active_members=min_active_members,\n min_up_members=min_up_members,\n min_up_members_action=min_up_members_action,\n min_up_members_checking=min_up_members_checking,\n monitor=monitor,\n profiles=profiles,\n queue_depth_limit=queue_depth_limit,\n queue_on_connection_limit=queue_on_connection_limit,\n queue_time_limit=queue_time_limit,\n reselect_tries=reselect_tries,\n service_down_action=service_down_action,\n slow_ramp_time=slow_ramp_time)\n\n #was the modification successful?\n if modified['code'] == 200:\n\n #remove member listings and self-links\n del existing['content']['membersReference']\n del modified['content']['membersReference']\n del existing['content']['selfLink']\n del modified['content']['selfLink']\n\n ret = _check_for_changes('Pool', ret, existing, modified)\n else:\n ret = _load_result(modified, ret)\n\n # if it doesn't exist\n elif existing['code'] == 404:\n\n new = __salt__['bigip.create_pool'](hostname=hostname,\n username=username,\n password=password,\n name=name,\n allow_nat=allow_nat,\n allow_snat=allow_snat,\n description=description,\n gateway_failsafe_device=gateway_failsafe_device,\n ignore_persisted_weight=ignore_persisted_weight,\n ip_tos_to_client=ip_tos_to_client,\n ip_tos_to_server=ip_tos_to_server,\n link_qos_to_client=link_qos_to_client,\n link_qos_to_server=link_qos_to_server,\n load_balancing_mode=load_balancing_mode,\n min_active_members=min_active_members,\n min_up_members=min_up_members,\n min_up_members_action=min_up_members_action,\n min_up_members_checking=min_up_members_checking,\n monitor=monitor,\n profiles=profiles,\n queue_depth_limit=queue_depth_limit,\n queue_on_connection_limit=queue_on_connection_limit,\n queue_time_limit=queue_time_limit,\n reselect_tries=reselect_tries,\n service_down_action=service_down_action,\n slow_ramp_time=slow_ramp_time)\n\n # were we able to create it?\n if new['code'] == 200:\n ret['result'] = True\n ret['comment'] = 'Pool was created and enforced to the desired state. Note: Only parameters specified ' \\\n 'were enforced. See changes for details.'\n ret['changes']['old'] = {}\n ret['changes']['new'] = new['content']\n\n # unable to create it\n else:\n ret = _load_result(new, ret)\n\n # else something else was returned\n else:\n ret = _load_result(existing, ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete_pool(hostname, username, password, name):\n '''\n Delete an existing pool.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the pool which will be deleted\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'delete', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n }\n )\n\n #is this pool currently configured?\n existing = __salt__['bigip.list_pool'](hostname, username, password, name)\n # if it exists by name\n if existing['code'] == 200:\n\n deleted = __salt__['bigip.delete_pool'](hostname, username, password, name)\n # did we get rid of it?\n if deleted['code'] == 200:\n ret['result'] = True\n ret['comment'] = 'Pool was successfully deleted.'\n ret['changes']['old'] = existing['content']\n ret['changes']['new'] = {}\n\n # something bad happened\n else:\n ret = _load_result(deleted, ret)\n\n # not found\n elif existing['code'] == 404:\n ret['result'] = True\n ret['comment'] = 'This pool already does not exist. No changes made.'\n ret['changes']['old'] = {}\n ret['changes']['new'] = {}\n\n else:\n ret = _load_result(existing, ret)\n\n return ret", "response": "Delete an existing pool."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmanages the members of an existing pool.", "response": "def manage_pool_members(hostname, username, password, name, members):\n '''\n Manage the members of an existing pool. This function replaces all current pool members.\n Only the parameters specified are enforced.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the pool to modify\n members\n list of pool members to manage.\n\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'manage', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'members': members\n }\n )\n\n #is this pool currently configured?\n existing = __salt__['bigip.list_pool'](hostname, username, password, name)\n\n # if it exists\n if existing['code'] == 200:\n\n #what are the current members?\n current_members = existing['content']['membersReference']['items']\n\n modified = __salt__['bigip.replace_pool_members'](hostname, username, password, name, members)\n\n #was the modification successful?\n if modified['code'] == 200:\n\n #re-list the pool with new membership\n new_listing = __salt__['bigip.list_pool'](hostname, username, password, name)\n\n #just in case something happened...\n if new_listing['code'] != 200:\n ret = _load_result(new_listing, ret)\n ret['comment'] = 'modification of the pool was successful but an error occurred upon retrieving new' \\\n ' listing.'\n return ret\n\n new_members = new_listing['content']['membersReference']['items']\n\n #remove generation keys and create new lists indexed by integers\n for current_member in current_members:\n del current_member['generation']\n\n for new_member in new_members:\n del new_member['generation']\n\n #anything changed?\n ret = _check_for_changes('Pool Membership', ret, current_members, new_members)\n\n else:\n ret = _load_result(modified, ret)\n\n #pool does not exists\n elif existing['code'] == 404:\n ret['comment'] = 'A pool with this name was not found.'\n\n else:\n ret = _load_result(existing, ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_pool_member(hostname, username, password, name, member):\n '''\n A function to connect to a bigip device and add a new member to an existing pool.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the pool to modify\n member\n The member to add to the pool\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'add', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'members': member\n }\n )\n\n #is this pool member currently configured?\n existing_pool = __salt__['bigip.list_pool'](hostname, username, password, name)\n\n if existing_pool['code'] == 200:\n\n # for some reason iControl REST doesn't support listing a single pool member.\n # the response from GET for listing a member will return 200 even if it doesn't exists.\n # because of this we have to do some rather \"unnecessary\" searching within a pool.\n\n #what are the current members?\n current_members = existing_pool['content']['membersReference']['items']\n\n #loop through them\n exists = False\n for current_member in current_members:\n if current_member['name'] == member['name']:\n exists = True\n break\n\n if exists:\n ret['result'] = True\n ret['comment'] = 'Member: {name} already exists within this pool. No changes made.'.format(name=member['name'])\n ret['changes']['old'] = {}\n ret['changes']['new'] = {}\n else:\n new_member = __salt__['bigip.add_pool_member'](hostname, username, password, name, member)\n\n if new_member['code'] == 200:\n ret['result'] = True\n ret['comment'] = 'Member: {name} has been successfully added to the pool.'.format(name=member['name'])\n ret['changes']['old'] = {}\n\n #look up the member again...\n pool_listing = __salt__['bigip.list_pool'](hostname, username, password, name)\n\n if pool_listing['code'] != 200:\n ret = _load_result(new_member, ret)\n return ret\n\n members = pool_listing['content']['membersReference']['items']\n #loop through them\n for current_member in members:\n if current_member['name'] == member['name']:\n added_member = current_member\n break\n\n ret['changes']['new'] = added_member\n\n # member wasn't added\n else:\n ret = _load_result(new_member, ret)\n\n #pool does not exists\n elif existing_pool['code'] == 404:\n ret['comment'] = 'A pool with this name was not found.'\n else:\n ret = _load_result(existing_pool, ret)\n\n return ret", "response": "A function to connect to a bigip device and add a new member to an existing pool."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmodify a member of a pool.", "response": "def modify_pool_member(hostname, username, password, name, member,\n connection_limit=None,\n description=None,\n dynamic_ratio=None,\n inherit_profile=None,\n logging=None,\n monitor=None,\n priority_group=None,\n profiles=None,\n rate_limit=None,\n ratio=None,\n session=None,\n member_state=None):\n '''\n A function to connect to a bigip device and modify a member of an existing pool.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the pool to modify\n member\n The member modify\n connection_limit\n [integer]\n description\n [string]\n dynamic_ratio\n [integer]\n inherit_profile\n [enabled | disabled]\n logging\n [enabled | disabled]\n monitor\n [name]\n priority_group\n [integer]\n profiles\n [none | profile_name]\n rate_limit\n [integer]\n ratio\n [integer]\n session\n [user-enabled | user-disabled]\n member_state (state)\n [ user-up | user-down ]\n\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'modify', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'members': member\n }\n )\n\n #is this pool member currently configured?\n existing_pool = __salt__['bigip.list_pool'](hostname, username, password, name)\n\n if existing_pool['code'] == 200:\n\n # for some reason iControl REST doesn't support listing a single pool member.\n # the response from GET for listing a member will return 200 even if it doesn't exists.\n # because of this we have to do some rather \"unnecessary\" searching within a pool.\n\n #what are the current members?\n current_members = existing_pool['content']['membersReference']['items']\n\n #loop through them\n exists = False\n for current_member in current_members:\n if current_member['name'] == member:\n exists = True\n existing_member = current_member\n break\n\n if exists:\n\n #modify the pool member\n modified = __salt__['bigip.modify_pool_member'](hostname=hostname,\n username=username,\n password=password,\n name=name,\n member=member,\n connection_limit=connection_limit,\n description=description,\n dynamic_ratio=dynamic_ratio,\n inherit_profile=inherit_profile,\n logging=logging,\n monitor=monitor,\n priority_group=priority_group,\n profiles=profiles,\n rate_limit=rate_limit,\n ratio=ratio,\n session=session,\n state=member_state)\n\n #re-list the pool\n new_pool = __salt__['bigip.list_pool'](hostname, username, password, name)\n\n if modified['code'] == 200 and modified['code'] == 200:\n\n #what are the new members?\n new_members = new_pool['content']['membersReference']['items']\n\n #loop through them\n for new_member in new_members:\n if new_member['name'] == member:\n modified_member = new_member\n break\n\n #check for changes\n old = {'content': existing_member}\n new = {'content': modified_member}\n ret = _check_for_changes('Pool Member: {member}'.format(member=member), ret, old, new)\n\n else:\n ret = _load_result(modified, ret)\n else:\n ret['comment'] = 'Member: {name} does not exists within this pool. No changes made.'.format(name=member['name'])\n\n #pool does not exists\n elif existing_pool['code'] == 404:\n ret['comment'] = 'A pool with this name was not found.'\n else:\n ret = _load_result(existing_pool, ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndeletes an existing pool member.", "response": "def delete_pool_member(hostname, username, password, name, member):\n '''\n Delete an existing pool member.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the pool to be modified\n member\n The name of the member to delete from the pool\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'delete', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'members': member\n }\n )\n\n #is this pool currently configured?\n existing = __salt__['bigip.list_pool'](hostname, username, password, name)\n\n # if it exists by name\n if existing['code'] == 200:\n\n #what are the current members?\n current_members = existing['content']['membersReference']['items']\n\n #loop through them\n exists = False\n for current_member in current_members:\n if current_member['name'] == member:\n exists = True\n existing_member = current_member\n break\n\n if exists:\n deleted = __salt__['bigip.delete_pool_member'](hostname, username, password, name, member)\n # did we get rid of it?\n if deleted['code'] == 200:\n ret['result'] = True\n ret['comment'] = 'Pool Member: {member} was successfully deleted.'.format(member=member)\n ret['changes']['old'] = existing_member\n ret['changes']['new'] = {}\n\n # something bad happened\n else:\n ret['result'] = True\n ret['comment'] = 'This pool member already does not exist. No changes made.'\n ret['changes']['old'] = {}\n ret['changes']['new'] = {}\n\n else:\n ret = _load_result(existing, ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef manage_virtual(hostname, username, password, name, destination,\n pool=None,\n address_status=None,\n auto_lasthop=None,\n bwc_policy=None,\n cmp_enabled=None,\n connection_limit=None,\n dhcp_relay=None,\n description=None,\n fallback_persistence=None,\n flow_eviction_policy=None,\n gtm_score=None,\n ip_forward=None,\n ip_protocol=None,\n internal=None,\n twelve_forward=None,\n last_hop_pool=None,\n mask=None,\n mirror=None,\n nat64=None,\n persist=None,\n profiles=None,\n policies=None,\n rate_class=None,\n rate_limit=None,\n rate_limit_mode=None,\n rate_limit_dst=None,\n rate_limit_src=None,\n rules=None,\n related_rules=None,\n reject=None,\n source=None,\n source_address_translation=None,\n source_port=None,\n virtual_state=None,\n traffic_classes=None,\n translate_address=None,\n translate_port=None,\n vlans=None):\n '''\n Manage a virtual server. If a virtual does not exists it will be created, otherwise only the\n parameters specified will be enforced.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the virtual to create\n destination\n [ [virtual_address_name:port] | [ipv4:port] | [ipv6.port] ]\n pool\n [ [pool_name] | none]\n address_status\n [yes | no]\n auto_lasthop\n [default | enabled | disabled ]\n bwc_policy\n [none] | string]\n cmp_enabled\n [yes | no]\n dhcp_relay\n [yes | no}\n connection_limit\n [integer]\n description\n [string]\n state\n [disabled | enabled]\n fallback_persistence\n [none | [profile name] ]\n flow_eviction_policy\n [none | [eviction policy name] ]\n gtm_score\n [integer]\n ip_forward\n [yes | no]\n ip_protocol\n [any | protocol]\n internal\n [yes | no]\n twelve_forward(12-forward)\n [yes | no]\n last_hop-pool\n [ [pool_name] | none]\n mask\n { [ipv4] | [ipv6] }\n mirror\n { [disabled | enabled | none] }\n nat64\n [enabled | disabled]\n persist\n [list]\n profiles\n [none | default | list ]\n policies\n [none | default | list ]\n rate_class\n [name]\n rate_limit\n [integer]\n rate_limit-mode\n [destination | object | object-destination |\n object-source | object-source-destination |\n source | source-destination]\n rate_limit-dst\n [integer]\n rate_limit-src\n [integer]\n rules\n [none | list ]\n related_rules\n [none | list ]\n reject\n [yes | no]\n source\n { [ipv4[/prefixlen]] | [ipv6[/prefixlen]] }\n source_address_translation\n [none | snat:pool_name | lsn | automap | dictionary ]\n source_port\n [change | preserve | preserve-strict]\n state\n [enabled | disabled]\n traffic_classes\n [none | default | list ]\n translate_address\n [enabled | disabled]\n translate_port\n [enabled | disabled]\n vlans\n [none | default | dictionary]\n\n vlan_ids\n [ list]\n enabled\n [ true | false ]\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'manage', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'destination': destination,\n 'pool': pool,\n 'address_status': address_status,\n 'auto_lasthop': auto_lasthop,\n 'bwc_policy': bwc_policy,\n 'cmp_enabled': cmp_enabled,\n 'connection_limit': connection_limit,\n 'dhcp_relay': dhcp_relay,\n 'description': description,\n 'fallback_persistence': fallback_persistence,\n 'flow_eviction_policy': flow_eviction_policy,\n 'gtm_score': gtm_score,\n 'ip_forward': ip_forward,\n 'ip_protocol': ip_protocol,\n 'internal': internal,\n 'twelve_forward': twelve_forward,\n 'last_hop_pool': last_hop_pool,\n 'mask': mask,\n 'mirror': mirror,\n 'nat64': nat64,\n 'persist': persist,\n 'profiles': profiles,\n 'policies': policies,\n 'rate_class': rate_class,\n 'rate_limit': rate_limit,\n 'rate_limit_mode': rate_limit_mode,\n 'rate_limit_dst': rate_limit_dst,\n 'rate_limit_src': rate_limit_src,\n 'rules': rules,\n 'related_rules': related_rules,\n 'reject': reject,\n 'source': source,\n 'source_address_translation': source_address_translation,\n 'source_port': source_port,\n 'virtual_state': virtual_state,\n 'traffic_classes': traffic_classes,\n 'translate_address': translate_address,\n 'translate_port': translate_port,\n 'vlans': vlans\n }\n )\n\n existing = __salt__['bigip.list_virtual'](hostname, username, password, name)\n\n # does this virtual exist?\n if existing['code'] == 200:\n\n # modify\n modified = __salt__['bigip.modify_virtual'](hostname=hostname,\n username=username,\n password=password,\n name=name,\n destination=destination,\n description=description,\n pool=pool,\n address_status=address_status,\n auto_lasthop=auto_lasthop,\n bwc_policy=bwc_policy,\n cmp_enabled=cmp_enabled,\n connection_limit=connection_limit,\n dhcp_relay=dhcp_relay,\n fallback_persistence=fallback_persistence,\n flow_eviction_policy=flow_eviction_policy,\n gtm_score=gtm_score,\n ip_forward=ip_forward,\n ip_protocol=ip_protocol,\n internal=internal,\n twelve_forward=twelve_forward,\n last_hop_pool=last_hop_pool,\n mask=mask,\n mirror=mirror,\n nat64=nat64,\n persist=persist,\n profiles=profiles,\n policies=policies,\n rate_class=rate_class,\n rate_limit=rate_limit,\n rate_limit_mode=rate_limit_mode,\n rate_limit_dst=rate_limit_dst,\n rate_limit_src=rate_limit_src,\n rules=rules,\n related_rules=related_rules,\n reject=reject,\n source=source,\n source_address_translation=source_address_translation,\n source_port=source_port,\n state=virtual_state,\n traffic_classes=traffic_classes,\n translate_address=translate_address,\n translate_port=translate_port,\n vlans=vlans)\n\n #was the modification successful?\n if modified['code'] == 200:\n\n #relist it to compare\n relisting = __salt__['bigip.list_virtual'](hostname, username, password, name)\n\n if relisting['code'] == 200:\n\n relisting = _strip_key(relisting, 'generation')\n existing = _strip_key(existing, 'generation')\n\n ret = _check_for_changes('Virtual', ret, existing, relisting)\n else:\n ret = _load_result(relisting, ret)\n\n else:\n ret = _load_result(modified, ret)\n\n elif existing['code'] == 404:\n\n #create it\n virtual = __salt__['bigip.create_virtual'](hostname=hostname,\n username=username,\n password=password,\n name=name,\n destination=destination,\n description=description,\n pool=pool,\n address_status=address_status,\n auto_lasthop=auto_lasthop,\n bwc_policy=bwc_policy,\n cmp_enabled=cmp_enabled,\n connection_limit=connection_limit,\n dhcp_relay=dhcp_relay,\n fallback_persistence=fallback_persistence,\n flow_eviction_policy=flow_eviction_policy,\n gtm_score=gtm_score,\n ip_forward=ip_forward,\n ip_protocol=ip_protocol,\n internal=internal,\n twelve_forward=twelve_forward,\n last_hop_pool=last_hop_pool,\n mask=mask,\n mirror=mirror,\n nat64=nat64,\n persist=persist,\n profiles=profiles,\n policies=policies,\n rate_class=rate_class,\n rate_limit=rate_limit,\n rate_limit_mode=rate_limit_mode,\n rate_limit_dst=rate_limit_dst,\n rate_limit_src=rate_limit_src,\n rules=rules,\n related_rules=related_rules,\n reject=reject,\n source=source,\n source_address_translation=source_address_translation,\n source_port=source_port,\n state=virtual_state,\n traffic_classes=traffic_classes,\n translate_address=translate_address,\n translate_port=translate_port,\n vlans=vlans)\n\n #were we able to create it?\n if virtual['code'] == 200:\n ret['result'] = True\n ret['changes']['old'] = {}\n ret['changes']['new'] = virtual['content']\n ret['comment'] = 'Virtual was successfully created and enforced to the desired state.'\n\n else:\n ret = _load_result(virtual, ret)\n\n else:\n ret = _load_result(existing, ret)\n\n return ret", "response": "This function creates a virtual server in the iControl REST server."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef modify_virtual(hostname, username, password, name, destination,\n pool=None,\n address_status=None,\n auto_lasthop=None,\n bwc_policy=None,\n cmp_enabled=None,\n connection_limit=None,\n dhcp_relay=None,\n description=None,\n fallback_persistence=None,\n flow_eviction_policy=None,\n gtm_score=None,\n ip_forward=None,\n ip_protocol=None,\n internal=None,\n twelve_forward=None,\n last_hop_pool=None,\n mask=None,\n mirror=None,\n nat64=None,\n persist=None,\n profiles=None,\n policies=None,\n rate_class=None,\n rate_limit=None,\n rate_limit_mode=None,\n rate_limit_dst=None,\n rate_limit_src=None,\n rules=None,\n related_rules=None,\n reject=None,\n source=None,\n source_address_translation=None,\n source_port=None,\n virtual_state=None,\n traffic_classes=None,\n translate_address=None,\n translate_port=None,\n vlans=None):\n '''\n Modify an virtual server. modify an existing virtual. Only parameters specified will be enforced.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the virtual to create\n destination\n [ [virtual_address_name:port] | [ipv4:port] | [ipv6.port] ]\n pool\n [ [pool_name] | none]\n address_status\n [yes | no]\n auto_lasthop\n [default | enabled | disabled ]\n bwc_policy\n [none] | string]\n cmp_enabled\n [yes | no]\n dhcp_relay\n [yes | no}\n connection_limit\n [integer]\n description\n [string]\n state\n [disabled | enabled]\n fallback_persistence\n [none | [profile name] ]\n flow_eviction_policy\n [none | [eviction policy name] ]\n gtm_score\n [integer]\n ip_forward\n [yes | no]\n ip_protocol\n [any | protocol]\n internal\n [yes | no]\n twelve_forward(12-forward)\n [yes | no]\n last_hop-pool\n [ [pool_name] | none]\n mask\n { [ipv4] | [ipv6] }\n mirror\n { [disabled | enabled | none] }\n nat64\n [enabled | disabled]\n persist\n [list]\n profiles\n [none | default | list ]\n policies\n [none | default | list ]\n rate_class\n [name]\n rate_limit\n [integer]\n rate_limit-mode\n [destination | object | object-destination |\n object-source | object-source-destination |\n source | source-destination]\n rate_limit_dst\n [integer]\n rate_limit_src\n [integer]\n rules\n [none | list ]\n related_rules\n [none | list ]\n reject\n [yes | no]\n source\n { [ipv4[/prefixlen]] | [ipv6[/prefixlen]] }\n source_address_translation\n [none | snat:pool_name | lsn | automap | dictionary ]\n source_port\n [change | preserve | preserve-strict]\n state\n [enabled | disabled]\n traffic_classes\n [none | default | list ]\n translate_address\n [enabled | disabled]\n translate_port\n [enabled | disabled]\n vlans\n [none | default | dictionary ]\n\n vlan_ids\n [ list]\n enabled\n [ true | false ]\n\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'modify', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'name': name,\n 'destination': destination,\n 'pool': pool,\n 'address_status': address_status,\n 'auto_lasthop': auto_lasthop,\n 'bwc_policy': bwc_policy,\n 'cmp_enabled': cmp_enabled,\n 'connection_limit': connection_limit,\n 'dhcp_relay': dhcp_relay,\n 'description': description,\n 'fallback_persistence': fallback_persistence,\n 'flow_eviction_policy': flow_eviction_policy,\n 'gtm_score': gtm_score,\n 'ip_forward': ip_forward,\n 'ip_protocol': ip_protocol,\n 'internal': internal,\n 'twelve_forward': twelve_forward,\n 'last_hop_pool': last_hop_pool,\n 'mask': mask,\n 'mirror': mirror,\n 'nat64': nat64,\n 'persist': persist,\n 'profiles': profiles,\n 'policies': policies,\n 'rate_class': rate_class,\n 'rate_limit': rate_limit,\n 'rate_limit_mode': rate_limit_mode,\n 'rate_limit_dst': rate_limit_dst,\n 'rate_limit_src': rate_limit_src,\n 'rules': rules,\n 'related_rules': related_rules,\n 'reject': reject,\n 'source': source,\n 'source_address_translation': source_address_translation,\n 'source_port': source_port,\n 'virtual_state': virtual_state,\n 'traffic_classes': traffic_classes,\n 'translate_address': translate_address,\n 'translate_port': translate_port,\n 'vlans': vlans\n }\n )\n\n existing = __salt__['bigip.list_virtual'](hostname, username, password, name)\n\n # does this virtual exist?\n if existing['code'] == 200:\n\n # modify\n modified = __salt__['bigip.modify_virtual'](hostname=hostname,\n username=username,\n password=password,\n name=name,\n destination=destination,\n description=description,\n pool=pool,\n address_status=address_status,\n auto_lasthop=auto_lasthop,\n bwc_policy=bwc_policy,\n cmp_enabled=cmp_enabled,\n connection_limit=connection_limit,\n dhcp_relay=dhcp_relay,\n fallback_persistence=fallback_persistence,\n flow_eviction_policy=flow_eviction_policy,\n gtm_score=gtm_score,\n ip_forward=ip_forward,\n ip_protocol=ip_protocol,\n internal=internal,\n twelve_forward=twelve_forward,\n last_hop_pool=last_hop_pool,\n mask=mask,\n mirror=mirror,\n nat64=nat64,\n persist=persist,\n profiles=profiles,\n policies=policies,\n rate_class=rate_class,\n rate_limit=rate_limit,\n rate_limit_mode=rate_limit_mode,\n rate_limit_dst=rate_limit_dst,\n rate_limit_src=rate_limit_src,\n rules=rules,\n related_rules=related_rules,\n reject=reject,\n source=source,\n source_address_translation=source_address_translation,\n source_port=source_port,\n state=virtual_state,\n traffic_classes=traffic_classes,\n translate_address=translate_address,\n translate_port=translate_port,\n vlans=vlans)\n\n #was the modification successful?\n if modified['code'] == 200:\n\n #relist it to compare\n relisting = __salt__['bigip.list_virtual'](hostname, username, password, name)\n\n if relisting['code'] == 200:\n\n relisting = _strip_key(relisting, 'generation')\n existing = _strip_key(existing, 'generation')\n\n ret = _check_for_changes('Virtual', ret, existing, relisting)\n else:\n ret = _load_result(relisting, ret)\n\n else:\n ret = _load_result(modified, ret)\n\n elif existing['code'] == 404:\n ret['comment'] = 'A Virtual with this name was not found.'\n # else something else was returned\n else:\n ret = _load_result(existing, ret)\n\n return ret", "response": "This function is used to modify an existing virtual server."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_monitor(hostname, username, password, monitor_type, name, **kwargs):\n '''\n A function to connect to a bigip device and create a monitor.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n monitor_type\n The type of monitor to create\n name\n The name of the monitor to create\n kwargs\n [ arg=val ] ...\n\n Consult F5 BIGIP user guide for specific options for each monitor type.\n Typically, tmsh arg names are used.\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n\n params = {\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'monitor_type': monitor_type,\n 'name': name\n }\n\n for key, value in six.iteritems(kwargs):\n params[key] = value\n\n return _test_output(ret, 'create', params)\n\n #is this monitor currently configured?\n existing = __salt__['bigip.list_monitor'](hostname, username, password, monitor_type, name)\n\n # if it exists\n if existing['code'] == 200:\n\n ret['result'] = True\n ret['comment'] = 'A monitor by this name currently exists. No change made.'\n\n # if it doesn't exist\n elif existing['code'] == 404:\n\n response = __salt__['bigip.create_monitor'](hostname, username, password, monitor_type, name, **kwargs)\n if response['code'] == 200:\n ret['result'] = True\n ret['changes']['old'] = {}\n ret['changes']['new'] = response['content']\n ret['comment'] = 'Monitor was successfully created.'\n else:\n ret = _load_result(response, ret)\n\n # else something else was returned\n else:\n ret = _load_result(existing, ret)\n\n return ret", "response": "Create a monitor on a bigip device."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_profile(hostname, username, password, profile_type, name, **kwargs):\n r'''\n A function to connect to a bigip device and create a profile.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n profile_type\n The type of profile to create\n name\n The name of the profile to create\n kwargs\n [ arg=val ] ...\n\n Consult F5 BIGIP user guide for specific options for each profile type.\n Typically, tmsh arg names are used.\n\n Special Characters ``|``, ``,`` and ``:`` must be escaped using ``\\`` when\n used within strings.\n\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n if __opts__['test']:\n return _test_output(ret, 'create', params={\n 'hostname': hostname,\n 'username': username,\n 'password': password,\n 'profile_type': profile_type,\n 'name': name\n })\n\n #is this profile currently configured?\n existing = __salt__['bigip.list_profile'](hostname, username, password, profile_type, name)\n\n # if it exists\n if existing['code'] == 200:\n\n ret['result'] = True\n ret['comment'] = 'A profile by this name currently exists. No change made.'\n\n # if it doesn't exist\n elif existing['code'] == 404:\n\n response = __salt__['bigip.create_profile'](hostname, username, password, profile_type, name, **kwargs)\n\n if response['code'] == 200:\n ret['result'] = True\n ret['changes']['old'] = {}\n ret['changes']['new'] = response['content']\n ret['comment'] = 'Profile was successfully created.'\n else:\n ret = _load_result(response, ret)\n\n # else something else was returned\n else:\n ret = _load_result(existing, ret)\n\n return ret", "response": "r Creates a profile on a bigip device"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of the images that are on the provider.", "response": "def avail_images(call=None):\n ''' Return a list of the images that are on the provider.\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_images function must be called with '\n '-f or --function, or with the --list-images option'\n )\n\n items = query(method='images', root='marketplace_root')\n ret = {}\n for image in items['images']:\n ret[image['id']] = {}\n for item in image:\n ret[image['id']][item] = six.text_type(image[item])\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a list of the BareMetal servers that are on the provider.", "response": "def list_nodes(call=None):\n ''' Return a list of the BareMetal servers that are on the provider.\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes function must be called with -f or --function.'\n )\n\n items = query(method='servers')\n\n ret = {}\n for node in items['servers']:\n public_ips = []\n private_ips = []\n image_id = ''\n\n if node.get('public_ip'):\n public_ips = [node['public_ip']['address']]\n\n if node.get('private_ip'):\n private_ips = [node['private_ip']]\n\n if node.get('image'):\n image_id = node['image']['id']\n\n ret[node['name']] = {\n 'id': node['id'],\n 'image_id': image_id,\n 'public_ips': public_ips,\n 'private_ips': private_ips,\n 'size': node['volumes']['0']['size'],\n 'state': node['state'],\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list_nodes_full(call=None):\n ''' Return a list of the BareMetal servers that are on the provider.\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'list_nodes_full must be called with -f or --function'\n )\n\n items = query(method='servers')\n\n # For each server, iterate on its parameters.\n ret = {}\n for node in items['servers']:\n ret[node['name']] = {}\n for item in node:\n value = node[item]\n ret[node['name']][item] = value\n return ret", "response": "Return a list of the BareMetal servers that are on the provider."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_image(server_):\n ''' Return the image object to use.\n '''\n images = avail_images()\n server_image = six.text_type(config.get_cloud_config_value(\n 'image', server_, __opts__, search_global=False\n ))\n for image in images:\n if server_image in (images[image]['name'], images[image]['id']):\n return images[image]['id']\n raise SaltCloudNotFound(\n 'The specified image, \\'{0}\\', could not be found.'.format(server_image)\n )", "response": "Return the image object to use."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a single BareMetal server from a data dict.", "response": "def create(server_):\n '''\n Create a single BareMetal server from a data dict.\n '''\n try:\n # Check for required profile parameters before sending any API calls.\n if server_['profile'] and config.is_profile_configured(__opts__,\n __active_provider_name__ or 'scaleway',\n server_['profile'],\n vm_=server_) 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(server_['name']),\n args=__utils__['cloud.filter_event']('creating', server_, ['name', 'profile', 'provider', 'driver']),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n log.info('Creating a BareMetal server %s', server_['name'])\n\n access_key = config.get_cloud_config_value(\n 'access_key', get_configured_provider(), __opts__, search_global=False\n )\n\n commercial_type = config.get_cloud_config_value(\n 'commercial_type', server_, __opts__, default='C1'\n )\n\n key_filename = config.get_cloud_config_value(\n 'ssh_key_file', server_, __opts__, search_global=False, default=None\n )\n\n if key_filename is not None and not os.path.isfile(key_filename):\n raise SaltCloudConfigError(\n 'The defined key_filename \\'{0}\\' does not exist'.format(\n key_filename\n )\n )\n\n ssh_password = config.get_cloud_config_value(\n 'ssh_password', server_, __opts__\n )\n\n kwargs = {\n 'name': server_['name'],\n 'organization': access_key,\n 'image': get_image(server_),\n 'commercial_type': commercial_type,\n }\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(server_['name']),\n args={\n 'kwargs': __utils__['cloud.filter_event']('requesting', kwargs, list(kwargs)),\n },\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n try:\n ret = create_node(kwargs)\n except Exception as exc:\n log.error(\n 'Error creating %s on Scaleway\\n\\n'\n 'The following exception was thrown when trying to '\n 'run the initial deployment: %s',\n server_['name'], exc,\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n return False\n\n def __query_node_data(server_name):\n ''' Called to check if the server has a public IP address.\n '''\n data = show_instance(server_name, 'action')\n if data and data.get('public_ip'):\n return data\n return False\n\n try:\n data = salt.utils.cloud.wait_for_ip(\n __query_node_data,\n update_args=(server_['name'],),\n timeout=config.get_cloud_config_value(\n 'wait_for_ip_timeout', server_, __opts__, default=10 * 60),\n interval=config.get_cloud_config_value(\n 'wait_for_ip_interval', server_, __opts__, default=10),\n )\n except (SaltCloudExecutionTimeout, SaltCloudExecutionFailure) as exc:\n try:\n # It might be already up, let's destroy it!\n destroy(server_['name'])\n except SaltCloudSystemExit:\n pass\n finally:\n raise SaltCloudSystemExit(six.text_type(exc))\n\n server_['ssh_host'] = data['public_ip']['address']\n server_['ssh_password'] = ssh_password\n server_['key_filename'] = key_filename\n ret = __utils__['cloud.bootstrap'](server_, __opts__)\n\n ret.update(data)\n\n log.info('Created BareMetal server \\'%s\\'', server_['name'])\n log.debug(\n '\\'%s\\' BareMetal server creation details:\\n%s',\n server_['name'], pprint.pformat(data)\n )\n\n __utils__['cloud.fire_event'](\n 'event',\n 'created instance',\n 'salt/cloud/{0}/created'.format(server_['name']),\n args=__utils__['cloud.filter_event']('created', server_, ['name', 'profile', 'provider', 'driver']),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nmaking a call to the Scaleway API.", "response": "def query(method='servers', server_id=None, command=None, args=None,\n http_method='GET', root='api_root'):\n ''' Make a call to the Scaleway API.\n '''\n\n if root == 'api_root':\n default_url = 'https://cp-par1.scaleway.com'\n else:\n default_url = 'https://api-marketplace.scaleway.com'\n\n base_path = six.text_type(config.get_cloud_config_value(\n root,\n get_configured_provider(),\n __opts__,\n search_global=False,\n default=default_url\n ))\n\n path = '{0}/{1}/'.format(base_path, method)\n\n if server_id:\n path += '{0}/'.format(server_id)\n\n if command:\n path += command\n\n if not isinstance(args, dict):\n args = {}\n\n token = config.get_cloud_config_value(\n 'token', get_configured_provider(), __opts__, search_global=False\n )\n\n data = salt.utils.json.dumps(args)\n\n request = __utils__[\"http.query\"](path,\n method=http_method,\n data=data,\n status=True,\n decode=True,\n decode_type='json',\n data_render=True,\n data_renderer='json',\n headers=True,\n header_dict={'X-Auth-Token': token,\n 'User-Agent': \"salt-cloud\",\n 'Content-Type': 'application/json'})\n if request['status'] > 299:\n raise SaltCloudSystemExit(\n 'An error occurred while querying Scaleway. HTTP Code: {0} '\n 'Error: \\'{1}\\''.format(\n request['status'],\n request['error']\n )\n )\n\n # success without data\n if request['status'] == 204:\n return True\n\n return salt.utils.json.loads(request['body'])"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef script(server_):\n ''' Return the script deployment object.\n '''\n return salt.utils.cloud.os_script(\n config.get_cloud_config_value('script', server_, __opts__),\n server_,\n __opts__,\n salt.utils.cloud.salt_config_to_yaml(\n salt.utils.cloud.minion_config(__opts__, server_)\n )\n )", "response": "Return the script deployment object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nensure that the specified SSH key is present for the specified user.", "response": "def present(\n name,\n user,\n enc='ssh-rsa',\n comment='',\n source='',\n options=None,\n config='.ssh/authorized_keys',\n fingerprint_hash_type=None,\n **kwargs):\n '''\n Verifies that the specified SSH key is present for the specified user\n\n name\n The SSH key to manage\n\n user\n The user who owns the SSH authorized keys file to modify\n\n enc\n Defines what type of key is being used; can be ed25519, ecdsa, ssh-rsa\n or ssh-dss\n\n comment\n The comment to be placed with the SSH public key\n\n source\n The source file for the key(s). Can contain any number of public keys,\n in standard \"authorized_keys\" format. If this is set, comment and enc\n will be ignored.\n\n .. note::\n The source file must contain keys in the format `` \n ``. If you have generated a keypair using PuTTYgen, then you\n will need to do the following to retrieve an OpenSSH-compatible public\n key.\n\n 1. In PuTTYgen, click ``Load``, and select the *private* key file (not\n the public key), and click ``Open``.\n 2. Copy the public key from the box labeled ``Public key for pasting\n into OpenSSH authorized_keys file``.\n 3. Paste it into a new file.\n\n options\n The options passed to the key, pass a list object\n\n config\n The location of the authorized keys file relative to the user's home\n directory, defaults to \".ssh/authorized_keys\". Token expansion %u and\n %h for username and home path supported.\n\n fingerprint_hash_type\n The public key fingerprint hash type that the public key fingerprint\n was originally hashed with. This defaults to ``sha256`` if not specified.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n if source == '':\n # check if this is of form {options} {enc} {key} {comment}\n sshre = re.compile(r'^(.*?)\\s?((?:ssh\\-|ecds)[\\w-]+\\s.+)$')\n fullkey = sshre.search(name)\n # if it is {key} [comment]\n if not fullkey:\n key_and_comment = name.split(None, 1)\n name = key_and_comment[0]\n if len(key_and_comment) == 2:\n comment = key_and_comment[1]\n else:\n # if there are options, set them\n if fullkey.group(1):\n options = fullkey.group(1).split(',')\n # key is of format: {enc} {key} [comment]\n comps = fullkey.group(2).split(None, 2)\n enc = comps[0]\n name = comps[1]\n if len(comps) == 3:\n comment = comps[2]\n\n if __opts__['test']:\n ret['result'], ret['comment'] = _present_test(\n user,\n name,\n enc,\n comment,\n options or [],\n source,\n config,\n fingerprint_hash_type)\n return ret\n\n # Get only the path to the file without env referrences to check if exists\n if source != '':\n source_path = __salt__['cp.get_url'](\n source,\n None,\n saltenv=__env__)\n\n if source != '' and not source_path:\n data = 'no key'\n elif source != '' and source_path:\n key = __salt__['cp.get_file_str'](\n source,\n saltenv=__env__)\n filehasoptions = False\n # check if this is of form {options} {enc} {key} {comment}\n sshre = re.compile(r'^(ssh\\-|ecds).*')\n key = key.rstrip().split('\\n')\n for keyline in key:\n filehasoptions = sshre.match(keyline)\n if not filehasoptions:\n data = __salt__['ssh.set_auth_key_from_file'](\n user,\n source,\n config=config,\n saltenv=__env__,\n fingerprint_hash_type=fingerprint_hash_type)\n else:\n # Split keyline to get key and comment\n keyline = keyline.split(' ')\n key_type = keyline[0]\n key_value = keyline[1]\n key_comment = keyline[2] if len(keyline) > 2 else ''\n data = __salt__['ssh.set_auth_key'](\n user,\n key_value,\n enc=key_type,\n comment=key_comment,\n options=options or [],\n config=config,\n fingerprint_hash_type=fingerprint_hash_type)\n else:\n data = __salt__['ssh.set_auth_key'](\n user,\n name,\n enc=enc,\n comment=comment,\n options=options or [],\n config=config,\n fingerprint_hash_type=fingerprint_hash_type)\n\n if data == 'replace':\n ret['changes'][name] = 'Updated'\n ret['comment'] = ('The authorized host key {0} for user {1} was '\n 'updated'.format(name, user))\n return ret\n elif data == 'no change':\n ret['comment'] = ('The authorized host key {0} is already present '\n 'for user {1}'.format(name, user))\n elif data == 'new':\n ret['changes'][name] = 'New'\n ret['comment'] = ('The authorized host key {0} for user {1} was added'\n .format(name, user))\n elif data == 'no key':\n ret['result'] = False\n ret['comment'] = ('Failed to add the ssh key. Source file {0} is '\n 'missing'.format(source))\n elif data == 'fail':\n ret['result'] = False\n err = sys.modules[\n __salt__['test.ping'].__module__\n ].__context__.pop('ssh_auth.error', None)\n if err:\n ret['comment'] = err\n else:\n ret['comment'] = ('Failed to add the ssh key. Is the home '\n 'directory available, and/or does the key file '\n 'exist?')\n elif data == 'invalid' or data == 'Invalid public key':\n ret['result'] = False\n ret['comment'] = 'Invalid public ssh key, most likely has spaces or invalid syntax'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring that the specified SSH key is absent.", "response": "def absent(name,\n user,\n enc='ssh-rsa',\n comment='',\n source='',\n options=None,\n config='.ssh/authorized_keys',\n fingerprint_hash_type=None):\n '''\n Verifies that the specified SSH key is absent\n\n name\n The SSH key to manage\n\n user\n The user who owns the SSH authorized keys file to modify\n\n enc\n Defines what type of key is being used; can be ed25519, ecdsa, ssh-rsa\n or ssh-dss\n\n comment\n The comment to be placed with the SSH public key\n\n options\n The options passed to the key, pass a list object\n\n source\n The source file for the key(s). Can contain any number of public keys,\n in standard \"authorized_keys\" format. If this is set, comment, enc and\n options will be ignored.\n\n .. versionadded:: 2015.8.0\n\n config\n The location of the authorized keys file relative to the user's home\n directory, defaults to \".ssh/authorized_keys\". Token expansion %u and\n %h for username and home path supported.\n\n fingerprint_hash_type\n The public key fingerprint hash type that the public key fingerprint\n was originally hashed with. This defaults to ``sha256`` if not specified.\n\n .. versionadded:: 2016.11.7\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n if __opts__['test']:\n ret['result'], ret['comment'] = _absent_test(\n user,\n name,\n enc,\n comment,\n options or [],\n source,\n config,\n fingerprint_hash_type)\n return ret\n\n # Extract Key from file if source is present\n if source != '':\n key = __salt__['cp.get_file_str'](\n source,\n saltenv=__env__)\n filehasoptions = False\n # check if this is of form {options} {enc} {key} {comment}\n sshre = re.compile(r'^(ssh\\-|ecds).*')\n key = key.rstrip().split('\\n')\n for keyline in key:\n filehasoptions = sshre.match(keyline)\n if not filehasoptions:\n ret['comment'] = __salt__['ssh.rm_auth_key_from_file'](user,\n source,\n config,\n saltenv=__env__,\n fingerprint_hash_type=fingerprint_hash_type)\n else:\n # Split keyline to get key\n keyline = keyline.split(' ')\n ret['comment'] = __salt__['ssh.rm_auth_key'](user,\n keyline[1],\n config=config,\n fingerprint_hash_type=fingerprint_hash_type)\n else:\n # Get just the key\n sshre = re.compile(r'^(.*?)\\s?((?:ssh\\-|ecds)[\\w-]+\\s.+)$')\n fullkey = sshre.search(name)\n # if it is {key} [comment]\n if not fullkey:\n key_and_comment = name.split(None, 1)\n name = key_and_comment[0]\n if len(key_and_comment) == 2:\n comment = key_and_comment[1]\n else:\n # if there are options, set them\n if fullkey.group(1):\n options = fullkey.group(1).split(',')\n # key is of format: {enc} {key} [comment]\n comps = fullkey.group(2).split()\n enc = comps[0]\n name = comps[1]\n if len(comps) == 3:\n comment = comps[2]\n ret['comment'] = __salt__['ssh.rm_auth_key'](user,\n name,\n config=config,\n fingerprint_hash_type=fingerprint_hash_type)\n\n if ret['comment'] == 'User authorized keys file not present':\n ret['result'] = False\n return ret\n elif ret['comment'] == 'Key removed':\n ret['changes'][name] = 'Removed'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmanage the contents of the specified SSH authorized keys file.", "response": "def manage(\n name,\n ssh_keys,\n user,\n enc='ssh-rsa',\n comment='',\n source='',\n options=None,\n config='.ssh/authorized_keys',\n fingerprint_hash_type=None,\n **kwargs):\n '''\n .. versionadded:: Neon\n\n Ensures that only the specified ssh_keys are present for the specified user\n\n ssh_keys\n The SSH key to manage\n\n user\n The user who owns the SSH authorized keys file to modify\n\n enc\n Defines what type of key is being used; can be ed25519, ecdsa, ssh-rsa\n or ssh-dss\n\n comment\n The comment to be placed with the SSH public key\n\n source\n The source file for the key(s). Can contain any number of public keys,\n in standard \"authorized_keys\" format. If this is set, comment and enc\n will be ignored.\n\n .. note::\n The source file must contain keys in the format `` \n ``. If you have generated a keypair using PuTTYgen, then you\n will need to do the following to retrieve an OpenSSH-compatible public\n key.\n\n 1. In PuTTYgen, click ``Load``, and select the *private* key file (not\n the public key), and click ``Open``.\n 2. Copy the public key from the box labeled ``Public key for pasting\n into OpenSSH authorized_keys file``.\n 3. Paste it into a new file.\n\n options\n The options passed to the keys, pass a list object\n\n config\n The location of the authorized keys file relative to the user's home\n directory, defaults to \".ssh/authorized_keys\". Token expansion %u and\n %h for username and home path supported.\n\n fingerprint_hash_type\n The public key fingerprint hash type that the public key fingerprint\n was originally hashed with. This defaults to ``sha256`` if not specified.\n '''\n ret = {'name': '',\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n all_potential_keys = []\n for ssh_key in ssh_keys:\n # gather list potential ssh keys for removal comparison\n # options, enc, and comments could be in the mix\n all_potential_keys.extend(ssh_key.split(' '))\n existing_keys = __salt__['ssh.auth_keys'](user=user).keys()\n remove_keys = set(existing_keys).difference(all_potential_keys)\n for remove_key in remove_keys:\n if __opts__['test']:\n remove_comment = '{0} Key set for removal'.format(remove_key)\n ret['comment'] = remove_comment\n ret['result'] = None\n else:\n remove_comment = absent(remove_key, user)['comment']\n ret['changes'][remove_key] = remove_comment\n\n for ssh_key in ssh_keys:\n run_return = present(ssh_key, user, enc, comment, source,\n options, config, fingerprint_hash_type, **kwargs)\n if run_return['changes']:\n ret['changes'].update(run_return['changes'])\n else:\n ret['comment'] += '\\n' + run_return['comment']\n ret['comment'].strip()\n\n if run_return['result'] is None:\n ret['result'] = None\n elif not run_return['result']:\n ret['result'] = False\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _action(action='get', search=None, one=True, force=False):\n '''\n Multi action helper for start, stop, get, ...\n '''\n vms = {}\n matched_vms = []\n client = salt.client.get_local_client(__opts__['conf_file'])\n\n ## lookup vms\n try:\n vmadm_args = {}\n vmadm_args['order'] = 'uuid,alias,hostname,state'\n if '=' in search:\n vmadm_args['search'] = search\n for cn in client.cmd_iter('G@virtual:physical and G@os:smartos',\n 'vmadm.list', kwarg=vmadm_args,\n tgt_type='compound'):\n if not cn:\n continue\n node = next(six.iterkeys(cn))\n if not isinstance(cn[node], dict) or \\\n 'ret' not in cn[node] or \\\n not isinstance(cn[node]['ret'], dict):\n continue\n for vm in cn[node]['ret']:\n vmcfg = cn[node]['ret'][vm]\n vmcfg['node'] = node\n vms[vm] = vmcfg\n except SaltClientError as client_error:\n pass\n\n ## check if we have vms\n if not vms:\n return {'Error': 'No vms found.'}\n\n ## simple search\n if '=' not in search:\n loop_pass = 0\n while loop_pass < 3:\n ## each pass will try a different field\n if loop_pass == 0:\n field = 'uuid'\n elif loop_pass == 1:\n field = 'hostname'\n else:\n field = 'alias'\n\n ## loop vms and try to match\n for vm in vms:\n if field == 'uuid' and vm == search:\n matched_vms.append(vm)\n break # exit for on uuid match (max = 1)\n elif field in vms[vm] and vms[vm][field] == search:\n matched_vms.append(vm)\n\n ## exit on match(es) or try again\n if matched_vms:\n break\n else:\n loop_pass += 1\n else:\n for vm in vms:\n matched_vms.append(vm)\n\n ## check if we have vms\n if not matched_vms:\n return {'Error': 'No vms matched.'}\n\n ## multiple allowed?\n if one and len(matched_vms) > 1:\n return {\n 'Error': 'Matched {0} vms, only one allowed!'.format(len(matched_vms)),\n 'Matches': matched_vms\n }\n\n ## perform action\n ret = {}\n if action in ['start', 'stop', 'reboot', 'get']:\n for vm in matched_vms:\n vmadm_args = {\n 'key': 'uuid',\n 'vm': vm\n }\n try:\n for vmadm_res in client.cmd_iter(vms[vm]['node'], 'vmadm.{0}'.format(action), kwarg=vmadm_args):\n if not vmadm_res:\n continue\n if vms[vm]['node'] in vmadm_res:\n ret[vm] = vmadm_res[vms[vm]['node']]['ret']\n except SaltClientError as client_error:\n ret[vm] = False\n elif action in ['is_running']:\n ret = True\n for vm in matched_vms:\n if vms[vm]['state'] != 'running':\n ret = False\n break\n return ret", "response": "Multi action helper for start stop get"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nlists all compute nodes in the cluster", "response": "def nodes(verbose=False):\n '''\n List all compute nodes\n\n verbose : boolean\n print additional information about the node\n e.g. platform version, hvm capable, ...\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run vmadm.nodes\n salt-run vmadm.nodes verbose=True\n '''\n ret = {} if verbose else []\n client = salt.client.get_local_client(__opts__['conf_file'])\n\n ## get list of nodes\n try:\n for cn in client.cmd_iter('G@virtual:physical and G@os:smartos',\n 'grains.items', tgt_type='compound'):\n if not cn:\n continue\n node = next(six.iterkeys(cn))\n if not isinstance(cn[node], dict) or \\\n 'ret' not in cn[node] or \\\n not isinstance(cn[node]['ret'], dict):\n continue\n if verbose:\n ret[node] = {}\n ret[node]['version'] = {}\n ret[node]['version']['platform'] = cn[node]['ret']['osrelease']\n if 'computenode_sdc_version' in cn[node]['ret']:\n ret[node]['version']['sdc'] = cn[node]['ret']['computenode_sdc_version']\n ret[node]['vms'] = {}\n if 'computenode_vm_capable' in cn[node]['ret'] and \\\n cn[node]['ret']['computenode_vm_capable'] and \\\n 'computenode_vm_hw_virt' in cn[node]['ret']:\n ret[node]['vms']['hw_cap'] = cn[node]['ret']['computenode_vm_hw_virt']\n else:\n ret[node]['vms']['hw_cap'] = False\n if 'computenode_vms_running' in cn[node]['ret']:\n ret[node]['vms']['running'] = cn[node]['ret']['computenode_vms_running']\n else:\n ret.append(node)\n except SaltClientError as client_error:\n return \"{0}\".format(client_error)\n\n if not verbose:\n ret.sort()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nlist all vms in the virtual machine", "response": "def list_vms(search=None, verbose=False):\n '''\n List all vms\n\n search : string\n filter vms, see the execution module\n verbose : boolean\n print additional information about the vm\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run vmadm.list\n salt-run vmadm.list search='type=KVM'\n salt-run vmadm.list verbose=True\n '''\n ret = OrderedDict() if verbose else []\n client = salt.client.get_local_client(__opts__['conf_file'])\n try:\n vmadm_args = {}\n vmadm_args['order'] = 'uuid,alias,hostname,state,type,cpu_cap,vcpus,ram'\n if search:\n vmadm_args['search'] = search\n for cn in client.cmd_iter('G@virtual:physical and G@os:smartos',\n 'vmadm.list', kwarg=vmadm_args,\n tgt_type='compound'):\n if not cn:\n continue\n node = next(six.iterkeys(cn))\n if not isinstance(cn[node], dict) or \\\n 'ret' not in cn[node] or \\\n not isinstance(cn[node]['ret'], dict):\n continue\n for vm in cn[node]['ret']:\n vmcfg = cn[node]['ret'][vm]\n if verbose:\n ret[vm] = OrderedDict()\n ret[vm]['hostname'] = vmcfg['hostname']\n ret[vm]['alias'] = vmcfg['alias']\n ret[vm]['computenode'] = node\n ret[vm]['state'] = vmcfg['state']\n ret[vm]['resources'] = OrderedDict()\n ret[vm]['resources']['memory'] = vmcfg['ram']\n if vmcfg['type'] == 'KVM':\n ret[vm]['resources']['cpu'] = \"{0:.2f}\".format(int(vmcfg['vcpus']))\n else:\n if vmcfg['cpu_cap'] != '':\n ret[vm]['resources']['cpu'] = \"{0:.2f}\".format(int(vmcfg['cpu_cap'])/100)\n else:\n ret.append(vm)\n except SaltClientError as client_error:\n return \"{0}\".format(client_error)\n\n if not verbose:\n ret = sorted(ret)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef reboot(search, one=True, force=False):\n '''\n Reboot one or more vms\n\n search : string\n filter vms, see the execution module.\n one : boolean\n reboot only one vm\n force : boolean\n force reboot, faster but no graceful shutdown\n\n .. note::\n If the search parameter does not contain an equal (=) symbol it will be\n assumed it will be tried as uuid, hostname, and alias.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run vmadm.reboot 91244bba-1146-e4ec-c07e-e825e0223aa9\n salt-run vmadm.reboot search='alias=marije'\n salt-run vmadm.reboot search='type=KVM' one=False\n '''\n return _action('reboot', search, one, force)", "response": "Reboot one or more virtual machines"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconverting an XML tree into a dict", "response": "def _xml_to_dict(xmltree):\n '''\n Convert an XML tree into a dict\n '''\n if sys.version_info < (2, 7):\n children_len = len(xmltree.getchildren())\n else:\n children_len = len(xmltree)\n\n if children_len < 1:\n name = xmltree.tag\n if '}' in name:\n comps = name.split('}')\n name = comps[1]\n return {name: xmltree.text}\n\n xmldict = {}\n for item in xmltree:\n name = item.tag\n if '}' in name:\n comps = name.split('}')\n name = comps[1]\n if name not in xmldict:\n if sys.version_info < (2, 7):\n children_len = len(item.getchildren())\n else:\n children_len = len(item)\n\n if children_len > 0:\n xmldict[name] = _xml_to_dict(item)\n else:\n xmldict[name] = item.text\n else:\n if not isinstance(xmldict[name], list):\n tempvar = xmldict[name]\n xmldict[name] = []\n xmldict[name].append(tempvar)\n xmldict[name].append(_xml_to_dict(item))\n return xmldict"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef optimize_providers(providers):\n '''\n Return an optimized list of providers.\n\n We want to reduce the duplication of querying\n the same region.\n\n If a provider is using the same credentials for the same region\n the same data will be returned for each provider, thus causing\n un-wanted duplicate data and API calls to EC2.\n\n '''\n tmp_providers = {}\n optimized_providers = {}\n\n for name, data in six.iteritems(providers):\n if 'location' not in data:\n data['location'] = DEFAULT_LOCATION\n\n if data['location'] not in tmp_providers:\n tmp_providers[data['location']] = {}\n\n creds = (data['id'], data['key'])\n if creds not in tmp_providers[data['location']]:\n tmp_providers[data['location']][creds] = {'name': name,\n 'data': data,\n }\n\n for location, tmp_data in six.iteritems(tmp_providers):\n for creds, data in six.iteritems(tmp_data):\n _id, _key = creds\n _name = data['name']\n _data = data['data']\n if _name not in optimized_providers:\n optimized_providers[_name] = _data\n\n return optimized_providers", "response": "Return an optimized list of providers."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _wait_for_spot_instance(update_callback,\n update_args=None,\n update_kwargs=None,\n timeout=10 * 60,\n interval=30,\n interval_multiplier=1,\n max_failures=10):\n '''\n Helper function that waits for a spot instance request to become active\n for a specific maximum amount of time.\n\n :param update_callback: callback function which queries the cloud provider\n for spot instance request. It must return None if\n the required data, running instance included, is\n not available yet.\n :param update_args: Arguments to pass to update_callback\n :param update_kwargs: Keyword arguments to pass to update_callback\n :param timeout: The maximum amount of time(in seconds) to wait for the IP\n address.\n :param interval: The looping interval, i.e., the amount of time to sleep\n before the next iteration.\n :param interval_multiplier: Increase the interval by this multiplier after\n each request; helps with throttling\n :param max_failures: If update_callback returns ``False`` it's considered\n query failure. This value is the amount of failures\n accepted before giving up.\n :returns: The update_callback returned data\n :raises: SaltCloudExecutionTimeout\n\n '''\n if update_args is None:\n update_args = ()\n if update_kwargs is None:\n update_kwargs = {}\n\n duration = timeout\n while True:\n log.debug(\n 'Waiting for spot instance reservation. Giving up in '\n '00:%02d:%02d', int(timeout // 60), int(timeout % 60)\n )\n data = update_callback(*update_args, **update_kwargs)\n if data is False:\n log.debug(\n 'update_callback has returned False which is considered a '\n 'failure. Remaining Failures: %s', max_failures\n )\n max_failures -= 1\n if max_failures <= 0:\n raise SaltCloudExecutionFailure(\n 'Too many failures occurred while waiting for '\n 'the spot instance reservation to become active.'\n )\n elif data is not None:\n return data\n\n if timeout < 0:\n raise SaltCloudExecutionTimeout(\n 'Unable to get an active spot instance request for '\n '00:{0:02d}:{1:02d}'.format(\n int(duration // 60),\n int(duration % 60)\n )\n )\n time.sleep(interval)\n timeout -= interval\n\n if interval_multiplier > 1:\n interval *= interval_multiplier\n if interval > timeout:\n interval = timeout + 1\n log.info('Interval multiplier in effect; interval is '\n 'now %ss', interval)", "response": "Helper function that waits for a spot instance request to become active."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a dict of all available VM images on the cloud provider.", "response": "def avail_images(kwargs=None, call=None):\n '''\n Return a dict of all available VM images on the cloud provider.\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_images function must be called with '\n '-f or --function, or with the --list-images option'\n )\n\n if not isinstance(kwargs, dict):\n kwargs = {}\n\n if 'owner' in kwargs:\n owner = kwargs['owner']\n else:\n provider = get_configured_provider()\n\n owner = config.get_cloud_config_value(\n 'owner', provider, __opts__, default='amazon'\n )\n\n ret = {}\n params = {'Action': 'DescribeImages',\n 'Owner': owner}\n images = aws.query(params,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n for image in images:\n ret[image['imageId']] = image\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef ssh_interface(vm_):\n '''\n Return the ssh_interface type to connect to. Either 'public_ips' (default)\n or 'private_ips'.\n '''\n ret = config.get_cloud_config_value(\n 'ssh_interface', vm_, __opts__, default='public_ips',\n search_global=False\n )\n if ret not in ('public_ips', 'private_ips'):\n log.warning(\n 'Invalid ssh_interface: %s. '\n 'Allowed options are (\"public_ips\", \"private_ips\"). '\n 'Defaulting to \"public_ips\".', ret\n )\n ret = 'public_ips'\n return ret", "response": "Return the ssh_interface type to connect to. Either public_ips or private_ips."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_ssh_gateway_config(vm_):\n '''\n Return the ssh_gateway configuration.\n '''\n ssh_gateway = config.get_cloud_config_value(\n 'ssh_gateway', vm_, __opts__, default=None,\n search_global=False\n )\n\n # Check to see if a SSH Gateway will be used.\n if not isinstance(ssh_gateway, six.string_types):\n return None\n\n # Create dictionary of configuration items\n\n # ssh_gateway\n ssh_gateway_config = {'ssh_gateway': ssh_gateway}\n\n # ssh_gateway_port\n ssh_gateway_config['ssh_gateway_port'] = config.get_cloud_config_value(\n 'ssh_gateway_port', vm_, __opts__, default=None,\n search_global=False\n )\n\n # ssh_gateway_username\n ssh_gateway_config['ssh_gateway_user'] = config.get_cloud_config_value(\n 'ssh_gateway_username', vm_, __opts__, default=None,\n search_global=False\n )\n\n # ssh_gateway_private_key\n ssh_gateway_config['ssh_gateway_key'] = config.get_cloud_config_value(\n 'ssh_gateway_private_key', vm_, __opts__, default=None,\n search_global=False\n )\n\n # ssh_gateway_password\n ssh_gateway_config['ssh_gateway_password'] = config.get_cloud_config_value(\n 'ssh_gateway_password', vm_, __opts__, default=None,\n search_global=False\n )\n\n # ssh_gateway_command\n ssh_gateway_config['ssh_gateway_command'] = config.get_cloud_config_value(\n 'ssh_gateway_command', vm_, __opts__, default=None,\n search_global=False\n )\n\n # Check if private key exists\n key_filename = ssh_gateway_config['ssh_gateway_key']\n if key_filename is not None and not os.path.isfile(key_filename):\n raise SaltCloudConfigError(\n 'The defined ssh_gateway_private_key \\'{0}\\' does not exist'\n .format(key_filename)\n )\n elif (\n key_filename is None and\n not ssh_gateway_config['ssh_gateway_password']\n ):\n raise SaltCloudConfigError(\n 'No authentication method. Please define: '\n ' ssh_gateway_password or ssh_gateway_private_key'\n )\n\n return ssh_gateway_config", "response": "Return the ssh_gateway configuration."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a list of available locations for the current user", "response": "def avail_locations(call=None):\n '''\n List all available 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 ret = {}\n\n params = {'Action': 'DescribeRegions'}\n result = aws.query(params,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n for region in result:\n ret[region['regionName']] = {\n 'name': region['regionName'],\n 'endpoint': region['regionEndpoint'],\n }\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the availability zone to use", "response": "def get_availability_zone(vm_):\n '''\n Return the availability zone to use\n '''\n avz = config.get_cloud_config_value(\n 'availability_zone', vm_, __opts__, search_global=False\n )\n\n if avz is None:\n return None\n\n zones = _list_availability_zones(vm_)\n\n # Validate user-specified AZ\n if avz not in zones:\n raise SaltCloudException(\n 'The specified availability zone isn\\'t valid in this region: '\n '{0}\\n'.format(\n avz\n )\n )\n\n # check specified AZ is available\n elif zones[avz] != 'available':\n raise SaltCloudException(\n 'The specified availability zone isn\\'t currently available: '\n '{0}\\n'.format(\n avz\n )\n )\n\n return avz"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_imageid(vm_):\n '''\n Returns the ImageId to use\n '''\n image = config.get_cloud_config_value(\n 'image', vm_, __opts__, search_global=False\n )\n if image.startswith('ami-'):\n return image\n # a poor man's cache\n if not hasattr(get_imageid, 'images'):\n get_imageid.images = {}\n elif image in get_imageid.images:\n return get_imageid.images[image]\n params = {'Action': 'DescribeImages',\n 'Filter.0.Name': 'name',\n 'Filter.0.Value.0': image}\n # Query AWS, sort by 'creationDate' and get the last imageId\n _t = lambda x: datetime.datetime.strptime(x['creationDate'], '%Y-%m-%dT%H:%M:%S.%fZ')\n image_id = sorted(aws.query(params, location=get_location(),\n provider=get_provider(), opts=__opts__, sigver='4'),\n lambda i, j: salt.utils.compat.cmp(_t(i), _t(j))\n )[-1]['imageId']\n get_imageid.images[image] = image_id\n return image_id", "response": "Returns the ImageId to use for a resource in the cache"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_subnetname_id(subnetname):\n '''\n Returns the SubnetId of a SubnetName to use\n '''\n params = {'Action': 'DescribeSubnets'}\n for subnet in aws.query(params, location=get_location(),\n provider=get_provider(), opts=__opts__, sigver='4'):\n tags = subnet.get('tagSet', {}).get('item', {})\n if not isinstance(tags, list):\n tags = [tags]\n for tag in tags:\n if tag['key'] == 'Name' and tag['value'] == subnetname:\n log.debug(\n 'AWS Subnet ID of %s is %s',\n subnetname, subnet['subnetId']\n )\n return subnet['subnetId']\n return None", "response": "Returns the SubnetId of a SubnetName to use\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the SubnetId to use", "response": "def get_subnetid(vm_):\n '''\n Returns the SubnetId to use\n '''\n subnetid = config.get_cloud_config_value(\n 'subnetid', vm_, __opts__, search_global=False\n )\n if subnetid:\n return subnetid\n\n subnetname = config.get_cloud_config_value(\n 'subnetname', vm_, __opts__, search_global=False\n )\n if subnetname:\n return _get_subnetname_id(subnetname)\n return None"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_securitygroupname_id(securitygroupname_list):\n '''\n Returns the SecurityGroupId of a SecurityGroupName to use\n '''\n securitygroupid_set = set()\n if not isinstance(securitygroupname_list, list):\n securitygroupname_list = [securitygroupname_list]\n params = {'Action': 'DescribeSecurityGroups'}\n for sg in aws.query(params, location=get_location(),\n provider=get_provider(), opts=__opts__, sigver='4'):\n if sg['groupName'] in securitygroupname_list:\n log.debug(\n 'AWS SecurityGroup ID of %s is %s',\n sg['groupName'], sg['groupId']\n )\n securitygroupid_set.add(sg['groupId'])\n return list(securitygroupid_set)", "response": "Returns the SecurityGroupId of a SecurityGroupName to use\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the SecurityGroupId of the current resource", "response": "def securitygroupid(vm_):\n '''\n Returns the SecurityGroupId\n '''\n securitygroupid_set = set()\n securitygroupid_list = config.get_cloud_config_value(\n 'securitygroupid',\n vm_,\n __opts__,\n search_global=False\n )\n # If the list is None, then the set will remain empty\n # If the list is already a set then calling 'set' on it is a no-op\n # If the list is a string, then calling 'set' generates a one-element set\n # If the list is anything else, stacktrace\n if securitygroupid_list:\n securitygroupid_set = securitygroupid_set.union(set(securitygroupid_list))\n\n securitygroupname_list = config.get_cloud_config_value(\n 'securitygroupname', vm_, __opts__, search_global=False\n )\n if securitygroupname_list:\n if not isinstance(securitygroupname_list, list):\n securitygroupname_list = [securitygroupname_list]\n params = {'Action': 'DescribeSecurityGroups'}\n for sg in aws.query(params, location=get_location(),\n provider=get_provider(), opts=__opts__, sigver='4'):\n if sg['groupName'] in securitygroupname_list:\n log.debug(\n 'AWS SecurityGroup ID of %s is %s',\n sg['groupName'], sg['groupId']\n )\n securitygroupid_set.add(sg['groupId'])\n return list(securitygroupid_set)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_provider(vm_=None):\n '''\n Extract the provider name from vm\n '''\n if vm_ is None:\n provider = __active_provider_name__ or 'ec2'\n else:\n provider = vm_.get('provider', 'ec2')\n\n if ':' in provider:\n prov_comps = provider.split(':')\n provider = prov_comps[0]\n return provider", "response": "Extract the provider name from vm\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _list_availability_zones(vm_=None):\n '''\n List all availability zones in the current region\n '''\n ret = {}\n\n params = {'Action': 'DescribeAvailabilityZones',\n 'Filter.0.Name': 'region-name',\n 'Filter.0.Value.0': get_location(vm_)}\n result = aws.query(params,\n location=get_location(vm_),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n for zone in result:\n ret[zone['zoneName']] = zone['zoneState']\n\n return ret", "response": "List all availability zones in the current region"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _request_eip(interface, vm_):\n '''\n Request and return Elastic IP\n '''\n params = {'Action': 'AllocateAddress'}\n params['Domain'] = interface.setdefault('domain', 'vpc')\n eips = aws.query(params,\n return_root=True,\n location=get_location(vm_),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n for eip in eips:\n if 'allocationId' in eip:\n return eip['allocationId']\n return None", "response": "Request and return Elastic IP"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _create_eni_if_necessary(interface, vm_):\n '''\n Create an Elastic Interface if necessary and return a Network Interface Specification\n '''\n if 'NetworkInterfaceId' in interface and interface['NetworkInterfaceId'] is not None:\n return {'DeviceIndex': interface['DeviceIndex'],\n 'NetworkInterfaceId': interface['NetworkInterfaceId']}\n\n params = {'Action': 'DescribeSubnets'}\n subnet_query = aws.query(params,\n return_root=True,\n location=get_location(vm_),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n if 'SecurityGroupId' not in interface and 'securitygroupname' in interface:\n interface['SecurityGroupId'] = _get_securitygroupname_id(interface['securitygroupname'])\n if 'SubnetId' not in interface and 'subnetname' in interface:\n interface['SubnetId'] = _get_subnetname_id(interface['subnetname'])\n\n subnet_id = _get_subnet_id_for_interface(subnet_query, interface)\n if not subnet_id:\n raise SaltCloudConfigError(\n 'No such subnet <{0}>'.format(interface.get('SubnetId'))\n )\n params = {'SubnetId': subnet_id}\n\n for k in 'Description', 'PrivateIpAddress', 'SecondaryPrivateIpAddressCount':\n if k in interface:\n params[k] = interface[k]\n\n for k in 'PrivateIpAddresses', 'SecurityGroupId':\n if k in interface:\n params.update(_param_from_config(k, interface[k]))\n\n if 'AssociatePublicIpAddress' in interface:\n # Associating a public address in a VPC only works when the interface is not\n # created beforehand, but as a part of the machine creation request.\n for k in ('DeviceIndex', 'AssociatePublicIpAddress', 'NetworkInterfaceId'):\n if k in interface:\n params[k] = interface[k]\n params['DeleteOnTermination'] = interface.get('delete_interface_on_terminate', True)\n return params\n\n params['Action'] = 'CreateNetworkInterface'\n\n result = aws.query(params,\n return_root=True,\n location=get_location(vm_),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n eni_desc = result[1]\n if not eni_desc or not eni_desc.get('networkInterfaceId'):\n raise SaltCloudException('Failed to create interface: {0}'.format(result))\n\n eni_id = eni_desc.get('networkInterfaceId')\n log.debug(\n 'Created network interface %s inst %s',\n eni_id, interface['DeviceIndex']\n )\n\n associate_public_ip = interface.get('AssociatePublicIpAddress', False)\n if isinstance(associate_public_ip, six.string_types):\n # Assume id of EIP as value\n _associate_eip_with_interface(eni_id, associate_public_ip, vm_=vm_)\n\n if interface.get('associate_eip'):\n _associate_eip_with_interface(eni_id, interface.get('associate_eip'), vm_=vm_)\n elif interface.get('allocate_new_eip'):\n _new_eip = _request_eip(interface, vm_)\n _associate_eip_with_interface(eni_id, _new_eip, vm_=vm_)\n elif interface.get('allocate_new_eips'):\n addr_list = _list_interface_private_addrs(eni_desc)\n eip_list = []\n for idx, addr in enumerate(addr_list):\n eip_list.append(_request_eip(interface, vm_))\n for idx, addr in enumerate(addr_list):\n _associate_eip_with_interface(eni_id, eip_list[idx], addr, vm_=vm_)\n\n if 'Name' in interface:\n tag_params = {'Action': 'CreateTags',\n 'ResourceId.0': eni_id,\n 'Tag.0.Key': 'Name',\n 'Tag.0.Value': interface['Name']}\n tag_response = aws.query(tag_params,\n return_root=True,\n location=get_location(vm_),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n if 'error' in tag_response:\n log.error('Failed to set name of interface {0}')\n\n return {'DeviceIndex': interface['DeviceIndex'],\n 'NetworkInterfaceId': eni_id}", "response": "Create an Elastic Interface if necessary and return a Network Interface Specification"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _list_interface_private_addrs(eni_desc):\n '''\n Returns a list of all of the private IP addresses attached to a\n network interface. The 'primary' address will be listed first.\n '''\n primary = eni_desc.get('privateIpAddress')\n if not primary:\n return None\n\n addresses = [primary]\n\n lst = eni_desc.get('privateIpAddressesSet', {}).get('item', [])\n if not isinstance(lst, list):\n return addresses\n\n for entry in lst:\n if entry.get('primary') == 'true':\n continue\n if entry.get('privateIpAddress'):\n addresses.append(entry.get('privateIpAddress'))\n\n return addresses", "response": "Returns a list of all of the private IP addresses attached to a\n network interface. The primary address will be listed first."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmodify properties of the interface with id eni_id to the values in properties dict", "response": "def _modify_eni_properties(eni_id, properties=None, vm_=None):\n '''\n Change properties of the interface\n with id eni_id to the values in properties dict\n '''\n if not isinstance(properties, dict):\n raise SaltCloudException(\n 'ENI properties must be a dictionary'\n )\n\n params = {'Action': 'ModifyNetworkInterfaceAttribute',\n 'NetworkInterfaceId': eni_id}\n for k, v in six.iteritems(properties):\n params[k] = v\n\n result = aws.query(params,\n return_root=True,\n location=get_location(vm_),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n if isinstance(result, dict) and result.get('error'):\n raise SaltCloudException(\n 'Could not change interface <{0}> attributes <\\'{1}\\'>'.format(\n eni_id, properties\n )\n )\n else:\n return result"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _associate_eip_with_interface(eni_id, eip_id, private_ip=None, vm_=None):\n '''\n Accept the id of a network interface, and the id of an elastic ip\n address, and associate the two of them, such that traffic sent to the\n elastic ip address will be forwarded (NATted) to this network interface.\n\n Optionally specify the private (10.x.x.x) IP address that traffic should\n be NATted to - useful if you have multiple IP addresses assigned to an\n interface.\n '''\n params = {'Action': 'AssociateAddress',\n 'NetworkInterfaceId': eni_id,\n 'AllocationId': eip_id}\n\n if private_ip:\n params['PrivateIpAddress'] = private_ip\n\n result = aws.query(params,\n return_root=True,\n location=get_location(vm_),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n if not result[2].get('associationId'):\n raise SaltCloudException(\n 'Could not associate elastic ip address '\n '<{0}> with network interface <{1}>'.format(\n eip_id, eni_id\n )\n )\n\n log.debug(\n 'Associated ElasticIP address %s with interface %s',\n eip_id, eni_id\n )\n\n return result[2].get('associationId')", "response": "Associate an elastic ip with an elastic ip."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns EC2 API parameters based on the given config data.", "response": "def _param_from_config(key, data):\n '''\n Return EC2 API parameters based on the given config data.\n\n Examples:\n 1. List of dictionaries\n >>> data = [\n ... {'DeviceIndex': 0, 'SubnetId': 'subid0',\n ... 'AssociatePublicIpAddress': True},\n ... {'DeviceIndex': 1,\n ... 'SubnetId': 'subid1',\n ... 'PrivateIpAddress': '192.168.1.128'}\n ... ]\n >>> _param_from_config('NetworkInterface', data)\n ... {'NetworkInterface.0.SubnetId': 'subid0',\n ... 'NetworkInterface.0.DeviceIndex': 0,\n ... 'NetworkInterface.1.SubnetId': 'subid1',\n ... 'NetworkInterface.1.PrivateIpAddress': '192.168.1.128',\n ... 'NetworkInterface.0.AssociatePublicIpAddress': 'true',\n ... 'NetworkInterface.1.DeviceIndex': 1}\n\n 2. List of nested dictionaries\n >>> data = [\n ... {'DeviceName': '/dev/sdf',\n ... 'Ebs': {\n ... 'SnapshotId': 'dummy0',\n ... 'VolumeSize': 200,\n ... 'VolumeType': 'standard'}},\n ... {'DeviceName': '/dev/sdg',\n ... 'Ebs': {\n ... 'SnapshotId': 'dummy1',\n ... 'VolumeSize': 100,\n ... 'VolumeType': 'standard'}}\n ... ]\n >>> _param_from_config('BlockDeviceMapping', data)\n ... {'BlockDeviceMapping.0.Ebs.VolumeType': 'standard',\n ... 'BlockDeviceMapping.1.Ebs.SnapshotId': 'dummy1',\n ... 'BlockDeviceMapping.0.Ebs.VolumeSize': 200,\n ... 'BlockDeviceMapping.0.Ebs.SnapshotId': 'dummy0',\n ... 'BlockDeviceMapping.1.Ebs.VolumeType': 'standard',\n ... 'BlockDeviceMapping.1.DeviceName': '/dev/sdg',\n ... 'BlockDeviceMapping.1.Ebs.VolumeSize': 100,\n ... 'BlockDeviceMapping.0.DeviceName': '/dev/sdf'}\n\n 3. Dictionary of dictionaries\n >>> data = { 'Arn': 'dummyarn', 'Name': 'Tester' }\n >>> _param_from_config('IamInstanceProfile', data)\n {'IamInstanceProfile.Arn': 'dummyarn', 'IamInstanceProfile.Name': 'Tester'}\n\n '''\n\n param = {}\n\n if isinstance(data, dict):\n for k, v in six.iteritems(data):\n param.update(_param_from_config('{0}.{1}'.format(key, k), v))\n\n elif isinstance(data, list) or isinstance(data, tuple):\n for idx, conf_item in enumerate(data):\n prefix = '{0}.{1}'.format(key, idx)\n param.update(_param_from_config(prefix, conf_item))\n\n else:\n if isinstance(data, bool):\n # convert boolean True/False to 'true'/'false'\n param.update({key: six.text_type(data).lower()})\n else:\n param.update({key: data})\n\n return param"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nrequest an instance on EC2.", "response": "def request_instance(vm_=None, call=None):\n '''\n Put together all of the information necessary to request an instance on EC2,\n 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\n location = vm_.get('location', get_location(vm_))\n\n # do we launch a regular vm or a spot instance?\n # see http://goo.gl/hYZ13f for more information on EC2 API\n spot_config = get_spot_config(vm_)\n if spot_config is not None:\n if 'spot_price' not in spot_config:\n raise SaltCloudSystemExit(\n 'Spot instance config for {0} requires a spot_price '\n 'attribute.'.format(vm_['name'])\n )\n\n params = {'Action': 'RequestSpotInstances',\n 'InstanceCount': '1',\n 'Type': spot_config['type']\n if 'type' in spot_config else 'one-time',\n 'SpotPrice': spot_config['spot_price']}\n\n # All of the necessary launch parameters for a VM when using\n # spot instances are the same except for the prefix below\n # being tacked on.\n spot_prefix = 'LaunchSpecification.'\n\n # regular EC2 instance\n else:\n # WARNING! EXPERIMENTAL!\n # This allows more than one instance to be spun up in a single call.\n # The first instance will be called by the name provided, but all other\n # instances will be nameless (or more specifically, they will use the\n # InstanceId as the name). This interface is expected to change, so\n # use at your own risk.\n min_instance = config.get_cloud_config_value(\n 'min_instance', vm_, __opts__, search_global=False, default=1\n )\n max_instance = config.get_cloud_config_value(\n 'max_instance', vm_, __opts__, search_global=False, default=1\n )\n params = {'Action': 'RunInstances',\n 'MinCount': min_instance,\n 'MaxCount': max_instance}\n\n # Normal instances should have no prefix.\n spot_prefix = ''\n\n image_id = get_imageid(vm_)\n params[spot_prefix + 'ImageId'] = image_id\n\n userdata = None\n userdata_file = config.get_cloud_config_value(\n 'userdata_file', vm_, __opts__, search_global=False, default=None\n )\n if userdata_file is None:\n userdata = config.get_cloud_config_value(\n 'userdata', vm_, __opts__, search_global=False, default=None\n )\n else:\n log.trace('userdata_file: %s', userdata_file)\n if os.path.exists(userdata_file):\n with salt.utils.files.fopen(userdata_file, 'r') as fh_:\n userdata = salt.utils.stringutils.to_unicode(fh_.read())\n\n userdata = salt.utils.cloud.userdata_template(__opts__, vm_, userdata)\n\n if userdata is not None:\n try:\n params[spot_prefix + 'UserData'] = base64.b64encode(\n salt.utils.stringutils.to_bytes(userdata)\n )\n except Exception as exc:\n log.exception('Failed to encode userdata: %s', exc)\n\n vm_size = config.get_cloud_config_value(\n 'size', vm_, __opts__, search_global=False\n )\n params[spot_prefix + 'InstanceType'] = vm_size\n\n ex_keyname = keyname(vm_)\n if ex_keyname:\n params[spot_prefix + 'KeyName'] = ex_keyname\n\n ex_securitygroup = securitygroup(vm_)\n if ex_securitygroup:\n if not isinstance(ex_securitygroup, list):\n params[spot_prefix + 'SecurityGroup.1'] = ex_securitygroup\n else:\n for counter, sg_ in enumerate(ex_securitygroup):\n params[spot_prefix + 'SecurityGroup.{0}'.format(counter)] = sg_\n\n ex_iam_profile = iam_profile(vm_)\n if ex_iam_profile:\n try:\n if ex_iam_profile.startswith('arn:aws:iam:'):\n params[\n spot_prefix + 'IamInstanceProfile.Arn'\n ] = ex_iam_profile\n else:\n params[\n spot_prefix + 'IamInstanceProfile.Name'\n ] = ex_iam_profile\n except AttributeError:\n raise SaltCloudConfigError(\n '\\'iam_profile\\' should be a string value.'\n )\n\n az_ = get_availability_zone(vm_)\n if az_ is not None:\n params[spot_prefix + 'Placement.AvailabilityZone'] = az_\n\n tenancy_ = get_tenancy(vm_)\n if tenancy_ is not None:\n if spot_config is not None:\n raise SaltCloudConfigError(\n 'Spot instance config for {0} does not support '\n 'specifying tenancy.'.format(vm_['name'])\n )\n params['Placement.Tenancy'] = tenancy_\n\n subnetid_ = get_subnetid(vm_)\n if subnetid_ is not None:\n params[spot_prefix + 'SubnetId'] = subnetid_\n\n ex_securitygroupid = securitygroupid(vm_)\n if ex_securitygroupid:\n if not isinstance(ex_securitygroupid, list):\n params[spot_prefix + 'SecurityGroupId.1'] = ex_securitygroupid\n else:\n for counter, sg_ in enumerate(ex_securitygroupid):\n params[\n spot_prefix + 'SecurityGroupId.{0}'.format(counter)\n ] = sg_\n\n placementgroup_ = get_placementgroup(vm_)\n if placementgroup_ is not None:\n params[spot_prefix + 'Placement.GroupName'] = placementgroup_\n\n blockdevicemappings_holder = block_device_mappings(vm_)\n if blockdevicemappings_holder:\n for _bd in blockdevicemappings_holder:\n if 'tag' in _bd:\n _bd.pop('tag')\n\n ex_blockdevicemappings = blockdevicemappings_holder\n if ex_blockdevicemappings:\n params.update(_param_from_config(spot_prefix + 'BlockDeviceMapping',\n ex_blockdevicemappings))\n\n network_interfaces = config.get_cloud_config_value(\n 'network_interfaces',\n vm_,\n __opts__,\n search_global=False\n )\n\n if network_interfaces:\n eni_devices = []\n for interface in network_interfaces:\n log.debug('Create network interface: %s', interface)\n _new_eni = _create_eni_if_necessary(interface, vm_)\n eni_devices.append(_new_eni)\n params.update(_param_from_config(spot_prefix + 'NetworkInterface',\n eni_devices))\n\n set_ebs_optimized = config.get_cloud_config_value(\n 'ebs_optimized', vm_, __opts__, search_global=False\n )\n\n if set_ebs_optimized is not None:\n if not isinstance(set_ebs_optimized, bool):\n raise SaltCloudConfigError(\n '\\'ebs_optimized\\' should be a boolean value.'\n )\n params[spot_prefix + 'EbsOptimized'] = set_ebs_optimized\n\n set_del_root_vol_on_destroy = config.get_cloud_config_value(\n 'del_root_vol_on_destroy', vm_, __opts__, search_global=False\n )\n\n set_termination_protection = config.get_cloud_config_value(\n 'termination_protection', vm_, __opts__, search_global=False\n )\n\n if set_termination_protection is not None:\n if not isinstance(set_termination_protection, bool):\n raise SaltCloudConfigError(\n '\\'termination_protection\\' should be a boolean value.'\n )\n params.update(_param_from_config(spot_prefix + 'DisableApiTermination',\n set_termination_protection))\n\n if set_del_root_vol_on_destroy and not isinstance(set_del_root_vol_on_destroy, bool):\n raise SaltCloudConfigError(\n '\\'del_root_vol_on_destroy\\' should be a boolean value.'\n )\n\n vm_['set_del_root_vol_on_destroy'] = set_del_root_vol_on_destroy\n\n if set_del_root_vol_on_destroy:\n # first make sure to look up the root device name\n # as Ubuntu and CentOS (and most likely other OSs)\n # use different device identifiers\n\n log.info('Attempting to look up root device name for image id %s on '\n 'VM %s', image_id, vm_['name'])\n\n rd_params = {\n 'Action': 'DescribeImages',\n 'ImageId.1': image_id\n }\n try:\n rd_data = aws.query(rd_params,\n location=get_location(vm_),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n if 'error' in rd_data:\n return rd_data['error']\n log.debug('EC2 Response: \\'%s\\'', rd_data)\n except Exception as exc:\n log.error(\n 'Error getting root device name for image id %s for '\n 'VM %s: \\n%s', image_id, vm_['name'], exc,\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n raise\n\n # make sure we have a response\n if not rd_data:\n err_msg = 'There was an error querying EC2 for the root device ' \\\n 'of image id {0}. Empty response.'.format(image_id)\n raise SaltCloudSystemExit(err_msg)\n\n # pull the root device name from the result and use it when\n # launching the new VM\n rd_name = None\n rd_type = None\n if 'blockDeviceMapping' in rd_data[0]:\n # Some ami instances do not have a root volume. Ignore such cases\n if rd_data[0]['blockDeviceMapping'] is not None:\n item = rd_data[0]['blockDeviceMapping']['item']\n if isinstance(item, list):\n item = item[0]\n rd_name = item['deviceName']\n # Grab the volume type\n rd_type = item['ebs'].get('volumeType', None)\n\n log.info('Found root device name: %s', rd_name)\n\n if rd_name is not None:\n if ex_blockdevicemappings:\n dev_list = [\n dev['DeviceName'] for dev in ex_blockdevicemappings\n ]\n else:\n dev_list = []\n\n if rd_name in dev_list:\n # Device already listed, just grab the index\n dev_index = dev_list.index(rd_name)\n else:\n dev_index = len(dev_list)\n # Add the device name in since it wasn't already there\n params[\n '{0}BlockDeviceMapping.{1}.DeviceName'.format(\n spot_prefix, dev_index\n )\n ] = rd_name\n\n # Set the termination value\n termination_key = '{0}BlockDeviceMapping.{1}.Ebs.DeleteOnTermination'.format(spot_prefix, dev_index)\n params[termination_key] = six.text_type(set_del_root_vol_on_destroy).lower()\n\n # Use default volume type if not specified\n if ex_blockdevicemappings and dev_index < len(ex_blockdevicemappings) and \\\n 'Ebs.VolumeType' not in ex_blockdevicemappings[dev_index]:\n type_key = '{0}BlockDeviceMapping.{1}.Ebs.VolumeType'.format(spot_prefix, dev_index)\n params[type_key] = rd_type\n\n set_del_all_vols_on_destroy = config.get_cloud_config_value(\n 'del_all_vols_on_destroy', vm_, __opts__, search_global=False, default=False\n )\n\n if set_del_all_vols_on_destroy and not isinstance(set_del_all_vols_on_destroy, bool):\n raise SaltCloudConfigError(\n '\\'del_all_vols_on_destroy\\' should be a boolean value.'\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'](\n 'requesting', params, list(params)\n ),\n 'location': location,\n },\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n provider = get_provider(vm_)\n\n try:\n data = aws.query(params,\n 'instancesSet',\n location=location,\n provider=provider,\n opts=__opts__,\n sigver='4')\n if 'error' in data:\n return data['error']\n except Exception as exc:\n log.error(\n 'Error creating %s on EC2 when trying to run the initial '\n 'deployment: \\n%s', vm_['name'], exc,\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n raise\n\n # if we're using spot instances, we need to wait for the spot request\n # to become active before we continue\n if spot_config:\n sir_id = data[0]['spotInstanceRequestId']\n\n vm_['spotRequestId'] = sir_id\n\n def __query_spot_instance_request(sir_id, location):\n params = {'Action': 'DescribeSpotInstanceRequests',\n 'SpotInstanceRequestId.1': sir_id}\n data = aws.query(params,\n location=location,\n provider=provider,\n opts=__opts__,\n sigver='4')\n if not data:\n log.error(\n 'There was an error while querying EC2. Empty response'\n )\n # Trigger a failure in the wait for spot instance method\n return False\n\n if isinstance(data, dict) and 'error' in data:\n log.warning('There was an error in the query. %s', data['error'])\n # Trigger a failure in the wait for spot instance method\n return False\n\n log.debug('Returned query data: %s', data)\n\n state = data[0].get('state')\n\n if state == 'active':\n return data\n\n if state == 'open':\n # Still waiting for an active state\n log.info('Spot instance status: %s', data[0]['status']['message'])\n return None\n\n if state in ['cancelled', 'failed', 'closed']:\n # Request will never be active, fail\n log.error('Spot instance request resulted in state \\'{0}\\'. '\n 'Nothing else we can do here.')\n return False\n\n __utils__['cloud.fire_event'](\n 'event',\n 'waiting for spot instance',\n 'salt/cloud/{0}/waiting_for_spot'.format(vm_['name']),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n try:\n data = _wait_for_spot_instance(\n __query_spot_instance_request,\n update_args=(sir_id, location),\n timeout=config.get_cloud_config_value(\n 'wait_for_spot_timeout', vm_, __opts__, default=10 * 60),\n interval=config.get_cloud_config_value(\n 'wait_for_spot_interval', vm_, __opts__, default=30),\n interval_multiplier=config.get_cloud_config_value(\n 'wait_for_spot_interval_multiplier',\n vm_,\n __opts__,\n default=1),\n max_failures=config.get_cloud_config_value(\n 'wait_for_spot_max_failures',\n vm_,\n __opts__,\n default=10),\n )\n log.debug('wait_for_spot_instance data %s', data)\n\n except (SaltCloudExecutionTimeout, SaltCloudExecutionFailure) as exc:\n try:\n # Cancel the existing spot instance request\n params = {'Action': 'CancelSpotInstanceRequests',\n 'SpotInstanceRequestId.1': sir_id}\n data = aws.query(params,\n location=location,\n provider=provider,\n opts=__opts__,\n sigver='4')\n\n log.debug('Canceled spot instance request %s. Data '\n 'returned: %s', sir_id, data)\n\n except SaltCloudSystemExit:\n pass\n finally:\n raise SaltCloudSystemExit(six.text_type(exc))\n\n return data, vm_"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef query_instance(vm_=None, call=None):\n '''\n Query an instance upon creation from the EC2 API\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 query_instance action must be called with -a or --action.'\n )\n\n instance_id = vm_['instance_id']\n location = vm_.get('location', get_location(vm_))\n __utils__['cloud.fire_event'](\n 'event',\n 'querying instance',\n 'salt/cloud/{0}/querying'.format(vm_['name']),\n args={'instance_id': instance_id},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n log.debug('The new VM instance_id is %s', instance_id)\n\n params = {'Action': 'DescribeInstances',\n 'InstanceId.1': instance_id}\n\n provider = get_provider(vm_)\n\n attempts = 0\n while attempts < aws.AWS_MAX_RETRIES:\n data, requesturl = aws.query(params, # pylint: disable=unbalanced-tuple-unpacking\n location=location,\n provider=provider,\n opts=__opts__,\n return_url=True,\n sigver='4')\n log.debug('The query returned: %s', data)\n\n if isinstance(data, dict) and 'error' in data:\n log.warning(\n 'There was an error in the query. %s attempts '\n 'remaining: %s', attempts, data['error']\n )\n elif isinstance(data, list) and not data:\n log.warning(\n 'Query returned an empty list. %s attempts '\n 'remaining.', attempts\n )\n else:\n break\n\n aws.sleep_exponential_backoff(attempts)\n attempts += 1\n continue\n else:\n raise SaltCloudSystemExit(\n 'An error occurred while creating VM: {0}'.format(data['error'])\n )\n\n def __query_ip_address(params, url): # pylint: disable=W0613\n data = aws.query(params,\n location=location,\n provider=provider,\n opts=__opts__,\n sigver='4')\n if not data:\n log.error(\n 'There was an error while querying EC2. Empty response'\n )\n # Trigger a failure in the wait for IP function\n return False\n\n if isinstance(data, dict) and 'error' in data:\n log.warning('There was an error in the query. %s', data['error'])\n # Trigger a failure in the wait for IP function\n return False\n\n log.debug('Returned query data: %s', data)\n\n if ssh_interface(vm_) == 'public_ips':\n if 'ipAddress' in data[0]['instancesSet']['item']:\n return data\n else:\n log.error(\n 'Public IP not detected.'\n )\n\n if ssh_interface(vm_) == 'private_ips':\n if 'privateIpAddress' in data[0]['instancesSet']['item']:\n return data\n else:\n log.error(\n 'Private IP not detected.'\n )\n\n try:\n data = salt.utils.cloud.wait_for_ip(\n __query_ip_address,\n update_args=(params, requesturl),\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 interval_multiplier=config.get_cloud_config_value(\n 'wait_for_ip_interval_multiplier', vm_, __opts__, default=1),\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 if 'reactor' in vm_ and vm_['reactor'] is True:\n __utils__['cloud.fire_event'](\n 'event',\n 'instance queried',\n 'salt/cloud/{0}/query_reactor'.format(vm_['name']),\n args={'data': data},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n return data", "response": "Query an instance upon creation from the EC2 API."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nwait for an instance on the EC2 API to become available.", "response": "def wait_for_instance(\n vm_=None,\n data=None,\n ip_address=None,\n display_ssh_output=True,\n call=None,\n ):\n '''\n Wait for an instance upon creation from the EC2 API, to become available\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 wait_for_instance action must be called with -a or --action.'\n )\n\n if vm_ is None:\n vm_ = {}\n\n if data is None:\n data = {}\n\n ssh_gateway_config = vm_.get(\n 'gateway', get_ssh_gateway_config(vm_)\n )\n\n __utils__['cloud.fire_event'](\n 'event',\n 'waiting for ssh',\n 'salt/cloud/{0}/waiting_for_ssh'.format(vm_['name']),\n args={'ip_address': ip_address},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n ssh_connect_timeout = config.get_cloud_config_value(\n 'ssh_connect_timeout', vm_, __opts__, 900 # 15 minutes\n )\n ssh_port = config.get_cloud_config_value(\n 'ssh_port', vm_, __opts__, 22\n )\n\n if config.get_cloud_config_value('win_installer', vm_, __opts__):\n username = config.get_cloud_config_value(\n 'win_username', vm_, __opts__, default='Administrator'\n )\n win_passwd = config.get_cloud_config_value(\n 'win_password', vm_, __opts__, default=''\n )\n win_deploy_auth_retries = config.get_cloud_config_value(\n 'win_deploy_auth_retries', vm_, __opts__, default=10\n )\n win_deploy_auth_retry_delay = config.get_cloud_config_value(\n 'win_deploy_auth_retry_delay', vm_, __opts__, default=1\n )\n use_winrm = config.get_cloud_config_value(\n 'use_winrm', vm_, __opts__, default=False\n )\n winrm_verify_ssl = config.get_cloud_config_value(\n 'winrm_verify_ssl', vm_, __opts__, default=True\n )\n\n if win_passwd and win_passwd == 'auto':\n log.debug('Waiting for auto-generated Windows EC2 password')\n while True:\n password_data = get_password_data(\n name=vm_['name'],\n kwargs={\n 'key_file': vm_['private_key'],\n },\n call='action',\n )\n win_passwd = password_data.get('password', None)\n if win_passwd is None:\n log.debug(password_data)\n # This wait is so high, because the password is unlikely to\n # be generated for at least 4 minutes\n time.sleep(60)\n else:\n logging_data = password_data\n\n logging_data['password'] = 'XXX-REDACTED-XXX'\n logging_data['passwordData'] = 'XXX-REDACTED-XXX'\n log.debug(logging_data)\n\n vm_['win_password'] = win_passwd\n break\n\n # SMB used whether psexec or winrm\n if not salt.utils.cloud.wait_for_port(ip_address,\n port=445,\n timeout=ssh_connect_timeout):\n raise SaltCloudSystemExit(\n 'Failed to connect to remote windows host'\n )\n\n # If not using winrm keep same psexec behavior\n if not use_winrm:\n\n log.debug('Trying to authenticate via SMB using psexec')\n\n if not salt.utils.cloud.validate_windows_cred(ip_address,\n username,\n win_passwd,\n retries=win_deploy_auth_retries,\n retry_delay=win_deploy_auth_retry_delay):\n raise SaltCloudSystemExit(\n 'Failed to authenticate against remote windows host (smb)'\n )\n\n # If using winrm\n else:\n\n # Default HTTPS port can be changed in cloud configuration\n winrm_port = config.get_cloud_config_value(\n 'winrm_port', vm_, __opts__, default=5986\n )\n\n # Wait for winrm port to be available\n if not salt.utils.cloud.wait_for_port(ip_address,\n port=winrm_port,\n timeout=ssh_connect_timeout):\n raise SaltCloudSystemExit(\n 'Failed to connect to remote windows host (winrm)'\n )\n\n log.debug('Trying to authenticate via Winrm using pywinrm')\n\n if not salt.utils.cloud.wait_for_winrm(ip_address,\n winrm_port,\n username,\n win_passwd,\n timeout=ssh_connect_timeout,\n verify=winrm_verify_ssl):\n raise SaltCloudSystemExit(\n 'Failed to authenticate against remote windows host'\n )\n\n elif salt.utils.cloud.wait_for_port(ip_address,\n port=ssh_port,\n timeout=ssh_connect_timeout,\n gateway=ssh_gateway_config\n ):\n # If a known_hosts_file is configured, this instance will not be\n # accessible until it has a host key. Since this is provided on\n # supported instances by cloud-init, and viewable to us only from the\n # console output (which may take several minutes to become available,\n # we have some more waiting to do here.\n known_hosts_file = config.get_cloud_config_value(\n 'known_hosts_file', vm_, __opts__, default=None\n )\n if known_hosts_file:\n console = {}\n while 'output_decoded' not in console:\n console = get_console_output(\n instance_id=vm_['instance_id'],\n call='action',\n location=get_location(vm_)\n )\n pprint.pprint(console)\n time.sleep(5)\n output = salt.utils.stringutils.to_unicode(console['output_decoded'])\n comps = output.split('-----BEGIN SSH HOST KEY KEYS-----')\n if len(comps) < 2:\n # Fail; there are no host keys\n return False\n\n comps = comps[1].split('-----END SSH HOST KEY KEYS-----')\n keys = ''\n for line in comps[0].splitlines():\n if not line:\n continue\n keys += '\\n{0} {1}'.format(ip_address, line)\n\n with salt.utils.files.fopen(known_hosts_file, 'a') as fp_:\n fp_.write(salt.utils.stringutils.to_str(keys))\n fp_.close()\n\n for user in vm_['usernames']:\n if salt.utils.cloud.wait_for_passwd(\n host=ip_address,\n port=ssh_port,\n username=user,\n ssh_timeout=config.get_cloud_config_value(\n 'wait_for_passwd_timeout', vm_, __opts__, default=1 * 60\n ),\n key_filename=vm_['key_filename'],\n display_ssh_output=display_ssh_output,\n gateway=ssh_gateway_config,\n maxtries=config.get_cloud_config_value(\n 'wait_for_passwd_maxtries', vm_, __opts__, default=15\n ),\n known_hosts_file=config.get_cloud_config_value(\n 'known_hosts_file', vm_, __opts__,\n default='/dev/null'\n ),\n ):\n __opts__['ssh_username'] = user\n vm_['ssh_username'] = user\n break\n else:\n raise SaltCloudSystemExit(\n 'Failed to authenticate against remote ssh'\n )\n else:\n raise SaltCloudSystemExit(\n 'Failed to connect to remote ssh'\n )\n\n if 'reactor' in vm_ and vm_['reactor'] is True:\n __utils__['cloud.fire_event'](\n 'event',\n 'ssh is available',\n 'salt/cloud/{0}/ssh_ready_reactor'.format(vm_['name']),\n args={'ip_address': ip_address},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n return vm_"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create(vm_=None, call=None):\n '''\n Create a single VM from a data dict\n '''\n if call:\n raise SaltCloudSystemExit(\n 'You cannot create an instance with -a or -f.'\n )\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 'ec2',\n vm_['profile'],\n vm_=vm_) is False:\n return False\n except AttributeError:\n pass\n\n # Check for private_key and keyfile name for bootstrapping new instances\n deploy = config.get_cloud_config_value(\n 'deploy', vm_, __opts__, default=True\n )\n win_password = config.get_cloud_config_value(\n 'win_password', vm_, __opts__, default=''\n )\n key_filename = config.get_cloud_config_value(\n 'private_key', vm_, __opts__, search_global=False, default=None\n )\n if deploy:\n # The private_key and keyname settings are only needed for bootstrapping\n # new instances when deploy is True\n _validate_key_path_and_mode(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 __utils__['cloud.cachedir_index_add'](\n vm_['name'], vm_['profile'], 'ec2', vm_['driver']\n )\n\n vm_['key_filename'] = key_filename\n # wait_for_instance requires private_key\n vm_['private_key'] = key_filename\n\n # Get SSH Gateway config early to verify the private_key,\n # if used, exists or not. We don't want to deploy an instance\n # and not be able to access it via the gateway.\n vm_['gateway'] = get_ssh_gateway_config(vm_)\n\n location = get_location(vm_)\n vm_['location'] = location\n\n log.info('Creating Cloud VM %s in %s', vm_['name'], location)\n vm_['usernames'] = salt.utils.cloud.ssh_usernames(\n vm_,\n __opts__,\n default_users=(\n 'ec2-user', # Amazon Linux, Fedora, RHEL; FreeBSD\n 'centos', # CentOS AMIs from AWS Marketplace\n 'ubuntu', # Ubuntu\n 'admin', # Debian GNU/Linux\n 'bitnami', # BitNami AMIs\n 'root' # Last resort, default user on RHEL 5, SUSE\n )\n )\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 else:\n # Put together all of the information required to request the instance,\n # and then fire off the request for it\n if keyname(vm_) is None:\n raise SaltCloudSystemExit(\n 'The required \\'keyname\\' configuration setting is missing from the '\n '\\'ec2\\' driver.'\n )\n\n data, vm_ = request_instance(vm_, location)\n\n # If data is a str, it's an error\n if isinstance(data, six.string_types):\n log.error('Error requesting instance: %s', data)\n return {}\n\n # Pull the instance ID, valid for both spot and normal instances\n\n # Multiple instances may have been spun up, get all their IDs\n vm_['instance_id_list'] = []\n for instance in data:\n vm_['instance_id_list'].append(instance['instanceId'])\n\n vm_['instance_id'] = vm_['instance_id_list'].pop()\n if vm_['instance_id_list']:\n # Multiple instances were spun up, get one now, and queue the rest\n queue_instances(vm_['instance_id_list'])\n\n # Wait for vital information, such as IP addresses, to be available\n # for the new instance\n data = query_instance(vm_)\n\n # Now that the instance is available, tag it appropriately. Should\n # mitigate race conditions with tags\n tags = config.get_cloud_config_value('tag',\n vm_,\n __opts__,\n {},\n search_global=False)\n if not isinstance(tags, dict):\n raise SaltCloudConfigError(\n '\\'tag\\' should be a dict.'\n )\n\n for value in six.itervalues(tags):\n if not isinstance(value, six.string_types):\n raise SaltCloudConfigError(\n '\\'tag\\' values must be strings. Try quoting the values. '\n 'e.g. \"2013-09-19T20:09:46Z\".'\n )\n\n tags['Name'] = vm_['name']\n\n __utils__['cloud.fire_event'](\n 'event',\n 'setting tags',\n 'salt/cloud/{0}/tagging'.format(vm_['name']),\n args={'tags': tags},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n salt.utils.cloud.wait_for_fun(\n set_tags,\n timeout=30,\n name=vm_['name'],\n tags=tags,\n instance_id=vm_['instance_id'],\n call='action',\n location=location\n )\n\n # Once instance tags are set, tag the spot request if configured\n if 'spot_config' in vm_ and 'tag' in vm_['spot_config']:\n\n if not isinstance(vm_['spot_config']['tag'], dict):\n raise SaltCloudConfigError(\n '\\'tag\\' should be a dict.'\n )\n\n for value in six.itervalues(vm_['spot_config']['tag']):\n if not isinstance(value, str):\n raise SaltCloudConfigError(\n '\\'tag\\' values must be strings. Try quoting the values. '\n 'e.g. \"2013-09-19T20:09:46Z\".'\n )\n\n spot_request_tags = {}\n\n if 'spotRequestId' not in vm_:\n raise SaltCloudConfigError('Failed to find spotRequestId')\n\n sir_id = vm_['spotRequestId']\n\n spot_request_tags['Name'] = vm_['name']\n\n for k, v in six.iteritems(vm_['spot_config']['tag']):\n spot_request_tags[k] = v\n\n __utils__['cloud.fire_event'](\n 'event',\n 'setting tags',\n 'salt/cloud/spot_request_{0}/tagging'.format(sir_id),\n args={'tags': spot_request_tags},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n salt.utils.cloud.wait_for_fun(\n set_tags,\n timeout=30,\n name=vm_['name'],\n tags=spot_request_tags,\n instance_id=sir_id,\n call='action',\n location=location\n )\n\n network_interfaces = config.get_cloud_config_value(\n 'network_interfaces',\n vm_,\n __opts__,\n search_global=False\n )\n\n if network_interfaces:\n _update_enis(network_interfaces, data, vm_)\n\n # At this point, the node is created and tagged, and now needs to be\n # bootstrapped, once the necessary port is available.\n log.info('Created node %s', vm_['name'])\n\n instance = data[0]['instancesSet']['item']\n\n # Wait for the necessary port to become available to bootstrap\n if ssh_interface(vm_) == 'private_ips':\n ip_address = instance['privateIpAddress']\n log.info('Salt node data. Private_ip: %s', ip_address)\n else:\n ip_address = instance['ipAddress']\n log.info('Salt node data. Public_ip: %s', ip_address)\n vm_['ssh_host'] = ip_address\n\n if salt.utils.cloud.get_salt_interface(vm_, __opts__) == 'private_ips':\n salt_ip_address = instance['privateIpAddress']\n log.info('Salt interface set to: %s', salt_ip_address)\n else:\n salt_ip_address = instance['ipAddress']\n log.debug('Salt interface set to: %s', salt_ip_address)\n vm_['salt_host'] = salt_ip_address\n\n if deploy:\n display_ssh_output = config.get_cloud_config_value(\n 'display_ssh_output', vm_, __opts__, default=True\n )\n\n vm_ = wait_for_instance(\n vm_, data, ip_address, display_ssh_output\n )\n\n # The instance is booted and accessible, let's Salt it!\n ret = instance.copy()\n\n # Get ANY defined volumes settings, merging data, in the following order\n # 1. VM config\n # 2. Profile config\n # 3. Global configuration\n volumes = config.get_cloud_config_value(\n 'volumes', vm_, __opts__, search_global=True\n )\n if volumes:\n __utils__['cloud.fire_event'](\n 'event',\n 'attaching volumes',\n 'salt/cloud/{0}/attaching_volumes'.format(vm_['name']),\n args={'volumes': volumes},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n log.info('Create and attach volumes to node %s', vm_['name'])\n created = create_attach_volumes(\n vm_['name'],\n {\n 'volumes': volumes,\n 'zone': ret['placement']['availabilityZone'],\n 'instance_id': ret['instanceId'],\n 'del_all_vols_on_destroy': vm_.get('del_all_vols_on_destroy', False)\n },\n call='action'\n )\n ret['Attached Volumes'] = created\n\n # Associate instance with a ssm document, if present\n ssm_document = config.get_cloud_config_value(\n 'ssm_document', vm_, __opts__, None, search_global=False\n )\n if ssm_document:\n log.debug('Associating with ssm document: %s', ssm_document)\n assoc = ssm_create_association(\n vm_['name'],\n {'ssm_document': ssm_document},\n instance_id=vm_['instance_id'],\n call='action'\n )\n if isinstance(assoc, dict) and assoc.get('error', None):\n log.error(\n 'Failed to associate instance %s with ssm document %s',\n vm_['instance_id'], ssm_document\n )\n return {}\n\n for key, value in six.iteritems(__utils__['cloud.bootstrap'](vm_, __opts__)):\n ret.setdefault(key, value)\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(instance)\n )\n\n event_data = {\n 'name': vm_['name'],\n 'profile': vm_['profile'],\n 'provider': vm_['driver'],\n 'instance_id': vm_['instance_id'],\n }\n if volumes:\n event_data['volumes'] = volumes\n if ssm_document:\n event_data['ssm_document'] = ssm_document\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\n # Ensure that the latest node data is returned\n node = _get_node(instance_id=vm_['instance_id'])\n __utils__['cloud.cache_node'](node, __active_provider_name__, __opts__)\n ret.update(node)\n\n # Add any block device tags specified\n ex_blockdevicetags = {}\n blockdevicemappings_holder = block_device_mappings(vm_)\n if blockdevicemappings_holder:\n for _bd in blockdevicemappings_holder:\n if 'tag' in _bd:\n ex_blockdevicetags[_bd['DeviceName']] = _bd['tag']\n\n block_device_volume_id_map = {}\n\n if ex_blockdevicetags:\n for _device, _map in six.iteritems(ret['blockDeviceMapping']):\n bd_items = []\n if isinstance(_map, dict):\n bd_items.append(_map)\n else:\n for mapitem in _map:\n bd_items.append(mapitem)\n\n for blockitem in bd_items:\n if blockitem['deviceName'] in ex_blockdevicetags and 'Name' not in ex_blockdevicetags[blockitem['deviceName']]:\n ex_blockdevicetags[blockitem['deviceName']]['Name'] = vm_['name']\n if blockitem['deviceName'] in ex_blockdevicetags:\n block_device_volume_id_map[blockitem[ret['rootDeviceType']]['volumeId']] = ex_blockdevicetags[blockitem['deviceName']]\n\n if block_device_volume_id_map:\n\n for volid, tags in six.iteritems(block_device_volume_id_map):\n __utils__['cloud.fire_event'](\n 'event',\n 'setting tags',\n 'salt/cloud/block_volume_{0}/tagging'.format(str(volid)),\n args={'tags': tags},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n __utils__['cloud.wait_for_fun'](\n set_tags,\n timeout=30,\n name=vm_['name'],\n tags=tags,\n resource_id=volid,\n call='action',\n location=location\n )\n\n return ret", "response": "Create a single VM from a data dict."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nqueues a set of instances to be provisioned later. Expects a list.", "response": "def queue_instances(instances):\n '''\n Queue a set of instances to be provisioned later. Expects a list.\n\n Currently this only queries node data, and then places it in the cloud\n cache (if configured). If the salt-cloud-reactor is being used, these\n instances will be automatically provisioned using that.\n\n For more information about the salt-cloud-reactor, see:\n\n https://github.com/saltstack-formulas/salt-cloud-reactor\n '''\n for instance_id in instances:\n node = _get_node(instance_id=instance_id)\n __utils__['cloud.cache_node'](node, __active_provider_name__, __opts__)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate and attach volumes to a node", "response": "def create_attach_volumes(name, kwargs, call=None, wait_to_finish=True):\n '''\n Create and attach volumes to created node\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The create_attach_volumes action must be called with '\n '-a or --action.'\n )\n\n if 'instance_id' not in kwargs:\n kwargs['instance_id'] = _get_node(name)['instanceId']\n\n if isinstance(kwargs['volumes'], six.string_types):\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 volume_name = '{0} on {1}'.format(volume['device'], name)\n\n volume_dict = {\n 'volume_name': volume_name,\n 'zone': kwargs['zone']\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 elif 'size' in volume:\n volume_dict['size'] = volume['size']\n else:\n raise SaltCloudConfigError(\n 'Cannot create volume. Please define one of \\'volume_id\\', '\n '\\'snapshot\\', or \\'size\\''\n )\n\n if 'tags' in volume:\n volume_dict['tags'] = volume['tags']\n if 'type' in volume:\n volume_dict['type'] = volume['type']\n if 'iops' in volume:\n volume_dict['iops'] = volume['iops']\n if 'encrypted' in volume:\n volume_dict['encrypted'] = volume['encrypted']\n if 'kmskeyid' in volume:\n volume_dict['kmskeyid'] = volume['kmskeyid']\n\n if 'volume_id' not in volume_dict:\n created_volume = create_volume(volume_dict, call='function', wait_to_finish=wait_to_finish)\n created = True\n if 'volumeId' in created_volume:\n volume_dict['volume_id'] = created_volume['volumeId']\n\n attach = attach_volume(\n name,\n {'volume_id': volume_dict['volume_id'],\n 'device': volume['device']},\n instance_id=kwargs['instance_id'],\n call='action'\n )\n\n # Update the delvol parameter for this volume\n delvols_on_destroy = kwargs.get('del_all_vols_on_destroy', None)\n\n if attach and created and delvols_on_destroy is not None:\n _toggle_delvol(instance_id=kwargs['instance_id'],\n device=volume['device'],\n value=delvols_on_destroy)\n\n if attach:\n msg = (\n '{0} attached to {1} (aka {2}) as device {3}'.format(\n volume_dict['volume_id'],\n kwargs['instance_id'],\n name,\n volume['device']\n )\n )\n log.info(msg)\n ret.append(msg)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef stop(name, call=None):\n '''\n Stop a node\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The stop action must be called with -a or --action.'\n )\n\n log.info('Stopping node %s', name)\n\n instance_id = _get_node(name)['instanceId']\n\n __utils__['cloud.fire_event'](\n 'event',\n 'stopping instance',\n 'salt/cloud/{0}/stopping'.format(name),\n args={'name': name, 'instance_id': instance_id},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n params = {'Action': 'StopInstances',\n 'InstanceId.1': instance_id}\n result = aws.query(params,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n return result", "response": "Stop a node in the cluster"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_tags(name=None,\n tags=None,\n call=None,\n location=None,\n instance_id=None,\n resource_id=None,\n kwargs=None): # pylint: disable=W0613\n '''\n Set tags for a resource. Normally a VM name or instance_id is passed in,\n but a resource_id may be passed instead. If both are passed in, the\n instance_id will be used.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -a set_tags mymachine tag1=somestuff tag2='Other stuff'\n salt-cloud -a set_tags resource_id=vol-3267ab32 tag=somestuff\n '''\n if kwargs is None:\n kwargs = {}\n\n if location is None:\n location = get_location()\n\n if instance_id is None:\n if 'resource_id' in kwargs:\n resource_id = kwargs['resource_id']\n del kwargs['resource_id']\n\n if 'instance_id' in kwargs:\n instance_id = kwargs['instance_id']\n del kwargs['instance_id']\n\n if resource_id is None:\n if instance_id is None:\n instance_id = _get_node(name=name, instance_id=None, location=location)['instanceId']\n else:\n instance_id = resource_id\n\n # This second check is a safety, in case the above still failed to produce\n # a usable ID\n if instance_id is None:\n return {\n 'Error': 'A valid instance_id or resource_id was not specified.'\n }\n\n params = {'Action': 'CreateTags',\n 'ResourceId.1': instance_id}\n\n log.debug('Tags to set for %s: %s', name, tags)\n\n if kwargs and not tags:\n tags = kwargs\n\n for idx, (tag_k, tag_v) in enumerate(six.iteritems(tags)):\n params['Tag.{0}.Key'.format(idx)] = tag_k\n params['Tag.{0}.Value'.format(idx)] = tag_v\n\n attempts = 0\n while attempts < aws.AWS_MAX_RETRIES:\n aws.query(params,\n setname='tagSet',\n location=location,\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n settags = get_tags(\n instance_id=instance_id, call='action', location=location\n )\n\n log.debug('Setting the tags returned: %s', settags)\n\n failed_to_set_tags = False\n for tag in settags:\n if tag['key'] not in tags:\n # We were not setting this tag\n continue\n\n if tag.get('value') is None and tags.get(tag['key']) == '':\n # This is a correctly set tag with no value\n continue\n\n if six.text_type(tags.get(tag['key'])) != six.text_type(tag['value']):\n # Not set to the proper value!?\n log.debug(\n 'Setting the tag %s returned %s instead of %s',\n tag['key'], tags.get(tag['key']), tag['value']\n )\n failed_to_set_tags = True\n break\n\n if failed_to_set_tags:\n log.warning('Failed to set tags. Remaining attempts %s', attempts)\n attempts += 1\n aws.sleep_exponential_backoff(attempts)\n continue\n\n return settags\n\n raise SaltCloudSystemExit(\n 'Failed to set tags on {0}!'.format(name)\n )", "response": "Set tags for a resource."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_tags(name=None,\n instance_id=None,\n call=None,\n location=None,\n kwargs=None,\n resource_id=None): # pylint: disable=W0613\n '''\n Retrieve tags for a resource. Normally a VM name or instance_id is passed\n in, but a resource_id may be passed instead. If both are passed in, the\n instance_id will be used.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -a get_tags mymachine\n salt-cloud -a get_tags resource_id=vol-3267ab32\n '''\n if location is None:\n location = get_location()\n\n if instance_id is None:\n if resource_id is None:\n if name:\n instance_id = _get_node(name)['instanceId']\n elif 'instance_id' in kwargs:\n instance_id = kwargs['instance_id']\n elif 'resource_id' in kwargs:\n instance_id = kwargs['resource_id']\n else:\n instance_id = resource_id\n\n params = {'Action': 'DescribeTags',\n 'Filter.1.Name': 'resource-id',\n 'Filter.1.Value': instance_id}\n\n return aws.query(params,\n setname='tagSet',\n location=location,\n provider=get_provider(),\n opts=__opts__,\n sigver='4')", "response": "Retrieve tags for a resource."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef del_tags(name=None,\n kwargs=None,\n call=None,\n instance_id=None,\n resource_id=None): # pylint: disable=W0613\n '''\n Delete tags for a resource. Normally a VM name or instance_id is passed in,\n but a resource_id may be passed instead. If both are passed in, the\n instance_id will be used.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -a del_tags mymachine tags=mytag,\n salt-cloud -a del_tags mymachine tags=tag1,tag2,tag3\n salt-cloud -a del_tags resource_id=vol-3267ab32 tags=tag1,tag2,tag3\n '''\n if kwargs is None:\n kwargs = {}\n\n if 'tags' not in kwargs:\n raise SaltCloudSystemExit(\n 'A tag or tags must be specified using tags=list,of,tags'\n )\n\n if not name and 'resource_id' in kwargs:\n instance_id = kwargs['resource_id']\n del kwargs['resource_id']\n\n if not instance_id:\n instance_id = _get_node(name)['instanceId']\n\n params = {'Action': 'DeleteTags',\n 'ResourceId.1': instance_id}\n\n for idx, tag in enumerate(kwargs['tags'].split(',')):\n params['Tag.{0}.Key'.format(idx)] = tag\n\n aws.query(params,\n setname='tagSet',\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n if resource_id:\n return get_tags(resource_id=resource_id)\n else:\n return get_tags(instance_id=instance_id)", "response": "Delete tags for a resource. Normally a VM name or instance_id is passed in but a resource_id may be passed in."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef rename(name, kwargs, call=None):\n '''\n Properly rename a node. Pass in the new name as \"new name\".\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -a rename mymachine newname=yourmachine\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The rename action must be called with -a or --action.'\n )\n\n log.info('Renaming %s to %s', name, kwargs['newname'])\n\n set_tags(name, {'Name': kwargs['newname']}, call='action')\n\n salt.utils.cloud.rename_key(\n __opts__['pki_dir'], name, kwargs['newname']\n )", "response": "Properly rename a node. Pass in the new name as \"new name\".\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -a rename mymachine newname=yourmachine"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndestroys a node. Will check termination protection and warn if enabled. CLI Example: .. code-block:: bash salt-cloud --destroy mymachine", "response": "def destroy(name, call=None):\n '''\n Destroy a node. Will check termination protection and warn if enabled.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud --destroy mymachine\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 node_metadata = _get_node(name)\n instance_id = node_metadata['instanceId']\n sir_id = node_metadata.get('spotInstanceRequestId')\n protected = show_term_protect(\n name=name,\n instance_id=instance_id,\n call='action',\n quiet=True\n )\n\n __utils__['cloud.fire_event'](\n 'event',\n 'destroying instance',\n 'salt/cloud/{0}/destroying'.format(name),\n args={'name': name, 'instance_id': instance_id},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n if protected == 'true':\n raise SaltCloudSystemExit(\n 'This instance has been protected from being destroyed. '\n 'Use the following command to disable protection:\\n\\n'\n 'salt-cloud -a disable_term_protect {0}'.format(\n name\n )\n )\n\n ret = {}\n\n # Default behavior is to rename EC2 VMs when destroyed\n # via salt-cloud, unless explicitly set to False.\n rename_on_destroy = config.get_cloud_config_value('rename_on_destroy',\n get_configured_provider(),\n __opts__,\n search_global=False)\n if rename_on_destroy is not False:\n newname = '{0}-DEL{1}'.format(name, uuid.uuid4().hex)\n rename(name, kwargs={'newname': newname}, call='action')\n log.info(\n 'Machine will be identified as %s until it has been '\n 'cleaned up.', newname\n )\n ret['newname'] = newname\n\n params = {'Action': 'TerminateInstances',\n 'InstanceId.1': instance_id}\n\n location = get_location()\n provider = get_provider()\n result = aws.query(params,\n location=location,\n provider=provider,\n opts=__opts__,\n sigver='4')\n\n log.info(result)\n ret.update(result[0])\n\n # If this instance is part of a spot instance request, we\n # need to cancel it as well\n if sir_id is not None:\n params = {'Action': 'CancelSpotInstanceRequests',\n 'SpotInstanceRequestId.1': sir_id}\n result = aws.query(params,\n location=location,\n provider=provider,\n opts=__opts__,\n sigver='4')\n ret['spotInstance'] = result[0]\n\n __utils__['cloud.fire_event'](\n 'event',\n 'destroyed instance',\n 'salt/cloud/{0}/destroyed'.format(name),\n args={'name': name, 'instance_id': instance_id},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n __utils__['cloud.cachedir_index_del'](name)\n\n if __opts__.get('update_cachedir', False) is True:\n __utils__['cloud.delete_minion_cachedir'](name, __active_provider_name__.split(':')[0], __opts__)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nshow the details from EC2 concerning an AMI", "response": "def show_image(kwargs, call=None):\n '''\n Show the details from EC2 concerning an AMI\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_image action must be called with -f or --function.'\n )\n\n params = {'ImageId.1': kwargs['image'],\n 'Action': 'DescribeImages'}\n result = aws.query(params,\n setname='tagSet',\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n log.info(result)\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef show_instance(name=None, instance_id=None, call=None, kwargs=None):\n '''\n Show the details from EC2 concerning an AMI.\n\n Can be called as an action (which requires a name):\n\n .. code-block:: bash\n\n salt-cloud -a show_instance myinstance\n\n ...or as a function (which requires either a name or instance_id):\n\n .. code-block:: bash\n\n salt-cloud -f show_instance my-ec2 name=myinstance\n salt-cloud -f show_instance my-ec2 instance_id=i-d34db33f\n '''\n if not name and call == 'action':\n raise SaltCloudSystemExit(\n 'The show_instance action requires a name.'\n )\n\n if call == 'function':\n name = kwargs.get('name', None)\n instance_id = kwargs.get('instance_id', None)\n\n if not name and not instance_id:\n raise SaltCloudSystemExit(\n 'The show_instance function requires '\n 'either a name or an instance_id'\n )\n\n node = _get_node(name=name, instance_id=instance_id)\n __utils__['cloud.cache_node'](node, __active_provider_name__, __opts__)\n return node", "response": "Show the details from an AMI."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngive an instance query return a dict of all instance data", "response": "def _extract_instance_info(instances):\n '''\n Given an instance query, return a dict of all instance data\n '''\n ret = {}\n for instance in instances:\n # items could be type dict or list (for stopped EC2 instances)\n if isinstance(instance['instancesSet']['item'], list):\n for item in instance['instancesSet']['item']:\n name = _extract_name_tag(item)\n ret[name] = item\n ret[name]['name'] = name\n ret[name].update(\n dict(\n id=item['instanceId'],\n image=item['imageId'],\n size=item['instanceType'],\n state=item['instanceState']['name'],\n private_ips=item.get('privateIpAddress', []),\n public_ips=item.get('ipAddress', [])\n )\n )\n else:\n item = instance['instancesSet']['item']\n name = _extract_name_tag(item)\n ret[name] = item\n ret[name]['name'] = name\n ret[name].update(\n dict(\n id=item['instanceId'],\n image=item['imageId'],\n size=item['instanceType'],\n state=item['instanceState']['name'],\n private_ips=item.get('privateIpAddress', []),\n public_ips=item.get('ipAddress', [])\n )\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of the VMs that are in this location", "response": "def _list_nodes_full(location=None):\n '''\n Return a list of the VMs that in this location\n '''\n provider = __active_provider_name__ or 'ec2'\n if ':' in provider:\n comps = provider.split(':')\n provider = comps[0]\n\n params = {'Action': 'DescribeInstances'}\n instances = aws.query(params,\n location=location,\n provider=provider,\n opts=__opts__,\n sigver='4')\n if 'error' in instances:\n raise SaltCloudSystemExit(\n 'An error occurred while listing nodes: {0}'.format(\n instances['error']['Errors']['Error']['Message']\n )\n )\n\n ret = _extract_instance_info(instances)\n\n __utils__['cloud.cache_node_list'](ret, provider, __opts__)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of the VMs that are on the provider that are not on the provider. Only a list of VM names and their state is returned.", "response": "def list_nodes_min(location=None, call=None):\n '''\n Return a list of the VMs that are on the provider. Only a list of VM names,\n and their state, is returned. This is the minimum amount of information\n needed to check for existing VMs.\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes_min function must be called with -f or --function.'\n )\n\n ret = {}\n params = {'Action': 'DescribeInstances'}\n instances = aws.query(params,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n if 'error' in instances:\n raise SaltCloudSystemExit(\n 'An error occurred while listing nodes: {0}'.format(\n instances['error']['Errors']['Error']['Message']\n )\n )\n\n for instance in instances:\n if isinstance(instance['instancesSet']['item'], list):\n items = instance['instancesSet']['item']\n else:\n items = [instance['instancesSet']['item']]\n\n for item in items:\n state = item['instanceState']['name']\n name = _extract_name_tag(item)\n id = item['instanceId']\n ret[name] = {'state': state, 'id': id}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a list of the VMs that are on the provider with select fields", "response": "def list_nodes_select(call=None):\n '''\n Return a list of the VMs that are on the provider, with select fields\n '''\n return salt.utils.cloud.list_nodes_select(\n list_nodes_full(get_location()), __opts__['query.selection'], call,\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nshow the details from EC2 concerning an instance s termination protection state", "response": "def show_term_protect(name=None, instance_id=None, call=None, quiet=False):\n '''\n Show the details from EC2 concerning an instance's termination protection state\n\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The show_term_protect action must be called with -a or --action.'\n )\n\n if not instance_id:\n instance_id = _get_node(name)['instanceId']\n params = {'Action': 'DescribeInstanceAttribute',\n 'InstanceId': instance_id,\n 'Attribute': 'disableApiTermination'}\n result = aws.query(params,\n location=get_location(),\n provider=get_provider(),\n return_root=True,\n opts=__opts__,\n sigver='4')\n\n disable_protect = False\n for item in result:\n if 'value' in item:\n disable_protect = item['value']\n break\n\n log.log(\n logging.DEBUG if quiet is True else logging.INFO,\n 'Termination Protection is %s for %s',\n disable_protect == 'true' and 'enabled' or 'disabled', name\n )\n\n return disable_protect"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nshow the details from EC2 regarding cloudwatch detailed monitoring.", "response": "def show_detailed_monitoring(name=None, instance_id=None, call=None, quiet=False):\n '''\n Show the details from EC2 regarding cloudwatch detailed monitoring.\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The show_detailed_monitoring action must be called with -a or --action.'\n )\n location = get_location()\n if six.text_type(name).startswith('i-') and (len(name) == 10 or len(name) == 19):\n instance_id = name\n\n if not name and not instance_id:\n raise SaltCloudSystemExit(\n 'The show_detailed_monitoring action must be provided with a name or instance\\\n ID'\n )\n matched = _get_node(name=name, instance_id=instance_id, location=location)\n log.log(\n logging.DEBUG if quiet is True else logging.INFO,\n 'Detailed Monitoring is %s for %s', matched['monitoring'], name\n )\n return matched['monitoring']"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nenable or disable termination protection on a node", "response": "def _toggle_term_protect(name, value):\n '''\n Enable or Disable termination protection on a node\n\n '''\n instance_id = _get_node(name)['instanceId']\n params = {'Action': 'ModifyInstanceAttribute',\n 'InstanceId': instance_id,\n 'DisableApiTermination.Value': value}\n\n result = aws.query(params,\n location=get_location(),\n provider=get_provider(),\n return_root=True,\n opts=__opts__,\n sigver='4')\n\n return show_term_protect(name=name, instance_id=instance_id, call='action')"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef disable_detailed_monitoring(name, call=None):\n '''\n Enable/disable detailed monitoring on a node\n\n CLI Example:\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The enable_term_protect action must be called with '\n '-a or --action.'\n )\n\n instance_id = _get_node(name)['instanceId']\n params = {'Action': 'UnmonitorInstances',\n 'InstanceId.1': instance_id}\n\n result = aws.query(params,\n location=get_location(),\n provider=get_provider(),\n return_root=True,\n opts=__opts__,\n sigver='4')\n\n return show_detailed_monitoring(name=name, instance_id=instance_id, call='action')", "response": "Enable or disable detailed monitoring on a node."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nshow the details of the specified EBS volumes on instance termination.", "response": "def show_delvol_on_destroy(name, kwargs=None, call=None):\n '''\n Do not delete all/specified EBS volumes upon instance termination\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -a show_delvol_on_destroy mymachine\n '''\n\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The show_delvol_on_destroy action must be called '\n 'with -a or --action.'\n )\n\n if not kwargs:\n kwargs = {}\n\n instance_id = kwargs.get('instance_id', None)\n device = kwargs.get('device', None)\n volume_id = kwargs.get('volume_id', None)\n\n if instance_id is None:\n instance_id = _get_node(name)['instanceId']\n\n params = {'Action': 'DescribeInstances',\n 'InstanceId.1': instance_id}\n\n data = aws.query(params,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n blockmap = data[0]['instancesSet']['item']['blockDeviceMapping']\n\n if not isinstance(blockmap['item'], list):\n blockmap['item'] = [blockmap['item']]\n\n items = []\n\n for idx, item in enumerate(blockmap['item']):\n device_name = item['deviceName']\n\n if device is not None and device != device_name:\n continue\n\n if volume_id is not None and volume_id != item['ebs']['volumeId']:\n continue\n\n info = {\n 'device_name': device_name,\n 'volume_id': item['ebs']['volumeId'],\n 'deleteOnTermination': item['ebs']['deleteOnTermination']\n }\n\n items.append(info)\n\n return items"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndeleting all EBS volumes upon instance termination", "response": "def keepvol_on_destroy(name, kwargs=None, call=None):\n '''\n Do not delete all/specified EBS volumes upon instance termination\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -a keepvol_on_destroy mymachine\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The keepvol_on_destroy action must be called with -a or --action.'\n )\n\n if not kwargs:\n kwargs = {}\n\n device = kwargs.get('device', None)\n volume_id = kwargs.get('volume_id', None)\n\n return _toggle_delvol(name=name, device=device,\n volume_id=volume_id, value='false')"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef register_image(kwargs=None, call=None):\n '''\n Create an ami from a snapshot\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f register_image my-ec2-config ami_name=my_ami description=\"my description\"\n root_device_name=/dev/xvda snapshot_id=snap-xxxxxxxx\n '''\n\n if call != 'function':\n log.error(\n 'The create_volume function must be called with -f or --function.'\n )\n return False\n\n if 'ami_name' not in kwargs:\n log.error('ami_name must be specified to register an image.')\n return False\n\n block_device_mapping = kwargs.get('block_device_mapping', None)\n if not block_device_mapping:\n if 'snapshot_id' not in kwargs:\n log.error('snapshot_id or block_device_mapping must be specified to register an image.')\n return False\n if 'root_device_name' not in kwargs:\n log.error('root_device_name or block_device_mapping must be specified to register an image.')\n return False\n block_device_mapping = [{\n 'DeviceName': kwargs['root_device_name'],\n 'Ebs': {\n 'VolumeType': kwargs.get('volume_type', 'gp2'),\n 'SnapshotId': kwargs['snapshot_id'],\n }\n }]\n\n if not isinstance(block_device_mapping, list):\n block_device_mapping = [block_device_mapping]\n\n params = {'Action': 'RegisterImage',\n 'Name': kwargs['ami_name']}\n\n params.update(_param_from_config('BlockDeviceMapping', block_device_mapping))\n\n if 'root_device_name' in kwargs:\n params['RootDeviceName'] = kwargs['root_device_name']\n\n if 'description' in kwargs:\n params['Description'] = kwargs['description']\n\n if 'virtualization_type' in kwargs:\n params['VirtualizationType'] = kwargs['virtualization_type']\n\n if 'architecture' in kwargs:\n params['Architecture'] = kwargs['architecture']\n\n log.debug(params)\n\n data = aws.query(params,\n return_url=True,\n return_root=True,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n r_data = {}\n for d in data[0]:\n for k, v in d.items():\n r_data[k] = v\n\n return r_data", "response": "Register an image in a snapshot"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_volume(kwargs=None, call=None, wait_to_finish=False):\n '''\n Create a volume.\n\n zone\n The availability zone used to create the volume. Required. String.\n\n size\n The size of the volume, in GiBs. Defaults to ``10``. Integer.\n\n snapshot\n The snapshot-id from which to create the volume. Integer.\n\n type\n The volume type. This can be gp2 for General Purpose SSD, io1 for Provisioned\n IOPS SSD, st1 for Throughput Optimized HDD, sc1 for Cold HDD, or standard for\n Magnetic volumes. String.\n\n iops\n The number of I/O operations per second (IOPS) to provision for the volume,\n with a maximum ratio of 50 IOPS/GiB. Only valid for Provisioned IOPS SSD\n volumes. Integer.\n\n This option will only be set if ``type`` is also specified as ``io1``.\n\n encrypted\n Specifies whether the volume will be encrypted. Boolean.\n\n If ``snapshot`` is also given in the list of kwargs, then this value is ignored\n since volumes that are created from encrypted snapshots are also automatically\n encrypted.\n\n tags\n The tags to apply to the volume during creation. Dictionary.\n\n call\n The ``create_volume`` function must be called with ``-f`` or ``--function``.\n String.\n\n wait_to_finish\n Whether or not to wait for the volume to be available. Boolean. Defaults to\n ``False``.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f create_volume my-ec2-config zone=us-east-1b\n salt-cloud -f create_volume my-ec2-config zone=us-east-1b tags='{\"tag1\": \"val1\", \"tag2\", \"val2\"}'\n '''\n if call != 'function':\n log.error(\n 'The create_volume function must be called with -f or --function.'\n )\n return False\n\n if 'zone' not in kwargs:\n log.error('An availability zone must be specified to create a volume.')\n return False\n\n if 'size' not in kwargs and 'snapshot' not in kwargs:\n # This number represents GiB\n kwargs['size'] = '10'\n\n params = {'Action': 'CreateVolume',\n 'AvailabilityZone': kwargs['zone']}\n\n if 'size' in kwargs:\n params['Size'] = kwargs['size']\n\n if 'snapshot' in kwargs:\n params['SnapshotId'] = kwargs['snapshot']\n\n if 'type' in kwargs:\n params['VolumeType'] = kwargs['type']\n\n if 'iops' in kwargs and kwargs.get('type', 'standard') == 'io1':\n params['Iops'] = kwargs['iops']\n\n # You can't set `encrypted` if you pass a snapshot\n if 'encrypted' in kwargs and 'snapshot' not in kwargs:\n params['Encrypted'] = kwargs['encrypted']\n if 'kmskeyid' in kwargs:\n params['KmsKeyId'] = kwargs['kmskeyid']\n if 'kmskeyid' in kwargs and 'encrypted' not in kwargs:\n log.error(\n 'If a KMS Key ID is specified, encryption must be enabled'\n )\n return False\n\n log.debug(params)\n\n data = aws.query(params,\n return_url=True,\n return_root=True,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n r_data = {}\n for d in data[0]:\n for k, v in six.iteritems(d):\n r_data[k] = v\n volume_id = r_data['volumeId']\n\n # Allow tags to be set upon creation\n if 'tags' in kwargs:\n if isinstance(kwargs['tags'], six.string_types):\n tags = salt.utils.yaml.safe_load(kwargs['tags'])\n else:\n tags = kwargs['tags']\n\n if isinstance(tags, dict):\n new_tags = set_tags(tags=tags,\n resource_id=volume_id,\n call='action',\n location=get_location())\n r_data['tags'] = new_tags\n\n # Waits till volume is available\n if wait_to_finish:\n salt.utils.cloud.run_func_until_ret_arg(fun=describe_volumes,\n kwargs={'volume_id': volume_id},\n fun_call=call,\n argument_being_watched='status',\n required_argument_response='available')\n\n return r_data", "response": "Create a new volume in the specified availability zone."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nattach a volume to an instance", "response": "def attach_volume(name=None, kwargs=None, instance_id=None, call=None):\n '''\n Attach a volume to an instance\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The attach_volume action must be called with -a or --action.'\n )\n\n if not kwargs:\n kwargs = {}\n\n if 'instance_id' in kwargs:\n instance_id = kwargs['instance_id']\n\n if name and not instance_id:\n instance_id = _get_node(name)['instanceId']\n\n if not name and not instance_id:\n log.error('Either a name or an instance_id is required.')\n return False\n\n if 'volume_id' not in kwargs:\n log.error('A volume_id is required.')\n return False\n\n if 'device' not in kwargs:\n log.error('A device is required (ex. /dev/sdb1).')\n return False\n\n params = {'Action': 'AttachVolume',\n 'VolumeId': kwargs['volume_id'],\n 'InstanceId': instance_id,\n 'Device': kwargs['device']}\n\n log.debug(params)\n\n vm_ = get_configured_provider()\n\n data = salt.utils.cloud.wait_for_ip(\n __attach_vol_to_instance,\n update_args=(params, kwargs, instance_id),\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 interval_multiplier=config.get_cloud_config_value(\n 'wait_for_ip_interval_multiplier', vm_, __opts__, default=1),\n )\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef describe_volumes(kwargs=None, call=None):\n '''\n Describe a volume (or volumes)\n\n volume_id\n One or more volume IDs. Multiple IDs must be separated by \",\".\n\n TODO: Add all of the filters.\n '''\n if call != 'function':\n log.error(\n 'The describe_volumes function must be called with -f '\n 'or --function.'\n )\n return False\n\n if not kwargs:\n kwargs = {}\n\n params = {'Action': 'DescribeVolumes'}\n\n if 'volume_id' in kwargs:\n volume_id = kwargs['volume_id'].split(',')\n for volume_index, volume_id in enumerate(volume_id):\n params['VolumeId.{0}'.format(volume_index)] = volume_id\n\n log.debug(params)\n\n data = aws.query(params,\n return_url=True,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n return data", "response": "Describe a volume or volumes."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef create_keypair(kwargs=None, call=None):\n '''\n Create an SSH keypair\n '''\n if call != 'function':\n log.error(\n 'The create_keypair function must be called with -f or --function.'\n )\n return False\n\n if not kwargs:\n kwargs = {}\n\n if 'keyname' not in kwargs:\n log.error('A keyname is required.')\n return False\n\n params = {'Action': 'CreateKeyPair',\n 'KeyName': kwargs['keyname']}\n\n data = aws.query(params,\n return_url=True,\n return_root=True,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n keys = [x for x in data[0] if 'requestId' not in x]\n\n return (keys, data[1])", "response": "Create an SSH keypair"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nimporting an SSH public key from a file.", "response": "def import_keypair(kwargs=None, call=None):\n '''\n Import an SSH public key.\n\n .. versionadded:: 2015.8.3\n '''\n if call != 'function':\n log.error(\n 'The import_keypair function must be called with -f or --function.'\n )\n return False\n\n if not kwargs:\n kwargs = {}\n\n if 'keyname' not in kwargs:\n log.error('A keyname is required.')\n return False\n\n if 'file' not in kwargs:\n log.error('A public key file is required.')\n return False\n\n params = {'Action': 'ImportKeyPair',\n 'KeyName': kwargs['keyname']}\n\n public_key_file = kwargs['file']\n\n if os.path.exists(public_key_file):\n with salt.utils.files.fopen(public_key_file, 'r') as fh_:\n public_key = salt.utils.stringutils.to_unicode(fh_.read())\n\n if public_key is not None:\n params['PublicKeyMaterial'] = base64.b64encode(public_key)\n\n data = aws.query(params,\n return_url=True,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete_keypair(kwargs=None, call=None):\n '''\n Delete an SSH keypair\n '''\n if call != 'function':\n log.error(\n 'The delete_keypair function must be called with -f or --function.'\n )\n return False\n\n if not kwargs:\n kwargs = {}\n\n if 'keyname' not in kwargs:\n log.error('A keyname is required.')\n return False\n\n params = {'Action': 'DeleteKeyPair',\n 'KeyName': kwargs['keyname']}\n\n data = aws.query(params,\n return_url=True,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n return data", "response": "Delete an SSH keypair"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_snapshot(kwargs=None, call=None, wait_to_finish=False):\n '''\n Create a snapshot.\n\n volume_id\n The ID of the Volume from which to create a snapshot.\n\n description\n The optional description of the snapshot.\n\n CLI Exampe:\n\n .. code-block:: bash\n\n salt-cloud -f create_snapshot my-ec2-config volume_id=vol-351d8826\n salt-cloud -f create_snapshot my-ec2-config volume_id=vol-351d8826 \\\\\n description=\"My Snapshot Description\"\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The create_snapshot function must be called with -f '\n 'or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n volume_id = kwargs.get('volume_id', None)\n description = kwargs.get('description', '')\n\n if volume_id is None:\n raise SaltCloudSystemExit(\n 'A volume_id must be specified to create a snapshot.'\n )\n\n params = {'Action': 'CreateSnapshot',\n 'VolumeId': volume_id,\n 'Description': description}\n\n log.debug(params)\n\n data = aws.query(params,\n return_url=True,\n return_root=True,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')[0]\n\n r_data = {}\n for d in data:\n for k, v in six.iteritems(d):\n r_data[k] = v\n\n if 'snapshotId' in r_data:\n snapshot_id = r_data['snapshotId']\n\n # Waits till volume is available\n if wait_to_finish:\n salt.utils.cloud.run_func_until_ret_arg(fun=describe_snapshots,\n kwargs={'snapshot_id': snapshot_id},\n fun_call=call,\n argument_being_watched='status',\n required_argument_response='completed')\n\n return r_data", "response": "Creates a snapshot on the specified volume."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncopying a snapshot of a resource.", "response": "def copy_snapshot(kwargs=None, call=None):\n '''\n Copy a snapshot\n '''\n if call != 'function':\n log.error(\n 'The copy_snapshot function must be called with -f or --function.'\n )\n return False\n\n if 'source_region' not in kwargs:\n log.error('A source_region must be specified to copy a snapshot.')\n return False\n\n if 'source_snapshot_id' not in kwargs:\n log.error('A source_snapshot_id must be specified to copy a snapshot.')\n return False\n\n if 'description' not in kwargs:\n kwargs['description'] = ''\n\n params = {'Action': 'CopySnapshot'}\n\n if 'source_region' in kwargs:\n params['SourceRegion'] = kwargs['source_region']\n\n if 'source_snapshot_id' in kwargs:\n params['SourceSnapshotId'] = kwargs['source_snapshot_id']\n\n if 'description' in kwargs:\n params['Description'] = kwargs['description']\n\n log.debug(params)\n\n data = aws.query(params,\n return_url=True,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef describe_snapshots(kwargs=None, call=None):\n '''\n Describe a snapshot (or snapshots)\n\n snapshot_id\n One or more snapshot IDs. Multiple IDs must be separated by \",\".\n\n owner\n Return the snapshots owned by the specified owner. Valid values\n include: self, amazon, . Multiple values must be\n separated by \",\".\n\n restorable_by\n One or more AWS accounts IDs that can create volumes from the snapshot.\n Multiple aws account IDs must be separated by \",\".\n\n TODO: Add all of the filters.\n '''\n if call != 'function':\n log.error(\n 'The describe_snapshot function must be called with -f '\n 'or --function.'\n )\n return False\n\n params = {'Action': 'DescribeSnapshots'}\n\n # The AWS correct way is to use non-plurals like snapshot_id INSTEAD of snapshot_ids.\n if 'snapshot_ids' in kwargs:\n kwargs['snapshot_id'] = kwargs['snapshot_ids']\n\n if 'snapshot_id' in kwargs:\n snapshot_ids = kwargs['snapshot_id'].split(',')\n for snapshot_index, snapshot_id in enumerate(snapshot_ids):\n params['SnapshotId.{0}'.format(snapshot_index)] = snapshot_id\n\n if 'owner' in kwargs:\n owners = kwargs['owner'].split(',')\n for owner_index, owner in enumerate(owners):\n params['Owner.{0}'.format(owner_index)] = owner\n\n if 'restorable_by' in kwargs:\n restorable_bys = kwargs['restorable_by'].split(',')\n for restorable_by_index, restorable_by in enumerate(restorable_bys):\n params[\n 'RestorableBy.{0}'.format(restorable_by_index)\n ] = restorable_by\n\n log.debug(params)\n\n data = aws.query(params,\n return_url=True,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n return data", "response": "Describe a snapshot or snapshots."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget the console output from the instance.", "response": "def get_console_output(\n name=None,\n location=None,\n instance_id=None,\n call=None,\n kwargs=None,\n ):\n '''\n Show the console output from the instance.\n\n By default, returns decoded data, not the Base64-encoded data that is\n actually returned from the EC2 API.\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The get_console_output action must be called with '\n '-a or --action.'\n )\n\n if location is None:\n location = get_location()\n\n if not instance_id:\n instance_id = _get_node(name)['instanceId']\n\n if kwargs is None:\n kwargs = {}\n\n if instance_id is None:\n if 'instance_id' in kwargs:\n instance_id = kwargs['instance_id']\n del kwargs['instance_id']\n\n params = {'Action': 'GetConsoleOutput',\n 'InstanceId': instance_id}\n\n ret = {}\n data = aws.query(params,\n return_root=True,\n location=location,\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n for item in data:\n if next(six.iterkeys(item)) == 'output':\n ret['output_decoded'] = binascii.a2b_base64(next(six.itervalues(item)))\n else:\n ret[next(six.iterkeys(item))] = next(six.itervalues(item))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_password_data(\n name=None,\n kwargs=None,\n instance_id=None,\n call=None,\n ):\n '''\n Return password data for a Windows instance.\n\n By default only the encrypted password data will be returned. However, if a\n key_file is passed in, then a decrypted password will also be returned.\n\n Note that the key_file references the private key that was used to generate\n the keypair associated with this instance. This private key will _not_ be\n transmitted to Amazon; it is only used internally inside of Salt Cloud to\n decrypt data _after_ it has been received from Amazon.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -a get_password_data mymachine\n salt-cloud -a get_password_data mymachine key_file=/root/ec2key.pem\n\n Note: PKCS1_v1_5 was added in PyCrypto 2.5\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The get_password_data action must be called with '\n '-a or --action.'\n )\n\n if not instance_id:\n instance_id = _get_node(name)['instanceId']\n\n if kwargs is None:\n kwargs = {}\n\n if instance_id is None:\n if 'instance_id' in kwargs:\n instance_id = kwargs['instance_id']\n del kwargs['instance_id']\n\n params = {'Action': 'GetPasswordData',\n 'InstanceId': instance_id}\n\n ret = {}\n data = aws.query(params,\n return_root=True,\n location=get_location(),\n provider=get_provider(),\n opts=__opts__,\n sigver='4')\n\n for item in data:\n ret[next(six.iterkeys(item))] = next(six.itervalues(item))\n\n if not HAS_M2 and not HAS_PYCRYPTO:\n return ret\n\n if 'key' not in kwargs:\n if 'key_file' in kwargs:\n with salt.utils.files.fopen(kwargs['key_file'], 'r') as kf_:\n kwargs['key'] = salt.utils.stringutils.to_unicode(kf_.read())\n\n if 'key' in kwargs:\n pwdata = ret.get('passwordData', None)\n if pwdata is not None:\n rsa_key = kwargs['key']\n pwdata = base64.b64decode(pwdata)\n if HAS_M2:\n key = RSA.load_key_string(rsa_key.encode('ascii'))\n password = key.private_decrypt(pwdata, RSA.pkcs1_padding)\n else:\n dsize = Crypto.Hash.SHA.digest_size\n sentinel = Crypto.Random.new().read(15 + dsize)\n key_obj = Crypto.PublicKey.RSA.importKey(rsa_key)\n key_obj = PKCS1_v1_5.new(key_obj)\n password = key_obj.decrypt(pwdata, sentinel)\n ret['password'] = salt.utils.stringutils.to_unicode(password)\n\n return ret", "response": "Get the encrypted password data for a Windows instance."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndownload most recent pricing information from AWS and convert to a local JSON file.", "response": "def update_pricing(kwargs=None, call=None):\n '''\n Download most recent pricing information from AWS and convert to a local\n JSON file.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f update_pricing my-ec2-config\n salt-cloud -f update_pricing my-ec2-config type=linux\n\n .. versionadded:: 2015.8.0\n '''\n sources = {\n 'linux': 'https://a0.awsstatic.com/pricing/1/ec2/linux-od.min.js',\n 'rhel': 'https://a0.awsstatic.com/pricing/1/ec2/rhel-od.min.js',\n 'sles': 'https://a0.awsstatic.com/pricing/1/ec2/sles-od.min.js',\n 'mswin': 'https://a0.awsstatic.com/pricing/1/ec2/mswin-od.min.js',\n 'mswinsql': 'https://a0.awsstatic.com/pricing/1/ec2/mswinSQL-od.min.js',\n 'mswinsqlweb': 'https://a0.awsstatic.com/pricing/1/ec2/mswinSQLWeb-od.min.js',\n }\n\n if kwargs is None:\n kwargs = {}\n\n if 'type' not in kwargs:\n for source in sources:\n _parse_pricing(sources[source], source)\n else:\n _parse_pricing(sources[kwargs['type']], kwargs['type'])"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _parse_pricing(url, name):\n '''\n Download and parse an individual pricing file from AWS\n\n .. versionadded:: 2015.8.0\n '''\n price_js = http.query(url, text=True)\n\n items = []\n current_item = ''\n\n price_js = re.sub(JS_COMMENT_RE, '', price_js['text'])\n price_js = price_js.strip().rstrip(');').lstrip('callback(')\n for keyword in (\n 'vers',\n 'config',\n 'rate',\n 'valueColumns',\n 'currencies',\n 'instanceTypes',\n 'type',\n 'ECU',\n 'storageGB',\n 'name',\n 'vCPU',\n 'memoryGiB',\n 'storageGiB',\n 'USD',\n ):\n price_js = price_js.replace(keyword, '\"{0}\"'.format(keyword))\n\n for keyword in ('region', 'price', 'size'):\n price_js = price_js.replace(keyword, '\"{0}\"'.format(keyword))\n price_js = price_js.replace('\"{0}\"s'.format(keyword), '\"{0}s\"'.format(keyword))\n\n price_js = price_js.replace('\"\"', '\"')\n\n # Turn the data into something that's easier/faster to process\n regions = {}\n price_json = salt.utils.json.loads(price_js)\n for region in price_json['config']['regions']:\n sizes = {}\n for itype in region['instanceTypes']:\n for size in itype['sizes']:\n sizes[size['size']] = size\n regions[region['region']] = sizes\n\n outfile = os.path.join(\n __opts__['cachedir'], 'ec2-pricing-{0}.p'.format(name)\n )\n with salt.utils.files.fopen(outfile, 'w') as fho:\n salt.utils.msgpack.dump(regions, fho)\n\n return True", "response": "Download and parse an individual pricing file from AWS and return the contents of the individual pricing file"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef show_pricing(kwargs=None, call=None):\n '''\n Show pricing for a particular profile. This is only an estimate, based on\n unofficial pricing sources.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f show_pricing my-ec2-config profile=my-profile\n\n If pricing sources have not been cached, they will be downloaded. Once they\n have been cached, they will not be updated automatically. To manually update\n all prices, use the following command:\n\n .. code-block:: bash\n\n salt-cloud -f update_pricing \n\n .. versionadded:: 2015.8.0\n '''\n profile = __opts__['profiles'].get(kwargs['profile'], {})\n if not profile:\n return {'Error': 'The requested profile was not found'}\n\n # Make sure the profile belongs to ec2\n provider = profile.get('provider', '0:0')\n comps = provider.split(':')\n if len(comps) < 2 or comps[1] != 'ec2':\n return {'Error': 'The requested profile does not belong to EC2'}\n\n image_id = profile.get('image', None)\n image_dict = show_image({'image': image_id}, 'function')\n image_info = image_dict[0]\n\n # Find out what platform it is\n if image_info.get('imageOwnerAlias', '') == 'amazon':\n if image_info.get('platform', '') == 'windows':\n image_description = image_info.get('description', '')\n if 'sql' in image_description.lower():\n if 'web' in image_description.lower():\n name = 'mswinsqlweb'\n else:\n name = 'mswinsql'\n else:\n name = 'mswin'\n elif image_info.get('imageLocation', '').strip().startswith('amazon/suse'):\n name = 'sles'\n else:\n name = 'linux'\n elif image_info.get('imageOwnerId', '') == '309956199498':\n name = 'rhel'\n else:\n name = 'linux'\n\n pricefile = os.path.join(\n __opts__['cachedir'], 'ec2-pricing-{0}.p'.format(name)\n )\n\n if not os.path.isfile(pricefile):\n update_pricing({'type': name}, 'function')\n\n with salt.utils.files.fopen(pricefile, 'r') as fhi:\n ec2_price = salt.utils.stringutils.to_unicode(\n salt.utils.msgpack.load(fhi))\n\n region = get_location(profile)\n size = profile.get('size', None)\n if size is None:\n return {'Error': 'The requested profile does not contain a size'}\n\n try:\n raw = ec2_price[region][size]\n except KeyError:\n return {'Error': 'The size ({0}) in the requested profile does not have '\n 'a price associated with it for the {1} region'.format(size, region)}\n\n ret = {}\n if kwargs.get('raw', False):\n ret['_raw'] = raw\n\n ret['per_hour'] = 0\n for col in raw.get('valueColumns', []):\n ret['per_hour'] += decimal.Decimal(col['prices'].get('USD', 0))\n\n ret['per_hour'] = decimal.Decimal(ret['per_hour'])\n ret['per_day'] = ret['per_hour'] * 24\n ret['per_week'] = ret['per_day'] * 7\n ret['per_month'] = ret['per_day'] * 30\n ret['per_year'] = ret['per_week'] * 52\n\n return {profile['profile']: ret}", "response": "Show pricing for a particular profile"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef ssm_create_association(name=None, kwargs=None, instance_id=None, call=None):\n '''\n Associates the specified SSM document with the specified instance\n\n http://docs.aws.amazon.com/ssm/latest/APIReference/API_CreateAssociation.html\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -a ssm_create_association ec2-instance-name ssm_document=ssm-document-name\n '''\n\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The ssm_create_association action must be called with '\n '-a or --action.'\n )\n\n if not kwargs:\n kwargs = {}\n\n if 'instance_id' in kwargs:\n instance_id = kwargs['instance_id']\n\n if name and not instance_id:\n instance_id = _get_node(name)['instanceId']\n\n if not name and not instance_id:\n log.error('Either a name or an instance_id is required.')\n return False\n\n if 'ssm_document' not in kwargs:\n log.error('A ssm_document is required.')\n return False\n\n params = {'Action': 'CreateAssociation',\n 'InstanceId': instance_id,\n 'Name': kwargs['ssm_document']}\n\n result = aws.query(params,\n return_root=True,\n location=get_location(),\n provider=get_provider(),\n product='ssm',\n opts=__opts__,\n sigver='4')\n log.info(result)\n return result", "response": "Associate the specified SSM document with the specified instance."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nbuilding the URI for a given machine name", "response": "def _build_machine_uri(machine, cwd):\n '''\n returns string used to fetch id names from the sdb store.\n\n the cwd and machine name are concatenated with '?' which should\n never collide with a Salt node id -- which is important since we\n will be storing both in the same table.\n '''\n key = '{}?{}'.format(machine, os.path.abspath(cwd))\n return _build_sdb_uri(key)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nstoring the vm_ information keyed by name", "response": "def _update_vm_info(name, vm_):\n ''' store the vm_ information keyed by name '''\n __utils__['sdb.sdb_set'](_build_sdb_uri(name), vm_, __opts__)\n\n # store machine-to-name mapping, too\n if vm_['machine']:\n __utils__['sdb.sdb_set'](\n _build_machine_uri(vm_['machine'], vm_.get('cwd', '.')),\n name,\n __opts__)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_vm_info(name):\n '''\n get the information for a VM.\n\n :param name: salt_id name\n :return: dictionary of {'machine': x, 'cwd': y, ...}.\n '''\n try:\n vm_ = __utils__['sdb.sdb_get'](_build_sdb_uri(name), __opts__)\n except KeyError:\n raise SaltInvocationError(\n 'Probable sdb driver not found. Check your configuration.')\n if vm_ is None or 'machine' not in vm_:\n raise SaltInvocationError(\n 'No Vagrant machine defined for Salt_id {}'.format(name))\n return vm_", "response": "get the information for a VM."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_machine_id(machine, cwd):\n '''\n returns the salt_id name of the Vagrant VM\n\n :param machine: the Vagrant machine name\n :param cwd: the path to Vagrantfile\n :return: salt_id name\n '''\n name = __utils__['sdb.sdb_get'](_build_machine_uri(machine, cwd), __opts__)\n return name", "response": "Returns the salt_id name of the Vagrant VM\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nerase the information for a VM.", "response": "def _erase_vm_info(name):\n '''\n erase the information for a VM the we are destroying.\n\n some sdb drivers (such as the SQLite driver we expect to use)\n do not have a `delete` method, so if the delete fails, we have\n to replace the with a blank entry.\n '''\n try:\n # delete the machine record\n vm_ = get_vm_info(name)\n if vm_['machine']:\n key = _build_machine_uri(vm_['machine'], vm_.get('cwd', '.'))\n try:\n __utils__['sdb.sdb_delete'](key, __opts__)\n except KeyError:\n # no delete method found -- load a blank value\n __utils__['sdb.sdb_set'](key, None, __opts__)\n except Exception:\n pass\n\n uri = _build_sdb_uri(name)\n try:\n # delete the name record\n __utils__['sdb.sdb_delete'](uri, __opts__)\n except KeyError:\n # no delete method found -- load an empty dictionary\n __utils__['sdb.sdb_set'](uri, {}, __opts__)\n except Exception:\n pass"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget the information for ssh communication from the new VM", "response": "def _vagrant_ssh_config(vm_):\n '''\n get the information for ssh communication from the new VM\n\n :param vm_: the VM's info as we have it now\n :return: dictionary of ssh stuff\n '''\n machine = vm_['machine']\n log.info('requesting vagrant ssh-config for VM %s', machine or '(default)')\n cmd = 'vagrant ssh-config {}'.format(machine)\n reply = __salt__['cmd.shell'](cmd,\n runas=vm_.get('runas'),\n cwd=vm_.get('cwd'),\n ignore_retcode=True)\n ssh_config = {}\n for line in reply.split('\\n'): # build a dictionary of the text reply\n tokens = line.strip().split()\n if len(tokens) == 2: # each two-token line becomes a key:value pair\n ssh_config[tokens[0]] = tokens[1]\n log.debug('ssh_config=%s', repr(ssh_config))\n return ssh_config"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of the salt_id names of all available Vagrant VMs on this host without regard to the path where they are defined.", "response": "def list_domains():\n '''\n Return a list of the salt_id names of all available Vagrant VMs on\n this host without regard to the path where they are defined.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' vagrant.list_domains --log-level=info\n\n The log shows information about all known Vagrant environments\n on this machine. This data is cached and may not be completely\n up-to-date.\n '''\n vms = []\n cmd = 'vagrant global-status'\n reply = __salt__['cmd.shell'](cmd)\n log.info('--->\\n%s', reply)\n for line in reply.split('\\n'): # build a list of the text reply\n tokens = line.strip().split()\n try:\n _ = int(tokens[0], 16) # valid id numbers are hexadecimal\n except (ValueError, IndexError):\n continue # skip lines without valid id numbers\n machine = tokens[1]\n cwd = tokens[-1]\n name = get_machine_id(machine, cwd)\n if name:\n vms.append(name)\n return vms"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list_active_vms(cwd=None):\n '''\n Return a list of machine names for active virtual machine on the host,\n which are defined in the Vagrantfile at the indicated path.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' vagrant.list_active_vms cwd=/projects/project_1\n '''\n vms = []\n cmd = 'vagrant status'\n reply = __salt__['cmd.shell'](cmd, cwd=cwd)\n log.info('--->\\n%s', reply)\n for line in reply.split('\\n'): # build a list of the text reply\n tokens = line.strip().split()\n if len(tokens) > 1:\n if tokens[1] == 'running':\n vms.append(tokens[0])\n return vms", "response": "Return a list of machine names for active virtual machine on the host"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning list of information for all the VMs in the Vagrantfile that are in the specified state.", "response": "def vm_state(name='', cwd=None):\n '''\n Return list of information for all the vms indicating their state.\n\n If you pass a VM name in as an argument then it will return info\n for just the named VM, otherwise it will return all VMs defined by\n the Vagrantfile in the `cwd` directory.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' vagrant.vm_state cwd=/projects/project_1\n\n returns a list of dictionaries with machine name, state, provider,\n and salt_id name.\n\n .. code-block:: python\n\n datum = {'machine': _, # Vagrant machine name,\n 'state': _, # string indicating machine state, like 'running'\n 'provider': _, # the Vagrant VM provider\n 'name': _} # salt_id name\n\n Known bug: if there are multiple machines in your Vagrantfile, and you request\n the status of the ``primary`` machine, which you defined by leaving the ``machine``\n parameter blank, then you may receive the status of all of them.\n Please specify the actual machine name for each VM if there are more than one.\n\n '''\n\n if name:\n vm_ = get_vm_info(name)\n machine = vm_['machine']\n cwd = vm_['cwd'] or cwd # usually ignore passed-in cwd\n else:\n if not cwd:\n raise SaltInvocationError(\n 'Path to Vagranfile must be defined, but cwd={}'.format(cwd))\n machine = ''\n\n info = []\n cmd = 'vagrant status {}'.format(machine)\n reply = __salt__['cmd.shell'](cmd, cwd)\n log.info('--->\\n%s', reply)\n for line in reply.split('\\n'): # build a list of the text reply\n tokens = line.strip().split()\n if len(tokens) > 1 and tokens[-1].endswith(')'):\n try:\n datum = {'machine': tokens[0],\n 'state': ' '.join(tokens[1:-1]),\n 'provider': tokens[-1].lstrip('(').rstrip(')'),\n 'name': get_machine_id(tokens[0], cwd)\n }\n info.append(datum)\n except IndexError:\n pass\n return info"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ninitializes a new Vagrant VM.", "response": "def init(name, # Salt_id for created VM\n cwd=None, # path to find Vagrantfile\n machine='', # name of machine in Vagrantfile\n runas=None, # username who owns Vagrant box\n start=False, # start the machine when initialized\n vagrant_provider='', # vagrant provider (default=virtualbox)\n vm=None, # a dictionary of VM configuration settings\n ):\n '''\n Initialize a new Vagrant VM.\n\n This inputs all the information needed to start a Vagrant VM. These settings are stored in\n a Salt sdb database on the Vagrant host minion and used to start, control, and query the\n guest VMs. The salt_id assigned here is the key field for that database and must be unique.\n\n :param name: The salt_id name you will use to control this VM\n :param cwd: The path to the directory where the Vagrantfile is located\n :param machine: The machine name in the Vagrantfile. If blank, the primary machine will be used.\n :param runas: The username on the host who owns the Vagrant work files.\n :param start: (default: False) Start the virtual machine now.\n :param vagrant_provider: The name of a Vagrant VM provider (if not the default).\n :param vm: Optionally, all the above information may be supplied in this dictionary.\n :return: A string indicating success, or False.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt vagrant.init /path/to/Vagrantfile\n salt my_laptop vagrant.init x1 /projects/bevy_master machine=quail1\n '''\n vm_ = {} if vm is None else vm.copy() # passed configuration data\n vm_['name'] = name\n # passed-in keyword arguments overwrite vm dictionary values\n vm_['cwd'] = cwd or vm_.get('cwd')\n if not vm_['cwd']:\n raise SaltInvocationError('Path to Vagrantfile must be defined by \"cwd\" argument')\n vm_['machine'] = machine or vm_.get('machine', machine)\n vm_['runas'] = runas or vm_.get('runas', runas)\n vm_['vagrant_provider'] = vagrant_provider or vm_.get('vagrant_provider', '')\n _update_vm_info(name, vm_)\n\n if start:\n log.debug('Starting VM %s', name)\n ret = _start(name, vm_)\n else:\n ret = 'Name {} defined using VM {}'.format(name, vm_['machine'] or '(default)')\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef stop(name):\n '''\n Hard shutdown the virtual machine. (vagrant halt)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt vagrant.stop \n '''\n vm_ = get_vm_info(name)\n machine = vm_['machine']\n\n cmd = 'vagrant halt {}'.format(machine)\n ret = __salt__['cmd.retcode'](cmd,\n runas=vm_.get('runas'),\n cwd=vm_.get('cwd'))\n return ret == 0", "response": "Hard shutdown the virtual machine."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef reboot(name, provision=False):\n '''\n Reboot a VM. (vagrant reload)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt vagrant.reboot provision=True\n\n :param name: The salt_id name you will use to control this VM\n :param provision: (False) also re-run the Vagrant provisioning scripts.\n '''\n vm_ = get_vm_info(name)\n machine = vm_['machine']\n prov = '--provision' if provision else ''\n\n cmd = 'vagrant reload {} {}'.format(machine, prov)\n ret = __salt__['cmd.retcode'](cmd,\n runas=vm_.get('runas'),\n cwd=vm_.get('cwd'))\n return ret == 0", "response": "Reboot a VM. (vagrant reload)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt vagrant.reboot provision=True\n\n :param name: The salt_id name you will use to control this VM\n :param provision: (False) also re-run the Vagrant provisioning scripts."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndestroys and delete a virtual machine.", "response": "def destroy(name):\n '''\n Destroy and delete a virtual machine. (vagrant destroy -f)\n\n This also removes the salt_id name defined by vagrant.init.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt vagrant.destroy \n '''\n vm_ = get_vm_info(name)\n machine = vm_['machine']\n\n cmd = 'vagrant destroy -f {}'.format(machine)\n\n ret = __salt__['cmd.run_all'](cmd,\n runas=vm_.get('runas'),\n cwd=vm_.get('cwd'),\n output_loglevel='info')\n if ret['retcode'] == 0:\n _erase_vm_info(name)\n return 'Destroyed VM {0}'.format(name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconfiguring a virtual machine.", "response": "def vm_configured(name, vm_name, cpu, memory, image, version, interfaces,\n disks, scsi_devices, serial_ports, datacenter, datastore,\n placement, cd_dvd_drives=None, sata_controllers=None,\n advanced_configs=None, template=None, tools=True,\n power_on=False, deploy=False):\n '''\n Selects the correct operation to be executed on a virtual machine, non\n existing machines will be created, existing ones will be updated if the\n config differs.\n '''\n result = {'name': name,\n 'result': None,\n 'changes': {},\n 'comment': ''}\n\n log.trace('Validating virtual machine configuration')\n schema = ESXVirtualMachineConfigSchema.serialize()\n log.trace('schema = %s', schema)\n try:\n jsonschema.validate({'vm_name': vm_name,\n 'cpu': cpu,\n 'memory': memory,\n 'image': image,\n 'version': version,\n 'interfaces': interfaces,\n 'disks': disks,\n 'scsi_devices': scsi_devices,\n 'serial_ports': serial_ports,\n 'cd_dvd_drives': cd_dvd_drives,\n 'sata_controllers': sata_controllers,\n 'datacenter': datacenter,\n 'datastore': datastore,\n 'placement': placement,\n 'template': template,\n 'tools': tools,\n 'power_on': power_on,\n 'deploy': deploy}, schema)\n except jsonschema.exceptions.ValidationError as exc:\n raise salt.exceptions.InvalidConfigError(exc)\n\n service_instance = __salt__['vsphere.get_service_instance_via_proxy']()\n try:\n __salt__['vsphere.get_vm'](vm_name, vm_properties=['name'],\n service_instance=service_instance)\n except salt.exceptions.VMwareObjectRetrievalError:\n vm_file = __salt__['vsphere.get_vm_config_file'](\n vm_name, datacenter,\n placement, datastore,\n service_instance=service_instance)\n if vm_file:\n if __opts__['test']:\n result.update({'comment': 'The virtual machine {0}'\n ' will be registered.'.format(vm_name)})\n __salt__['vsphere.disconnect'](service_instance)\n return result\n result = vm_registered(vm_name, datacenter, placement,\n vm_file, power_on=power_on)\n return result\n else:\n if __opts__['test']:\n result.update({'comment': 'The virtual machine {0}'\n ' will be created.'.format(vm_name)})\n __salt__['vsphere.disconnect'](service_instance)\n return result\n if template:\n result = vm_cloned(name)\n else:\n result = vm_created(name, vm_name, cpu, memory, image, version,\n interfaces, disks, scsi_devices,\n serial_ports, datacenter, datastore,\n placement, cd_dvd_drives=cd_dvd_drives,\n advanced_configs=advanced_configs,\n power_on=power_on)\n return result\n\n result = vm_updated(name, vm_name, cpu, memory, image, version,\n interfaces, disks, scsi_devices,\n serial_ports, datacenter, datastore,\n cd_dvd_drives=cd_dvd_drives,\n sata_controllers=sata_controllers,\n advanced_configs=advanced_configs,\n power_on=power_on)\n __salt__['vsphere.disconnect'](service_instance)\n\n log.trace(result)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating a virtual machine configuration.", "response": "def vm_updated(name, vm_name, cpu, memory, image, version, interfaces,\n disks, scsi_devices, serial_ports, datacenter, datastore,\n cd_dvd_drives=None, sata_controllers=None,\n advanced_configs=None, power_on=False):\n '''\n Updates a virtual machine configuration if there is a difference between\n the given and deployed configuration.\n '''\n result = {'name': name,\n 'result': None,\n 'changes': {},\n 'comment': ''}\n\n service_instance = __salt__['vsphere.get_service_instance_via_proxy']()\n current_config = __salt__['vsphere.get_vm_config'](\n vm_name,\n datacenter=datacenter,\n objects=False,\n service_instance=service_instance)\n\n diffs = __salt__['vsphere.compare_vm_configs'](\n {'name': vm_name,\n 'cpu': cpu,\n 'memory': memory,\n 'image': image,\n 'version': version,\n 'interfaces': interfaces,\n 'disks': disks,\n 'scsi_devices': scsi_devices,\n 'serial_ports': serial_ports,\n 'datacenter': datacenter,\n 'datastore': datastore,\n 'cd_drives': cd_dvd_drives,\n 'sata_controllers': sata_controllers,\n 'advanced_configs': advanced_configs},\n current_config)\n if not diffs:\n result.update({\n 'result': True,\n 'changes': {},\n 'comment': 'Virtual machine {0} is already up to date'.format(vm_name)})\n return result\n\n if __opts__['test']:\n comment = 'State vm_updated will update virtual machine \\'{0}\\' ' \\\n 'in datacenter \\'{1}\\':\\n{2}'.format(vm_name,\n datacenter,\n '\\n'.join([':\\n'.join([key, difference.changes_str])\n for key, difference in six.iteritems(diffs)]))\n result.update({'result': None,\n 'comment': comment})\n __salt__['vsphere.disconnect'](service_instance)\n return result\n\n try:\n changes = __salt__['vsphere.update_vm'](vm_name, cpu, memory, image,\n version, interfaces, disks,\n scsi_devices, serial_ports,\n datacenter, datastore,\n cd_dvd_drives=cd_dvd_drives,\n sata_controllers=sata_controllers,\n advanced_configs=advanced_configs,\n service_instance=service_instance)\n except salt.exceptions.CommandExecutionError as exc:\n log.error('Error: %s', exc)\n if service_instance:\n __salt__['vsphere.disconnect'](service_instance)\n result.update({\n 'result': False,\n 'comment': six.text_type(exc)})\n return result\n\n if power_on:\n try:\n __salt__['vsphere.power_on_vm'](vm_name, datacenter)\n except salt.exceptions.VMwarePowerOnError as exc:\n log.error('Error: %s', exc)\n if service_instance:\n __salt__['vsphere.disconnect'](service_instance)\n result.update({\n 'result': False,\n 'comment': six.text_type(exc)})\n return result\n changes.update({'power_on': True})\n\n __salt__['vsphere.disconnect'](service_instance)\n\n result = {'name': name,\n 'result': True,\n 'changes': changes,\n 'comment': 'Virtual machine '\n '{0} was updated successfully'.format(vm_name)}\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a virtual machine with the given properties if it doesn t exist.", "response": "def vm_created(name, vm_name, cpu, memory, image, version, interfaces,\n disks, scsi_devices, serial_ports, datacenter, datastore,\n placement, ide_controllers=None, sata_controllers=None,\n cd_dvd_drives=None, advanced_configs=None, power_on=False):\n '''\n Creates a virtual machine with the given properties if it doesn't exist.\n '''\n result = {'name': name,\n 'result': None,\n 'changes': {},\n 'comment': ''}\n\n if __opts__['test']:\n result['comment'] = 'Virtual machine {0} will be created'.format(\n vm_name)\n return result\n\n service_instance = __salt__['vsphere.get_service_instance_via_proxy']()\n try:\n info = __salt__['vsphere.create_vm'](vm_name, cpu, memory, image,\n version, datacenter, datastore,\n placement, interfaces, disks,\n scsi_devices,\n serial_ports=serial_ports,\n ide_controllers=ide_controllers,\n sata_controllers=sata_controllers,\n cd_drives=cd_dvd_drives,\n advanced_configs=advanced_configs,\n service_instance=service_instance)\n except salt.exceptions.CommandExecutionError as exc:\n log.error('Error: %s', exc)\n if service_instance:\n __salt__['vsphere.disconnect'](service_instance)\n result.update({\n 'result': False,\n 'comment': six.text_type(exc)})\n return result\n\n if power_on:\n try:\n __salt__['vsphere.power_on_vm'](vm_name, datacenter,\n service_instance=service_instance)\n except salt.exceptions.VMwarePowerOnError as exc:\n log.error('Error: %s', exc)\n if service_instance:\n __salt__['vsphere.disconnect'](service_instance)\n result.update({\n 'result': False,\n 'comment': six.text_type(exc)})\n return result\n info['power_on'] = power_on\n\n changes = {'name': vm_name, 'info': info}\n __salt__['vsphere.disconnect'](service_instance)\n result = {'name': name,\n 'result': True,\n 'changes': changes,\n 'comment': 'Virtual machine '\n '{0} created successfully'.format(vm_name)}\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nregister a virtual machine with the main datastore.", "response": "def vm_registered(vm_name, datacenter, placement, vm_file, power_on=False):\n '''\n Registers a virtual machine if the machine files are available on\n the main datastore.\n '''\n result = {'name': vm_name,\n 'result': None,\n 'changes': {},\n 'comment': ''}\n\n vmx_path = '{0}{1}'.format(vm_file.folderPath, vm_file.file[0].path)\n log.trace('Registering virtual machine with vmx file: %s', vmx_path)\n service_instance = __salt__['vsphere.get_service_instance_via_proxy']()\n try:\n __salt__['vsphere.register_vm'](vm_name, datacenter,\n placement, vmx_path,\n service_instance=service_instance)\n except salt.exceptions.VMwareMultipleObjectsError as exc:\n log.error('Error: %s', exc)\n if service_instance:\n __salt__['vsphere.disconnect'](service_instance)\n result.update({'result': False,\n 'comment': six.text_type(exc)})\n return result\n except salt.exceptions.VMwareVmRegisterError as exc:\n log.error('Error: %s', exc)\n if service_instance:\n __salt__['vsphere.disconnect'](service_instance)\n result.update({'result': False,\n 'comment': six.text_type(exc)})\n return result\n\n if power_on:\n try:\n __salt__['vsphere.power_on_vm'](vm_name, datacenter,\n service_instance=service_instance)\n except salt.exceptions.VMwarePowerOnError as exc:\n log.error('Error: %s', exc)\n if service_instance:\n __salt__['vsphere.disconnect'](service_instance)\n result.update({\n 'result': False,\n 'comment': six.text_type(exc)})\n return result\n __salt__['vsphere.disconnect'](service_instance)\n result.update({'result': True,\n 'changes': {'name': vm_name, 'power_on': power_on},\n 'comment': 'Virtual machine '\n '{0} registered successfully'.format(vm_name)})\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nverifying that the given NPM package is installed at the correct version.", "response": "def installed(name,\n pkgs=None,\n dir=None,\n user=None,\n force_reinstall=False,\n registry=None,\n env=None):\n '''\n Verify that the given package is installed and is at the correct version\n (if specified).\n\n .. code-block:: yaml\n\n coffee-script:\n npm.installed:\n - user: someuser\n\n coffee-script@1.0.1:\n npm.installed: []\n\n name\n The package to install\n\n .. versionchanged:: 2014.7.2\n This parameter is no longer lowercased by salt so that\n case-sensitive NPM package names will work.\n\n pkgs\n A list of packages to install with a single npm invocation; specifying\n this argument will ignore the ``name`` argument\n\n .. versionadded:: 2014.7.0\n\n dir\n The target directory in which to install the package, or None for\n global installation\n\n user\n The user to run NPM with\n\n .. versionadded:: 0.17.0\n\n registry\n The NPM registry from which to install the package\n\n .. versionadded:: 2014.7.0\n\n env\n A list of environment variables to be set prior to execution. The\n format is the same as the :py:func:`cmd.run `.\n state function.\n\n .. versionadded:: 2014.7.0\n\n force_reinstall\n Install the package even if it is already installed\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n\n pkg_list = pkgs if pkgs else [name]\n\n try:\n installed_pkgs = __salt__['npm.list'](dir=dir, runas=user, env=env, depth=0)\n except (CommandNotFoundError, CommandExecutionError) as err:\n ret['result'] = False\n ret['comment'] = 'Error looking up \\'{0}\\': {1}'.format(name, err)\n return ret\n else:\n installed_pkgs = dict((p, info)\n for p, info in six.iteritems(installed_pkgs))\n\n pkgs_satisfied = []\n pkgs_to_install = []\n\n def _pkg_is_installed(pkg, installed_pkgs):\n '''\n Helper function to determine if a package is installed\n\n This performs more complex comparison than just checking\n keys, such as examining source repos to see if the package\n was installed by a different name from the same repo\n\n :pkg str: The package to compare\n :installed_pkgs: A dictionary produced by npm list --json\n '''\n if (pkg_name in installed_pkgs and\n 'version' in installed_pkgs[pkg_name]):\n return True\n # Check to see if we are trying to install from a URI\n elif '://' in pkg_name: # TODO Better way?\n for pkg_details in installed_pkgs.values():\n try:\n pkg_from = pkg_details.get('from', '').split('://')[1]\n # Catch condition where we may have specified package as\n # git://github.com/foo/bar but packager describes it as\n # git://github.com/foo/bar.git in the package\n if not pkg_from.endswith('.git') and pkg_name.startswith('git://'):\n pkg_from += '.git'\n if pkg_name.split('://')[1] == pkg_from:\n return True\n except IndexError:\n pass\n return False\n for pkg in pkg_list:\n # Valid:\n #\n # @google-cloud/bigquery@^0.9.6\n # @foobar\n # buffer-equal-constant-time@1.0.1\n # coffee-script\n matches = re.search(r'^(@?[^@\\s]+)(?:@(\\S+))?', pkg)\n pkg_name, pkg_ver = matches.group(1), matches.group(2) or None\n\n if force_reinstall is True:\n pkgs_to_install.append(pkg)\n continue\n if not _pkg_is_installed(pkg, installed_pkgs):\n pkgs_to_install.append(pkg)\n continue\n\n installed_name_ver = '{0}@{1}'.format(pkg_name,\n installed_pkgs[pkg_name]['version'])\n\n # If given an explicit version check the installed version matches.\n if pkg_ver:\n if installed_pkgs[pkg_name].get('version') != pkg_ver:\n pkgs_to_install.append(pkg)\n else:\n pkgs_satisfied.append(installed_name_ver)\n\n continue\n else:\n pkgs_satisfied.append(installed_name_ver)\n continue\n\n if __opts__['test']:\n ret['result'] = None\n\n comment_msg = []\n if pkgs_to_install:\n comment_msg.append('NPM package(s) \\'{0}\\' are set to be installed'\n .format(', '.join(pkgs_to_install)))\n\n ret['changes'] = {'old': [], 'new': pkgs_to_install}\n\n if pkgs_satisfied:\n comment_msg.append('Package(s) \\'{0}\\' satisfied by {1}'\n .format(', '.join(pkg_list), ', '.join(pkgs_satisfied)))\n ret['result'] = True\n\n ret['comment'] = '. '.join(comment_msg)\n return ret\n\n if not pkgs_to_install:\n ret['result'] = True\n ret['comment'] = ('Package(s) \\'{0}\\' satisfied by {1}'\n .format(', '.join(pkg_list), ', '.join(pkgs_satisfied)))\n return ret\n\n try:\n cmd_args = {\n 'dir': dir,\n 'runas': user,\n 'registry': registry,\n 'env': env,\n 'pkgs': pkg_list,\n }\n\n call = __salt__['npm.install'](**cmd_args)\n except (CommandNotFoundError, CommandExecutionError) as err:\n ret['result'] = False\n ret['comment'] = 'Error installing \\'{0}\\': {1}'.format(\n ', '.join(pkg_list), err)\n return ret\n\n if call and (isinstance(call, list) or isinstance(call, dict)):\n ret['result'] = True\n ret['changes'] = {'old': [], 'new': pkgs_to_install}\n ret['comment'] = 'Package(s) \\'{0}\\' successfully installed'.format(\n ', '.join(pkgs_to_install))\n else:\n ret['result'] = False\n ret['comment'] = 'Could not install package(s) \\'{0}\\''.format(\n ', '.join(pkg_list))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef removed(name, dir=None, user=None):\n '''\n Verify that the given package is not installed.\n\n dir\n The target directory in which to install the package, or None for\n global installation\n\n user\n The user to run NPM with\n\n .. versionadded:: 0.17.0\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n\n try:\n installed_pkgs = __salt__['npm.list'](dir=dir, depth=0)\n except (CommandExecutionError, CommandNotFoundError) as err:\n ret['result'] = False\n ret['comment'] = 'Error uninstalling \\'{0}\\': {1}'.format(name, err)\n return ret\n\n if name not in installed_pkgs:\n ret['result'] = True\n ret['comment'] = 'Package \\'{0}\\' is not installed'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Package \\'{0}\\' is set to be removed'.format(name)\n return ret\n\n if __salt__['npm.uninstall'](pkg=name, dir=dir, runas=user):\n ret['result'] = True\n ret['changes'][name] = 'Removed'\n ret['comment'] = 'Package \\'{0}\\' was successfully removed'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'Error removing package \\'{0}\\''.format(name)\n\n return ret", "response": "Verify that the given package is not installed and uninstalls it."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef bootstrap(name, user=None, silent=True):\n '''\n Bootstraps a node.js application.\n\n Will execute 'npm install --json' on the specified directory.\n\n user\n The user to run NPM with\n\n .. versionadded:: 0.17.0\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n\n if __opts__['test']:\n try:\n call = __salt__['npm.install'](dir=name, runas=user, pkg=None, silent=silent, dry_run=True)\n if call:\n ret['result'] = None\n ret['changes'] = {'old': [], 'new': call}\n ret['comment'] = '{0} is set to be bootstrapped'.format(name)\n else:\n ret['result'] = True\n ret['comment'] = '{0} is already bootstrapped'.format(name)\n except (CommandNotFoundError, CommandExecutionError) as err:\n ret['result'] = False\n ret['comment'] = 'Error Bootstrapping \\'{0}\\': {1}'.format(name, err)\n return ret\n\n try:\n call = __salt__['npm.install'](dir=name, runas=user, pkg=None, silent=silent)\n except (CommandNotFoundError, CommandExecutionError) as err:\n ret['result'] = False\n ret['comment'] = 'Error Bootstrapping \\'{0}\\': {1}'.format(name, err)\n return ret\n\n if not call:\n ret['result'] = True\n ret['comment'] = 'Directory is already bootstrapped'\n return ret\n\n # npm.install will return a string if it can't parse a JSON result\n if isinstance(call, six.string_types):\n ret['result'] = False\n ret['changes'] = call\n ret['comment'] = 'Could not bootstrap directory'\n else:\n ret['result'] = True\n ret['changes'] = {name: 'Bootstrapped'}\n ret['comment'] = 'Directory was successfully bootstrapped'\n\n return ret", "response": "Bootstraps a node. js application on the specified directory."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef cache_cleaned(name=None,\n user=None,\n force=False):\n '''\n Ensure that the given package is not cached.\n\n If no package is specified, this ensures the entire cache is cleared.\n\n name\n The name of the package to remove from the cache, or None for all packages\n\n user\n The user to run NPM with\n\n force\n Force cleaning of cache. Required for npm@5 and greater\n\n .. versionadded:: 2016.11.6\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n specific_pkg = None\n\n try:\n cached_pkgs = __salt__['npm.cache_list'](path=name, runas=user)\n except (CommandExecutionError, CommandNotFoundError) as err:\n ret['result'] = False\n ret['comment'] = 'Error looking up cached {0}: {1}'.format(\n name or 'packages', err)\n return ret\n\n if name:\n all_cached_pkgs = __salt__['npm.cache_list'](path=None, runas=user)\n # The first package is always the cache path\n cache_root_path = all_cached_pkgs[0]\n specific_pkg = '{0}/{1}/'.format(cache_root_path, name)\n\n if specific_pkg not in cached_pkgs:\n ret['result'] = True\n ret['comment'] = 'Package {0} is not in the cache'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Cached {0} set to be removed'.format(name or 'packages')\n return ret\n\n if __salt__['npm.cache_clean'](path=name, runas=user):\n ret['result'] = True\n ret['changes'][name or 'cache'] = 'Removed'\n ret['comment'] = 'Cached {0} successfully removed'.format(\n name or 'packages'\n )\n else:\n ret['result'] = False\n ret['comment'] = 'Error cleaning cached {0}'.format(name or 'packages')\n\n return ret", "response": "Ensure that the given package is not cached."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nensure a node is present in the system.", "response": "def present(name, profile=\"splunk\", **kwargs):\n '''\n Ensure a search is present\n\n .. code-block:: yaml\n\n API Error Search:\n splunk_search.present:\n search: index=main sourcetype=blah\n template: alert_5min\n\n The following parameters are required:\n\n name\n This is the name of the search in splunk\n '''\n ret = {\n 'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''\n }\n\n target = __salt__['splunk_search.get'](name, profile=profile)\n if target:\n if __opts__['test']:\n ret['comment'] = \"Would update {0}\".format(name)\n return ret\n # found a search... updating\n result = __salt__['splunk_search.update'](\n name, profile=profile, **kwargs\n )\n if not result:\n # no update\n ret['result'] = True\n ret['comment'] = \"No changes\"\n else:\n (newvalues, diffs) = result\n old_content = dict(target.content)\n old_changes = {}\n for x in newvalues:\n old_changes[x] = old_content.get(x, None)\n ret['result'] = True\n ret['changes']['diff'] = diffs\n ret['changes']['old'] = old_changes\n ret['changes']['new'] = newvalues\n else:\n if __opts__['test']:\n ret['comment'] = \"Would create {0}\".format(name)\n return ret\n # creating a new search\n result = __salt__['splunk_search.create'](\n name, profile=profile, **kwargs\n )\n if result:\n ret['result'] = True\n ret['changes']['old'] = False\n ret['changes']['new'] = kwargs\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to create {0}'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef absent(name, profile=\"splunk\"):\n '''\n Ensure a search is absent\n\n .. code-block:: yaml\n\n API Error Search:\n splunk_search.absent\n\n The following parameters are required:\n\n name\n This is the name of the search in splunk\n '''\n ret = {\n 'name': name,\n 'changes': {},\n 'result': True,\n 'comment': '{0} is absent.'.format(name)\n }\n\n target = __salt__['splunk_search.get'](name, profile=profile)\n if target:\n if __opts__['test']:\n ret = {}\n ret[\"name\"] = name\n ret['comment'] = \"Would delete {0}\".format(name)\n ret['result'] = None\n return ret\n\n result = __salt__['splunk_search.delete'](name, profile=profile)\n if result:\n ret['comment'] = '{0} was deleted'.format(name)\n else:\n ret['comment'] = 'Failed to delete {0}'.format(name)\n ret['result'] = False\n return ret", "response": "Ensure a node is absent in the search"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _bulk_state(saltfunc, lbn, workers, profile):\n '''\n Generic function for bulk worker operation\n '''\n ret = {'name': lbn,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n\n if not isinstance(workers, list):\n ret['result'] = False\n ret['comment'] = 'workers should be a list not a {0}'.format(\n type(workers)\n )\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n return ret\n\n log.info('executing %s to modjk workers %s', saltfunc, workers)\n try:\n cmdret = __salt__[saltfunc](workers, lbn, profile=profile)\n except KeyError:\n ret['result'] = False\n ret['comment'] = 'unsupported function {0}'.format(\n saltfunc\n )\n return ret\n\n errors = []\n for worker, ok in six.iteritems(cmdret):\n if not ok:\n errors.append(worker)\n\n ret['changes'] = {'status': cmdret}\n if errors:\n ret['result'] = False\n ret['comment'] = '{0} failed on some workers'.format(saltfunc)\n\n return ret", "response": "Generic function for bulk worker operation\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef worker_stopped(name, workers=None, profile='default'):\n '''\n Stop all the workers in the modjk load balancer\n\n Example:\n\n .. code-block:: yaml\n\n loadbalancer:\n modjk.worker_stopped:\n - workers:\n - app1\n - app2\n '''\n if workers is None:\n workers = []\n return _bulk_state(\n 'modjk.bulk_stop', name, workers, profile\n )", "response": "Stop all the workers in the modjk load balancer"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nactivating all the workers in the modjk load balancer", "response": "def worker_activated(name, workers=None, profile='default'):\n '''\n Activate all the workers in the modjk load balancer\n\n Example:\n\n .. code-block:: yaml\n\n loadbalancer:\n modjk.worker_activated:\n - workers:\n - app1\n - app2\n '''\n if workers is None:\n workers = []\n return _bulk_state(\n 'modjk.bulk_activate', name, workers, profile\n )"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef worker_disabled(name, workers=None, profile='default'):\n '''\n Disable all the workers in the modjk load balancer\n\n Example:\n\n .. code-block:: yaml\n\n loadbalancer:\n modjk.worker_disabled:\n - workers:\n - app1\n - app2\n '''\n if workers is None:\n workers = []\n return _bulk_state(\n 'modjk.bulk_disable', name, workers, profile\n )", "response": "Disable all the workers in the modjk load balancer"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef worker_recover(name, workers=None, profile='default'):\n '''\n Recover all the workers in the modjk load balancer\n\n Example:\n\n .. code-block:: yaml\n\n loadbalancer:\n modjk.worker_recover:\n - workers:\n - app1\n - app2\n '''\n if workers is None:\n workers = []\n return _bulk_state(\n 'modjk.bulk_recover', name, workers, profile\n )", "response": "Recover all the workers in the modjk load balancer"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing a port forwarding statement in the form used by this state from_port to_port protocol and destination addresses.", "response": "def _parse_forward(mapping):\n '''\n Parses a port forwarding statement in the form used by this state:\n\n from_port:to_port:protocol[:destination]\n\n and returns a ForwardingMapping object\n '''\n if len(mapping.split(':')) > 3:\n (srcport, destport, protocol, destaddr) = mapping.split(':')\n else:\n (srcport, destport, protocol) = mapping.split(':')\n destaddr = ''\n return ForwardingMapping(srcport, destport, protocol, destaddr)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef present(name,\n block_icmp=None,\n prune_block_icmp=False,\n default=None,\n masquerade=False,\n ports=None,\n prune_ports=False,\n port_fwd=None,\n prune_port_fwd=False,\n services=None,\n prune_services=False,\n interfaces=None,\n prune_interfaces=False,\n sources=None,\n prune_sources=False,\n rich_rules=None,\n prune_rich_rules=False):\n\n '''\n Ensure a zone has specific attributes.\n\n name\n The zone to modify.\n\n default : None\n Set this zone as the default zone if ``True``.\n\n masquerade : False\n Enable or disable masquerade for a zone.\n\n block_icmp : None\n List of ICMP types to block in the zone.\n\n prune_block_icmp : False\n If ``True``, remove all but the specified block_icmp from the zone.\n\n ports : None\n List of ports to add to the zone.\n\n prune_ports : False\n If ``True``, remove all but the specified ports from the zone.\n\n port_fwd : None\n List of port forwards to add to the zone.\n\n prune_port_fwd : False\n If ``True``, remove all but the specified port_fwd from the zone.\n\n services : None\n List of services to add to the zone.\n\n prune_services : False\n If ``True``, remove all but the specified services from the zone.\n .. note:: Currently defaults to True for compatibility, but will be changed to False in a future release.\n\n interfaces : None\n List of interfaces to add to the zone.\n\n prune_interfaces : False\n If ``True``, remove all but the specified interfaces from the zone.\n\n sources : None\n List of sources to add to the zone.\n\n prune_sources : False\n If ``True``, remove all but the specified sources from the zone.\n\n rich_rules : None\n List of rich rules to add to the zone.\n\n prune_rich_rules : False\n If ``True``, remove all but the specified rich rules from the zone.\n '''\n ret = _present(name, block_icmp, prune_block_icmp, default, masquerade, ports, prune_ports,\n port_fwd, prune_port_fwd, services, prune_services, interfaces, prune_interfaces,\n sources, prune_sources, rich_rules, prune_rich_rules)\n\n # Reload firewalld service on changes\n if ret['changes'] != {}:\n __salt__['firewalld.reload_rules']()\n\n return ret", "response": "Ensure a zone is present in the specified attributes."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef service(name,\n ports=None,\n protocols=None):\n '''\n Ensure the service exists and encompasses the specified ports and\n protocols.\n\n .. versionadded:: 2016.11.0\n '''\n ret = {'name': name,\n 'result': False,\n 'changes': {},\n 'comment': ''}\n\n if name not in __salt__['firewalld.get_services']():\n __salt__['firewalld.new_service'](name, restart=False)\n\n ports = ports or []\n\n try:\n _current_ports = __salt__['firewalld.get_service_ports'](name)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n new_ports = set(ports) - set(_current_ports)\n old_ports = set(_current_ports) - set(ports)\n\n for port in new_ports:\n if not __opts__['test']:\n try:\n __salt__['firewalld.add_service_port'](name, port)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n for port in old_ports:\n if not __opts__['test']:\n try:\n __salt__['firewalld.remove_service_port'](name, port)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if new_ports or old_ports:\n ret['changes'].update({'ports':\n {'old': _current_ports,\n 'new': ports}})\n\n protocols = protocols or []\n\n try:\n _current_protocols = __salt__['firewalld.get_service_protocols'](name)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n new_protocols = set(protocols) - set(_current_protocols)\n old_protocols = set(_current_protocols) - set(protocols)\n\n for protocol in new_protocols:\n if not __opts__['test']:\n try:\n __salt__['firewalld.add_service_protocol'](name, protocol)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n for protocol in old_protocols:\n if not __opts__['test']:\n try:\n __salt__['firewalld.remove_service_protocol'](name, protocol)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if new_protocols or old_protocols:\n ret['changes'].update({'protocols':\n {'old': _current_protocols,\n 'new': protocols}})\n\n if ret['changes'] != {}:\n __salt__['firewalld.reload_rules']()\n\n ret['result'] = True\n if ret['changes'] == {}:\n ret['comment'] = '\\'{0}\\' is already in the desired state.'.format(\n name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Configuration for \\'{0}\\' will change.'.format(name)\n return ret\n\n ret['comment'] = '\\'{0}\\' was configured.'.format(name)\n return ret", "response": "Ensure the named service exists and encompasses the specified ports and protocols."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _present(name,\n block_icmp=None,\n prune_block_icmp=False,\n default=None,\n masquerade=False,\n ports=None,\n prune_ports=False,\n port_fwd=None,\n prune_port_fwd=False,\n services=None,\n # TODO: prune_services=False in future release\n # prune_services=False,\n prune_services=None,\n interfaces=None,\n prune_interfaces=False,\n sources=None,\n prune_sources=False,\n rich_rules=None,\n prune_rich_rules=False):\n '''\n Ensure a zone has specific attributes.\n '''\n ret = {'name': name,\n 'result': False,\n 'changes': {},\n 'comment': ''}\n\n try:\n zones = __salt__['firewalld.get_zones'](permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if name not in zones:\n if not __opts__['test']:\n try:\n __salt__['firewalld.new_zone'](name)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n ret['changes'].update({name:\n {'old': zones,\n 'new': name}})\n\n if block_icmp or prune_block_icmp:\n block_icmp = block_icmp or []\n new_icmp_types = []\n old_icmp_types = []\n\n try:\n _current_icmp_blocks = __salt__['firewalld.list_icmp_block'](name,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if block_icmp:\n try:\n _valid_icmp_types = __salt__['firewalld.get_icmp_types'](\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n # log errors for invalid ICMP types in block_icmp input\n for icmp_type in set(block_icmp) - set(_valid_icmp_types):\n log.error('%s is an invalid ICMP type', icmp_type)\n block_icmp.remove(icmp_type)\n\n new_icmp_types = set(block_icmp) - set(_current_icmp_blocks)\n for icmp_type in new_icmp_types:\n if not __opts__['test']:\n try:\n __salt__['firewalld.block_icmp'](name, icmp_type,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if prune_block_icmp:\n old_icmp_types = set(_current_icmp_blocks) - set(block_icmp)\n for icmp_type in old_icmp_types:\n # no need to check against _valid_icmp_types here, because all\n # elements in old_icmp_types are guaranteed to be in\n # _current_icmp_blocks, whose elements are inherently valid\n if not __opts__['test']:\n try:\n __salt__['firewalld.allow_icmp'](name, icmp_type,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if new_icmp_types or old_icmp_types:\n # If we're not pruning, include current items in new output so it's clear\n # that they're still present\n if not prune_block_icmp:\n block_icmp = list(new_icmp_types | set(_current_icmp_blocks))\n ret['changes'].update({'icmp_types':\n {'old': _current_icmp_blocks,\n 'new': block_icmp}})\n\n # that's the only parameter that can't be permanent or runtime, it's\n # directly both\n if default:\n try:\n default_zone = __salt__['firewalld.default_zone']()\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n if name != default_zone:\n if not __opts__['test']:\n try:\n __salt__['firewalld.set_default_zone'](name)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n ret['changes'].update({'default':\n {'old': default_zone,\n 'new': name}})\n\n try:\n masquerade_ret = __salt__['firewalld.get_masquerade'](name,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if masquerade and not masquerade_ret:\n if not __opts__['test']:\n try:\n __salt__['firewalld.add_masquerade'](name, permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n ret['changes'].update({'masquerade':\n {'old': '',\n 'new': 'Masquerading successfully set.'}})\n elif not masquerade and masquerade_ret:\n if not __opts__['test']:\n try:\n __salt__['firewalld.remove_masquerade'](name,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n ret['changes'].update({'masquerade':\n {'old': '',\n 'new': 'Masquerading successfully '\n 'disabled.'}})\n\n if ports or prune_ports:\n ports = ports or []\n try:\n _current_ports = __salt__['firewalld.list_ports'](name, permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n new_ports = set(ports) - set(_current_ports)\n old_ports = []\n\n for port in new_ports:\n if not __opts__['test']:\n try:\n # TODO: force_masquerade to be removed in future release\n __salt__['firewalld.add_port'](name, port, permanent=True, force_masquerade=False)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if prune_ports:\n old_ports = set(_current_ports) - set(ports)\n for port in old_ports:\n if not __opts__['test']:\n try:\n __salt__['firewalld.remove_port'](name, port, permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if new_ports or old_ports:\n # If we're not pruning, include current items in new output so it's clear\n # that they're still present\n if not prune_ports:\n ports = list(new_ports | set(_current_ports))\n ret['changes'].update({'ports':\n {'old': _current_ports,\n 'new': ports}})\n\n if port_fwd or prune_port_fwd:\n port_fwd = port_fwd or []\n try:\n _current_port_fwd = __salt__['firewalld.list_port_fwd'](name,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n port_fwd = [_parse_forward(fwd) for fwd in port_fwd]\n _current_port_fwd = [\n ForwardingMapping(\n srcport=fwd['Source port'],\n destport=fwd['Destination port'],\n protocol=fwd['Protocol'],\n destaddr=fwd['Destination address']\n ) for fwd in _current_port_fwd]\n\n new_port_fwd = set(port_fwd) - set(_current_port_fwd)\n old_port_fwd = []\n\n for fwd in new_port_fwd:\n if not __opts__['test']:\n try:\n # TODO: force_masquerade to be removed in future release\n __salt__['firewalld.add_port_fwd'](name, fwd.srcport,\n fwd.destport, fwd.protocol, fwd.destaddr, permanent=True,\n force_masquerade=False)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if prune_port_fwd:\n old_port_fwd = set(_current_port_fwd) - set(port_fwd)\n for fwd in old_port_fwd:\n if not __opts__['test']:\n try:\n __salt__['firewalld.remove_port_fwd'](name, fwd.srcport,\n fwd.destport, fwd.protocol, fwd.destaddr, permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if new_port_fwd or old_port_fwd:\n # If we're not pruning, include current items in new output so it's clear\n # that they're still present\n if not prune_port_fwd:\n port_fwd = list(new_port_fwd | set(_current_port_fwd))\n ret['changes'].update({'port_fwd':\n {'old': [fwd.todict() for fwd in\n _current_port_fwd],\n 'new': [fwd.todict() for fwd in port_fwd]}})\n\n if services or prune_services:\n services = services or []\n try:\n _current_services = __salt__['firewalld.list_services'](name,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n new_services = set(services) - set(_current_services)\n old_services = []\n\n for new_service in new_services:\n if not __opts__['test']:\n try:\n __salt__['firewalld.add_service'](new_service, name,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if prune_services:\n old_services = set(_current_services) - set(services)\n for old_service in old_services:\n if not __opts__['test']:\n try:\n __salt__['firewalld.remove_service'](old_service, name,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if new_services or old_services:\n # If we're not pruning, include current items in new output so it's clear\n # that they're still present\n if not prune_services:\n services = list(new_services | set(_current_services))\n ret['changes'].update({'services':\n {'old': _current_services,\n 'new': services}})\n\n if interfaces or prune_interfaces:\n interfaces = interfaces or []\n try:\n _current_interfaces = __salt__['firewalld.get_interfaces'](name,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n new_interfaces = set(interfaces) - set(_current_interfaces)\n old_interfaces = []\n\n for interface in new_interfaces:\n if not __opts__['test']:\n try:\n __salt__['firewalld.add_interface'](name, interface,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if prune_interfaces:\n old_interfaces = set(_current_interfaces) - set(interfaces)\n for interface in old_interfaces:\n if not __opts__['test']:\n try:\n __salt__['firewalld.remove_interface'](name, interface,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if new_interfaces or old_interfaces:\n # If we're not pruning, include current items in new output so it's clear\n # that they're still present\n if not prune_interfaces:\n interfaces = list(new_interfaces | set(_current_interfaces))\n ret['changes'].update({'interfaces':\n {'old': _current_interfaces,\n 'new': interfaces}})\n\n if sources or prune_sources:\n sources = sources or []\n try:\n _current_sources = __salt__['firewalld.get_sources'](name,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n new_sources = set(sources) - set(_current_sources)\n old_sources = []\n\n for source in new_sources:\n if not __opts__['test']:\n try:\n __salt__['firewalld.add_source'](name, source, permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if prune_sources:\n old_sources = set(_current_sources) - set(sources)\n for source in old_sources:\n if not __opts__['test']:\n try:\n __salt__['firewalld.remove_source'](name, source,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if new_sources or old_sources:\n # If we're not pruning, include current items in new output so it's clear\n # that they're still present\n if not prune_sources:\n sources = list(new_sources | set(_current_sources))\n ret['changes'].update({'sources':\n {'old': _current_sources,\n 'new': sources}})\n\n if rich_rules or prune_rich_rules:\n rich_rules = rich_rules or []\n try:\n _current_rich_rules = __salt__['firewalld.get_rich_rules'](name,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n new_rich_rules = set(rich_rules) - set(_current_rich_rules)\n old_rich_rules = []\n\n for rich_rule in new_rich_rules:\n if not __opts__['test']:\n try:\n __salt__['firewalld.add_rich_rule'](name, rich_rule,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if prune_rich_rules:\n old_rich_rules = set(_current_rich_rules) - set(rich_rules)\n for rich_rule in old_rich_rules:\n if not __opts__['test']:\n try:\n __salt__['firewalld.remove_rich_rule'](name, rich_rule,\n permanent=True)\n except CommandExecutionError as err:\n ret['comment'] = 'Error: {0}'.format(err)\n return ret\n\n if new_rich_rules or old_rich_rules:\n # If we're not pruning, include current items in new output so it's clear\n # that they're still present\n if not prune_rich_rules:\n rich_rules = list(new_rich_rules | set(_current_rich_rules))\n ret['changes'].update({'rich_rules':\n {'old': _current_rich_rules,\n 'new': rich_rules}})\n\n # No changes\n if ret['changes'] == {}:\n ret['result'] = True\n ret['comment'] = '\\'{0}\\' is already in the desired state.'.format(name)\n return ret\n\n # test=True and changes predicted\n if __opts__['test']:\n ret['result'] = None\n # build comment string\n nested.__opts__ = __opts__\n comment = []\n comment.append('Configuration for \\'{0}\\' will change:'.format(name))\n comment.append(nested.output(ret['changes']).rstrip())\n ret['comment'] = '\\n'.join(comment)\n ret['changes'] = {}\n return ret\n\n # Changes were made successfully\n ret['result'] = True\n ret['comment'] = '\\'{0}\\' was configured.'.format(name)\n return ret", "response": "Ensure a zone is present in the cluster"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef todict(self):\n '''\n Returns a pretty dictionary meant for command line output.\n '''\n return {\n 'Source port': self.srcport,\n 'Destination port': self.destport,\n 'Protocol': self.protocol,\n 'Destination address': self.destaddr}", "response": "Returns a pretty dictionary meant for command line output."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _connect():\n '''\n Return server object used to interact with Jenkins.\n\n :return: server object used to interact with Jenkins\n '''\n jenkins_url = __salt__['config.get']('jenkins.url') or \\\n __salt__['config.get']('jenkins:url') or \\\n __salt__['pillar.get']('jenkins.url')\n\n jenkins_user = __salt__['config.get']('jenkins.user') or \\\n __salt__['config.get']('jenkins:user') or \\\n __salt__['pillar.get']('jenkins.user')\n\n jenkins_password = __salt__['config.get']('jenkins.password') or \\\n __salt__['config.get']('jenkins:password') or \\\n __salt__['pillar.get']('jenkins.password')\n\n if not jenkins_url:\n raise SaltInvocationError('No Jenkins URL found.')\n\n return jenkins.Jenkins(jenkins_url,\n username=jenkins_user,\n password=jenkins_password)", "response": "Connect to Jenkins and return the server object used to interact with Jenkins."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _retrieve_config_xml(config_xml, saltenv):\n '''\n Helper to cache the config XML and raise a CommandExecutionError if we fail\n to do so. If we successfully cache the file, return the cached path.\n '''\n ret = __salt__['cp.cache_file'](config_xml, saltenv)\n\n if not ret:\n raise CommandExecutionError('Failed to retrieve {0}'.format(config_xml))\n\n return ret", "response": "Helper to retrieve the config XML from the salt master"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nchecks whether the job exists in Jenkins jobs.", "response": "def job_exists(name=None):\n '''\n Check whether the job exists in configured Jenkins jobs.\n\n :param name: The name of the job is check if it exists.\n :return: True if job exists, False if job does not exist.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jenkins.job_exists jobname\n\n '''\n if not name:\n raise SaltInvocationError('Required parameter \\'name\\' is missing')\n\n server = _connect()\n if server.job_exists(name):\n return True\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn information about the Jenkins job.", "response": "def get_job_info(name=None):\n '''\n Return information about the Jenkins job.\n\n :param name: The name of the job is check if it exists.\n :return: Information about the Jenkins job.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jenkins.get_job_info jobname\n\n '''\n if not name:\n raise SaltInvocationError('Required parameter \\'name\\' is missing')\n\n server = _connect()\n\n if not job_exists(name):\n raise CommandExecutionError('Job \\'{0}\\' does not exist'.format(name))\n\n job_info = server.get_job_info(name)\n if job_info:\n return job_info\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ninitiates a build for the provided job.", "response": "def build_job(name=None, parameters=None):\n '''\n Initiate a build for the provided job.\n\n :param name: The name of the job is check if it exists.\n :param parameters: Parameters to send to the job.\n :return: True is successful, otherwise raise an exception.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jenkins.build_job jobname\n\n '''\n if not name:\n raise SaltInvocationError('Required parameter \\'name\\' is missing')\n\n server = _connect()\n\n if not job_exists(name):\n raise CommandExecutionError('Job \\'{0}\\' does not exist.'.format(name))\n\n try:\n server.build_job(name, parameters)\n except jenkins.JenkinsException as err:\n raise CommandExecutionError(\n 'Encountered error building job \\'{0}\\': {1}'.format(name, err)\n )\n return True"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a new job in the Jenkins server.", "response": "def create_job(name=None,\n config_xml=None,\n saltenv='base'):\n '''\n Return the configuration file.\n\n :param name: The name of the job is check if it exists.\n :param config_xml: The configuration file to use to create the job.\n :param saltenv: The environment to look for the file in.\n :return: The configuration file used for the job.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jenkins.create_job jobname\n\n salt '*' jenkins.create_job jobname config_xml='salt://jenkins/config.xml'\n\n '''\n if not name:\n raise SaltInvocationError('Required parameter \\'name\\' is missing')\n\n if job_exists(name):\n raise CommandExecutionError('Job \\'{0}\\' already exists'.format(name))\n\n if not config_xml:\n config_xml = jenkins.EMPTY_CONFIG_XML\n else:\n config_xml_file = _retrieve_config_xml(config_xml, saltenv)\n\n with salt.utils.files.fopen(config_xml_file) as _fp:\n config_xml = salt.utils.stringutils.to_unicode(_fp.read())\n\n server = _connect()\n try:\n server.create_job(name, config_xml)\n except jenkins.JenkinsException as err:\n raise CommandExecutionError(\n 'Encountered error creating job \\'{0}\\': {1}'.format(name, err)\n )\n return config_xml"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndeleting a job from the Jenkins server.", "response": "def delete_job(name=None):\n '''\n Return true is job is deleted successfully.\n\n :param name: The name of the job to delete.\n :return: Return true if job is deleted successfully.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jenkins.delete_job jobname\n\n '''\n if not name:\n raise SaltInvocationError('Required parameter \\'name\\' is missing')\n\n server = _connect()\n\n if not job_exists(name):\n raise CommandExecutionError('Job \\'{0}\\' does not exist'.format(name))\n\n try:\n server.delete_job(name)\n except jenkins.JenkinsException as err:\n raise CommandExecutionError(\n 'Encountered error deleting job \\'{0}\\': {1}'.format(name, err)\n )\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the current status of a job.", "response": "def job_status(name=None):\n '''\n Return the current status, enabled or disabled, of the job.\n\n :param name: The name of the job to return status for\n :return: Return true if enabled or false if disabled.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jenkins.job_status jobname\n\n '''\n\n if not name:\n raise SaltInvocationError('Required parameter \\'name\\' is missing')\n\n server = _connect()\n\n if not job_exists(name):\n raise CommandExecutionError('Job \\'{0}\\' does not exist'.format(name))\n\n return server.get_job_info('empty')['buildable']"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the current job configuration for the specified job.", "response": "def get_job_config(name=None):\n '''\n Return the current job configuration for the provided job.\n\n :param name: The name of the job to return the configuration for.\n :return: The configuration for the job specified.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jenkins.get_job_config jobname\n\n '''\n\n if not name:\n raise SaltInvocationError('Required parameter \\'name\\' is missing')\n\n server = _connect()\n\n if not job_exists(name):\n raise CommandExecutionError('Job \\'{0}\\' does not exist'.format(name))\n\n job_info = server.get_job_config(name)\n return job_info"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncheck if the plugin is installed for the provided plugin name.", "response": "def plugin_installed(name):\n '''\n .. versionadded:: 2016.11.0\n\n Return if the plugin is installed for the provided plugin name.\n\n :param name: The name of the parameter to confirm installation.\n :return: True if plugin exists, False if plugin does not exist.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jenkins.plugin_installed pluginName\n\n '''\n\n server = _connect()\n plugins = server.get_plugins()\n\n exists = [plugin for plugin in plugins.keys() if name in plugin]\n\n if exists:\n return True\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates or updates a record set within a DNS zone.", "response": "def record_set_create_or_update(name, zone_name, resource_group, record_type, **kwargs):\n '''\n .. versionadded:: Fluorine\n\n Creates or updates a record set within a DNS zone.\n\n :param name: The name of the record set, relative to the name of the zone.\n\n :param zone_name: The name of the DNS zone (without a terminating dot).\n\n :param resource_group: The name of the resource group.\n\n :param record_type: The type of DNS record in this record set. Record sets of type SOA can be\n updated but not created (they are created when the DNS zone is created).\n Possible values include: 'A', 'AAAA', 'CAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT'\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call azurearm_dns.record_set_create_or_update myhost myzone testgroup A\n arecords='[{ipv4_address: 10.0.0.1}]' ttl=300\n\n '''\n dnsconn = __utils__['azurearm.get_client']('dns', **kwargs)\n\n try:\n record_set_model = __utils__['azurearm.create_object_model']('dns', 'RecordSet', **kwargs)\n except TypeError as exc:\n result = {'error': 'The object model could not be built. ({0})'.format(str(exc))}\n return result\n\n try:\n record_set = dnsconn.record_sets.create_or_update(\n relative_record_set_name=name,\n zone_name=zone_name,\n resource_group_name=resource_group,\n record_type=record_type,\n parameters=record_set_model,\n if_match=kwargs.get('if_match'),\n if_none_match=kwargs.get('if_none_match')\n )\n result = record_set.as_dict()\n except CloudError as exc:\n __utils__['azurearm.log_cloud_error']('dns', str(exc), **kwargs)\n result = {'error': str(exc)}\n except SerializationError as exc:\n result = {'error': 'The object model could not be parsed. ({0})'.format(str(exc))}\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef record_set_delete(name, zone_name, resource_group, record_type, **kwargs):\n '''\n .. versionadded:: Fluorine\n\n Deletes a record set from a DNS zone. This operation cannot be undone.\n\n :param name: The name of the record set, relative to the name of the zone.\n\n :param zone_name: The name of the DNS zone (without a terminating dot).\n\n :param resource_group: The name of the resource group.\n\n :param record_type: The type of DNS record in this record set. Record sets of type SOA cannot be\n deleted (they are deleted when the DNS zone is deleted).\n Possible values include: 'A', 'AAAA', 'CAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT'\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call azurearm_dns.record_set_delete myhost myzone testgroup A\n\n '''\n result = False\n dnsconn = __utils__['azurearm.get_client']('dns', **kwargs)\n try:\n record_set = dnsconn.record_sets.delete(\n relative_record_set_name=name,\n zone_name=zone_name,\n resource_group_name=resource_group,\n record_type=record_type,\n if_match=kwargs.get('if_match')\n )\n result = True\n except CloudError as exc:\n __utils__['azurearm.log_cloud_error']('dns', str(exc), **kwargs)\n\n return result", "response": "Delete a record set from a DNS zone."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting a dictionary representing a record set.", "response": "def record_set_get(name, zone_name, resource_group, record_type, **kwargs):\n '''\n .. versionadded:: Fluorine\n\n Get a dictionary representing a record set's properties.\n\n :param name: The name of the record set, relative to the name of the zone.\n\n :param zone_name: The name of the DNS zone (without a terminating dot).\n\n :param resource_group: The name of the resource group.\n\n :param record_type: The type of DNS record in this record set.\n Possible values include: 'A', 'AAAA', 'CAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT'\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call azurearm_dns.record_set_get '@' myzone testgroup SOA\n\n '''\n dnsconn = __utils__['azurearm.get_client']('dns', **kwargs)\n try:\n record_set = dnsconn.record_sets.get(\n relative_record_set_name=name,\n zone_name=zone_name,\n resource_group_name=resource_group,\n record_type=record_type\n )\n result = record_set.as_dict()\n\n except CloudError as exc:\n __utils__['azurearm.log_cloud_error']('dns', str(exc), **kwargs)\n result = {'error': str(exc)}\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef record_sets_list_by_type(zone_name, resource_group, record_type, top=None, recordsetnamesuffix=None, **kwargs):\n '''\n .. versionadded:: Fluorine\n\n Lists the record sets of a specified type in a DNS zone.\n\n :param zone_name: The name of the DNS zone (without a terminating dot).\n\n :param resource_group: The name of the resource group.\n\n :param record_type: The type of record sets to enumerate.\n Possible values include: 'A', 'AAAA', 'CAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT'\n\n :param top: The maximum number of record sets to return. If not specified,\n returns up to 100 record sets.\n\n :param recordsetnamesuffix: The suffix label of the record set name that has\n to be used to filter the record set enumerations.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call azurearm_dns.record_sets_list_by_type myzone testgroup SOA\n\n '''\n result = {}\n dnsconn = __utils__['azurearm.get_client']('dns', **kwargs)\n try:\n record_sets = __utils__['azurearm.paged_object_to_list'](\n dnsconn.record_sets.list_by_type(\n zone_name=zone_name,\n resource_group_name=resource_group,\n record_type=record_type,\n top=top,\n recordsetnamesuffix=recordsetnamesuffix\n )\n )\n\n for record_set in record_sets:\n result[record_set['name']] = record_set\n except CloudError as exc:\n __utils__['azurearm.log_cloud_error']('dns', str(exc), **kwargs)\n result = {'error': str(exc)}\n\n return result", "response": "List all record sets of a specified type in a resource group."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nlisting all record sets in a DNS zone.", "response": "def record_sets_list_by_dns_zone(zone_name, resource_group, top=None, recordsetnamesuffix=None, **kwargs):\n '''\n .. versionadded:: Fluorine\n\n Lists all record sets in a DNS zone.\n\n :param zone_name: The name of the DNS zone (without a terminating dot).\n\n :param resource_group: The name of the resource group.\n\n :param top: The maximum number of record sets to return. If not specified,\n returns up to 100 record sets.\n\n :param recordsetnamesuffix: The suffix label of the record set name that has\n to be used to filter the record set enumerations.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call azurearm_dns.record_sets_list_by_dns_zone myzone testgroup\n\n '''\n result = {}\n dnsconn = __utils__['azurearm.get_client']('dns', **kwargs)\n try:\n record_sets = __utils__['azurearm.paged_object_to_list'](\n dnsconn.record_sets.list_by_dns_zone(\n zone_name=zone_name,\n resource_group_name=resource_group,\n top=top,\n recordsetnamesuffix=recordsetnamesuffix\n )\n )\n\n for record_set in record_sets:\n result[record_set['name']] = record_set\n except CloudError as exc:\n __utils__['azurearm.log_cloud_error']('dns', str(exc), **kwargs)\n result = {'error': str(exc)}\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate or update a DNS zone.", "response": "def zone_create_or_update(name, resource_group, **kwargs):\n '''\n .. versionadded:: Fluorine\n\n Creates or updates a DNS zone. Does not modify DNS records within the zone.\n\n :param name: The name of the DNS zone to create (without a terminating dot).\n\n :param resource_group: The name of the resource group.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call azurearm_dns.zone_create_or_update myzone testgroup\n\n '''\n # DNS zones are global objects\n kwargs['location'] = 'global'\n\n dnsconn = __utils__['azurearm.get_client']('dns', **kwargs)\n\n # Convert list of ID strings to list of dictionaries with id key.\n if isinstance(kwargs.get('registration_virtual_networks'), list):\n kwargs['registration_virtual_networks'] = [{'id': vnet} for vnet in kwargs['registration_virtual_networks']]\n\n if isinstance(kwargs.get('resolution_virtual_networks'), list):\n kwargs['resolution_virtual_networks'] = [{'id': vnet} for vnet in kwargs['resolution_virtual_networks']]\n\n try:\n zone_model = __utils__['azurearm.create_object_model']('dns', 'Zone', **kwargs)\n except TypeError as exc:\n result = {'error': 'The object model could not be built. ({0})'.format(str(exc))}\n return result\n\n try:\n zone = dnsconn.zones.create_or_update(\n zone_name=name,\n resource_group_name=resource_group,\n parameters=zone_model,\n if_match=kwargs.get('if_match'),\n if_none_match=kwargs.get('if_none_match')\n )\n result = zone.as_dict()\n except CloudError as exc:\n __utils__['azurearm.log_cloud_error']('dns', str(exc), **kwargs)\n result = {'error': str(exc)}\n except SerializationError as exc:\n result = {'error': 'The object model could not be parsed. ({0})'.format(str(exc))}\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndelete a DNS zone within a resource group.", "response": "def zone_delete(name, resource_group, **kwargs):\n '''\n .. versionadded:: Fluorine\n\n Delete a DNS zone within a resource group.\n\n :param name: The name of the DNS zone to delete.\n\n :param resource_group: The name of the resource group.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call azurearm_dns.zone_delete myzone testgroup\n\n '''\n result = False\n dnsconn = __utils__['azurearm.get_client']('dns', **kwargs)\n try:\n zone = dnsconn.zones.delete(\n zone_name=name,\n resource_group_name=resource_group,\n if_match=kwargs.get('if_match')\n )\n zone.wait()\n result = True\n except CloudError as exc:\n __utils__['azurearm.log_cloud_error']('dns', str(exc), **kwargs)\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef zone_get(name, resource_group, **kwargs):\n '''\n .. versionadded:: Fluorine\n\n Get a dictionary representing a DNS zone's properties, but not the\n record sets within the zone.\n\n :param name: The DNS zone to get.\n\n :param resource_group: The name of the resource group.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call azurearm_dns.zone_get myzone testgroup\n\n '''\n dnsconn = __utils__['azurearm.get_client']('dns', **kwargs)\n try:\n zone = dnsconn.zones.get(\n zone_name=name,\n resource_group_name=resource_group\n )\n result = zone.as_dict()\n\n except CloudError as exc:\n __utils__['azurearm.log_cloud_error']('dns', str(exc), **kwargs)\n result = {'error': str(exc)}\n\n return result", "response": "Get a dictionary representing a DNS zone."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlist the DNS zones in a resource group.", "response": "def zones_list_by_resource_group(resource_group, top=None, **kwargs):\n '''\n .. versionadded:: Fluorine\n\n Lists the DNS zones in a resource group.\n\n :param resource_group: The name of the resource group.\n\n :param top: The maximum number of DNS zones to return. If not specified,\n returns up to 100 zones.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call azurearm_dns.zones_list_by_resource_group testgroup\n\n '''\n result = {}\n dnsconn = __utils__['azurearm.get_client']('dns', **kwargs)\n try:\n zones = __utils__['azurearm.paged_object_to_list'](\n dnsconn.zones.list_by_resource_group(\n resource_group_name=resource_group,\n top=top\n )\n )\n\n for zone in zones:\n result[zone['name']] = zone\n except CloudError as exc:\n __utils__['azurearm.log_cloud_error']('dns', str(exc), **kwargs)\n result = {'error': str(exc)}\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef zones_list(top=None, **kwargs):\n '''\n .. versionadded:: Fluorine\n\n Lists the DNS zones in all resource groups in a subscription.\n\n :param top: The maximum number of DNS zones to return. If not specified,\n returns up to 100 zones.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call azurearm_dns.zones_list\n\n '''\n result = {}\n dnsconn = __utils__['azurearm.get_client']('dns', **kwargs)\n try:\n zones = __utils__['azurearm.paged_object_to_list'](dnsconn.zones.list(top=top))\n\n for zone in zones:\n result[zone['name']] = zone\n except CloudError as exc:\n __utils__['azurearm.log_cloud_error']('dns', str(exc), **kwargs)\n result = {'error': str(exc)}\n\n return result", "response": "List the DNS zones in all resource groups in a subscription."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef exists(name, region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if an ELB exists.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.exists myelb region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n elb = conn.get_all_load_balancers(load_balancer_names=[name])\n if elb:\n return True\n else:\n log.debug('The load balancer does not exist in region %s', region)\n return False\n except boto.exception.BotoServerError as error:\n log.warning(error)\n return False", "response": "Check to see if an ELB exists."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting all load balancers associated with an account", "response": "def get_all_elbs(region=None, key=None, keyid=None, profile=None):\n '''\n Return all load balancers associated with an account\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.get_all_elbs region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n return [e for e in conn.get_all_load_balancers()]\n except boto.exception.BotoServerError as error:\n log.warning(error)\n return []"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn names of all load balancers associated with an account", "response": "def list_elbs(region=None, key=None, keyid=None, profile=None):\n '''\n Return names of all load balancers associated with an account\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.list_elbs region=us-east-1\n '''\n\n return [e.name for e in get_all_elbs(region=region, key=key, keyid=keyid,\n profile=profile)]"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets an ELB configuration.", "response": "def get_elb_config(name, region=None, key=None, keyid=None, profile=None):\n '''\n Get an ELB configuration.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.exists myelb region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n retries = 30\n\n while retries:\n try:\n lb = conn.get_all_load_balancers(load_balancer_names=[name])\n lb = lb[0]\n ret = {}\n ret['availability_zones'] = lb.availability_zones\n listeners = []\n for _listener in lb.listeners:\n listener_dict = {}\n listener_dict['elb_port'] = _listener.load_balancer_port\n listener_dict['elb_protocol'] = _listener.protocol\n listener_dict['instance_port'] = _listener.instance_port\n listener_dict['instance_protocol'] = _listener.instance_protocol\n listener_dict['policies'] = _listener.policy_names\n if _listener.ssl_certificate_id:\n listener_dict['certificate'] = _listener.ssl_certificate_id\n listeners.append(listener_dict)\n ret['listeners'] = listeners\n backends = []\n for _backend in lb.backends:\n bs_dict = {}\n bs_dict['instance_port'] = _backend.instance_port\n bs_dict['policies'] = [p.policy_name for p in _backend.policies]\n backends.append(bs_dict)\n ret['backends'] = backends\n ret['subnets'] = lb.subnets\n ret['security_groups'] = lb.security_groups\n ret['scheme'] = lb.scheme\n ret['dns_name'] = lb.dns_name\n ret['tags'] = _get_all_tags(conn, name)\n lb_policy_lists = [\n lb.policies.app_cookie_stickiness_policies,\n lb.policies.lb_cookie_stickiness_policies,\n lb.policies.other_policies\n ]\n policies = []\n for policy_list in lb_policy_lists:\n policies += [p.policy_name for p in policy_list]\n ret['policies'] = policies\n return ret\n except boto.exception.BotoServerError as error:\n if error.error_code == 'Throttling':\n log.debug('Throttled by AWS API, will retry in 5 seconds.')\n time.sleep(5)\n retries -= 1\n continue\n log.error('Error fetching config for ELB %s: %s', name, error.message)\n log.error(error)\n return {}\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconvert an ELB listener dict into a tuple used by certain parts of the AWS ELB API.", "response": "def listener_dict_to_tuple(listener):\n '''\n Convert an ELB listener dict into a listener tuple used by certain parts of\n the AWS ELB API.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.listener_dict_to_tuple '{\"elb_port\":80,\"instance_port\":80,\"elb_protocol\":\"HTTP\"}'\n '''\n # We define all listeners as complex listeners.\n if 'instance_protocol' not in listener:\n instance_protocol = listener['elb_protocol'].upper()\n else:\n instance_protocol = listener['instance_protocol'].upper()\n listener_tuple = [listener['elb_port'], listener['instance_port'],\n listener['elb_protocol'], instance_protocol]\n if 'certificate' in listener:\n listener_tuple.append(listener['certificate'])\n return tuple(listener_tuple)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates an ELB CLI example to create an ELB: .. code-block:: bash salt myminion boto_elb.create myelb '[\"us-east-1a\", \"us-east-1e\"]' '{\"elb_port\": 443, \"elb_protocol\": \"HTTPS\", ...}' region=us-east-1", "response": "def create(name, availability_zones, listeners, subnets=None,\n security_groups=None, scheme='internet-facing',\n region=None, key=None, keyid=None,\n profile=None):\n '''\n Create an ELB\n\n CLI example to create an ELB:\n\n .. code-block:: bash\n\n salt myminion boto_elb.create myelb '[\"us-east-1a\", \"us-east-1e\"]' '{\"elb_port\": 443, \"elb_protocol\": \"HTTPS\", ...}' region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if exists(name, region, key, keyid, profile):\n return True\n if isinstance(availability_zones, six.string_types):\n availability_zones = salt.utils.json.loads(availability_zones)\n\n if isinstance(listeners, six.string_types):\n listeners = salt.utils.json.loads(listeners)\n\n _complex_listeners = []\n for listener in listeners:\n _complex_listeners.append(listener_dict_to_tuple(listener))\n\n try:\n lb = conn.create_load_balancer(name=name, zones=availability_zones, subnets=subnets,\n security_groups=security_groups, scheme=scheme,\n complex_listeners=_complex_listeners)\n if lb:\n log.info('Created ELB %s', name)\n return True\n else:\n log.error('Failed to create ELB %s', name)\n return False\n except boto.exception.BotoServerError as error:\n log.error('Failed to create ELB %s: %s: %s',\n name, error.error_code, error.message,\n exc_info_on_loglevel=logging.DEBUG)\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete(name, region=None, key=None, keyid=None, profile=None):\n '''\n Delete an ELB.\n\n CLI example to delete an ELB:\n\n .. code-block:: bash\n\n salt myminion boto_elb.delete myelb region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if not exists(name, region, key, keyid, profile):\n return True\n try:\n conn.delete_load_balancer(name)\n log.info('Deleted ELB %s.', name)\n return True\n except boto.exception.BotoServerError as error:\n log.error('Failed to delete ELB %s', name,\n exc_info_on_loglevel=logging.DEBUG)\n return False", "response": "Delete an ELB.\n\n CLI example to delete an ELB:\n\n .. code-block:: bash\n\n salt myminion boto_elb.delete myelb region=us-east-1"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_listeners(name, listeners, region=None, key=None, keyid=None,\n profile=None):\n '''\n Create listeners on an ELB.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.create_listeners myelb '[[\"HTTPS\", \"HTTP\", 443, 80, \"arn:aws:iam::11 11111:server-certificate/mycert\"]]'\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if isinstance(listeners, six.string_types):\n listeners = salt.utils.json.loads(listeners)\n\n _complex_listeners = []\n for listener in listeners:\n _complex_listeners.append(listener_dict_to_tuple(listener))\n try:\n conn.create_load_balancer_listeners(name, [], _complex_listeners)\n log.info('Created ELB listeners on %s', name)\n return True\n except boto.exception.BotoServerError as error:\n log.error('Failed to create ELB listeners on %s: %s', name, error,\n exc_info_on_loglevel=logging.DEBUG)\n return False", "response": "Create listeners on an ELB."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete_listeners(name, ports, region=None, key=None, keyid=None,\n profile=None):\n '''\n Delete listeners on an ELB.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.delete_listeners myelb '[80,443]'\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if isinstance(ports, six.string_types):\n ports = salt.utils.json.loads(ports)\n try:\n conn.delete_load_balancer_listeners(name, ports)\n log.info('Deleted ELB listeners on %s', name)\n return True\n except boto.exception.BotoServerError as error:\n log.error('Failed to delete ELB listeners on %s: %s', name, error,\n exc_info_on_loglevel=logging.DEBUG)\n return False", "response": "Delete listeners on an ELB."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\napplies security groups to an ELB.", "response": "def apply_security_groups(name, security_groups, region=None, key=None,\n keyid=None, profile=None):\n '''\n Apply security groups to ELB.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.apply_security_groups myelb '[\"mysecgroup1\"]'\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if isinstance(security_groups, six.string_types):\n security_groups = salt.utils.json.loads(security_groups)\n try:\n conn.apply_security_groups_to_lb(name, security_groups)\n log.info('Applied security_groups on ELB %s', name)\n return True\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to appply security_groups on ELB %s: %s',\n name, e.message)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget attributes of an ELB.", "response": "def get_attributes(name, region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if attributes are set on an ELB.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.get_attributes myelb\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n retries = 30\n\n while retries:\n try:\n lbattrs = conn.get_all_lb_attributes(name)\n ret = odict.OrderedDict()\n ret['access_log'] = odict.OrderedDict()\n ret['cross_zone_load_balancing'] = odict.OrderedDict()\n ret['connection_draining'] = odict.OrderedDict()\n ret['connecting_settings'] = odict.OrderedDict()\n al = lbattrs.access_log\n czlb = lbattrs.cross_zone_load_balancing\n cd = lbattrs.connection_draining\n cs = lbattrs.connecting_settings\n ret['access_log']['enabled'] = al.enabled\n ret['access_log']['s3_bucket_name'] = al.s3_bucket_name\n ret['access_log']['s3_bucket_prefix'] = al.s3_bucket_prefix\n ret['access_log']['emit_interval'] = al.emit_interval\n ret['cross_zone_load_balancing']['enabled'] = czlb.enabled\n ret['connection_draining']['enabled'] = cd.enabled\n ret['connection_draining']['timeout'] = cd.timeout\n ret['connecting_settings']['idle_timeout'] = cs.idle_timeout\n return ret\n except boto.exception.BotoServerError as e:\n if e.error_code == 'Throttling':\n log.debug(\"Throttled by AWS API, will retry in 5 seconds...\")\n time.sleep(5)\n retries -= 1\n continue\n log.error('ELB %s does not exist: %s', name, e.message)\n return {}\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef set_attributes(name, attributes, region=None, key=None, keyid=None,\n profile=None):\n '''\n Set attributes on an ELB.\n\n name (string)\n Name of the ELB instance to set attributes for\n\n attributes\n A dict of attributes to set.\n\n Valid attributes are:\n\n access_log (dict)\n enabled (bool)\n Enable storage of access logs.\n s3_bucket_name (string)\n The name of the S3 bucket to place logs.\n s3_bucket_prefix (string)\n Prefix for the log file name.\n emit_interval (int)\n Interval for storing logs in S3 in minutes. Valid values are\n 5 and 60.\n\n connection_draining (dict)\n enabled (bool)\n Enable connection draining.\n timeout (int)\n Maximum allowed time in seconds for sending existing\n connections to an instance that is deregistering or unhealthy.\n Default is 300.\n\n cross_zone_load_balancing (dict)\n enabled (bool)\n Enable cross-zone load balancing.\n\n CLI example to set attributes on an ELB:\n\n .. code-block:: bash\n\n salt myminion boto_elb.set_attributes myelb '{\"access_log\": {\"enabled\": \"true\", \"s3_bucket_name\": \"mybucket\", \"s3_bucket_prefix\": \"mylogs/\", \"emit_interval\": \"5\"}}' region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n al = attributes.get('access_log', {})\n czlb = attributes.get('cross_zone_load_balancing', {})\n cd = attributes.get('connection_draining', {})\n cs = attributes.get('connecting_settings', {})\n if not al and not czlb and not cd and not cs:\n log.error('No supported attributes for ELB.')\n return False\n if al:\n _al = AccessLogAttribute()\n _al.enabled = al.get('enabled', False)\n if not _al.enabled:\n msg = 'Access log attribute configured, but enabled config missing'\n log.error(msg)\n return False\n _al.s3_bucket_name = al.get('s3_bucket_name', None)\n _al.s3_bucket_prefix = al.get('s3_bucket_prefix', None)\n _al.emit_interval = al.get('emit_interval', None)\n added_attr = conn.modify_lb_attribute(name, 'accessLog', _al)\n if added_attr:\n log.info('Added access_log attribute to %s elb.', name)\n else:\n log.error('Failed to add access_log attribute to %s elb.', name)\n return False\n if czlb:\n _czlb = CrossZoneLoadBalancingAttribute()\n _czlb.enabled = czlb['enabled']\n added_attr = conn.modify_lb_attribute(name, 'crossZoneLoadBalancing',\n _czlb.enabled)\n if added_attr:\n log.info('Added cross_zone_load_balancing attribute to %s elb.', name)\n else:\n log.error('Failed to add cross_zone_load_balancing attribute.')\n return False\n if cd:\n _cd = ConnectionDrainingAttribute()\n _cd.enabled = cd['enabled']\n _cd.timeout = cd.get('timeout', 300)\n added_attr = conn.modify_lb_attribute(name, 'connectionDraining', _cd)\n if added_attr:\n log.info('Added connection_draining attribute to %s elb.', name)\n else:\n log.error('Failed to add connection_draining attribute.')\n return False\n if cs:\n _cs = ConnectionSettingAttribute()\n _cs.idle_timeout = cs.get('idle_timeout', 60)\n added_attr = conn.modify_lb_attribute(name, 'connectingSettings', _cs)\n if added_attr:\n log.info('Added connecting_settings attribute to %s elb.', name)\n else:\n log.error('Failed to add connecting_settings attribute.')\n return False\n return True", "response": "Set attributes on an ELB instance."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_health_check(name, region=None, key=None, keyid=None, profile=None):\n '''\n Get the health check configured for this ELB.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.get_health_check myelb\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n retries = 30\n\n while True:\n try:\n lb = conn.get_all_load_balancers(load_balancer_names=[name])\n lb = lb[0]\n ret = odict.OrderedDict()\n hc = lb.health_check\n ret['interval'] = hc.interval\n ret['target'] = hc.target\n ret['healthy_threshold'] = hc.healthy_threshold\n ret['timeout'] = hc.timeout\n ret['unhealthy_threshold'] = hc.unhealthy_threshold\n return ret\n except boto.exception.BotoServerError as e:\n if retries and e.code == 'Throttling':\n log.debug('Throttled by AWS API, will retry in 5 seconds.')\n time.sleep(5)\n retries -= 1\n continue\n log.error('ELB %s not found.', name,\n exc_info_on_logleve=logging.DEBUG)\n return {}", "response": "Get the health check configured for this ELB."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconfiguring the health check on an ELB.", "response": "def set_health_check(name, health_check, region=None, key=None, keyid=None,\n profile=None):\n '''\n Set attributes on an ELB.\n\n CLI example to set attributes on an ELB:\n\n .. code-block:: bash\n\n salt myminion boto_elb.set_health_check myelb '{\"target\": \"HTTP:80/\"}'\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n retries = 30\n\n hc = HealthCheck(**health_check)\n while True:\n try:\n conn.configure_health_check(name, hc)\n log.info('Configured health check on ELB %s', name)\n return True\n except boto.exception.BotoServerError as error:\n if retries and e.code == 'Throttling':\n log.debug('Throttled by AWS API, will retry in 5 seconds.')\n time.sleep(5)\n retries -= 1\n continue\n log.exception('Failed to configure health check on ELB %s', name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nregisters instances with an ELB.", "response": "def register_instances(name, instances, region=None, key=None, keyid=None,\n profile=None):\n '''\n Register instances with an ELB. Instances is either a string\n instance id or a list of string instance id's.\n\n Returns:\n\n - ``True``: instance(s) registered successfully\n - ``False``: instance(s) failed to be registered\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.register_instances myelb instance_id\n salt myminion boto_elb.register_instances myelb \"[instance_id,instance_id]\"\n '''\n # convert instances to list type, enabling consistent use of instances\n # variable throughout the register_instances method\n if isinstance(instances, six.string_types) or isinstance(instances, six.text_type):\n instances = [instances]\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n registered_instances = conn.register_instances(name, instances)\n except boto.exception.BotoServerError as error:\n log.warning(error)\n return False\n registered_instance_ids = [instance.id for instance in\n registered_instances]\n # register_failues is a set that will contain any instances that were not\n # able to be registered with the given ELB\n register_failures = set(instances).difference(set(registered_instance_ids))\n if register_failures:\n log.warning('Instance(s): %s not registered with ELB %s.',\n list(register_failures), name)\n register_result = False\n else:\n register_result = True\n return register_result"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef deregister_instances(name, instances, region=None, key=None, keyid=None,\n profile=None):\n '''\n Deregister instances with an ELB. Instances is either a string\n instance id or a list of string instance id's.\n\n Returns:\n\n - ``True``: instance(s) deregistered successfully\n - ``False``: instance(s) failed to be deregistered\n - ``None``: instance(s) not valid or not registered, no action taken\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.deregister_instances myelb instance_id\n salt myminion boto_elb.deregister_instances myelb \"[instance_id, instance_id]\"\n '''\n # convert instances to list type, enabling consistent use of instances\n # variable throughout the deregister_instances method\n if isinstance(instances, six.string_types) or isinstance(instances, six.text_type):\n instances = [instances]\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n registered_instances = conn.deregister_instances(name, instances)\n except boto.exception.BotoServerError as error:\n # if the instance(s) given as an argument are not members of the ELB\n # boto returns error.error_code == 'InvalidInstance'\n # deregister_instances returns \"None\" because the instances are\n # effectively deregistered from ELB\n if error.error_code == 'InvalidInstance':\n log.warning(\n 'One or more of instance(s) %s are not part of ELB %s. '\n 'deregister_instances not performed.', instances, name\n )\n return None\n else:\n log.warning(error)\n return False\n registered_instance_ids = [instance.id for instance in\n registered_instances]\n # deregister_failures is a set that will contain any instances that were\n # unable to be deregistered from the given ELB\n deregister_failures = set(instances).intersection(set(registered_instance_ids))\n if deregister_failures:\n log.warning(\n 'Instance(s): %s not deregistered from ELB %s.',\n list(deregister_failures), name\n )\n deregister_result = False\n else:\n deregister_result = True\n return deregister_result", "response": "Deregisters instances with an ELB. Instances can be a string instance id or a list of string instance id s."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_instances(name, instances, test=False, region=None, key=None, keyid=None,\n profile=None):\n '''\n Set the instances assigned to an ELB to exactly the list given\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.set_instances myelb region=us-east-1 instances=\"[instance_id,instance_id]\"\n '''\n ret = True\n current = set([i['instance_id'] for i in get_instance_health(name, region, key, keyid, profile)])\n desired = set(instances)\n add = desired - current\n remove = current - desired\n if test:\n return bool(add or remove)\n if remove:\n if deregister_instances(name, list(remove), region, key, keyid, profile) is False:\n ret = False\n if add:\n if register_instances(name, list(add), region, key, keyid, profile) is False:\n ret = False\n return ret", "response": "Set the instances assigned to an ELB to exactly the list given"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_instance_health(name, region=None, key=None, keyid=None, profile=None, instances=None):\n '''\n Get a list of instances and their health state\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.get_instance_health myelb\n salt myminion boto_elb.get_instance_health myelb region=us-east-1 instances=\"[instance_id,instance_id]\"\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n instance_states = conn.describe_instance_health(name, instances)\n ret = []\n for _instance in instance_states:\n ret.append({'instance_id': _instance.instance_id,\n 'description': _instance.description,\n 'state': _instance.state,\n 'reason_code': _instance.reason_code\n })\n return ret\n except boto.exception.BotoServerError as error:\n log.debug(error)\n return []", "response": "Get a list of instances and their health state"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating an ELB policy.", "response": "def create_policy(name, policy_name, policy_type, policy, region=None,\n key=None, keyid=None, profile=None):\n '''\n Create an ELB policy.\n\n .. versionadded:: 2016.3.0\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.create_policy myelb mypolicy LBCookieStickinessPolicyType '{\"CookieExpirationPeriod\": 3600}'\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if not exists(name, region, key, keyid, profile):\n return False\n try:\n success = conn.create_lb_policy(name, policy_name, policy_type, policy)\n if success:\n log.info('Created policy %s on ELB %s', policy_name, name)\n return True\n else:\n log.error('Failed to create policy %s on ELB %s', policy_name, name)\n return False\n except boto.exception.BotoServerError as e:\n log.error('Failed to create policy %s on ELB %s: %s',\n policy_name, name, e.message,\n exc_info_on_loglevel=logging.DEBUG)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndeletes an ELB policy.", "response": "def delete_policy(name, policy_name, region=None, key=None, keyid=None,\n profile=None):\n '''\n Delete an ELB policy.\n\n .. versionadded:: 2016.3.0\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.delete_policy myelb mypolicy\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if not exists(name, region, key, keyid, profile):\n return True\n try:\n conn.delete_lb_policy(name, policy_name)\n log.info('Deleted policy %s on ELB %s', policy_name, name)\n return True\n except boto.exception.BotoServerError as e:\n log.error('Failed to delete policy %s on ELB %s: %s',\n policy_name, name, e.message,\n exc_info_on_loglevel=logging.DEBUG)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the policies of an ELB listener.", "response": "def set_listener_policy(name, port, policies=None, region=None, key=None,\n keyid=None, profile=None):\n '''\n Set the policies of an ELB listener.\n\n .. versionadded:: 2016.3.0\n\n CLI example:\n\n .. code-block:: Bash\n\n salt myminion boto_elb.set_listener_policy myelb 443 \"[policy1,policy2]\"\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if not exists(name, region, key, keyid, profile):\n return True\n if policies is None:\n policies = []\n try:\n conn.set_lb_policies_of_listener(name, port, policies)\n log.info('Set policies %s on ELB %s listener %s', policies, name, port)\n except boto.exception.BotoServerError as e:\n log.info('Failed to set policy %s on ELB %s listener %s: %s',\n policies, name, port, e.message,\n exc_info_on_loglevel=logging.DEBUG)\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nadding the tags to an ELB", "response": "def set_tags(name, tags, region=None, key=None, keyid=None, profile=None):\n '''\n Add the tags on an ELB\n\n .. versionadded:: 2016.3.0\n\n name\n name of the ELB\n\n tags\n dict of name/value pair tags\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.set_tags my-elb-name \"{'Tag1': 'Value', 'Tag2': 'Another Value'}\"\n '''\n\n if exists(name, region, key, keyid, profile):\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n ret = _add_tags(conn, name, tags)\n return ret\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete_tags(name, tags, region=None, key=None, keyid=None, profile=None):\n '''\n Add the tags on an ELB\n\n name\n name of the ELB\n\n tags\n list of tags to remove\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_elb.delete_tags my-elb-name ['TagToRemove1', 'TagToRemove2']\n '''\n if exists(name, region, key, keyid, profile):\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n ret = _remove_tags(conn, name, tags)\n return ret\n else:\n return False", "response": "Remove the tags from an ELB\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_all_tags(conn, load_balancer_names=None):\n '''\n Retrieve all the metadata tags associated with your ELB(s).\n\n :type load_balancer_names: list\n :param load_balancer_names: An optional list of load balancer names.\n\n :rtype: list\n :return: A list of :class:`boto.ec2.elb.tag.Tag` objects\n '''\n params = {}\n if load_balancer_names:\n conn.build_list_params(params, load_balancer_names,\n 'LoadBalancerNames.member.%d')\n\n tags = conn.get_object(\n 'DescribeTags',\n params,\n __utils__['boto_elb_tag.get_tag_descriptions'](),\n verb='POST'\n )\n if tags[load_balancer_names]:\n return tags[load_balancer_names]\n else:\n return None", "response": "Retrieves all the metadata tags associated with your ELBs."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating new metadata tags for the specified resource ids.", "response": "def _add_tags(conn, load_balancer_names, tags):\n '''\n Create new metadata tags for the specified resource ids.\n\n :type load_balancer_names: list\n :param load_balancer_names: A list of load balancer names.\n\n :type tags: dict\n :param tags: A dictionary containing the name/value pairs.\n If you want to create only a tag name, the\n value for that tag should be the empty string\n (e.g. '').\n '''\n params = {}\n conn.build_list_params(params, load_balancer_names,\n 'LoadBalancerNames.member.%d')\n _build_tag_param_list(params, tags)\n return conn.get_status('AddTags', params, verb='POST')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _remove_tags(conn, load_balancer_names, tags):\n '''\n Delete metadata tags for the specified resource ids.\n\n :type load_balancer_names: list\n :param load_balancer_names: A list of load balancer names.\n\n :type tags: list\n :param tags: A list containing just tag names for the tags to be\n deleted.\n '''\n params = {}\n conn.build_list_params(params, load_balancer_names,\n 'LoadBalancerNames.member.%d')\n conn.build_list_params(params, tags,\n 'Tags.member.%d.Key')\n return conn.get_status('RemoveTags', params, verb='POST')", "response": "Delete metadata tags for the specified resource ids."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _repack_pkgs(pkgs, normalize=True):\n '''\n Repack packages specified using \"pkgs\" argument to pkg states into a single\n dictionary\n '''\n if normalize and 'pkg.normalize_name' in __salt__:\n _normalize_name = __salt__['pkg.normalize_name']\n else:\n _normalize_name = lambda pkgname: pkgname\n return dict(\n [\n (_normalize_name(six.text_type(x)), six.text_type(y) if y is not None else y)\n for x, y in six.iteritems(salt.utils.data.repack_dictlist(pkgs))\n ]\n )", "response": "Repack packages specified using pkgs argument to pkg states into a single\n dictionary"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef pack_sources(sources, normalize=True):\n '''\n Accepts list of dicts (or a string representing a list of dicts) and packs\n the key/value pairs into a single dict.\n\n ``'[{\"foo\": \"salt://foo.rpm\"}, {\"bar\": \"salt://bar.rpm\"}]'`` would become\n ``{\"foo\": \"salt://foo.rpm\", \"bar\": \"salt://bar.rpm\"}``\n\n normalize : True\n Normalize the package name by removing the architecture, if the\n architecture of the package is different from the architecture of the\n operating system. The ability to disable this behavior is useful for\n poorly-created packages which include the architecture as an actual\n part of the name, such as kernel modules which match a specific kernel\n version.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg_resource.pack_sources '[{\"foo\": \"salt://foo.rpm\"}, {\"bar\": \"salt://bar.rpm\"}]'\n '''\n if normalize and 'pkg.normalize_name' in __salt__:\n _normalize_name = __salt__['pkg.normalize_name']\n else:\n _normalize_name = lambda pkgname: pkgname\n\n if isinstance(sources, six.string_types):\n try:\n sources = salt.utils.yaml.safe_load(sources)\n except salt.utils.yaml.parser.ParserError as err:\n log.error(err)\n return {}\n ret = {}\n for source in sources:\n if (not isinstance(source, dict)) or len(source) != 1:\n log.error('Invalid input: %s', pprint.pformat(sources))\n log.error('Input must be a list of 1-element dicts')\n return {}\n else:\n key = next(iter(source))\n ret[_normalize_name(key)] = source[key]\n return ret", "response": "Packs a list of dicts into a single dict."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef parse_targets(name=None,\n pkgs=None,\n sources=None,\n saltenv='base',\n normalize=True,\n **kwargs):\n '''\n Parses the input to pkg.install and returns back the package(s) to be\n installed. Returns a list of packages, as well as a string noting whether\n the packages are to come from a repository or a binary package.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg_resource.parse_targets\n '''\n if '__env__' in kwargs:\n # \"env\" is not supported; Use \"saltenv\".\n kwargs.pop('__env__')\n\n if __grains__['os'] == 'MacOS' and sources:\n log.warning('Parameter \"sources\" ignored on MacOS hosts.')\n\n version = kwargs.get('version')\n\n if pkgs and sources:\n log.error('Only one of \"pkgs\" and \"sources\" can be used.')\n return None, None\n\n elif 'advisory_ids' in kwargs:\n if pkgs:\n log.error('Cannot use \"advisory_ids\" and \"pkgs\" at the same time')\n return None, None\n elif kwargs['advisory_ids']:\n return kwargs['advisory_ids'], 'advisory'\n else:\n return [name], 'advisory'\n\n elif pkgs:\n if version is not None:\n log.warning('\\'version\\' argument will be ignored for multiple '\n 'package targets')\n pkgs = _repack_pkgs(pkgs, normalize=normalize)\n if not pkgs:\n return None, None\n else:\n return pkgs, 'repository'\n\n elif sources and __grains__['os'] != 'MacOS':\n if version is not None:\n log.warning('\\'version\\' argument will be ignored for multiple '\n 'package targets')\n sources = pack_sources(sources, normalize=normalize)\n if not sources:\n return None, None\n\n srcinfo = []\n for pkg_name, pkg_src in six.iteritems(sources):\n if __salt__['config.valid_fileproto'](pkg_src):\n # Cache package from remote source (salt master, HTTP, FTP) and\n # append the cached path.\n srcinfo.append(__salt__['cp.cache_file'](pkg_src, saltenv))\n else:\n # Package file local to the minion, just append the path to the\n # package file.\n if not os.path.isabs(pkg_src):\n raise SaltInvocationError(\n 'Path {0} for package {1} is either not absolute or '\n 'an invalid protocol'.format(pkg_src, pkg_name)\n )\n srcinfo.append(pkg_src)\n\n return srcinfo, 'file'\n\n elif name:\n if normalize:\n _normalize_name = \\\n __salt__.get('pkg.normalize_name', lambda pkgname: pkgname)\n packed = dict([(_normalize_name(x), version) for x in name.split(',')])\n else:\n packed = dict([(x, version) for x in name.split(',')])\n return packed, 'repository'\n\n else:\n log.error('No package sources provided')\n return None, None", "response": "Parses the input to pkg. install and returns back the package names and source files."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the version of the installed packages.", "response": "def version(*names, **kwargs):\n '''\n Common interface for obtaining the version of installed packages.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg_resource.version vim\n salt '*' pkg_resource.version foo bar baz\n salt '*' pkg_resource.version 'python*'\n '''\n ret = {}\n versions_as_list = \\\n salt.utils.data.is_true(kwargs.pop('versions_as_list', False))\n pkg_glob = False\n if names:\n pkgs = __salt__['pkg.list_pkgs'](versions_as_list=True, **kwargs)\n for name in names:\n if '*' in name:\n pkg_glob = True\n for match in fnmatch.filter(pkgs, name):\n ret[match] = pkgs.get(match, [])\n else:\n ret[name] = pkgs.get(name, [])\n if not versions_as_list:\n __salt__['pkg_resource.stringify'](ret)\n # Return a string if no globbing is used, and there is one item in the\n # return dict\n if len(ret) == 1 and not pkg_glob:\n try:\n return next(six.itervalues(ret))\n except StopIteration:\n return ''\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nadding a package to a dict of installed packages.", "response": "def add_pkg(pkgs, name, pkgver):\n '''\n Add a package to a dict of installed packages.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg_resource.add_pkg '{}' bind 9\n '''\n try:\n pkgs.setdefault(name, []).append(pkgver)\n except AttributeError as exc:\n log.exception(exc)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nsort packages in place.", "response": "def sort_pkglist(pkgs):\n '''\n Accepts a dict obtained from pkg.list_pkgs() and sorts in place the list of\n versions for any packages that have multiple versions installed, so that\n two package lists can be compared to one another.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg_resource.sort_pkglist '[\"3.45\", \"2.13\"]'\n '''\n # It doesn't matter that ['4.9','4.10'] would be sorted to ['4.10','4.9'],\n # so long as the sorting is consistent.\n try:\n for key in pkgs:\n # Passing the pkglist to set() also removes duplicate version\n # numbers (if present).\n pkgs[key] = sorted(set(pkgs[key]))\n except AttributeError as exc:\n log.exception(exc)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef stringify(pkgs):\n '''\n Takes a dict of package name/version information and joins each list of\n installed versions into a string.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg_resource.stringify 'vim: 7.127'\n '''\n try:\n for key in pkgs:\n pkgs[key] = ','.join(pkgs[key])\n except AttributeError as exc:\n log.exception(exc)", "response": "Takes a dict of package name and version information and joins each list of\n installed versions into a string."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef format_pkg_list(packages, versions_as_list, attr):\n '''\n Formats packages according to parameters for list_pkgs.\n '''\n ret = copy.deepcopy(packages)\n if attr:\n requested_attr = {'epoch', 'version', 'release', 'arch', 'install_date', 'install_date_time_t'}\n\n if attr != 'all':\n requested_attr &= set(attr + ['version'])\n\n for name in ret:\n versions = []\n for all_attr in ret[name]:\n filtered_attr = {}\n for key in requested_attr:\n if all_attr[key]:\n filtered_attr[key] = all_attr[key]\n versions.append(filtered_attr)\n ret[name] = versions\n return ret\n\n for name in ret:\n ret[name] = [format_version(d['epoch'], d['version'], d['release'])\n for d in ret[name]]\n if not versions_as_list:\n stringify(ret)\n return ret", "response": "Formats packages according to parameters for list_pkgs."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef format_version(epoch, version, release):\n '''\n Formats a version string for list_pkgs.\n '''\n full_version = '{0}:{1}'.format(epoch, version) if epoch else version\n if release:\n full_version += '-{0}'.format(release)\n return full_version", "response": "Formats a version string for list_pkgs."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nforces var to be a set", "response": "def _make_set(var):\n '''\n Force var to be a set\n '''\n if var is None:\n return set()\n if not isinstance(var, list):\n if isinstance(var, six.string_types):\n var = var.split()\n else:\n var = list(var)\n return set(var)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure that the variable is present in the base base", "response": "def present(name, value=None, contains=None, excludes=None):\n '''\n Verify that the variable is in the ``make.conf`` and has the provided\n settings. If value is set, contains and excludes will be ignored.\n\n name\n The variable name. This will automatically be converted to upper\n case since variables in ``make.conf`` are in upper case\n\n value\n Enforce that the value of the variable is set to the provided value\n\n contains\n Enforce that the value of the variable contains the provided value\n\n excludes\n Enforce that the value of the variable does not contain the provided\n value.\n '''\n ret = {'changes': {},\n 'comment': '',\n 'name': name,\n 'result': True}\n\n # Make name all Uppers since make.conf uses all Upper vars\n upper_name = name.upper()\n\n old_value = __salt__['makeconf.get_var'](upper_name)\n\n # If only checking if variable is present allows for setting the\n # variable outside of salt states, but the state can still ensure\n # that is exists\n if value is None and contains is None and excludes is None:\n # variable is present\n if old_value is not None:\n msg = 'Variable {0} is already present in make.conf'\n ret['comment'] = msg.format(name)\n else:\n if __opts__['test']:\n msg = 'Variable {0} is to be set in make.conf'\n ret['comment'] = msg.format(name)\n ret['result'] = None\n else:\n changes = __salt__['makeconf.set_var'](upper_name, '')\n\n # If failed to be set\n if changes[upper_name]['new'] is None:\n msg = 'Variable {0} failed to be set in make.conf'\n ret['comment'] = msg.format(name)\n ret['result'] = False\n else:\n msg = 'Variable {0} set in make.conf'\n ret['comment'] = msg.format(name)\n\n elif value is not None:\n # variable is present and is set to value\n if old_value is not None and old_value == value:\n msg = 'Variable {0} is already \"{1}\" in make.conf'\n ret['comment'] = msg.format(name, value)\n else:\n if __opts__['test']:\n msg = 'Variable {0} is to be set to \"{1}\" in make.conf'\n ret['comment'] = msg.format(name, value)\n ret['result'] = None\n else:\n changes = __salt__['makeconf.set_var'](upper_name, value)\n\n # If failed to be set\n new_value = __salt__['makeconf.get_var'](upper_name)\n if new_value is None or new_value != value:\n msg = 'Variable {0} failed to be set in make.conf'\n ret['comment'] = msg.format(name)\n ret['result'] = False\n else:\n msg = 'Variable {0} is set in make.conf'\n ret['changes'] = changes\n ret['comment'] = msg.format(name)\n\n elif contains is not None or excludes is not None:\n # Make these into sets to easily compare things\n contains_set = _make_set(contains)\n excludes_set = _make_set(excludes)\n old_value_set = _make_set(old_value)\n if contains_set.intersection(excludes_set):\n msg = 'Variable {0} cannot contain and exclude the same value'\n ret['comment'] = msg.format(name)\n ret['result'] = False\n else:\n to_append = set()\n to_trim = set()\n if contains is not None:\n to_append = contains_set.difference(old_value_set)\n if excludes is not None:\n to_trim = excludes_set.intersection(old_value_set)\n if not to_append and not to_trim:\n msg = 'Variable {0} is correct in make.conf'\n ret['comment'] = msg.format(name)\n else:\n if __opts__['test']:\n msg = 'Variable {0} is set to'.format(name)\n if to_append:\n msg += ' append \"{0}\"'.format(list(to_append))\n if to_trim:\n msg += ' trim \"{0}\"'.format(list(to_trim))\n msg += ' in make.conf'\n ret['comment'] = msg\n ret['result'] = None\n else:\n for value in to_append:\n __salt__['makeconf.append_var'](upper_name, value)\n for value in to_trim:\n __salt__['makeconf.trim_var'](upper_name, value)\n new_value = __salt__['makeconf.get_var'](upper_name)\n\n # TODO verify appends and trims worked\n ret['changes'] = {upper_name: {'old': old_value,\n 'new': new_value}}\n msg = 'Variable {0} is correct in make.conf'\n ret['comment'] = msg.format(name)\n\n # Now finally return\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef absent(name):\n '''\n Verify that the variable is not in the ``make.conf``.\n\n name\n The variable name. This will automatically be converted to upper\n case since variables in ``make.conf`` are in upper case\n '''\n ret = {'changes': {},\n 'comment': '',\n 'name': name,\n 'result': True}\n\n # Make name all Uppers since make.conf uses all Upper vars\n upper_name = name.upper()\n\n old_value = __salt__['makeconf.get_var'](upper_name)\n\n if old_value is None:\n msg = 'Variable {0} is already absent from make.conf'\n ret['comment'] = msg.format(name)\n else:\n if __opts__['test']:\n msg = 'Variable {0} is set to be removed from make.conf'\n ret['comment'] = msg.format(name)\n ret['result'] = None\n else:\n __salt__['makeconf.remove_var'](upper_name)\n\n new_value = __salt__['makeconf.get_var'](upper_name)\n if new_value is not None:\n msg = 'Variable {0} failed to be removed from make.conf'\n ret['comment'] = msg.format(name)\n ret['result'] = False\n else:\n msg = 'Variable {0} was removed from make.conf'\n ret['comment'] = msg.format(name)\n ret['result'] = True\n return ret", "response": "Verify that the variable is not in the make. conf"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn information for the specified user CLI Example : bash salt '*' shadow. info root", "response": "def info(name):\n '''\n Return information for the specified user\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' shadow.info root\n '''\n if HAS_SPWD:\n try:\n data = spwd.getspnam(name)\n ret = {\n 'name': data.sp_nam,\n 'passwd': data.sp_pwd,\n 'lstchg': data.sp_lstchg,\n 'min': data.sp_min,\n 'max': data.sp_max,\n 'warn': data.sp_warn,\n 'inact': data.sp_inact,\n 'expire': data.sp_expire}\n except KeyError:\n ret = {\n 'name': '',\n 'passwd': '',\n 'lstchg': '',\n 'min': '',\n 'max': '',\n 'warn': '',\n 'inact': '',\n 'expire': ''}\n return ret\n\n # SmartOS joyent_20130322T181205Z does not have spwd, but not all is lost\n # Return what we can know\n ret = {\n 'name': '',\n 'passwd': '',\n 'lstchg': '',\n 'min': '',\n 'max': '',\n 'warn': '',\n 'inact': '',\n 'expire': ''}\n\n try:\n data = pwd.getpwnam(name)\n ret.update({\n 'name': name\n })\n except KeyError:\n return ret\n\n # To compensate for lack of spwd module, read in password hash from /etc/shadow\n s_file = '/etc/shadow'\n if not os.path.isfile(s_file):\n return ret\n with salt.utils.files.fopen(s_file, 'rb') as ifile:\n for line in ifile:\n comps = line.strip().split(':')\n if comps[0] == name:\n ret.update({'passwd': comps[1]})\n\n # For SmartOS `passwd -s ` and the output format is:\n # name status mm/dd/yy min max warn\n #\n # Fields:\n # 1. Name: username\n # 2. Status:\n # - LK: locked\n # - NL: no login\n # - NP: No password\n # - PS: Password\n # 3. Last password change\n # 4. Minimum age\n # 5. Maximum age\n # 6. Warning period\n\n output = __salt__['cmd.run_all']('passwd -s {0}'.format(name), python_shell=False)\n if output['retcode'] != 0:\n return ret\n\n fields = output['stdout'].split()\n if len(fields) == 2:\n # For example:\n # root NL\n return ret\n # We have all fields:\n # buildbot L 05/09/2013 0 99999 7\n ret.update({\n 'name': data.pw_name,\n 'lstchg': fields[2],\n 'min': int(fields[3]),\n 'max': int(fields[4]),\n 'warn': int(fields[5]),\n 'inact': '',\n 'expire': ''\n })\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_maxdays(name, maxdays):\n '''\n Set the maximum number of days during which a password is valid. See man\n passwd.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' shadow.set_maxdays username 90\n '''\n pre_info = info(name)\n if maxdays == pre_info['max']:\n return True\n cmd = 'passwd -x {0} {1}'.format(maxdays, name)\n __salt__['cmd.run'](cmd, python_shell=False)\n post_info = info(name)\n if post_info['max'] != pre_info['max']:\n return post_info['max'] == maxdays", "response": "Set the maximum number of days during which a password is valid."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef set_mindays(name, mindays):\n '''\n Set the minimum number of days between password changes. See man passwd.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' shadow.set_mindays username 7\n '''\n pre_info = info(name)\n if mindays == pre_info['min']:\n return True\n cmd = 'passwd -n {0} {1}'.format(mindays, name)\n __salt__['cmd.run'](cmd, python_shell=False)\n post_info = info(name)\n if post_info['min'] != pre_info['min']:\n return post_info['min'] == mindays\n return False", "response": "Set the minimum number of days between password changes."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef del_password(name):\n '''\n .. versionadded:: 2015.8.8\n\n Delete the password from name user\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' shadow.del_password username\n '''\n cmd = 'passwd -d {0}'.format(name)\n __salt__['cmd.run'](cmd, python_shell=False, output_loglevel='quiet')\n uinfo = info(name)\n return not uinfo['passwd']", "response": "Delete the password from user\nCTYPE"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsets the password for a named user.", "response": "def set_password(name, password):\n '''\n Set the password for a named user. The password must be a properly defined\n hash, the password hash can be generated with this command:\n ``openssl passwd -1 ``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' shadow.set_password root $1$UYCIxa628.9qXjpQCjM4a..\n '''\n s_file = '/etc/shadow'\n ret = {}\n if not os.path.isfile(s_file):\n return ret\n lines = []\n with salt.utils.files.fopen(s_file, 'rb') as ifile:\n for line in ifile:\n comps = line.strip().split(':')\n if comps[0] != name:\n lines.append(line)\n continue\n comps[1] = password\n line = ':'.join(comps)\n lines.append('{0}\\n'.format(line))\n with salt.utils.files.fopen(s_file, 'w+') as ofile:\n lines = [salt.utils.stringutils.to_str(_l) for _l in lines]\n ofile.writelines(lines)\n uinfo = info(name)\n return uinfo['passwd'] == password"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the number of days of warning before a password change is required.", "response": "def set_warndays(name, warndays):\n '''\n Set the number of days of warning before a password change is required.\n See man passwd.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' shadow.set_warndays username 7\n '''\n pre_info = info(name)\n if warndays == pre_info['warn']:\n return True\n cmd = 'passwd -w {0} {1}'.format(warndays, name)\n __salt__['cmd.run'](cmd, python_shell=False)\n post_info = info(name)\n if post_info['warn'] != pre_info['warn']:\n return post_info['warn'] == warndays\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the ext_pillar for the specified minion and pillar repos", "response": "def ext_pillar(minion_id, pillar, *repos): # pylint: disable=unused-argument\n '''\n Checkout the ext_pillar sources and compile the resulting pillar SLS\n '''\n opts = copy.deepcopy(__opts__)\n opts['pillar_roots'] = {}\n opts['__git_pillar'] = True\n git_pillar = salt.utils.gitfs.GitPillar(\n opts,\n repos,\n per_remote_overrides=PER_REMOTE_OVERRIDES,\n per_remote_only=PER_REMOTE_ONLY,\n global_only=GLOBAL_ONLY)\n if __opts__.get('__role') == 'minion':\n # If masterless, fetch the remotes. We'll need to remove this once\n # we make the minion daemon able to run standalone.\n git_pillar.fetch_remotes()\n git_pillar.checkout()\n ret = {}\n merge_strategy = __opts__.get(\n 'pillar_source_merging_strategy',\n 'smart'\n )\n merge_lists = __opts__.get(\n 'pillar_merge_lists',\n False\n )\n for pillar_dir, env in six.iteritems(git_pillar.pillar_dirs):\n # Map env if env == '__env__' before checking the env value\n if env == '__env__':\n env = opts.get('pillarenv') \\\n or opts.get('saltenv') \\\n or opts.get('git_pillar_base')\n log.debug('__env__ maps to %s', env)\n\n # If pillarenv is set, only grab pillars with that match pillarenv\n if opts['pillarenv'] and env != opts['pillarenv']:\n log.debug(\n 'env \\'%s\\' for pillar dir \\'%s\\' does not match '\n 'pillarenv \\'%s\\', skipping',\n env, pillar_dir, opts['pillarenv']\n )\n continue\n if pillar_dir in git_pillar.pillar_linked_dirs:\n log.debug(\n 'git_pillar is skipping processing on %s as it is a '\n 'mounted repo', pillar_dir\n )\n continue\n else:\n log.debug(\n 'git_pillar is processing pillar SLS from %s for pillar '\n 'env \\'%s\\'', pillar_dir, env\n )\n\n pillar_roots = [pillar_dir]\n\n if __opts__['git_pillar_includes']:\n # Add the rest of the pillar_dirs in this environment to the\n # list, excluding the current pillar_dir being processed. This\n # is because it was already specified above as the first in the\n # list, so that its top file is sourced from the correct\n # location and not from another git_pillar remote.\n pillar_roots.extend(\n [d for (d, e) in six.iteritems(git_pillar.pillar_dirs)\n if env == e and d != pillar_dir]\n )\n\n opts['pillar_roots'] = {env: pillar_roots}\n\n local_pillar = Pillar(opts, __grains__, minion_id, env)\n ret = salt.utils.dictupdate.merge(\n ret,\n local_pillar.compile_pillar(ext=False),\n strategy=merge_strategy,\n merge_lists=merge_lists\n )\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_remote_login(enable):\n '''\n Set the remote login (SSH) to either on or off.\n\n :param bool enable: True to enable, False to disable. \"On\" and \"Off\" are\n also acceptable values. Additionally you can pass 1 and 0 to represent\n True and False respectively\n\n :return: True if successful, False if not\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.set_remote_login True\n '''\n state = __utils__['mac_utils.validate_enabled'](enable)\n\n cmd = 'systemsetup -f -setremotelogin {0}'.format(state)\n __utils__['mac_utils.execute_return_success'](cmd)\n\n return __utils__['mac_utils.confirm_updated'](state,\n get_remote_login,\n normalize_ret=True)", "response": "Sets the remote login on or off."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsetting whether the server responds to events sent by other computers.", "response": "def set_remote_events(enable):\n '''\n Set whether the server responds to events sent by other computers (such as\n AppleScripts)\n\n :param bool enable: True to enable, False to disable. \"On\" and \"Off\" are\n also acceptable values. Additionally you can pass 1 and 0 to represent\n True and False respectively\n\n :return: True if successful, False if not\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.set_remote_events On\n '''\n state = __utils__['mac_utils.validate_enabled'](enable)\n\n cmd = 'systemsetup -setremoteappleevents {0}'.format(state)\n __utils__['mac_utils.execute_return_success'](cmd)\n\n return __utils__['mac_utils.confirm_updated'](\n state,\n get_remote_events,\n normalize_ret=True,\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_computer_name(name):\n '''\n Set the computer name\n\n :param str name: The new computer name\n\n :return: True if successful, False if not\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.set_computer_name \"Mike's Mac\"\n '''\n cmd = 'systemsetup -setcomputername \"{0}\"'.format(name)\n __utils__['mac_utils.execute_return_success'](cmd)\n\n return __utils__['mac_utils.confirm_updated'](\n name,\n get_computer_name,\n )", "response": "Set the computer name"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets the local subnet name", "response": "def set_subnet_name(name):\n '''\n Set the local subnet name\n\n :param str name: The new local subnet name\n\n .. note::\n Spaces are changed to dashes. Other special characters are removed.\n\n :return: True if successful, False if not\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n The following will be set as 'Mikes-Mac'\n salt '*' system.set_subnet_name \"Mike's Mac\"\n '''\n cmd = 'systemsetup -setlocalsubnetname \"{0}\"'.format(name)\n __utils__['mac_utils.execute_return_success'](cmd)\n\n return __utils__['mac_utils.confirm_updated'](\n name,\n get_subnet_name,\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_startup_disk(path):\n '''\n Set the current startup disk to the indicated path. Use\n ``system.list_startup_disks`` to find valid startup disks on the system.\n\n :param str path: The valid startup disk path\n\n :return: True if successful, False if not\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.set_startup_disk /System/Library/CoreServices\n '''\n if path not in list_startup_disks():\n msg = 'Invalid value passed for path.\\n' \\\n 'Must be a valid startup disk as found in ' \\\n 'system.list_startup_disks.\\n' \\\n 'Passed: {0}'.format(path)\n raise SaltInvocationError(msg)\n\n cmd = 'systemsetup -setstartupdisk {0}'.format(path)\n __utils__['mac_utils.execute_return_result'](cmd)\n\n return __utils__['mac_utils.confirm_updated'](\n path,\n get_startup_disk,\n )", "response": "Set the current startup disk to the specified path."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset the number of seconds after which the computer will start up after power failure.", "response": "def set_restart_delay(seconds):\n '''\n Set the number of seconds after which the computer will start up after a\n power failure.\n\n .. warning::\n\n This command fails with the following error:\n\n ``Error, IOServiceOpen returned 0x10000003``\n\n The setting is not updated. This is an apple bug. It seems like it may\n only work on certain versions of Mac Server X. This article explains the\n issue in more detail, though it is quite old.\n\n http://lists.apple.com/archives/macos-x-server/2006/Jul/msg00967.html\n\n :param int seconds: The number of seconds. Must be a multiple of 30\n\n :return: True if successful, False if not\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.set_restart_delay 180\n '''\n if seconds % 30 != 0:\n msg = 'Invalid value passed for seconds.\\n' \\\n 'Must be a multiple of 30.\\n' \\\n 'Passed: {0}'.format(seconds)\n raise SaltInvocationError(msg)\n\n cmd = 'systemsetup -setwaitforstartupafterpowerfailure {0}'.format(seconds)\n __utils__['mac_utils.execute_return_success'](cmd)\n\n return __utils__['mac_utils.confirm_updated'](\n seconds,\n get_restart_delay,\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets whether or not the keyboard should be disabled when the X Serve enclosure lock is engaged.", "response": "def set_disable_keyboard_on_lock(enable):\n '''\n Get whether or not the keyboard should be disabled when the X Serve\n enclosure lock is engaged.\n\n :param bool enable: True to enable, False to disable. \"On\" and \"Off\" are\n also acceptable values. Additionally you can pass 1 and 0 to represent\n True and False respectively\n\n :return: True if successful, False if not\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.set_disable_keyboard_on_lock False\n '''\n state = __utils__['mac_utils.validate_enabled'](enable)\n\n cmd = 'systemsetup -setdisablekeyboardwhenenclosurelockisengaged ' \\\n '{0}'.format(state)\n __utils__['mac_utils.execute_return_success'](cmd)\n\n return __utils__['mac_utils.confirm_updated'](\n state,\n get_disable_keyboard_on_lock,\n normalize_ret=True,\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting the kernel to boot in 32 or 64 bit mode on next boot.", "response": "def set_boot_arch(arch='default'):\n '''\n Set the kernel to boot in 32 or 64 bit mode on next boot.\n\n .. note::\n When this function fails with the error ``changes to kernel\n architecture failed to save!``, then the boot arch is not updated.\n This is either an Apple bug, not available on the test system, or a\n result of system files being locked down in macOS (SIP Protection).\n\n :param str arch: A string representing the desired architecture. If no\n value is passed, default is assumed. Valid values include:\n\n - i386\n - x86_64\n - default\n\n :return: True if successful, False if not\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' system.set_boot_arch i386\n '''\n if arch not in ['i386', 'x86_64', 'default']:\n msg = 'Invalid value passed for arch.\\n' \\\n 'Must be i386, x86_64, or default.\\n' \\\n 'Passed: {0}'.format(arch)\n raise SaltInvocationError(msg)\n\n cmd = 'systemsetup -setkernelbootarchitecture {0}'.format(arch)\n __utils__['mac_utils.execute_return_success'](cmd)\n\n return __utils__['mac_utils.confirm_updated'](\n arch,\n get_boot_arch,\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef create_target_group(name,\n protocol,\n port,\n vpc_id,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n health_check_protocol='HTTP',\n health_check_port='traffic-port',\n health_check_path='/',\n health_check_interval_seconds=30,\n health_check_timeout_seconds=5,\n healthy_threshold_count=5,\n unhealthy_threshold_count=2):\n '''\n Create target group if not present.\n\n name\n (string) - The name of the target group.\n protocol\n (string) - The protocol to use for routing traffic to the targets\n port\n (int) - The port on which the targets receive traffic. This port is used unless\n you specify a port override when registering the traffic.\n vpc_id\n (string) - The identifier of the virtual private cloud (VPC).\n health_check_protocol\n (string) - The protocol the load balancer uses when performing health check on\n targets. The default is the HTTP protocol.\n health_check_port\n (string) - The port the load balancer uses when performing health checks on\n targets. The default is 'traffic-port', which indicates the port on which each\n target receives traffic from the load balancer.\n health_check_path\n (string) - The ping path that is the destination on the targets for health\n checks. The default is /.\n health_check_interval_seconds\n (integer) - The approximate amount of time, in seconds, between health checks\n of an individual target. The default is 30 seconds.\n health_check_timeout_seconds\n (integer) - The amount of time, in seconds, during which no response from a\n target means a failed health check. The default is 5 seconds.\n healthy_threshold_count\n (integer) - The number of consecutive health checks successes required before\n considering an unhealthy target healthy. The default is 5.\n unhealthy_threshold_count\n (integer) - The number of consecutive health check failures required before\n considering a target unhealthy. The default is 2.\n\n returns\n (bool) - True on success, False on failure.\n\n CLI example:\n .. code-block:: bash\n\n salt myminion boto_elbv2.create_target_group learn1give1 protocol=HTTP port=54006 vpc_id=vpc-deadbeef\n '''\n\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if target_group_exists(name, region, key, keyid, profile):\n return True\n\n try:\n alb = conn.create_target_group(Name=name, Protocol=protocol, Port=port,\n VpcId=vpc_id, HealthCheckProtocol=health_check_protocol,\n HealthCheckPort=health_check_port,\n HealthCheckPath=health_check_path,\n HealthCheckIntervalSeconds=health_check_interval_seconds,\n HealthCheckTimeoutSeconds=health_check_timeout_seconds,\n HealthyThresholdCount=healthy_threshold_count,\n UnhealthyThresholdCount=unhealthy_threshold_count)\n if alb:\n log.info('Created ALB %s: %s', name, alb['TargetGroups'][0]['TargetGroupArn'])\n return True\n else:\n log.error('Failed to create ALB %s', name)\n return False\n except ClientError as error:\n log.error(\n 'Failed to create ALB %s: %s: %s',\n name, error.response['Error']['Code'],\n error.response['Error']['Message'],\n exc_info_on_loglevel=logging.DEBUG\n )", "response": "Creates a target group for routing traffic to the targets."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delete_target_group(name,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Delete target group.\n\n name\n (string) - Target Group Name or Amazon Resource Name (ARN).\n\n returns\n (bool) - True on success, False on failure.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elbv2.delete_target_group arn:aws:elasticloadbalancing:us-west-2:644138682826:targetgroup/learn1give1-api/414788a16b5cf163\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if not target_group_exists(name, region, key, keyid, profile):\n return True\n\n try:\n if name.startswith('arn:aws:elasticloadbalancing'):\n conn.delete_target_group(TargetGroupArn=name)\n log.info('Deleted target group %s', name)\n else:\n tg_info = conn.describe_target_groups(Names=[name])\n if len(tg_info['TargetGroups']) != 1:\n return False\n arn = tg_info['TargetGroups'][0]['TargetGroupArn']\n conn.delete_target_group(TargetGroupArn=arn)\n log.info('Deleted target group %s ARN %s', name, arn)\n return True\n except ClientError as error:\n log.error('Failed to delete target group %s', name,\n exc_info_on_loglevel=logging.DEBUG)\n return False", "response": "Delete an elastic load balancing target group."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking to see if an elastic load balancer target group exists.", "response": "def target_group_exists(name,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Check to see if an target group exists.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elbv2.target_group_exists arn:aws:elasticloadbalancing:us-west-2:644138682826:targetgroup/learn1give1-api/414788a16b5cf163\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n if name.startswith('arn:aws:elasticloadbalancing'):\n alb = conn.describe_target_groups(TargetGroupArns=[name])\n else:\n alb = conn.describe_target_groups(Names=[name])\n if alb:\n return True\n else:\n log.warning('The target group does not exist in region %s', region)\n return False\n except ClientError as error:\n log.warning('target_group_exists check for %s returned: %s', name, error)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the curret health check status for the specified targets.", "response": "def describe_target_health(name,\n targets=None,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Get the curret health check status for targets in a target group.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elbv2.describe_target_health arn:aws:elasticloadbalancing:us-west-2:644138682826:targetgroup/learn1give1-api/414788a16b5cf163 targets=[\"i-isdf23ifjf\"]\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n if targets:\n targetsdict = []\n for target in targets:\n targetsdict.append({\"Id\": target})\n instances = conn.describe_target_health(TargetGroupArn=name, Targets=targetsdict)\n else:\n instances = conn.describe_target_health(TargetGroupArn=name)\n ret = {}\n for instance in instances['TargetHealthDescriptions']:\n ret.update({instance['Target']['Id']: instance['TargetHealth']['State']})\n\n return ret\n except ClientError as error:\n log.warning(error)\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef register_targets(name,\n targets,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Register targets to a target froup of an ALB. ``targets`` is either a\n instance id string or a list of instance id's.\n\n Returns:\n\n - ``True``: instance(s) registered successfully\n - ``False``: instance(s) failed to be registered\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elbv2.register_targets myelb instance_id\n salt myminion boto_elbv2.register_targets myelb \"[instance_id,instance_id]\"\n '''\n targetsdict = []\n if isinstance(targets, six.string_types) or isinstance(targets, six.text_type):\n targetsdict.append({\"Id\": targets})\n else:\n for target in targets:\n targetsdict.append({\"Id\": target})\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n registered_targets = conn.register_targets(TargetGroupArn=name, Targets=targetsdict)\n if registered_targets:\n return True\n return False\n except ClientError as error:\n log.warning(error)\n return False", "response": "Register targets to an ALB."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndescribe the load balancers or all of your load balancers.", "response": "def describe_load_balancers(names=None,\n load_balancer_arns=None,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Describes the specified load balancer or all of your load balancers.\n\n Returns: list\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elbv2.describe_load_balancers\n salt myminion boto_elbv2.describe_load_balancers alb_name\n salt myminion boto_elbv2.describe_load_balancers \"[alb_name,alb_name]\"\n '''\n if names and load_balancer_arns:\n raise SaltInvocationError('At most one of names or load_balancer_arns may '\n 'be provided')\n if names:\n albs = names\n elif load_balancer_arns:\n albs = load_balancer_arns\n else:\n albs = None\n\n albs_list = []\n if albs:\n if isinstance(albs, str) or isinstance(albs, six.text_type):\n albs_list.append(albs)\n else:\n for alb in albs:\n albs_list.append(alb)\n\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n if names:\n ret = conn.describe_load_balancers(Names=albs_list)['LoadBalancers']\n elif load_balancer_arns:\n ret = conn.describe_load_balancers(LoadBalancerArns=albs_list)['LoadBalancers']\n else:\n ret = []\n next_marker = ''\n while True:\n r = conn.describe_load_balancers(Marker=next_marker)\n for alb in r['LoadBalancers']:\n ret.append(alb)\n if 'NextMarker' in r:\n next_marker = r['NextMarker']\n else:\n break\n return ret if ret else []\n\n except ClientError as error:\n log.warning(error)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef describe_target_groups(names=None,\n target_group_arns=None,\n load_balancer_arn=None,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Describes the specified target groups or all of your target groups. By default,\n all target groups are described. Alternatively, you can specify one of the\n following to filter the results: the ARN of the load balancer, the names of\n one or more target groups, or the ARNs of one or more target groups.\n\n Returns: list\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_elbv2.describe_target_groups\n salt myminion boto_elbv2.describe_target_groups target_group_name\n salt myminion boto_elbv2.describe_target_groups \"[tg_name,tg_name]\"\n '''\n if names and target_group_arns:\n raise SaltInvocationError('At most one of names or target_group_arns may '\n 'be provided')\n if names:\n target_groups = names\n elif target_group_arns:\n target_groups = target_group_arns\n else:\n target_groups = None\n\n tg_list = []\n if target_groups:\n if isinstance(target_groups, str) or isinstance(target_groups, six.text_type):\n tg_list.append(target_groups)\n else:\n for group in target_groups:\n tg_list.append(group)\n\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n if names:\n ret = conn.describe_target_groups(Names=tg_list)['TargetGroups']\n elif target_group_arns:\n ret = conn.describe_target_groups(TargetGroupArns=tg_list)['TargetGroups']\n elif load_balancer_arn:\n ret = conn.describe_target_groups(LoadBalancerArn=load_balancer_arn)['TargetGroups']\n else:\n ret = []\n next_marker = ''\n while True:\n r = conn.describe_target_groups(Marker=next_marker)\n for alb in r['TargetGroups']:\n ret.append(alb)\n if 'NextMarker' in r:\n next_marker = r['NextMarker']\n else:\n break\n return ret if ret else []\n\n except ClientError as error:\n log.warning(error)\n return False", "response": "Describe the target groups."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets a SSM Parameter Store object from SSM Parameter Store", "response": "def get_parameter(name, withdecryption=False, resp_json=False, region=None, key=None, keyid=None, profile=None):\n '''\n Retrives a parameter from SSM Parameter Store\n\n .. versionadded:: Neon\n\n .. code-block:: text\n\n salt-call boto_ssm.get_parameter test-param withdescription=True\n '''\n conn = __utils__['boto3.get_connection']('ssm', region=region, key=key, keyid=keyid, profile=profile)\n try:\n resp = conn.get_parameter(Name=name, WithDecryption=withdecryption)\n except conn.exceptions.ParameterNotFound:\n log.warning(\"get_parameter: Unable to locate name: %s\", name)\n return False\n\n if resp_json:\n return json.loads(resp['Parameter']['Value'])\n else:\n return resp['Parameter']['Value']"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsets a SSM parameter in SSM", "response": "def put_parameter(Name,\n Value,\n Description=None,\n Type='String',\n KeyId=None,\n Overwrite=False,\n AllowedPattern=None,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Sets a parameter in the SSM parameter store\n\n .. versionadded:: Neon\n\n .. code-block:: text\n\n salt-call boto_ssm.put_parameter test-param test_value Type=SecureString KeyId=alias/aws/ssm Description='test encrypted key'\n '''\n conn = __utils__['boto3.get_connection']('ssm', region=region, key=key, keyid=keyid, profile=profile)\n if Type not in ('String', 'StringList', 'SecureString'):\n raise AssertionError('Type needs to be String|StringList|SecureString')\n if Type == 'SecureString' and not KeyId:\n raise AssertionError('Require KeyId with SecureString')\n\n boto_args = {}\n if Description:\n boto_args['Description'] = Description\n if KeyId:\n boto_args['KeyId'] = KeyId\n if AllowedPattern:\n boto_args['AllowedPattern'] = AllowedPattern\n\n try:\n resp = conn.put_parameter(Name=Name, Value=Value, Type=Type, Overwrite=Overwrite, **boto_args)\n except conn.exceptions.ParameterAlreadyExists:\n log.warning(\"The parameter already exists.\"\n \" To overwrite this value, set the Overwrite option in the request to True\")\n return False\n return resp['Version']"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delete_parameter(Name, region=None, key=None, keyid=None, profile=None):\n '''\n Removes a parameter from the SSM parameter store\n\n .. versionadded:: Neon\n\n .. code-block:: text\n salt-call boto_ssm.delete_parameter test-param\n '''\n conn = __utils__['boto3.get_connection']('ssm', region=region, key=key, keyid=keyid, profile=profile)\n try:\n resp = conn.delete_parameter(Name=Name)\n except conn.exceptions.ParameterNotFound:\n log.warning(\"delete_parameter: Unable to locate name: %s\", Name)\n return False\n if resp['ResponseMetadata']['HTTPStatusCode'] == 200:\n return True\n else:\n return False", "response": "Delete a SSM parameter from the SSM parameter store."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef base64_b64encode(instr):\n '''\n Encode a string as base64 using the \"modern\" Python interface.\n\n Among other possible differences, the \"modern\" encoder does not include\n newline ('\\\\n') characters in the encoded output.\n '''\n return salt.utils.stringutils.to_unicode(\n base64.b64encode(salt.utils.stringutils.to_bytes(instr)),\n encoding='utf8' if salt.utils.platform.is_windows() else None\n )", "response": "Encode a string as base64 using the modern Python interface."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef base64_b64decode(instr):\n '''\n Decode a base64-encoded string using the \"modern\" Python interface.\n '''\n decoded = base64.b64decode(salt.utils.stringutils.to_bytes(instr))\n try:\n return salt.utils.stringutils.to_unicode(\n decoded,\n encoding='utf8' if salt.utils.platform.is_windows() else None\n )\n except UnicodeDecodeError:\n return decoded", "response": "Decode a base64 - encoded string using the modern Python interface."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef base64_encodestring(instr):\n '''\n Encode a string as base64 using the \"legacy\" Python interface.\n\n Among other possible differences, the \"legacy\" encoder includes\n a newline ('\\\\n') character after every 76 characters and always\n at the end of the encoded string.\n '''\n return salt.utils.stringutils.to_unicode(\n base64.encodestring(salt.utils.stringutils.to_bytes(instr)),\n encoding='utf8' if salt.utils.platform.is_windows() else None\n )", "response": "Encode a string as base64 using the legacy Python interface."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef base64_decodestring(instr):\n '''\n Decode a base64-encoded string using the \"legacy\" Python interface.\n '''\n b = salt.utils.stringutils.to_bytes(instr)\n try:\n # PY3\n decoded = base64.decodebytes(b)\n except AttributeError:\n # PY2\n decoded = base64.decodestring(b)\n try:\n return salt.utils.stringutils.to_unicode(\n decoded,\n encoding='utf8' if salt.utils.platform.is_windows() else None\n )\n except UnicodeDecodeError:\n return decoded", "response": "Decode a base64 - encoded string using the legacy Python interface."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngenerating an md5 hash of a given string.", "response": "def md5_digest(instr):\n '''\n Generate an md5 hash of a given string.\n '''\n return salt.utils.stringutils.to_unicode(\n hashlib.md5(salt.utils.stringutils.to_bytes(instr)).hexdigest()\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef sha1_digest(instr):\n '''\n Generate an sha1 hash of a given string.\n '''\n if six.PY3:\n b = salt.utils.stringutils.to_bytes(instr)\n return hashlib.sha1(b).hexdigest()\n return hashlib.sha1(instr).hexdigest()", "response": "Generate an sha1 hash of a given string."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngenerating a sha256 hash of a given string.", "response": "def sha256_digest(instr):\n '''\n Generate a sha256 hash of a given string.\n '''\n return salt.utils.stringutils.to_unicode(\n hashlib.sha256(salt.utils.stringutils.to_bytes(instr)).hexdigest()\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngenerating a sha512 hash of a given string", "response": "def sha512_digest(instr):\n '''\n Generate a sha512 hash of a given string\n '''\n return salt.utils.stringutils.to_unicode(\n hashlib.sha512(salt.utils.stringutils.to_bytes(instr)).hexdigest()\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef hmac_signature(string, shared_secret, challenge_hmac):\n '''\n Verify a challenging hmac signature against a string / shared-secret\n Returns a boolean if the verification succeeded or failed.\n '''\n msg = salt.utils.stringutils.to_bytes(string)\n key = salt.utils.stringutils.to_bytes(shared_secret)\n challenge = salt.utils.stringutils.to_bytes(challenge_hmac)\n hmac_hash = hmac.new(key, msg, hashlib.sha256)\n valid_hmac = base64.b64encode(hmac_hash.digest())\n return valid_hmac == challenge", "response": "Verify a challenging hmac signature against a string"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef random_hash(size=9999999999, hash_type=None):\n '''\n Return a hash of a randomized data from random.SystemRandom()\n '''\n if not hash_type:\n hash_type = 'md5'\n hasher = getattr(hashlib, hash_type)\n return hasher(salt.utils.stringutils.to_bytes(six.text_type(random.SystemRandom().randint(0, size)))).hexdigest()", "response": "Return a randomized hash of a randomized data from random. SystemRandom"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_hash(path, form='sha256', chunk_size=65536):\n '''\n Get the hash sum of a file\n\n This is better than ``get_sum`` for the following reasons:\n - It does not read the entire file into memory.\n - It does not return a string on error. The returned value of\n ``get_sum`` cannot really be trusted since it is vulnerable to\n collisions: ``get_sum(..., 'xyz') == 'Hash xyz not supported'``\n '''\n hash_type = hasattr(hashlib, form) and getattr(hashlib, form) or None\n if hash_type is None:\n raise ValueError('Invalid hash type: {0}'.format(form))\n\n with salt.utils.files.fopen(path, 'rb') as ifile:\n hash_obj = hash_type()\n # read the file in in chunks, not the entire file\n for chunk in iter(lambda: ifile.read(chunk_size), b''):\n hash_obj.update(chunk)\n return hash_obj.hexdigest()", "response": "Get the hash of a file"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate the digest with the content of the file at path.", "response": "def add(self, path):\n '''\n Update digest with the file content by path.\n\n :param path:\n :return:\n '''\n with salt.utils.files.fopen(path, 'rb') as ifile:\n for chunk in iter(lambda: ifile.read(self.__buff), b''):\n self.__digest.update(chunk)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef digest(self):\n '''\n Get digest.\n\n :return:\n '''\n\n return salt.utils.stringutils.to_str(self.__digest.hexdigest() + os.linesep)", "response": "Get digest of the current object"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _load_properties(property_name, config_option, set_default=False, default=None):\n '''\n Load properties for the cassandra module from config or pillar.\n\n :param property_name: The property to load.\n :type property_name: str or list of str\n :param config_option: The name of the config option.\n :type config_option: str\n :param set_default: Should a default be set if not found in config.\n :type set_default: bool\n :param default: The default value to be set.\n :type default: str or int\n :return: The property fetched from the configuration or default.\n :rtype: str or list of str\n '''\n if not property_name:\n log.debug(\"No property specified in function, trying to load from salt configuration\")\n try:\n options = __salt__['config.option']('cassandra')\n except BaseException as e:\n log.error(\"Failed to get cassandra config options. Reason: %s\", e)\n raise\n\n loaded_property = options.get(config_option)\n if not loaded_property:\n if set_default:\n log.debug('Setting default Cassandra %s to %s', config_option, default)\n loaded_property = default\n else:\n log.error('No cassandra %s specified in the configuration or passed to the module.', config_option)\n raise CommandExecutionError(\"ERROR: Cassandra {0} cannot be empty.\".format(config_option))\n return loaded_property\n return property_name", "response": "Load properties for cassandra module from config or pillar."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nparse out ssl_options for Cassandra cluster connection.", "response": "def _get_ssl_opts():\n '''\n Parse out ssl_options for Cassandra cluster connection.\n Make sure that the ssl_version (if any specified) is valid.\n '''\n sslopts = __salt__['config.option']('cassandra').get('ssl_options', None)\n ssl_opts = {}\n\n if sslopts:\n ssl_opts['ca_certs'] = sslopts['ca_certs']\n if SSL_VERSION in sslopts:\n if not sslopts[SSL_VERSION].startswith('PROTOCOL_'):\n valid_opts = ', '.join(\n [x for x in dir(ssl) if x.startswith('PROTOCOL_')]\n )\n raise CommandExecutionError('Invalid protocol_version '\n 'specified! '\n 'Please make sure '\n 'that the ssl protocol'\n 'version is one from the SSL'\n 'module. '\n 'Valid options are '\n '{0}'.format(valid_opts))\n else:\n ssl_opts[SSL_VERSION] = \\\n getattr(ssl, sslopts[SSL_VERSION])\n return ssl_opts\n else:\n return None"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _connect(contact_points=None, port=None, cql_user=None, cql_pass=None,\n protocol_version=None):\n '''\n Connect to a Cassandra cluster.\n\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str or list of str\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :param protocol_version: Cassandra protocol version to use.\n :type port: int\n :return: The session and cluster objects.\n :rtype: cluster object, session object\n '''\n # Lazy load the Cassandra cluster and session for this module by creating a\n # cluster and session when cql_query is called the first time. Get the\n # Cassandra cluster and session from this module's __context__ after it is\n # loaded the first time cql_query is called.\n #\n # TODO: Call cluster.shutdown() when the module is unloaded on\n # master/minion shutdown. Currently, Master.shutdown() and Minion.shutdown()\n # do nothing to allow loaded modules to gracefully handle resources stored\n # in __context__ (i.e. connection pools). This means that the connection\n # pool is orphaned and Salt relies on Cassandra to reclaim connections.\n # Perhaps if Master/Minion daemons could be enhanced to call an \"__unload__\"\n # function, or something similar for each loaded module, connection pools\n # and the like can be gracefully reclaimed/shutdown.\n if (__context__\n and 'cassandra_cql_returner_cluster' in __context__\n and 'cassandra_cql_returner_session' in __context__):\n return __context__['cassandra_cql_returner_cluster'], __context__['cassandra_cql_returner_session']\n else:\n\n contact_points = _load_properties(property_name=contact_points, config_option='cluster')\n contact_points = contact_points if isinstance(contact_points, list) else contact_points.split(',')\n port = _load_properties(property_name=port, config_option='port', set_default=True, default=9042)\n cql_user = _load_properties(property_name=cql_user, config_option='username', set_default=True, default=\"cassandra\")\n cql_pass = _load_properties(property_name=cql_pass, config_option='password', set_default=True, default=\"cassandra\")\n protocol_version = _load_properties(property_name=protocol_version,\n config_option='protocol_version',\n set_default=True, default=4)\n\n try:\n auth_provider = PlainTextAuthProvider(username=cql_user, password=cql_pass)\n ssl_opts = _get_ssl_opts()\n if ssl_opts:\n cluster = Cluster(contact_points,\n port=port,\n auth_provider=auth_provider,\n ssl_options=ssl_opts,\n protocol_version=protocol_version,\n compression=True)\n else:\n cluster = Cluster(contact_points, port=port,\n auth_provider=auth_provider,\n protocol_version=protocol_version,\n compression=True)\n for recontimes in range(1, 4):\n try:\n session = cluster.connect()\n break\n except OperationTimedOut:\n log.warning('Cassandra cluster.connect timed out, try %s', recontimes)\n if recontimes >= 3:\n raise\n\n # TODO: Call cluster.shutdown() when the module is unloaded on shutdown.\n __context__['cassandra_cql_returner_cluster'] = cluster\n __context__['cassandra_cql_returner_session'] = session\n __context__['cassandra_cql_prepared'] = {}\n\n log.debug('Successfully connected to Cassandra cluster at %s', contact_points)\n return cluster, session\n except TypeError:\n pass\n except (ConnectionException, ConnectionShutdown, NoHostAvailable):\n log.error('Could not connect to Cassandra cluster at %s', contact_points)\n raise CommandExecutionError('ERROR: Could not connect to Cassandra cluster.')", "response": "Connect to a Cassandra cluster."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nexecuting a query on a Cassandra cluster and return a list of results.", "response": "def cql_query(query, contact_points=None, port=None, cql_user=None, cql_pass=None):\n '''\n Run a query on a Cassandra cluster and return a dictionary.\n\n :param query: The query to execute.\n :type query: str\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str | list[str]\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :param params: The parameters for the query, optional.\n :type params: str\n :return: A dictionary from the return values of the query\n :rtype: list[dict]\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'cassandra-server' cassandra_cql.cql_query \"SELECT * FROM users_by_name WHERE first_name = 'jane'\"\n '''\n try:\n cluster, session = _connect(contact_points=contact_points, port=port, cql_user=cql_user, cql_pass=cql_pass)\n except CommandExecutionError:\n log.critical('Could not get Cassandra cluster session.')\n raise\n except BaseException as e:\n log.critical('Unexpected error while getting Cassandra cluster session: %s', e)\n raise\n\n session.row_factory = dict_factory\n ret = []\n\n # Cassandra changed their internal schema from v2 to v3\n # If the query contains a dictionary sorted by versions\n # Find the query for the current cluster version.\n # https://issues.apache.org/jira/browse/CASSANDRA-6717\n if isinstance(query, dict):\n cluster_version = version(contact_points=contact_points,\n port=port,\n cql_user=cql_user,\n cql_pass=cql_pass)\n match = re.match(r'^(\\d+)\\.(\\d+)(?:\\.(\\d+))?', cluster_version)\n major, minor, point = match.groups()\n # try to find the specific version in the query dictionary\n # then try the major version\n # otherwise default to the highest version number\n try:\n query = query[cluster_version]\n except KeyError:\n query = query.get(major, max(query))\n log.debug('New query is: %s', query)\n\n try:\n results = session.execute(query)\n except BaseException as e:\n log.error('Failed to execute query: %s\\n reason: %s', query, e)\n msg = \"ERROR: Cassandra query failed: {0} reason: {1}\".format(query, e)\n raise CommandExecutionError(msg)\n\n if results:\n for result in results:\n values = {}\n for key, value in six.iteritems(result):\n # Salt won't return dictionaries with odd types like uuid.UUID\n if not isinstance(value, six.text_type):\n # Must support Cassandra collection types.\n # Namely, Cassandras set, list, and map collections.\n if not isinstance(value, (set, list, dict)):\n value = six.text_type(value)\n values[key] = value\n ret.append(values)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef cql_query_with_prepare(query, statement_name, statement_arguments, callback_errors=None, contact_points=None,\n port=None, cql_user=None, cql_pass=None, **kwargs):\n '''\n Run a query on a Cassandra cluster and return a dictionary.\n\n This function should not be used asynchronously for SELECTs -- it will not\n return anything and we don't currently have a mechanism for handling a future\n that will return results.\n\n :param query: The query to execute.\n :type query: str\n :param statement_name: Name to assign the prepared statement in the __context__ dictionary\n :type statement_name: str\n :param statement_arguments: Bind parameters for the SQL statement\n :type statement_arguments: list[str]\n :param async: Run this query in asynchronous mode\n :type async: bool\n :param callback_errors: Function to call after query runs if there is an error\n :type callback_errors: Function callable\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str | list[str]\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :param params: The parameters for the query, optional.\n :type params: str\n :return: A dictionary from the return values of the query\n :rtype: list[dict]\n\n\n CLI Example:\n\n .. code-block:: bash\n\n # Insert data asynchronously\n salt this-node cassandra_cql.cql_query_with_prepare \"name_insert\" \"INSERT INTO USERS (first_name, last_name) VALUES (?, ?)\" \\\n statement_arguments=['John','Doe'], asynchronous=True\n\n # Select data, should not be asynchronous because there is not currently a facility to return data from a future\n salt this-node cassandra_cql.cql_query_with_prepare \"name_select\" \"SELECT * FROM USERS WHERE first_name=?\" \\\n statement_arguments=['John']\n '''\n # Backward-compatibility with Python 3.7: \"async\" is a reserved word\n asynchronous = kwargs.get('async', False)\n try:\n cluster, session = _connect(contact_points=contact_points, port=port,\n cql_user=cql_user, cql_pass=cql_pass)\n except CommandExecutionError:\n log.critical('Could not get Cassandra cluster session.')\n raise\n except BaseException as e:\n log.critical('Unexpected error while getting Cassandra cluster session: %s', e)\n raise\n\n if statement_name not in __context__['cassandra_cql_prepared']:\n try:\n bound_statement = session.prepare(query)\n __context__['cassandra_cql_prepared'][statement_name] = bound_statement\n except BaseException as e:\n log.critical('Unexpected error while preparing SQL statement: %s', e)\n raise\n else:\n bound_statement = __context__['cassandra_cql_prepared'][statement_name]\n\n session.row_factory = dict_factory\n ret = []\n\n try:\n if asynchronous:\n future_results = session.execute_async(bound_statement.bind(statement_arguments))\n # future_results.add_callbacks(_async_log_errors)\n else:\n results = session.execute(bound_statement.bind(statement_arguments))\n except BaseException as e:\n log.error('Failed to execute query: %s\\n reason: %s', query, e)\n msg = \"ERROR: Cassandra query failed: {0} reason: {1}\".format(query, e)\n raise CommandExecutionError(msg)\n\n if not asynchronous and results:\n for result in results:\n values = {}\n for key, value in six.iteritems(result):\n # Salt won't return dictionaries with odd types like uuid.UUID\n if not isinstance(value, six.text_type):\n # Must support Cassandra collection types.\n # Namely, Cassandras set, list, and map collections.\n if not isinstance(value, (set, list, dict)):\n value = six.text_type(value)\n values[key] = value\n ret.append(values)\n\n # If this was a synchronous call, then we either have an empty list\n # because there was no return, or we have a return\n # If this was an asynchronous call we only return the empty list\n return ret", "response": "This function is used to execute a CQL query on a Cassandra cluster and return a dictionary of the result values."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list_column_families(keyspace=None, contact_points=None, port=None, cql_user=None, cql_pass=None):\n '''\n List column families in a Cassandra cluster for all keyspaces or just the provided one.\n\n :param keyspace: The keyspace to provide the column families for, optional.\n :type keyspace: str\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str | list[str]\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :return: The column families in this Cassandra cluster.\n :rtype: list[dict]\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion1' cassandra_cql.list_column_families\n\n salt 'minion1' cassandra_cql.list_column_families contact_points=minion1\n\n salt 'minion1' cassandra_cql.list_column_families keyspace=system\n '''\n where_clause = \"where keyspace_name = '{0}'\".format(keyspace) if keyspace else \"\"\n\n query = {\n '2': '''select columnfamily_name from system.schema_columnfamilies\n {0};'''.format(where_clause),\n '3': '''select column_name from system_schema.columns\n {0};'''.format(where_clause),\n }\n\n ret = {}\n\n try:\n ret = cql_query(query, contact_points, port, cql_user, cql_pass)\n except CommandExecutionError:\n log.critical('Could not list column families.')\n raise\n except BaseException as e:\n log.critical('Unexpected error while listing column families: %s', e)\n raise\n\n return ret", "response": "List the column families in a Cassandra cluster."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef keyspace_exists(keyspace, contact_points=None, port=None, cql_user=None, cql_pass=None):\n '''\n Check if a keyspace exists in a Cassandra cluster.\n\n :param keyspace The keyspace name to check for.\n :type keyspace: str\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str | list[str]\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :return: The info for the keyspace or False if it does not exist.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion1' cassandra_cql.keyspace_exists keyspace=system\n '''\n query = {\n '2': '''select keyspace_name from system.schema_keyspaces\n where keyspace_name = '{0}';'''.format(keyspace),\n '3': '''select keyspace_name from system_schema.keyspaces\n where keyspace_name = '{0}';'''.format(keyspace),\n }\n\n try:\n ret = cql_query(query, contact_points, port, cql_user, cql_pass)\n except CommandExecutionError:\n log.critical('Could not determine if keyspace exists.')\n raise\n except BaseException as e:\n log.critical('Unexpected error while determining if keyspace exists: %s', e)\n raise\n\n return True if ret else False", "response": "Check if a keyspace exists in a Cassandra cluster."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncreates a new keyspace in Cassandra.", "response": "def create_keyspace(keyspace, replication_strategy='SimpleStrategy', replication_factor=1, replication_datacenters=None,\n contact_points=None, port=None, cql_user=None, cql_pass=None):\n '''\n Create a new keyspace in Cassandra.\n\n :param keyspace: The keyspace name\n :type keyspace: str\n :param replication_strategy: either `SimpleStrategy` or `NetworkTopologyStrategy`\n :type replication_strategy: str\n :param replication_factor: number of replicas of data on multiple nodes. not used if using NetworkTopologyStrategy\n :type replication_factor: int\n :param replication_datacenters: string or dict of datacenter names to replication factors, required if using\n NetworkTopologyStrategy (will be a dict if coming from state file).\n :type replication_datacenters: str | dict[str, int]\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str | list[str]\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :return: The info for the keyspace or False if it does not exist.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n # CLI Example:\n salt 'minion1' cassandra_cql.create_keyspace keyspace=newkeyspace\n\n salt 'minion1' cassandra_cql.create_keyspace keyspace=newkeyspace replication_strategy=NetworkTopologyStrategy \\\n replication_datacenters='{\"datacenter_1\": 3, \"datacenter_2\": 2}'\n '''\n existing_keyspace = keyspace_exists(keyspace, contact_points, port)\n if not existing_keyspace:\n # Add the strategy, replication_factor, etc.\n replication_map = {\n 'class': replication_strategy\n }\n\n if replication_datacenters:\n if isinstance(replication_datacenters, six.string_types):\n try:\n replication_datacenter_map = salt.utils.json.loads(replication_datacenters)\n replication_map.update(**replication_datacenter_map)\n except BaseException: # pylint: disable=W0703\n log.error(\"Could not load json replication_datacenters.\")\n return False\n else:\n replication_map.update(**replication_datacenters)\n else:\n replication_map['replication_factor'] = replication_factor\n\n query = '''create keyspace {0}\n with replication = {1}\n and durable_writes = true;'''.format(keyspace, replication_map)\n\n try:\n cql_query(query, contact_points, port, cql_user, cql_pass)\n except CommandExecutionError:\n log.critical('Could not create keyspace.')\n raise\n except BaseException as e:\n log.critical('Unexpected error while creating keyspace: %s', e)\n raise"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndrop a keyspace if it exists in a Cassandra cluster.", "response": "def drop_keyspace(keyspace, contact_points=None, port=None, cql_user=None, cql_pass=None):\n '''\n Drop a keyspace if it exists in a Cassandra cluster.\n\n :param keyspace: The keyspace to drop.\n :type keyspace: str\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str | list[str]\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :return: The info for the keyspace or False if it does not exist.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion1' cassandra_cql.drop_keyspace keyspace=test\n\n salt 'minion1' cassandra_cql.drop_keyspace keyspace=test contact_points=minion1\n '''\n existing_keyspace = keyspace_exists(keyspace, contact_points, port)\n if existing_keyspace:\n query = '''drop keyspace {0};'''.format(keyspace)\n try:\n cql_query(query, contact_points, port, cql_user, cql_pass)\n except CommandExecutionError:\n log.critical('Could not drop keyspace.')\n raise\n except BaseException as e:\n log.critical('Unexpected error while dropping keyspace: %s', e)\n raise\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef list_users(contact_points=None, port=None, cql_user=None, cql_pass=None):\n '''\n List existing users in this Cassandra cluster.\n\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str | list[str]\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :return: The list of existing users.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion1' cassandra_cql.list_users\n\n salt 'minion1' cassandra_cql.list_users contact_points=minion1\n '''\n query = \"list users;\"\n\n ret = {}\n\n try:\n ret = cql_query(query, contact_points, port, cql_user, cql_pass)\n except CommandExecutionError:\n log.critical('Could not list users.')\n raise\n except BaseException as e:\n log.critical('Unexpected error while listing users: %s', e)\n raise\n\n return ret", "response": "List existing users in this Cassandra cluster."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a new cassandra user with credentials and superuser status.", "response": "def create_user(username, password, superuser=False, contact_points=None, port=None, cql_user=None, cql_pass=None):\n '''\n Create a new cassandra user with credentials and superuser status.\n\n :param username: The name of the new user.\n :type username: str\n :param password: The password of the new user.\n :type password: str\n :param superuser: Is the new user going to be a superuser? default: False\n :type superuser: bool\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str | list[str]\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :return:\n :rtype:\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion1' cassandra_cql.create_user username=joe password=secret\n\n salt 'minion1' cassandra_cql.create_user username=joe password=secret superuser=True\n\n salt 'minion1' cassandra_cql.create_user username=joe password=secret superuser=True contact_points=minion1\n '''\n superuser_cql = 'superuser' if superuser else 'nosuperuser'\n query = '''create user if not exists {0} with password '{1}' {2};'''.format(username, password, superuser_cql)\n log.debug(\"Attempting to create a new user with username=%s superuser=%s\", username, superuser_cql)\n\n # The create user query doesn't actually return anything if the query succeeds.\n # If the query fails, catch the exception, log a messange and raise it again.\n try:\n cql_query(query, contact_points, port, cql_user, cql_pass)\n except CommandExecutionError:\n log.critical('Could not create user.')\n raise\n except BaseException as e:\n log.critical('Unexpected error while creating user: %s', e)\n raise\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_permissions(username=None, resource=None, resource_type='keyspace', permission=None, contact_points=None,\n port=None, cql_user=None, cql_pass=None):\n '''\n List permissions.\n\n :param username: The name of the user to list permissions for.\n :type username: str\n :param resource: The resource (keyspace or table), if None, permissions for all resources are listed.\n :type resource: str\n :param resource_type: The resource_type (keyspace or table), defaults to 'keyspace'.\n :type resource_type: str\n :param permission: A permission name (e.g. select), if None, all permissions are listed.\n :type permission: str\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str | list[str]\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :return: Dictionary of permissions.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion1' cassandra_cql.list_permissions\n\n salt 'minion1' cassandra_cql.list_permissions username=joe resource=test_keyspace permission=select\n\n salt 'minion1' cassandra_cql.list_permissions username=joe resource=test_table resource_type=table \\\n permission=select contact_points=minion1\n '''\n keyspace_cql = \"{0} {1}\".format(resource_type, resource) if resource else \"all keyspaces\"\n permission_cql = \"{0} permission\".format(permission) if permission else \"all permissions\"\n query = \"list {0} on {1}\".format(permission_cql, keyspace_cql)\n\n if username:\n query = \"{0} of {1}\".format(query, username)\n\n log.debug(\"Attempting to list permissions with query '%s'\", query)\n\n ret = {}\n\n try:\n ret = cql_query(query, contact_points, port, cql_user, cql_pass)\n except CommandExecutionError:\n log.critical('Could not list permissions.')\n raise\n except BaseException as e:\n log.critical('Unexpected error while listing permissions: %s', e)\n raise\n\n return ret", "response": "List permissions for a keyspace or table."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef grant_permission(username, resource=None, resource_type='keyspace', permission=None, contact_points=None, port=None,\n cql_user=None, cql_pass=None):\n '''\n Grant permissions to a user.\n\n :param username: The name of the user to grant permissions to.\n :type username: str\n :param resource: The resource (keyspace or table), if None, permissions for all resources are granted.\n :type resource: str\n :param resource_type: The resource_type (keyspace or table), defaults to 'keyspace'.\n :type resource_type: str\n :param permission: A permission name (e.g. select), if None, all permissions are granted.\n :type permission: str\n :param contact_points: The Cassandra cluster addresses, can either be a string or a list of IPs.\n :type contact_points: str | list[str]\n :param cql_user: The Cassandra user if authentication is turned on.\n :type cql_user: str\n :param cql_pass: The Cassandra user password if authentication is turned on.\n :type cql_pass: str\n :param port: The Cassandra cluster port, defaults to None.\n :type port: int\n :return:\n :rtype:\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion1' cassandra_cql.grant_permission\n\n salt 'minion1' cassandra_cql.grant_permission username=joe resource=test_keyspace permission=select\n\n salt 'minion1' cassandra_cql.grant_permission username=joe resource=test_table resource_type=table \\\n permission=select contact_points=minion1\n '''\n permission_cql = \"grant {0}\".format(permission) if permission else \"grant all permissions\"\n resource_cql = \"on {0} {1}\".format(resource_type, resource) if resource else \"on all keyspaces\"\n query = \"{0} {1} to {2}\".format(permission_cql, resource_cql, username)\n log.debug(\"Attempting to grant permissions with query '%s'\", query)\n\n try:\n cql_query(query, contact_points, port, cql_user, cql_pass)\n except CommandExecutionError:\n log.critical('Could not grant permissions.')\n raise\n except BaseException as e:\n log.critical('Unexpected error while granting permissions: %s', e)\n raise\n\n return True", "response": "Grant permissions to a user."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_journal():\n '''\n Return the active running journal object\n '''\n if 'systemd.journald' in __context__:\n return __context__['systemd.journald']\n __context__['systemd.journald'] = systemd.journal.Reader()\n # get to the end of the journal\n __context__['systemd.journald'].seek_tail()\n __context__['systemd.journald'].get_previous()\n return __context__['systemd.journald']", "response": "Return the active running journal object"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_options(ret=None):\n '''\n Get the couchdb options from salt.\n '''\n attrs = {'url': 'url',\n 'db': 'db',\n 'user': 'user',\n 'passwd': 'passwd',\n 'redact_pws': 'redact_pws',\n 'minimum_return': 'minimum_return'}\n\n _options = salt.returners.get_returner_options(__virtualname__,\n ret,\n attrs,\n __salt__=__salt__,\n __opts__=__opts__)\n if 'url' not in _options:\n log.debug(\"Using default url.\")\n _options['url'] = \"http://salt:5984/\"\n\n if 'db' not in _options:\n log.debug(\"Using default database.\")\n _options['db'] = \"salt\"\n\n if 'user' not in _options:\n log.debug(\"Not athenticating with a user.\")\n _options['user'] = None\n\n if 'passwd' not in _options:\n log.debug(\"Not athenticating with a password.\")\n _options['passwd'] = None\n\n if 'redact_pws' not in _options:\n log.debug(\"Not redacting passwords.\")\n _options['redact_pws'] = None\n\n if 'minimum_return' not in _options:\n log.debug(\"Not minimizing the return object.\")\n _options['minimum_return'] = None\n\n return _options", "response": "Get the couchdb options from salt."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _generate_doc(ret):\n '''\n Create a object that will be saved into the database based on\n options.\n '''\n\n # Create a copy of the object that we will return.\n retc = ret.copy()\n\n # Set the ID of the document to be the JID.\n retc[\"_id\"] = ret[\"jid\"]\n\n # Add a timestamp field to the document\n retc[\"timestamp\"] = time.time()\n\n return retc", "response": "Generate a document that will be saved into the database based on the options passed in."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nmaking a HTTP request. Returns the JSON parse or an error.", "response": "def _request(method, url, content_type=None, _data=None, user=None, passwd=None):\n '''\n Makes a HTTP request. Returns the JSON parse, or an obj with an error.\n '''\n opener = _build_opener(_HTTPHandler)\n request = _Request(url, data=_data)\n if content_type:\n request.add_header('Content-Type', content_type)\n if user and passwd:\n auth_encode = '{0}:{1}'.format(user, passwd).encode('base64')[:-1]\n auth_basic = \"Basic {0}\".format(auth_encode)\n request.add_header('Authorization', auth_basic)\n request.add_header('Accept', 'application/json')\n request.get_method = lambda: method\n try:\n handler = opener.open(request)\n except HTTPError as exc:\n return {'error': '{0}'.format(exc)}\n return salt.utils.json.loads(handler.read())"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _generate_event_doc(event):\n '''\n Create a object that will be saved into the database based in\n options.\n '''\n\n # Create a copy of the object that we will return.\n eventc = event.copy()\n\n # Set the ID of the document to be the JID.\n eventc[\"_id\"] = '{}-{}'.format(\n event.get('tag', '').split('/')[2],\n event.get('tag', '').split('/')[3]\n )\n\n # Add a timestamp field to the document\n eventc[\"timestamp\"] = time.time()\n\n # remove any return data as it's captured in the \"returner\" function\n if eventc.get('data').get('return'):\n del eventc['data']['return']\n\n return eventc", "response": "Generate the document that will be returned by the event."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ntakes in the return and shove it into the couchdb database.", "response": "def returner(ret):\n '''\n Take in the return and shove it into the couchdb database.\n '''\n\n options = _get_options(ret)\n\n # Check to see if the database exists.\n _response = _request(\"GET\",\n options['url'] + \"_all_dbs\",\n user=options['user'],\n passwd=options['passwd'])\n if options['db'] not in _response:\n\n # Make a PUT request to create the database.\n _response = _request(\"PUT\",\n options['url'] + options['db'],\n user=options['user'],\n passwd=options['passwd'])\n\n # Confirm that the response back was simple 'ok': true.\n if 'ok' not in _response or _response['ok'] is not True:\n log.error('Nothing logged! Lost data. Unable to create database \"%s\"', options['db'])\n log.debug('_response object is: %s', _response)\n return\n log.info('Created database \"%s\"', options['db'])\n\n if boltons_lib:\n # redact all passwords if options['redact_pws'] is True\n if options['redact_pws']:\n ret_remap_pws = remap(ret, visit=_redact_passwords)\n else:\n ret_remap_pws = ret\n\n # remove all return values starting with '__pub' if options['minimum_return'] is True\n if options['minimum_return']:\n ret_remapped = remap(ret_remap_pws, visit=_minimize_return)\n else:\n ret_remapped = ret_remap_pws\n else:\n log.info('boltons library not installed. pip install boltons. https://github.com/mahmoud/boltons.')\n ret_remapped = ret\n\n # Call _generate_doc to get a dict object of the document we're going to shove into the database.\n doc = _generate_doc(ret_remapped)\n\n # Make the actual HTTP PUT request to create the doc.\n _response = _request(\"PUT\",\n options['url'] + options['db'] + \"/\" + doc['_id'],\n 'application/json',\n salt.utils.json.dumps(doc))\n\n # Sanity check regarding the response..\n if 'ok' not in _response or _response['ok'] is not True:\n log.error('Nothing logged! Lost data. Unable to create document: \"%s\"', _response)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of events to CouchDB server.", "response": "def event_return(events):\n '''\n Return event to CouchDB server\n Requires that configuration be enabled via 'event_return'\n option in master config.\n\n Example:\n\n event_return:\n - couchdb\n\n '''\n log.debug('events data is: %s', events)\n\n options = _get_options()\n\n # Check to see if the database exists.\n _response = _request(\"GET\", options['url'] + \"_all_dbs\")\n event_db = '{}-events'.format(options['db'])\n if event_db not in _response:\n\n # Make a PUT request to create the database.\n log.info('Creating database \"%s\"', event_db)\n _response = _request(\"PUT\",\n options['url'] + event_db,\n user=options['user'],\n passwd=options['passwd'])\n\n # Confirm that the response back was simple 'ok': true.\n if 'ok' not in _response or _response['ok'] is not True:\n log.error('Nothing logged! Lost data. Unable to create database \"%s\"', event_db)\n return\n log.info('Created database \"%s\"', event_db)\n\n for event in events:\n # Call _generate_doc to get a dict object of the document we're going to shove into the database.\n log.debug('event data is: %s', event)\n doc = _generate_event_doc(event)\n\n # Make the actual HTTP PUT request to create the doc.\n _response = _request(\"PUT\",\n options['url'] + event_db + \"/\" + doc['_id'],\n 'application/json',\n salt.utils.json.dumps(doc))\n # Sanity check regarding the response..\n if 'ok' not in _response or _response['ok'] is not True:\n log.error('Nothing logged! Lost data. Unable to create document: \"%s\"', _response)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_jids():\n '''\n List all the jobs that we have..\n '''\n options = _get_options(ret=None)\n _response = _request(\"GET\", options['url'] + options['db'] + \"/_all_docs?include_docs=true\")\n\n # Make sure the 'total_rows' is returned.. if not error out.\n if 'total_rows' not in _response:\n log.error('Didn\\'t get valid response from requesting all docs: %s', _response)\n return {}\n\n # Return the rows.\n ret = {}\n for row in _response['rows']:\n # Because this shows all the documents in the database, including the\n # design documents, verify the id is salt jid\n jid = row['id']\n if not salt.utils.jid.is_jid(jid):\n continue\n\n ret[jid] = salt.utils.jid.format_jid_instance(jid, row['doc'])\n\n return ret", "response": "Get all the jobs that we have"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a dict with key being minion and value being the job details of the last run of function fun.", "response": "def get_fun(fun):\n '''\n Return a dict with key being minion and value\n being the job details of the last run of function 'fun'.\n '''\n\n # Get the options..\n options = _get_options(ret=None)\n\n # Define a simple return object.\n _ret = {}\n\n # get_minions takes care of calling ensure_views for us. For each minion we know about\n for minion in get_minions():\n\n # Make a query of the by-minion-and-timestamp view and limit the count to 1.\n _response = _request(\"GET\",\n options['url'] +\n options['db'] +\n ('/_design/salt/_view/by-minion-fun-times'\n 'tamp?descending=true&endkey=[\"{0}\",\"{1}'\n '\",0]&startkey=[\"{2}\",\"{3}\",9999999999]&'\n 'limit=1').format(minion,\n fun,\n minion,\n fun))\n # Skip the minion if we got an error..\n if 'error' in _response:\n log.warning('Got an error when querying for last command '\n 'by a minion: %s', _response['error'])\n continue\n\n # Skip the minion if we didn't get any rows back. ( IE function that\n # they're looking for has a typo in it or some such ).\n if not _response['rows']:\n continue\n\n # Set the respnse ..\n _ret[minion] = _response['rows'][0]['value']\n\n return _ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a list of minion identifiers from a request of the view.", "response": "def get_minions():\n '''\n Return a list of minion identifiers from a request of the view.\n '''\n options = _get_options(ret=None)\n\n # Make sure the views are valid, which includes the minions..\n if not ensure_views():\n return []\n\n # Make the request for the view..\n _response = _request(\"GET\",\n options['url'] +\n options['db'] +\n \"/_design/salt/_view/minions?group=true\")\n\n # Verify that we got a response back.\n if 'rows' not in _response:\n log.error('Unable to get available minions: %s', _response)\n return []\n\n # Iterate over the rows to build up a list return it.\n _ret = []\n for row in _response['rows']:\n _ret.append(row['key'])\n return _ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_salt_view():\n '''\n Helper function that sets the salt design\n document. Uses get_valid_salt_views and some hardcoded values.\n '''\n\n options = _get_options(ret=None)\n\n # Create the new object that we will shove in as the design doc.\n new_doc = {}\n new_doc['views'] = get_valid_salt_views()\n new_doc['language'] = \"javascript\"\n\n # Make the request to update the design doc.\n _response = _request(\"PUT\",\n options['url'] + options['db'] + \"/_design/salt\",\n \"application/json\", salt.utils.json.dumps(new_doc))\n if 'error' in _response:\n log.warning('Unable to set the salt design document: %s', _response['error'])\n return False\n return True", "response": "Helper function that sets the salt design document. Uses get_valid_salt_views and some hardcoded values. Returns True if successful False otherwise."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget the load of a resource", "response": "def get_load(jid):\n '''\n Included for API consistency\n '''\n options = _get_options(ret=None)\n _response = _request(\"GET\", options['url'] + options['db'] + '/' + jid)\n if 'error' in _response:\n log.error('Unable to get JID \"%s\" : \"%s\"', jid, _response)\n return {}\n return {_response['id']: _response}"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _auth(profile=None, api_version=1, **connection_args):\n '''\n Set up heat credentials, returns\n `heatclient.client.Client`. Optional parameter\n \"api_version\" defaults to 1.\n\n Only intended to be used within heat-enabled modules\n '''\n\n if profile:\n prefix = profile + ':keystone.'\n else:\n prefix = 'keystone.'\n\n def get(key, default=None):\n '''\n Checks connection_args, then salt-minion config,\n falls back to specified default value.\n '''\n return connection_args.get('connection_' + key,\n __salt__['config.get'](prefix + key, default))\n\n user = get('user', 'admin')\n password = get('password', None)\n tenant = get('tenant', 'admin')\n tenant_id = get('tenant_id')\n auth_url = get('auth_url', 'http://127.0.0.1:35357/v2.0')\n insecure = get('insecure', False)\n admin_token = get('token')\n region_name = get('region_name', None)\n\n if admin_token and api_version != 1 and not password:\n # If we had a password we could just\n # ignore the admin-token and move on...\n raise SaltInvocationError('Only can use keystone admin token ' +\n 'with Heat API v1')\n elif password:\n # Can't use the admin-token anyway\n kwargs = {'username': user,\n 'password': password,\n 'tenant_id': tenant_id,\n 'auth_url': auth_url,\n 'region_name': region_name,\n 'tenant_name': tenant}\n # 'insecure' keyword not supported by all v2.0 keystone clients\n # this ensures it's only passed in when defined\n if insecure:\n kwargs['insecure'] = True\n elif api_version == 1 and admin_token:\n kwargs = {'token': admin_token,\n 'auth_url': auth_url}\n else:\n raise SaltInvocationError('No credentials to authenticate with.')\n\n token = __salt__['keystone.token_get'](profile)\n kwargs['token'] = token['id']\n # This doesn't realy prevent the password to show up\n # in the minion log as keystoneclient.session is\n # logging it anyway when in debug-mode\n kwargs.pop('password')\n try:\n heat_endpoint = __salt__['keystone.endpoint_get']('heat', profile)['url']\n except KeyError:\n heat_endpoint = __salt__['keystone.endpoint_get']('heat', profile)['publicurl']\n heat_endpoint = heat_endpoint % token\n log.debug('Calling heatclient.client.Client(%s, %s, **%s)',\n api_version, heat_endpoint, kwargs)\n # may raise exc.HTTPUnauthorized, exc.HTTPNotFound\n # but we deal with those elsewhere\n return heatclient.client.Client(api_version, endpoint=heat_endpoint, **kwargs)", "response": "Return a keystone client. Client object for authentication"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _parse_environment(env_str):\n '''\n Parsing template\n '''\n try:\n env = salt.utils.yaml.safe_load(env_str)\n except salt.utils.yaml.YAMLError as exc:\n raise ValueError(six.text_type(exc))\n else:\n if env is None:\n env = {}\n elif not isinstance(env, dict):\n raise ValueError(\n 'The environment is not a valid YAML mapping data type.'\n )\n\n for param in env:\n if param not in SECTIONS:\n raise ValueError('environment has wrong section \"{0}\"'.format(param))\n\n return env", "response": "Parses the environment string into a dictionary of key - value pairs."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_stack_events(h_client, stack_id, event_args):\n '''\n Get event for stack\n '''\n event_args['stack_id'] = stack_id\n event_args['resource_name'] = None\n try:\n events = h_client.events.list(**event_args)\n except heatclient.exc.HTTPNotFound as exc:\n raise heatclient.exc.CommandError(six.text_type(exc))\n else:\n for event in events:\n event.stack_name = stack_id.split('/')[0]\n return events", "response": "Get events for a given stack."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _poll_for_events(h_client, stack_name, action=None, poll_period=5,\n timeout=60, marker=None):\n '''\n Polling stack events\n '''\n if action:\n stop_status = ('{0}_FAILED'.format(action), '{0}_COMPLETE'.format(action))\n stop_check = lambda a: a in stop_status\n else:\n stop_check = lambda a: a.endswith('_COMPLETE') or a.endswith('_FAILED')\n timeout_sec = timeout * 60\n no_event_polls = 0\n msg_template = ('\\n Stack %(name)s %(status)s \\n')\n while True:\n events = _get_stack_events(h_client, stack_id=stack_name,\n event_args={'sort_dir': 'asc', 'marker': marker})\n\n if not events:\n no_event_polls += 1\n else:\n no_event_polls = 0\n # set marker to last event that was received.\n marker = getattr(events[-1], 'id', None)\n for event in events:\n # check if stack event was also received\n if getattr(event, 'resource_name', '') == stack_name:\n stack_status = getattr(event, 'resource_status', '')\n msg = msg_template % dict(\n name=stack_name, status=stack_status)\n if stop_check(stack_status):\n return stack_status, msg\n\n if no_event_polls >= 2:\n # after 2 polls with no events, fall back to a stack get\n stack = h_client.stacks.get(stack_name)\n stack_status = stack.stack_status\n msg = msg_template % dict(\n name=stack_name, status=stack_status)\n if stop_check(stack_status):\n return stack_status, msg\n # go back to event polling again\n no_event_polls = 0\n\n time.sleep(poll_period)\n timeout_sec -= poll_period\n if timeout_sec <= 0:\n stack_status = '{0}_FAILED'.format(action)\n msg = 'Timeout expired'\n return stack_status, msg", "response": "Poll for events from a stack."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of available stack", "response": "def list_stack(profile=None):\n '''\n Return a list of available stack (heat stack-list)\n\n profile\n Profile to use\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' heat.list_stack profile=openstack1\n '''\n ret = {}\n h_client = _auth(profile)\n for stack in h_client.stacks.list():\n links = {}\n for link in stack.links:\n links[link['rel']] = link['href']\n ret[stack.stack_name] = {\n 'status': stack.stack_status,\n 'id': stack.id,\n 'name': stack.stack_name,\n 'creation': stack.creation_time,\n 'owner': stack.stack_owner,\n 'reason': stack.stack_status_reason,\n 'links': links,\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns details about a specific stack", "response": "def show_stack(name=None, profile=None):\n '''\n Return details about a specific stack (heat stack-show)\n\n name\n Name of the stack\n\n profile\n Profile to use\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' heat.show_stack name=mystack profile=openstack1\n '''\n h_client = _auth(profile)\n if not name:\n return {\n 'result': False,\n 'comment':\n 'Parameter name missing or None'\n }\n try:\n ret = {}\n stack = h_client.stacks.get(name)\n links = {}\n for link in stack.links:\n links[link['rel']] = link['href']\n ret[stack.stack_name] = {\n 'status': stack.stack_status,\n 'id': stack.id,\n 'name': stack.stack_name,\n 'creation': stack.creation_time,\n 'owner': stack.stack_owner,\n 'reason': stack.stack_status_reason,\n 'parameters': stack.parameters,\n 'links': links,\n }\n ret['result'] = True\n except heatclient.exc.HTTPNotFound:\n return {\n 'result': False,\n 'comment': 'No stack {0}'.format(name)\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete_stack(name=None, poll=0, timeout=60, profile=None):\n '''\n Delete a stack (heat stack-delete)\n\n name\n Name of the stack\n\n poll\n Poll and report events until stack complete\n\n timeout\n Stack creation timeout in minute\n\n profile\n Profile to use\n\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' heat.delete_stack name=mystack poll=5 \\\\\n profile=openstack1\n '''\n h_client = _auth(profile)\n ret = {\n 'result': True,\n 'comment': ''\n }\n if not name:\n ret['result'] = False\n ret['comment'] = 'Parameter name missing or None'\n return ret\n try:\n h_client.stacks.delete(name)\n except heatclient.exc.HTTPNotFound:\n ret['result'] = False\n ret['comment'] = 'No stack {0}'.format(name)\n except heatclient.exc.HTTPForbidden as forbidden:\n log.exception(forbidden)\n ret['result'] = False\n ret['comment'] = six.text_type(forbidden)\n if ret['result'] is False:\n return ret\n\n if poll > 0:\n try:\n stack_status, msg = _poll_for_events(h_client, name, action='DELETE',\n poll_period=poll, timeout=timeout)\n except heatclient.exc.CommandError:\n ret['comment'] = 'Deleted stack {0}.'.format(name)\n return ret\n except Exception as ex: # pylint: disable=W0703\n log.exception('Delete failed %s', ex)\n ret['result'] = False\n ret['comment'] = '{0}'.format(ex)\n return ret\n\n if stack_status == 'DELETE_FAILED':\n ret['result'] = False\n ret['comment'] = 'Deleted stack FAILED\\'{0}\\'{1}.'.format(name, msg)\n else:\n ret['comment'] = 'Deleted stack {0}.'.format(name)\n return ret", "response": "Delete a stack in a single resource."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_stack(name=None, template_file=None, environment=None,\n parameters=None, poll=0, rollback=False, timeout=60,\n profile=None, enviroment=None):\n '''\n Create a stack (heat stack-create)\n\n name\n Name of the new stack\n\n template_file\n File of template\n\n environment\n File of environment\n\n parameters\n Parameter dict used to create the stack\n\n poll\n Poll and report events until stack complete\n\n rollback\n Enable rollback on create failure\n\n timeout\n Stack creation timeout in minutes\n\n profile\n Profile to build on\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' heat.create_stack name=mystack \\\\\n template_file=salt://template.yaml \\\\\n environment=salt://environment.yaml \\\\\n parameters=\"{\"image\": \"Debian 8\", \"flavor\": \"m1.small\"}\" \\\\\n poll=5 rollback=False timeout=60 profile=openstack1\n\n .. versionadded:: 2017.7.5,2018.3.1\n\n The spelling mistake in parameter `enviroment` was corrected to `environment`.\n The misspelled version is still supported for backward compatibility, but will\n be removed in Salt Neon.\n\n '''\n if environment is None and enviroment is not None:\n salt.utils.versions.warn_until('Neon', (\n \"Please use the 'environment' parameter instead of the misspelled 'enviroment' \"\n \"parameter which will be removed in Salt Neon.\"\n ))\n environment = enviroment\n h_client = _auth(profile)\n ret = {\n 'result': True,\n 'comment': ''\n }\n if not parameters:\n parameters = {}\n if template_file:\n template_tmp_file = salt.utils.files.mkstemp()\n tsfn, source_sum, comment_ = __salt__['file.get_managed'](\n name=template_tmp_file,\n template=None,\n source=template_file,\n source_hash=None,\n user=None,\n group=None,\n mode=None,\n saltenv='base',\n context=None,\n defaults=None,\n skip_verify=False,\n kwargs=None)\n\n template_manage_result = __salt__['file.manage_file'](\n name=template_tmp_file,\n sfn=tsfn,\n ret=None,\n source=template_file,\n source_sum=source_sum,\n user=None,\n group=None,\n mode=None,\n saltenv='base',\n backup=None,\n makedirs=True,\n template=None,\n show_changes=False,\n contents=None,\n dir_mode=None)\n if template_manage_result['result']:\n with salt.utils.files.fopen(template_tmp_file, 'r') as tfp_:\n tpl = salt.utils.stringutils.to_unicode(tfp_.read())\n salt.utils.files.safe_rm(template_tmp_file)\n try:\n template = _parse_template(tpl)\n except ValueError as ex:\n ret['result'] = False\n ret['comment'] = 'Error parsing template {0}'.format(ex)\n else:\n ret['result'] = False\n ret['comment'] = 'Can not open template: {0} {1}'.format(template_file, comment_)\n else:\n ret['result'] = False\n ret['comment'] = 'Can not open template'\n if ret['result'] is False:\n return ret\n\n kwargs = {}\n kwargs['template'] = template\n try:\n h_client.stacks.validate(**kwargs)\n except Exception as ex:\n log.exception('Template not valid')\n ret['result'] = False\n ret['comment'] = 'Template not valid: {0}'.format(ex)\n return ret\n env = {}\n if environment:\n environment_tmp_file = salt.utils.files.mkstemp()\n esfn, source_sum, comment_ = __salt__['file.get_managed'](\n name=environment_tmp_file,\n template=None,\n source=environment,\n source_hash=None,\n user=None,\n group=None,\n mode=None,\n saltenv='base',\n context=None,\n defaults=None,\n skip_verify=False,\n kwargs=None)\n\n environment_manage_result = __salt__['file.manage_file'](\n name=environment_tmp_file,\n sfn=esfn,\n ret=None,\n source=environment,\n source_sum=source_sum,\n user=None,\n group=None,\n mode=None,\n saltenv='base',\n backup=None,\n makedirs=True,\n template=None,\n show_changes=False,\n contents=None,\n dir_mode=None)\n if environment_manage_result['result']:\n with salt.utils.files.fopen(environment_tmp_file, 'r') as efp_:\n env_str = salt.utils.stringutils.to_unicode(efp_.read())\n salt.utils.files.safe_rm(environment_tmp_file)\n try:\n env = _parse_environment(env_str)\n except ValueError as ex:\n ret['result'] = False\n ret['comment'] = 'Error parsing template {0}'.format(ex)\n else:\n ret['result'] = False\n ret['comment'] = 'Can not open environment: {0}, {1}'.format(environment, comment_)\n if ret['result'] is False:\n return ret\n\n fields = {\n 'stack_name': name,\n 'disable_rollback': not rollback,\n 'parameters': parameters,\n 'template': template,\n 'environment': env,\n 'timeout_mins': timeout\n }\n\n # If one or more environments is found, pass the listing to the server\n try:\n h_client.stacks.create(**fields)\n except Exception as ex: # pylint: disable=W0703\n log.exception('Create failed')\n ret['result'] = False\n ret['comment'] = six.text_type(ex)\n return ret\n if poll > 0:\n stack_status, msg = _poll_for_events(h_client, name, action='CREATE',\n poll_period=poll, timeout=timeout)\n if stack_status == 'CREATE_FAILED':\n ret['result'] = False\n ret['comment'] = 'Created stack FAILED\\'{0}\\'{1}.'.format(name, msg)\n if ret['result'] is True:\n ret['comment'] = 'Created stack \\'{0}\\'.'.format(name)\n return ret", "response": "Create a new stack in a given environment"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a specific stack", "response": "def template_stack(name=None, profile=None):\n '''\n Return template a specific stack (heat stack-template)\n\n name\n Name of the stack\n\n profile\n Profile to use\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' heat.template_stack name=mystack profile=openstack1\n '''\n h_client = _auth(profile)\n\n if not name:\n return {\n 'result': False,\n 'comment':\n 'Parameter name missing or None'\n }\n try:\n get_template = h_client.stacks.template(name)\n except heatclient.exc.HTTPNotFound:\n return {\n 'result': False,\n 'comment': 'No stack with {0}'.format(name)\n }\n except heatclient.exc.BadRequest:\n return {\n 'result': False,\n 'comment': 'Bad request fot stack {0}'.format(name)\n }\n if 'heat_template_version' in get_template:\n template = salt.utils.yaml.safe_dump(get_template)\n else:\n template = jsonutils.dumps(get_template, indent=2, ensure_ascii=False)\n\n checksum = __salt__['hashutil.digest'](template)\n ret = {\n 'template': template,\n 'result': True,\n 'checksum': checksum\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmake sure an URL is monitored by uptime. Checks if URL is already monitored and adds it.", "response": "def monitored(name, **params):\n '''\n Makes sure an URL is monitored by uptime. Checks if URL is already\n monitored, and if not, adds it.\n\n '''\n\n ret = {'name': name, 'changes': {}, 'result': None, 'comment': ''}\n if __salt__['uptime.check_exists'](name=name):\n ret['result'] = True\n ret['comment'] = 'URL {0} is already monitored'.format(name)\n ret['changes'] = {}\n return ret\n if not __opts__['test']:\n url_monitored = __salt__['uptime.create'](name, **params)\n if url_monitored:\n ret['result'] = True\n msg = 'Successfully added the URL {0} to uptime'\n ret['comment'] = msg.format(name)\n ret['changes'] = {'url_monitored': url_monitored}\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to add {0} to uptime'.format(name)\n ret['changes'] = {}\n else:\n msg = 'URL {0} is going to be added to uptime'\n ret.update(result=None,\n comment=msg.format(name))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef lock_holders(path,\n zk_hosts=None,\n identifier=None,\n max_concurrency=1,\n timeout=None,\n ephemeral_lease=False,\n profile=None,\n scheme=None,\n username=None,\n password=None,\n default_acl=None):\n '''\n Return an un-ordered list of lock holders\n\n path\n The path in zookeeper where the lock is\n\n zk_hosts\n zookeeper connect string\n\n identifier\n Name to identify this minion, if unspecified defaults to hostname\n\n max_concurrency\n Maximum number of lock holders\n\n timeout\n timeout to wait for the lock. A None timeout will block forever\n\n ephemeral_lease\n Whether the locks in zookeper should be ephemeral\n\n Example:\n\n .. code-block: bash\n\n salt minion zk_concurrency.lock_holders /lock/path host1:1234,host2:1234\n '''\n zk = _get_zk_conn(profile=profile, hosts=zk_hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n if path not in __context__['semaphore_map']:\n __context__['semaphore_map'][path] = _Semaphore(zk, path, identifier,\n max_leases=max_concurrency,\n ephemeral_lease=ephemeral_lease)\n return __context__['semaphore_map'][path].lease_holders()", "response": "Return a list of lock holders for a specific path in a specific Zookeeper node."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget lock (with optional timeout) path The path in zookeeper where the lock is zk_hosts zookeeper connect string identifier Name to identify this minion, if unspecified defaults to the hostname max_concurrency Maximum number of lock holders timeout timeout to wait for the lock. A None timeout will block forever ephemeral_lease Whether the locks in zookeper should be ephemeral force Forcibly acquire the lock regardless of available slots Example: .. code-block: bash salt minion zk_concurrency.lock /lock/path host1:1234,host2:1234", "response": "def lock(path,\n zk_hosts=None,\n identifier=None,\n max_concurrency=1,\n timeout=None,\n ephemeral_lease=False,\n force=False, # foricble get the lock regardless of open slots\n profile=None,\n scheme=None,\n username=None,\n password=None,\n default_acl=None,\n ):\n '''\n Get lock (with optional timeout)\n\n path\n The path in zookeeper where the lock is\n\n zk_hosts\n zookeeper connect string\n\n identifier\n Name to identify this minion, if unspecified defaults to the hostname\n\n max_concurrency\n Maximum number of lock holders\n\n timeout\n timeout to wait for the lock. A None timeout will block forever\n\n ephemeral_lease\n Whether the locks in zookeper should be ephemeral\n\n force\n Forcibly acquire the lock regardless of available slots\n\n Example:\n\n .. code-block: bash\n\n salt minion zk_concurrency.lock /lock/path host1:1234,host2:1234\n '''\n zk = _get_zk_conn(profile=profile, hosts=zk_hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n if path not in __context__['semaphore_map']:\n __context__['semaphore_map'][path] = _Semaphore(zk, path, identifier,\n max_leases=max_concurrency,\n ephemeral_lease=ephemeral_lease)\n\n # forcibly get the lock regardless of max_concurrency\n if force:\n __context__['semaphore_map'][path].assured_path = True\n __context__['semaphore_map'][path].max_leases = sys.maxint\n\n # block waiting for lock acquisition\n if timeout:\n logging.info('Acquiring lock %s with timeout=%s', path, timeout)\n __context__['semaphore_map'][path].acquire(timeout=timeout)\n else:\n logging.info('Acquiring lock %s with no timeout', path)\n __context__['semaphore_map'][path].acquire()\n\n return __context__['semaphore_map'][path].is_acquired"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef unlock(path,\n zk_hosts=None, # in case you need to unlock without having run lock (failed execution for example)\n identifier=None,\n max_concurrency=1,\n ephemeral_lease=False,\n scheme=None,\n profile=None,\n username=None,\n password=None,\n default_acl=None\n ):\n '''\n Remove lease from semaphore\n\n path\n The path in zookeeper where the lock is\n\n zk_hosts\n zookeeper connect string\n\n identifier\n Name to identify this minion, if unspecified defaults to hostname\n\n max_concurrency\n Maximum number of lock holders\n\n timeout\n timeout to wait for the lock. A None timeout will block forever\n\n ephemeral_lease\n Whether the locks in zookeper should be ephemeral\n\n Example:\n\n .. code-block: bash\n\n salt minion zk_concurrency.unlock /lock/path host1:1234,host2:1234\n '''\n # if someone passed in zk_hosts, and the path isn't in __context__['semaphore_map'], lets\n # see if we can find it\n zk = _get_zk_conn(profile=profile, hosts=zk_hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n if path not in __context__['semaphore_map']:\n __context__['semaphore_map'][path] = _Semaphore(zk, path, identifier,\n max_leases=max_concurrency,\n ephemeral_lease=ephemeral_lease)\n\n if path in __context__['semaphore_map']:\n __context__['semaphore_map'][path].release()\n del __context__['semaphore_map'][path]\n return True\n else:\n logging.error('Unable to find lease for path %s', path)\n return False", "response": "Unlock a lock on a specific path"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef party_members(path,\n zk_hosts=None,\n min_nodes=1,\n blocking=False,\n profile=None,\n scheme=None,\n username=None,\n password=None,\n default_acl=None,\n ):\n '''\n Get the List of identifiers in a particular party, optionally waiting for the\n specified minimum number of nodes (min_nodes) to appear\n\n path\n The path in zookeeper where the lock is\n\n zk_hosts\n zookeeper connect string\n\n min_nodes\n The minimum number of nodes expected to be present in the party\n\n blocking\n The boolean indicating if we need to block until min_nodes are available\n\n Example:\n\n .. code-block: bash\n\n salt minion zk_concurrency.party_members /lock/path host1:1234,host2:1234\n salt minion zk_concurrency.party_members /lock/path host1:1234,host2:1234 min_nodes=3 blocking=True\n '''\n zk = _get_zk_conn(profile=profile, hosts=zk_hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n party = kazoo.recipe.party.ShallowParty(zk, path)\n if blocking:\n barrier = kazoo.recipe.barrier.DoubleBarrier(zk, path, min_nodes)\n barrier.enter()\n party = kazoo.recipe.party.ShallowParty(zk, path)\n barrier.leave()\n return list(party)", "response": "Get the list of identifiers in a particular party"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _default_logfile(exe_name):\n '''\n Retrieve the logfile name\n '''\n if salt.utils.platform.is_windows():\n tmp_dir = os.path.join(__opts__['cachedir'], 'tmp')\n if not os.path.isdir(tmp_dir):\n os.mkdir(tmp_dir)\n logfile_tmp = tempfile.NamedTemporaryFile(dir=tmp_dir,\n prefix=exe_name,\n suffix='.log',\n delete=False)\n logfile = logfile_tmp.name\n logfile_tmp.close()\n else:\n logfile = salt.utils.path.join(\n '/var/log',\n '{0}.log'.format(exe_name)\n )\n\n return logfile", "response": "Retrieve the logfile name\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef client(whyrun=False,\n localmode=False,\n logfile=None,\n **kwargs):\n '''\n Execute a chef client run and return a dict with the stderr, stdout,\n return code, and pid.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' chef.client server=https://localhost\n\n server\n The chef server URL\n\n client_key\n Set the client key file location\n\n config\n The configuration file to use\n\n config-file-jail\n Directory under which config files are allowed to be loaded\n (no client.rb or knife.rb outside this path will be loaded).\n\n environment\n Set the Chef Environment on the node\n\n group\n Group to set privilege to\n\n json-attributes\n Load attributes from a JSON file or URL\n\n localmode\n Point chef-client at local repository if True\n\n log_level\n Set the log level (debug, info, warn, error, fatal)\n\n logfile\n Set the log file location\n\n node-name\n The node name for this client\n\n override-runlist\n Replace current run list with specified items for a single run\n\n pid\n Set the PID file location, defaults to /tmp/chef-client.pid\n\n run-lock-timeout\n Set maximum duration to wait for another client run to finish,\n default is indefinitely.\n\n runlist\n Permanently replace current run list with specified items\n\n user\n User to set privilege to\n\n validation_key\n Set the validation key file location, used for registering new clients\n\n whyrun\n Enable whyrun mode when set to True\n\n '''\n if logfile is None:\n logfile = _default_logfile('chef-client')\n args = ['chef-client',\n '--no-color',\n '--once',\n '--logfile \"{0}\"'.format(logfile),\n '--format doc']\n\n if whyrun:\n args.append('--why-run')\n\n if localmode:\n args.append('--local-mode')\n\n return _exec_cmd(*args, **kwargs)", "response": "Execute a chef. client run and return a dict with the stderr stdout and pid."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nexecute a chef. solo run and return a dict with stderr stdout and pid", "response": "def solo(whyrun=False,\n logfile=None,\n **kwargs):\n '''\n Execute a chef solo run and return a dict with the stderr, stdout,\n return code, and pid.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' chef.solo override-runlist=test\n\n config\n The configuration file to use\n\n environment\n Set the Chef Environment on the node\n\n group\n Group to set privilege to\n\n json-attributes\n Load attributes from a JSON file or URL\n\n log_level\n Set the log level (debug, info, warn, error, fatal)\n\n logfile\n Set the log file location\n\n node-name\n The node name for this client\n\n override-runlist\n Replace current run list with specified items for a single run\n\n recipe-url\n Pull down a remote gzipped tarball of recipes and untar it to\n the cookbook cache\n\n run-lock-timeout\n Set maximum duration to wait for another client run to finish,\n default is indefinitely.\n\n user\n User to set privilege to\n\n whyrun\n Enable whyrun mode when set to True\n '''\n if logfile is None:\n logfile = _default_logfile('chef-solo')\n args = ['chef-solo',\n '--no-color',\n '--logfile \"{0}\"'.format(logfile),\n '--format doc']\n\n if whyrun:\n args.append('--why-run')\n\n return _exec_cmd(*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef frame_msg(body, header=None, raw_body=False): # pylint: disable=unused-argument\n '''\n Frame the given message with our wire protocol\n '''\n framed_msg = {}\n if header is None:\n header = {}\n\n framed_msg['head'] = header\n framed_msg['body'] = body\n return salt.utils.msgpack.dumps(framed_msg)", "response": "Frame the given message with our wire protocol"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef frame_msg_ipc(body, header=None, raw_body=False): # pylint: disable=unused-argument\n '''\n Frame the given message with our wire protocol for IPC\n\n For IPC, we don't need to be backwards compatible, so\n use the more efficient \"use_bin_type=True\" on Python 3.\n '''\n framed_msg = {}\n if header is None:\n header = {}\n\n framed_msg['head'] = header\n framed_msg['body'] = body\n if six.PY2:\n return salt.utils.msgpack.dumps(framed_msg)\n else:\n return salt.utils.msgpack.dumps(framed_msg, use_bin_type=True)", "response": "Frame the given message with our wire protocol for IPC."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nconverting enbedded bytes to strings if possible. List helper.", "response": "def _decode_embedded_list(src):\n '''\n Convert enbedded bytes to strings if possible.\n List helper.\n '''\n output = []\n for elem in src:\n if isinstance(elem, dict):\n elem = _decode_embedded_dict(elem)\n elif isinstance(elem, list):\n elem = _decode_embedded_list(elem) # pylint: disable=redefined-variable-type\n elif isinstance(elem, bytes):\n try:\n elem = elem.decode()\n except UnicodeError:\n pass\n output.append(elem)\n return output"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nconvert enbedded bytes to strings if possible. Dict helper.", "response": "def _decode_embedded_dict(src):\n '''\n Convert enbedded bytes to strings if possible.\n Dict helper.\n '''\n output = {}\n for key, val in six.iteritems(src):\n if isinstance(val, dict):\n val = _decode_embedded_dict(val)\n elif isinstance(val, list):\n val = _decode_embedded_list(val) # pylint: disable=redefined-variable-type\n elif isinstance(val, bytes):\n try:\n val = val.decode()\n except UnicodeError:\n pass\n if isinstance(key, bytes):\n try:\n key = key.decode()\n except UnicodeError:\n pass\n output[key] = val\n return output"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef decode_embedded_strs(src):\n '''\n Convert enbedded bytes to strings if possible.\n This is necessary because Python 3 makes a distinction\n between these types.\n\n This wouldn't be needed if we used \"use_bin_type=True\" when encoding\n and \"encoding='utf-8'\" when decoding. Unfortunately, this would break\n backwards compatibility due to a change in wire protocol, so this less\n than ideal solution is used instead.\n '''\n if not six.PY3:\n return src\n\n if isinstance(src, dict):\n return _decode_embedded_dict(src)\n elif isinstance(src, list):\n return _decode_embedded_list(src)\n elif isinstance(src, bytes):\n try:\n return src.decode() # pylint: disable=redefined-variable-type\n except UnicodeError:\n return src\n else:\n return src", "response": "Convert enbedded bytes to strings if possible."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _validate_int(name, value, limits=(), strip='%'):\n '''\n Validate the named integer within the supplied limits inclusive and\n strip supplied unit characters\n '''\n comment = ''\n # Must be integral\n try:\n if isinstance(value, string_types):\n value = value.strip(' ' + strip)\n value = int(value)\n except (TypeError, ValueError):\n comment += '{0} must be an integer '.format(name)\n # Must be in range\n else:\n if len(limits) == 2:\n if value < limits[0] or value > limits[1]:\n comment += '{0} must be in the range [{1[0]}, {1[1]}] '.format(name, limits)\n return value, comment", "response": "Validate the named integer within the supplied limits inclusive and return the integer value and comment"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the current disk usage stats for the named mount point.", "response": "def status(name, maximum=None, minimum=None, absolute=False, free=False):\n '''\n Return the current disk usage stats for the named mount point\n\n name\n Disk mount or directory for which to check used space\n\n maximum\n The maximum disk utilization\n\n minimum\n The minimum disk utilization\n\n absolute\n By default, the utilization is measured in percentage. Set\n the `absolute` flag to use kilobytes.\n\n .. versionadded:: 2016.11.0\n\n free\n By default, `minimum` & `maximum` refer to the amount of used space.\n Set to `True` to evaluate the free space instead.\n '''\n # Monitoring state, no changes will be made so no test interface needed\n ret = {'name': name,\n 'result': False,\n 'comment': '',\n 'changes': {},\n 'data': {}} # Data field for monitoring state\n\n # Validate extrema\n if maximum is not None:\n if not absolute:\n maximum, comment = _validate_int('maximum', maximum, [0, 100])\n else:\n maximum, comment = _validate_int('maximum', maximum, strip='KB')\n ret['comment'] += comment\n if minimum is not None:\n if not absolute:\n minimum, comment = _validate_int('minimum', minimum, [0, 100])\n else:\n minimum, comment = _validate_int('minimum', minimum, strip='KB')\n ret['comment'] += comment\n if minimum is not None and maximum is not None:\n if minimum >= maximum:\n ret['comment'] += 'minimum must be less than maximum '\n if ret['comment']:\n return ret\n\n data = __salt__['disk.usage']()\n\n # Validate name\n if name not in data:\n ret['comment'] += ('Disk mount {0} not present. '.format(name))\n return _status_path(name, ret, minimum, maximum, absolute, free)\n else:\n return _status_mount(name, ret, minimum, maximum, absolute, free, data)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a new key - level entry for the specified host in the specified user s known_hosts file.", "response": "def present(\n name,\n user=None,\n fingerprint=None,\n key=None,\n port=None,\n enc=None,\n config=None,\n hash_known_hosts=True,\n timeout=5,\n fingerprint_hash_type=None):\n '''\n Verifies that the specified host is known by the specified user\n\n On many systems, specifically those running with openssh 4 or older, the\n ``enc`` option must be set, only openssh 5 and above can detect the key\n type.\n\n name\n The name of the remote host (e.g. \"github.com\")\n Note that only a single hostname is supported, if foo.example.com and\n bar.example.com have the same host you will need two separate Salt\n States to represent them.\n\n user\n The user who owns the ssh authorized keys file to modify\n\n fingerprint\n The fingerprint of the key which must be present in the known_hosts\n file (optional if key specified)\n\n key\n The public key which must be present in the known_hosts file\n (optional if fingerprint specified)\n\n port\n optional parameter, port which will be used to when requesting the\n public key from the remote host, defaults to port 22.\n\n enc\n Defines what type of key is being used, can be ed25519, ecdsa ssh-rsa\n or ssh-dss\n\n config\n The location of the authorized keys file relative to the user's home\n directory, defaults to \".ssh/known_hosts\". If no user is specified,\n defaults to \"/etc/ssh/ssh_known_hosts\". If present, must be an\n absolute path when a user is not specified.\n\n hash_known_hosts : True\n Hash all hostnames and addresses in the known hosts file.\n\n timeout : int\n Set the timeout for connection attempts. If ``timeout`` seconds have\n elapsed since a connection was initiated to a host or since the last\n time anything was read from that host, then the connection is closed\n and the host in question considered unavailable. Default is 5 seconds.\n\n .. versionadded:: 2016.3.0\n\n fingerprint_hash_type\n The public key fingerprint hash type that the public key fingerprint\n was originally hashed with. This defaults to ``sha256`` if not specified.\n\n .. versionadded:: 2016.11.4\n .. versionchanged:: 2017.7.0: default changed from ``md5`` to ``sha256``\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None if __opts__['test'] else True,\n 'comment': ''}\n\n if not user:\n config = config or '/etc/ssh/ssh_known_hosts'\n else:\n config = config or '.ssh/known_hosts'\n\n if not user and not os.path.isabs(config):\n comment = 'If not specifying a \"user\", specify an absolute \"config\".'\n ret['result'] = False\n return dict(ret, comment=comment)\n\n if __opts__['test']:\n if key and fingerprint:\n comment = 'Specify either \"key\" or \"fingerprint\", not both.'\n ret['result'] = False\n return dict(ret, comment=comment)\n elif key and not enc:\n comment = 'Required argument \"enc\" if using \"key\" argument.'\n ret['result'] = False\n return dict(ret, comment=comment)\n\n try:\n result = __salt__['ssh.check_known_host'](user, name,\n key=key,\n fingerprint=fingerprint,\n config=config,\n port=port,\n fingerprint_hash_type=fingerprint_hash_type)\n except CommandNotFoundError as err:\n ret['result'] = False\n ret['comment'] = 'ssh.check_known_host error: {0}'.format(err)\n return ret\n\n if result == 'exists':\n comment = 'Host {0} is already in {1}'.format(name, config)\n ret['result'] = True\n return dict(ret, comment=comment)\n elif result == 'add':\n comment = 'Key for {0} is set to be added to {1}'.format(name,\n config)\n return dict(ret, comment=comment)\n else: # 'update'\n comment = 'Key for {0} is set to be updated in {1}'.format(name,\n config)\n return dict(ret, comment=comment)\n\n result = __salt__['ssh.set_known_host'](\n user=user,\n hostname=name,\n fingerprint=fingerprint,\n key=key,\n port=port,\n enc=enc,\n config=config,\n hash_known_hosts=hash_known_hosts,\n timeout=timeout,\n fingerprint_hash_type=fingerprint_hash_type)\n if result['status'] == 'exists':\n return dict(ret,\n comment='{0} already exists in {1}'.format(name, config))\n elif result['status'] == 'error':\n return dict(ret, result=False, comment=result['error'])\n else: # 'updated'\n if key:\n new_key = result['new'][0]['key']\n return dict(ret,\n changes={'old': result['old'], 'new': result['new']},\n comment='{0}\\'s key saved to {1} (key: {2})'.format(\n name, config, new_key))\n else:\n fingerprint = result['new'][0]['fingerprint']\n return dict(ret,\n changes={'old': result['old'], 'new': result['new']},\n comment='{0}\\'s key saved to {1} (fingerprint: {2})'.format(\n name, config, fingerprint))"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nensures that the specified host is not known by the given user.", "response": "def absent(name, user=None, config=None):\n '''\n Verifies that the specified host is not known by the given user\n\n name\n The host name\n Note that only single host names are supported. If foo.example.com\n and bar.example.com are the same machine and you need to exclude both,\n you will need one Salt state for each.\n\n user\n The user who owns the ssh authorized keys file to modify\n\n config\n The location of the authorized keys file relative to the user's home\n directory, defaults to \".ssh/known_hosts\". If no user is specified,\n defaults to \"/etc/ssh/ssh_known_hosts\". If present, must be an\n absolute path when a user is not specified.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n if not user:\n config = config or '/etc/ssh/ssh_known_hosts'\n else:\n config = config or '.ssh/known_hosts'\n\n if not user and not os.path.isabs(config):\n comment = 'If not specifying a \"user\", specify an absolute \"config\".'\n ret['result'] = False\n return dict(ret, comment=comment)\n\n known_host = __salt__['ssh.get_known_host_entries'](user=user, hostname=name, config=config)\n if not known_host:\n return dict(ret, comment='Host is already absent')\n\n if __opts__['test']:\n comment = 'Key for {0} is set to be removed from {1}'.format(name,\n config)\n ret['result'] = None\n return dict(ret, comment=comment)\n\n rm_result = __salt__['ssh.rm_known_host'](user=user, hostname=name, config=config)\n if rm_result['status'] == 'error':\n return dict(ret, result=False, comment=rm_result['error'])\n else:\n return dict(ret,\n changes={'old': known_host, 'new': None},\n result=True,\n comment=rm_result['comment'])"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef start_engines(opts, proc_mgr, proxy=None):\n '''\n Fire up the configured engines!\n '''\n utils = salt.loader.utils(opts, proxy=proxy)\n if opts['__role'] == 'master':\n runners = salt.loader.runner(opts, utils=utils)\n else:\n runners = []\n funcs = salt.loader.minion_mods(opts, utils=utils, proxy=proxy)\n engines = salt.loader.engines(opts, funcs, runners, utils, proxy=proxy)\n\n engines_opt = opts.get('engines', [])\n if isinstance(engines_opt, dict):\n engines_opt = [{k: v} for k, v in engines_opt.items()]\n\n # Function references are not picklable. Windows needs to pickle when\n # spawning processes. On Windows, these will need to be recalculated\n # in the spawned child process.\n if salt.utils.platform.is_windows():\n runners = None\n utils = None\n funcs = None\n\n for engine in engines_opt:\n if isinstance(engine, dict):\n engine, engine_opts = next(iter(engine.items()))\n else:\n engine_opts = None\n engine_name = None\n if engine_opts is not None and 'engine_module' in engine_opts:\n fun = '{0}.start'.format(engine_opts['engine_module'])\n engine_name = engine\n del engine_opts['engine_module']\n else:\n fun = '{0}.start'.format(engine)\n if fun in engines:\n start_func = engines[fun]\n if engine_name:\n name = '{0}.Engine({1}-{2})'.format(__name__,\n start_func.__module__,\n engine_name)\n else:\n name = '{0}.Engine({1})'.format(__name__,\n start_func.__module__)\n log.info('Starting Engine %s', name)\n proc_mgr.add_process(\n Engine,\n args=(\n opts,\n fun,\n engine_opts,\n funcs,\n runners,\n proxy\n ),\n name=name\n )", "response": "Start the configured engines."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrunning the master service!", "response": "def run(self):\n '''\n Run the master service!\n '''\n self.utils = salt.loader.utils(self.opts, proxy=self.proxy)\n if salt.utils.platform.is_windows():\n # Calculate function references since they can't be pickled.\n if self.opts['__role'] == 'master':\n self.runners = salt.loader.runner(self.opts, utils=self.utils)\n else:\n self.runners = []\n self.funcs = salt.loader.minion_mods(self.opts, utils=self.utils, proxy=self.proxy)\n\n self.engine = salt.loader.engines(self.opts,\n self.funcs,\n self.runners,\n self.utils,\n proxy=self.proxy)\n kwargs = self.config or {}\n try:\n self.engine[self.fun](**kwargs)\n except Exception as exc:\n log.critical(\n 'Engine \\'%s\\' could not be started!',\n self.fun.split('.')[0], exc_info=True\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef targets(tgt, tgt_type='range', **kwargs):\n '''\n Return the targets from a range query\n '''\n\n r = seco.range.Range(__opts__['range_server'])\n log.debug('Range connection to \\'%s\\' established', __opts__['range_server'])\n\n hosts = []\n try:\n log.debug('Querying range for \\'%s\\'', tgt)\n hosts = r.expand(tgt)\n except seco.range.RangeException as err:\n log.error('Range server exception: %s', err)\n return {}\n log.debug('Range responded with: \\'%s\\'', hosts)\n\n # Currently we only support giving a raw range entry, no target filtering supported other than what range returns :S\n tgt_func = {\n 'range': target_range,\n 'glob': target_range,\n # 'glob': target_glob,\n }\n\n log.debug('Filtering using tgt_type: \\'%s\\'', tgt_type)\n try:\n targeted_hosts = tgt_func[tgt_type](tgt, hosts)\n except KeyError:\n raise NotImplementedError\n log.debug('Targeting data for salt-ssh: \\'%s\\'', targeted_hosts)\n\n return targeted_hosts", "response": "Return the targets from a range query"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates an authentication handler.", "response": "def _auth(url, user, passwd, realm):\n '''\n returns a authentication handler.\n '''\n\n basic = _HTTPBasicAuthHandler()\n basic.add_password(realm=realm, uri=url, user=user, passwd=passwd)\n digest = _HTTPDigestAuthHandler()\n digest.add_password(realm=realm, uri=url, user=user, passwd=passwd)\n return _build_opener(basic, digest)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmake the http request and return the data", "response": "def _do_http(opts, profile='default'):\n '''\n Make the http request and return the data\n '''\n\n ret = {}\n\n url = __salt__['config.get']('modjk:{0}:url'.format(profile), '')\n user = __salt__['config.get']('modjk:{0}:user'.format(profile), '')\n passwd = __salt__['config.get']('modjk:{0}:pass'.format(profile), '')\n realm = __salt__['config.get']('modjk:{0}:realm'.format(profile), '')\n timeout = __salt__['config.get']('modjk:{0}:timeout'.format(profile), '')\n\n if not url:\n raise Exception('missing url in profile {0}'.format(profile))\n\n if user and passwd:\n auth = _auth(url=url, realm=realm, user=user, passwd=passwd)\n _install_opener(auth)\n\n url += '?{0}'.format(_urlencode(opts))\n\n for line in _urlopen(url, timeout=timeout).read().splitlines():\n splt = line.split('=', 1)\n if splt[0] in ret:\n ret[splt[0]] += ',{0}'.format(splt[1])\n else:\n ret[splt[0]] = splt[1]\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nenabling or disable a worker", "response": "def _worker_ctl(worker, lbn, vwa, profile='default'):\n '''\n enable/disable/stop a worker\n '''\n\n cmd = {\n 'cmd': 'update',\n 'mime': 'prop',\n 'w': lbn,\n 'sw': worker,\n 'vwa': vwa,\n }\n return _do_http(cmd, profile)['worker.result.type'] == 'OK'"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a list of member workers from the configuration files", "response": "def list_configured_members(lbn, profile='default'):\n '''\n Return a list of member workers from the configuration files\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.list_configured_members loadbalancer1\n salt '*' modjk.list_configured_members loadbalancer1 other-profile\n '''\n\n config = dump_config(profile)\n\n try:\n ret = config['worker.{0}.balance_workers'.format(lbn)]\n except KeyError:\n return []\n\n return [_f for _f in ret.strip().split(',') if _f]"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef workers(profile='default'):\n '''\n Return a list of member workers and their status\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.workers\n salt '*' modjk.workers other-profile\n '''\n\n config = get_running(profile)\n lbn = config['worker.list'].split(',')\n worker_list = []\n ret = {}\n\n for lb in lbn:\n try:\n worker_list.extend(\n config['worker.{0}.balance_workers'.format(lb)].split(',')\n )\n except KeyError:\n pass\n\n worker_list = list(set(worker_list))\n\n for worker in worker_list:\n ret[worker] = {\n 'activation': config['worker.{0}.activation'.format(worker)],\n 'state': config['worker.{0}.state'.format(worker)],\n }\n\n return ret", "response": "Return a list of member workers and their status"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning all the workers in lbn to recover and activate them if they are not", "response": "def recover_all(lbn, profile='default'):\n '''\n Set the all the workers in lbn to recover and activate them if they are not\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.recover_all loadbalancer1\n salt '*' modjk.recover_all loadbalancer1 other-profile\n '''\n\n ret = {}\n config = get_running(profile)\n try:\n workers_ = config['worker.{0}.balance_workers'.format(lbn)].split(',')\n except KeyError:\n return ret\n\n for worker in workers_:\n curr_state = worker_status(worker, profile)\n if curr_state['activation'] != 'ACT':\n worker_activate(worker, lbn, profile)\n if not curr_state['state'].startswith('OK'):\n worker_recover(worker, lbn, profile)\n ret[worker] = worker_status(worker, profile)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef lb_edit(lbn, settings, profile='default'):\n '''\n Edit the loadbalancer settings\n\n Note: http://tomcat.apache.org/connectors-doc/reference/status.html\n Data Parameters for the standard Update Action\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.lb_edit loadbalancer1 \"{'vlr': 1, 'vlt': 60}\"\n salt '*' modjk.lb_edit loadbalancer1 \"{'vlr': 1, 'vlt': 60}\" other-profile\n '''\n\n settings['cmd'] = 'update'\n settings['mime'] = 'prop'\n settings['w'] = lbn\n\n return _do_http(settings, profile)['worker.result.type'] == 'OK'", "response": "Edit the loadbalancer settings"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef bulk_stop(workers, lbn, profile='default'):\n '''\n Stop all the given workers in the specific load balancer\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.bulk_stop node1,node2,node3 loadbalancer1\n salt '*' modjk.bulk_stop node1,node2,node3 loadbalancer1 other-profile\n\n salt '*' modjk.bulk_stop [\"node1\",\"node2\",\"node3\"] loadbalancer1\n salt '*' modjk.bulk_stop [\"node1\",\"node2\",\"node3\"] loadbalancer1 other-profile\n '''\n\n ret = {}\n\n if isinstance(workers, six.string_types):\n workers = workers.split(',')\n\n for worker in workers:\n try:\n ret[worker] = worker_stop(worker, lbn, profile)\n except Exception:\n ret[worker] = False\n\n return ret", "response": "Stop all the given workers in the specific load balancer"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nactivate all the given workers in the specific load balancer", "response": "def bulk_activate(workers, lbn, profile='default'):\n '''\n Activate all the given workers in the specific load balancer\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.bulk_activate node1,node2,node3 loadbalancer1\n salt '*' modjk.bulk_activate node1,node2,node3 loadbalancer1 other-profile\n\n salt '*' modjk.bulk_activate [\"node1\",\"node2\",\"node3\"] loadbalancer1\n salt '*' modjk.bulk_activate [\"node1\",\"node2\",\"node3\"] loadbalancer1 other-profile\n '''\n\n ret = {}\n\n if isinstance(workers, six.string_types):\n workers = workers.split(',')\n\n for worker in workers:\n try:\n ret[worker] = worker_activate(worker, lbn, profile)\n except Exception:\n ret[worker] = False\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndisable all the given workers in the specific load balancer", "response": "def bulk_disable(workers, lbn, profile='default'):\n '''\n Disable all the given workers in the specific load balancer\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.bulk_disable node1,node2,node3 loadbalancer1\n salt '*' modjk.bulk_disable node1,node2,node3 loadbalancer1 other-profile\n\n salt '*' modjk.bulk_disable [\"node1\",\"node2\",\"node3\"] loadbalancer1\n salt '*' modjk.bulk_disable [\"node1\",\"node2\",\"node3\"] loadbalancer1 other-profile\n '''\n\n ret = {}\n\n if isinstance(workers, six.string_types):\n workers = workers.split(',')\n\n for worker in workers:\n try:\n ret[worker] = worker_disable(worker, lbn, profile)\n except Exception:\n ret[worker] = False\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nbulking recovery of the given workers in the specific load balancer", "response": "def bulk_recover(workers, lbn, profile='default'):\n '''\n Recover all the given workers in the specific load balancer\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.bulk_recover node1,node2,node3 loadbalancer1\n salt '*' modjk.bulk_recover node1,node2,node3 loadbalancer1 other-profile\n\n salt '*' modjk.bulk_recover [\"node1\",\"node2\",\"node3\"] loadbalancer1\n salt '*' modjk.bulk_recover [\"node1\",\"node2\",\"node3\"] loadbalancer1 other-profile\n '''\n\n ret = {}\n\n if isinstance(workers, six.string_types):\n workers = workers.split(',')\n\n for worker in workers:\n try:\n ret[worker] = worker_recover(worker, lbn, profile)\n except Exception:\n ret[worker] = False\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the state of the worker", "response": "def worker_status(worker, profile='default'):\n '''\n Return the state of the worker\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.worker_status node1\n salt '*' modjk.worker_status node1 other-profile\n '''\n\n config = get_running(profile)\n try:\n return {\n 'activation': config['worker.{0}.activation'.format(worker)],\n 'state': config['worker.{0}.state'.format(worker)],\n }\n except KeyError:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef worker_recover(worker, lbn, profile='default'):\n '''\n Set the worker to recover\n this module will fail if it is in OK state\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.worker_recover node1 loadbalancer1\n salt '*' modjk.worker_recover node1 loadbalancer1 other-profile\n '''\n\n cmd = {\n 'cmd': 'recover',\n 'mime': 'prop',\n 'w': lbn,\n 'sw': worker,\n }\n return _do_http(cmd, profile)", "response": "This function will recover the worker from the loadbalancer"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef worker_edit(worker, lbn, settings, profile='default'):\n '''\n Edit the worker settings\n\n Note: http://tomcat.apache.org/connectors-doc/reference/status.html\n Data Parameters for the standard Update Action\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' modjk.worker_edit node1 loadbalancer1 \"{'vwf': 500, 'vwd': 60}\"\n salt '*' modjk.worker_edit node1 loadbalancer1 \"{'vwf': 500, 'vwd': 60}\" other-profile\n '''\n\n settings['cmd'] = 'update'\n settings['mime'] = 'prop'\n settings['w'] = lbn\n settings['sw'] = worker\n\n return _do_http(settings, profile)['worker.result.type'] == 'OK'", "response": "Edit the worker settings"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef build_info():\n '''\n Return server and build arguments\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' nginx.build_info\n '''\n ret = {'info': []}\n out = __salt__['cmd.run']('{0} -V'.format(__detect_os()))\n\n for i in out.splitlines():\n if i.startswith('configure argument'):\n ret['build arguments'] = re.findall(r\"(?:[^\\s]*'.*')|(?:[^\\s]+)\", i)[2:]\n continue\n\n ret['info'].append(i)\n\n return ret", "response": "Return server and build arguments\n CLI Example : bash\n\n salt '*' nginx. build_info\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nsignal nginx to start reload or stop.", "response": "def signal(signal=None):\n '''\n Signals nginx to start, reload, reopen or stop.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' nginx.signal reload\n '''\n valid_signals = ('start', 'reopen', 'stop', 'quit', 'reload')\n\n if signal not in valid_signals:\n return\n\n # Make sure you use the right arguments\n if signal == \"start\":\n arguments = ''\n else:\n arguments = ' -s {0}'.format(signal)\n cmd = __detect_os() + arguments\n out = __salt__['cmd.run_all'](cmd)\n\n # A non-zero return code means fail\n if out['retcode'] and out['stderr']:\n ret = out['stderr'].strip()\n # 'nginxctl configtest' returns 'Syntax OK' to stderr\n elif out['stderr']:\n ret = out['stderr'].strip()\n elif out['stdout']:\n ret = out['stdout'].strip()\n # No output for something like: nginxctl graceful\n else:\n ret = 'Command: \"{0}\" completed successfully!'.format(cmd)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef status(url=\"http://127.0.0.1/status\"):\n resp = _urlopen(url)\n status_data = resp.read()\n resp.close()\n\n lines = status_data.splitlines()\n if not len(lines) == 4:\n return\n # \"Active connections: 1 \"\n active_connections = lines[0].split()[2]\n # \"server accepts handled requests\"\n # \" 12 12 9 \"\n accepted, handled, requests = lines[2].split()\n # \"Reading: 0 Writing: 1 Waiting: 0 \"\n _, reading, _, writing, _, waiting = lines[3].split()\n return {\n 'active connections': int(active_connections),\n 'accepted': int(accepted),\n 'handled': int(handled),\n 'requests': int(requests),\n 'reading': int(reading),\n 'writing': int(writing),\n 'waiting': int(waiting),\n }", "response": "Return the data from an Nginx status page as a dictionary."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef init(opts):\n '''\n This function gets called when the proxy starts up. For\n ESXi devices, the host, login credentials, and, if configured,\n the protocol and port are cached.\n '''\n log.debug('Initting esxi proxy module in process %s', os.getpid())\n log.debug('Validating esxi proxy input')\n schema = EsxiProxySchema.serialize()\n log.trace('esxi_proxy_schema = %s', schema)\n proxy_conf = merge(opts.get('proxy', {}), __pillar__.get('proxy', {}))\n log.trace('proxy_conf = %s', proxy_conf)\n try:\n jsonschema.validate(proxy_conf, schema)\n except jsonschema.exceptions.ValidationError as exc:\n raise InvalidConfigError(exc)\n\n DETAILS['proxytype'] = proxy_conf['proxytype']\n if ('host' not in proxy_conf) and ('vcenter' not in proxy_conf):\n log.critical('Neither \\'host\\' nor \\'vcenter\\' keys found in pillar '\n 'for this proxy.')\n return False\n if 'host' in proxy_conf:\n # We have started the proxy by connecting directly to the host\n if 'username' not in proxy_conf:\n log.critical('No \\'username\\' key found in pillar for this proxy.')\n return False\n if 'passwords' not in proxy_conf:\n log.critical('No \\'passwords\\' key found in pillar for this proxy.')\n return False\n host = proxy_conf['host']\n\n # Get the correct login details\n try:\n username, password = find_credentials(host)\n except SaltSystemExit as err:\n log.critical('Error: %s', err)\n return False\n\n # Set configuration details\n DETAILS['host'] = host\n DETAILS['username'] = username\n DETAILS['password'] = password\n DETAILS['protocol'] = proxy_conf.get('protocol')\n DETAILS['port'] = proxy_conf.get('port')\n return True\n\n if 'vcenter' in proxy_conf:\n vcenter = proxy_conf['vcenter']\n if not proxy_conf.get('esxi_host'):\n log.critical('No \\'esxi_host\\' key found in pillar for this proxy.')\n DETAILS['esxi_host'] = proxy_conf['esxi_host']\n # We have started the proxy by connecting via the vCenter\n if 'mechanism' not in proxy_conf:\n log.critical('No \\'mechanism\\' key found in pillar for this proxy.')\n return False\n mechanism = proxy_conf['mechanism']\n # Save mandatory fields in cache\n for key in ('vcenter', 'mechanism'):\n DETAILS[key] = proxy_conf[key]\n\n if mechanism == 'userpass':\n if 'username' not in proxy_conf:\n log.critical('No \\'username\\' key found in pillar for this '\n 'proxy.')\n return False\n if 'passwords' not in proxy_conf and proxy_conf['passwords']:\n log.critical('Mechanism is set to \\'userpass\\' , but no '\n '\\'passwords\\' key found in pillar for this '\n 'proxy.')\n return False\n for key in ('username', 'passwords'):\n DETAILS[key] = proxy_conf[key]\n elif mechanism == 'sspi':\n if 'domain' not in proxy_conf:\n log.critical('Mechanism is set to \\'sspi\\' , but no '\n '\\'domain\\' key found in pillar for this proxy.')\n return False\n if 'principal' not in proxy_conf:\n log.critical('Mechanism is set to \\'sspi\\' , but no '\n '\\'principal\\' key found in pillar for this '\n 'proxy.')\n return False\n for key in ('domain', 'principal'):\n DETAILS[key] = proxy_conf[key]\n\n if mechanism == 'userpass':\n # Get the correct login details\n log.debug('Retrieving credentials and testing vCenter connection'\n ' for mehchanism \\'userpass\\'')\n try:\n username, password = find_credentials(DETAILS['vcenter'])\n DETAILS['password'] = password\n except SaltSystemExit as err:\n log.critical('Error: %s', err)\n return False\n\n # Save optional\n DETAILS['protocol'] = proxy_conf.get('protocol', 'https')\n DETAILS['port'] = proxy_conf.get('port', '443')\n DETAILS['credstore'] = proxy_conf.get('credstore')", "response": "Initialize the esxi proxy module."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nping the ESXi host.", "response": "def ping():\n '''\n Returns True if connection is to be done via a vCenter (no connection is attempted).\n Check to see if the host is responding when connecting directly via an ESXi\n host.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt esxi-host test.ping\n '''\n if DETAILS.get('esxi_host'):\n return True\n else:\n # TODO Check connection if mechanism is SSPI\n if DETAILS['mechanism'] == 'userpass':\n find_credentials(DETAILS['host'])\n try:\n __salt__['vsphere.system_info'](host=DETAILS['host'],\n username=DETAILS['username'],\n password=DETAILS['password'])\n except SaltSystemExit as err:\n log.warning(err)\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef ch_config(cmd, *args, **kwargs):\n '''\n This function is called by the\n :mod:`salt.modules.esxi.cmd <salt.modules.esxi.cmd>` shim.\n It then calls whatever is passed in ``cmd`` inside the\n :mod:`salt.modules.vsphere <salt.modules.vsphere>` module.\n Passes the return through from the vsphere module.\n\n cmd\n The command to call inside salt.modules.vsphere\n\n args\n Arguments that need to be passed to that command.\n\n kwargs\n Keyword arguments that need to be passed to that command.\n\n '''\n # Strip the __pub_ keys...is there a better way to do this?\n for k in kwargs:\n if k.startswith('__pub_'):\n kwargs.pop(k)\n\n kwargs['host'] = DETAILS['host']\n kwargs['username'] = DETAILS['username']\n kwargs['password'] = DETAILS['password']\n kwargs['port'] = DETAILS['port']\n kwargs['protocol'] = DETAILS['protocol']\n kwargs['credstore'] = DETAILS['credstore']\n\n if 'vsphere.' + cmd not in __salt__:\n return {'retcode': -1, 'message': 'vsphere.' + cmd + ' is not available.'}\n else:\n return __salt__['vsphere.' + cmd](*args, **kwargs)", "response": "This function is called by the vsphere module to configure the master configuration of the master."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef find_credentials(host):\n '''\n Cycle through all the possible credentials and return the first one that\n works.\n '''\n user_names = [__pillar__['proxy'].get('username', 'root')]\n passwords = __pillar__['proxy']['passwords']\n for user in user_names:\n for password in passwords:\n try:\n # Try to authenticate with the given user/password combination\n ret = __salt__['vsphere.system_info'](host=host,\n username=user,\n password=password)\n except SaltSystemExit:\n # If we can't authenticate, continue on to try the next password.\n continue\n # If we have data returned from above, we've successfully authenticated.\n if ret:\n DETAILS['username'] = user\n DETAILS['password'] = password\n return user, password\n # We've reached the end of the list without successfully authenticating.\n raise SaltSystemExit('Cannot complete login due to an incorrect user name or password.')", "response": "Find the first credentials that can be used for the given host."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _grains(host, protocol=None, port=None):\n '''\n Helper function to the grains from the proxied device.\n '''\n username, password = find_credentials(DETAILS['host'])\n ret = __salt__['vsphere.system_info'](host=host,\n username=username,\n password=password,\n protocol=protocol,\n port=port)\n GRAINS_CACHE.update(ret)\n return GRAINS_CACHE", "response": "Helper function to get grains from the proxied device."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nsend a message to a Telegram chat.", "response": "def post_message(message, chat_id=None, token=None):\n '''\n Send a message to a Telegram chat.\n\n :param message: The message to send to the Telegram chat.\n :param chat_id: (optional) The Telegram chat id.\n :param token: (optional) The Telegram API token.\n :return: Boolean if message was sent successfully.\n\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' telegram.post_message message=\"Hello Telegram!\"\n\n '''\n if not chat_id:\n chat_id = _get_chat_id()\n\n if not token:\n token = _get_token()\n\n if not message:\n log.error('message is a required option.')\n\n return _post_message(message=message, chat_id=chat_id, token=token)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _post_message(message, chat_id, token):\n '''\n Send a message to a Telegram chat.\n\n :param chat_id: The chat id.\n :param message: The message to send to the telegram chat.\n :param token: The Telegram API token.\n :return: Boolean if message was sent successfully.\n '''\n url = 'https://api.telegram.org/bot{0}/sendMessage'.format(token)\n\n parameters = dict()\n if chat_id:\n parameters['chat_id'] = chat_id\n if message:\n parameters['text'] = message\n\n try:\n response = requests.post(\n url,\n data=parameters\n )\n result = response.json()\n\n log.debug('Raw response of the telegram request is %s', response)\n\n except Exception:\n log.exception(\n 'Sending telegram api request failed'\n )\n return False\n\n # Check if the Telegram Bot API returned successfully.\n if not result.get('ok', False):\n log.debug(\n 'Sending telegram api request failed due to error %s (%s)',\n result.get('error_code'), result.get('description')\n )\n return False\n\n return True", "response": "Send a message to a Telegram chat."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the targets from the ansible inventory file Default : / etc / roster", "response": "def targets(tgt, tgt_type='glob', **kwargs):\n '''\n Return the targets from the ansible inventory_file\n Default: /etc/salt/roster\n '''\n inventory = __runner__['salt.cmd']('cmd.run', 'ansible-inventory -i {0} --list'.format(get_roster_file(__opts__)))\n __context__['inventory'] = __utils__['json.loads'](__utils__['stringutils.to_str'](inventory))\n\n if tgt_type == 'glob':\n hosts = [host for host in _get_hosts_from_group('all') if fnmatch.fnmatch(host, tgt)]\n elif tgt_type == 'nodegroup':\n hosts = _get_hosts_from_group(tgt)\n return {host: _get_hostvars(host) for host in hosts}"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_(key, value, service=None, profile=None): # pylint: disable=W0613\n '''\n Set a key/value pair in the cache service\n '''\n key, profile = _parse_key(key, profile)\n cache = salt.cache.Cache(__opts__)\n cache.store(profile['bank'], key, value)\n return get(key, service, profile)", "response": "Set a key value pair in the cache service\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets a value from the cache service", "response": "def get(key, service=None, profile=None): # pylint: disable=W0613\n '''\n Get a value from the cache service\n '''\n key, profile = _parse_key(key, profile)\n cache = salt.cache.Cache(__opts__)\n return cache.fetch(profile['bank'], key=key)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delete(key, service=None, profile=None): # pylint: disable=W0613\n '''\n Get a value from the cache service\n '''\n key, profile = _parse_key(key, profile)\n cache = salt.cache.Cache(__opts__)\n try:\n cache.flush(profile['bank'], key=key)\n return True\n except Exception:\n return False", "response": "Delete a value from the cache service\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _parse_key(key, profile):\n '''\n Parse out a key and update the opts with any override data\n '''\n comps = key.split('?')\n if len(comps) > 1:\n for item in comps[1].split('&'):\n newkey, newval = item.split('=')\n profile[newkey] = newval\n if 'cachedir' in profile:\n __opts__['cachedir'] = profile['cachedir']\n return comps[0], profile", "response": "Parse out a key and update the opts with any override data\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _redis_client(opts):\n '''\n Connect to the redis host and return a StrictRedisCluster client object.\n If connection fails then return None.\n '''\n redis_host = opts.get(\"eauth_redis_host\", \"localhost\")\n redis_port = opts.get(\"eauth_redis_port\", 6379)\n try:\n return rediscluster.StrictRedisCluster(host=redis_host, port=redis_port, decode_responses=True)\n except rediscluster.exceptions.RedisClusterException as err:\n log.warning(\n 'Failed to connect to redis at %s:%s - %s',\n redis_host, redis_port, err\n )\n return None", "response": "Connect to the redis host and return a StrictRedisCluster client object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmint a new token using the config option hash_type and store tdata with token attribute set to the token.", "response": "def mk_token(opts, tdata):\n '''\n Mint a new token using the config option hash_type and store tdata with 'token' attribute set\n to the token.\n This module uses the hash of random 512 bytes as a token.\n\n :param opts: Salt master config options\n :param tdata: Token data to be stored with 'token' attirbute of this dict set to the token.\n :returns: tdata with token if successful. Empty dict if failed.\n '''\n redis_client = _redis_client(opts)\n if not redis_client:\n return {}\n hash_type = getattr(hashlib, opts.get('hash_type', 'md5'))\n tok = six.text_type(hash_type(os.urandom(512)).hexdigest())\n try:\n while redis_client.get(tok) is not None:\n tok = six.text_type(hash_type(os.urandom(512)).hexdigest())\n except Exception as err:\n log.warning(\n 'Authentication failure: cannot get token %s from redis: %s',\n tok, err\n )\n return {}\n tdata['token'] = tok\n serial = salt.payload.Serial(opts)\n try:\n redis_client.set(tok, serial.dumps(tdata))\n except Exception as err:\n log.warning(\n 'Authentication failure: cannot save token %s to redis: %s',\n tok, err\n )\n return {}\n return tdata"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_token(opts, tok):\n '''\n Fetch the token data from the store.\n\n :param opts: Salt master config options\n :param tok: Token value to get\n :returns: Token data if successful. Empty dict if failed.\n '''\n redis_client = _redis_client(opts)\n if not redis_client:\n return {}\n serial = salt.payload.Serial(opts)\n try:\n tdata = serial.loads(redis_client.get(tok))\n return tdata\n except Exception as err:\n log.warning(\n 'Authentication failure: cannot get token %s from redis: %s',\n tok, err\n )\n return {}", "response": "Get the token data from the store."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef rm_token(opts, tok):\n '''\n Remove token from the store.\n\n :param opts: Salt master config options\n :param tok: Token to remove\n :returns: Empty dict if successful. None if failed.\n '''\n redis_client = _redis_client(opts)\n if not redis_client:\n return\n try:\n redis_client.delete(tok)\n return {}\n except Exception as err:\n log.warning('Could not remove token %s: %s', tok, err)", "response": "Remove a token from the store."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_tokens(opts):\n '''\n List all tokens in the store.\n\n :param opts: Salt master config options\n :returns: List of dicts (token_data)\n '''\n ret = []\n redis_client = _redis_client(opts)\n if not redis_client:\n return []\n serial = salt.payload.Serial(opts)\n try:\n return [k.decode('utf8') for k in redis_client.keys()]\n except Exception as err:\n log.warning('Failed to list keys: %s', err)\n return []", "response": "List all tokens in the store."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef installed(name, user, admin_user, admin_password, admin_email, title, url):\n '''\n Run the initial setup of wordpress\n\n name\n path to the wordpress installation\n\n user\n user that owns the files for the wordpress installation\n\n admin_user\n username for wordpress website administrator user\n\n admin_password\n password for wordpress website administrator user\n\n admin_email\n email for wordpress website administrator user\n\n title\n title for the wordpress website\n\n url\n url for the wordpress website\n\n .. code-block:: yaml\n\n /var/www/html:\n wordpress.installed:\n - title: Daniel's Awesome Blog\n - user: apache\n - admin_user: dwallace\n - admin_email: dwallace@example.com\n - admin_password: password123\n - url: https://blog.dwallace.com\n '''\n ret = {'name': name,\n 'changes': {},\n 'comment': '',\n 'result': False}\n\n check = __salt__['wordpress.is_installed'](name, user)\n\n if check:\n ret['result'] = True\n ret['comment'] = 'Wordpress is already installed: {0}'.format(name)\n return ret\n elif __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Wordpress will be installed: {0}'.format(name)\n return ret\n\n resp = __salt__['wordpress.install'](name, user, admin_user, admin_password, admin_email, title, url)\n if resp:\n ret['result'] = True\n ret['comment'] = 'Wordpress Installed: {0}'.format(name)\n ret['changes'] = {\n 'new': resp\n }\n else:\n ret['comment'] = 'Failed to install wordpress: {0}'.format(name)\n\n return ret", "response": "Ensure that the wordpress module is installed"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nactivating wordpress plugins ArcGIS and return the new wordpress object", "response": "def activated(name, path, user):\n '''\n Activate wordpress plugins\n\n name\n name of plugin to activate\n\n path\n path to wordpress installation\n\n user\n user who should own the files in the wordpress installation\n\n .. code-block:: yaml\n\n HyperDB:\n wordpress.activated:\n - path: /var/www/html\n - user: apache\n '''\n ret = {'name': name,\n 'changes': {},\n 'comment': '',\n 'result': False}\n\n check = __salt__['wordpress.show_plugin'](name, path, user)\n\n if check['status'] == 'active':\n ret['result'] = True\n ret['comment'] = 'Plugin already activated: {0}'.format(name)\n return ret\n elif __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Plugin will be activated: {0}'.format(name)\n return ret\n\n resp = __salt__['wordpress.activate'](name, path, user)\n if resp is True:\n ret['result'] = True\n ret['comment'] = 'Plugin activated: {0}'.format(name)\n ret['changes'] = {\n 'old': check,\n 'new': __salt__['wordpress.show_plugin'](name, path, user)\n }\n elif resp is None:\n ret['result'] = True\n ret['comment'] = 'Plugin already activated: {0}'.format(name)\n ret['changes'] = {\n 'old': check,\n 'new': __salt__['wordpress.show_plugin'](name, path, user)\n }\n else:\n ret['comment'] = 'Plugin failed to activate: {0}'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _prepare_connection(**kwargs):\n '''\n Prepare the underlying SSH connection with the remote target.\n '''\n paramiko_kwargs, scp_kwargs = _select_kwargs(**kwargs)\n ssh = paramiko.SSHClient()\n if paramiko_kwargs.pop('auto_add_policy', False):\n ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n ssh.connect(**paramiko_kwargs)\n scp_client = scp.SCPClient(ssh.get_transport(),\n **scp_kwargs)\n return scp_client", "response": "Prepare the underlying SSH connection with the remote target."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get(remote_path,\n local_path='',\n recursive=False,\n preserve_times=False,\n **kwargs):\n '''\n Transfer files and directories from remote host to the localhost of the\n Minion.\n\n remote_path\n Path to retrieve from remote host. Since this is evaluated by scp on the\n remote host, shell wildcards and environment variables may be used.\n\n recursive: ``False``\n Transfer files and directories recursively.\n\n preserve_times: ``False``\n Preserve ``mtime`` and ``atime`` of transferred files and directories.\n\n hostname\n The hostname of the remote device.\n\n port: ``22``\n The port of the remote device.\n\n username\n The username required for SSH authentication on the device.\n\n password\n Used for password authentication. It is also used for private key\n decryption if ``passphrase`` is not given.\n\n passphrase\n Used for decrypting private keys.\n\n pkey\n An optional private key to use for authentication.\n\n key_filename\n The filename, or list of filenames, of optional private key(s) and/or\n certificates to try for authentication.\n\n timeout\n An optional timeout (in seconds) for the TCP connect.\n\n socket_timeout: ``10``\n The channel socket timeout in seconds.\n\n buff_size: ``16384``\n The size of the SCP send buffer.\n\n allow_agent: ``True``\n Set to ``False`` to disable connecting to the SSH agent.\n\n look_for_keys: ``True``\n Set to ``False`` to disable searching for discoverable private key\n files in ``~/.ssh/``\n\n banner_timeout\n An optional timeout (in seconds) to wait for the SSH banner to be\n presented.\n\n auth_timeout\n An optional timeout (in seconds) to wait for an authentication\n response.\n\n auto_add_policy: ``False``\n Automatically add the host to the ``known_hosts``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' scp.get /var/tmp/file /tmp/file hostname=10.10.10.1 auto_add_policy=True\n '''\n scp_client = _prepare_connection(**kwargs)\n get_kwargs = {\n 'recursive': recursive,\n 'preserve_times': preserve_times\n }\n if local_path:\n get_kwargs['local_path'] = local_path\n return scp_client.get(remote_path, **get_kwargs)", "response": "Returns a Minion that fetches a file from the remote host."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef put(files,\n remote_path=None,\n recursive=False,\n preserve_times=False,\n saltenv='base',\n **kwargs):\n '''\n Transfer files and directories to remote host.\n\n files\n A single path or a list of paths to be transferred.\n\n remote_path\n The path on the remote device where to store the files.\n\n recursive: ``True``\n Transfer files and directories recursively.\n\n preserve_times: ``False``\n Preserve ``mtime`` and ``atime`` of transferred files and directories.\n\n hostname\n The hostname of the remote device.\n\n port: ``22``\n The port of the remote device.\n\n username\n The username required for SSH authentication on the device.\n\n password\n Used for password authentication. It is also used for private key\n decryption if ``passphrase`` is not given.\n\n passphrase\n Used for decrypting private keys.\n\n pkey\n An optional private key to use for authentication.\n\n key_filename\n The filename, or list of filenames, of optional private key(s) and/or\n certificates to try for authentication.\n\n timeout\n An optional timeout (in seconds) for the TCP connect.\n\n socket_timeout: ``10``\n The channel socket timeout in seconds.\n\n buff_size: ``16384``\n The size of the SCP send buffer.\n\n allow_agent: ``True``\n Set to ``False`` to disable connecting to the SSH agent.\n\n look_for_keys: ``True``\n Set to ``False`` to disable searching for discoverable private key\n files in ``~/.ssh/``\n\n banner_timeout\n An optional timeout (in seconds) to wait for the SSH banner to be\n presented.\n\n auth_timeout\n An optional timeout (in seconds) to wait for an authentication\n response.\n\n auto_add_policy: ``False``\n Automatically add the host to the ``known_hosts``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' scp.put /path/to/file /var/tmp/file hostname=server1 auto_add_policy=True\n '''\n scp_client = _prepare_connection(**kwargs)\n put_kwargs = {\n 'recursive': recursive,\n 'preserve_times': preserve_times\n }\n if remote_path:\n put_kwargs['remote_path'] = remote_path\n cached_files = []\n if not isinstance(files, (list, tuple)):\n files = [files]\n for file_ in files:\n cached_file = __salt__['cp.cache_file'](file_, saltenv=saltenv)\n cached_files.append(cached_file)\n return scp_client.put(cached_files, **put_kwargs)", "response": "Transfer files and directories to the remote host."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a session to be used when connecting to iControl REST.", "response": "def _build_session(username, password, trans_label=None):\n '''\n Create a session to be used when connecting to iControl REST.\n '''\n\n bigip = requests.session()\n bigip.auth = (username, password)\n bigip.verify = False\n bigip.headers.update({'Content-Type': 'application/json'})\n\n if trans_label:\n #pull the trans id from the grain\n trans_id = __salt__['grains.get']('bigip_f5_trans:{label}'.format(label=trans_label))\n\n if trans_id:\n bigip.headers.update({'X-F5-REST-Coordination-Id': trans_id})\n else:\n bigip.headers.update({'X-F5-REST-Coordination-Id': None})\n\n return bigip"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nloads the response from json data return the dictionary or raw text", "response": "def _load_response(response):\n '''\n Load the response from json data, return the dictionary or raw text\n '''\n\n try:\n data = salt.utils.json.loads(response.text)\n except ValueError:\n data = response.text\n\n ret = {'code': response.status_code, 'content': data}\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _load_connection_error(hostname, error):\n '''\n Format and Return a connection error\n '''\n\n ret = {'code': None, 'content': 'Error: Unable to connect to the bigip device: {host}\\n{error}'.format(host=hostname, error=error)}\n\n return ret", "response": "Format and Return a connection error\n "} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nloops through the parameters and build a payload containing the parameters who s values are not None.", "response": "def _loop_payload(params):\n '''\n Pass in a dictionary of parameters, loop through them and build a payload containing,\n parameters who's values are not None.\n '''\n\n #construct the payload\n payload = {}\n\n #set the payload\n for param, value in six.iteritems(params):\n if value is not None:\n payload[param] = value\n\n return payload"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _build_list(option_value, item_kind):\n '''\n pass in an option to check for a list of items, create a list of dictionary of items to set\n for this option\n '''\n #specify profiles if provided\n if option_value is not None:\n\n items = []\n\n #if user specified none, return an empty list\n if option_value == 'none':\n return items\n\n #was a list already passed in?\n if not isinstance(option_value, list):\n values = option_value.split(',')\n else:\n values = option_value\n\n for value in values:\n # sometimes the bigip just likes a plain ol list of items\n if item_kind is None:\n items.append(value)\n # other times it's picky and likes key value pairs...\n else:\n items.append({'kind': item_kind, 'name': value})\n return items\n return None", "response": "Build a list of items for a given option value"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndetermines which toggle the given payload should be used.", "response": "def _determine_toggles(payload, toggles):\n '''\n BigIP can't make up its mind if it likes yes / no or true or false.\n Figure out what it likes to hear without confusing the user.\n '''\n\n for toggle, definition in six.iteritems(toggles):\n #did the user specify anything?\n if definition['value'] is not None:\n #test for yes_no toggle\n if (definition['value'] is True or definition['value'] == 'yes') and definition['type'] == 'yes_no':\n payload[toggle] = 'yes'\n elif (definition['value'] is False or definition['value'] == 'no') and definition['type'] == 'yes_no':\n payload[toggle] = 'no'\n\n #test for true_false toggle\n if (definition['value'] is True or definition['value'] == 'yes') and definition['type'] == 'true_false':\n payload[toggle] = True\n elif (definition['value'] is False or definition['value'] == 'no') and definition['type'] == 'true_false':\n payload[toggle] = False\n\n return payload"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _set_value(value):\n '''\n A function to detect if user is trying to pass a dictionary or list. parse it and return a\n dictionary list or a string\n '''\n #don't continue if already an acceptable data-type\n if isinstance(value, bool) or isinstance(value, dict) or isinstance(value, list):\n return value\n\n #check if json\n if value.startswith('j{') and value.endswith('}j'):\n\n value = value.replace('j{', '{')\n value = value.replace('}j', '}')\n\n try:\n return salt.utils.json.loads(value)\n except Exception:\n raise salt.exceptions.CommandExecutionError\n\n #detect list of dictionaries\n if '|' in value and r'\\|' not in value:\n values = value.split('|')\n items = []\n for value in values:\n items.append(_set_value(value))\n return items\n\n #parse out dictionary if detected\n if ':' in value and r'\\:' not in value:\n options = {}\n #split out pairs\n key_pairs = value.split(',')\n for key_pair in key_pairs:\n k = key_pair.split(':')[0]\n v = key_pair.split(':')[1]\n options[k] = v\n return options\n\n #try making a list\n elif ',' in value and r'\\,' not in value:\n value_items = value.split(',')\n return value_items\n\n #just return a string\n else:\n\n #remove escape chars if added\n if r'\\|' in value:\n value = value.replace(r'\\|', '|')\n\n if r'\\:' in value:\n value = value.replace(r'\\:', ':')\n\n if r'\\,' in value:\n value = value.replace(r'\\,', ',')\n\n return value", "response": "A function to detect if user is trying to pass a dictionary or list."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef start_transaction(hostname, username, password, label):\n '''\n A function to connect to a bigip device and start a new transaction.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n label\n The name / alias for this transaction. The actual transaction\n id will be stored within a grain called ``bigip_f5_trans:<label>``\n\n CLI Example::\n\n salt '*' bigip.start_transaction bigip admin admin my_transaction\n\n '''\n\n #build the session\n bigip_session = _build_session(username, password)\n\n payload = {}\n\n #post to REST to get trans id\n try:\n response = bigip_session.post(\n BIG_IP_URL_BASE.format(host=hostname) + '/transaction',\n data=salt.utils.json.dumps(payload)\n )\n except requests.exceptions.ConnectionError as e:\n return _load_connection_error(hostname, e)\n\n #extract the trans_id\n data = _load_response(response)\n\n if data['code'] == 200:\n\n trans_id = data['content']['transId']\n\n __salt__['grains.setval']('bigip_f5_trans', {label: trans_id})\n\n return 'Transaction: {trans_id} - has successfully been stored in the grain: bigip_f5_trans:{label}'.format(trans_id=trans_id,\n label=label)\n else:\n return data", "response": "Start a new transaction on a device"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_transaction(hostname, username, password, label):\n '''\n A function to connect to a bigip device and list an existing transaction.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n label\n the label of this transaction stored within the grain:\n ``bigip_f5_trans:<label>``\n\n CLI Example::\n\n salt '*' bigip.list_transaction bigip admin admin my_transaction\n\n '''\n\n #build the session\n bigip_session = _build_session(username, password)\n\n #pull the trans id from the grain\n trans_id = __salt__['grains.get']('bigip_f5_trans:{label}'.format(label=label))\n\n if trans_id:\n\n #post to REST to get trans id\n try:\n response = bigip_session.get(BIG_IP_URL_BASE.format(host=hostname)+'/transaction/{trans_id}/commands'.format(trans_id=trans_id))\n return _load_response(response)\n except requests.exceptions.ConnectionError as e:\n return _load_connection_error(hostname, e)\n else:\n return 'Error: the label for this transaction was not defined as a grain. Begin a new transaction using the' \\\n ' bigip.start_transaction function'", "response": "A function to connect to a bigip device and list an existing transaction."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfunction to connect to a bigip device and create a node.", "response": "def create_node(hostname, username, password, name, address, trans_label=None):\n '''\n A function to connect to a bigip device and create a node.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the node\n address\n The address of the node\n trans_label\n The label of the transaction stored within the grain:\n ``bigip_f5_trans:<label>``\n\n CLI Example::\n\n salt '*' bigip.create_node bigip admin admin 10.1.1.2\n '''\n\n #build session\n bigip_session = _build_session(username, password, trans_label)\n\n #construct the payload\n payload = {}\n payload['name'] = name\n payload['address'] = address\n\n #post to REST\n try:\n response = bigip_session.post(\n BIG_IP_URL_BASE.format(host=hostname) + '/ltm/node',\n data=salt.utils.json.dumps(payload))\n except requests.exceptions.ConnectionError as e:\n return _load_connection_error(hostname, e)\n\n return _load_response(response)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef modify_node(hostname, username, password, name,\n connection_limit=None,\n description=None,\n dynamic_ratio=None,\n logging=None,\n monitor=None,\n rate_limit=None,\n ratio=None,\n session=None,\n state=None,\n trans_label=None):\n '''\n A function to connect to a bigip device and modify an existing node.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the node to modify\n connection_limit\n [integer]\n description\n [string]\n dynamic_ratio\n [integer]\n logging\n [enabled | disabled]\n monitor\n [[name] | none | default]\n rate_limit\n [integer]\n ratio\n [integer]\n session\n [user-enabled | user-disabled]\n state\n [user-down | user-up ]\n trans_label\n The label of the transaction stored within the grain:\n ``bigip_f5_trans:<label>``\n\n CLI Example::\n\n salt '*' bigip.modify_node bigip admin admin 10.1.1.2 ratio=2 logging=enabled\n '''\n\n params = {\n 'connection-limit': connection_limit,\n 'description': description,\n 'dynamic-ratio': dynamic_ratio,\n 'logging': logging,\n 'monitor': monitor,\n 'rate-limit': rate_limit,\n 'ratio': ratio,\n 'session': session,\n 'state': state,\n }\n\n #build session\n bigip_session = _build_session(username, password, trans_label)\n\n #build payload\n payload = _loop_payload(params)\n payload['name'] = name\n\n #put to REST\n try:\n response = bigip_session.put(\n BIG_IP_URL_BASE.format(host=hostname) + '/ltm/node/{name}'.format(name=name),\n data=salt.utils.json.dumps(payload)\n )\n except requests.exceptions.ConnectionError as e:\n return _load_connection_error(hostname, e)\n\n return _load_response(response)", "response": "A function to connect to a bigip device and modify an existing node."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_pool(hostname, username, password, name, members=None,\n allow_nat=None,\n allow_snat=None,\n description=None,\n gateway_failsafe_device=None,\n ignore_persisted_weight=None,\n ip_tos_to_client=None,\n ip_tos_to_server=None,\n link_qos_to_client=None,\n link_qos_to_server=None,\n load_balancing_mode=None,\n min_active_members=None,\n min_up_members=None,\n min_up_members_action=None,\n min_up_members_checking=None,\n monitor=None,\n profiles=None,\n queue_depth_limit=None,\n queue_on_connection_limit=None,\n queue_time_limit=None,\n reselect_tries=None,\n service_down_action=None,\n slow_ramp_time=None):\n '''\n A function to connect to a bigip device and create a pool.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the pool to create.\n members\n List of comma delimited pool members to add to the pool.\n i.e. 10.1.1.1:80,10.1.1.2:80,10.1.1.3:80\n allow_nat\n [yes | no]\n allow_snat\n [yes | no]\n description\n [string]\n gateway_failsafe_device\n [string]\n ignore_persisted_weight\n [enabled | disabled]\n ip_tos_to_client\n [pass-through | [integer]]\n ip_tos_to_server\n [pass-through | [integer]]\n link_qos_to_client\n [pass-through | [integer]]\n link_qos_to_server\n [pass-through | [integer]]\n load_balancing_mode\n [dynamic-ratio-member | dynamic-ratio-node |\n fastest-app-response | fastest-node |\n least-connections-members |\n least-connections-node |\n least-sessions |\n observed-member | observed-node |\n predictive-member | predictive-node |\n ratio-least-connections-member |\n ratio-least-connections-node |\n ratio-member | ratio-node | ratio-session |\n round-robin | weighted-least-connections-member |\n weighted-least-connections-node]\n min_active_members\n [integer]\n min_up_members\n [integer]\n min_up_members_action\n [failover | reboot | restart-all]\n min_up_members_checking\n [enabled | disabled]\n monitor\n [name]\n profiles\n [none | profile_name]\n queue_depth_limit\n [integer]\n queue_on_connection_limit\n [enabled | disabled]\n queue_time_limit\n [integer]\n reselect_tries\n [integer]\n service_down_action\n [drop | none | reselect | reset]\n slow_ramp_time\n [integer]\n\n CLI Example::\n\n salt '*' bigip.create_pool bigip admin admin my-pool 10.1.1.1:80,10.1.1.2:80,10.1.1.3:80 monitor=http\n '''\n\n params = {\n 'description': description,\n 'gateway-failsafe-device': gateway_failsafe_device,\n 'ignore-persisted-weight': ignore_persisted_weight,\n 'ip-tos-to-client': ip_tos_to_client,\n 'ip-tos-to-server': ip_tos_to_server,\n 'link-qos-to-client': link_qos_to_client,\n 'link-qos-to-server': link_qos_to_server,\n 'load-balancing-mode': load_balancing_mode,\n 'min-active-members': min_active_members,\n 'min-up-members': min_up_members,\n 'min-up-members-action': min_up_members_action,\n 'min-up-members-checking': min_up_members_checking,\n 'monitor': monitor,\n 'profiles': profiles,\n 'queue-on-connection-limit': queue_on_connection_limit,\n 'queue-depth-limit': queue_depth_limit,\n 'queue-time-limit': queue_time_limit,\n 'reselect-tries': reselect_tries,\n 'service-down-action': service_down_action,\n 'slow-ramp-time': slow_ramp_time\n }\n\n # some options take yes no others take true false. Figure out when to use which without\n # confusing the end user\n toggles = {\n 'allow-nat': {'type': 'yes_no', 'value': allow_nat},\n 'allow-snat': {'type': 'yes_no', 'value': allow_snat}\n }\n\n #build payload\n payload = _loop_payload(params)\n payload['name'] = name\n\n #determine toggles\n payload = _determine_toggles(payload, toggles)\n\n #specify members if provided\n if members is not None:\n payload['members'] = _build_list(members, 'ltm:pool:members')\n\n #build session\n bigip_session = _build_session(username, password)\n\n #post to REST\n try:\n response = bigip_session.post(\n BIG_IP_URL_BASE.format(host=hostname) + '/ltm/pool',\n data=salt.utils.json.dumps(payload)\n )\n except requests.exceptions.ConnectionError as e:\n return _load_connection_error(hostname, e)\n\n return _load_response(response)", "response": "This function creates a new pool in the iControl REST interface."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef replace_pool_members(hostname, username, password, name, members):\n '''\n A function to connect to a bigip device and replace members of an existing pool with new members.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the pool to modify\n members\n List of comma delimited pool members to replace existing members with.\n i.e. 10.1.1.1:80,10.1.1.2:80,10.1.1.3:80\n\n CLI Example::\n\n salt '*' bigip.replace_pool_members bigip admin admin my-pool 10.2.2.1:80,10.2.2.2:80,10.2.2.3:80\n '''\n\n payload = {}\n payload['name'] = name\n #specify members if provided\n if members is not None:\n\n if isinstance(members, six.string_types):\n members = members.split(',')\n\n pool_members = []\n for member in members:\n\n #check to see if already a dictionary ( for states)\n if isinstance(member, dict):\n\n #check for state alternative name 'member_state', replace with state\n if 'member_state' in member.keys():\n member['state'] = member.pop('member_state')\n\n #replace underscore with dash\n for key in member:\n new_key = key.replace('_', '-')\n member[new_key] = member.pop(key)\n\n pool_members.append(member)\n\n #parse string passed via execution command (for executions)\n else:\n pool_members.append({'name': member, 'address': member.split(':')[0]})\n\n payload['members'] = pool_members\n\n #build session\n bigip_session = _build_session(username, password)\n\n #put to REST\n try:\n response = bigip_session.put(\n BIG_IP_URL_BASE.format(host=hostname) + '/ltm/pool/{name}'.format(name=name),\n data=salt.utils.json.dumps(payload)\n )\n except requests.exceptions.ConnectionError as e:\n return _load_connection_error(hostname, e)\n\n return _load_response(response)", "response": "This function is used to connect to a bigip device and replace members of an existing pool with new members."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_virtual(hostname, username, password, name, destination,\n pool=None,\n address_status=None,\n auto_lasthop=None,\n bwc_policy=None,\n cmp_enabled=None,\n connection_limit=None,\n dhcp_relay=None,\n description=None,\n fallback_persistence=None,\n flow_eviction_policy=None,\n gtm_score=None,\n ip_forward=None,\n ip_protocol=None,\n internal=None,\n twelve_forward=None,\n last_hop_pool=None,\n mask=None,\n mirror=None,\n nat64=None,\n persist=None,\n profiles=None,\n policies=None,\n rate_class=None,\n rate_limit=None,\n rate_limit_mode=None,\n rate_limit_dst=None,\n rate_limit_src=None,\n rules=None,\n related_rules=None,\n reject=None,\n source=None,\n source_address_translation=None,\n source_port=None,\n state=None,\n traffic_classes=None,\n translate_address=None,\n translate_port=None,\n vlans=None):\n r'''\n A function to connect to a bigip device and create a virtual server.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n name\n The name of the virtual to create\n destination\n [ [virtual_address_name:port] | [ipv4:port] | [ipv6.port] ]\n pool\n [ [pool_name] | none]\n address_status\n [yes | no]\n auto_lasthop\n [default | enabled | disabled ]\n bwc_policy\n [none] | string]\n cmp_enabled\n [yes | no]\n dhcp_relay\n [yes | no]\n connection_limit\n [integer]\n description\n [string]\n state\n [disabled | enabled]\n fallback_persistence\n [none | [profile name] ]\n flow_eviction_policy\n [none | [eviction policy name] ]\n gtm_score\n [integer]\n ip_forward\n [yes | no]\n ip_protocol\n [any | protocol]\n internal\n [yes | no]\n twelve_forward\n (12-forward)\n [yes | no]\n last_hop-pool\n [ [pool_name] | none]\n mask\n { [ipv4] | [ipv6] }\n mirror\n { [disabled | enabled | none] }\n nat64\n [enabled | disabled]\n persist\n [none | profile1,profile2,profile3 ... ]\n profiles\n [none | default | profile1,profile2,profile3 ... ]\n policies\n [none | default | policy1,policy2,policy3 ... ]\n rate_class\n [name]\n rate_limit\n [integer]\n rate_limit_mode\n [destination | object | object-destination |\n object-source | object-source-destination |\n source | source-destination]\n rate_limit_dst\n [integer]\n rate_limit\u00e7src\n [integer]\n rules\n [none | [rule_one,rule_two ...] ]\n related_rules\n [none | [rule_one,rule_two ...] ]\n reject\n [yes | no]\n source\n { [ipv4[/prefixlen]] | [ipv6[/prefixlen]] }\n source_address_translation\n [none | snat:pool_name | lsn | automap ]\n source_port\n [change | preserve | preserve-strict]\n state\n [enabled | disabled]\n traffic_classes\n [none | default | class_one,class_two ... ]\n translate_address\n [enabled | disabled]\n translate_port\n [enabled | disabled]\n vlans\n [none | default | [enabled|disabled]:vlan1,vlan2,vlan3 ... ]\n\n CLI Examples::\n\n salt '*' bigip.create_virtual bigip admin admin my-virtual-3 26.2.2.5:80 \\\n pool=my-http-pool-http profiles=http,tcp\n\n salt '*' bigip.create_virtual bigip admin admin my-virtual-3 43.2.2.5:80 \\\n pool=test-http-pool-http profiles=http,websecurity persist=cookie,hash \\\n policies=asm_auto_l7_policy__http-virtual \\\n rules=_sys_APM_ExchangeSupport_helper,_sys_https_redirect \\\n related_rules=_sys_APM_activesync,_sys_APM_ExchangeSupport_helper \\\n source_address_translation=snat:my-snat-pool \\\n translate_address=enabled translate_port=enabled \\\n traffic_classes=my-class,other-class \\\n vlans=enabled:external,internal\n\n '''\n\n params = {\n 'pool': pool,\n 'auto-lasthop': auto_lasthop,\n 'bwc-policy': bwc_policy,\n 'connection-limit': connection_limit,\n 'description': description,\n 'fallback-persistence': fallback_persistence,\n 'flow-eviction-policy': flow_eviction_policy,\n 'gtm-score': gtm_score,\n 'ip-protocol': ip_protocol,\n 'last-hop-pool': last_hop_pool,\n 'mask': mask,\n 'mirror': mirror,\n 'nat64': nat64,\n 'persist': persist,\n 'rate-class': rate_class,\n 'rate-limit': rate_limit,\n 'rate-limit-mode': rate_limit_mode,\n 'rate-limit-dst': rate_limit_dst,\n 'rate-limit-src': rate_limit_src,\n 'source': source,\n 'source-port': source_port,\n 'translate-address': translate_address,\n 'translate-port': translate_port\n }\n\n # some options take yes no others take true false. Figure out when to use which without\n # confusing the end user\n toggles = {\n 'address-status': {'type': 'yes_no', 'value': address_status},\n 'cmp-enabled': {'type': 'yes_no', 'value': cmp_enabled},\n 'dhcp-relay': {'type': 'true_false', 'value': dhcp_relay},\n 'reject': {'type': 'true_false', 'value': reject},\n '12-forward': {'type': 'true_false', 'value': twelve_forward},\n 'internal': {'type': 'true_false', 'value': internal},\n 'ip-forward': {'type': 'true_false', 'value': ip_forward}\n }\n\n #build session\n bigip_session = _build_session(username, password)\n\n #build payload\n payload = _loop_payload(params)\n\n payload['name'] = name\n payload['destination'] = destination\n\n #determine toggles\n payload = _determine_toggles(payload, toggles)\n\n #specify profiles if provided\n if profiles is not None:\n payload['profiles'] = _build_list(profiles, 'ltm:virtual:profile')\n\n #specify persist if provided\n if persist is not None:\n payload['persist'] = _build_list(persist, 'ltm:virtual:persist')\n\n #specify policies if provided\n if policies is not None:\n payload['policies'] = _build_list(policies, 'ltm:virtual:policy')\n\n #specify rules if provided\n if rules is not None:\n payload['rules'] = _build_list(rules, None)\n\n #specify related-rules if provided\n if related_rules is not None:\n payload['related-rules'] = _build_list(related_rules, None)\n\n #handle source-address-translation\n if source_address_translation is not None:\n\n #check to see if this is already a dictionary first\n if isinstance(source_address_translation, dict):\n payload['source-address-translation'] = source_address_translation\n elif source_address_translation == 'none':\n payload['source-address-translation'] = {'pool': 'none', 'type': 'none'}\n elif source_address_translation == 'automap':\n payload['source-address-translation'] = {'pool': 'none', 'type': 'automap'}\n elif source_address_translation == 'lsn':\n payload['source-address-translation'] = {'pool': 'none', 'type': 'lsn'}\n elif source_address_translation.startswith('snat'):\n snat_pool = source_address_translation.split(':')[1]\n payload['source-address-translation'] = {'pool': snat_pool, 'type': 'snat'}\n\n #specify related-rules if provided\n if traffic_classes is not None:\n payload['traffic-classes'] = _build_list(traffic_classes, None)\n\n #handle vlans\n if vlans is not None:\n #ceck to see if vlans is a dictionary (used when state makes use of function)\n if isinstance(vlans, dict):\n try:\n payload['vlans'] = vlans['vlan_ids']\n if vlans['enabled']:\n payload['vlans-enabled'] = True\n elif vlans['disabled']:\n payload['vlans-disabled'] = True\n except Exception:\n return 'Error: Unable to Parse vlans dictionary: \\n\\tvlans={vlans}'.format(vlans=vlans)\n elif vlans == 'none':\n payload['vlans'] = 'none'\n elif vlans == 'default':\n payload['vlans'] = 'default'\n elif isinstance(vlans, six.string_types) and (vlans.startswith('enabled') or vlans.startswith('disabled')):\n try:\n vlans_setting = vlans.split(':')[0]\n payload['vlans'] = vlans.split(':')[1].split(',')\n if vlans_setting == 'disabled':\n payload['vlans-disabled'] = True\n elif vlans_setting == 'enabled':\n payload['vlans-enabled'] = True\n except Exception:\n return 'Error: Unable to Parse vlans option: \\n\\tvlans={vlans}'.format(vlans=vlans)\n else:\n return 'Error: vlans must be a dictionary or string.'\n\n #determine state\n if state is not None:\n if state == 'enabled':\n payload['enabled'] = True\n elif state == 'disabled':\n payload['disabled'] = True\n\n #post to REST\n try:\n response = bigip_session.post(\n BIG_IP_URL_BASE.format(host=hostname) + '/ltm/virtual',\n data=salt.utils.json.dumps(payload)\n )\n except requests.exceptions.ConnectionError as e:\n return _load_connection_error(hostname, e)\n\n return _load_response(response)", "response": "r Creates a virtual server in the iControl REST server."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_profile(hostname, username, password, profile_type, name, **kwargs):\n r'''\n A function to connect to a bigip device and create a profile.\n\n hostname\n The host/address of the bigip device\n username\n The iControl REST username\n password\n The iControl REST password\n profile_type\n The type of profile to create\n name\n The name of the profile to create\n kwargs\n ``[ arg=val ] ... [arg=key1:val1,key2:val2] ...``\n\n Consult F5 BIGIP user guide for specific options for each monitor type.\n Typically, tmsh arg names are used.\n\n Creating Complex Args\n Profiles can get pretty complicated in terms of the amount of possible\n config options. Use the following shorthand to create complex arguments such\n as lists, dictionaries, and lists of dictionaries. An option is also\n provided to pass raw json as well.\n\n lists ``[i,i,i]``:\n ``param='item1,item2,item3'``\n\n Dictionary ``[k:v,k:v,k,v]``:\n ``param='key-1:val-1,key-2:val2,key-3:va-3'``\n\n List of Dictionaries ``[k:v,k:v|k:v,k:v|k:v,k:v]``:\n ``param='key-1:val-1,key-2:val-2|key-1:val-1,key-2:val-2|key-1:val-1,key-2:val-2'``\n\n JSON: ``'j{ ... }j'``:\n ``cert-key-chain='j{ \"default\": { \"cert\": \"default.crt\", \"chain\": \"default.crt\", \"key\": \"default.key\" } }j'``\n\n Escaping Delimiters:\n Use ``\\,`` or ``\\:`` or ``\\|`` to escape characters which shouldn't\n be treated as delimiters i.e. ``ciphers='DEFAULT\\:!SSLv3'``\n\n CLI Examples::\n\n salt '*' bigip.create_profile bigip admin admin http my-http-profile defaultsFrom='/Common/http'\n salt '*' bigip.create_profile bigip admin admin http my-http-profile defaultsFrom='/Common/http' \\\n enforcement=maxHeaderCount:3200,maxRequests:10\n\n '''\n\n #build session\n bigip_session = _build_session(username, password)\n\n #construct the payload\n payload = {}\n payload['name'] = name\n\n #there's a ton of different profiles and a ton of options for each type of profile.\n #this logic relies that the end user knows which options are meant for which profile types\n for key, value in six.iteritems(kwargs):\n if not key.startswith('__'):\n if key not in ['hostname', 'username', 'password', 'profile_type']:\n key = key.replace('_', '-')\n\n try:\n payload[key] = _set_value(value)\n except salt.exceptions.CommandExecutionError:\n return 'Error: Unable to Parse JSON data for parameter: {key}\\n{value}'.format(key=key, value=value)\n\n #post to REST\n try:\n response = bigip_session.post(\n BIG_IP_URL_BASE.format(host=hostname) + '/ltm/profile/{type}'.format(type=profile_type),\n data=salt.utils.json.dumps(payload)\n )\n except requests.exceptions.ConnectionError as e:\n return _load_connection_error(hostname, e)\n\n return _load_response(response)", "response": "r Creates a profile on a bigip device."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nlist the currently configured Beacons on the minion.", "response": "def list_(return_yaml=True,\n include_pillar=True,\n include_opts=True,\n **kwargs):\n '''\n List the beacons currently configured on the minion.\n\n Args:\n\n return_yaml (bool):\n Whether to return YAML formatted output, default ``True``.\n\n include_pillar (bool):\n Whether to include beacons that are configured in pillar, default\n is ``True``.\n\n include_opts (bool):\n Whether to include beacons that are configured in opts, default is\n ``True``.\n\n Returns:\n list: List of currently configured Beacons.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' beacons.list\n\n '''\n beacons = None\n\n try:\n eventer = salt.utils.event.get_event('minion', opts=__opts__)\n res = __salt__['event.fire']({'func': 'list',\n 'include_pillar': include_pillar,\n 'include_opts': include_opts},\n 'manage_beacons')\n if res:\n event_ret = eventer.get_event(\n tag='/salt/minion/minion_beacons_list_complete',\n wait=kwargs.get('timeout', 30))\n log.debug('event_ret %s', event_ret)\n if event_ret and event_ret['complete']:\n beacons = event_ret['beacons']\n except KeyError:\n # Effectively a no-op, since we can't really return without an event\n # system\n ret = {'comment': 'Event module not available. Beacon list failed.',\n 'result': False}\n return ret\n\n if beacons:\n if return_yaml:\n tmp = {'beacons': beacons}\n return salt.utils.yaml.safe_dump(tmp, default_flow_style=False)\n else:\n return beacons\n else:\n return {'beacons': {}}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nadding a beacon to the minion", "response": "def add(name, beacon_data, **kwargs):\n '''\n Add a beacon on the minion\n\n Args:\n\n name (str):\n Name of the beacon to configure\n\n beacon_data (dict):\n Dictionary or list containing configuration for beacon.\n\n Returns:\n dict: Boolean and status message on success or failure of add.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' beacons.add ps \"[{'processes': {'salt-master': 'stopped', 'apache2': 'stopped'}}]\"\n\n '''\n ret = {'comment': 'Failed to add beacon {0}.'.format(name),\n 'result': False}\n\n if name in list_(return_yaml=False, **kwargs):\n ret['comment'] = 'Beacon {0} is already configured.'.format(name)\n return ret\n\n # Check to see if a beacon_module is specified, if so, verify it is\n # valid and available beacon type.\n if any('beacon_module' in key for key in beacon_data):\n res = next(value for value in beacon_data if 'beacon_module' in value)\n beacon_name = res['beacon_module']\n else:\n beacon_name = name\n\n if beacon_name not in list_available(return_yaml=False, **kwargs):\n ret['comment'] = 'Beacon \"{0}\" is not available.'.format(beacon_name)\n return ret\n\n if 'test' in kwargs and kwargs['test']:\n ret['result'] = True\n ret['comment'] = 'Beacon: {0} would be added.'.format(name)\n else:\n try:\n # Attempt to load the beacon module so we have access to the\n # validate function\n eventer = salt.utils.event.get_event('minion', opts=__opts__)\n res = __salt__['event.fire']({'name': name,\n 'beacon_data': beacon_data,\n 'func': 'validate_beacon'},\n 'manage_beacons')\n if res:\n event_ret = eventer.get_event(\n tag='/salt/minion/minion_beacon_validation_complete',\n wait=kwargs.get('timeout', 30))\n valid = event_ret['valid']\n vcomment = event_ret['vcomment']\n\n if not valid:\n ret['result'] = False\n ret['comment'] = ('Beacon {0} configuration invalid, '\n 'not adding.\\n{1}'.format(name, vcomment))\n return ret\n\n except KeyError:\n # Effectively a no-op, since we can't really return without an event\n # system\n ret['result'] = False\n ret['comment'] = 'Event module not available. Beacon add failed.'\n return ret\n\n try:\n res = __salt__['event.fire']({'name': name,\n 'beacon_data': beacon_data,\n 'func': 'add'}, 'manage_beacons')\n if res:\n event_ret = eventer.get_event(\n tag='/salt/minion/minion_beacon_add_complete',\n wait=kwargs.get('timeout', 30))\n if event_ret and event_ret['complete']:\n beacons = event_ret['beacons']\n if name in beacons and beacons[name] == beacon_data:\n ret['result'] = True\n ret['comment'] = 'Added beacon: {0}.'.format(name)\n elif event_ret:\n ret['result'] = False\n ret['comment'] = event_ret['comment']\n else:\n ret['result'] = False\n ret['comment'] = 'Did not receive the manage event ' \\\n 'before the timeout of {0}s' \\\n ''.format(kwargs.get('timeout', 30))\n return ret\n except KeyError:\n # Effectively a no-op, since we can't really return without an event\n # system\n ret['result'] = False\n ret['comment'] = 'Event module not available. Beacon add failed.'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmodify an existing beacon.", "response": "def modify(name, beacon_data, **kwargs):\n '''\n Modify an existing beacon.\n\n Args:\n\n name (str):\n Name of the beacon to configure.\n\n beacon_data (dict):\n Dictionary or list containing updated configuration for beacon.\n\n Returns:\n dict: Boolean and status message on success or failure of modify.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' beacons.modify ps \"[{'salt-master': 'stopped'}, {'apache2': 'stopped'}]\"\n '''\n\n ret = {'comment': '',\n 'result': True}\n\n current_beacons = list_(return_yaml=False, **kwargs)\n if name not in current_beacons:\n ret['comment'] = 'Beacon {0} is not configured.'.format(name)\n return ret\n\n if 'test' in kwargs and kwargs['test']:\n ret['result'] = True\n ret['comment'] = 'Beacon: {0} would be modified.'.format(name)\n else:\n try:\n # Attempt to load the beacon module so we have access to the\n # validate function\n eventer = salt.utils.event.get_event('minion', opts=__opts__)\n res = __salt__['event.fire']({'name': name,\n 'beacon_data': beacon_data,\n 'func': 'validate_beacon'},\n 'manage_beacons')\n if res:\n event_ret = eventer.get_event(\n tag='/salt/minion/minion_beacon_validation_complete',\n wait=kwargs.get('timeout', 30))\n valid = event_ret['valid']\n vcomment = event_ret['vcomment']\n\n if not valid:\n ret['result'] = False\n ret['comment'] = ('Beacon {0} configuration invalid, '\n 'not modifying.\\n{1}'.format(name, vcomment))\n return ret\n\n except KeyError:\n # Effectively a no-op, since we can't really return without an event\n # system\n ret['result'] = False\n ret['comment'] = 'Event module not available. Beacon modify failed.'\n return ret\n\n if not valid:\n ret['result'] = False\n ret['comment'] = ('Beacon {0} configuration invalid, '\n 'not modifying.\\n{1}'.format(name, vcomment))\n return ret\n\n _current = current_beacons[name]\n _new = beacon_data\n\n if _new == _current:\n ret['comment'] = 'Job {0} in correct state'.format(name)\n return ret\n\n _current_lines = []\n for _item in _current:\n _current_lines.extend(['{0}:{1}\\n'.format(key, value)\n for (key, value) in six.iteritems(_item)])\n _new_lines = []\n for _item in _new:\n _new_lines.extend(['{0}:{1}\\n'.format(key, value)\n for (key, value) in six.iteritems(_item)])\n _diff = difflib.unified_diff(_current_lines, _new_lines)\n\n ret['changes'] = {}\n ret['changes']['diff'] = ''.join(_diff)\n\n try:\n eventer = salt.utils.event.get_event('minion', opts=__opts__)\n res = __salt__['event.fire']({'name': name,\n 'beacon_data': beacon_data,\n 'func': 'modify'},\n 'manage_beacons')\n if res:\n event_ret = eventer.get_event(\n tag='/salt/minion/minion_beacon_modify_complete',\n wait=kwargs.get('timeout', 30))\n if event_ret and event_ret['complete']:\n beacons = event_ret['beacons']\n if name in beacons and beacons[name] == beacon_data:\n ret['result'] = True\n ret['comment'] = 'Modified beacon: {0}.'.format(name)\n elif event_ret:\n ret['result'] = False\n ret['comment'] = event_ret['comment']\n else:\n ret['result'] = False\n ret['comment'] = 'Did not receive the manage event ' \\\n 'before the timeout of {0}s' \\\n ''.format(kwargs.get('timeout', 30))\n return ret\n except KeyError:\n # Effectively a no-op, since we can't really return without an event\n # system\n ret['result'] = False\n ret['comment'] = 'Event module not available. Beacon modify failed.'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsaving all configured beacons to the minion config.", "response": "def save(**kwargs):\n '''\n Save all configured beacons to the minion config.\n\n Returns:\n dict: Boolean and status message on success or failure of save.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' beacons.save\n '''\n\n ret = {'comment': [],\n 'result': True}\n\n beacons = list_(return_yaml=False, include_pillar=False, **kwargs)\n\n # move this file into an configurable opt\n sfn = os.path.join(os.path.dirname(__opts__['conf_file']),\n os.path.dirname(__opts__['default_include']),\n 'beacons.conf')\n if beacons:\n tmp = {'beacons': beacons}\n yaml_out = salt.utils.yaml.safe_dump(tmp, default_flow_style=False)\n else:\n yaml_out = ''\n\n try:\n with salt.utils.files.fopen(sfn, 'w+') as fp_:\n fp_.write(yaml_out)\n ret['comment'] = 'Beacons saved to {0}.'.format(sfn)\n except (IOError, OSError):\n ret['comment'] = 'Unable to write to beacons file at {0}. Check ' \\\n 'permissions.'.format(sfn)\n ret['result'] = False\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef enable_beacon(name, **kwargs):\n '''\n Enable a beacon on the minion.\n\n Args:\n name (str): Name of the beacon to enable.\n\n Returns:\n dict: Boolean and status message on success or failure of enable.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' beacons.enable_beacon ps\n '''\n\n ret = {'comment': [],\n 'result': True}\n\n if not name:\n ret['comment'] = 'Beacon name is required.'\n ret['result'] = False\n return ret\n\n if 'test' in kwargs and kwargs['test']:\n ret['comment'] = 'Beacon {0} would be enabled.'.format(name)\n else:\n _beacons = list_(return_yaml=False, **kwargs)\n if name not in _beacons:\n ret['comment'] = 'Beacon {0} is not currently configured.' \\\n ''.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']({'func': 'enable_beacon',\n 'name': name},\n 'manage_beacons')\n if res:\n event_ret = eventer.get_event(\n tag='/salt/minion/minion_beacon_enabled_complete',\n wait=kwargs.get('timeout', 30))\n if event_ret and event_ret['complete']:\n beacons = event_ret['beacons']\n beacon_config_dict = _get_beacon_config_dict(beacons[name])\n\n if 'enabled' in beacon_config_dict and beacon_config_dict['enabled']:\n ret['result'] = True\n ret['comment'] = 'Enabled beacon {0} on minion.' \\\n ''.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to enable beacon {0} on ' \\\n 'minion.'.format(name)\n elif event_ret:\n ret['result'] = False\n ret['comment'] = event_ret['comment']\n else:\n ret['result'] = False\n ret['comment'] = 'Did not receive the manage event ' \\\n 'before the timeout of {0}s' \\\n ''.format(kwargs.get('timeout', 30))\n return ret\n except KeyError:\n # Effectively a no-op, since we can't really return without an event\n # system\n ret['result'] = False\n ret['comment'] = 'Event module not available. Beacon enable job ' \\\n 'failed.'\n return ret", "response": "Enable a beacon on the minion."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset a key value pair in the REST interface", "response": "def set_(key, value, service=None, profile=None): # pylint: disable=W0613\n '''\n Set a key/value pair in the REST interface\n '''\n return query(key, value, service, profile)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef query(key, value=None, service=None, profile=None): # pylint: disable=W0613\n '''\n Get a value from the REST interface\n '''\n comps = key.split('?')\n key = comps[0]\n key_vars = {}\n for pair in comps[1].split('&'):\n pair_key, pair_val = pair.split('=')\n key_vars[pair_key] = pair_val\n\n renderer = __opts__.get('renderer', 'jinja|yaml')\n rend = salt.loader.render(__opts__, {})\n blacklist = __opts__.get('renderer_blacklist')\n whitelist = __opts__.get('renderer_whitelist')\n url = compile_template(\n ':string:',\n rend,\n renderer,\n blacklist,\n whitelist,\n input_data=profile[key]['url'],\n **key_vars\n )\n\n extras = {}\n for item in profile[key]:\n if item not in ('backend', 'url'):\n extras[item] = profile[key][item]\n\n result = http.query(\n url,\n decode=True,\n **extras\n )\n\n return result['dict']", "response": "Get a value from the REST interface\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef send(name,\n data=None,\n preload=None,\n with_env=False,\n with_grains=False,\n with_pillar=False,\n show_changed=True,\n **kwargs):\n '''\n Send an event to the Salt Master\n\n .. versionadded:: 2014.7.0\n\n Accepts the same arguments as the :py:func:`event.send\n <salt.modules.event.send>` execution module of the same name,\n with the additional argument:\n\n :param show_changed: If ``True``, state will show as changed with the data\n argument as the change value. If ``False``, shows as unchanged.\n\n Example:\n\n .. code-block:: yaml\n\n # ...snip bunch of states above\n\n mycompany/mystaterun/status/update:\n event.send:\n - data:\n status: \"Half-way through the state run!\"\n\n # ...snip bunch of states below\n '''\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n if show_changed:\n ret['changes'] = {'tag': name, 'data': data}\n else:\n ret['changes'] = {}\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Event would have been fired'\n return ret\n\n ret['result'] = __salt__['event.send'](name,\n data=data,\n preload=preload,\n with_env=with_env,\n with_grains=with_grains,\n with_pillar=with_pillar,\n **kwargs)\n ret['comment'] = 'Event fired'\n\n return ret", "response": "Send an event to the specified module"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncompares the common keys between security group rules against eachother security group rules.", "response": "def _rule_compare(rule1, rule2):\n '''\n Compare the common keys between security group rules against eachother\n '''\n\n commonkeys = set(rule1.keys()).intersection(rule2.keys())\n for key in commonkeys:\n if rule1[key] != rule2[key]:\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef present(name, auth=None, **kwargs):\n '''\n Ensure a security group rule exists\n\n defaults: port_range_min=None, port_range_max=None, protocol=None,\n remote_ip_prefix=None, remote_group_id=None, direction='ingress',\n ethertype='IPv4', project_id=None\n\n name\n Name of the security group to associate with this rule\n\n project_name\n Name of the project associated with the security group\n\n protocol\n The protocol that is matched by the security group rule.\n Valid values are None, tcp, udp, and icmp.\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n kwargs = __utils__['args.clean_kwargs'](**kwargs)\n\n __salt__['neutronng.setup_clouds'](auth)\n\n if 'project_name' in kwargs:\n kwargs['project_id'] = kwargs['project_name']\n del kwargs['project_name']\n\n project = __salt__['keystoneng.project_get'](\n name=kwargs['project_id'])\n\n if project is None:\n ret['result'] = False\n ret['comment'] = \"Project does not exist\"\n return ret\n\n secgroup = __salt__['neutronng.security_group_get'](\n name=name,\n filters={'tenant_id': project.id}\n )\n\n if secgroup is None:\n ret['result'] = False\n ret['changes'] = {},\n ret['comment'] = 'Security Group does not exist {}'.format(name)\n return ret\n\n # we have to search through all secgroup rules for a possible match\n rule_exists = None\n for rule in secgroup['security_group_rules']:\n if _rule_compare(rule, kwargs) is True:\n rule_exists = True\n\n if rule_exists is None:\n if __opts__['test'] is True:\n ret['result'] = None\n ret['changes'] = kwargs\n ret['comment'] = 'Security Group rule will be created.'\n return ret\n\n # The variable differences are a little clumsy right now\n kwargs['secgroup_name_or_id'] = secgroup\n\n new_rule = __salt__['neutronng.security_group_rule_create'](**kwargs)\n ret['changes'] = new_rule\n ret['comment'] = 'Created security group rule'\n return ret\n\n return ret", "response": "Ensure a security group rule exists"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef absent(name, auth=None, **kwargs):\n '''\n Ensure a security group rule does not exist\n\n name\n name or id of the security group rule to delete\n\n rule_id\n uuid of the rule to delete\n\n project_id\n id of project to delete rule from\n '''\n rule_id = kwargs['rule_id']\n ret = {'name': rule_id,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n __salt__['neutronng.setup_clouds'](auth)\n\n secgroup = __salt__['neutronng.security_group_get'](\n name=name,\n filters={'tenant_id': kwargs['project_id']}\n )\n\n # no need to delete a rule if the security group doesn't exist\n if secgroup is None:\n ret['comment'] = \"security group does not exist\"\n return ret\n\n # This should probably be done with compare on fields instead of\n # rule_id in the future\n rule_exists = None\n for rule in secgroup['security_group_rules']:\n if _rule_compare(rule, {\"id\": rule_id}) is True:\n rule_exists = True\n\n if rule_exists:\n if __opts__['test']:\n ret['result'] = None\n ret['changes'] = {'id': kwargs['rule_id']}\n ret['comment'] = 'Security group rule will be deleted.'\n return ret\n\n __salt__['neutronng.security_group_rule_delete'](rule_id=rule_id)\n ret['changes']['id'] = rule_id\n ret['comment'] = 'Deleted security group rule'\n\n return ret", "response": "Ensure a security group rule does not exist"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef query(name, match=None, match_type='string', status=None, status_type='string', wait_for=None, **kwargs):\n '''\n Perform an HTTP query and statefully return the result\n\n Passes through all the parameters described in the\n :py:func:`utils.http.query function <salt.utils.http.query>`:\n\n name\n The name of the query.\n\n match\n Specifies a pattern to look for in the return text. By default, this will\n perform a string comparison of looking for the value of match in the return\n text.\n\n match_type\n Specifies the type of pattern matching to use on match. Default is ``string``, but\n can also be set to ``pcre`` to use regular expression matching if a more\n complex pattern matching is required.\n\n .. note::\n\n Despite the name of ``match_type`` for this argument, this setting\n actually uses Python's ``re.search()`` function rather than Python's\n ``re.match()`` function.\n\n status\n The status code for a URL for which to be checked. Can be used instead of\n or in addition to the ``match`` setting.\n\n status_type\n Specifies the type of pattern matching to use for status. Default is ``string``, but\n can also be set to ``pcre`` to use regular expression matching if a more\n complex pattern matching is required.\n\n .. versionadded:: Neon\n\n .. note::\n\n Despite the name of ``match_type`` for this argument, this setting\n actually uses Python's ``re.search()`` function rather than Python's\n ``re.match()`` function.\n\n If both ``match`` and ``status`` options are set, both settings will be checked.\n However, note that if only one option is ``True`` and the other is ``False``,\n then ``False`` will be returned. If this case is reached, the comments in the\n return data will contain troubleshooting information.\n\n For more information about the ``http.query`` state, refer to the\n :ref:`HTTP Tutorial <tutorial-http>`.\n\n .. code-block:: yaml\n\n query_example:\n http.query:\n - name: 'http://example.com/'\n - status: 200\n\n '''\n # Monitoring state, but changes may be made over HTTP\n ret = {'name': name,\n 'result': None,\n 'comment': '',\n 'changes': {},\n 'data': {}} # Data field for monitoring state\n\n if match is None and status is None:\n ret['result'] = False\n ret['comment'] += (\n ' Either match text (match) or a status code (status) is required.'\n )\n return ret\n\n if 'decode' not in kwargs:\n kwargs['decode'] = False\n kwargs['text'] = True\n kwargs['status'] = True\n if __opts__['test']:\n kwargs['test'] = True\n\n if wait_for:\n data = __salt__['http.wait_for_successful_query'](name, wait_for=wait_for, **kwargs)\n else:\n data = __salt__['http.query'](name, **kwargs)\n\n if match is not None:\n if match_type == 'string':\n if str(match) in data.get('text', ''):\n ret['result'] = True\n ret['comment'] += ' Match text \"{0}\" was found.'.format(match)\n else:\n ret['result'] = False\n ret['comment'] += ' Match text \"{0}\" was not found.'.format(match)\n elif match_type == 'pcre':\n if re.search(str(match), str(data.get('text', ''))):\n ret['result'] = True\n ret['comment'] += ' Match pattern \"{0}\" was found.'.format(match)\n else:\n ret['result'] = False\n ret['comment'] += ' Match pattern \"{0}\" was not found.'.format(match)\n\n if status is not None:\n if status_type == 'string':\n if str(data.get('status', '')) == str(status):\n ret['comment'] += ' Status {0} was found.'.format(status)\n if ret['result'] is None:\n ret['result'] = True\n else:\n ret['comment'] += ' Status {0} was not found.'.format(status)\n ret['result'] = False\n elif status_type == 'pcre':\n if re.search(str(status), str(data.get('status', ''))):\n ret['comment'] += ' Status pattern \"{0}\" was found.'.format(status)\n if ret['result'] is None:\n ret['result'] = True\n else:\n ret['comment'] += ' Status pattern \"{0}\" was not found.'.format(status)\n ret['result'] = False\n\n # cleanup spaces in comment\n ret['comment'] = ret['comment'].strip()\n\n if __opts__['test'] is True:\n ret['result'] = None\n ret['comment'] += ' (TEST MODE'\n if 'test_url' in kwargs:\n ret['comment'] += ', TEST URL WAS: {0}'.format(kwargs['test_url'])\n ret['comment'] += ')'\n\n ret['data'] = data\n return ret", "response": "A simple HTTP query that returns the contents of the text of the specified URL."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nquerying the last available item in the last available item in the last available item.", "response": "def wait_for_successful_query(name, wait_for=300, **kwargs):\n '''\n Like query but, repeat and wait until match/match_type or status is fulfilled. State returns result from last\n query state in case of success or if no successful query was made within wait_for timeout.\n\n name\n The name of the query.\n\n wait_for\n Total time to wait for requests that succeed.\n\n request_interval\n Optional interval to delay requests by N seconds to reduce the number of requests sent.\n\n .. note::\n\n All other arguments are passed to the http.query state.\n '''\n starttime = time.time()\n\n while True:\n caught_exception = None\n ret = None\n try:\n ret = query(name, **kwargs)\n if ret['result']:\n return ret\n except Exception as exc:\n caught_exception = exc\n\n if time.time() > starttime + wait_for:\n if not ret and caught_exception:\n # workaround pylint bug https://www.logilab.org/ticket/3207\n raise caught_exception # pylint: disable=E0702\n return ret\n else:\n # Space requests out by delaying for an interval\n if 'request_interval' in kwargs:\n log.debug('delaying query for %s seconds.', kwargs['request_interval'])\n time.sleep(kwargs['request_interval'])"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __parse_drac(output):\n '''\n Parse Dell DRAC output\n '''\n drac = {}\n section = ''\n\n for i in output.splitlines():\n if i.strip().endswith(':') and '=' not in i:\n section = i[0:-1]\n drac[section] = {}\n if i.rstrip() and '=' in i:\n if section in drac:\n drac[section].update(dict(\n [[prop.strip() for prop in i.split('=')]]\n ))\n else:\n section = i.strip()\n if section not in drac and section:\n drac[section] = {}\n\n return drac", "response": "Parse Dell DRAC output."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __execute_cmd(command, host=None,\n admin_username=None, admin_password=None,\n module=None):\n '''\n Execute rac commands\n '''\n if module:\n # -a takes 'server' or 'switch' to represent all servers\n # or all switches in a chassis. Allow\n # user to say 'module=ALL_SERVER' or 'module=ALL_SWITCH'\n if module.startswith('ALL_'):\n modswitch = '-a '\\\n + module[module.index('_') + 1:len(module)].lower()\n else:\n modswitch = '-m {0}'.format(module)\n else:\n modswitch = ''\n if not host:\n # This is a local call\n cmd = __salt__['cmd.run_all']('racadm {0} {1}'.format(command,\n modswitch))\n else:\n cmd = __salt__['cmd.run_all'](\n 'racadm -r {0} -u {1} -p {2} {3} {4}'.format(host,\n admin_username,\n admin_password,\n command,\n modswitch),\n output_loglevel='quiet')\n\n if cmd['retcode'] != 0:\n log.warning('racadm returned an exit code of %s', cmd['retcode'])\n return False\n\n return True", "response": "Execute a rac command"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nexecuting rac commands and return the output", "response": "def __execute_ret(command, host=None,\n admin_username=None, admin_password=None,\n module=None):\n '''\n Execute rac commands\n '''\n if module:\n if module == 'ALL':\n modswitch = '-a '\n else:\n modswitch = '-m {0}'.format(module)\n else:\n modswitch = ''\n if not host:\n # This is a local call\n cmd = __salt__['cmd.run_all']('racadm {0} {1}'.format(command,\n modswitch))\n else:\n cmd = __salt__['cmd.run_all'](\n 'racadm -r {0} -u {1} -p {2} {3} {4}'.format(host,\n admin_username,\n admin_password,\n command,\n modswitch),\n output_loglevel='quiet')\n\n if cmd['retcode'] != 0:\n log.warning('racadm returned an exit code of %s', cmd['retcode'])\n else:\n fmtlines = []\n for l in cmd['stdout'].splitlines():\n if l.startswith('Security Alert'):\n continue\n if l.startswith('RAC1168:'):\n break\n if l.startswith('RAC1169:'):\n break\n if l.startswith('Continuing execution'):\n continue\n\n if not l.strip():\n continue\n fmtlines.append(l)\n if '=' in l:\n continue\n cmd['stdout'] = '\\n'.join(fmtlines)\n\n return cmd"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget specific property of a chassis.", "response": "def get_property(host=None, admin_username=None, admin_password=None, property=None):\n '''\n .. versionadded:: Fluorine\n\n Return specific property\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n property:\n The property which should be get.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.get_property property=System.ServerOS.HostName\n '''\n if property is None:\n raise SaltException('No property specified!')\n ret = __execute_ret('get \\'{0}\\''.format(property), host=host,\n admin_username=admin_username,\n admin_password=admin_password)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_property(host=None, admin_username=None, admin_password=None, property=None, value=None):\n '''\n .. versionadded:: Fluorine\n\n Set specific property\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n property:\n The property which should be set.\n\n value:\n The value which should be set to property.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.set_property property=System.ServerOS.HostName value=Pretty-server\n '''\n if property is None:\n raise SaltException('No property specified!')\n elif value is None:\n raise SaltException('No value specified!')\n ret = __execute_ret('set \\'{0}\\' \\'{1}\\''.format(property, value), host=host,\n admin_username=admin_username,\n admin_password=admin_password)\n return ret", "response": "Set specific property of a chassis."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure that the property is set to the given value.", "response": "def ensure_property_set(host=None, admin_username=None, admin_password=None, property=None, value=None):\n '''\n .. versionadded:: Fluorine\n\n Ensure that property is set to specific value\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n property:\n The property which should be set.\n\n value:\n The value which should be set to property.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.ensure_property_set property=System.ServerOS.HostName value=Pretty-server\n '''\n ret = get_property(host, admin_username, admin_password, property)\n if ret['stdout'] == value:\n return True\n\n ret = set_property(host, admin_username, admin_password, property, value)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn System information for a single resource in a single resource group.", "response": "def system_info(host=None,\n admin_username=None, admin_password=None,\n module=None):\n '''\n Return System information\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.system_info\n '''\n cmd = __execute_ret('getsysinfo', host=host,\n admin_username=admin_username,\n admin_password=admin_password,\n module=module)\n\n if cmd['retcode'] != 0:\n log.warning('racadm returned an exit code of %s', cmd['retcode'])\n return cmd\n\n return __parse_drac(cmd['stdout'])"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef network_info(host=None,\n admin_username=None,\n admin_password=None,\n module=None):\n '''\n Return Network Configuration\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.network_info\n '''\n\n inv = inventory(host=host, admin_username=admin_username,\n admin_password=admin_password)\n if inv is None:\n cmd = {}\n cmd['retcode'] = -1\n cmd['stdout'] = 'Problem getting switch inventory'\n return cmd\n\n if module not in inv.get('switch') and module not in inv.get('server'):\n cmd = {}\n cmd['retcode'] = -1\n cmd['stdout'] = 'No module {0} found.'.format(module)\n return cmd\n\n cmd = __execute_ret('getniccfg', host=host,\n admin_username=admin_username,\n admin_password=admin_password,\n module=module)\n\n if cmd['retcode'] != 0:\n log.warning('racadm returned an exit code of %s', cmd['retcode'])\n\n cmd['stdout'] = 'Network:\\n' + 'Device = ' + module + '\\n' + \\\n cmd['stdout']\n return __parse_drac(cmd['stdout'])", "response": "Return Network Configuration\n CLI Example : bash\n\n salt dell dracr. network_info\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef nameservers(ns,\n host=None,\n admin_username=None,\n admin_password=None,\n module=None):\n '''\n Configure the nameservers on the DRAC\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.nameservers [NAMESERVERS]\n salt dell dracr.nameservers ns1.example.com ns2.example.com\n admin_username=root admin_password=calvin module=server-1\n host=192.168.1.1\n '''\n if len(ns) > 2:\n log.warning('racadm only supports two nameservers')\n return False\n\n for i in range(1, len(ns) + 1):\n if not __execute_cmd('config -g cfgLanNetworking -o '\n 'cfgDNSServer{0} {1}'.format(i, ns[i - 1]),\n host=host,\n admin_username=admin_username,\n admin_password=admin_password,\n module=module):\n return False\n\n return True", "response": "Configure the nameservers on DRAC\notope CLI Example"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconfigure syslog remote logging", "response": "def syslog(server, enable=True, host=None,\n admin_username=None, admin_password=None, module=None):\n '''\n Configure syslog remote logging, by default syslog will automatically be\n enabled if a server is specified. However, if you want to disable syslog\n you will need to specify a server followed by False\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.syslog [SYSLOG IP] [ENABLE/DISABLE]\n salt dell dracr.syslog 0.0.0.0 False\n '''\n if enable and __execute_cmd('config -g cfgRemoteHosts -o '\n 'cfgRhostsSyslogEnable 1',\n host=host,\n admin_username=admin_username,\n admin_password=admin_password,\n module=None):\n return __execute_cmd('config -g cfgRemoteHosts -o '\n 'cfgRhostsSyslogServer1 {0}'.format(server),\n host=host,\n admin_username=admin_username,\n admin_password=admin_password,\n module=module)\n\n return __execute_cmd('config -g cfgRemoteHosts -o cfgRhostsSyslogEnable 0',\n host=host,\n admin_username=admin_username,\n admin_password=admin_password,\n module=module)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nenables or disable email alerts on the resource.", "response": "def email_alerts(action,\n host=None,\n admin_username=None,\n admin_password=None):\n '''\n Enable/Disable email alerts\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.email_alerts True\n salt dell dracr.email_alerts False\n '''\n\n if action:\n return __execute_cmd('config -g cfgEmailAlert -o '\n 'cfgEmailAlertEnable -i 1 1', host=host,\n admin_username=admin_username,\n admin_password=admin_password)\n else:\n return __execute_cmd('config -g cfgEmailAlert -o '\n 'cfgEmailAlertEnable -i 1 0')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list_users(host=None,\n admin_username=None,\n admin_password=None,\n module=None):\n '''\n List all DRAC users\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.list_users\n '''\n users = {}\n _username = ''\n\n for idx in range(1, 17):\n cmd = __execute_ret('getconfig -g '\n 'cfgUserAdmin -i {0}'.format(idx),\n host=host, admin_username=admin_username,\n admin_password=admin_password)\n\n if cmd['retcode'] != 0:\n log.warning('racadm returned an exit code of %s', cmd['retcode'])\n\n for user in cmd['stdout'].splitlines():\n if not user.startswith('cfg'):\n continue\n\n (key, val) = user.split('=')\n\n if key.startswith('cfgUserAdminUserName'):\n _username = val.strip()\n\n if val:\n users[_username] = {'index': idx}\n else:\n break\n else:\n if _username:\n users[_username].update({key: val})\n\n return users", "response": "List all DRAC users"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete a user in the neccesary way", "response": "def delete_user(username,\n uid=None,\n host=None,\n admin_username=None,\n admin_password=None):\n '''\n Delete a user\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.delete_user [USERNAME] [UID - optional]\n salt dell dracr.delete_user diana 4\n '''\n if uid is None:\n user = list_users()\n uid = user[username]['index']\n\n if uid:\n return __execute_cmd('config -g cfgUserAdmin -o '\n 'cfgUserAdminUserName -i {0} \"\"'.format(uid),\n host=host, admin_username=admin_username,\n admin_password=admin_password)\n\n else:\n log.warning('User \\'%s\\' does not exist', username)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef change_password(username, password, uid=None, host=None,\n admin_username=None, admin_password=None,\n module=None):\n '''\n Change user's password\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.change_password [USERNAME] [PASSWORD] uid=[OPTIONAL]\n host=<remote DRAC> admin_username=<DRAC user>\n admin_password=<DRAC PW>\n salt dell dracr.change_password diana secret\n\n Note that if only a username is specified then this module will look up\n details for all 16 possible DRAC users. This is time consuming, but might\n be necessary if one is not sure which user slot contains the one you want.\n Many late-model Dell chassis have 'root' as UID 1, so if you can depend\n on that then setting the password is much quicker.\n Raises an error if the supplied password is greater than 20 chars.\n '''\n if len(password) > 20:\n raise CommandExecutionError('Supplied password should be 20 characters or less')\n\n if uid is None:\n user = list_users(host=host, admin_username=admin_username,\n admin_password=admin_password, module=module)\n uid = user[username]['index']\n\n if uid:\n return __execute_cmd('config -g cfgUserAdmin -o '\n 'cfgUserAdminPassword -i {0} {1}'\n .format(uid, password),\n host=host, admin_username=admin_username,\n admin_password=admin_password, module=module)\n else:\n log.warning('racadm: user \\'%s\\' does not exist', username)\n return False", "response": "Change the password of a user"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef deploy_password(username, password, host=None, admin_username=None,\n admin_password=None, module=None):\n '''\n Change the QuickDeploy password, used for switches as well\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.deploy_password [USERNAME] [PASSWORD]\n host=<remote DRAC> admin_username=<DRAC user>\n admin_password=<DRAC PW>\n salt dell dracr.change_password diana secret\n\n Note that if only a username is specified then this module will look up\n details for all 16 possible DRAC users. This is time consuming, but might\n be necessary if one is not sure which user slot contains the one you want.\n Many late-model Dell chassis have 'root' as UID 1, so if you can depend\n on that then setting the password is much quicker.\n '''\n return __execute_cmd('deploy -u {0} -p {1}'.format(\n username, password), host=host, admin_username=admin_username,\n admin_password=admin_password, module=module\n )", "response": "Change the QuickDeploy password for a specific user"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef deploy_snmp(snmp, host=None, admin_username=None,\n admin_password=None, module=None):\n '''\n Change the QuickDeploy SNMP community string, used for switches as well\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.deploy_snmp SNMP_STRING\n host=<remote DRAC or CMC> admin_username=<DRAC user>\n admin_password=<DRAC PW>\n salt dell dracr.deploy_password diana secret\n\n '''\n return __execute_cmd('deploy -v SNMPv2 {0} ro'.format(snmp),\n host=host,\n admin_username=admin_username,\n admin_password=admin_password,\n module=module)", "response": "Deploy the specified SNMP community string to the current node."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_snmp(community, host=None,\n admin_username=None, admin_password=None):\n '''\n Configure CMC or individual iDRAC SNMP community string.\n Use ``deploy_snmp`` for configuring chassis switch SNMP.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.set_snmp [COMMUNITY]\n salt dell dracr.set_snmp public\n '''\n return __execute_cmd('config -g cfgOobSnmp -o '\n 'cfgOobSnmpAgentCommunity {0}'.format(community),\n host=host, admin_username=admin_username,\n admin_password=admin_password)", "response": "Configure CMC or individual iDRAC SNMP community string."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_network(ip, netmask, gateway, host=None,\n admin_username=None, admin_password=None):\n '''\n Configure Network on the CMC or individual iDRAC.\n Use ``set_niccfg`` for blade and switch addresses.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.set_network [DRAC IP] [NETMASK] [GATEWAY]\n salt dell dracr.set_network 192.168.0.2 255.255.255.0 192.168.0.1\n admin_username=root admin_password=calvin host=192.168.1.1\n '''\n return __execute_cmd('setniccfg -s {0} {1} {2}'.format(\n ip, netmask, gateway, host=host, admin_username=admin_username,\n admin_password=admin_password\n ))", "response": "Configure Network on the CMC or individual iDRAC."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef server_power(status, host=None,\n admin_username=None,\n admin_password=None,\n module=None):\n '''\n status\n One of 'powerup', 'powerdown', 'powercycle', 'hardreset',\n 'graceshutdown'\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n module\n The element to reboot on the chassis such as a blade. If not provided,\n the chassis will be rebooted.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.server_reboot\n salt dell dracr.server_reboot module=server-1\n\n '''\n return __execute_cmd('serveraction {0}'.format(status),\n host=host, admin_username=admin_username,\n admin_password=admin_password, module=module)", "response": "Execute a server power command."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef server_reboot(host=None,\n admin_username=None,\n admin_password=None,\n module=None):\n '''\n Issues a power-cycle operation on the managed server. This action is\n similar to pressing the power button on the system's front panel to\n power down and then power up the system.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n module\n The element to reboot on the chassis such as a blade. If not provided,\n the chassis will be rebooted.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.server_reboot\n salt dell dracr.server_reboot module=server-1\n\n '''\n return __execute_cmd('serveraction powercycle',\n host=host, admin_username=admin_username,\n admin_password=admin_password, module=module)", "response": "Execute a power - cycle operation on the managed server."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef server_poweroff(host=None,\n admin_username=None,\n admin_password=None,\n module=None):\n '''\n Powers down the managed server.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n module\n The element to power off on the chassis such as a blade.\n If not provided, the chassis will be powered off.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.server_poweroff\n salt dell dracr.server_poweroff module=server-1\n '''\n return __execute_cmd('serveraction powerdown',\n host=host, admin_username=admin_username,\n admin_password=admin_password, module=module)", "response": "Power off the chassis on the managed server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef server_poweron(host=None,\n admin_username=None,\n admin_password=None,\n module=None):\n '''\n Powers up the managed server.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n module\n The element to power on located on the chassis such as a blade. If\n not provided, the chassis will be powered on.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.server_poweron\n salt dell dracr.server_poweron module=server-1\n '''\n return __execute_cmd('serveraction powerup',\n host=host, admin_username=admin_username,\n admin_password=admin_password, module=module)", "response": "Power on the chassis."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nexecutes a hard reset on the managed server.", "response": "def server_hardreset(host=None,\n admin_username=None,\n admin_password=None,\n module=None):\n '''\n Performs a reset (reboot) operation on the managed server.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n module\n The element to hard reset on the chassis such as a blade. If\n not provided, the chassis will be reset.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.server_hardreset\n salt dell dracr.server_hardreset module=server-1\n '''\n return __execute_cmd('serveraction hardreset',\n host=host,\n admin_username=admin_username,\n admin_password=admin_password,\n module=module)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nexecutes the powerstatus command on the passed module WorkItem", "response": "def server_powerstatus(host=None,\n admin_username=None,\n admin_password=None,\n module=None):\n '''\n return the power status for the passed module\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell drac.server_powerstatus\n '''\n ret = __execute_ret('serveraction powerstatus',\n host=host, admin_username=admin_username,\n admin_password=admin_password,\n module=module)\n\n result = {'retcode': 0}\n if ret['stdout'] == 'ON':\n result['status'] = True\n result['comment'] = 'Power is on'\n if ret['stdout'] == 'OFF':\n result['status'] = False\n result['comment'] = 'Power is on'\n if ret['stdout'].startswith('ERROR'):\n result['status'] = False\n result['comment'] = ret['stdout']\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef server_pxe(host=None,\n admin_username=None,\n admin_password=None):\n '''\n Configure server to PXE perform a one off PXE boot\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dell dracr.server_pxe\n '''\n if __execute_cmd('config -g cfgServerInfo -o cfgServerFirstBootDevice PXE',\n host=host, admin_username=admin_username,\n admin_password=admin_password):\n if __execute_cmd('config -g cfgServerInfo -o cfgServerBootOnce 1',\n host=host, admin_username=admin_username,\n admin_password=admin_password):\n return server_reboot\n else:\n log.warning('failed to set boot order')\n return False\n\n log.warning('failed to configure PXE boot')\n return False", "response": "Configure server to PXE perform a one off PXE boot"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list_slotnames(host=None,\n admin_username=None,\n admin_password=None):\n '''\n List the names of all slots in the chassis.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call --local dracr.list_slotnames host=111.222.333.444\n admin_username=root admin_password=secret\n\n '''\n slotraw = __execute_ret('getslotname',\n host=host, admin_username=admin_username,\n admin_password=admin_password)\n\n if slotraw['retcode'] != 0:\n return slotraw\n slots = {}\n stripheader = True\n for l in slotraw['stdout'].splitlines():\n if l.startswith('<'):\n stripheader = False\n continue\n if stripheader:\n continue\n fields = l.split()\n slots[fields[0]] = {}\n slots[fields[0]]['slot'] = fields[0]\n if len(fields) > 1:\n slots[fields[0]]['slotname'] = fields[1]\n else:\n slots[fields[0]]['slotname'] = ''\n if len(fields) > 2:\n slots[fields[0]]['hostname'] = fields[2]\n else:\n slots[fields[0]]['hostname'] = ''\n\n return slots", "response": "List the names of all slots in the chassis."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_slotname(slot, host=None, admin_username=None, admin_password=None):\n '''\n Get the name of a slot number in the chassis.\n\n slot\n The number of the slot for which to obtain the name.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call --local dracr.get_slotname 0 host=111.222.333.444\n admin_username=root admin_password=secret\n\n '''\n slots = list_slotnames(host=host, admin_username=admin_username,\n admin_password=admin_password)\n # The keys for this dictionary are strings, not integers, so convert the\n # argument to a string\n slot = six.text_type(slot)\n return slots[slot]['slotname']", "response": "Get the name of a slot in the chassis."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef set_slotname(slot, name, host=None,\n admin_username=None, admin_password=None):\n '''\n Set the name of a slot in a chassis.\n\n slot\n The slot number to change.\n\n name\n The name to set. Can only be 15 characters long.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' dracr.set_slotname 2 my-slotname host=111.222.333.444\n admin_username=root admin_password=secret\n\n '''\n return __execute_cmd('config -g cfgServerInfo -o cfgServerName -i {0} {1}'.format(slot, name),\n host=host, admin_username=admin_username,\n admin_password=admin_password)", "response": "Set the name of a chassis in a chassis."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets the name of the chassis.", "response": "def set_chassis_name(name,\n host=None,\n admin_username=None,\n admin_password=None):\n '''\n Set the name of the chassis.\n\n name\n The name to be set on the chassis.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' dracr.set_chassis_name my-chassis host=111.222.333.444\n admin_username=root admin_password=secret\n\n '''\n return __execute_cmd('setsysinfo -c chassisname {0}'.format(name),\n host=host, admin_username=admin_username,\n admin_password=admin_password)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_chassis_name(host=None, admin_username=None, admin_password=None):\n '''\n Get the name of a chassis.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' dracr.get_chassis_name host=111.222.333.444\n admin_username=root admin_password=secret\n\n '''\n return bare_rac_cmd('getchassisname', host=host,\n admin_username=admin_username,\n admin_password=admin_password)", "response": "Get the name of a chassis."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset the location of the chassis.", "response": "def set_chassis_location(location,\n host=None,\n admin_username=None,\n admin_password=None):\n '''\n Set the location of the chassis.\n\n location\n The name of the location to be set on the chassis.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' dracr.set_chassis_location location-name host=111.222.333.444\n admin_username=root admin_password=secret\n\n '''\n return __execute_cmd('setsysinfo -c chassislocation {0}'.format(location),\n host=host, admin_username=admin_username,\n admin_password=admin_password)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting the location of the chassis.", "response": "def get_chassis_location(host=None,\n admin_username=None,\n admin_password=None):\n '''\n Get the location of the chassis.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' dracr.set_chassis_location host=111.222.333.444\n admin_username=root admin_password=secret\n\n '''\n return system_info(host=host,\n admin_username=admin_username,\n admin_password=admin_password)['Chassis Information']['Chassis Location']"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_chassis_datacenter(location,\n host=None,\n admin_username=None,\n admin_password=None):\n '''\n Set the location of the chassis.\n\n location\n The name of the datacenter to be set on the chassis.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' dracr.set_chassis_datacenter datacenter-name host=111.222.333.444\n admin_username=root admin_password=secret\n\n '''\n return set_general('cfgLocation', 'cfgLocationDatacenter', location,\n host=host, admin_username=admin_username,\n admin_password=admin_password)", "response": "Sets the location of the chassis s datacenter."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the datacenter of the chassis.", "response": "def get_chassis_datacenter(host=None,\n admin_username=None,\n admin_password=None):\n '''\n Get the datacenter of the chassis.\n\n host\n The chassis host.\n\n admin_username\n The username used to access the chassis.\n\n admin_password\n The password used to access the chassis.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' dracr.set_chassis_location host=111.222.333.444\n admin_username=root admin_password=secret\n\n '''\n return get_general('cfgLocation', 'cfgLocationDatacenter', host=host,\n admin_username=admin_username, admin_password=admin_password)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef idrac_general(blade_name, command, idrac_password=None,\n host=None,\n admin_username=None, admin_password=None):\n '''\n Run a generic racadm command against a particular\n blade in a chassis. Blades are usually named things like\n 'server-1', 'server-2', etc. If the iDRAC has a different\n password than the CMC, then you can pass it with the\n idrac_password kwarg.\n\n :param blade_name: Name of the blade to run the command on\n :param command: Command like to pass to racadm\n :param idrac_password: Password for the iDRAC if different from the CMC\n :param host: Chassis hostname\n :param admin_username: CMC username\n :param admin_password: CMC password\n :return: stdout if the retcode is 0, otherwise a standard cmd.run_all dictionary\n\n CLI Example:\n\n .. code-block:: bash\n\n salt fx2 chassis.cmd idrac_general server-1 'get BIOS.SysProfileSettings'\n\n '''\n\n module_network = network_info(host, admin_username,\n admin_password, blade_name)\n\n if idrac_password is not None:\n password = idrac_password\n else:\n password = admin_password\n\n idrac_ip = module_network['Network']['IP Address']\n\n ret = __execute_ret(command, host=idrac_ip,\n admin_username='root',\n admin_password=password)\n\n if ret['retcode'] == 0:\n return ret['stdout']\n else:\n return ret", "response": "Run a generic racadm command against a particular iDRAC blade"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef update_firmware(filename,\n host=None,\n admin_username=None,\n admin_password=None):\n '''\n Updates firmware using local firmware file\n\n .. code-block:: bash\n\n salt dell dracr.update_firmware firmware.exe\n\n This executes the following command on your FX2\n (using username and password stored in the pillar data)\n\n .. code-block:: bash\n\n racadm update \u2013f firmware.exe -u user \u2013p pass\n\n '''\n if os.path.exists(filename):\n return _update_firmware('update -f {0}'.format(filename),\n host=None,\n admin_username=None,\n admin_password=None)\n else:\n raise CommandExecutionError('Unable to find firmware file {0}'\n .format(filename))", "response": "Update the firmware using local firmware file"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nupdate the firmware of a file in the nfs or cifs repository", "response": "def update_firmware_nfs_or_cifs(filename, share,\n host=None,\n admin_username=None,\n admin_password=None):\n '''\n Executes the following for CIFS\n (using username and password stored in the pillar data)\n\n .. code-block:: bash\n\n racadm update -f <updatefile> -u user \u2013p pass -l //IP-Address/share\n\n Or for NFS\n (using username and password stored in the pillar data)\n\n .. code-block:: bash\n\n racadm update -f <updatefile> -u user \u2013p pass -l IP-address:/share\n\n\n Salt command for CIFS:\n\n .. code-block:: bash\n\n salt dell dracr.update_firmware_nfs_or_cifs \\\n firmware.exe //IP-Address/share\n\n\n Salt command for NFS:\n\n .. code-block:: bash\n\n salt dell dracr.update_firmware_nfs_or_cifs \\\n firmware.exe IP-address:/share\n '''\n if os.path.exists(filename):\n return _update_firmware('update -f {0} -l {1}'.format(filename, share),\n host=None,\n admin_username=None,\n admin_password=None)\n else:\n raise CommandExecutionError('Unable to find firmware file {0}'\n .format(filename))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nsend a message to a Microsft Teams channel", "response": "def post_card(name,\n message,\n hook_url=None,\n title=None,\n theme_color=None):\n '''\n Send a message to a Microsft Teams channel\n\n .. code-block:: yaml\n\n send-msteams-message:\n msteams.post_card:\n - message: 'This state was executed successfully.'\n - hook_url: https://outlook.office.com/webhook/837\n\n The following parameters are required:\n\n message\n The message that is to be sent to the MS Teams channel.\n\n The following parameters are optional:\n\n hook_url\n The webhook URL given configured in Teams interface,\n if not specified in the configuration options of master or minion.\n title\n The title for the card posted to the channel\n theme_color\n A hex code for the desired highlight color\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'The following message is to be sent to Teams: {0}'.format(message)\n ret['result'] = None\n return ret\n\n if not message:\n ret['comment'] = 'Teams message is missing: {0}'.format(message)\n return ret\n\n try:\n result = __salt__['msteams.post_card'](\n message=message,\n hook_url=hook_url,\n title=title,\n theme_color=theme_color,\n )\n except SaltInvocationError as sie:\n ret['comment'] = 'Failed to send message ({0}): {1}'.format(sie, name)\n else:\n if isinstance(result, bool) and result:\n ret['result'] = True\n ret['comment'] = 'Sent message: {0}'.format(name)\n else:\n ret['comment'] = 'Failed to send message ({0}): {1}'.format(result['message'], name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\naccepts a dict of keys based on a dict of keys. Returns a dictionary of keys.", "response": "def accept_dict(match, include_rejected=False, include_denied=False):\n '''\n Accept keys based on a dict of keys. Returns a dictionary.\n\n match\n The dictionary of keys to accept.\n\n include_rejected\n To include rejected keys in the match along with pending keys, set this\n to ``True``. Defaults to ``False``.\n\n .. versionadded:: 2016.3.4\n\n include_denied\n To include denied keys in the match along with pending keys, set this\n to ``True``. Defaults to ``False``.\n\n .. versionadded:: 2016.3.4\n\n Example to move a list of keys from the ``minions_pre`` (pending) directory\n to the ``minions`` (accepted) directory:\n\n .. code-block:: python\n\n >>> wheel.cmd('key.accept_dict',\n {\n 'minions_pre': [\n 'jerry',\n 'stuart',\n 'bob',\n ],\n })\n {'minions': ['jerry', 'stuart', 'bob']}\n '''\n skey = get_key(__opts__)\n return skey.accept(match_dict=match,\n include_rejected=include_rejected,\n include_denied=include_denied)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nrejecting a set of keys based on a glob match. Returns a dictionary.", "response": "def reject(match, include_accepted=False, include_denied=False):\n '''\n Reject keys based on a glob match. Returns a dictionary.\n\n match\n The glob match of keys to reject.\n\n include_accepted\n To include accepted keys in the match along with pending keys, set this\n to ``True``. Defaults to ``False``.\n\n include_denied\n To include denied keys in the match along with pending keys, set this\n to ``True``. Defaults to ``False``.\n\n .. code-block:: python\n\n >>> wheel.cmd_async({'fun': 'key.reject', 'match': 'minion1'})\n {'jid': '20160826201244808521', 'tag': 'salt/wheel/20160826201244808521'}\n '''\n skey = get_key(__opts__)\n return skey.reject(match, include_accepted=include_accepted, include_denied=include_denied)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the matching key fingerprints. Returns a dictionary.", "response": "def finger(match, hash_type=None):\n '''\n Return the matching key fingerprints. Returns a dictionary.\n\n match\n The key for with to retrieve the fingerprint.\n\n hash_type\n The hash algorithm used to calculate the fingerprint\n\n .. code-block:: python\n\n >>> wheel.cmd('key.finger', ['minion1'])\n {'minions': {'minion1': '5d:f6:79:43:5e:d4:42:3f:57:b8:45:a8:7e:a4:6e:ca'}}\n\n '''\n if hash_type is None:\n hash_type = __opts__['hash_type']\n\n skey = get_key(__opts__)\n return skey.finger(match, hash_type)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the fingerprint of the master s public key", "response": "def finger_master(hash_type=None):\n '''\n Return the fingerprint of the master's public key\n\n hash_type\n The hash algorithm used to calculate the fingerprint\n\n .. code-block:: python\n\n >>> wheel.cmd('key.finger_master')\n {'local': {'master.pub': '5d:f6:79:43:5e:d4:42:3f:57:b8:45:a8:7e:a4:6e:ca'}}\n '''\n keyname = 'master.pub'\n if hash_type is None:\n hash_type = __opts__['hash_type']\n\n fingerprint = salt.utils.crypt.pem_finger(\n os.path.join(__opts__['pki_dir'], keyname), sum_type=hash_type)\n return {'local': {keyname: fingerprint}}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef gen(id_=None, keysize=2048):\n r'''\n Generate a key pair. No keys are stored on the master. A key pair is\n returned as a dict containing pub and priv keys. Returns a dictionary\n containing the the ``pub`` and ``priv`` keys with their generated values.\n\n id\\_\n Set a name to generate a key pair for use with salt. If not specified,\n a random name will be specified.\n\n keysize\n The size of the key pair to generate. The size must be ``2048``, which\n is the default, or greater. If set to a value less than ``2048``, the\n key size will be rounded up to ``2048``.\n\n .. code-block:: python\n\n >>> wheel.cmd('key.gen')\n {'pub': '-----BEGIN PUBLIC KEY-----\\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBC\n ...\n BBPfamX9gGPQTpN9e8HwcZjXQnmg8OrcUl10WHw09SDWLOlnW+ueTWugEQpPt\\niQIDAQAB\\n\n -----END PUBLIC KEY-----',\n 'priv': '-----BEGIN RSA PRIVATE KEY-----\\nMIIEpAIBAAKCAQEA42Kf+w9XeZWgguzv\n ...\n QH3/W74X1+WTBlx4R2KGLYBiH+bCCFEQ/Zvcu4Xp4bIOPtRKozEQ==\\n\n -----END RSA PRIVATE KEY-----'}\n\n '''\n if id_ is None:\n id_ = hashlib.sha512(os.urandom(32)).hexdigest()\n else:\n id_ = clean.filename(id_)\n ret = {'priv': '',\n 'pub': ''}\n priv = salt.crypt.gen_keys(__opts__['pki_dir'], id_, keysize)\n pub = '{0}.pub'.format(priv[:priv.rindex('.')])\n with salt.utils.files.fopen(priv) as fp_:\n ret['priv'] = salt.utils.stringutils.to_unicode(fp_.read())\n with salt.utils.files.fopen(pub) as fp_:\n ret['pub'] = salt.utils.stringutils.to_unicode(fp_.read())\n\n # The priv key is given the Read-Only attribute. The causes `os.remove` to\n # fail in Windows.\n if salt.utils.platform.is_windows():\n os.chmod(priv, 128)\n\n os.remove(priv)\n os.remove(pub)\n return ret", "response": "r Generate a key pair."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef gen_accept(id_, keysize=2048, force=False):\n r'''\n Generate a key pair then accept the public key. This function returns the\n key pair in a dict, only the public key is preserved on the master. Returns\n a dictionary.\n\n id\\_\n The name of the minion for which to generate a key pair.\n\n keysize\n The size of the key pair to generate. The size must be ``2048``, which\n is the default, or greater. If set to a value less than ``2048``, the\n key size will be rounded up to ``2048``.\n\n force\n If a public key has already been accepted for the given minion on the\n master, then the gen_accept function will return an empty dictionary\n and not create a new key. This is the default behavior. If ``force``\n is set to ``True``, then the minion's previously accepted key will be\n overwritten.\n\n .. code-block:: python\n\n >>> wheel.cmd('key.gen_accept', ['foo'])\n {'pub': '-----BEGIN PUBLIC KEY-----\\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBC\n ...\n BBPfamX9gGPQTpN9e8HwcZjXQnmg8OrcUl10WHw09SDWLOlnW+ueTWugEQpPt\\niQIDAQAB\\n\n -----END PUBLIC KEY-----',\n 'priv': '-----BEGIN RSA PRIVATE KEY-----\\nMIIEpAIBAAKCAQEA42Kf+w9XeZWgguzv\n ...\n QH3/W74X1+WTBlx4R2KGLYBiH+bCCFEQ/Zvcu4Xp4bIOPtRKozEQ==\\n\n -----END RSA PRIVATE KEY-----'}\n\n We can now see that the ``foo`` minion's key has been accepted by the master:\n\n .. code-block:: python\n\n >>> wheel.cmd('key.list', ['accepted'])\n {'minions': ['foo', 'minion1', 'minion2', 'minion3']}\n '''\n id_ = clean.id(id_)\n ret = gen(id_, keysize)\n acc_path = os.path.join(__opts__['pki_dir'], 'minions', id_)\n if os.path.isfile(acc_path) and not force:\n return {}\n with salt.utils.files.fopen(acc_path, 'w+') as fp_:\n fp_.write(salt.utils.stringutils.to_str(ret['pub']))\n return ret", "response": "r Generate a key pair then accept the public key."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates minion RSA public keypair", "response": "def gen_keys(keydir=None, keyname=None, keysize=None, user=None):\n '''\n Generate minion RSA public keypair\n '''\n skey = get_key(__opts__)\n return skey.gen_keys(keydir, keyname, keysize, user)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngenerating master public - key - signature", "response": "def gen_signature(priv, pub, signature_path, auto_create=False, keysize=None):\n '''\n Generate master public-key-signature\n '''\n skey = get_key(__opts__)\n return skey.gen_keys_signature(priv, pub, signature_path, auto_create, keysize)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn information on a directory located on the Moose CLI Example", "response": "def dirinfo(path, opts=None):\n '''\n Return information on a directory located on the Moose\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' moosefs.dirinfo /path/to/dir/ [-[n][h|H]]\n '''\n cmd = 'mfsdirinfo'\n ret = {}\n if opts:\n cmd += ' -' + opts\n cmd += ' ' + path\n out = __salt__['cmd.run_all'](cmd, python_shell=False)\n\n output = out['stdout'].splitlines()\n for line in output:\n if not line:\n continue\n comps = line.split(':')\n ret[comps[0].strip()] = comps[1].strip()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef fileinfo(path):\n '''\n Return information on a file located on the Moose\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' moosefs.fileinfo /path/to/dir/\n '''\n cmd = 'mfsfileinfo ' + path\n ret = {}\n chunknum = ''\n out = __salt__['cmd.run_all'](cmd, python_shell=False)\n\n output = out['stdout'].splitlines()\n for line in output:\n if not line:\n continue\n if '/' in line:\n comps = line.split('/')\n\n chunknum = comps[0].strip().split(':')\n meta = comps[1].strip().split(' ')\n\n chunk = chunknum[0].replace('chunk ', '')\n loc = chunknum[1].strip()\n id_ = meta[0].replace('(id:', '')\n ver = meta[1].replace(')', '').replace('ver:', '')\n\n ret[chunknum[0]] = {\n 'chunk': chunk,\n 'loc': loc,\n 'id': id_,\n 'ver': ver,\n }\n if 'copy' in line:\n copyinfo = line.strip().split(':')\n ret[chunknum[0]][copyinfo[0]] = {\n 'copy': copyinfo[0].replace('copy ', ''),\n 'ip': copyinfo[1].strip(),\n 'port': copyinfo[2],\n }\n return ret", "response": "Return information on a file located on the Moose\nApps CLI Example :"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a list of current MooseFS mounts", "response": "def mounts():\n '''\n Return a list of current MooseFS mounts\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' moosefs.mounts\n '''\n cmd = 'mount'\n ret = {}\n out = __salt__['cmd.run_all'](cmd)\n\n output = out['stdout'].splitlines()\n for line in output:\n if not line:\n continue\n if 'fuse.mfs' in line:\n comps = line.split(' ')\n info1 = comps[0].split(':')\n info2 = info1[1].split('/')\n ret[comps[2]] = {\n 'remote': {\n 'master': info1[0],\n 'port': info2[0],\n 'subfolder': '/' + info2[1],\n },\n 'local': comps[2],\n 'options': (comps[5].replace('(', '').replace(')', '')\n .split(',')),\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning goal for a file or directory", "response": "def getgoal(path, opts=None):\n '''\n Return goal(s) for a file or directory\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' moosefs.getgoal /path/to/file [-[n][h|H]]\n salt '*' moosefs.getgoal /path/to/dir/ [-[n][h|H][r]]\n '''\n cmd = 'mfsgetgoal'\n ret = {}\n if opts:\n cmd += ' -' + opts\n else:\n opts = ''\n cmd += ' ' + path\n out = __salt__['cmd.run_all'](cmd, python_shell=False)\n\n output = out['stdout'].splitlines()\n if 'r' not in opts:\n goal = output[0].split(': ')\n ret = {\n 'goal': goal[1],\n }\n else:\n for line in output:\n if not line:\n continue\n if path in line:\n continue\n comps = line.split()\n keytext = comps[0] + ' with goal'\n if keytext not in ret:\n ret[keytext] = {}\n ret[keytext][comps[3]] = comps[5]\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _auditpol_cmd(cmd):\n '''\n Helper function for running the auditpol command\n\n Args:\n cmd (str): the auditpol command to run\n\n Returns:\n list: A list containing each line of the return (splitlines)\n\n Raises:\n CommandExecutionError: If the command encounters an error\n '''\n ret = salt.modules.cmdmod.run_all(cmd='auditpol {0}'.format(cmd),\n python_shell=True)\n if ret['retcode'] == 0:\n return ret['stdout'].splitlines()\n\n msg = 'Error executing auditpol command: {0}\\n'.format(cmd)\n msg += '\\n'.join(ret['stdout'])\n raise CommandExecutionError(msg)", "response": "Helper function for running the auditpol command"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_settings(category='All'):\n '''\n Get the current configuration for all audit settings specified in the\n category\n\n Args:\n category (str):\n One of the nine categories to return. Can also be ``All`` to return\n the settings for all categories. Valid options are:\n\n - Account Logon\n - Account Management\n - Detailed Tracking\n - DS Access\n - Logon/Logoff\n - Object Access\n - Policy Change\n - Privilege Use\n - System\n - All\n\n Default value is ``All``\n\n Returns:\n dict: A dictionary containing all subcategories for the specified\n category along with their current configuration\n\n Raises:\n KeyError: On invalid category\n CommandExecutionError: If an error is encountered retrieving the settings\n\n Usage:\n\n .. code-block:: python\n\n import salt.utils.win_lgpo_auditpol\n\n # Get current state of all audit settings\n salt.utils.win_lgpo_auditpol.get_settings()\n\n # Get the current state of all audit settings in the \"Account Logon\"\n # category\n salt.utils.win_lgpo_auditpol.get_settings(category=\"Account Logon\")\n '''\n # Parameter validation\n if category.lower() in ['all', '*']:\n category = '*'\n elif category.lower() not in [x.lower() for x in categories]:\n raise KeyError('Invalid category: \"{0}\"'.format(category))\n\n cmd = '/get /category:\"{0}\"'.format(category)\n results = _auditpol_cmd(cmd)\n\n ret = {}\n # Skip the first 2 lines\n for line in results[3:]:\n if ' ' in line.strip():\n ret.update(dict(list(zip(*[iter(re.split(r\"\\s{2,}\", line.strip()))]*2))))\n return ret", "response": "Get the current state of all audit settings for the specified category"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget the current configuration for the named audit setting", "response": "def get_setting(name):\n '''\n Get the current configuration for the named audit setting\n\n Args:\n name (str): The name of the setting to retrieve\n\n Returns:\n str: The current configuration for the named setting\n\n Raises:\n KeyError: On invalid setting name\n CommandExecutionError: If an error is encountered retrieving the settings\n\n Usage:\n\n .. code-block:: python\n\n import salt.utils.win_lgpo_auditpol\n\n # Get current state of the \"Credential Validation\" setting\n salt.utils.win_lgpo_auditpol.get_setting(name='Credential Validation')\n '''\n current_settings = get_settings(category='All')\n for setting in current_settings:\n if name.lower() == setting.lower():\n return current_settings[setting]\n raise KeyError('Invalid name: {0}'.format(name))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_setting(name, value):\n '''\n Set the configuration for the named audit setting\n\n Args:\n\n name (str):\n The name of the setting to configure\n\n value (str):\n The configuration for the named value. Valid options are:\n\n - No Auditing\n - Success\n - Failure\n - Success and Failure\n\n Returns:\n bool: True if successful\n\n Raises:\n KeyError: On invalid ``name`` or ``value``\n CommandExecutionError: If an error is encountered modifying the setting\n\n Usage:\n\n .. code-block:: python\n\n import salt.utils.win_lgpo_auditpol\n\n # Set the state of the \"Credential Validation\" setting to Success and\n # Failure\n salt.utils.win_lgpo_auditpol.set_setting(name='Credential Validation',\n value='Success and Failure')\n\n # Set the state of the \"Credential Validation\" setting to No Auditing\n salt.utils.win_lgpo_auditpol.set_setting(name='Credential Validation',\n value='No Auditing')\n '''\n # Input validation\n if name.lower() not in _get_valid_names():\n raise KeyError('Invalid name: {0}'.format(name))\n for setting in settings:\n if value.lower() == setting.lower():\n cmd = '/set /subcategory:\"{0}\" {1}'.format(name, settings[setting])\n break\n else:\n raise KeyError('Invalid setting value: {0}'.format(value))\n\n _auditpol_cmd(cmd)\n\n return True", "response": "Set the value of a named audit setting"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_auditpol_dump():\n '''\n Gets the contents of an auditpol /backup. Used by the LGPO module to get\n fieldnames and GUIDs for Advanced Audit policies.\n\n Returns:\n list: A list of lines form the backup file\n\n Usage:\n\n .. code-block:: python\n\n import salt.utils.win_lgpo_auditpol\n\n dump = salt.utils.win_lgpo_auditpol.get_auditpol_dump()\n '''\n # Just get a temporary file name\n # NamedTemporaryFile will delete the file it creates by default on Windows\n with tempfile.NamedTemporaryFile(suffix='.csv') as tmp_file:\n csv_file = tmp_file.name\n\n cmd = '/backup /file:{0}'.format(csv_file)\n _auditpol_cmd(cmd)\n\n with salt.utils.files.fopen(csv_file) as fp:\n return fp.readlines()", "response": "Get the contents of an auditpol file"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck to see if given ruby is installed.", "response": "def _ruby_installed(ret, ruby, user=None):\n '''\n Check to see if given ruby is installed.\n '''\n default = __salt__['rbenv.default'](runas=user)\n for version in __salt__['rbenv.versions'](user):\n if version == ruby:\n ret['result'] = True\n ret['comment'] = 'Requested ruby exists'\n ret['default'] = default == ruby\n break\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nverifies that ruby is installed install if unavailable", "response": "def _check_and_install_ruby(ret, ruby, default=False, user=None):\n '''\n Verify that ruby is installed, install if unavailable\n '''\n ret = _ruby_installed(ret, ruby, user=user)\n if not ret['result']:\n if __salt__['rbenv.install_ruby'](ruby, runas=user):\n ret['result'] = True\n ret['changes'][ruby] = 'Installed'\n ret['comment'] = 'Successfully installed ruby'\n ret['default'] = default\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to install ruby'\n return ret\n\n if default:\n __salt__['rbenv.default'](ruby, runas=user)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef installed(name, default=False, user=None):\n '''\n Verify that the specified ruby is installed with rbenv. Rbenv is\n installed if necessary.\n\n name\n The version of ruby to install\n\n default : False\n Whether to make this ruby the default.\n\n user: None\n The user to run rbenv as.\n\n .. versionadded:: 0.17.0\n\n .. versionadded:: 0.16.0\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n rbenv_installed_ret = copy.deepcopy(ret)\n\n if name.startswith('ruby-'):\n name = re.sub(r'^ruby-', '', name)\n\n if __opts__['test']:\n ret = _ruby_installed(ret, name, user=user)\n if not ret['result']:\n ret['comment'] = 'Ruby {0} is set to be installed'.format(name)\n else:\n ret['comment'] = 'Ruby {0} is already installed'.format(name)\n return ret\n\n rbenv_installed_ret = _check_and_install_rbenv(rbenv_installed_ret, user)\n if rbenv_installed_ret['result'] is False:\n ret['result'] = False\n ret['comment'] = 'Rbenv failed to install'\n return ret\n else:\n return _check_and_install_ruby(ret, name, default, user=user)", "response": "Verify that the specified ruby is installed with rbenv."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nverifying that ruby is uninstalled and uninstalls it", "response": "def _check_and_uninstall_ruby(ret, ruby, user=None):\n '''\n Verify that ruby is uninstalled\n '''\n ret = _ruby_installed(ret, ruby, user=user)\n if ret['result']:\n if ret['default']:\n __salt__['rbenv.default']('system', runas=user)\n\n if __salt__['rbenv.uninstall_ruby'](ruby, runas=user):\n ret['result'] = True\n ret['changes'][ruby] = 'Uninstalled'\n ret['comment'] = 'Successfully removed ruby'\n return ret\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to uninstall ruby'\n return ret\n else:\n ret['result'] = True\n ret['comment'] = 'Ruby {0} is already absent'.format(ruby)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nverifying that rbenv is installed install if unavailable", "response": "def _check_and_install_rbenv(ret, user=None):\n '''\n Verify that rbenv is installed, install if unavailable\n '''\n ret = _check_rbenv(ret, user)\n if ret['result'] is False:\n if __salt__['rbenv.install'](user):\n ret['result'] = True\n ret['comment'] = 'Rbenv installed'\n else:\n ret['result'] = False\n ret['comment'] = 'Rbenv failed to install'\n else:\n ret['result'] = True\n ret['comment'] = 'Rbenv is already installed'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ninstall rbenv if not installed", "response": "def install_rbenv(name, user=None):\n '''\n Install rbenv if not installed. Allows you to require rbenv be installed\n prior to installing the plugins. Useful if you want to install rbenv\n plugins via the git or file modules and need them installed before\n installing any rubies.\n\n Use the rbenv.root configuration option to set the path for rbenv if you\n want a system wide install that is not in a user home dir.\n\n user: None\n The user to run rbenv as.\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n\n if __opts__['test']:\n ret = _check_rbenv(ret, user=user)\n if ret['result'] is False:\n ret['result'] = None\n ret['comment'] = 'Rbenv is set to be installed'\n else:\n ret['result'] = True\n ret['comment'] = 'Rbenv is already installed'\n return ret\n\n return _check_and_install_rbenv(ret, user)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nbuilding a well - formatted nftables rule based on the given arguments.", "response": "def build_rule(table=None, chain=None, command=None, position='', full=None, family='ipv4',\n **kwargs):\n '''\n Build a well-formatted nftables rule based on kwargs.\n A `table` and `chain` are not required, unless `full` is True.\n\n If `full` is `True`, then `table`, `chain` and `command` are required.\n `command` may be specified as either insert, append, or delete.\n This will return the nftables command, exactly as it would\n be used from the command line.\n\n If a position is required (as with `insert` or `delete`), it may be specified as\n `position`. This will only be useful if `full` is True.\n\n If `connstate` is passed in, it will automatically be changed to `state`.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' nftables.build_rule match=state \\\\\n connstate=RELATED,ESTABLISHED jump=ACCEPT\n salt '*' nftables.build_rule filter input command=insert position=3 \\\\\n full=True match=state state=related,established jump=accept\n\n IPv6:\n salt '*' nftables.build_rule match=state \\\\\n connstate=related,established jump=accept \\\\\n family=ipv6\n salt '*' nftables.build_rule filter input command=insert position=3 \\\\\n full=True match=state state=related,established jump=accept \\\\\n family=ipv6\n\n '''\n ret = {'comment': '',\n 'rule': '',\n 'result': False}\n\n if 'target' in kwargs:\n kwargs['jump'] = kwargs['target']\n del kwargs['target']\n\n for ignore in list(_STATE_INTERNAL_KEYWORDS) + ['chain', 'save', 'table']:\n if ignore in kwargs:\n del kwargs[ignore]\n\n rule = ''\n proto = ''\n\n nft_family = _NFTABLES_FAMILIES[family]\n\n if 'if' in kwargs:\n rule += 'meta iifname {0} '.format(kwargs['if'])\n del kwargs['if']\n\n if 'of' in kwargs:\n rule += 'meta oifname {0} '.format(kwargs['of'])\n del kwargs['of']\n\n if 'proto' in kwargs:\n proto = kwargs['proto']\n\n if 'state' in kwargs:\n del kwargs['state']\n\n if 'connstate' in kwargs:\n rule += 'ct state {{ {0}}} '.format(kwargs['connstate'])\n del kwargs['connstate']\n\n if 'dport' in kwargs:\n kwargs['dport'] = six.text_type(kwargs['dport'])\n if ':' in kwargs['dport']:\n kwargs['dport'] = kwargs['dport'].replace(':', '-')\n rule += 'dport {{ {0} }} '.format(kwargs['dport'])\n del kwargs['dport']\n\n if 'sport' in kwargs:\n kwargs['sport'] = six.text_type(kwargs['sport'])\n if ':' in kwargs['sport']:\n kwargs['sport'] = kwargs['sport'].replace(':', '-')\n rule += 'sport {{ {0} }} '.format(kwargs['sport'])\n del kwargs['sport']\n\n if 'dports' in kwargs:\n # nftables reverse sorts the ports from\n # high to low, create rule like this\n # so that the check will work\n _dports = kwargs['dports'].split(',')\n _dports = [int(x) for x in _dports]\n _dports.sort(reverse=True)\n kwargs['dports'] = ', '.join(six.text_type(x) for x in _dports)\n\n rule += 'dport {{ {0} }} '.format(kwargs['dports'])\n del kwargs['dports']\n\n if 'sports' in kwargs:\n # nftables reverse sorts the ports from\n # high to low, create rule like this\n # so that the check will work\n _sports = kwargs['sports'].split(',')\n _sports = [int(x) for x in _sports]\n _sports.sort(reverse=True)\n kwargs['sports'] = ', '.join(six.text_type(x) for x in _sports)\n\n rule += 'sport {{ {0} }} '.format(kwargs['sports'])\n del kwargs['sports']\n\n # Jumps should appear last, except for any arguments that are passed to\n # jumps, which of course need to follow.\n after_jump = []\n\n if 'jump' in kwargs:\n after_jump.append('{0} '.format(kwargs['jump']))\n del kwargs['jump']\n\n if 'j' in kwargs:\n after_jump.append('{0} '.format(kwargs['j']))\n del kwargs['j']\n\n if 'to-port' in kwargs:\n after_jump.append('--to-port {0} '.format(kwargs['to-port']))\n del kwargs['to-port']\n\n if 'to-ports' in kwargs:\n after_jump.append('--to-ports {0} '.format(kwargs['to-ports']))\n del kwargs['to-ports']\n\n if 'to-destination' in kwargs:\n after_jump.append('--to-destination {0} '.format(kwargs['to-destination']))\n del kwargs['to-destination']\n\n if 'reject-with' in kwargs:\n after_jump.append('--reject-with {0} '.format(kwargs['reject-with']))\n del kwargs['reject-with']\n\n for item in after_jump:\n rule += item\n\n # Strip trailing spaces off rule\n rule = rule.strip()\n\n # Insert the protocol prior to dport or sport\n rule = rule.replace('dport', '{0} dport'.format(proto))\n rule = rule.replace('sport', '{0} sport'.format(proto))\n\n ret['rule'] = rule\n\n if full in ['True', 'true']:\n\n if not table:\n ret['comment'] = 'Table needs to be specified'\n return ret\n\n if not chain:\n ret['comment'] = 'Chain needs to be specified'\n return ret\n\n if not command:\n ret['comment'] = 'Command needs to be specified'\n return ret\n\n if command in ['Insert', 'insert', 'INSERT']:\n if position:\n ret['rule'] = '{0} insert rule {1} {2} {3} ' \\\n 'position {4} {5}'.format(_nftables_cmd(),\n nft_family,\n table,\n chain,\n position,\n rule)\n else:\n ret['rule'] = '{0} insert rule ' \\\n '{1} {2} {3} {4}'.format(_nftables_cmd(),\n nft_family,\n table,\n chain,\n rule)\n else:\n ret['rule'] = '{0} {1} rule {2} {3} {4} {5}'.format(_nftables_cmd(),\n command,\n nft_family,\n table,\n chain,\n rule)\n\n if ret['rule']:\n ret['comment'] = 'Successfully built rule'\n ret['result'] = True\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a data structure of the rules in the conf file", "response": "def get_saved_rules(conf_file=None):\n '''\n Return a data structure of the rules in the conf file\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' nftables.get_saved_rules\n\n '''\n if _conf() and not conf_file:\n conf_file = _conf()\n\n with salt.utils.files.fopen(conf_file) as fp_:\n lines = salt.utils.data.decode(fp_.readlines())\n rules = []\n for line in lines:\n tmpline = line.strip()\n if not tmpline:\n continue\n if tmpline.startswith('#'):\n continue\n rules.append(line)\n return rules"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_rules(family='ipv4'):\n '''\n Return a data structure of the current, in-memory rules\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' nftables.get_rules\n\n salt '*' nftables.get_rules family=ipv6\n\n '''\n nft_family = _NFTABLES_FAMILIES[family]\n rules = []\n cmd = '{0} --numeric --numeric --numeric ' \\\n 'list tables {1}'. format(_nftables_cmd(),\n nft_family)\n out = __salt__['cmd.run'](cmd, python_shell=False)\n if not out:\n return rules\n\n tables = re.split('\\n+', out)\n for table in tables:\n table_name = table.split(' ')[1]\n cmd = '{0} --numeric --numeric --numeric ' \\\n 'list table {1} {2}'.format(_nftables_cmd(),\n nft_family, table_name)\n out = __salt__['cmd.run'](cmd, python_shell=False)\n rules.append(out)\n return rules", "response": "Return a data structure of the current in - memory rules"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsave the current in - memory rules to disk", "response": "def save(filename=None, family='ipv4'):\n '''\n Save the current in-memory rules to disk\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' nftables.save /etc/nftables\n '''\n if _conf() and not filename:\n filename = _conf()\n\n nft_families = ['ip', 'ip6', 'arp', 'bridge']\n rules = \"#! nft -f\\n\"\n for family in nft_families:\n out = get_rules(family)\n if out:\n rules += '\\n'\n rules = rules + '\\n'.join(out)\n rules = rules + '\\n'\n\n try:\n with salt.utils.files.fopen(filename, 'wb') as _fh:\n # Write out any changes\n _fh.writelines(salt.utils.data.encode(rules))\n except (IOError, OSError) as exc:\n raise CommandExecutionError(\n 'Problem writing to configuration file: {0}'.format(exc)\n )\n return rules"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets the handle for a particular rule in a specific chain", "response": "def get_rule_handle(table='filter', chain=None, rule=None, family='ipv4'):\n '''\n Get the handle for a particular rule\n\n This function accepts a rule in a standard nftables command format,\n starting with the chain. Trying to force users to adapt to a new\n method of creating rules would be irritating at best, and we\n already have a parser that can handle it.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' nftables.get_rule_handle filter input \\\\\n rule='tcp dport 22 log accept'\n\n IPv6:\n salt '*' nftables.get_rule_handle filter input \\\\\n rule='tcp dport 22 log accept' \\\\\n family=ipv6\n '''\n ret = {'comment': '',\n 'result': False}\n\n if not chain:\n ret['comment'] = 'Chain needs to be specified'\n return ret\n\n if not rule:\n ret['comment'] = 'Rule needs to be specified'\n return ret\n\n res = check_table(table, family=family)\n if not res['result']:\n return res\n\n res = check_chain(table, chain, family=family)\n if not res['result']:\n return res\n\n res = check(table, chain, rule, family=family)\n if not res['result']:\n return res\n\n nft_family = _NFTABLES_FAMILIES[family]\n cmd = '{0} --numeric --numeric --numeric --handle list chain {1} {2} {3}'.\\\n format(_nftables_cmd(), nft_family, table, chain)\n out = __salt__['cmd.run'](cmd, python_shell=False)\n rules = re.split('\\n+', out)\n\n pat = re.compile(r'{0} # handle (?P<handle>\\d+)'.format(rule))\n for r in rules:\n match = pat.search(r)\n if match:\n return {'result': True, 'handle': match.group('handle')}\n return {'result': False,\n 'comment': 'Could not find rule {0}'.format(rule)}"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks for the existence of a rule in a table and chain.", "response": "def check(table='filter', chain=None, rule=None, family='ipv4'):\n '''\n Check for the existence of a rule in the table and chain\n\n This function accepts a rule in a standard nftables command format,\n starting with the chain. Trying to force users to adapt to a new\n method of creating rules would be irritating at best, and we\n already have a parser that can handle it.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' nftables.check filter input \\\\\n rule='tcp dport 22 log accept'\n\n IPv6:\n salt '*' nftables.check filter input \\\\\n rule='tcp dport 22 log accept' \\\\\n family=ipv6\n '''\n ret = {'comment': '',\n 'result': False}\n\n if not chain:\n ret['comment'] = 'Chain needs to be specified'\n return ret\n\n if not rule:\n ret['comment'] = 'Rule needs to be specified'\n return ret\n\n res = check_table(table, family=family)\n if not res['result']:\n return res\n\n res = check_chain(table, chain, family=family)\n if not res['result']:\n return res\n\n nft_family = _NFTABLES_FAMILIES[family]\n cmd = '{0} --handle --numeric --numeric --numeric list chain {1} {2} {3}'.\\\n format(_nftables_cmd(), nft_family, table, chain)\n search_rule = '{0} #'.format(rule)\n out = __salt__['cmd.run'](cmd, python_shell=False).find(search_rule)\n\n if out == -1:\n ret['comment'] = 'Rule {0} in chain {1} in table {2} in family {3} does not exist'.\\\n format(rule, chain, table, family)\n else:\n ret['comment'] = 'Rule {0} in chain {1} in table {2} in family {3} exists'.\\\n format(rule, chain, table, family)\n ret['result'] = True\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef check_table(table=None, family='ipv4'):\n '''\n Check for the existence of a table\n\n CLI Example::\n\n salt '*' nftables.check_table nat\n '''\n ret = {'comment': '',\n 'result': False}\n\n if not table:\n ret['comment'] = 'Table needs to be specified'\n return ret\n\n nft_family = _NFTABLES_FAMILIES[family]\n cmd = '{0} list tables {1}' . format(_nftables_cmd(), nft_family)\n out = __salt__['cmd.run'](cmd, python_shell=False).find('table {0} {1}'.format(nft_family, table))\n\n if out == -1:\n ret['comment'] = 'Table {0} in family {1} does not exist'.\\\n format(table, family)\n else:\n ret['comment'] = 'Table {0} in family {1} exists'.\\\n format(table, family)\n ret['result'] = True\n return ret", "response": "Check for the existence of a table in a specific family CLI Example :: nftables. check_table nat\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a new chain in the specified table.", "response": "def new_chain(table='filter', chain=None, table_type=None, hook=None, priority=None, family='ipv4'):\n '''\n .. versionadded:: 2014.7.0\n\n Create new chain to the specified table.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' nftables.new_chain filter input\n\n salt '*' nftables.new_chain filter input \\\\\n table_type=filter hook=input priority=0\n\n salt '*' nftables.new_chain filter foo\n\n IPv6:\n salt '*' nftables.new_chain filter input family=ipv6\n\n salt '*' nftables.new_chain filter input \\\\\n table_type=filter hook=input priority=0 family=ipv6\n\n salt '*' nftables.new_chain filter foo family=ipv6\n '''\n ret = {'comment': '',\n 'result': False}\n\n if not chain:\n ret['comment'] = 'Chain needs to be specified'\n return ret\n\n res = check_table(table, family=family)\n if not res['result']:\n return res\n\n res = check_chain(table, chain, family=family)\n if res['result']:\n ret['comment'] = 'Chain {0} in table {1} in family {2} already exists'.\\\n format(chain, table, family)\n return ret\n\n nft_family = _NFTABLES_FAMILIES[family]\n cmd = '{0} add chain {1} {2} {3}'.\\\n format(_nftables_cmd(), nft_family, table, chain)\n if table_type or hook or priority:\n if table_type and hook and six.text_type(priority):\n cmd = r'{0} \\{{ type {1} hook {2} priority {3}\\; \\}}'.\\\n format(cmd, table_type, hook, priority)\n else:\n # Specify one, require all\n ret['comment'] = 'Table_type, hook, and priority required.'\n return ret\n\n out = __salt__['cmd.run'](cmd, python_shell=False)\n\n if not out:\n ret['comment'] = 'Chain {0} in table {1} in family {2} created'.\\\n format(chain, table, family)\n ret['result'] = True\n else:\n ret['comment'] = 'Chain {0} in table {1} in family {2} could not be created'.\\\n format(chain, table, family)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nflush all rules in the specified chain in the specified table.", "response": "def flush(table='filter', chain='', family='ipv4'):\n '''\n Flush the chain in the specified table, flush all chains in the specified\n table if chain is not specified.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' nftables.flush filter\n\n salt '*' nftables.flush filter input\n\n IPv6:\n salt '*' nftables.flush filter input family=ipv6\n '''\n ret = {'comment': 'Failed to flush rules from chain {0} in table {1}.'.format(chain, table),\n 'result': False}\n\n res = check_table(table, family=family)\n if not res['result']:\n return res\n\n nft_family = _NFTABLES_FAMILIES[family]\n\n if chain:\n res = check_chain(table, chain, family=family)\n if not res['result']:\n return res\n cmd = '{0} flush chain {1} {2} {3}'.\\\n format(_nftables_cmd(), nft_family, table, chain)\n comment = 'from chain {0} in table {1} in family {2}.'.\\\n format(chain, table, family)\n else:\n cmd = '{0} flush table {1} {2}'.\\\n format(_nftables_cmd(), nft_family, table)\n comment = 'from table {0} in family {1}.'.\\\n format(table, family)\n out = __salt__['cmd.run'](cmd, python_shell=False)\n\n if len(out) == 0:\n ret['result'] = True\n ret['comment'] = 'Flushed rules {0}'.format(comment)\n else:\n ret['comment'] = 'Failed to flush rules {0}'.format(comment)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef rewrite_single_shorthand_state_decl(data): # pylint: disable=C0103\n '''\n Rewrite all state declarations that look like this::\n\n state_id_decl:\n state.func\n\n into::\n\n state_id_decl:\n state.func: []\n '''\n for sid, states in six.iteritems(data):\n if isinstance(states, six.string_types):\n data[sid] = {states: []}", "response": "Rewrite all state declarations that look like this state_id_decl into state_id_decl. func."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _relative_to_abs_sls(relative, sls):\n '''\n Convert ``relative`` sls reference into absolute, relative to ``sls``.\n '''\n levels, suffix = re.match(r'^(\\.+)(.*)$', relative).groups()\n level_count = len(levels)\n p_comps = sls.split('.')\n if level_count > len(p_comps):\n raise SaltRenderError(\n 'Attempted relative include goes beyond top level package'\n )\n return '.'.join(p_comps[:-level_count] + [suffix])", "response": "Convert relative sls reference into absolute relative to sls."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nyielding the tuple of tuples for each returned value in the list.", "response": "def nvlist2(thelist, names=None):\n '''\n Like nvlist but applied one more time to each returned value.\n So, given a list, args, of arguments to a state like this::\n\n - name: echo test\n - cwd: /\n - require:\n - file: test.sh\n\n nvlist2(args, ['require']) would yield the tuple,\n (dict_item, 'file', 'test.sh') where dict_item is the single-key\n dictionary of {'file': 'test.sh'}.\n\n '''\n for _, _, value in nvlist(thelist, names):\n for each in nvlist(value):\n yield each"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_api_params(api_url=None,\n page_id=None,\n api_key=None,\n api_version=None):\n '''\n Retrieve the API params from the config file.\n '''\n statuspage_cfg = __salt__['config.get']('statuspage')\n if not statuspage_cfg:\n statuspage_cfg = {}\n return {\n 'api_url': api_url or statuspage_cfg.get('api_url') or BASE_URL, # optional\n 'api_page_id': page_id or statuspage_cfg.get('page_id'), # mandatory\n 'api_key': api_key or statuspage_cfg.get('api_key'), # mandatory\n 'api_version': api_version or statuspage_cfg.get('api_version') or DEFAULT_VERSION\n }", "response": "Retrieve the API params from the config file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _validate_api_params(params):\n '''\n Validate the API params as specified in the config file.\n '''\n # page_id and API key are mandatory and they must be string/unicode\n return (isinstance(params['api_page_id'], (six.string_types, six.text_type)) and\n isinstance(params['api_key'], (six.string_types, six.text_type)))", "response": "Validate the API params as specified in the config file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nmaking the HTTP request and return the body as python object.", "response": "def _http_request(url,\n method='GET',\n headers=None,\n data=None):\n '''\n Make the HTTP request and return the body as python object.\n '''\n req = requests.request(method,\n url,\n headers=headers,\n data=data)\n ret = _default_ret()\n ok_status = METHOD_OK_STATUS.get(method, 200)\n if req.status_code != ok_status:\n ret.update({\n 'comment': req.json().get('error', '')\n })\n return ret\n ret.update({\n 'result': True,\n 'out': req.json() if method != 'DELETE' else None # no body when DELETE\n })\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef retrieve(endpoint='incidents',\n api_url=None,\n page_id=None,\n api_key=None,\n api_version=None):\n '''\n Retrieve a specific endpoint from the Statuspage API.\n\n endpoint: incidents\n Request a specific endpoint.\n\n page_id\n Page ID. Can also be specified in the config file.\n\n api_key\n API key. Can also be specified in the config file.\n\n api_version: 1\n API version. Can also be specified in the config file.\n\n api_url\n Custom API URL in case the user has a StatusPage service running in a custom environment.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion' statuspage.retrieve components\n\n Example output:\n\n .. code-block:: bash\n\n minion:\n ----------\n comment:\n out:\n |_\n ----------\n backfilled:\n False\n created_at:\n 2015-01-26T20:25:02.702Z\n id:\n kh2qwjbheqdc36\n impact:\n major\n impact_override:\n None\n incident_updates:\n |_\n ----------\n affected_components:\n None\n body:\n We are currently investigating this issue.\n created_at:\n 2015-01-26T20:25:02.849Z\n display_at:\n 2015-01-26T20:25:02.849Z\n id:\n zvx7xz2z5skr\n incident_id:\n kh2qwjbheqdc36\n status:\n investigating\n twitter_updated_at:\n None\n updated_at:\n 2015-01-26T20:25:02.849Z\n wants_twitter_update:\n False\n monitoring_at:\n None\n name:\n just testing some stuff\n page_id:\n ksdhgfyiuhaa\n postmortem_body:\n None\n postmortem_body_last_updated_at:\n None\n postmortem_ignored:\n False\n postmortem_notified_subscribers:\n False\n postmortem_notified_twitter:\n False\n postmortem_published_at:\n None\n resolved_at:\n None\n scheduled_auto_completed:\n False\n scheduled_auto_in_progress:\n False\n scheduled_for:\n None\n scheduled_remind_prior:\n False\n scheduled_reminded_at:\n None\n scheduled_until:\n None\n shortlink:\n http://stspg.io/voY\n status:\n investigating\n updated_at:\n 2015-01-26T20:25:13.379Z\n result:\n True\n '''\n params = _get_api_params(api_url=api_url,\n page_id=page_id,\n api_key=api_key,\n api_version=api_version)\n if not _validate_api_params(params):\n log.error('Invalid API params.')\n log.error(params)\n return {\n 'result': False,\n 'comment': 'Invalid API params. See log for details'\n }\n headers = _get_headers(params)\n retrieve_url = '{base_url}/v{version}/pages/{page_id}/{endpoint}.json'.format(\n base_url=params['api_url'],\n version=params['api_version'],\n page_id=params['api_page_id'],\n endpoint=endpoint\n )\n return _http_request(retrieve_url,\n headers=headers)", "response": "This function returns a list of components that are available to the Statuspage API."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef update(endpoint='incidents',\n id=None,\n api_url=None,\n page_id=None,\n api_key=None,\n api_version=None,\n **kwargs):\n '''\n Update attribute(s) of a specific endpoint.\n\n id\n The unique ID of the enpoint entry.\n\n endpoint: incidents\n Endpoint name.\n\n page_id\n Page ID. Can also be specified in the config file.\n\n api_key\n API key. Can also be specified in the config file.\n\n api_version: 1\n API version. Can also be specified in the config file.\n\n api_url\n Custom API URL in case the user has a StatusPage service running in a custom environment.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion' statuspage.update id=dz959yz2nd4l status=resolved\n\n Example output:\n\n .. code-block:: bash\n\n minion:\n ----------\n comment:\n out:\n ----------\n created_at:\n 2017-01-03T15:25:30.718Z\n description:\n None\n group_id:\n 993vgplshj12\n id:\n dz959yz2nd4l\n name:\n Management Portal\n page_id:\n xzwjjdw87vpf\n position:\n 11\n status:\n resolved\n updated_at:\n 2017-01-05T15:34:27.676Z\n result:\n True\n '''\n endpoint_sg = endpoint[:-1] # singular\n if not id:\n log.error('Invalid %s ID', endpoint_sg)\n return {\n 'result': False,\n 'comment': 'Please specify a valid {endpoint} ID'.format(endpoint=endpoint_sg)\n }\n params = _get_api_params(api_url=api_url,\n page_id=page_id,\n api_key=api_key,\n api_version=api_version)\n if not _validate_api_params(params):\n log.error('Invalid API params.')\n log.error(params)\n return {\n 'result': False,\n 'comment': 'Invalid API params. See log for details'\n }\n headers = _get_headers(params)\n update_url = '{base_url}/v{version}/pages/{page_id}/{endpoint}/{id}.json'.format(\n base_url=params['api_url'],\n version=params['api_version'],\n page_id=params['api_page_id'],\n endpoint=endpoint,\n id=id\n )\n change_request = {}\n for karg, warg in six.iteritems(kwargs):\n if warg is None or karg.startswith('__') or karg in UPDATE_FORBIDDEN_FILEDS:\n continue\n change_request_key = '{endpoint_sg}[{karg}]'.format(\n endpoint_sg=endpoint_sg,\n karg=karg\n )\n change_request[change_request_key] = warg\n return _http_request(update_url,\n method='PATCH',\n headers=headers,\n data=change_request)", "response": "Update an enpoint entry."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndeleting an entry from an endpoint.", "response": "def delete(endpoint='incidents',\n id=None,\n api_url=None,\n page_id=None,\n api_key=None,\n api_version=None):\n '''\n Remove an entry from an endpoint.\n\n endpoint: incidents\n Request a specific endpoint.\n\n page_id\n Page ID. Can also be specified in the config file.\n\n api_key\n API key. Can also be specified in the config file.\n\n api_version: 1\n API version. Can also be specified in the config file.\n\n api_url\n Custom API URL in case the user has a StatusPage service running in a custom environment.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion' statuspage.delete endpoint='components' id='ftgks51sfs2d'\n\n Example output:\n\n .. code-block:: bash\n\n minion:\n ----------\n comment:\n out:\n None\n result:\n True\n '''\n params = _get_api_params(api_url=api_url,\n page_id=page_id,\n api_key=api_key,\n api_version=api_version)\n if not _validate_api_params(params):\n log.error('Invalid API params.')\n log.error(params)\n return {\n 'result': False,\n 'comment': 'Invalid API params. See log for details'\n }\n endpoint_sg = endpoint[:-1] # singular\n if not id:\n log.error('Invalid %s ID', endpoint_sg)\n return {\n 'result': False,\n 'comment': 'Please specify a valid {endpoint} ID'.format(endpoint=endpoint_sg)\n }\n headers = _get_headers(params)\n delete_url = '{base_url}/v{version}/pages/{page_id}/{endpoint}/{id}.json'.format(\n base_url=params['api_url'],\n version=params['api_version'],\n page_id=params['api_page_id'],\n endpoint=endpoint,\n id=id\n )\n return _http_request(delete_url,\n method='DELETE',\n headers=headers)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_repo_options(fromrepo=None, packagesite=None):\n '''\n Return a list of tuples to seed the \"env\" list, which is used to set\n environment variables for any pkg_add commands that are spawned.\n\n If ``fromrepo`` or ``packagesite`` are None, then their corresponding\n config parameter will be looked up with config.get.\n\n If both ``fromrepo`` and ``packagesite`` are None, and neither\n freebsdpkg.PACKAGEROOT nor freebsdpkg.PACKAGESITE are specified, then an\n empty list is returned, and it is assumed that the system defaults (or\n environment variables) will be used.\n '''\n root = fromrepo if fromrepo is not None \\\n else __salt__['config.get']('freebsdpkg.PACKAGEROOT', None)\n site = packagesite if packagesite is not None \\\n else __salt__['config.get']('freebsdpkg.PACKAGESITE', None)\n ret = {}\n if root is not None:\n ret['PACKAGEROOT'] = root\n if site is not None:\n ret['PACKAGESITE'] = site\n return ret", "response": "Return a list of tuples to seed the env list for pkg_add commands that are spawned by pkg_add commands."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _match(names):\n '''\n Since pkg_delete requires the full \"pkgname-version\" string, this function\n will attempt to match the package name with its version. Returns a list of\n partial matches and package names that match the \"pkgname-version\" string\n required by pkg_delete, and a list of errors encountered.\n '''\n pkgs = list_pkgs(versions_as_list=True)\n errors = []\n\n # Look for full matches\n full_pkg_strings = []\n out = __salt__['cmd.run_stdout'](['pkg_info'],\n output_loglevel='trace',\n python_shell=False)\n for line in out.splitlines():\n try:\n full_pkg_strings.append(line.split()[0])\n except IndexError:\n continue\n full_matches = [x for x in names if x in full_pkg_strings]\n\n # Look for pkgname-only matches\n matches = []\n ambiguous = []\n for name in set(names) - set(full_matches):\n cver = pkgs.get(name)\n if cver is not None:\n if len(cver) == 1:\n matches.append('{0}-{1}'.format(name, cver[0]))\n else:\n ambiguous.append(name)\n errors.append(\n 'Ambiguous package \\'{0}\\'. Full name/version required. '\n 'Possible matches: {1}'.format(\n name,\n ', '.join(['{0}-{1}'.format(name, x) for x in cver])\n )\n )\n\n # Find packages that did not match anything\n not_matched = \\\n set(names) - set(matches) - set(full_matches) - set(ambiguous)\n for name in not_matched:\n errors.append('Package \\'{0}\\' not found'.format(name))\n\n return matches + full_matches, errors", "response": "Returns a list of partial matches and package names that match the pkgname - version string."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nlisting the currently installed packages.", "response": "def list_pkgs(versions_as_list=False, with_origin=False, **kwargs):\n '''\n List the packages currently installed as a dict::\n\n {'<package_name>': '<version>'}\n\n with_origin : False\n Return a nested dictionary containing both the origin name and version\n for each installed package.\n\n .. versionadded:: 2014.1.0\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 ret = copy.deepcopy(__context__['pkg.list_pkgs'])\n if not versions_as_list:\n __salt__['pkg_resource.stringify'](ret)\n if salt.utils.data.is_true(with_origin):\n origins = __context__.get('pkg.origin', {})\n return dict([\n (x, {'origin': origins.get(x, ''), 'version': y})\n for x, y in six.iteritems(ret)\n ])\n return ret\n\n ret = {}\n origins = {}\n out = __salt__['cmd.run_stdout'](['pkg_info', '-ao'],\n output_loglevel='trace',\n python_shell=False)\n pkgs_re = re.compile(r'Information for ([^:]+):\\s*Origin:\\n([^\\n]+)')\n for pkg, origin in pkgs_re.findall(out):\n if not pkg:\n continue\n try:\n pkgname, pkgver = pkg.rsplit('-', 1)\n except ValueError:\n continue\n __salt__['pkg_resource.add_pkg'](ret, pkgname, pkgver)\n origins[pkgname] = origin\n\n __salt__['pkg_resource.sort_pkglist'](ret)\n __context__['pkg.list_pkgs'] = copy.deepcopy(ret)\n __context__['pkg.origin'] = origins\n if not versions_as_list:\n __salt__['pkg_resource.stringify'](ret)\n if salt.utils.data.is_true(with_origin):\n return dict([\n (x, {'origin': origins.get(x, ''), 'version': y})\n for x, y in six.iteritems(ret)\n ])\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef install(name=None,\n refresh=False,\n fromrepo=None,\n pkgs=None,\n sources=None,\n **kwargs):\n '''\n Install package(s) using ``pkg_add(1)``\n\n name\n The name of the package to be installed.\n\n refresh\n Whether or not to refresh the package database before installing.\n\n fromrepo or packageroot\n Specify a package repository from which to install. Overrides the\n system default, as well as the PACKAGEROOT environment variable.\n\n packagesite\n Specify the exact directory from which to install the remote package.\n Overrides the PACKAGESITE environment variable, if present.\n\n\n Multiple Package Installation Options:\n\n pkgs\n A list of packages to install from a software repository. Must be\n passed as a python list.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install pkgs='[\"foo\", \"bar\"]'\n\n sources\n A list of packages to install. Must be passed as a list of dicts,\n with the keys being package names, and the values being the source URI\n or local path to the package.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install sources='[{\"foo\": \"salt://foo.deb\"}, {\"bar\": \"salt://bar.deb\"}]'\n\n Return a dict containing the new package names and versions::\n\n {'<package>': {'old': '<old-version>',\n 'new': '<new-version>'}}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install <package name>\n '''\n try:\n pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](\n name, pkgs, sources, **kwargs\n )\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n if not pkg_params:\n return {}\n\n packageroot = kwargs.get('packageroot')\n if not fromrepo and packageroot:\n fromrepo = packageroot\n\n env = _get_repo_options(fromrepo, kwargs.get('packagesite'))\n args = []\n\n if pkg_type == 'repository':\n args.append('-r') # use remote repo\n\n args.extend(pkg_params)\n\n old = list_pkgs()\n out = __salt__['cmd.run_all'](\n ['pkg_add'] + args,\n env=env,\n output_loglevel='trace',\n python_shell=False\n )\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 _rehash()\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": "Installs a single package from a software repository."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nremoves packages from the current node.", "response": "def remove(name=None, pkgs=None, **kwargs):\n '''\n Remove packages using ``pkg_delete(1)``\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 <package name>\n salt '*' pkg.remove <package1>,<package2>,<package3>\n salt '*' pkg.remove pkgs='[\"foo\", \"bar\"]'\n '''\n try:\n pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n old = list_pkgs()\n targets, errors = _match([x for x in pkg_params])\n for error in errors:\n log.error(error)\n if not targets:\n return {}\n\n out = __salt__['cmd.run_all'](\n ['pkg_delete'] + targets,\n output_loglevel='trace',\n python_shell=False\n )\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"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a dict of all files in the system that belong to a package.", "response": "def file_dict(*packages, **kwargs):\n '''\n List the files that belong to a package, grouped by package. Not\n specifying any packages will return a list of _every_ file on the\n system's package database (not generally recommended).\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.file_list httpd\n salt '*' pkg.file_list httpd postfix\n salt '*' pkg.file_list\n '''\n errors = []\n files = {}\n\n if packages:\n match_pattern = '\\'{0}-[0-9]*\\''\n cmd = ['pkg_info', '-QL'] + [match_pattern.format(p) for p in packages]\n else:\n cmd = ['pkg_info', '-QLa']\n\n ret = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n\n for line in ret['stderr'].splitlines():\n errors.append(line)\n\n pkg = None\n for line in ret['stdout'].splitlines():\n if pkg is not None and line.startswith('/'):\n files[pkg].append(line)\n elif ':/' in line:\n pkg, fn = line.split(':', 1)\n pkg, ver = pkg.rsplit('-', 1)\n files[pkg] = [fn]\n else:\n continue # unexpected string\n\n return {'errors': errors, 'files': files}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef namespaced_function(function, global_dict, defaults=None, preserve_context=False):\n '''\n Redefine (clone) a function under a different globals() namespace scope\n\n preserve_context:\n Allow keeping the context taken from orignal namespace,\n and extend it with globals() taken from\n new targetted namespace.\n '''\n if defaults is None:\n defaults = function.__defaults__\n\n if preserve_context:\n _global_dict = function.__globals__.copy()\n _global_dict.update(global_dict)\n global_dict = _global_dict\n new_namespaced_function = types.FunctionType(\n function.__code__,\n global_dict,\n name=function.__name__,\n argdefs=defaults,\n closure=function.__closure__\n )\n new_namespaced_function.__dict__.update(function.__dict__)\n return new_namespaced_function", "response": "Returns a new function that is namespaced by the given function."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef alias_function(fun, name, doc=None):\n '''\n Copy a function\n '''\n alias_fun = types.FunctionType(fun.__code__,\n fun.__globals__,\n str(name), # future lint: disable=blacklisted-function\n fun.__defaults__,\n fun.__closure__)\n alias_fun.__dict__.update(fun.__dict__)\n\n if doc and isinstance(doc, six.string_types):\n alias_fun.__doc__ = doc\n else:\n orig_name = fun.__name__\n alias_msg = ('\\nThis function is an alias of '\n '``{0}``.\\n'.format(orig_name))\n alias_fun.__doc__ = alias_msg + (fun.__doc__ or '')\n\n return alias_fun", "response": "Returns a copy of a function that is an alias of the given function."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures that the specified DNS server list exists in the specified interface.", "response": "def dns_exists(name, servers=None, interface='Local Area Connection', replace=False):\n '''\n Configure the DNS server list in the specified interface\n\n Example:\n\n .. code-block:: yaml\n\n config_dns_servers:\n win_dns_client.dns_exists:\n - replace: True #remove any servers not in the \"servers\" list, default is False\n - servers:\n - 8.8.8.8\n - 8.8.8.9\n '''\n ret = {'name': name,\n 'result': True,\n 'changes': {'Servers Reordered': [], 'Servers Added': [], 'Servers Removed': []},\n 'comment': ''}\n\n if __opts__['test']:\n ret['comment'] = 'DNS Servers are set to be updated'\n ret['result'] = None\n else:\n ret['comment'] = 'DNS Servers have been updated'\n\n # Validate syntax\n if not isinstance(servers, list):\n ret['result'] = False\n ret['comment'] = 'servers entry is not a list !'\n return ret\n\n # Do nothing is already configured\n configured_list = __salt__['win_dns_client.get_dns_servers'](interface)\n if configured_list == servers:\n ret['comment'] = '{0} are already configured'.format(servers)\n ret['changes'] = {}\n ret['result'] = True\n return ret\n\n # add the DNS servers\n for i, server in enumerate(servers):\n if __opts__['test']:\n if server in configured_list:\n if configured_list.index(server) != i:\n ret['changes']['Servers Reordered'].append(server)\n else:\n ret['changes']['Servers Added'].append(server)\n else:\n if not __salt__['win_dns_client.add_dns'](server, interface, i + 1):\n ret['comment'] = (\n 'Failed to add {0} as DNS server number {1}'\n ).format(server, i + 1)\n ret['result'] = False\n ret['changes'] = {}\n return ret\n else:\n if server in configured_list:\n if configured_list.index(server) != i:\n ret['changes']['Servers Reordered'].append(server)\n else:\n ret['changes']['Servers Added'].append(server)\n\n # remove dns servers that weren't in our list\n if replace:\n for i, server in enumerate(configured_list):\n if server not in servers:\n if __opts__['test']:\n ret['changes']['Servers Removed'].append(server)\n else:\n if not __salt__['win_dns_client.rm_dns'](server, interface):\n ret['comment'] = (\n 'Failed to remove {0} from DNS server list').format(server)\n ret['result'] = False\n return ret\n else:\n ret['changes']['Servers Removed'].append(server)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef dns_dhcp(name, interface='Local Area Connection'):\n '''\n Configure the DNS server list from DHCP Server\n '''\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n\n # Check the config\n config = __salt__['win_dns_client.get_dns_config'](interface)\n if config == 'dhcp':\n ret['comment'] = '{0} already configured with DNS from DHCP'.format(\n interface)\n return ret\n else:\n ret['changes'] = {'dns': 'configured from DHCP'}\n\n if __opts__['test']:\n ret['result'] = None\n return ret\n\n # change the configuration\n ret['result'] = __salt__['win_dns_client.dns_dhcp'](interface)\n if not ret['result']:\n ret['changes'] = {}\n ret['comment'] = (\n 'Could not configure \"{0}\" DNS servers from DHCP'\n ).format(interface)\n\n return ret", "response": "Configure the DNS server list from DHCP Server\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef primary_suffix(name,\n suffix=None,\n updates=False):\n '''\n .. versionadded:: 2014.7.0\n\n Configure the global primary DNS suffix of a DHCP client.\n\n suffix : None\n The suffix which is advertised for this client when acquiring a DHCP lease\n When none is set, the explicitly configured DNS suffix will be removed.\n\n updates : False\n Allow syncing the DNS suffix with the AD domain when the client's AD domain membership changes\n\n .. code-block:: yaml\n\n primary_dns_suffix:\n win_dns_client.primary_suffix:\n - suffix: sub.domain.tld\n - updates: True\n '''\n\n ret = {\n 'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'No changes needed'\n }\n\n suffix = str(suffix)\n\n if not isinstance(updates, bool):\n ret['result'] = False\n ret['comment'] = '\\'updates\\' must be a boolean value'\n return ret\n\n # TODO: waiting for an implementation of\n # https://github.com/saltstack/salt/issues/6792 to be able to handle the\n # requirement for a reboot to actually apply this state.\n # Until then, this method will only be able to verify that the required\n # value has been written to the registry and rebooting needs to be handled\n # manually\n\n reg_data = {\n 'suffix': {\n 'hive': 'HKEY_LOCAL_MACHINE',\n 'key': r'SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters',\n 'vname': 'NV Domain',\n 'vtype': 'REG_SZ',\n 'old': None,\n 'new': suffix\n },\n 'updates': {\n 'hive': 'HKEY_LOCAL_MACHINE',\n 'key': r'SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters',\n 'vname': 'SyncDomainWithMembership',\n 'vtype': 'REG_DWORD',\n 'old': None,\n 'new': updates\n }\n }\n\n reg_data['suffix']['old'] = __utils__['reg.read_value'](\n reg_data['suffix']['hive'],\n reg_data['suffix']['key'],\n reg_data['suffix']['vname'],)['vdata']\n\n reg_data['updates']['old'] = bool(__utils__['reg.read_value'](\n reg_data['updates']['hive'],\n reg_data['updates']['key'],\n reg_data['updates']['vname'],)['vdata'])\n\n updates_operation = 'enabled' if reg_data['updates']['new'] else 'disabled'\n\n # No changes to suffix needed\n if reg_data['suffix']['new'] == reg_data['suffix']['old']:\n # No changes to updates policy needed\n if reg_data['updates']['new'] == reg_data['updates']['old']:\n return ret\n # Changes to update policy needed\n else:\n ret['comment'] = '{0} suffix updates'.format(updates_operation)\n ret['changes'] = {\n 'old': {\n 'updates': reg_data['updates']['old']},\n 'new': {\n 'updates': reg_data['updates']['new']}}\n # Changes to suffix needed\n else:\n # Changes to updates policy needed\n if reg_data['updates']['new'] != reg_data['updates']['old']:\n ret['comment'] = 'Updated primary DNS suffix ({0}) and {1} suffix updates'.format(suffix, updates_operation)\n ret['changes'] = {\n 'old': {\n 'suffix': reg_data['suffix']['old'],\n 'updates': reg_data['updates']['old']},\n 'new': {\n 'suffix': reg_data['suffix']['new'],\n 'updates': reg_data['updates']['new']}}\n # No changes to updates policy needed\n else:\n ret['comment'] = 'Updated primary DNS suffix ({0})'.format(suffix)\n ret['changes'] = {\n 'old': {\n 'suffix': reg_data['suffix']['old']},\n 'new': {\n 'suffix': reg_data['suffix']['new']}}\n\n suffix_result = __utils__['reg.set_value'](\n reg_data['suffix']['hive'],\n reg_data['suffix']['key'],\n reg_data['suffix']['vname'],\n reg_data['suffix']['new'],\n reg_data['suffix']['vtype'])\n\n updates_result = __utils__['reg.set_value'](\n reg_data['updates']['hive'],\n reg_data['updates']['key'],\n reg_data['updates']['vname'],\n reg_data['updates']['new'],\n reg_data['updates']['vtype'])\n\n ret['result'] = suffix_result & updates_result\n\n return ret", "response": "Configure the global primary DNS suffix of a DHCP client."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ninsert a new rule into a chain.", "response": "def insert(name, table='filter', family='ipv4', **kwargs):\n '''\n .. versionadded:: 2014.1.0\n\n Insert a rule into a chain\n\n name\n A user-defined name to call this rule by in another part of a state or\n formula. This should not be an actual rule.\n\n table\n The table that owns the chain that should be modified\n\n family\n Networking family, either ipv4 or ipv6\n\n position\n The numerical representation of where the rule should be inserted into\n the chain. Note that ``-1`` is not a supported position value.\n\n All other arguments are passed in with the same name as the long option\n that would normally be used for iptables, with one exception: ``--state`` is\n specified as `connstate` instead of `state` (not to be confused with\n `ctstate`).\n\n Jump options that doesn't take arguments should be passed in with an empty\n string.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if 'rules' in kwargs:\n ret['changes']['locale'] = []\n comments = []\n save = False\n for rule in kwargs['rules']:\n if 'rules' in rule:\n del rule['rules']\n if '__agg__' in rule:\n del rule['__agg__']\n if 'save' in rule and rule['save']:\n save = True\n if rule['save'] is not True:\n save_file = rule['save']\n else:\n save_file = True\n rule['save'] = False\n _ret = insert(**rule)\n if 'locale' in _ret['changes']:\n ret['changes']['locale'].append(_ret['changes']['locale'])\n comments.append(_ret['comment'])\n ret['result'] = _ret['result']\n if save:\n if save_file is True:\n save_file = None\n __salt__['iptables.save'](save_file, family=family)\n if not ret['changes']['locale']:\n del ret['changes']['locale']\n ret['comment'] = '\\n'.join(comments)\n return ret\n\n for ignore in _STATE_INTERNAL_KEYWORDS:\n if ignore in kwargs:\n del kwargs[ignore]\n kwargs['name'] = name\n kwargs['table'] = table\n rule = __salt__['iptables.build_rule'](family=family, **kwargs)\n command = __salt__['iptables.build_rule'](full=True, family=family, command='I', **kwargs)\n if __salt__['iptables.check'](table,\n kwargs['chain'],\n rule,\n family) is True:\n ret['result'] = True\n ret['comment'] = 'iptables rule for {0} already set for {1} ({2})'.format(\n name,\n family,\n command.strip())\n if 'save' in kwargs and kwargs['save']:\n if kwargs['save'] is not True:\n filename = kwargs['save']\n else:\n filename = None\n saved_rules = __salt__['iptables.get_saved_rules'](family=family)\n _rules = __salt__['iptables.get_rules'](family=family)\n __rules = []\n for table in _rules:\n for chain in _rules[table]:\n __rules.append(_rules[table][chain].get('rules'))\n __saved_rules = []\n for table in saved_rules:\n for chain in saved_rules[table]:\n __saved_rules.append(saved_rules[table][chain].get('rules'))\n # Only save if rules in memory are different than saved rules\n if __rules != __saved_rules:\n out = __salt__['iptables.save'](filename, family=family)\n ret['comment'] += ('\\nSaved iptables rule {0} for {1}\\n'\n '{2}\\n{3}').format(name, family, command.strip(), out)\n return ret\n if __opts__['test']:\n ret['comment'] = 'iptables rule for {0} needs to be set for {1} ({2})'.format(\n name,\n family,\n command.strip())\n return ret\n if not __salt__['iptables.insert'](table, kwargs['chain'], kwargs['position'], rule, family):\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = 'Set iptables rule for {0} to: {1} for {2}'.format(\n name,\n command.strip(),\n family)\n if 'save' in kwargs:\n if kwargs['save']:\n out = __salt__['iptables.save'](filename=None, family=family)\n ret['comment'] = ('Set and saved iptables rule {0} for {1}\\n'\n '{2}\\n{3}').format(name, family, command.strip(), out)\n return ret\n else:\n ret['result'] = False\n ret['comment'] = ('Failed to set iptables rule for {0}.\\n'\n 'Attempted rule was {1}').format(\n name,\n command.strip())\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete(name, table='filter', family='ipv4', **kwargs):\n '''\n .. versionadded:: 2014.1.0\n\n Delete a rule to a chain\n\n name\n A user-defined name to call this rule by in another part of a state or\n formula. This should not be an actual rule.\n\n table\n The table that owns the chain that should be modified\n\n family\n Networking family, either ipv4 or ipv6\n\n All other arguments are passed in with the same name as the long option\n that would normally be used for iptables, with one exception: ``--state`` is\n specified as `connstate` instead of `state` (not to be confused with\n `ctstate`).\n\n Jump options that doesn't take arguments should be passed in with an empty\n string.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if 'rules' in kwargs:\n ret['changes']['locale'] = []\n comments = []\n save = False\n for rule in kwargs['rules']:\n if 'rules' in rule:\n del rule['rules']\n if '__agg__' in rule:\n del rule['__agg__']\n if 'save' in rule and rule['save']:\n if rule['save'] is not True:\n save_file = rule['save']\n else:\n save_file = True\n rule['save'] = False\n _ret = delete(**rule)\n if 'locale' in _ret['changes']:\n ret['changes']['locale'].append(_ret['changes']['locale'])\n comments.append(_ret['comment'])\n ret['result'] = _ret['result']\n if save:\n if save_file is True:\n save_file = None\n __salt__['iptables.save'](save_file, family=family)\n if not ret['changes']['locale']:\n del ret['changes']['locale']\n ret['comment'] = '\\n'.join(comments)\n return ret\n\n for ignore in _STATE_INTERNAL_KEYWORDS:\n if ignore in kwargs:\n del kwargs[ignore]\n kwargs['name'] = name\n kwargs['table'] = table\n rule = __salt__['iptables.build_rule'](family=family, **kwargs)\n command = __salt__['iptables.build_rule'](full=True, family=family, command='D', **kwargs)\n\n if not __salt__['iptables.check'](table,\n kwargs['chain'],\n rule,\n family) is True:\n if 'position' not in kwargs:\n ret['result'] = True\n ret['comment'] = 'iptables rule for {0} already absent for {1} ({2})'.format(\n name,\n family,\n command.strip())\n return ret\n if __opts__['test']:\n ret['comment'] = 'iptables rule for {0} needs to be deleted for {1} ({2})'.format(\n name,\n family,\n command.strip())\n return ret\n\n if 'position' in kwargs:\n result = __salt__['iptables.delete'](\n table,\n kwargs['chain'],\n family=family,\n position=kwargs['position'])\n else:\n result = __salt__['iptables.delete'](\n table,\n kwargs['chain'],\n family=family,\n rule=rule)\n\n if not result:\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = 'Delete iptables rule for {0} {1}'.format(\n name,\n command.strip())\n if 'save' in kwargs:\n if kwargs['save']:\n out = __salt__['iptables.save'](filename=None, family=family)\n ret['comment'] = ('Deleted and saved iptables rule {0} for {1}\\n'\n '{2}\\n{3}').format(name, family, command.strip(), out)\n return ret\n else:\n ret['result'] = False\n ret['comment'] = ('Failed to delete iptables rule for {0}.\\n'\n 'Attempted rule was {1}').format(\n name,\n command.strip())\n return ret", "response": "Delete a single rule from a chain."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_policy(name, table='filter', family='ipv4', **kwargs):\n '''\n .. versionadded:: 2014.1.0\n\n Sets the default policy for iptables firewall tables\n\n table\n The table that owns the chain that should be modified\n\n family\n Networking family, either ipv4 or ipv6\n\n policy\n The requested table policy\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n for ignore in _STATE_INTERNAL_KEYWORDS:\n if ignore in kwargs:\n del kwargs[ignore]\n\n if __salt__['iptables.get_policy'](\n table,\n kwargs['chain'],\n family) == kwargs['policy']:\n ret['result'] = True\n ret['comment'] = ('iptables default policy for chain {0} on table {1} for {2} already set to {3}'\n .format(kwargs['chain'], table, family, kwargs['policy']))\n return ret\n if __opts__['test']:\n ret['comment'] = 'iptables default policy for chain {0} on table {1} for {2} needs to be set to {3}'.format(\n kwargs['chain'],\n table,\n family,\n kwargs['policy']\n )\n return ret\n if not __salt__['iptables.set_policy'](\n table,\n kwargs['chain'],\n kwargs['policy'],\n family):\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = 'Set default policy for {0} to {1} family {2}'.format(\n kwargs['chain'],\n kwargs['policy'],\n family\n )\n if 'save' in kwargs:\n if kwargs['save']:\n __salt__['iptables.save'](filename=None, family=family)\n ret['comment'] = 'Set and saved default policy for {0} to {1} family {2}'.format(\n kwargs['chain'],\n kwargs['policy'],\n family\n )\n return ret\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to set iptables default policy'\n return ret", "response": "Set the default policy for iptables firewall tables"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef flush(name, table='filter', family='ipv4', **kwargs):\n '''\n .. versionadded:: 2014.1.0\n\n Flush current iptables state\n\n table\n The table that owns the chain that should be modified\n\n family\n Networking family, either ipv4 or ipv6\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n for ignore in _STATE_INTERNAL_KEYWORDS:\n if ignore in kwargs:\n del kwargs[ignore]\n\n if 'chain' not in kwargs:\n kwargs['chain'] = ''\n if __opts__['test']:\n ret['comment'] = 'iptables rules in {0} table {1} chain {2} family needs to be flushed'.format(\n name,\n table,\n family)\n return ret\n if not __salt__['iptables.flush'](table, kwargs['chain'], family):\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = 'Flush iptables rules in {0} table {1} chain {2} family'.format(\n table,\n kwargs['chain'],\n family\n )\n return ret\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to flush iptables rules'\n return ret", "response": "Flush iptables rules in a specific table and family"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef mod_aggregate(low, chunks, running):\n '''\n The mod_aggregate function which looks up all rules in the available\n low chunks and merges them into a single rules ref in the present low data\n '''\n rules = []\n agg_enabled = [\n 'append',\n 'insert',\n ]\n if low.get('fun') not in agg_enabled:\n return low\n for chunk in chunks:\n tag = __utils__['state.gen_tag'](chunk)\n if tag in running:\n # Already ran the iptables state, skip aggregation\n continue\n if chunk.get('state') == 'iptables':\n if '__agg__' in chunk:\n continue\n # Check for the same function\n if chunk.get('fun') != low.get('fun'):\n continue\n\n if chunk not in rules:\n rules.append(chunk)\n chunk['__agg__'] = True\n\n if rules:\n if 'rules' in low:\n low['rules'].extend(rules)\n else:\n low['rules'] = rules\n return low", "response": "The mod_aggregate function which looks up all rules in the available low chunks and merges them into a single rule ref in the present low data."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nmonitors the memory usage of the minion and emit a beacon", "response": "def beacon(config):\n '''\n Monitor the memory usage of the minion\n\n Specify thresholds for percent used and only emit a beacon\n if it is exceeded.\n\n .. code-block:: yaml\n\n beacons:\n memusage:\n - percent: 63%\n '''\n ret = []\n\n _config = {}\n list(map(_config.update, config))\n\n _current_usage = psutil.virtual_memory()\n\n current_usage = _current_usage.percent\n monitor_usage = _config['percent']\n if '%' in monitor_usage:\n monitor_usage = re.sub('%', '', monitor_usage)\n monitor_usage = float(monitor_usage)\n if current_usage >= monitor_usage:\n ret.append({'memusage': current_usage})\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nformats the function name", "response": "def format_name(self):\n '''\n Format the function name\n '''\n if not hasattr(self.module, '__func_alias__'):\n # Resume normal sphinx.ext.autodoc operation\n return super(FunctionDocumenter, self).format_name()\n\n if not self.objpath:\n # Resume normal sphinx.ext.autodoc operation\n return super(FunctionDocumenter, self).format_name()\n\n if len(self.objpath) > 1:\n # Resume normal sphinx.ext.autodoc operation\n return super(FunctionDocumenter, self).format_name()\n\n # Use the salt func aliased name instead of the real name\n return self.module.__func_alias__.get(self.objpath[0], self.objpath[0])"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nmanage NTP servers and set the NTP servers to the servers specified in servers", "response": "def managed(name, servers=None):\n '''\n Manage NTP servers\n\n servers\n A list of NTP servers\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'NTP servers already configured as specified'}\n\n if not _check_servers(servers):\n ret['result'] = False\n ret['comment'] = 'NTP servers must be a list of strings'\n\n before_servers = _get_servers()\n desired_servers = set(servers)\n\n if before_servers == desired_servers:\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = ('NTP servers will be updated to: {0}'\n .format(', '.join(sorted(desired_servers))))\n return ret\n\n __salt__['ntp.set_servers'](*desired_servers)\n after_servers = _get_servers()\n\n if after_servers == desired_servers:\n ret['comment'] = 'NTP servers updated'\n ret['changes'] = {'old': sorted(before_servers),\n 'new': sorted(after_servers)}\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to update NTP servers'\n if before_servers != after_servers:\n ret['changes'] = {'old': sorted(before_servers),\n 'new': sorted(after_servers)}\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the command to use based on the family", "response": "def _iptables_cmd(family='ipv4'):\n '''\n Return correct command based on the family, e.g. ipv4 or ipv6\n '''\n if family == 'ipv6':\n return salt.utils.path.which('ip6tables')\n else:\n return salt.utils.path.which('iptables')"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _has_option(option, family='ipv4'):\n '''\n Return truth of whether iptables has `option`. For example:\n\n .. code-block:: python\n\n _has_option('--wait')\n _has_option('--check', family='ipv6')\n '''\n cmd = '{0} --help'.format(_iptables_cmd(family))\n if option in __salt__['cmd.run'](cmd, output_loglevel='quiet'):\n return True\n return False", "response": "Return truth of whether iptables has option."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _conf(family='ipv4'):\n '''\n Some distros have a specific location for config files\n '''\n if __grains__['os_family'] == 'RedHat':\n if family == 'ipv6':\n return '/etc/sysconfig/ip6tables'\n else:\n return '/etc/sysconfig/iptables'\n elif __grains__['os_family'] == 'Arch':\n if family == 'ipv6':\n return '/etc/iptables/ip6tables.rules'\n else:\n return '/etc/iptables/iptables.rules'\n elif __grains__['os_family'] == 'Debian':\n if family == 'ipv6':\n return '/etc/iptables/rules.v6'\n else:\n return '/etc/iptables/rules.v4'\n elif __grains__['os_family'] == 'Gentoo':\n if family == 'ipv6':\n return '/var/lib/ip6tables/rules-save'\n else:\n return '/var/lib/iptables/rules-save'\n elif __grains__['os_family'] == 'Suse':\n # SuSE does not seem to use separate files for IPv4 and IPv6\n return '/etc/sysconfig/scripts/SuSEfirewall2-custom'\n elif __grains__['os_family'] == 'Void':\n if family == 'ipv4':\n return '/etc/iptables/iptables.rules'\n else:\n return '/etc/iptables/ip6tables.rules'\n elif __grains__['os'] == 'Alpine':\n if family == 'ipv6':\n return '/etc/iptables/rules6-save'\n else:\n return '/etc/iptables/rules-save'\n else:\n raise SaltException('Saving iptables to file is not' +\n ' supported on {0}.'.format(__grains__['os']) +\n ' Please file an issue with SaltStack')", "response": "Return the config file for the given family"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning string with save_filter regex entries removed.", "response": "def _regex_iptables_save(cmd_output, filters=None):\n '''\n Return string with `save_filter` regex entries removed. For example:\n\n If `filters` is not provided, it will be pulled from minion config,\n minion grains, minion pillar, or master config. Default return value\n if no filters found is the original cmd_output string.\n\n .. code-block:: python\n\n _regex_iptables_save(cmd_output, ['-A DOCKER*'])\n '''\n # grab RE compiled filters from context for performance\n if 'iptables.save_filters' not in __context__:\n __context__['iptables.save_filters'] = []\n for pattern in filters or _conf_save_filters():\n try:\n __context__['iptables.save_filters']\\\n .append(re.compile(pattern))\n except re.error as e:\n log.warning('Skipping regex rule: \\'%s\\': %s',\n pattern, e)\n continue\n\n if __context__['iptables.save_filters']:\n # line by line get rid of any regex matches\n _filtered_cmd_output = \\\n [line for line in cmd_output.splitlines(True)\n if not any(reg.search(line) for reg\n in __context__['iptables.save_filters'])]\n return ''.join(_filtered_cmd_output)\n\n return cmd_output"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef version(family='ipv4'):\n '''\n Return version from iptables --version\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' iptables.version\n\n IPv6:\n salt '*' iptables.version family=ipv6\n '''\n cmd = '{0} --version' . format(_iptables_cmd(family))\n out = __salt__['cmd.run'](cmd).split()\n return out[1]", "response": "Return version from iptables"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nbuilding a well - formatted iptables rule.", "response": "def build_rule(table='filter', chain=None, command=None, position='', full=None, family='ipv4',\n **kwargs):\n '''\n Build a well-formatted iptables rule based on kwargs. A `table` and `chain`\n are not required, unless `full` is True.\n\n If `full` is `True`, then `table`, `chain` and `command` are required.\n `command` may be specified as either a short option ('I') or a long option\n (`--insert`). This will return the iptables command, exactly as it would\n be used from the command line.\n\n If a position is required (as with `-I` or `-D`), it may be specified as\n `position`. This will only be useful if `full` is True.\n\n If `state` is passed, it will be ignored, use `connstate`.\n If `connstate` is passed in, it will automatically be changed to `state`.\n\n To pass in jump options that doesn't take arguments, pass in an empty\n string.\n\n .. note::\n\n Whereas iptables will accept ``-p``, ``--proto[c[o[l]]]`` as synonyms\n of ``--protocol``, if ``--proto`` appears in an iptables command after\n the appearance of ``-m policy``, it is interpreted as the ``--proto``\n option of the policy extension (see the iptables-extensions(8) man\n page).\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' iptables.build_rule match=state \\\\\n connstate=RELATED,ESTABLISHED jump=ACCEPT\n\n salt '*' iptables.build_rule filter INPUT command=I position=3 \\\\\n full=True match=state connstate=RELATED,ESTABLISHED jump=ACCEPT\n\n salt '*' iptables.build_rule filter INPUT command=A \\\\\n full=True match=state connstate=RELATED,ESTABLISHED \\\\\n source='127.0.0.1' jump=ACCEPT\n\n .. Invert Rules\n salt '*' iptables.build_rule filter INPUT command=A \\\\\n full=True match=state connstate=RELATED,ESTABLISHED \\\\\n source='!127.0.0.1' jump=ACCEPT\n\n salt '*' iptables.build_rule filter INPUT command=A \\\\\n full=True match=state connstate=RELATED,ESTABLISHED \\\\\n destination='not 127.0.0.1' jump=ACCEPT\n\n IPv6:\n salt '*' iptables.build_rule match=state \\\\\n connstate=RELATED,ESTABLISHED jump=ACCEPT \\\\\n family=ipv6\n salt '*' iptables.build_rule filter INPUT command=I position=3 \\\\\n full=True match=state connstate=RELATED,ESTABLISHED jump=ACCEPT \\\\\n family=ipv6\n '''\n if 'target' in kwargs:\n kwargs['jump'] = kwargs.pop('target')\n\n # Ignore name and state for this function\n kwargs.pop('name', None)\n kwargs.pop('state', None)\n\n for ignore in list(_STATE_INTERNAL_KEYWORDS) + ['chain', 'save', 'table']:\n if ignore in kwargs:\n del kwargs[ignore]\n\n rule = []\n protocol = False\n bang_not_pat = re.compile(r'(!|not)\\s?')\n\n def maybe_add_negation(arg):\n '''\n Will check if the defined argument is intended to be negated,\n (i.e. prefixed with '!' or 'not'), and add a '! ' to the rule.\n\n The prefix will be removed from the value in the kwargs dict.\n '''\n value = str(kwargs[arg])\n if value.startswith('!') or value.startswith('not'):\n kwargs[arg] = re.sub(bang_not_pat, '', value)\n return '! '\n return ''\n\n if 'if' in kwargs:\n rule.append('{0}-i {1}'.format(maybe_add_negation('if'), kwargs['if']))\n del kwargs['if']\n\n if 'of' in kwargs:\n rule.append('{0}-o {1}'.format(maybe_add_negation('of'), kwargs['of']))\n del kwargs['of']\n\n if 'proto' in kwargs and kwargs.get('match') != 'policy':\n kwargs['protocol'] = kwargs['proto']\n del kwargs['proto']\n # Handle the case 'proto' in kwargs and kwargs.get('match') == 'policy' below\n if 'protocol' in kwargs:\n if not protocol:\n rule.append('{0}-p {1}'.format(maybe_add_negation('protocol'), kwargs['protocol']))\n protocol = True\n del kwargs['protocol']\n\n if 'match' in kwargs:\n match_value = kwargs['match']\n if not isinstance(match_value, list):\n match_value = match_value.split(',')\n for match in match_value:\n rule.append('-m {0}'.format(match))\n if 'name_' in kwargs and match.strip() in ('pknock', 'quota2', 'recent'):\n rule.append('--name {0}'.format(kwargs['name_']))\n del kwargs['name_']\n if 'proto' in kwargs and kwargs.get('match') == 'policy':\n rule.append('{0}--proto {1}'.format(maybe_add_negation('proto'), kwargs['proto']))\n del kwargs['proto']\n del kwargs['match']\n\n if 'match-set' in kwargs:\n if isinstance(kwargs['match-set'], six.string_types):\n kwargs['match-set'] = [kwargs['match-set']]\n for match_set in kwargs['match-set']:\n negative_match_set = ''\n if match_set.startswith('!') or match_set.startswith('not'):\n negative_match_set = '! '\n match_set = re.sub(bang_not_pat, '', match_set)\n rule.append('-m set {0}--match-set {1}'.format(negative_match_set, match_set))\n del kwargs['match-set']\n\n if 'connstate' in kwargs:\n if '-m state' not in rule:\n rule.append('-m state')\n\n rule.append('{0}--state {1}'.format(maybe_add_negation('connstate'), kwargs['connstate']))\n\n del kwargs['connstate']\n\n if 'dport' in kwargs:\n rule.append('{0}--dport {1}'.format(maybe_add_negation('dport'), kwargs['dport']))\n del kwargs['dport']\n\n if 'sport' in kwargs:\n rule.append('{0}--sport {1}'.format(maybe_add_negation('sport'), kwargs['sport']))\n del kwargs['sport']\n\n for multiport_arg in ('dports', 'sports'):\n if multiport_arg in kwargs:\n if '-m multiport' not in rule:\n rule.append('-m multiport')\n if not protocol:\n return 'Error: protocol must be specified'\n\n mp_value = kwargs[multiport_arg]\n if isinstance(mp_value, list):\n if any(i for i in mp_value if str(i).startswith('!') or str(i).startswith('not')):\n mp_value = [re.sub(bang_not_pat, '', str(item)) for item in mp_value]\n rule.append('!')\n dports = ','.join(str(i) for i in mp_value)\n else:\n if str(mp_value).startswith('!') or str(mp_value).startswith('not'):\n dports = re.sub(bang_not_pat, '', mp_value)\n rule.append('!')\n else:\n dports = mp_value\n\n rule.append('--{0} {1}'.format(multiport_arg, dports))\n del kwargs[multiport_arg]\n\n if 'comment' in kwargs:\n if '-m comment' not in rule:\n rule.append('-m comment')\n\n rule.append('--comment \"{0}\"'.format(kwargs['comment']))\n del kwargs['comment']\n\n # --set in ipset is deprecated, works but returns error.\n # rewrite to --match-set if not empty, otherwise treat as recent option\n if 'set' in kwargs and kwargs['set']:\n rule.append('{0}--match-set {1}'.format(maybe_add_negation('set'), kwargs['set']))\n del kwargs['set']\n\n # Jumps should appear last, except for any arguments that are passed to\n # jumps, which of course need to follow.\n after_jump = []\n # All jump arguments as extracted from man iptables-extensions, man iptables,\n # man xtables-addons and http://www.iptables.info/en/iptables-targets-and-jumps.html\n after_jump_arguments = (\n 'j', # j and jump needs to be first\n 'jump',\n\n # IPTABLES\n 'add-set',\n 'and-mark',\n 'and-tos',\n 'checksum-fill',\n 'clamp-mss-to-pmtu',\n 'clustermac',\n 'ctevents',\n 'ctmask',\n 'del-set',\n 'ecn-tcp-remove',\n 'exist',\n 'expevents',\n 'gateway',\n 'hash-init',\n 'hashmode',\n 'helper',\n 'label',\n 'local-node',\n 'log-ip-options',\n 'log-level',\n 'log-prefix',\n 'log-tcp-options',\n 'log-tcp-sequence',\n 'log-uid',\n 'mask',\n 'new',\n 'nfmask',\n 'nflog-group',\n 'nflog-prefix',\n 'nflog-range',\n 'nflog-threshold',\n 'nodst',\n 'notrack',\n 'on-ip',\n 'on-port',\n 'or-mark',\n 'or-tos',\n 'persistent',\n 'queue-balance',\n 'queue-bypass',\n 'queue-num',\n 'random',\n 'rateest-ewmalog',\n 'rateest-interval',\n 'rateest-name',\n 'reject-with',\n 'restore',\n 'restore-mark',\n #'save', # no arg, problematic name: How do we avoid collision with this?\n 'save-mark',\n 'selctx',\n 'set-class',\n 'set-dscp',\n 'set-dscp-class',\n 'set-mark',\n 'set-mss',\n 'set-tos',\n 'set-xmark',\n 'strip-options',\n 'timeout',\n 'to',\n 'to-destination',\n 'to-ports',\n 'to-source',\n 'total-nodes',\n 'tproxy-mark',\n 'ttl-dec',\n 'ttl-inc',\n 'ttl-set',\n 'type',\n 'ulog-cprange',\n 'ulog-nlgroup',\n 'ulog-prefix',\n 'ulog-qthreshold',\n 'xor-mark',\n 'xor-tos',\n 'zone',\n\n # IPTABLES-EXTENSIONS\n 'dst-pfx',\n 'hl-dec',\n 'hl-inc',\n 'hl-set',\n 'hmark-dport-mask',\n 'hmark-dst-prefix',\n 'hmark-mod',\n 'hmark-offset',\n 'hmark-proto-mask',\n 'hmark-rnd',\n 'hmark-spi-mask',\n 'hmark-sport-mask',\n 'hmark-src-prefix',\n 'hmark-tuple',\n 'led-always-blink',\n 'led-delay',\n 'led-trigger-id',\n 'queue-cpu-fanout',\n 'src-pfx',\n\n # WEB\n 'to-port',\n\n # XTABLES\n 'addr',\n 'and-mask',\n 'delude',\n 'honeypot',\n 'or-mask',\n 'prefix',\n 'reset',\n 'reuse',\n 'set-mac',\n 'shift',\n 'static',\n 'tarpit',\n 'tname',\n 'ttl',\n )\n for after_jump_argument in after_jump_arguments:\n if after_jump_argument in kwargs:\n value = kwargs[after_jump_argument]\n if value in (None, ''): # options without arguments\n after_jump.append('--{0}'.format(after_jump_argument))\n elif any(ws_char in str(value) for ws_char in string.whitespace):\n after_jump.append('--{0} \"{1}\"'.format(after_jump_argument, value))\n else:\n after_jump.append('--{0} {1}'.format(after_jump_argument, value))\n del kwargs[after_jump_argument]\n\n for key in kwargs:\n negation = maybe_add_negation(key)\n # don't use .items() since maybe_add_negation removes the prefix from\n # the value in the kwargs, thus we need to fetch it after that has run\n value = kwargs[key]\n flag = '-' if len(key) == 1 else '--'\n value = '' if value in (None, '') else ' {0}'.format(value)\n rule.append('{0}{1}{2}{3}'.format(negation, flag, key, value))\n\n rule += after_jump\n\n if full:\n if not table:\n return 'Error: Table needs to be specified'\n if not chain:\n return 'Error: Chain needs to be specified'\n if not command:\n return 'Error: Command needs to be specified'\n\n if command in 'ACDIRLSFZNXPE':\n flag = '-'\n else:\n flag = '--'\n\n wait = '--wait' if _has_option('--wait', family) else ''\n\n return '{0} {1} -t {2} {3}{4} {5} {6} {7}'.format(_iptables_cmd(family),\n wait, table, flag, command, chain, position, ' '.join(rule))\n\n return ' '.join(rule)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_saved_policy(table='filter', chain=None, conf_file=None, family='ipv4'):\n '''\n Return the current policy for the specified table/chain\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' iptables.get_saved_policy filter INPUT\n salt '*' iptables.get_saved_policy filter INPUT \\\\\n conf_file=/etc/iptables.saved\n\n IPv6:\n salt '*' iptables.get_saved_policy filter INPUT family=ipv6\n salt '*' iptables.get_saved_policy filter INPUT \\\\\n conf_file=/etc/iptables.saved family=ipv6\n\n '''\n if not chain:\n return 'Error: Chain needs to be specified'\n\n rules = _parse_conf(conf_file, family=family)\n try:\n return rules[table][chain]['policy']\n except KeyError:\n return None", "response": "Get the current policy for the specified table and chain"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_policy(table='filter', chain=None, family='ipv4'):\n '''\n Return the current policy for the specified table/chain\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' iptables.get_policy filter INPUT\n\n IPv6:\n salt '*' iptables.get_policy filter INPUT family=ipv6\n '''\n if not chain:\n return 'Error: Chain needs to be specified'\n\n rules = _parse_conf(in_mem=True, family=family)\n try:\n return rules[table][chain]['policy']\n except KeyError:\n return None", "response": "Get the current policy for the specified table chain"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set_policy(table='filter', chain=None, policy=None, family='ipv4'):\n '''\n Set the current policy for the specified table/chain\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' iptables.set_policy filter INPUT ACCEPT\n\n IPv6:\n salt '*' iptables.set_policy filter INPUT ACCEPT family=ipv6\n '''\n if not chain:\n return 'Error: Chain needs to be specified'\n if not policy:\n return 'Error: Policy needs to be specified'\n\n wait = '--wait' if _has_option('--wait', family) else ''\n cmd = '{0} {1} -t {2} -P {3} {4}'.format(\n _iptables_cmd(family), wait, table, chain, policy)\n out = __salt__['cmd.run'](cmd)\n return out", "response": "Set the current policy for the specified table chain and family"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef save(filename=None, family='ipv4'):\n '''\n Save the current in-memory rules to disk\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' iptables.save /etc/sysconfig/iptables\n\n IPv6:\n salt '*' iptables.save /etc/sysconfig/iptables family=ipv6\n '''\n if _conf() and not filename:\n filename = _conf(family)\n\n log.debug('Saving rules to %s', filename)\n\n parent_dir = os.path.dirname(filename)\n if not os.path.isdir(parent_dir):\n os.makedirs(parent_dir)\n cmd = '{0}-save'.format(_iptables_cmd(family))\n ipt = __salt__['cmd.run'](cmd)\n\n # regex out the output if configured with filters\n if _conf_save_filters():\n ipt = _regex_iptables_save(ipt)\n\n out = __salt__['file.write'](filename, ipt)\n return out", "response": "Save the current in - memory rules to disk"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking for the existence of a rule in a table and chain", "response": "def check(table='filter', chain=None, rule=None, family='ipv4'):\n '''\n Check for the existence of a rule in the table and chain\n\n This function accepts a rule in a standard iptables command format,\n starting with the chain. Trying to force users to adapt to a new\n method of creating rules would be irritating at best, and we\n already have a parser that can handle it.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' iptables.check filter INPUT \\\\\n rule='-m state --state RELATED,ESTABLISHED -j ACCEPT'\n\n IPv6:\n salt '*' iptables.check filter INPUT \\\\\n rule='-m state --state RELATED,ESTABLISHED -j ACCEPT' \\\\\n family=ipv6\n '''\n if not chain:\n return 'Error: Chain needs to be specified'\n if not rule:\n return 'Error: Rule needs to be specified'\n ipt_cmd = _iptables_cmd(family)\n\n if _has_option('--check', family):\n cmd = '{0} -t {1} -C {2} {3}'.format(ipt_cmd, table, chain, rule)\n out = __salt__['cmd.run'](cmd, output_loglevel='quiet')\n else:\n _chain_name = hex(uuid.getnode())\n\n # Create temporary table\n __salt__['cmd.run']('{0} -t {1} -N {2}'.format(ipt_cmd, table, _chain_name))\n __salt__['cmd.run']('{0} -t {1} -A {2} {3}'.format(ipt_cmd, table, _chain_name, rule))\n\n out = __salt__['cmd.run']('{0}-save'.format(ipt_cmd))\n\n # Clean up temporary table\n __salt__['cmd.run']('{0} -t {1} -F {2}'.format(ipt_cmd, table, _chain_name))\n __salt__['cmd.run']('{0} -t {1} -X {2}'.format(ipt_cmd, table, _chain_name))\n\n for i in out.splitlines():\n if i.startswith('-A {0}'.format(_chain_name)):\n if i.replace(_chain_name, chain) in out.splitlines():\n return True\n\n return False\n\n if not out:\n return True\n return out"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking if the existence of a chain in a table", "response": "def check_chain(table='filter', chain=None, family='ipv4'):\n '''\n .. versionadded:: 2014.1.0\n\n Check for the existence of a chain in the table\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' iptables.check_chain filter INPUT\n\n IPv6:\n salt '*' iptables.check_chain filter INPUT family=ipv6\n '''\n\n if not chain:\n return 'Error: Chain needs to be specified'\n\n cmd = '{0}-save -t {1}'.format(_iptables_cmd(family), table)\n out = __salt__['cmd.run'](cmd).find(':{0} '.format(chain))\n\n if out != -1:\n out = True\n else:\n out = False\n\n return out"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef new_chain(table='filter', chain=None, family='ipv4'):\n '''\n .. versionadded:: 2014.1.0\n\n Create new custom chain to the specified table.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' iptables.new_chain filter CUSTOM_CHAIN\n\n IPv6:\n salt '*' iptables.new_chain filter CUSTOM_CHAIN family=ipv6\n '''\n\n if not chain:\n return 'Error: Chain needs to be specified'\n\n wait = '--wait' if _has_option('--wait', family) else ''\n cmd = '{0} {1} -t {2} -N {3}'.format(\n _iptables_cmd(family), wait, table, chain)\n out = __salt__['cmd.run'](cmd)\n\n if not out:\n out = True\n return out", "response": "Create a new custom chain to the specified table."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nappending a rule to a table in a chain.", "response": "def append(table='filter', chain=None, rule=None, family='ipv4'):\n '''\n Append a rule to the specified table/chain.\n\n This function accepts a rule in a standard iptables command format,\n starting with the chain. Trying to force users to adapt to a new\n method of creating rules would be irritating at best, and we\n already have a parser that can handle it.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' iptables.append filter INPUT \\\\\n rule='-m state --state RELATED,ESTABLISHED -j ACCEPT'\n\n IPv6:\n salt '*' iptables.append filter INPUT \\\\\n rule='-m state --state RELATED,ESTABLISHED -j ACCEPT' \\\\\n family=ipv6\n '''\n if not chain:\n return 'Error: Chain needs to be specified'\n if not rule:\n return 'Error: Rule needs to be specified'\n\n wait = '--wait' if _has_option('--wait', family) else ''\n returnCheck = check(table, chain, rule, family)\n if isinstance(returnCheck, bool) and returnCheck:\n return False\n cmd = '{0} {1} -t {2} -A {3} {4}'.format(\n _iptables_cmd(family), wait, table, chain, rule)\n out = __salt__['cmd.run'](cmd)\n return not out"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef insert(table='filter', chain=None, position=None, rule=None, family='ipv4'):\n '''\n Insert a rule into the specified table/chain, at the specified position.\n\n This function accepts a rule in a standard iptables command format,\n starting with the chain. Trying to force users to adapt to a new\n method of creating rules would be irritating at best, and we\n already have a parser that can handle it.\n\n If the position specified is a negative number, then the insert will be\n performed counting from the end of the list. For instance, a position\n of -1 will insert the rule as the second to last rule. To insert a rule\n in the last position, use the append function instead.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' iptables.insert filter INPUT position=3 \\\\\n rule='-m state --state RELATED,ESTABLISHED -j ACCEPT'\n\n IPv6:\n salt '*' iptables.insert filter INPUT position=3 \\\\\n rule='-m state --state RELATED,ESTABLISHED -j ACCEPT' \\\\\n family=ipv6\n '''\n if not chain:\n return 'Error: Chain needs to be specified'\n if not position:\n return 'Error: Position needs to be specified or use append (-A)'\n if not rule:\n return 'Error: Rule needs to be specified'\n\n if position < 0:\n rules = get_rules(family=family)\n size = len(rules[table][chain]['rules'])\n position = (size + position) + 1\n if position is 0:\n position = 1\n\n wait = '--wait' if _has_option('--wait', family) else ''\n returnCheck = check(table, chain, rule, family)\n if isinstance(returnCheck, bool) and returnCheck:\n return False\n cmd = '{0} {1} -t {2} -I {3} {4} {5}'.format(\n _iptables_cmd(family), wait, table, chain, position, rule)\n out = __salt__['cmd.run'](cmd)\n return out", "response": "Insert a rule into the specified table or chain at the specified position."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndeleting a rule from the specified table chain and position.", "response": "def delete(table, chain=None, position=None, rule=None, family='ipv4'):\n '''\n Delete a rule from the specified table/chain, specifying either the rule\n in its entirety, or the rule's position in the chain.\n\n This function accepts a rule in a standard iptables command format,\n starting with the chain. Trying to force users to adapt to a new\n method of creating rules would be irritating at best, and we\n already have a parser that can handle it.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' iptables.delete filter INPUT position=3\n salt '*' iptables.delete filter INPUT \\\\\n rule='-m state --state RELATED,ESTABLISHED -j ACCEPT'\n\n IPv6:\n salt '*' iptables.delete filter INPUT position=3 family=ipv6\n salt '*' iptables.delete filter INPUT \\\\\n rule='-m state --state RELATED,ESTABLISHED -j ACCEPT' \\\\\n family=ipv6\n '''\n\n if position and rule:\n return 'Error: Only specify a position or a rule, not both'\n\n if position:\n rule = position\n\n wait = '--wait' if _has_option('--wait', family) else ''\n cmd = '{0} {1} -t {2} -D {3} {4}'.format(\n _iptables_cmd(family), wait, table, chain, rule)\n out = __salt__['cmd.run'](cmd)\n return out"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nflushing all chains in the specified chain in the specified table.", "response": "def flush(table='filter', chain='', family='ipv4'):\n '''\n Flush the chain in the specified table, flush all chains in the specified\n table if not specified chain.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' iptables.flush filter INPUT\n\n IPv6:\n salt '*' iptables.flush filter INPUT family=ipv6\n '''\n\n wait = '--wait' if _has_option('--wait', family) else ''\n cmd = '{0} {1} -t {2} -F {3}'.format(_iptables_cmd(family), wait, table, chain)\n out = __salt__['cmd.run'](cmd)\n return out"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse the configuration file and return a dict of the rules and table names.", "response": "def _parse_conf(conf_file=None, in_mem=False, family='ipv4'):\n '''\n If a file is not passed in, and the correct one for this OS is not\n detected, return False\n '''\n if _conf() and not conf_file and not in_mem:\n conf_file = _conf(family)\n\n rules = ''\n if conf_file:\n with salt.utils.files.fopen(conf_file, 'r') as ifile:\n rules = ifile.read()\n elif in_mem:\n cmd = '{0}-save' . format(_iptables_cmd(family))\n rules = __salt__['cmd.run'](cmd)\n else:\n raise SaltException('A file was not found to parse')\n\n ret = {}\n table = ''\n parser = _parser()\n for line in rules.splitlines():\n line = salt.utils.stringutils.to_unicode(line)\n if line.startswith('*'):\n table = line.replace('*', '')\n ret[table] = {}\n elif line.startswith(':'):\n comps = line.split()\n chain = comps[0].replace(':', '')\n ret[table][chain] = {}\n ret[table][chain]['policy'] = comps[1]\n counters = comps[2].replace('[', '').replace(']', '')\n (pcount, bcount) = counters.split(':')\n ret[table][chain]['packet count'] = pcount\n ret[table][chain]['byte count'] = bcount\n ret[table][chain]['rules'] = []\n ret[table][chain]['rules_comment'] = {}\n elif line.startswith('-A'):\n args = salt.utils.args.shlex_split(line)\n index = 0\n while index + 1 < len(args):\n swap = args[index] == '!' and args[index + 1].startswith('-')\n if swap:\n args[index], args[index + 1] = args[index + 1], args[index]\n if args[index].startswith('-'):\n index += 1\n if args[index].startswith('-') or (args[index] == '!' and\n not swap):\n args.insert(index, '')\n else:\n while (index + 1 < len(args) and\n args[index + 1] != '!' and\n not args[index + 1].startswith('-')):\n args[index] += ' {0}'.format(args.pop(index + 1))\n index += 1\n if args[-1].startswith('-'):\n args.append('')\n parsed_args = []\n opts, _ = parser.parse_known_args(args)\n parsed_args = vars(opts)\n ret_args = {}\n chain = parsed_args['append']\n for arg in parsed_args:\n if parsed_args[arg] and arg is not 'append':\n ret_args[arg] = parsed_args[arg]\n if parsed_args['comment'] is not None:\n comment = parsed_args['comment'][0].strip('\"')\n ret[table][chain[0]]['rules_comment'][comment] = ret_args\n ret[table][chain[0]]['rules'].append(ret_args)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _parser():\n '''\n This function attempts to list all the options documented in the\n iptables(8) and iptables-extensions(8) man pages. They will not all be\n used by all parts of the module; use them intelligently and appropriately.\n '''\n add_arg = None\n if sys.version.startswith('2.6'):\n import optparse\n parser = optparse.OptionParser()\n add_arg = parser.add_option\n else:\n import argparse # pylint: disable=minimum-python-version\n parser = argparse.ArgumentParser()\n add_arg = parser.add_argument\n\n # COMMANDS\n add_arg('-A', '--append', dest='append', action='append')\n add_arg('-D', '--delete', dest='delete', action='append')\n add_arg('-I', '--insert', dest='insert', action='append')\n add_arg('-R', '--replace', dest='replace', action='append')\n add_arg('-L', '--list', dest='list', action='append')\n add_arg('-F', '--flush', dest='flush', action='append')\n add_arg('-Z', '--zero', dest='zero', action='append')\n add_arg('-N', '--new-chain', dest='new-chain', action='append')\n add_arg('-X', '--delete-chain', dest='delete-chain', action='append')\n add_arg('-P', '--policy', dest='policy', action='append')\n add_arg('-E', '--rename-chain', dest='rename-chain', action='append')\n\n # PARAMETERS\n add_arg('-p', '--protocol', dest='protocol', action='append')\n add_arg('-s', '--source', dest='source', action='append')\n add_arg('-d', '--destination', dest='destination', action='append')\n add_arg('-j', '--jump', dest='jump', action='append')\n add_arg('-g', '--goto', dest='goto', action='append')\n add_arg('-i', '--in-interface', dest='in-interface', action='append')\n add_arg('-o', '--out-interface', dest='out-interface', action='append')\n add_arg('-f', '--fragment', dest='fragment', action='append')\n add_arg('-c', '--set-counters', dest='set-counters', action='append')\n\n # MATCH EXTENSIONS\n add_arg('-m', '--match', dest='match', action='append')\n ## addrtype\n add_arg('--src-type', dest='src-type', action='append')\n add_arg('--dst-type', dest='dst-type', action='append')\n add_arg('--limit-iface-in', dest='limit-iface-in', action='append')\n add_arg('--limit-iface-out', dest='limit-iface-out', action='append')\n ## ah\n add_arg('--ahspi', dest='ahspi', action='append')\n add_arg('--ahlen', dest='ahlen', action='append')\n add_arg('--ahres', dest='ahres', action='append')\n ## bpf\n add_arg('--bytecode', dest='bytecode', action='append')\n ## cgroup\n add_arg('--cgroup', dest='cgroup', action='append')\n ## cluster\n add_arg('--cluster-total-nodes',\n dest='cluster-total-nodes',\n action='append')\n add_arg('--cluster-local-node', dest='cluster-local-node', action='append')\n add_arg('--cluster-local-nodemask',\n dest='cluster-local-nodemask',\n action='append')\n add_arg('--cluster-hash-seed', dest='cluster-hash-seed', action='append')\n add_arg('--h-length', dest='h-length', action='append')\n add_arg('--mangle-mac-s', dest='mangle-mac-s', action='append')\n add_arg('--mangle-mac-d', dest='mangle-mac-d', action='append')\n ## comment\n add_arg('--comment', dest='comment', action='append')\n ## connbytes\n add_arg('--connbytes', dest='connbytes', action='append')\n add_arg('--connbytes-dir', dest='connbytes-dir', action='append')\n add_arg('--connbytes-mode', dest='connbytes-mode', action='append')\n ## connlabel\n add_arg('--label', dest='label', action='append')\n ## connlimit\n add_arg('--connlimit-upto', dest='connlimit-upto', action='append')\n add_arg('--connlimit-above', dest='connlimit-above', action='append')\n add_arg('--connlimit-mask', dest='connlimit-mask', action='append')\n add_arg('--connlimit-saddr', dest='connlimit-saddr', action='append')\n add_arg('--connlimit-daddr', dest='connlimit-daddr', action='append')\n ## connmark\n add_arg('--mark', dest='mark', action='append')\n ## conntrack\n add_arg('--ctstate', dest='ctstate', action='append')\n add_arg('--ctproto', dest='ctproto', action='append')\n add_arg('--ctorigsrc', dest='ctorigsrc', action='append')\n add_arg('--ctorigdst', dest='ctorigdst', action='append')\n add_arg('--ctreplsrc', dest='ctreplsrc', action='append')\n add_arg('--ctrepldst', dest='ctrepldst', action='append')\n add_arg('--ctorigsrcport', dest='ctorigsrcport', action='append')\n add_arg('--ctorigdstport', dest='ctorigdstport', action='append')\n add_arg('--ctreplsrcport', dest='ctreplsrcport', action='append')\n add_arg('--ctrepldstport', dest='ctrepldstport', action='append')\n add_arg('--ctstatus', dest='ctstatus', action='append')\n add_arg('--ctexpire', dest='ctexpire', action='append')\n add_arg('--ctdir', dest='ctdir', action='append')\n ## cpu\n add_arg('--cpu', dest='cpu', action='append')\n ## dccp\n add_arg('--sport', '--source-port', dest='source_port', action='append')\n add_arg('--dport',\n '--destination-port',\n dest='destination_port',\n action='append')\n add_arg('--dccp-types', dest='dccp-types', action='append')\n add_arg('--dccp-option', dest='dccp-option', action='append')\n ## devgroup\n add_arg('--src-group', dest='src-group', action='append')\n add_arg('--dst-group', dest='dst-group', action='append')\n ## dscp\n add_arg('--dscp', dest='dscp', action='append')\n add_arg('--dscp-class', dest='dscp-class', action='append')\n ## dst\n add_arg('--dst-len', dest='dst-len', action='append')\n add_arg('--dst-opts', dest='dst-opts', action='append')\n ## ecn\n add_arg('--ecn-tcp-cwr', dest='ecn-tcp-cwr', action='append')\n add_arg('--ecn-tcp-ece', dest='ecn-tcp-ece', action='append')\n add_arg('--ecn-ip-ect', dest='ecn-ip-ect', action='append')\n ## esp\n add_arg('--espspi', dest='espspi', action='append')\n ## frag\n add_arg('--fragid', dest='fragid', action='append')\n add_arg('--fraglen', dest='fraglen', action='append')\n add_arg('--fragres', dest='fragres', action='append')\n add_arg('--fragfirst', dest='fragfirst', action='append')\n add_arg('--fragmore', dest='fragmore', action='append')\n add_arg('--fraglast', dest='fraglast', action='append')\n ## hashlimit\n add_arg('--hashlimit-upto', dest='hashlimit-upto', action='append')\n add_arg('--hashlimit-above', dest='hashlimit-above', action='append')\n add_arg('--hashlimit-burst', dest='hashlimit-burst', action='append')\n add_arg('--hashlimit-mode', dest='hashlimit-mode', action='append')\n add_arg('--hashlimit-srcmask', dest='hashlimit-srcmask', action='append')\n add_arg('--hashlimit-dstmask', dest='hashlimit-dstmask', action='append')\n add_arg('--hashlimit-name', dest='hashlimit-name', action='append')\n add_arg('--hashlimit-htable-size',\n dest='hashlimit-htable-size',\n action='append')\n add_arg('--hashlimit-htable-max',\n dest='hashlimit-htable-max',\n action='append')\n add_arg('--hashlimit-htable-expire',\n dest='hashlimit-htable-expire',\n action='append')\n add_arg('--hashlimit-htable-gcinterval',\n dest='hashlimit-htable-gcinterval',\n action='append')\n ## hbh\n add_arg('--hbh-len', dest='hbh-len', action='append')\n add_arg('--hbh-opts', dest='hbh-opts', action='append')\n ## helper\n add_arg('--helper', dest='helper', action='append')\n ## hl\n add_arg('--hl-eq', dest='hl-eq', action='append')\n add_arg('--hl-lt', dest='hl-lt', action='append')\n add_arg('--hl-gt', dest='hl-gt', action='append')\n ## icmp\n add_arg('--icmp-type', dest='icmp-type', action='append')\n ## icmp6\n add_arg('--icmpv6-type', dest='icmpv6-type', action='append')\n ## iprange\n add_arg('--src-range', dest='src-range', action='append')\n add_arg('--dst-range', dest='dst-range', action='append')\n ## ipv6header\n add_arg('--soft', dest='soft', action='append')\n add_arg('--header', dest='header', action='append')\n ## ipvs\n add_arg('--ipvs', dest='ipvs', action='append')\n add_arg('--vproto', dest='vproto', action='append')\n add_arg('--vaddr', dest='vaddr', action='append')\n add_arg('--vport', dest='vport', action='append')\n add_arg('--vdir', dest='vdir', action='append')\n add_arg('--vmethod', dest='vmethod', action='append')\n add_arg('--vportctl', dest='vportctl', action='append')\n ## length\n add_arg('--length', dest='length', action='append')\n ## limit\n add_arg('--limit', dest='limit', action='append')\n add_arg('--limit-burst', dest='limit-burst', action='append')\n ## mac\n add_arg('--mac-source', dest='mac-source', action='append')\n ## mh\n add_arg('--mh-type', dest='mh-type', action='append')\n ## multiport\n add_arg('--sports', '--source-ports', dest='source-ports', action='append')\n add_arg('--dports',\n '--destination-ports',\n dest='destination-ports',\n action='append')\n add_arg('--ports', dest='ports', action='append')\n ## nfacct\n add_arg('--nfacct-name', dest='nfacct-name', action='append')\n ## osf\n add_arg('--genre', dest='genre', action='append')\n add_arg('--ttl', dest='ttl', action='append')\n add_arg('--log', dest='log', action='append')\n ## owner\n add_arg('--uid-owner', dest='uid-owner', action='append')\n add_arg('--gid-owner', dest='gid-owner', action='append')\n add_arg('--socket-exists', dest='socket-exists', action='append')\n ## physdev\n add_arg('--physdev-in', dest='physdev-in', action='append')\n add_arg('--physdev-out', dest='physdev-out', action='append')\n add_arg('--physdev-is-in', dest='physdev-is-in', action='append')\n add_arg('--physdev-is-out', dest='physdev-is-out', action='append')\n add_arg('--physdev-is-bridged', dest='physdev-is-bridged', action='append')\n ## pkttype\n add_arg('--pkt-type', dest='pkt-type', action='append')\n ## policy\n add_arg('--dir', dest='dir', action='append')\n add_arg('--pol', dest='pol', action='append')\n add_arg('--strict', dest='strict', action='append')\n add_arg('--reqid', dest='reqid', action='append')\n add_arg('--spi', dest='spi', action='append')\n add_arg('--proto', dest='proto', action='append')\n add_arg('--mode', dest='mode', action='append')\n add_arg('--tunnel-src', dest='tunnel-src', action='append')\n add_arg('--tunnel-dst', dest='tunnel-dst', action='append')\n add_arg('--next', dest='next', action='append')\n ## quota\n add_arg('--quota', dest='quota', action='append')\n ## rateest\n add_arg('--rateest', dest='rateest', action='append')\n add_arg('--rateest1', dest='rateest1', action='append')\n add_arg('--rateest2', dest='rateest2', action='append')\n add_arg('--rateest-delta', dest='rateest-delta', action='append')\n add_arg('--rateest-bps', dest='rateest-bps', action='append')\n add_arg('--rateest-bps1', dest='rateest-bps1', action='append')\n add_arg('--rateest-bps2', dest='rateest-bps2', action='append')\n add_arg('--rateest-pps', dest='rateest-pps', action='append')\n add_arg('--rateest-pps1', dest='rateest-pps1', action='append')\n add_arg('--rateest-pps2', dest='rateest-pps2', action='append')\n add_arg('--rateest-lt', dest='rateest-lt', action='append')\n add_arg('--rateest-gt', dest='rateest-gt', action='append')\n add_arg('--rateest-eq', dest='rateest-eq', action='append')\n add_arg('--rateest-name', dest='rateest-name', action='append')\n add_arg('--rateest-interval', dest='rateest-interval', action='append')\n add_arg('--rateest-ewma', dest='rateest-ewma', action='append')\n ## realm\n add_arg('--realm', dest='realm', action='append')\n ## recent\n add_arg('--name', dest='name', action='append')\n add_arg('--set', dest='set', action='append')\n add_arg('--rsource', dest='rsource', action='append')\n add_arg('--rdest', dest='rdest', action='append')\n add_arg('--mask', dest='mask', action='append')\n add_arg('--rcheck', dest='rcheck', action='append')\n add_arg('--update', dest='update', action='append')\n add_arg('--remove', dest='remove', action='append')\n add_arg('--seconds', dest='seconds', action='append')\n add_arg('--reap', dest='reap', action='append')\n add_arg('--hitcount', dest='hitcount', action='append')\n add_arg('--rttl', dest='rttl', action='append')\n ## rpfilter\n add_arg('--loose', dest='loose', action='append')\n add_arg('--validmark', dest='validmark', action='append')\n add_arg('--accept-local', dest='accept-local', action='append')\n add_arg('--invert', dest='invert', action='append')\n ## rt\n add_arg('--rt-type', dest='rt-type', action='append')\n add_arg('--rt-segsleft', dest='rt-segsleft', action='append')\n add_arg('--rt-len', dest='rt-len', action='append')\n add_arg('--rt-0-res', dest='rt-0-res', action='append')\n add_arg('--rt-0-addrs', dest='rt-0-addrs', action='append')\n add_arg('--rt-0-not-strict', dest='rt-0-not-strict', action='append')\n ## sctp\n add_arg('--chunk-types', dest='chunk-types', action='append')\n ## set\n add_arg('--match-set', dest='match-set', action='append')\n add_arg('--return-nomatch', dest='return-nomatch', action='append')\n add_arg('--update-counters', dest='update-counters', action='append')\n add_arg('--update-subcounters', dest='update-subcounters', action='append')\n add_arg('--packets-eq', dest='packets-eq', action='append')\n add_arg('--packets-lt', dest='packets-lt', action='append')\n add_arg('--packets-gt', dest='packets-gt', action='append')\n add_arg('--bytes-eq', dest='bytes-eq', action='append')\n add_arg('--bytes-lt', dest='bytes-lt', action='append')\n add_arg('--bytes-gt', dest='bytes-gt', action='append')\n ## socket\n add_arg('--transparent', dest='transparent', action='append')\n add_arg('--nowildcard', dest='nowildcard', action='append')\n ## state\n add_arg('--state', dest='state', action='append')\n ## statistic\n add_arg('--probability', dest='probability', action='append')\n add_arg('--every', dest='every', action='append')\n add_arg('--packet', dest='packet', action='append')\n ## string\n add_arg('--algo', dest='algo', action='append')\n add_arg('--from', dest='from', action='append')\n add_arg('--to', dest='to', action='append')\n add_arg('--string', dest='string', action='append')\n add_arg('--hex-string', dest='hex-string', action='append')\n ## tcp\n add_arg('--tcp-flags', dest='tcp-flags', action='append')\n add_arg('--syn', dest='syn', action='append')\n add_arg('--tcp-option', dest='tcp-option', action='append')\n ## tcpmss\n add_arg('--mss', dest='mss', action='append')\n ## time\n add_arg('--datestart', dest='datestart', action='append')\n add_arg('--datestop', dest='datestop', action='append')\n add_arg('--timestart', dest='timestart', action='append')\n add_arg('--timestop', dest='timestop', action='append')\n add_arg('--monthdays', dest='monthdays', action='append')\n add_arg('--weekdays', dest='weekdays', action='append')\n add_arg('--contiguous', dest='contiguous', action='append')\n add_arg('--kerneltz', dest='kerneltz', action='append')\n add_arg('--utc', dest='utc', action='append')\n add_arg('--localtz', dest='localtz', action='append')\n ## tos\n add_arg('--tos', dest='tos', action='append')\n ## ttl\n add_arg('--ttl-eq', dest='ttl-eq', action='append')\n add_arg('--ttl-gt', dest='ttl-gt', action='append')\n add_arg('--ttl-lt', dest='ttl-lt', action='append')\n ## u32\n add_arg('--u32', dest='u32', action='append')\n\n # Xtables-addons matches\n ## condition\n add_arg('--condition', dest='condition', action='append')\n ## dhcpmac\n add_arg('--mac', dest='mac', action='append')\n ## fuzzy\n add_arg('--lower-limit', dest='lower-limit', action='append')\n add_arg('--upper-limit', dest='upper-limit', action='append')\n ## geoip\n add_arg('--src-cc',\n '--source-country',\n dest='source-country',\n action='append')\n add_arg('--dst-cc',\n '--destination-country',\n dest='destination-country',\n action='append')\n ## gradm\n add_arg('--enabled', dest='enabled', action='append')\n add_arg('--disabled', dest='disabled', action='append')\n ## iface\n add_arg('--iface', dest='iface', action='append')\n add_arg('--dev-in', dest='dev-in', action='append')\n add_arg('--dev-out', dest='dev-out', action='append')\n add_arg('--up', dest='up', action='append')\n add_arg('--down', dest='down', action='append')\n add_arg('--broadcast', dest='broadcast', action='append')\n add_arg('--loopback', dest='loopback', action='append')\n add_arg('--pointtopoint', dest='pointtopoint', action='append')\n add_arg('--running', dest='running', action='append')\n add_arg('--noarp', dest='noarp', action='append')\n add_arg('--arp', dest='arp', action='append')\n add_arg('--promisc', dest='promisc', action='append')\n add_arg('--multicast', dest='multicast', action='append')\n add_arg('--dynamic', dest='dynamic', action='append')\n add_arg('--lower-up', dest='lower-up', action='append')\n add_arg('--dormant', dest='dormant', action='append')\n ## ipp2p\n add_arg('--edk', dest='edk', action='append')\n add_arg('--kazaa', dest='kazaa', action='append')\n add_arg('--gnu', dest='gnu', action='append')\n add_arg('--dc', dest='dc', action='append')\n add_arg('--bit', dest='bit', action='append')\n add_arg('--apple', dest='apple', action='append')\n add_arg('--soul', dest='soul', action='append')\n add_arg('--winmx', dest='winmx', action='append')\n add_arg('--ares', dest='ares', action='append')\n add_arg('--debug', dest='debug', action='append')\n ## ipv4options\n add_arg('--flags', dest='flags', action='append')\n add_arg('--any', dest='any', action='append')\n ## length2\n add_arg('--layer3', dest='layer3', action='append')\n add_arg('--layer4', dest='layer4', action='append')\n add_arg('--layer5', dest='layer5', action='append')\n ## lscan\n add_arg('--stealth', dest='stealth', action='append')\n add_arg('--synscan', dest='synscan', action='append')\n add_arg('--cnscan', dest='cnscan', action='append')\n add_arg('--grscan', dest='grscan', action='append')\n ## psd\n add_arg('--psd-weight-threshold',\n dest='psd-weight-threshold',\n action='append')\n add_arg('--psd-delay-threshold',\n dest='psd-delay-threshold',\n action='append')\n add_arg('--psd-lo-ports-weight',\n dest='psd-lo-ports-weight',\n action='append')\n add_arg('--psd-hi-ports-weight',\n dest='psd-hi-ports-weight',\n action='append')\n ## quota2\n add_arg('--grow', dest='grow', action='append')\n add_arg('--no-change', dest='no-change', action='append')\n add_arg('--packets', dest='packets', action='append')\n ## pknock\n add_arg('--knockports', dest='knockports', action='append')\n add_arg('--time', dest='time', action='append')\n add_arg('--autoclose', dest='autoclose', action='append')\n add_arg('--checkip', dest='checkip', action='append')\n\n # TARGET EXTENSIONS\n ## AUDIT\n add_arg('--type', dest='type', action='append')\n ## CHECKSUM\n add_arg('--checksum-fill', dest='checksum-fill', action='append')\n ## CLASSIFY\n add_arg('--set-class', dest='set-class', action='append')\n ## CLUSTERIP\n add_arg('--new', dest='new', action='append')\n add_arg('--hashmode', dest='hashmode', action='append')\n add_arg('--clustermac', dest='clustermac', action='append')\n add_arg('--total-nodes', dest='total-nodes', action='append')\n add_arg('--local-node', dest='local-node', action='append')\n add_arg('--hash-init', dest='hash-init', action='append')\n ## CONNMARK\n add_arg('--set-xmark', dest='set-xmark', action='append')\n add_arg('--save-mark', dest='save-mark', action='append')\n add_arg('--restore-mark', dest='restore-mark', action='append')\n add_arg('--and-mark', dest='and-mark', action='append')\n add_arg('--or-mark', dest='or-mark', action='append')\n add_arg('--xor-mark', dest='xor-mark', action='append')\n add_arg('--set-mark', dest='set-mark', action='append')\n add_arg('--nfmask', dest='nfmask', action='append')\n add_arg('--ctmask', dest='ctmask', action='append')\n ## CONNSECMARK\n add_arg('--save', dest='save', action='append')\n add_arg('--restore', dest='restore', action='append')\n ## CT\n add_arg('--notrack', dest='notrack', action='append')\n add_arg('--ctevents', dest='ctevents', action='append')\n add_arg('--expevents', dest='expevents', action='append')\n add_arg('--zone', dest='zone', action='append')\n add_arg('--timeout', dest='timeout', action='append')\n ## DNAT\n add_arg('--to-destination', dest='to-destination', action='append')\n add_arg('--random', dest='random', action='append')\n add_arg('--persistent', dest='persistent', action='append')\n ## DNPT\n add_arg('--src-pfx', dest='src-pfx', action='append')\n add_arg('--dst-pfx', dest='dst-pfx', action='append')\n ## DSCP\n add_arg('--set-dscp', dest='set-dscp', action='append')\n add_arg('--set-dscp-class', dest='set-dscp-class', action='append')\n ## ECN\n add_arg('--ecn-tcp-remove', dest='ecn-tcp-remove', action='append')\n ## HL\n add_arg('--hl-set', dest='hl-set', action='append')\n add_arg('--hl-dec', dest='hl-dec', action='append')\n add_arg('--hl-inc', dest='hl-inc', action='append')\n ## HMARK\n add_arg('--hmark-tuple', dest='hmark-tuple', action='append')\n add_arg('--hmark-mod', dest='hmark-mod', action='append')\n add_arg('--hmark-offset', dest='hmark-offset', action='append')\n add_arg('--hmark-src-prefix', dest='hmark-src-prefix', action='append')\n add_arg('--hmark-dst-prefix', dest='hmark-dst-prefix', action='append')\n add_arg('--hmark-sport-mask', dest='hmark-sport-mask', action='append')\n add_arg('--hmark-dport-mask', dest='hmark-dport-mask', action='append')\n add_arg('--hmark-spi-mask', dest='hmark-spi-mask', action='append')\n add_arg('--hmark-proto-mask', dest='hmark-proto-mask', action='append')\n add_arg('--hmark-rnd', dest='hmark-rnd', action='append')\n ## LED\n add_arg('--led-trigger-id', dest='led-trigger-id', action='append')\n add_arg('--led-delay', dest='led-delay', action='append')\n add_arg('--led-always-blink', dest='led-always-blink', action='append')\n ## LOG\n add_arg('--log-level', dest='log-level', action='append')\n add_arg('--log-prefix', dest='log-prefix', action='append')\n add_arg('--log-tcp-sequence', dest='log-tcp-sequence', action='append')\n add_arg('--log-tcp-options', dest='log-tcp-options', action='append')\n add_arg('--log-ip-options', dest='log-ip-options', action='append')\n add_arg('--log-uid', dest='log-uid', action='append')\n ## MASQUERADE\n add_arg('--to-ports', dest='to-ports', action='append')\n ## NFLOG\n add_arg('--nflog-group', dest='nflog-group', action='append')\n add_arg('--nflog-prefix', dest='nflog-prefix', action='append')\n add_arg('--nflog-range', dest='nflog-range', action='append')\n add_arg('--nflog-threshold', dest='nflog-threshold', action='append')\n ## NFQUEUE\n add_arg('--queue-num', dest='queue-num', action='append')\n add_arg('--queue-balance', dest='queue-balance', action='append')\n add_arg('--queue-bypass', dest='queue-bypass', action='append')\n add_arg('--queue-cpu-fanout', dest='queue-cpu-fanout', action='append')\n ## RATEEST\n add_arg('--rateest-ewmalog', dest='rateest-ewmalog', action='append')\n ## REJECT\n add_arg('--reject-with', dest='reject-with', action='append')\n ## SAME\n add_arg('--nodst', dest='nodst', action='append')\n ## SECMARK\n add_arg('--selctx', dest='selctx', action='append')\n ## SET\n add_arg('--add-set', dest='add-set', action='append')\n add_arg('--del-set', dest='del-set', action='append')\n add_arg('--exist', dest='exist', action='append')\n ## SNAT\n add_arg('--to-source', dest='to-source', action='append')\n ## TCPMSS\n add_arg('--set-mss', dest='set-mss', action='append')\n add_arg('--clamp-mss-to-pmtu', dest='clamp-mss-to-pmtu', action='append')\n ## TCPOPTSTRIP\n add_arg('--strip-options', dest='strip-options', action='append')\n ## TEE\n add_arg('--gateway', dest='gateway', action='append')\n ## TOS\n add_arg('--set-tos', dest='set-tos', action='append')\n add_arg('--and-tos', dest='and-tos', action='append')\n add_arg('--or-tos', dest='or-tos', action='append')\n add_arg('--xor-tos', dest='xor-tos', action='append')\n ## TPROXY\n add_arg('--on-port', dest='on-port', action='append')\n add_arg('--on-ip', dest='on-ip', action='append')\n add_arg('--tproxy-mark', dest='tproxy-mark', action='append')\n ## TTL\n add_arg('--ttl-set', dest='ttl-set', action='append')\n add_arg('--ttl-dec', dest='ttl-dec', action='append')\n add_arg('--ttl-inc', dest='ttl-inc', action='append')\n ## ULOG\n add_arg('--ulog-nlgroup', dest='ulog-nlgroup', action='append')\n add_arg('--ulog-prefix', dest='ulog-prefix', action='append')\n add_arg('--ulog-cprange', dest='ulog-cprange', action='append')\n add_arg('--ulog-qthreshold', dest='ulog-qthreshold', action='append')\n\n # Xtables-addons targets\n ## ACCOUNT\n add_arg('--addr', dest='addr', action='append')\n add_arg('--tname', dest='tname', action='append')\n ## CHAOS\n add_arg('--delude', dest='delude', action='append')\n add_arg('--tarpit', dest='tarpit', action='append')\n ## DHCPMAC\n add_arg('--set-mac', dest='set-mac', action='append')\n ## DNETMAP\n add_arg('--prefix', dest='prefix', action='append')\n add_arg('--reuse', dest='reuse', action='append')\n add_arg('--static', dest='static', action='append')\n ## IPMARK\n add_arg('--and-mask', dest='and-mask', action='append')\n add_arg('--or-mask', dest='or-mask', action='append')\n add_arg('--shift', dest='shift', action='append')\n ## TARPIT\n add_arg('--honeypot', dest='honeypot', action='append')\n add_arg('--reset', dest='reset', action='append')\n\n return parser", "response": "This function returns an argument parser that will parse all the options documented in the\n iptables ( 8 ) and iptables - extensions 8."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a dict of the changes required for a group.", "response": "def _changes(name,\n gid=None,\n addusers=None,\n delusers=None,\n members=None):\n '''\n Return a dict of the changes required for a group if the group is present,\n otherwise return False.\n '''\n lgrp = __salt__['group.info'](name)\n if not lgrp:\n return False\n\n # User and Domain names are not case sensitive in Windows. Let's make them\n # all lower case so we can compare properly\n if salt.utils.platform.is_windows():\n if lgrp['members']:\n lgrp['members'] = [user.lower() for user in lgrp['members']]\n if members:\n members = [salt.utils.win_functions.get_sam_name(user).lower() for user in members]\n if addusers:\n addusers = [salt.utils.win_functions.get_sam_name(user).lower() for user in addusers]\n if delusers:\n delusers = [salt.utils.win_functions.get_sam_name(user).lower() for user in delusers]\n\n change = {}\n ret = {}\n if gid:\n try:\n gid = int(gid)\n if lgrp['gid'] != gid:\n change['gid'] = gid\n except (TypeError, ValueError):\n ret['result'] = False\n ret['comment'] = 'Invalid gid'\n return ret\n\n if members is not None and not members:\n if set(lgrp['members']).symmetric_difference(members):\n change['delusers'] = set(lgrp['members'])\n elif members:\n # if new member list if different than the current\n if set(lgrp['members']).symmetric_difference(members):\n change['members'] = members\n\n if addusers:\n users_2add = [user for user in addusers if user not in lgrp['members']]\n if users_2add:\n change['addusers'] = users_2add\n\n if delusers:\n users_2del = [user for user in delusers if user in lgrp['members']]\n if users_2del:\n change['delusers'] = users_2del\n\n return change"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a list of paths to nix - env", "response": "def _nix_env():\n '''\n nix-env with quiet option. By default, nix is extremely verbose and prints the build log of every package to stderr. This tells nix to\n only show changes.\n '''\n nixhome = os.path.join(os.path.expanduser('~{0}'.format(__opts__['user'])), '.nix-profile/bin/')\n return [os.path.join(nixhome, 'nix-env')]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _nix_collect_garbage():\n '''\n Make sure we get the right nix-store, too.\n '''\n nixhome = os.path.join(os.path.expanduser('~{0}'.format(__opts__['user'])), '.nix-profile/bin/')\n return [os.path.join(nixhome, 'nix-collect-garbage')]", "response": "Return the nix - store - garbage directory"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nintersperse x into ys with an extra element at the beginning.", "response": "def _zip_flatten(x, ys):\n '''\n intersperse x into ys, with an extra element at the beginning.\n '''\n return itertools.chain.from_iterable(\n zip(itertools.repeat(x), ys))"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nformat the output of the list of packages affected by operation.", "response": "def _output_format(out,\n operation):\n '''\n gets a list of all the packages that were affected by ``operation``, splits it up (there can be multiple packages on a line), and then\n flattens that list. We make it to a list for easier parsing.\n '''\n return [s.split()[1:] for s in out\n if s.startswith(operation)]"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nrunning an update operation on the specified packages or all packages if none is specified.", "response": "def upgrade(*pkgs):\n '''\n Runs an update operation on the specified packages, or all packages if none is specified.\n\n :type pkgs: list(str)\n :param pkgs:\n List of packages to update\n\n :return: The upgraded packages. Example element: ``['libxslt-1.1.0', 'libxslt-1.1.10']``\n :rtype: list(tuple(str, str))\n\n .. code-block:: bash\n\n salt '*' nix.update\n salt '*' nix.update pkgs=one,two\n '''\n cmd = _quietnix()\n cmd.append('--upgrade')\n cmd.extend(pkgs)\n\n out = _run(cmd)\n\n upgrades = [_format_upgrade(s.split(maxsplit=1)[1])\n for s in out['stderr'].splitlines()\n if s.startswith('upgrading')]\n\n return [[_strip_quotes(s_) for s_ in s]\n for s in upgrades]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef install(*pkgs, **kwargs):\n '''\n Installs a single or multiple packages via nix\n\n :type pkgs: list(str)\n :param pkgs:\n packages to update\n :param bool attributes:\n Pass the list of packages or single package as attribues, not package names.\n default: False\n\n :return: Installed packages. Example element: ``gcc-3.3.2``\n :rtype: list(str)\n\n .. code-block:: bash\n\n salt '*' nix.install package [package2 ...]\n salt '*' nix.install attributes=True attr.name [attr.name2 ...]\n '''\n\n attributes = kwargs.get('attributes', False)\n\n if not pkgs:\n return \"Plese specify a package or packages to upgrade\"\n\n cmd = _quietnix()\n cmd.append('--install')\n\n if kwargs.get('attributes', False):\n cmd.extend(_zip_flatten('--attr', pkgs))\n else:\n cmd.extend(pkgs)\n\n out = _run(cmd)\n\n installs = list(itertools.chain.from_iterable(\n [s.split()[1:] for s in out['stderr'].splitlines()\n if s.startswith('installing')]\n ))\n\n return [_strip_quotes(s) for s in installs]", "response": "Installs a single or multiple packages via nix. install package."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_pkgs(installed=True,\n attributes=True):\n '''\n Lists installed packages. Due to how nix works, it defaults to just doing a ``nix-env -q``.\n\n :param bool installed:\n list only installed packages. This can be a very long list (12,000+ elements), so caution is advised.\n Default: True\n\n :param bool attributes:\n show the attributes of the packages when listing all packages.\n Default: True\n\n :return: Packages installed or available, along with their attributes.\n :rtype: list(list(str))\n\n .. code-block:: bash\n\n salt '*' nix.list_pkgs\n salt '*' nix.list_pkgs installed=False\n '''\n\n # We don't use -Q here, as it obfuscates the attribute names on full package listings.\n cmd = _nix_env()\n cmd.append('--query')\n\n if installed:\n # explicitly add this option for consistency, it's normally the default\n cmd.append('--installed')\n if not installed:\n cmd.append('--available')\n # We only show attributes if we're not doing an `installed` run.\n # The output of `nix-env -qaP` and `nix-env -qP` are vastly different:\n # `nix-env -qaP` returns a list such as 'attr.path name-version'\n # `nix-env -qP` returns a list of 'installOrder name-version'\n # Install order is useful to unambiguously select packages on a single\n # machine, but on more than one it can be a bad thing to specify.\n if attributes:\n cmd.append('--attr-path')\n\n out = _run(cmd)\n\n return [s.split() for s in salt.utils.itertools.split(out['stdout'], '\\n')]", "response": "Lists the packages installed or available on the current machine."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef uninstall(*pkgs):\n '''\n Erases a package from the current nix profile. Nix uninstalls work differently than other package managers, and the symlinks in the\n profile are removed, while the actual package remains. There is also a ``nix.purge`` function, to clear the package cache of unused\n packages.\n\n :type pkgs: list(str)\n :param pkgs:\n List, single package to uninstall\n\n :return: Packages that have been uninstalled\n :rtype: list(str)\n\n .. code-block:: bash\n\n salt '*' nix.uninstall pkg1 [pkg2 ...]\n '''\n\n cmd = _quietnix()\n cmd.append('--uninstall')\n cmd.extend(pkgs)\n\n out = _run(cmd)\n\n fmtout = out['stderr'].splitlines(), 'uninstalling'\n\n return [_strip_quotes(s.split()[1])\n for s in out['stderr'].splitlines()\n if s.startswith('uninstalling')]", "response": "Uninstalls a single package from the current nix profile."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef collect_garbage():\n '''\n Completely removed all currently 'uninstalled' packages in the nix store.\n\n Tells the user how many store paths were removed and how much space was freed.\n\n :return: How much space was freed and how many derivations were removed\n :rtype: str\n\n .. warning::\n This is a destructive action on the nix store.\n\n .. code-block:: bash\n\n salt '*' nix.collect_garbage\n '''\n cmd = _nix_collect_garbage()\n cmd.append('--delete-old')\n\n out = _run(cmd)\n\n return out['stdout'].splitlines()", "response": "Completely removed all currently uninstalled packages in the nix store and returns a list of lines that were removed and how much space was freed."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a single external pillars from a RethinkDB database.", "response": "def ext_pillar(minion_id,\n pillar,\n table='pillar',\n id_field=None,\n field=None,\n pillar_key=None):\n '''\n Collect minion external pillars from a RethinkDB database\n\n Arguments:\n * `table`: The RethinkDB table containing external pillar information.\n Defaults to ``'pillar'``\n * `id_field`: Field in document containing the minion id.\n If blank then we assume the table index matches minion ids\n * `field`: Specific field in the document used for pillar data, if blank\n then the entire document will be used\n * `pillar_key`: The salt-master will nest found external pillars under\n this key before merging into the minion pillars. If blank, external\n pillars will be merged at top level\n '''\n host = __opts__['rethinkdb.host']\n port = __opts__['rethinkdb.port']\n database = __opts__['rethinkdb.database']\n username = __opts__['rethinkdb.username']\n password = __opts__['rethinkdb.password']\n\n log.debug('Connecting to %s:%s as user \\'%s\\' for RethinkDB ext_pillar',\n host, port, username)\n\n # Connect to the database\n conn = rethinkdb.connect(host=host,\n port=port,\n db=database,\n user=username,\n password=password)\n\n data = None\n\n try:\n\n if id_field:\n log.debug('ext_pillar.rethinkdb: looking up pillar. '\n 'table: %s, field: %s, minion: %s',\n table, id_field, minion_id)\n\n if field:\n data = rethinkdb.table(table).filter(\n {id_field: minion_id}).pluck(field).run(conn)\n else:\n data = rethinkdb.table(table).filter(\n {id_field: minion_id}).run(conn)\n\n else:\n log.debug('ext_pillar.rethinkdb: looking up pillar. '\n 'table: %s, field: id, minion: %s',\n table, minion_id)\n\n if field:\n data = rethinkdb.table(table).get(minion_id).pluck(field).run(\n conn)\n else:\n data = rethinkdb.table(table).get(minion_id).run(conn)\n\n finally:\n if conn.is_open():\n conn.close()\n\n if data.items:\n\n # Return nothing if multiple documents are found for a minion\n if len(data.items) > 1:\n log.error('ext_pillar.rethinkdb: ambiguous documents found for '\n 'minion %s', minion_id)\n return {}\n\n else:\n result = data.items.pop()\n\n if pillar_key:\n return {pillar_key: result}\n return result\n\n else:\n # No document found in the database\n log.debug('ext_pillar.rethinkdb: no document found')\n return {}"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _proc_function(self, fun, low, user, tag, jid, daemonize=True):\n '''\n Same as original _proc_function in AsyncClientMixin,\n except it calls \"low\" without firing a print event.\n '''\n if daemonize and not salt.utils.platform.is_windows():\n salt.log.setup.shutdown_multiprocessing_logging()\n salt.utils.process.daemonize()\n salt.log.setup.setup_multiprocessing_logging()\n\n low['__jid__'] = jid\n low['__user__'] = user\n low['__tag__'] = tag\n\n return self.low(fun, low, print_event=False, full_return=False)", "response": "Same as original _proc_function in AsyncClientMixin except it calls low without firing a print event."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef run(*args, **kwargs):\n '''\n Execute a puppet run and return a dict with the stderr, stdout,\n return code, etc. The first positional argument given is checked as a\n subcommand. Following positional arguments should be ordered with arguments\n required by the subcommand first, followed by non-keyword arguments.\n Tags are specified by a tag keyword and comma separated list of values. --\n http://docs.puppetlabs.com/puppet/latest/reference/lang_tags.html\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' puppet.run\n salt '*' puppet.run tags=basefiles::edit,apache::server\n salt '*' puppet.run agent onetime no-daemonize no-usecacheonfailure no-splay ignorecache\n salt '*' puppet.run debug\n salt '*' puppet.run apply /a/b/manifest.pp modulepath=/a/b/modules tags=basefiles::edit,apache::server\n '''\n puppet = _Puppet()\n\n # new args tuple to filter out agent/apply for _Puppet.arguments()\n buildargs = ()\n for arg in range(len(args)):\n # based on puppet documentation action must come first. making the same\n # assertion. need to ensure the list of supported cmds here matches\n # those defined in _Puppet.arguments()\n if args[arg] in ['agent', 'apply']:\n puppet.subcmd = args[arg]\n else:\n buildargs += (args[arg],)\n # args will exist as an empty list even if none have been provided\n puppet.arguments(buildargs)\n\n puppet.kwargs.update(salt.utils.args.clean_kwargs(**kwargs))\n\n ret = __salt__['cmd.run_all'](repr(puppet), python_shell=True)\n return ret", "response": "Execute a puppet run and return a dict with the stderr stdout and return code etc."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef enable():\n '''\n .. versionadded:: 2014.7.0\n\n Enable the puppet agent\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' puppet.enable\n '''\n puppet = _Puppet()\n\n if os.path.isfile(puppet.disabled_lockfile):\n try:\n os.remove(puppet.disabled_lockfile)\n except (IOError, OSError) as exc:\n msg = 'Failed to enable: {0}'.format(exc)\n log.error(msg)\n raise CommandExecutionError(msg)\n else:\n return True\n return False", "response": "Enable the puppet agent\nTaxonomy CLI Example"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndisabling the puppet agent and set the disabled message to send to the puppet agent", "response": "def disable(message=None):\n '''\n .. versionadded:: 2014.7.0\n\n Disable the puppet agent\n\n message\n .. versionadded:: 2015.5.2\n\n Disable message to send to puppet\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' puppet.disable\n salt '*' puppet.disable 'Disabled, contact XYZ before enabling'\n '''\n\n puppet = _Puppet()\n\n if os.path.isfile(puppet.disabled_lockfile):\n return False\n else:\n with salt.utils.files.fopen(puppet.disabled_lockfile, 'w') as lockfile:\n try:\n # Puppet chokes when no valid json is found\n msg = '{{\"disabled_message\":\"{0}\"}}'.format(message) if message is not None else '{}'\n lockfile.write(salt.utils.stringutils.to_str(msg))\n lockfile.close()\n return True\n except (IOError, OSError) as exc:\n msg = 'Failed to disable: {0}'.format(exc)\n log.error(msg)\n raise CommandExecutionError(msg)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef status():\n '''\n .. versionadded:: 2014.7.0\n\n Display puppet agent status\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' puppet.status\n '''\n puppet = _Puppet()\n\n if os.path.isfile(puppet.disabled_lockfile):\n return 'Administratively disabled'\n\n if os.path.isfile(puppet.run_lockfile):\n try:\n with salt.utils.files.fopen(puppet.run_lockfile, 'r') as fp_:\n pid = int(salt.utils.stringutils.to_unicode(fp_.read()))\n os.kill(pid, 0) # raise an OSError if process doesn't exist\n except (OSError, ValueError):\n return 'Stale lockfile'\n else:\n return 'Applying a catalog'\n\n if os.path.isfile(puppet.agent_pidfile):\n try:\n with salt.utils.files.fopen(puppet.agent_pidfile, 'r') as fp_:\n pid = int(salt.utils.stringutils.to_unicode(fp_.read()))\n os.kill(pid, 0) # raise an OSError if process doesn't exist\n except (OSError, ValueError):\n return 'Stale pidfile'\n else:\n return 'Idle daemon'\n\n return 'Stopped'", "response": "Display the status of the current nacacatalatalite"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nshow a summary of the last puppet agent run", "response": "def summary():\n '''\n .. versionadded:: 2014.7.0\n\n Show a summary of the last puppet agent run\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' puppet.summary\n '''\n\n puppet = _Puppet()\n\n try:\n with salt.utils.files.fopen(puppet.lastrunfile, 'r') as fp_:\n report = salt.utils.yaml.safe_load(fp_)\n result = {}\n\n if 'time' in report:\n try:\n result['last_run'] = datetime.datetime.fromtimestamp(\n int(report['time']['last_run'])).isoformat()\n except (TypeError, ValueError, KeyError):\n result['last_run'] = 'invalid or missing timestamp'\n\n result['time'] = {}\n for key in ('total', 'config_retrieval'):\n if key in report['time']:\n result['time'][key] = report['time'][key]\n\n if 'resources' in report:\n result['resources'] = report['resources']\n\n except salt.utils.yaml.YAMLError as exc:\n raise CommandExecutionError(\n 'YAML error parsing puppet run summary: {0}'.format(exc)\n )\n except IOError as exc:\n raise CommandExecutionError(\n 'Unable to read puppet run summary: {0}'.format(exc)\n )\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nruns facter and return the results CLI Example : bash salt '*' puppet. facts", "response": "def facts(puppet=False):\n '''\n Run facter and return the results\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' puppet.facts\n '''\n ret = {}\n opt_puppet = '--puppet' if puppet else ''\n cmd_ret = __salt__['cmd.run_all']('facter {0}'.format(opt_puppet))\n\n if cmd_ret['retcode'] != 0:\n raise CommandExecutionError(cmd_ret['stderr'])\n\n output = cmd_ret['stdout']\n\n # Loop over the facter output and properly\n # parse it into a nice dictionary for using\n # elsewhere\n for line in output.splitlines():\n if not line:\n continue\n fact, value = _format_fact(line)\n if not fact:\n continue\n ret[fact] = value\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nrun facter for a specific fact", "response": "def fact(name, puppet=False):\n '''\n Run facter for a specific fact\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' puppet.fact kernel\n '''\n opt_puppet = '--puppet' if puppet else ''\n ret = __salt__['cmd.run_all'](\n 'facter {0} {1}'.format(opt_puppet, name),\n python_shell=False)\n\n if ret['retcode'] != 0:\n raise CommandExecutionError(ret['stderr'])\n\n if not ret['stdout']:\n return ''\n return ret['stdout']"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nread in the arguments for the current subcommand.", "response": "def arguments(self, args=None):\n '''\n Read in arguments for the current subcommand. These are added to the\n cmd line without '--' appended. Any others are redirected as standard\n options with the double hyphen prefixed.\n '''\n # permits deleting elements rather than using slices\n args = args and list(args) or []\n\n # match against all known/supported subcmds\n if self.subcmd == 'apply':\n # apply subcommand requires a manifest file to execute\n self.subcmd_args = [args[0]]\n del args[0]\n\n if self.subcmd == 'agent':\n # no arguments are required\n args.extend([\n 'test'\n ])\n\n # finally do this after subcmd has been matched for all remaining args\n self.args = args"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nensure IAM access keys are present for a new user.", "response": "def keys_present(name, number, save_dir, region=None, key=None, keyid=None, profile=None,\n save_format=\"{2}\\n{0}\\n{3}\\n{1}\\n\"):\n '''\n .. versionadded:: 2015.8.0\n\n Ensure the IAM access keys are present.\n\n name (string)\n The name of the new user.\n\n number (int)\n Number of keys that user should have.\n\n save_dir (string)\n The directory that the key/keys will be saved. Keys are saved to a file named according\n to the username privided.\n\n region (string)\n Region to connect to.\n\n key (string)\n Secret key to be used.\n\n keyid (string)\n Access key to be used.\n\n profile (dict)\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n\n save_format (dict)\n Save format is repeated for each key. Default format is\n \"{2}\\\\n{0}\\\\n{3}\\\\n{1}\\\\n\", where {0} and {1} are placeholders for new\n key_id and key respectively, whereas {2} and {3} are \"key_id-{number}\"\n and 'key-{number}' strings kept for compatibility.\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n if not __salt__['boto_iam.get_user'](name, region, key, keyid, profile):\n ret['result'] = False\n ret['comment'] = 'IAM User {0} does not exist.'.format(name)\n return ret\n if not isinstance(number, int):\n ret['comment'] = 'The number of keys must be an integer.'\n ret['result'] = False\n return ret\n if not os.path.isdir(save_dir):\n ret['comment'] = 'The directory {0} does not exist.'.format(save_dir)\n ret['result'] = False\n return ret\n keys = __salt__['boto_iam.get_all_access_keys'](user_name=name, region=region, key=key,\n keyid=keyid, profile=profile)\n if isinstance(keys, six.string_types):\n log.debug('keys are : false %s', keys)\n error, message = _get_error(keys)\n ret['comment'] = 'Could not get keys.\\n{0}\\n{1}'.format(error, message)\n ret['result'] = False\n return ret\n keys = keys['list_access_keys_response']['list_access_keys_result']['access_key_metadata']\n log.debug('Keys are : %s.', keys)\n if len(keys) >= number:\n ret['comment'] = 'The number of keys exist for user {0}'.format(name)\n ret['result'] = True\n return ret\n if __opts__['test']:\n ret['comment'] = 'Access key is set to be created for {0}.'.format(name)\n ret['result'] = None\n return ret\n new_keys = {}\n for i in range(number-len(keys)):\n created = __salt__['boto_iam.create_access_key'](name, region, key, keyid, profile)\n if isinstance(created, six.string_types):\n error, message = _get_error(created)\n ret['comment'] = 'Could not create keys.\\n{0}\\n{1}'.format(error, message)\n ret['result'] = False\n return ret\n log.debug('Created is : %s', created)\n response = 'create_access_key_response'\n result = 'create_access_key_result'\n new_keys[six.text_type(i)] = {}\n new_keys[six.text_type(i)]['key_id'] = created[response][result]['access_key']['access_key_id']\n new_keys[six.text_type(i)]['secret_key'] = created[response][result]['access_key']['secret_access_key']\n try:\n with salt.utils.files.fopen('{0}/{1}'.format(save_dir, name), 'a') as _wrf:\n for key_num, key in new_keys.items():\n key_id = key['key_id']\n secret_key = key['secret_key']\n _wrf.write(salt.utils.stringutils.to_str(\n save_format.format(\n key_id,\n secret_key,\n 'key_id-{0}'.format(key_num),\n 'key-{0}'.format(key_num)\n )\n ))\n ret['comment'] = 'Keys have been written to file {0}/{1}.'.format(save_dir, name)\n ret['result'] = True\n ret['changes'] = new_keys\n return ret\n except IOError:\n ret['comment'] = 'Could not write to file {0}/{1}.'.format(save_dir, name)\n ret['result'] = False\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef account_policy(name=None, allow_users_to_change_password=None,\n hard_expiry=None, max_password_age=None,\n minimum_password_length=None, password_reuse_prevention=None,\n require_lowercase_characters=None, require_numbers=None,\n require_symbols=None, require_uppercase_characters=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Change account policy.\n\n .. versionadded:: 2015.8.0\n\n name (string)\n The name of the account policy\n\n allow_users_to_change_password (bool)\n Allows all IAM users in your account to\n use the AWS Management Console to change their own passwords.\n\n hard_expiry (bool)\n Prevents IAM users from setting a new password after their\n password has expired.\n\n max_password_age (int)\n The number of days that an IAM user password is valid.\n\n minimum_password_length (int)\n The minimum number of characters allowed in an IAM user password.\n\n password_reuse_prevention (int)\n Specifies the number of previous passwords\n that IAM users are prevented from reusing.\n\n require_lowercase_characters (bool)\n Specifies whether IAM user passwords\n must contain at least one lowercase character from the ISO basic Latin alphabet (a to z).\n\n require_numbers (bool)\n Specifies whether IAM user passwords must contain at\n least one numeric character (0 to 9).\n\n require_symbols (bool)\n Specifies whether IAM user passwords must contain at\n least one of the following non-alphanumeric characters: ! @ # $ % ^ & * ( ) _ + - = [ ] { } | '\n\n require_uppercase_characters (bool)\n Specifies whether IAM user passwords must\n contain at least one uppercase character from the ISO basic Latin alphabet (A to Z).\n\n region (string)\n Region to connect to.\n\n key (string)\n Secret key to be used.\n\n keyid (string)\n Access key to be used.\n\n profile (dict)\n A dict with region, key and keyid, or a pillar key (string)\n '''\n config = locals()\n ret = {'name': 'Account Policy', 'result': True, 'comment': '', 'changes': {}}\n info = __salt__['boto_iam.get_account_policy'](region, key, keyid, profile)\n if not info:\n ret['comment'] = 'Account policy is not Enabled.'\n ret['result'] = False\n return ret\n for key, value in config.items():\n if key in ('region', 'key', 'keyid', 'profile', 'name'):\n continue\n if value is not None and six.text_type(info[key]) != six.text_type(value).lower():\n ret['comment'] = ' '.join([ret['comment'], 'Policy value {0} has been set to {1}.'.format(value, info[key])])\n ret['changes'][key] = six.text_type(value).lower()\n if not ret['changes']:\n ret['comment'] = 'Account policy is not changed.'\n return ret\n if __opts__['test']:\n ret['comment'] = 'Account policy is set to be changed.'\n ret['result'] = None\n return ret\n if __salt__['boto_iam.update_account_password_policy'](allow_users_to_change_password,\n hard_expiry,\n max_password_age,\n minimum_password_length,\n password_reuse_prevention,\n require_lowercase_characters,\n require_numbers,\n require_symbols,\n require_uppercase_characters,\n region, key, keyid, profile):\n return ret\n ret['comment'] = 'Account policy is not changed.'\n ret['changes'] = {}\n ret['result'] = False\n return ret", "response": "Return a new account policy."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef server_cert_present(name, public_key, private_key, cert_chain=None, path=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Crete server certificate.\n\n .. versionadded:: 2015.8.0\n\n name (string)\n The name for the server certificate. Do not include the path in this value.\n\n public_key (string)\n The contents of the public key certificate in PEM-encoded format.\n\n private_key (string)\n The contents of the private key in PEM-encoded format.\n\n cert_chain (string)\n The contents of the certificate chain. This is typically a\n concatenation of the PEM-encoded public key certificates of the chain.\n\n path (string)\n The path for the server certificate.\n\n region (string)\n The name of the region to connect to.\n\n key (string)\n The key to be used in order to connect\n\n keyid (string)\n The keyid to be used in order to connect\n\n profile (string)\n The profile that contains a dict of region, key, keyid\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n exists = __salt__['boto_iam.get_server_certificate'](name, region, key, keyid, profile)\n log.debug('Variables are : %s.', locals())\n if exists:\n ret['comment'] = 'Certificate {0} exists.'.format(name)\n return ret\n if 'salt://' in public_key:\n try:\n public_key = __salt__['cp.get_file_str'](public_key)\n except IOError as e:\n log.debug(e)\n ret['comment'] = 'File {0} not found.'.format(public_key)\n ret['result'] = False\n return ret\n if 'salt://' in private_key:\n try:\n private_key = __salt__['cp.get_file_str'](private_key)\n except IOError as e:\n log.debug(e)\n ret['comment'] = 'File {0} not found.'.format(private_key)\n ret['result'] = False\n return ret\n if cert_chain is not None and 'salt://' in cert_chain:\n try:\n cert_chain = __salt__['cp.get_file_str'](cert_chain)\n except IOError as e:\n log.debug(e)\n ret['comment'] = 'File {0} not found.'.format(cert_chain)\n ret['result'] = False\n return ret\n if __opts__['test']:\n ret['comment'] = 'Server certificate {0} is set to be created.'.format(name)\n ret['result'] = None\n return ret\n created = __salt__['boto_iam.upload_server_cert'](name, public_key, private_key, cert_chain,\n path, region, key, keyid, profile)\n if created is not False:\n ret['comment'] = 'Certificate {0} was created.'.format(name)\n ret['changes'] = created\n return ret\n ret['result'] = False\n ret['comment'] = 'Certificate {0} failed to be created.'.format(name)\n return ret", "response": "Ensures that the server certificate is present in the current region."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure SAML provider with the specified name is present.", "response": "def saml_provider_present(name, saml_metadata_document, region=None, key=None, keyid=None, profile=None):\n '''\n .. versionadded:: 2016.11.0\n\n Ensure the SAML provider with the specified name is present.\n\n name (string)\n The name of the SAML provider.\n\n saml_metadata_document (string)\n The xml document of the SAML provider.\n\n region (string)\n Region to connect to.\n\n key (string)\n Secret key to be used.\n\n keyid (string)\n Access key to be used.\n\n profile (dict)\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n if 'salt://' in saml_metadata_document:\n try:\n saml_metadata_document = __salt__['cp.get_file_str'](saml_metadata_document)\n ET.fromstring(saml_metadata_document)\n except IOError as e:\n log.debug(e)\n ret['comment'] = 'SAML document file {0} not found or could not be loaded'.format(name)\n ret['result'] = False\n return ret\n for provider in __salt__['boto_iam.list_saml_providers'](region=region,\n key=key, keyid=keyid,\n profile=profile):\n if provider == name:\n ret['comment'] = 'SAML provider {0} is present.'.format(name)\n return ret\n if __opts__['test']:\n ret['comment'] = 'SAML provider {0} is set to be create.'.format(name)\n ret['result'] = None\n return ret\n created = __salt__['boto_iam.create_saml_provider'](name, saml_metadata_document,\n region=region, key=key, keyid=keyid,\n profile=profile)\n if created:\n ret['comment'] = 'SAML provider {0} was created.'.format(name)\n ret['changes']['new'] = name\n return ret\n ret['result'] = False\n ret['comment'] = 'SAML provider {0} failed to be created.'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nensure the SAML provider with the specified name is absent.", "response": "def saml_provider_absent(name, region=None, key=None, keyid=None, profile=None):\n '''\n .. versionadded:: 2016.11.0\n\n Ensure the SAML provider with the specified name is absent.\n\n name (string)\n The name of the SAML provider.\n\n saml_metadata_document (string)\n The xml document of the SAML provider.\n\n region (string)\n Region to connect to.\n\n key (string)\n Secret key to be used.\n\n keyid (string)\n Access key to be used.\n\n profile (dict)\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n provider = __salt__['boto_iam.list_saml_providers'](region=region,\n key=key, keyid=keyid,\n profile=profile)\n if not provider:\n ret['comment'] = 'SAML provider {0} is absent.'.format(name)\n return ret\n if __opts__['test']:\n ret['comment'] = 'SAML provider {0} is set to be removed.'.format(name)\n ret['result'] = None\n return ret\n deleted = __salt__['boto_iam.delete_saml_provider'](name, region=region,\n key=key, keyid=keyid,\n profile=profile)\n if deleted is not False:\n ret['comment'] = 'SAML provider {0} was deleted.'.format(name)\n ret['changes']['old'] = name\n return ret\n ret['result'] = False\n ret['comment'] = 'SAML provider {0} failed to be deleted.'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nprovide the BGP configuration on the device. :param group: Name of the group selected to display the configuration. :param neighbor: IP Address of the neighbor to display the configuration. If the group parameter is not specified, the neighbor setting will be ignored. :return: A dictionary containing the BGP configuration from the network device. The keys of the main dictionary are the group names. Each group has the following properties: * type (string) * description (string) * apply_groups (string list) * multihop_ttl (int) * multipath (True/False) * local_address (string) * local_as (int) * remote_as (int) * import_policy (string) * export_policy (string) * remove_private_as (True/False) * prefix_limit (dictionary) * neighbors (dictionary) Each neighbor in the dictionary of neighbors provides: * description (string) * import_policy (string) * export_policy (string) * local_address (string) * local_as (int) * remote_as (int) * authentication_key (string) * prefix_limit (dictionary) * route_reflector_client (True/False) * nhs (True/False) CLI Example: .. code-block:: bash salt '*' bgp.config # entire BGP config salt '*' bgp.config PEERS-GROUP-NAME # provides detail only about BGP group PEERS-GROUP-NAME salt '*' bgp.config PEERS-GROUP-NAME 172.17.17.1 # provides details only about BGP neighbor 172.17.17.1, # configured in the group PEERS-GROUP-NAME Output Example: .. code-block:: python { 'PEERS-GROUP-NAME':{ 'type' : 'external', 'description' : 'Here we should have a nice description', 'apply_groups' : ['BGP-PREFIX-LIMIT'], 'import_policy' : 'PUBLIC-PEER-IN', 'export_policy' : 'PUBLIC-PEER-OUT', 'remove_private': True, 'multipath' : True, 'multihop_ttl' : 30, 'neighbors' : { '192.168.0.1': { 'description' : 'Facebook [CDN]', 'prefix_limit' : { 'inet': { 'unicast': { 'limit': 100, 'teardown': { 'threshold' : 95, 'timeout' : 5 } } } } 'peer-as' : 32934, 'route_reflector': False, 'nhs' : True }, '172.17.17.1': { 'description' : 'Twitter [CDN]', 'prefix_limit' : { 'inet': { 'unicast': { 'limit': 500, 'no-validate': 'IMPORT-FLOW-ROUTES' } } } 'peer_as' : 13414 'route_reflector': False, 'nhs' : False } } } }", "response": "def config(group=None, neighbor=None, **kwargs):\n\n '''\n Provides the BGP configuration on the device.\n\n :param group: Name of the group selected to display the configuration.\n :param neighbor: IP Address of the neighbor to display the configuration.\n If the group parameter is not specified, the neighbor setting will be\n ignored.\n\n :return: A dictionary containing the BGP configuration from the network\n device. The keys of the main dictionary are the group names.\n\n Each group has the following properties:\n\n * type (string)\n * description (string)\n * apply_groups (string list)\n * multihop_ttl (int)\n * multipath (True/False)\n * local_address (string)\n * local_as (int)\n * remote_as (int)\n * import_policy (string)\n * export_policy (string)\n * remove_private_as (True/False)\n * prefix_limit (dictionary)\n * neighbors (dictionary)\n\n Each neighbor in the dictionary of neighbors provides:\n\n * description (string)\n * import_policy (string)\n * export_policy (string)\n * local_address (string)\n * local_as (int)\n * remote_as (int)\n * authentication_key (string)\n * prefix_limit (dictionary)\n * route_reflector_client (True/False)\n * nhs (True/False)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' bgp.config # entire BGP config\n salt '*' bgp.config PEERS-GROUP-NAME # provides detail only about BGP group PEERS-GROUP-NAME\n salt '*' bgp.config PEERS-GROUP-NAME 172.17.17.1 # provides details only about BGP neighbor 172.17.17.1,\n # configured in the group PEERS-GROUP-NAME\n\n Output Example:\n\n .. code-block:: python\n\n {\n 'PEERS-GROUP-NAME':{\n 'type' : 'external',\n 'description' : 'Here we should have a nice description',\n 'apply_groups' : ['BGP-PREFIX-LIMIT'],\n 'import_policy' : 'PUBLIC-PEER-IN',\n 'export_policy' : 'PUBLIC-PEER-OUT',\n 'remove_private': True,\n 'multipath' : True,\n 'multihop_ttl' : 30,\n 'neighbors' : {\n '192.168.0.1': {\n 'description' : 'Facebook [CDN]',\n 'prefix_limit' : {\n 'inet': {\n 'unicast': {\n 'limit': 100,\n 'teardown': {\n 'threshold' : 95,\n 'timeout' : 5\n }\n }\n }\n }\n 'peer-as' : 32934,\n 'route_reflector': False,\n 'nhs' : True\n },\n '172.17.17.1': {\n 'description' : 'Twitter [CDN]',\n 'prefix_limit' : {\n 'inet': {\n 'unicast': {\n 'limit': 500,\n 'no-validate': 'IMPORT-FLOW-ROUTES'\n }\n }\n }\n 'peer_as' : 13414\n 'route_reflector': False,\n 'nhs' : False\n }\n }\n }\n }\n '''\n\n return salt.utils.napalm.call(\n napalm_device, # pylint: disable=undefined-variable\n 'get_bgp_config',\n **{\n 'group': group,\n 'neighbor': neighbor\n }\n )"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nexecute a salt function on a specific minion", "response": "def _salt(fun, *args, **kw):\n '''Execute a salt function on a specific minion\n\n Special kwargs:\n\n salt_target\n target to exec things on\n salt_timeout\n timeout for jobs\n salt_job_poll\n poll interval to wait for job finish result\n '''\n try:\n poll = kw.pop('salt_job_poll')\n except KeyError:\n poll = 0.1\n try:\n target = kw.pop('salt_target')\n except KeyError:\n target = None\n try:\n timeout = int(kw.pop('salt_timeout'))\n except (KeyError, ValueError):\n # try to has some low timeouts for very basic commands\n timeout = __FUN_TIMEOUT.get(\n fun,\n 900 # wait up to 15 minutes for the default timeout\n )\n try:\n kwargs = kw.pop('kwargs')\n except KeyError:\n kwargs = {}\n if not target:\n infos = get_configured_provider()\n if not infos:\n return\n target = infos['target']\n laps = time.time()\n cache = False\n if fun in __CACHED_FUNS:\n cache = True\n laps = laps // __CACHED_FUNS[fun]\n try:\n sargs = salt.utils.json.dumps(args)\n except TypeError:\n sargs = ''\n try:\n skw = salt.utils.json.dumps(kw)\n except TypeError:\n skw = ''\n try:\n skwargs = salt.utils.json.dumps(kwargs)\n except TypeError:\n skwargs = ''\n cache_key = (laps, target, fun, sargs, skw, skwargs)\n if not cache or (cache and (cache_key not in __CACHED_CALLS)):\n conn = _client()\n runner = _runner()\n rkwargs = kwargs.copy()\n rkwargs['timeout'] = timeout\n rkwargs.setdefault('tgt_type', 'list')\n kwargs.setdefault('tgt_type', 'list')\n ping_retries = 0\n # the target(s) have environ one minute to respond\n # we call 60 ping request, this prevent us\n # from blindly send commands to unmatched minions\n ping_max_retries = 60\n ping = True\n # do not check ping... if we are pinguing\n if fun == 'test.ping':\n ping_retries = ping_max_retries + 1\n # be sure that the executors are alive\n while ping_retries <= ping_max_retries:\n try:\n if ping_retries > 0:\n time.sleep(1)\n pings = conn.cmd(tgt=target,\n timeout=10,\n fun='test.ping')\n values = list(pings.values())\n if not values:\n ping = False\n for v in values:\n if v is not True:\n ping = False\n if not ping:\n raise ValueError('Unreachable')\n break\n except Exception:\n ping = False\n ping_retries += 1\n log.error('%s unreachable, retrying', target)\n if not ping:\n raise SaltCloudSystemExit('Target {0} unreachable'.format(target))\n jid = conn.cmd_async(tgt=target,\n fun=fun,\n arg=args,\n kwarg=kw,\n **rkwargs)\n cret = conn.cmd(tgt=target,\n fun='saltutil.find_job',\n arg=[jid],\n timeout=10,\n **kwargs)\n running = bool(cret.get(target, False))\n endto = time.time() + timeout\n while running:\n rkwargs = {\n 'tgt': target,\n 'fun': 'saltutil.find_job',\n 'arg': [jid],\n 'timeout': 10\n }\n cret = conn.cmd(**rkwargs)\n running = bool(cret.get(target, False))\n if not running:\n break\n if running and (time.time() > endto):\n raise Exception('Timeout {0}s for {1} is elapsed'.format(\n timeout, pprint.pformat(rkwargs)))\n time.sleep(poll)\n # timeout for the master to return data about a specific job\n wait_for_res = float({\n 'test.ping': '5',\n }.get(fun, '120'))\n while wait_for_res:\n wait_for_res -= 0.5\n cret = runner.cmd(\n 'jobs.lookup_jid',\n [jid, {'__kwarg__': True}])\n if target in cret:\n ret = cret[target]\n break\n # recent changes\n elif 'data' in cret and 'outputter' in cret:\n ret = cret['data']\n break\n # special case, some answers may be crafted\n # to handle the unresponsivness of a specific command\n # which is also meaningful, e.g. a minion not yet provisioned\n if fun in ['test.ping'] and not wait_for_res:\n ret = {\n 'test.ping': False,\n }.get(fun, False)\n time.sleep(0.5)\n try:\n if 'is not available.' in ret:\n raise SaltCloudSystemExit(\n 'module/function {0} is not available'.format(fun))\n except SaltCloudSystemExit:\n raise\n except TypeError:\n pass\n if cache:\n __CACHED_CALLS[cache_key] = ret\n elif cache and cache_key in __CACHED_CALLS:\n ret = __CACHED_CALLS[cache_key]\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_nodes_select(call=None):\n '''\n Return a list of the VMs that are on the provider, with select fields\n '''\n if not call:\n call = 'select'\n if not get_configured_provider():\n return\n info = ['id', 'name', 'image', 'size', 'state', 'public_ips', 'private_ips']\n return salt.utils.cloud.list_nodes_select(\n list_nodes_full(call='action'),\n __opts__.get('query.selection', info), call)", "response": "Return a list of the VMs that are on the provider with select fields"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef destroy(vm_, call=None):\n '''Destroy a lxc container'''\n destroy_opt = __opts__.get('destroy', False)\n profiles = __opts__.get('profiles', {})\n profile = __opts__.get('profile',\n __opts__.get('internal_lxc_profile', []))\n path = None\n if profile and profile in profiles:\n path = profiles[profile].get('path', None)\n action = __opts__.get('action', '')\n if action != 'destroy' and not destroy_opt:\n raise SaltCloudSystemExit(\n 'The destroy action must be called with -d, --destroy, '\n '-a or --action.'\n )\n if not get_configured_provider():\n return\n ret = {'comment': '{0} was not found'.format(vm_),\n 'result': False}\n if _salt('lxc.info', vm_, path=path):\n __utils__['cloud.fire_event'](\n 'event',\n 'destroying instance',\n 'salt/cloud/{0}/destroying'.format(vm_),\n args={'name': vm_, 'instance_id': vm_},\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n cret = _salt('lxc.destroy', vm_, stop=True, path=path)\n ret['result'] = cret['result']\n if ret['result']:\n ret['comment'] = '{0} was destroyed'.format(vm_)\n __utils__['cloud.fire_event'](\n 'event',\n 'destroyed instance',\n 'salt/cloud/{0}/destroyed'.format(vm_),\n args={'name': vm_, 'instance_id': vm_},\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'](vm_, __active_provider_name__.split(':')[0], __opts__)\n return ret", "response": "Destroy a lxc container"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create(vm_, call=None):\n '''Create an lxc Container.\n This function is idempotent and will try to either provision\n or finish the provision of an lxc container.\n\n NOTE: Most of the initialization code has been moved and merged\n with the lxc runner and lxc.init functions\n '''\n prov = get_configured_provider(vm_)\n if not prov:\n return\n # we cant use profile as a configuration key as it conflicts\n # with salt cloud internals\n profile = vm_.get(\n 'lxc_profile',\n vm_.get('container_profile', None))\n\n event_data = vm_.copy()\n event_data['profile'] = profile\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', event_data, ['name', 'profile', 'provider', 'driver']),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n ret = {'name': vm_['name'], 'changes': {}, 'result': True, 'comment': ''}\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', vm_, __opts__))\n # get the minion key pair to distribute back to the container\n kwarg = copy.deepcopy(vm_)\n kwarg['host'] = prov['target']\n kwarg['profile'] = profile\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(vm_['name']),\n args=__utils__['cloud.filter_event']('requesting', vm_, ['name', 'profile', 'provider', 'driver']),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n cret = _runner().cmd('lxc.cloud_init', [vm_['name']], kwarg=kwarg)\n ret['runner_return'] = cret\n ret['result'] = cret['result']\n if not ret['result']:\n ret['Error'] = 'Error while creating {0},'.format(vm_['name'])\n else:\n ret['changes']['created'] = 'created'\n\n # When using cloud states to manage LXC containers\n # __opts__['profile'] is not implicitly reset between operations\n # on different containers. However list_nodes will hide container\n # if profile is set in opts assuming that it have to be created.\n # But in cloud state we do want to check at first if it really\n # exists hence the need to remove profile from global opts once\n # current container is created.\n if 'profile' in __opts__:\n __opts__['internal_lxc_profile'] = __opts__['profile']\n del __opts__['profile']\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 an lxc container"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the contextual provider of None if no configured provider can be found.", "response": "def get_configured_provider(vm_=None):\n '''\n Return the contextual provider of None if no configured\n one can be found.\n '''\n if vm_ is None:\n vm_ = {}\n dalias, driver = __active_provider_name__.split(':')\n data = None\n tgt = 'unknown'\n img_provider = __opts__.get('list_images', '')\n arg_providers = __opts__.get('names', [])\n matched = False\n # --list-images level\n if img_provider:\n tgt = 'provider: {0}'.format(img_provider)\n if dalias == img_provider:\n data = get_provider(img_provider)\n matched = True\n # providers are set in configuration\n if not data and 'profile' not in __opts__ and arg_providers:\n for name in arg_providers:\n tgt = 'provider: {0}'.format(name)\n if dalias == name:\n data = get_provider(name)\n if data:\n matched = True\n break\n # -p is providen, get the uplinked provider\n elif 'profile' in __opts__:\n curprof = __opts__['profile']\n profs = __opts__['profiles']\n tgt = 'profile: {0}'.format(curprof)\n if (\n curprof in profs and\n profs[curprof]['provider'] == __active_provider_name__\n ):\n prov, cdriver = profs[curprof]['provider'].split(':')\n tgt += ' provider: {0}'.format(prov)\n data = get_provider(prov)\n matched = True\n # fallback if we have only __active_provider_name__\n if (\n (__opts__.get('destroy', False) and not data) or (\n not matched and __active_provider_name__\n )\n ):\n data = __opts__.get('providers',\n {}).get(dalias, {}).get(driver, {})\n # in all cases, verify that the linked saltmaster is alive.\n if data:\n ret = _salt('test.ping', salt_target=data['target'])\n if ret:\n return data\n else:\n log.error(\n 'Configured provider %s minion: %s is unreachable',\n __active_provider_name__, data['target']\n )\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef write_launchd_plist(program):\n '''\n Write a launchd plist for managing salt-master or salt-minion\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run launchd.write_launchd_plist salt-master\n '''\n plist_sample_text = '''\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n <dict>\n <key>Label</key>\n <string>org.saltstack.{program}</string>\n <key>RunAtLoad</key>\n <true/>\n <key>KeepAlive</key>\n <true/>\n <key>ProgramArguments</key>\n <array>\n <string>{script}</string>\n </array>\n <key>SoftResourceLimits</key>\n <dict>\n <key>NumberOfFiles</key>\n <integer>100000</integer>\n </dict>\n <key>HardResourceLimits</key>\n <dict>\n <key>NumberOfFiles</key>\n <integer>100000</integer>\n </dict>\n </dict>\n</plist>\n '''.strip()\n\n supported_programs = ['salt-master', 'salt-minion']\n\n if program not in supported_programs:\n sys.stderr.write(\n 'Supported programs: \\'{0}\\'\\n'.format(supported_programs)\n )\n sys.exit(-1)\n\n return plist_sample_text.format(\n program=program,\n python=sys.executable,\n script=os.path.join(os.path.dirname(sys.executable), program)\n )", "response": "Writes a launchd plist for managing salt - master or salt - minion"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _windows_cpudata():\n '''\n Return some CPU information on Windows minions\n '''\n # Provides:\n # num_cpus\n # cpu_model\n grains = {}\n if 'NUMBER_OF_PROCESSORS' in os.environ:\n # Cast to int so that the logic isn't broken when used as a\n # conditional in templating. Also follows _linux_cpudata()\n try:\n grains['num_cpus'] = int(os.environ['NUMBER_OF_PROCESSORS'])\n except ValueError:\n grains['num_cpus'] = 1\n grains['cpu_model'] = salt.utils.win_reg.read_value(\n hive=\"HKEY_LOCAL_MACHINE\",\n key=\"HARDWARE\\\\DESCRIPTION\\\\System\\\\CentralProcessor\\\\0\",\n vname=\"ProcessorNameString\").get('vdata')\n return grains", "response": "Return some CPU information on Windows minions\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning some CPU information for Linux minions", "response": "def _linux_cpudata():\n '''\n Return some CPU information for Linux minions\n '''\n # Provides:\n # num_cpus\n # cpu_model\n # cpu_flags\n grains = {}\n cpuinfo = '/proc/cpuinfo'\n # Parse over the cpuinfo file\n if os.path.isfile(cpuinfo):\n with salt.utils.files.fopen(cpuinfo, 'r') as _fp:\n grains['num_cpus'] = 0\n for line in _fp:\n comps = line.split(':')\n if not len(comps) > 1:\n continue\n key = comps[0].strip()\n val = comps[1].strip()\n if key == 'processor':\n grains['num_cpus'] += 1\n elif key == 'model name':\n grains['cpu_model'] = val\n elif key == 'flags':\n grains['cpu_flags'] = val.split()\n elif key == 'Features':\n grains['cpu_flags'] = val.split()\n # ARM support - /proc/cpuinfo\n #\n # Processor : ARMv6-compatible processor rev 7 (v6l)\n # BogoMIPS : 697.95\n # Features : swp half thumb fastmult vfp edsp java tls\n # CPU implementer : 0x41\n # CPU architecture: 7\n # CPU variant : 0x0\n # CPU part : 0xb76\n # CPU revision : 7\n #\n # Hardware : BCM2708\n # Revision : 0002\n # Serial : 00000000\n elif key == 'Processor':\n grains['cpu_model'] = val.split('-')[0]\n grains['num_cpus'] = 1\n if 'num_cpus' not in grains:\n grains['num_cpus'] = 0\n if 'cpu_model' not in grains:\n grains['cpu_model'] = 'Unknown'\n if 'cpu_flags' not in grains:\n grains['cpu_flags'] = []\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _linux_gpu_data():\n '''\n num_gpus: int\n gpus:\n - vendor: nvidia|amd|ati|...\n model: string\n '''\n if __opts__.get('enable_lspci', True) is False:\n return {}\n\n if __opts__.get('enable_gpu_grains', True) is False:\n return {}\n\n lspci = salt.utils.path.which('lspci')\n if not lspci:\n log.debug(\n 'The `lspci` binary is not available on the system. GPU grains '\n 'will not be available.'\n )\n return {}\n\n # dominant gpu vendors to search for (MUST be lowercase for matching below)\n known_vendors = ['nvidia', 'amd', 'ati', 'intel', 'cirrus logic', 'vmware', 'matrox', 'aspeed']\n gpu_classes = ('vga compatible controller', '3d controller')\n\n devs = []\n try:\n lspci_out = __salt__['cmd.run']('{0} -vmm'.format(lspci))\n\n cur_dev = {}\n error = False\n # Add a blank element to the lspci_out.splitlines() list,\n # otherwise the last device is not evaluated as a cur_dev and ignored.\n lspci_list = lspci_out.splitlines()\n lspci_list.append('')\n for line in lspci_list:\n # check for record-separating empty lines\n if line == '':\n if cur_dev.get('Class', '').lower() in gpu_classes:\n devs.append(cur_dev)\n cur_dev = {}\n continue\n if re.match(r'^\\w+:\\s+.*', line):\n key, val = line.split(':', 1)\n cur_dev[key.strip()] = val.strip()\n else:\n error = True\n log.debug('Unexpected lspci output: \\'%s\\'', line)\n\n if error:\n log.warning(\n 'Error loading grains, unexpected linux_gpu_data output, '\n 'check that you have a valid shell configured and '\n 'permissions to run lspci command'\n )\n except OSError:\n pass\n\n gpus = []\n for gpu in devs:\n vendor_strings = re.split('[^A-Za-z0-9]', gpu['Vendor'].lower())\n # default vendor to 'unknown', overwrite if we match a known one\n vendor = 'unknown'\n for name in known_vendors:\n # search for an 'expected' vendor name in the list of strings\n if name in vendor_strings:\n vendor = name\n break\n gpus.append({'vendor': vendor, 'model': gpu['Device']})\n\n grains = {}\n grains['num_gpus'] = len(gpus)\n grains['gpus'] = gpus\n return grains", "response": "Return a list of linux gpu vendors and devices."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the netbsd GPU data for the current node", "response": "def _netbsd_gpu_data():\n '''\n num_gpus: int\n gpus:\n - vendor: nvidia|amd|ati|...\n model: string\n '''\n known_vendors = ['nvidia', 'amd', 'ati', 'intel', 'cirrus logic', 'vmware', 'matrox', 'aspeed']\n\n gpus = []\n try:\n pcictl_out = __salt__['cmd.run']('pcictl pci0 list')\n\n for line in pcictl_out.splitlines():\n for vendor in known_vendors:\n vendor_match = re.match(\n r'[0-9:]+ ({0}) (.+) \\(VGA .+\\)'.format(vendor),\n line,\n re.IGNORECASE\n )\n if vendor_match:\n gpus.append({'vendor': vendor_match.group(1), 'model': vendor_match.group(2)})\n except OSError:\n pass\n\n grains = {}\n grains['num_gpus'] = len(gpus)\n grains['gpus'] = gpus\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _osx_gpudata():\n '''\n num_gpus: int\n gpus:\n - vendor: nvidia|amd|ati|...\n model: string\n '''\n\n gpus = []\n try:\n pcictl_out = __salt__['cmd.run']('system_profiler SPDisplaysDataType')\n\n for line in pcictl_out.splitlines():\n fieldname, _, fieldval = line.partition(': ')\n if fieldname.strip() == \"Chipset Model\":\n vendor, _, model = fieldval.partition(' ')\n vendor = vendor.lower()\n gpus.append({'vendor': vendor, 'model': model})\n\n except OSError:\n pass\n\n grains = {}\n grains['num_gpus'] = len(gpus)\n grains['gpus'] = gpus\n return grains", "response": "Return the GPU data for the current system"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _bsd_cpudata(osdata):\n '''\n Return CPU information for BSD-like systems\n '''\n # Provides:\n # cpuarch\n # num_cpus\n # cpu_model\n # cpu_flags\n sysctl = salt.utils.path.which('sysctl')\n arch = salt.utils.path.which('arch')\n cmds = {}\n\n if sysctl:\n cmds.update({\n 'num_cpus': '{0} -n hw.ncpu'.format(sysctl),\n 'cpuarch': '{0} -n hw.machine'.format(sysctl),\n 'cpu_model': '{0} -n hw.model'.format(sysctl),\n })\n\n if arch and osdata['kernel'] == 'OpenBSD':\n cmds['cpuarch'] = '{0} -s'.format(arch)\n\n if osdata['kernel'] == 'Darwin':\n cmds['cpu_model'] = '{0} -n machdep.cpu.brand_string'.format(sysctl)\n cmds['cpu_flags'] = '{0} -n machdep.cpu.features'.format(sysctl)\n\n grains = dict([(k, __salt__['cmd.run'](v)) for k, v in six.iteritems(cmds)])\n\n if 'cpu_flags' in grains and isinstance(grains['cpu_flags'], six.string_types):\n grains['cpu_flags'] = grains['cpu_flags'].split(' ')\n\n if osdata['kernel'] == 'NetBSD':\n grains['cpu_flags'] = []\n for line in __salt__['cmd.run']('cpuctl identify 0').splitlines():\n cpu_match = re.match(r'cpu[0-9]:\\ features[0-9]?\\ .+<(.+)>', line)\n if cpu_match:\n flag = cpu_match.group(1).split(',')\n grains['cpu_flags'].extend(flag)\n\n if osdata['kernel'] == 'FreeBSD' and os.path.isfile('/var/run/dmesg.boot'):\n grains['cpu_flags'] = []\n # TODO: at least it needs to be tested for BSD other then FreeBSD\n with salt.utils.files.fopen('/var/run/dmesg.boot', 'r') as _fp:\n cpu_here = False\n for line in _fp:\n if line.startswith('CPU: '):\n cpu_here = True # starts CPU descr\n continue\n if cpu_here:\n if not line.startswith(' '):\n break # game over\n if 'Features' in line:\n start = line.find('<')\n end = line.find('>')\n if start > 0 and end > 0:\n flag = line[start + 1:end].split(',')\n grains['cpu_flags'].extend(flag)\n try:\n grains['num_cpus'] = int(grains['num_cpus'])\n except ValueError:\n grains['num_cpus'] = 1\n\n return grains", "response": "Return CPU information for BSD - like systems"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _sunos_cpudata():\n '''\n Return the CPU information for Solaris-like systems\n '''\n # Provides:\n # cpuarch\n # num_cpus\n # cpu_model\n # cpu_flags\n grains = {}\n grains['cpu_flags'] = []\n\n grains['cpuarch'] = __salt__['cmd.run']('isainfo -k')\n psrinfo = '/usr/sbin/psrinfo 2>/dev/null'\n grains['num_cpus'] = len(__salt__['cmd.run'](psrinfo, python_shell=True).splitlines())\n kstat_info = 'kstat -p cpu_info:*:*:brand'\n for line in __salt__['cmd.run'](kstat_info).splitlines():\n match = re.match(r'(\\w+:\\d+:\\w+\\d+:\\w+)\\s+(.+)', line)\n if match:\n grains['cpu_model'] = match.group(2)\n isainfo = 'isainfo -n -v'\n for line in __salt__['cmd.run'](isainfo).splitlines():\n match = re.match(r'^\\s+(.+)', line)\n if match:\n cpu_flags = match.group(1).split()\n grains['cpu_flags'].extend(cpu_flags)\n\n return grains", "response": "Return the CPU information for Solaris - like systems\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _aix_cpudata():\n '''\n Return CPU information for AIX systems\n '''\n # Provides:\n # cpuarch\n # num_cpus\n # cpu_model\n # cpu_flags\n grains = {}\n cmd = salt.utils.path.which('prtconf')\n if cmd:\n data = __salt__['cmd.run']('{0}'.format(cmd)) + os.linesep\n for dest, regstring in (('cpuarch', r'(?im)^\\s*Processor\\s+Type:\\s+(\\S+)'),\n ('cpu_flags', r'(?im)^\\s*Processor\\s+Version:\\s+(\\S+)'),\n ('cpu_model', r'(?im)^\\s*Processor\\s+Implementation\\s+Mode:\\s+(.*)'),\n ('num_cpus', r'(?im)^\\s*Number\\s+Of\\s+Processors:\\s+(\\S+)')):\n for regex in [re.compile(r) for r in [regstring]]:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n grains[dest] = res.group(1).strip().replace(\"'\", '')\n else:\n log.error('The \\'prtconf\\' binary was not found in $PATH.')\n return grains", "response": "Return CPU information for AIX systems\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the memory information for Linux - like systems", "response": "def _linux_memdata():\n '''\n Return the memory information for Linux-like systems\n '''\n grains = {'mem_total': 0, 'swap_total': 0}\n\n meminfo = '/proc/meminfo'\n if os.path.isfile(meminfo):\n with salt.utils.files.fopen(meminfo, 'r') as ifile:\n for line in ifile:\n comps = line.rstrip('\\n').split(':')\n if not len(comps) > 1:\n continue\n if comps[0].strip() == 'MemTotal':\n # Use floor division to force output to be an integer\n grains['mem_total'] = int(comps[1].split()[0]) // 1024\n if comps[0].strip() == 'SwapTotal':\n # Use floor division to force output to be an integer\n grains['swap_total'] = int(comps[1].split()[0]) // 1024\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the memory information for OSX - like systems", "response": "def _osx_memdata():\n '''\n Return the memory information for BSD-like systems\n '''\n grains = {'mem_total': 0, 'swap_total': 0}\n\n sysctl = salt.utils.path.which('sysctl')\n if sysctl:\n mem = __salt__['cmd.run']('{0} -n hw.memsize'.format(sysctl))\n swap_total = __salt__['cmd.run']('{0} -n vm.swapusage'.format(sysctl)).split()[2].replace(',', '.')\n if swap_total.endswith('K'):\n _power = 2**10\n elif swap_total.endswith('M'):\n _power = 2**20\n elif swap_total.endswith('G'):\n _power = 2**30\n swap_total = float(swap_total[:-1]) * _power\n\n grains['mem_total'] = int(mem) // 1024 // 1024\n grains['swap_total'] = int(swap_total) // 1024 // 1024\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _bsd_memdata(osdata):\n '''\n Return the memory information for BSD-like systems\n '''\n grains = {'mem_total': 0, 'swap_total': 0}\n\n sysctl = salt.utils.path.which('sysctl')\n if sysctl:\n mem = __salt__['cmd.run']('{0} -n hw.physmem'.format(sysctl))\n if osdata['kernel'] == 'NetBSD' and mem.startswith('-'):\n mem = __salt__['cmd.run']('{0} -n hw.physmem64'.format(sysctl))\n grains['mem_total'] = int(mem) // 1024 // 1024\n\n if osdata['kernel'] in ['OpenBSD', 'NetBSD']:\n swapctl = salt.utils.path.which('swapctl')\n swap_data = __salt__['cmd.run']('{0} -sk'.format(swapctl))\n if swap_data == 'no swap devices configured':\n swap_total = 0\n else:\n swap_total = swap_data.split(' ')[1]\n else:\n swap_total = __salt__['cmd.run']('{0} -n vm.swap_total'.format(sysctl))\n grains['swap_total'] = int(swap_total) // 1024 // 1024\n return grains", "response": "Return the memory information for BSD - like systems\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _sunos_memdata():\n '''\n Return the memory information for SunOS-like systems\n '''\n grains = {'mem_total': 0, 'swap_total': 0}\n\n prtconf = '/usr/sbin/prtconf 2>/dev/null'\n for line in __salt__['cmd.run'](prtconf, python_shell=True).splitlines():\n comps = line.split(' ')\n if comps[0].strip() == 'Memory' and comps[1].strip() == 'size:':\n grains['mem_total'] = int(comps[2].strip())\n\n swap_cmd = salt.utils.path.which('swap')\n swap_data = __salt__['cmd.run']('{0} -s'.format(swap_cmd)).split()\n try:\n swap_avail = int(swap_data[-2][:-1])\n swap_used = int(swap_data[-4][:-1])\n swap_total = (swap_avail + swap_used) // 1024\n except ValueError:\n swap_total = None\n grains['swap_total'] = swap_total\n return grains", "response": "Return the memory information for SunOS - like systems\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the memory information for AIX systems", "response": "def _aix_memdata():\n '''\n Return the memory information for AIX systems\n '''\n grains = {'mem_total': 0, 'swap_total': 0}\n prtconf = salt.utils.path.which('prtconf')\n if prtconf:\n for line in __salt__['cmd.run'](prtconf, python_shell=True).splitlines():\n comps = [x for x in line.strip().split(' ') if x]\n if len(comps) > 2 and 'Memory' in comps[0] and 'Size' in comps[1]:\n grains['mem_total'] = int(comps[2])\n break\n else:\n log.error('The \\'prtconf\\' binary was not found in $PATH.')\n\n swap_cmd = salt.utils.path.which('swap')\n if swap_cmd:\n swap_data = __salt__['cmd.run']('{0} -s'.format(swap_cmd)).split()\n try:\n swap_total = (int(swap_data[-2]) + int(swap_data[-6])) * 4\n except ValueError:\n swap_total = None\n grains['swap_total'] = swap_total\n else:\n log.error('The \\'swap\\' binary was not found in $PATH.')\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngathers information about the system memory.", "response": "def _memdata(osdata):\n '''\n Gather information about the system memory\n '''\n # Provides:\n # mem_total\n # swap_total, for supported systems.\n grains = {'mem_total': 0}\n if osdata['kernel'] == 'Linux':\n grains.update(_linux_memdata())\n elif osdata['kernel'] in ('FreeBSD', 'OpenBSD', 'NetBSD'):\n grains.update(_bsd_memdata(osdata))\n elif osdata['kernel'] == 'Darwin':\n grains.update(_osx_memdata())\n elif osdata['kernel'] == 'SunOS':\n grains.update(_sunos_memdata())\n elif osdata['kernel'] == 'AIX':\n grains.update(_aix_memdata())\n elif osdata['kernel'] == 'Windows' and HAS_WMI:\n grains.update(_windows_memdata())\n return grains"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nparse the output of lsattr - El sys0 for os_uuid", "response": "def _aix_get_machine_id():\n '''\n Parse the output of lsattr -El sys0 for os_uuid\n '''\n grains = {}\n cmd = salt.utils.path.which('lsattr')\n if cmd:\n data = __salt__['cmd.run']('{0} -El sys0'.format(cmd)) + os.linesep\n uuid_regexes = [re.compile(r'(?im)^\\s*os_uuid\\s+(\\S+)\\s+(.*)')]\n for regex in uuid_regexes:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n grains['machine_id'] = res.group(1).strip()\n break\n else:\n log.error('The \\'lsattr\\' binary was not found in $PATH.')\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _windows_virtual(osdata):\n '''\n Returns what type of virtual hardware is under the hood, kvm or physical\n '''\n # Provides:\n # virtual\n # virtual_subtype\n grains = dict()\n if osdata['kernel'] != 'Windows':\n return grains\n\n grains['virtual'] = 'physical'\n\n # It is possible that the 'manufacturer' and/or 'productname' grains\n # exist but have a value of None.\n manufacturer = osdata.get('manufacturer', '')\n if manufacturer is None:\n manufacturer = ''\n productname = osdata.get('productname', '')\n if productname is None:\n productname = ''\n\n if 'QEMU' in manufacturer:\n # FIXME: Make this detect between kvm or qemu\n grains['virtual'] = 'kvm'\n if 'Bochs' in manufacturer:\n grains['virtual'] = 'kvm'\n # Product Name: (oVirt) www.ovirt.org\n # Red Hat Community virtualization Project based on kvm\n elif 'oVirt' in productname:\n grains['virtual'] = 'kvm'\n grains['virtual_subtype'] = 'oVirt'\n # Red Hat Enterprise Virtualization\n elif 'RHEV Hypervisor' in productname:\n grains['virtual'] = 'kvm'\n grains['virtual_subtype'] = 'rhev'\n # Product Name: VirtualBox\n elif 'VirtualBox' in productname:\n grains['virtual'] = 'VirtualBox'\n # Product Name: VMware Virtual Platform\n elif 'VMware Virtual Platform' in productname:\n grains['virtual'] = 'VMware'\n # Manufacturer: Microsoft Corporation\n # Product Name: Virtual Machine\n elif 'Microsoft' in manufacturer and \\\n 'Virtual Machine' in productname:\n grains['virtual'] = 'VirtualPC'\n # Manufacturer: Parallels Software International Inc.\n elif 'Parallels Software' in manufacturer:\n grains['virtual'] = 'Parallels'\n # Apache CloudStack\n elif 'CloudStack KVM Hypervisor' in productname:\n grains['virtual'] = 'kvm'\n grains['virtual_subtype'] = 'cloudstack'\n return grains", "response": "Return what type of virtual hardware is under the hood kvm or physical."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _virtual(osdata):\n '''\n Returns what type of virtual hardware is under the hood, kvm or physical\n '''\n # This is going to be a monster, if you are running a vm you can test this\n # grain with please submit patches!\n # Provides:\n # virtual\n # virtual_subtype\n grains = {'virtual': 'physical'}\n\n # Skip the below loop on platforms which have none of the desired cmds\n # This is a temporary measure until we can write proper virtual hardware\n # detection.\n skip_cmds = ('AIX',)\n\n # list of commands to be executed to determine the 'virtual' grain\n _cmds = ['systemd-detect-virt', 'virt-what', 'dmidecode']\n # test first for virt-what, which covers most of the desired functionality\n # on most platforms\n if not salt.utils.platform.is_windows() and osdata['kernel'] not in skip_cmds:\n if salt.utils.path.which('virt-what'):\n _cmds = ['virt-what']\n\n # Check if enable_lspci is True or False\n if __opts__.get('enable_lspci', True) is True:\n # /proc/bus/pci does not exists, lspci will fail\n if os.path.exists('/proc/bus/pci'):\n _cmds += ['lspci']\n\n # Add additional last resort commands\n if osdata['kernel'] in skip_cmds:\n _cmds = ()\n\n # Quick backout for BrandZ (Solaris LX Branded zones)\n # Don't waste time trying other commands to detect the virtual grain\n if HAS_UNAME and osdata['kernel'] == 'Linux' and 'BrandZ virtual linux' in os.uname():\n grains['virtual'] = 'zone'\n return grains\n\n failed_commands = set()\n for command in _cmds:\n args = []\n if osdata['kernel'] == 'Darwin':\n command = 'system_profiler'\n args = ['SPDisplaysDataType']\n elif osdata['kernel'] == 'SunOS':\n virtinfo = salt.utils.path.which('virtinfo')\n if virtinfo:\n try:\n ret = __salt__['cmd.run_all']('{0} -a'.format(virtinfo))\n except salt.exceptions.CommandExecutionError:\n if salt.log.is_logging_configured():\n failed_commands.add(virtinfo)\n else:\n if ret['stdout'].endswith('not supported'):\n command = 'prtdiag'\n else:\n command = 'virtinfo'\n else:\n command = 'prtdiag'\n\n cmd = salt.utils.path.which(command)\n\n if not cmd:\n continue\n\n cmd = '{0} {1}'.format(cmd, ' '.join(args))\n\n try:\n ret = __salt__['cmd.run_all'](cmd)\n\n if ret['retcode'] > 0:\n if salt.log.is_logging_configured():\n # systemd-detect-virt always returns > 0 on non-virtualized\n # systems\n # prtdiag only works in the global zone, skip if it fails\n if salt.utils.platform.is_windows() or 'systemd-detect-virt' in cmd or 'prtdiag' in cmd:\n continue\n failed_commands.add(command)\n continue\n except salt.exceptions.CommandExecutionError:\n if salt.log.is_logging_configured():\n if salt.utils.platform.is_windows():\n continue\n failed_commands.add(command)\n continue\n\n output = ret['stdout']\n if command == \"system_profiler\":\n macoutput = output.lower()\n if '0x1ab8' in macoutput:\n grains['virtual'] = 'Parallels'\n if 'parallels' in macoutput:\n grains['virtual'] = 'Parallels'\n if 'vmware' in macoutput:\n grains['virtual'] = 'VMware'\n if '0x15ad' in macoutput:\n grains['virtual'] = 'VMware'\n if 'virtualbox' in macoutput:\n grains['virtual'] = 'VirtualBox'\n # Break out of the loop so the next log message is not issued\n break\n elif command == 'systemd-detect-virt':\n if output in ('qemu', 'kvm', 'oracle', 'xen', 'bochs', 'chroot', 'uml', 'systemd-nspawn'):\n grains['virtual'] = output\n break\n elif 'vmware' in output:\n grains['virtual'] = 'VMware'\n break\n elif 'microsoft' in output:\n grains['virtual'] = 'VirtualPC'\n break\n elif 'lxc' in output:\n grains['virtual'] = 'LXC'\n break\n elif 'systemd-nspawn' in output:\n grains['virtual'] = 'LXC'\n break\n elif command == 'virt-what':\n try:\n output = output.splitlines()[-1]\n except IndexError:\n pass\n if output in ('kvm', 'qemu', 'uml', 'xen', 'lxc'):\n grains['virtual'] = output\n break\n elif 'vmware' in output:\n grains['virtual'] = 'VMware'\n break\n elif 'parallels' in output:\n grains['virtual'] = 'Parallels'\n break\n elif 'hyperv' in output:\n grains['virtual'] = 'HyperV'\n break\n elif command == 'dmidecode':\n # Product Name: VirtualBox\n if 'Vendor: QEMU' in output:\n # FIXME: Make this detect between kvm or qemu\n grains['virtual'] = 'kvm'\n if 'Manufacturer: QEMU' in output:\n grains['virtual'] = 'kvm'\n if 'Vendor: Bochs' in output:\n grains['virtual'] = 'kvm'\n if 'Manufacturer: Bochs' in output:\n grains['virtual'] = 'kvm'\n if 'BHYVE' in output:\n grains['virtual'] = 'bhyve'\n # Product Name: (oVirt) www.ovirt.org\n # Red Hat Community virtualization Project based on kvm\n elif 'Manufacturer: oVirt' in output:\n grains['virtual'] = 'kvm'\n grains['virtual_subtype'] = 'ovirt'\n # Red Hat Enterprise Virtualization\n elif 'Product Name: RHEV Hypervisor' in output:\n grains['virtual'] = 'kvm'\n grains['virtual_subtype'] = 'rhev'\n elif 'VirtualBox' in output:\n grains['virtual'] = 'VirtualBox'\n # Product Name: VMware Virtual Platform\n elif 'VMware' in output:\n grains['virtual'] = 'VMware'\n # Manufacturer: Microsoft Corporation\n # Product Name: Virtual Machine\n elif ': Microsoft' in output and 'Virtual Machine' in output:\n grains['virtual'] = 'VirtualPC'\n # Manufacturer: Parallels Software International Inc.\n elif 'Parallels Software' in output:\n grains['virtual'] = 'Parallels'\n elif 'Manufacturer: Google' in output:\n grains['virtual'] = 'kvm'\n # Proxmox KVM\n elif 'Vendor: SeaBIOS' in output:\n grains['virtual'] = 'kvm'\n # Break out of the loop, lspci parsing is not necessary\n break\n elif command == 'lspci':\n # dmidecode not available or the user does not have the necessary\n # permissions\n model = output.lower()\n if 'vmware' in model:\n grains['virtual'] = 'VMware'\n # 00:04.0 System peripheral: InnoTek Systemberatung GmbH\n # VirtualBox Guest Service\n elif 'virtualbox' in model:\n grains['virtual'] = 'VirtualBox'\n elif 'qemu' in model:\n grains['virtual'] = 'kvm'\n elif 'virtio' in model:\n grains['virtual'] = 'kvm'\n # Break out of the loop so the next log message is not issued\n break\n elif command == 'prtdiag':\n model = output.lower().split(\"\\n\")[0]\n if 'vmware' in model:\n grains['virtual'] = 'VMware'\n elif 'virtualbox' in model:\n grains['virtual'] = 'VirtualBox'\n elif 'qemu' in model:\n grains['virtual'] = 'kvm'\n elif 'joyent smartdc hvm' in model:\n grains['virtual'] = 'kvm'\n break\n elif command == 'virtinfo':\n grains['virtual'] = 'LDOM'\n break\n\n choices = ('Linux', 'HP-UX')\n isdir = os.path.isdir\n sysctl = salt.utils.path.which('sysctl')\n if osdata['kernel'] in choices:\n if os.path.isdir('/proc'):\n try:\n self_root = os.stat('/')\n init_root = os.stat('/proc/1/root/.')\n if self_root != init_root:\n grains['virtual_subtype'] = 'chroot'\n except (IOError, OSError):\n pass\n if isdir('/proc/vz'):\n if os.path.isfile('/proc/vz/version'):\n grains['virtual'] = 'openvzhn'\n elif os.path.isfile('/proc/vz/veinfo'):\n grains['virtual'] = 'openvzve'\n # a posteriori, it's expected for these to have failed:\n failed_commands.discard('lspci')\n failed_commands.discard('dmidecode')\n # Provide additional detection for OpenVZ\n if os.path.isfile('/proc/self/status'):\n with salt.utils.files.fopen('/proc/self/status') as status_file:\n vz_re = re.compile(r'^envID:\\s+(\\d+)$')\n for line in status_file:\n vz_match = vz_re.match(line.rstrip('\\n'))\n if vz_match and int(vz_match.groups()[0]) != 0:\n grains['virtual'] = 'openvzve'\n elif vz_match and int(vz_match.groups()[0]) == 0:\n grains['virtual'] = 'openvzhn'\n if isdir('/proc/sys/xen') or \\\n isdir('/sys/bus/xen') or isdir('/proc/xen'):\n if os.path.isfile('/proc/xen/xsd_kva'):\n # Tested on CentOS 5.3 / 2.6.18-194.26.1.el5xen\n # Tested on CentOS 5.4 / 2.6.18-164.15.1.el5xen\n grains['virtual_subtype'] = 'Xen Dom0'\n else:\n if osdata.get('productname', '') == 'HVM domU':\n # Requires dmidecode!\n grains['virtual_subtype'] = 'Xen HVM DomU'\n elif os.path.isfile('/proc/xen/capabilities') and \\\n os.access('/proc/xen/capabilities', os.R_OK):\n with salt.utils.files.fopen('/proc/xen/capabilities') as fhr:\n if 'control_d' not in fhr.read():\n # Tested on CentOS 5.5 / 2.6.18-194.3.1.el5xen\n grains['virtual_subtype'] = 'Xen PV DomU'\n else:\n # Shouldn't get to this, but just in case\n grains['virtual_subtype'] = 'Xen Dom0'\n # Tested on Fedora 10 / 2.6.27.30-170.2.82 with xen\n # Tested on Fedora 15 / 2.6.41.4-1 without running xen\n elif isdir('/sys/bus/xen'):\n if 'xen:' in __salt__['cmd.run']('dmesg').lower():\n grains['virtual_subtype'] = 'Xen PV DomU'\n elif os.path.isfile('/sys/bus/xen/drivers/xenconsole'):\n # An actual DomU will have the xenconsole driver\n grains['virtual_subtype'] = 'Xen PV DomU'\n # If a Dom0 or DomU was detected, obviously this is xen\n if 'dom' in grains.get('virtual_subtype', '').lower():\n grains['virtual'] = 'xen'\n # Check container type after hypervisors, to avoid variable overwrite on containers running in virtual environment.\n if os.path.isfile('/proc/1/cgroup'):\n try:\n with salt.utils.files.fopen('/proc/1/cgroup', 'r') as fhr:\n fhr_contents = fhr.read()\n if ':/lxc/' in fhr_contents:\n grains['virtual_subtype'] = 'LXC'\n elif ':/kubepods/' in fhr_contents:\n grains['virtual_subtype'] = 'kubernetes'\n elif ':/libpod_parent/' in fhr_contents:\n grains['virtual_subtype'] = 'libpod'\n else:\n if any(x in fhr_contents\n for x in (':/system.slice/docker', ':/docker/',\n ':/docker-ce/')):\n grains['virtual_subtype'] = 'Docker'\n except IOError:\n pass\n if os.path.isfile('/proc/cpuinfo'):\n with salt.utils.files.fopen('/proc/cpuinfo', 'r') as fhr:\n if 'QEMU Virtual CPU' in fhr.read():\n grains['virtual'] = 'kvm'\n if os.path.isfile('/sys/devices/virtual/dmi/id/product_name'):\n try:\n with salt.utils.files.fopen('/sys/devices/virtual/dmi/id/product_name', 'r') as fhr:\n output = salt.utils.stringutils.to_unicode(fhr.read(), errors='replace')\n if 'VirtualBox' in output:\n grains['virtual'] = 'VirtualBox'\n elif 'RHEV Hypervisor' in output:\n grains['virtual'] = 'kvm'\n grains['virtual_subtype'] = 'rhev'\n elif 'oVirt Node' in output:\n grains['virtual'] = 'kvm'\n grains['virtual_subtype'] = 'ovirt'\n elif 'Google' in output:\n grains['virtual'] = 'gce'\n elif 'BHYVE' in output:\n grains['virtual'] = 'bhyve'\n except IOError:\n pass\n elif osdata['kernel'] == 'FreeBSD':\n kenv = salt.utils.path.which('kenv')\n if kenv:\n product = __salt__['cmd.run'](\n '{0} smbios.system.product'.format(kenv)\n )\n maker = __salt__['cmd.run'](\n '{0} smbios.system.maker'.format(kenv)\n )\n if product.startswith('VMware'):\n grains['virtual'] = 'VMware'\n if product.startswith('VirtualBox'):\n grains['virtual'] = 'VirtualBox'\n if maker.startswith('Xen'):\n grains['virtual_subtype'] = '{0} {1}'.format(maker, product)\n grains['virtual'] = 'xen'\n if maker.startswith('Microsoft') and product.startswith('Virtual'):\n grains['virtual'] = 'VirtualPC'\n if maker.startswith('OpenStack'):\n grains['virtual'] = 'OpenStack'\n if maker.startswith('Bochs'):\n grains['virtual'] = 'kvm'\n if sysctl:\n hv_vendor = __salt__['cmd.run']('{0} hw.hv_vendor'.format(sysctl))\n model = __salt__['cmd.run']('{0} hw.model'.format(sysctl))\n jail = __salt__['cmd.run'](\n '{0} -n security.jail.jailed'.format(sysctl)\n )\n if 'bhyve' in hv_vendor:\n grains['virtual'] = 'bhyve'\n if jail == '1':\n grains['virtual_subtype'] = 'jail'\n if 'QEMU Virtual CPU' in model:\n grains['virtual'] = 'kvm'\n elif osdata['kernel'] == 'OpenBSD':\n if 'manufacturer' in osdata:\n if osdata['manufacturer'] in ['QEMU', 'Red Hat', 'Joyent']:\n grains['virtual'] = 'kvm'\n if osdata['manufacturer'] == 'OpenBSD':\n grains['virtual'] = 'vmm'\n elif osdata['kernel'] == 'SunOS':\n if grains['virtual'] == 'LDOM':\n roles = []\n for role in ('control', 'io', 'root', 'service'):\n subtype_cmd = '{0} -c current get -H -o value {1}-role'.format(cmd, role)\n ret = __salt__['cmd.run_all']('{0}'.format(subtype_cmd))\n if ret['stdout'] == 'true':\n roles.append(role)\n if roles:\n grains['virtual_subtype'] = roles\n else:\n # Check if it's a \"regular\" zone. (i.e. Solaris 10/11 zone)\n zonename = salt.utils.path.which('zonename')\n if zonename:\n zone = __salt__['cmd.run']('{0}'.format(zonename))\n if zone != 'global':\n grains['virtual'] = 'zone'\n # Check if it's a branded zone (i.e. Solaris 8/9 zone)\n if isdir('/.SUNWnative'):\n grains['virtual'] = 'zone'\n elif osdata['kernel'] == 'NetBSD':\n if sysctl:\n if 'QEMU Virtual CPU' in __salt__['cmd.run'](\n '{0} -n machdep.cpu_brand'.format(sysctl)):\n grains['virtual'] = 'kvm'\n elif 'invalid' not in __salt__['cmd.run'](\n '{0} -n machdep.xen.suspend'.format(sysctl)):\n grains['virtual'] = 'Xen PV DomU'\n elif 'VMware' in __salt__['cmd.run'](\n '{0} -n machdep.dmi.system-vendor'.format(sysctl)):\n grains['virtual'] = 'VMware'\n # NetBSD has Xen dom0 support\n elif __salt__['cmd.run'](\n '{0} -n machdep.idle-mechanism'.format(sysctl)) == 'xen':\n if os.path.isfile('/var/run/xenconsoled.pid'):\n grains['virtual_subtype'] = 'Xen Dom0'\n\n for command in failed_commands:\n log.info(\n \"Although '%s' was found in path, the current user \"\n 'cannot execute it. Grains output might not be '\n 'accurate.', command\n )\n return grains", "response": "Return what type of virtual hardware is under the hood kvm or physical hardware."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the ps grain", "response": "def _ps(osdata):\n '''\n Return the ps grain\n '''\n grains = {}\n bsd_choices = ('FreeBSD', 'NetBSD', 'OpenBSD', 'MacOS')\n if osdata['os'] in bsd_choices:\n grains['ps'] = 'ps auxwww'\n elif osdata['os_family'] == 'Solaris':\n grains['ps'] = '/usr/ucb/ps auxwww'\n elif osdata['os'] == 'Windows':\n grains['ps'] = 'tasklist.exe'\n elif osdata.get('virtual', '') == 'openvzhn':\n grains['ps'] = (\n 'ps -fH -p $(grep -l \\\"^envID:[[:space:]]*0\\\\$\\\" '\n '/proc/[0-9]*/status | sed -e \\\"s=/proc/\\\\([0-9]*\\\\)/.*=\\\\1=\\\") '\n '| awk \\'{ $7=\\\"\\\"; print }\\''\n )\n elif osdata['os_family'] == 'AIX':\n grains['ps'] = '/usr/bin/ps auxww'\n elif osdata['os_family'] == 'NILinuxRT':\n grains['ps'] = 'ps -o user,pid,ppid,tty,time,comm'\n else:\n grains['ps'] = 'ps -efHww'\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _clean_value(key, val):\n '''\n Clean out well-known bogus values.\n If it isn't clean (for example has value 'None'), return None.\n Otherwise, return the original value.\n\n NOTE: This logic also exists in the smbios module. This function is\n for use when not using smbios to retrieve the value.\n '''\n if (val is None or not val or\n re.match('none', val, flags=re.IGNORECASE)):\n return None\n elif 'uuid' in key:\n # Try each version (1-5) of RFC4122 to check if it's actually a UUID\n for uuidver in range(1, 5):\n try:\n uuid.UUID(val, version=uuidver)\n return val\n except ValueError:\n continue\n log.trace('HW %s value %s is an invalid UUID', key, val.replace('\\n', ' '))\n return None\n elif re.search('serial|part|version', key):\n # 'To be filled by O.E.M.\n # 'Not applicable' etc.\n # 'Not specified' etc.\n # 0000000, 1234567 etc.\n # begone!\n if (re.match(r'^[0]+$', val) or\n re.match(r'[0]?1234567[8]?[9]?[0]?', val) or\n re.search(r'sernum|part[_-]?number|specified|filled|applicable', val, flags=re.IGNORECASE)):\n return None\n elif re.search('asset|manufacturer', key):\n # AssetTag0. Manufacturer04. Begone.\n if re.search(r'manufacturer|to be filled|available|asset|^no(ne|t)', val, flags=re.IGNORECASE):\n return None\n else:\n # map unspecified, undefined, unknown & whatever to None\n if (re.search(r'to be filled', val, flags=re.IGNORECASE) or\n re.search(r'un(known|specified)|no(t|ne)? (asset|provided|defined|available|present|specified)',\n val, flags=re.IGNORECASE)):\n return None\n return val", "response": "Clean out well - known bogus values."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _windows_platform_data():\n '''\n Use the platform module for as much as we can.\n '''\n # Provides:\n # kernelrelease\n # kernelversion\n # osversion\n # osrelease\n # osservicepack\n # osmanufacturer\n # manufacturer\n # productname\n # biosversion\n # serialnumber\n # osfullname\n # timezone\n # windowsdomain\n # windowsdomaintype\n # motherboard.productname\n # motherboard.serialnumber\n # virtual\n\n if not HAS_WMI:\n return {}\n\n with salt.utils.winapi.Com():\n wmi_c = wmi.WMI()\n # http://msdn.microsoft.com/en-us/library/windows/desktop/aa394102%28v=vs.85%29.aspx\n systeminfo = wmi_c.Win32_ComputerSystem()[0]\n # https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx\n osinfo = wmi_c.Win32_OperatingSystem()[0]\n # http://msdn.microsoft.com/en-us/library/windows/desktop/aa394077(v=vs.85).aspx\n biosinfo = wmi_c.Win32_BIOS()[0]\n # http://msdn.microsoft.com/en-us/library/windows/desktop/aa394498(v=vs.85).aspx\n timeinfo = wmi_c.Win32_TimeZone()[0]\n\n # http://msdn.microsoft.com/en-us/library/windows/desktop/aa394072(v=vs.85).aspx\n motherboard = {'product': None,\n 'serial': None}\n try:\n motherboardinfo = wmi_c.Win32_BaseBoard()[0]\n motherboard['product'] = motherboardinfo.Product\n motherboard['serial'] = motherboardinfo.SerialNumber\n except IndexError:\n log.debug('Motherboard info not available on this system')\n\n os_release = platform.release()\n kernel_version = platform.version()\n info = salt.utils.win_osinfo.get_os_version_info()\n net_info = salt.utils.win_osinfo.get_join_info()\n\n service_pack = None\n if info['ServicePackMajor'] > 0:\n service_pack = ''.join(['SP', six.text_type(info['ServicePackMajor'])])\n\n # This creates the osrelease grain based on the Windows Operating\n # System Product Name. As long as Microsoft maintains a similar format\n # this should be future proof\n version = 'Unknown'\n release = ''\n if 'Server' in osinfo.Caption:\n for item in osinfo.Caption.split(' '):\n # If it's all digits, then it's version\n if re.match(r'\\d+', item):\n version = item\n # If it starts with R and then numbers, it's the release\n # ie: R2\n if re.match(r'^R\\d+$', item):\n release = item\n os_release = '{0}Server{1}'.format(version, release)\n else:\n for item in osinfo.Caption.split(' '):\n # If it's a number, decimal number, Thin or Vista, then it's the\n # version\n if re.match(r'^(\\d+(\\.\\d+)?)|Thin|Vista$', item):\n version = item\n os_release = version\n\n grains = {\n 'kernelrelease': _clean_value('kernelrelease', osinfo.Version),\n 'kernelversion': _clean_value('kernelversion', kernel_version),\n 'osversion': _clean_value('osversion', osinfo.Version),\n 'osrelease': _clean_value('osrelease', os_release),\n 'osservicepack': _clean_value('osservicepack', service_pack),\n 'osmanufacturer': _clean_value('osmanufacturer', osinfo.Manufacturer),\n 'manufacturer': _clean_value('manufacturer', systeminfo.Manufacturer),\n 'productname': _clean_value('productname', systeminfo.Model),\n # bios name had a bunch of whitespace appended to it in my testing\n # 'PhoenixBIOS 4.0 Release 6.0 '\n 'biosversion': _clean_value('biosversion', biosinfo.Name.strip()),\n 'serialnumber': _clean_value('serialnumber', biosinfo.SerialNumber),\n 'osfullname': _clean_value('osfullname', osinfo.Caption),\n 'timezone': _clean_value('timezone', timeinfo.Description),\n 'windowsdomain': _clean_value('windowsdomain', net_info['Domain']),\n 'windowsdomaintype': _clean_value('windowsdomaintype', net_info['DomainType']),\n 'motherboard': {\n 'productname': _clean_value('motherboard.productname', motherboard['product']),\n 'serialnumber': _clean_value('motherboard.serialnumber', motherboard['serial']),\n }\n }\n\n # test for virtualized environments\n # I only had VMware available so the rest are unvalidated\n if 'VRTUAL' in biosinfo.Version: # (not a typo)\n grains['virtual'] = 'HyperV'\n elif 'A M I' in biosinfo.Version:\n grains['virtual'] = 'VirtualPC'\n elif 'VMware' in systeminfo.Model:\n grains['virtual'] = 'VMware'\n elif 'VirtualBox' in systeminfo.Model:\n grains['virtual'] = 'VirtualBox'\n elif 'Xen' in biosinfo.Version:\n grains['virtual'] = 'Xen'\n if 'HVM domU' in systeminfo.Model:\n grains['virtual_subtype'] = 'HVM domU'\n elif 'OpenStack' in systeminfo.Model:\n grains['virtual'] = 'OpenStack'\n\n return grains", "response": "Return a dict of all available Windows data."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the data for macOS systems", "response": "def _osx_platform_data():\n '''\n Additional data for macOS systems\n Returns: A dictionary containing values for the following:\n - model_name\n - boot_rom_version\n - smc_version\n - system_serialnumber\n '''\n cmd = 'system_profiler SPHardwareDataType'\n hardware = __salt__['cmd.run'](cmd)\n\n grains = {}\n for line in hardware.splitlines():\n field_name, _, field_val = line.partition(': ')\n if field_name.strip() == \"Model Name\":\n key = 'model_name'\n grains[key] = _clean_value(key, field_val)\n if field_name.strip() == \"Boot ROM Version\":\n key = 'boot_rom_version'\n grains[key] = _clean_value(key, field_val)\n if field_name.strip() == \"SMC Version (system)\":\n key = 'smc_version'\n grains[key] = _clean_value(key, field_val)\n if field_name.strip() == \"Serial Number (system)\":\n key = 'system_serialnumber'\n grains[key] = _clean_value(key, field_val)\n\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _linux_bin_exists(binary):\n '''\n Does a binary exist in linux (depends on which, type, or whereis)\n '''\n for search_cmd in ('which', 'type -ap'):\n try:\n return __salt__['cmd.retcode'](\n '{0} {1}'.format(search_cmd, binary)\n ) == 0\n except salt.exceptions.CommandExecutionError:\n pass\n\n try:\n return len(__salt__['cmd.run_all'](\n 'whereis -b {0}'.format(binary)\n )['stdout'].split()) > 1\n except salt.exceptions.CommandExecutionError:\n return False", "response": "Returns True if a linux binary exists in linux"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nparses os - release and return a parameter dictionary.", "response": "def _parse_os_release(*os_release_files):\n '''\n Parse os-release and return a parameter dictionary\n\n See http://www.freedesktop.org/software/systemd/man/os-release.html\n for specification of the file format.\n '''\n ret = {}\n for filename in os_release_files:\n try:\n with salt.utils.files.fopen(filename) as ifile:\n regex = re.compile('^([\\\\w]+)=(?:\\'|\")?(.*?)(?:\\'|\")?$')\n for line in ifile:\n match = regex.match(line.strip())\n if match:\n # Shell special characters (\"$\", quotes, backslash,\n # backtick) are escaped with backslashes\n ret[match.group(1)] = re.sub(\n r'\\\\([$\"\\'\\\\`])', r'\\1', match.group(2)\n )\n break\n except (IOError, OSError):\n pass\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _parse_cpe_name(cpe):\n '''\n Parse CPE_NAME data from the os-release\n\n Info: https://csrc.nist.gov/projects/security-content-automation-protocol/scap-specifications/cpe\n\n :param cpe:\n :return:\n '''\n part = {\n 'o': 'operating system',\n 'h': 'hardware',\n 'a': 'application',\n }\n ret = {}\n cpe = (cpe or '').split(':')\n if len(cpe) > 4 and cpe[0] == 'cpe':\n if cpe[1].startswith('/'): # WFN to URI\n ret['vendor'], ret['product'], ret['version'] = cpe[2:5]\n ret['phase'] = cpe[5] if len(cpe) > 5 else None\n ret['part'] = part.get(cpe[1][1:])\n elif len(cpe) == 13 and cpe[1] == '2.3': # WFN to a string\n ret['vendor'], ret['product'], ret['version'], ret['phase'] = [x if x != '*' else None for x in cpe[3:7]]\n ret['part'] = part.get(cpe[2])\n\n return ret", "response": "Parse the CPE_NAME data from the os - release - enumeration - protocol."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the operating system information for the current system.", "response": "def os_data():\n '''\n Return grains pertaining to the operating system\n '''\n grains = {\n 'num_gpus': 0,\n 'gpus': [],\n }\n\n # Windows Server 2008 64-bit\n # ('Windows', 'MINIONNAME', '2008ServerR2', '6.1.7601', 'AMD64',\n # 'Intel64 Fam ily 6 Model 23 Stepping 6, GenuineIntel')\n # Ubuntu 10.04\n # ('Linux', 'MINIONNAME', '2.6.32-38-server',\n # '#83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012', 'x86_64', '')\n\n # pylint: disable=unpacking-non-sequence\n (grains['kernel'], grains['nodename'],\n grains['kernelrelease'], grains['kernelversion'], grains['cpuarch'], _) = platform.uname()\n # pylint: enable=unpacking-non-sequence\n\n if salt.utils.platform.is_proxy():\n grains['kernel'] = 'proxy'\n grains['kernelrelease'] = 'proxy'\n grains['kernelversion'] = 'proxy'\n grains['osrelease'] = 'proxy'\n grains['os'] = 'proxy'\n grains['os_family'] = 'proxy'\n grains['osfullname'] = 'proxy'\n elif salt.utils.platform.is_windows():\n grains['os'] = 'Windows'\n grains['os_family'] = 'Windows'\n grains.update(_memdata(grains))\n grains.update(_windows_platform_data())\n grains.update(_windows_cpudata())\n grains.update(_windows_virtual(grains))\n grains.update(_ps(grains))\n\n if 'Server' in grains['osrelease']:\n osrelease_info = grains['osrelease'].split('Server', 1)\n osrelease_info[1] = osrelease_info[1].lstrip('R')\n else:\n osrelease_info = grains['osrelease'].split('.')\n\n for idx, value in enumerate(osrelease_info):\n if not value.isdigit():\n continue\n osrelease_info[idx] = int(value)\n grains['osrelease_info'] = tuple(osrelease_info)\n\n grains['osfinger'] = '{os}-{ver}'.format(\n os=grains['os'],\n ver=grains['osrelease'])\n\n grains['init'] = 'Windows'\n\n return grains\n elif salt.utils.platform.is_linux():\n # Add SELinux grain, if you have it\n if _linux_bin_exists('selinuxenabled'):\n log.trace('Adding selinux grains')\n grains['selinux'] = {}\n grains['selinux']['enabled'] = __salt__['cmd.retcode'](\n 'selinuxenabled'\n ) == 0\n if _linux_bin_exists('getenforce'):\n grains['selinux']['enforced'] = __salt__['cmd.run'](\n 'getenforce'\n ).strip()\n\n # Add systemd grain, if you have it\n if _linux_bin_exists('systemctl') and _linux_bin_exists('localectl'):\n log.trace('Adding systemd grains')\n grains['systemd'] = {}\n systemd_info = __salt__['cmd.run'](\n 'systemctl --version'\n ).splitlines()\n grains['systemd']['version'] = systemd_info[0].split()[1]\n grains['systemd']['features'] = systemd_info[1]\n\n # Add init grain\n grains['init'] = 'unknown'\n log.trace('Adding init grain')\n try:\n os.stat('/run/systemd/system')\n grains['init'] = 'systemd'\n except (OSError, IOError):\n try:\n with salt.utils.files.fopen('/proc/1/cmdline') as fhr:\n init_cmdline = fhr.read().replace('\\x00', ' ').split()\n except (IOError, OSError):\n pass\n else:\n try:\n init_bin = salt.utils.path.which(init_cmdline[0])\n except IndexError:\n # Emtpy init_cmdline\n init_bin = None\n log.warning('Unable to fetch data from /proc/1/cmdline')\n if init_bin is not None and init_bin.endswith('bin/init'):\n supported_inits = (b'upstart', b'sysvinit', b'systemd')\n edge_len = max(len(x) for x in supported_inits) - 1\n try:\n buf_size = __opts__['file_buffer_size']\n except KeyError:\n # Default to the value of file_buffer_size for the minion\n buf_size = 262144\n try:\n with salt.utils.files.fopen(init_bin, 'rb') as fp_:\n edge = b''\n buf = fp_.read(buf_size).lower()\n while buf:\n buf = edge + buf\n for item in supported_inits:\n if item in buf:\n if six.PY3:\n item = item.decode('utf-8')\n grains['init'] = item\n buf = b''\n break\n edge = buf[-edge_len:]\n buf = fp_.read(buf_size).lower()\n except (IOError, OSError) as exc:\n log.error(\n 'Unable to read from init_bin (%s): %s',\n init_bin, exc\n )\n elif salt.utils.path.which('supervisord') in init_cmdline:\n grains['init'] = 'supervisord'\n elif salt.utils.path.which('dumb-init') in init_cmdline:\n # https://github.com/Yelp/dumb-init\n grains['init'] = 'dumb-init'\n elif salt.utils.path.which('tini') in init_cmdline:\n # https://github.com/krallin/tini\n grains['init'] = 'tini'\n elif init_cmdline == ['runit']:\n grains['init'] = 'runit'\n elif '/sbin/my_init' in init_cmdline:\n # Phusion Base docker container use runit for srv mgmt, but\n # my_init as pid1\n grains['init'] = 'runit'\n else:\n log.debug(\n 'Could not determine init system from command line: (%s)',\n ' '.join(init_cmdline)\n )\n\n # Add lsb grains on any distro with lsb-release. Note that this import\n # can fail on systems with lsb-release installed if the system package\n # does not install the python package for the python interpreter used by\n # Salt (i.e. python2 or python3)\n try:\n log.trace('Getting lsb_release distro information')\n import lsb_release # pylint: disable=import-error\n release = lsb_release.get_distro_information()\n for key, value in six.iteritems(release):\n key = key.lower()\n lsb_param = 'lsb_{0}{1}'.format(\n '' if key.startswith('distrib_') else 'distrib_',\n key\n )\n grains[lsb_param] = value\n # Catch a NameError to workaround possible breakage in lsb_release\n # See https://github.com/saltstack/salt/issues/37867\n except (ImportError, NameError):\n # if the python library isn't available, try to parse\n # /etc/lsb-release using regex\n log.trace('lsb_release python bindings not available')\n grains.update(_parse_lsb_release())\n\n if grains.get('lsb_distrib_description', '').lower().startswith('antergos'):\n # Antergos incorrectly configures their /etc/lsb-release,\n # setting the DISTRIB_ID to \"Arch\". This causes the \"os\" grain\n # to be incorrectly set to \"Arch\".\n grains['osfullname'] = 'Antergos Linux'\n elif 'lsb_distrib_id' not in grains:\n log.trace(\n 'Failed to get lsb_distrib_id, trying to parse os-release'\n )\n os_release = _parse_os_release('/etc/os-release', '/usr/lib/os-release')\n if os_release:\n if 'NAME' in os_release:\n grains['lsb_distrib_id'] = os_release['NAME'].strip()\n if 'VERSION_ID' in os_release:\n grains['lsb_distrib_release'] = os_release['VERSION_ID']\n if 'VERSION_CODENAME' in os_release:\n grains['lsb_distrib_codename'] = os_release['VERSION_CODENAME']\n elif 'PRETTY_NAME' in os_release:\n codename = os_release['PRETTY_NAME']\n # https://github.com/saltstack/salt/issues/44108\n if os_release['ID'] == 'debian':\n codename_match = re.search(r'\\((\\w+)\\)$', codename)\n if codename_match:\n codename = codename_match.group(1)\n grains['lsb_distrib_codename'] = codename\n if 'CPE_NAME' in os_release:\n cpe = _parse_cpe_name(os_release['CPE_NAME'])\n if not cpe:\n log.error('Broken CPE_NAME format in /etc/os-release!')\n elif cpe.get('vendor', '').lower() in ['suse', 'opensuse']:\n grains['os'] = \"SUSE\"\n # openSUSE `osfullname` grain normalization\n if os_release.get(\"NAME\") == \"openSUSE Leap\":\n grains['osfullname'] = \"Leap\"\n elif os_release.get(\"VERSION\") == \"Tumbleweed\":\n grains['osfullname'] = os_release[\"VERSION\"]\n # Override VERSION_ID, if CPE_NAME around\n if cpe.get('version') and cpe.get('vendor') == 'opensuse': # Keep VERSION_ID for SLES\n grains['lsb_distrib_release'] = cpe['version']\n\n elif os.path.isfile('/etc/SuSE-release'):\n log.trace('Parsing distrib info from /etc/SuSE-release')\n grains['lsb_distrib_id'] = 'SUSE'\n version = ''\n patch = ''\n with salt.utils.files.fopen('/etc/SuSE-release') as fhr:\n for line in fhr:\n if 'enterprise' in line.lower():\n grains['lsb_distrib_id'] = 'SLES'\n grains['lsb_distrib_codename'] = re.sub(r'\\(.+\\)', '', line).strip()\n elif 'version' in line.lower():\n version = re.sub(r'[^0-9]', '', line)\n elif 'patchlevel' in line.lower():\n patch = re.sub(r'[^0-9]', '', line)\n grains['lsb_distrib_release'] = version\n if patch:\n grains['lsb_distrib_release'] += '.' + patch\n patchstr = 'SP' + patch\n if grains['lsb_distrib_codename'] and patchstr not in grains['lsb_distrib_codename']:\n grains['lsb_distrib_codename'] += ' ' + patchstr\n if not grains.get('lsb_distrib_codename'):\n grains['lsb_distrib_codename'] = 'n.a'\n elif os.path.isfile('/etc/altlinux-release'):\n log.trace('Parsing distrib info from /etc/altlinux-release')\n # ALT Linux\n grains['lsb_distrib_id'] = 'altlinux'\n with salt.utils.files.fopen('/etc/altlinux-release') as ifile:\n # This file is symlinked to from:\n # /etc/fedora-release\n # /etc/redhat-release\n # /etc/system-release\n for line in ifile:\n # ALT Linux Sisyphus (unstable)\n comps = line.split()\n if comps[0] == 'ALT':\n grains['lsb_distrib_release'] = comps[2]\n grains['lsb_distrib_codename'] = \\\n comps[3].replace('(', '').replace(')', '')\n elif os.path.isfile('/etc/centos-release'):\n log.trace('Parsing distrib info from /etc/centos-release')\n # Maybe CentOS Linux; could also be SUSE Expanded Support.\n # SUSE ES has both, centos-release and redhat-release.\n if os.path.isfile('/etc/redhat-release'):\n with salt.utils.files.fopen('/etc/redhat-release') as ifile:\n for line in ifile:\n if \"red hat enterprise linux server\" in line.lower():\n # This is a SUSE Expanded Support Rhel installation\n grains['lsb_distrib_id'] = 'RedHat'\n break\n grains.setdefault('lsb_distrib_id', 'CentOS')\n with salt.utils.files.fopen('/etc/centos-release') as ifile:\n for line in ifile:\n # Need to pull out the version and codename\n # in the case of custom content in /etc/centos-release\n find_release = re.compile(r'\\d+\\.\\d+')\n find_codename = re.compile(r'(?<=\\()(.*?)(?=\\))')\n release = find_release.search(line)\n codename = find_codename.search(line)\n if release is not None:\n grains['lsb_distrib_release'] = release.group()\n if codename is not None:\n grains['lsb_distrib_codename'] = codename.group()\n elif os.path.isfile('/etc.defaults/VERSION') \\\n and os.path.isfile('/etc.defaults/synoinfo.conf'):\n grains['osfullname'] = 'Synology'\n log.trace(\n 'Parsing Synology distrib info from /etc/.defaults/VERSION'\n )\n with salt.utils.files.fopen('/etc.defaults/VERSION', 'r') as fp_:\n synoinfo = {}\n for line in fp_:\n try:\n key, val = line.rstrip('\\n').split('=')\n except ValueError:\n continue\n if key in ('majorversion', 'minorversion',\n 'buildnumber'):\n synoinfo[key] = val.strip('\"')\n if len(synoinfo) != 3:\n log.warning(\n 'Unable to determine Synology version info. '\n 'Please report this, as it is likely a bug.'\n )\n else:\n grains['osrelease'] = (\n '{majorversion}.{minorversion}-{buildnumber}'\n .format(**synoinfo)\n )\n\n # Use the already intelligent platform module to get distro info\n # (though apparently it's not intelligent enough to strip quotes)\n log.trace(\n 'Getting OS name, release, and codename from '\n 'distro.linux_distribution()'\n )\n (osname, osrelease, oscodename) = \\\n [x.strip('\"').strip(\"'\") for x in\n linux_distribution(supported_dists=_supported_dists)]\n # Try to assign these three names based on the lsb info, they tend to\n # be more accurate than what python gets from /etc/DISTRO-release.\n # It's worth noting that Ubuntu has patched their Python distribution\n # so that linux_distribution() does the /etc/lsb-release parsing, but\n # we do it anyway here for the sake for full portability.\n if 'osfullname' not in grains:\n # If NI Linux RT distribution, set the grains['osfullname'] to 'nilrt'\n if grains.get('lsb_distrib_id', '').lower().startswith('nilrt'):\n grains['osfullname'] = 'nilrt'\n else:\n grains['osfullname'] = grains.get('lsb_distrib_id', osname).strip()\n if 'osrelease' not in grains:\n # NOTE: This is a workaround for CentOS 7 os-release bug\n # https://bugs.centos.org/view.php?id=8359\n # /etc/os-release contains no minor distro release number so we fall back to parse\n # /etc/centos-release file instead.\n # Commit introducing this comment should be reverted after the upstream bug is released.\n if 'CentOS Linux 7' in grains.get('lsb_distrib_codename', ''):\n grains.pop('lsb_distrib_release', None)\n grains['osrelease'] = grains.get('lsb_distrib_release', osrelease).strip()\n grains['oscodename'] = grains.get('lsb_distrib_codename', '').strip() or oscodename\n if 'Red Hat' in grains['oscodename']:\n grains['oscodename'] = oscodename\n distroname = _REPLACE_LINUX_RE.sub('', grains['osfullname']).strip()\n # return the first ten characters with no spaces, lowercased\n shortname = distroname.replace(' ', '').lower()[:10]\n # this maps the long names from the /etc/DISTRO-release files to the\n # traditional short names that Salt has used.\n if 'os' not in grains:\n grains['os'] = _OS_NAME_MAP.get(shortname, distroname)\n grains.update(_linux_cpudata())\n grains.update(_linux_gpu_data())\n elif grains['kernel'] == 'SunOS':\n if salt.utils.platform.is_smartos():\n # See https://github.com/joyent/smartos-live/issues/224\n if HAS_UNAME:\n uname_v = os.uname()[3] # format: joyent_20161101T004406Z\n else:\n uname_v = os.name\n uname_v = uname_v[uname_v.index('_')+1:]\n grains['os'] = grains['osfullname'] = 'SmartOS'\n # store a parsed version of YYYY.MM.DD as osrelease\n grains['osrelease'] = \".\".join([\n uname_v.split('T')[0][0:4],\n uname_v.split('T')[0][4:6],\n uname_v.split('T')[0][6:8],\n ])\n # store a untouched copy of the timestamp in osrelease_stamp\n grains['osrelease_stamp'] = uname_v\n elif os.path.isfile('/etc/release'):\n with salt.utils.files.fopen('/etc/release', 'r') as fp_:\n rel_data = fp_.read()\n try:\n release_re = re.compile(\n r'((?:Open|Oracle )?Solaris|OpenIndiana|OmniOS) (Development)?'\n r'\\s*(\\d+\\.?\\d*|v\\d+)\\s?[A-Z]*\\s?(r\\d+|\\d+\\/\\d+|oi_\\S+|snv_\\S+)?'\n )\n osname, development, osmajorrelease, osminorrelease = release_re.search(rel_data).groups()\n except AttributeError:\n # Set a blank osrelease grain and fallback to 'Solaris'\n # as the 'os' grain.\n grains['os'] = grains['osfullname'] = 'Solaris'\n grains['osrelease'] = ''\n else:\n if development is not None:\n osname = ' '.join((osname, development))\n if HAS_UNAME:\n uname_v = os.uname()[3]\n else:\n uname_v = os.name\n grains['os'] = grains['osfullname'] = osname\n if osname in ['Oracle Solaris'] and uname_v.startswith(osmajorrelease):\n # Oracla Solars 11 and up have minor version in uname\n grains['osrelease'] = uname_v\n elif osname in ['OmniOS']:\n # OmniOS\n osrelease = []\n osrelease.append(osmajorrelease[1:])\n osrelease.append(osminorrelease[1:])\n grains['osrelease'] = \".\".join(osrelease)\n grains['osrelease_stamp'] = uname_v\n else:\n # Sun Solaris 10 and earlier/comparable\n osrelease = []\n osrelease.append(osmajorrelease)\n if osminorrelease:\n osrelease.append(osminorrelease)\n grains['osrelease'] = \".\".join(osrelease)\n grains['osrelease_stamp'] = uname_v\n\n grains.update(_sunos_cpudata())\n elif grains['kernel'] == 'VMkernel':\n grains['os'] = 'ESXi'\n elif grains['kernel'] == 'Darwin':\n osrelease = __salt__['cmd.run']('sw_vers -productVersion')\n osname = __salt__['cmd.run']('sw_vers -productName')\n osbuild = __salt__['cmd.run']('sw_vers -buildVersion')\n grains['os'] = 'MacOS'\n grains['os_family'] = 'MacOS'\n grains['osfullname'] = \"{0} {1}\".format(osname, osrelease)\n grains['osrelease'] = osrelease\n grains['osbuild'] = osbuild\n grains['init'] = 'launchd'\n grains.update(_bsd_cpudata(grains))\n grains.update(_osx_gpudata())\n grains.update(_osx_platform_data())\n elif grains['kernel'] == 'AIX':\n osrelease = __salt__['cmd.run']('oslevel')\n osrelease_techlevel = __salt__['cmd.run']('oslevel -r')\n osname = __salt__['cmd.run']('uname')\n grains['os'] = 'AIX'\n grains['osfullname'] = osname\n grains['osrelease'] = osrelease\n grains['osrelease_techlevel'] = osrelease_techlevel\n grains.update(_aix_cpudata())\n else:\n grains['os'] = grains['kernel']\n if grains['kernel'] == 'FreeBSD':\n try:\n grains['osrelease'] = __salt__['cmd.run']('freebsd-version -u').split('-')[0]\n except salt.exceptions.CommandExecutionError:\n # freebsd-version was introduced in 10.0.\n # derive osrelease from kernelversion prior to that\n grains['osrelease'] = grains['kernelrelease'].split('-')[0]\n grains.update(_bsd_cpudata(grains))\n if grains['kernel'] in ('OpenBSD', 'NetBSD'):\n grains.update(_bsd_cpudata(grains))\n grains['osrelease'] = grains['kernelrelease'].split('-')[0]\n if grains['kernel'] == 'NetBSD':\n grains.update(_netbsd_gpu_data())\n if not grains['os']:\n grains['os'] = 'Unknown {0}'.format(grains['kernel'])\n grains['os_family'] = 'Unknown'\n else:\n # this assigns family names based on the os name\n # family defaults to the os name if not found\n grains['os_family'] = _OS_FAMILY_MAP.get(grains['os'],\n grains['os'])\n\n # Build the osarch grain. This grain will be used for platform-specific\n # considerations such as package management. Fall back to the CPU\n # architecture.\n if grains.get('os_family') == 'Debian':\n osarch = __salt__['cmd.run']('dpkg --print-architecture').strip()\n elif grains.get('os_family') in ['RedHat', 'Suse']:\n osarch = salt.utils.pkg.rpm.get_osarch()\n elif grains.get('os_family') in ('NILinuxRT', 'Poky'):\n archinfo = {}\n for line in __salt__['cmd.run']('opkg print-architecture').splitlines():\n if line.startswith('arch'):\n _, arch, priority = line.split()\n archinfo[arch.strip()] = int(priority.strip())\n\n # Return osarch in priority order (higher to lower)\n osarch = sorted(archinfo, key=archinfo.get, reverse=True)\n else:\n osarch = grains['cpuarch']\n grains['osarch'] = osarch\n\n grains.update(_memdata(grains))\n\n # Get the hardware and bios data\n grains.update(_hw_data(grains))\n\n # Load the virtual machine info\n grains.update(_virtual(grains))\n grains.update(_virtual_hv(grains))\n grains.update(_ps(grains))\n\n if grains.get('osrelease', ''):\n osrelease_info = grains['osrelease'].split('.')\n for idx, value in enumerate(osrelease_info):\n if not value.isdigit():\n continue\n osrelease_info[idx] = int(value)\n grains['osrelease_info'] = tuple(osrelease_info)\n try:\n grains['osmajorrelease'] = int(grains['osrelease_info'][0])\n except (IndexError, TypeError, ValueError):\n log.debug(\n 'Unable to derive osmajorrelease from osrelease_info \\'%s\\'. '\n 'The osmajorrelease grain will not be set.',\n grains['osrelease_info']\n )\n os_name = grains['os' if grains.get('os') in (\n 'Debian', 'FreeBSD', 'OpenBSD', 'NetBSD', 'Mac', 'Raspbian') else 'osfullname']\n grains['osfinger'] = '{0}-{1}'.format(\n os_name, grains['osrelease'] if os_name in ('Ubuntu',) else grains['osrelease_info'][0])\n\n return grains"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef locale_info():\n '''\n Provides\n defaultlanguage\n defaultencoding\n '''\n grains = {}\n grains['locale_info'] = {}\n\n if salt.utils.platform.is_proxy():\n return grains\n\n try:\n (\n grains['locale_info']['defaultlanguage'],\n grains['locale_info']['defaultencoding']\n ) = locale.getdefaultlocale()\n except Exception:\n # locale.getdefaultlocale can ValueError!! Catch anything else it\n # might do, per #2205\n grains['locale_info']['defaultlanguage'] = 'unknown'\n grains['locale_info']['defaultencoding'] = 'unknown'\n grains['locale_info']['detectedencoding'] = __salt_system_encoding__\n if _DATEUTIL_TZ:\n grains['locale_info']['timezone'] = datetime.datetime.now(dateutil.tz.tzlocal()).tzname()\n return grains", "response": "Provides a dictionary of locale info for the current locale."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef hostname():\n '''\n Return fqdn, hostname, domainname\n '''\n # This is going to need some work\n # Provides:\n # fqdn\n # host\n # localhost\n # domain\n global __FQDN__\n grains = {}\n\n if salt.utils.platform.is_proxy():\n return grains\n\n grains['localhost'] = socket.gethostname()\n if __FQDN__ is None:\n __FQDN__ = salt.utils.network.get_fqhostname()\n\n # On some distros (notably FreeBSD) if there is no hostname set\n # salt.utils.network.get_fqhostname() will return None.\n # In this case we punt and log a message at error level, but force the\n # hostname and domain to be localhost.localdomain\n # Otherwise we would stacktrace below\n if __FQDN__ is None: # still!\n log.error('Having trouble getting a hostname. Does this machine have its hostname and domain set properly?')\n __FQDN__ = 'localhost.localdomain'\n\n grains['fqdn'] = __FQDN__\n (grains['host'], grains['domain']) = grains['fqdn'].partition('.')[::2]\n return grains", "response": "Return fqdn hostname domainname"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef append_domain():\n '''\n Return append_domain if set\n '''\n\n grain = {}\n\n if salt.utils.platform.is_proxy():\n return grain\n\n if 'append_domain' in __opts__:\n grain['append_domain'] = __opts__['append_domain']\n return grain", "response": "Return append_domain if set\n Return append_domain if proxy"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning all known FQDNs for the system.", "response": "def fqdns():\n '''\n Return all known FQDNs for the system by enumerating all interfaces and\n then trying to reverse resolve them (excluding 'lo' interface).\n '''\n # Provides:\n # fqdns\n\n grains = {}\n fqdns = set()\n\n addresses = salt.utils.network.ip_addrs(include_loopback=False, interface_data=_get_interfaces())\n addresses.extend(salt.utils.network.ip_addrs6(include_loopback=False, interface_data=_get_interfaces()))\n err_message = 'Exception during resolving address: %s'\n for ip in addresses:\n try:\n name, aliaslist, addresslist = socket.gethostbyaddr(ip)\n fqdns.update([socket.getfqdn(name)] + [als for als in aliaslist if salt.utils.network.is_fqdn(als)])\n except socket.herror as err:\n if err.errno == 0:\n # No FQDN for this IP address, so we don't need to know this all the time.\n log.debug(\"Unable to resolve address %s: %s\", ip, err)\n else:\n log.error(err_message, err)\n except (socket.error, socket.gaierror, socket.timeout) as err:\n log.error(err_message, err)\n\n return {\"fqdns\": sorted(list(fqdns))}"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a dict of the connected interfaces and their ip addresses", "response": "def ip_interfaces():\n '''\n Provide a dict of the connected interfaces and their ip addresses\n The addresses will be passed as a list for each interface\n '''\n # Provides:\n # ip_interfaces\n\n if salt.utils.platform.is_proxy():\n return {}\n\n ret = {}\n ifaces = _get_interfaces()\n for face in ifaces:\n iface_ips = []\n for inet in ifaces[face].get('inet', []):\n if 'address' in inet:\n iface_ips.append(inet['address'])\n for inet in ifaces[face].get('inet6', []):\n if 'address' in inet:\n iface_ips.append(inet['address'])\n for secondary in ifaces[face].get('secondary', []):\n if 'address' in secondary:\n iface_ips.append(secondary['address'])\n ret[face] = iface_ips\n return {'ip_interfaces': ret}"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef hwaddr_interfaces():\n '''\n Provide a dict of the connected interfaces and their\n hw addresses (Mac Address)\n '''\n # Provides:\n # hwaddr_interfaces\n ret = {}\n ifaces = _get_interfaces()\n for face in ifaces:\n if 'hwaddr' in ifaces[face]:\n ret[face] = ifaces[face]['hwaddr']\n return {'hwaddr_interfaces': ret}", "response": "Provides a dict of the connected interfaces and their\n hw addresses"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef dns():\n '''\n Parse the resolver configuration file\n\n .. versionadded:: 2016.3.0\n '''\n # Provides:\n # dns\n if salt.utils.platform.is_windows() or 'proxyminion' in __opts__:\n return {}\n\n resolv = salt.utils.dns.parse_resolv()\n for key in ('nameservers', 'ip4_nameservers', 'ip6_nameservers',\n 'sortlist'):\n if key in resolv:\n resolv[key] = [six.text_type(i) for i in resolv[key]]\n\n return {'dns': resolv} if resolv else {}", "response": "Parse the DNS configuration file and return a dict of resolv entries."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef saltpath():\n '''\n Return the path of the salt module\n '''\n # Provides:\n # saltpath\n salt_path = os.path.abspath(os.path.join(__file__, os.path.pardir))\n return {'saltpath': os.path.dirname(salt_path)}", "response": "Returns the absolute path of the salt module"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the system specific hardware data from dmidecode.", "response": "def _hw_data(osdata):\n '''\n Get system specific hardware data from dmidecode\n\n Provides\n biosversion\n productname\n manufacturer\n serialnumber\n biosreleasedate\n uuid\n\n .. versionadded:: 0.9.5\n '''\n\n if salt.utils.platform.is_proxy():\n return {}\n\n grains = {}\n if osdata['kernel'] == 'Linux' and os.path.exists('/sys/class/dmi/id'):\n # On many Linux distributions basic firmware information is available via sysfs\n # requires CONFIG_DMIID to be enabled in the Linux kernel configuration\n sysfs_firmware_info = {\n 'biosversion': 'bios_version',\n 'productname': 'product_name',\n 'manufacturer': 'sys_vendor',\n 'biosreleasedate': 'bios_date',\n 'uuid': 'product_uuid',\n 'serialnumber': 'product_serial'\n }\n for key, fw_file in sysfs_firmware_info.items():\n contents_file = os.path.join('/sys/class/dmi/id', fw_file)\n if os.path.exists(contents_file):\n try:\n with salt.utils.files.fopen(contents_file, 'r') as ifile:\n grains[key] = salt.utils.stringutils.to_unicode(ifile.read().strip(), errors='replace')\n if key == 'uuid':\n grains['uuid'] = grains['uuid'].lower()\n except (IOError, OSError) as err:\n # PermissionError is new to Python 3, but corresponds to the EACESS and\n # EPERM error numbers. Use those instead here for PY2 compatibility.\n if err.errno == EACCES or err.errno == EPERM:\n # Skip the grain if non-root user has no access to the file.\n pass\n elif salt.utils.path.which_bin(['dmidecode', 'smbios']) is not None and not (\n salt.utils.platform.is_smartos() or\n ( # SunOS on SPARC - 'smbios: failed to load SMBIOS: System does not export an SMBIOS table'\n osdata['kernel'] == 'SunOS' and\n osdata['cpuarch'].startswith('sparc')\n )):\n # On SmartOS (possibly SunOS also) smbios only works in the global zone\n # smbios is also not compatible with linux's smbios (smbios -s = print summarized)\n grains = {\n 'biosversion': __salt__['smbios.get']('bios-version'),\n 'productname': __salt__['smbios.get']('system-product-name'),\n 'manufacturer': __salt__['smbios.get']('system-manufacturer'),\n 'biosreleasedate': __salt__['smbios.get']('bios-release-date'),\n 'uuid': __salt__['smbios.get']('system-uuid')\n }\n grains = dict([(key, val) for key, val in grains.items() if val is not None])\n uuid = __salt__['smbios.get']('system-uuid')\n if uuid is not None:\n grains['uuid'] = uuid.lower()\n for serial in ('system-serial-number', 'chassis-serial-number', 'baseboard-serial-number'):\n serial = __salt__['smbios.get'](serial)\n if serial is not None:\n grains['serialnumber'] = serial\n break\n elif salt.utils.path.which_bin(['fw_printenv']) is not None:\n # ARM Linux devices expose UBOOT env variables via fw_printenv\n hwdata = {\n 'manufacturer': 'manufacturer',\n 'serialnumber': 'serial#',\n 'productname': 'DeviceDesc',\n }\n for grain_name, cmd_key in six.iteritems(hwdata):\n result = __salt__['cmd.run_all']('fw_printenv {0}'.format(cmd_key))\n if result['retcode'] == 0:\n uboot_keyval = result['stdout'].split('=')\n grains[grain_name] = _clean_value(grain_name, uboot_keyval[1])\n elif osdata['kernel'] == 'FreeBSD':\n # On FreeBSD /bin/kenv (already in base system)\n # can be used instead of dmidecode\n kenv = salt.utils.path.which('kenv')\n if kenv:\n # In theory, it will be easier to add new fields to this later\n fbsd_hwdata = {\n 'biosversion': 'smbios.bios.version',\n 'manufacturer': 'smbios.system.maker',\n 'serialnumber': 'smbios.system.serial',\n 'productname': 'smbios.system.product',\n 'biosreleasedate': 'smbios.bios.reldate',\n 'uuid': 'smbios.system.uuid',\n }\n for key, val in six.iteritems(fbsd_hwdata):\n value = __salt__['cmd.run']('{0} {1}'.format(kenv, val))\n grains[key] = _clean_value(key, value)\n elif osdata['kernel'] == 'OpenBSD':\n sysctl = salt.utils.path.which('sysctl')\n hwdata = {'biosversion': 'hw.version',\n 'manufacturer': 'hw.vendor',\n 'productname': 'hw.product',\n 'serialnumber': 'hw.serialno',\n 'uuid': 'hw.uuid'}\n for key, oid in six.iteritems(hwdata):\n value = __salt__['cmd.run']('{0} -n {1}'.format(sysctl, oid))\n if not value.endswith(' value is not available'):\n grains[key] = _clean_value(key, value)\n elif osdata['kernel'] == 'NetBSD':\n sysctl = salt.utils.path.which('sysctl')\n nbsd_hwdata = {\n 'biosversion': 'machdep.dmi.board-version',\n 'manufacturer': 'machdep.dmi.system-vendor',\n 'serialnumber': 'machdep.dmi.system-serial',\n 'productname': 'machdep.dmi.system-product',\n 'biosreleasedate': 'machdep.dmi.bios-date',\n 'uuid': 'machdep.dmi.system-uuid',\n }\n for key, oid in six.iteritems(nbsd_hwdata):\n result = __salt__['cmd.run_all']('{0} -n {1}'.format(sysctl, oid))\n if result['retcode'] == 0:\n grains[key] = _clean_value(key, result['stdout'])\n elif osdata['kernel'] == 'Darwin':\n grains['manufacturer'] = 'Apple Inc.'\n sysctl = salt.utils.path.which('sysctl')\n hwdata = {'productname': 'hw.model'}\n for key, oid in hwdata.items():\n value = __salt__['cmd.run']('{0} -b {1}'.format(sysctl, oid))\n if not value.endswith(' is invalid'):\n grains[key] = _clean_value(key, value)\n elif osdata['kernel'] == 'SunOS' and osdata['cpuarch'].startswith('sparc'):\n # Depending on the hardware model, commands can report different bits\n # of information. With that said, consolidate the output from various\n # commands and attempt various lookups.\n data = \"\"\n for (cmd, args) in (('/usr/sbin/prtdiag', '-v'), ('/usr/sbin/prtconf', '-vp'), ('/usr/sbin/virtinfo', '-a')):\n if salt.utils.path.which(cmd): # Also verifies that cmd is executable\n data += __salt__['cmd.run']('{0} {1}'.format(cmd, args))\n data += '\\n'\n\n sn_regexes = [\n re.compile(r) for r in [\n r'(?im)^\\s*Chassis\\s+Serial\\s+Number\\n-+\\n(\\S+)', # prtdiag\n r'(?im)^\\s*chassis-sn:\\s*(\\S+)', # prtconf\n r'(?im)^\\s*Chassis\\s+Serial#:\\s*(\\S+)', # virtinfo\n ]\n ]\n\n obp_regexes = [\n re.compile(r) for r in [\n r'(?im)^\\s*System\\s+PROM\\s+revisions.*\\nVersion\\n-+\\nOBP\\s+(\\S+)\\s+(\\S+)', # prtdiag\n r'(?im)^\\s*version:\\s*\\'OBP\\s+(\\S+)\\s+(\\S+)', # prtconf\n ]\n ]\n\n fw_regexes = [\n re.compile(r) for r in [\n r'(?im)^\\s*Sun\\s+System\\s+Firmware\\s+(\\S+)\\s+(\\S+)', # prtdiag\n ]\n ]\n\n uuid_regexes = [\n re.compile(r) for r in [\n r'(?im)^\\s*Domain\\s+UUID:\\s*(\\S+)', # virtinfo\n ]\n ]\n\n manufacture_regexes = [\n re.compile(r) for r in [\n r'(?im)^\\s*System\\s+Configuration:\\s*(.*)(?=sun)', # prtdiag\n ]\n ]\n\n product_regexes = [\n re.compile(r) for r in [\n r'(?im)^\\s*System\\s+Configuration:\\s*.*?sun\\d\\S+[^\\S\\r\\n]*(.*)', # prtdiag\n r'(?im)^[^\\S\\r\\n]*banner-name:[^\\S\\r\\n]*(.*)', # prtconf\n r'(?im)^[^\\S\\r\\n]*product-name:[^\\S\\r\\n]*(.*)', # prtconf\n ]\n ]\n\n sn_regexes = [\n re.compile(r) for r in [\n r'(?im)Chassis\\s+Serial\\s+Number\\n-+\\n(\\S+)', # prtdiag\n r'(?i)Chassis\\s+Serial#:\\s*(\\S+)', # virtinfo\n r'(?i)chassis-sn:\\s*(\\S+)', # prtconf\n ]\n ]\n\n obp_regexes = [\n re.compile(r) for r in [\n r'(?im)System\\s+PROM\\s+revisions.*\\nVersion\\n-+\\nOBP\\s+(\\S+)\\s+(\\S+)', # prtdiag\n r'(?im)version:\\s*\\'OBP\\s+(\\S+)\\s+(\\S+)', # prtconf\n ]\n ]\n\n fw_regexes = [\n re.compile(r) for r in [\n r'(?i)Sun\\s+System\\s+Firmware\\s+(\\S+)\\s+(\\S+)', # prtdiag\n ]\n ]\n\n uuid_regexes = [\n re.compile(r) for r in [\n r'(?i)Domain\\s+UUID:\\s+(\\S+)', # virtinfo\n ]\n ]\n\n for regex in sn_regexes:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n grains['serialnumber'] = res.group(1).strip().replace(\"'\", \"\")\n break\n\n for regex in obp_regexes:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n obp_rev, obp_date = res.groups()[0:2] # Limit the number in case we found the data in multiple places\n grains['biosversion'] = obp_rev.strip().replace(\"'\", \"\")\n grains['biosreleasedate'] = obp_date.strip().replace(\"'\", \"\")\n\n for regex in fw_regexes:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n fw_rev, fw_date = res.groups()[0:2]\n grains['systemfirmware'] = fw_rev.strip().replace(\"'\", \"\")\n grains['systemfirmwaredate'] = fw_date.strip().replace(\"'\", \"\")\n break\n\n for regex in uuid_regexes:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n grains['uuid'] = res.group(1).strip().replace(\"'\", \"\")\n break\n\n for regex in manufacture_regexes:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n grains['manufacture'] = res.group(1).strip().replace(\"'\", \"\")\n break\n\n for regex in product_regexes:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n t_productname = res.group(1).strip().replace(\"'\", \"\")\n if t_productname:\n grains['product'] = t_productname\n grains['productname'] = t_productname\n break\n elif osdata['kernel'] == 'AIX':\n cmd = salt.utils.path.which('prtconf')\n if cmd:\n data = __salt__['cmd.run']('{0}'.format(cmd)) + os.linesep\n for dest, regstring in (('serialnumber', r'(?im)^\\s*Machine\\s+Serial\\s+Number:\\s+(\\S+)'),\n ('systemfirmware', r'(?im)^\\s*Firmware\\s+Version:\\s+(.*)')):\n for regex in [re.compile(r) for r in [regstring]]:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n grains[dest] = res.group(1).strip().replace(\"'\", '')\n\n product_regexes = [re.compile(r'(?im)^\\s*System\\s+Model:\\s+(\\S+)')]\n for regex in product_regexes:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n grains['manufacturer'], grains['productname'] = res.group(1).strip().replace(\"'\", \"\").split(\",\")\n break\n else:\n log.error('The \\'prtconf\\' binary was not found in $PATH.')\n\n elif osdata['kernel'] == 'AIX':\n cmd = salt.utils.path.which('prtconf')\n if data:\n data = __salt__['cmd.run']('{0}'.format(cmd)) + os.linesep\n for dest, regstring in (('serialnumber', r'(?im)^\\s*Machine\\s+Serial\\s+Number:\\s+(\\S+)'),\n ('systemfirmware', r'(?im)^\\s*Firmware\\s+Version:\\s+(.*)')):\n for regex in [re.compile(r) for r in [regstring]]:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n grains[dest] = res.group(1).strip().replace(\"'\", '')\n\n product_regexes = [re.compile(r'(?im)^\\s*System\\s+Model:\\s+(\\S+)')]\n for regex in product_regexes:\n res = regex.search(data)\n if res and len(res.groups()) >= 1:\n grains['manufacturer'], grains['productname'] = res.group(1).strip().replace(\"'\", \"\").split(\",\")\n break\n else:\n log.error('The \\'prtconf\\' binary was not found in $PATH.')\n\n return grains"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nshell - out Python 3 for compute reliable hash", "response": "def _get_hash_by_shell():\n '''\n Shell-out Python 3 for compute reliable hash\n :return:\n '''\n id_ = __opts__.get('id', '')\n id_hash = None\n py_ver = sys.version_info[:2]\n if py_ver >= (3, 3):\n # Python 3.3 enabled hash randomization, so we need to shell out to get\n # a reliable hash.\n id_hash = __salt__['cmd.run']([sys.executable, '-c', 'print(hash(\"{0}\"))'.format(id_)],\n env={'PYTHONHASHSEED': '0'})\n try:\n id_hash = int(id_hash)\n except (TypeError, ValueError):\n log.debug('Failed to hash the ID to get the server_id grain. Result of hash command: %s', id_hash)\n id_hash = None\n if id_hash is None:\n # Python < 3.3 or error encountered above\n id_hash = hash(id_)\n\n return abs(id_hash % (2 ** 31))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nproviding an integer based on the FQDN of a machine.", "response": "def get_server_id():\n '''\n Provides an integer based on the FQDN of a machine.\n Useful as server-id in MySQL replication or anywhere else you'll need an ID\n like this.\n '''\n # Provides:\n # server_id\n\n if salt.utils.platform.is_proxy():\n server_id = {}\n else:\n use_crc = __opts__.get('server_id_use_crc')\n if bool(use_crc):\n id_hash = getattr(zlib, use_crc, zlib.adler32)(__opts__.get('id', '').encode()) & 0xffffffff\n else:\n log.debug('This server_id is computed not by Adler32 nor by CRC32. '\n 'Please use \"server_id_use_crc\" option and define algorithm you '\n 'prefer (default \"Adler32\"). Starting with Sodium, the '\n 'server_id will be computed with Adler32 by default.')\n id_hash = _get_hash_by_shell()\n server_id = {'server_id': id_hash}\n\n return server_id"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn a list of grains which describe whether a server has a default gateway.", "response": "def default_gateway():\n '''\n Populates grains which describe whether a server has a default gateway\n configured or not. Uses `ip -4 route show` and `ip -6 route show` and greps\n for a `default` at the beginning of any line. Assuming the standard\n `default via <ip>` format for default gateways, it will also parse out the\n ip address of the default gateway, and put it in ip4_gw or ip6_gw.\n\n If the `ip` command is unavailable, no grains will be populated.\n\n Currently does not support multiple default gateways. The grains will be\n set to the first default gateway found.\n\n List of grains:\n\n ip4_gw: True # ip/True/False if default ipv4 gateway\n ip6_gw: True # ip/True/False if default ipv6 gateway\n ip_gw: True # True if either of the above is True, False otherwise\n '''\n grains = {}\n ip_bin = salt.utils.path.which('ip')\n if not ip_bin:\n return {}\n grains['ip_gw'] = False\n grains['ip4_gw'] = False\n grains['ip6_gw'] = False\n for ip_version in ('4', '6'):\n try:\n out = __salt__['cmd.run']([ip_bin, '-' + ip_version, 'route', 'show'])\n for line in out.splitlines():\n if line.startswith('default'):\n grains['ip_gw'] = True\n grains['ip{0}_gw'.format(ip_version)] = True\n try:\n via, gw_ip = line.split()[1:3]\n except ValueError:\n pass\n else:\n if via == 'via':\n grains['ip{0}_gw'.format(ip_version)] = gw_ip\n break\n except Exception:\n continue\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the kernel boot parameters", "response": "def kernelparams():\n '''\n Return the kernel boot parameters\n '''\n if salt.utils.platform.is_windows():\n # TODO: add grains using `bcdedit /enum {current}`\n return {}\n else:\n try:\n with salt.utils.files.fopen('/proc/cmdline', 'r') as fhr:\n cmdline = fhr.read()\n grains = {'kernelparams': []}\n for data in [item.split('=') for item in salt.utils.args.shlex_split(cmdline)]:\n value = None\n if len(data) == 2:\n value = data[1].strip('\"')\n\n grains['kernelparams'] += [(data[0], value)]\n except IOError as exc:\n grains = {}\n log.debug('Failed to read /proc/cmdline: %s', exc)\n\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _osquery_cmd(table, attrs=None, where=None, format='json'):\n '''\n Helper function to run osquery queries\n '''\n ret = {\n 'result': True,\n }\n\n if attrs:\n if isinstance(attrs, list):\n valid_attrs = _table_attrs(table)\n if valid_attrs:\n for a in attrs:\n if a not in valid_attrs:\n ret['result'] = False\n ret['comment'] = '{0} is not a valid attribute for table {1}'.format(a, table)\n return ret\n _attrs = ','.join(attrs)\n else:\n ret['result'] = False\n ret['comment'] = 'Invalid table {0}.'.format(table)\n return ret\n else:\n ret['comment'] = 'attrs must be specified as a list.'\n ret['result'] = False\n return ret\n else:\n _attrs = '*'\n\n sql = 'select {0} from {1}'.format(_attrs, table)\n\n if where:\n sql = '{0} where {1}'.format(sql, where)\n\n sql = '{0};'.format(sql)\n\n res = _osquery(sql)\n if res['result']:\n ret['data'] = res['data']\n else:\n ret['comment'] = res['error']\n return ret", "response": "Helper function to run osquery queries\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef version():\n '''\n Return version of osquery\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' osquery.version\n '''\n _false_return = {'result': False,\n 'comment': 'OSQuery version unavailable.'}\n res = _osquery_cmd(table='osquery_info', attrs=['version'])\n if 'result' in res and res['result']:\n if 'data' in res and isinstance(res['data'], list):\n return res['data'][0].get('version', '') or _false_return\n return _false_return", "response": "Return version of osquery\nTaxonomy CLI Example : OSQuery version of OSQuery version available."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef rpm_packages(attrs=None, where=None):\n '''\n Return cpuid information from osquery\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' osquery.rpm_packages\n '''\n if __grains__['os_family'] == 'RedHat':\n return _osquery_cmd(table='rpm_packages', attrs=attrs, where=where)\n return {'result': False,\n 'comment': 'Only available on Red Hat based systems.'}", "response": "Return cpuid information from osquery\n CLI Example : osquery\n salt '*' osquery. rpm_packages"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns kernel_integrity information from osquery", "response": "def kernel_integrity(attrs=None, where=None):\n '''\n Return kernel_integrity information from osquery\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' osquery.kernel_integrity\n '''\n if __grains__['os_family'] in ['RedHat', 'Debian']:\n return _osquery_cmd(table='kernel_integrity', attrs=attrs, where=where)\n return {'result': False, 'comment': 'Only available on Red Hat or Debian based systems.'}"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef kernel_modules(attrs=None, where=None):\n '''\n Return kernel_modules information from osquery\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' osquery.kernel_modules\n '''\n if __grains__['os_family'] in ['RedHat', 'Debian']:\n return _osquery_cmd(table='kernel_modules', attrs=attrs, where=where)\n return {'result': False, 'comment': 'Only available on Red Hat or Debian based systems.'}", "response": "Return kernel_modules information from osquery"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns memory_map information from osquery", "response": "def memory_map(attrs=None, where=None):\n '''\n Return memory_map information from osquery\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' osquery.memory_map\n '''\n if __grains__['os_family'] in ['RedHat', 'Debian']:\n return _osquery_cmd(table='memory_map', attrs=attrs, where=where)\n return {'result': False, 'comment': 'Only available on Red Hat or Debian based systems.'}"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef process_memory_map(attrs=None, where=None):\n '''\n Return process_memory_map information from osquery\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' osquery.process_memory_map\n '''\n if __grains__['os_family'] in ['RedHat', 'Debian']:\n return _osquery_cmd(table='process_memory_map', attrs=attrs, where=where)\n return {'result': False, 'comment': 'Only available on Red Hat or Debian based systems.'}", "response": "Return process_memory_map information from osquery"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the current shared_memory information from osquery", "response": "def shared_memory(attrs=None, where=None):\n '''\n Return shared_memory information from osquery\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' osquery.shared_memory\n '''\n if __grains__['os_family'] in ['RedHat', 'Debian']:\n return _osquery_cmd(table='shared_memory', attrs=attrs, where=where)\n return {'result': False, 'comment': 'Only available on Red Hat or Debian based systems.'}"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef apt_sources(attrs=None, where=None):\n '''\n Return apt_sources information from osquery\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' osquery.apt_sources\n '''\n if __grains__['os_family'] == 'Debian':\n return _osquery_cmd(table='apt_sources', attrs=attrs, where=where)\n return {'result': False, 'comment': 'Only available on Debian based systems.'}", "response": "Return apt_sources information from osquery"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef deb_packages(attrs=None, where=None):\n '''\n Return deb_packages information from osquery\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' osquery.deb_packages\n '''\n if __grains__['os_family'] == 'Debian':\n return _osquery_cmd(table='deb_packages', attrs=attrs, where=where)\n return {'result': False, 'comment': 'Only available on Debian based systems.'}", "response": "Return deb_packages information from osquery"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn alf information from osquery", "response": "def alf(attrs=None, where=None):\n '''\n Return alf information from osquery\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' osquery.alf\n '''\n if salt.utils.platform.is_darwin():\n return _osquery_cmd(table='alf', attrs=attrs, where=where)\n return {'result': False, 'comment': 'Only available on macOS systems.'}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef verify_fun(lazy_obj, fun):\n '''\n Check that the function passed really exists\n '''\n if not fun:\n raise salt.exceptions.SaltInvocationError(\n 'Must specify a function to run!\\n'\n 'ex: manage.up'\n )\n if fun not in lazy_obj:\n # If the requested function isn't available, lets say why\n raise salt.exceptions.CommandExecutionError(lazy_obj.missing_fun_string(fun))", "response": "Verify that the function passed in is available in the lazy_obj"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nclears the dict of all the keys in the object", "response": "def clear(self):\n '''\n Clear the dict\n '''\n # create a dict to store loaded values in\n self._dict = getattr(self, 'mod_dict_class', dict)()\n\n # have we already loded everything?\n self.loaded = False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking to see if an instance profile exists.", "response": "def instance_profile_exists(name, region=None, key=None, keyid=None,\n profile=None):\n '''\n Check to see if an instance profile exists.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.instance_profile_exists myiprofile\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n # Boto weirdly returns an exception here if an instance profile doesn't\n # exist.\n conn.get_instance_profile(name)\n return True\n except boto.exception.BotoServerError:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef role_exists(name, region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if an IAM role exists.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.role_exists myirole\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n conn.get_role(name)\n return True\n except boto.exception.BotoServerError:\n return False", "response": "Check to see if an IAM role exists."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef describe_role(name, region=None, key=None, keyid=None, profile=None):\n '''\n Get information for a role.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.describe_role myirole\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n info = conn.get_role(name)\n if not info:\n return False\n role = info.get_role_response.get_role_result.role\n role['assume_role_policy_document'] = salt.utils.json.loads(_unquote(\n role.assume_role_policy_document\n ))\n # If Sid wasn't defined by the user, boto will still return a Sid in\n # each policy. To properly check idempotently, let's remove the Sid\n # from the return if it's not actually set.\n for policy_key, policy in role['assume_role_policy_document'].items():\n if policy_key == 'Statement':\n for val in policy:\n if 'Sid' in val and not val['Sid']:\n del val['Sid']\n return role\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to get %s information.', name)\n return False", "response": "Get information for a role."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef create_user(user_name, path=None, region=None, key=None, keyid=None,\n profile=None):\n '''\n Create a user.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.create_user myuser\n '''\n if not path:\n path = '/'\n if get_user(user_name, region, key, keyid, profile):\n return True\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n conn.create_user(user_name, path)\n log.info('Created IAM user : %s.', user_name)\n return True\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to create IAM user %s.', user_name)\n return False", "response": "Create a user.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.create_user myuser"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_all_access_keys(user_name, marker=None, max_items=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Get all access keys from a user.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_all_access_keys myuser\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n return conn.get_all_access_keys(user_name, marker, max_items)\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to get access keys for IAM user %s.', user_name)\n return six.text_type(e)", "response": "Get all access keys for a user."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndelete a user. .. versionadded:: 2015.8.0 CLI Example: .. code-block:: bash salt myminion boto_iam.delete_user myuser", "response": "def delete_user(user_name, region=None, key=None, keyid=None,\n profile=None):\n '''\n Delete a user.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.delete_user myuser\n '''\n if not get_user(user_name, region, key, keyid, profile):\n return True\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n conn.delete_user(user_name)\n log.info('Deleted IAM user : %s .', user_name)\n return True\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to delete IAM user %s', user_name)\n return six.text_type(e)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget user information. .. versionadded:: 2015.8.0 CLI Example: .. code-block:: bash salt myminion boto_iam.get_user myuser", "response": "def get_user(user_name=None, region=None, key=None, keyid=None, profile=None):\n '''\n Get user information.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_user myuser\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n info = conn.get_user(user_name)\n if not info:\n return False\n return info\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to get IAM user %s info.', user_name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget group information. .. versionadded:: 2015.8.0 CLI Example: .. code-block:: bash salt myminion boto_iam.get_group mygroup", "response": "def get_group(group_name, region=None, key=None, keyid=None, profile=None):\n '''\n Get group information.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_group mygroup\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n info = conn.get_group(group_name, max_items=1)\n if not info:\n return False\n return info['get_group_response']['get_group_result']['group']\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to get IAM group %s info.', group_name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_group_members(group_name, region=None, key=None, keyid=None, profile=None):\n '''\n Get group information.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_group mygroup\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n marker = None\n truncated = True\n users = []\n while truncated:\n info = conn.get_group(group_name, marker=marker, max_items=1000)\n if not info:\n return False\n truncated = bool(info['get_group_response']['get_group_result']['is_truncated'])\n if truncated and 'marker' in info['get_group_response']['get_group_result']:\n marker = info['get_group_response']['get_group_result']['marker']\n else:\n marker = None\n truncated = False\n users += info['get_group_response']['get_group_result']['users']\n return users\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to get members for IAM group %s.', group_name)\n return False", "response": "Get group information.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_group mygroup"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add_user_to_group(user_name, group_name, region=None, key=None, keyid=None,\n profile=None):\n '''\n Add user to group.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.add_user_to_group myuser mygroup\n '''\n user = get_user(user_name, region, key, keyid, profile)\n if not user:\n log.error('Username : %s does not exist.', user_name)\n return False\n if user_exists_in_group(user_name, group_name, region=region, key=key,\n keyid=keyid, profile=profile):\n return True\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n info = conn.add_user_to_group(group_name, user_name)\n if not info:\n return False\n return info\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to add IAM user %s to group %s.', user_name, group_name)\n return False", "response": "Add a user to a group."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef user_exists_in_group(user_name, group_name, region=None, key=None, keyid=None,\n profile=None):\n '''\n Check if user exists in group.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.user_exists_in_group myuser mygroup\n '''\n # TODO this should probably use boto.iam.get_groups_for_user\n users = get_group_members(\n group_name=group_name, region=region, key=key, keyid=keyid,\n profile=profile\n )\n if users:\n for _user in users:\n if user_name == _user['user_name']:\n log.debug('IAM user %s is already in IAM group %s.', user_name, group_name)\n return True\n return False", "response": "Check if user exists in group."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd or updates a policy document for the specified group.", "response": "def put_group_policy(group_name, policy_name, policy_json, region=None, key=None,\n keyid=None, profile=None):\n '''\n Adds or updates the specified policy document for the specified group.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.put_group_policy mygroup policyname policyrules\n '''\n group = get_group(group_name, region=region, key=key, keyid=keyid,\n profile=profile)\n if not group:\n log.error('Group %s does not exist', group_name)\n return False\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n if not isinstance(policy_json, six.string_types):\n policy_json = salt.utils.json.dumps(policy_json)\n created = conn.put_group_policy(group_name, policy_name,\n policy_json)\n if created:\n log.info('Created policy for IAM group %s.', group_name)\n return True\n log.error('Could not create policy for IAM group %s', group_name)\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to create policy for IAM group %s', group_name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete_group_policy(group_name, policy_name, region=None, key=None,\n keyid=None, profile=None):\n '''\n Delete a group policy.\n\n CLI Example::\n\n .. code-block:: bash\n\n salt myminion boto_iam.delete_group_policy mygroup mypolicy\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if not conn:\n return False\n _policy = get_group_policy(\n group_name, policy_name, region, key, keyid, profile\n )\n if not _policy:\n return True\n try:\n conn.delete_group_policy(group_name, policy_name)\n log.info('Successfully deleted policy %s for IAM group %s.', policy_name, group_name)\n return True\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to delete policy %s for IAM group %s.', policy_name, group_name)\n return False", "response": "Delete a group policy."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the specified policy document for the specified group.", "response": "def get_group_policy(group_name, policy_name, region=None, key=None,\n keyid=None, profile=None):\n '''\n Retrieves the specified policy document for the specified group.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_group_policy mygroup policyname\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n info = conn.get_group_policy(group_name, policy_name)\n log.debug('info for group policy is : %s', info)\n if not info:\n return False\n info = info.get_group_policy_response.get_group_policy_result.policy_document\n info = _unquote(info)\n info = salt.utils.json.loads(info, object_pairs_hook=odict.OrderedDict)\n return info\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to get IAM group %s info.', group_name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets and return all IAM group details starting at the optional path.", "response": "def get_all_groups(path_prefix='/', region=None, key=None, keyid=None,\n profile=None):\n '''\n Get and return all IAM group details, starting at the optional path.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n salt-call boto_iam.get_all_groups\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if not conn:\n return None\n _groups = conn.get_all_groups(path_prefix=path_prefix)\n groups = _groups.list_groups_response.list_groups_result.groups\n marker = getattr(\n _groups.list_groups_response.list_groups_result, 'marker', None\n )\n while marker:\n _groups = conn.get_all_groups(path_prefix=path_prefix, marker=marker)\n groups = groups + _groups.list_groups_response.list_groups_result.groups\n marker = getattr(\n _groups.list_groups_response.list_groups_result, 'marker', None\n )\n return groups"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_all_instance_profiles(path_prefix='/', region=None, key=None,\n keyid=None, profile=None):\n '''\n Get and return all IAM instance profiles, starting at the optional path.\n\n .. versionadded:: 2016.11.0\n\n CLI Example:\n\n salt-call boto_iam.get_all_instance_profiles\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n marker = False\n profiles = []\n while marker is not None:\n marker = marker if marker else None\n p = conn.list_instance_profiles(path_prefix=path_prefix,\n marker=marker)\n res = p.list_instance_profiles_response.list_instance_profiles_result\n profiles += res.instance_profiles\n marker = getattr(res, 'marker', None)\n return profiles", "response": "Get and return all IAM instance profiles starting at the optional path."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlist all IAM instance profiles starting at the optional path.", "response": "def list_instance_profiles(path_prefix='/', region=None, key=None,\n keyid=None, profile=None):\n '''\n List all IAM instance profiles, starting at the optional path.\n\n .. versionadded:: 2016.11.0\n\n CLI Example:\n\n salt-call boto_iam.list_instance_profiles\n '''\n p = get_all_instance_profiles(path_prefix, region, key, keyid, profile)\n return [i['instance_profile_name'] for i in p]"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting a list of policy names from a group.", "response": "def get_all_group_policies(group_name, region=None, key=None, keyid=None,\n profile=None):\n '''\n Get a list of policy names from a group.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_all_group_policies mygroup\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if not conn:\n return False\n try:\n response = conn.get_all_group_policies(group_name)\n _list = response.list_group_policies_response.list_group_policies_result\n return _list.policy_names\n except boto.exception.BotoServerError as e:\n log.debug(e)\n return []"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndeleting a group policy.", "response": "def delete_group(group_name, region=None, key=None,\n keyid=None, profile=None):\n '''\n Delete a group policy.\n\n CLI Example::\n\n .. code-block:: bash\n\n salt myminion boto_iam.delete_group mygroup\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if not conn:\n return False\n _group = get_group(\n group_name, region, key, keyid, profile\n )\n if not _group:\n return True\n try:\n conn.delete_group(group_name)\n log.info('Successfully deleted IAM group %s.', group_name)\n return True\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to delete IAM group %s.', group_name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_login_profile(user_name, password, region=None, key=None,\n keyid=None, profile=None):\n '''\n Creates a login profile for the specified user, give the user the\n ability to access AWS services and the AWS Management Console.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.create_login_profile user_name password\n '''\n user = get_user(user_name, region, key, keyid, profile)\n if not user:\n log.error('IAM user %s does not exist', user_name)\n return False\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n info = conn.create_login_profile(user_name, password)\n log.info('Created profile for IAM user %s.', user_name)\n return info\n except boto.exception.BotoServerError as e:\n log.debug(e)\n if 'Conflict' in e:\n log.info('Profile already exists for IAM user %s.', user_name)\n return 'Conflict'\n log.error('Failed to update profile for IAM user %s.', user_name)\n return False", "response": "Create a login profile for the specified user."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_all_mfa_devices(user_name, region=None, key=None, keyid=None,\n profile=None):\n '''\n Get all MFA devices associated with an IAM user.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_all_mfa_devices user_name\n '''\n user = get_user(user_name, region, key, keyid, profile)\n if not user:\n log.error('IAM user %s does not exist', user_name)\n return False\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n result = conn.get_all_mfa_devices(user_name)\n devices = result['list_mfa_devices_response']['list_mfa_devices_result']['mfa_devices']\n return devices\n except boto.exception.BotoServerError as e:\n log.debug(e)\n if 'Not Found' in e:\n log.info('Could not find IAM user %s.', user_name)\n return []\n log.error('Failed to get all MFA devices for IAM user %s.', user_name)\n return False", "response": "Get all MFA devices associated with an IAM user."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delete_virtual_mfa_device(serial, region=None, key=None, keyid=None, profile=None):\n '''\n Deletes the specified virtual MFA device.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.delete_virtual_mfa_device serial_num\n '''\n conn = __utils__['boto3.get_connection_func']('iam')()\n try:\n conn.delete_virtual_mfa_device(SerialNumber=serial)\n log.info('Deleted virtual MFA device %s.', serial)\n return True\n except botocore.exceptions.ClientError as e:\n log.debug(e)\n if 'NoSuchEntity' in six.text_type(e):\n log.info('Virtual MFA device %s not found.', serial)\n return True\n log.error('Failed to delete virtual MFA device %s.', serial)\n return False", "response": "Deletes the specified virtual MFA device."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef update_account_password_policy(allow_users_to_change_password=None,\n hard_expiry=None, max_password_age=None,\n minimum_password_length=None,\n password_reuse_prevention=None,\n require_lowercase_characters=None,\n require_numbers=None, require_symbols=None,\n require_uppercase_characters=None,\n region=None, key=None, keyid=None,\n profile=None):\n '''\n Update the password policy for the AWS account.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.update_account_password_policy True\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n conn.update_account_password_policy(allow_users_to_change_password,\n hard_expiry, max_password_age,\n minimum_password_length,\n password_reuse_prevention,\n require_lowercase_characters,\n require_numbers, require_symbols,\n require_uppercase_characters)\n log.info('The password policy has been updated.')\n return True\n except boto.exception.BotoServerError as e:\n log.debug(e)\n msg = 'Failed to update the password policy'\n log.error(msg)\n return False", "response": "Update the password policy for the AWS account."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_account_policy(region=None, key=None, keyid=None, profile=None):\n '''\n Get account policy for the AWS account.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_account_policy\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n info = conn.get_account_password_policy()\n return info.get_account_password_policy_response.get_account_password_policy_result.password_policy\n except boto.exception.BotoServerError as e:\n log.debug(e)\n msg = 'Failed to update the password policy.'\n log.error(msg)\n return False", "response": "Get account policy for the AWS account."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_role(name, policy_document=None, path=None, region=None, key=None,\n keyid=None, profile=None):\n '''\n Create an instance role.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.create_role myrole\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if role_exists(name, region, key, keyid, profile):\n return True\n if not policy_document:\n policy_document = None\n try:\n conn.create_role(name, assume_role_policy_document=policy_document,\n path=path)\n log.info('Created IAM role %s.', name)\n return True\n except boto.exception.BotoServerError as e:\n log.error(e)\n log.error('Failed to create IAM role %s.', name)\n return False", "response": "Create an instance role."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete_role(name, region=None, key=None, keyid=None, profile=None):\n '''\n Delete an IAM role.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.delete_role myirole\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if not role_exists(name, region, key, keyid, profile):\n return True\n try:\n conn.delete_role(name)\n log.info('Deleted %s IAM role.', name)\n return True\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to delete %s IAM role.', name)\n return False", "response": "Delete an IAM role."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef profile_associated(role_name, profile_name, region, key, keyid, profile):\n '''\n Check to see if an instance profile is associated with an IAM role.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.profile_associated myirole myiprofile\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n # The IAM module of boto doesn't return objects. Instead you need to grab\n # values through its properties. Sigh.\n try:\n profiles = conn.list_instance_profiles_for_role(role_name)\n except boto.exception.BotoServerError as e:\n log.debug(e)\n return False\n profiles = profiles.list_instance_profiles_for_role_response\n profiles = profiles.list_instance_profiles_for_role_result\n profiles = profiles.instance_profiles\n for profile in profiles:\n if profile.instance_profile_name == profile_name:\n return True\n return False", "response": "Check to see if an instance profile is associated with an IAM role."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef associate_profile_to_role(profile_name, role_name, region=None, key=None,\n keyid=None, profile=None):\n '''\n Associate an instance profile with an IAM role.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.associate_profile_to_role myirole myiprofile\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if not role_exists(role_name, region, key, keyid, profile):\n log.error('IAM role %s does not exist.', role_name)\n return False\n if not instance_profile_exists(profile_name, region, key, keyid, profile):\n log.error('Instance profile %s does not exist.', profile_name)\n return False\n associated = profile_associated(role_name, profile_name, region, key, keyid, profile)\n if associated:\n return True\n else:\n try:\n conn.add_role_to_instance_profile(profile_name, role_name)\n log.info('Added %s instance profile to IAM role %s.', profile_name, role_name)\n return True\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to add %s instance profile to IAM role %s', profile_name, role_name)\n return False", "response": "Associate an instance profile with an IAM role."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef list_role_policies(role_name, region=None, key=None, keyid=None,\n profile=None):\n '''\n Get a list of policy names from a role.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.list_role_policies myirole\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n response = conn.list_role_policies(role_name)\n _list = response.list_role_policies_response.list_role_policies_result\n return _list.policy_names\n except boto.exception.BotoServerError as e:\n log.debug(e)\n return []", "response": "Get a list of policy names from a role."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets a role policy.", "response": "def get_role_policy(role_name, policy_name, region=None, key=None,\n keyid=None, profile=None):\n '''\n Get a role policy.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_role_policy myirole mypolicy\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n _policy = conn.get_role_policy(role_name, policy_name)\n # I _hate_ you for not giving me an object boto.\n _policy = _policy.get_role_policy_response.policy_document\n # Policy is url encoded\n _policy = _unquote(_policy)\n _policy = salt.utils.json.loads(_policy, object_pairs_hook=odict.OrderedDict)\n return _policy\n except boto.exception.BotoServerError:\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate or modify a role policy.", "response": "def create_role_policy(role_name, policy_name, policy, region=None, key=None,\n keyid=None, profile=None):\n '''\n Create or modify a role policy.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.create_role_policy myirole mypolicy '{\"MyPolicy\": \"Statement\": [{\"Action\": [\"sqs:*\"], \"Effect\": \"Allow\", \"Resource\": [\"arn:aws:sqs:*:*:*\"], \"Sid\": \"MyPolicySqs1\"}]}'\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n _policy = get_role_policy(role_name, policy_name, region, key, keyid, profile)\n mode = 'create'\n if _policy:\n if _policy == policy:\n return True\n mode = 'modify'\n if isinstance(policy, six.string_types):\n policy = salt.utils.json.loads(policy, object_pairs_hook=odict.OrderedDict)\n try:\n _policy = salt.utils.json.dumps(policy)\n conn.put_role_policy(role_name, policy_name, _policy)\n if mode == 'create':\n msg = 'Successfully added policy %s to IAM role %s.'\n else:\n msg = 'Successfully modified policy %s for IAM role %s.'\n log.info(msg, policy_name, role_name)\n return True\n except boto.exception.BotoServerError as e:\n log.error(e)\n log.error('Failed to %s policy %s for IAM role %s.',\n mode, policy_name, role_name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef delete_role_policy(role_name, policy_name, region=None, key=None,\n keyid=None, profile=None):\n '''\n Delete a role policy.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.delete_role_policy myirole mypolicy\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n _policy = get_role_policy(role_name, policy_name, region, key, keyid, profile)\n if not _policy:\n return True\n try:\n conn.delete_role_policy(role_name, policy_name)\n log.info('Successfully deleted policy %s for IAM role %s.',\n policy_name, role_name)\n return True\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to delete policy %s for IAM role %s.',\n policy_name, role_name)\n return False", "response": "Delete a role policy."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef update_assume_role_policy(role_name, policy_document, region=None,\n key=None, keyid=None, profile=None):\n '''\n Update an assume role policy for a role.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.update_assume_role_policy myrole '{\"Statement\":\"...\"}'\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if isinstance(policy_document, six.string_types):\n policy_document = salt.utils.json.loads(policy_document,\n object_pairs_hook=odict.OrderedDict)\n try:\n _policy_document = salt.utils.json.dumps(policy_document)\n conn.update_assume_role_policy(role_name, _policy_document)\n log.info('Successfully updated assume role policy for IAM role %s.', role_name)\n return True\n except boto.exception.BotoServerError as e:\n log.error(e)\n log.error('Failed to update assume role policy for IAM role %s.', role_name)\n return False", "response": "Update an assume role policy for a role."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nbuild a default assume role policy.", "response": "def build_policy(region=None, key=None, keyid=None, profile=None):\n '''\n Build a default assume role policy.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.build_policy\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if hasattr(conn, 'build_policy'):\n policy = salt.utils.json.loads(conn.build_policy())\n elif hasattr(conn, '_build_policy'):\n policy = salt.utils.json.loads(conn._build_policy())\n else:\n return {}\n # The format we get from build_policy isn't going to be what we get back\n # from AWS for the exact same policy. AWS converts single item list values\n # into strings, so let's do the same here.\n for key, policy_val in policy.items():\n for statement in policy_val:\n if (isinstance(statement['Action'], list)\n and len(statement['Action']) == 1):\n statement['Action'] = statement['Action'][0]\n if (isinstance(statement['Principal']['Service'], list)\n and len(statement['Principal']['Service']) == 1):\n statement['Principal']['Service'] = statement['Principal']['Service'][0]\n # build_policy doesn't add a version field, which AWS is going to set to a\n # default value, when we get it back, so let's set it.\n policy['Version'] = '2008-10-17'\n return policy"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a AWS account id associated with the used credentials.", "response": "def get_account_id(region=None, key=None, keyid=None, profile=None):\n '''\n Get a the AWS account id associated with the used credentials.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_account_id\n '''\n cache_key = 'boto_iam.account_id'\n if cache_key not in __context__:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n ret = conn.get_user()\n # The get_user call returns an user ARN:\n # arn:aws:iam::027050522557:user/salt-test\n arn = ret['get_user_response']['get_user_result']['user']['arn']\n account_id = arn.split(':')[4]\n except boto.exception.BotoServerError:\n # If call failed, then let's try to get the ARN from the metadata\n timeout = boto.config.getfloat(\n 'Boto', 'metadata_service_timeout', 1.0\n )\n attempts = boto.config.getint(\n 'Boto', 'metadata_service_num_attempts', 1\n )\n identity = boto.utils.get_instance_identity(\n timeout=timeout, num_retries=attempts\n )\n try:\n account_id = identity['document']['accountId']\n except KeyError:\n log.error('Failed to get account id from instance_identity in'\n ' boto_iam.get_account_id.')\n __context__[cache_key] = account_id\n return __context__[cache_key]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget and return all IAM role details starting at the optional path.", "response": "def get_all_roles(path_prefix=None, region=None, key=None, keyid=None,\n profile=None):\n '''\n Get and return all IAM role details, starting at the optional path.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n salt-call boto_iam.get_all_roles\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if not conn:\n return None\n _roles = conn.list_roles(path_prefix=path_prefix)\n roles = _roles.list_roles_response.list_roles_result.roles\n marker = getattr(\n _roles.list_roles_response.list_roles_result, 'marker', None\n )\n while marker:\n _roles = conn.list_roles(path_prefix=path_prefix, marker=marker)\n roles = roles + _roles.list_roles_response.list_roles_result.roles\n marker = getattr(\n _roles.list_roles_response.list_roles_result, 'marker', None\n )\n return roles"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_all_users(path_prefix='/', region=None, key=None, keyid=None,\n profile=None):\n '''\n Get and return all IAM user details, starting at the optional path.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n salt-call boto_iam.get_all_users\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if not conn:\n return None\n _users = conn.get_all_users(path_prefix=path_prefix)\n users = _users.list_users_response.list_users_result.users\n marker = getattr(\n _users.list_users_response.list_users_result, 'marker', None\n )\n while marker:\n _users = conn.get_all_users(path_prefix=path_prefix, marker=marker)\n users = users + _users.list_users_response.list_users_result.users\n marker = getattr(\n _users.list_users_response.list_users_result, 'marker', None\n )\n return users", "response": "Get and return all IAM user details starting at the optional path."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget all user policies.", "response": "def get_all_user_policies(user_name, marker=None, max_items=None, region=None, key=None, keyid=None, profile=None):\n '''\n Get all user policies.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_all_user_policies myuser\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n info = conn.get_all_user_policies(user_name, marker, max_items)\n if not info:\n return False\n _list = info.list_user_policies_response.list_user_policies_result\n return _list.policy_names\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to get policies for user %s.', user_name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_user_policy(user_name, policy_name, region=None, key=None, keyid=None, profile=None):\n '''\n Retrieves the specified policy document for the specified user.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_user_policy myuser mypolicyname\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n info = conn.get_user_policy(user_name, policy_name)\n log.debug('Info for IAM user %s policy %s: %s.', user_name, policy_name, info)\n if not info:\n return False\n info = info.get_user_policy_response.get_user_policy_result.policy_document\n info = _unquote(info)\n info = salt.utils.json.loads(info, object_pairs_hook=odict.OrderedDict)\n return info\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to get policy %s for IAM user %s.', policy_name, user_name)\n return False", "response": "Get the specified policy document for the specified user."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadds or updates a policy document for a user.", "response": "def put_user_policy(user_name, policy_name, policy_json, region=None, key=None, keyid=None, profile=None):\n '''\n Adds or updates the specified policy document for the specified user.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.put_user_policy myuser policyname policyrules\n '''\n user = get_user(user_name, region, key, keyid, profile)\n if not user:\n log.error('IAM user %s does not exist', user_name)\n return False\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n if not isinstance(policy_json, six.string_types):\n policy_json = salt.utils.json.dumps(policy_json)\n created = conn.put_user_policy(user_name, policy_name,\n policy_json)\n if created:\n log.info('Created policy %s for IAM user %s.', policy_name, user_name)\n return True\n log.error('Could not create policy %s for IAM user %s.', policy_name, user_name)\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to create policy %s for IAM user %s.', policy_name, user_name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndeleting a user policy.", "response": "def delete_user_policy(user_name, policy_name, region=None, key=None, keyid=None, profile=None):\n '''\n Delete a user policy.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.delete_user_policy myuser mypolicy\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if not conn:\n return False\n _policy = get_user_policy(\n user_name, policy_name, region, key, keyid, profile\n )\n if not _policy:\n return True\n try:\n conn.delete_user_policy(user_name, policy_name)\n log.info('Successfully deleted policy %s for IAM user %s.', policy_name, user_name)\n return True\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to delete policy %s for IAM user %s.', policy_name, user_name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nuploads a server certificate to Amazon.", "response": "def upload_server_cert(cert_name, cert_body, private_key, cert_chain=None, path=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Upload a certificate to Amazon.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.upload_server_cert mycert_name crt priv_key\n\n :param cert_name: The name for the server certificate. Do not include the path in this value.\n :param cert_body: The contents of the public key certificate in PEM-encoded format.\n :param private_key: The contents of the private key in PEM-encoded format.\n :param cert_chain: The contents of the certificate chain. This is typically a concatenation of the PEM-encoded public key certificates of the chain.\n :param path: The path for the server certificate.\n :param region: The name of the region to connect to.\n :param key: The key to be used in order to connect\n :param keyid: The keyid to be used in order to connect\n :param profile: The profile that contains a dict of region, key, keyid\n :return: True / False\n '''\n\n exists = get_server_certificate(cert_name, region, key, keyid, profile)\n if exists:\n return True\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n info = conn.upload_server_cert(cert_name, cert_body, private_key, cert_chain)\n log.info('Created certificate %s.', cert_name)\n return info\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to failed to create certificate %s.', cert_name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist the server certificates stored in IAM that have the specified path prefix.", "response": "def list_server_certificates(path_prefix='/', region=None, key=None, keyid=None, profile=None):\n '''\n Lists the server certificates stored in IAM that have the specified path prefix.\n\n .. versionadded:: ???\n\n :param path_prefix:\n The path prefix for filtering the results. For example: /company/servercerts would get\n all server certificates for which the path starts with /company/servercerts .\n This parameter is optional. If it is not included, it defaults to a slash (/), listing all\n server certificates. This parameter allows (per its regex pattern) a string of characters\n consisting of either a forward slash (/) by itself or a string that must begin and end with\n forward slashes. In addition, it can contain any ASCII character from the ! (u0021)\n through the DEL character (u007F), including most punctuation characters, digits, and upper\n and lowercased letters.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.list_server_certificates path_prefix=/somepath/\n '''\n retries = 10\n sleep = 6\n conn = __utils__['boto3.get_connection']('iam', region=region, key=key, keyid=keyid,\n profile=profile)\n Items = []\n while retries:\n try:\n log.debug('Garnering list of IAM Server Certificates')\n IsTruncated = True\n while IsTruncated:\n kwargs = {'PathPrefix': path_prefix}\n ret = conn.list_server_certificates(**kwargs)\n Items += ret.get('ServerCertificateMetadataList', [])\n IsTruncated = ret.get('IsTruncated')\n kwargs.update({'Marker': ret.get('Marker')})\n return Items\n except botocore.exceptions.ParamValidationError as err:\n raise SaltInvocationError(str(err))\n except botocore.exceptions.ClientError as err:\n if retries and jmespath.search('Error.Code', err.response) == 'Throttling':\n retries -= 1\n log.debug('Throttled by AWS API, retrying in %s seconds...', sleep)\n time.sleep(sleep)\n continue\n log.error('Failed to list IAM Server Certificates: %s', err.message)\n return None"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef delete_server_cert(cert_name, region=None, key=None, keyid=None, profile=None):\n '''\n Deletes a certificate from Amazon.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.delete_server_cert mycert_name\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n return conn.delete_server_cert(cert_name)\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to delete certificate %s.', cert_name)\n return False", "response": "Delete a server certificate from Amazon."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef export_users(path_prefix='/', region=None, key=None, keyid=None,\n profile=None):\n '''\n Get all IAM user details. Produces results that can be used to create an\n sls file.\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n salt-call boto_iam.export_users --out=txt | sed \"s/local: //\" > iam_users.sls\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if not conn:\n return None\n results = odict.OrderedDict()\n users = get_all_users(path_prefix, region, key, keyid, profile)\n for user in users:\n name = user.user_name\n _policies = conn.get_all_user_policies(name, max_items=100)\n _policies = _policies.list_user_policies_response.list_user_policies_result.policy_names\n policies = {}\n for policy_name in _policies:\n _policy = conn.get_user_policy(name, policy_name)\n _policy = salt.utils.json.loads(_unquote(\n _policy.get_user_policy_response.get_user_policy_result.policy_document\n ))\n policies[policy_name] = _policy\n user_sls = []\n user_sls.append({\"name\": name})\n user_sls.append({\"policies\": policies})\n user_sls.append({\"path\": user.path})\n results[\"manage user \" + name] = {\"boto_iam.user_present\": user_sls}\n return __utils__['yaml.safe_dump'](\n results,\n default_flow_style=False,\n indent=2)", "response": "Get all IAM user details. Produces results that can be used to create an anon user sls file."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets all IAM role details. Produces results that can be used to create an anonymized sls file.", "response": "def export_roles(path_prefix='/', region=None, key=None, keyid=None, profile=None):\n '''\n Get all IAM role details. Produces results that can be used to create an\n sls file.\n\n CLI Example:\n\n salt-call boto_iam.export_roles --out=txt | sed \"s/local: //\" > iam_roles.sls\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if not conn:\n return None\n results = odict.OrderedDict()\n roles = get_all_roles(path_prefix, region, key, keyid, profile)\n for role in roles:\n name = role.role_name\n _policies = conn.list_role_policies(name, max_items=100)\n _policies = _policies.list_role_policies_response.list_role_policies_result.policy_names\n policies = {}\n for policy_name in _policies:\n _policy = conn.get_role_policy(name, policy_name)\n _policy = salt.utils.json.loads(_unquote(\n _policy.get_role_policy_response.get_role_policy_result.policy_document\n ))\n policies[policy_name] = _policy\n role_sls = []\n role_sls.append({\"name\": name})\n role_sls.append({\"policies\": policies})\n role_sls.append({'policy_document': salt.utils.json.loads(_unquote(role.assume_role_policy_document))})\n role_sls.append({\"path\": role.path})\n results[\"manage role \" + name] = {\"boto_iam_role.present\": role_sls}\n return __utils__['yaml.safe_dump'](\n results,\n default_flow_style=False,\n indent=2)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef policy_exists(policy_name,\n region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if policy exists.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.instance_profile_exists myiprofile\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n conn.get_policy(_get_policy_arn(policy_name,\n region=region, key=key, keyid=keyid, profile=profile))\n return True\n except boto.exception.BotoServerError:\n return False", "response": "Check to see if a policy exists."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_policy(policy_name,\n region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if policy exists.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.instance_profile_exists myiprofile\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n ret = conn.get_policy(_get_policy_arn(policy_name,\n region=region, key=key, keyid=keyid, profile=profile))\n return ret.get('get_policy_response', {}).get('get_policy_result', {})\n except boto.exception.BotoServerError:\n return None", "response": "Get a single resource from a policy."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates a policy. CLI Example: .. code-block:: bash salt myminios boto_iam.create_policy mypolicy '{\"Version\": \"2012-10-17\", \"Statement\": [{ \"Effect\": \"Allow\", \"Action\": [\"s3:Get*\", \"s3:List*\"], \"Resource\": [\"arn:aws:s3:::my-bucket/shared/*\"]},]}'", "response": "def create_policy(policy_name, policy_document, path=None, description=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Create a policy.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminios boto_iam.create_policy mypolicy '{\"Version\": \"2012-10-17\", \"Statement\": [{ \"Effect\": \"Allow\", \"Action\": [\"s3:Get*\", \"s3:List*\"], \"Resource\": [\"arn:aws:s3:::my-bucket/shared/*\"]},]}'\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if not isinstance(policy_document, six.string_types):\n policy_document = salt.utils.json.dumps(policy_document)\n params = {}\n for arg in 'path', 'description':\n if locals()[arg] is not None:\n params[arg] = locals()[arg]\n if policy_exists(policy_name, region, key, keyid, profile):\n return True\n try:\n conn.create_policy(policy_name, policy_document, **params)\n log.info('Created IAM policy %s.', policy_name)\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to create IAM policy %s.', policy_name)\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nlists the available policy versions.", "response": "def list_policies(region=None, key=None, keyid=None, profile=None):\n '''\n List policies.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.list_policies\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n policies = []\n for ret in __utils__['boto.paged_call'](conn.list_policies):\n policies.append(ret.get('list_policies_response', {}).get('list_policies_result', {}).get('policies'))\n return policies\n except boto.exception.BotoServerError as e:\n log.debug(e)\n msg = 'Failed to list policy versions.'\n log.error(msg)\n return []"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef policy_version_exists(policy_name, version_id,\n region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if policy exists.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.instance_profile_exists myiprofile\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)\n try:\n conn.get_policy_version(policy_arn, version_id)\n return True\n except boto.exception.BotoServerError:\n return False", "response": "Check to see if a policy version exists."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_policy_version(policy_name, version_id,\n region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if policy exists.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.instance_profile_exists myiprofile\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n ret = conn.get_policy_version(_get_policy_arn(policy_name,\n region=region, key=key, keyid=keyid, profile=profile), version_id)\n retval = ret.get('get_policy_version_response', {}).get('get_policy_version_result', {}).get('policy_version', {})\n retval['document'] = _unquote(retval.get('document'))\n return {'policy_version': retval}\n except boto.exception.BotoServerError:\n return None", "response": "Get the version of a resource in a node."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a policy version.", "response": "def create_policy_version(policy_name, policy_document, set_as_default=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Create a policy version.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminios boto_iam.create_policy_version mypolicy '{\"Version\": \"2012-10-17\", \"Statement\": [{ \"Effect\": \"Allow\", \"Action\": [\"s3:Get*\", \"s3:List*\"], \"Resource\": [\"arn:aws:s3:::my-bucket/shared/*\"]},]}'\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if not isinstance(policy_document, six.string_types):\n policy_document = salt.utils.json.dumps(policy_document)\n params = {}\n for arg in ('set_as_default',):\n if locals()[arg] is not None:\n params[arg] = locals()[arg]\n policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)\n try:\n ret = conn.create_policy_version(policy_arn, policy_document, **params)\n vid = ret.get('create_policy_version_response', {}).get('create_policy_version_result', {}).get('policy_version', {}).get('version_id')\n log.info('Created IAM policy %s version %s.', policy_name, vid)\n return {'created': True, 'version_id': vid}\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to create IAM policy %s version %s.', policy_name, vid)\n return {'created': False, 'error': __utils__['boto.get_error'](e)}"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete_policy_version(policy_name, version_id,\n region=None, key=None, keyid=None, profile=None):\n '''\n Delete a policy version.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.delete_policy_version mypolicy v1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)\n if not policy_version_exists(policy_arn, version_id, region, key, keyid, profile):\n return True\n try:\n conn.delete_policy_version(policy_arn, version_id)\n log.info('Deleted IAM policy %s version %s.', policy_name, version_id)\n except boto.exception.BotoServerError as e:\n aws = __utils__['boto.get_error'](e)\n log.debug(aws)\n log.error('Failed to delete IAM policy %s version %s: %s',\n policy_name, version_id, aws.get('message'))\n return False\n return True", "response": "Delete a policy version."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlist versions of a policy.", "response": "def list_policy_versions(policy_name,\n region=None, key=None, keyid=None, profile=None):\n '''\n List versions of a policy.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.list_policy_versions mypolicy\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)\n try:\n ret = conn.list_policy_versions(policy_arn)\n return ret.get('list_policy_versions_response', {}).get('list_policy_versions_result', {}).get('versions')\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to list versions for IAM policy %s.', policy_name)\n return []"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndetach a managed policy from a user.", "response": "def detach_user_policy(policy_name, user_name,\n region=None, key=None, keyid=None, profile=None):\n '''\n Detach a managed policy to a user.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.detach_user_policy mypolicy myuser\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)\n try:\n conn.detach_user_policy(policy_arn, user_name)\n log.info('Detached %s policy from IAM user %s.', policy_name, user_name)\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to detach %s policy from IAM user %s.', policy_name, user_name)\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlist entities that a policy is attached to.", "response": "def list_entities_for_policy(policy_name, path_prefix=None, entity_filter=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n List entities that a policy is attached to.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.list_entities_for_policy mypolicy\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n retries = 30\n\n params = {}\n for arg in ('path_prefix', 'entity_filter'):\n if locals()[arg] is not None:\n params[arg] = locals()[arg]\n\n policy_arn = _get_policy_arn(policy_name, region, key, keyid, profile)\n while retries:\n try:\n allret = {\n 'policy_groups': [],\n 'policy_users': [],\n 'policy_roles': [],\n }\n for ret in __utils__['boto.paged_call'](conn.list_entities_for_policy, policy_arn=policy_arn, **params):\n for k, v in six.iteritems(allret):\n v.extend(ret.get('list_entities_for_policy_response', {}).get('list_entities_for_policy_result', {}).get(k))\n return allret\n except boto.exception.BotoServerError as e:\n if e.error_code == 'Throttling':\n log.debug(\"Throttled by AWS API, will retry in 5 seconds...\")\n time.sleep(5)\n retries -= 1\n continue\n log.error('Failed to list entities for IAM policy %s: %s', policy_name, e.message)\n return {}\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlist the entities attached to the given role.", "response": "def list_attached_role_policies(role_name, path_prefix=None, entity_filter=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n List entities attached to the given role.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.list_entities_for_policy mypolicy\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n params = {'RoleName': role_name}\n if path_prefix is not None:\n params['PathPrefix'] = path_prefix\n\n policies = []\n try:\n # Using conn.get_response is a bit of a hack, but it avoids having to\n # rewrite this whole module based on boto3\n for ret in __utils__['boto.paged_call'](conn.get_response, 'ListAttachedRolePolicies', params, list_marker='AttachedPolicies'):\n policies.extend(ret.get('list_attached_role_policies_response', {}).get('list_attached_role_policies_result', {}\n ).get('attached_policies', []))\n return policies\n except boto.exception.BotoServerError as e:\n log.debug(e)\n log.error('Failed to list attached policies for IAM role %s.', role_name)\n return []"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating SAML provider with metadata document.", "response": "def create_saml_provider(name, saml_metadata_document, region=None, key=None, keyid=None, profile=None):\n '''\n Create SAML provider\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.create_saml_provider my_saml_provider_name saml_metadata_document\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n conn.create_saml_provider(saml_metadata_document, name)\n log.info('Successfully created %s SAML provider.', name)\n return True\n except boto.exception.BotoServerError as e:\n aws = __utils__['boto.get_error'](e)\n log.debug(aws)\n log.error('Failed to create SAML provider %s.', name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the ARN of a SAML provider.", "response": "def get_saml_provider_arn(name, region=None, key=None, keyid=None, profile=None):\n '''\n Get SAML provider\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_saml_provider_arn my_saml_provider_name\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n response = conn.list_saml_providers()\n for saml_provider in response.list_saml_providers_response.list_saml_providers_result.saml_provider_list:\n if saml_provider['arn'].endswith(':saml-provider/' + name):\n return saml_provider['arn']\n return False\n except boto.exception.BotoServerError as e:\n aws = __utils__['boto.get_error'](e)\n log.debug(aws)\n log.error('Failed to get ARN of SAML provider %s.', name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef delete_saml_provider(name, region=None, key=None, keyid=None, profile=None):\n '''\n Delete SAML provider\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.delete_saml_provider my_saml_provider_name\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n saml_provider_arn = get_saml_provider_arn(name, region=region, key=key, keyid=keyid, profile=profile)\n if not saml_provider_arn:\n log.info('SAML provider %s not found.', name)\n return True\n conn.delete_saml_provider(saml_provider_arn)\n log.info('Successfully deleted SAML provider %s.', name)\n return True\n except boto.exception.BotoServerError as e:\n aws = __utils__['boto.get_error'](e)\n log.debug(aws)\n log.error('Failed to delete SAML provider %s.', name)\n return False", "response": "Delete a SAML provider."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists SAML providers. CLI Example: .. code-block:: bash salt myminion boto_iam.list_saml_providers", "response": "def list_saml_providers(region=None, key=None, keyid=None, profile=None):\n '''\n List SAML providers.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.list_saml_providers\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n providers = []\n info = conn.list_saml_providers()\n for arn in info['list_saml_providers_response']['list_saml_providers_result']['saml_provider_list']:\n providers.append(arn['arn'].rsplit('/', 1)[1])\n return providers\n except boto.exception.BotoServerError as e:\n log.debug(__utils__['boto.get_error'](e))\n log.error('Failed to get list of SAML providers.')\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting SAML provider document.", "response": "def get_saml_provider(name, region=None, key=None, keyid=None, profile=None):\n '''\n Get SAML provider document.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_iam.get_saml_provider arn\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n try:\n provider = conn.get_saml_provider(name)\n return provider['get_saml_provider_response']['get_saml_provider_result']['saml_metadata_document']\n except boto.exception.BotoServerError as e:\n log.debug(__utils__['boto.get_error'](e))\n log.error('Failed to get SAML provider document %s.', name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef validate(config):\n '''\n Validate the beacon configuration\n '''\n # Configuration for adb beacon should be a dictionary with states array\n if not isinstance(config, list):\n log.info('Configuration for adb beacon must be a list.')\n return False, ('Configuration for adb beacon must be a list.')\n\n _config = {}\n list(map(_config.update, config))\n\n if 'states' not in _config:\n log.info('Configuration for adb beacon must include a states array.')\n return False, ('Configuration for adb beacon must include a states array.')\n else:\n if not isinstance(_config['states'], list):\n log.info('Configuration for adb beacon must include a states array.')\n return False, ('Configuration for adb beacon must include a states array.')\n else:\n states = ['offline', 'bootloader', 'device', 'host',\n 'recovery', 'no permissions',\n 'sideload', 'unauthorized', 'unknown', 'missing']\n if any(s not in states for s in _config['states']):\n log.info('Need a one of the following adb '\n 'states: %s', ', '.join(states))\n return False, ('Need a one of the following adb '\n 'states: {0}'.format(', '.join(states)))\n return True, 'Valid beacon configuration'", "response": "Validate the beacon configuration."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef beacon(config):\n '''\n Emit the status of all devices returned by adb\n\n Specify the device states that should emit an event,\n there will be an event for each device with the\n event type and device specified.\n\n .. code-block:: yaml\n\n beacons:\n adb:\n - states:\n - offline\n - unauthorized\n - missing\n - no_devices_event: True\n - battery_low: 25\n\n '''\n\n log.trace('adb beacon starting')\n ret = []\n\n _config = {}\n list(map(_config.update, config))\n\n out = __salt__['cmd.run']('adb devices', runas=_config.get('user', None))\n\n lines = out.split('\\n')[1:]\n last_state_devices = list(last_state.keys())\n found_devices = []\n\n for line in lines:\n try:\n device, state = line.split('\\t')\n found_devices.append(device)\n if device not in last_state_devices or \\\n ('state' in last_state[device] and last_state[device]['state'] != state):\n if state in _config['states']:\n ret.append({'device': device, 'state': state, 'tag': state})\n last_state[device] = {'state': state}\n\n if 'battery_low' in _config:\n val = last_state.get(device, {})\n cmd = 'adb -s {0} shell cat /sys/class/power_supply/*/capacity'.format(device)\n battery_levels = __salt__['cmd.run'](cmd, runas=_config.get('user', None)).split('\\n')\n\n for l in battery_levels:\n battery_level = int(l)\n if 0 < battery_level < 100:\n if 'battery' not in val or battery_level != val['battery']:\n if ('battery' not in val or val['battery'] > _config['battery_low']) and \\\n battery_level <= _config['battery_low']:\n ret.append({'device': device, 'battery_level': battery_level, 'tag': 'battery_low'})\n\n if device not in last_state:\n last_state[device] = {}\n\n last_state[device].update({'battery': battery_level})\n\n except ValueError:\n continue\n\n # Find missing devices and remove them / send an event\n for device in last_state_devices:\n if device not in found_devices:\n if 'missing' in _config['states']:\n ret.append({'device': device, 'state': 'missing', 'tag': 'missing'})\n\n del last_state[device]\n\n # Maybe send an event if we don't have any devices\n if 'no_devices_event' in _config and _config['no_devices_event'] is True:\n if not found_devices and not last_state_extra['no_devices']:\n ret.append({'tag': 'no_devices'})\n\n # Did we have no devices listed this time around?\n\n last_state_extra['no_devices'] = not found_devices\n\n return ret", "response": "Emit the status of all devices returned by adb"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting a value to the named set ArcGIS resource.", "response": "def set_(name, add, match):\n '''\n Add a value to the named set\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n reg.set:\n - add: bar\n - match: my/custom/event\n '''\n ret = {'name': name,\n 'changes': {},\n 'comment': '',\n 'result': True}\n if name not in __reg__:\n __reg__[name] = {}\n __reg__[name]['val'] = set()\n for event in __events__:\n if salt.utils.stringutils.expr_match(event['tag'], match):\n try:\n val = event['data']['data'].get(add)\n except KeyError:\n val = event['data'].get(add)\n if val is None:\n val = 'None'\n ret['changes'][add] = val\n __reg__[name]['val'].add(val)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a list of the specified values from the named resource.", "response": "def list_(name, add, match, stamp=False, prune=0):\n '''\n Add the specified values to the named list\n\n If ``stamp`` is True, then the timestamp from the event will also be added\n if ``prune`` is set to an integer higher than ``0``, then only the last\n ``prune`` values will be kept in the list.\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n reg.list:\n - add: bar\n - match: my/custom/event\n - stamp: True\n '''\n ret = {'name': name,\n 'changes': {},\n 'comment': '',\n 'result': True}\n if not isinstance(add, list):\n add = add.split(',')\n if name not in __reg__:\n __reg__[name] = {}\n __reg__[name]['val'] = []\n for event in __events__:\n try:\n event_data = event['data']['data']\n except KeyError:\n event_data = event['data']\n if salt.utils.stringutils.expr_match(event['tag'], match):\n item = {}\n for key in add:\n if key in event_data:\n item[key] = event_data[key]\n if stamp is True:\n item['time'] = event['data']['_stamp']\n __reg__[name]['val'].append(item)\n if prune > 0:\n __reg__[name]['val'] = __reg__[name]['val'][:prune]\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef mean(name, add, match):\n '''\n Accept a numeric value from the matched events and store a running average\n of the values in the given register. If the specified value is not numeric\n it will be skipped\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n reg.mean:\n - add: data_field\n - match: my/custom/event\n '''\n ret = {'name': name,\n 'changes': {},\n 'comment': '',\n 'result': True}\n if name not in __reg__:\n __reg__[name] = {}\n __reg__[name]['val'] = 0\n __reg__[name]['total'] = 0\n __reg__[name]['count'] = 0\n for event in __events__:\n try:\n event_data = event['data']['data']\n except KeyError:\n event_data = event['data']\n if salt.utils.stringutils.expr_match(event['tag'], match):\n if add in event_data:\n try:\n comp = int(event_data)\n except ValueError:\n continue\n __reg__[name]['total'] += comp\n __reg__[name]['count'] += 1\n __reg__[name]['val'] = __reg__[name]['total'] / __reg__[name]['count']\n return ret", "response": "Return a dict that associates the value of a numeric value with the value of the specified event."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nclearing the namespace from the register", "response": "def clear(name):\n '''\n Clear the namespace from the register\n\n USAGE:\n\n .. code-block:: yaml\n\n clearns:\n reg.clear:\n - name: myregister\n '''\n ret = {'name': name,\n 'changes': {},\n 'comment': '',\n 'result': True}\n if name in __reg__:\n __reg__[name].clear()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef delete(name):\n '''\n Delete the namespace from the register\n\n USAGE:\n\n .. code-block:: yaml\n\n deletens:\n reg.delete:\n - name: myregister\n '''\n ret = {'name': name,\n 'changes': {},\n 'comment': '',\n 'result': True}\n if name in __reg__:\n del __reg__[name]\n return ret", "response": "Delete the namespace from the register"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsetting a key in etcd", "response": "def set_(name, value, profile=None, **kwargs):\n '''\n Set a key in etcd\n\n name\n The etcd key name, for example: ``/foo/bar/baz``.\n value\n The value the key should contain.\n\n profile\n Optional, defaults to ``None``. Sets the etcd profile to use which has\n been defined in the Salt Master config.\n\n .. code-block:: yaml\n\n my_etd_config:\n etcd.host: 127.0.0.1\n etcd.port: 4001\n\n '''\n\n created = False\n\n rtn = {\n 'name': name,\n 'comment': 'Key contains correct value',\n 'result': True,\n 'changes': {}\n }\n\n current = __salt__['etcd.get'](name, profile=profile, **kwargs)\n if not current:\n created = True\n\n result = __salt__['etcd.set'](name, value, profile=profile, **kwargs)\n\n if result and result != current:\n if created:\n rtn['comment'] = 'New key created'\n else:\n rtn['comment'] = 'Key value updated'\n rtn['changes'] = {\n name: value\n }\n\n return rtn"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef directory(name, profile=None, **kwargs):\n '''\n Create a directory in etcd.\n\n name\n The etcd directory name, for example: ``/foo/bar/baz``.\n profile\n Optional, defaults to ``None``. Sets the etcd profile to use which has\n been defined in the Salt Master config.\n\n .. code-block:: yaml\n\n my_etd_config:\n etcd.host: 127.0.0.1\n etcd.port: 4001\n '''\n\n created = False\n\n rtn = {\n 'name': name,\n 'comment': 'Directory exists',\n 'result': True,\n 'changes': {}\n }\n\n current = __salt__['etcd.get'](name, profile=profile, recurse=True, **kwargs)\n if not current:\n created = True\n\n result = __salt__['etcd.set'](name, None, directory=True, profile=profile, **kwargs)\n\n if result and result != current:\n if created:\n rtn['comment'] = 'New directory created'\n rtn['changes'] = {\n name: 'Created'\n }\n\n return rtn", "response": "Create a directory in etcd."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef rm(name, recurse=False, profile=None, **kwargs):\n '''\n Deletes a key from etcd\n\n name\n The etcd key name to remove, for example ``/foo/bar/baz``.\n\n recurse\n Optional, defaults to ``False``. If ``True`` performs a recursive delete.\n\n profile\n Optional, defaults to ``None``. Sets the etcd profile to use which has\n been defined in the Salt Master config.\n\n .. code-block:: yaml\n\n my_etd_config:\n etcd.host: 127.0.0.1\n etcd.port: 4001\n '''\n\n rtn = {\n 'name': name,\n 'result': True,\n 'changes': {}\n }\n\n if not __salt__['etcd.get'](name, profile=profile, **kwargs):\n rtn['comment'] = 'Key does not exist'\n return rtn\n\n if __salt__['etcd.rm'](name, recurse=recurse, profile=profile, **kwargs):\n rtn['comment'] = 'Key removed'\n rtn['changes'] = {\n name: 'Deleted'\n }\n else:\n rtn['comment'] = 'Unable to remove key'\n\n return rtn", "response": "Deletes a key from etcd\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of all running services that are owned by the currently running services.", "response": "def running_service_owners(\n exclude=('/dev', '/home', '/media', '/proc', '/run', '/sys/', '/tmp',\n '/var')\n ):\n '''\n Determine which packages own the currently running services. By default,\n excludes files whose full path starts with ``/dev``, ``/home``, ``/media``,\n ``/proc``, ``/run``, ``/sys``, ``/tmp`` and ``/var``. This can be\n overridden by passing in a new list to ``exclude``.\n\n CLI Example:\n\n salt myminion introspect.running_service_owners\n '''\n error = {}\n if 'pkg.owner' not in __salt__:\n error['Unsupported Package Manager'] = (\n 'The module for the package manager on this system does not '\n 'support looking up which package(s) owns which file(s)'\n )\n\n if 'file.open_files' not in __salt__:\n error['Unsupported File Module'] = (\n 'The file module on this system does not '\n 'support looking up open files on the system'\n )\n\n if error:\n return {'Error': error}\n\n ret = {}\n open_files = __salt__['file.open_files']()\n\n execs = __salt__['service.execs']()\n for path in open_files:\n ignore = False\n for bad_dir in exclude:\n if path.startswith(bad_dir):\n ignore = True\n\n if ignore:\n continue\n\n if not os.access(path, os.X_OK):\n continue\n\n for service in execs:\n if path == execs[service]:\n pkg = __salt__['pkg.owner'](path)\n ret[service] = next(six.itervalues(pkg))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef enabled_service_owners():\n '''\n Return which packages own each of the services that are currently enabled.\n\n CLI Example:\n\n salt myminion introspect.enabled_service_owners\n '''\n error = {}\n if 'pkg.owner' not in __salt__:\n error['Unsupported Package Manager'] = (\n 'The module for the package manager on this system does not '\n 'support looking up which package(s) owns which file(s)'\n )\n\n if 'service.show' not in __salt__:\n error['Unsupported Service Manager'] = (\n 'The module for the service manager on this system does not '\n 'support showing descriptive service data'\n )\n\n if error:\n return {'Error': error}\n\n ret = {}\n services = __salt__['service.get_enabled']()\n\n for service in services:\n data = __salt__['service.show'](service)\n if 'ExecStart' not in data:\n continue\n start_cmd = data['ExecStart']['path']\n pkg = __salt__['pkg.owner'](start_cmd)\n ret[service] = next(six.itervalues(pkg))\n\n return ret", "response": "Return which packages own each of the services that are currently enabled."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef service_highstate(requires=True):\n '''\n Return running and enabled services in a highstate structure. By default\n also returns package dependencies for those services, which means that\n package definitions must be created outside this function. To drop the\n package dependencies, set ``requires`` to False.\n\n CLI Example:\n\n salt myminion introspect.service_highstate\n salt myminion introspect.service_highstate requires=False\n '''\n ret = {}\n running = running_service_owners()\n for service in running:\n ret[service] = {'service': ['running']}\n\n if requires:\n ret[service]['service'].append(\n {'require': {'pkg': running[service]}}\n )\n\n enabled = enabled_service_owners()\n for service in enabled:\n if service in ret:\n ret[service]['service'].append({'enabled': True})\n else:\n ret[service] = {'service': [{'enabled': True}]}\n\n if requires:\n exists = False\n for item in ret[service]['service']:\n if isinstance(item, dict) and next(six.iterkeys(item)) == 'require':\n exists = True\n if not exists:\n ret[service]['service'].append(\n {'require': {'pkg': enabled[service]}}\n )\n\n return ret", "response": "Return running and enabled services in a highstate structure."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the targets of the targeted node set", "response": "def targets(tgt, tgt_type='glob', **kwargs):\n '''\n Return the targets\n '''\n ret = {}\n ports = __opts__['ssh_scan_ports']\n if not isinstance(ports, list):\n # Comma-separate list of integers\n ports = list(map(int, six.text_type(ports).split(',')))\n\n hosts = list(NodeSet(tgt))\n host_addrs = dict([(h, socket.gethostbyname(h)) for h in hosts])\n\n for host, addr in host_addrs.items():\n addr = six.text_type(addr)\n ret[host] = copy.deepcopy(__opts__.get('roster_defaults', {}))\n for port in ports:\n try:\n sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n sock.settimeout(float(__opts__['ssh_scan_timeout']))\n sock.connect((addr, port))\n sock.shutdown(socket.SHUT_RDWR)\n sock.close()\n ret[host].update({'host': addr, 'port': port})\n except socket.error:\n pass\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _gather_buffer_space():\n '''\n Gather some system data and then calculate\n buffer space.\n\n Result is in bytes.\n '''\n if HAS_PSUTIL and psutil.version_info >= (0, 6, 0):\n # Oh good, we have psutil. This will be quick.\n total_mem = psutil.virtual_memory().total\n else:\n # Avoid loading core grains unless absolutely required\n import platform\n import salt.grains.core\n # We need to load up ``mem_total`` grain. Let's mimic required OS data.\n os_data = {'kernel': platform.system()}\n grains = salt.grains.core._memdata(os_data)\n total_mem = grains['mem_total'] * 1024 * 1024\n # Return the higher number between 5% of the system memory and 10MiB\n return max([total_mem * 0.05, 10 << 20])", "response": "Gather some system data and then calculate\n buffer space."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _normalize_roots(file_roots):\n '''\n Normalize file or pillar roots.\n '''\n for saltenv, dirs in six.iteritems(file_roots):\n normalized_saltenv = six.text_type(saltenv)\n if normalized_saltenv != saltenv:\n file_roots[normalized_saltenv] = file_roots.pop(saltenv)\n if not isinstance(dirs, (list, tuple)):\n file_roots[normalized_saltenv] = []\n file_roots[normalized_saltenv] = \\\n _expand_glob_path(file_roots[normalized_saltenv])\n return file_roots", "response": "Normalize file or pillar roots."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _validate_pillar_roots(pillar_roots):\n '''\n If the pillar_roots option has a key that is None then we will error out,\n just replace it with an empty list\n '''\n if not isinstance(pillar_roots, dict):\n log.warning('The pillar_roots parameter is not properly formatted,'\n ' using defaults')\n return {'base': _expand_glob_path([salt.syspaths.BASE_PILLAR_ROOTS_DIR])}\n return _normalize_roots(pillar_roots)", "response": "Validate the pillar_roots option and return a dict of the correct values"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nvalidates the file_roots option", "response": "def _validate_file_roots(file_roots):\n '''\n If the file_roots option has a key that is None then we will error out,\n just replace it with an empty list\n '''\n if not isinstance(file_roots, dict):\n log.warning('The file_roots parameter is not properly formatted,'\n ' using defaults')\n return {'base': _expand_glob_path([salt.syspaths.BASE_FILE_ROOTS_DIR])}\n return _normalize_roots(file_roots)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _expand_glob_path(file_roots):\n '''\n Applies shell globbing to a set of directories and returns\n the expanded paths\n '''\n unglobbed_path = []\n for path in file_roots:\n try:\n if glob.has_magic(path):\n unglobbed_path.extend(glob.glob(path))\n else:\n unglobbed_path.append(path)\n except Exception:\n unglobbed_path.append(path)\n return unglobbed_path", "response": "Applies shell globbing to a set of directories and returns\n the expanded paths\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _validate_opts(opts):\n '''\n Check that all of the types of values passed into the config are\n of the right types\n '''\n def format_multi_opt(valid_type):\n try:\n num_types = len(valid_type)\n except TypeError:\n # Bare type name won't have a length, return the name of the type\n # passed.\n return valid_type.__name__\n else:\n def get_types(types, type_tuple):\n for item in type_tuple:\n if isinstance(item, tuple):\n get_types(types, item)\n else:\n try:\n types.append(item.__name__)\n except AttributeError:\n log.warning(\n 'Unable to interpret type %s while validating '\n 'configuration', item\n )\n types = []\n get_types(types, valid_type)\n\n ret = ', '.join(types[:-1])\n ret += ' or ' + types[-1]\n return ret\n\n errors = []\n\n err = (\n 'Config option \\'{0}\\' with value {1} has an invalid type of {2}, a '\n '{3} is required for this option'\n )\n for key, val in six.iteritems(opts):\n if key in VALID_OPTS:\n if val is None:\n if VALID_OPTS[key] is None:\n continue\n else:\n try:\n if None in VALID_OPTS[key]:\n continue\n except TypeError:\n # VALID_OPTS[key] is not iterable and not None\n pass\n\n if isinstance(val, VALID_OPTS[key]):\n continue\n\n # We don't know what data type sdb will return at run-time so we\n # simply cannot check it for correctness here at start-time.\n if isinstance(val, six.string_types) and val.startswith('sdb://'):\n continue\n\n if hasattr(VALID_OPTS[key], '__call__'):\n try:\n VALID_OPTS[key](val)\n if isinstance(val, (list, dict)):\n # We'll only get here if VALID_OPTS[key] is str or\n # bool, and the passed value is a list/dict. Attempting\n # to run int() or float() on a list/dict will raise an\n # exception, but running str() or bool() on it will\n # pass despite not being the correct type.\n errors.append(\n err.format(\n key,\n val,\n type(val).__name__,\n VALID_OPTS[key].__name__\n )\n )\n except (TypeError, ValueError):\n errors.append(\n err.format(key,\n val,\n type(val).__name__,\n VALID_OPTS[key].__name__)\n )\n continue\n\n errors.append(\n err.format(key,\n val,\n type(val).__name__,\n format_multi_opt(VALID_OPTS[key]))\n )\n\n # Convert list to comma-delimited string for 'return' config option\n if isinstance(opts.get('return'), list):\n opts['return'] = ','.join(opts['return'])\n\n for error in errors:\n log.warning(error)\n if errors:\n return False\n return True", "response": "Validate the options passed into the config file."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure we're not using any invalid ssh_minion_opts. We want to make sure that the ssh_minion_opts does not override any pillar or fileserver options inherited from the master config. To add other items, modify the if statement in the for loop below.", "response": "def _validate_ssh_minion_opts(opts):\n '''\n Ensure we're not using any invalid ssh_minion_opts. We want to make sure\n that the ssh_minion_opts does not override any pillar or fileserver options\n inherited from the master config. To add other items, modify the if\n statement in the for loop below.\n '''\n ssh_minion_opts = opts.get('ssh_minion_opts', {})\n if not isinstance(ssh_minion_opts, dict):\n log.error('Invalidly-formatted ssh_minion_opts')\n opts.pop('ssh_minion_opts')\n\n for opt_name in list(ssh_minion_opts):\n if re.match('^[a-z0-9]+fs_', opt_name, flags=re.IGNORECASE) \\\n or ('pillar' in opt_name and not 'ssh_merge_pillar' == opt_name) \\\n or opt_name in ('fileserver_backend',):\n log.warning(\n '\\'%s\\' is not a valid ssh_minion_opts parameter, ignoring',\n opt_name\n )\n ssh_minion_opts.pop(opt_name)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _append_domain(opts):\n '''\n Append a domain to the existing id if it doesn't already exist\n '''\n # Domain already exists\n if opts['id'].endswith(opts['append_domain']):\n return opts['id']\n # Trailing dot should mean an FQDN that is terminated, leave it alone.\n if opts['id'].endswith('.'):\n return opts['id']\n return '{0[id]}.{0[append_domain]}'.format(opts)", "response": "Append a domain to the existing id if it doesn t already exist"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _read_conf_file(path):\n '''\n Read in a config file from a given path and process it into a dictionary\n '''\n log.debug('Reading configuration from %s', path)\n with salt.utils.files.fopen(path, 'r') as conf_file:\n try:\n conf_opts = salt.utils.yaml.safe_load(conf_file) or {}\n except salt.utils.yaml.YAMLError as err:\n message = 'Error parsing configuration file: {0} - {1}'.format(path, err)\n log.error(message)\n raise salt.exceptions.SaltConfigurationError(message)\n\n # only interpret documents as a valid conf, not things like strings,\n # which might have been caused by invalid yaml syntax\n if not isinstance(conf_opts, dict):\n message = 'Error parsing configuration file: {0} - conf ' \\\n 'should be a document, not {1}.'.format(path, type(conf_opts))\n log.error(message)\n raise salt.exceptions.SaltConfigurationError(message)\n\n # allow using numeric ids: convert int to string\n if 'id' in conf_opts:\n if not isinstance(conf_opts['id'], six.string_types):\n conf_opts['id'] = six.text_type(conf_opts['id'])\n else:\n conf_opts['id'] = salt.utils.data.decode(conf_opts['id'])\n return conf_opts", "response": "Read in a config file from a given path and process it into a dictionary\n ArcGIS"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _absolute_path(path, relative_to=None):\n '''\n Return an absolute path. In case ``relative_to`` is passed and ``path`` is\n not an absolute path, we try to prepend ``relative_to`` to ``path``and if\n that path exists, return that one\n '''\n\n if path and os.path.isabs(path):\n return path\n if path and relative_to is not None:\n _abspath = os.path.join(relative_to, path)\n if os.path.isfile(_abspath):\n log.debug(\n 'Relative path \\'%s\\' converted to existing absolute path '\n '\\'%s\\'', path, _abspath\n )\n return _abspath\n return path", "response": "Return an absolute path."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef load_config(path, env_var, default_path=None, exit_on_config_errors=True):\n '''\n Returns configuration dict from parsing either the file described by\n ``path`` or the environment variable described by ``env_var`` as YAML.\n '''\n if path is None:\n # When the passed path is None, we just want the configuration\n # defaults, not actually loading the whole configuration.\n return {}\n\n if default_path is None:\n # This is most likely not being used from salt, i.e., could be salt-cloud\n # or salt-api which have not yet migrated to the new default_path\n # argument. Let's issue a warning message that the environ vars won't\n # work.\n import inspect\n previous_frame = inspect.getframeinfo(inspect.currentframe().f_back)\n log.warning(\n \"The function '%s()' defined in '%s' is not yet using the \"\n \"new 'default_path' argument to `salt.config.load_config()`. \"\n \"As such, the '%s' environment variable will be ignored\",\n previous_frame.function, previous_frame.filename, env_var\n )\n # In this case, maintain old behavior\n default_path = DEFAULT_MASTER_OPTS['conf_file']\n\n # Default to the environment variable path, if it exists\n env_path = os.environ.get(env_var, path)\n if not env_path or not os.path.isfile(env_path):\n env_path = path\n # If non-default path from `-c`, use that over the env variable\n if path != default_path:\n env_path = path\n\n path = env_path\n\n # If the configuration file is missing, attempt to copy the template,\n # after removing the first header line.\n if not os.path.isfile(path):\n template = '{0}.template'.format(path)\n if os.path.isfile(template):\n log.debug('Writing %s based on %s', path, template)\n with salt.utils.files.fopen(path, 'w') as out:\n with salt.utils.files.fopen(template, 'r') as ifile:\n ifile.readline() # skip first line\n out.write(ifile.read())\n\n opts = {}\n\n if salt.utils.validate.path.is_readable(path):\n try:\n opts = _read_conf_file(path)\n opts['conf_file'] = path\n except salt.exceptions.SaltConfigurationError as error:\n log.error(error)\n if exit_on_config_errors:\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)\n else:\n log.debug('Missing configuration file: %s', path)\n\n return opts", "response": "Load the configuration file from the passed path or the environment variable defined by env_var as YAML."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nparsing extra configuration files specified in a list of include paths and returns a dictionary containing the configuration.", "response": "def include_config(include, orig_path, verbose, exit_on_config_errors=False):\n '''\n Parses extra configuration file(s) specified in an include list in the\n main config file.\n '''\n # Protect against empty option\n if not include:\n return {}\n\n if orig_path is None:\n # When the passed path is None, we just want the configuration\n # defaults, not actually loading the whole configuration.\n return {}\n\n if isinstance(include, six.string_types):\n include = [include]\n\n configuration = {}\n for path in include:\n # Allow for includes like ~/foo\n path = os.path.expanduser(path)\n if not os.path.isabs(path):\n path = os.path.join(os.path.dirname(orig_path), path)\n\n # Catch situation where user typos path in configuration; also warns\n # for empty include directory (which might be by design)\n glob_matches = glob.glob(path)\n if not glob_matches:\n if verbose:\n log.warning(\n 'Warning parsing configuration file: \"include\" path/glob '\n \"'%s' matches no files\", path\n )\n\n for fn_ in sorted(glob_matches):\n log.debug('Including configuration from \\'%s\\'', fn_)\n try:\n opts = _read_conf_file(fn_)\n except salt.exceptions.SaltConfigurationError as error:\n log.error(error)\n if exit_on_config_errors:\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)\n else:\n # Initialize default config if we wish to skip config errors\n opts = {}\n schedule = opts.get('schedule', {})\n if schedule and 'schedule' in configuration:\n configuration['schedule'].update(schedule)\n include = opts.get('include', [])\n if include:\n opts.update(include_config(include, fn_, verbose))\n\n salt.utils.dictupdate.update(configuration, opts, True, True)\n\n return configuration"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ninserts path into sys. path taking into consideration root_dir option.", "response": "def insert_system_path(opts, paths):\n '''\n Inserts path into python path taking into consideration 'root_dir' option.\n '''\n if isinstance(paths, six.string_types):\n paths = [paths]\n for path in paths:\n path_options = {'path': path, 'root_dir': opts['root_dir']}\n prepend_root_dir(path_options, path_options)\n if (os.path.isdir(path_options['path'])\n and path_options['path'] not in sys.path):\n sys.path.insert(0, path_options['path'])"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef minion_config(path,\n env_var='SALT_MINION_CONFIG',\n defaults=None,\n cache_minion_id=False,\n ignore_config_errors=True,\n minion_id=None,\n role='minion'):\n '''\n Reads in the minion configuration file and sets up special options\n\n This is useful for Minion-side operations, such as the\n :py:class:`~salt.client.Caller` class, and manually running the loader\n interface.\n\n .. code-block:: python\n\n import salt.config\n minion_opts = salt.config.minion_config('/etc/salt/minion')\n '''\n if defaults is None:\n defaults = DEFAULT_MINION_OPTS.copy()\n\n if not os.environ.get(env_var, None):\n # No valid setting was given using the configuration variable.\n # Lets see is SALT_CONFIG_DIR is of any use\n salt_config_dir = os.environ.get('SALT_CONFIG_DIR', None)\n if salt_config_dir:\n env_config_file_path = os.path.join(salt_config_dir, 'minion')\n if salt_config_dir and os.path.isfile(env_config_file_path):\n # We can get a configuration file using SALT_CONFIG_DIR, let's\n # update the environment with this information\n os.environ[env_var] = env_config_file_path\n\n overrides = load_config(path, env_var, DEFAULT_MINION_OPTS['conf_file'])\n default_include = overrides.get('default_include',\n defaults['default_include'])\n include = overrides.get('include', [])\n\n overrides.update(include_config(default_include, path, verbose=False,\n exit_on_config_errors=not ignore_config_errors))\n overrides.update(include_config(include, path, verbose=True,\n exit_on_config_errors=not ignore_config_errors))\n\n opts = apply_minion_config(overrides, defaults,\n cache_minion_id=cache_minion_id,\n minion_id=minion_id)\n opts['__role'] = role\n apply_sdb(opts)\n _validate_opts(opts)\n return opts", "response": "Load the minion configuration file and set up special options"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef apply_sdb(opts, sdb_opts=None):\n '''\n Recurse for sdb:// links for opts\n '''\n # Late load of SDB to keep CLI light\n import salt.utils.sdb\n if sdb_opts is None:\n sdb_opts = opts\n if isinstance(sdb_opts, six.string_types) and sdb_opts.startswith('sdb://'):\n return salt.utils.sdb.sdb_get(sdb_opts, opts)\n elif isinstance(sdb_opts, dict):\n for key, value in six.iteritems(sdb_opts):\n if value is None:\n continue\n sdb_opts[key] = apply_sdb(opts, value)\n elif isinstance(sdb_opts, list):\n for key, value in enumerate(sdb_opts):\n if value is None:\n continue\n sdb_opts[key] = apply_sdb(opts, value)\n\n return sdb_opts", "response": "Recursively apply SDB links for opts\n "} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreads in the Salt Cloud config file and return the dict containing the cloud config values.", "response": "def cloud_config(path=None, env_var='SALT_CLOUD_CONFIG', defaults=None,\n master_config_path=None, master_config=None,\n providers_config_path=None, providers_config=None,\n profiles_config_path=None, profiles_config=None):\n '''\n Read in the Salt Cloud config and return the dict\n '''\n if path:\n config_dir = os.path.dirname(path)\n else:\n config_dir = salt.syspaths.CONFIG_DIR\n\n # Load the cloud configuration\n overrides = load_config(\n path,\n env_var,\n os.path.join(config_dir, 'cloud')\n )\n\n if defaults is None:\n defaults = DEFAULT_CLOUD_OPTS.copy()\n\n # Set defaults early to override Salt Master's default config values later\n defaults.update(overrides)\n overrides = defaults\n\n # Load cloud configuration from any default or provided includes\n overrides.update(\n salt.config.include_config(overrides['default_include'], config_dir, verbose=False)\n )\n include = overrides.get('include', [])\n overrides.update(\n salt.config.include_config(include, config_dir, verbose=True)\n )\n\n # The includes have been evaluated, let's see if master, providers and\n # profiles configuration settings have been included and if not, set the\n # default value\n if 'master_config' in overrides and master_config_path is None:\n # The configuration setting is being specified in the main cloud\n # configuration file\n master_config_path = overrides['master_config']\n elif 'master_config' not in overrides and not master_config \\\n and not master_config_path:\n # The configuration setting is not being provided in the main cloud\n # configuration file, and\n master_config_path = os.path.join(config_dir, 'master')\n\n # Convert relative to absolute paths if necessary\n master_config_path = _absolute_path(master_config_path, config_dir)\n\n if 'providers_config' in overrides and providers_config_path is None:\n # The configuration setting is being specified in the main cloud\n # configuration file\n providers_config_path = overrides['providers_config']\n elif 'providers_config' not in overrides and not providers_config \\\n and not providers_config_path:\n providers_config_path = os.path.join(config_dir, 'cloud.providers')\n\n # Convert relative to absolute paths if necessary\n providers_config_path = _absolute_path(providers_config_path, config_dir)\n\n if 'profiles_config' in overrides and profiles_config_path is None:\n # The configuration setting is being specified in the main cloud\n # configuration file\n profiles_config_path = overrides['profiles_config']\n elif 'profiles_config' not in overrides and not profiles_config \\\n and not profiles_config_path:\n profiles_config_path = os.path.join(config_dir, 'cloud.profiles')\n\n # Convert relative to absolute paths if necessary\n profiles_config_path = _absolute_path(profiles_config_path, config_dir)\n\n # Prepare the deploy scripts search path\n deploy_scripts_search_path = overrides.get(\n 'deploy_scripts_search_path',\n defaults.get('deploy_scripts_search_path', 'cloud.deploy.d')\n )\n if isinstance(deploy_scripts_search_path, six.string_types):\n deploy_scripts_search_path = [deploy_scripts_search_path]\n\n # Check the provided deploy scripts search path removing any non existing\n # entries.\n for idx, entry in enumerate(deploy_scripts_search_path[:]):\n if not os.path.isabs(entry):\n # Let's try adding the provided path's directory name turns the\n # entry into a proper directory\n entry = os.path.join(config_dir, entry)\n\n if os.path.isdir(entry):\n # Path exists, let's update the entry (its path might have been\n # made absolute)\n deploy_scripts_search_path[idx] = entry\n continue\n\n # It's not a directory? Remove it from the search path\n deploy_scripts_search_path.pop(idx)\n\n # Add the built-in scripts directory to the search path (last resort)\n deploy_scripts_search_path.append(\n os.path.abspath(\n os.path.join(\n os.path.dirname(__file__),\n '..',\n 'cloud',\n 'deploy'\n )\n )\n )\n\n # Let's make the search path a tuple and add it to the overrides.\n overrides.update(\n deploy_scripts_search_path=tuple(deploy_scripts_search_path)\n )\n\n # Grab data from the 4 sources\n # 1st - Master config\n if master_config_path is not None and master_config is not None:\n raise salt.exceptions.SaltCloudConfigError(\n 'Only pass `master_config` or `master_config_path`, not both.'\n )\n elif master_config_path is None and master_config is None:\n master_config = salt.config.master_config(\n overrides.get(\n # use the value from the cloud config file\n 'master_config',\n # if not found, use the default path\n os.path.join(salt.syspaths.CONFIG_DIR, 'master')\n )\n )\n elif master_config_path is not None and master_config is None:\n master_config = salt.config.master_config(master_config_path)\n\n # cloud config has a separate cachedir\n del master_config['cachedir']\n\n # 2nd - salt-cloud configuration which was loaded before so we could\n # extract the master configuration file if needed.\n\n # Override master configuration with the salt cloud(current overrides)\n master_config.update(overrides)\n # We now set the overridden master_config as the overrides\n overrides = master_config\n\n if providers_config_path is not None and providers_config is not None:\n raise salt.exceptions.SaltCloudConfigError(\n 'Only pass `providers_config` or `providers_config_path`, '\n 'not both.'\n )\n elif providers_config_path is None and providers_config is None:\n providers_config_path = overrides.get(\n # use the value from the cloud config file\n 'providers_config',\n # if not found, use the default path\n os.path.join(salt.syspaths.CONFIG_DIR, 'cloud.providers')\n )\n\n if profiles_config_path is not None and profiles_config is not None:\n raise salt.exceptions.SaltCloudConfigError(\n 'Only pass `profiles_config` or `profiles_config_path`, not both.'\n )\n elif profiles_config_path is None and profiles_config is None:\n profiles_config_path = overrides.get(\n # use the value from the cloud config file\n 'profiles_config',\n # if not found, use the default path\n os.path.join(salt.syspaths.CONFIG_DIR, 'cloud.profiles')\n )\n\n # Apply the salt-cloud configuration\n opts = apply_cloud_config(overrides, defaults)\n\n # 3rd - Include Cloud Providers\n if 'providers' in opts:\n if providers_config is not None:\n raise salt.exceptions.SaltCloudConfigError(\n 'Do not mix the old cloud providers configuration with '\n 'the passing a pre-configured providers configuration '\n 'dictionary.'\n )\n\n if providers_config_path is not None:\n providers_confd = os.path.join(\n os.path.dirname(providers_config_path),\n 'cloud.providers.d', '*'\n )\n\n if (os.path.isfile(providers_config_path) or\n glob.glob(providers_confd)):\n raise salt.exceptions.SaltCloudConfigError(\n 'Do not mix the old cloud providers configuration with '\n 'the new one. The providers configuration should now go '\n 'in the file `{0}` or a separate `*.conf` file within '\n '`cloud.providers.d/` which is relative to `{0}`.'.format(\n os.path.join(salt.syspaths.CONFIG_DIR, 'cloud.providers')\n )\n )\n # No exception was raised? It's the old configuration alone\n providers_config = opts['providers']\n\n elif providers_config_path is not None:\n # Load from configuration file, even if that files does not exist since\n # it will be populated with defaults.\n providers_config = cloud_providers_config(providers_config_path)\n\n # Let's assign back the computed providers configuration\n opts['providers'] = providers_config\n\n # 4th - Include VM profiles config\n if profiles_config is None:\n # Load profiles configuration from the provided file\n profiles_config = vm_profiles_config(profiles_config_path,\n providers_config)\n opts['profiles'] = profiles_config\n\n # recurse opts for sdb configs\n apply_sdb(opts)\n\n # prepend root_dir\n prepend_root_dirs = ['cachedir']\n if 'log_file' in opts and urlparse(opts['log_file']).scheme == '':\n prepend_root_dirs.append(opts['log_file'])\n prepend_root_dir(opts, prepend_root_dirs)\n\n # Return the final options\n return opts"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a cloud config file with the given overrides and defaults.", "response": "def apply_cloud_config(overrides, defaults=None):\n '''\n Return a cloud config\n '''\n if defaults is None:\n defaults = DEFAULT_CLOUD_OPTS.copy()\n\n config = defaults.copy()\n if overrides:\n config.update(overrides)\n\n # If the user defined providers in salt cloud's main configuration file, we\n # need to take care for proper and expected format.\n if 'providers' in config:\n # Keep a copy of the defined providers\n providers = config['providers'].copy()\n # Reset the providers dictionary\n config['providers'] = {}\n # Populate the providers dictionary\n for alias, details in six.iteritems(providers):\n if isinstance(details, list):\n for detail in details:\n if 'driver' not in detail:\n raise salt.exceptions.SaltCloudConfigError(\n 'The cloud provider alias \\'{0}\\' has an entry '\n 'missing the required setting of \\'driver\\'.'.format(\n alias\n )\n )\n\n driver = detail['driver']\n\n if ':' in driver:\n # Weird, but...\n alias, driver = driver.split(':')\n\n if alias not in config['providers']:\n config['providers'][alias] = {}\n\n detail['provider'] = '{0}:{1}'.format(alias, driver)\n config['providers'][alias][driver] = detail\n elif isinstance(details, dict):\n if 'driver' not in details:\n raise salt.exceptions.SaltCloudConfigError(\n 'The cloud provider alias \\'{0}\\' has an entry '\n 'missing the required setting of \\'driver\\''.format(\n alias\n )\n )\n\n driver = details['driver']\n\n if ':' in driver:\n # Weird, but...\n alias, driver = driver.split(':')\n if alias not in config['providers']:\n config['providers'][alias] = {}\n\n details['provider'] = '{0}:{1}'.format(alias, driver)\n config['providers'][alias][driver] = details\n\n # Migrate old configuration\n config = old_to_new(config)\n\n return config"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading in the salt cloud VM config file and apply the specified providers and defaults.", "response": "def vm_profiles_config(path,\n providers,\n env_var='SALT_CLOUDVM_CONFIG',\n defaults=None):\n '''\n Read in the salt cloud VM config file\n '''\n if defaults is None:\n defaults = VM_CONFIG_DEFAULTS\n\n overrides = salt.config.load_config(\n path, env_var, os.path.join(salt.syspaths.CONFIG_DIR, 'cloud.profiles')\n )\n\n default_include = overrides.get(\n 'default_include', defaults['default_include']\n )\n include = overrides.get('include', [])\n\n overrides.update(\n salt.config.include_config(default_include, path, verbose=False)\n )\n overrides.update(\n salt.config.include_config(include, path, verbose=True)\n )\n return apply_vm_profiles_config(providers, overrides, defaults)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef cloud_providers_config(path,\n env_var='SALT_CLOUD_PROVIDERS_CONFIG',\n defaults=None):\n '''\n Read in the salt cloud providers configuration file\n '''\n if defaults is None:\n defaults = PROVIDER_CONFIG_DEFAULTS\n\n overrides = salt.config.load_config(\n path, env_var, os.path.join(salt.syspaths.CONFIG_DIR, 'cloud.providers')\n )\n\n default_include = overrides.get(\n 'default_include', defaults['default_include']\n )\n include = overrides.get('include', [])\n\n overrides.update(\n salt.config.include_config(default_include, path, verbose=False)\n )\n overrides.update(\n salt.config.include_config(include, path, verbose=True)\n )\n return apply_cloud_providers_config(overrides, defaults)", "response": "Read in the salt cloud providers configuration file and apply the defaults"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef apply_cloud_providers_config(overrides, defaults=None):\n '''\n Apply the loaded cloud providers configuration.\n '''\n if defaults is None:\n defaults = PROVIDER_CONFIG_DEFAULTS\n\n config = defaults.copy()\n if overrides:\n config.update(overrides)\n\n # Is the user still using the old format in the new configuration file?!\n for name, settings in six.iteritems(config.copy()):\n if '.' in name:\n log.warning(\n 'Please switch to the new providers configuration syntax'\n )\n\n # Let's help out and migrate the data\n config = old_to_new(config)\n\n # old_to_new will migrate the old data into the 'providers' key of\n # the config dictionary. Let's map it correctly\n for prov_name, prov_settings in six.iteritems(config.pop('providers')):\n config[prov_name] = prov_settings\n break\n\n providers = {}\n ext_count = 0\n for key, val in six.iteritems(config):\n if key in ('conf_file', 'include', 'default_include', 'user'):\n continue\n\n if not isinstance(val, (list, tuple)):\n val = [val]\n else:\n # Need to check for duplicate cloud provider entries per \"alias\" or\n # we won't be able to properly reference it.\n handled_providers = set()\n for details in val:\n if 'driver' not in details:\n if 'extends' not in details:\n log.error(\n 'Please check your cloud providers configuration. '\n 'There\\'s no \\'driver\\' nor \\'extends\\' definition '\n 'referenced.'\n )\n continue\n\n if details['driver'] in handled_providers:\n log.error(\n 'You can only have one entry per cloud provider. For '\n 'example, if you have a cloud provider configuration '\n 'section named, \\'production\\', you can only have a '\n 'single entry for EC2, Joyent, Openstack, and so '\n 'forth.'\n )\n raise salt.exceptions.SaltCloudConfigError(\n 'The cloud provider alias \\'{0}\\' has multiple entries '\n 'for the \\'{1[driver]}\\' driver.'.format(key, details)\n )\n handled_providers.add(details['driver'])\n\n for entry in val:\n\n if 'driver' not in entry:\n entry['driver'] = '-only-extendable-{0}'.format(ext_count)\n ext_count += 1\n\n if key not in providers:\n providers[key] = {}\n\n provider = entry['driver']\n if provider not in providers[key]:\n providers[key][provider] = entry\n\n # Is any provider extending data!?\n while True:\n keep_looping = False\n for provider_alias, entries in six.iteritems(providers.copy()):\n for driver, details in six.iteritems(entries):\n # Set a holder for the defined profiles\n providers[provider_alias][driver]['profiles'] = {}\n\n if 'extends' not in details:\n continue\n\n extends = details.pop('extends')\n\n if ':' in extends:\n alias, provider = extends.split(':')\n if alias not in providers:\n raise salt.exceptions.SaltCloudConfigError(\n 'The \\'{0}\\' cloud provider entry in \\'{1}\\' is '\n 'trying to extend data from \\'{2}\\' though '\n '\\'{2}\\' is not defined in the salt cloud '\n 'providers loaded data.'.format(\n details['driver'],\n provider_alias,\n alias\n )\n )\n\n if provider not in providers.get(alias):\n raise salt.exceptions.SaltCloudConfigError(\n 'The \\'{0}\\' cloud provider entry in \\'{1}\\' is '\n 'trying to extend data from \\'{2}:{3}\\' though '\n '\\'{3}\\' is not defined in \\'{1}\\''.format(\n details['driver'],\n provider_alias,\n alias,\n provider\n )\n )\n details['extends'] = '{0}:{1}'.format(alias, provider)\n # change provider details '-only-extendable-' to extended\n # provider name\n details['driver'] = provider\n elif providers.get(extends):\n raise salt.exceptions.SaltCloudConfigError(\n 'The \\'{0}\\' cloud provider entry in \\'{1}\\' is '\n 'trying to extend from \\'{2}\\' and no provider was '\n 'specified. Not extending!'.format(\n details['driver'], provider_alias, extends\n )\n )\n elif extends not in providers:\n raise salt.exceptions.SaltCloudConfigError(\n 'The \\'{0}\\' cloud provider entry in \\'{1}\\' is '\n 'trying to extend data from \\'{2}\\' though \\'{2}\\' '\n 'is not defined in the salt cloud providers loaded '\n 'data.'.format(\n details['driver'], provider_alias, extends\n )\n )\n else:\n if driver in providers.get(extends):\n details['extends'] = '{0}:{1}'.format(extends, driver)\n elif '-only-extendable-' in providers.get(extends):\n details['extends'] = '{0}:{1}'.format(\n extends, '-only-extendable-{0}'.format(ext_count)\n )\n else:\n # We're still not aware of what we're trying to extend\n # from. Let's try on next iteration\n details['extends'] = extends\n keep_looping = True\n if not keep_looping:\n break\n\n while True:\n # Merge provided extends\n keep_looping = False\n for alias, entries in six.iteritems(providers.copy()):\n for driver, details in six.iteritems(entries):\n\n if 'extends' not in details:\n # Extends resolved or non existing, continue!\n continue\n\n if 'extends' in details['extends']:\n # Since there's a nested extends, resolve this one in the\n # next iteration\n keep_looping = True\n continue\n\n # Let's get a reference to what we're supposed to extend\n extends = details.pop('extends')\n # Split the setting in (alias, driver)\n ext_alias, ext_driver = extends.split(':')\n # Grab a copy of what should be extended\n extended = providers.get(ext_alias).get(ext_driver).copy()\n # Merge the data to extend with the details\n extended = salt.utils.dictupdate.update(extended, details)\n # Update the providers dictionary with the merged data\n providers[alias][driver] = extended\n # Update name of the driver, now that it's populated with extended information\n if driver.startswith('-only-extendable-'):\n providers[alias][ext_driver] = providers[alias][driver]\n # Delete driver with old name to maintain dictionary size\n del providers[alias][driver]\n\n if not keep_looping:\n break\n\n # Now clean up any providers entry that was just used to be a data tree to\n # extend from\n for provider_alias, entries in six.iteritems(providers.copy()):\n for driver, details in six.iteritems(entries.copy()):\n if not driver.startswith('-only-extendable-'):\n continue\n\n log.info(\n \"There's at least one cloud driver under the '%s' \"\n 'cloud provider alias which does not have the required '\n \"'driver' setting. Removing it from the available \"\n 'providers listing.', provider_alias\n )\n providers[provider_alias].pop(driver)\n\n if not providers[provider_alias]:\n providers.pop(provider_alias)\n\n return providers", "response": "Apply the loaded cloud providers configuration."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget the value of a cloud configuration setting.", "response": "def get_cloud_config_value(name, vm_, opts, default=None, search_global=True):\n '''\n Search and return a setting in a known order:\n\n 1. In the virtual machine's configuration\n 2. In the virtual machine's profile configuration\n 3. In the virtual machine's provider configuration\n 4. In the salt cloud configuration if global searching is enabled\n 5. Return the provided default\n '''\n\n # As a last resort, return the default\n value = default\n\n if search_global is True and opts.get(name, None) is not None:\n # The setting name exists in the cloud(global) configuration\n value = deepcopy(opts[name])\n\n if vm_ and name:\n # Let's get the value from the profile, if present\n if 'profile' in vm_ and vm_['profile'] is not None:\n if name in opts['profiles'][vm_['profile']]:\n if isinstance(value, dict):\n value.update(opts['profiles'][vm_['profile']][name].copy())\n else:\n value = deepcopy(opts['profiles'][vm_['profile']][name])\n\n # Let's get the value from the provider, if present.\n if ':' in vm_['driver']:\n # The provider is defined as <provider-alias>:<driver-name>\n alias, driver = vm_['driver'].split(':')\n if alias in opts['providers'] and \\\n driver in opts['providers'][alias]:\n details = opts['providers'][alias][driver]\n if name in details:\n if isinstance(value, dict):\n value.update(details[name].copy())\n else:\n value = deepcopy(details[name])\n elif len(opts['providers'].get(vm_['driver'], ())) > 1:\n # The provider is NOT defined as <provider-alias>:<driver-name>\n # and there's more than one entry under the alias.\n # WARN the user!!!!\n log.error(\n \"The '%s' cloud provider definition has more than one \"\n 'entry. Your VM configuration should be specifying the '\n \"provider as 'driver: %s:<driver-engine>'. Since \"\n \"it's not, we're returning the first definition which \"\n 'might not be what you intended.',\n vm_['driver'], vm_['driver']\n )\n\n if vm_['driver'] in opts['providers']:\n # There's only one driver defined for this provider. This is safe.\n alias_defs = opts['providers'].get(vm_['driver'])\n provider_driver_defs = alias_defs[next(iter(list(alias_defs.keys())))]\n if name in provider_driver_defs:\n # The setting name exists in the VM's provider configuration.\n # Return it!\n if isinstance(value, dict):\n value.update(provider_driver_defs[name].copy())\n else:\n value = deepcopy(provider_driver_defs[name])\n\n if name and vm_ and name in vm_:\n # The setting name exists in VM configuration.\n if isinstance(vm_[name], types.GeneratorType):\n value = next(vm_[name], '')\n else:\n if isinstance(value, dict) and isinstance(vm_[name], dict):\n value.update(vm_[name].copy())\n else:\n value = deepcopy(vm_[name])\n\n return value"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef is_provider_configured(opts, provider, required_keys=(), log_message=True, aliases=()):\n '''\n Check and return the first matching and fully configured cloud provider\n configuration.\n '''\n if ':' in provider:\n alias, driver = provider.split(':')\n if alias not in opts['providers']:\n return False\n if driver not in opts['providers'][alias]:\n return False\n for key in required_keys:\n if opts['providers'][alias][driver].get(key, None) is None:\n if log_message is True:\n # There's at least one require configuration key which is not\n # set.\n log.warning(\n \"The required '%s' configuration setting is missing \"\n \"from the '%s' driver, which is configured under the \"\n \"'%s' alias.\", key, provider, alias\n )\n return False\n # If we reached this far, there's a properly configured provider.\n # Return it!\n return opts['providers'][alias][driver]\n\n for alias, drivers in six.iteritems(opts['providers']):\n for driver, provider_details in six.iteritems(drivers):\n if driver != provider and driver not in aliases:\n continue\n\n # If we reached this far, we have a matching provider, let's see if\n # all required configuration keys are present and not None.\n skip_provider = False\n for key in required_keys:\n if provider_details.get(key, None) is None:\n if log_message is True:\n # This provider does not include all necessary keys,\n # continue to next one.\n log.warning(\n \"The required '%s' configuration setting is \"\n \"missing from the '%s' driver, which is configured \"\n \"under the '%s' alias.\", key, provider, alias\n )\n skip_provider = True\n break\n\n if skip_provider:\n continue\n\n # If we reached this far, the provider included all required keys\n return provider_details\n\n # If we reached this point, the provider is not configured.\n return False", "response": "Check and return the first matching cloud provider and cloud provider configuration."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef is_profile_configured(opts, provider, profile_name, vm_=None):\n '''\n Check if the requested profile contains the minimum required parameters for\n a profile.\n\n Required parameters include image and provider for all drivers, while some\n drivers also require size keys.\n\n .. versionadded:: 2015.8.0\n '''\n # Standard dict keys required by all drivers.\n required_keys = ['provider']\n alias, driver = provider.split(':')\n\n # Most drivers need an image to be specified, but some do not.\n non_image_drivers = ['nova', 'virtualbox', 'libvirt', 'softlayer', 'oneandone', 'profitbricks']\n\n # Most drivers need a size, but some do not.\n non_size_drivers = ['opennebula', 'parallels', 'proxmox', 'scaleway',\n 'softlayer', 'softlayer_hw', 'vmware', 'vsphere',\n 'virtualbox', 'libvirt', 'oneandone', 'profitbricks']\n\n provider_key = opts['providers'][alias][driver]\n profile_key = opts['providers'][alias][driver]['profiles'][profile_name]\n\n # If cloning on Linode, size and image are not necessary.\n # They are obtained from the to-be-cloned VM.\n if driver == 'linode' and profile_key.get('clonefrom', False):\n non_image_drivers.append('linode')\n non_size_drivers.append('linode')\n elif driver == 'gce' and 'sourceImage' in six.text_type(vm_.get('ex_disks_gce_struct')):\n non_image_drivers.append('gce')\n\n # If cloning on VMware, specifying image is not necessary.\n if driver == 'vmware' and 'image' not in list(profile_key.keys()):\n non_image_drivers.append('vmware')\n\n if driver not in non_image_drivers:\n required_keys.append('image')\n if driver == 'vmware':\n required_keys.append('datastore')\n elif driver in ['linode', 'virtualbox']:\n required_keys.append('clonefrom')\n elif driver == 'nova':\n nova_image_keys = ['image', 'block_device_mapping', 'block_device', 'boot_volume']\n if not any([key in provider_key for key in nova_image_keys]) and not any([key in profile_key for key in nova_image_keys]):\n required_keys.extend(nova_image_keys)\n\n if driver not in non_size_drivers:\n required_keys.append('size')\n\n # Check if required fields are supplied in the provider config. If they\n # are present, remove it from the required_keys list.\n for item in list(required_keys):\n if item in provider_key:\n required_keys.remove(item)\n\n # If a vm_ dict was passed in, use that information to get any other configs\n # that we might have missed thus far, such as a option provided in a map file.\n if vm_:\n for item in list(required_keys):\n if item in vm_:\n required_keys.remove(item)\n\n # Check for remaining required parameters in the profile config.\n for item in required_keys:\n if profile_key.get(item, None) is None:\n # There's at least one required configuration item which is not set.\n log.error(\n \"The required '%s' configuration setting is missing from \"\n \"the '%s' profile, which is configured under the '%s' alias.\",\n item, profile_name, alias\n )\n return False\n\n return True", "response": "Check if a profile is configured."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks if the driver s dependencies are available.", "response": "def check_driver_dependencies(driver, dependencies):\n '''\n Check if the driver's dependencies are available.\n\n .. versionadded:: 2015.8.0\n\n driver\n The name of the driver.\n\n dependencies\n The dictionary of dependencies to check.\n '''\n ret = True\n for key, value in six.iteritems(dependencies):\n if value is False:\n log.warning(\n \"Missing dependency: '%s'. The %s driver requires \"\n \"'%s' to be installed.\", key, driver, key\n )\n ret = False\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nevaluating the function that determines the ID and return the result", "response": "def call_id_function(opts):\n '''\n Evaluate the function that determines the ID if the 'id_function'\n option is set and return the result\n '''\n if opts.get('id'):\n return opts['id']\n\n # Import 'salt.loader' here to avoid a circular dependency\n import salt.loader as loader\n\n if isinstance(opts['id_function'], six.string_types):\n mod_fun = opts['id_function']\n fun_kwargs = {}\n elif isinstance(opts['id_function'], dict):\n mod_fun, fun_kwargs = six.next(six.iteritems(opts['id_function']))\n if fun_kwargs is None:\n fun_kwargs = {}\n else:\n log.error('\\'id_function\\' option is neither a string nor a dictionary')\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)\n\n # split module and function and try loading the module\n mod, fun = mod_fun.split('.')\n if not opts.get('grains'):\n # Get grains for use by the module\n opts['grains'] = loader.grains(opts)\n\n try:\n id_mod = loader.raw_mod(opts, mod, fun)\n if not id_mod:\n raise KeyError\n # we take whatever the module returns as the minion ID\n newid = id_mod[mod_fun](**fun_kwargs)\n if not isinstance(newid, six.string_types) or not newid:\n log.error(\n 'Function %s returned value \"%s\" of type %s instead of string',\n mod_fun, newid, type(newid)\n )\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)\n log.info('Evaluated minion ID from module: %s', mod_fun)\n return newid\n except TypeError:\n log.error(\n 'Function arguments %s are incorrect for function %s',\n fun_kwargs, mod_fun\n )\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)\n except KeyError:\n log.error('Failed to load module %s', mod_fun)\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef remove_domain_from_fqdn(opts, newid):\n '''\n Depending on the values of `minion_id_remove_domain`,\n remove all domains or a single domain from a FQDN, effectivly generating a hostname.\n '''\n opt_domain = opts.get('minion_id_remove_domain')\n if opt_domain is True:\n if '.' in newid:\n # Remove any domain\n newid, xdomain = newid.split('.', 1)\n log.debug('Removed any domain (%s) from minion id.', xdomain)\n else:\n # Must be string type\n if newid.upper().endswith('.' + opt_domain.upper()):\n # Remove single domain\n newid = newid[:-len('.' + opt_domain)]\n log.debug('Removed single domain %s from minion id.', opt_domain)\n return newid", "response": "Remove domains from a FQDN."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_id(opts, cache_minion_id=False):\n '''\n Guess the id of the minion.\n\n If CONFIG_DIR/minion_id exists, use the cached minion ID from that file.\n If no minion id is configured, use multiple sources to find a FQDN.\n If no FQDN is found you may get an ip address.\n\n Returns two values: the detected ID, and a boolean value noting whether or\n not an IP address is being used for the ID.\n '''\n if opts['root_dir'] is None:\n root_dir = salt.syspaths.ROOT_DIR\n else:\n root_dir = opts['root_dir']\n\n config_dir = salt.syspaths.CONFIG_DIR\n if config_dir.startswith(salt.syspaths.ROOT_DIR):\n config_dir = config_dir.split(salt.syspaths.ROOT_DIR, 1)[-1]\n\n # Check for cached minion ID\n id_cache = os.path.join(root_dir,\n config_dir.lstrip(os.path.sep),\n 'minion_id')\n\n if opts.get('minion_id_caching', True):\n try:\n with salt.utils.files.fopen(id_cache) as idf:\n name = salt.utils.stringutils.to_unicode(idf.readline().strip())\n bname = salt.utils.stringutils.to_bytes(name)\n if bname.startswith(codecs.BOM): # Remove BOM if exists\n name = salt.utils.stringutils.to_str(bname.replace(codecs.BOM, '', 1))\n if name and name != 'localhost':\n log.debug('Using cached minion ID from %s: %s', id_cache, name)\n return name, False\n except (IOError, OSError):\n pass\n if '__role' in opts and opts.get('__role') == 'minion':\n log.debug(\n 'Guessing ID. The id can be explicitly set in %s',\n os.path.join(salt.syspaths.CONFIG_DIR, 'minion')\n )\n\n if opts.get('id_function'):\n newid = call_id_function(opts)\n else:\n newid = salt.utils.network.generate_minion_id()\n\n if opts.get('minion_id_lowercase'):\n newid = newid.lower()\n log.debug('Changed minion id %s to lowercase.', newid)\n\n # Optionally remove one or many domains in a generated minion id\n if opts.get('minion_id_remove_domain'):\n newid = remove_domain_from_fqdn(opts, newid)\n\n if '__role' in opts and opts.get('__role') == 'minion':\n if opts.get('id_function'):\n log.debug(\n 'Found minion id from external function %s: %s',\n opts['id_function'], newid\n )\n else:\n log.debug('Found minion id from generate_minion_id(): %s', newid)\n if cache_minion_id and opts.get('minion_id_caching', True):\n _cache_id(newid, id_cache)\n is_ipv4 = salt.utils.network.is_ipv4(newid)\n return newid, is_ipv4", "response": "Guess the id of the minion."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nresolves string names to integer constant in ssl configuration.", "response": "def _update_ssl_config(opts):\n '''\n Resolves string names to integer constant in ssl configuration.\n '''\n if opts['ssl'] in (None, False):\n opts['ssl'] = None\n return\n if opts['ssl'] is True:\n opts['ssl'] = {}\n return\n import ssl\n for key, prefix in (('cert_reqs', 'CERT_'),\n ('ssl_version', 'PROTOCOL_')):\n val = opts['ssl'].get(key)\n if val is None:\n continue\n if not isinstance(val, six.string_types) or not val.startswith(prefix) or not hasattr(ssl, val):\n message = 'SSL option \\'{0}\\' must be set to one of the following values: \\'{1}\\'.' \\\n .format(key, '\\', \\''.join([val for val in dir(ssl) if val.startswith(prefix)]))\n log.error(message)\n raise salt.exceptions.SaltConfigurationError(message)\n opts['ssl'][key] = getattr(ssl, val)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _adjust_log_file_override(overrides, default_log_file):\n '''\n Adjusts the log_file based on the log_dir override\n '''\n if overrides.get('log_dir'):\n # Adjust log_file if a log_dir override is introduced\n if overrides.get('log_file'):\n if not os.path.isabs(overrides['log_file']):\n # Prepend log_dir if log_file is relative\n overrides['log_file'] = os.path.join(overrides['log_dir'],\n overrides['log_file'])\n else:\n # Create the log_file override\n overrides['log_file'] = \\\n os.path.join(overrides['log_dir'],\n os.path.basename(default_log_file))", "response": "Adjusts the log_file based on the log_dir override and creates the log_file based on the default_log_file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef apply_minion_config(overrides=None,\n defaults=None,\n cache_minion_id=False,\n minion_id=None):\n '''\n Returns minion configurations dict.\n '''\n if defaults is None:\n defaults = DEFAULT_MINION_OPTS.copy()\n if overrides is None:\n overrides = {}\n\n opts = defaults.copy()\n opts['__role'] = 'minion'\n _adjust_log_file_override(overrides, defaults['log_file'])\n if overrides:\n opts.update(overrides)\n\n if 'environment' in opts:\n if opts['saltenv'] is not None:\n log.warning(\n 'The \\'saltenv\\' and \\'environment\\' minion config options '\n 'cannot both be used. Ignoring \\'environment\\' in favor of '\n '\\'saltenv\\'.',\n )\n # Set environment to saltenv in case someone's custom module is\n # refrencing __opts__['environment']\n opts['environment'] = opts['saltenv']\n else:\n log.warning(\n 'The \\'environment\\' minion config option has been renamed '\n 'to \\'saltenv\\'. Using %s as the \\'saltenv\\' config value.',\n opts['environment']\n )\n opts['saltenv'] = opts['environment']\n\n for idx, val in enumerate(opts['fileserver_backend']):\n if val in ('git', 'hg', 'svn', 'minion'):\n new_val = val + 'fs'\n log.debug(\n 'Changed %s to %s in minion opts\\' fileserver_backend list',\n val, new_val\n )\n opts['fileserver_backend'][idx] = new_val\n\n opts['__cli'] = salt.utils.stringutils.to_unicode(\n os.path.basename(sys.argv[0])\n )\n\n # No ID provided. Will getfqdn save us?\n using_ip_for_id = False\n if not opts.get('id'):\n if minion_id:\n opts['id'] = minion_id\n else:\n opts['id'], using_ip_for_id = get_id(\n opts,\n cache_minion_id=cache_minion_id)\n\n # it does not make sense to append a domain to an IP based id\n if not using_ip_for_id and 'append_domain' in opts:\n opts['id'] = _append_domain(opts)\n\n for directory in opts.get('append_minionid_config_dirs', []):\n if directory in ('pki_dir', 'cachedir', 'extension_modules'):\n newdirectory = os.path.join(opts[directory], opts['id'])\n opts[directory] = newdirectory\n elif directory == 'default_include' and directory in opts:\n include_dir = os.path.dirname(opts[directory])\n new_include_dir = os.path.join(include_dir,\n opts['id'],\n os.path.basename(opts[directory]))\n opts[directory] = new_include_dir\n\n # pidfile can be in the list of append_minionid_config_dirs, but pidfile\n # is the actual path with the filename, not a directory.\n if 'pidfile' in opts.get('append_minionid_config_dirs', []):\n newpath_list = os.path.split(opts['pidfile'])\n opts['pidfile'] = os.path.join(newpath_list[0], 'salt', opts['id'], newpath_list[1])\n\n if len(opts['sock_dir']) > len(opts['cachedir']) + 10:\n opts['sock_dir'] = os.path.join(opts['cachedir'], '.salt-unix')\n\n # Enabling open mode requires that the value be set to True, and\n # nothing else!\n opts['open_mode'] = opts['open_mode'] is True\n opts['file_roots'] = _validate_file_roots(opts['file_roots'])\n opts['pillar_roots'] = _validate_pillar_roots(opts['pillar_roots'])\n # Make sure ext_mods gets set if it is an untrue value\n # (here to catch older bad configs)\n opts['extension_modules'] = (\n opts.get('extension_modules') or\n os.path.join(opts['cachedir'], 'extmods')\n )\n # Set up the utils_dirs location from the extension_modules location\n opts['utils_dirs'] = (\n opts.get('utils_dirs') or\n [os.path.join(opts['extension_modules'], 'utils')]\n )\n\n # Insert all 'utils_dirs' directories to the system path\n insert_system_path(opts, opts['utils_dirs'])\n\n # Prepend root_dir to other paths\n prepend_root_dirs = [\n 'pki_dir', 'cachedir', 'sock_dir', 'extension_modules', 'pidfile',\n ]\n\n # These can be set to syslog, so, not actual paths on the system\n for config_key in ('log_file', 'key_logfile'):\n if urlparse(opts.get(config_key, '')).scheme == '':\n prepend_root_dirs.append(config_key)\n\n prepend_root_dir(opts, prepend_root_dirs)\n\n # if there is no beacons option yet, add an empty beacons dict\n if 'beacons' not in opts:\n opts['beacons'] = {}\n\n if overrides.get('ipc_write_buffer', '') == 'dynamic':\n opts['ipc_write_buffer'] = _DFLT_IPC_WBUFFER\n\n # Make sure hash_type is lowercase\n opts['hash_type'] = opts['hash_type'].lower()\n\n # Check and update TLS/SSL configuration\n _update_ssl_config(opts)\n _update_discovery_config(opts)\n\n return opts", "response": "Applies the minion configuration to the minion s configuration dictionary."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nupdate the discovery config for all instances.", "response": "def _update_discovery_config(opts):\n '''\n Update discovery config for all instances.\n\n :param opts:\n :return:\n '''\n if opts.get('discovery') not in (None, False):\n if opts['discovery'] is True:\n opts['discovery'] = {}\n discovery_config = {'attempts': 3, 'pause': 5, 'port': 4520, 'match': 'any', 'mapping': {}, 'multimaster': False}\n for key in opts['discovery']:\n if key not in discovery_config:\n raise salt.exceptions.SaltConfigurationError('Unknown discovery option: {0}'.format(key))\n if opts.get('__role') != 'minion':\n for key in ['attempts', 'pause', 'match']:\n del discovery_config[key]\n opts['discovery'] = salt.utils.dictupdate.update(discovery_config, opts['discovery'], True, True)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreads in the master configuration file and sets up default options for the master daemon.", "response": "def master_config(path, env_var='SALT_MASTER_CONFIG', defaults=None, exit_on_config_errors=False):\n '''\n Reads in the master configuration file and sets up default options\n\n This is useful for running the actual master daemon. For running\n Master-side client interfaces that need the master opts see\n :py:func:`salt.client.client_config`.\n '''\n if defaults is None:\n defaults = DEFAULT_MASTER_OPTS.copy()\n\n if not os.environ.get(env_var, None):\n # No valid setting was given using the configuration variable.\n # Lets see is SALT_CONFIG_DIR is of any use\n salt_config_dir = os.environ.get('SALT_CONFIG_DIR', None)\n if salt_config_dir:\n env_config_file_path = os.path.join(salt_config_dir, 'master')\n if salt_config_dir and os.path.isfile(env_config_file_path):\n # We can get a configuration file using SALT_CONFIG_DIR, let's\n # update the environment with this information\n os.environ[env_var] = env_config_file_path\n\n overrides = load_config(path, env_var, DEFAULT_MASTER_OPTS['conf_file'])\n default_include = overrides.get('default_include',\n defaults['default_include'])\n include = overrides.get('include', [])\n\n overrides.update(include_config(default_include, path, verbose=False,\n exit_on_config_errors=exit_on_config_errors))\n overrides.update(include_config(include, path, verbose=True,\n exit_on_config_errors=exit_on_config_errors))\n opts = apply_master_config(overrides, defaults)\n _validate_ssh_minion_opts(opts)\n _validate_opts(opts)\n # If 'nodegroups:' is uncommented in the master config file, and there are\n # no nodegroups defined, opts['nodegroups'] will be None. Fix this by\n # reverting this value to the default, as if 'nodegroups:' was commented\n # out or not present.\n if opts.get('nodegroups') is None:\n opts['nodegroups'] = DEFAULT_MASTER_OPTS.get('nodegroups', {})\n if salt.utils.data.is_dictlist(opts['nodegroups']):\n opts['nodegroups'] = salt.utils.data.repack_dictlist(opts['nodegroups'])\n apply_sdb(opts)\n return opts"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\napply master configuration options to the master config file.", "response": "def apply_master_config(overrides=None, defaults=None):\n '''\n Returns master configurations dict.\n '''\n if defaults is None:\n defaults = DEFAULT_MASTER_OPTS.copy()\n if overrides is None:\n overrides = {}\n\n opts = defaults.copy()\n opts['__role'] = 'master'\n _adjust_log_file_override(overrides, defaults['log_file'])\n if overrides:\n opts.update(overrides)\n\n opts['__cli'] = salt.utils.stringutils.to_unicode(\n os.path.basename(sys.argv[0])\n )\n\n if 'environment' in opts:\n if opts['saltenv'] is not None:\n log.warning(\n 'The \\'saltenv\\' and \\'environment\\' master config options '\n 'cannot both be used. Ignoring \\'environment\\' in favor of '\n '\\'saltenv\\'.',\n )\n # Set environment to saltenv in case someone's custom runner is\n # refrencing __opts__['environment']\n opts['environment'] = opts['saltenv']\n else:\n log.warning(\n 'The \\'environment\\' master config option has been renamed '\n 'to \\'saltenv\\'. Using %s as the \\'saltenv\\' config value.',\n opts['environment']\n )\n opts['saltenv'] = opts['environment']\n\n if six.PY2 and 'rest_cherrypy' in opts:\n # CherryPy is not unicode-compatible\n opts['rest_cherrypy'] = salt.utils.data.encode(opts['rest_cherrypy'])\n\n for idx, val in enumerate(opts['fileserver_backend']):\n if val in ('git', 'hg', 'svn', 'minion'):\n new_val = val + 'fs'\n log.debug(\n 'Changed %s to %s in master opts\\' fileserver_backend list',\n val, new_val\n )\n opts['fileserver_backend'][idx] = new_val\n\n if len(opts['sock_dir']) > len(opts['cachedir']) + 10:\n opts['sock_dir'] = os.path.join(opts['cachedir'], '.salt-unix')\n\n opts['token_dir'] = os.path.join(opts['cachedir'], 'tokens')\n opts['syndic_dir'] = os.path.join(opts['cachedir'], 'syndics')\n # Make sure ext_mods gets set if it is an untrue value\n # (here to catch older bad configs)\n opts['extension_modules'] = (\n opts.get('extension_modules') or\n os.path.join(opts['cachedir'], 'extmods')\n )\n # Set up the utils_dirs location from the extension_modules location\n opts['utils_dirs'] = (\n opts.get('utils_dirs') or\n [os.path.join(opts['extension_modules'], 'utils')]\n )\n\n # Insert all 'utils_dirs' directories to the system path\n insert_system_path(opts, opts['utils_dirs'])\n\n if overrides.get('ipc_write_buffer', '') == 'dynamic':\n opts['ipc_write_buffer'] = _DFLT_IPC_WBUFFER\n\n using_ip_for_id = False\n append_master = False\n if not opts.get('id'):\n opts['id'], using_ip_for_id = get_id(\n opts,\n cache_minion_id=None)\n append_master = True\n\n # it does not make sense to append a domain to an IP based id\n if not using_ip_for_id and 'append_domain' in opts:\n opts['id'] = _append_domain(opts)\n if append_master:\n opts['id'] += '_master'\n\n # Prepend root_dir to other paths\n prepend_root_dirs = [\n 'pki_dir', 'cachedir', 'pidfile', 'sock_dir', 'extension_modules',\n 'autosign_file', 'autoreject_file', 'token_dir', 'syndic_dir',\n 'sqlite_queue_dir', 'autosign_grains_dir'\n ]\n\n # These can be set to syslog, so, not actual paths on the system\n for config_key in ('log_file', 'key_logfile', 'ssh_log_file'):\n log_setting = opts.get(config_key, '')\n if log_setting is None:\n continue\n\n if urlparse(log_setting).scheme == '':\n prepend_root_dirs.append(config_key)\n\n prepend_root_dir(opts, prepend_root_dirs)\n\n # Enabling open mode requires that the value be set to True, and\n # nothing else!\n opts['open_mode'] = opts['open_mode'] is True\n opts['auto_accept'] = opts['auto_accept'] is True\n opts['file_roots'] = _validate_file_roots(opts['file_roots'])\n opts['pillar_roots'] = _validate_file_roots(opts['pillar_roots'])\n\n if opts['file_ignore_regex']:\n # If file_ignore_regex was given, make sure it's wrapped in a list.\n # Only keep valid regex entries for improved performance later on.\n if isinstance(opts['file_ignore_regex'], six.string_types):\n ignore_regex = [opts['file_ignore_regex']]\n elif isinstance(opts['file_ignore_regex'], list):\n ignore_regex = opts['file_ignore_regex']\n\n opts['file_ignore_regex'] = []\n for regex in ignore_regex:\n try:\n # Can't store compiled regex itself in opts (breaks\n # serialization)\n re.compile(regex)\n opts['file_ignore_regex'].append(regex)\n except Exception:\n log.warning(\n 'Unable to parse file_ignore_regex. Skipping: %s',\n regex\n )\n\n if opts['file_ignore_glob']:\n # If file_ignore_glob was given, make sure it's wrapped in a list.\n if isinstance(opts['file_ignore_glob'], six.string_types):\n opts['file_ignore_glob'] = [opts['file_ignore_glob']]\n\n # Let's make sure `worker_threads` does not drop below 3 which has proven\n # to make `salt.modules.publish` not work under the test-suite.\n if opts['worker_threads'] < 3 and opts.get('peer', None):\n log.warning(\n \"The 'worker_threads' setting in '%s' cannot be lower than \"\n '3. Resetting it to the default value of 3.', opts['conf_file']\n )\n opts['worker_threads'] = 3\n\n opts.setdefault('pillar_source_merging_strategy', 'smart')\n\n # Make sure hash_type is lowercase\n opts['hash_type'] = opts['hash_type'].lower()\n\n # Check and update TLS/SSL configuration\n _update_ssl_config(opts)\n _update_discovery_config(opts)\n\n return opts"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef client_config(path, env_var='SALT_CLIENT_CONFIG', defaults=None):\n '''\n Load Master configuration data\n\n Usage:\n\n .. code-block:: python\n\n import salt.config\n master_opts = salt.config.client_config('/etc/salt/master')\n\n Returns a dictionary of the Salt Master configuration file with necessary\n options needed to communicate with a locally-running Salt Master daemon.\n This function searches for client specific configurations and adds them to\n the data from the master configuration.\n\n This is useful for master-side operations like\n :py:class:`~salt.client.LocalClient`.\n '''\n if defaults is None:\n defaults = DEFAULT_MASTER_OPTS.copy()\n\n xdg_dir = salt.utils.xdg.xdg_config_dir()\n if os.path.isdir(xdg_dir):\n client_config_dir = xdg_dir\n saltrc_config_file = 'saltrc'\n else:\n client_config_dir = os.path.expanduser('~')\n saltrc_config_file = '.saltrc'\n\n # Get the token file path from the provided defaults. If not found, specify\n # our own, sane, default\n opts = {\n 'token_file': defaults.get(\n 'token_file',\n os.path.join(client_config_dir, 'salt_token')\n )\n }\n # Update options with the master configuration, either from the provided\n # path, salt's defaults or provided defaults\n opts.update(\n master_config(path, defaults=defaults)\n )\n # Update with the users salt dot file or with the environment variable\n saltrc_config = os.path.join(client_config_dir, saltrc_config_file)\n opts.update(\n load_config(\n saltrc_config,\n env_var,\n saltrc_config\n )\n )\n # Make sure we have a proper and absolute path to the token file\n if 'token_file' in opts:\n opts['token_file'] = os.path.abspath(\n os.path.expanduser(\n opts['token_file']\n )\n )\n # If the token file exists, read and store the contained token\n if os.path.isfile(opts['token_file']):\n # Make sure token is still valid\n expire = opts.get('token_expire', 43200)\n if os.stat(opts['token_file']).st_mtime + expire > time.mktime(time.localtime()):\n with salt.utils.files.fopen(opts['token_file']) as fp_:\n opts['token'] = fp_.read().strip()\n # On some platforms, like OpenBSD, 0.0.0.0 won't catch a master running on localhost\n if opts['interface'] == '0.0.0.0':\n opts['interface'] = '127.0.0.1'\n\n # Make sure the master_uri is set\n if 'master_uri' not in opts:\n opts['master_uri'] = 'tcp://{ip}:{port}'.format(\n ip=salt.utils.zeromq.ip_bracket(opts['interface']),\n port=opts['ret_port']\n )\n\n # Return the client options\n _validate_opts(opts)\n return opts", "response": "Load the Salt Master configuration file and return a dictionary of options needed to communicate with a locally - running Salt Master daemon."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef api_config(path):\n '''\n Read in the Salt Master config file and add additional configs that\n need to be stubbed out for salt-api\n '''\n # Let's grab a copy of salt-api's required defaults\n opts = DEFAULT_API_OPTS.copy()\n\n # Let's override them with salt's master opts\n opts.update(client_config(path, defaults=DEFAULT_MASTER_OPTS.copy()))\n\n # Let's set the pidfile and log_file values in opts to api settings\n opts.update({\n 'pidfile': opts.get('api_pidfile', DEFAULT_API_OPTS['api_pidfile']),\n 'log_file': opts.get('api_logfile', DEFAULT_API_OPTS['api_logfile']),\n })\n\n prepend_root_dir(opts, [\n 'api_pidfile',\n 'api_logfile',\n 'log_file',\n 'pidfile'\n ])\n return opts", "response": "Read in the Salt Master config file and add additional configs that need to be stubbed out for salt - api\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef spm_config(path):\n '''\n Read in the salt master config file and add additional configs that\n need to be stubbed out for spm\n\n .. versionadded:: 2015.8.0\n '''\n # Let's grab a copy of salt's master default opts\n defaults = DEFAULT_MASTER_OPTS.copy()\n # Let's override them with spm's required defaults\n defaults.update(DEFAULT_SPM_OPTS)\n\n overrides = load_config(path, 'SPM_CONFIG', DEFAULT_SPM_OPTS['spm_conf_file'])\n default_include = overrides.get('spm_default_include',\n defaults['spm_default_include'])\n include = overrides.get('include', [])\n\n overrides.update(include_config(default_include, path, verbose=False))\n overrides.update(include_config(include, path, verbose=True))\n defaults = apply_master_config(overrides, defaults)\n defaults = apply_spm_config(overrides, defaults)\n return client_config(path, env_var='SPM_CONFIG', defaults=defaults)", "response": "Read in the master config file and add additional configs that need to be stubbed out for spm"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the spm configurations dict.", "response": "def apply_spm_config(overrides, defaults):\n '''\n Returns the spm configurations dict.\n\n .. versionadded:: 2015.8.1\n '''\n opts = defaults.copy()\n _adjust_log_file_override(overrides, defaults['log_file'])\n if overrides:\n opts.update(overrides)\n\n # Prepend root_dir to other paths\n prepend_root_dirs = [\n 'formula_path', 'pillar_path', 'reactor_path',\n 'spm_cache_dir', 'spm_build_dir'\n ]\n\n # These can be set to syslog, so, not actual paths on the system\n for config_key in ('spm_logfile',):\n log_setting = opts.get(config_key, '')\n if log_setting is None:\n continue\n\n if urlparse(log_setting).scheme == '':\n prepend_root_dirs.append(config_key)\n\n prepend_root_dir(opts, prepend_root_dirs)\n return opts"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _render_template(config_file):\n '''\n Render config template, substituting grains where found.\n '''\n dirname, filename = os.path.split(config_file)\n env = jinja2.Environment(loader=jinja2.FileSystemLoader(dirname))\n template = env.get_template(filename)\n return template.render(__grains__)", "response": "Render config template substituting grains where found."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _config(name, conf, default=None):\n '''\n Return a value for 'name' from the config file options. If the 'name' is\n not in the config, the 'default' value is returned. This method converts\n unicode values to str type under python 2.\n '''\n try:\n value = conf[name]\n except KeyError:\n value = default\n return salt.utils.data.decode(value, to_str=True)", "response": "Return a value for name from the config file options."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ntake LDAP search result and returns a dictionary.", "response": "def _result_to_dict(data, result, conf, source):\n '''\n Aggregates LDAP search result based on rules, returns a dictionary.\n\n Rules:\n Attributes tagged in the pillar config as 'attrs' or 'lists' are\n scanned for a 'key=value' format (non matching entries are ignored.\n\n Entries matching the 'attrs' tag overwrite previous values where\n the key matches a previous result.\n\n Entries matching the 'lists' tag are appended to list of values where\n the key matches a previous result.\n\n All Matching entries are then written directly to the pillar data\n dictionary as data[key] = value.\n\n For example, search result:\n\n { saltKeyValue': ['ntpserver=ntp.acme.local', 'foo=myfoo'],\n 'saltList': ['vhost=www.acme.net', 'vhost=www.acme.local'] }\n\n is written to the pillar data dictionary as:\n\n { 'ntpserver': 'ntp.acme.local', 'foo': 'myfoo',\n 'vhost': ['www.acme.net', 'www.acme.local'] }\n '''\n attrs = _config('attrs', conf) or []\n lists = _config('lists', conf) or []\n dict_key_attr = _config('dict_key_attr', conf) or 'dn'\n # TODO:\n # deprecate the default 'mode: split' and make the more\n # straightforward 'mode: map' the new default\n mode = _config('mode', conf) or 'split'\n if mode == 'map':\n data[source] = []\n for record in result:\n ret = {}\n if 'dn' in attrs or 'distinguishedName' in attrs:\n log.debug('dn: %s', record[0])\n ret['dn'] = record[0]\n record = record[1]\n log.debug('record: %s', record)\n for key in record:\n if key in attrs:\n for item in record.get(key):\n ret[key] = item\n if key in lists:\n ret[key] = record.get(key)\n data[source].append(ret)\n elif mode == 'dict':\n data[source] = {}\n for record in result:\n ret = {}\n distinguished_name = record[0]\n log.debug('dn: %s', distinguished_name)\n if 'dn' in attrs or 'distinguishedName' in attrs:\n ret['dn'] = distinguished_name\n record = record[1]\n log.debug('record: %s', record)\n for key in record:\n if key in attrs:\n for item in record.get(key):\n ret[key] = item\n if key in lists:\n ret[key] = record.get(key)\n if dict_key_attr in ['dn', 'distinguishedName']:\n dict_key = distinguished_name\n else:\n dict_key = ','.join(sorted(record.get(dict_key_attr, [])))\n try:\n data[source][dict_key].append(ret)\n except KeyError:\n data[source][dict_key] = [ret]\n elif mode == 'split':\n for key in result[0][1]:\n if key in attrs:\n for item in result.get(key):\n skey, sval = item.split('=', 1)\n data[skey] = sval\n elif key in lists:\n for item in result.get(key):\n if '=' in item:\n skey, sval = item.split('=', 1)\n if skey not in data:\n data[skey] = [sval]\n else:\n data[skey].append(sval)\n return data"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nperforming an LDAP search and returns the results as a dictionary appropriate for pillar use.", "response": "def _do_search(conf):\n '''\n Builds connection and search arguments, performs the LDAP search and\n formats the results as a dictionary appropriate for pillar use.\n '''\n # Build LDAP connection args\n connargs = {}\n for name in ['server', 'port', 'tls', 'binddn', 'bindpw', 'anonymous']:\n connargs[name] = _config(name, conf)\n if connargs['binddn'] and connargs['bindpw']:\n connargs['anonymous'] = False\n # Build search args\n try:\n _filter = conf['filter']\n except KeyError:\n raise SaltInvocationError('missing filter')\n _dn = _config('dn', conf)\n scope = _config('scope', conf)\n _lists = _config('lists', conf) or []\n _attrs = _config('attrs', conf) or []\n _dict_key_attr = _config('dict_key_attr', conf, 'dn')\n attrs = _lists + _attrs + [_dict_key_attr]\n if not attrs:\n attrs = None\n # Perform the search\n try:\n result = __salt__['ldap.search'](_filter, _dn, scope, attrs,\n **connargs)['results']\n except IndexError: # we got no results for this search\n log.debug('LDAP search returned no results for filter %s', _filter)\n result = {}\n except Exception:\n log.critical(\n 'Failed to retrieve pillar data from LDAP:\\n', exc_info=True\n )\n return {}\n return result"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef ext_pillar(minion_id, # pylint: disable=W0613\n pillar, # pylint: disable=W0613\n config_file):\n '''\n Execute LDAP searches and return the aggregated data\n '''\n config_template = None\n try:\n config_template = _render_template(config_file)\n except jinja2.exceptions.TemplateNotFound:\n log.debug('pillar_ldap: missing configuration file %s', config_file)\n except Exception:\n log.debug('pillar_ldap: failed to render template for %s',\n config_file, exc_info=True)\n\n if not config_template:\n # We don't have a config file\n return {}\n\n import salt.utils.yaml\n try:\n opts = salt.utils.yaml.safe_load(config_template) or {}\n opts['conf_file'] = config_file\n except Exception as err:\n import salt.log\n msg = 'pillar_ldap: error parsing configuration file: {0} - {1}'.format(\n config_file, err\n )\n if salt.log.is_console_configured():\n log.warning(msg)\n else:\n print(msg)\n return {}\n else:\n if not isinstance(opts, dict):\n log.warning(\n 'pillar_ldap: %s is invalidly formatted, must be a YAML '\n 'dictionary. See the documentation for more information.',\n config_file\n )\n return {}\n\n if 'search_order' not in opts:\n log.warning(\n 'pillar_ldap: search_order missing from configuration. See the '\n 'documentation for more information.'\n )\n return {}\n\n data = {}\n for source in opts['search_order']:\n config = opts[source]\n result = _do_search(config)\n log.debug('source %s got result %s', source, result)\n if result:\n data = _result_to_dict(data, result, config, source)\n return data", "response": "Execute LDAP searches and return the aggregated data"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef import_cert(name, cert_format=_DEFAULT_FORMAT, context=_DEFAULT_CONTEXT, store=_DEFAULT_STORE,\n exportable=True, password='', saltenv='base'):\n '''\n Import the certificate file into the given certificate store.\n\n :param str name: The path of the certificate file to import.\n :param str cert_format: The certificate format. Specify 'cer' for X.509, or 'pfx' for PKCS #12.\n :param str context: The name of the certificate store location context.\n :param str store: The name of the certificate store.\n :param bool exportable: Mark the certificate as exportable. Only applicable to pfx format.\n :param str password: The password of the certificate. Only applicable to pfx format.\n :param str saltenv: The environment the file resides in.\n\n Example of usage with only the required arguments:\n\n .. code-block:: yaml\n\n site0-cert-imported:\n win_pki.import_cert:\n - name: salt://win/webserver/certs/site0.cer\n\n Example of usage specifying all available arguments:\n\n .. code-block:: yaml\n\n site0-cert-imported:\n win_pki.import_cert:\n - name: salt://win/webserver/certs/site0.pfx\n - cert_format: pfx\n - context: LocalMachine\n - store: My\n - exportable: True\n - password: TestPassword\n - saltenv: base\n '''\n ret = {'name': name,\n 'changes': dict(),\n 'comment': six.text_type(),\n 'result': None}\n\n store_path = r'Cert:\\{0}\\{1}'.format(context, store)\n\n cached_source_path = __salt__['cp.cache_file'](name, saltenv)\n current_certs = __salt__['win_pki.get_certs'](context=context, store=store)\n if password:\n cert_props = __salt__['win_pki.get_cert_file'](name=cached_source_path, cert_format=cert_format, password=password)\n else:\n cert_props = __salt__['win_pki.get_cert_file'](name=cached_source_path, cert_format=cert_format)\n\n if cert_props['thumbprint'] in current_certs:\n ret['comment'] = (\"Certificate '{0}' already contained in store:\"\n ' {1}').format(cert_props['thumbprint'], store_path)\n ret['result'] = True\n elif __opts__['test']:\n ret['comment'] = (\"Certificate '{0}' will be imported into store:\"\n ' {1}').format(cert_props['thumbprint'], store_path)\n ret['changes'] = {'old': None,\n 'new': cert_props['thumbprint']}\n else:\n ret['changes'] = {'old': None,\n 'new': cert_props['thumbprint']}\n ret['result'] = __salt__['win_pki.import_cert'](name=name, cert_format=cert_format,\n context=context, store=store,\n exportable=exportable, password=password,\n saltenv=saltenv)\n if ret['result']:\n ret['comment'] = (\"Certificate '{0}' imported into store:\"\n ' {1}').format(cert_props['thumbprint'], store_path)\n else:\n ret['comment'] = (\"Certificate '{0}' unable to be imported into store:\"\n ' {1}').format(cert_props['thumbprint'], store_path)\n return ret", "response": "Imports the certificate file into the given certificate store."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef remove_cert(name, thumbprint, context=_DEFAULT_CONTEXT, store=_DEFAULT_STORE):\n '''\n Remove the certificate from the given certificate store.\n\n :param str thumbprint: The thumbprint value of the target certificate.\n :param str context: The name of the certificate store location context.\n :param str store: The name of the certificate store.\n\n Example of usage with only the required arguments:\n\n .. code-block:: yaml\n\n site0-cert-removed:\n win_pki.remove_cert:\n - thumbprint: 9988776655443322111000AAABBBCCCDDDEEEFFF\n\n Example of usage specifying all available arguments:\n\n .. code-block:: yaml\n\n site0-cert-removed:\n win_pki.remove_cert:\n - thumbprint: 9988776655443322111000AAABBBCCCDDDEEEFFF\n - context: LocalMachine\n - store: My\n '''\n ret = {'name': name,\n 'changes': dict(),\n 'comment': six.text_type(),\n 'result': None}\n\n store_path = r'Cert:\\{0}\\{1}'.format(context, store)\n current_certs = __salt__['win_pki.get_certs'](context=context, store=store)\n\n if thumbprint not in current_certs:\n ret['comment'] = \"Certificate '{0}' already removed from store: {1}\".format(thumbprint,\n store_path)\n ret['result'] = True\n elif __opts__['test']:\n ret['comment'] = \"Certificate '{0}' will be removed from store: {1}\".format(thumbprint,\n store_path)\n ret['changes'] = {'old': thumbprint,\n 'new': None}\n else:\n ret['changes'] = {'old': thumbprint,\n 'new': None}\n ret['result'] = __salt__['win_pki.remove_cert'](thumbprint=thumbprint, context=context,\n store=store)\n if ret['result']:\n ret['comment'] = \"Certificate '{0}' removed from store: {1}\".format(thumbprint, store_path)\n else:\n ret['comment'] = \"Certificate '{0}' unable to be removed from store: {1}\".format(thumbprint,\n store_path)\n return ret", "response": "Remove a certificate from the given certificate store."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset a key value pair in the etcd service", "response": "def set_(key, value, service=None, profile=None): # pylint: disable=W0613\n '''\n Set a key/value pair in the etcd service\n '''\n client = _get_conn(profile)\n client.set(key, value)\n return get(key, service, profile)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets a value from the etcd service", "response": "def get(key, service=None, profile=None): # pylint: disable=W0613\n '''\n Get a value from the etcd service\n '''\n client = _get_conn(profile)\n result = client.get(key)\n return result.value"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef delete(key, service=None, profile=None): # pylint: disable=W0613\n '''\n Get a value from the etcd service\n '''\n client = _get_conn(profile)\n try:\n client.delete(key)\n return True\n except Exception:\n return False", "response": "Delete a value from the etcd service\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef chain_present(name, table='filter', table_type=None, hook=None, priority=None, family='ipv4'):\n '''\n .. versionadded:: 2014.7.0\n\n Verify the chain is exist.\n\n name\n A user-defined chain name.\n\n table\n The table to own the chain.\n\n family\n Networking family, either ipv4 or ipv6\n '''\n\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n chain_check = __salt__['nftables.check_chain'](table, name, family=family)\n if chain_check['result'] is True:\n ret['result'] = True\n ret['comment'] = ('nftables {0} chain is already exist in {1} table for {2}'\n .format(name, table, family))\n return ret\n\n res = __salt__['nftables.new_chain'](\n table,\n name,\n table_type=table_type,\n hook=hook,\n priority=priority,\n family=family\n )\n\n if res['result'] is True:\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = ('nftables {0} chain in {1} table create success for {2}'\n .format(name, table, family))\n return ret\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to create {0} chain in {1} table: {2} for {3}'.format(\n name,\n table,\n res['comment'].strip(),\n family\n )\n return ret", "response": "Ensure the chain exists in the specified table."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nverify that the chain is absent in the specified table.", "response": "def chain_absent(name, table='filter', family='ipv4'):\n '''\n .. versionadded:: 2014.7.0\n\n Verify the chain is absent.\n\n family\n Networking family, either ipv4 or ipv6\n '''\n\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n chain_check = __salt__['nftables.check_chain'](table, name, family)\n if not chain_check:\n ret['result'] = True\n ret['comment'] = ('nftables {0} chain is already absent in {1} table for {2}'\n .format(name, table, family))\n return ret\n\n flush_chain = __salt__['nftables.flush'](table, name, family)\n if flush_chain:\n command = __salt__['nftables.delete_chain'](table, name, family)\n if command is True:\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = ('nftables {0} chain in {1} table delete success for {2}'\n .format(name, table, family))\n else:\n ret['result'] = False\n ret['comment'] = ('Failed to delete {0} chain in {1} table: {2} for {3}'\n .format(name, table, command.strip(), family))\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to flush {0} chain in {1} table: {2} for {3}'.format(\n name,\n table,\n flush_chain.strip(),\n family\n )\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nappends a nftables rule to a chain of nftables rules.", "response": "def append(name, family='ipv4', **kwargs):\n '''\n .. versionadded:: 0.17.0\n\n Append a rule to a chain\n\n name\n A user-defined name to call this rule by in another part of a state or\n formula. This should not be an actual rule.\n\n family\n Network family, ipv4 or ipv6.\n\n All other arguments are passed in with the same name as the long option\n that would normally be used for nftables, with one exception: `--state` is\n specified as `connstate` instead of `state` (not to be confused with\n `ctstate`).\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n for ignore in _STATE_INTERNAL_KEYWORDS:\n if ignore in kwargs:\n del kwargs[ignore]\n res = __salt__['nftables.build_rule'](family=family, **kwargs)\n if not res['result']:\n return res\n rule = res['rule']\n\n res = __salt__['nftables.build_rule'](full=True, family=family, command='add', **kwargs)\n if not res['result']:\n return res\n command = res['rule']\n\n res = __salt__['nftables.check'](kwargs['table'],\n kwargs['chain'],\n rule,\n family)\n if res['result']:\n ret['result'] = True\n ret['comment'] = 'nftables rule for {0} already set ({1}) for {2}'.format(\n name,\n command.strip(),\n family)\n return ret\n if 'test' in __opts__ and __opts__['test']:\n ret['comment'] = 'nftables rule for {0} needs to be set ({1}) for {2}'.format(\n name,\n command.strip(),\n family)\n return ret\n res = __salt__['nftables.append'](kwargs['table'],\n kwargs['chain'],\n rule,\n family)\n if res['result']:\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = 'Set nftables rule for {0} to: {1} for {2}'.format(\n name,\n command.strip(),\n family)\n if 'save' in kwargs:\n if kwargs['save']:\n __salt__['nftables.save'](filename=None, family=family)\n ret['comment'] = ('Set and Saved nftables rule for {0} to: '\n '{1} for {2}'.format(name, command.strip(), family))\n return ret\n else:\n ret['result'] = False\n ret['comment'] = ('Failed to set nftables rule for {0}.\\n'\n 'Attempted rule was {1} for {2}.\\n'\n '{3}').format(\n name,\n command.strip(), family, res['comment'])\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nflush current nftables rules in a specific nftables table or chain.", "response": "def flush(name, family='ipv4', **kwargs):\n '''\n .. versionadded:: 2014.7.0\n\n Flush current nftables state\n\n family\n Networking family, either ipv4 or ipv6\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n for ignore in _STATE_INTERNAL_KEYWORDS:\n if ignore in kwargs:\n del kwargs[ignore]\n\n if 'table' not in kwargs:\n kwargs['table'] = 'filter'\n\n res = __salt__['nftables.check_table'](kwargs['table'], family=family)\n if not res['result']:\n ret['result'] = False\n ret['comment'] = 'Failed to flush table {0} in family {1}, table does not exist.'.format(\n kwargs['table'],\n family\n )\n return ret\n\n if 'chain' not in kwargs:\n kwargs['chain'] = ''\n else:\n res = __salt__['nftables.check_chain'](kwargs['table'],\n kwargs['chain'],\n family=family)\n if not res['result']:\n ret['result'] = False\n ret['comment'] = 'Failed to flush chain {0} in table {1} in family {2}, chain does not exist.'.format(\n kwargs['chain'],\n kwargs['table'],\n family\n )\n return ret\n\n res = __salt__['nftables.flush'](kwargs['table'],\n kwargs['chain'],\n family)\n if res['result']:\n ret['changes'] = {'locale': name}\n ret['result'] = True\n ret['comment'] = 'Flush nftables rules in {0} table {1} chain {2} family'.format(\n kwargs['table'],\n kwargs['chain'],\n family\n )\n return ret\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to flush nftables rules'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nstarting Riak CLI Example", "response": "def start():\n '''\n Start Riak\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' riak.start\n '''\n ret = {'comment': '', 'success': False}\n\n cmd = __execute_cmd('riak', 'start')\n\n if cmd['retcode'] != 0:\n ret['comment'] = cmd['stderr']\n else:\n ret['comment'] = cmd['stdout']\n ret['success'] = True\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef stop():\n '''\n Stop Riak\n\n .. versionchanged:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' riak.stop\n '''\n ret = {'comment': '', 'success': False}\n\n cmd = __execute_cmd('riak', 'stop')\n\n if cmd['retcode'] != 0:\n ret['comment'] = cmd['stderr']\n else:\n ret['comment'] = cmd['stdout']\n ret['success'] = True\n\n return ret", "response": "Stop the current Riak\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\njoining a Riak cluster", "response": "def cluster_join(username, hostname):\n '''\n Join a Riak cluster\n\n .. versionchanged:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' riak.cluster_join <user> <host>\n\n username - The riak username to join the cluster\n hostname - The riak hostname you are connecting to\n '''\n ret = {'comment': '', 'success': False}\n\n cmd = __execute_cmd(\n 'riak-admin', 'cluster join {0}@{1}'.format(username, hostname)\n )\n\n if cmd['retcode'] != 0:\n ret['comment'] = cmd['stdout']\n else:\n ret['comment'] = cmd['stdout']\n ret['success'] = True\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncommit the current state of the", "response": "def cluster_commit():\n '''\n Commit Cluster Changes\n\n .. versionchanged:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' riak.cluster_commit\n '''\n ret = {'comment': '', 'success': False}\n\n cmd = __execute_cmd('riak-admin', 'cluster commit')\n\n if cmd['retcode'] != 0:\n ret['comment'] = cmd['stdout']\n else:\n ret['comment'] = cmd['stdout']\n ret['success'] = True\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef member_status():\n '''\n Get cluster member status\n\n .. versionchanged:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' riak.member_status\n '''\n ret = {'membership': {},\n 'summary': {'Valid': 0,\n 'Leaving': 0,\n 'Exiting': 0,\n 'Joining': 0,\n 'Down': 0,\n }}\n\n out = __execute_cmd('riak-admin', 'member-status')['stdout'].splitlines()\n\n for line in out:\n if line.startswith(('=', '-', 'Status')):\n continue\n if '/' in line:\n # We're in the summary line\n for item in line.split('/'):\n key, val = item.split(':')\n ret['summary'][key.strip()] = val.strip()\n\n if len(line.split()) == 4:\n # We're on a node status line\n (status, ring, pending, node) = line.split()\n\n ret['membership'][node] = {\n 'Status': status,\n 'Ring': ring,\n 'Pending': pending\n }\n\n return ret", "response": "Return the current cluster member status"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the current node status", "response": "def status():\n '''\n Current node status\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' riak.status\n '''\n ret = {}\n\n cmd = __execute_cmd('riak-admin', 'status')\n\n for i in cmd['stdout'].splitlines():\n if ':' in i:\n (name, val) = i.split(':', 1)\n ret[name.strip()] = val.strip()\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef flags(name,\n use=None,\n accept_keywords=None,\n env=None,\n license=None,\n properties=None,\n unmask=False,\n mask=False):\n '''\n Enforce the given flags on the given package or ``DEPEND`` atom.\n\n .. warning::\n\n In most cases, the affected package(s) need to be rebuilt in\n order to apply the changes.\n\n name\n The name of the package or its DEPEND atom\n use\n A list of ``USE`` flags\n accept_keywords\n A list of keywords to accept. ``~ARCH`` means current host arch, and will\n be translated into a line without keywords\n env\n A list of environment files\n license\n A list of accepted licenses\n properties\n A list of additional properties\n unmask\n A boolean to unmask the package\n mask\n A boolean to mask the package\n '''\n ret = {'changes': {},\n 'comment': '',\n 'name': name,\n 'result': True}\n\n if use:\n result = _flags_helper('use', name, use, __opts__['test'])\n if result['result']:\n ret['changes']['use'] = result['changes']\n elif result['result'] is False:\n ret['result'] = False\n ret['comment'] = result['comment']\n return ret\n\n if accept_keywords:\n result = _flags_helper('accept_keywords', name, accept_keywords, __opts__['test'])\n if result['result']:\n ret['changes']['accept_keywords'] = result['changes']\n elif result['result'] is False:\n ret['result'] = False\n ret['comment'] = result['comment']\n return ret\n\n if env:\n result = _flags_helper('env', name, env, __opts__['test'])\n if result['result']:\n ret['changes']['env'] = result['changes']\n elif result['result'] is False:\n ret['result'] = False\n ret['comment'] = result['comment']\n return ret\n\n if license:\n result = _flags_helper('license', name, license, __opts__['test'])\n if result['result']:\n ret['changes']['license'] = result['changes']\n elif result['result'] is False:\n ret['result'] = False\n ret['comment'] = result['comment']\n return ret\n\n if properties:\n result = _flags_helper('properties', name, properties, __opts__['test'])\n if result['result']:\n ret['changes']['properties'] = result['changes']\n elif result['result'] is False:\n ret['result'] = False\n ret['comment'] = result['comment']\n return ret\n\n if mask:\n result = _mask_helper('mask', name, __opts__['test'])\n if result['result']:\n ret['changes']['mask'] = 'masked'\n elif result['result'] is False:\n ret['result'] = False\n ret['comment'] = result['comment']\n return ret\n\n if unmask:\n result = _mask_helper('unmask', name, __opts__['test'])\n if result['result']:\n ret['changes']['unmask'] = 'unmasked'\n elif result['result'] is False:\n ret['result'] = False\n ret['comment'] = result['comment']\n return ret\n\n if __opts__['test'] and not ret['result']:\n ret['result'] = None\n\n return ret", "response": "Enforce the given flags on the given package or DEPEND atom."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nadds a user to an htpasswd file using the webutil useradd command", "response": "def useradd(pwfile, user, password, opts='', runas=None):\n '''\n Add a user to htpasswd file using the htpasswd command. If the htpasswd\n file does not exist, it will be created.\n\n pwfile\n Path to htpasswd file\n\n user\n User name\n\n password\n User password\n\n opts\n Valid options that can be passed are:\n\n - `n` Don't update file; display results on stdout.\n - `m` Force MD5 encryption of the password (default).\n - `d` Force CRYPT encryption of the password.\n - `p` Do not encrypt the password (plaintext).\n - `s` Force SHA encryption of the password.\n\n runas\n The system user to run htpasswd command with\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' webutil.useradd /etc/httpd/htpasswd larry badpassword\n salt '*' webutil.useradd /etc/httpd/htpasswd larry badpass opts=ns\n '''\n if not os.path.exists(pwfile):\n opts += 'c'\n\n cmd = ['htpasswd', '-b{0}'.format(opts), pwfile, user, password]\n return __salt__['cmd.run_all'](cmd, runas=runas, python_shell=False)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndeletes a user from the specified htpasswd file", "response": "def userdel(pwfile, user, runas=None, all_results=False):\n '''\n Delete a user from the specified htpasswd file.\n\n pwfile\n Path to htpasswd file\n\n user\n User name\n\n runas\n The system user to run htpasswd command with\n\n all_results\n Return stdout, stderr, and retcode, not just stdout\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' webutil.userdel /etc/httpd/htpasswd larry\n '''\n if not os.path.exists(pwfile):\n return 'Error: The specified htpasswd file does not exist'\n\n cmd = ['htpasswd', '-D', pwfile, user]\n\n if all_results:\n out = __salt__['cmd.run_all'](cmd, runas=runas, python_shell=False)\n else:\n out = __salt__['cmd.run'](cmd, runas=runas,\n python_shell=False).splitlines()\n\n return out"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef verify(pwfile, user, password, opts='', runas=None):\n '''\n Return True if the htpasswd file exists, the user has an entry, and their\n password matches.\n\n pwfile\n Fully qualified path to htpasswd file\n\n user\n User name\n\n password\n User password\n\n opts\n Valid options that can be passed are:\n\n - `m` Force MD5 encryption of the password (default).\n - `d` Force CRYPT encryption of the password.\n - `p` Do not encrypt the password (plaintext).\n - `s` Force SHA encryption of the password.\n\n runas\n The system user to run htpasswd command with\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' webutil.verify /etc/httpd/htpasswd larry maybepassword\n salt '*' webutil.verify /etc/httpd/htpasswd larry maybepassword opts=ns\n '''\n if not os.path.exists(pwfile):\n return False\n\n cmd = ['htpasswd', '-bv{0}'.format(opts), pwfile, user, password]\n ret = __salt__['cmd.run_all'](cmd, runas=runas, python_shell=False)\n log.debug('Result of verifying htpasswd for user %s: %s', user, ret)\n\n return ret['retcode'] == 0", "response": "Verify the htpasswd file for a user and password."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck if the passed user is a member of the Administrators group", "response": "def is_admin(name):\n '''\n Is the passed user a member of the Administrators group\n\n Args:\n name (str): The name to check\n\n Returns:\n bool: True if user is a member of the Administrators group, False\n otherwise\n '''\n groups = get_user_groups(name, True)\n\n for group in groups:\n if group in ('S-1-5-32-544', 'S-1-5-18'):\n return True\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_user_groups(name, sid=False):\n '''\n Get the groups to which a user belongs\n\n Args:\n name (str): The user name to query\n sid (bool): True will return a list of SIDs, False will return a list of\n group names\n\n Returns:\n list: A list of group names or sids\n '''\n if name == 'SYSTEM':\n # 'win32net.NetUserGetLocalGroups' will fail if you pass in 'SYSTEM'.\n groups = [name]\n else:\n groups = win32net.NetUserGetLocalGroups(None, name)\n\n if not sid:\n return groups\n\n ret_groups = set()\n for group in groups:\n ret_groups.add(get_sid_from_name(group))\n\n return ret_groups", "response": "Get the groups to which a user belongs."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget the user executing the process", "response": "def get_current_user(with_domain=True):\n '''\n Gets the user executing the process\n\n Args:\n\n with_domain (bool):\n ``True`` will prepend the user name with the machine name or domain\n separated by a backslash\n\n Returns:\n str: The user name\n '''\n try:\n user_name = win32api.GetUserNameEx(win32api.NameSamCompatible)\n if user_name[-1] == '$':\n # Make the system account easier to identify.\n # Fetch sid so as to handle other language than english\n test_user = win32api.GetUserName()\n if test_user == 'SYSTEM':\n user_name = 'SYSTEM'\n elif get_sid_from_name(test_user) == 'S-1-5-18':\n user_name = 'SYSTEM'\n elif not with_domain:\n user_name = win32api.GetUserName()\n except pywintypes.error as exc:\n raise CommandExecutionError(\n 'Failed to get current user: {0}'.format(exc))\n\n if not user_name:\n return False\n\n return user_name"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_sam_name(username):\n r'''\n Gets the SAM name for a user. It basically prefixes a username without a\n backslash with the computer name. If the user does not exist, a SAM\n compatible name will be returned using the local hostname as the domain.\n\n i.e. salt.utils.get_same_name('Administrator') would return 'DOMAIN.COM\\Administrator'\n\n .. note:: Long computer names are truncated to 15 characters\n '''\n try:\n sid_obj = win32security.LookupAccountName(None, username)[0]\n except pywintypes.error:\n return '\\\\'.join([platform.node()[:15].upper(), username])\n username, domain, _ = win32security.LookupAccountSid(None, sid_obj)\n return '\\\\'.join([domain, username])", "response": "r Gets the SAM name for a user."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nescaping the argument for the cmd. exe shell.", "response": "def escape_argument(arg, escape=True):\n '''\n Escape the argument for the cmd.exe shell.\n See http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx\n\n First we escape the quote chars to produce a argument suitable for\n CommandLineToArgvW. We don't need to do this for simple arguments.\n\n Args:\n arg (str): a single command line argument to escape for the cmd.exe shell\n\n Kwargs:\n escape (bool): True will call the escape_for_cmd_exe() function\n which escapes the characters '()%!^\"<>&|'. False\n will not call the function and only quotes the cmd\n\n Returns:\n str: an escaped string suitable to be passed as a program argument to the cmd.exe shell\n '''\n if not arg or re.search(r'([\"\\s])', arg):\n arg = '\"' + arg.replace('\"', r'\\\"') + '\"'\n\n if not escape:\n return arg\n return escape_for_cmd_exe(arg)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nescaping an argument string for use by cmd. exe on Windows", "response": "def escape_for_cmd_exe(arg):\n '''\n Escape an argument string to be suitable to be passed to\n cmd.exe on Windows\n\n This method takes an argument that is expected to already be properly\n escaped for the receiving program to be properly parsed. This argument\n will be further escaped to pass the interpolation performed by cmd.exe\n unchanged.\n\n Any meta-characters will be escaped, removing the ability to e.g. use\n redirects or variables.\n\n Args:\n arg (str): a single command line argument to escape for cmd.exe\n\n Returns:\n str: an escaped string suitable to be passed as a program argument to cmd.exe\n '''\n meta_chars = '()%!^\"<>&|'\n meta_re = re.compile('(' + '|'.join(re.escape(char) for char in list(meta_chars)) + ')')\n meta_map = {char: \"^{0}\".format(char) for char in meta_chars}\n\n def escape_meta_chars(m):\n char = m.group(1)\n return meta_map[char]\n\n return meta_re.sub(escape_meta_chars, arg)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nbroadcasting a WM_SETTINGCHANGE to all Windows objects.", "response": "def broadcast_setting_change(message='Environment'):\n '''\n Send a WM_SETTINGCHANGE Broadcast to all Windows\n\n Args:\n\n message (str):\n A string value representing the portion of the system that has been\n updated and needs to be refreshed. Default is ``Environment``. These\n are some common values:\n\n - \"Environment\" : to effect a change in the environment variables\n - \"intl\" : to effect a change in locale settings\n - \"Policy\" : to effect a change in Group Policy Settings\n - a leaf node in the registry\n - the name of a section in the ``Win.ini`` file\n\n See lParam within msdn docs for\n `WM_SETTINGCHANGE <https://msdn.microsoft.com/en-us/library/ms725497%28VS.85%29.aspx>`_\n for more information on Broadcasting Messages.\n\n See GWL_WNDPROC within msdn docs for\n `SetWindowLong <https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx>`_\n for information on how to retrieve those messages.\n\n .. note::\n This will only affect new processes that aren't launched by services. To\n apply changes to the path or registry to services, the host must be\n restarted. The ``salt-minion``, if running as a service, will not see\n changes to the environment until the system is restarted. Services\n inherit their environment from ``services.exe`` which does not respond\n to messaging events. See\n `MSDN Documentation <https://support.microsoft.com/en-us/help/821761/changes-that-you-make-to-environment-variables-do-not-affect-services>`_\n for more information.\n\n CLI Example:\n\n ... code-block:: python\n\n import salt.utils.win_functions\n salt.utils.win_functions.broadcast_setting_change('Environment')\n '''\n # Listen for messages sent by this would involve working with the\n # SetWindowLong function. This can be accessed via win32gui or through\n # ctypes. You can find examples on how to do this by searching for\n # `Accessing WGL_WNDPROC` on the internet. Here are some examples of how\n # this might work:\n #\n # # using win32gui\n # import win32con\n # import win32gui\n # old_function = win32gui.SetWindowLong(window_handle, win32con.GWL_WNDPROC, new_function)\n #\n # # using ctypes\n # import ctypes\n # import win32con\n # from ctypes import c_long, c_int\n # user32 = ctypes.WinDLL('user32', use_last_error=True)\n # WndProcType = ctypes.WINFUNCTYPE(c_int, c_long, c_int, c_int)\n # new_function = WndProcType\n # old_function = user32.SetWindowLongW(window_handle, win32con.GWL_WNDPROC, new_function)\n broadcast_message = ctypes.create_unicode_buffer(message)\n user32 = ctypes.WinDLL('user32', use_last_error=True)\n result = user32.SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0,\n broadcast_message, SMTO_ABORTIFHUNG,\n 5000, 0)\n return result == 1"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts a GUID to a compressed GUID string.", "response": "def guid_to_squid(guid):\n '''\n Converts a GUID to a compressed guid (SQUID)\n\n Each Guid has 5 parts separated by '-'. For the first three each one will be\n totally reversed, and for the remaining two each one will be reversed by\n every other character. Then the final compressed Guid will be constructed by\n concatenating all the reversed parts without '-'.\n\n .. Example::\n\n Input: 2BE0FA87-5B36-43CF-95C8-C68D6673FB94\n Reversed: 78AF0EB2-63B5-FC34-598C-6CD86637BF49\n Final Compressed Guid: 78AF0EB263B5FC34598C6CD86637BF49\n\n Args:\n\n guid (str): A valid GUID\n\n Returns:\n str: A valid compressed GUID (SQUID)\n '''\n guid_pattern = re.compile(r'^\\{(\\w{8})-(\\w{4})-(\\w{4})-(\\w\\w)(\\w\\w)-(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)\\}$')\n guid_match = guid_pattern.match(guid)\n squid = ''\n if guid_match is not None:\n for index in range(1, 12):\n squid += guid_match.group(index)[::-1]\n return squid"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef squid_to_guid(squid):\n '''\n Converts a compressed GUID (SQUID) back into a GUID\n\n Args:\n\n squid (str): A valid compressed GUID\n\n Returns:\n str: A valid GUID\n '''\n squid_pattern = re.compile(r'^(\\w{8})(\\w{4})(\\w{4})(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)(\\w\\w)$')\n squid_match = squid_pattern.match(squid)\n guid = ''\n if squid_match is not None:\n guid = '{' + \\\n squid_match.group(1)[::-1]+'-' + \\\n squid_match.group(2)[::-1]+'-' + \\\n squid_match.group(3)[::-1]+'-' + \\\n squid_match.group(4)[::-1]+squid_match.group(5)[::-1] + '-'\n for index in range(6, 12):\n guid += squid_match.group(index)[::-1]\n guid += '}'\n return guid", "response": "Converts a compressed GUID ( SQUID ) back into a GUID"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the data from the specified minions mine", "response": "def get(tgt, fun, tgt_type='glob'):\n '''\n Gathers the data from the specified minions' mine, pass in the target,\n function to look up and the target type\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run mine.get '*' network.interfaces\n '''\n ret = salt.utils.minions.mine_get(tgt, fun, tgt_type, __opts__)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef update(tgt,\n tgt_type='glob',\n clear=False,\n mine_functions=None):\n '''\n .. versionadded:: 2017.7.0\n\n Update the mine data on a certain group of minions.\n\n tgt\n Which minions to target for the execution.\n\n tgt_type: ``glob``\n The type of ``tgt``.\n\n clear: ``False``\n Boolean flag specifying whether updating will clear the existing\n mines, or will update. Default: ``False`` (update).\n\n mine_functions\n Update the mine data on certain functions only.\n This feature can be used when updating the mine for functions\n that require refresh at different intervals than the rest of\n the functions specified under ``mine_functions`` in the\n minion/master config or pillar.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run mine.update '*'\n salt-run mine.update 'juniper-edges' tgt_type='nodegroup'\n '''\n ret = __salt__['salt.execute'](tgt,\n 'mine.update',\n tgt_type=tgt_type,\n clear=clear,\n mine_functions=mine_functions)\n return ret", "response": "Update the mine data for a specific target"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef nxapi_request(commands,\n method='cli_show',\n **kwargs):\n '''\n Send exec and config commands to the NX-OS device over NX-API.\n\n commands\n The exec or config commands to be sent.\n\n method:\n ``cli_show_ascii``: Return raw test or unstructured output.\n ``cli_show``: Return structured output.\n ``cli_conf``: Send configuration commands to the device.\n Defaults to ``cli_show``.\n\n transport: ``https``\n Specifies the type of connection transport to use. Valid values for the\n connection are ``http``, and ``https``.\n\n host: ``localhost``\n The IP address or DNS host name of the device.\n\n username: ``admin``\n The username to pass to the device to authenticate the NX-API connection.\n\n password\n The password to pass to the device to authenticate the NX-API connection.\n\n port\n The TCP port of the endpoint for the NX-API connection. If this keyword is\n not specified, the default value is automatically determined by the\n transport type (``80`` for ``http``, or ``443`` for ``https``).\n\n timeout: ``60``\n Time in seconds to wait for the device to respond. Default: 60 seconds.\n\n verify: ``True``\n Either a boolean, in which case it controls whether we verify the NX-API\n TLS certificate, or a string, in which case it must be a path to a CA bundle\n to use. Defaults to ``True``.\n '''\n client = NxapiClient(**kwargs)\n return client.request(method, commands)", "response": "Send exec and config commands to NX - OS device over NX - API."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef system_info(data):\n '''\n Helper method to return parsed system_info\n from the 'show version' command.\n '''\n if not data:\n return {}\n info = {\n 'software': _parse_software(data),\n 'hardware': _parse_hardware(data),\n 'plugins': _parse_plugins(data),\n }\n return {'nxos': info}", "response": "Helper method to return parsed system_info from the show version command."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _use_remote_connection(self, kwargs):\n '''\n Determine if connection is local or remote\n '''\n kwargs['host'] = kwargs.get('host')\n kwargs['username'] = kwargs.get('username')\n kwargs['password'] = kwargs.get('password')\n if kwargs['host'] is None or \\\n kwargs['username'] is None or \\\n kwargs['password'] is None:\n return False\n else:\n return True", "response": "Determine if connection is remote or local"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _prepare_conn_args(self, kwargs):\n '''\n Set connection arguments for remote or local connection.\n '''\n kwargs['connect_over_uds'] = True\n kwargs['timeout'] = kwargs.get('timeout', 60)\n kwargs['cookie'] = kwargs.get('cookie', 'admin')\n if self._use_remote_connection(kwargs):\n kwargs['transport'] = kwargs.get('transport', 'https')\n if kwargs['transport'] == 'https':\n kwargs['port'] = kwargs.get('port', 443)\n else:\n kwargs['port'] = kwargs.get('port', 80)\n kwargs['verify'] = kwargs.get('verify', True)\n if isinstance(kwargs['verify'], bool):\n kwargs['verify_ssl'] = kwargs['verify']\n else:\n kwargs['ca_bundle'] = kwargs['verify']\n kwargs['connect_over_uds'] = False\n return kwargs", "response": "Prepare connection arguments for remote or local connection."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _build_request(self, type, commands):\n '''\n Build NX-API JSON request.\n '''\n request = {}\n headers = {\n 'content-type': 'application/json',\n }\n if self.nxargs['connect_over_uds']:\n user = self.nxargs['cookie']\n headers['cookie'] = 'nxapi_auth=' + user + ':local'\n request['url'] = self.NXAPI_UDS_URI_PATH\n else:\n request['url'] = '{transport}://{host}:{port}{uri}'.format(\n transport=self.nxargs['transport'],\n host=self.nxargs['host'],\n port=self.nxargs['port'],\n uri=self.NXAPI_REMOTE_URI_PATH,\n )\n\n if isinstance(commands, (list, set, tuple)):\n commands = ' ; '.join(commands)\n payload = {}\n payload['ins_api'] = {\n 'version': self.NXAPI_VERSION,\n 'type': type,\n 'chunk': '0',\n 'sid': '1',\n 'input': commands,\n 'output_format': 'json',\n }\n request['headers'] = headers\n request['payload'] = json.dumps(payload)\n request['opts'] = {\n 'http_request_timeout': self.nxargs['timeout']\n }\n log.info('request: %s', request)\n return request", "response": "Builds NX - API JSON request."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef request(self, type, command_list):\n '''\n Send NX-API JSON request to the NX-OS device.\n '''\n req = self._build_request(type, command_list)\n if self.nxargs['connect_over_uds']:\n self.connection.request('POST', req['url'], req['payload'], req['headers'])\n response = self.connection.getresponse()\n else:\n response = self.connection(req['url'],\n method='POST',\n opts=req['opts'],\n data=req['payload'],\n header_dict=req['headers'],\n decode=True,\n decode_type='json',\n **self.nxargs)\n\n return self.parse_response(response, command_list)", "response": "Send NX - API JSON request to the NX - OS device."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef parse_response(self, response, command_list):\n '''\n Parse NX-API JSON response from the NX-OS device.\n '''\n # Check for 500 level NX-API Server Errors\n if isinstance(response, collections.Iterable) and 'status' in response:\n if int(response['status']) >= 500:\n raise NxosError('{}'.format(response))\n else:\n raise NxosError('NX-API Request Not Supported: {}'.format(response))\n\n if isinstance(response, collections.Iterable):\n body = response['dict']\n else:\n body = response\n\n if self.nxargs['connect_over_uds']:\n body = json.loads(response.read().decode('utf-8'))\n\n # Proceed with caution. The JSON may not be complete.\n # Don't just return body['ins_api']['outputs']['output'] directly.\n output = body.get('ins_api')\n if output is None:\n raise NxosClientError('Unexpected JSON output\\n{0}'.format(body))\n if output.get('outputs'):\n output = output['outputs']\n if output.get('output'):\n output = output['output']\n\n # The result list stores results for each command that was sent to\n # nxapi.\n result = []\n # Keep track of successful commands using previous_commands list so\n # they can be displayed if a specific command fails in a chain of\n # commands.\n previous_commands = []\n\n # Make sure output and command_list lists to be processed in the\n # subesequent loop.\n if not isinstance(output, list):\n output = [output]\n if isinstance(command_list, string_types):\n command_list = [cmd.strip() for cmd in command_list.split(';')]\n if not isinstance(command_list, list):\n command_list = [command_list]\n\n for cmd_result, cmd in zip(output, command_list):\n code = cmd_result.get('code')\n msg = cmd_result.get('msg')\n log.info('command %s:', cmd)\n log.info('PARSE_RESPONSE: %s %s', code, msg)\n if code == '400':\n raise CommandExecutionError({\n 'rejected_input': cmd,\n 'code': code,\n 'message': msg,\n 'cli_error': cmd_result.get('clierror'),\n 'previous_commands': previous_commands,\n })\n elif code == '413':\n raise NxosRequestNotSupported('Error 413: {}'.format(msg))\n elif code != '200':\n raise NxosError('Unknown Error: {}, Code: {}'.format(msg, code))\n else:\n previous_commands.append(cmd)\n result.append(cmd_result['body'])\n\n return result", "response": "Parse NX - API JSON response from NX - OS device."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_modules():\n '''\n Get a list of the PowerShell modules which are potentially available to be\n imported. The intent is to mimic the functionality of ``Get-Module\n -ListAvailable | Select-Object -Expand Name``, without the delay of loading\n PowerShell to do so.\n\n Returns:\n list: A list of modules available to Powershell\n\n Example:\n\n .. code-block:: python\n\n import salt.utils.powershell\n modules = salt.utils.powershell.get_modules()\n '''\n ret = list()\n valid_extensions = ('.psd1', '.psm1', '.cdxml', '.xaml', '.dll')\n # need to create an info function to get PS information including version\n # __salt__ is not available from salt.utils... need to create a salt.util\n # for the registry to avoid loading powershell to get the version\n # not sure how to get the powershell version in linux outside of powershell\n # if running powershell to get version need to use subprocess.Popen\n # That information will be loaded here\n # ps_version = info()['version_raw']\n root_paths = []\n\n home_dir = os.environ.get('HOME', os.environ.get('HOMEPATH'))\n system_dir = '{0}\\\\System32'.format(os.environ.get('WINDIR', 'C:\\\\Windows'))\n program_files = os.environ.get('ProgramFiles', 'C:\\\\Program Files')\n default_paths = [\n '{0}/.local/share/powershell/Modules'.format(home_dir),\n # Once version is available, these can be enabled\n # '/opt/microsoft/powershell/{0}/Modules'.format(ps_version),\n # '/usr/local/microsoft/powershell/{0}/Modules'.format(ps_version),\n '/usr/local/share/powershell/Modules',\n '{0}\\\\WindowsPowerShell\\\\v1.0\\\\Modules\\\\'.format(system_dir),\n '{0}\\\\WindowsPowerShell\\\\Modules'.format(program_files)]\n default_paths = ';'.join(default_paths)\n\n ps_module_path = os.environ.get('PSModulePath', default_paths)\n\n # Check if defaults exist, add them if they do\n ps_module_path = ps_module_path.split(';')\n for item in ps_module_path:\n if os.path.exists(item):\n root_paths.append(item)\n\n # Did we find any, if not log the error and return\n if not root_paths:\n log.error('Default paths not found')\n return ret\n\n for root_path in root_paths:\n\n # only recurse directories\n if not os.path.isdir(root_path):\n continue\n\n # get a list of all files in the root_path\n for root_dir, sub_dirs, file_names in salt.utils.path.os_walk(root_path):\n for file_name in file_names:\n base_name, file_extension = os.path.splitext(file_name)\n\n # If a module file or module manifest is present, check if\n # the base name matches the directory name.\n\n if file_extension.lower() in valid_extensions:\n dir_name = os.path.basename(os.path.normpath(root_dir))\n\n # Stop recursion once we find a match, and use\n # the capitalization from the directory name.\n if dir_name not in ret and \\\n base_name.lower() == dir_name.lower():\n del sub_dirs[:]\n ret.append(dir_name)\n\n return ret", "response": "Get a list of the PowerShell modules that are potentially available to be\n imported."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngenerates an icinga2 client certificate and key.", "response": "def generate_cert(domain):\n '''\n Generate an icinga2 client certificate and key.\n\n Returns::\n icinga2 pki new-cert --cn domain.tld --key /etc/icinga2/pki/domain.tld.key --cert /etc/icinga2/pki/domain.tld.crt\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' icinga2.generate_cert domain.tld\n\n '''\n result = __salt__['cmd.run_all']([\"icinga2\", \"pki\", \"new-cert\", \"--cn\", domain, \"--key\", \"{0}{1}.key\".format(get_certs_path(), domain), \"--cert\", \"{0}{1}.crt\".format(get_certs_path(), domain)], python_shell=False)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsaves the certificate for master icinga2 node.", "response": "def save_cert(domain, master):\n '''\n Save the certificate for master icinga2 node.\n\n Returns::\n icinga2 pki save-cert --key /etc/icinga2/pki/domain.tld.key --cert /etc/icinga2/pki/domain.tld.crt --trustedcert /etc/icinga2/pki/trusted-master.crt --host master.domain.tld\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' icinga2.save_cert domain.tld master.domain.tld\n\n '''\n result = __salt__['cmd.run_all']([\"icinga2\", \"pki\", \"save-cert\", \"--key\", \"{0}{1}.key\".format(get_certs_path(), domain), \"--cert\", \"{0}{1}.cert\".format(get_certs_path(), domain), \"--trustedcert\",\n \"{0}trusted-master.crt\".format(get_certs_path()), \"--host\", master], python_shell=False)\n return result"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrequest CA cert from master icinga2 node.", "response": "def request_cert(domain, master, ticket, port):\n '''\n Request CA cert from master icinga2 node.\n\n Returns::\n icinga2 pki request --host master.domain.tld --port 5665 --ticket TICKET_ID --key /etc/icinga2/pki/domain.tld.key --cert /etc/icinga2/pki/domain.tld.crt --trustedcert \\\n /etc/icinga2/pki/trusted-master.crt --ca /etc/icinga2/pki/ca.crt\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' icinga2.request_cert domain.tld master.domain.tld TICKET_ID\n\n '''\n result = __salt__['cmd.run_all']([\"icinga2\", \"pki\", \"request\", \"--host\", master, \"--port\", port, \"--ticket\", ticket, \"--key\", \"{0}{1}.key\".format(get_certs_path(), domain), \"--cert\",\n \"{0}{1}.crt\".format(get_certs_path(), domain), \"--trustedcert\", \"{0}trusted-master.crt\".format(get_certs_path()), \"--ca\", \"{0}ca.crt\".format(get_certs_path())], python_shell=False)\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef node_setup(domain, master, ticket):\n '''\n Setup the icinga2 node.\n\n Returns::\n icinga2 node setup --ticket TICKET_ID --endpoint master.domain.tld --zone domain.tld --master_host master.domain.tld --trustedcert \\\n /etc/icinga2/pki/trusted-master.crt\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' icinga2.node_setup domain.tld master.domain.tld TICKET_ID\n\n '''\n result = __salt__['cmd.run_all']([\"icinga2\", \"node\", \"setup\", \"--ticket\", ticket, \"--endpoint\", master, \"--zone\", domain, \"--master_host\", master, \"--trustedcert\", \"{0}trusted-master.crt\".format(get_certs_path())],\n python_shell=False)\n return result", "response": "Setup the icinga2 node."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _handle_salt_host_resource(resource):\n '''\n Handles salt_host resources.\n See https://github.com/dmacvicar/terraform-provider-salt\n\n Returns roster attributes for the resource or None\n '''\n ret = {}\n attrs = resource.get('primary', {}).get('attributes', {})\n ret[MINION_ID] = attrs.get(MINION_ID)\n valid_attrs = set(attrs.keys()).intersection(TF_ROSTER_ATTRS.keys())\n for attr in valid_attrs:\n ret[attr] = _cast_output_to_type(attrs.get(attr), TF_ROSTER_ATTRS.get(attr))\n return ret", "response": "Handles salt_host resources.\n Returns roster attributes for the resource or None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _add_ssh_key(ret):\n '''\n Setups the salt-ssh minion to be accessed with salt-ssh default key\n '''\n priv = None\n if __opts__.get('ssh_use_home_key') and os.path.isfile(os.path.expanduser('~/.ssh/id_rsa')):\n priv = os.path.expanduser('~/.ssh/id_rsa')\n else:\n priv = __opts__.get(\n 'ssh_priv',\n os.path.abspath(os.path.join(\n __opts__['pki_dir'],\n 'ssh',\n 'salt-ssh.rsa'\n ))\n )\n if priv and os.path.isfile(priv):\n ret['priv'] = priv", "response": "Add the salt - ssh key to the minion to be accessed with salt - ssh default key\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _cast_output_to_type(value, typ):\n '''cast the value depending on the terraform type'''\n if typ == 'b':\n return bool(value)\n if typ == 'i':\n return int(value)\n return value", "response": "cast the value depending on the terraform type"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nparse the terraform state file passing different resource types to the right handler", "response": "def _parse_state_file(state_file_path='terraform.tfstate'):\n '''\n Parses the terraform state file passing different resource types to the right handler\n '''\n ret = {}\n with salt.utils.files.fopen(state_file_path, 'r') as fh_:\n tfstate = salt.utils.json.load(fh_)\n\n modules = tfstate.get('modules')\n if not modules:\n log.error('Malformed tfstate file. No modules found')\n return ret\n\n for module in modules:\n resources = module.get('resources', [])\n for resource_name, resource in salt.ext.six.iteritems(resources):\n roster_entry = None\n if resource['type'] == 'salt_host':\n roster_entry = _handle_salt_host_resource(resource)\n\n if not roster_entry:\n continue\n\n minion_id = roster_entry.get(MINION_ID, resource.get('id'))\n if not minion_id:\n continue\n\n if MINION_ID in roster_entry:\n del roster_entry[MINION_ID]\n _add_ssh_key(roster_entry)\n ret[minion_id] = roster_entry\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef targets(tgt, tgt_type='glob', **kwargs): # pylint: disable=W0613\n '''\n Returns the roster from the terraform state file, checks opts for location, but defaults to terraform.tfstate\n '''\n roster_file = os.path.abspath('terraform.tfstate')\n if __opts__.get('roster_file'):\n roster_file = os.path.abspath(__opts__['roster_file'])\n\n if not os.path.isfile(roster_file):\n log.error(\"Can't find terraform state file '%s'\", roster_file)\n return {}\n\n log.debug('terraform roster: using %s state file', roster_file)\n\n if not roster_file.endswith('.tfstate'):\n log.error(\"Terraform roster can only be used with terraform state files\")\n return {}\n\n raw = _parse_state_file(roster_file)\n log.debug('%s hosts in terraform state file', len(raw))\n return __utils__['roster_matcher.targets'](raw, tgt, tgt_type, 'ipv4')", "response": "Return a list of hosts that match the given tgt"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_timeout(name, value, power='ac', scheme=None):\n '''\n Set the sleep timeouts of specific items such as disk, monitor, etc.\n\n Args:\n\n name (str)\n The setting to change, can be one of the following:\n\n - ``monitor``\n - ``disk``\n - ``standby``\n - ``hibernate``\n\n value (int):\n The amount of time in minutes before the item will timeout\n\n power (str):\n Set the value for AC or DC power. Default is ``ac``. Valid options\n are:\n\n - ``ac`` (AC Power)\n - ``dc`` (Battery)\n\n scheme (str):\n The scheme to use, leave as ``None`` to use the current. Default is\n ``None``. This can be the GUID or the Alias for the Scheme. Known\n Aliases are:\n\n - ``SCHEME_BALANCED`` - Balanced\n - ``SCHEME_MAX`` - Power saver\n - ``SCHEME_MIN`` - High performance\n\n CLI Example:\n\n .. code-block:: yaml\n\n # Set monitor timeout to 30 minutes on Battery\n monitor:\n powercfg.set_timeout:\n - value: 30\n - power: dc\n\n # Set disk timeout to 10 minutes on AC Power\n disk:\n powercfg.set_timeout:\n - value: 10\n - power: ac\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}}\n\n # Validate name values\n name = name.lower()\n if name not in ['monitor', 'disk', 'standby', 'hibernate']:\n ret['result'] = False\n ret['comment'] = '\"{0}\" is not a valid setting'.format(name)\n log.debug(ret['comment'])\n return ret\n\n # Validate power values\n power = power.lower()\n if power not in ['ac', 'dc']:\n ret['result'] = False\n ret['comment'] = '\"{0}\" is not a power type'.format(power)\n log.debug(ret['comment'])\n return ret\n\n # Get current settings\n old = __salt__['powercfg.get_{0}_timeout'.format(name)](scheme=scheme)\n\n # Check current settings\n if old[power] == value:\n ret['comment'] = '{0} timeout on {1} power is already set to {2}' \\\n ''.format(name.capitalize(), power.upper(), value)\n return ret\n else:\n ret['comment'] = '{0} timeout on {1} power will be set to {2}' \\\n ''.format(name.capitalize(), power.upper(), value)\n\n # Check for test=True\n if __opts__['test']:\n ret['result'] = None\n return ret\n\n # Set the timeout value\n __salt__['powercfg.set_{0}_timeout'.format(name)](\n timeout=value,\n power=power,\n scheme=scheme)\n\n # Get the setting after the change\n new = __salt__['powercfg.get_{0}_timeout'.format(name)](scheme=scheme)\n\n changes = salt.utils.data.compare_dicts(old, new)\n\n if changes:\n ret['changes'] = {name: changes}\n ret['comment'] = '{0} timeout on {1} power set to {2}' \\\n ''.format(name.capitalize(), power.upper(), value)\n log.debug(ret['comment'])\n else:\n ret['changes'] = {}\n ret['comment'] = 'Failed to set {0} timeout on {1} power to {2}' \\\n ''.format(name, power.upper(), value)\n log.debug(ret['comment'])\n ret['result'] = False\n\n return ret", "response": "Set the sleep timeouts of specific items such as monitor disk or dc."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nresolve the master_ip and master_uri options and returns the master_ip and master_uri", "response": "def resolve_dns(opts, fallback=True):\n '''\n Resolves the master_ip and master_uri options\n '''\n ret = {}\n check_dns = True\n if (opts.get('file_client', 'remote') == 'local' and\n not opts.get('use_master_when_local', False)):\n check_dns = False\n # Since salt.log is imported below, salt.utils.network needs to be imported here as well\n import salt.utils.network\n\n if check_dns is True:\n try:\n if opts['master'] == '':\n raise SaltSystemExit\n ret['master_ip'] = salt.utils.network.dns_check(\n opts['master'],\n int(opts['master_port']),\n True,\n opts['ipv6'],\n attempt_connect=False)\n except SaltClientError:\n retry_dns_count = opts.get('retry_dns_count', None)\n if opts['retry_dns']:\n while True:\n if retry_dns_count is not None:\n if retry_dns_count == 0:\n raise SaltMasterUnresolvableError\n retry_dns_count -= 1\n import salt.log\n msg = ('Master hostname: \\'{0}\\' not found or not responsive. '\n 'Retrying in {1} seconds').format(opts['master'], opts['retry_dns'])\n if salt.log.setup.is_console_configured():\n log.error(msg)\n else:\n print('WARNING: {0}'.format(msg))\n time.sleep(opts['retry_dns'])\n try:\n ret['master_ip'] = salt.utils.network.dns_check(\n opts['master'],\n int(opts['master_port']),\n True,\n opts['ipv6'],\n attempt_connect=False)\n break\n except SaltClientError:\n pass\n else:\n if fallback:\n ret['master_ip'] = '127.0.0.1'\n else:\n raise\n except SaltSystemExit:\n unknown_str = 'unknown address'\n master = opts.get('master', unknown_str)\n if master == '':\n master = unknown_str\n if opts.get('__role') == 'syndic':\n err = 'Master address: \\'{0}\\' could not be resolved. Invalid or unresolveable address. ' \\\n 'Set \\'syndic_master\\' value in minion config.'.format(master)\n else:\n err = 'Master address: \\'{0}\\' could not be resolved. Invalid or unresolveable address. ' \\\n 'Set \\'master\\' value in minion config.'.format(master)\n log.error(err)\n raise SaltSystemExit(code=42, msg=err)\n else:\n ret['master_ip'] = '127.0.0.1'\n\n if 'master_ip' in ret and 'master_ip' in opts:\n if ret['master_ip'] != opts['master_ip']:\n log.warning(\n 'Master ip address changed from %s to %s',\n opts['master_ip'], ret['master_ip']\n )\n if opts['source_interface_name']:\n log.trace('Custom source interface required: %s', opts['source_interface_name'])\n interfaces = salt.utils.network.interfaces()\n log.trace('The following interfaces are available on this Minion:')\n log.trace(interfaces)\n if opts['source_interface_name'] in interfaces:\n if interfaces[opts['source_interface_name']]['up']:\n addrs = interfaces[opts['source_interface_name']]['inet'] if not opts['ipv6'] else\\\n interfaces[opts['source_interface_name']]['inet6']\n ret['source_ip'] = addrs[0]['address']\n log.debug('Using %s as source IP address', ret['source_ip'])\n else:\n log.warning('The interface %s is down so it cannot be used as source to connect to the Master',\n opts['source_interface_name'])\n else:\n log.warning('%s is not a valid interface. Ignoring.', opts['source_interface_name'])\n elif opts['source_address']:\n ret['source_ip'] = salt.utils.network.dns_check(\n opts['source_address'],\n int(opts['source_ret_port']),\n True,\n opts['ipv6'],\n attempt_connect=False)\n log.debug('Using %s as source IP address', ret['source_ip'])\n if opts['source_ret_port']:\n ret['source_ret_port'] = int(opts['source_ret_port'])\n log.debug('Using %d as source port for the ret server', ret['source_ret_port'])\n if opts['source_publish_port']:\n ret['source_publish_port'] = int(opts['source_publish_port'])\n log.debug('Using %d as source port for the master pub', ret['source_publish_port'])\n ret['master_uri'] = 'tcp://{ip}:{port}'.format(\n ip=ret['master_ip'], port=opts['master_port'])\n log.debug('Master URI: %s', ret['master_uri'])\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef prep_ip_port(opts):\n '''\n parse host:port values from opts['master'] and return valid:\n master: ip address or hostname as a string\n master_port: (optional) master returner port as integer\n\n e.g.:\n - master: 'localhost:1234' -> {'master': 'localhost', 'master_port': 1234}\n - master: '127.0.0.1:1234' -> {'master': '127.0.0.1', 'master_port' :1234}\n - master: '[::1]:1234' -> {'master': '::1', 'master_port': 1234}\n - master: 'fe80::a00:27ff:fedc:ba98' -> {'master': 'fe80::a00:27ff:fedc:ba98'}\n '''\n ret = {}\n # Use given master IP if \"ip_only\" is set or if master_ip is an ipv6 address without\n # a port specified. The is_ipv6 check returns False if brackets are used in the IP\n # definition such as master: '[::1]:1234'.\n if opts['master_uri_format'] == 'ip_only':\n ret['master'] = ipaddress.ip_address(opts['master'])\n else:\n host, port = parse_host_port(opts['master'])\n ret = {'master': host}\n if port:\n ret.update({'master_port': port})\n\n return ret", "response": "Prepare the IP and port values for the master_ip option."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_proc_dir(cachedir, **kwargs):\n '''\n Given the cache directory, return the directory that process data is\n stored in, creating it if it doesn't exist.\n The following optional Keyword Arguments are handled:\n\n mode: which is anything os.makedir would accept as mode.\n\n uid: the uid to set, if not set, or it is None or -1 no changes are\n made. Same applies if the directory is already owned by this\n uid. Must be int. Works only on unix/unix like systems.\n\n gid: the gid to set, if not set, or it is None or -1 no changes are\n made. Same applies if the directory is already owned by this\n gid. Must be int. Works only on unix/unix like systems.\n '''\n fn_ = os.path.join(cachedir, 'proc')\n mode = kwargs.pop('mode', None)\n\n if mode is None:\n mode = {}\n else:\n mode = {'mode': mode}\n\n if not os.path.isdir(fn_):\n # proc_dir is not present, create it with mode settings\n os.makedirs(fn_, **mode)\n\n d_stat = os.stat(fn_)\n\n # if mode is not an empty dict then we have an explicit\n # dir mode. So lets check if mode needs to be changed.\n if mode:\n mode_part = S_IMODE(d_stat.st_mode)\n if mode_part != mode['mode']:\n os.chmod(fn_, (d_stat.st_mode ^ mode_part) | mode['mode'])\n\n if hasattr(os, 'chown'):\n # only on unix/unix like systems\n uid = kwargs.pop('uid', -1)\n gid = kwargs.pop('gid', -1)\n\n # if uid and gid are both -1 then go ahead with\n # no changes at all\n if (d_stat.st_uid != uid or d_stat.st_gid != gid) and \\\n [i for i in (uid, gid) if i != -1]:\n os.chown(fn_, uid, gid)\n\n return fn_", "response": "Given the cache directory return the directory that process data is is\n stored in creating it if it doesn t exist."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nloads the arguments and kwargs from the passed arguments and data into the internal state of the object.", "response": "def load_args_and_kwargs(func, args, data=None, ignore_invalid=False):\n '''\n Detect the args and kwargs that need to be passed to a function call, and\n check them against what was passed.\n '''\n argspec = salt.utils.args.get_function_argspec(func)\n _args = []\n _kwargs = {}\n invalid_kwargs = []\n\n for arg in args:\n if isinstance(arg, dict) and arg.pop('__kwarg__', False) is True:\n # if the arg is a dict with __kwarg__ == True, then its a kwarg\n for key, val in six.iteritems(arg):\n if argspec.keywords or key in argspec.args:\n # Function supports **kwargs or is a positional argument to\n # the function.\n _kwargs[key] = val\n else:\n # **kwargs not in argspec and parsed argument name not in\n # list of positional arguments. This keyword argument is\n # invalid.\n invalid_kwargs.append('{0}={1}'.format(key, val))\n continue\n\n else:\n string_kwarg = salt.utils.args.parse_input([arg], condition=False)[1] # pylint: disable=W0632\n if string_kwarg:\n if argspec.keywords or next(six.iterkeys(string_kwarg)) in argspec.args:\n # Function supports **kwargs or is a positional argument to\n # the function.\n _kwargs.update(string_kwarg)\n else:\n # **kwargs not in argspec and parsed argument name not in\n # list of positional arguments. This keyword argument is\n # invalid.\n for key, val in six.iteritems(string_kwarg):\n invalid_kwargs.append('{0}={1}'.format(key, val))\n else:\n _args.append(arg)\n\n if invalid_kwargs and not ignore_invalid:\n salt.utils.args.invalid_kwargs(invalid_kwargs)\n\n if argspec.keywords and isinstance(data, dict):\n # this function accepts **kwargs, pack in the publish data\n for key, val in six.iteritems(data):\n _kwargs['__pub_{0}'.format(key)] = val\n\n return _args, _kwargs"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef eval_master_func(opts):\n '''\n Evaluate master function if master type is 'func'\n and save it result in opts['master']\n '''\n if '__master_func_evaluated' not in opts:\n # split module and function and try loading the module\n mod_fun = opts['master']\n mod, fun = mod_fun.split('.')\n try:\n master_mod = salt.loader.raw_mod(opts, mod, fun)\n if not master_mod:\n raise KeyError\n # we take whatever the module returns as master address\n opts['master'] = master_mod[mod_fun]()\n # Check for valid types\n if not isinstance(opts['master'], (six.string_types, list)):\n raise TypeError\n opts['__master_func_evaluated'] = True\n except KeyError:\n log.error('Failed to load module %s', mod_fun)\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)\n except TypeError:\n log.error('%s returned from %s is not a string', opts['master'], mod_fun)\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)\n log.info('Evaluated master from module: %s', mod_fun)", "response": "Evaluate master function and save it result in opts['master']"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef master_event(type, master=None):\n '''\n Centralized master event function which will return event type based on event_map\n '''\n event_map = {'connected': '__master_connected',\n 'disconnected': '__master_disconnected',\n 'failback': '__master_failback',\n 'alive': '__master_alive'}\n\n if type == 'alive' and master is not None:\n return '{0}_{1}'.format(event_map.get(type), master)\n\n return event_map.get(type, None)", "response": "Returns the master event type based on event_map\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef process_beacons(self, functions):\n '''\n Evaluate all of the configured beacons, grab the config again in case\n the pillar or grains changed\n '''\n if 'config.merge' in functions:\n b_conf = functions['config.merge']('beacons', self.opts['beacons'], omit_opts=True)\n if b_conf:\n return self.beacons.process(b_conf, self.opts['grains']) # pylint: disable=no-member\n return []", "response": "Evaluate all of the configured beacons and process them"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef eval_master(self,\n opts,\n timeout=60,\n safe=True,\n failed=False,\n failback=False):\n '''\n Evaluates and returns a tuple of the current master address and the pub_channel.\n\n In standard mode, just creates a pub_channel with the given master address.\n\n With master_type=func evaluates the current master address from the given\n module and then creates a pub_channel.\n\n With master_type=failover takes the list of masters and loops through them.\n The first one that allows the minion to create a pub_channel is then\n returned. If this function is called outside the minions initialization\n phase (for example from the minions main event-loop when a master connection\n loss was detected), 'failed' should be set to True. The current\n (possibly failed) master will then be removed from the list of masters.\n '''\n # return early if we are not connecting to a master\n if opts['master_type'] == 'disable':\n log.warning('Master is set to disable, skipping connection')\n self.connected = False\n raise tornado.gen.Return((None, None))\n\n # Run masters discovery over SSDP. This may modify the whole configuration,\n # depending of the networking and sets of masters.\n # if we are using multimaster, discovery can only happen at start time\n # because MinionManager handles it. by eval_master time the minion doesn't\n # know about other siblings currently running\n if isinstance(self.opts['discovery'], dict) and not self.opts['discovery'].get('multimaster'):\n self._discover_masters()\n\n # check if master_type was altered from its default\n if opts['master_type'] != 'str' and opts['__role'] != 'syndic':\n # check for a valid keyword\n if opts['master_type'] == 'func':\n eval_master_func(opts)\n\n # if failover or distributed is set, master has to be of type list\n elif opts['master_type'] in ('failover', 'distributed'):\n if isinstance(opts['master'], list):\n log.info(\n 'Got list of available master addresses: %s',\n opts['master']\n )\n\n if opts['master_type'] == 'distributed':\n master_len = len(opts['master'])\n if master_len > 1:\n secondary_masters = opts['master'][1:]\n master_idx = crc32(opts['id']) % master_len\n try:\n preferred_masters = opts['master']\n preferred_masters[0] = opts['master'][master_idx]\n preferred_masters[1:] = [m for m in opts['master'] if m != preferred_masters[0]]\n opts['master'] = preferred_masters\n log.info('Distributed to the master at \\'%s\\'.', opts['master'][0])\n except (KeyError, AttributeError, TypeError):\n log.warning('Failed to distribute to a specific master.')\n else:\n log.warning('master_type = distributed needs more than 1 master.')\n\n if opts['master_shuffle']:\n log.warning(\n 'Use of \\'master_shuffle\\' detected. \\'master_shuffle\\' is deprecated in favor '\n 'of \\'random_master\\'. Please update your minion config file.'\n )\n opts['random_master'] = opts['master_shuffle']\n\n opts['auth_tries'] = 0\n if opts['master_failback'] and opts['master_failback_interval'] == 0:\n opts['master_failback_interval'] = opts['master_alive_interval']\n # if opts['master'] is a str and we have never created opts['master_list']\n elif isinstance(opts['master'], six.string_types) and ('master_list' not in opts):\n # We have a string, but a list was what was intended. Convert.\n # See issue 23611 for details\n opts['master'] = [opts['master']]\n elif opts['__role'] == 'syndic':\n log.info('Syndic setting master_syndic to \\'%s\\'', opts['master'])\n\n # if failed=True, the minion was previously connected\n # we're probably called from the minions main-event-loop\n # because a master connection loss was detected. remove\n # the possibly failed master from the list of masters.\n elif failed:\n if failback:\n # failback list of masters to original config\n opts['master'] = opts['master_list']\n else:\n log.info(\n 'Moving possibly failed master %s to the end of '\n 'the list of masters', opts['master']\n )\n if opts['master'] in opts['local_masters']:\n # create new list of master with the possibly failed\n # one moved to the end\n failed_master = opts['master']\n opts['master'] = [x for x in opts['local_masters'] if opts['master'] != x]\n opts['master'].append(failed_master)\n else:\n opts['master'] = opts['master_list']\n else:\n msg = ('master_type set to \\'failover\\' but \\'master\\' '\n 'is not of type list but of type '\n '{0}'.format(type(opts['master'])))\n log.error(msg)\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)\n # If failover is set, minion have to failover on DNS errors instead of retry DNS resolve.\n # See issue 21082 for details\n if opts['retry_dns'] and opts['master_type'] == 'failover':\n msg = ('\\'master_type\\' set to \\'failover\\' but \\'retry_dns\\' is not 0. '\n 'Setting \\'retry_dns\\' to 0 to failover to the next master on DNS errors.')\n log.critical(msg)\n opts['retry_dns'] = 0\n else:\n msg = ('Invalid keyword \\'{0}\\' for variable '\n '\\'master_type\\''.format(opts['master_type']))\n log.error(msg)\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)\n\n # FIXME: if SMinion don't define io_loop, it can't switch master see #29088\n # Specify kwargs for the channel factory so that SMinion doesn't need to define an io_loop\n # (The channel factories will set a default if the kwarg isn't passed)\n factory_kwargs = {'timeout': timeout, 'safe': safe}\n if getattr(self, 'io_loop', None):\n factory_kwargs['io_loop'] = self.io_loop # pylint: disable=no-member\n\n tries = opts.get('master_tries', 1)\n attempts = 0\n\n # if we have a list of masters, loop through them and be\n # happy with the first one that allows us to connect\n if isinstance(opts['master'], list):\n conn = False\n last_exc = None\n opts['master_uri_list'] = []\n opts['local_masters'] = copy.copy(opts['master'])\n\n # shuffle the masters and then loop through them\n if opts['random_master']:\n # master_failback is only used when master_type is set to failover\n if opts['master_type'] == 'failover' and opts['master_failback']:\n secondary_masters = opts['local_masters'][1:]\n shuffle(secondary_masters)\n opts['local_masters'][1:] = secondary_masters\n else:\n shuffle(opts['local_masters'])\n\n # This sits outside of the connection loop below because it needs to set\n # up a list of master URIs regardless of which masters are available\n # to connect _to_. This is primarily used for masterless mode, when\n # we need a list of master URIs to fire calls back to.\n for master in opts['local_masters']:\n opts['master'] = master\n opts.update(prep_ip_port(opts))\n opts['master_uri_list'].append(resolve_dns(opts)['master_uri'])\n\n while True:\n if attempts != 0:\n # Give up a little time between connection attempts\n # to allow the IOLoop to run any other scheduled tasks.\n yield tornado.gen.sleep(opts['acceptance_wait_time'])\n attempts += 1\n if tries > 0:\n log.debug(\n 'Connecting to master. Attempt %s of %s',\n attempts, tries\n )\n else:\n log.debug(\n 'Connecting to master. Attempt %s (infinite attempts)',\n attempts\n )\n for master in opts['local_masters']:\n opts['master'] = master\n opts.update(prep_ip_port(opts))\n opts.update(resolve_dns(opts))\n\n # on first run, update self.opts with the whole master list\n # to enable a minion to re-use old masters if they get fixed\n if 'master_list' not in opts:\n opts['master_list'] = copy.copy(opts['local_masters'])\n\n self.opts = opts\n\n pub_channel = salt.transport.client.AsyncPubChannel.factory(opts, **factory_kwargs)\n try:\n yield pub_channel.connect()\n conn = True\n break\n except SaltClientError as exc:\n last_exc = exc\n if exc.strerror.startswith('Could not access'):\n msg = (\n 'Failed to initiate connection with Master '\n '%s: check ownership/permissions. Error '\n 'message: %s', opts['master'], exc\n )\n else:\n msg = ('Master %s could not be reached, trying next '\n 'next master (if any)', opts['master'])\n log.info(msg)\n continue\n\n if not conn:\n if attempts == tries:\n # Exhausted all attempts. Return exception.\n self.connected = False\n self.opts['master'] = copy.copy(self.opts['local_masters'])\n log.error(\n 'No master could be reached or all masters '\n 'denied the minion\\'s connection attempt.'\n )\n # If the code reaches this point, 'last_exc'\n # should already be set.\n raise last_exc # pylint: disable=E0702\n else:\n self.tok = pub_channel.auth.gen_token(b'salt')\n self.connected = True\n raise tornado.gen.Return((opts['master'], pub_channel))\n\n # single master sign in\n else:\n if opts['random_master']:\n log.warning('random_master is True but there is only one master specified. Ignoring.')\n while True:\n if attempts != 0:\n # Give up a little time between connection attempts\n # to allow the IOLoop to run any other scheduled tasks.\n yield tornado.gen.sleep(opts['acceptance_wait_time'])\n attempts += 1\n if tries > 0:\n log.debug(\n 'Connecting to master. Attempt %s of %s',\n attempts, tries\n )\n else:\n log.debug(\n 'Connecting to master. Attempt %s (infinite attempts)',\n attempts\n )\n opts.update(prep_ip_port(opts))\n opts.update(resolve_dns(opts))\n try:\n if self.opts['transport'] == 'detect':\n self.opts['detect_mode'] = True\n for trans in ('zeromq', 'tcp'):\n if trans == 'zeromq' and not zmq:\n continue\n self.opts['transport'] = trans\n pub_channel = salt.transport.client.AsyncPubChannel.factory(self.opts, **factory_kwargs)\n yield pub_channel.connect()\n if not pub_channel.auth.authenticated:\n continue\n del self.opts['detect_mode']\n break\n else:\n pub_channel = salt.transport.client.AsyncPubChannel.factory(self.opts, **factory_kwargs)\n yield pub_channel.connect()\n self.tok = pub_channel.auth.gen_token(b'salt')\n self.connected = True\n raise tornado.gen.Return((opts['master'], pub_channel))\n except SaltClientError as exc:\n if attempts == tries:\n # Exhausted all attempts. Return exception.\n self.connected = False\n raise exc", "response": "Evaluate the current master address and create a pub_channel with the given master address."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndiscovers master and decide where to connect to", "response": "def _discover_masters(self):\n '''\n Discover master(s) and decide where to connect, if SSDP is around.\n This modifies the configuration on the fly.\n :return:\n '''\n if self.opts['master'] == DEFAULT_MINION_OPTS['master'] and self.opts['discovery'] is not False:\n master_discovery_client = salt.utils.ssdp.SSDPDiscoveryClient()\n masters = {}\n for att in range(self.opts['discovery'].get('attempts', 3)):\n try:\n att += 1\n log.info('Attempting %s time(s) to discover masters', att)\n masters.update(master_discovery_client.discover())\n if not masters:\n time.sleep(self.opts['discovery'].get('pause', 5))\n else:\n break\n except Exception as err:\n log.error('SSDP discovery failure: %s', err)\n break\n\n if masters:\n policy = self.opts.get('discovery', {}).get('match', 'any')\n if policy not in ['any', 'all']:\n log.error('SSDP configuration matcher failure: unknown value \"%s\". '\n 'Should be \"any\" or \"all\"', policy)\n return\n mapping = self.opts['discovery'].get('mapping', {})\n discovered = []\n for addr, mappings in masters.items():\n for proto_data in mappings:\n cnt = len([key for key, value in mapping.items()\n if proto_data.get('mapping', {}).get(key) == value])\n if policy == 'any' and bool(cnt) or cnt == len(mapping):\n if self.opts['discovery'].get('multimaster'):\n discovered.append(proto_data['master'])\n else:\n self.opts['master'] = proto_data['master']\n return\n self.opts['master'] = discovered"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _return_retry_timer(self):\n '''\n Based on the minion configuration, either return a randomized timer or\n just return the value of the return_retry_timer.\n '''\n msg = 'Minion return retry timer set to %s seconds'\n if self.opts.get('return_retry_timer_max'):\n try:\n random_retry = randint(self.opts['return_retry_timer'], self.opts['return_retry_timer_max'])\n retry_msg = msg % random_retry\n log.debug('%s (randomized)', msg % random_retry)\n return random_retry\n except ValueError:\n # Catch wiseguys using negative integers here\n log.error(\n 'Invalid value (return_retry_timer: %s or '\n 'return_retry_timer_max: %s). Both must be positive '\n 'integers.',\n self.opts['return_retry_timer'],\n self.opts['return_retry_timer_max'],\n )\n log.debug(msg, DEFAULT_MINION_OPTS['return_retry_timer'])\n return DEFAULT_MINION_OPTS['return_retry_timer']\n else:\n log.debug(msg, self.opts.get('return_retry_timer'))\n return self.opts.get('return_retry_timer')", "response": "Return the value of the return_retry_timer option."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef gen_modules(self, initial_load=False):\n '''\n Tell the minion to reload the execution modules\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.reload_modules\n '''\n self.opts['pillar'] = salt.pillar.get_pillar(\n self.opts,\n self.opts['grains'],\n self.opts['id'],\n self.opts['saltenv'],\n pillarenv=self.opts.get('pillarenv'),\n ).compile_pillar()\n\n self.utils = salt.loader.utils(self.opts)\n self.functions = salt.loader.minion_mods(self.opts, utils=self.utils)\n self.serializers = salt.loader.serializers(self.opts)\n self.returners = salt.loader.returners(self.opts, self.functions)\n self.proxy = salt.loader.proxy(self.opts, self.functions, self.returners, None)\n # TODO: remove\n self.function_errors = {} # Keep the funcs clean\n self.states = salt.loader.states(self.opts,\n self.functions,\n self.utils,\n self.serializers)\n self.rend = salt.loader.render(self.opts, self.functions)\n# self.matcher = Matcher(self.opts, self.functions)\n self.matchers = salt.loader.matchers(self.opts)\n self.functions['sys.reload_modules'] = self.gen_modules\n self.executors = salt.loader.executors(self.opts)", "response": "Generate the modules for the current minion"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef gen_modules(self, initial_load=False):\n '''\n Tell the minion to reload the execution modules\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.reload_modules\n '''\n self.utils = salt.loader.utils(self.opts)\n self.functions = salt.loader.minion_mods(\n self.opts,\n utils=self.utils,\n whitelist=self.whitelist,\n initial_load=initial_load)\n self.serializers = salt.loader.serializers(self.opts)\n if self.mk_returners:\n self.returners = salt.loader.returners(self.opts, self.functions)\n if self.mk_states:\n self.states = salt.loader.states(self.opts,\n self.functions,\n self.utils,\n self.serializers)\n if self.mk_rend:\n self.rend = salt.loader.render(self.opts, self.functions)\n if self.mk_matcher:\n self.matchers = salt.loader.matchers(self.opts)\n self.functions['sys.reload_modules'] = self.gen_modules", "response": "Generate the modules for the current minion"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _create_minion_object(self, opts, timeout, safe,\n io_loop=None, loaded_base_name=None,\n jid_queue=None):\n '''\n Helper function to return the correct type of object\n '''\n return Minion(opts,\n timeout,\n safe,\n io_loop=io_loop,\n loaded_base_name=loaded_base_name,\n jid_queue=jid_queue)", "response": "Helper function to create the correct type of object."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nspawns all the coroutines which will sign in to masters", "response": "def _spawn_minions(self, timeout=60):\n '''\n Spawn all the coroutines which will sign in to masters\n '''\n # Run masters discovery over SSDP. This may modify the whole configuration,\n # depending of the networking and sets of masters. If match is 'any' we let\n # eval_master handle the discovery instead so disconnections can also handle\n # discovery\n if isinstance(self.opts['discovery'], dict) and self.opts['discovery'].get('multimaster'):\n self._discover_masters()\n\n masters = self.opts['master']\n if (self.opts['master_type'] in ('failover', 'distributed')) or not isinstance(self.opts['master'], list):\n masters = [masters]\n\n for master in masters:\n s_opts = copy.deepcopy(self.opts)\n s_opts['master'] = master\n s_opts['multimaster'] = True\n minion = self._create_minion_object(s_opts,\n s_opts['auth_timeout'],\n False,\n io_loop=self.io_loop,\n loaded_base_name='salt.loader.{0}'.format(s_opts['master']),\n jid_queue=self.jid_queue)\n self.io_loop.spawn_callback(self._connect_minion, minion)\n self.io_loop.call_later(timeout, self._check_minions)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconnects a minion to a master", "response": "def _connect_minion(self, minion):\n '''\n Create a minion, and asynchronously connect it to a master\n '''\n auth_wait = minion.opts['acceptance_wait_time']\n failed = False\n while True:\n if failed:\n if auth_wait < self.max_auth_wait:\n auth_wait += self.auth_wait\n log.debug(\n \"sleeping before reconnect attempt to %s [%d/%d]\",\n minion.opts['master'],\n auth_wait,\n self.max_auth_wait,\n )\n yield tornado.gen.sleep(auth_wait) # TODO: log?\n try:\n if minion.opts.get('beacons_before_connect', False):\n minion.setup_beacons(before_connect=True)\n if minion.opts.get('scheduler_before_connect', False):\n minion.setup_scheduler(before_connect=True)\n yield minion.connect_master(failed=failed)\n minion.tune_in(start=False)\n self.minions.append(minion)\n break\n except SaltClientError as exc:\n failed = True\n log.error(\n 'Error while bringing up minion for multi-master. Is '\n 'master at %s responding?', minion.opts['master']\n )\n except SaltMasterUnresolvableError:\n err = 'Master address: \\'{0}\\' could not be resolved. Invalid or unresolveable address. ' \\\n 'Set \\'master\\' value in minion config.'.format(minion.opts['master'])\n log.error(err)\n break\n except Exception as e:\n failed = True\n log.critical(\n 'Unexpected error while connecting to %s',\n minion.opts['master'], exc_info=True\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef sync_connect_master(self, timeout=None, failed=False):\n '''\n Block until we are connected to a master\n '''\n self._sync_connect_master_success = False\n log.debug(\"sync_connect_master\")\n\n def on_connect_master_future_done(future):\n self._sync_connect_master_success = True\n self.io_loop.stop()\n\n self._connect_master_future = self.connect_master(failed=failed)\n # finish connecting to master\n self._connect_master_future.add_done_callback(on_connect_master_future_done)\n if timeout:\n self.io_loop.call_later(timeout, self.io_loop.stop)\n try:\n self.io_loop.start()\n except KeyboardInterrupt:\n self.destroy()\n # I made the following 3 line oddity to preserve traceback.\n # Please read PR #23978 before changing, hopefully avoiding regressions.\n # Good luck, we're all counting on you. Thanks.\n if self._connect_master_future.done():\n future_exception = self._connect_master_future.exception()\n if future_exception:\n # This needs to be re-raised to preserve restart_on_error behavior.\n raise six.reraise(*future_exception)\n if timeout and self._sync_connect_master_success is False:\n raise SaltDaemonNotRunning('Failed to connect to the salt-master')", "response": "Connect to the master and wait for the connection to complete."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a future which will complete when you are connected to a master", "response": "def connect_master(self, failed=False):\n '''\n Return a future which will complete when you are connected to a master\n '''\n master, self.pub_channel = yield self.eval_master(self.opts, self.timeout, self.safe, failed)\n yield self._post_master_init(master)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nfunctions to finish init after connecting to a master", "response": "def _post_master_init(self, master):\n '''\n Function to finish init after connecting to a master\n\n This is primarily loading modules, pillars, etc. (since they need\n to know which master they connected to)\n\n If this function is changed, please check ProxyMinion._post_master_init\n to see if those changes need to be propagated.\n\n Minions and ProxyMinions need significantly different post master setups,\n which is why the differences are not factored out into separate helper\n functions.\n '''\n if self.connected:\n self.opts['master'] = master\n\n # Initialize pillar before loader to make pillar accessible in modules\n async_pillar = salt.pillar.get_async_pillar(\n self.opts,\n self.opts['grains'],\n self.opts['id'],\n self.opts['saltenv'],\n pillarenv=self.opts.get('pillarenv')\n )\n self.opts['pillar'] = yield async_pillar.compile_pillar()\n async_pillar.destroy()\n\n if not self.ready:\n self._setup_core()\n elif self.connected and self.opts['pillar']:\n # The pillar has changed due to the connection to the master.\n # Reload the functions so that they can use the new pillar data.\n self.functions, self.returners, self.function_errors, self.executors = self._load_modules()\n if hasattr(self, 'schedule'):\n self.schedule.functions = self.functions\n self.schedule.returners = self.returners\n\n if not hasattr(self, 'schedule'):\n self.schedule = salt.utils.schedule.Schedule(\n self.opts,\n self.functions,\n self.returners,\n cleanup=[master_event(type='alive')])\n\n # add default scheduling jobs to the minions scheduler\n if self.opts['mine_enabled'] and 'mine.update' in self.functions:\n self.schedule.add_job({\n '__mine_interval':\n {\n 'function': 'mine.update',\n 'minutes': self.opts['mine_interval'],\n 'jid_include': True,\n 'maxrunning': 2,\n 'run_on_start': True,\n 'return_job': self.opts.get('mine_return_job', False)\n }\n }, persist=True)\n log.info('Added mine.update to scheduler')\n else:\n self.schedule.delete_job('__mine_interval', persist=True)\n\n # add master_alive job if enabled\n if (self.opts['transport'] != 'tcp' and\n self.opts['master_alive_interval'] > 0 and\n self.connected):\n self.schedule.add_job({\n master_event(type='alive', master=self.opts['master']):\n {\n 'function': 'status.master',\n 'seconds': self.opts['master_alive_interval'],\n 'jid_include': True,\n 'maxrunning': 1,\n 'return_job': False,\n 'kwargs': {'master': self.opts['master'],\n 'connected': True}\n }\n }, persist=True)\n if self.opts['master_failback'] and \\\n 'master_list' in self.opts and \\\n self.opts['master'] != self.opts['master_list'][0]:\n self.schedule.add_job({\n master_event(type='failback'):\n {\n 'function': 'status.ping_master',\n 'seconds': self.opts['master_failback_interval'],\n 'jid_include': True,\n 'maxrunning': 1,\n 'return_job': False,\n 'kwargs': {'master': self.opts['master_list'][0]}\n }\n }, persist=True)\n else:\n self.schedule.delete_job(master_event(type='failback'), persist=True)\n else:\n self.schedule.delete_job(master_event(type='alive', master=self.opts['master']), persist=True)\n self.schedule.delete_job(master_event(type='failback'), persist=True)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a copy of the opts with key bits stripped out", "response": "def _prep_mod_opts(self):\n '''\n Returns a copy of the opts with key bits stripped out\n '''\n mod_opts = {}\n for key, val in six.iteritems(self.opts):\n if key == 'logger':\n continue\n mod_opts[key] = val\n return mod_opts"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nloading the modules from the loader", "response": "def _load_modules(self, force_refresh=False, notify=False, grains=None, opts=None):\n '''\n Return the functions and the returners loaded up from the loader\n module\n '''\n opt_in = True\n if not opts:\n opts = self.opts\n opt_in = False\n # if this is a *nix system AND modules_max_memory is set, lets enforce\n # a memory limit on module imports\n # this feature ONLY works on *nix like OSs (resource module doesn't work on windows)\n modules_max_memory = False\n if opts.get('modules_max_memory', -1) > 0 and HAS_PSUTIL and HAS_RESOURCE:\n log.debug(\n 'modules_max_memory set, enforcing a maximum of %s',\n opts['modules_max_memory']\n )\n modules_max_memory = True\n old_mem_limit = resource.getrlimit(resource.RLIMIT_AS)\n rss, vms = psutil.Process(os.getpid()).memory_info()[:2]\n mem_limit = rss + vms + opts['modules_max_memory']\n resource.setrlimit(resource.RLIMIT_AS, (mem_limit, mem_limit))\n elif opts.get('modules_max_memory', -1) > 0:\n if not HAS_PSUTIL:\n log.error('Unable to enforce modules_max_memory because psutil is missing')\n if not HAS_RESOURCE:\n log.error('Unable to enforce modules_max_memory because resource is missing')\n\n # This might be a proxy minion\n if hasattr(self, 'proxy'):\n proxy = self.proxy\n else:\n proxy = None\n\n if grains is None:\n opts['grains'] = salt.loader.grains(opts, force_refresh, proxy=proxy)\n self.utils = salt.loader.utils(opts, proxy=proxy)\n\n if opts.get('multimaster', False):\n s_opts = copy.deepcopy(opts)\n functions = salt.loader.minion_mods(s_opts, utils=self.utils, proxy=proxy,\n loaded_base_name=self.loaded_base_name, notify=notify)\n else:\n functions = salt.loader.minion_mods(opts, utils=self.utils, notify=notify, proxy=proxy)\n returners = salt.loader.returners(opts, functions, proxy=proxy)\n errors = {}\n if '_errors' in functions:\n errors = functions['_errors']\n functions.pop('_errors')\n\n # we're done, reset the limits!\n if modules_max_memory is True:\n resource.setrlimit(resource.RLIMIT_AS, old_mem_limit)\n\n executors = salt.loader.executors(opts, functions, proxy=proxy)\n\n if opt_in:\n self.opts = opts\n\n return functions, returners, errors, executors"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _fire_master(self, data=None, tag=None, events=None, pretag=None, timeout=60, sync=True, timeout_handler=None):\n '''\n Fire an event on the master, or drop message if unable to send.\n '''\n load = {'id': self.opts['id'],\n 'cmd': '_minion_event',\n 'pretag': pretag,\n 'tok': self.tok}\n if events:\n load['events'] = events\n elif data and tag:\n load['data'] = data\n load['tag'] = tag\n elif not data and tag:\n load['data'] = {}\n load['tag'] = tag\n else:\n return\n\n if sync:\n try:\n self._send_req_sync(load, timeout)\n except salt.exceptions.SaltReqTimeoutError:\n log.info('fire_master failed: master could not be contacted. Request timed out.')\n # very likely one of the masters is dead, status.master will flush it\n self.functions['status.master'](self.opts['master'])\n return False\n except Exception:\n log.info('fire_master failed: %s', traceback.format_exc())\n return False\n else:\n if timeout_handler is None:\n def handle_timeout(*_):\n log.info('fire_master failed: master could not be contacted. Request timed out.')\n # very likely one of the masters is dead, status.master will flush it\n self.functions['status.master'](self.opts['master'])\n return True\n timeout_handler = handle_timeout\n\n with tornado.stack_context.ExceptionStackContext(timeout_handler):\n self._send_req_async(load, timeout, callback=lambda f: None) # pylint: disable=unexpected-keyword-arg\n return True", "response": "Fire an event on the master or drop message if unable to send it."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef ctx(self):\n '''\n Return a single context manager for the minion's data\n '''\n if six.PY2:\n return contextlib.nested(\n self.functions.context_dict.clone(),\n self.returners.context_dict.clone(),\n self.executors.context_dict.clone(),\n )\n else:\n exitstack = contextlib.ExitStack()\n exitstack.enter_context(self.functions.context_dict.clone())\n exitstack.enter_context(self.returners.context_dict.clone())\n exitstack.enter_context(self.executors.context_dict.clone())\n return exitstack", "response": "Return a single context manager for the minion s data\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _thread_return(cls, minion_instance, opts, data):\n '''\n This method should be used as a threading target, start the actual\n minion side execution.\n '''\n fn_ = os.path.join(minion_instance.proc_dir, data['jid'])\n\n if opts['multiprocessing'] and not salt.utils.platform.is_windows():\n # Shutdown the multiprocessing before daemonizing\n salt.log.setup.shutdown_multiprocessing_logging()\n\n salt.utils.process.daemonize_if(opts)\n\n # Reconfigure multiprocessing logging after daemonizing\n salt.log.setup.setup_multiprocessing_logging()\n\n salt.utils.process.appendproctitle('{0}._thread_return {1}'.format(cls.__name__, data['jid']))\n\n sdata = {'pid': os.getpid()}\n sdata.update(data)\n log.info('Starting a new job %s with PID %s', data['jid'], sdata['pid'])\n with salt.utils.files.fopen(fn_, 'w+b') as fp_:\n fp_.write(minion_instance.serial.dumps(sdata))\n ret = {'success': False}\n function_name = data['fun']\n executors = data.get('module_executors') or \\\n getattr(minion_instance, 'module_executors', []) or \\\n opts.get('module_executors', ['direct_call'])\n allow_missing_funcs = any([\n minion_instance.executors['{0}.allow_missing_func'.format(executor)](function_name)\n for executor in executors\n if '{0}.allow_missing_func' in minion_instance.executors\n ])\n if function_name in minion_instance.functions or allow_missing_funcs is True:\n try:\n minion_blackout_violation = False\n if minion_instance.connected and minion_instance.opts['pillar'].get('minion_blackout', False):\n whitelist = minion_instance.opts['pillar'].get('minion_blackout_whitelist', [])\n # this minion is blacked out. Only allow saltutil.refresh_pillar and the whitelist\n if function_name != 'saltutil.refresh_pillar' and function_name not in whitelist:\n minion_blackout_violation = True\n # use minion_blackout_whitelist from grains if it exists\n if minion_instance.opts['grains'].get('minion_blackout', False):\n whitelist = minion_instance.opts['grains'].get('minion_blackout_whitelist', [])\n if function_name != 'saltutil.refresh_pillar' and function_name 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 if function_name in minion_instance.functions:\n func = minion_instance.functions[function_name]\n args, kwargs = load_args_and_kwargs(\n func,\n data['arg'],\n data)\n else:\n # only run if function_name is not in minion_instance.functions and allow_missing_funcs is True\n func = function_name\n args, kwargs = data['arg'], data\n minion_instance.functions.pack['__context__']['retcode'] = 0\n if isinstance(executors, six.string_types):\n executors = [executors]\n elif not isinstance(executors, list) or not executors:\n raise SaltInvocationError(\"Wrong executors specification: {0}. String or non-empty list expected\".\n format(executors))\n if opts.get('sudo_user', '') and executors[-1] != 'sudo':\n executors[-1] = 'sudo' # replace the last one with sudo\n log.trace('Executors list %s', executors) # pylint: disable=no-member\n\n for name in executors:\n fname = '{0}.execute'.format(name)\n if fname not in minion_instance.executors:\n raise SaltInvocationError(\"Executor '{0}' is not available\".format(name))\n return_data = minion_instance.executors[fname](opts, data, func, args, kwargs)\n if return_data is not None:\n break\n\n if isinstance(return_data, types.GeneratorType):\n ind = 0\n iret = {}\n for single in return_data:\n if isinstance(single, dict) and isinstance(iret, dict):\n iret.update(single)\n else:\n if not iret:\n iret = []\n iret.append(single)\n tag = tagify([data['jid'], 'prog', opts['id'], six.text_type(ind)], 'job')\n event_data = {'return': single}\n minion_instance._fire_master(event_data, tag)\n ind += 1\n ret['return'] = iret\n else:\n ret['return'] = return_data\n\n retcode = minion_instance.functions.pack['__context__'].get(\n 'retcode',\n salt.defaults.exitcodes.EX_OK\n )\n if retcode == salt.defaults.exitcodes.EX_OK:\n # No nonzero retcode in __context__ dunder. Check if return\n # is a dictionary with a \"result\" or \"success\" key.\n try:\n func_result = all(return_data.get(x, True)\n for x in ('result', 'success'))\n except Exception:\n # return data is not a dict\n func_result = True\n if not func_result:\n retcode = salt.defaults.exitcodes.EX_GENERIC\n\n ret['retcode'] = retcode\n ret['success'] = retcode == salt.defaults.exitcodes.EX_OK\n except CommandNotFoundError as exc:\n msg = 'Command required for \\'{0}\\' not found'.format(\n function_name\n )\n log.debug(msg, exc_info=True)\n ret['return'] = '{0}: {1}'.format(msg, exc)\n ret['out'] = 'nested'\n ret['retcode'] = salt.defaults.exitcodes.EX_GENERIC\n except CommandExecutionError as exc:\n log.error(\n 'A command in \\'%s\\' had a problem: %s',\n function_name, exc,\n exc_info_on_loglevel=logging.DEBUG\n )\n ret['return'] = 'ERROR: {0}'.format(exc)\n ret['out'] = 'nested'\n ret['retcode'] = salt.defaults.exitcodes.EX_GENERIC\n except SaltInvocationError as exc:\n log.error(\n 'Problem executing \\'%s\\': %s',\n function_name, exc,\n exc_info_on_loglevel=logging.DEBUG\n )\n ret['return'] = 'ERROR executing \\'{0}\\': {1}'.format(\n function_name, exc\n )\n ret['out'] = 'nested'\n ret['retcode'] = salt.defaults.exitcodes.EX_GENERIC\n except TypeError as exc:\n msg = 'Passed invalid arguments to {0}: {1}\\n{2}'.format(\n function_name, exc, func.__doc__ or ''\n )\n log.warning(msg, exc_info_on_loglevel=logging.DEBUG)\n ret['return'] = msg\n ret['out'] = 'nested'\n ret['retcode'] = salt.defaults.exitcodes.EX_GENERIC\n except Exception:\n msg = 'The minion function caused an exception'\n log.warning(msg, exc_info_on_loglevel=True)\n salt.utils.error.fire_exception(salt.exceptions.MinionError(msg), opts, job=data)\n ret['return'] = '{0}: {1}'.format(msg, traceback.format_exc())\n ret['out'] = 'nested'\n ret['retcode'] = salt.defaults.exitcodes.EX_GENERIC\n else:\n docs = minion_instance.functions['sys.doc']('{0}*'.format(function_name))\n if docs:\n docs[function_name] = minion_instance.functions.missing_fun_string(function_name)\n ret['return'] = docs\n else:\n ret['return'] = minion_instance.functions.missing_fun_string(function_name)\n mod_name = function_name.split('.')[0]\n if mod_name in minion_instance.function_errors:\n ret['return'] += ' Possible reasons: \\'{0}\\''.format(\n minion_instance.function_errors[mod_name]\n )\n ret['success'] = False\n ret['retcode'] = salt.defaults.exitcodes.EX_GENERIC\n ret['out'] = 'nested'\n\n ret['jid'] = data['jid']\n ret['fun'] = data['fun']\n ret['fun_args'] = data['arg']\n if 'master_id' in data:\n ret['master_id'] = data['master_id']\n if 'metadata' in data:\n if isinstance(data['metadata'], dict):\n ret['metadata'] = data['metadata']\n else:\n log.warning('The metadata parameter must be a dictionary. Ignoring.')\n if minion_instance.connected:\n minion_instance._return_pub(\n ret,\n timeout=minion_instance._return_retry_timer()\n )\n\n # Add default returners from minion config\n # Should have been coverted to comma-delimited string already\n if isinstance(opts.get('return'), six.string_types):\n if data['ret']:\n data['ret'] = ','.join((data['ret'], opts['return']))\n else:\n data['ret'] = opts['return']\n\n log.debug('minion return: %s', ret)\n # TODO: make a list? Seems odd to split it this late :/\n if data['ret'] and isinstance(data['ret'], six.string_types):\n if 'ret_config' in data:\n ret['ret_config'] = data['ret_config']\n if 'ret_kwargs' in data:\n ret['ret_kwargs'] = data['ret_kwargs']\n ret['id'] = opts['id']\n for returner in set(data['ret'].split(',')):\n try:\n returner_str = '{0}.returner'.format(returner)\n if returner_str in minion_instance.returners:\n minion_instance.returners[returner_str](ret)\n else:\n returner_err = minion_instance.returners.missing_fun_string(returner_str)\n log.error(\n 'Returner %s could not be loaded: %s',\n returner_str, returner_err\n )\n except Exception as exc:\n log.exception(\n 'The return failed for job %s: %s', data['jid'], exc\n )", "response": "This method is used as a threading target start the actual\n minion side execution."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _return_pub_multi(self, rets, ret_cmd='_return', timeout=60, sync=True):\n '''\n Return the data from the executed command to the master server\n '''\n if not isinstance(rets, list):\n rets = [rets]\n jids = {}\n for ret in rets:\n jid = ret.get('jid', ret.get('__jid__'))\n fun = ret.get('fun', ret.get('__fun__'))\n if self.opts['multiprocessing']:\n fn_ = os.path.join(self.proc_dir, jid)\n if os.path.isfile(fn_):\n try:\n os.remove(fn_)\n except (OSError, IOError):\n # The file is gone already\n pass\n log.info('Returning information for job: %s', jid)\n load = jids.setdefault(jid, {})\n if ret_cmd == '_syndic_return':\n if not load:\n load.update({'id': self.opts['id'],\n 'jid': jid,\n 'fun': fun,\n 'arg': ret.get('arg'),\n 'tgt': ret.get('tgt'),\n 'tgt_type': ret.get('tgt_type'),\n 'load': ret.get('__load__'),\n 'return': {}})\n if '__master_id__' in ret:\n load['master_id'] = ret['__master_id__']\n for key, value in six.iteritems(ret):\n if key.startswith('__'):\n continue\n load['return'][key] = value\n else:\n load.update({'id': self.opts['id']})\n for key, value in six.iteritems(ret):\n load[key] = value\n\n if 'out' in ret:\n if isinstance(ret['out'], six.string_types):\n load['out'] = ret['out']\n else:\n log.error(\n 'Invalid outputter %s. This is likely a bug.',\n ret['out']\n )\n else:\n try:\n oput = self.functions[fun].__outputter__\n except (KeyError, AttributeError, TypeError):\n pass\n else:\n if isinstance(oput, six.string_types):\n load['out'] = oput\n if self.opts['cache_jobs']:\n # Local job cache has been enabled\n salt.utils.minion.cache_jobs(self.opts, load['jid'], ret)\n\n load = {'cmd': ret_cmd,\n 'load': list(six.itervalues(jids))}\n\n def timeout_handler(*_):\n log.warning(\n 'The minion failed to return the job information for job %s. '\n 'This is often due to the master being shut down or '\n 'overloaded. If the master is running, consider increasing '\n 'the worker_threads value.', jid\n )\n return True\n\n if sync:\n try:\n ret_val = self._send_req_sync(load, timeout=timeout)\n except SaltReqTimeoutError:\n timeout_handler()\n return ''\n else:\n with tornado.stack_context.ExceptionStackContext(timeout_handler):\n ret_val = self._send_req_async(load, timeout=timeout, callback=lambda f: None) # pylint: disable=unexpected-keyword-arg\n\n log.trace('ret_val = %s', ret_val) # pylint: disable=no-member\n return ret_val", "response": "Return the data from the executed command to the master server"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _state_run(self):\n '''\n Execute a state run based on information set in the minion config file\n '''\n if self.opts['startup_states']:\n if self.opts.get('master_type', 'str') == 'disable' and \\\n self.opts.get('file_client', 'remote') == 'remote':\n log.warning(\n 'Cannot run startup_states when \\'master_type\\' is set '\n 'to \\'disable\\' and \\'file_client\\' is set to '\n '\\'remote\\'. Skipping.'\n )\n else:\n data = {'jid': 'req', 'ret': self.opts.get('ext_job_cache', '')}\n if self.opts['startup_states'] == 'sls':\n data['fun'] = 'state.sls'\n data['arg'] = [self.opts['sls_list']]\n elif self.opts['startup_states'] == 'top':\n data['fun'] = 'state.top'\n data['arg'] = [self.opts['top_file']]\n else:\n data['fun'] = 'state.highstate'\n data['arg'] = []\n self._handle_decoded_payload(data)", "response": "Execute a state run based on information set in the minion config file"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _refresh_grains_watcher(self, refresh_interval_in_minutes):\n '''\n Create a loop that will fire a pillar refresh to inform a master about a change in the grains of this minion\n :param refresh_interval_in_minutes:\n :return: None\n '''\n if '__update_grains' not in self.opts.get('schedule', {}):\n if 'schedule' not in self.opts:\n self.opts['schedule'] = {}\n self.opts['schedule'].update({\n '__update_grains':\n {\n 'function': 'event.fire',\n 'args': [{}, 'grains_refresh'],\n 'minutes': refresh_interval_in_minutes\n }\n })", "response": "Create a loop that will fire a grains refresh to inform a master about a change in the grains of this minion"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nrefreshing the functions and returners and executors.", "response": "def module_refresh(self, force_refresh=False, notify=False):\n '''\n Refresh the functions and returners.\n '''\n log.debug('Refreshing modules. Notify=%s', notify)\n self.functions, self.returners, _, self.executors = self._load_modules(force_refresh, notify=notify)\n\n self.schedule.functions = self.functions\n self.schedule.returners = self.returners"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nrefreshing the functions and returners and beacons.", "response": "def beacons_refresh(self):\n '''\n Refresh the functions and returners.\n '''\n log.debug('Refreshing beacons.')\n self.beacons = salt.beacons.Beacon(self.opts, self.functions)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nrefreshes the matchers dictionary.", "response": "def matchers_refresh(self):\n '''\n Refresh the matchers\n '''\n log.debug('Refreshing matchers.')\n self.matchers = salt.loader.matchers(self.opts)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrefreshing the pillar data.", "response": "def pillar_refresh(self, force_refresh=False, notify=False):\n '''\n Refresh the pillar\n '''\n if self.connected:\n log.debug('Refreshing pillar. Notify: %s', notify)\n async_pillar = salt.pillar.get_async_pillar(\n self.opts,\n self.opts['grains'],\n self.opts['id'],\n self.opts['saltenv'],\n pillarenv=self.opts.get('pillarenv'),\n )\n try:\n self.opts['pillar'] = yield async_pillar.compile_pillar()\n if notify:\n evt = salt.utils.event.get_event('minion', opts=self.opts, listen=False)\n evt.fire_event({'complete': True}, tag=salt.defaults.events.MINION_PILLAR_COMPLETE)\n except SaltClientError:\n # Do not exit if a pillar refresh fails.\n log.error('Pillar data could not be refreshed. '\n 'One or more masters may be down!')\n finally:\n async_pillar.destroy()\n self.module_refresh(force_refresh, notify)\n self.matchers_refresh()\n self.beacons_refresh()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nrefreshing the functions and returners.", "response": "def manage_schedule(self, tag, data):\n '''\n Refresh the functions and returners.\n '''\n func = data.get('func', None)\n name = data.get('name', None)\n schedule = data.get('schedule', None)\n where = data.get('where', None)\n persist = data.get('persist', None)\n\n funcs = {'delete': ('delete_job', (name, persist)),\n 'add': ('add_job', (schedule, persist)),\n 'modify': ('modify_job',\n (name, schedule, persist)),\n 'enable': ('enable_schedule', ()),\n 'disable': ('disable_schedule', ()),\n 'enable_job': ('enable_job', (name, persist)),\n 'disable_job': ('disable_job', (name, persist)),\n 'postpone_job': ('postpone_job', (name, data)),\n 'skip_job': ('skip_job', (name, data)),\n 'reload': ('reload', (schedule,)),\n 'list': ('list', (where,)),\n 'save_schedule': ('save_schedule', ()),\n 'get_next_fire_time': ('get_next_fire_time',\n (name,))}\n\n # Call the appropriate schedule function\n try:\n alias, params = funcs.get(func)\n getattr(self.schedule, alias)(*params)\n except TypeError:\n log.error('Function \"%s\" is unavailable in salt.utils.scheduler',\n func)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmanages Beacons related to a specific tag.", "response": "def manage_beacons(self, tag, data):\n '''\n Manage Beacons\n '''\n func = data.get('func', None)\n name = data.get('name', None)\n beacon_data = data.get('beacon_data', None)\n include_pillar = data.get('include_pillar', None)\n include_opts = data.get('include_opts', None)\n\n funcs = {'add': ('add_beacon', (name, beacon_data)),\n 'modify': ('modify_beacon', (name, beacon_data)),\n 'delete': ('delete_beacon', (name,)),\n 'enable': ('enable_beacons', ()),\n 'disable': ('disable_beacons', ()),\n 'enable_beacon': ('enable_beacon', (name,)),\n 'disable_beacon': ('disable_beacon', (name,)),\n 'list': ('list_beacons', (include_opts,\n include_pillar)),\n 'list_available': ('list_available_beacons', ()),\n 'validate_beacon': ('validate_beacon', (name,\n beacon_data)),\n 'reset': ('reset', ())}\n\n # Call the appropriate beacon function\n try:\n alias, params = funcs.get(func)\n getattr(self.beacons, alias)(*params)\n except AttributeError:\n log.error('Function \"%s\" is unavailable in salt.beacons', func)\n except TypeError as exc:\n log.info(\n 'Failed to handle %s with data(%s). Error: %s',\n tag, data, exc,\n exc_info_on_loglevel=logging.DEBUG\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the salt - minion main process environment according to the data contained in the minion event data.", "response": "def environ_setenv(self, tag, data):\n '''\n Set the salt-minion main process environment according to\n the data contained in the minion event data\n '''\n environ = data.get('environ', None)\n if environ is None:\n return False\n false_unsets = data.get('false_unsets', False)\n clear_all = data.get('clear_all', False)\n import salt.modules.environ as mod_environ\n return mod_environ.setenv(environ, false_unsets, clear_all)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _pre_tune(self):\n '''\n Set the minion running flag and issue the appropriate warnings if\n the minion cannot be started or is already running\n '''\n if self._running is None:\n self._running = True\n elif self._running is False:\n log.error(\n 'This %s was scheduled to stop. Not running %s.tune_in()',\n self.__class__.__name__, self.__class__.__name__\n )\n return\n elif self._running is True:\n log.error(\n 'This %s is already running. Not running %s.tune_in()',\n self.__class__.__name__, self.__class__.__name__\n )\n return\n\n try:\n log.info(\n '%s is starting as user \\'%s\\'',\n self.__class__.__name__, salt.utils.user.get_user()\n )\n except Exception as err:\n # Only windows is allowed to fail here. See #3189. Log as debug in\n # that case. Else, error.\n log.log(\n salt.utils.platform.is_windows() and logging.DEBUG or logging.ERROR,\n 'Failed to get the user who is starting %s',\n self.__class__.__name__,\n exc_info=err\n )", "response": "Check if the minion is running and if so issue the appropriate warnings."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nsend mine data to the master", "response": "def _mine_send(self, tag, data):\n '''\n Send mine data to the master\n '''\n channel = salt.transport.client.ReqChannel.factory(self.opts)\n data['tok'] = self.tok\n try:\n ret = channel.send(data)\n return ret\n except SaltReqTimeoutError:\n log.warning('Unable to send mine data to master.')\n return None\n finally:\n channel.close()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _handle_tag_module_refresh(self, tag, data):\n '''\n Handle a module_refresh event\n '''\n self.module_refresh(\n force_refresh=data.get('force_refresh', False),\n notify=data.get('notify', False)\n )", "response": "Handle a module_refresh event."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nhandling a pillar_refresh event.", "response": "def _handle_tag_pillar_refresh(self, tag, data):\n '''\n Handle a pillar_refresh event\n '''\n yield self.pillar_refresh(\n force_refresh=data.get('force_refresh', False),\n notify=data.get('notify', False)\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _handle_tag_grains_refresh(self, tag, data):\n '''\n Handle a grains_refresh event\n '''\n if (data.get('force_refresh', False) or\n self.grains_cache != self.opts['grains']):\n self.pillar_refresh(force_refresh=True)\n self.grains_cache = self.opts['grains']", "response": "Handle a grains_refresh event"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nhandling a fire_master event.", "response": "def _handle_tag_fire_master(self, tag, data):\n '''\n Handle a fire_master event\n '''\n if self.connected:\n log.debug('Forwarding master event tag=%s', data['tag'])\n self._fire_master(data['data'], data['tag'], data['events'], data['pretag'])"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _handle_tag_master_disconnected_failback(self, tag, data):\n '''\n Handle a master_disconnected_failback event\n '''\n # if the master disconnect event is for a different master, raise an exception\n if tag.startswith(master_event(type='disconnected')) and data['master'] != self.opts['master']:\n # not mine master, ignore\n return\n if tag.startswith(master_event(type='failback')):\n # if the master failback event is not for the top master, raise an exception\n if data['master'] != self.opts['master_list'][0]:\n raise SaltException('Bad master \\'{0}\\' when mine failback is \\'{1}\\''.format(\n data['master'], self.opts['master']))\n # if the master failback event is for the current master, raise an exception\n elif data['master'] == self.opts['master'][0]:\n raise SaltException('Already connected to \\'{0}\\''.format(data['master']))\n\n if self.connected:\n # we are not connected anymore\n self.connected = False\n log.info('Connection to master %s lost', self.opts['master'])\n\n # we can't use the config default here because the default '0' value is overloaded\n # to mean 'if 0 disable the job', but when salt detects a timeout it also sets up\n # these jobs\n master_alive_interval = self.opts['master_alive_interval'] or 60\n\n if self.opts['master_type'] != 'failover':\n # modify the scheduled job to fire on reconnect\n if self.opts['transport'] != 'tcp':\n schedule = {\n 'function': 'status.master',\n 'seconds': master_alive_interval,\n 'jid_include': True,\n 'maxrunning': 1,\n 'return_job': False,\n 'kwargs': {'master': self.opts['master'],\n 'connected': False}\n }\n self.schedule.modify_job(name=master_event(type='alive', master=self.opts['master']),\n schedule=schedule)\n else:\n # delete the scheduled job to don't interfere with the failover process\n if self.opts['transport'] != 'tcp':\n self.schedule.delete_job(name=master_event(type='alive', master=self.opts['master']),\n persist=True)\n\n log.info('Trying to tune in to next master from master-list')\n\n if hasattr(self, 'pub_channel'):\n self.pub_channel.on_recv(None)\n if hasattr(self.pub_channel, 'auth'):\n self.pub_channel.auth.invalidate()\n if hasattr(self.pub_channel, 'close'):\n self.pub_channel.close()\n del self.pub_channel\n\n # if eval_master finds a new master for us, self.connected\n # will be True again on successful master authentication\n try:\n master, self.pub_channel = yield self.eval_master(\n opts=self.opts,\n failed=True,\n failback=tag.startswith(master_event(type='failback')))\n except SaltClientError:\n pass\n\n if self.connected:\n self.opts['master'] = master\n\n # re-init the subsystems to work with the new master\n log.info(\n 'Re-initialising subsystems for new master %s',\n self.opts['master']\n )\n # put the current schedule into the new loaders\n self.opts['schedule'] = self.schedule.option('schedule')\n self.functions, self.returners, self.function_errors, self.executors = self._load_modules()\n # make the schedule to use the new 'functions' loader\n self.schedule.functions = self.functions\n self.pub_channel.on_recv(self._handle_payload)\n self._fire_master_minion_start()\n log.info('Minion is ready to receive requests!')\n\n # update scheduled job to run with the new master addr\n if self.opts['transport'] != 'tcp':\n schedule = {\n 'function': 'status.master',\n 'seconds': master_alive_interval,\n 'jid_include': True,\n 'maxrunning': 1,\n 'return_job': False,\n 'kwargs': {'master': self.opts['master'],\n 'connected': True}\n }\n self.schedule.modify_job(name=master_event(type='alive', master=self.opts['master']),\n schedule=schedule)\n\n if self.opts['master_failback'] and 'master_list' in self.opts:\n if self.opts['master'] != self.opts['master_list'][0]:\n schedule = {\n 'function': 'status.ping_master',\n 'seconds': self.opts['master_failback_interval'],\n 'jid_include': True,\n 'maxrunning': 1,\n 'return_job': False,\n 'kwargs': {'master': self.opts['master_list'][0]}\n }\n self.schedule.modify_job(name=master_event(type='failback'),\n schedule=schedule)\n else:\n self.schedule.delete_job(name=master_event(type='failback'), persist=True)\n else:\n self.restart = True\n self.io_loop.stop()", "response": "Handle a master_disconnected_failback event."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _handle_tag_master_connected(self, tag, data):\n '''\n Handle a master_connected event\n '''\n # handle this event only once. otherwise it will pollute the log\n # also if master type is failover all the reconnection work is done\n # by `disconnected` event handler and this event must never happen,\n # anyway check it to be sure\n if not self.connected and self.opts['master_type'] != 'failover':\n log.info('Connection to master %s re-established', self.opts['master'])\n self.connected = True\n # modify the __master_alive job to only fire,\n # if the connection is lost again\n if self.opts['transport'] != 'tcp':\n if self.opts['master_alive_interval'] > 0:\n schedule = {\n 'function': 'status.master',\n 'seconds': self.opts['master_alive_interval'],\n 'jid_include': True,\n 'maxrunning': 1,\n 'return_job': False,\n 'kwargs': {'master': self.opts['master'],\n 'connected': True}\n }\n\n self.schedule.modify_job(name=master_event(type='alive', master=self.opts['master']),\n schedule=schedule)\n else:\n self.schedule.delete_job(name=master_event(type='alive', master=self.opts['master']), persist=True)", "response": "Handle a master_connected event."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _handle_tag_schedule_return(self, tag, data):\n '''\n Handle a _schedule_return event\n '''\n # reporting current connection with master\n if data['schedule'].startswith(master_event(type='alive', master='')):\n if data['return']:\n log.debug(\n 'Connected to master %s',\n data['schedule'].split(master_event(type='alive', master=''))[1]\n )\n self._return_pub(data, ret_cmd='_return', sync=False)", "response": "Handle a _schedule_return event."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nhandle a _salt_error event.", "response": "def _handle_tag_salt_error(self, tag, data):\n '''\n Handle a _salt_error event\n '''\n if self.connected:\n log.debug('Forwarding salt error event tag=%s', tag)\n self._fire_master(data, tag)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nhandle a salt_auth_creds event.", "response": "def _handle_tag_salt_auth_creds(self, tag, data):\n '''\n Handle a salt_auth_creds event\n '''\n key = tuple(data['key'])\n log.debug(\n 'Updating auth data for %s: %s -> %s',\n key, salt.crypt.AsyncAuth.creds_map.get(key), data['creds']\n )\n salt.crypt.AsyncAuth.creds_map[tuple(data['key'])] = data['creds']"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef handle_event(self, package):\n '''\n Handle an event from the epull_sock (all local minion events)\n '''\n if not self.ready:\n raise tornado.gen.Return()\n tag, data = salt.utils.event.SaltEvent.unpack(package)\n log.debug(\n 'Minion of \\'%s\\' is handling event tag \\'%s\\'',\n self.opts['master'], tag\n )\n tag_functions = {\n 'beacons_refresh': self._handle_tag_beacons_refresh,\n 'environ_setenv': self._handle_tag_environ_setenv,\n 'fire_master': self._handle_tag_fire_master,\n 'grains_refresh': self._handle_tag_grains_refresh,\n 'matchers_refresh': self._handle_tag_matchers_refresh,\n 'manage_schedule': self._handle_tag_manage_schedule,\n 'manage_beacons': self._handle_tag_manage_beacons,\n '_minion_mine': self._handle_tag_minion_mine,\n 'module_refresh': self._handle_tag_module_refresh,\n 'pillar_refresh': self._handle_tag_pillar_refresh,\n 'salt/auth/creds': self._handle_tag_salt_auth_creds,\n '_salt_error': self._handle_tag_salt_error,\n '__schedule_return': self._handle_tag_schedule_return,\n master_event(type='disconnected'): self._handle_tag_master_disconnected_failback,\n master_event(type='failback'): self._handle_tag_master_disconnected_failback,\n master_event(type='connected'): self._handle_tag_master_connected,\n }\n\n # Run the appropriate function\n for tag_function in tag_functions:\n if tag.startswith(tag_function):\n tag_functions[tag_function](tag, data)", "response": "Handle an event from the epull_sock"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _setup_core(self):\n '''\n Set up the core minion attributes.\n This is safe to call multiple times.\n '''\n if not self.ready:\n # First call. Initialize.\n self.functions, self.returners, self.function_errors, self.executors = self._load_modules()\n self.serial = salt.payload.Serial(self.opts)\n self.mod_opts = self._prep_mod_opts()\n# self.matcher = Matcher(self.opts, self.functions)\n self.matchers = salt.loader.matchers(self.opts)\n self.beacons = salt.beacons.Beacon(self.opts, self.functions)\n uid = salt.utils.user.get_uid(user=self.opts.get('user', None))\n self.proc_dir = get_proc_dir(self.opts['cachedir'], uid=uid)\n self.grains_cache = self.opts['grains']\n self.ready = True", "response": "Setup the core minion attributes."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef setup_beacons(self, before_connect=False):\n '''\n Set up the beacons.\n This is safe to call multiple times.\n '''\n self._setup_core()\n\n loop_interval = self.opts['loop_interval']\n new_periodic_callbacks = {}\n\n if 'beacons' not in self.periodic_callbacks:\n self.beacons = salt.beacons.Beacon(self.opts, self.functions)\n\n def handle_beacons():\n # Process Beacons\n beacons = None\n try:\n beacons = self.process_beacons(self.functions)\n except Exception:\n log.critical('The beacon errored: ', exc_info=True)\n if beacons and self.connected:\n self._fire_master(events=beacons)\n\n new_periodic_callbacks['beacons'] = tornado.ioloop.PeriodicCallback(\n handle_beacons, loop_interval * 1000)\n if before_connect:\n # Make sure there is a chance for one iteration to occur before connect\n handle_beacons()\n\n if 'cleanup' not in self.periodic_callbacks:\n new_periodic_callbacks['cleanup'] = tornado.ioloop.PeriodicCallback(\n self._fallback_cleanups, loop_interval * 1000)\n\n # start all the other callbacks\n for periodic_cb in six.itervalues(new_periodic_callbacks):\n periodic_cb.start()\n\n self.periodic_callbacks.update(new_periodic_callbacks)", "response": "Set up the beacons.\n This is safe to call multiple times."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef setup_scheduler(self, before_connect=False):\n '''\n Set up the scheduler.\n This is safe to call multiple times.\n '''\n self._setup_core()\n\n loop_interval = self.opts['loop_interval']\n new_periodic_callbacks = {}\n\n if 'schedule' not in self.periodic_callbacks:\n if 'schedule' not in self.opts:\n self.opts['schedule'] = {}\n if not hasattr(self, 'schedule'):\n self.schedule = salt.utils.schedule.Schedule(\n self.opts,\n self.functions,\n self.returners,\n utils=self.utils,\n cleanup=[master_event(type='alive')])\n\n try:\n if self.opts['grains_refresh_every']: # In minutes, not seconds!\n log.debug(\n 'Enabling the grains refresher. Will run every %d minute(s).',\n self.opts['grains_refresh_every']\n )\n self._refresh_grains_watcher(abs(self.opts['grains_refresh_every']))\n except Exception as exc:\n log.error(\n 'Exception occurred in attempt to initialize grain refresh '\n 'routine during minion tune-in: %s', exc\n )\n\n # TODO: actually listen to the return and change period\n def handle_schedule():\n self.process_schedule(self, loop_interval)\n new_periodic_callbacks['schedule'] = tornado.ioloop.PeriodicCallback(handle_schedule, 1000)\n\n if before_connect:\n # Make sure there is a chance for one iteration to occur before connect\n handle_schedule()\n\n if 'cleanup' not in self.periodic_callbacks:\n new_periodic_callbacks['cleanup'] = tornado.ioloop.PeriodicCallback(\n self._fallback_cleanups, loop_interval * 1000)\n\n # start all the other callbacks\n for periodic_cb in six.itervalues(new_periodic_callbacks):\n periodic_cb.start()\n\n self.periodic_callbacks.update(new_periodic_callbacks)", "response": "Setup the scheduler for the current minion."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef tune_in(self, start=True):\n '''\n Lock onto the publisher. This is the main event loop for the minion\n :rtype : None\n '''\n self._pre_tune()\n\n log.debug('Minion \\'%s\\' trying to tune in', self.opts['id'])\n\n if start:\n if self.opts.get('beacons_before_connect', False):\n self.setup_beacons(before_connect=True)\n if self.opts.get('scheduler_before_connect', False):\n self.setup_scheduler(before_connect=True)\n self.sync_connect_master()\n if self.connected:\n self._fire_master_minion_start()\n log.info('Minion is ready to receive requests!')\n\n # Make sure to gracefully handle SIGUSR1\n enable_sigusr1_handler()\n\n # Make sure to gracefully handle CTRL_LOGOFF_EVENT\n if HAS_WIN_FUNCTIONS:\n salt.utils.win_functions.enable_ctrl_logoff_handler()\n\n # On first startup execute a state run if configured to do so\n self._state_run()\n\n self.setup_beacons()\n self.setup_scheduler()\n\n # schedule the stuff that runs every interval\n ping_interval = self.opts.get('ping_interval', 0) * 60\n if ping_interval > 0 and self.connected:\n def ping_master():\n try:\n def ping_timeout_handler(*_):\n if self.opts.get('auth_safemode', False):\n log.error('** Master Ping failed. Attempting to restart minion**')\n delay = self.opts.get('random_reauth_delay', 5)\n log.info('delaying random_reauth_delay %ss', delay)\n try:\n self.functions['service.restart'](service_name())\n except KeyError:\n # Probably no init system (running in docker?)\n log.warning(\n 'ping_interval reached without response '\n 'from the master, but service.restart '\n 'could not be run to restart the minion '\n 'daemon. ping_interval requires that the '\n 'minion is running under an init system.'\n )\n\n self._fire_master('ping', 'minion_ping', sync=False, timeout_handler=ping_timeout_handler)\n except Exception:\n log.warning('Attempt to ping master failed.', exc_on_loglevel=logging.DEBUG)\n self.periodic_callbacks['ping'] = tornado.ioloop.PeriodicCallback(ping_master, ping_interval * 1000)\n self.periodic_callbacks['ping'].start()\n\n # add handler to subscriber\n if hasattr(self, 'pub_channel') and self.pub_channel is not None:\n self.pub_channel.on_recv(self._handle_payload)\n elif self.opts.get('master_type') != 'disable':\n log.error('No connection to master found. Scheduled jobs will not run.')\n\n if start:\n try:\n self.io_loop.start()\n if self.restart:\n self.destroy()\n except (KeyboardInterrupt, RuntimeError): # A RuntimeError can be re-raised by Tornado on shutdown\n self.destroy()", "response": "Start the minion tuning in."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ntear down the minion", "response": "def destroy(self):\n '''\n Tear down the minion\n '''\n if self._running is False:\n return\n\n self._running = False\n if hasattr(self, 'schedule'):\n del self.schedule\n if hasattr(self, 'pub_channel') and self.pub_channel is not None:\n self.pub_channel.on_recv(None)\n if hasattr(self.pub_channel, 'close'):\n self.pub_channel.close()\n del self.pub_channel\n if hasattr(self, 'periodic_callbacks'):\n for cb in six.itervalues(self.periodic_callbacks):\n cb.stop()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _handle_decoded_payload(self, data):\n '''\n Override this method if you wish to handle the decoded data\n differently.\n '''\n # TODO: even do this??\n data['to'] = int(data.get('to', self.opts['timeout'])) - 1\n # Only forward the command if it didn't originate from ourselves\n if data.get('master_id', 0) != self.opts.get('master_id', 1):\n self.syndic_cmd(data)", "response": "Handle the decoded payload."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nforward the load and forward it on to the client cmd Take the now clear load and forward it on to the client cmd Set up default tgt_type if not set", "response": "def syndic_cmd(self, data):\n '''\n Take the now clear load and forward it on to the client cmd\n '''\n # Set up default tgt_type\n if 'tgt_type' not in data:\n data['tgt_type'] = 'glob'\n kwargs = {}\n\n # optionally add a few fields to the publish data\n for field in ('master_id', # which master the job came from\n 'user', # which user ran the job\n ):\n if field in data:\n kwargs[field] = data[field]\n\n def timeout_handler(*args):\n log.warning('Unable to forward pub data: %s', args[1])\n return True\n\n with tornado.stack_context.ExceptionStackContext(timeout_handler):\n self.local.pub_async(data['tgt'],\n data['fun'],\n data['arg'],\n data['tgt_type'],\n data['ret'],\n data['jid'],\n data['to'],\n io_loop=self.io_loop,\n callback=lambda _: None,\n **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nexecutes the tune_in sequence but omits extra logging and the management of the event bus assuming that these are handled outside the tune_in sequence", "response": "def tune_in_no_block(self):\n '''\n Executes the tune_in sequence but omits extra logging and the\n management of the event bus assuming that these are handled outside\n the tune_in sequence\n '''\n # Instantiate the local client\n self.local = salt.client.get_local_client(\n self.opts['_minion_conf_file'], io_loop=self.io_loop)\n\n # add handler to subscriber\n self.pub_channel.on_recv(self._process_cmd_socket)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntearing down the syndic minion", "response": "def destroy(self):\n '''\n Tear down the syndic minion\n '''\n # We borrowed the local clients poller so give it back before\n # it's destroyed. Reset the local poller reference.\n super(Syndic, self).destroy()\n if hasattr(self, 'local'):\n del self.local\n\n if hasattr(self, 'forward_events'):\n self.forward_events.stop()"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nspawn all the coroutines which will sign in the syndics", "response": "def _spawn_syndics(self):\n '''\n Spawn all the coroutines which will sign in the syndics\n '''\n self._syndics = OrderedDict() # mapping of opts['master'] -> syndic\n masters = self.opts['master']\n if not isinstance(masters, list):\n masters = [masters]\n for master in masters:\n s_opts = copy.copy(self.opts)\n s_opts['master'] = master\n self._syndics[master] = self._connect_syndic(s_opts)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _connect_syndic(self, opts):\n '''\n Create a syndic, and asynchronously connect it to a master\n '''\n auth_wait = opts['acceptance_wait_time']\n failed = False\n while True:\n if failed:\n if auth_wait < self.max_auth_wait:\n auth_wait += self.auth_wait\n log.debug(\n \"sleeping before reconnect attempt to %s [%d/%d]\",\n opts['master'],\n auth_wait,\n self.max_auth_wait,\n )\n yield tornado.gen.sleep(auth_wait) # TODO: log?\n log.debug(\n 'Syndic attempting to connect to %s',\n opts['master']\n )\n try:\n syndic = Syndic(opts,\n timeout=self.SYNDIC_CONNECT_TIMEOUT,\n safe=False,\n io_loop=self.io_loop,\n )\n yield syndic.connect_master(failed=failed)\n # set up the syndic to handle publishes (specifically not event forwarding)\n syndic.tune_in_no_block()\n\n # Send an event to the master that the minion is live\n syndic.fire_master_syndic_start()\n\n log.info(\n 'Syndic successfully connected to %s',\n opts['master']\n )\n break\n except SaltClientError as exc:\n failed = True\n log.error(\n 'Error while bringing up syndic for multi-syndic. Is the '\n 'master at %s responding?', opts['master']\n )\n except (KeyboardInterrupt, SystemExit):\n raise\n except Exception:\n failed = True\n log.critical(\n 'Unexpected error while connecting to %s',\n opts['master'], exc_info=True\n )\n\n raise tornado.gen.Return(syndic)", "response": "Connect to a master and wait for it to finish."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nmark a master as dead. This will start the sign - in routine .", "response": "def _mark_master_dead(self, master):\n '''\n Mark a master as dead. This will start the sign-in routine\n '''\n # if its connected, mark it dead\n if self._syndics[master].done():\n syndic = self._syndics[master].result() # pylint: disable=no-member\n self._syndics[master] = syndic.reconnect()\n else:\n # TODO: debug?\n log.info(\n 'Attempting to mark %s as dead, although it is already '\n 'marked dead', master\n )"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _call_syndic(self, func, args=(), kwargs=None, master_id=None):\n '''\n Wrapper to call a given func on a syndic, best effort to get the one you asked for\n '''\n if kwargs is None:\n kwargs = {}\n successful = False\n # Call for each master\n for master, syndic_future in self.iter_master_options(master_id):\n if not syndic_future.done() or syndic_future.exception():\n log.error(\n 'Unable to call %s on %s, that syndic is not connected',\n func, master\n )\n continue\n\n try:\n getattr(syndic_future.result(), func)(*args, **kwargs)\n successful = True\n except SaltClientError:\n log.error(\n 'Unable to call %s on %s, trying another...',\n func, master\n )\n self._mark_master_dead(master)\n if not successful:\n log.critical('Unable to call %s on any masters!', func)", "response": "Wrapper to call a given function on a syndic"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _return_pub_syndic(self, values, master_id=None):\n '''\n Wrapper to call the '_return_pub_multi' a syndic, best effort to get the one you asked for\n '''\n func = '_return_pub_multi'\n for master, syndic_future in self.iter_master_options(master_id):\n if not syndic_future.done() or syndic_future.exception():\n log.error(\n 'Unable to call %s on %s, that syndic is not connected',\n func, master\n )\n continue\n\n future, data = self.pub_futures.get(master, (None, None))\n if future is not None:\n if not future.done():\n if master == master_id:\n # Targeted master previous send not done yet, call again later\n return False\n else:\n # Fallback master is busy, try the next one\n continue\n elif future.exception():\n # Previous execution on this master returned an error\n log.error(\n 'Unable to call %s on %s, trying another...',\n func, master\n )\n self._mark_master_dead(master)\n del self.pub_futures[master]\n # Add not sent data to the delayed list and try the next master\n self.delayed.extend(data)\n continue\n future = getattr(syndic_future.result(), func)(values,\n '_syndic_return',\n timeout=self._return_retry_timer(),\n sync=False)\n self.pub_futures[master] = (future, values)\n return True\n # Loop done and didn't exit: wasn't sent, try again later\n return False", "response": "Wrapper to call _return_pub_multi and return the master_id of the first available pub_id"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef iter_master_options(self, master_id=None):\n '''\n Iterate (in order) over your options for master\n '''\n masters = list(self._syndics.keys())\n if self.opts['syndic_failover'] == 'random':\n shuffle(masters)\n if master_id not in self._syndics:\n master_id = masters.pop(0)\n else:\n masters.remove(master_id)\n\n while True:\n yield master_id, self._syndics[master_id]\n if not masters:\n break\n master_id = masters.pop(0)", "response": "Iterate over the options for master_id."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ntunes in the syndic.", "response": "def tune_in(self):\n '''\n Lock onto the publisher. This is the main event loop for the syndic\n '''\n self._spawn_syndics()\n # Instantiate the local client\n self.local = salt.client.get_local_client(\n self.opts['_minion_conf_file'], io_loop=self.io_loop)\n self.local.event.subscribe('')\n\n log.debug('SyndicManager \\'%s\\' trying to tune in', self.opts['id'])\n\n # register the event sub to the poller\n self.job_rets = {}\n self.raw_events = []\n self._reset_event_aggregation()\n future = self.local.event.set_event_handler(self._process_event)\n self.io_loop.add_future(future, self.reconnect_event_bus)\n\n # forward events every syndic_event_forward_timeout\n self.forward_events = tornado.ioloop.PeriodicCallback(self._forward_events,\n self.opts['syndic_event_forward_timeout'] * 1000,\n )\n self.forward_events.start()\n\n # Make sure to gracefully handle SIGUSR1\n enable_sigusr1_handler()\n\n self.io_loop.start()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nfunctioning to finish init after connecting to a master", "response": "def _post_master_init(self, master):\n '''\n Function to finish init after connecting to a master\n\n This is primarily loading modules, pillars, etc. (since they need\n to know which master they connected to)\n\n If this function is changed, please check Minion._post_master_init\n to see if those changes need to be propagated.\n\n ProxyMinions need a significantly different post master setup,\n which is why the differences are not factored out into separate helper\n functions.\n '''\n mp_call = _metaproxy_call(self.opts, 'post_master_init')\n return mp_call(self, master)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _target_load(self, load):\n '''\n Verify that the publication is valid and applies to this minion\n '''\n mp_call = _metaproxy_call(self.opts, 'target_load')\n return mp_call(self, load)", "response": "Return the target load for this minion"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ngenerate the modules for the current minion", "response": "def gen_modules(self, initial_load=False):\n '''\n Tell the minion to reload the execution modules\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.reload_modules\n '''\n self.opts['grains'] = salt.loader.grains(self.opts)\n self.opts['pillar'] = salt.pillar.get_pillar(\n self.opts,\n self.opts['grains'],\n self.opts['id'],\n saltenv=self.opts['saltenv'],\n pillarenv=self.opts.get('pillarenv'),\n ).compile_pillar()\n\n if 'proxy' not in self.opts['pillar'] and 'proxy' not in self.opts:\n errmsg = (\n 'No \"proxy\" configuration key found in pillar or opts '\n 'dictionaries for id {id}. Check your pillar/options '\n 'configuration and contents. Salt-proxy aborted.'\n ).format(id=self.opts['id'])\n log.error(errmsg)\n self._running = False\n raise SaltSystemExit(code=salt.defaults.exitcodes.EX_GENERIC, msg=errmsg)\n\n if 'proxy' not in self.opts:\n self.opts['proxy'] = self.opts['pillar']['proxy']\n\n # Then load the proxy module\n self.proxy = salt.loader.proxy(self.opts)\n\n self.utils = salt.loader.utils(self.opts, proxy=self.proxy)\n\n self.functions = salt.loader.minion_mods(self.opts, utils=self.utils, notify=False, proxy=self.proxy)\n self.returners = salt.loader.returners(self.opts, self.functions, proxy=self.proxy)\n self.matchers = salt.loader.matchers(self.opts)\n self.functions['sys.reload_modules'] = self.gen_modules\n self.executors = salt.loader.executors(self.opts, self.functions, proxy=self.proxy)\n\n fq_proxyname = self.opts['proxy']['proxytype']\n\n # we can then sync any proxymodules down from the master\n # we do a sync_all here in case proxy code was installed by\n # SPM or was manually placed in /srv/salt/_modules etc.\n self.functions['saltutil.sync_all'](saltenv=self.opts['saltenv'])\n\n self.functions.pack['__proxy__'] = self.proxy\n self.proxy.pack['__salt__'] = self.functions\n self.proxy.pack['__ret__'] = self.returners\n self.proxy.pack['__pillar__'] = self.opts['pillar']\n\n # Reload utils as well (chicken and egg, __utils__ needs __proxy__ and __proxy__ needs __utils__\n self.utils = salt.loader.utils(self.opts, proxy=self.proxy)\n self.proxy.pack['__utils__'] = self.utils\n\n # Reload all modules so all dunder variables are injected\n self.proxy.reload_modules()\n\n if ('{0}.init'.format(fq_proxyname) not in self.proxy\n or '{0}.shutdown'.format(fq_proxyname) not in self.proxy):\n errmsg = 'Proxymodule {0} is missing an init() or a shutdown() or both. '.format(fq_proxyname) + \\\n 'Check your proxymodule. Salt-proxy aborted.'\n log.error(errmsg)\n self._running = False\n raise SaltSystemExit(code=salt.defaults.exitcodes.EX_GENERIC, msg=errmsg)\n\n self.module_executors = self.proxy.get('{0}.module_executors'.format(fq_proxyname), lambda: [])()\n proxy_init_fn = self.proxy[fq_proxyname + '.init']\n proxy_init_fn(self.opts)\n\n self.opts['grains'] = salt.loader.grains(self.opts, proxy=self.proxy)\n\n # Sync the grains here so the proxy can communicate them to the master\n self.functions['saltutil.sync_grains'](saltenv='base')\n self.grains_cache = self.opts['grains']\n self.ready = True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a tuple containing the comparison operator and the version of the no .", "response": "def _get_comparison_spec(pkgver):\n '''\n Return a tuple containing the comparison operator and the version. If no\n comparison operator was passed, the comparison is assumed to be an \"equals\"\n comparison, and \"==\" will be the operator returned.\n '''\n oper, verstr = salt.utils.pkg.split_comparison(pkgver.strip())\n if oper in ('=', ''):\n oper = '=='\n return oper, verstr"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _parse_version_string(version_conditions_string):\n '''\n Returns a list of two-tuples containing (operator, version).\n '''\n result = []\n version_conditions_string = version_conditions_string.strip()\n if not version_conditions_string:\n return result\n for version_condition in version_conditions_string.split(','):\n operator_and_version = _get_comparison_spec(version_condition)\n result.append(operator_and_version)\n return result", "response": "Returns a list of two - tuples containing operator and version."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn True if any of the installed versions match the specified version conditions string.", "response": "def _fulfills_version_string(installed_versions, version_conditions_string, ignore_epoch=False, allow_updates=False):\n '''\n Returns True if any of the installed versions match the specified version conditions,\n otherwise returns False.\n\n installed_versions\n The installed versions\n\n version_conditions_string\n The string containing all version conditions. E.G.\n 1.2.3-4\n >=1.2.3-4\n >=1.2.3-4, <2.3.4-5\n >=1.2.3-4, <2.3.4-5, !=1.2.4-1\n\n ignore_epoch : False\n When a package version contains an non-zero epoch (e.g.\n ``1:3.14.159-2.el7``, and a specific version of a package is desired,\n set this option to ``True`` to ignore the epoch when comparing\n versions.\n\n allow_updates : False\n Allow the package to be updated outside Salt's control (e.g. auto updates on Windows).\n This means a package on the Minion can have a newer version than the latest available in\n the repository without enforcing a re-installation of the package.\n (Only applicable if only one strict version condition is specified E.G. version: 2.0.6~ubuntu3)\n '''\n version_conditions = _parse_version_string(version_conditions_string)\n for installed_version in installed_versions:\n fullfills_all = True\n for operator, version_string in version_conditions:\n if allow_updates and len(version_conditions) == 1 and operator == '==':\n operator = '>='\n fullfills_all = fullfills_all and _fulfills_version_spec([installed_version], operator, version_string, ignore_epoch=ignore_epoch)\n if fullfills_all:\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _fulfills_version_spec(versions, oper, desired_version,\n ignore_epoch=False):\n '''\n Returns True if any of the installed versions match the specified version,\n otherwise returns False\n '''\n cmp_func = __salt__.get('pkg.version_cmp')\n # stripping \"with_origin\" dict wrapper\n if salt.utils.platform.is_freebsd():\n if isinstance(versions, dict) and 'version' in versions:\n versions = versions['version']\n for ver in versions:\n if (oper == '==' and fnmatch.fnmatch(ver, desired_version)) \\\n or salt.utils.versions.compare(ver1=ver,\n oper=oper,\n ver2=desired_version,\n cmp_func=cmp_func,\n ignore_epoch=ignore_epoch):\n return True\n return False", "response": "Returns True if any of the installed versions match the specified version otherwise returns False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _find_unpurge_targets(desired, **kwargs):\n '''\n Find packages which are marked to be purged but can't yet be removed\n because they are dependencies for other installed packages. These are the\n packages which will need to be 'unpurged' because they are part of\n pkg.installed states. This really just applies to Debian-based Linuxes.\n '''\n return [\n x for x in desired\n if x in __salt__['pkg.list_pkgs'](purge_desired=True, **kwargs)\n ]", "response": "Find packages which can t be purged but can t yet be removed."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _find_download_targets(name=None,\n version=None,\n pkgs=None,\n normalize=True,\n skip_suggestions=False,\n ignore_epoch=False,\n **kwargs):\n '''\n Inspect the arguments to pkg.downloaded and discover what packages need to\n be downloaded. Return a dict of packages to download.\n '''\n cur_pkgs = __salt__['pkg.list_downloaded'](**kwargs)\n if pkgs:\n to_download = _repack_pkgs(pkgs, normalize=normalize)\n\n if not to_download:\n # Badly-formatted SLS\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': 'Invalidly formatted pkgs parameter. See '\n 'minion log.'}\n else:\n if normalize:\n _normalize_name = \\\n __salt__.get('pkg.normalize_name', lambda pkgname: pkgname)\n to_download = {_normalize_name(name): version}\n else:\n to_download = {name: version}\n\n cver = cur_pkgs.get(name, {})\n if name in to_download:\n # Package already downloaded, no need to download again\n if cver and version in cver:\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'Version {0} of package \\'{1}\\' is already '\n 'downloaded'.format(version, name)}\n\n # if cver is not an empty string, the package is already downloaded\n elif cver and version is None:\n # The package is downloaded\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'Package {0} is already '\n 'downloaded'.format(name)}\n\n version_spec = False\n if not skip_suggestions:\n try:\n problems = _preflight_check(to_download, **kwargs)\n except CommandExecutionError:\n pass\n else:\n comments = []\n if problems.get('no_suggest'):\n comments.append(\n 'The following package(s) were not found, and no '\n 'possible matches were found in the package db: '\n '{0}'.format(\n ', '.join(sorted(problems['no_suggest']))\n )\n )\n if problems.get('suggest'):\n for pkgname, suggestions in \\\n six.iteritems(problems['suggest']):\n comments.append(\n 'Package \\'{0}\\' not found (possible matches: '\n '{1})'.format(pkgname, ', '.join(suggestions))\n )\n if comments:\n if len(comments) > 1:\n comments.append('')\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': '. '.join(comments).rstrip()}\n\n # Find out which packages will be targeted in the call to pkg.download\n # Check current downloaded versions against specified versions\n targets = {}\n problems = []\n for pkgname, pkgver in six.iteritems(to_download):\n cver = cur_pkgs.get(pkgname, {})\n # Package not yet downloaded, so add to targets\n if not cver:\n targets[pkgname] = pkgver\n continue\n # No version specified but package is already downloaded\n elif cver and not pkgver:\n continue\n\n version_spec = True\n try:\n if not _fulfills_version_string(cver.keys(), pkgver, ignore_epoch=ignore_epoch):\n targets[pkgname] = pkgver\n except CommandExecutionError as exc:\n problems.append(exc.strerror)\n continue\n\n if problems:\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ' '.join(problems)}\n\n if not targets:\n # All specified packages are already downloaded\n msg = (\n 'All specified packages{0} are already downloaded'\n .format(' (matching specified versions)' if version_spec else '')\n )\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': msg}\n\n return targets", "response": "Return a dict of packages to download."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _find_advisory_targets(name=None,\n advisory_ids=None,\n **kwargs):\n '''\n Inspect the arguments to pkg.patch_installed and discover what advisory\n patches need to be installed. Return a dict of advisory patches to install.\n '''\n cur_patches = __salt__['pkg.list_installed_patches'](**kwargs)\n if advisory_ids:\n to_download = advisory_ids\n else:\n to_download = [name]\n if cur_patches.get(name, {}):\n # Advisory patch already installed, no need to install it again\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'Advisory patch {0} is already '\n 'installed'.format(name)}\n\n # Find out which advisory patches will be targeted in the call to pkg.install\n targets = []\n for patch_name in to_download:\n cver = cur_patches.get(patch_name, {})\n # Advisory patch not yet installed, so add to targets\n if not cver:\n targets.append(patch_name)\n continue\n\n if not targets:\n # All specified packages are already downloaded\n msg = ('All specified advisory patches are already installed')\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': msg}\n\n return targets", "response": "Find the advisory patches that need to be installed."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _find_remove_targets(name=None,\n version=None,\n pkgs=None,\n normalize=True,\n ignore_epoch=False,\n **kwargs):\n '''\n Inspect the arguments to pkg.removed and discover what packages need to\n be removed. Return a dict of packages to remove.\n '''\n if __grains__['os'] == 'FreeBSD':\n kwargs['with_origin'] = True\n cur_pkgs = __salt__['pkg.list_pkgs'](versions_as_list=True, **kwargs)\n if pkgs:\n to_remove = _repack_pkgs(pkgs, normalize=normalize)\n\n if not to_remove:\n # Badly-formatted SLS\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': 'Invalidly formatted pkgs parameter. See '\n 'minion log.'}\n else:\n _normalize_name = \\\n __salt__.get('pkg.normalize_name', lambda pkgname: pkgname)\n to_remove = {_normalize_name(name): version}\n\n version_spec = False\n # Find out which packages will be targeted in the call to pkg.remove\n # Check current versions against specified versions\n targets = []\n problems = []\n for pkgname, pkgver in six.iteritems(to_remove):\n # FreeBSD pkg supports `openjdk` and `java/openjdk7` package names\n origin = bool(re.search('/', pkgname))\n\n if __grains__['os'] == 'FreeBSD' and origin:\n cver = [k for k, v in six.iteritems(cur_pkgs) if v['origin'] == pkgname]\n else:\n cver = cur_pkgs.get(pkgname, [])\n\n # Package not installed, no need to remove\n if not cver:\n continue\n # No version specified and pkg is installed\n elif __salt__['pkg_resource.version_clean'](pkgver) is None:\n targets.append(pkgname)\n continue\n version_spec = True\n try:\n if _fulfills_version_string(cver, pkgver, ignore_epoch=ignore_epoch):\n targets.append(pkgname)\n else:\n log.debug(\n 'Current version (%s) did not match desired version '\n 'specification (%s), will not remove', cver, pkgver\n )\n except CommandExecutionError as exc:\n problems.append(exc.strerror)\n continue\n\n if problems:\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ' '.join(problems)}\n\n if not targets:\n # All specified packages are already absent\n msg = 'All specified packages{0} are already absent'.format(\n ' (matching specified versions)' if version_spec else ''\n )\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': msg}\n\n return targets", "response": "Find packages that need to be removed from the current version of the current version."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _find_install_targets(name=None,\n version=None,\n pkgs=None,\n sources=None,\n skip_suggestions=False,\n pkg_verify=False,\n normalize=True,\n ignore_epoch=False,\n reinstall=False,\n refresh=False,\n **kwargs):\n '''\n Inspect the arguments to pkg.installed and discover what packages need to\n be installed. Return a dict of desired packages\n '''\n was_refreshed = False\n\n if all((pkgs, sources)):\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': 'Only one of \"pkgs\" and \"sources\" is permitted.'}\n\n # dict for packages that fail pkg.verify and their altered files\n altered_files = {}\n # Get the ignore_types list if any from the pkg_verify argument\n if isinstance(pkg_verify, list) \\\n and any(x.get('ignore_types') is not None\n for x in pkg_verify\n if isinstance(x, _OrderedDict)\n and 'ignore_types' in x):\n ignore_types = next(x.get('ignore_types')\n for x in pkg_verify\n if 'ignore_types' in x)\n else:\n ignore_types = []\n\n # Get the verify_options list if any from the pkg_verify argument\n if isinstance(pkg_verify, list) \\\n and any(x.get('verify_options') is not None\n for x in pkg_verify\n if isinstance(x, _OrderedDict)\n and 'verify_options' in x):\n verify_options = next(x.get('verify_options')\n for x in pkg_verify\n if 'verify_options' in x)\n else:\n verify_options = []\n\n if __grains__['os'] == 'FreeBSD':\n kwargs['with_origin'] = True\n\n if salt.utils.platform.is_windows():\n # Windows requires a refresh to establish a pkg db if refresh=True, so\n # add it to the kwargs.\n kwargs['refresh'] = refresh\n\n resolve_capabilities = kwargs.get('resolve_capabilities', False) and 'pkg.list_provides' in __salt__\n try:\n cur_pkgs = __salt__['pkg.list_pkgs'](versions_as_list=True, **kwargs)\n cur_prov = resolve_capabilities and __salt__['pkg.list_provides'](**kwargs) or dict()\n except CommandExecutionError as exc:\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': exc.strerror}\n\n if salt.utils.platform.is_windows() and kwargs.pop('refresh', False):\n # We already refreshed when we called pkg.list_pkgs\n was_refreshed = True\n refresh = False\n\n if any((pkgs, sources)):\n if pkgs:\n desired = _repack_pkgs(pkgs, normalize=normalize)\n elif sources:\n desired = __salt__['pkg_resource.pack_sources'](\n sources,\n normalize=normalize,\n )\n\n if not desired:\n # Badly-formatted SLS\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': 'Invalidly formatted \\'{0}\\' parameter. See '\n 'minion log.'.format('pkgs' if pkgs\n else 'sources')}\n\n to_unpurge = _find_unpurge_targets(desired, **kwargs)\n else:\n if salt.utils.platform.is_windows():\n pkginfo = _get_package_info(name, saltenv=kwargs['saltenv'])\n if not pkginfo:\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': 'Package {0} not found in the '\n 'repository.'.format(name)}\n if version is None:\n version = _get_latest_pkg_version(pkginfo)\n\n if normalize:\n _normalize_name = \\\n __salt__.get('pkg.normalize_name', lambda pkgname: pkgname)\n desired = {_normalize_name(name): version}\n else:\n desired = {name: version}\n\n to_unpurge = _find_unpurge_targets(desired, **kwargs)\n\n # FreeBSD pkg supports `openjdk` and `java/openjdk7` package names\n origin = bool(re.search('/', name))\n\n if __grains__['os'] == 'FreeBSD' and origin:\n cver = [k for k, v in six.iteritems(cur_pkgs)\n if v['origin'] == name]\n else:\n cver = cur_pkgs.get(name, [])\n\n if name not in to_unpurge:\n if version and version in cver \\\n and not reinstall \\\n and not pkg_verify:\n # The package is installed and is the correct version\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'Version {0} of package \\'{1}\\' is already '\n 'installed'.format(version, name)}\n\n # if cver is not an empty string, the package is already installed\n elif cver and version is None \\\n and not reinstall \\\n and not pkg_verify:\n # The package is installed\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'Package {0} is already '\n 'installed'.format(name)}\n\n version_spec = False\n if not sources:\n # Check for alternate package names if strict processing is not\n # enforced. Takes extra time. Disable for improved performance\n if not skip_suggestions:\n # Perform platform-specific pre-flight checks\n not_installed = dict([\n (name, version)\n for name, version in desired.items()\n if not (name in cur_pkgs and (version is None or _fulfills_version_string(cur_pkgs[name], version)))\n ])\n if not_installed:\n try:\n problems = _preflight_check(not_installed, **kwargs)\n except CommandExecutionError:\n pass\n else:\n comments = []\n if problems.get('no_suggest'):\n comments.append(\n 'The following package(s) were not found, and no '\n 'possible matches were found in the package db: '\n '{0}'.format(\n ', '.join(sorted(problems['no_suggest']))\n )\n )\n if problems.get('suggest'):\n for pkgname, suggestions in \\\n six.iteritems(problems['suggest']):\n comments.append(\n 'Package \\'{0}\\' not found (possible matches: '\n '{1})'.format(pkgname, ', '.join(suggestions))\n )\n if comments:\n if len(comments) > 1:\n comments.append('')\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': '. '.join(comments).rstrip()}\n\n # Resolve the latest package version for any packages with \"latest\" in the\n # package version\n wants_latest = [] \\\n if sources \\\n else [x for x, y in six.iteritems(desired) if y == 'latest']\n if wants_latest:\n resolved_latest = __salt__['pkg.latest_version'](*wants_latest,\n refresh=refresh,\n **kwargs)\n if len(wants_latest) == 1:\n resolved_latest = {wants_latest[0]: resolved_latest}\n if refresh:\n was_refreshed = True\n refresh = False\n\n # pkg.latest_version returns an empty string when the package is\n # up-to-date. So check the currently-installed packages. If found, the\n # resolved latest version will be the currently installed one from\n # cur_pkgs. If not found, then the package doesn't exist and the\n # resolved latest version will be None.\n for key in resolved_latest:\n if not resolved_latest[key]:\n if key in cur_pkgs:\n resolved_latest[key] = cur_pkgs[key][-1]\n else:\n resolved_latest[key] = None\n # Update the desired versions with the ones we resolved\n desired.update(resolved_latest)\n\n # Find out which packages will be targeted in the call to pkg.install\n targets = {}\n to_reinstall = {}\n problems = []\n warnings = []\n failed_verify = False\n for package_name, version_string in six.iteritems(desired):\n cver = cur_pkgs.get(package_name, [])\n if resolve_capabilities and not cver and package_name in cur_prov:\n cver = cur_pkgs.get(cur_prov.get(package_name)[0], [])\n\n # Package not yet installed, so add to targets\n if not cver:\n targets[package_name] = version_string\n continue\n if sources:\n if reinstall:\n to_reinstall[package_name] = version_string\n continue\n elif 'lowpkg.bin_pkg_info' not in __salt__:\n continue\n # Metadata parser is available, cache the file and derive the\n # package's name and version\n err = 'Unable to cache {0}: {1}'\n try:\n cached_path = __salt__['cp.cache_file'](version_string, saltenv=kwargs['saltenv'])\n except CommandExecutionError as exc:\n problems.append(err.format(version_string, exc))\n continue\n if not cached_path:\n problems.append(err.format(version_string, 'file not found'))\n continue\n elif not os.path.exists(cached_path):\n problems.append('{0} does not exist on minion'.format(version_string))\n continue\n source_info = __salt__['lowpkg.bin_pkg_info'](cached_path)\n if source_info is None:\n warnings.append('Failed to parse metadata for {0}'.format(version_string))\n continue\n else:\n verstr = source_info['version']\n else:\n verstr = version_string\n if reinstall:\n to_reinstall[package_name] = version_string\n continue\n if not __salt__['pkg_resource.check_extra_requirements'](package_name, version_string):\n targets[package_name] = version_string\n continue\n # No version specified and pkg is installed\n elif __salt__['pkg_resource.version_clean'](version_string) is None:\n if (not reinstall) and pkg_verify:\n try:\n verify_result = __salt__['pkg.verify'](\n package_name,\n ignore_types=ignore_types,\n verify_options=verify_options,\n **kwargs\n )\n except (CommandExecutionError, SaltInvocationError) as exc:\n failed_verify = exc.strerror\n continue\n if verify_result:\n to_reinstall[package_name] = version_string\n altered_files[package_name] = verify_result\n continue\n version_fulfilled = False\n allow_updates = bool(not sources and kwargs.get('allow_updates'))\n try:\n version_fulfilled = _fulfills_version_string(cver, verstr, ignore_epoch=ignore_epoch, allow_updates=allow_updates)\n except CommandExecutionError as exc:\n problems.append(exc.strerror)\n continue\n\n # Compare desired version against installed version.\n version_spec = True\n if not version_fulfilled:\n if reinstall:\n to_reinstall[package_name] = version_string\n else:\n version_conditions = _parse_version_string(version_string)\n if pkg_verify and any(oper == '==' for oper, version in version_conditions):\n try:\n verify_result = __salt__['pkg.verify'](\n package_name,\n ignore_types=ignore_types,\n verify_options=verify_options,\n **kwargs\n )\n except (CommandExecutionError, SaltInvocationError) as exc:\n failed_verify = exc.strerror\n continue\n if verify_result:\n to_reinstall[package_name] = version_string\n altered_files[package_name] = verify_result\n else:\n log.debug(\n 'Current version (%s) did not match desired version '\n 'specification (%s), adding to installation targets',\n cver, version_string\n )\n targets[package_name] = version_string\n\n if failed_verify:\n problems.append(failed_verify)\n\n if problems:\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ' '.join(problems)}\n\n if not any((targets, to_unpurge, to_reinstall)):\n # All specified packages are installed\n msg = 'All specified packages are already installed{0}'\n msg = msg.format(\n ' and are at the desired version' if version_spec and not sources\n else ''\n )\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': msg}\n if warnings:\n ret.setdefault('warnings', []).extend(warnings)\n return ret\n\n return (desired, targets, to_unpurge, to_reinstall, altered_files,\n warnings, was_refreshed)", "response": "Find the packages that need to be installed and install them."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _verify_install(desired, new_pkgs, ignore_epoch=False, new_caps=None):\n '''\n Determine whether or not the installed packages match what was requested in\n the SLS file.\n '''\n ok = []\n failed = []\n if not new_caps:\n new_caps = dict()\n for pkgname, pkgver in desired.items():\n # FreeBSD pkg supports `openjdk` and `java/openjdk7` package names.\n # Homebrew for Mac OSX does something similar with tap names\n # prefixing package names, separated with a slash.\n has_origin = '/' in pkgname\n\n if __grains__['os'] == 'FreeBSD' and has_origin:\n cver = [k for k, v in six.iteritems(new_pkgs) if v['origin'] == pkgname]\n elif __grains__['os'] == 'MacOS' and has_origin:\n cver = new_pkgs.get(pkgname, new_pkgs.get(pkgname.split('/')[-1]))\n elif __grains__['os'] == 'OpenBSD':\n cver = new_pkgs.get(pkgname.split('%')[0])\n elif __grains__['os_family'] == 'Debian':\n cver = new_pkgs.get(pkgname.split('=')[0])\n else:\n cver = new_pkgs.get(pkgname)\n if not cver and pkgname in new_caps:\n cver = new_pkgs.get(new_caps.get(pkgname)[0])\n\n if not cver:\n failed.append(pkgname)\n continue\n elif pkgver == 'latest':\n ok.append(pkgname)\n continue\n elif not __salt__['pkg_resource.version_clean'](pkgver):\n ok.append(pkgname)\n continue\n elif pkgver.endswith(\"*\") and cver[0].startswith(pkgver[:-1]):\n ok.append(pkgname)\n continue\n if _fulfills_version_string(cver, pkgver, ignore_epoch=ignore_epoch):\n ok.append(pkgname)\n else:\n failed.append(pkgname)\n return ok, failed", "response": "Verify that the installed packages match what was requested in the SLS file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_desired_pkg(name, desired):\n '''\n Helper function that retrieves and nicely formats the desired pkg (and\n version if specified) so that helpful information can be printed in the\n comment for the state.\n '''\n if not desired[name] or desired[name].startswith(('<', '>', '=')):\n oper = ''\n else:\n oper = '='\n return '{0}{1}{2}'.format(name, oper,\n '' if not desired[name] else desired[name])", "response": "Helper function that retrieves and nicely formats the desired pkg"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nperforming platform - specific checks on desired packages", "response": "def _preflight_check(desired, fromrepo, **kwargs):\n '''\n Perform platform-specific checks on desired packages\n '''\n if 'pkg.check_db' not in __salt__:\n return {}\n ret = {'suggest': {}, 'no_suggest': []}\n pkginfo = __salt__['pkg.check_db'](\n *list(desired.keys()), fromrepo=fromrepo, **kwargs\n )\n for pkgname in pkginfo:\n if pkginfo[pkgname]['found'] is False:\n if pkginfo[pkgname]['suggestions']:\n ret['suggest'][pkgname] = pkginfo[pkgname]['suggestions']\n else:\n ret['no_suggest'].append(pkgname)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _nested_output(obj):\n '''\n Serialize obj and format for output\n '''\n nested.__opts__ = __opts__\n ret = nested.output(obj).rstrip()\n return ret", "response": "Serialize obj and format for output\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _resolve_capabilities(pkgs, refresh=False, **kwargs):\n '''\n Resolve capabilities in ``pkgs`` and exchange them with real package\n names, when the result is distinct.\n This feature can be turned on while setting the paramter\n ``resolve_capabilities`` to True.\n\n Return the input dictionary with replaced capability names and as\n second return value a bool which say if a refresh need to be run.\n\n In case of ``resolve_capabilities`` is False (disabled) or not\n supported by the implementation the input is returned unchanged.\n '''\n if not pkgs or 'pkg.resolve_capabilities' not in __salt__:\n return pkgs, refresh\n\n ret = __salt__['pkg.resolve_capabilities'](pkgs, refresh=refresh, **kwargs)\n return ret, False", "response": "Resolve capabilities in pkgs and exchange them with real package\n names."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nensures that the named package is installed and that it is compatible with the version number specified.", "response": "def installed(\n name,\n version=None,\n refresh=None,\n fromrepo=None,\n skip_verify=False,\n skip_suggestions=False,\n pkgs=None,\n sources=None,\n allow_updates=False,\n pkg_verify=False,\n normalize=True,\n ignore_epoch=False,\n reinstall=False,\n update_holds=False,\n bypass_file=None,\n bypass_file_contains=None,\n **kwargs):\n '''\n Ensure that the package is installed, and that it is the correct version\n (if specified).\n\n :param str name:\n The name of the package to be installed. This parameter is ignored if\n either \"pkgs\" or \"sources\" is used. Additionally, please note that this\n option can only be used to install packages from a software repository.\n To install a package file manually, use the \"sources\" option detailed\n below.\n\n :param str version:\n Install a specific version of a package. This option is ignored if\n \"sources\" is used. Currently, this option is supported\n for the following pkg providers: :mod:`apt <salt.modules.aptpkg>`,\n :mod:`ebuild <salt.modules.ebuild>`,\n :mod:`pacman <salt.modules.pacman>`,\n :mod:`pkgin <salt.modules.pkgin>`,\n :mod:`win_pkg <salt.modules.win_pkg>`,\n :mod:`yumpkg <salt.modules.yumpkg>`, and\n :mod:`zypper <salt.modules.zypper>`. The version number includes the\n release designation where applicable, to allow Salt to target a\n specific release of a given version. When in doubt, using the\n ``pkg.latest_version`` function for an uninstalled package will tell\n you the version available.\n\n .. code-block:: bash\n\n # salt myminion pkg.latest_version vim-enhanced\n myminion:\n 2:7.4.160-1.el7\n\n .. important::\n As of version 2015.8.7, for distros which use yum/dnf, packages\n which have a version with a nonzero epoch (that is, versions which\n start with a number followed by a colon like in the\n ``pkg.latest_version`` output above) must have the epoch included\n when specifying the version number. For example:\n\n .. code-block:: yaml\n\n vim-enhanced:\n pkg.installed:\n - version: 2:7.4.160-1.el7\n\n In version 2015.8.9, an **ignore_epoch** argument has been added to\n :py:mod:`pkg.installed <salt.states.pkg.installed>`,\n :py:mod:`pkg.removed <salt.states.pkg.removed>`, and\n :py:mod:`pkg.purged <salt.states.pkg.purged>` states, which\n causes the epoch to be disregarded when the state checks to see if\n the desired version was installed.\n\n Also, while this function is not yet implemented for all pkg frontends,\n :mod:`pkg.list_repo_pkgs <salt.modules.yumpkg.list_repo_pkgs>` will\n show all versions available in the various repositories for a given\n package, irrespective of whether or not it is installed.\n\n .. code-block:: bash\n\n # salt myminion pkg.list_repo_pkgs bash\n myminion:\n ----------\n bash:\n - 4.2.46-21.el7_3\n - 4.2.46-20.el7_2\n\n This function was first added for :mod:`pkg.list_repo_pkgs\n <salt.modules.yumpkg.list_repo_pkgs>` in 2014.1.0, and was expanded to\n :py:func:`Debian/Ubuntu <salt.modules.aptpkg.list_repo_pkgs>` and\n :py:func:`Arch Linux <salt.modules.pacman.list_repo_pkgs>`-based\n distros in the 2017.7.0 release.\n\n The version strings returned by either of these functions can be used\n as version specifiers in pkg states.\n\n You can install a specific version when using the ``pkgs`` argument by\n including the version after the package:\n\n .. code-block:: yaml\n\n common_packages:\n pkg.installed:\n - pkgs:\n - unzip\n - dos2unix\n - salt-minion: 2015.8.5-1.el6\n\n If the version given is the string ``latest``, the latest available\n package version will be installed \u00e0 la ``pkg.latest``.\n\n **WILDCARD VERSIONS**\n\n As of the 2017.7.0 release, this state now supports wildcards in\n package versions for SUSE SLES/Leap/Tumbleweed, Debian/Ubuntu,\n RHEL/CentOS, Arch Linux, and their derivatives. Using wildcards can be\n useful for packages where the release name is built into the version in\n some way, such as for RHEL/CentOS which typically has version numbers\n like ``1.2.34-5.el7``. An example of the usage for this would be:\n\n .. code-block:: yaml\n\n mypkg:\n pkg.installed:\n - version: '1.2.34*'\n\n Keep in mind that using wildcard versions will result in a slower state\n run since Salt must gather the available versions of the specified\n packages and figure out which of them match the specified wildcard\n expression.\n\n :param bool refresh:\n This parameter controls whether or not the package repo database is\n updated prior to installing the requested package(s).\n\n If ``True``, the package database will be refreshed (``apt-get\n update`` or equivalent, depending on platform) before installing.\n\n If ``False``, the package database will *not* be refreshed before\n installing.\n\n If unset, then Salt treats package database refreshes differently\n depending on whether or not a ``pkg`` state has been executed already\n during the current Salt run. Once a refresh has been performed in a\n ``pkg`` state, for the remainder of that Salt run no other refreshes\n will be performed for ``pkg`` states which do not explicitly set\n ``refresh`` to ``True``. This prevents needless additional refreshes\n from slowing down the Salt run.\n\n :param str cache_valid_time:\n\n .. versionadded:: 2016.11.0\n\n This parameter sets the value in seconds after which the cache is\n marked as invalid, and a cache update is necessary. This overwrites\n the ``refresh`` parameter's default behavior.\n\n Example:\n\n .. code-block:: yaml\n\n httpd:\n pkg.installed:\n - fromrepo: mycustomrepo\n - skip_verify: True\n - skip_suggestions: True\n - version: 2.0.6~ubuntu3\n - refresh: True\n - cache_valid_time: 300\n - allow_updates: True\n - hold: False\n\n In this case, a refresh will not take place for 5 minutes since the last\n ``apt-get update`` was executed on the system.\n\n .. note::\n\n This parameter is available only on Debian based distributions and\n has no effect on the rest.\n\n :param str fromrepo:\n Specify a repository from which to install\n\n .. note::\n\n Distros which use APT (Debian, Ubuntu, etc.) do not have a concept\n of repositories, in the same way as YUM-based distros do. When a\n source is added, it is assigned to a given release. Consider the\n following source configuration:\n\n .. code-block:: text\n\n deb http://ppa.launchpad.net/saltstack/salt/ubuntu precise main\n\n The packages provided by this source would be made available via\n the ``precise`` release, therefore ``fromrepo`` would need to be\n set to ``precise`` for Salt to install the package from this\n source.\n\n Having multiple sources in the same release may result in the\n default install candidate being newer than what is desired. If this\n is the case, the desired version must be specified using the\n ``version`` parameter.\n\n If the ``pkgs`` parameter is being used to install multiple\n packages in the same state, then instead of using ``version``,\n use the method of version specification described in the **Multiple\n Package Installation Options** section below.\n\n Running the shell command ``apt-cache policy pkgname`` on a minion\n can help elucidate the APT configuration and aid in properly\n configuring states:\n\n .. code-block:: bash\n\n root@saltmaster:~# salt ubuntu01 cmd.run 'apt-cache policy ffmpeg'\n ubuntu01:\n ffmpeg:\n Installed: (none)\n Candidate: 7:0.10.11-1~precise1\n Version table:\n 7:0.10.11-1~precise1 0\n 500 http://ppa.launchpad.net/jon-severinsson/ffmpeg/ubuntu/ precise/main amd64 Packages\n 4:0.8.10-0ubuntu0.12.04.1 0\n 500 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main amd64 Packages\n 500 http://security.ubuntu.com/ubuntu/ precise-security/main amd64 Packages\n 4:0.8.1-0ubuntu1 0\n 500 http://us.archive.ubuntu.com/ubuntu/ precise/main amd64 Packages\n\n The release is located directly after the source's URL. The actual\n release name is the part before the slash, so to install version\n **4:0.8.10-0ubuntu0.12.04.1** either ``precise-updates`` or\n ``precise-security`` could be used for the ``fromrepo`` value.\n\n :param bool skip_verify:\n Skip the GPG verification check for the package to be installed\n\n :param bool skip_suggestions:\n Force strict package naming. Disables lookup of package alternatives.\n\n .. versionadded:: 2014.1.1\n\n :param bool resolve_capabilities:\n Turn on resolving capabilities. This allow one to name \"provides\" or alias names for packages.\n\n .. versionadded:: 2018.3.0\n\n :param bool allow_updates:\n Allow the package to be updated outside Salt's control (e.g. auto\n updates on Windows). This means a package on the Minion can have a\n newer version than the latest available in the repository without\n enforcing a re-installation of the package.\n\n .. versionadded:: 2014.7.0\n\n Example:\n\n .. code-block:: yaml\n\n httpd:\n pkg.installed:\n - fromrepo: mycustomrepo\n - skip_verify: True\n - skip_suggestions: True\n - version: 2.0.6~ubuntu3\n - refresh: True\n - allow_updates: True\n - hold: False\n\n :param bool pkg_verify:\n\n .. versionadded:: 2014.7.0\n\n For requested packages that are already installed and would not be\n targeted for upgrade or downgrade, use pkg.verify to determine if any\n of the files installed by the package have been altered. If files have\n been altered, the reinstall option of pkg.install is used to force a\n reinstall. Types to ignore can be passed to pkg.verify. Additionally,\n ``verify_options`` can be used to modify further the behavior of\n pkg.verify. See examples below. Currently, this option is supported\n for the following pkg providers: :mod:`yumpkg <salt.modules.yumpkg>`.\n\n Examples:\n\n .. code-block:: yaml\n\n httpd:\n pkg.installed:\n - version: 2.2.15-30.el6.centos\n - pkg_verify: True\n\n .. code-block:: yaml\n\n mypkgs:\n pkg.installed:\n - pkgs:\n - foo\n - bar: 1.2.3-4\n - baz\n - pkg_verify:\n - ignore_types:\n - config\n - doc\n\n .. code-block:: yaml\n\n mypkgs:\n pkg.installed:\n - pkgs:\n - foo\n - bar: 1.2.3-4\n - baz\n - pkg_verify:\n - ignore_types:\n - config\n - doc\n - verify_options:\n - nodeps\n - nofiledigest\n\n :param list ignore_types:\n List of types to ignore when verifying the package\n\n .. versionadded:: 2014.7.0\n\n :param list verify_options:\n List of additional options to pass when verifying the package. These\n options will be added to the ``rpm -V`` command, prepended with ``--``\n (for example, when ``nodeps`` is passed in this option, ``rpm -V`` will\n be run with ``--nodeps``).\n\n .. versionadded:: 2016.11.0\n\n :param bool normalize:\n Normalize the package name by removing the architecture, if the\n architecture of the package is different from the architecture of the\n operating system. The ability to disable this behavior is useful for\n poorly-created packages which include the architecture as an actual\n part of the name, such as kernel modules which match a specific kernel\n version.\n\n .. versionadded:: 2014.7.0\n\n Example:\n\n .. code-block:: yaml\n\n gpfs.gplbin-2.6.32-279.31.1.el6.x86_64:\n pkg.installed:\n - normalize: False\n\n :param bool ignore_epoch:\n When a package version contains an non-zero epoch (e.g.\n ``1:3.14.159-2.el7``, and a specific version of a package is desired,\n set this option to ``True`` to ignore the epoch when comparing\n versions. This allows for the following SLS to be used:\n\n .. code-block:: yaml\n\n # Actual vim-enhanced version: 2:7.4.160-1.el7\n vim-enhanced:\n pkg.installed:\n - version: 7.4.160-1.el7\n - ignore_epoch: True\n\n Without this option set to ``True`` in the above example, the package\n would be installed, but the state would report as failed because the\n actual installed version would be ``2:7.4.160-1.el7``. Alternatively,\n this option can be left as ``False`` and the full version string (with\n epoch) can be specified in the SLS file:\n\n .. code-block:: yaml\n\n vim-enhanced:\n pkg.installed:\n - version: 2:7.4.160-1.el7\n\n .. versionadded:: 2015.8.9\n\n |\n\n **MULTIPLE PACKAGE INSTALLATION OPTIONS: (not supported in pkgng)**\n\n :param list pkgs:\n A list of packages to install from a software repository. All packages\n listed under ``pkgs`` will be installed via a single command.\n\n .. code-block:: yaml\n\n mypkgs:\n pkg.installed:\n - pkgs:\n - foo\n - bar\n - baz\n - hold: True\n\n ``NOTE:`` For :mod:`apt <salt.modules.aptpkg>`,\n :mod:`ebuild <salt.modules.ebuild>`,\n :mod:`pacman <salt.modules.pacman>`,\n :mod:`winrepo <salt.modules.win_pkg>`,\n :mod:`yumpkg <salt.modules.yumpkg>`, and\n :mod:`zypper <salt.modules.zypper>`,\n version numbers can be specified\n in the ``pkgs`` argument. For example:\n\n .. code-block:: yaml\n\n mypkgs:\n pkg.installed:\n - pkgs:\n - foo\n - bar: 1.2.3-4\n - baz\n\n Additionally, :mod:`ebuild <salt.modules.ebuild>`, :mod:`pacman\n <salt.modules.pacman>`, :mod:`zypper <salt.modules.zypper>`,\n :mod:`yum/dnf <salt.modules.yumpkg>`, and :mod:`apt\n <salt.modules.aptpkg>` support the ``<``, ``<=``, ``>=``, and ``>``\n operators for more control over what versions will be installed. For\n example:\n\n .. code-block:: yaml\n\n mypkgs:\n pkg.installed:\n - pkgs:\n - foo\n - bar: '>=1.2.3-4'\n - baz\n\n ``NOTE:`` When using comparison operators, the expression must be enclosed\n in quotes to avoid a YAML render error.\n\n With :mod:`ebuild <salt.modules.ebuild>` is also possible to specify a\n use flag list and/or if the given packages should be in\n package.accept_keywords file and/or the overlay from which you want the\n package to be installed. For example:\n\n .. code-block:: yaml\n\n mypkgs:\n pkg.installed:\n - pkgs:\n - foo: '~'\n - bar: '~>=1.2:slot::overlay[use,-otheruse]'\n - baz\n\n :param list sources:\n A list of packages to install, along with the source URI or local path\n from which to install each package. In the example below, ``foo``,\n ``bar``, ``baz``, etc. refer to the name of the package, as it would\n appear in the output of the ``pkg.version`` or ``pkg.list_pkgs`` salt\n CLI commands.\n\n .. code-block:: yaml\n\n mypkgs:\n pkg.installed:\n - sources:\n - foo: salt://rpms/foo.rpm\n - bar: http://somesite.org/bar.rpm\n - baz: ftp://someothersite.org/baz.rpm\n - qux: /minion/path/to/qux.rpm\n\n **PLATFORM-SPECIFIC ARGUMENTS**\n\n These are specific to each OS. If it does not apply to the execution\n module for your OS, it is ignored.\n\n :param bool hold:\n Force the package to be held at the current installed version.\n Currently works with YUM/DNF & APT based systems.\n\n .. versionadded:: 2014.7.0\n\n :param bool update_holds:\n If ``True``, and this function would update the package version, any\n packages which are being held will be temporarily unheld so that they\n can be updated. Otherwise, if this function attempts to update a held\n package, the held package(s) will be skipped and the state will fail.\n By default, this parameter is set to ``False``.\n\n Currently works with YUM/DNF & APT based systems.\n\n .. versionadded:: 2016.11.0\n\n :param list names:\n A list of packages to install from a software repository. Each package\n will be installed individually by the package manager.\n\n .. warning::\n\n Unlike ``pkgs``, the ``names`` parameter cannot specify a version.\n In addition, it makes a separate call to the package management\n frontend to install each package, whereas ``pkgs`` makes just a\n single call. It is therefore recommended to use ``pkgs`` instead of\n ``names`` to install multiple packages, both for the additional\n features and the performance improvement that it brings.\n\n :param bool install_recommends:\n Whether to install the packages marked as recommended. Default is\n ``True``. Currently only works with APT-based systems.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: yaml\n\n httpd:\n pkg.installed:\n - install_recommends: False\n\n :param bool only_upgrade:\n Only upgrade the packages, if they are already installed. Default is\n ``False``. Currently only works with APT-based systems.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: yaml\n\n httpd:\n pkg.installed:\n - only_upgrade: True\n\n .. note::\n If this parameter is set to True and the package is not already\n installed, the state will fail.\n\n :param bool report_reboot_exit_codes:\n If the installer exits with a recognized exit code indicating that\n a reboot is required, the module function\n\n *win_system.set_reboot_required_witnessed*\n\n will be called, preserving the knowledge of this event\n for the remainder of the current boot session. For the time being,\n ``3010`` is the only recognized exit code,\n but this is subject to future refinement.\n The value of this param\n defaults to ``True``. This parameter has no effect\n on non-Windows systems.\n\n .. versionadded:: 2016.11.0\n\n .. code-block:: yaml\n\n ms vcpp installed:\n pkg.installed:\n - name: ms-vcpp\n - version: 10.0.40219\n - report_reboot_exit_codes: False\n\n :param str bypass_file:\n If you wish to bypass the full package validation process, you can\n specify a file related to the installed pacakge as a way to\n validate the pacakge has already been installed. A good example\n would be a config file that is deployed with the package. Another\n bypass_file could be ``/run/salt-minon.pid``.\n\n .. code-block:: yaml\n\n install_ntp:\n pkg.installed:\n - name: ntp\n - bypass_file: /etc/ntp.conf\n\n The use case for this feature is when running salt at significant scale.\n Each state that has a requisite for a ``pkg.installed`` will have salt\n querying the package manager of the system. Compared to simple diff\n checks, querying the pacakge manager is a lengthy process. This feature\n is an attempt to reduce the run time of states. If only a config change\n is being made but you wish to keep all of the self resolving requisites\n this bypasses the lenghty cost of the package manager. The assumption is\n that if this file is present, the package should already be installed.\n\n :param str bypass_file_contains:\n This option can only be used in conjunction with the ``bypass_file``\n option. It is to provide a second layer of validation before bypassing\n the ``pkg.installed`` process.\n\n .. code-block:: yaml\n\n install_ntp:\n pkg.installed:\n - name: ntp\n - bypass_file: /etc/ntp.conf\n - bypass_file_contains: version-20181218\n\n The will have salt check to see if the file contains the specified\n string. If the value is found, the ``pkg.installed`` process will be\n bypassed under the assumption that two pieces of validation have passed\n and the package is already installed.\n\n .. warning::\n\n Do not try and use ``{{ salt['pkg.version']('ntp') }}`` in a jinja\n template as part of your bypass_file_contains match. This will\n trigger a ``pkg.version`` lookup with the pacakge manager and negate\n any time saved by trying to use the bypass feature.\n\n :return:\n A dictionary containing the state of the software installation\n :rtype dict:\n\n .. note::\n\n The ``pkg.installed`` state supports the usage of ``reload_modules``.\n This functionality allows you to force Salt to reload all modules. In\n many cases, Salt is clever enough to transparently reload the modules.\n For example, if you install a package, Salt reloads modules because some\n other module or state might require the package which was installed.\n However, there are some edge cases where this may not be the case, which\n is what ``reload_modules`` is meant to resolve.\n\n You should only use ``reload_modules`` if your ``pkg.installed`` does some\n sort of installation where if you do not reload the modules future items\n in your state which rely on the software being installed will fail. Please\n see the :ref:`Reloading Modules <reloading-modules>` documentation for more\n information.\n\n '''\n if not pkgs and isinstance(pkgs, list):\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'No packages to install provided'}\n\n # If just a name (and optionally a version) is passed, just pack them into\n # the pkgs argument.\n if name and not any((pkgs, sources)):\n if version:\n pkgs = [{name: version}]\n version = None\n else:\n pkgs = [name]\n\n kwargs['saltenv'] = __env__\n refresh = salt.utils.pkg.check_refresh(__opts__, refresh)\n\n if bypass_file is not None and bypass_file_contains is not None:\n if os.path.isfile(bypass_file):\n with salt.utils.fopen(bypass_file) as bypass_file_open:\n if bypass_file_contains in bypass_file_open.read():\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'pkg.installed was bypassed as {} was present in {}'.format(bypass_file_contains, bypass_file)}\n if bypass_file is not None and bypass_file_contains is None:\n if os.path.isfile(bypass_file):\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'pkg.installed was bypassed as bypass_file {} was present'.format(bypass_file)}\n\n # check if capabilities should be checked and modify the requested packages\n # accordingly.\n if pkgs:\n pkgs, refresh = _resolve_capabilities(pkgs, refresh=refresh, **kwargs)\n\n if not isinstance(pkg_verify, list):\n pkg_verify = pkg_verify is True\n if (pkg_verify or isinstance(pkg_verify, list)) \\\n and 'pkg.verify' not in __salt__:\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': 'pkg.verify not implemented'}\n\n if not isinstance(version, six.string_types) and version is not None:\n version = six.text_type(version)\n\n kwargs['allow_updates'] = allow_updates\n\n result = _find_install_targets(name, version, pkgs, sources,\n fromrepo=fromrepo,\n skip_suggestions=skip_suggestions,\n pkg_verify=pkg_verify,\n normalize=normalize,\n ignore_epoch=ignore_epoch,\n reinstall=reinstall,\n refresh=refresh,\n **kwargs)\n\n try:\n (desired, targets, to_unpurge, to_reinstall,\n altered_files, warnings, was_refreshed) = result\n if was_refreshed:\n refresh = False\n except ValueError:\n # _find_install_targets() found no targets or encountered an error\n\n # check that the hold function is available\n if 'pkg.hold' in __salt__ and 'hold' in kwargs:\n try:\n action = 'pkg.hold' if kwargs['hold'] else 'pkg.unhold'\n hold_ret = __salt__[action](\n name=name, pkgs=pkgs, sources=sources\n )\n except (CommandExecutionError, SaltInvocationError) as exc:\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': six.text_type(exc)}\n\n if 'result' in hold_ret and not hold_ret['result']:\n return {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': 'An error was encountered while '\n 'holding/unholding package(s): {0}'\n .format(hold_ret['comment'])}\n else:\n modified_hold = [hold_ret[x] for x in hold_ret\n if hold_ret[x]['changes']]\n not_modified_hold = [hold_ret[x] for x in hold_ret\n if not hold_ret[x]['changes']\n and hold_ret[x]['result']]\n failed_hold = [hold_ret[x] for x in hold_ret\n if not hold_ret[x]['result']]\n\n for i in modified_hold:\n result['comment'] += '.\\n{0}'.format(i['comment'])\n result['result'] = i['result']\n result['changes'][i['name']] = i['changes']\n\n for i in not_modified_hold:\n result['comment'] += '.\\n{0}'.format(i['comment'])\n result['result'] = i['result']\n\n for i in failed_hold:\n result['comment'] += '.\\n{0}'.format(i['comment'])\n result['result'] = i['result']\n return result\n\n if to_unpurge and 'lowpkg.unpurge' not in __salt__:\n ret = {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': 'lowpkg.unpurge not implemented'}\n if warnings:\n ret.setdefault('warnings', []).extend(warnings)\n return ret\n\n # Remove any targets not returned by _find_install_targets\n if pkgs:\n pkgs = [dict([(x, y)]) for x, y in six.iteritems(targets)]\n pkgs.extend([dict([(x, y)]) for x, y in six.iteritems(to_reinstall)])\n elif sources:\n oldsources = sources\n sources = [x for x in oldsources\n if next(iter(list(x.keys()))) in targets]\n sources.extend([x for x in oldsources\n if next(iter(list(x.keys()))) in to_reinstall])\n\n comment = []\n if __opts__['test']:\n if targets:\n if sources:\n summary = ', '.join(targets)\n else:\n summary = ', '.join([_get_desired_pkg(x, targets)\n for x in targets])\n comment.append('The following packages would be '\n 'installed/updated: {0}'.format(summary))\n if to_unpurge:\n comment.append(\n 'The following packages would have their selection status '\n 'changed from \\'purge\\' to \\'install\\': {0}'\n .format(', '.join(to_unpurge))\n )\n if to_reinstall:\n # Add a comment for each package in to_reinstall with its\n # pkg.verify output\n if reinstall:\n reinstall_targets = []\n for reinstall_pkg in to_reinstall:\n if sources:\n reinstall_targets.append(reinstall_pkg)\n else:\n reinstall_targets.append(\n _get_desired_pkg(reinstall_pkg, to_reinstall)\n )\n msg = 'The following packages would be reinstalled: '\n msg += ', '.join(reinstall_targets)\n comment.append(msg)\n else:\n for reinstall_pkg in to_reinstall:\n if sources:\n pkgstr = reinstall_pkg\n else:\n pkgstr = _get_desired_pkg(reinstall_pkg, to_reinstall)\n comment.append(\n 'Package \\'{0}\\' would be reinstalled because the '\n 'following files have been altered:'.format(pkgstr)\n )\n comment.append(\n _nested_output(altered_files[reinstall_pkg])\n )\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': '\\n'.join(comment)}\n if warnings:\n ret.setdefault('warnings', []).extend(warnings)\n return ret\n\n changes = {'installed': {}}\n modified_hold = None\n not_modified_hold = None\n failed_hold = None\n if targets or to_reinstall:\n try:\n pkg_ret = __salt__['pkg.install'](name=None,\n refresh=refresh,\n version=version,\n fromrepo=fromrepo,\n skip_verify=skip_verify,\n pkgs=pkgs,\n sources=sources,\n reinstall=bool(to_reinstall),\n normalize=normalize,\n update_holds=update_holds,\n ignore_epoch=ignore_epoch,\n **kwargs)\n except CommandExecutionError as exc:\n ret = {'name': name, 'result': False}\n if exc.info:\n # Get information for state return from the exception.\n ret['changes'] = exc.info.get('changes', {})\n ret['comment'] = exc.strerror_without_changes\n else:\n ret['changes'] = {}\n ret['comment'] = ('An error was encountered while installing '\n 'package(s): {0}'.format(exc))\n if warnings:\n ret.setdefault('warnings', []).extend(warnings)\n return ret\n\n if refresh:\n refresh = False\n\n if isinstance(pkg_ret, dict):\n changes['installed'].update(pkg_ret)\n elif isinstance(pkg_ret, six.string_types):\n comment.append(pkg_ret)\n # Code below will be looking for a dictionary. If this is a string\n # it means that there was an exception raised and that no packages\n # changed, so now that we have added this error to the comments we\n # set this to an empty dictionary so that the code below which\n # checks reinstall targets works.\n pkg_ret = {}\n\n if 'pkg.hold' in __salt__ and 'hold' in kwargs:\n try:\n action = 'pkg.hold' if kwargs['hold'] else 'pkg.unhold'\n hold_ret = __salt__[action](\n name=name, pkgs=desired\n )\n except (CommandExecutionError, SaltInvocationError) as exc:\n comment.append(six.text_type(exc))\n ret = {'name': name,\n 'changes': changes,\n 'result': False,\n 'comment': '\\n'.join(comment)}\n if warnings:\n ret.setdefault('warnings', []).extend(warnings)\n return ret\n else:\n if 'result' in hold_ret and not hold_ret['result']:\n ret = {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': 'An error was encountered while '\n 'holding/unholding package(s): {0}'\n .format(hold_ret['comment'])}\n if warnings:\n ret.setdefault('warnings', []).extend(warnings)\n return ret\n else:\n modified_hold = [hold_ret[x] for x in hold_ret\n if hold_ret[x]['changes']]\n not_modified_hold = [hold_ret[x] for x in hold_ret\n if not hold_ret[x]['changes']\n and hold_ret[x]['result']]\n failed_hold = [hold_ret[x] for x in hold_ret\n if not hold_ret[x]['result']]\n\n if to_unpurge:\n changes['purge_desired'] = __salt__['lowpkg.unpurge'](*to_unpurge)\n\n # Analyze pkg.install results for packages in targets\n if sources:\n modified = [x for x in changes['installed'] if x in targets]\n not_modified = [x for x in desired\n if x not in targets\n and x not in to_reinstall]\n failed = [x for x in targets if x not in modified]\n else:\n if __grains__['os'] == 'FreeBSD':\n kwargs['with_origin'] = True\n new_pkgs = __salt__['pkg.list_pkgs'](versions_as_list=True, **kwargs)\n if kwargs.get('resolve_capabilities', False) and 'pkg.list_provides' in __salt__:\n new_caps = __salt__['pkg.list_provides'](**kwargs)\n else:\n new_caps = {}\n ok, failed = _verify_install(desired, new_pkgs,\n ignore_epoch=ignore_epoch,\n new_caps=new_caps)\n modified = [x for x in ok if x in targets]\n not_modified = [x for x in ok\n if x not in targets\n and x not in to_reinstall]\n failed = [x for x in failed if x in targets]\n\n # If there was nothing unpurged, just set the changes dict to the contents\n # of changes['installed'].\n if not changes.get('purge_desired'):\n changes = changes['installed']\n\n if modified:\n if sources:\n summary = ', '.join(modified)\n else:\n summary = ', '.join([_get_desired_pkg(x, desired)\n for x in modified])\n if len(summary) < 20:\n comment.append('The following packages were installed/updated: '\n '{0}'.format(summary))\n else:\n comment.append(\n '{0} targeted package{1} {2} installed/updated.'.format(\n len(modified),\n 's' if len(modified) > 1 else '',\n 'were' if len(modified) > 1 else 'was'\n )\n )\n\n if modified_hold:\n for i in modified_hold:\n change_name = i['name']\n if change_name in changes:\n comment.append(i['comment'])\n if changes[change_name]['new']:\n changes[change_name]['new'] += '\\n'\n changes[change_name]['new'] += '{0}'.format(i['changes']['new'])\n if changes[change_name]['old']:\n changes[change_name]['old'] += '\\n'\n changes[change_name]['old'] += '{0}'.format(i['changes']['old'])\n else:\n comment.append(i['comment'])\n changes[change_name] = {}\n changes[change_name]['new'] = '{0}'.format(i['changes']['new'])\n\n # Any requested packages that were not targeted for install or reinstall\n if not_modified:\n if sources:\n summary = ', '.join(not_modified)\n else:\n summary = ', '.join([_get_desired_pkg(x, desired)\n for x in not_modified])\n if len(not_modified) <= 20:\n comment.append('The following packages were already installed: '\n '{0}'.format(summary))\n else:\n comment.append(\n '{0} targeted package{1} {2} already installed'.format(\n len(not_modified),\n 's' if len(not_modified) > 1 else '',\n 'were' if len(not_modified) > 1 else 'was'\n )\n )\n\n if not_modified_hold:\n for i in not_modified_hold:\n comment.append(i['comment'])\n\n result = True\n\n if failed:\n if sources:\n summary = ', '.join(failed)\n else:\n summary = ', '.join([_get_desired_pkg(x, desired)\n for x in failed])\n comment.insert(0, 'The following packages failed to '\n 'install/update: {0}'.format(summary))\n result = False\n\n if failed_hold:\n for i in failed_hold:\n comment.append(i['comment'])\n result = False\n\n # Get the ignore_types list if any from the pkg_verify argument\n if isinstance(pkg_verify, list) \\\n and any(x.get('ignore_types') is not None\n for x in pkg_verify\n if isinstance(x, _OrderedDict)\n and 'ignore_types' in x):\n ignore_types = next(x.get('ignore_types')\n for x in pkg_verify\n if 'ignore_types' in x)\n else:\n ignore_types = []\n\n # Get the verify_options list if any from the pkg_verify argument\n if isinstance(pkg_verify, list) \\\n and any(x.get('verify_options') is not None\n for x in pkg_verify\n if isinstance(x, _OrderedDict)\n and 'verify_options' in x):\n verify_options = next(x.get('verify_options')\n for x in pkg_verify\n if 'verify_options' in x)\n else:\n verify_options = []\n\n # Rerun pkg.verify for packages in to_reinstall to determine failed\n modified = []\n failed = []\n for reinstall_pkg in to_reinstall:\n if reinstall:\n if reinstall_pkg in pkg_ret:\n modified.append(reinstall_pkg)\n else:\n failed.append(reinstall_pkg)\n elif pkg_verify:\n # No need to wrap this in a try/except because we would already\n # have caught invalid arguments earlier.\n verify_result = __salt__['pkg.verify'](reinstall_pkg,\n ignore_types=ignore_types,\n verify_options=verify_options,\n **kwargs)\n if verify_result:\n failed.append(reinstall_pkg)\n altered_files[reinstall_pkg] = verify_result\n else:\n modified.append(reinstall_pkg)\n\n if modified:\n # Add a comment for each package in modified with its pkg.verify output\n for modified_pkg in modified:\n if sources:\n pkgstr = modified_pkg\n else:\n pkgstr = _get_desired_pkg(modified_pkg, desired)\n msg = 'Package {0} was reinstalled.'.format(pkgstr)\n if modified_pkg in altered_files:\n msg += ' The following files were remediated:'\n comment.append(msg)\n comment.append(_nested_output(altered_files[modified_pkg]))\n else:\n comment.append(msg)\n\n if failed:\n # Add a comment for each package in failed with its pkg.verify output\n for failed_pkg in failed:\n if sources:\n pkgstr = failed_pkg\n else:\n pkgstr = _get_desired_pkg(failed_pkg, desired)\n msg = ('Reinstall was not successful for package {0}.'\n .format(pkgstr))\n if failed_pkg in altered_files:\n msg += ' The following files could not be remediated:'\n comment.append(msg)\n comment.append(_nested_output(altered_files[failed_pkg]))\n else:\n comment.append(msg)\n result = False\n\n ret = {'name': name,\n 'changes': changes,\n 'result': result,\n 'comment': '\\n'.join(comment)}\n if warnings:\n ret.setdefault('warnings', []).extend(warnings)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef downloaded(name,\n version=None,\n pkgs=None,\n fromrepo=None,\n ignore_epoch=None,\n **kwargs):\n '''\n .. versionadded:: 2017.7.0\n\n Ensure that the package is downloaded, and that it is the correct version\n (if specified).\n\n Currently supported for the following pkg providers:\n :mod:`yumpkg <salt.modules.yumpkg>` and :mod:`zypper <salt.modules.zypper>`\n\n :param str name:\n The name of the package to be downloaded. This parameter is ignored if\n either \"pkgs\" is used. Additionally, please note that this option can\n only be used to download packages from a software repository.\n\n :param str version:\n Download a specific version of a package.\n\n .. important::\n As of version 2015.8.7, for distros which use yum/dnf, packages\n which have a version with a nonzero epoch (that is, versions which\n start with a number followed by a colon must have the epoch included\n when specifying the version number. For example:\n\n .. code-block:: yaml\n\n vim-enhanced:\n pkg.downloaded:\n - version: 2:7.4.160-1.el7\n\n An **ignore_epoch** argument has been added to which causes the\n epoch to be disregarded when the state checks to see if the desired\n version was installed.\n\n You can install a specific version when using the ``pkgs`` argument by\n including the version after the package:\n\n .. code-block:: yaml\n\n common_packages:\n pkg.downloaded:\n - pkgs:\n - unzip\n - dos2unix\n - salt-minion: 2015.8.5-1.el6\n\n :param bool resolve_capabilities:\n Turn on resolving capabilities. This allow one to name \"provides\" or alias names for packages.\n\n .. versionadded:: 2018.3.0\n\n CLI Example:\n\n .. code-block:: yaml\n\n zsh:\n pkg.downloaded:\n - version: 5.0.5-4.63\n - fromrepo: \"myrepository\"\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if 'pkg.list_downloaded' not in __salt__:\n ret['result'] = False\n ret['comment'] = 'The pkg.downloaded state is not available on ' \\\n 'this platform'\n return ret\n\n if not pkgs and isinstance(pkgs, list):\n ret['result'] = True\n ret['comment'] = 'No packages to download provided'\n return ret\n\n # If just a name (and optionally a version) is passed, just pack them into\n # the pkgs argument.\n if name and not pkgs:\n if version:\n pkgs = [{name: version}]\n version = None\n else:\n pkgs = [name]\n\n # It doesn't make sense here to received 'downloadonly' as kwargs\n # as we're explicitly passing 'downloadonly=True' to execution module.\n if 'downloadonly' in kwargs:\n del kwargs['downloadonly']\n\n pkgs, _refresh = _resolve_capabilities(pkgs, **kwargs)\n\n # Only downloading not yet downloaded packages\n targets = _find_download_targets(name,\n version,\n pkgs,\n fromrepo=fromrepo,\n ignore_epoch=ignore_epoch,\n **kwargs)\n if isinstance(targets, dict) and 'result' in targets:\n return targets\n elif not isinstance(targets, dict):\n ret['result'] = False\n ret['comment'] = 'An error was encountered while checking targets: ' \\\n '{0}'.format(targets)\n return ret\n\n if __opts__['test']:\n summary = ', '.join(targets)\n ret['comment'] = 'The following packages would be ' \\\n 'downloaded: {0}'.format(summary)\n return ret\n\n try:\n pkg_ret = __salt__['pkg.install'](name=name,\n pkgs=pkgs,\n version=version,\n downloadonly=True,\n fromrepo=fromrepo,\n ignore_epoch=ignore_epoch,\n **kwargs)\n ret['result'] = True\n ret['changes'].update(pkg_ret)\n except CommandExecutionError as exc:\n ret = {'name': name, 'result': False}\n if exc.info:\n # Get information for state return from the exception.\n ret['changes'] = exc.info.get('changes', {})\n ret['comment'] = exc.strerror_without_changes\n else:\n ret['changes'] = {}\n ret['comment'] = 'An error was encountered while downloading ' \\\n 'package(s): {0}'.format(exc)\n return ret\n\n new_pkgs = __salt__['pkg.list_downloaded'](**kwargs)\n ok, failed = _verify_install(targets, new_pkgs, ignore_epoch=ignore_epoch)\n\n if failed:\n summary = ', '.join([_get_desired_pkg(x, targets)\n for x in failed])\n ret['result'] = False\n ret['comment'] = 'The following packages failed to ' \\\n 'download: {0}'.format(summary)\n\n if not ret['changes'] and not ret['comment']:\n ret['result'] = True\n ret['comment'] = 'Packages are already downloaded: ' \\\n '{0}'.format(', '.join(targets))\n\n return ret", "response": "Download a specific version of a package from a software repository."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef patch_installed(name, advisory_ids=None, downloadonly=None, **kwargs):\n '''\n .. versionadded:: 2017.7.0\n\n Ensure that packages related to certain advisory ids are installed.\n\n Currently supported for the following pkg providers:\n :mod:`yumpkg <salt.modules.yumpkg>` and :mod:`zypper <salt.modules.zypper>`\n\n CLI Example:\n\n .. code-block:: yaml\n\n issue-foo-fixed:\n pkg.patch_installed:\n - advisory_ids:\n - SUSE-SLE-SERVER-12-SP2-2017-185\n - SUSE-SLE-SERVER-12-SP2-2017-150\n - SUSE-SLE-SERVER-12-SP2-2017-120\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if 'pkg.list_patches' not in __salt__:\n ret['result'] = False\n ret['comment'] = 'The pkg.patch_installed state is not available on ' \\\n 'this platform'\n return ret\n\n if not advisory_ids and isinstance(advisory_ids, list):\n ret['result'] = True\n ret['comment'] = 'No advisory ids provided'\n return ret\n\n # Only downloading not yet downloaded packages\n targets = _find_advisory_targets(name, advisory_ids, **kwargs)\n if isinstance(targets, dict) and 'result' in targets:\n return targets\n elif not isinstance(targets, list):\n ret['result'] = False\n ret['comment'] = 'An error was encountered while checking targets: ' \\\n '{0}'.format(targets)\n return ret\n\n if __opts__['test']:\n summary = ', '.join(targets)\n ret['comment'] = 'The following advisory patches would be ' \\\n 'downloaded: {0}'.format(summary)\n return ret\n\n try:\n pkg_ret = __salt__['pkg.install'](name=name,\n advisory_ids=advisory_ids,\n downloadonly=downloadonly,\n **kwargs)\n ret['result'] = True\n ret['changes'].update(pkg_ret)\n except CommandExecutionError as exc:\n ret = {'name': name, 'result': False}\n if exc.info:\n # Get information for state return from the exception.\n ret['changes'] = exc.info.get('changes', {})\n ret['comment'] = exc.strerror_without_changes\n else:\n ret['changes'] = {}\n ret['comment'] = ('An error was encountered while downloading '\n 'package(s): {0}'.format(exc))\n return ret\n\n if not ret['changes'] and not ret['comment']:\n status = 'downloaded' if downloadonly else 'installed'\n ret['result'] = True\n ret['comment'] = ('Advisory patch is not needed or related packages '\n 'are already {0}'.format(status))\n\n return ret", "response": "Ensure that the named advisory patch is installed on the specified packages."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\npatches the installed packages with the given advisory ids.", "response": "def patch_downloaded(name, advisory_ids=None, **kwargs):\n '''\n .. versionadded:: 2017.7.0\n\n Ensure that packages related to certain advisory ids are downloaded.\n\n Currently supported for the following pkg providers:\n :mod:`yumpkg <salt.modules.yumpkg>` and :mod:`zypper <salt.modules.zypper>`\n\n CLI Example:\n\n .. code-block:: yaml\n\n preparing-to-fix-issues:\n pkg.patch_downloaded:\n - advisory_ids:\n - SUSE-SLE-SERVER-12-SP2-2017-185\n - SUSE-SLE-SERVER-12-SP2-2017-150\n - SUSE-SLE-SERVER-12-SP2-2017-120\n '''\n if 'pkg.list_patches' not in __salt__:\n return {'name': name,\n 'result': False,\n 'changes': {},\n 'comment': 'The pkg.patch_downloaded state is not available on '\n 'this platform'}\n\n # It doesn't make sense here to received 'downloadonly' as kwargs\n # as we're explicitly passing 'downloadonly=True' to execution module.\n if 'downloadonly' in kwargs:\n del kwargs['downloadonly']\n return patch_installed(name=name, advisory_ids=advisory_ids, downloadonly=True, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef purged(name,\n version=None,\n pkgs=None,\n normalize=True,\n ignore_epoch=False,\n **kwargs):\n '''\n Verify that a package is not installed, calling ``pkg.purge`` if necessary\n to purge the package. All configuration files are also removed.\n\n name\n The name of the package to be purged.\n\n version\n The version of the package that should be removed. Don't do anything if\n the package is installed with an unmatching version.\n\n .. important::\n As of version 2015.8.7, for distros which use yum/dnf, packages\n which have a version with a nonzero epoch (that is, versions which\n start with a number followed by a colon like in the example above)\n must have the epoch included when specifying the version number.\n For example:\n\n .. code-block:: yaml\n\n vim-enhanced:\n pkg.purged:\n - version: 2:7.4.160-1.el7\n\n In version 2015.8.9, an **ignore_epoch** argument has been added to\n :py:mod:`pkg.installed <salt.states.pkg.installed>`,\n :py:mod:`pkg.removed <salt.states.pkg.removed>`, and\n :py:mod:`pkg.purged <salt.states.pkg.purged>` states, which\n causes the epoch to be disregarded when the state checks to see if\n the desired version was installed. If **ignore_epoch** was not set\n to ``True``, and instead of ``2:7.4.160-1.el7`` a version of\n ``7.4.160-1.el7`` were used, this state would report success since\n the actual installed version includes the epoch, and the specified\n version would not match.\n\n normalize : True\n Normalize the package name by removing the architecture, if the\n architecture of the package is different from the architecture of the\n operating system. The ability to disable this behavior is useful for\n poorly-created packages which include the architecture as an actual\n part of the name, such as kernel modules which match a specific kernel\n version.\n\n .. versionadded:: 2015.8.0\n\n ignore_epoch : False\n When a package version contains an non-zero epoch (e.g.\n ``1:3.14.159-2.el7``, and a specific version of a package is desired,\n set this option to ``True`` to ignore the epoch when comparing\n versions. This allows for the following SLS to be used:\n\n .. code-block:: yaml\n\n # Actual vim-enhanced version: 2:7.4.160-1.el7\n vim-enhanced:\n pkg.purged:\n - version: 7.4.160-1.el7\n - ignore_epoch: True\n\n Without this option set to ``True`` in the above example, the state\n would falsely report success since the actual installed version is\n ``2:7.4.160-1.el7``. Alternatively, this option can be left as\n ``False`` and the full version string (with epoch) can be specified in\n the SLS file:\n\n .. code-block:: yaml\n\n vim-enhanced:\n pkg.purged:\n - version: 2:7.4.160-1.el7\n\n .. versionadded:: 2015.8.9\n\n Multiple Package Options:\n\n pkgs\n A list of packages to purge. Must be passed as a python list. The\n ``name`` parameter will be ignored if this option is passed. It accepts\n version numbers as well.\n\n .. versionadded:: 0.16.0\n '''\n kwargs['saltenv'] = __env__\n try:\n return _uninstall(action='purge', name=name, version=version,\n pkgs=pkgs, normalize=normalize,\n ignore_epoch=ignore_epoch, **kwargs)\n except CommandExecutionError as exc:\n ret = {'name': name, 'result': False}\n if exc.info:\n # Get information for state return from the exception.\n ret['changes'] = exc.info.get('changes', {})\n ret['comment'] = exc.strerror_without_changes\n else:\n ret['changes'] = {}\n ret['comment'] = ('An error was encountered while purging '\n 'package(s): {0}'.format(exc))\n return ret", "response": "Purge the specified package and version."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef uptodate(name, refresh=False, pkgs=None, **kwargs):\n '''\n .. versionadded:: 2014.7.0\n .. versionchanged:: 2018.3.0\n\n Added support for the ``pkgin`` provider.\n\n Verify that the system is completely up to date.\n\n name\n The name has no functional value and is only used as a tracking\n reference\n\n refresh\n refresh the package database before checking for new upgrades\n\n pkgs\n list of packages to upgrade\n\n :param str cache_valid_time:\n This parameter sets the value in seconds after which cache marked as invalid,\n and cache update is necessary. This overwrite ``refresh`` parameter\n default behavior.\n\n In this case cache_valid_time is set, refresh will not take place for\n amount in seconds since last ``apt-get update`` executed on the system.\n\n .. note::\n\n This parameter available only on Debian based distributions, and\n have no effect on the rest.\n\n :param bool resolve_capabilities:\n Turn on resolving capabilities. This allow one to name \"provides\" or alias names for packages.\n\n .. versionadded:: 2018.3.0\n\n kwargs\n Any keyword arguments to pass through to ``pkg.upgrade``.\n\n .. versionadded:: 2015.5.0\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': 'Failed to update'}\n\n if 'pkg.list_upgrades' not in __salt__:\n ret['comment'] = 'State pkg.uptodate is not available'\n return ret\n\n # emerge --update doesn't appear to support repo notation\n if 'fromrepo' in kwargs and __grains__['os_family'] == 'Gentoo':\n ret['comment'] = '\\'fromrepo\\' argument not supported on this platform'\n return ret\n\n if isinstance(refresh, bool):\n pkgs, refresh = _resolve_capabilities(pkgs, refresh=refresh, **kwargs)\n try:\n packages = __salt__['pkg.list_upgrades'](refresh=refresh, **kwargs)\n expected = {pkgname: {'new': pkgver, 'old': __salt__['pkg.version'](pkgname, **kwargs)}\n for pkgname, pkgver in six.iteritems(packages)}\n if isinstance(pkgs, list):\n packages = [pkg for pkg in packages if pkg in pkgs]\n expected = {pkgname: pkgver for pkgname, pkgver in six.iteritems(expected) if pkgname in pkgs}\n except Exception as exc:\n ret['comment'] = six.text_type(exc)\n return ret\n else:\n ret['comment'] = 'refresh must be either True or False'\n return ret\n\n if not packages:\n ret['comment'] = 'System is already up-to-date'\n ret['result'] = True\n return ret\n elif __opts__['test']:\n ret['comment'] = 'System update will be performed'\n ret['changes'] = expected\n ret['result'] = None\n return ret\n\n try:\n ret['changes'] = __salt__['pkg.upgrade'](refresh=refresh, pkgs=pkgs, **kwargs)\n except CommandExecutionError as exc:\n if exc.info:\n # Get information for state return from the exception.\n ret['changes'] = exc.info.get('changes', {})\n ret['comment'] = exc.strerror_without_changes\n else:\n ret['changes'] = {}\n ret['comment'] = ('An error was encountered while updating '\n 'packages: {0}'.format(exc))\n return ret\n\n # If a package list was provided, ensure those packages were updated\n missing = []\n if isinstance(pkgs, list):\n missing = [pkg for pkg in six.iterkeys(expected) if pkg not in ret['changes']]\n\n if missing:\n ret['comment'] = 'The following package(s) failed to update: {0}'.format(', '.join(missing))\n ret['result'] = False\n else:\n ret['comment'] = 'Upgrade ran successfully'\n ret['result'] = True\n\n return ret", "response": "Return a new version of the named package in a new version of the system."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nensuring that a group is installed for a specific item.", "response": "def group_installed(name, skip=None, include=None, **kwargs):\n '''\n .. versionadded:: 2015.8.0\n\n .. versionchanged:: 2016.11.0\n Added support in :mod:`pacman <salt.modules.pacman>`\n\n Ensure that an entire package group is installed. This state is currently\n only supported for the :mod:`yum <salt.modules.yumpkg>` and :mod:`pacman <salt.modules.pacman>`\n package managers.\n\n skip\n Packages that would normally be installed by the package group\n (\"default\" packages), which should not be installed.\n\n .. code-block:: yaml\n\n Load Balancer:\n pkg.group_installed:\n - skip:\n - piranha\n\n include\n Packages which are included in a group, which would not normally be\n installed by a ``yum groupinstall`` (\"optional\" packages). Note that\n this will not enforce group membership; if you include packages which\n are not members of the specified groups, they will still be installed.\n\n .. code-block:: yaml\n\n Load Balancer:\n pkg.group_installed:\n - include:\n - haproxy\n\n .. versionchanged:: 2016.3.0\n This option can no longer be passed as a comma-separated list, it\n must now be passed as a list (as shown in the above example).\n\n .. note::\n Because this is essentially a wrapper around :py:func:`pkg.install\n <salt.modules.yumpkg.install>`, any argument which can be passed to\n pkg.install may also be included here, and it will be passed on to the\n call to :py:func:`pkg.install <salt.modules.yumpkg.install>`.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n if 'pkg.group_diff' not in __salt__:\n ret['comment'] = 'pkg.group_install not available for this platform'\n return ret\n\n if skip is None:\n skip = []\n else:\n if not isinstance(skip, list):\n ret['comment'] = 'skip must be formatted as a list'\n return ret\n for idx, item in enumerate(skip):\n if not isinstance(item, six.string_types):\n skip[idx] = six.text_type(item)\n\n if include is None:\n include = []\n else:\n if not isinstance(include, list):\n ret['comment'] = 'include must be formatted as a list'\n return ret\n for idx, item in enumerate(include):\n if not isinstance(item, six.string_types):\n include[idx] = six.text_type(item)\n\n try:\n diff = __salt__['pkg.group_diff'](name)\n except CommandExecutionError as err:\n ret['comment'] = ('An error was encountered while installing/updating '\n 'group \\'{0}\\': {1}.'.format(name, err))\n return ret\n\n mandatory = diff['mandatory']['installed'] + \\\n diff['mandatory']['not installed']\n\n invalid_skip = [x for x in mandatory if x in skip]\n if invalid_skip:\n ret['comment'] = (\n 'The following mandatory packages cannot be skipped: {0}'\n .format(', '.join(invalid_skip))\n )\n return ret\n\n targets = diff['mandatory']['not installed']\n targets.extend([x for x in diff['default']['not installed']\n if x not in skip])\n targets.extend(include)\n\n if not targets:\n ret['result'] = True\n ret['comment'] = 'Group \\'{0}\\' is already installed'.format(name)\n return ret\n\n partially_installed = diff['mandatory']['installed'] \\\n or diff['default']['installed'] \\\n or diff['optional']['installed']\n\n if __opts__['test']:\n ret['result'] = None\n if partially_installed:\n ret['comment'] = (\n 'Group \\'{0}\\' is partially installed and will be updated'\n .format(name)\n )\n else:\n ret['comment'] = 'Group \\'{0}\\' will be installed'.format(name)\n return ret\n\n try:\n ret['changes'] = __salt__['pkg.install'](pkgs=targets, **kwargs)\n except CommandExecutionError as exc:\n ret = {'name': name, 'result': False}\n if exc.info:\n # Get information for state return from the exception.\n ret['changes'] = exc.info.get('changes', {})\n ret['comment'] = exc.strerror_without_changes\n else:\n ret['changes'] = {}\n ret['comment'] = ('An error was encountered while '\n 'installing/updating group \\'{0}\\': {1}'\n .format(name, exc))\n return ret\n\n failed = [x for x in targets if x not in __salt__['pkg.list_pkgs'](**kwargs)]\n if failed:\n ret['comment'] = (\n 'Failed to install the following packages: {0}'\n .format(', '.join(failed))\n )\n return ret\n\n ret['result'] = True\n ret['comment'] = 'Group \\'{0}\\' was {1}'.format(\n name,\n 'updated' if partially_installed else 'installed'\n )\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef mod_init(low):\n '''\n Set a flag to tell the install functions to refresh the package database.\n This ensures that the package database is refreshed only once during\n a state run significantly improving the speed of package management\n during a state run.\n\n It sets a flag for a number of reasons, primarily due to timeline logic.\n When originally setting up the mod_init for pkg a number of corner cases\n arose with different package managers and how they refresh package data.\n\n It also runs the \"ex_mod_init\" from the package manager module that is\n currently loaded. The \"ex_mod_init\" is expected to work as a normal\n \"mod_init\" function.\n\n .. seealso::\n :py:func:`salt.modules.ebuild.ex_mod_init`\n\n '''\n ret = True\n if 'pkg.ex_mod_init' in __salt__:\n ret = __salt__['pkg.ex_mod_init'](low)\n\n if low['fun'] == 'installed' or low['fun'] == 'latest':\n salt.utils.pkg.write_rtag(__opts__)\n return ret\n return False", "response": "Initializes the package database."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef mod_watch(name, **kwargs):\n '''\n Install/reinstall a package based on a watch requisite\n\n .. note::\n This state exists to support special handling of the ``watch``\n :ref:`requisite <requisites>`. It should not be called directly.\n\n Parameters for this function should be set by the state being triggered.\n '''\n sfun = kwargs.pop('sfun', None)\n mapfun = {'purged': purged,\n 'latest': latest,\n 'removed': removed,\n 'installed': installed}\n if sfun in mapfun:\n return mapfun[sfun](name, **kwargs)\n return {'name': name,\n 'changes': {},\n 'comment': 'pkg.{0} does not work with the watch requisite'.format(sfun),\n 'result': False}", "response": "This function is used to install or reinstall a package based on a watch requisite."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nensuring that the named port exists on the bridge.", "response": "def present(name, bridge, tunnel_type=None, id=None, remote=None, dst_port=None, internal=False):\n '''\n Ensures that the named port exists on bridge, eventually creates it.\n\n Args:\n name: The name of the port.\n bridge: The name of the bridge.\n tunnel_type: Optional type of interface to create, currently supports: vlan, vxlan and gre.\n id: Optional tunnel's key.\n remote: Remote endpoint's IP address.\n dst_port: Port to use when creating tunnelport in the switch.\n internal: Create an internal port if one does not exist\n\n '''\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n tunnel_types = ('vlan', 'vxlan', 'gre')\n\n if tunnel_type and tunnel_type not in tunnel_types:\n raise TypeError('The optional type argument must be one of these values: {0}.'.format(\n six.text_type(tunnel_types))\n )\n\n bridge_exists = __salt__['openvswitch.bridge_exists'](bridge)\n port_list = []\n if bridge_exists:\n port_list = __salt__['openvswitch.port_list'](bridge)\n\n # Comment and change messages\n\n comments = {}\n\n comments['comment_bridge_notexists'] = 'Bridge {0} does not exist.'.format(bridge)\n comments['comment_port_exists'] = 'Port {0} already exists.'.format(name)\n comments['comment_port_created'] = 'Port {0} created on bridge {1}.'.format(name, bridge)\n comments['comment_port_notcreated'] = 'Unable to create port {0} on bridge {1}.'.format(name, bridge)\n comments['changes_port_created'] = {name: {'old': 'No port named {0} present.'.format(name),\n 'new': 'Created port {1} on bridge {0}.'.format(bridge, name),\n }\n }\n comments['comment_port_internal'] = 'Port {0} already exists, but interface type has been changed to internal.'.format(name)\n comments['changes_port_internal'] = {'internal': {'old': False, 'new': True}}\n comments['comment_port_internal_not_changed'] = 'Port {0} already exists, but the interface type could not be changed to internal.'.format(name)\n\n if tunnel_type:\n comments['comment_invalid_ip'] = 'Remote is not valid ip address.'\n if tunnel_type == \"vlan\":\n comments['comment_vlan_invalid_id'] = 'VLANs id must be between 0 and 4095.'\n comments['comment_vlan_invalid_name'] = 'Could not find network interface {0}.'.format(name)\n comments['comment_vlan_port_exists'] = 'Port {0} with access to VLAN {1} already exists on bridge {2}.'.format(name, id, bridge)\n comments['comment_vlan_created'] = 'Created port {0} with access to VLAN {1} on bridge {2}.'.format(name, id, bridge)\n comments['comment_vlan_notcreated'] = 'Unable to create port {0} with access to VLAN {1} on ' \\\n 'bridge {2}.'.format(name, id, bridge)\n comments['changes_vlan_created'] = {name: {'old': 'No port named {0} with access to VLAN {1} present on '\n 'bridge {2} present.'.format(name, id, bridge),\n 'new': 'Created port {1} with access to VLAN {2} on '\n 'bridge {0}.'.format(bridge, name, id),\n }\n }\n\n elif tunnel_type == \"gre\":\n comments['comment_gre_invalid_id'] = 'Id of GRE tunnel must be an unsigned 32-bit integer.'\n comments['comment_gre_interface_exists'] = 'GRE tunnel interface {0} with rempte ip {1} and key {2} ' \\\n 'already exists on bridge {3}.'.format(name, remote, id, bridge)\n comments['comment_gre_created'] = 'Created GRE tunnel interface {0} with remote ip {1} and key {2} ' \\\n 'on bridge {3}.'.format(name, remote, id, bridge)\n comments['comment_gre_notcreated'] = 'Unable to create GRE tunnel interface {0} with remote ip {1} and key {2} ' \\\n 'on bridge {3}.'.format(name, remote, id, bridge)\n comments['changes_gre_created'] = {name: {'old': 'No GRE tunnel interface {0} with remote ip {1} and key {2} '\n 'on bridge {3} present.'.format(name, remote, id, bridge),\n 'new': 'Created GRE tunnel interface {0} with remote ip {1} and key {2} '\n 'on bridge {3}.'.format(name, remote, id, bridge),\n }\n }\n elif tunnel_type == \"vxlan\":\n comments['comment_dstport'] = ' (dst_port' + six.text_type(dst_port) + ')' if 0 < dst_port <= 65535 else ''\n comments['comment_vxlan_invalid_id'] = 'Id of VXLAN tunnel must be an unsigned 64-bit integer.'\n comments['comment_vxlan_interface_exists'] = 'VXLAN tunnel interface {0} with rempte ip {1} and key {2} ' \\\n 'already exists on bridge {3}{4}.'.format(name, remote, id, bridge, comments['comment_dstport'])\n comments['comment_vxlan_created'] = 'Created VXLAN tunnel interface {0} with remote ip {1} and key {2} ' \\\n 'on bridge {3}{4}.'.format(name, remote, id, bridge, comments['comment_dstport'])\n comments['comment_vxlan_notcreated'] = 'Unable to create VXLAN tunnel interface {0} with remote ip {1} and key {2} ' \\\n 'on bridge {3}{4}.'.format(name, remote, id, bridge, comments['comment_dstport'])\n comments['changes_vxlan_created'] = {name: {'old': 'No VXLAN tunnel interface {0} with remote ip {1} and key {2} '\n 'on bridge {3}{4} present.'.format(name, remote, id, bridge, comments['comment_dstport']),\n 'new': 'Created VXLAN tunnel interface {0} with remote ip {1} and key {2} '\n 'on bridge {3}{4}.'.format(name, remote, id, bridge, comments['comment_dstport']),\n }\n }\n\n # Check VLANs attributes\n def _check_vlan():\n tag = __salt__['openvswitch.port_get_tag'](name)\n interfaces = __salt__['network.interfaces']()\n if not 0 <= id <= 4095:\n ret['result'] = False\n ret['comment'] = comments['comment_vlan_invalid_id']\n elif not internal and name not in interfaces:\n ret['result'] = False\n ret['comment'] = comments['comment_vlan_invalid_name']\n elif tag and name in port_list:\n try:\n if int(tag[0]) == id:\n ret['result'] = True\n ret['comment'] = comments['comment_vlan_port_exists']\n except (ValueError, KeyError):\n pass\n\n # Check GRE tunnels attributes\n def _check_gre():\n interface_options = __salt__['openvswitch.interface_get_options'](name)\n interface_type = __salt__['openvswitch.interface_get_type'](name)\n if not 0 <= id <= 2**32:\n ret['result'] = False\n ret['comment'] = comments['comment_gre_invalid_id']\n elif not __salt__['dig.check_ip'](remote):\n ret['result'] = False\n ret['comment'] = comments['comment_invalid_ip']\n elif interface_options and interface_type and name in port_list:\n interface_attroptions = '{key=\\\"' + six.text_type(id) + '\\\", remote_ip=\\\"' + six.text_type(remote) + '\\\"}'\n try:\n if interface_type[0] == 'gre' and interface_options[0] == interface_attroptions:\n ret['result'] = True\n ret['comment'] = comments['comment_gre_interface_exists']\n except KeyError:\n pass\n\n # Check VXLAN tunnels attributes\n def _check_vxlan():\n interface_options = __salt__['openvswitch.interface_get_options'](name)\n interface_type = __salt__['openvswitch.interface_get_type'](name)\n if not 0 <= id <= 2**64:\n ret['result'] = False\n ret['comment'] = comments['comment_vxlan_invalid_id']\n elif not __salt__['dig.check_ip'](remote):\n ret['result'] = False\n ret['comment'] = comments['comment_invalid_ip']\n elif interface_options and interface_type and name in port_list:\n opt_port = 'dst_port=\\\"' + six.text_type(dst_port) + '\\\", ' if 0 < dst_port <= 65535 else ''\n interface_attroptions = '{{{0}key=\\\"'.format(opt_port) + six.text_type(id) + '\\\", remote_ip=\\\"' + six.text_type(remote) + '\\\"}'\n try:\n if interface_type[0] == 'vxlan' and interface_options[0] == interface_attroptions:\n ret['result'] = True\n ret['comment'] = comments['comment_vxlan_interface_exists']\n except KeyError:\n pass\n\n # Dry run, test=true mode\n if __opts__['test']:\n if bridge_exists:\n if tunnel_type == 'vlan':\n _check_vlan()\n if not ret['comment']:\n ret['result'] = None\n ret['comment'] = comments['comment_vlan_created']\n elif tunnel_type == 'vxlan':\n _check_vxlan()\n if not ret['comment']:\n ret['result'] = None\n ret['comment'] = comments['comment_vxlan_created']\n elif tunnel_type == 'gre':\n _check_gre()\n if not ret['comment']:\n ret['result'] = None\n ret['comment'] = comments['comment_gre_created']\n else:\n if name in port_list:\n ret['result'] = True\n current_type = __salt__['openvswitch.interface_get_type'](\n name)\n # The interface type is returned as a single-element list.\n if internal and (current_type != ['internal']):\n ret['comment'] = comments['comment_port_internal']\n else:\n ret['comment'] = comments['comment_port_exists']\n else:\n ret['result'] = None\n ret['comment'] = comments['comment_port_created']\n else:\n ret['result'] = None\n ret['comment'] = comments['comment_bridge_notexists']\n\n return ret\n\n if bridge_exists:\n if tunnel_type == 'vlan':\n _check_vlan()\n if not ret['comment']:\n port_create_vlan = __salt__['openvswitch.port_create_vlan'](bridge, name, id, internal)\n if port_create_vlan:\n ret['result'] = True\n ret['comment'] = comments['comment_vlan_created']\n ret['changes'] = comments['changes_vlan_created']\n else:\n ret['result'] = False\n ret['comment'] = comments['comment_vlan_notcreated']\n elif tunnel_type == 'vxlan':\n _check_vxlan()\n if not ret['comment']:\n port_create_vxlan = __salt__['openvswitch.port_create_vxlan'](bridge, name, id, remote, dst_port)\n if port_create_vxlan:\n ret['result'] = True\n ret['comment'] = comments['comment_vxlan_created']\n ret['changes'] = comments['changes_vxlan_created']\n else:\n ret['result'] = False\n ret['comment'] = comments['comment_vxlan_notcreated']\n elif tunnel_type == 'gre':\n _check_gre()\n if not ret['comment']:\n port_create_gre = __salt__['openvswitch.port_create_gre'](bridge, name, id, remote)\n if port_create_gre:\n ret['result'] = True\n ret['comment'] = comments['comment_gre_created']\n ret['changes'] = comments['changes_gre_created']\n else:\n ret['result'] = False\n ret['comment'] = comments['comment_gre_notcreated']\n else:\n if name in port_list:\n current_type = __salt__['openvswitch.interface_get_type'](name)\n # The interface type is returned as a single-element list.\n if internal and (current_type != ['internal']):\n # We do not have a direct way of only setting the interface\n # type to internal, so we add the port with the --may-exist\n # option.\n port_add = __salt__['openvswitch.port_add'](\n bridge, name, may_exist=True, internal=internal)\n if port_add:\n ret['result'] = True\n ret['comment'] = comments['comment_port_internal']\n ret['changes'] = comments['changes_port_internal']\n else:\n ret['result'] = False\n ret['comment'] = comments[\n 'comment_port_internal_not_changed']\n else:\n ret['result'] = True\n ret['comment'] = comments['comment_port_exists']\n else:\n port_add = __salt__['openvswitch.port_add'](bridge, name, internal=internal)\n if port_add:\n ret['result'] = True\n ret['comment'] = comments['comment_port_created']\n ret['changes'] = comments['changes_port_created']\n else:\n ret['result'] = False\n ret['comment'] = comments['comment_port_notcreated']\n else:\n ret['result'] = False\n ret['comment'] = comments['comment_bridge_notexists']\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nensure that the named port does not exist on the named bridge.", "response": "def absent(name, bridge=None):\n '''\n Ensures that the named port exists on bridge, eventually deletes it.\n If bridge is not set, port is removed from whatever bridge contains it.\n\n Args:\n name: The name of the port.\n bridge: The name of the bridge.\n\n '''\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n bridge_exists = False\n if bridge:\n bridge_exists = __salt__['openvswitch.bridge_exists'](bridge)\n if bridge_exists:\n port_list = __salt__['openvswitch.port_list'](bridge)\n else:\n port_list = ()\n else:\n port_list = [name]\n\n # Comment and change messages\n comments = {}\n comments['comment_bridge_notexists'] = 'Bridge {0} does not exist.'.format(bridge)\n comments['comment_port_notexists'] = 'Port {0} does not exist on bridge {1}.'.format(name, bridge)\n comments['comment_port_deleted'] = 'Port {0} deleted.'.format(name)\n comments['comment_port_notdeleted'] = 'Unable to delete port {0}.'.format(name)\n comments['changes_port_deleted'] = {name: {'old': 'Port named {0} may exist.'.format(name),\n 'new': 'Deleted port {0}.'.format(name),\n }\n }\n\n # Dry run, test=true mode\n if __opts__['test']:\n if bridge and not bridge_exists:\n ret['result'] = None\n ret['comment'] = comments['comment_bridge_notexists']\n elif name not in port_list:\n ret['result'] = True\n ret['comment'] = comments['comment_port_notexists']\n else:\n ret['result'] = None\n ret['comment'] = comments['comment_port_deleted']\n return ret\n\n if bridge and not bridge_exists:\n ret['result'] = False\n ret['comment'] = comments['comment_bridge_notexists']\n elif name not in port_list:\n ret['result'] = True\n ret['comment'] = comments['comment_port_notexists']\n else:\n if bridge:\n port_remove = __salt__['openvswitch.port_remove'](br=bridge, port=name)\n else:\n port_remove = __salt__['openvswitch.port_remove'](br=None, port=name)\n\n if port_remove:\n ret['result'] = True\n ret['comment'] = comments['comment_port_deleted']\n ret['changes'] = comments['changes_port_deleted']\n else:\n ret['result'] = False\n ret['comment'] = comments['comment_port_notdeleted']\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncall out to system_profiler. Return a dictionary of the stuff we are interested in.", "response": "def _call_system_profiler(datatype):\n '''\n Call out to system_profiler. Return a dictionary\n of the stuff we are interested in.\n '''\n\n p = subprocess.Popen(\n [PROFILER_BINARY, '-detailLevel', 'full',\n '-xml', datatype], stdout=subprocess.PIPE)\n (sysprofresults, sysprof_stderr) = p.communicate(input=None)\n\n if six.PY2:\n plist = plistlib.readPlistFromString(sysprofresults)\n else:\n plist = plistlib.readPlistFromBytes(sysprofresults)\n\n try:\n apps = plist[0]['_items']\n except (IndexError, KeyError):\n apps = []\n\n return apps"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef receipts():\n '''\n Return the results of a call to\n ``system_profiler -xml -detail full SPInstallHistoryDataType``\n as a dictionary. Top-level keys of the dictionary\n are the names of each set of install receipts, since\n there can be multiple receipts with the same name.\n Contents of each key are a list of dictionaries.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' systemprofiler.receipts\n '''\n\n apps = _call_system_profiler('SPInstallHistoryDataType')\n\n appdict = {}\n\n for a in apps:\n details = dict(a)\n details.pop('_name')\n if 'install_date' in details:\n details['install_date'] = details['install_date'].strftime('%Y-%m-%d %H:%M:%S')\n if 'info' in details:\n try:\n details['info'] = '{0}: {1}'.format(details['info'][0],\n details['info'][1].strftime('%Y-%m-%d %H:%M:%S'))\n except (IndexError, AttributeError):\n pass\n\n if a['_name'] not in appdict:\n appdict[a['_name']] = []\n\n appdict[a['_name']].append(details)\n\n return appdict", "response": "Return the results of a call to system_profiler - xml - detail full SPInstallHistoryDataType"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef alias(name, collections, **kwargs):\n '''\n Create alias and enforce collection list.\n\n Use the solrcloud module to get alias members and set them.\n\n You can pass additional arguments that will be forwarded to http.query\n\n name\n The collection name\n collections\n list of collections to include in the alias\n '''\n ret = {\n 'name': name,\n 'changes': {},\n 'result': False,\n 'comment': '',\n }\n\n if __salt__['solrcloud.alias_exists'](name, **kwargs):\n alias_content = __salt__['solrcloud.alias_get_collections'](name, **kwargs)\n diff = set(alias_content).difference(set(collections))\n\n if not diff:\n ret['result'] = True\n ret['comment'] = 'Alias is in desired state'\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'The alias \"{0}\" will be updated.'.format(name)\n ret['result'] = None\n else:\n __salt__['solrcloud.alias_set_collections'](name, collections, **kwargs)\n ret['comment'] = 'The alias \"{0}\" has been updated.'.format(name)\n ret['result'] = True\n\n ret['changes'] = {\n 'old': ','.join(alias_content),\n 'new': ','.join(collections),\n }\n\n else:\n if __opts__['test']:\n ret['comment'] = 'The alias \"{0}\" will be created.'.format(name)\n ret['result'] = None\n else:\n __salt__['solrcloud.alias_set_collections'](name, collections, **kwargs)\n ret['comment'] = 'The alias \"{0}\" has been created.'.format(name)\n ret['result'] = True\n\n ret['changes'] = {\n 'old': None,\n 'new': ','.join(collections),\n }\n\n return ret", "response": "Create alias and enforce collection list."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a new collection and enforce options.", "response": "def collection(name, options=None, **kwargs):\n '''\n Create collection and enforce options.\n\n Use the solrcloud module to get collection parameters.\n\n You can pass additional arguments that will be forwarded to http.query\n\n name\n The collection name\n options : {}\n options to ensure\n '''\n ret = {\n 'name': name,\n 'changes': {},\n 'result': False,\n 'comment': '',\n }\n\n if options is None:\n options = {}\n\n if __salt__[\"solrcloud.collection_exists\"](name, **kwargs):\n\n diff = {}\n\n current_options = __salt__[\"solrcloud.collection_get_options\"](name, **kwargs)\n\n # Filter options that can be updated\n updatable_options = [\n \"maxShardsPerNode\",\n \"replicationFactor\",\n \"autoAddReplicas\",\n \"collection.configName\",\n \"rule\",\n \"snitch\"]\n\n options = [k for k in six.iteritems(options) if k in updatable_options]\n\n for key, value in options:\n if key not in current_options or current_options[key] != value:\n diff[key] = value\n\n if not diff:\n ret['result'] = True\n ret['comment'] = 'Collection options are in desired state'\n return ret\n\n else:\n\n if __opts__['test']:\n ret['comment'] = 'Collection options \"{0}\" will be changed.'.format(name)\n ret['result'] = None\n else:\n __salt__['solrcloud.collection_set_options'](name, diff, **kwargs)\n ret['comment'] = 'Parameters were updated for collection \"{0}\".'.format(name)\n ret['result'] = True\n\n ret['changes'] = {\n 'old': salt.utils.json.dumps(current_options, sort_keys=True, indent=4, separators=(',', ': ')),\n 'new': salt.utils.json.dumps(options, sort_keys=True, indent=4, separators=(',', ': '))\n }\n return ret\n\n else:\n\n new_changes = salt.utils.json.dumps(options, sort_keys=True, indent=4, separators=(',', ': '))\n if __opts__['test']:\n ret['comment'] = 'The collection \"{0}\" will be created.'.format(name)\n ret['result'] = None\n else:\n __salt__[\"solrcloud.collection_create\"](name, options, **kwargs)\n ret['comment'] = 'The collection \"{0}\" has been created.'.format(name)\n ret['result'] = True\n\n ret['changes'] = {\n 'old': None,\n 'new': str('options=') + new_changes # future lint: disable=blacklisted-function\n }\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nquerying the object on the Consul API URL.", "response": "def _query(function,\n consul_url,\n token=None,\n method='GET',\n api_version='v1',\n data=None,\n query_params=None):\n '''\n Consul object method function to construct and execute on the API URL.\n\n :param api_url: The Consul api url.\n :param api_version The Consul api version\n :param function: The Consul api function to perform.\n :param method: The HTTP method, e.g. GET or POST.\n :param data: The data to be sent for POST method. This param is ignored for GET requests.\n :return: The json response from the API call or False.\n '''\n\n if not query_params:\n query_params = {}\n\n ret = {'data': '',\n 'res': True}\n\n if not token:\n token = _get_token()\n\n headers = {\"X-Consul-Token\": token, \"Content-Type\": \"application/json\"}\n base_url = urllib.parse.urljoin(consul_url, '{0}/'.format(api_version))\n url = urllib.parse.urljoin(base_url, function, False)\n\n if method == 'GET':\n data = None\n else:\n if data is None:\n data = {}\n data = salt.utils.json.dumps(data)\n\n result = salt.utils.http.query(\n url,\n method=method,\n params=query_params,\n data=data,\n decode=True,\n status=True,\n header_dict=headers,\n opts=__opts__,\n )\n\n if result.get('status', None) == http_client.OK:\n ret['data'] = result.get('dict', result)\n ret['res'] = True\n elif result.get('status', None) == http_client.NO_CONTENT:\n ret['res'] = False\n elif result.get('status', None) == http_client.NOT_FOUND:\n ret['data'] = 'Key not found.'\n ret['res'] = False\n else:\n if result:\n ret['data'] = result\n ret['res'] = True\n else:\n ret['res'] = False\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists the keys in Consul", "response": "def list_(consul_url=None, token=None, key=None, **kwargs):\n '''\n List keys in Consul\n\n :param consul_url: The Consul server URL.\n :param key: The key to use as the starting point for the list.\n :return: The list of keys.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.list\n salt '*' consul.list key='web'\n\n '''\n ret = {}\n\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n query_params = {}\n\n if 'recurse' in kwargs:\n query_params['recurse'] = 'True'\n\n # No key so recurse and show all values\n if not key:\n query_params['recurse'] = 'True'\n function = 'kv/'\n else:\n function = 'kv/{0}'.format(key)\n\n query_params['keys'] = 'True'\n query_params['separator'] = '/'\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n query_params=query_params)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get(consul_url=None, key=None, token=None, recurse=False, decode=False, raw=False):\n '''\n Get key from Consul\n\n :param consul_url: The Consul server URL.\n :param key: The key to use as the starting point for the list.\n :param recurse: Return values recursively beginning at the value of key.\n :param decode: By default values are stored as Base64 encoded values,\n decode will return the whole key with the value decoded.\n :param raw: Simply return the decoded value of the key.\n :return: The keys in Consul.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.get key='web/key1'\n salt '*' consul.get key='web' recurse=True\n salt '*' consul.get key='web' recurse=True decode=True\n\n By default values stored in Consul are base64 encoded, passing the\n decode option will show them as the decoded values.\n\n .. code-block:: bash\n\n salt '*' consul.get key='web' recurse=True decode=True raw=True\n\n By default Consult will return other information about the key, the raw\n option will return only the raw value.\n\n '''\n ret = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not key:\n raise SaltInvocationError('Required argument \"key\" is missing.')\n\n query_params = {}\n function = 'kv/{0}'.format(key)\n if recurse:\n query_params['recurse'] = 'True'\n if raw:\n query_params['raw'] = True\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n query_params=query_params)\n\n if ret['res']:\n if decode:\n for item in ret['data']:\n if item['Value'] is None:\n item['Value'] = \"\"\n else:\n item['Value'] = base64.b64decode(item['Value'])\n return ret", "response": "Get the key from Consul."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nputting values into Consul WorkItem", "response": "def put(consul_url=None, token=None, key=None, value=None, **kwargs):\n '''\n Put values into Consul\n\n :param consul_url: The Consul server URL.\n :param key: The key to use as the starting point for the list.\n :param value: The value to set the key to.\n :param flags: This can be used to specify an unsigned value\n between 0 and 2^64-1. Clients can choose to use\n this however makes sense for their application.\n :param cas: This flag is used to turn the PUT into a\n Check-And-Set operation.\n :param acquire: This flag is used to turn the PUT into a\n lock acquisition operation.\n :param release: This flag is used to turn the PUT into a\n lock release operation.\n :return: Boolean & message of success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.put key='web/key1' value=\"Hello there\"\n\n salt '*' consul.put key='web/key1' value=\"Hello there\" acquire='d5d371f4-c380-5280-12fd-8810be175592'\n\n salt '*' consul.put key='web/key1' value=\"Hello there\" release='d5d371f4-c380-5280-12fd-8810be175592'\n\n '''\n ret = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not key:\n raise SaltInvocationError('Required argument \"key\" is missing.')\n\n # Invalid to specified these together\n conflicting_args = ['cas', 'release', 'acquire']\n for _l1 in conflicting_args:\n for _l2 in conflicting_args:\n if _l1 in kwargs and _l2 in kwargs and _l1 != _l2:\n raise SaltInvocationError('Using arguments `{0}` and `{1}`'\n ' together is invalid.'.format(_l1, _l2))\n\n query_params = {}\n\n available_sessions = session_list(consul_url=consul_url, return_list=True)\n _current = get(consul_url=consul_url, key=key)\n\n if 'flags' in kwargs:\n if kwargs['flags'] >= 0 and kwargs['flags'] <= 2**64:\n query_params['flags'] = kwargs['flags']\n\n if 'cas' in kwargs:\n if _current['res']:\n if kwargs['cas'] == 0:\n ret['message'] = ('Key {0} exists, index '\n 'must be non-zero.'.format(key))\n ret['res'] = False\n return ret\n\n if kwargs['cas'] != _current['data']['ModifyIndex']:\n ret['message'] = ('Key {0} exists, but indexes '\n 'do not match.'.format(key))\n ret['res'] = False\n return ret\n query_params['cas'] = kwargs['cas']\n else:\n ret['message'] = ('Key {0} does not exists, '\n 'CAS argument can not be used.'.format(key))\n ret['res'] = False\n return ret\n\n if 'acquire' in kwargs:\n if kwargs['acquire'] not in available_sessions:\n ret['message'] = '{0} is not a valid session.'.format(kwargs['acquire'])\n ret['res'] = False\n return ret\n\n query_params['acquire'] = kwargs['acquire']\n\n if 'release' in kwargs:\n if _current['res']:\n if 'Session' in _current['data']:\n if _current['data']['Session'] == kwargs['release']:\n query_params['release'] = kwargs['release']\n else:\n ret['message'] = '{0} locked by another session.'.format(key)\n ret['res'] = False\n return ret\n\n else:\n ret['message'] = '{0} is not a valid session.'.format(kwargs['acquire'])\n ret['res'] = False\n else:\n log.error('Key {0} does not exist. Skipping release.')\n\n data = value\n function = 'kv/{0}'.format(key)\n method = 'PUT'\n ret = _query(consul_url=consul_url,\n token=token,\n function=function,\n method=method,\n data=data,\n query_params=query_params)\n\n if ret['res']:\n ret['res'] = True\n ret['data'] = 'Added key {0} with value {1}.'.format(key, value)\n else:\n ret['res'] = False\n ret['data'] = 'Unable to add key {0} with value {1}.'.format(key, value)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef delete(consul_url=None, token=None, key=None, **kwargs):\n '''\n Delete values from Consul\n\n :param consul_url: The Consul server URL.\n :param key: The key to use as the starting point for the list.\n :param recurse: Delete values recursively beginning at the value of key.\n :param cas: This flag is used to turn the DELETE into\n a Check-And-Set operation.\n :return: Boolean & message of success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.delete key='web'\n salt '*' consul.delete key='web' recurse='True'\n\n '''\n ret = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not key:\n raise SaltInvocationError('Required argument \"key\" is missing.')\n\n query_params = {}\n\n if 'recurse' in kwargs:\n query_params['recurse'] = True\n\n if 'cas' in kwargs:\n if kwargs['cas'] > 0:\n query_params['cas'] = kwargs['cas']\n else:\n ret['message'] = ('Check and Set Operation ',\n 'value must be greater than 0.')\n ret['res'] = False\n return ret\n\n function = 'kv/{0}'.format(key)\n ret = _query(consul_url=consul_url,\n token=token,\n function=function,\n method='DELETE',\n query_params=query_params)\n\n if ret['res']:\n ret['res'] = True\n ret['message'] = 'Deleted key {0}.'.format(key)\n else:\n ret['res'] = False\n ret['message'] = 'Unable to delete key {0}.'.format(key)\n return ret", "response": "Delete the value of a key in the current node."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef agent_checks(consul_url=None, token=None):\n '''\n Returns the checks the local agent is managing\n\n :param consul_url: The Consul server URL.\n :return: Returns the checks the local agent is managing\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.agent_checks\n\n '''\n ret = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n function = 'agent/checks'\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n method='GET')\n return ret", "response": "Returns the checks the local agent is managing\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the members as seen by the local serf agent", "response": "def agent_members(consul_url=None, token=None, **kwargs):\n '''\n Returns the members as seen by the local serf agent\n\n :param consul_url: The Consul server URL.\n :return: Returns the members as seen by the local serf agent\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.agent_members\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if 'wan' in kwargs:\n query_params['wan'] = kwargs['wan']\n\n function = 'agent/members'\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n method='GET',\n query_params=query_params)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef agent_self(consul_url=None, token=None):\n '''\n Returns the local node configuration\n\n :param consul_url: The Consul server URL.\n :return: Returns the local node configuration\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.agent_self\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n function = 'agent/self'\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n method='GET',\n query_params=query_params)\n return ret", "response": "Returns the local node configuration"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef agent_maintenance(consul_url=None, token=None, **kwargs):\n '''\n Manages node maintenance mode\n\n :param consul_url: The Consul server URL.\n :param enable: The enable flag is required.\n Acceptable values are either true\n (to enter maintenance mode) or\n false (to resume normal operation).\n :param reason: If provided, its value should be a\n text string explaining the reason for\n placing the node into maintenance mode.\n :return: Boolean and message indicating success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.agent_maintenance enable='False' reason='Upgrade in progress'\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if 'enable' in kwargs:\n query_params['enable'] = kwargs['enable']\n else:\n ret['message'] = 'Required parameter \"enable\" is missing.'\n ret['res'] = False\n return ret\n\n if 'reason' in kwargs:\n query_params['reason'] = kwargs['reason']\n\n function = 'agent/maintenance'\n res = _query(consul_url=consul_url,\n function=function,\n token=token,\n method='PUT',\n query_params=query_params)\n if res['res']:\n ret['res'] = True\n ret['message'] = ('Agent maintenance mode '\n '{0}ed.'.format(kwargs['enable']))\n else:\n ret['res'] = True\n ret['message'] = 'Unable to change maintenance mode for agent.'\n return ret", "response": "This function is used to manage the node maintenance mode."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef agent_leave(consul_url=None, token=None, node=None):\n '''\n Used to instruct the agent to force a node into the left state.\n\n :param consul_url: The Consul server URL.\n :param node: The node the agent will force into left state\n :return: Boolean and message indicating success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.agent_leave node='web1.example.com'\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not node:\n raise SaltInvocationError('Required argument \"node\" is missing.')\n\n function = 'agent/force-leave/{0}'.format(node)\n res = _query(consul_url=consul_url,\n function=function,\n token=token,\n method='GET',\n query_params=query_params)\n if res['res']:\n ret['res'] = True\n ret['message'] = 'Node {0} put in leave state.'.format(node)\n else:\n ret['res'] = False\n ret['message'] = 'Unable to change state for {0}.'.format(node)\n return ret", "response": "This function allows the agent to force a node into the left state."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef agent_check_register(consul_url=None, token=None, **kwargs):\n '''\n The register endpoint is used to add a new check to the local agent.\n\n :param consul_url: The Consul server URL.\n :param name: The description of what the check is for.\n :param id: The unique name to use for the check, if not\n provided 'name' is used.\n :param notes: Human readable description of the check.\n :param script: If script is provided, the check type is\n a script, and Consul will evaluate that script\n based on the interval parameter.\n :param http: Check will perform an HTTP GET request against\n the value of HTTP (expected to be a URL) based\n on the interval parameter.\n :param ttl: If a TTL type is used, then the TTL update endpoint\n must be used periodically to update the state of the check.\n :param interval: Interval at which the check should run.\n :return: Boolean and message indicating success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.agent_check_register name='Memory Utilization' script='/usr/local/bin/check_mem.py' interval='15s'\n\n '''\n ret = {}\n data = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if 'name' in kwargs:\n data['Name'] = kwargs['name']\n else:\n raise SaltInvocationError('Required argument \"name\" is missing.')\n\n if True not in [True for item in ('script', 'http', 'ttl') if item in kwargs]:\n ret['message'] = 'Required parameter \"script\" or \"http\" is missing.'\n ret['res'] = False\n return ret\n\n if 'id' in kwargs:\n data['ID'] = kwargs['id']\n\n if 'notes' in kwargs:\n data['Notes'] = kwargs['notes']\n\n if 'script' in kwargs:\n if 'interval' not in kwargs:\n ret['message'] = 'Required parameter \"interval\" is missing.'\n ret['res'] = False\n return ret\n data['Script'] = kwargs['script']\n data['Interval'] = kwargs['interval']\n\n if 'http' in kwargs:\n if 'interval' not in kwargs:\n ret['message'] = 'Required parameter \"interval\" is missing.'\n ret['res'] = False\n return ret\n data['HTTP'] = kwargs['http']\n data['Interval'] = kwargs['interval']\n\n if 'ttl' in kwargs:\n data['TTL'] = kwargs['ttl']\n\n function = 'agent/check/register'\n res = _query(consul_url=consul_url,\n function=function,\n token=token,\n method='PUT',\n data=data)\n\n if res['res']:\n ret['res'] = True\n ret['message'] = ('Check {0} added to agent.'.format(kwargs['name']))\n else:\n ret['res'] = False\n ret['message'] = 'Unable to add check to agent.'\n return ret", "response": "Register a new check in the local agent."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef agent_check_deregister(consul_url=None, token=None, checkid=None):\n '''\n The agent will take care of deregistering the check from the Catalog.\n\n :param consul_url: The Consul server URL.\n :param checkid: The ID of the check to deregister from Consul.\n :return: Boolean and message indicating success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.agent_check_deregister checkid='Memory Utilization'\n\n '''\n ret = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not checkid:\n raise SaltInvocationError('Required argument \"checkid\" is missing.')\n\n function = 'agent/check/deregister/{0}'.format(checkid)\n res = _query(consul_url=consul_url,\n function=function,\n token=token,\n method='GET')\n if res['res']:\n ret['res'] = True\n ret['message'] = ('Check {0} removed from agent.'.format(checkid))\n else:\n ret['res'] = False\n ret['message'] = 'Unable to remove check from agent.'\n return ret", "response": "This function will take care of deregistering a check from the catalog."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef agent_check_warn(consul_url=None, token=None, checkid=None, **kwargs):\n '''\n This endpoint is used with a check that is of the TTL type. When this\n is called, the status of the check is set to warning and the TTL\n clock is reset.\n\n :param consul_url: The Consul server URL.\n :param checkid: The ID of the check to deregister from Consul.\n :param note: A human-readable message with the status of the check.\n :return: Boolean and message indicating success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.agent_check_warn checkid='redis_check1' note='Forcing check into warning state.'\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not checkid:\n raise SaltInvocationError('Required argument \"checkid\" is missing.')\n\n if 'note' in kwargs:\n query_params['note'] = kwargs['note']\n\n function = 'agent/check/warn/{0}'.format(checkid)\n res = _query(consul_url=consul_url,\n function=function,\n token=token,\n query_params=query_params,\n method='GET')\n if res['res']:\n ret['res'] = True\n ret['message'] = 'Check {0} marked as warning.'.format(checkid)\n else:\n ret['res'] = False\n ret['message'] = 'Unable to update check {0}.'.format(checkid)\n return ret", "response": "This endpoint is used to mark a check as warning."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef agent_service_deregister(consul_url=None, token=None, serviceid=None):\n '''\n Used to remove a service.\n\n :param consul_url: The Consul server URL.\n :param serviceid: A serviceid describing the service.\n :return: Boolean and message indicating success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.agent_service_deregister serviceid='redis'\n\n '''\n ret = {}\n data = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not serviceid:\n raise SaltInvocationError('Required argument \"serviceid\" is missing.')\n\n function = 'agent/service/deregister/{0}'.format(serviceid)\n res = _query(consul_url=consul_url,\n function=function,\n token=token,\n method='PUT',\n data=data)\n if res['res']:\n ret['res'] = True\n ret['message'] = 'Service {0} removed from agent.'.format(serviceid)\n else:\n ret['res'] = False\n ret['message'] = 'Unable to remove service {0}.'.format(serviceid)\n return ret", "response": "This function removes a service from the current node."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef agent_service_maintenance(consul_url=None, token=None, serviceid=None, **kwargs):\n '''\n Used to place a service into maintenance mode.\n\n :param consul_url: The Consul server URL.\n :param serviceid: A name of the service.\n :param enable: Whether the service should be enabled or disabled.\n :param reason: A human readable message of why the service was\n enabled or disabled.\n :return: Boolean and message indicating success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.agent_service_deregister serviceid='redis' enable='True' reason='Down for upgrade'\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not serviceid:\n raise SaltInvocationError('Required argument \"serviceid\" is missing.')\n\n if 'enable' in kwargs:\n query_params['enable'] = kwargs['enable']\n else:\n ret['message'] = 'Required parameter \"enable\" is missing.'\n ret['res'] = False\n return ret\n\n if 'reason' in kwargs:\n query_params['reason'] = kwargs['reason']\n\n function = 'agent/service/maintenance/{0}'.format(serviceid)\n res = _query(consul_url=consul_url,\n token=token,\n function=function,\n query_params=query_params)\n\n if res['res']:\n ret['res'] = True\n ret['message'] = ('Service {0} set in '\n 'maintenance mode.'.format(serviceid))\n else:\n ret['res'] = False\n ret['message'] = ('Unable to set service '\n '{0} to maintenance mode.'.format(serviceid))\n return ret", "response": "This function is used to set a service into maintenance mode."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef session_create(consul_url=None, token=None, **kwargs):\n '''\n Used to create a session.\n\n :param consul_url: The Consul server URL.\n :param lockdelay: Duration string using a \"s\" suffix for seconds.\n The default is 15s.\n :param node: Must refer to a node that is already registered,\n if specified. By default, the agent's own node\n name is used.\n :param name: A human-readable name for the session\n :param checks: A list of associated health checks. It is highly\n recommended that, if you override this list, you\n include the default \"serfHealth\".\n :param behavior: Can be set to either release or delete. This controls\n the behavior when a session is invalidated. By default,\n this is release, causing any locks that are held to be\n released. Changing this to delete causes any locks that\n are held to be deleted. delete is useful for creating\n ephemeral key/value entries.\n :param ttl: Session is invalidated if it is not renewed before\n the TTL expires\n :return: Boolean and message indicating success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.session_create node='node1' name='my-session' behavior='delete' ttl='3600s'\n\n '''\n ret = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n data = {}\n\n if 'lockdelay' in kwargs:\n data['LockDelay'] = kwargs['lockdelay']\n\n if 'node' in kwargs:\n data['Node'] = kwargs['node']\n\n if 'name' in kwargs:\n data['Name'] = kwargs['name']\n else:\n raise SaltInvocationError('Required argument \"name\" is missing.')\n\n if 'checks' in kwargs:\n data['Touch'] = kwargs['touch']\n\n if 'behavior' in kwargs:\n if not kwargs['behavior'] in ('delete', 'release'):\n ret['message'] = ('Behavior must be ',\n 'either delete or release.')\n ret['res'] = False\n return ret\n data['Behavior'] = kwargs['behavior']\n\n if 'ttl' in kwargs:\n _ttl = kwargs['ttl']\n if six.text_type(_ttl).endswith('s'):\n _ttl = _ttl[:-1]\n\n if int(_ttl) < 0 or int(_ttl) > 3600:\n ret['message'] = ('TTL must be ',\n 'between 0 and 3600.')\n ret['res'] = False\n return ret\n data['TTL'] = '{0}s'.format(_ttl)\n\n function = 'session/create'\n res = _query(consul_url=consul_url,\n function=function,\n token=token,\n method='PUT',\n data=data)\n\n if res['res']:\n ret['res'] = True\n ret['message'] = 'Created session {0}.'.format(kwargs['name'])\n else:\n ret['res'] = False\n ret['message'] = 'Unable to create session {0}.'.format(kwargs['name'])\n return ret", "response": "This function creates a new session in a node."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nuses to list sessions. :param consul_url: The Consul server URL. :param dc: By default, the datacenter of the agent is queried; however, the dc can be provided using the \"dc\" parameter. :param return_list: By default, all information about the sessions is returned, using the return_list parameter will return a list of session IDs. :return: A list of all available sessions. CLI Example: .. code-block:: bash salt '*' consul.session_list", "response": "def session_list(consul_url=None, token=None, return_list=False, **kwargs):\n '''\n Used to list sessions.\n\n :param consul_url: The Consul server URL.\n :param dc: By default, the datacenter of the agent is queried;\n however, the dc can be provided using the \"dc\" parameter.\n :param return_list: By default, all information about the sessions is\n returned, using the return_list parameter will return\n a list of session IDs.\n :return: A list of all available sessions.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.session_list\n\n '''\n ret = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n query_params = {}\n\n if 'dc' in kwargs:\n query_params['dc'] = kwargs['dc']\n\n function = 'session/list'\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n query_params=query_params)\n\n if return_list:\n _list = []\n for item in ret['data']:\n _list.append(item['ID'])\n return _list\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndestroying session :param consul_url: The Consul server URL. :param session: The ID of the session to destroy. :param dc: By default, the datacenter of the agent is queried; however, the dc can be provided using the \"dc\" parameter. :return: Boolean & message of success or failure. CLI Example: .. code-block:: bash salt '*' consul.session_destroy session='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'", "response": "def session_destroy(consul_url=None, token=None, session=None, **kwargs):\n '''\n Destroy session\n\n :param consul_url: The Consul server URL.\n :param session: The ID of the session to destroy.\n :param dc: By default, the datacenter of the agent is queried;\n however, the dc can be provided using the \"dc\" parameter.\n :return: Boolean & message of success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.session_destroy session='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'\n\n '''\n ret = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not session:\n raise SaltInvocationError('Required argument \"session\" is missing.')\n\n query_params = {}\n\n if 'dc' in kwargs:\n query_params['dc'] = kwargs['dc']\n\n function = 'session/destroy/{0}'.format(session)\n res = _query(consul_url=consul_url,\n function=function,\n token=token,\n query_params=query_params)\n if res['res']:\n ret['res'] = True\n ret['message'] = 'Created Service {0}.'.format(kwargs['name'])\n else:\n ret['res'] = False\n ret['message'] = 'Unable to create service {0}.'.format(kwargs['name'])\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef session_info(consul_url=None, token=None, session=None, **kwargs):\n '''\n Information about a session\n\n :param consul_url: The Consul server URL.\n :param session: The ID of the session to return information about.\n :param dc: By default, the datacenter of the agent is queried;\n however, the dc can be provided using the \"dc\" parameter.\n :return: Boolean & message of success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.session_info session='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'\n\n '''\n ret = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not session:\n raise SaltInvocationError('Required argument \"session\" is missing.')\n\n query_params = {}\n\n if 'dc' in kwargs:\n query_params['dc'] = kwargs['dc']\n\n function = 'session/info/{0}'.format(session)\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n query_params=query_params)\n return ret", "response": "Return information about a session."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef catalog_register(consul_url=None, token=None, **kwargs):\n '''\n Registers a new node, service, or check\n\n :param consul_url: The Consul server URL.\n :param dc: By default, the datacenter of the agent is queried;\n however, the dc can be provided using the \"dc\" parameter.\n :param node: The node to register.\n :param address: The address of the node.\n :param service: The service that will be registered.\n :param service_address: The address that the service listens on.\n :param service_port: The port for the service.\n :param service_id: A unique identifier for the service, if this is not\n provided \"name\" will be used.\n :param service_tags: Any tags associated with the service.\n :param check: The name of the health check to register\n :param check_status: The initial status of the check,\n must be one of unknown, passing, warning, or critical.\n :param check_service: The service that the check is performed against.\n :param check_id: Unique identifier for the service.\n :param check_notes: An opaque field that is meant to hold human-readable text.\n :return: Boolean & message of success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.catalog_register node='node1' address='192.168.1.1' service='redis' service_address='127.0.0.1' service_port='8080' service_id='redis_server1'\n\n '''\n ret = {}\n data = {}\n data['NodeMeta'] = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if 'datacenter' in kwargs:\n data['Datacenter'] = kwargs['datacenter']\n\n if 'node' in kwargs:\n data['Node'] = kwargs['node']\n else:\n ret['message'] = 'Required argument node argument is missing.'\n ret['res'] = False\n return ret\n\n if 'address' in kwargs:\n if isinstance(kwargs['address'], list):\n _address = kwargs['address'][0]\n else:\n _address = kwargs['address']\n data['Address'] = _address\n else:\n ret['message'] = 'Required argument address argument is missing.'\n ret['res'] = False\n return ret\n\n if 'ip_interfaces' in kwargs:\n data['TaggedAddresses'] = {}\n for k in kwargs['ip_interfaces']:\n if kwargs['ip_interfaces'].get(k):\n data['TaggedAddresses'][k] = kwargs['ip_interfaces'][k][0]\n\n if 'service' in kwargs:\n data['Service'] = {}\n data['Service']['Service'] = kwargs['service']\n\n if 'service_address' in kwargs:\n data['Service']['Address'] = kwargs['service_address']\n\n if 'service_port' in kwargs:\n data['Service']['Port'] = kwargs['service_port']\n\n if 'service_id' in kwargs:\n data['Service']['ID'] = kwargs['service_id']\n\n if 'service_tags' in kwargs:\n _tags = kwargs['service_tags']\n if not isinstance(_tags, list):\n _tags = [_tags]\n data['Service']['Tags'] = _tags\n\n if 'cpu' in kwargs:\n data['NodeMeta']['Cpu'] = kwargs['cpu']\n\n if 'num_cpus' in kwargs:\n data['NodeMeta']['Cpu_num'] = kwargs['num_cpus']\n\n if 'mem' in kwargs:\n data['NodeMeta']['Memory'] = kwargs['mem']\n\n if 'oscode' in kwargs:\n data['NodeMeta']['Os'] = kwargs['oscode']\n\n if 'osarch' in kwargs:\n data['NodeMeta']['Osarch'] = kwargs['osarch']\n\n if 'kernel' in kwargs:\n data['NodeMeta']['Kernel'] = kwargs['kernel']\n\n if 'kernelrelease' in kwargs:\n data['NodeMeta']['Kernelrelease'] = kwargs['kernelrelease']\n\n if 'localhost' in kwargs:\n data['NodeMeta']['localhost'] = kwargs['localhost']\n\n if 'nodename' in kwargs:\n data['NodeMeta']['nodename'] = kwargs['nodename']\n\n if 'os_family' in kwargs:\n data['NodeMeta']['os_family'] = kwargs['os_family']\n\n if 'lsb_distrib_description' in kwargs:\n data['NodeMeta']['lsb_distrib_description'] = kwargs['lsb_distrib_description']\n\n if 'master' in kwargs:\n data['NodeMeta']['master'] = kwargs['master']\n\n if 'check' in kwargs:\n data['Check'] = {}\n data['Check']['Name'] = kwargs['check']\n\n if 'check_status' in kwargs:\n if kwargs['check_status'] not in ('unknown', 'passing', 'warning', 'critical'):\n ret['message'] = 'Check status must be unknown, passing, warning, or critical.'\n ret['res'] = False\n return ret\n data['Check']['Status'] = kwargs['check_status']\n\n if 'check_service' in kwargs:\n data['Check']['ServiceID'] = kwargs['check_service']\n\n if 'check_id' in kwargs:\n data['Check']['CheckID'] = kwargs['check_id']\n\n if 'check_notes' in kwargs:\n data['Check']['Notes'] = kwargs['check_notes']\n\n function = 'catalog/register'\n res = _query(consul_url=consul_url,\n function=function,\n token=token,\n method='PUT',\n data=data)\n if res['res']:\n ret['res'] = True\n ret['message'] = ('Catalog registration '\n 'for {0} successful.'.format(kwargs['node']))\n else:\n ret['res'] = False\n ret['message'] = ('Catalog registration '\n 'for {0} failed.'.format(kwargs['node']))\n ret['data'] = data\n return ret", "response": "This function registers a new node service or check with the Redis server."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns list of available datacenters from catalog.", "response": "def catalog_datacenters(consul_url=None, token=None):\n '''\n Return list of available datacenters from catalog.\n\n :param consul_url: The Consul server URL.\n :return: The list of available datacenters.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.catalog_datacenters\n\n '''\n ret = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n function = 'catalog/datacenters'\n ret = _query(consul_url=consul_url,\n function=function,\n token=token)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning list of available nodes from catalog.", "response": "def catalog_nodes(consul_url=None, token=None, **kwargs):\n '''\n Return list of available nodes from catalog.\n\n :param consul_url: The Consul server URL.\n :param dc: By default, the datacenter of the agent is queried;\n however, the dc can be provided using the \"dc\" parameter.\n :return: The list of available nodes.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.catalog_nodes\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if 'dc' in kwargs:\n query_params['dc'] = kwargs['dc']\n\n function = 'catalog/nodes'\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n query_params=query_params)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting health information about a node.", "response": "def health_node(consul_url=None, token=None, node=None, **kwargs):\n '''\n Health information about the registered node.\n\n :param consul_url: The Consul server URL.\n :param node: The node to request health information about.\n :param dc: By default, the datacenter of the agent is queried;\n however, the dc can be provided using the \"dc\" parameter.\n :return: Health information about the requested node.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.health_node node='node1'\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not node:\n raise SaltInvocationError('Required argument \"node\" is missing.')\n\n if 'dc' in kwargs:\n query_params['dc'] = kwargs['dc']\n\n function = 'health/node/{0}'.format(node)\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n query_params=query_params)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets health information about a node.", "response": "def health_checks(consul_url=None, token=None, service=None, **kwargs):\n '''\n Health information about the registered service.\n\n :param consul_url: The Consul server URL.\n :param service: The service to request health information about.\n :param dc: By default, the datacenter of the agent is queried;\n however, the dc can be provided using the \"dc\" parameter.\n :return: Health information about the requested node.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.health_checks service='redis1'\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not service:\n raise SaltInvocationError('Required argument \"service\" is missing.')\n\n if 'dc' in kwargs:\n query_params['dc'] = kwargs['dc']\n\n function = 'health/checks/{0}'.format(service)\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n query_params=query_params)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef health_state(consul_url=None, token=None, state=None, **kwargs):\n '''\n Returns the checks in the state provided on the path.\n\n :param consul_url: The Consul server URL.\n :param state: The state to show checks for. The supported states\n are any, unknown, passing, warning, or critical.\n The any state is a wildcard that can be used to\n return all checks.\n :param dc: By default, the datacenter of the agent is queried;\n however, the dc can be provided using the \"dc\" parameter.\n :return: The checks in the provided state.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.health_state state='redis1'\n\n salt '*' consul.health_state service='redis1' passing='True'\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not state:\n raise SaltInvocationError('Required argument \"state\" is missing.')\n\n if 'dc' in kwargs:\n query_params['dc'] = kwargs['dc']\n\n if state not in ('any', 'unknown', 'passing', 'warning', 'critical'):\n ret['message'] = 'State must be any, unknown, passing, warning, or critical.'\n ret['res'] = False\n return ret\n\n function = 'health/state/{0}'.format(state)\n ret = _query(consul_url=consul_url,\n function=function,\n token=token,\n query_params=query_params)\n return ret", "response": "Return the checks in the provided state on the path."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef acl_delete(consul_url=None, token=None, **kwargs):\n '''\n Delete an ACL token.\n\n :param consul_url: The Consul server URL.\n :param id: Unique identifier for the ACL to update.\n :return: Boolean & message of success or failure.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.acl_delete id='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'\n\n '''\n ret = {}\n data = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if 'id' not in kwargs:\n ret['message'] = 'Required parameter \"id\" is missing.'\n ret['res'] = False\n return ret\n\n function = 'acl/delete/{0}'.format(kwargs['id'])\n res = _query(consul_url=consul_url,\n token=token,\n data=data,\n method='PUT',\n function=function)\n\n if res['res']:\n ret['res'] = True\n ret['message'] = 'ACL {0} deleted.'.format(kwargs['id'])\n else:\n ret['res'] = False\n ret['message'] = ('Removing ACL '\n '{0} failed.'.format(kwargs['id']))\n\n return ret", "response": "Delete an ACL token."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef acl_info(consul_url=None, **kwargs):\n '''\n Information about an ACL token.\n\n :param consul_url: The Consul server URL.\n :param id: Unique identifier for the ACL to update.\n :return: Information about the ACL requested.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.acl_info id='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'\n\n '''\n ret = {}\n data = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if 'id' not in kwargs:\n ret['message'] = 'Required parameter \"id\" is missing.'\n ret['res'] = False\n return ret\n\n function = 'acl/info/{0}'.format(kwargs['id'])\n ret = _query(consul_url=consul_url,\n data=data,\n method='GET',\n function=function)\n return ret", "response": "Return information about an ACL token."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlist the ACL tokens.", "response": "def acl_list(consul_url=None, token=None, **kwargs):\n '''\n List the ACL tokens.\n\n :param consul_url: The Consul server URL.\n :return: List of ACLs\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.acl_list\n\n '''\n ret = {}\n data = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if 'id' not in kwargs:\n ret['message'] = 'Required parameter \"id\" is missing.'\n ret['res'] = False\n return ret\n\n function = 'acl/list'\n ret = _query(consul_url=consul_url,\n token=token,\n data=data,\n method='PUT',\n function=function)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nlists the ACL tokens. :param consul_url: The Consul server URL. :param name: The name of the event to fire. :param dc: By default, the datacenter of the agent is queried; however, the dc can be provided using the \"dc\" parameter. :param node: Filter by node name. :param service: Filter by service name. :param tag: Filter by tag name. :return: List of ACLs CLI Example: .. code-block:: bash salt '*' consul.event_fire name='deploy'", "response": "def event_fire(consul_url=None, token=None, name=None, **kwargs):\n '''\n List the ACL tokens.\n\n :param consul_url: The Consul server URL.\n :param name: The name of the event to fire.\n :param dc: By default, the datacenter of the agent is queried;\n however, the dc can be provided using the \"dc\" parameter.\n :param node: Filter by node name.\n :param service: Filter by service name.\n :param tag: Filter by tag name.\n :return: List of ACLs\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.event_fire name='deploy'\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if not name:\n raise SaltInvocationError('Required argument \"name\" is missing.')\n\n if 'dc' in kwargs:\n query_params = kwargs['dc']\n\n if 'node' in kwargs:\n query_params = kwargs['node']\n\n if 'service' in kwargs:\n query_params = kwargs['service']\n\n if 'tag' in kwargs:\n query_params = kwargs['tag']\n\n function = 'event/fire/{0}'.format(name)\n res = _query(consul_url=consul_url,\n token=token,\n query_params=query_params,\n method='PUT',\n function=function)\n\n if res['res']:\n ret['res'] = True\n ret['message'] = 'Event {0} fired.'.format(name)\n ret['data'] = ret['data']\n else:\n ret['res'] = False\n ret['message'] = ('Cloning ACL'\n 'item {0} failed.'.format(kwargs['name']))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlisting the recent events.", "response": "def event_list(consul_url=None, token=None, **kwargs):\n '''\n List the recent events.\n\n :param consul_url: The Consul server URL.\n :param name: The name of the event to fire.\n :return: List of ACLs\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' consul.event_list\n\n '''\n ret = {}\n query_params = {}\n if not consul_url:\n consul_url = _get_config()\n if not consul_url:\n log.error('No Consul URL found.')\n ret['message'] = 'No Consul URL found.'\n ret['res'] = False\n return ret\n\n if 'name' in kwargs:\n query_params = kwargs['name']\n else:\n raise SaltInvocationError('Required argument \"name\" is missing.')\n\n function = 'event/list/'\n ret = _query(consul_url=consul_url,\n token=token,\n query_params=query_params,\n function=function)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef managed(name, value, **kwargs):\n '''\n Ensure a sysrc variable is set to a specific value.\n\n name\n The variable name to set\n value\n Value to set the variable to\n file\n (optional) The rc file to add the variable to.\n jail\n (option) the name or JID of the jail to set the value in.\n\n Example:\n\n .. code-block:: yaml\n\n syslogd:\n sysrc.managed:\n - name: syslogd_flags\n - value: -ss\n '''\n\n ret = {'name': name, 'changes': {}, 'result': False, 'comment': ''}\n\n # Check the current state\n current_state = __salt__['sysrc.get'](name=name, **kwargs)\n if current_state is not None:\n for rcname, rcdict in six.iteritems(current_state):\n if rcdict[name] == value:\n ret['result'] = True\n ret['comment'] = '{0} is already set to the desired value.'.format(name)\n return ret\n\n if __opts__['test'] is True:\n ret['comment'] = 'The value of \"{0}\" will be changed!'.format(name)\n ret['changes'] = {\n 'old': current_state,\n 'new': name+' = '+value+' will be set.'\n }\n\n # When test=true return none\n ret['result'] = None\n\n return ret\n\n new_state = __salt__['sysrc.set'](name=name, value=value, **kwargs)\n\n ret['comment'] = 'The value of \"{0}\" was changed!'.format(name)\n\n ret['changes'] = {\n 'old': current_state,\n 'new': new_state\n }\n\n ret['result'] = True\n\n return ret", "response": "Ensure a sysrc variable is set to a specific value."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef item(*args):\n '''\n .. versionadded:: 0.16.2\n\n Return one or more pillar entries\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pillar.item foo\n salt '*' pillar.item foo bar baz\n '''\n ret = {}\n for arg in args:\n try:\n ret[arg] = __pillar__[arg]\n except KeyError:\n pass\n return ret", "response": "Return one or more pillar entries"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the raw pillar data that is available in the module.", "response": "def raw(key=None):\n '''\n Return the raw pillar data that is available in the module. This will\n show the pillar as it is loaded as the __pillar__ dict.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pillar.raw\n\n With the optional key argument, you can select a subtree of the\n pillar raw data.::\n\n salt '*' pillar.raw key='roles'\n '''\n if key:\n ret = __pillar__.get(key, {})\n else:\n ret = __pillar__\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of keys from the named value in the pillar", "response": "def keys(key, delimiter=DEFAULT_TARGET_DELIM):\n '''\n .. versionadded:: 2015.8.0\n\n Attempt to retrieve a list of keys from the named value from the pillar.\n\n The value can also represent a value in a nested dict using a \":\" delimiter\n for the dict, similar to how pillar.get works.\n\n delimiter\n Specify an alternate delimiter to use when traversing a nested dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pillar.keys web:sites\n '''\n ret = salt.utils.data.traverse_dict_and_list(\n __pillar__, key, KeyError, delimiter)\n\n if ret is KeyError:\n raise KeyError(\"Pillar key not found: {0}\".format(key))\n\n if not isinstance(ret, dict):\n raise ValueError(\"Pillar value in key {0} is not a dict\".format(key))\n\n return ret.keys()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring a domain exists in the specified state definition.", "response": "def present(name, DomainName,\n ElasticsearchClusterConfig=None,\n EBSOptions=None,\n AccessPolicies=None,\n SnapshotOptions=None,\n AdvancedOptions=None,\n Tags=None,\n region=None, key=None, keyid=None, profile=None,\n ElasticsearchVersion=\"1.5\"):\n '''\n Ensure domain exists.\n\n name\n The name of the state definition\n\n DomainName\n Name of the domain.\n\n ElasticsearchClusterConfig\n Configuration options for an Elasticsearch domain. Specifies the\n instance type and number of instances in the domain cluster.\n\n InstanceType (string) --\n The instance type for an Elasticsearch cluster.\n\n InstanceCount (integer) --\n The number of instances in the specified domain cluster.\n\n DedicatedMasterEnabled (boolean) --\n A boolean value to indicate whether a dedicated master node is enabled.\n See About Dedicated Master Nodes for more information.\n\n ZoneAwarenessEnabled (boolean) --\n A boolean value to indicate whether zone awareness is enabled. See About\n Zone Awareness for more information.\n\n DedicatedMasterType (string) --\n The instance type for a dedicated master node.\n\n DedicatedMasterCount (integer) --\n Total number of dedicated master nodes, active and on standby, for the\n cluster.\n\n EBSOptions\n Options to enable, disable and specify the type and size of EBS storage\n volumes.\n\n EBSEnabled (boolean) --\n Specifies whether EBS-based storage is enabled.\n\n VolumeType (string) --\n Specifies the volume type for EBS-based storage.\n\n VolumeSize (integer) --\n Integer to specify the size of an EBS volume.\n\n Iops (integer) --\n Specifies the IOPD for a Provisioned IOPS EBS volume (SSD).\n\n AccessPolicies\n IAM access policy\n\n SnapshotOptions\n Option to set time, in UTC format, of the daily automated snapshot.\n Default value is 0 hours.\n\n AutomatedSnapshotStartHour (integer) --\n Specifies the time, in UTC format, when the service takes a daily\n automated snapshot of the specified Elasticsearch domain. Default value\n is 0 hours.\n\n AdvancedOptions\n Option to allow references to indices in an HTTP request body. Must be\n false when configuring access to individual sub-resources. By default,\n the value is true .\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n\n ElasticsearchVersion\n String of format X.Y to specify version for the Elasticsearch domain eg.\n \"1.5\" or \"2.3\".\n '''\n ret = {'name': DomainName,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n if ElasticsearchClusterConfig is None:\n ElasticsearchClusterConfig = {\n 'DedicatedMasterEnabled': False,\n 'InstanceCount': 1,\n 'InstanceType': 'm3.medium.elasticsearch',\n 'ZoneAwarenessEnabled': False\n }\n if EBSOptions is None:\n EBSOptions = {\n 'EBSEnabled': False,\n }\n if SnapshotOptions is None:\n SnapshotOptions = {\n 'AutomatedSnapshotStartHour': 0\n }\n if AdvancedOptions is None:\n AdvancedOptions = {\n 'rest.action.multi.allow_explicit_index': 'true'\n }\n if Tags is None:\n Tags = {}\n if AccessPolicies is not None and isinstance(AccessPolicies, six.string_types):\n try:\n AccessPolicies = salt.utils.json.loads(AccessPolicies)\n except ValueError as e:\n ret['result'] = False\n ret['comment'] = 'Failed to create domain: {0}.'.format(e.message)\n return ret\n r = __salt__['boto_elasticsearch_domain.exists'](DomainName=DomainName,\n region=region, key=key, keyid=keyid, profile=profile)\n\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Failed to create domain: {0}.'.format(r['error']['message'])\n return ret\n\n if not r.get('exists'):\n if __opts__['test']:\n ret['comment'] = 'Domain {0} is set to be created.'.format(DomainName)\n ret['result'] = None\n return ret\n r = __salt__['boto_elasticsearch_domain.create'](DomainName=DomainName,\n ElasticsearchClusterConfig=ElasticsearchClusterConfig,\n EBSOptions=EBSOptions,\n AccessPolicies=AccessPolicies,\n SnapshotOptions=SnapshotOptions,\n AdvancedOptions=AdvancedOptions,\n ElasticsearchVersion=str(ElasticsearchVersion), # future lint: disable=blacklisted-function\n region=region, key=key,\n keyid=keyid, profile=profile)\n if not r.get('created'):\n ret['result'] = False\n ret['comment'] = 'Failed to create domain: {0}.'.format(r['error']['message'])\n return ret\n _describe = __salt__['boto_elasticsearch_domain.describe'](DomainName,\n region=region, key=key, keyid=keyid, profile=profile)\n ret['changes']['old'] = {'domain': None}\n ret['changes']['new'] = _describe\n ret['comment'] = 'Domain {0} created.'.format(DomainName)\n return ret\n\n ret['comment'] = os.linesep.join([ret['comment'], 'Domain {0} is present.'.format(DomainName)])\n ret['changes'] = {}\n # domain exists, ensure config matches\n _status = __salt__['boto_elasticsearch_domain.status'](DomainName=DomainName,\n region=region, key=key, keyid=keyid,\n profile=profile)['domain']\n if _status.get('ElasticsearchVersion') != str(ElasticsearchVersion): # future lint: disable=blacklisted-function\n ret['result'] = False\n ret['comment'] = (\n 'Failed to update domain: version cannot be modified '\n 'from {0} to {1}.'.format(\n _status.get('ElasticsearchVersion'),\n str(ElasticsearchVersion) # future lint: disable=blacklisted-function\n )\n )\n return ret\n _describe = __salt__['boto_elasticsearch_domain.describe'](DomainName=DomainName,\n region=region, key=key, keyid=keyid,\n profile=profile)['domain']\n _describe['AccessPolicies'] = salt.utils.json.loads(_describe['AccessPolicies'])\n\n # When EBSEnabled is false, describe returns extra values that can't be set\n if not _describe.get('EBSOptions', {}).get('EBSEnabled'):\n opts = _describe.get('EBSOptions', {})\n opts.pop('VolumeSize', None)\n opts.pop('VolumeType', None)\n\n comm_args = {}\n need_update = False\n es_opts = {'ElasticsearchClusterConfig': ElasticsearchClusterConfig,\n 'EBSOptions': EBSOptions,\n 'AccessPolicies': AccessPolicies,\n 'SnapshotOptions': SnapshotOptions,\n 'AdvancedOptions': AdvancedOptions}\n\n for k, v in six.iteritems(es_opts):\n if not _compare_json(v, _describe[k]):\n need_update = True\n comm_args[k] = v\n ret['changes'].setdefault('new', {})[k] = v\n ret['changes'].setdefault('old', {})[k] = _describe[k]\n if need_update:\n if __opts__['test']:\n msg = 'Domain {0} set to be modified.'.format(DomainName)\n ret['comment'] = msg\n ret['result'] = None\n return ret\n\n ret['comment'] = os.linesep.join([ret['comment'], 'Domain to be modified'])\n\n r = __salt__['boto_elasticsearch_domain.update'](DomainName=DomainName,\n region=region, key=key,\n keyid=keyid, profile=profile,\n **comm_args)\n if not r.get('updated'):\n ret['result'] = False\n ret['comment'] = 'Failed to update domain: {0}.'.format(r['error'])\n ret['changes'] = {}\n return ret\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef open(self, new=False):\n '''\n Init the database, if required.\n '''\n self._db.new() if new else self._db.open() # pylint: disable=W0106\n self._run_init_queries()", "response": "Open the database if required."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ninitializing the tables in the database.", "response": "def _run_init_queries(self):\n '''\n Initialization queries\n '''\n for obj in (Package, PackageCfgFile, PayloadFile, IgnoredDir, AllowedDir):\n self._db.create_table_from_object(obj())"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef purge(self):\n '''\n Purge whole database.\n '''\n for table_name in self._db.list_tables():\n self._db.flush(table_name)\n\n self._run_init_queries()", "response": "Purge all the data in the database."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_config_value(profile, config_name):\n '''\n Helper function that returns a profile's configuration value based on\n the supplied configuration name.\n\n profile\n The profile name that contains configuration information.\n\n config_name\n The configuration item's name to use to return configuration values.\n '''\n config = __salt__['config.option'](profile)\n if not config:\n raise CommandExecutionError(\n 'Authentication information could not be found for the '\n '\\'{0}\\' profile.'.format(profile)\n )\n\n config_value = config.get(config_name)\n if config_value is None:\n raise CommandExecutionError(\n 'The \\'{0}\\' parameter was not found in the \\'{1}\\' '\n 'profile.'.format(\n config_name,\n profile\n )\n )\n\n return config_value", "response": "Helper function that returns a profile s configuration value based on the supplied configuration name."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_client(profile):\n '''\n Return the GitHub client, cached into __context__ for performance\n '''\n token = _get_config_value(profile, 'token')\n key = 'github.{0}:{1}'.format(\n token,\n _get_config_value(profile, 'org_name')\n )\n\n if key not in __context__:\n __context__[key] = github.Github(\n token,\n per_page=100\n )\n return __context__[key]", "response": "Return the GitHub client cached into __context__ for performance\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_users(profile=\"github\", ignore_cache=False):\n '''\n List all users within the organization.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n ignore_cache\n Bypasses the use of cached users.\n\n .. versionadded:: 2016.11.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.list_users\n salt myminion github.list_users profile='my-github-profile'\n '''\n org_name = _get_config_value(profile, 'org_name')\n key = \"github.{0}:users\".format(\n org_name\n )\n if key not in __context__ or ignore_cache:\n client = _get_client(profile)\n organization = client.get_organization(org_name)\n __context__[key] = [member.login for member in _get_members(organization, None)]\n return __context__[key]", "response": "List all users within the organization."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets a GitHub user by name.", "response": "def get_user(name, profile='github', user_details=False):\n '''\n Get a GitHub user by name.\n\n name\n The user for which to obtain information.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n user_details\n Prints user information details. Defaults to ``False``. If the user is\n already in the organization and user_details is set to False, the\n get_user function returns ``True``. If the user is not already present\n in the organization, user details will be printed by default.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.get_user github-handle\n salt myminion github.get_user github-handle user_details=true\n\n '''\n\n if not user_details and name in list_users(profile):\n # User is in the org, no need for additional Data\n return True\n\n response = {}\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n\n try:\n user = client.get_user(name)\n except UnknownObjectException:\n log.exception(\"Resource not found\")\n return False\n\n response['company'] = user.company\n response['created_at'] = user.created_at\n response['email'] = user.email\n response['html_url'] = user.html_url\n response['id'] = user.id\n response['login'] = user.login\n response['name'] = user.name\n response['type'] = user.type\n response['url'] = user.url\n\n try:\n headers, data = organization._requester.requestJsonAndCheck(\n \"GET\",\n organization.url + \"/memberships/\" + user._identity\n )\n except UnknownObjectException:\n response['membership_state'] = 'nonexistent'\n response['in_org'] = False\n return response\n\n response['in_org'] = organization.has_in_members(user)\n response['membership_state'] = data.get('state')\n\n return response"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding a GitHub user to the list of users.", "response": "def add_user(name, profile='github'):\n '''\n Add a GitHub user.\n\n name\n The user for which to obtain information.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.add_user github-handle\n '''\n\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n\n try:\n github_named_user = client.get_user(name)\n except UnknownObjectException:\n log.exception(\"Resource not found\")\n return False\n\n headers, data = organization._requester.requestJsonAndCheck(\n \"PUT\",\n organization.url + \"/memberships/\" + github_named_user._identity\n )\n\n return data.get('state') == 'pending'"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef remove_user(name, profile='github'):\n '''\n Remove a Github user by name.\n\n name\n The user for which to obtain information.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.remove_user github-handle\n '''\n\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n\n try:\n git_user = client.get_user(name)\n except UnknownObjectException:\n log.exception(\"Resource not found\")\n return False\n\n if organization.has_in_members(git_user):\n organization.remove_from_members(git_user)\n\n return not organization.has_in_members(git_user)", "response": "Remove a Github user by name."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_issue(issue_number, repo_name=None, profile='github', output='min'):\n '''\n Return information about a single issue in a named repository.\n\n .. versionadded:: 2016.11.0\n\n issue_number\n The number of the issue to retrieve.\n\n repo_name\n The name of the repository from which to get the issue. This argument is\n required, either passed via the CLI, or defined in the configured\n profile. A ``repo_name`` passed as a CLI argument will override the\n repo_name defined in the configured profile, if provided.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n output\n The amount of data returned by each issue. Defaults to ``min``. Change\n to ``full`` to see all issue output.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.get_issue 514\n salt myminion github.get_issue 514 repo_name=salt\n '''\n org_name = _get_config_value(profile, 'org_name')\n if repo_name is None:\n repo_name = _get_config_value(profile, 'repo_name')\n\n action = '/'.join(['repos', org_name, repo_name])\n command = 'issues/' + six.text_type(issue_number)\n\n ret = {}\n issue_data = _query(profile, action=action, command=command)\n\n issue_id = issue_data.get('id')\n if output == 'full':\n ret[issue_id] = issue_data\n else:\n ret[issue_id] = _format_issue(issue_data)\n\n return ret", "response": "Get information about a single issue in a named repository."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets the comments for a given issue in a named repository.", "response": "def get_issue_comments(issue_number,\n repo_name=None,\n profile='github',\n since=None,\n output='min'):\n '''\n Return information about the comments for a given issue in a named repository.\n\n .. versionadded:: 2016.11.0\n\n issue_number\n The number of the issue for which to retrieve comments.\n\n repo_name\n The name of the repository to which the issue belongs. This argument is\n required, either passed via the CLI, or defined in the configured\n profile. A ``repo_name`` passed as a CLI argument will override the\n repo_name defined in the configured profile, if provided.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n since\n Only comments updated at or after this time are returned. This is a\n timestamp in ISO 8601 format: ``YYYY-MM-DDTHH:MM:SSZ``.\n\n output\n The amount of data returned by each issue. Defaults to ``min``. Change\n to ``full`` to see all issue output.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.get_issue_comments 514\n salt myminion github.get_issue 514 repo_name=salt\n '''\n org_name = _get_config_value(profile, 'org_name')\n if repo_name is None:\n repo_name = _get_config_value(profile, 'repo_name')\n\n action = '/'.join(['repos', org_name, repo_name])\n command = '/'.join(['issues', six.text_type(issue_number), 'comments'])\n\n args = {}\n if since:\n args['since'] = since\n\n comments = _query(profile, action=action, command=command, args=args)\n\n ret = {}\n for comment in comments:\n comment_id = comment.get('id')\n if output == 'full':\n ret[comment_id] = comment\n else:\n ret[comment_id] = {'id': comment.get('id'),\n 'created_at': comment.get('created_at'),\n 'updated_at': comment.get('updated_at'),\n 'user_login': comment.get('user').get('login')}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a new tree of issues in a given repository.", "response": "def get_issues(repo_name=None,\n profile='github',\n milestone=None,\n state='open',\n assignee=None,\n creator=None,\n mentioned=None,\n labels=None,\n sort='created',\n direction='desc',\n since=None,\n output='min',\n per_page=None):\n '''\n Returns information for all issues in a given repository, based on the search options.\n\n .. versionadded:: 2016.11.0\n\n repo_name\n The name of the repository for which to list issues. This argument is\n required, either passed via the CLI, or defined in the configured\n profile. A ``repo_name`` passed as a CLI argument will override the\n repo_name defined in the configured profile, if provided.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n milestone\n The number of a GitHub milestone, or a string of either ``*`` or\n ``none``.\n\n If a number is passed, it should refer to a milestone by its number\n field. Use the ``github.get_milestone`` function to obtain a milestone's\n number.\n\n If the string ``*`` is passed, issues with any milestone are\n accepted. If the string ``none`` is passed, issues without milestones\n are returned.\n\n state\n Indicates the state of the issues to return. Can be either ``open``,\n ``closed``, or ``all``. Default is ``open``.\n\n assignee\n Can be the name of a user. Pass in ``none`` (as a string) for issues\n with no assigned user or ``*`` for issues assigned to any user.\n\n creator\n The user that created the issue.\n\n mentioned\n A user that's mentioned in the issue.\n\n labels\n A string of comma separated label names. For example, ``bug,ui,@high``.\n\n sort\n What to sort results by. Can be either ``created``, ``updated``, or\n ``comments``. Default is ``created``.\n\n direction\n The direction of the sort. Can be either ``asc`` or ``desc``. Default\n is ``desc``.\n\n since\n Only issues updated at or after this time are returned. This is a\n timestamp in ISO 8601 format: ``YYYY-MM-DDTHH:MM:SSZ``.\n\n output\n The amount of data returned by each issue. Defaults to ``min``. Change\n to ``full`` to see all issue output.\n\n per_page\n GitHub paginates data in their API calls. Use this value to increase or\n decrease the number of issues gathered from GitHub, per page. If not set,\n GitHub defaults are used. Maximum is 100.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.get_issues my-github-repo\n '''\n org_name = _get_config_value(profile, 'org_name')\n if repo_name is None:\n repo_name = _get_config_value(profile, 'repo_name')\n\n action = '/'.join(['repos', org_name, repo_name])\n args = {}\n\n # Build API arguments, as necessary.\n if milestone:\n args['milestone'] = milestone\n if assignee:\n args['assignee'] = assignee\n if creator:\n args['creator'] = creator\n if mentioned:\n args['mentioned'] = mentioned\n if labels:\n args['labels'] = labels\n if since:\n args['since'] = since\n if per_page:\n args['per_page'] = per_page\n\n # Only pass the following API args if they're not the defaults listed.\n if state and state != 'open':\n args['state'] = state\n if sort and sort != 'created':\n args['sort'] = sort\n if direction and direction != 'desc':\n args['direction'] = direction\n\n ret = {}\n issues = _query(profile, action=action, command='issues', args=args)\n\n for issue in issues:\n # Pull requests are included in the issue list from GitHub\n # Let's not include those in the return.\n if issue.get('pull_request'):\n continue\n issue_id = issue.get('id')\n if output == 'full':\n ret[issue_id] = issue\n else:\n ret[issue_id] = _format_issue(issue)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn information about milestones for a given repository.", "response": "def get_milestones(repo_name=None,\n profile='github',\n state='open',\n sort='due_on',\n direction='asc',\n output='min',\n per_page=None):\n '''\n Return information about milestones for a given repository.\n\n .. versionadded:: 2016.11.0\n\n repo_name\n The name of the repository for which to list issues. This argument is\n required, either passed via the CLI, or defined in the configured\n profile. A ``repo_name`` passed as a CLI argument will override the\n repo_name defined in the configured profile, if provided.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n state\n The state of the milestone. Either ``open``, ``closed``, or ``all``.\n Default is ``open``.\n\n sort\n What to sort results by. Either ``due_on`` or ``completeness``. Default\n is ``due_on``.\n\n direction\n The direction of the sort. Either ``asc`` or ``desc``. Default is ``asc``.\n\n output\n The amount of data returned by each issue. Defaults to ``min``. Change\n to ``full`` to see all issue output.\n\n per_page\n GitHub paginates data in their API calls. Use this value to increase or\n decrease the number of issues gathered from GitHub, per page. If not set,\n GitHub defaults are used.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.get_milestones\n\n '''\n org_name = _get_config_value(profile, 'org_name')\n if repo_name is None:\n repo_name = _get_config_value(profile, 'repo_name')\n\n action = '/'.join(['repos', org_name, repo_name])\n args = {}\n\n if per_page:\n args['per_page'] = per_page\n\n # Only pass the following API args if they're not the defaults listed.\n if state and state != 'open':\n args['state'] = state\n if sort and sort != 'due_on':\n args['sort'] = sort\n if direction and direction != 'asc':\n args['direction'] = direction\n\n ret = {}\n milestones = _query(profile, action=action, command='milestones', args=args)\n\n for milestone in milestones:\n milestone_id = milestone.get('id')\n if output == 'full':\n ret[milestone_id] = milestone\n else:\n milestone.pop('creator')\n milestone.pop('html_url')\n milestone.pop('labels_url')\n ret[milestone_id] = milestone\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns information about a single milestone in a named repository.", "response": "def get_milestone(number=None,\n name=None,\n repo_name=None,\n profile='github',\n output='min'):\n '''\n Return information about a single milestone in a named repository.\n\n .. versionadded:: 2016.11.0\n\n number\n The number of the milestone to retrieve. If provided, this option\n will be favored over ``name``.\n\n name\n The name of the milestone to retrieve.\n\n repo_name\n The name of the repository for which to list issues. This argument is\n required, either passed via the CLI, or defined in the configured\n profile. A ``repo_name`` passed as a CLI argument will override the\n repo_name defined in the configured profile, if provided.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n output\n The amount of data returned by each issue. Defaults to ``min``. Change\n to ``full`` to see all issue output.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.get_milestone 72\n salt myminion github.get_milestone name=my_milestone\n\n '''\n ret = {}\n\n if not any([number, name]):\n raise CommandExecutionError(\n 'Either a milestone \\'name\\' or \\'number\\' must be provided.'\n )\n\n org_name = _get_config_value(profile, 'org_name')\n if repo_name is None:\n repo_name = _get_config_value(profile, 'repo_name')\n\n action = '/'.join(['repos', org_name, repo_name])\n if number:\n command = 'milestones/' + six.text_type(number)\n milestone_data = _query(profile, action=action, command=command)\n milestone_id = milestone_data.get('id')\n if output == 'full':\n ret[milestone_id] = milestone_data\n else:\n milestone_data.pop('creator')\n milestone_data.pop('html_url')\n milestone_data.pop('labels_url')\n ret[milestone_id] = milestone_data\n return ret\n\n else:\n milestones = get_milestones(repo_name=repo_name, profile=profile, output=output)\n for key, val in six.iteritems(milestones):\n if val.get('title') == name:\n ret[key] = val\n return ret\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns information for a given repository.", "response": "def get_repo_info(repo_name, profile='github', ignore_cache=False):\n '''\n Return information for a given repo.\n\n .. versionadded:: 2016.11.0\n\n repo_name\n The name of the repository.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.get_repo_info salt\n salt myminion github.get_repo_info salt profile='my-github-profile'\n '''\n\n org_name = _get_config_value(profile, 'org_name')\n key = \"github.{0}:{1}:repo_info\".format(\n _get_config_value(profile, 'org_name'),\n repo_name.lower()\n )\n\n if key not in __context__ or ignore_cache:\n client = _get_client(profile)\n try:\n repo = client.get_repo('/'.join([org_name, repo_name]))\n if not repo:\n return {}\n\n # client.get_repo can return a github.Repository.Repository object,\n # even if the repo is invalid. We need to catch the exception when\n # we try to perform actions on the repo object, rather than above\n # the if statement.\n ret = _repo_to_dict(repo)\n\n __context__[key] = ret\n except github.UnknownObjectException:\n raise CommandExecutionError(\n 'The \\'{0}\\' repository under the \\'{1}\\' organization could not '\n 'be found.'.format(\n repo_name,\n org_name\n )\n )\n return __context__[key]"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a list of teams belonging to a repository.", "response": "def get_repo_teams(repo_name, profile='github'):\n '''\n Return teams belonging to a repository.\n\n .. versionadded:: 2017.7.0\n\n repo_name\n The name of the repository from which to retrieve teams.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.get_repo_teams salt\n salt myminion github.get_repo_teams salt profile='my-github-profile'\n '''\n ret = []\n org_name = _get_config_value(profile, 'org_name')\n client = _get_client(profile)\n\n try:\n repo = client.get_repo('/'.join([org_name, repo_name]))\n except github.UnknownObjectException:\n raise CommandExecutionError(\n 'The \\'{0}\\' repository under the \\'{1}\\' organization could not '\n 'be found.'.format(repo_name, org_name)\n )\n try:\n teams = repo.get_teams()\n for team in teams:\n ret.append({\n 'id': team.id,\n 'name': team.name,\n 'permission': team.permission\n })\n except github.UnknownObjectException:\n raise CommandExecutionError(\n 'Unable to retrieve teams for repository \\'{0}\\' under the \\'{1}\\' '\n 'organization.'.format(repo_name, org_name)\n )\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_private_repos(profile='github'):\n '''\n List private repositories within the organization. Dependent upon the access\n rights of the profile token.\n\n .. versionadded:: 2016.11.0\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.list_private_repos\n salt myminion github.list_private_repos profile='my-github-profile'\n '''\n repos = []\n for repo in _get_repos(profile):\n if repo.private is True:\n repos.append(repo.name)\n return repos", "response": "List private repositories within the organization."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlisting public repositories within the organization.", "response": "def list_public_repos(profile='github'):\n '''\n List public repositories within the organization.\n\n .. versionadded:: 2016.11.0\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.list_public_repos\n salt myminion github.list_public_repos profile='my-github-profile'\n '''\n repos = []\n for repo in _get_repos(profile):\n if repo.private is False:\n repos.append(repo.name)\n return repos"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef add_repo(name,\n description=None,\n homepage=None,\n private=None,\n has_issues=None,\n has_wiki=None,\n has_downloads=None,\n auto_init=None,\n gitignore_template=None,\n license_template=None,\n profile=\"github\"):\n '''\n Create a new github repository.\n\n name\n The name of the team to be created.\n\n description\n The description of the repository.\n\n homepage\n The URL with more information about the repository.\n\n private\n The visiblity of the repository. Note that private repositories require\n a paid GitHub account.\n\n has_issues\n Whether to enable issues for this repository.\n\n has_wiki\n Whether to enable the wiki for this repository.\n\n has_downloads\n Whether to enable downloads for this repository.\n\n auto_init\n Whether to create an initial commit with an empty README.\n\n gitignore_template\n The desired language or platform for a .gitignore, e.g \"Haskell\".\n\n license_template\n The desired LICENSE template to apply, e.g \"mit\" or \"mozilla\".\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.add_repo 'repo_name'\n\n .. versionadded:: 2016.11.0\n '''\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n given_params = {\n 'description': description,\n 'homepage': homepage,\n 'private': private,\n 'has_issues': has_issues,\n 'has_wiki': has_wiki,\n 'has_downloads': has_downloads,\n 'auto_init': auto_init,\n 'gitignore_template': gitignore_template,\n 'license_template': license_template\n }\n parameters = {'name': name}\n for param_name, param_value in six.iteritems(given_params):\n if param_value is not None:\n parameters[param_name] = param_value\n\n organization._requester.requestJsonAndCheck(\n \"POST\",\n organization.url + \"/repos\",\n input=parameters\n )\n return True\n except github.GithubException:\n log.exception('Error creating a repo')\n return False", "response": "Creates a new github repository."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nedits an existing Github repository.", "response": "def edit_repo(name,\n description=None,\n homepage=None,\n private=None,\n has_issues=None,\n has_wiki=None,\n has_downloads=None,\n profile=\"github\"):\n '''\n Updates an existing Github repository.\n\n name\n The name of the team to be created.\n\n description\n The description of the repository.\n\n homepage\n The URL with more information about the repository.\n\n private\n The visiblity of the repository. Note that private repositories require\n a paid GitHub account.\n\n has_issues\n Whether to enable issues for this repository.\n\n has_wiki\n Whether to enable the wiki for this repository.\n\n has_downloads\n Whether to enable downloads for this repository.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.add_repo 'repo_name'\n\n .. versionadded:: 2016.11.0\n '''\n\n try:\n allow_private_change = _get_config_value(profile, 'allow_repo_privacy_changes')\n except CommandExecutionError:\n allow_private_change = False\n\n if private is not None and not allow_private_change:\n raise CommandExecutionError(\"The private field is set to be changed for \"\n \"repo {0} but allow_repo_privacy_changes \"\n \"disallows this.\".format(name))\n\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n repo = organization.get_repo(name)\n\n given_params = {\n 'description': description,\n 'homepage': homepage,\n 'private': private,\n 'has_issues': has_issues,\n 'has_wiki': has_wiki,\n 'has_downloads': has_downloads\n }\n parameters = {'name': name}\n for param_name, param_value in six.iteritems(given_params):\n if param_value is not None:\n parameters[param_name] = param_value\n\n organization._requester.requestJsonAndCheck(\n \"PATCH\",\n repo.url,\n input=parameters\n )\n get_repo_info(name, profile=profile, ignore_cache=True) # Refresh cache\n return True\n except github.GithubException:\n log.exception('Error editing a repo')\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nremoves a Github repository.", "response": "def remove_repo(name, profile=\"github\"):\n '''\n Remove a Github repository.\n\n name\n The name of the repository to be removed.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.remove_repo 'my-repo'\n\n .. versionadded:: 2016.11.0\n '''\n repo_info = get_repo_info(name, profile=profile)\n if not repo_info:\n log.error('Repo %s to be removed does not exist.', name)\n return False\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n repo = organization.get_repo(name)\n repo.delete()\n _get_repos(profile=profile, ignore_cache=True) # refresh cache\n return True\n except github.GithubException:\n log.exception('Error deleting a repo')\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef add_team(name,\n description=None,\n repo_names=None,\n privacy=None,\n permission=None,\n profile=\"github\"):\n '''\n Create a new Github team within an organization.\n\n name\n The name of the team to be created.\n\n description\n The description of the team.\n\n repo_names\n The names of repositories to add the team to.\n\n privacy\n The level of privacy for the team, can be 'secret' or 'closed'.\n\n permission\n The default permission for new repositories added to the team, can be\n 'pull', 'push' or 'admin'.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.add_team 'team_name'\n\n .. versionadded:: 2016.11.0\n '''\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n parameters = {}\n parameters['name'] = name\n\n if description is not None:\n parameters['description'] = description\n if repo_names is not None:\n parameters['repo_names'] = repo_names\n if permission is not None:\n parameters['permission'] = permission\n if privacy is not None:\n parameters['privacy'] = privacy\n\n organization._requester.requestJsonAndCheck(\n 'POST',\n organization.url + '/teams',\n input=parameters\n )\n list_teams(ignore_cache=True) # Refresh cache\n return True\n except github.GithubException:\n log.exception('Error creating a team')\n return False", "response": "Creates a new Github team within an organization."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nedits an existing Github team.", "response": "def edit_team(name,\n description=None,\n privacy=None,\n permission=None,\n profile=\"github\"):\n '''\n Updates an existing Github team.\n\n name\n The name of the team to be edited.\n\n description\n The description of the team.\n\n privacy\n The level of privacy for the team, can be 'secret' or 'closed'.\n\n permission\n The default permission for new repositories added to the team, can be\n 'pull', 'push' or 'admin'.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.edit_team 'team_name' description='Team description'\n\n .. versionadded:: 2016.11.0\n '''\n team = get_team(name, profile=profile)\n if not team:\n log.error('Team %s does not exist', name)\n return False\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n team = organization.get_team(team['id'])\n\n parameters = {}\n if name is not None:\n parameters['name'] = name\n if description is not None:\n parameters['description'] = description\n if privacy is not None:\n parameters['privacy'] = privacy\n if permission is not None:\n parameters['permission'] = permission\n\n team._requester.requestJsonAndCheck(\n \"PATCH\",\n team.url,\n input=parameters\n )\n return True\n except UnknownObjectException:\n log.exception('Resource not found')\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nremoves a github team.", "response": "def remove_team(name, profile=\"github\"):\n '''\n Remove a github team.\n\n name\n The name of the team to be removed.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.remove_team 'team_name'\n\n .. versionadded:: 2016.11.0\n '''\n team_info = get_team(name, profile=profile)\n if not team_info:\n log.error('Team %s to be removed does not exist.', name)\n return False\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n team = organization.get_team(team_info['id'])\n team.delete()\n return list_teams(ignore_cache=True, profile=profile).get(name) is None\n except github.GithubException:\n log.exception('Error deleting a team')\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the list of repos for a given team.", "response": "def list_team_repos(team_name, profile=\"github\", ignore_cache=False):\n '''\n Gets the repo details for a given team as a dict from repo_name to repo details.\n Note that repo names are always in lower case.\n\n team_name\n The name of the team from which to list repos.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n ignore_cache\n Bypasses the use of cached team repos.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.list_team_repos 'team_name'\n\n .. versionadded:: 2016.11.0\n '''\n cached_team = get_team(team_name, profile=profile)\n if not cached_team:\n log.error('Team %s does not exist.', team_name)\n return False\n\n # Return from cache if available\n if cached_team.get('repos') and not ignore_cache:\n return cached_team.get('repos')\n\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n team = organization.get_team(cached_team['id'])\n except UnknownObjectException:\n log.exception('Resource not found: %s', cached_team['id'])\n try:\n repos = {}\n for repo in team.get_repos():\n permission = 'pull'\n if repo.permissions.admin:\n permission = 'admin'\n elif repo.permissions.push:\n permission = 'push'\n\n repos[repo.name.lower()] = {\n 'permission': permission\n }\n cached_team['repos'] = repos\n return repos\n except UnknownObjectException:\n log.exception('Resource not found: %s', cached_team['id'])\n return []"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding a repository to a team with team_name.", "response": "def add_team_repo(repo_name, team_name, profile=\"github\", permission=None):\n '''\n Adds a repository to a team with team_name.\n\n repo_name\n The name of the repository to add.\n\n team_name\n The name of the team of which to add the repository.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n permission\n The permission for team members within the repository, can be 'pull',\n 'push' or 'admin'. If not specified, the default permission specified on\n the team will be used.\n\n .. versionadded:: 2017.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.add_team_repo 'my_repo' 'team_name'\n\n .. versionadded:: 2016.11.0\n '''\n team = get_team(team_name, profile=profile)\n if not team:\n log.error('Team %s does not exist', team_name)\n return False\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n team = organization.get_team(team['id'])\n repo = organization.get_repo(repo_name)\n except UnknownObjectException:\n log.exception('Resource not found: %s', team['id'])\n return False\n params = None\n if permission is not None:\n params = {'permission': permission}\n\n headers, data = team._requester.requestJsonAndCheck(\n \"PUT\",\n team.url + \"/repos/\" + repo._identity,\n input=params\n )\n # Try to refresh cache\n list_team_repos(team_name, profile=profile, ignore_cache=True)\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef remove_team_repo(repo_name, team_name, profile=\"github\"):\n '''\n Removes a repository from a team with team_name.\n\n repo_name\n The name of the repository to remove.\n\n team_name\n The name of the team of which to remove the repository.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.remove_team_repo 'my_repo' 'team_name'\n\n .. versionadded:: 2016.11.0\n '''\n team = get_team(team_name, profile=profile)\n if not team:\n log.error('Team %s does not exist', team_name)\n return False\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n team = organization.get_team(team['id'])\n repo = organization.get_repo(repo_name)\n except UnknownObjectException:\n log.exception('Resource not found: %s', team['id'])\n return False\n team.remove_from_repos(repo)\n return repo_name not in list_team_repos(team_name, profile=profile, ignore_cache=True)", "response": "Removes a repository from a team with team_name."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_team_members(team_name, profile=\"github\", ignore_cache=False):\n '''\n Gets the names of team members in lower case.\n\n team_name\n The name of the team from which to list members.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n ignore_cache\n Bypasses the use of cached team members.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.list_team_members 'team_name'\n\n .. versionadded:: 2016.11.0\n '''\n cached_team = get_team(team_name, profile=profile)\n if not cached_team:\n log.error('Team %s does not exist.', team_name)\n return False\n # Return from cache if available\n if cached_team.get('members') and not ignore_cache:\n return cached_team.get('members')\n\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n team = organization.get_team(cached_team['id'])\n except UnknownObjectException:\n log.exception('Resource not found: %s', cached_team['id'])\n try:\n cached_team['members'] = [member.login.lower()\n for member in team.get_members()]\n return cached_team['members']\n except UnknownObjectException:\n log.exception('Resource not found: %s', cached_team['id'])\n return []", "response": "Returns a list of team members in lower case."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_members_without_mfa(profile=\"github\", ignore_cache=False):\n '''\n List all members (in lower case) without MFA turned on.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n ignore_cache\n Bypasses the use of cached team repos.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.list_members_without_mfa\n\n .. versionadded:: 2016.11.0\n '''\n key = \"github.{0}:non_mfa_users\".format(\n _get_config_value(profile, 'org_name')\n )\n\n if key not in __context__ or ignore_cache:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n\n filter_key = 'filter'\n # Silly hack to see if we're past PyGithub 1.26.0, where the name of\n # the filter kwarg changed\n if hasattr(github.Team.Team, 'membership'):\n filter_key = 'filter_'\n\n __context__[key] = [m.login.lower() for m in\n _get_members(organization, {filter_key: '2fa_disabled'})]\n return __context__[key]", "response": "Return a list of members that are not MFA enabled."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef is_team_member(name, team_name, profile=\"github\"):\n '''\n Returns True if the github user is in the team with team_name, or False\n otherwise.\n\n name\n The name of the user whose membership to check.\n\n team_name\n The name of the team to check membership in.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.is_team_member 'user_name' 'team_name'\n\n .. versionadded:: 2016.11.0\n '''\n return name.lower() in list_team_members(team_name, profile=profile)", "response": "Returns True if the github user is in the team with team_name False otherwise."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nadd a team member to a team with team_name.", "response": "def add_team_member(name, team_name, profile=\"github\"):\n '''\n Adds a team member to a team with team_name.\n\n name\n The name of the team member to add.\n\n team_name\n The name of the team of which to add the user.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.add_team_member 'user_name' 'team_name'\n\n .. versionadded:: 2016.11.0\n '''\n team = get_team(team_name, profile=profile)\n if not team:\n log.error('Team %s does not exist', team_name)\n return False\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n team = organization.get_team(team['id'])\n member = client.get_user(name)\n except UnknownObjectException:\n log.exception('Resource not found: %s', team['id'])\n return False\n\n try:\n # Can't use team.add_membership due to this bug that hasn't made it into\n # a PyGithub release yet https://github.com/PyGithub/PyGithub/issues/363\n headers, data = team._requester.requestJsonAndCheck(\n \"PUT\",\n team.url + \"/memberships/\" + member._identity,\n input={'role': 'member'},\n parameters={'role': 'member'}\n )\n except github.GithubException:\n log.exception('Error in adding a member to a team')\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef remove_team_member(name, team_name, profile=\"github\"):\n '''\n Removes a team member from a team with team_name.\n\n name\n The name of the team member to remove.\n\n team_name\n The name of the team from which to remove the user.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.remove_team_member 'user_name' 'team_name'\n\n .. versionadded:: 2016.11.0\n '''\n team = get_team(team_name, profile=profile)\n if not team:\n log.error('Team %s does not exist', team_name)\n return False\n try:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n team = organization.get_team(team['id'])\n member = client.get_user(name)\n\n except UnknownObjectException:\n log.exception('Resource not found: %s', team['id'])\n return False\n\n if not hasattr(team, 'remove_from_members'):\n return (False, 'PyGithub 1.26.0 or greater is required for team '\n 'management, please upgrade.')\n\n team.remove_from_members(member)\n return not team.has_in_members(member)", "response": "Removes a team member from a team with team_name."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists all teams with the organization.", "response": "def list_teams(profile=\"github\", ignore_cache=False):\n '''\n Lists all teams with the organization.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n ignore_cache\n Bypasses the use of cached teams.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.list_teams\n\n .. versionadded:: 2016.11.0\n '''\n key = 'github.{0}:teams'.format(\n _get_config_value(profile, 'org_name')\n )\n\n if key not in __context__ or ignore_cache:\n client = _get_client(profile)\n organization = client.get_organization(\n _get_config_value(profile, 'org_name')\n )\n teams_data = organization.get_teams()\n teams = {}\n for team in teams_data:\n # Note that _rawData is used to access some properties here as they\n # are not exposed in older versions of PyGithub. It's VERY important\n # to use team._rawData instead of team.raw_data, as the latter forces\n # an API call to retrieve team details again.\n teams[team.name] = {\n 'id': team.id,\n 'slug': team.slug,\n 'description': team._rawData['description'],\n 'permission': team.permission,\n 'privacy': team._rawData['privacy']\n }\n __context__[key] = teams\n\n return __context__[key]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_prs(repo_name=None,\n profile='github',\n state='open',\n head=None,\n base=None,\n sort='created',\n direction='desc',\n output='min',\n per_page=None):\n '''\n Returns information for all pull requests in a given repository, based on\n the search options provided.\n\n .. versionadded:: 2017.7.0\n\n repo_name\n The name of the repository for which to list pull requests. This\n argument is required, either passed via the CLI, or defined in the\n configured profile. A ``repo_name`` passed as a CLI argument will\n override the ``repo_name`` defined in the configured profile, if\n provided.\n\n profile\n The name of the profile configuration to use. Defaults to ``github``.\n\n state\n Indicates the state of the pull requests to return. Can be either\n ``open``, ``closed``, or ``all``. Default is ``open``.\n\n head\n Filter pull requests by head user and branch name in the format of\n ``user:ref-name``. Example: ``'github:new-script-format'``. Default\n is ``None``.\n\n base\n Filter pulls by base branch name. Example: ``gh-pages``. Default is\n ``None``.\n\n sort\n What to sort results by. Can be either ``created``, ``updated``,\n ``popularity`` (comment count), or ``long-running`` (age, filtering\n by pull requests updated within the last month). Default is ``created``.\n\n direction\n The direction of the sort. Can be either ``asc`` or ``desc``. Default\n is ``desc``.\n\n output\n The amount of data returned by each pull request. Defaults to ``min``.\n Change to ``full`` to see all pull request output.\n\n per_page\n GitHub paginates data in their API calls. Use this value to increase or\n decrease the number of pull requests gathered from GitHub, per page. If\n not set, GitHub defaults are used. Maximum is 100.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion github.get_prs\n salt myminion github.get_prs base=2016.11\n '''\n org_name = _get_config_value(profile, 'org_name')\n if repo_name is None:\n repo_name = _get_config_value(profile, 'repo_name')\n\n action = '/'.join(['repos', org_name, repo_name])\n args = {}\n\n # Build API arguments, as necessary.\n if head:\n args['head'] = head\n if base:\n args['base'] = base\n if per_page:\n args['per_page'] = per_page\n\n # Only pass the following API args if they're not the defaults listed.\n if state and state != 'open':\n args['state'] = state\n if sort and sort != 'created':\n args['sort'] = sort\n if direction and direction != 'desc':\n args['direction'] = direction\n\n ret = {}\n prs = _query(profile, action=action, command='pulls', args=args)\n\n for pr_ in prs:\n pr_id = pr_.get('id')\n if output == 'full':\n ret[pr_id] = pr_\n else:\n ret[pr_id] = _format_pr(pr_)\n\n return ret", "response": "Return a list of pull requests in a given repository."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _format_pr(pr_):\n '''\n Helper function to format API return information into a more manageable\n and useful dictionary for pull request information.\n\n pr_\n The pull request to format.\n '''\n ret = {'id': pr_.get('id'),\n 'pr_number': pr_.get('number'),\n 'state': pr_.get('state'),\n 'title': pr_.get('title'),\n 'user': pr_.get('user').get('login'),\n 'html_url': pr_.get('html_url'),\n 'base_branch': pr_.get('base').get('ref')}\n\n return ret", "response": "Helper function to format pull request information into a more manageable\n and useful dictionary for pull request information."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _format_issue(issue):\n '''\n Helper function to format API return information into a more manageable\n and useful dictionary for issue information.\n\n issue\n The issue to format.\n '''\n ret = {'id': issue.get('id'),\n 'issue_number': issue.get('number'),\n 'state': issue.get('state'),\n 'title': issue.get('title'),\n 'user': issue.get('user').get('login'),\n 'html_url': issue.get('html_url')}\n\n assignee = issue.get('assignee')\n if assignee:\n assignee = assignee.get('login')\n\n labels = issue.get('labels')\n label_names = []\n for label in labels:\n label_names.append(label.get('name'))\n\n milestone = issue.get('milestone')\n if milestone:\n milestone = milestone.get('title')\n\n ret['assignee'] = assignee\n ret['labels'] = label_names\n ret['milestone'] = milestone\n\n return ret", "response": "Helper function to format an issue into a more manageable\n and useful dictionary for issue information."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _query(profile,\n action=None,\n command=None,\n args=None,\n method='GET',\n header_dict=None,\n data=None,\n url='https://api.github.com/',\n per_page=None):\n '''\n Make a web call to the GitHub API and deal with paginated results.\n '''\n if not isinstance(args, dict):\n args = {}\n\n if action:\n url += action\n\n if command:\n url += '/{0}'.format(command)\n\n log.debug('GitHub URL: %s', url)\n\n if 'access_token' not in args.keys():\n args['access_token'] = _get_config_value(profile, 'token')\n if per_page and 'per_page' not in args.keys():\n args['per_page'] = per_page\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 # GitHub paginates all queries when returning many items.\n # Gather all data using multiple queries and handle pagination.\n complete_result = []\n next_page = True\n page_number = ''\n while next_page is True:\n if page_number:\n args['page'] = page_number\n result = salt.utils.http.query(url,\n method,\n params=args,\n data=data,\n header_dict=header_dict,\n decode=decode,\n decode_type='json',\n headers=True,\n status=True,\n text=True,\n hide_fields=['access_token'],\n opts=__opts__,\n )\n log.debug('GitHub Response Status Code: %s',\n result['status'])\n\n if result['status'] == 200:\n if isinstance(result['dict'], dict):\n # If only querying for one item, such as a single issue\n # The GitHub API returns a single dictionary, instead of\n # A list of dictionaries. In that case, we can return.\n return result['dict']\n\n complete_result = complete_result + result['dict']\n else:\n raise CommandExecutionError(\n 'GitHub Response Error: {0}'.format(result.get('error'))\n )\n\n try:\n link_info = result.get('headers').get('Link').split(',')[0]\n except AttributeError:\n # Only one page of data was returned; exit the loop.\n next_page = False\n continue\n\n if 'next' in link_info:\n # Get the 'next' page number from the Link header.\n page_number = link_info.split('>')[0].split('&page=')[1]\n else:\n # Last page already processed; break the loop.\n next_page = False\n\n return complete_result", "response": "Query the GitHub API."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the last created baseline snapshot marked with tag", "response": "def _get_baseline_from_tag(config, tag):\n '''\n Returns the last created baseline snapshot marked with `tag`\n '''\n last_snapshot = None\n for snapshot in __salt__['snapper.list_snapshots'](config):\n if tag == snapshot['userdata'].get(\"baseline_tag\"):\n if not last_snapshot or last_snapshot['timestamp'] < snapshot['timestamp']:\n last_snapshot = snapshot\n return last_snapshot"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a new base language tree that is used to compare a previously - defined baseline snapshot.", "response": "def baseline_snapshot(name, number=None, tag=None, include_diff=True, config='root', ignore=None):\n '''\n Enforces that no file is modified comparing against a previously\n defined snapshot identified by number.\n\n number\n Number of selected baseline snapshot.\n\n tag\n Tag of the selected baseline snapshot. Most recent baseline baseline\n snapshot is used in case of multiple snapshots with the same tag.\n (`tag` and `number` cannot be used at the same time)\n\n include_diff\n Include a diff in the response (Default: True)\n\n config\n Snapper config name (Default: root)\n\n ignore\n List of files to ignore. (Default: None)\n '''\n if not ignore:\n ignore = []\n\n ret = {'changes': {},\n 'comment': '',\n 'name': name,\n 'result': True}\n\n if number is None and tag is None:\n ret.update({'result': False,\n 'comment': 'Snapshot tag or number must be specified'})\n return ret\n\n if number and tag:\n ret.update({'result': False,\n 'comment': 'Cannot use snapshot tag and number at the same time'})\n return ret\n\n if tag:\n snapshot = _get_baseline_from_tag(config, tag)\n if not snapshot:\n ret.update({'result': False,\n 'comment': 'Baseline tag \"{0}\" not found'.format(tag)})\n return ret\n number = snapshot['id']\n\n status = __salt__['snapper.status'](\n config, num_pre=0, num_post=number)\n\n for target in ignore:\n if os.path.isfile(target):\n status.pop(target, None)\n elif os.path.isdir(target):\n for target_file in [target_file for target_file in status.keys() if target_file.startswith(target)]:\n status.pop(target_file, None)\n\n for file in status:\n # Only include diff for modified files\n if \"modified\" in status[file][\"status\"] and include_diff:\n status[file].pop(\"status\")\n status[file].update(__salt__['snapper.diff'](config,\n num_pre=0,\n num_post=number,\n filename=file).get(file, {}))\n\n if __opts__['test'] and status:\n ret['changes'] = status\n ret['comment'] = \"{0} files changes are set to be undone\".format(len(status.keys()))\n ret['result'] = None\n elif __opts__['test'] and not status:\n ret['changes'] = {}\n ret['comment'] = \"Nothing to be done\"\n ret['result'] = True\n elif not __opts__['test'] and status:\n undo = __salt__['snapper.undo'](config, num_pre=number, num_post=0,\n files=status.keys())\n ret['changes']['sumary'] = undo\n ret['changes']['files'] = status\n ret['result'] = True\n else:\n ret['comment'] = \"No changes were done\"\n ret['result'] = True\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nverifying that the command was successful.", "response": "def _verify_run(out, cmd=None):\n '''\n Crash to the log if command execution was not successful.\n '''\n if out.get('retcode', 0) and out['stderr']:\n if cmd:\n log.debug('Command: \\'%s\\'', cmd)\n\n log.debug('Return code: %s', out.get('retcode'))\n log.debug('Error output:\\n%s', out.get('stderr', 'N/A'))\n\n raise CommandExecutionError(out['stderr'])"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of mounted filesystems.", "response": "def _get_mounts(fs_type=None):\n '''\n List mounted filesystems.\n '''\n mounts = {}\n with salt.utils.files.fopen('/proc/mounts') as fhr:\n for line in fhr.readlines():\n line = salt.utils.stringutils.to_unicode(line)\n device, mntpnt, fstype, options, fs_freq, fs_passno = line.strip().split(\" \")\n if fs_type and fstype != fs_type:\n continue\n if mounts.get(device) is None:\n mounts[device] = []\n\n data = {\n 'mount_point': mntpnt,\n 'options': options.split(\",\")\n }\n if not fs_type:\n data['type'] = fstype\n mounts[device].append(data)\n return mounts"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _blkid(fs_type=None):\n '''\n Return available media devices.\n\n :param fs_type: Filter only devices that are formatted by that file system.\n '''\n flt = lambda data: [el for el in data if el.strip()]\n data = dict()\n for dev_meta in flt(os.popen(\"blkid -o full\").read().split(os.linesep)): # No __salt__ around at this point.\n dev_meta = dev_meta.strip()\n if not dev_meta:\n continue\n device = dev_meta.split(\" \")\n dev_name = device.pop(0)[:-1]\n data[dev_name] = dict()\n for k_set in device:\n ks_key, ks_value = [elm.replace('\"', '') for elm in k_set.split(\"=\")]\n data[dev_name][ks_key.lower()] = ks_value\n\n if fs_type:\n mounts = _get_mounts(fs_type)\n for device in six.iterkeys(mounts):\n if data.get(device):\n data[device]['mounts'] = mounts[device]\n\n return data", "response": "Return available media devices."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _is_device(path):\n '''\n Return True if path is a physical device.\n '''\n out = __salt__['cmd.run_all']('file -i {0}'.format(path))\n _verify_run(out)\n\n # Always [device, mime, charset]. See (file --help)\n return re.split(r'\\s+', out['stdout'])[1][:-1] == 'inode/blockdevice'", "response": "Return True if path is a physical device."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nsend a mattermost message with the data", "response": "def returner(ret):\n '''\n Send an mattermost message with the data\n '''\n\n _options = _get_options(ret)\n\n api_url = _options.get('api_url')\n channel = _options.get('channel')\n username = _options.get('username')\n hook = _options.get('hook')\n\n if not hook:\n log.error('mattermost.hook not defined in salt config')\n return\n\n returns = ret.get('return')\n\n message = ('id: {0}\\r\\n'\n 'function: {1}\\r\\n'\n 'function args: {2}\\r\\n'\n 'jid: {3}\\r\\n'\n 'return: {4}\\r\\n').format(\n ret.get('id'),\n ret.get('fun'),\n ret.get('fun_args'),\n ret.get('jid'),\n returns)\n\n mattermost = post_message(channel,\n message,\n username,\n api_url,\n hook)\n return mattermost"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef event_return(events):\n '''\n Send the events to a mattermost room.\n\n :param events: List of events\n :return: Boolean if messages were sent successfully.\n '''\n _options = _get_options()\n\n api_url = _options.get('api_url')\n channel = _options.get('channel')\n username = _options.get('username')\n hook = _options.get('hook')\n\n is_ok = True\n for event in events:\n log.debug('Event: %s', event)\n log.debug('Event data: %s', event['data'])\n message = 'tag: {0}\\r\\n'.format(event['tag'])\n for key, value in six.iteritems(event['data']):\n message += '{0}: {1}\\r\\n'.format(key, value)\n result = post_message(channel,\n message,\n username,\n api_url,\n hook)\n if not result:\n is_ok = False\n\n return is_ok", "response": "Send the events to a mattermost room."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef post_message(channel,\n message,\n username,\n api_url,\n hook):\n '''\n Send a message to a mattermost room.\n\n :param channel: The room name.\n :param message: The message to send to the mattermost room.\n :param username: Specify who the message is from.\n :param hook: The mattermost hook, if not specified in the configuration.\n :return: Boolean if message was sent successfully.\n '''\n\n parameters = dict()\n if channel:\n parameters['channel'] = channel\n if username:\n parameters['username'] = username\n parameters['text'] = '```' + message + '```' # pre-formatted, fixed-width text\n log.debug('Parameters: %s', parameters)\n result = salt.utils.mattermost.query(\n api_url=api_url,\n hook=hook,\n data=str('payload={0}').format(salt.utils.json.dumps(parameters))) # future lint: disable=blacklisted-function\n\n log.debug('result %s', result)\n return bool(result)", "response": "Send a message to a mattermost room."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates a new node in Zookeeper", "response": "def create(path, value='', acls=None, ephemeral=False, sequence=False, makepath=False, profile=None,\n hosts=None, scheme=None, username=None, password=None, default_acl=None):\n '''\n Create Znode\n\n path\n path of znode to create\n\n value\n value to assign to znode (Default: '')\n\n acls\n list of acl dictionaries to be assigned (Default: None)\n\n ephemeral\n indicate node is ephemeral (Default: False)\n\n sequence\n indicate node is suffixed with a unique index (Default: False)\n\n makepath\n Create parent paths if they do not exist (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 CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.create /test/name daniel profile=prod\n\n '''\n if acls is None:\n acls = []\n acls = [make_digest_acl(**acl) for acl in acls]\n conn = _get_zk_conn(profile=profile, hosts=hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n return conn.create(path, salt.utils.stringutils.to_bytes(value), acls, ephemeral, sequence, makepath)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure a Znode path exists", "response": "def ensure_path(path, acls=None, profile=None, hosts=None, scheme=None,\n username=None, password=None, default_acl=None):\n '''\n Ensure Znode path exists\n\n path\n Parent path to create\n\n acls\n list of acls dictionaries to be assigned (Default: None)\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 CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.ensure_path /test/name profile=prod\n\n '''\n if acls is None:\n acls = []\n acls = [make_digest_acl(**acl) for acl in acls]\n conn = _get_zk_conn(profile=profile, hosts=hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n return conn.ensure_path(path, acls)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef exists(path, profile=None, hosts=None, scheme=None, username=None, password=None, default_acl=None):\n '''\n Check if path exists\n\n path\n path to check\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 CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.exists /test/name profile=prod\n\n '''\n conn = _get_zk_conn(profile=profile, hosts=hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n return bool(conn.exists(path))", "response": "Check if a path exists in Zookeeper"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get(path, profile=None, hosts=None, scheme=None, username=None, password=None, default_acl=None):\n '''\n Get value saved in znode\n\n path\n path to check\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 CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.get /test/name profile=prod\n\n '''\n conn = _get_zk_conn(profile=profile, hosts=hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n ret, _ = conn.get(path)\n return salt.utils.stringutils.to_str(ret)", "response": "Get value saved in znode\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget children of a znode", "response": "def get_children(path, profile=None, hosts=None, scheme=None, username=None, password=None, default_acl=None):\n '''\n Get children in znode path\n\n path\n path to check\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 CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.get_children /test profile=prod\n\n '''\n conn = _get_zk_conn(profile=profile, hosts=hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n ret = conn.get_children(path)\n return ret or []"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef set(path, value, version=-1, profile=None, hosts=None, scheme=None,\n username=None, password=None, default_acl=None):\n '''\n Update znode with new value\n\n path\n znode to update\n\n value\n value to set in znode\n\n version\n only update znode if version matches (Default: -1 (always matches))\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 CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.set /test/name gtmanfred profile=prod\n\n '''\n conn = _get_zk_conn(profile=profile, hosts=hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n return conn.set(path, salt.utils.stringutils.to_bytes(value), version=version)", "response": "Set the value of a znode in a Zookeeper node"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget acls on a znode", "response": "def get_acls(path, profile=None, hosts=None, scheme=None, username=None, password=None, default_acl=None):\n '''\n Get acls on a znode\n\n path\n path to znode\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 CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.get_acls /test/name profile=prod\n\n '''\n conn = _get_zk_conn(profile=profile, hosts=hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n return conn.get_acls(path)[0]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_acls(path, acls, version=-1, profile=None, hosts=None, scheme=None,\n username=None, password=None, default_acl=None):\n '''\n Set acls on a znode\n\n path\n path to znode\n\n acls\n list of acl dictionaries to set on the znode\n\n version\n only set acls if version matches (Default: -1 (always matches))\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 CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.set_acls /test/name acls='[{\"username\": \"gtmanfred\", \"password\": \"test\", \"all\": True}]' profile=prod\n\n '''\n conn = _get_zk_conn(profile=profile, hosts=hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n if acls is None:\n acls = []\n acls = [make_digest_acl(**acl) for acl in acls]\n conn = _get_zk_conn(profile=profile, hosts=hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n return conn.set_acls(path, acls, version)", "response": "Set acls on a znode"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef delete(path, version=-1, recursive=False, profile=None, hosts=None, scheme=None,\n username=None, password=None, default_acl=None):\n '''\n Delete znode\n\n path\n path to znode\n\n version\n only delete if version matches (Default: -1 (always matches))\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 CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.delete /test/name profile=prod\n\n '''\n conn = _get_zk_conn(profile=profile, hosts=hosts, scheme=scheme,\n username=username, password=password, default_acl=default_acl)\n return conn.delete(path, version, recursive)", "response": "Delete znode\n\n path\n path to znode\n\n version\n only delete if version matches (Default: -1 (always matches))\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 CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.delete /test/name profile=prod"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngenerating a new acl for a digest", "response": "def make_digest_acl(username, password, read=False, write=False, create=False, delete=False, admin=False,\n allperms=False):\n '''\n Generate acl object\n\n .. note:: This is heavily used in the zookeeper state and probably is not useful as a cli module\n\n username\n username of acl\n\n password\n plain text password of acl\n\n read\n read acl\n\n write\n write acl\n\n create\n create acl\n\n delete\n delete acl\n\n admin\n admin acl\n\n allperms\n set all other acls to True\n\n CLI Example:\n\n .. code-block:: bash\n\n salt minion1 zookeeper.make_digest_acl username=daniel password=mypass allperms=True\n '''\n return kazoo.security.make_digest_acl(username, password, read, write, create, delete, admin, allperms)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nquerying Mattermost API URL and return the json response.", "response": "def query(hook=None,\n api_url=None,\n data=None):\n '''\n Mattermost object method function to construct and execute on the API URL.\n :param api_url: The Mattermost API URL\n :param hook: The Mattermost hook.\n :param data: The data to be sent for POST method.\n :return: The json response from the API call or False.\n '''\n method = 'POST'\n\n ret = {'message': '',\n 'res': True}\n\n base_url = _urljoin(api_url, '/hooks/')\n url = _urljoin(base_url, six.text_type(hook))\n\n result = salt.utils.http.query(url,\n method,\n data=data,\n decode=True,\n status=True)\n\n if result.get('status', None) == salt.ext.six.moves.http_client.OK:\n ret['message'] = 'Message posted {0} correctly'.format(data)\n return ret\n elif result.get('status', None) == salt.ext.six.moves.http_client.NO_CONTENT:\n return True\n else:\n log.debug(url)\n log.debug(data)\n log.debug(result)\n if 'dict' in result:\n _result = result['dict']\n if 'error' in _result:\n ret['message'] = result['error']\n ret['res'] = False\n return ret\n ret['message'] = 'Message not posted'\n else:\n ret['message'] = 'invalid_auth'\n ret['res'] = False\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef ext_pillar(minion_id, pillar, profile=None):\n '''\n Read pillar data from Confidant via its API.\n '''\n if profile is None:\n profile = {}\n # default to returning failure\n ret = {\n 'credentials_result': False,\n 'credentials': None,\n 'credentials_metadata': None\n }\n profile_data = copy.deepcopy(profile)\n if profile_data.get('disabled', False):\n ret['result'] = True\n return ret\n token_version = profile_data.get('token_version', 1)\n try:\n url = profile_data['url']\n auth_key = profile_data['auth_key']\n auth_context = profile_data['auth_context']\n role = auth_context['from']\n except (KeyError, TypeError):\n msg = ('profile has undefined url, auth_key or auth_context')\n log.debug(msg)\n return ret\n region = profile_data.get('region', 'us-east-1')\n token_duration = profile_data.get('token_duration', 60)\n retries = profile_data.get('retries', 5)\n token_cache_file = profile_data.get('token_cache_file')\n backoff = profile_data.get('backoff', 1)\n client = confidant.client.ConfidantClient(\n url,\n auth_key,\n auth_context,\n token_lifetime=token_duration,\n token_version=token_version,\n token_cache_file=token_cache_file,\n region=region,\n retries=retries,\n backoff=backoff\n )\n try:\n data = client.get_service(\n role,\n decrypt_blind=True\n )\n except confidant.client.TokenCreationError:\n return ret\n if not data['result']:\n return ret\n ret = confidant.formatter.combined_credential_pair_format(data)\n ret['credentials_result'] = True\n return ret", "response": "Read pillar data from Confidant via API."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef setup_temp_logger(log_level='error'):\n '''\n Setup the temporary console logger\n '''\n if is_temp_logging_configured():\n logging.getLogger(__name__).warning(\n 'Temporary logging is already configured'\n )\n return\n\n if log_level is None:\n log_level = 'warning'\n\n level = LOG_LEVELS.get(log_level.lower(), logging.ERROR)\n\n handler = None\n for handler in logging.root.handlers:\n if handler in (LOGGING_NULL_HANDLER, LOGGING_STORE_HANDLER):\n continue\n\n if not hasattr(handler, 'stream'):\n # Not a stream handler, continue\n continue\n\n if handler.stream is sys.stderr:\n # There's already a logging handler outputting to sys.stderr\n break\n else:\n handler = LOGGING_TEMP_HANDLER\n handler.setLevel(level)\n\n # Set the default temporary console formatter config\n formatter = logging.Formatter(\n '[%(levelname)-8s] %(message)s', datefmt='%H:%M:%S'\n )\n handler.setFormatter(formatter)\n logging.root.addHandler(handler)\n\n # Sync the null logging handler messages with the temporary handler\n if LOGGING_NULL_HANDLER is not None:\n LOGGING_NULL_HANDLER.sync_with_handlers([handler])\n else:\n logging.getLogger(__name__).debug(\n 'LOGGING_NULL_HANDLER is already None, can\\'t sync messages '\n 'with it'\n )\n\n # Remove the temporary null logging handler\n __remove_null_logging_handler()\n\n global __TEMP_LOGGING_CONFIGURED\n __TEMP_LOGGING_CONFIGURED = True", "response": "Setup the temporary console logger."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef setup_console_logger(log_level='error', log_format=None, date_format=None):\n '''\n Setup the console logger\n '''\n if is_console_configured():\n logging.getLogger(__name__).warning('Console logging already configured')\n return\n\n # Remove the temporary logging handler\n __remove_temp_logging_handler()\n\n if log_level is None:\n log_level = 'warning'\n\n level = LOG_LEVELS.get(log_level.lower(), logging.ERROR)\n\n setLogRecordFactory(SaltColorLogRecord)\n\n handler = None\n for handler in logging.root.handlers:\n if handler is LOGGING_STORE_HANDLER:\n continue\n\n if not hasattr(handler, 'stream'):\n # Not a stream handler, continue\n continue\n\n if handler.stream is sys.stderr:\n # There's already a logging handler outputting to sys.stderr\n break\n else:\n handler = StreamHandler(sys.stderr)\n handler.setLevel(level)\n\n # Set the default console formatter config\n if not log_format:\n log_format = '[%(levelname)-8s] %(message)s'\n if not date_format:\n date_format = '%H:%M:%S'\n\n formatter = logging.Formatter(log_format, datefmt=date_format)\n\n handler.setFormatter(formatter)\n logging.root.addHandler(handler)\n\n global __CONSOLE_CONFIGURED\n global __LOGGING_CONSOLE_HANDLER\n __CONSOLE_CONFIGURED = True\n __LOGGING_CONSOLE_HANDLER = handler", "response": "Setup the console logger."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef setup_multiprocessing_logging(queue=None):\n '''\n This code should be called from within a running multiprocessing\n process instance.\n '''\n from salt.utils.platform import is_windows\n\n global __MP_LOGGING_CONFIGURED\n global __MP_LOGGING_QUEUE_HANDLER\n\n if __MP_IN_MAINPROCESS is True and not is_windows():\n # We're in the MainProcess, return! No multiprocessing logging setup shall happen\n # Windows is the exception where we want to set up multiprocessing\n # logging in the MainProcess.\n return\n\n try:\n logging._acquireLock() # pylint: disable=protected-access\n\n if __MP_LOGGING_CONFIGURED is True:\n return\n\n # Let's set it to true as fast as possible\n __MP_LOGGING_CONFIGURED = True\n\n if __MP_LOGGING_QUEUE_HANDLER is not None:\n return\n\n # The temp null and temp queue logging handlers will store messages.\n # Since noone will process them, memory usage will grow. If they\n # exist, remove them.\n __remove_null_logging_handler()\n __remove_queue_logging_handler()\n\n # Let's add a queue handler to the logging root handlers\n __MP_LOGGING_QUEUE_HANDLER = SaltLogQueueHandler(queue or get_multiprocessing_logging_queue())\n logging.root.addHandler(__MP_LOGGING_QUEUE_HANDLER)\n # Set the logging root level to the lowest needed level to get all\n # desired messages.\n log_level = get_multiprocessing_logging_level()\n logging.root.setLevel(log_level)\n logging.getLogger(__name__).debug(\n 'Multiprocessing queue logging configured for the process running '\n 'under PID: %s at log level %s', os.getpid(), log_level\n )\n # The above logging call will create, in some situations, a futex wait\n # lock condition, probably due to the multiprocessing Queue's internal\n # lock and semaphore mechanisms.\n # A small sleep will allow us not to hit that futex wait lock condition.\n time.sleep(0.0001)\n finally:\n logging._releaseLock()", "response": "This function is called by the multiprocessing process to setup the logging for the process running in the main process."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef set_logger_level(logger_name, log_level='error'):\n '''\n Tweak a specific logger's logging level\n '''\n logging.getLogger(logger_name).setLevel(\n LOG_LEVELS.get(log_level.lower(), logging.ERROR)\n )", "response": "Tweak a specific logger s logging level"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef patch_python_logging_handlers():\n '''\n Patch the python logging handlers with out mixed-in classes\n '''\n logging.StreamHandler = StreamHandler\n logging.FileHandler = FileHandler\n logging.handlers.SysLogHandler = SysLogHandler\n logging.handlers.WatchedFileHandler = WatchedFileHandler\n logging.handlers.RotatingFileHandler = RotatingFileHandler\n if sys.version_info >= (3, 2):\n logging.handlers.QueueHandler = QueueHandler", "response": "Patch the python logging handlers with out mixed - in classes\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __remove_null_logging_handler():\n '''\n This function will run once the temporary logging has been configured. It\n just removes the NullHandler from the logging handlers.\n '''\n global LOGGING_NULL_HANDLER\n if LOGGING_NULL_HANDLER is None:\n # Already removed\n return\n\n root_logger = logging.getLogger()\n\n for handler in root_logger.handlers:\n if handler is LOGGING_NULL_HANDLER:\n root_logger.removeHandler(LOGGING_NULL_HANDLER)\n # Redefine the null handler to None so it can be garbage collected\n LOGGING_NULL_HANDLER = None\n break", "response": "This function will run once the temporary logging has been configured and removes the NullHandler from the logging handlers. It will run once the temporary logging has been configured and returns the original log element."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef __remove_temp_logging_handler():\n '''\n This function will run once logging has been configured. It just removes\n the temporary stream Handler from the logging handlers.\n '''\n if is_logging_configured():\n # In this case, the temporary logging handler has been removed, return!\n return\n\n # This should already be done, but...\n __remove_null_logging_handler()\n\n root_logger = logging.getLogger()\n global LOGGING_TEMP_HANDLER\n\n for handler in root_logger.handlers:\n if handler is LOGGING_TEMP_HANDLER:\n root_logger.removeHandler(LOGGING_TEMP_HANDLER)\n # Redefine the null handler to None so it can be garbage collected\n LOGGING_TEMP_HANDLER = None\n break\n\n if sys.version_info >= (2, 7):\n # Python versions >= 2.7 allow warnings to be redirected to the logging\n # system now that it's configured. Let's enable it.\n logging.captureWarnings(True)", "response": "This function will run once logging has been configured and removes the temporary stream Handler from the logging handlers."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse GRUB conf file and return a list of all the neccessary items in the order of the current state", "response": "def conf():\n '''\n Parse GRUB conf file\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' grub.conf\n '''\n stanza = ''\n stanzas = []\n in_stanza = False\n ret = {}\n pos = 0\n try:\n with salt.utils.files.fopen(_detect_conf(), 'r') as _fp:\n for line in _fp:\n line = salt.utils.stringutils.to_unicode(line)\n if line.startswith('#'):\n continue\n if line.startswith('\\n'):\n in_stanza = False\n if 'title' in stanza:\n stanza += 'order {0}'.format(pos)\n pos += 1\n stanzas.append(stanza)\n stanza = ''\n continue\n if line.strip().startswith('title'):\n if in_stanza:\n stanza += 'order {0}'.format(pos)\n pos += 1\n stanzas.append(stanza)\n stanza = ''\n else:\n in_stanza = True\n if in_stanza:\n stanza += line\n if not in_stanza:\n key, value = _parse_line(line)\n ret[key] = value\n if in_stanza:\n if not line.endswith('\\n'):\n line += '\\n'\n stanza += line\n stanza += 'order {0}'.format(pos)\n pos += 1\n stanzas.append(stanza)\n except (IOError, OSError) as exc:\n msg = \"Could not read grub config: {0}\"\n raise CommandExecutionError(msg.format(exc))\n\n ret['stanzas'] = []\n for stanza in stanzas:\n mydict = {}\n for line in stanza.strip().splitlines():\n key, value = _parse_line(line)\n mydict[key] = value\n ret['stanzas'].append(mydict)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nparse a line into a key value pair.", "response": "def _parse_line(line=''):\n '''\n Used by conf() to break config lines into\n name/value pairs\n '''\n parts = line.split()\n key = parts.pop(0)\n value = ' '.join(parts)\n return key, value"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck upgrade impact information.", "response": "def check_upgrade_impact(system_image, kickstart_image=None, issu=True, **kwargs):\n '''\n Display upgrade impact information without actually upgrading the device.\n\n system_image (Mandatory Option)\n Path on bootflash: to system image upgrade file.\n\n kickstart_image\n Path on bootflash: to kickstart image upgrade file.\n (Not required if using combined system/kickstart image file)\n Default: None\n\n issu\n When True: Attempt In Service Software Upgrade. (non-disruptive)\n The upgrade will abort if issu is not possible.\n When False: Force (disruptive) Upgrade/Downgrade.\n Default: True\n\n timeout\n Timeout in seconds for long running 'install all' impact command.\n Default: 900\n\n error_pattern\n Use the option to pass in a regular expression to search for in the\n output of the 'install all impact' command that indicates an error\n has occurred. This option is only used when proxy minion connection\n type is ssh and otherwise ignored.\n\n .. code-block:: bash\n\n salt 'n9k' nxos.check_upgrade_impact system_image=nxos.9.2.1.bin\n salt 'n7k' nxos.check_upgrade_impact system_image=n7000-s2-dk9.8.1.1.bin \\\\\n kickstart_image=n7000-s2-kickstart.8.1.1.bin issu=False\n '''\n # Input Validation\n if not isinstance(issu, bool):\n return 'Input Error: The [issu] parameter must be either True or False'\n\n si = system_image\n ki = kickstart_image\n dev = 'bootflash'\n cmd = 'terminal dont-ask ; show install all impact'\n\n if ki is not None:\n cmd = cmd + ' kickstart {0}:{1} system {0}:{2}'.format(dev, ki, si)\n else:\n cmd = cmd + ' nxos {0}:{1}'.format(dev, si)\n\n if issu and ki is None:\n cmd = cmd + ' non-disruptive'\n\n log.info(\"Check upgrade impact using command: '%s'\", cmd)\n kwargs.update({'timeout': kwargs.get('timeout', 900)})\n error_pattern_list = ['Another install procedure may be in progress',\n 'Pre-upgrade check failed']\n kwargs.update({'error_pattern': error_pattern_list})\n\n # Execute Upgrade Impact Check\n try:\n impact_check = __salt__['nxos.sendline'](cmd, **kwargs)\n except CommandExecutionError as e:\n impact_check = ast.literal_eval(e.message)\n return _parse_upgrade_data(impact_check)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nupgrades NX-OS switch. system_image (Mandatory Option) Path on bootflash: to system image upgrade file. kickstart_image Path on bootflash: to kickstart image upgrade file. (Not required if using combined system/kickstart image file) Default: None issu Set this option to True when an In Service Software Upgrade or non-disruptive upgrade is required. The upgrade will abort if issu is not possible. Default: True timeout Timeout in seconds for long running 'install all' upgrade command. Default: 900 error_pattern Use the option to pass in a regular expression to search for in the output of the 'install all upgrade command that indicates an error has occurred. This option is only used when proxy minion connection type is ssh and otherwise ignored. .. code-block:: bash salt 'n9k' nxos.upgrade system_image=nxos.9.2.1.bin salt 'n7k' nxos.upgrade system_image=n7000-s2-dk9.8.1.1.bin \\\\ kickstart_image=n7000-s2-kickstart.8.1.1.bin issu=False", "response": "def upgrade(system_image, kickstart_image=None, issu=True, **kwargs):\n '''\n Upgrade NX-OS switch.\n\n system_image (Mandatory Option)\n Path on bootflash: to system image upgrade file.\n\n kickstart_image\n Path on bootflash: to kickstart image upgrade file.\n (Not required if using combined system/kickstart image file)\n Default: None\n\n issu\n Set this option to True when an In Service Software Upgrade or\n non-disruptive upgrade is required. The upgrade will abort if issu is\n not possible.\n Default: True\n\n timeout\n Timeout in seconds for long running 'install all' upgrade command.\n Default: 900\n\n error_pattern\n Use the option to pass in a regular expression to search for in the\n output of the 'install all upgrade command that indicates an error\n has occurred. This option is only used when proxy minion connection\n type is ssh and otherwise ignored.\n\n .. code-block:: bash\n\n salt 'n9k' nxos.upgrade system_image=nxos.9.2.1.bin\n salt 'n7k' nxos.upgrade system_image=n7000-s2-dk9.8.1.1.bin \\\\\n kickstart_image=n7000-s2-kickstart.8.1.1.bin issu=False\n '''\n # Input Validation\n if not isinstance(issu, bool):\n return 'Input Error: The [issu] parameter must be either True or False'\n\n impact = None\n upgrade = None\n maxtry = 60\n for attempt in range(1, maxtry):\n # Gather impact data first. It's possible to loose upgrade data\n # when the switch reloads or switches over to the inactive supervisor.\n # The impact data will be used if data being collected during the\n # upgrade is lost.\n if impact is None:\n log.info('Gathering impact data')\n impact = check_upgrade_impact(system_image, kickstart_image,\n issu, **kwargs)\n if impact['installing']:\n log.info('Another show impact in progress... wait and retry')\n time.sleep(30)\n continue\n # If we are upgrading from a system running a separate system and\n # kickstart image to a combined image or vice versa then the impact\n # check will return a syntax error as it's not supported.\n # Skip the impact check in this case and attempt the upgrade.\n if impact['invalid_command']:\n impact = False\n continue\n log.info('Impact data gathered:\\n%s', impact)\n\n # Check to see if conditions are sufficent to return the impact\n # data and not proceed with the actual upgrade.\n #\n # Impact data indicates the upgrade or downgrade will fail\n if impact['error_data']:\n return impact\n # Requested ISSU but ISSU is not possible\n if issu and not impact['upgrade_non_disruptive']:\n impact['error_data'] = impact['upgrade_data']\n return impact\n # Impact data indicates a failure and no module_data collected\n if not impact['succeeded'] and not impact['module_data']:\n impact['error_data'] = impact['upgrade_data']\n return impact\n # Impact data indicates switch already running desired image\n if not impact['upgrade_required']:\n impact['succeeded'] = True\n return impact\n # If we get here, impact data indicates upgrade is needed.\n upgrade = _upgrade(system_image, kickstart_image, issu, **kwargs)\n if upgrade['installing']:\n log.info('Another install is in progress... wait and retry')\n time.sleep(30)\n continue\n # If the issu option is False and this upgrade request includes a\n # kickstart image then the 'force' option is used. This option is\n # only available in certain image sets.\n if upgrade['invalid_command']:\n log_msg = 'The [issu] option was set to False for this upgrade.'\n log_msg = log_msg + ' Attempt was made to ugrade using the force'\n log_msg = log_msg + ' keyword which is not supported in this'\n log_msg = log_msg + ' image. Set [issu=True] and re-try.'\n upgrade['upgrade_data'] = log_msg\n break\n break\n\n # Check for errors and return upgrade result:\n if upgrade['backend_processing_error']:\n # This means we received a backend processing error from the transport\n # and lost the upgrade data. This also indicates that the upgrade\n # is in progress so use the impact data for logging purposes.\n impact['upgrade_in_progress'] = True\n return impact\n return upgrade"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _parse_upgrade_data(data):\n '''\n Helper method to parse upgrade data from the NX-OS device.\n '''\n upgrade_result = {}\n upgrade_result['upgrade_data'] = None\n upgrade_result['succeeded'] = False\n upgrade_result['upgrade_required'] = False\n upgrade_result['upgrade_non_disruptive'] = False\n upgrade_result['upgrade_in_progress'] = False\n upgrade_result['installing'] = False\n upgrade_result['module_data'] = {}\n upgrade_result['error_data'] = None\n upgrade_result['backend_processing_error'] = False\n upgrade_result['invalid_command'] = False\n\n # Error handling\n if isinstance(data, string_types) and re.search('Code: 500', data):\n log.info('Detected backend processing error')\n upgrade_result['error_data'] = data\n upgrade_result['backend_processing_error'] = True\n return upgrade_result\n\n if isinstance(data, dict):\n if 'code' in data and data['code'] == '400':\n log.info('Detected client error')\n upgrade_result['error_data'] = data['cli_error']\n\n if re.search('install.*may be in progress', data['cli_error']):\n log.info('Detected install in progress...')\n upgrade_result['installing'] = True\n\n if re.search('Invalid command', data['cli_error']):\n log.info('Detected invalid command...')\n upgrade_result['invalid_command'] = True\n else:\n # If we get here then it's likely we lost access to the device\n # but the upgrade succeeded. We lost the actual upgrade data so\n # set the flag such that impact data is used instead.\n log.info('Probable backend processing error')\n upgrade_result['backend_processing_error'] = True\n return upgrade_result\n\n # Get upgrade data for further parsing\n # Case 1: Command terminal dont-ask returns empty {} that we don't need.\n if isinstance(data, list) and len(data) == 2:\n data = data[1]\n # Case 2: Command terminal dont-ask does not get included.\n if isinstance(data, list) and len(data) == 1:\n data = data[0]\n\n log.info('Parsing NX-OS upgrade data')\n upgrade_result['upgrade_data'] = data\n for line in data.split('\\n'):\n\n log.info('Processing line: (%s)', line)\n\n # Check to see if upgrade is disruptive or non-disruptive\n if re.search(r'non-disruptive', line):\n log.info('Found non-disruptive line')\n upgrade_result['upgrade_non_disruptive'] = True\n\n # Example:\n # Module Image Running-Version(pri:alt) New-Version Upg-Required\n # 1 nxos 7.0(3)I7(5a) 7.0(3)I7(5a) no\n # 1 bios v07.65(09/04/2018) v07.64(05/16/2018) no\n mo = re.search(r'(\\d+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(yes|no)', line)\n if mo:\n log.info('Matched Module Running/New Version Upg-Req Line')\n bk = 'module_data' # base key\n g1 = mo.group(1)\n g2 = mo.group(2)\n g3 = mo.group(3)\n g4 = mo.group(4)\n g5 = mo.group(5)\n mk = 'module {0}:image {1}'.format(g1, g2) # module key\n upgrade_result[bk][mk] = {}\n upgrade_result[bk][mk]['running_version'] = g3\n upgrade_result[bk][mk]['new_version'] = g4\n if g5 == 'yes':\n upgrade_result['upgrade_required'] = True\n upgrade_result[bk][mk]['upgrade_required'] = True\n continue\n\n # The following lines indicate a successfull upgrade.\n if re.search(r'Install has been successful', line):\n log.info('Install successful line')\n upgrade_result['succeeded'] = True\n continue\n\n if re.search(r'Finishing the upgrade, switch will reboot in', line):\n log.info('Finishing upgrade line')\n upgrade_result['upgrade_in_progress'] = True\n continue\n\n if re.search(r'Switch will be reloaded for disruptive upgrade', line):\n log.info('Switch will be reloaded line')\n upgrade_result['upgrade_in_progress'] = True\n continue\n\n if re.search(r'Switching over onto standby', line):\n log.info('Switching over onto standby line')\n upgrade_result['upgrade_in_progress'] = True\n continue\n\n return upgrade_result", "response": "Helper method to parse upgrade data from NX - OS device."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef __standardize_result(status, message, data=None, debug_msg=None):\n '''\n Standardizes all responses\n\n :param status:\n :param message:\n :param data:\n :param debug_msg:\n :return:\n '''\n result = {\n 'status': status,\n 'message': message\n }\n\n if data is not None:\n result['return'] = data\n\n if debug_msg is not None and debug:\n result['debug'] = debug_msg\n\n return result", "response": "Standardizes the response of a request."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __get_docker_file_path(path):\n '''\n Determines the filepath to use\n\n :param path:\n :return:\n '''\n if os.path.isfile(path):\n return path\n for dc_filename in DEFAULT_DC_FILENAMES:\n file_path = os.path.join(path, dc_filename)\n if os.path.isfile(file_path):\n return file_path", "response": "Determines the filepath to use\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreading the compose file if it exists in the directory", "response": "def __read_docker_compose_file(file_path):\n '''\n Read the compose file if it exists in the directory\n\n :param file_path:\n :return:\n '''\n if not os.path.isfile(file_path):\n return __standardize_result(False,\n 'Path {} is not present'.format(file_path),\n None, None)\n try:\n with salt.utils.files.fopen(file_path, 'r') as fl:\n file_name = os.path.basename(file_path)\n result = {file_name: ''}\n for line in fl:\n result[file_name] += salt.utils.stringutils.to_unicode(line)\n except EnvironmentError:\n return __standardize_result(False,\n 'Could not read {0}'.format(file_path),\n None, None)\n return __standardize_result(True,\n 'Reading content of {0}'.format(file_path),\n result, None)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nread the compose file and load its contents", "response": "def __load_docker_compose(path):\n '''\n Read the compose file and load its' contents\n\n :param path:\n :return:\n '''\n file_path = __get_docker_file_path(path)\n if file_path is None:\n msg = 'Could not find docker-compose file at {0}'.format(path)\n return None, __standardize_result(False, msg,\n None, None)\n if not os.path.isfile(file_path):\n return None, __standardize_result(False,\n 'Path {} is not present'.format(file_path),\n None, None)\n try:\n with salt.utils.files.fopen(file_path, 'r') as fl:\n loaded = yaml.load(fl)\n except EnvironmentError:\n return None, __standardize_result(False,\n 'Could not read {0}'.format(file_path),\n None, None)\n except yaml.YAMLError as yerr:\n msg = 'Could not parse {0} {1}'.format(file_path, yerr)\n return None, __standardize_result(False, msg,\n None, None)\n if not loaded:\n msg = 'Got empty compose file at {0}'.format(file_path)\n return None, __standardize_result(False, msg,\n None, None)\n if 'services' not in loaded:\n loaded['services'] = {}\n result = {\n 'compose_content': loaded,\n 'file_name': os.path.basename(file_path)\n }\n return result, None"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef __dump_docker_compose(path, content, already_existed):\n '''\n Dumps\n\n :param path:\n :param content: the not-yet dumped content\n :return:\n '''\n try:\n dumped = yaml.safe_dump(content, indent=2, default_flow_style=False)\n return __write_docker_compose(path, dumped, already_existed)\n except TypeError as t_err:\n msg = 'Could not dump {0} {1}'.format(content, t_err)\n return __standardize_result(False, msg,\n None, None)", "response": "Dumps the content of the object to a file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef __write_docker_compose(path, docker_compose, already_existed):\n '''\n Write docker-compose to a path\n in order to use it with docker-compose ( config check )\n\n :param path:\n\n docker_compose\n contains the docker-compose file\n\n :return:\n '''\n if path.lower().endswith(('.yml', '.yaml')):\n file_path = path\n dir_name = os.path.dirname(path)\n else:\n dir_name = path\n file_path = os.path.join(dir_name, DEFAULT_DC_FILENAMES[0])\n if os.path.isdir(dir_name) is False:\n os.mkdir(dir_name)\n try:\n with salt.utils.files.fopen(file_path, 'w') as fl:\n fl.write(salt.utils.stringutils.to_str(docker_compose))\n except EnvironmentError:\n return __standardize_result(False,\n 'Could not write {0}'.format(file_path),\n None, None)\n project = __load_project_from_file_path(file_path)\n if isinstance(project, dict):\n if not already_existed:\n os.remove(file_path)\n return project\n return file_path", "response": "Write docker - compose to a path"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef __load_project(path):\n '''\n Load a docker-compose project from path\n\n :param path:\n :return:\n '''\n file_path = __get_docker_file_path(path)\n if file_path is None:\n msg = 'Could not find docker-compose file at {0}'.format(path)\n return __standardize_result(False,\n msg,\n None, None)\n return __load_project_from_file_path(file_path)", "response": "Load a docker - compose project from path"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nload a docker - compose project from file path", "response": "def __load_project_from_file_path(file_path):\n '''\n Load a docker-compose project from file path\n\n :param path:\n :return:\n '''\n try:\n project = get_project(project_dir=os.path.dirname(file_path),\n config_path=[os.path.basename(file_path)])\n except Exception as inst:\n return __handle_except(inst)\n return project"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __load_compose_definitions(path, definition):\n '''\n Will load the compose file located at path\n Then determines the format/contents of the sent definition\n\n err or results are only set if there were any\n\n :param path:\n :param definition:\n :return tuple(compose_result, loaded_definition, err):\n '''\n compose_result, err = __load_docker_compose(path)\n if err:\n return None, None, err\n if isinstance(definition, dict):\n return compose_result, definition, None\n elif definition.strip().startswith('{'):\n try:\n loaded_definition = json.deserialize(definition)\n except json.DeserializationError as jerr:\n msg = 'Could not parse {0} {1}'.format(definition, jerr)\n return None, None, __standardize_result(False, msg,\n None, None)\n else:\n try:\n loaded_definition = yaml.load(definition)\n except yaml.YAMLError as yerr:\n msg = 'Could not parse {0} {1}'.format(definition, yerr)\n return None, None, __standardize_result(False, msg,\n None, None)\n return compose_result, loaded_definition, None", "response": "Loads the compose file located at path and determines the format and contents of the sent definition"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef __dump_compose_file(path, compose_result, success_msg, already_existed):\n '''\n Utility function to dump the compose result to a file.\n\n :param path:\n :param compose_result:\n :param success_msg: the message to give upon success\n :return:\n '''\n ret = __dump_docker_compose(path,\n compose_result['compose_content'],\n already_existed)\n if isinstance(ret, dict):\n return ret\n return __standardize_result(True, success_msg,\n compose_result['compose_content'], None)", "response": "Utility function to dump the compose result to a file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_convergence_plans(project, service_names):\n '''\n Get action executed for each container\n\n :param project:\n :param service_names:\n :return:\n '''\n ret = {}\n plans = project._get_convergence_plans(project.get_services(service_names),\n ConvergenceStrategy.changed)\n for cont in plans:\n (action, container) = plans[cont]\n if action == 'create':\n ret[cont] = 'Creating container'\n elif action == 'recreate':\n ret[cont] = 'Re-creating container'\n elif action == 'start':\n ret[cont] = 'Starting container'\n elif action == 'noop':\n ret[cont] = 'Container is up to date'\n return ret", "response": "Get action executed for each container in the list of service_names"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting the content of the docker - compose file into a directory", "response": "def get(path):\n '''\n Get the content of the docker-compose file into a directory\n\n path\n Path where the docker-compose file is stored on the server\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion dockercompose.get /path/where/docker-compose/stored\n '''\n file_path = __get_docker_file_path(path)\n if file_path is None:\n return __standardize_result(False,\n 'Path {} is not present'.format(path),\n None, None)\n salt_result = __read_docker_compose_file(file_path)\n if not salt_result['status']:\n return salt_result\n project = __load_project(path)\n if isinstance(project, dict):\n salt_result['return']['valid'] = False\n else:\n salt_result['return']['valid'] = True\n return salt_result"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a docker - compose file into a directory", "response": "def create(path, docker_compose):\n '''\n Create and validate a docker-compose file into a directory\n\n path\n Path where the docker-compose file will be stored on the server\n\n docker_compose\n docker_compose file\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion dockercompose.create /path/where/docker-compose/stored content\n '''\n if docker_compose:\n ret = __write_docker_compose(path,\n docker_compose,\n already_existed=False)\n if isinstance(ret, dict):\n return ret\n else:\n return __standardize_result(False,\n 'Creating a docker-compose project failed, you must send a valid docker-compose file',\n None, None)\n return __standardize_result(True,\n 'Successfully created the docker-compose file',\n {'compose.base_dir': path},\n None)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef pull(path, service_names=None):\n '''\n Pull image for containers in the docker-compose file, service_names is a\n python list, if omitted pull all images\n\n path\n Path where the docker-compose file is stored on the server\n service_names\n If specified will pull only the image for the specified services\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion dockercompose.pull /path/where/docker-compose/stored\n salt myminion dockercompose.pull /path/where/docker-compose/stored '[janus]'\n '''\n\n project = __load_project(path)\n if isinstance(project, dict):\n return project\n else:\n try:\n project.pull(service_names)\n except Exception as inst:\n return __handle_except(inst)\n return __standardize_result(True, 'Pulling containers images via docker-compose succeeded',\n None, None)", "response": "Pull images for containers in the docker - compose file"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\npause running containers in the docker - compose file", "response": "def pause(path, service_names=None):\n '''\n Pause running containers in the docker-compose file, service_names is a python\n list, if omitted pause all containers\n\n path\n Path where the docker-compose file is stored on the server\n service_names\n If specified will pause only the specified services\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion dockercompose.pause /path/where/docker-compose/stored\n salt myminion dockercompose.pause /path/where/docker-compose/stored '[janus]'\n '''\n\n project = __load_project(path)\n debug_ret = {}\n result = {}\n if isinstance(project, dict):\n return project\n else:\n try:\n project.pause(service_names)\n if debug:\n for container in project.containers():\n if service_names is None or container.get('Name')[1:] in service_names:\n container.inspect_if_not_inspected()\n debug_ret[container.get('Name')] = container.inspect()\n result[container.get('Name')] = 'paused'\n except Exception as inst:\n return __handle_except(inst)\n return __standardize_result(True, 'Pausing containers via docker-compose', result, debug_ret)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nremoving stopped containers in the docker - compose file", "response": "def rm(path, service_names=None):\n '''\n Remove stopped containers in the docker-compose file, service_names is a python\n list, if omitted remove all stopped containers\n\n path\n Path where the docker-compose file is stored on the server\n service_names\n If specified will remove only the specified stopped services\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion dockercompose.rm /path/where/docker-compose/stored\n salt myminion dockercompose.rm /path/where/docker-compose/stored '[janus]'\n '''\n\n project = __load_project(path)\n if isinstance(project, dict):\n return project\n else:\n try:\n project.remove_stopped(service_names)\n except Exception as inst:\n return __handle_except(inst)\n return __standardize_result(True, 'Removing stopped containers via docker-compose', None, None)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists all running containers and report some information about them", "response": "def ps(path):\n '''\n List all running containers and report some information about them\n\n path\n Path where the docker-compose file is stored on the server\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion dockercompose.ps /path/where/docker-compose/stored\n '''\n\n project = __load_project(path)\n result = {}\n if isinstance(project, dict):\n return project\n else:\n if USE_FILTERCLASS:\n containers = sorted(\n project.containers(None, stopped=True) +\n project.containers(None, OneOffFilter.only),\n key=attrgetter('name'))\n else:\n containers = sorted(\n project.containers(None, stopped=True) +\n project.containers(None, one_off=True),\n key=attrgetter('name'))\n for container in containers:\n command = container.human_readable_command\n if len(command) > 30:\n command = '{0} ...'.format(command[:26])\n result[container.name] = {\n 'id': container.id,\n 'name': container.name,\n 'command': command,\n 'state': container.human_readable_state,\n 'ports': container.human_readable_ports,\n }\n return __standardize_result(True, 'Listing docker-compose containers', result, None)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate and start containers in the docker - compose. yml file located in path service_names is a python list of service names", "response": "def up(path, service_names=None):\n '''\n Create and start containers defined in the docker-compose.yml file\n located in path, service_names is a python list, if omitted create and\n start all containers\n\n path\n Path where the docker-compose file is stored on the server\n service_names\n If specified will create and start only the specified services\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion dockercompose.up /path/where/docker-compose/stored\n salt myminion dockercompose.up /path/where/docker-compose/stored '[janus]'\n '''\n\n debug_ret = {}\n project = __load_project(path)\n if isinstance(project, dict):\n return project\n else:\n try:\n result = _get_convergence_plans(project, service_names)\n ret = project.up(service_names)\n if debug:\n for container in ret:\n if service_names is None or container.get('Name')[1:] in service_names:\n container.inspect_if_not_inspected()\n debug_ret[container.get('Name')] = container.inspect()\n except Exception as inst:\n return __handle_except(inst)\n return __standardize_result(True, 'Adding containers via docker-compose', result, debug_ret)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates or update a docker - compose service", "response": "def service_upsert(path, service_name, definition):\n '''\n Create or update the definition of a docker-compose service\n This does not pull or up the service\n This wil re-write your yaml file. Comments will be lost. Indentation is set to 2 spaces\n\n path\n Path where the docker-compose file is stored on the server\n service_name\n Name of the service to create\n definition\n Service definition as yaml or json string\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion dockercompose.service_upsert /path/where/docker-compose/stored service_name definition\n '''\n compose_result, loaded_definition, err = __load_compose_definitions(path, definition)\n if err:\n return err\n services = compose_result['compose_content']['services']\n if service_name in services:\n msg = 'Service {0} already exists'.format(service_name)\n return __standardize_result(False, msg, None, None)\n services[service_name] = loaded_definition\n return __dump_compose_file(path, compose_result,\n 'Service definition for {0} is set'.format(service_name),\n already_existed=True)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef service_remove(path, service_name):\n '''\n Remove the definition of a docker-compose service\n This does not rm the container\n This wil re-write your yaml file. Comments will be lost. Indentation is set to 2 spaces\n\n path\n Path where the docker-compose file is stored on the server\n service_name\n Name of the service to remove\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion dockercompose.service_remove /path/where/docker-compose/stored service_name\n '''\n compose_result, err = __load_docker_compose(path)\n if err:\n return err\n services = compose_result['compose_content']['services']\n if service_name not in services:\n return __standardize_result(False,\n 'Service {0} did not exists'.format(service_name),\n None, None)\n del services[service_name]\n return __dump_compose_file(path, compose_result,\n 'Service {0} is removed from {1}'.format(service_name, path),\n already_existed=True)", "response": "Remove a service from a docker - compose file"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchanges the tag of a docker - compose service", "response": "def service_set_tag(path, service_name, tag):\n '''\n Change the tag of a docker-compose service\n This does not pull or up the service\n This wil re-write your yaml file. Comments will be lost. Indentation is set to 2 spaces\n\n path\n Path where the docker-compose file is stored on the server\n service_name\n Name of the service to remove\n tag\n Name of the tag (often used as version) that the service image should have\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion dockercompose.service_create /path/where/docker-compose/stored service_name tag\n '''\n compose_result, err = __load_docker_compose(path)\n if err:\n return err\n services = compose_result['compose_content']['services']\n if service_name not in services:\n return __standardize_result(False,\n 'Service {0} did not exists'.format(service_name),\n None, None)\n if 'image' not in services[service_name]:\n return __standardize_result(False,\n 'Service {0} did not contain the variable \"image\"'.format(service_name),\n None, None)\n image = services[service_name]['image'].split(':')[0]\n services[service_name]['image'] = '{0}:{1}'.format(image, tag)\n return __dump_compose_file(path, compose_result,\n 'Service {0} is set to tag \"{1}\"'.format(service_name, tag),\n already_existed=True)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef split_low_tag(tag):\n '''\n Take a low tag and split it back into the low dict that it came from\n '''\n state, id_, name, fun = tag.split('_|-')\n\n return {'state': state,\n '__id__': id_,\n 'name': name,\n 'fun': fun}", "response": "Take a low tag and split it back into the low dict that it came from\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngenerating a NULL duration for when states do not run but we want the results to be consistent.", "response": "def _calculate_fake_duration():\n '''\n Generate a NULL duration for when states do not run\n but we want the results to be consistent.\n '''\n utc_start_time = datetime.datetime.utcnow()\n local_start_time = utc_start_time - \\\n (datetime.datetime.utcnow() - datetime.datetime.now())\n utc_finish_time = datetime.datetime.utcnow()\n start_time = local_start_time.time().isoformat()\n delta = (utc_finish_time - utc_start_time)\n # duration in milliseconds.microseconds\n duration = (delta.seconds * 1000000 + delta.microseconds) / 1000.0\n\n return start_time, duration"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_accumulator_dir(cachedir):\n '''\n Return the directory that accumulator data is stored in, creating it if it\n doesn't exist.\n '''\n fn_ = os.path.join(cachedir, 'accumulator')\n if not os.path.isdir(fn_):\n # accumulator_dir is not present, create it\n os.makedirs(fn_)\n return fn_", "response": "Returns the directory that accumulator data is stored in creating it if it doesn t exist."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ntrimming any function off of a requisite", "response": "def trim_req(req):\n '''\n Trim any function off of a requisite\n '''\n reqfirst = next(iter(req))\n if '.' in reqfirst:\n return {reqfirst.split('.')[0]: req[reqfirst]}\n return req"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef state_args(id_, state, high):\n '''\n Return a set of the arguments passed to the named state\n '''\n args = set()\n if id_ not in high:\n return args\n if state not in high[id_]:\n return args\n for item in high[id_][state]:\n if not isinstance(item, dict):\n continue\n if len(item) != 1:\n continue\n args.add(next(iter(item)))\n return args", "response": "Return a set of the arguments passed to the named state"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nfinding the name in the given state and return a list of tuples that match the given name", "response": "def find_name(name, state, high):\n '''\n Scan high data for the id referencing the given name and return a list of (IDs, state) tuples that match\n\n Note: if `state` is sls, then we are looking for all IDs that match the given SLS\n '''\n ext_id = []\n if name in high:\n ext_id.append((name, state))\n # if we are requiring an entire SLS, then we need to add ourselves to everything in that SLS\n elif state == 'sls':\n for nid, item in six.iteritems(high):\n if item['__sls__'] == name:\n ext_id.append((nid, next(iter(item))))\n # otherwise we are requiring a single state, lets find it\n else:\n # We need to scan for the name\n for nid in high:\n if state in high[nid]:\n if isinstance(high[nid][state], list):\n for arg in high[nid][state]:\n if not isinstance(arg, dict):\n continue\n if len(arg) != 1:\n continue\n if arg[next(iter(arg))] == name:\n ext_id.append((nid, state))\n return ext_id"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nscan for all ids in the given sls and return them in a dict ; { name : state }", "response": "def find_sls_ids(sls, high):\n '''\n Scan for all ids in the given sls and return them in a dict; {name: state}\n '''\n ret = []\n for nid, item in six.iteritems(high):\n try:\n sls_tgt = item['__sls__']\n except TypeError:\n if nid != '__exclude__':\n log.error(\n 'Invalid non-dict item \\'%s\\' in high data. Value: %r',\n nid, item\n )\n continue\n else:\n if sls_tgt == sls:\n for st_ in item:\n if st_.startswith('__'):\n continue\n ret.append((nid, st_))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef format_log(ret):\n '''\n Format the state into a log message\n '''\n msg = ''\n if isinstance(ret, dict):\n # Looks like the ret may be a valid state return\n if 'changes' in ret:\n # Yep, looks like a valid state return\n chg = ret['changes']\n if not chg:\n if ret['comment']:\n msg = ret['comment']\n else:\n msg = 'No changes made for {0[name]}'.format(ret)\n elif isinstance(chg, dict):\n if 'diff' in chg:\n if isinstance(chg['diff'], six.string_types):\n msg = 'File changed:\\n{0}'.format(chg['diff'])\n if all([isinstance(x, dict) for x in six.itervalues(chg)]):\n if all([('old' in x and 'new' in x)\n for x in six.itervalues(chg)]):\n msg = 'Made the following changes:\\n'\n for pkg in chg:\n old = chg[pkg]['old']\n if not old and old not in (False, None):\n old = 'absent'\n new = chg[pkg]['new']\n if not new and new not in (False, None):\n new = 'absent'\n # This must be able to handle unicode as some package names contain\n # non-ascii characters like \"Fran\u00e7ais\" or \"Espa\u00f1ol\". See Issue #33605.\n msg += '\\'{0}\\' changed from \\'{1}\\' to \\'{2}\\'\\n'.format(pkg, old, new)\n if not msg:\n msg = six.text_type(ret['changes'])\n if ret['result'] is True or ret['result'] is None:\n log.info(msg)\n else:\n log.error(msg)\n else:\n # catch unhandled data\n log.info(six.text_type(ret))", "response": "Format the state into a log message"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef master_compile(master_opts, minion_opts, grains, id_, saltenv):\n '''\n Compile the master side low state data, and build the hidden state file\n '''\n st_ = MasterHighState(master_opts, minion_opts, grains, id_, saltenv)\n return st_.compile_highstate()", "response": "Compile the master side low state data and build the hidden state file"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef render_template(self, template, **kwargs):\n '''\n Enforce the states in a template\n '''\n high = compile_template(template,\n self.rend,\n self.opts['renderer'],\n self.opts['renderer_blacklist'],\n self.opts['renderer_whitelist'],\n **kwargs)\n if not high:\n return high\n return self.pad_funcs(high)", "response": "Render a template and return the high state"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef verify_high(self, high):\n '''\n Verify that the high data is viable and follows the data structure\n '''\n errors = []\n if not isinstance(high, dict):\n errors.append('High data is not a dictionary and is invalid')\n reqs = OrderedDict()\n for name, body in six.iteritems(high):\n if name.startswith('__'):\n continue\n if not isinstance(name, six.string_types):\n errors.append(\n 'ID \\'{0}\\' in SLS \\'{1}\\' is not formed as a string, but '\n 'is a {2}'.format(\n name,\n body['__sls__'],\n type(name).__name__\n )\n )\n if not isinstance(body, dict):\n err = ('The type {0} in {1} is not formatted as a dictionary'\n .format(name, body))\n errors.append(err)\n continue\n for state in body:\n if state.startswith('__'):\n continue\n if not isinstance(body[state], list):\n errors.append(\n 'State \\'{0}\\' in SLS \\'{1}\\' is not formed as a list'\n .format(name, body['__sls__'])\n )\n else:\n fun = 0\n if '.' in state:\n fun += 1\n for arg in body[state]:\n if isinstance(arg, six.string_types):\n fun += 1\n if ' ' in arg.strip():\n errors.append(('The function \"{0}\" in state '\n '\"{1}\" in SLS \"{2}\" has '\n 'whitespace, a function with whitespace is '\n 'not supported, perhaps this is an argument '\n 'that is missing a \":\"').format(\n arg,\n name,\n body['__sls__']))\n elif isinstance(arg, dict):\n # The arg is a dict, if the arg is require or\n # watch, it must be a list.\n #\n # Add the requires to the reqs dict and check them\n # all for recursive requisites.\n argfirst = next(iter(arg))\n if argfirst in ('require', 'watch', 'prereq', 'onchanges'):\n if not isinstance(arg[argfirst], list):\n errors.append(('The {0}'\n ' statement in state \\'{1}\\' in SLS \\'{2}\\' '\n 'needs to be formed as a list').format(\n argfirst,\n name,\n body['__sls__']\n ))\n # It is a list, verify that the members of the\n # list are all single key dicts.\n else:\n reqs[name] = {'state': state}\n for req in arg[argfirst]:\n if isinstance(req, six.string_types):\n req = {'id': req}\n if not isinstance(req, dict):\n err = ('Requisite declaration {0}'\n ' in SLS {1} is not formed as a'\n ' single key dictionary').format(\n req,\n body['__sls__'])\n errors.append(err)\n continue\n req_key = next(iter(req))\n req_val = req[req_key]\n if '.' in req_key:\n errors.append((\n 'Invalid requisite type \\'{0}\\' '\n 'in state \\'{1}\\', in SLS '\n '\\'{2}\\'. Requisite types must '\n 'not contain dots, did you '\n 'mean \\'{3}\\'?'.format(\n req_key,\n name,\n body['__sls__'],\n req_key[:req_key.find('.')]\n )\n ))\n if not ishashable(req_val):\n errors.append((\n 'Illegal requisite \"{0}\", '\n 'is SLS {1}\\n'\n ).format(\n six.text_type(req_val),\n body['__sls__']))\n continue\n\n # Check for global recursive requisites\n reqs[name][req_val] = req_key\n # I am going beyond 80 chars on\n # purpose, this is just too much\n # of a pain to deal with otherwise\n if req_val in reqs:\n if name in reqs[req_val]:\n if reqs[req_val][name] == state:\n if reqs[req_val]['state'] == reqs[name][req_val]:\n err = ('A recursive '\n 'requisite was found, SLS '\n '\"{0}\" ID \"{1}\" ID \"{2}\"'\n ).format(\n body['__sls__'],\n name,\n req_val\n )\n errors.append(err)\n # Make sure that there is only one key in the\n # dict\n if len(list(arg)) != 1:\n errors.append(('Multiple dictionaries '\n 'defined in argument of state \\'{0}\\' in SLS'\n ' \\'{1}\\'').format(\n name,\n body['__sls__']))\n if not fun:\n if state == 'require' or state == 'watch':\n continue\n errors.append(('No function declared in state \\'{0}\\' in'\n ' SLS \\'{1}\\'').format(state, body['__sls__']))\n elif fun > 1:\n errors.append(\n 'Too many functions declared in state \\'{0}\\' in '\n 'SLS \\'{1}\\''.format(state, body['__sls__'])\n )\n return errors", "response": "Verify that the high data is viable and follows the data structure."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsorting the list of the chunks verifying that the chunks follow the order options.", "response": "def order_chunks(self, chunks):\n '''\n Sort the chunk list verifying that the chunks follow the order\n specified in the order options.\n '''\n cap = 1\n for chunk in chunks:\n if 'order' in chunk:\n if not isinstance(chunk['order'], int):\n continue\n\n chunk_order = chunk['order']\n if chunk_order > cap - 1 and chunk_order > 0:\n cap = chunk_order + 100\n for chunk in chunks:\n if 'order' not in chunk:\n chunk['order'] = cap\n continue\n\n if not isinstance(chunk['order'], (int, float)):\n if chunk['order'] == 'last':\n chunk['order'] = cap + 1000000\n elif chunk['order'] == 'first':\n chunk['order'] = 0\n else:\n chunk['order'] = cap\n if 'name_order' in chunk:\n chunk['order'] = chunk['order'] + chunk.pop('name_order') / 10000.0\n if chunk['order'] < 0:\n chunk['order'] = cap + 1000000 + chunk['order']\n chunk['name'] = salt.utils.data.decode(chunk['name'])\n chunks.sort(key=lambda chunk: (chunk['order'], '{0[state]}{0[name]}{0[fun]}'.format(chunk)))\n return chunks"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\napply the exclude statements to the high data structure.", "response": "def apply_exclude(self, high):\n '''\n Read in the __exclude__ list and remove all excluded objects from the\n high data\n '''\n if '__exclude__' not in high:\n return high\n ex_sls = set()\n ex_id = set()\n exclude = high.pop('__exclude__')\n for exc in exclude:\n if isinstance(exc, six.string_types):\n # The exclude statement is a string, assume it is an sls\n ex_sls.add(exc)\n if isinstance(exc, dict):\n # Explicitly declared exclude\n if len(exc) != 1:\n continue\n key = next(six.iterkeys(exc))\n if key == 'sls':\n ex_sls.add(exc['sls'])\n elif key == 'id':\n ex_id.add(exc['id'])\n # Now the excludes have been simplified, use them\n if ex_sls:\n # There are sls excludes, find the associtaed ids\n for name, body in six.iteritems(high):\n if name.startswith('__'):\n continue\n if body.get('__sls__', '') in ex_sls:\n ex_id.add(name)\n for id_ in ex_id:\n if id_ in high:\n high.pop(id_)\n return high"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _gather_pillar(self):\n '''\n Whenever a state run starts, gather the pillar data fresh\n '''\n if self._pillar_override:\n if self._pillar_enc:\n try:\n self._pillar_override = salt.utils.crypt.decrypt(\n self._pillar_override,\n self._pillar_enc,\n translate_newlines=True,\n renderers=getattr(self, 'rend', None),\n opts=self.opts,\n valid_rend=self.opts['decrypt_pillar_renderers'])\n except Exception as exc:\n log.error('Failed to decrypt pillar override: %s', exc)\n\n if isinstance(self._pillar_override, six.string_types):\n # This can happen if an entire pillar dictionary was passed as\n # a single encrypted string. The override will have been\n # decrypted above, and should now be a stringified dictionary.\n # Use the YAML loader to convert that to a Python dictionary.\n try:\n self._pillar_override = yamlloader.load(\n self._pillar_override,\n Loader=yamlloader.SaltYamlSafeLoader)\n except Exception as exc:\n log.error('Failed to load CLI pillar override')\n log.exception(exc)\n\n if not isinstance(self._pillar_override, dict):\n log.error('Pillar override was not passed as a dictionary')\n self._pillar_override = None\n\n pillar = salt.pillar.get_pillar(\n self.opts,\n self.opts['grains'],\n self.opts['id'],\n self.opts['saltenv'],\n pillar_override=self._pillar_override,\n pillarenv=self.opts.get('pillarenv'))\n return pillar.compile_pillar()", "response": "Gather pillar data from the pillar file"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _mod_init(self, low):\n '''\n Check the module initialization function, if this is the first run\n of a state package that has a mod_init function, then execute the\n mod_init function in the state module.\n '''\n # ensure that the module is loaded\n try:\n self.states['{0}.{1}'.format(low['state'], low['fun'])] # pylint: disable=W0106\n except KeyError:\n return\n minit = '{0}.mod_init'.format(low['state'])\n if low['state'] not in self.mod_init:\n if minit in self.states._dict:\n mret = self.states[minit](low)\n if not mret:\n return\n self.mod_init.add(low['state'])", "response": "Check the module initialization function and execute it if it is not already done."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _mod_aggregate(self, low, running, chunks):\n '''\n Execute the aggregation systems to runtime modify the low chunk\n '''\n agg_opt = self.functions['config.option']('state_aggregate')\n if 'aggregate' in low:\n agg_opt = low['aggregate']\n if agg_opt is True:\n agg_opt = [low['state']]\n elif not isinstance(agg_opt, list):\n return low\n if low['state'] in agg_opt and not low.get('__agg__'):\n agg_fun = '{0}.mod_aggregate'.format(low['state'])\n if agg_fun in self.states:\n try:\n low = self.states[agg_fun](low, chunks, running)\n low['__agg__'] = True\n except TypeError:\n log.error('Failed to execute aggregate for state %s', low['state'])\n return low", "response": "Execute the aggregation systems to runtime modify the low chunk"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking that onlyif doesn t return 0 and that unless returns a 0.", "response": "def _run_check(self, low_data):\n '''\n Check that unless doesn't return 0, and that onlyif returns a 0.\n '''\n ret = {'result': False, 'comment': []}\n cmd_opts = {}\n\n if 'shell' in self.opts['grains']:\n cmd_opts['shell'] = self.opts['grains'].get('shell')\n\n if 'onlyif' in low_data:\n _ret = self._run_check_onlyif(low_data, cmd_opts)\n ret['result'] = _ret['result']\n ret['comment'].append(_ret['comment'])\n if 'skip_watch' in _ret:\n ret['skip_watch'] = _ret['skip_watch']\n\n if 'unless' in low_data:\n _ret = self._run_check_unless(low_data, cmd_opts)\n # If either result is True, the returned result should be True\n ret['result'] = _ret['result'] or ret['result']\n ret['comment'].append(_ret['comment'])\n if 'skip_watch' in _ret:\n # If either result is True, the returned result should be True\n ret['skip_watch'] = _ret['skip_watch'] or ret['skip_watch']\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _run_check_onlyif(self, low_data, cmd_opts):\n '''\n Check that unless doesn't return 0, and that onlyif returns a 0.\n '''\n ret = {'result': False}\n\n if not isinstance(low_data['onlyif'], list):\n low_data_onlyif = [low_data['onlyif']]\n else:\n low_data_onlyif = low_data['onlyif']\n\n def _check_cmd(cmd):\n if cmd != 0 and ret['result'] is False:\n ret.update({'comment': 'onlyif condition is false',\n 'skip_watch': True,\n 'result': True})\n elif cmd == 0:\n ret.update({'comment': 'onlyif condition is true', 'result': False})\n\n for entry in low_data_onlyif:\n if isinstance(entry, six.string_types):\n cmd = self.functions['cmd.retcode'](\n entry, ignore_retcode=True, python_shell=True, **cmd_opts)\n log.debug('Last command return code: %s', cmd)\n _check_cmd(cmd)\n elif isinstance(entry, dict):\n if 'fun' not in entry:\n ret['comment'] = 'no `fun` argument in onlyif: {0}'.format(entry)\n log.warning(ret['comment'])\n return ret\n result = self.functions[entry.pop('fun')](**entry)\n if self.state_con.get('retcode', 0):\n _check_cmd(self.state_con['retcode'])\n elif not result:\n ret.update({'comment': 'onlyif condition is false',\n 'skip_watch': True,\n 'result': True})\n else:\n ret.update({'comment': 'onlyif condition is true',\n 'result': False})\n\n else:\n ret.update({'comment': 'onlyif execution failed, bad type passed', 'result': False})\n return ret", "response": "Check that unless doesn t return 0 and that onlyif returns a 0."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrun the check_cmd function", "response": "def _run_check_cmd(self, low_data):\n '''\n Alter the way a successful state run is determined\n '''\n ret = {'result': False}\n cmd_opts = {}\n if 'shell' in self.opts['grains']:\n cmd_opts['shell'] = self.opts['grains'].get('shell')\n for entry in low_data['check_cmd']:\n cmd = self.functions['cmd.retcode'](\n entry, ignore_retcode=True, python_shell=True, **cmd_opts)\n log.debug('Last command return code: %s', cmd)\n if cmd == 0 and ret['result'] is False:\n ret.update({'comment': 'check_cmd determined the state succeeded', 'result': True})\n elif cmd != 0:\n ret.update({'comment': 'check_cmd determined the state failed', 'result': False})\n return ret\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nread the state loader value and loadup the correct states subsystem", "response": "def _load_states(self):\n '''\n Read the state loader value and loadup the correct states subsystem\n '''\n if self.states_loader == 'thorium':\n self.states = salt.loader.thorium(self.opts, self.functions, {}) # TODO: Add runners, proxy?\n else:\n self.states = salt.loader.states(self.opts, self.functions, self.utils,\n self.serializers, context=self.state_con, proxy=self.proxy)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nloads the modules into the state", "response": "def load_modules(self, data=None, proxy=None):\n '''\n Load the modules into the state\n '''\n log.info('Loading fresh modules for state activity')\n self.utils = salt.loader.utils(self.opts)\n self.functions = salt.loader.minion_mods(self.opts, self.state_con,\n utils=self.utils,\n proxy=self.proxy)\n if isinstance(data, dict):\n if data.get('provider', False):\n if isinstance(data['provider'], six.string_types):\n providers = [{data['state']: data['provider']}]\n elif isinstance(data['provider'], list):\n providers = data['provider']\n else:\n providers = {}\n for provider in providers:\n for mod in provider:\n funcs = salt.loader.raw_mod(self.opts,\n provider[mod],\n self.functions)\n if funcs:\n for func in funcs:\n f_key = '{0}{1}'.format(\n mod,\n func[func.rindex('.'):]\n )\n self.functions[f_key] = funcs[func]\n self.serializers = salt.loader.serializers(self.opts)\n self._load_states()\n self.rend = salt.loader.render(self.opts, self.functions,\n states=self.states, proxy=self.proxy, context=self.state_con)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef module_refresh(self):\n '''\n Refresh all the modules\n '''\n log.debug('Refreshing modules...')\n if self.opts['grains'].get('os') != 'MacOS':\n # In case a package has been installed into the current python\n # process 'site-packages', the 'site' module needs to be reloaded in\n # order for the newly installed package to be importable.\n try:\n reload_module(site)\n except RuntimeError:\n log.error('Error encountered during module reload. Modules were not reloaded.')\n except TypeError:\n log.error('Error encountered during module reload. Modules were not reloaded.')\n self.load_modules()\n if not self.opts.get('local', False) and self.opts.get('multiprocessing', True):\n self.functions['saltutil.refresh_modules']()", "response": "Refresh all the modules in the system"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks to see if the modules for this state instance need to be updated if so refresh the modules for this state instance.", "response": "def check_refresh(self, data, ret):\n '''\n Check to see if the modules for this state instance need to be updated,\n only update if the state is a file or a package and if it changed\n something. If the file function is managed check to see if the file is a\n possible module type, e.g. a python, pyx, or .so. Always refresh if the\n function is recurse, since that can lay down anything.\n '''\n _reload_modules = False\n if data.get('reload_grains', False):\n log.debug('Refreshing grains...')\n self.opts['grains'] = salt.loader.grains(self.opts)\n _reload_modules = True\n\n if data.get('reload_pillar', False):\n log.debug('Refreshing pillar...')\n self.opts['pillar'] = self._gather_pillar()\n _reload_modules = True\n\n if not ret['changes']:\n if data.get('force_reload_modules', False):\n self.module_refresh()\n return\n\n if data.get('reload_modules', False) or _reload_modules:\n # User explicitly requests a reload\n self.module_refresh()\n return\n\n if data['state'] == 'file':\n if data['fun'] == 'managed':\n if data['name'].endswith(\n ('.py', '.pyx', '.pyo', '.pyc', '.so')):\n self.module_refresh()\n elif data['fun'] == 'recurse':\n self.module_refresh()\n elif data['fun'] == 'symlink':\n if 'bin' in data['name']:\n self.module_refresh()\n elif data['state'] in ('pkg', 'ports'):\n self.module_refresh()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nverify the data for the current state and state.", "response": "def verify_data(self, data):\n '''\n Verify the data, return an error statement if something is wrong\n '''\n errors = []\n if 'state' not in data:\n errors.append('Missing \"state\" data')\n if 'fun' not in data:\n errors.append('Missing \"fun\" data')\n if 'name' not in data:\n errors.append('Missing \"name\" data')\n if data['name'] and not isinstance(data['name'], six.string_types):\n errors.append(\n 'ID \\'{0}\\' {1}is not formed as a string, but is a {2}'.format(\n data['name'],\n 'in SLS \\'{0}\\' '.format(data['__sls__'])\n if '__sls__' in data else '',\n type(data['name']).__name__\n )\n )\n if errors:\n return errors\n full = data['state'] + '.' + data['fun']\n if full not in self.states:\n if '__sls__' in data:\n errors.append(\n 'State \\'{0}\\' was not found in SLS \\'{1}\\''.format(\n full,\n data['__sls__']\n )\n )\n reason = self.states.missing_fun_string(full)\n if reason:\n errors.append('Reason: {0}'.format(reason))\n else:\n errors.append(\n 'Specified state \\'{0}\\' was not found'.format(\n full\n )\n )\n else:\n # First verify that the parameters are met\n aspec = salt.utils.args.get_function_argspec(self.states[full])\n arglen = 0\n deflen = 0\n if isinstance(aspec.args, list):\n arglen = len(aspec.args)\n if isinstance(aspec.defaults, tuple):\n deflen = len(aspec.defaults)\n for ind in range(arglen - deflen):\n if aspec.args[ind] not in data:\n errors.append(\n 'Missing parameter {0} for state {1}'.format(\n aspec.args[ind],\n full\n )\n )\n # If this chunk has a recursive require, then it will cause a\n # recursive loop when executing, check for it\n reqdec = ''\n if 'require' in data:\n reqdec = 'require'\n if 'watch' in data:\n # Check to see if the service has a mod_watch function, if it does\n # not, then just require\n # to just require extend the require statement with the contents\n # of watch so that the mod_watch function is not called and the\n # requisite capability is still used\n if '{0}.mod_watch'.format(data['state']) not in self.states:\n if 'require' in data:\n data['require'].extend(data.pop('watch'))\n else:\n data['require'] = data.pop('watch')\n reqdec = 'require'\n else:\n reqdec = 'watch'\n if reqdec:\n for req in data[reqdec]:\n reqfirst = next(iter(req))\n if data['state'] == reqfirst:\n if (fnmatch.fnmatch(data['name'], req[reqfirst])\n or fnmatch.fnmatch(data['__id__'], req[reqfirst])):\n err = ('Recursive require detected in SLS {0} for'\n ' require {1} in ID {2}').format(\n data['__sls__'],\n req,\n data['__id__'])\n errors.append(err)\n return errors"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nverifies the chunks in a list of low data structures", "response": "def verify_chunks(self, chunks):\n '''\n Verify the chunks in a list of low data structures\n '''\n err = []\n for chunk in chunks:\n err.extend(self.verify_data(chunk))\n return err"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef order_chunks(self, chunks):\n '''\n Sort the chunk list verifying that the chunks follow the order\n specified in the order options.\n '''\n cap = 1\n for chunk in chunks:\n if 'order' in chunk:\n if not isinstance(chunk['order'], int):\n continue\n\n chunk_order = chunk['order']\n if chunk_order > cap - 1 and chunk_order > 0:\n cap = chunk_order + 100\n for chunk in chunks:\n if 'order' not in chunk:\n chunk['order'] = cap\n continue\n\n if not isinstance(chunk['order'], (int, float)):\n if chunk['order'] == 'last':\n chunk['order'] = cap + 1000000\n elif chunk['order'] == 'first':\n chunk['order'] = 0\n else:\n chunk['order'] = cap\n if 'name_order' in chunk:\n chunk['order'] = chunk['order'] + chunk.pop('name_order') / 10000.0\n if chunk['order'] < 0:\n chunk['order'] = cap + 1000000 + chunk['order']\n chunks.sort(key=lambda chunk: (chunk['order'], '{0[state]}{0[name]}{0[fun]}'.format(chunk)))\n return chunks", "response": "Sort the list of the chunks verifying that the chunks follow the order options."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef compile_high_data(self, high, orchestration_jid=None):\n '''\n \"Compile\" the high data as it is retrieved from the CLI or YAML into\n the individual state executor structures\n '''\n chunks = []\n for name, body in six.iteritems(high):\n if name.startswith('__'):\n continue\n for state, run in six.iteritems(body):\n funcs = set()\n names = []\n if state.startswith('__'):\n continue\n chunk = OrderedDict()\n chunk['state'] = state\n chunk['name'] = name\n if orchestration_jid is not None:\n chunk['__orchestration_jid__'] = orchestration_jid\n if '__sls__' in body:\n chunk['__sls__'] = body['__sls__']\n if '__env__' in body:\n chunk['__env__'] = body['__env__']\n chunk['__id__'] = name\n for arg in run:\n if isinstance(arg, six.string_types):\n funcs.add(arg)\n continue\n if isinstance(arg, dict):\n for key, val in six.iteritems(arg):\n if key == 'names':\n for _name in val:\n if _name not in names:\n names.append(_name)\n elif key == 'state':\n # Don't pass down a state override\n continue\n elif (key == 'name' and\n not isinstance(val, six.string_types)):\n # Invalid name, fall back to ID\n chunk[key] = name\n else:\n chunk[key] = val\n if names:\n name_order = 1\n for entry in names:\n live = copy.deepcopy(chunk)\n if isinstance(entry, dict):\n low_name = next(six.iterkeys(entry))\n live['name'] = low_name\n list(map(live.update, entry[low_name]))\n else:\n live['name'] = entry\n live['name_order'] = name_order\n name_order += 1\n for fun in funcs:\n live['fun'] = fun\n chunks.append(live)\n else:\n live = copy.deepcopy(chunk)\n for fun in funcs:\n live['fun'] = fun\n chunks.append(live)\n chunks = self.order_chunks(chunks)\n return chunks", "response": "Compile the high data into a dictionary of high data structures."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef reconcile_extend(self, high):\n '''\n Pull the extend data and add it to the respective high data\n '''\n errors = []\n if '__extend__' not in high:\n return high, errors\n ext = high.pop('__extend__')\n for ext_chunk in ext:\n for name, body in six.iteritems(ext_chunk):\n if name not in high:\n state_type = next(\n x for x in body if not x.startswith('__')\n )\n # Check for a matching 'name' override in high data\n ids = find_name(name, state_type, high)\n if len(ids) != 1:\n errors.append(\n 'Cannot extend ID \\'{0}\\' in \\'{1}:{2}\\'. It is not '\n 'part of the high state.\\n'\n 'This is likely due to a missing include statement '\n 'or an incorrectly typed ID.\\nEnsure that a '\n 'state with an ID of \\'{0}\\' is available\\nin '\n 'environment \\'{1}\\' and to SLS \\'{2}\\''.format(\n name,\n body.get('__env__', 'base'),\n body.get('__sls__', 'base'))\n )\n continue\n else:\n name = ids[0][0]\n\n for state, run in six.iteritems(body):\n if state.startswith('__'):\n continue\n if state not in high[name]:\n high[name][state] = run\n continue\n # high[name][state] is extended by run, both are lists\n for arg in run:\n update = False\n for hind in range(len(high[name][state])):\n if isinstance(arg, six.string_types) and isinstance(high[name][state][hind], six.string_types):\n # replacing the function, replace the index\n high[name][state].pop(hind)\n high[name][state].insert(hind, arg)\n update = True\n continue\n if isinstance(arg, dict) and isinstance(high[name][state][hind], dict):\n # It is an option, make sure the options match\n argfirst = next(iter(arg))\n if argfirst == next(iter(high[name][state][hind])):\n # If argfirst is a requisite then we must merge\n # our requisite with that of the target state\n if argfirst in STATE_REQUISITE_KEYWORDS:\n high[name][state][hind][argfirst].extend(arg[argfirst])\n # otherwise, its not a requisite and we are just extending (replacing)\n else:\n high[name][state][hind] = arg\n update = True\n if (argfirst == 'name' and\n next(iter(high[name][state][hind])) == 'names'):\n # If names are overwritten by name use the name\n high[name][state][hind] = arg\n if not update:\n high[name][state].append(arg)\n return high, errors", "response": "Reconcile the extend data in the high data structure."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nextends the data reference with requisite_in arguments", "response": "def requisite_in(self, high):\n '''\n Extend the data reference with requisite_in arguments\n '''\n req_in = {'require_in', 'watch_in', 'onfail_in', 'onchanges_in', 'use', 'use_in', 'prereq', 'prereq_in'}\n req_in_all = req_in.union({'require', 'watch', 'onfail', 'onfail_stop', 'onchanges'})\n extend = {}\n errors = []\n disabled_reqs = self.opts.get('disabled_requisites', [])\n if not isinstance(disabled_reqs, list):\n disabled_reqs = [disabled_reqs]\n for id_, body in six.iteritems(high):\n if not isinstance(body, dict):\n continue\n for state, run in six.iteritems(body):\n if state.startswith('__'):\n continue\n for arg in run:\n if isinstance(arg, dict):\n # It is not a function, verify that the arg is a\n # requisite in statement\n if not arg:\n # Empty arg dict\n # How did we get this far?\n continue\n # Split out the components\n key = next(iter(arg))\n if key not in req_in:\n continue\n if key in disabled_reqs:\n log.warning('The %s requisite has been disabled, Ignoring.', key)\n continue\n rkey = key.split('_')[0]\n items = arg[key]\n if isinstance(items, dict):\n # Formatted as a single req_in\n for _state, name in six.iteritems(items):\n\n # Not a use requisite_in\n found = False\n if name not in extend:\n extend[name] = OrderedDict()\n if '.' in _state:\n errors.append(\n 'Invalid requisite in {0}: {1} for '\n '{2}, in SLS \\'{3}\\'. Requisites must '\n 'not contain dots, did you mean \\'{4}\\'?'\n .format(\n rkey,\n _state,\n name,\n body['__sls__'],\n _state[:_state.find('.')]\n )\n )\n _state = _state.split('.')[0]\n if _state not in extend[name]:\n extend[name][_state] = []\n extend[name]['__env__'] = body['__env__']\n extend[name]['__sls__'] = body['__sls__']\n for ind in range(len(extend[name][_state])):\n if next(iter(\n extend[name][_state][ind])) == rkey:\n # Extending again\n extend[name][_state][ind][rkey].append(\n {state: id_}\n )\n found = True\n if found:\n continue\n # The rkey is not present yet, create it\n extend[name][_state].append(\n {rkey: [{state: id_}]}\n )\n\n if isinstance(items, list):\n # Formed as a list of requisite additions\n hinges = []\n for ind in items:\n if not isinstance(ind, dict):\n # Malformed req_in\n if ind in high:\n _ind_high = [x for x\n in high[ind]\n if not x.startswith('__')]\n ind = {_ind_high[0]: ind}\n else:\n found = False\n for _id in iter(high):\n for state in [state for state\n in iter(high[_id])\n if not state.startswith('__')]:\n for j in iter(high[_id][state]):\n if isinstance(j, dict) and 'name' in j:\n if j['name'] == ind:\n ind = {state: _id}\n found = True\n if not found:\n continue\n if not ind:\n continue\n pstate = next(iter(ind))\n pname = ind[pstate]\n if pstate == 'sls':\n # Expand hinges here\n hinges = find_sls_ids(pname, high)\n else:\n hinges.append((pname, pstate))\n if '.' in pstate:\n errors.append(\n 'Invalid requisite in {0}: {1} for '\n '{2}, in SLS \\'{3}\\'. Requisites must '\n 'not contain dots, did you mean \\'{4}\\'?'\n .format(\n rkey,\n pstate,\n pname,\n body['__sls__'],\n pstate[:pstate.find('.')]\n )\n )\n pstate = pstate.split(\".\")[0]\n for tup in hinges:\n name, _state = tup\n if key == 'prereq_in':\n # Add prerequired to origin\n if id_ not in extend:\n extend[id_] = OrderedDict()\n if state not in extend[id_]:\n extend[id_][state] = []\n extend[id_][state].append(\n {'prerequired': [{_state: name}]}\n )\n if key == 'prereq':\n # Add prerequired to prereqs\n ext_ids = find_name(name, _state, high)\n for ext_id, _req_state in ext_ids:\n if ext_id not in extend:\n extend[ext_id] = OrderedDict()\n if _req_state not in extend[ext_id]:\n extend[ext_id][_req_state] = []\n extend[ext_id][_req_state].append(\n {'prerequired': [{state: id_}]}\n )\n continue\n if key == 'use_in':\n # Add the running states args to the\n # use_in states\n ext_ids = find_name(name, _state, high)\n for ext_id, _req_state in ext_ids:\n if not ext_id:\n continue\n ext_args = state_args(ext_id, _state, high)\n if ext_id not in extend:\n extend[ext_id] = OrderedDict()\n if _req_state not in extend[ext_id]:\n extend[ext_id][_req_state] = []\n ignore_args = req_in_all.union(ext_args)\n for arg in high[id_][state]:\n if not isinstance(arg, dict):\n continue\n if len(arg) != 1:\n continue\n if next(iter(arg)) in ignore_args:\n continue\n # Don't use name or names\n if next(six.iterkeys(arg)) == 'name':\n continue\n if next(six.iterkeys(arg)) == 'names':\n continue\n extend[ext_id][_req_state].append(arg)\n continue\n if key == 'use':\n # Add the use state's args to the\n # running state\n ext_ids = find_name(name, _state, high)\n for ext_id, _req_state in ext_ids:\n if not ext_id:\n continue\n loc_args = state_args(id_, state, high)\n if id_ not in extend:\n extend[id_] = OrderedDict()\n if state not in extend[id_]:\n extend[id_][state] = []\n ignore_args = req_in_all.union(loc_args)\n for arg in high[ext_id][_req_state]:\n if not isinstance(arg, dict):\n continue\n if len(arg) != 1:\n continue\n if next(iter(arg)) in ignore_args:\n continue\n # Don't use name or names\n if next(six.iterkeys(arg)) == 'name':\n continue\n if next(six.iterkeys(arg)) == 'names':\n continue\n extend[id_][state].append(arg)\n continue\n found = False\n if name not in extend:\n extend[name] = OrderedDict()\n if _state not in extend[name]:\n extend[name][_state] = []\n extend[name]['__env__'] = body['__env__']\n extend[name]['__sls__'] = body['__sls__']\n for ind in range(len(extend[name][_state])):\n if next(iter(\n extend[name][_state][ind])) == rkey:\n # Extending again\n extend[name][_state][ind][rkey].append(\n {state: id_}\n )\n found = True\n if found:\n continue\n # The rkey is not present yet, create it\n extend[name][_state].append(\n {rkey: [{state: id_}]}\n )\n high['__extend__'] = []\n for key, val in six.iteritems(extend):\n high['__extend__'].append({key: val})\n req_in_high, req_in_errors = self.reconcile_extend(high)\n errors.extend(req_in_errors)\n return req_in_high, errors"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _call_parallel_target(self, name, cdata, low):\n '''\n The target function to call that will create the parallel thread/process\n '''\n # we need to re-record start/end duration here because it is impossible to\n # correctly calculate further down the chain\n utc_start_time = datetime.datetime.utcnow()\n\n tag = _gen_tag(low)\n try:\n ret = self.states[cdata['full']](*cdata['args'],\n **cdata['kwargs'])\n except Exception as exc:\n log.debug('An exception occurred in this state: %s', exc,\n exc_info_on_loglevel=logging.DEBUG)\n trb = traceback.format_exc()\n ret = {\n 'result': False,\n 'name': name,\n 'changes': {},\n 'comment': 'An exception occurred in this state: {0}'.format(trb)\n }\n\n utc_finish_time = datetime.datetime.utcnow()\n delta = (utc_finish_time - utc_start_time)\n # duration in milliseconds.microseconds\n duration = (delta.seconds * 1000000 + delta.microseconds) / 1000.0\n ret['duration'] = duration\n\n troot = os.path.join(self.opts['cachedir'], self.jid)\n tfile = os.path.join(\n troot,\n salt.utils.hashutils.sha1_digest(tag))\n if not os.path.isdir(troot):\n try:\n os.makedirs(troot)\n except OSError:\n # Looks like the directory was created between the check\n # and the attempt, we are safe to pass\n pass\n with salt.utils.files.fopen(tfile, 'wb+') as fp_:\n fp_.write(msgpack_serialize(ret))", "response": "The target function to call that will create the parallel thread and process the current state."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef call_parallel(self, cdata, low):\n '''\n Call the state defined in the given cdata in parallel\n '''\n # There are a number of possibilities to not have the cdata\n # populated with what we might have expected, so just be smart\n # enough to not raise another KeyError as the name is easily\n # guessable and fallback in all cases to present the real\n # exception to the user\n name = (cdata.get('args') or [None])[0] or cdata['kwargs'].get('name')\n if not name:\n name = low.get('name', low.get('__id__'))\n\n proc = salt.utils.process.MultiprocessingProcess(\n target=self._call_parallel_target,\n args=(name, cdata, low))\n proc.start()\n ret = {'name': name,\n 'result': None,\n 'changes': {},\n 'comment': 'Started in a separate process',\n 'proc': proc}\n return ret", "response": "Call the state defined in the given cdata in parallel"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncalling a state directly with the low data structure verify the data structure before processing.", "response": "def call(self, low, chunks=None, running=None, retries=1):\n '''\n Call a state directly with the low data structure, verify data\n before processing.\n '''\n use_uptime = False\n if os.path.isfile('/proc/uptime'):\n use_uptime = True\n with salt.utils.files.fopen('/proc/uptime', 'r') as fp_:\n start_uptime = float(fp_.readline().split()[0])\n utc_start_time = datetime.datetime.utcnow()\n local_start_time = utc_start_time - (datetime.datetime.utcnow() - datetime.datetime.now())\n log.info('Running state [%s] at time %s',\n low['name'].strip() if isinstance(low['name'], six.string_types)\n else low['name'],\n local_start_time.time().isoformat()\n )\n errors = self.verify_data(low)\n if errors:\n ret = {\n 'result': False,\n 'name': low['name'],\n 'changes': {},\n 'comment': '',\n }\n for err in errors:\n ret['comment'] += '{0}\\n'.format(err)\n ret['__run_num__'] = self.__run_num\n self.__run_num += 1\n format_log(ret)\n self.check_refresh(low, ret)\n return ret\n else:\n ret = {'result': False, 'name': low['name'], 'changes': {}}\n\n self.state_con['runas'] = low.get('runas', None)\n\n if low['state'] == 'cmd' and 'password' in low:\n self.state_con['runas_password'] = low['password']\n else:\n self.state_con['runas_password'] = low.get('runas_password', None)\n\n if not low.get('__prereq__'):\n log.info(\n 'Executing state %s.%s for [%s]',\n low['state'],\n low['fun'],\n low['name'].strip() if isinstance(low['name'], six.string_types)\n else low['name']\n )\n\n if 'provider' in low:\n self.load_modules(low)\n\n state_func_name = '{0[state]}.{0[fun]}'.format(low)\n cdata = salt.utils.args.format_call(\n self.states[state_func_name],\n low,\n initial_ret={'full': state_func_name},\n expected_extra_kws=STATE_INTERNAL_KEYWORDS\n )\n inject_globals = {\n # Pass a copy of the running dictionary, the low state chunks and\n # the current state dictionaries.\n # We pass deep copies here because we don't want any misbehaving\n # state module to change these at runtime.\n '__low__': immutabletypes.freeze(low),\n '__running__': immutabletypes.freeze(running) if running else {},\n '__instance_id__': self.instance_id,\n '__lowstate__': immutabletypes.freeze(chunks) if chunks else {}\n }\n\n if '__env__' in low:\n inject_globals['__env__'] = six.text_type(low['__env__'])\n\n if self.inject_globals:\n inject_globals.update(self.inject_globals)\n\n if low.get('__prereq__'):\n test = sys.modules[self.states[cdata['full']].__module__].__opts__['test']\n sys.modules[self.states[cdata['full']].__module__].__opts__['test'] = True\n try:\n # Let's get a reference to the salt environment to use within this\n # state call.\n #\n # If the state function accepts an 'env' keyword argument, it\n # allows the state to be overridden(we look for that in cdata). If\n # that's not found in cdata, we look for what we're being passed in\n # the original data, namely, the special dunder __env__. If that's\n # not found we default to 'base'\n if ('unless' in low and '{0[state]}.mod_run_check'.format(low) not in self.states) or \\\n ('onlyif' in low and '{0[state]}.mod_run_check'.format(low) not in self.states):\n ret.update(self._run_check(low))\n\n if not self.opts.get('lock_saltenv', False):\n # NOTE: Overriding the saltenv when lock_saltenv is blocked in\n # salt/modules/state.py, before we ever get here, but this\n # additional check keeps use of the State class outside of the\n # salt/modules/state.py from getting around this setting.\n if 'saltenv' in low:\n inject_globals['__env__'] = six.text_type(low['saltenv'])\n elif isinstance(cdata['kwargs'].get('env', None), six.string_types):\n # User is using a deprecated env setting which was parsed by\n # format_call.\n # We check for a string type since module functions which\n # allow setting the OS environ also make use of the \"env\"\n # keyword argument, which is not a string\n inject_globals['__env__'] = six.text_type(cdata['kwargs']['env'])\n\n if '__env__' not in inject_globals:\n # Let's use the default environment\n inject_globals['__env__'] = 'base'\n\n if '__orchestration_jid__' in low:\n inject_globals['__orchestration_jid__'] = \\\n low['__orchestration_jid__']\n\n if 'result' not in ret or ret['result'] is False:\n self.states.inject_globals = inject_globals\n if self.mocked:\n ret = mock_ret(cdata)\n else:\n # Execute the state function\n if not low.get('__prereq__') and low.get('parallel'):\n # run the state call in parallel, but only if not in a prereq\n ret = self.call_parallel(cdata, low)\n else:\n self.format_slots(cdata)\n if cdata['full'].split('.')[-1] == '__call__':\n # __call__ requires OrderedDict to preserve state order\n # kwargs are also invalid overall\n ret = self.states[cdata['full']](cdata['args'], module=None, state=cdata['kwargs'])\n else:\n ret = self.states[cdata['full']](*cdata['args'], **cdata['kwargs'])\n self.states.inject_globals = {}\n if 'check_cmd' in low and '{0[state]}.mod_run_check_cmd'.format(low) not in self.states:\n ret.update(self._run_check_cmd(low))\n except Exception as exc:\n log.debug('An exception occurred in this state: %s', exc,\n exc_info_on_loglevel=logging.DEBUG)\n trb = traceback.format_exc()\n # There are a number of possibilities to not have the cdata\n # populated with what we might have expected, so just be smart\n # enough to not raise another KeyError as the name is easily\n # guessable and fallback in all cases to present the real\n # exception to the user\n name = (cdata.get('args') or [None])[0] or cdata['kwargs'].get('name')\n if not name:\n name = low.get('name', low.get('__id__'))\n\n ret = {\n 'result': False,\n 'name': name,\n 'changes': {},\n 'comment': 'An exception occurred in this state: {0}'.format(trb)\n }\n finally:\n if low.get('__prereq__'):\n sys.modules[self.states[cdata['full']].__module__].__opts__['test'] = test\n\n self.state_con.pop('runas', None)\n self.state_con.pop('runas_password', None)\n\n if not isinstance(ret, dict):\n return ret\n\n # If format_call got any warnings, let's show them to the user\n if 'warnings' in cdata:\n ret.setdefault('warnings', []).extend(cdata['warnings'])\n\n if 'provider' in low:\n self.load_modules()\n\n if low.get('__prereq__'):\n low['__prereq__'] = False\n return ret\n\n ret['__sls__'] = low.get('__sls__')\n ret['__run_num__'] = self.__run_num\n self.__run_num += 1\n format_log(ret)\n self.check_refresh(low, ret)\n if use_uptime:\n with salt.utils.files.fopen('/proc/uptime', 'r') as fp_:\n finish_uptime = float(fp_.readline().split()[0])\n utc_finish_time = datetime.datetime.utcnow()\n timezone_delta = datetime.datetime.utcnow() - datetime.datetime.now()\n local_finish_time = utc_finish_time - timezone_delta\n local_start_time = utc_start_time - timezone_delta\n ret['start_time'] = local_start_time.time().isoformat()\n if use_uptime:\n duration = (finish_uptime - start_uptime) * 1000.0\n else:\n delta = (utc_finish_time - utc_start_time)\n # duration in milliseconds.microseconds\n duration = (delta.seconds * 1000000 + delta.microseconds) / 1000.0\n ret['duration'] = duration\n ret['__id__'] = low['__id__']\n log.info(\n 'Completed state [%s] at time %s (duration_in_ms=%s)',\n low['name'].strip() if isinstance(low['name'], six.string_types)\n else low['name'],\n local_finish_time.time().isoformat(),\n duration\n )\n if 'retry' in low:\n low['retry'] = self.verify_retry_data(low['retry'])\n if not sys.modules[self.states[cdata['full']].__module__].__opts__['test']:\n if low['retry']['until'] != ret['result']:\n if low['retry']['attempts'] > retries:\n interval = low['retry']['interval']\n if low['retry']['splay'] != 0:\n interval = interval + random.randint(0, low['retry']['splay'])\n log.info(\n 'State result does not match retry until value, '\n 'state will be re-run in %s seconds', interval\n )\n self.functions['test.sleep'](interval)\n retry_ret = self.call(low, chunks, running, retries=retries+1)\n orig_ret = ret\n ret = retry_ret\n ret['comment'] = '\\n'.join(\n [(\n 'Attempt {0}: Returned a result of \"{1}\", '\n 'with the following comment: \"{2}\"'.format(\n retries,\n orig_ret['result'],\n orig_ret['comment'])\n ),\n '' if not ret['comment'] else ret['comment']])\n ret['duration'] = ret['duration'] + orig_ret['duration'] + (interval * 1000)\n if retries == 1:\n ret['start_time'] = orig_ret['start_time']\n else:\n ret['comment'] = ' '.join(\n ['' if not ret['comment'] else ret['comment'],\n ('The state would be retried every {1} seconds '\n '(with a splay of up to {3} seconds) '\n 'a maximum of {0} times or until a result of {2} '\n 'is returned').format(low['retry']['attempts'],\n low['retry']['interval'],\n low['retry']['until'],\n low['retry']['splay'])])\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef format_slots(self, cdata):\n '''\n Read in the arguments from the low level slot syntax to make a last\n minute runtime call to gather relevant data for the specific routine\n\n Will parse strings, first level of dictionary values, and strings and\n first level dict values inside of lists\n '''\n # __slot__:salt.cmd.run(foo, bar, baz=qux)\n SLOT_TEXT = '__slot__:'\n ctx = (('args', enumerate(cdata['args'])),\n ('kwargs', cdata['kwargs'].items()))\n for atype, avalues in ctx:\n for ind, arg in avalues:\n arg = salt.utils.data.decode(arg, keep=True)\n if isinstance(arg, dict):\n # Search dictionary values for __slot__:\n for key, value in arg.items():\n try:\n if value.startswith(SLOT_TEXT):\n log.trace(\"Slot processsing dict value %s\", value)\n cdata[atype][ind][key] = self.__eval_slot(value)\n except AttributeError:\n # Not a string/slot\n continue\n elif isinstance(arg, list):\n for idx, listvalue in enumerate(arg):\n log.trace(\"Slot processing list value: %s\", listvalue)\n if isinstance(listvalue, dict):\n # Search dict values in list for __slot__:\n for key, value in listvalue.items():\n try:\n if value.startswith(SLOT_TEXT):\n log.trace(\"Slot processsing nested dict value %s\", value)\n cdata[atype][ind][idx][key] = self.__eval_slot(value)\n except AttributeError:\n # Not a string/slot\n continue\n if isinstance(listvalue, six.text_type):\n # Search strings in a list for __slot__:\n if listvalue.startswith(SLOT_TEXT):\n log.trace(\"Slot processsing nested string %s\", listvalue)\n cdata[atype][ind][idx] = self.__eval_slot(listvalue)\n elif isinstance(arg, six.text_type) \\\n and arg.startswith(SLOT_TEXT):\n # Search strings for __slot__:\n log.trace(\"Slot processsing %s\", arg)\n cdata[atype][ind] = self.__eval_slot(arg)\n else:\n # Not a slot, skip it\n continue", "response": "This function formats the low level slots in the salt master and returns the data in cdata."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef verify_retry_data(self, retry_data):\n '''\n verifies the specified retry data\n '''\n retry_defaults = {\n 'until': True,\n 'attempts': 2,\n 'splay': 0,\n 'interval': 30,\n }\n expected_data = {\n 'until': bool,\n 'attempts': int,\n 'interval': int,\n 'splay': int,\n }\n validated_retry_data = {}\n if isinstance(retry_data, dict):\n for expected_key, value_type in six.iteritems(expected_data):\n if expected_key in retry_data:\n if isinstance(retry_data[expected_key], value_type):\n validated_retry_data[expected_key] = retry_data[expected_key]\n else:\n log.warning(\n 'An invalid value was passed for the retry %s, '\n 'using default value \\'%s\\'',\n expected_key, retry_defaults[expected_key]\n )\n validated_retry_data[expected_key] = retry_defaults[expected_key]\n else:\n validated_retry_data[expected_key] = retry_defaults[expected_key]\n else:\n log.warning(('State is set to retry, but a valid dict for retry '\n 'configuration was not found. Using retry defaults'))\n validated_retry_data = retry_defaults\n return validated_retry_data", "response": "Verify the retry data."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\niterates over a list of chunks and call them checking for requires.", "response": "def call_chunks(self, chunks):\n '''\n Iterate over a list of chunks and call them, checking for requires.\n '''\n # Check for any disabled states\n disabled = {}\n if 'state_runs_disabled' in self.opts['grains']:\n for low in chunks[:]:\n state_ = '{0}.{1}'.format(low['state'], low['fun'])\n for pat in self.opts['grains']['state_runs_disabled']:\n if fnmatch.fnmatch(state_, pat):\n comment = (\n 'The state function \"{0}\" is currently disabled by \"{1}\", '\n 'to re-enable, run state.enable {1}.'\n ).format(\n state_,\n pat,\n )\n _tag = _gen_tag(low)\n disabled[_tag] = {'changes': {},\n 'result': False,\n 'comment': comment,\n '__run_num__': self.__run_num,\n '__sls__': low['__sls__']}\n self.__run_num += 1\n chunks.remove(low)\n break\n running = {}\n for low in chunks:\n if '__FAILHARD__' in running:\n running.pop('__FAILHARD__')\n return running\n tag = _gen_tag(low)\n if tag not in running:\n # Check if this low chunk is paused\n action = self.check_pause(low)\n if action == 'kill':\n break\n running = self.call_chunk(low, running, chunks)\n if self.check_failhard(low, running):\n return running\n self.active = set()\n while True:\n if self.reconcile_procs(running):\n break\n time.sleep(0.01)\n ret = dict(list(disabled.items()) + list(running.items()))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks if the low data chunk should send a failhard signal", "response": "def check_failhard(self, low, running):\n '''\n Check if the low data chunk should send a failhard signal\n '''\n tag = _gen_tag(low)\n if self.opts.get('test', False):\n return False\n if low.get('failhard', self.opts['failhard']) and tag in running:\n if running[tag]['result'] is None:\n return False\n return not running[tag]['result']\n return False"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef check_pause(self, low):\n '''\n Check to see if this low chunk has been paused\n '''\n if not self.jid:\n # Can't pause on salt-ssh since we can't track continuous state\n return\n pause_path = os.path.join(self.opts['cachedir'], 'state_pause', self.jid)\n start = time.time()\n if os.path.isfile(pause_path):\n try:\n while True:\n tries = 0\n with salt.utils.files.fopen(pause_path, 'rb') as fp_:\n try:\n pdat = msgpack_deserialize(fp_.read())\n except msgpack.UnpackValueError:\n # Reading race condition\n if tries > 10:\n # Break out if there are a ton of read errors\n return\n tries += 1\n time.sleep(1)\n continue\n id_ = low['__id__']\n key = ''\n if id_ in pdat:\n key = id_\n elif '__all__' in pdat:\n key = '__all__'\n if key:\n if 'duration' in pdat[key]:\n now = time.time()\n if now - start > pdat[key]['duration']:\n return 'run'\n if 'kill' in pdat[key]:\n return 'kill'\n else:\n return 'run'\n time.sleep(1)\n except Exception as exc:\n log.error('Failed to read in pause data for file located at: %s', pause_path)\n return 'run'\n return 'run'", "response": "Check to see if this low chunk has been paused and return the state that was paused."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncheck the running dict for processes and resolve them", "response": "def reconcile_procs(self, running):\n '''\n Check the running dict for processes and resolve them\n '''\n retset = set()\n for tag in running:\n proc = running[tag].get('proc')\n if proc:\n if not proc.is_alive():\n ret_cache = os.path.join(\n self.opts['cachedir'],\n self.jid,\n salt.utils.hashutils.sha1_digest(tag))\n if not os.path.isfile(ret_cache):\n ret = {'result': False,\n 'comment': 'Parallel process failed to return',\n 'name': running[tag]['name'],\n 'changes': {}}\n try:\n with salt.utils.files.fopen(ret_cache, 'rb') as fp_:\n ret = msgpack_deserialize(fp_.read())\n except (OSError, IOError):\n ret = {'result': False,\n 'comment': 'Parallel cache failure',\n 'name': running[tag]['name'],\n 'changes': {}}\n running[tag].update(ret)\n running[tag].pop('proc')\n else:\n retset.add(False)\n return False not in retset"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking the status of all requisites in the given low data.", "response": "def check_requisite(self, low, running, chunks, pre=False):\n '''\n Look into the running data to check the status of all requisite\n states\n '''\n disabled_reqs = self.opts.get('disabled_requisites', [])\n if not isinstance(disabled_reqs, list):\n disabled_reqs = [disabled_reqs]\n present = False\n # If mod_watch is not available make it a require\n if 'watch' in low:\n if '{0}.mod_watch'.format(low['state']) not in self.states:\n if 'require' in low:\n low['require'].extend(low.pop('watch'))\n else:\n low['require'] = low.pop('watch')\n else:\n present = True\n if 'watch_any' in low:\n if '{0}.mod_watch'.format(low['state']) not in self.states:\n if 'require_any' in low:\n low['require_any'].extend(low.pop('watch_any'))\n else:\n low['require_any'] = low.pop('watch_any')\n else:\n present = True\n if 'require' in low:\n present = True\n if 'require_any' in low:\n present = True\n if 'prerequired' in low:\n present = True\n if 'prereq' in low:\n present = True\n if 'onfail' in low:\n present = True\n if 'onfail_any' in low:\n present = True\n if 'onfail_all' in low:\n present = True\n if 'onchanges' in low:\n present = True\n if 'onchanges_any' in low:\n present = True\n if not present:\n return 'met', ()\n self.reconcile_procs(running)\n reqs = {\n 'require': [],\n 'require_any': [],\n 'watch': [],\n 'watch_any': [],\n 'prereq': [],\n 'onfail': [],\n 'onfail_any': [],\n 'onfail_all': [],\n 'onchanges': [],\n 'onchanges_any': []}\n if pre:\n reqs['prerequired'] = []\n for r_state in reqs:\n if r_state in low and low[r_state] is not None:\n if r_state in disabled_reqs:\n log.warning('The %s requisite has been disabled, Ignoring.', r_state)\n continue\n for req in low[r_state]:\n if isinstance(req, six.string_types):\n req = {'id': req}\n req = trim_req(req)\n found = False\n for chunk in chunks:\n req_key = next(iter(req))\n req_val = req[req_key]\n if req_val is None:\n continue\n if req_key == 'sls':\n # Allow requisite tracking of entire sls files\n if fnmatch.fnmatch(chunk['__sls__'], req_val):\n found = True\n reqs[r_state].append(chunk)\n continue\n try:\n if isinstance(req_val, six.string_types):\n if (fnmatch.fnmatch(chunk['name'], req_val) or\n fnmatch.fnmatch(chunk['__id__'], req_val)):\n if req_key == 'id' or chunk['state'] == req_key:\n found = True\n reqs[r_state].append(chunk)\n else:\n raise KeyError\n except KeyError as exc:\n raise SaltRenderError(\n 'Could not locate requisite of [{0}] present in state with name [{1}]'.format(\n req_key, chunk['name']))\n except TypeError:\n # On Python 2, the above req_val, being an OrderedDict, will raise a KeyError,\n # however on Python 3 it will raise a TypeError\n # This was found when running tests.unit.test_state.StateCompilerTestCase.test_render_error_on_invalid_requisite\n raise SaltRenderError(\n 'Could not locate requisite of [{0}] present in state with name [{1}]'.format(\n req_key, chunk['name']))\n if not found:\n return 'unmet', ()\n fun_stats = set()\n for r_state, chunks in six.iteritems(reqs):\n req_stats = set()\n if r_state.startswith('prereq') and not r_state.startswith('prerequired'):\n run_dict = self.pre\n else:\n run_dict = running\n\n while True:\n if self.reconcile_procs(run_dict):\n break\n time.sleep(0.01)\n\n for chunk in chunks:\n tag = _gen_tag(chunk)\n if tag not in run_dict:\n req_stats.add('unmet')\n continue\n if r_state.startswith('onfail'):\n if run_dict[tag]['result'] is True:\n req_stats.add('onfail') # At least one state is OK\n continue\n else:\n if run_dict[tag]['result'] is False:\n req_stats.add('fail')\n continue\n if r_state.startswith('onchanges'):\n if not run_dict[tag]['changes']:\n req_stats.add('onchanges')\n else:\n req_stats.add('onchangesmet')\n continue\n if r_state.startswith('watch') and run_dict[tag]['changes']:\n req_stats.add('change')\n continue\n if r_state.startswith('prereq') and run_dict[tag]['result'] is None:\n if not r_state.startswith('prerequired'):\n req_stats.add('premet')\n if r_state.startswith('prereq') and not run_dict[tag]['result'] is None:\n if not r_state.startswith('prerequired'):\n req_stats.add('pre')\n else:\n if run_dict[tag].get('__state_ran__', True):\n req_stats.add('met')\n if r_state.endswith('_any') or r_state == 'onfail':\n if 'met' in req_stats or 'change' in req_stats:\n if 'fail' in req_stats:\n req_stats.remove('fail')\n if 'onchangesmet' in req_stats:\n if 'onchanges' in req_stats:\n req_stats.remove('onchanges')\n if 'fail' in req_stats:\n req_stats.remove('fail')\n if 'onfail' in req_stats:\n # a met requisite in this case implies a success\n if 'met' in req_stats:\n req_stats.remove('onfail')\n if r_state.endswith('_all'):\n if 'onfail' in req_stats:\n # a met requisite in this case implies a failure\n if 'met' in req_stats:\n req_stats.remove('met')\n fun_stats.update(req_stats)\n\n if 'unmet' in fun_stats:\n status = 'unmet'\n elif 'fail' in fun_stats:\n status = 'fail'\n elif 'pre' in fun_stats:\n if 'premet' in fun_stats:\n status = 'met'\n else:\n status = 'pre'\n elif 'onfail' in fun_stats and 'onchangesmet' not in fun_stats:\n status = 'onfail'\n elif 'onchanges' in fun_stats and 'onchangesmet' not in fun_stats:\n status = 'onchanges'\n elif 'change' in fun_stats:\n status = 'change'\n else:\n status = 'met'\n\n return status, reqs"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef event(self, chunk_ret, length, fire_event=False):\n '''\n Fire an event on the master bus\n\n If `fire_event` is set to True an event will be sent with the\n chunk name in the tag and the chunk result in the event data.\n\n If `fire_event` is set to a string such as `mystate/is/finished`,\n an event will be sent with the string added to the tag and the chunk\n result in the event data.\n\n If the `state_events` is set to True in the config, then after the\n chunk is evaluated an event will be set up to the master with the\n results.\n '''\n if not self.opts.get('local') and (self.opts.get('state_events', True) or fire_event):\n if not self.opts.get('master_uri'):\n ev_func = lambda ret, tag, preload=None: salt.utils.event.get_master_event(\n self.opts, self.opts['sock_dir'], listen=False).fire_event(ret, tag)\n else:\n ev_func = self.functions['event.fire_master']\n\n ret = {'ret': chunk_ret}\n if fire_event is True:\n tag = salt.utils.event.tagify(\n [self.jid, self.opts['id'], six.text_type(chunk_ret['name'])], 'state_result'\n )\n elif isinstance(fire_event, six.string_types):\n tag = salt.utils.event.tagify(\n [self.jid, self.opts['id'], six.text_type(fire_event)], 'state_result'\n )\n else:\n tag = salt.utils.event.tagify(\n [self.jid, 'prog', self.opts['id'], six.text_type(chunk_ret['__run_num__'])], 'job'\n )\n ret['len'] = length\n preload = {'jid': self.jid}\n ev_func(ret, tag, preload=preload)", "response": "Fire an event on the master bus with the given chunk result."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef call_chunk(self, low, running, chunks):\n '''\n Check if a chunk has any requires, execute the requires and then\n the chunk\n '''\n low = self._mod_aggregate(low, running, chunks)\n self._mod_init(low)\n tag = _gen_tag(low)\n if not low.get('prerequired'):\n self.active.add(tag)\n requisites = ['require',\n 'require_any',\n 'watch',\n 'watch_any',\n 'prereq',\n 'onfail',\n 'onfail_any',\n 'onchanges',\n 'onchanges_any']\n if not low.get('__prereq__'):\n requisites.append('prerequired')\n status, reqs = self.check_requisite(low, running, chunks, pre=True)\n else:\n status, reqs = self.check_requisite(low, running, chunks)\n if status == 'unmet':\n lost = {}\n reqs = []\n for requisite in requisites:\n lost[requisite] = []\n if requisite not in low:\n continue\n for req in low[requisite]:\n if isinstance(req, six.string_types):\n req = {'id': req}\n req = trim_req(req)\n found = False\n req_key = next(iter(req))\n req_val = req[req_key]\n for chunk in chunks:\n if req_val is None:\n continue\n if req_key == 'sls':\n # Allow requisite tracking of entire sls files\n if fnmatch.fnmatch(chunk['__sls__'], req_val):\n if requisite == 'prereq':\n chunk['__prereq__'] = True\n reqs.append(chunk)\n found = True\n continue\n if (fnmatch.fnmatch(chunk['name'], req_val) or\n fnmatch.fnmatch(chunk['__id__'], req_val)):\n if req_key == 'id' or chunk['state'] == req_key:\n if requisite == 'prereq':\n chunk['__prereq__'] = True\n elif requisite == 'prerequired':\n chunk['__prerequired__'] = True\n reqs.append(chunk)\n found = True\n if not found:\n lost[requisite].append(req)\n if lost['require'] or lost['watch'] or lost['prereq'] \\\n or lost['onfail'] or lost['onchanges'] \\\n or lost.get('prerequired'):\n comment = 'The following requisites were not found:\\n'\n for requisite, lreqs in six.iteritems(lost):\n if not lreqs:\n continue\n comment += \\\n '{0}{1}:\\n'.format(' ' * 19, requisite)\n for lreq in lreqs:\n req_key = next(iter(lreq))\n req_val = lreq[req_key]\n comment += \\\n '{0}{1}: {2}\\n'.format(' ' * 23, req_key, req_val)\n if low.get('__prereq__'):\n run_dict = self.pre\n else:\n run_dict = running\n start_time, duration = _calculate_fake_duration()\n run_dict[tag] = {'changes': {},\n 'result': False,\n 'duration': duration,\n 'start_time': start_time,\n 'comment': comment,\n '__run_num__': self.__run_num,\n '__sls__': low['__sls__']}\n self.__run_num += 1\n self.event(run_dict[tag], len(chunks), fire_event=low.get('fire_event'))\n return running\n for chunk in reqs:\n # Check to see if the chunk has been run, only run it if\n # it has not been run already\n ctag = _gen_tag(chunk)\n if ctag not in running:\n if ctag in self.active:\n if chunk.get('__prerequired__'):\n # Prereq recusive, run this chunk with prereq on\n if tag not in self.pre:\n low['__prereq__'] = True\n self.pre[ctag] = self.call(low, chunks, running)\n return running\n else:\n return running\n elif ctag not in running:\n log.error('Recursive requisite found')\n running[tag] = {\n 'changes': {},\n 'result': False,\n 'comment': 'Recursive requisite found',\n '__run_num__': self.__run_num,\n '__sls__': low['__sls__']}\n self.__run_num += 1\n self.event(running[tag], len(chunks), fire_event=low.get('fire_event'))\n return running\n running = self.call_chunk(chunk, running, chunks)\n if self.check_failhard(chunk, running):\n running['__FAILHARD__'] = True\n return running\n if low.get('__prereq__'):\n status, reqs = self.check_requisite(low, running, chunks)\n self.pre[tag] = self.call(low, chunks, running)\n if not self.pre[tag]['changes'] and status == 'change':\n self.pre[tag]['changes'] = {'watch': 'watch'}\n self.pre[tag]['result'] = None\n else:\n running = self.call_chunk(low, running, chunks)\n if self.check_failhard(chunk, running):\n running['__FAILHARD__'] = True\n return running\n elif status == 'met':\n if low.get('__prereq__'):\n self.pre[tag] = self.call(low, chunks, running)\n else:\n running[tag] = self.call(low, chunks, running)\n elif status == 'fail':\n # if the requisite that failed was due to a prereq on this low state\n # show the normal error\n if tag in self.pre:\n running[tag] = self.pre[tag]\n running[tag]['__run_num__'] = self.__run_num\n running[tag]['__sls__'] = low['__sls__']\n # otherwise the failure was due to a requisite down the chain\n else:\n # determine what the requisite failures where, and return\n # a nice error message\n failed_requisites = set()\n # look at all requisite types for a failure\n for req_lows in six.itervalues(reqs):\n for req_low in req_lows:\n req_tag = _gen_tag(req_low)\n req_ret = self.pre.get(req_tag, running.get(req_tag))\n # if there is no run output for the requisite it\n # can't be the failure\n if req_ret is None:\n continue\n # If the result was False (not None) it was a failure\n if req_ret['result'] is False:\n # use SLS.ID for the key-- so its easier to find\n key = '{sls}.{_id}'.format(sls=req_low['__sls__'],\n _id=req_low['__id__'])\n failed_requisites.add(key)\n\n _cmt = 'One or more requisite failed: {0}'.format(\n ', '.join(six.text_type(i) for i in failed_requisites)\n )\n start_time, duration = _calculate_fake_duration()\n running[tag] = {\n 'changes': {},\n 'result': False,\n 'duration': duration,\n 'start_time': start_time,\n 'comment': _cmt,\n '__run_num__': self.__run_num,\n '__sls__': low['__sls__']\n }\n self.pre[tag] = running[tag]\n self.__run_num += 1\n elif status == 'change' and not low.get('__prereq__'):\n ret = self.call(low, chunks, running)\n if not ret['changes'] and not ret.get('skip_watch', False):\n low = low.copy()\n low['sfun'] = low['fun']\n low['fun'] = 'mod_watch'\n low['__reqs__'] = reqs\n ret = self.call(low, chunks, running)\n running[tag] = ret\n elif status == 'pre':\n start_time, duration = _calculate_fake_duration()\n pre_ret = {'changes': {},\n 'result': True,\n 'duration': duration,\n 'start_time': start_time,\n 'comment': 'No changes detected',\n '__run_num__': self.__run_num,\n '__sls__': low['__sls__']}\n running[tag] = pre_ret\n self.pre[tag] = pre_ret\n self.__run_num += 1\n elif status == 'onfail':\n start_time, duration = _calculate_fake_duration()\n running[tag] = {'changes': {},\n 'result': True,\n 'duration': duration,\n 'start_time': start_time,\n 'comment': 'State was not run because onfail req did not change',\n '__state_ran__': False,\n '__run_num__': self.__run_num,\n '__sls__': low['__sls__']}\n self.__run_num += 1\n elif status == 'onchanges':\n start_time, duration = _calculate_fake_duration()\n running[tag] = {'changes': {},\n 'result': True,\n 'duration': duration,\n 'start_time': start_time,\n 'comment': 'State was not run because none of the onchanges reqs changed',\n '__state_ran__': False,\n '__run_num__': self.__run_num,\n '__sls__': low['__sls__']}\n self.__run_num += 1\n else:\n if low.get('__prereq__'):\n self.pre[tag] = self.call(low, chunks, running)\n else:\n running[tag] = self.call(low, chunks, running)\n if tag in running:\n running[tag]['__saltfunc__'] = '{0}.{1}'.format(low['state'], low['fun'])\n self.event(running[tag], len(chunks), fire_event=low.get('fire_event'))\n return running", "response": "Check if a chunk has any requires execute the requires and then the then\n the chunk"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef call_listen(self, chunks, running):\n '''\n Find all of the listen routines and call the associated mod_watch runs\n '''\n listeners = []\n crefs = {}\n for chunk in chunks:\n crefs[(chunk['state'], chunk['__id__'], chunk['name'])] = chunk\n if 'listen' in chunk:\n listeners.append({(chunk['state'], chunk['__id__'], chunk['name']): chunk['listen']})\n if 'listen_in' in chunk:\n for l_in in chunk['listen_in']:\n for key, val in six.iteritems(l_in):\n listeners.append({(key, val, 'lookup'): [{chunk['state']: chunk['__id__']}]})\n mod_watchers = []\n errors = {}\n for l_dict in listeners:\n for key, val in six.iteritems(l_dict):\n for listen_to in val:\n if not isinstance(listen_to, dict):\n found = False\n for chunk in chunks:\n if chunk['__id__'] == listen_to or \\\n chunk['name'] == listen_to:\n listen_to = {chunk['state']: chunk['__id__']}\n found = True\n if not found:\n continue\n for lkey, lval in six.iteritems(listen_to):\n if not any(lkey == cref[0] and lval in cref for cref in crefs):\n rerror = {_l_tag(lkey, lval):\n {\n 'comment': 'Referenced state {0}: {1} does not exist'.format(lkey, lval),\n 'name': 'listen_{0}:{1}'.format(lkey, lval),\n 'result': False,\n 'changes': {}\n }}\n errors.update(rerror)\n continue\n to_tags = [\n _gen_tag(data) for cref, data in six.iteritems(crefs) if lkey == cref[0] and lval in cref\n ]\n for to_tag in to_tags:\n if to_tag not in running:\n continue\n if running[to_tag]['changes']:\n if not any(key[0] == cref[0] and key[1] in cref for cref in crefs):\n rerror = {_l_tag(key[0], key[1]):\n {'comment': 'Referenced state {0}: {1} does not exist'.format(key[0], key[1]),\n 'name': 'listen_{0}:{1}'.format(key[0], key[1]),\n 'result': False,\n 'changes': {}}}\n errors.update(rerror)\n continue\n\n new_chunks = [data for cref, data in six.iteritems(crefs) if key[0] == cref[0] and key[1] in cref]\n for chunk in new_chunks:\n low = chunk.copy()\n low['sfun'] = chunk['fun']\n low['fun'] = 'mod_watch'\n low['__id__'] = 'listener_{0}'.format(low['__id__'])\n for req in STATE_REQUISITE_KEYWORDS:\n if req in low:\n low.pop(req)\n mod_watchers.append(low)\n ret = self.call_chunks(mod_watchers)\n running.update(ret)\n for err in errors:\n errors[err]['__run_num__'] = self.__run_num\n self.__run_num += 1\n running.update(errors)\n return running", "response": "Find all of the listen routines and call the associated mod_watch runs."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef inject_default_call(self, high):\n '''\n Sets .call function to a state, if not there.\n\n :param high:\n :return:\n '''\n for chunk in high:\n state = high[chunk]\n if not isinstance(state, collections.Mapping):\n continue\n for state_ref in state:\n needs_default = True\n if not isinstance(state[state_ref], list):\n continue\n for argset in state[state_ref]:\n if isinstance(argset, six.string_types):\n needs_default = False\n break\n if needs_default:\n state[state_ref].insert(-1, '__call__')", "response": "Injects a default call function to a state if not there."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef call_high(self, high, orchestration_jid=None):\n '''\n Process a high data call and ensure the defined states.\n '''\n self.inject_default_call(high)\n errors = []\n # If there is extension data reconcile it\n high, ext_errors = self.reconcile_extend(high)\n errors.extend(ext_errors)\n errors.extend(self.verify_high(high))\n if errors:\n return errors\n high, req_in_errors = self.requisite_in(high)\n errors.extend(req_in_errors)\n high = self.apply_exclude(high)\n # Verify that the high data is structurally sound\n if errors:\n return errors\n # Compile and verify the raw chunks\n chunks = self.compile_high_data(high, orchestration_jid)\n\n # If there are extensions in the highstate, process them and update\n # the low data chunks\n if errors:\n return errors\n ret = self.call_chunks(chunks)\n ret = self.call_listen(chunks, ret)\n\n def _cleanup_accumulator_data():\n accum_data_path = os.path.join(\n get_accumulator_dir(self.opts['cachedir']),\n self.instance_id\n )\n try:\n os.remove(accum_data_path)\n log.debug('Deleted accumulator data file %s', accum_data_path)\n except OSError:\n log.debug('File %s does not exist, no need to cleanup', accum_data_path)\n _cleanup_accumulator_data()\n if self.jid is not None:\n pause_path = os.path.join(self.opts['cachedir'], 'state_pause', self.jid)\n if os.path.isfile(pause_path):\n try:\n os.remove(pause_path)\n except OSError:\n # File is not present, all is well\n pass\n\n return ret", "response": "Process a high data call and ensure the defined states."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef call_template(self, template):\n '''\n Enforce the states in a template\n '''\n high = compile_template(template,\n self.rend,\n self.opts['renderer'],\n self.opts['renderer_blacklist'],\n self.opts['renderer_whitelist'])\n if not high:\n return high\n high, errors = self.render_template(high, template)\n if errors:\n return errors\n return self.call_high(high)", "response": "Call the template and return the result."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nenforcing the states in a template pass the template as a string", "response": "def call_template_str(self, template):\n '''\n Enforce the states in a template, pass the template as a string\n '''\n high = compile_template_str(template,\n self.rend,\n self.opts['renderer'],\n self.opts['renderer_blacklist'],\n self.opts['renderer_whitelist'])\n if not high:\n return high\n high, errors = self.render_template(high, '<template-str>')\n if errors:\n return errors\n return self.call_high(high)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngenerating the options used by the high state object.", "response": "def __gen_opts(self, opts):\n '''\n The options used by the High State object are derived from options\n on the minion and the master, or just the minion if the high state\n call is entirely local.\n '''\n # If the state is intended to be applied locally, then the local opts\n # should have all of the needed data, otherwise overwrite the local\n # data items with data from the master\n if 'local_state' in opts:\n if opts['local_state']:\n return opts\n mopts = self.client.master_opts()\n if not isinstance(mopts, dict):\n # An error happened on the master\n opts['renderer'] = 'jinja|yaml'\n opts['failhard'] = False\n opts['state_top'] = salt.utils.url.create('top.sls')\n opts['nodegroups'] = {}\n opts['file_roots'] = {'base': [syspaths.BASE_FILE_ROOTS_DIR]}\n else:\n opts['renderer'] = mopts['renderer']\n opts['failhard'] = mopts.get('failhard', False)\n if mopts['state_top'].startswith('salt://'):\n opts['state_top'] = mopts['state_top']\n elif mopts['state_top'].startswith('/'):\n opts['state_top'] = salt.utils.url.create(mopts['state_top'][1:])\n else:\n opts['state_top'] = salt.utils.url.create(mopts['state_top'])\n opts['state_top_saltenv'] = mopts.get('state_top_saltenv', None)\n opts['nodegroups'] = mopts.get('nodegroups', {})\n opts['state_auto_order'] = mopts.get(\n 'state_auto_order',\n opts['state_auto_order'])\n opts['file_roots'] = mopts['file_roots']\n opts['top_file_merging_strategy'] = mopts.get('top_file_merging_strategy',\n opts.get('top_file_merging_strategy'))\n opts['env_order'] = mopts.get('env_order', opts.get('env_order', []))\n opts['default_top'] = mopts.get('default_top', opts.get('default_top'))\n opts['state_events'] = mopts.get('state_events')\n opts['state_aggregate'] = mopts.get('state_aggregate', opts.get('state_aggregate', False))\n opts['jinja_env'] = mopts.get('jinja_env', {})\n opts['jinja_sls_env'] = mopts.get('jinja_sls_env', {})\n opts['jinja_lstrip_blocks'] = mopts.get('jinja_lstrip_blocks', False)\n opts['jinja_trim_blocks'] = mopts.get('jinja_trim_blocks', False)\n return opts"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the file server environments out of the master options and the master client options.", "response": "def _get_envs(self):\n '''\n Pull the file server environments out of the master options\n '''\n envs = ['base']\n if 'file_roots' in self.opts:\n envs.extend([x for x in list(self.opts['file_roots'])\n if x not in envs])\n env_order = self.opts.get('env_order', [])\n # Remove duplicates while preserving the order\n members = set()\n env_order = [env for env in env_order if not (env in members or members.add(env))]\n client_envs = self.client.envs()\n if env_order and client_envs:\n return [env for env in env_order if env in client_envs]\n\n elif env_order:\n return env_order\n else:\n envs.extend([env for env in client_envs if env not in envs])\n return envs"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_tops(self):\n '''\n Gather the top files\n '''\n tops = DefaultOrderedDict(list)\n include = DefaultOrderedDict(list)\n done = DefaultOrderedDict(list)\n found = 0 # did we find any contents in the top files?\n # Gather initial top files\n merging_strategy = self.opts['top_file_merging_strategy']\n if merging_strategy == 'same' and not self.opts['saltenv']:\n if not self.opts['default_top']:\n raise SaltRenderError(\n 'top_file_merging_strategy set to \\'same\\', but no '\n 'default_top configuration option was set'\n )\n\n if self.opts['saltenv']:\n contents = self.client.cache_file(\n self.opts['state_top'],\n self.opts['saltenv']\n )\n if contents:\n found = 1\n tops[self.opts['saltenv']] = [\n compile_template(\n contents,\n self.state.rend,\n self.state.opts['renderer'],\n self.state.opts['renderer_blacklist'],\n self.state.opts['renderer_whitelist'],\n saltenv=self.opts['saltenv']\n )\n ]\n else:\n tops[self.opts['saltenv']] = [{}]\n\n else:\n found = 0\n state_top_saltenv = self.opts.get('state_top_saltenv', False)\n if state_top_saltenv \\\n and not isinstance(state_top_saltenv, six.string_types):\n state_top_saltenv = six.text_type(state_top_saltenv)\n\n for saltenv in [state_top_saltenv] if state_top_saltenv \\\n else self._get_envs():\n contents = self.client.cache_file(\n self.opts['state_top'],\n saltenv\n )\n if contents:\n found = found + 1\n tops[saltenv].append(\n compile_template(\n contents,\n self.state.rend,\n self.state.opts['renderer'],\n self.state.opts['renderer_blacklist'],\n self.state.opts['renderer_whitelist'],\n saltenv=saltenv\n )\n )\n else:\n tops[saltenv].append({})\n log.debug('No contents loaded for saltenv \\'%s\\'', saltenv)\n\n if found > 1 and merging_strategy == 'merge' and not self.opts.get('env_order', None):\n log.warning(\n 'top_file_merging_strategy is set to \\'%s\\' and '\n 'multiple top files were found. Merging order is not '\n 'deterministic, it may be desirable to either set '\n 'top_file_merging_strategy to \\'same\\' or use the '\n '\\'env_order\\' configuration parameter to specify the '\n 'merging order.', merging_strategy\n )\n\n if found == 0:\n log.debug(\n 'No contents found in top file. If this is not expected, '\n 'verify that the \\'file_roots\\' specified in \\'etc/master\\' '\n 'are accessible. The \\'file_roots\\' configuration is: %s',\n repr(self.state.opts['file_roots'])\n )\n\n # Search initial top files for includes\n for saltenv, ctops in six.iteritems(tops):\n for ctop in ctops:\n if 'include' not in ctop:\n continue\n for sls in ctop['include']:\n include[saltenv].append(sls)\n ctop.pop('include')\n # Go through the includes and pull out the extra tops and add them\n while include:\n pops = []\n for saltenv, states in six.iteritems(include):\n pops.append(saltenv)\n if not states:\n continue\n for sls_match in states:\n for sls in fnmatch.filter(self.avail[saltenv], sls_match):\n if sls in done[saltenv]:\n continue\n tops[saltenv].append(\n compile_template(\n self.client.get_state(\n sls,\n saltenv\n ).get('dest', False),\n self.state.rend,\n self.state.opts['renderer'],\n self.state.opts['renderer_blacklist'],\n self.state.opts['renderer_whitelist'],\n saltenv\n )\n )\n done[saltenv].append(sls)\n for saltenv in pops:\n if saltenv in include:\n include.pop(saltenv)\n return tops", "response": "Gather the top files and their contents"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _merge_tops_merge(self, tops):\n '''\n The default merging strategy. The base env is authoritative, so it is\n checked first, followed by the remaining environments. In top files\n from environments other than \"base\", only the section matching the\n environment from the top file will be considered, and it too will be\n ignored if that environment was defined in the \"base\" top file.\n '''\n top = DefaultOrderedDict(OrderedDict)\n\n # Check base env first as it is authoritative\n base_tops = tops.pop('base', DefaultOrderedDict(OrderedDict))\n for ctop in base_tops:\n for saltenv, targets in six.iteritems(ctop):\n if saltenv == 'include':\n continue\n try:\n for tgt in targets:\n top[saltenv][tgt] = ctop[saltenv][tgt]\n except TypeError:\n raise SaltRenderError('Unable to render top file. No targets found.')\n\n for cenv, ctops in six.iteritems(tops):\n for ctop in ctops:\n for saltenv, targets in six.iteritems(ctop):\n if saltenv == 'include':\n continue\n elif saltenv != cenv:\n log.debug(\n 'Section for saltenv \\'%s\\' in the \\'%s\\' '\n 'saltenv\\'s top file will be ignored, as the '\n 'top_file_merging_strategy is set to \\'merge\\' '\n 'and the saltenvs do not match',\n saltenv, cenv\n )\n continue\n elif saltenv in top:\n log.debug(\n 'Section for saltenv \\'%s\\' in the \\'%s\\' '\n 'saltenv\\'s top file will be ignored, as this '\n 'saltenv was already defined in the \\'base\\' top '\n 'file', saltenv, cenv\n )\n continue\n try:\n for tgt in targets:\n top[saltenv][tgt] = ctop[saltenv][tgt]\n except TypeError:\n raise SaltRenderError('Unable to render top file. No targets found.')\n return top", "response": "Internal method to merge the top files into a single top file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _merge_tops_same(self, tops):\n '''\n For each saltenv, only consider the top file from that saltenv. All\n sections matching a given saltenv, which appear in a different\n saltenv's top file, will be ignored.\n '''\n top = DefaultOrderedDict(OrderedDict)\n for cenv, ctops in six.iteritems(tops):\n if all([x == {} for x in ctops]):\n # No top file found in this env, check the default_top\n default_top = self.opts['default_top']\n fallback_tops = tops.get(default_top, [])\n if all([x == {} for x in fallback_tops]):\n # Nothing in the fallback top file\n log.error(\n 'The \\'%s\\' saltenv has no top file, and the fallback '\n 'saltenv specified by default_top (%s) also has no '\n 'top file', cenv, default_top\n )\n continue\n\n for ctop in fallback_tops:\n for saltenv, targets in six.iteritems(ctop):\n if saltenv != cenv:\n continue\n log.debug(\n 'The \\'%s\\' saltenv has no top file, using the '\n 'default_top saltenv (%s)', cenv, default_top\n )\n for tgt in targets:\n top[saltenv][tgt] = ctop[saltenv][tgt]\n break\n else:\n log.error(\n 'The \\'%s\\' saltenv has no top file, and no '\n 'matches were found in the top file for the '\n 'default_top saltenv (%s)', cenv, default_top\n )\n\n continue\n\n else:\n for ctop in ctops:\n for saltenv, targets in six.iteritems(ctop):\n if saltenv == 'include':\n continue\n elif saltenv != cenv:\n log.debug(\n 'Section for saltenv \\'%s\\' in the \\'%s\\' '\n 'saltenv\\'s top file will be ignored, as the '\n 'top_file_merging_strategy is set to \\'same\\' '\n 'and the saltenvs do not match',\n saltenv, cenv\n )\n continue\n\n try:\n for tgt in targets:\n top[saltenv][tgt] = ctop[saltenv][tgt]\n except TypeError:\n raise SaltRenderError('Unable to render top file. No targets found.')\n return top", "response": "Merge the top files from the given tops with the same environment."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _merge_tops_merge_all(self, tops):\n '''\n Merge the top files into a single dictionary\n '''\n def _read_tgt(tgt):\n match_type = None\n states = []\n for item in tgt:\n if isinstance(item, dict):\n match_type = item\n if isinstance(item, six.string_types):\n states.append(item)\n return match_type, states\n\n top = DefaultOrderedDict(OrderedDict)\n for ctops in six.itervalues(tops):\n for ctop in ctops:\n for saltenv, targets in six.iteritems(ctop):\n if saltenv == 'include':\n continue\n try:\n for tgt in targets:\n if tgt not in top[saltenv]:\n top[saltenv][tgt] = ctop[saltenv][tgt]\n continue\n m_type1, m_states1 = _read_tgt(top[saltenv][tgt])\n m_type2, m_states2 = _read_tgt(ctop[saltenv][tgt])\n merged = []\n match_type = m_type2 or m_type1\n if match_type is not None:\n merged.append(match_type)\n merged.extend(m_states1)\n merged.extend([x for x in m_states2 if x not in merged])\n top[saltenv][tgt] = merged\n except TypeError:\n raise SaltRenderError('Unable to render top file. No targets found.')\n return top", "response": "Merge the top files into a single dictionary."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef verify_tops(self, tops):\n '''\n Verify the contents of the top file data\n '''\n errors = []\n if not isinstance(tops, dict):\n errors.append('Top data was not formed as a dict')\n # No further checks will work, bail out\n return errors\n for saltenv, matches in six.iteritems(tops):\n if saltenv == 'include':\n continue\n if not isinstance(saltenv, six.string_types):\n errors.append(\n 'Environment {0} in top file is not formed as a '\n 'string'.format(saltenv)\n )\n if saltenv == '':\n errors.append('Empty saltenv statement in top file')\n if not isinstance(matches, dict):\n errors.append(\n 'The top file matches for saltenv {0} are not '\n 'formatted as a dict'.format(saltenv)\n )\n for slsmods in six.itervalues(matches):\n if not isinstance(slsmods, list):\n errors.append('Malformed topfile (state declarations not '\n 'formed as a list)')\n continue\n for slsmod in slsmods:\n if isinstance(slsmod, dict):\n # This value is a match option\n for val in six.itervalues(slsmod):\n if not val:\n errors.append(\n 'Improperly formatted top file matcher '\n 'in saltenv {0}: {1} file'.format(\n slsmod,\n val\n )\n )\n elif isinstance(slsmod, six.string_types):\n # This is a sls module\n if not slsmod:\n errors.append(\n 'Environment {0} contains an empty sls '\n 'index'.format(saltenv)\n )\n\n return errors", "response": "Verify the contents of the top file data."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the high data derived from the top file", "response": "def get_top(self):\n '''\n Returns the high data derived from the top file\n '''\n try:\n tops = self.get_tops()\n except SaltRenderError as err:\n log.error('Unable to render top file: %s', err.error)\n return {}\n return self.merge_tops(tops)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsearches through the top high data for matches and return the states that this minion needs to execute.", "response": "def top_matches(self, top):\n '''\n Search through the top high data for matches and return the states\n that this minion needs to execute.\n\n Returns:\n {'saltenv': ['state1', 'state2', ...]}\n '''\n matches = DefaultOrderedDict(OrderedDict)\n # pylint: disable=cell-var-from-loop\n for saltenv, body in six.iteritems(top):\n if self.opts['saltenv']:\n if saltenv != self.opts['saltenv']:\n continue\n for match, data in six.iteritems(body):\n def _filter_matches(_match, _data, _opts):\n if isinstance(_data, six.string_types):\n _data = [_data]\n if self.matchers['confirm_top.confirm_top'](\n _match,\n _data,\n _opts\n ):\n if saltenv not in matches:\n matches[saltenv] = []\n for item in _data:\n if 'subfilter' in item:\n _tmpdata = item.pop('subfilter')\n for match, data in six.iteritems(_tmpdata):\n _filter_matches(match, data, _opts)\n if isinstance(item, six.string_types):\n matches[saltenv].append(item)\n elif isinstance(item, dict):\n env_key, inc_sls = item.popitem()\n if env_key not in self.avail:\n continue\n if env_key not in matches:\n matches[env_key] = []\n matches[env_key].append(inc_sls)\n _filter_matches(match, data, self.opts['nodegroups'])\n ext_matches = self._master_tops()\n for saltenv in ext_matches:\n top_file_matches = matches.get(saltenv, [])\n if self.opts.get('master_tops_first'):\n first = ext_matches[saltenv]\n second = top_file_matches\n else:\n first = top_file_matches\n second = ext_matches[saltenv]\n matches[saltenv] = first + [x for x in second if x not in first]\n\n # pylint: enable=cell-var-from-loop\n return matches"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nloading the dynamic modules from the given matches.", "response": "def load_dynamic(self, matches):\n '''\n If autoload_dynamic_modules is True then automatically load the\n dynamic modules\n '''\n if not self.opts['autoload_dynamic_modules']:\n return\n syncd = self.state.functions['saltutil.sync_all'](list(matches),\n refresh=False)\n if syncd['grains']:\n self.opts['grains'] = salt.loader.grains(self.opts)\n self.state.opts['pillar'] = self.state._gather_pillar()\n self.state.module_refresh()"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nrendering a state file and retrieve all of the include states", "response": "def render_state(self, sls, saltenv, mods, matches, local=False):\n '''\n Render a state file and retrieve all of the include states\n '''\n errors = []\n if not local:\n state_data = self.client.get_state(sls, saltenv)\n fn_ = state_data.get('dest', False)\n else:\n fn_ = sls\n if not os.path.isfile(fn_):\n errors.append(\n 'Specified SLS {0} on local filesystem cannot '\n 'be found.'.format(sls)\n )\n state = None\n if not fn_:\n errors.append(\n 'Specified SLS {0} in saltenv {1} is not '\n 'available on the salt master or through a configured '\n 'fileserver'.format(sls, saltenv)\n )\n else:\n try:\n state = compile_template(fn_,\n self.state.rend,\n self.state.opts['renderer'],\n self.state.opts['renderer_blacklist'],\n self.state.opts['renderer_whitelist'],\n saltenv,\n sls,\n rendered_sls=mods\n )\n except SaltRenderError as exc:\n msg = 'Rendering SLS \\'{0}:{1}\\' failed: {2}'.format(\n saltenv, sls, exc\n )\n log.critical(msg)\n errors.append(msg)\n except Exception as exc:\n msg = 'Rendering SLS {0} failed, render error: {1}'.format(\n sls, exc\n )\n log.critical(\n msg,\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n errors.append('{0}\\n{1}'.format(msg, traceback.format_exc()))\n try:\n mods.add('{0}:{1}'.format(saltenv, sls))\n except AttributeError:\n pass\n\n if state:\n if not isinstance(state, dict):\n errors.append(\n 'SLS {0} does not render to a dictionary'.format(sls)\n )\n else:\n include = []\n if 'include' in state:\n if not isinstance(state['include'], list):\n err = ('Include Declaration in SLS {0} is not formed '\n 'as a list'.format(sls))\n errors.append(err)\n else:\n include = state.pop('include')\n\n self._handle_extend(state, sls, saltenv, errors)\n self._handle_exclude(state, sls, saltenv, errors)\n self._handle_state_decls(state, sls, saltenv, errors)\n\n for inc_sls in include:\n # inc_sls may take the form of:\n # 'sls.to.include' <- same as {<saltenv>: 'sls.to.include'}\n # {<env_key>: 'sls.to.include'}\n # {'_xenv': 'sls.to.resolve'}\n xenv_key = '_xenv'\n\n if isinstance(inc_sls, dict):\n env_key, inc_sls = inc_sls.popitem()\n else:\n env_key = saltenv\n\n if env_key not in self.avail:\n msg = ('Nonexistent saltenv \\'{0}\\' found in include '\n 'of \\'{1}\\' within SLS \\'{2}:{3}\\''\n .format(env_key, inc_sls, saltenv, sls))\n log.error(msg)\n errors.append(msg)\n continue\n\n if inc_sls.startswith('.'):\n match = re.match(r'^(\\.+)(.*)$', inc_sls)\n if match:\n levels, include = match.groups()\n else:\n msg = ('Badly formatted include {0} found in include '\n 'in SLS \\'{2}:{3}\\''\n .format(inc_sls, saltenv, sls))\n log.error(msg)\n errors.append(msg)\n continue\n level_count = len(levels)\n p_comps = sls.split('.')\n if state_data.get('source', '').endswith('/init.sls'):\n p_comps.append('init')\n if level_count > len(p_comps):\n msg = ('Attempted relative include of \\'{0}\\' '\n 'within SLS \\'{1}:{2}\\' '\n 'goes beyond top level package '\n .format(inc_sls, saltenv, sls))\n log.error(msg)\n errors.append(msg)\n continue\n inc_sls = '.'.join(p_comps[:-level_count] + [include])\n\n if env_key != xenv_key:\n if matches is None:\n matches = []\n # Resolve inc_sls in the specified environment\n if env_key in matches or fnmatch.filter(self.avail[env_key], inc_sls):\n resolved_envs = [env_key]\n else:\n resolved_envs = []\n else:\n # Resolve inc_sls in the subset of environment matches\n resolved_envs = [\n aenv for aenv in matches\n if fnmatch.filter(self.avail[aenv], inc_sls)\n ]\n\n # An include must be resolved to a single environment, or\n # the include must exist in the current environment\n if len(resolved_envs) == 1 or saltenv in resolved_envs:\n # Match inc_sls against the available states in the\n # resolved env, matching wildcards in the process. If\n # there were no matches, then leave inc_sls as the\n # target so that the next recursion of render_state\n # will recognize the error.\n sls_targets = fnmatch.filter(\n self.avail[saltenv],\n inc_sls\n ) or [inc_sls]\n\n for sls_target in sls_targets:\n r_env = resolved_envs[0] if len(resolved_envs) == 1 else saltenv\n mod_tgt = '{0}:{1}'.format(r_env, sls_target)\n if mod_tgt not in mods:\n nstate, err = self.render_state(\n sls_target,\n r_env,\n mods,\n matches\n )\n if nstate:\n self.merge_included_states(state, nstate, errors)\n state.update(nstate)\n if err:\n errors.extend(err)\n else:\n msg = ''\n if not resolved_envs:\n msg = ('Unknown include: Specified SLS {0}: {1} is not available on the salt '\n 'master in saltenv(s): {2} '\n ).format(env_key,\n inc_sls,\n ', '.join(matches) if env_key == xenv_key else env_key)\n elif len(resolved_envs) > 1:\n msg = ('Ambiguous include: Specified SLS {0}: {1} is available on the salt master '\n 'in multiple available saltenvs: {2}'\n ).format(env_key,\n inc_sls,\n ', '.join(resolved_envs))\n log.critical(msg)\n errors.append(msg)\n try:\n self._handle_iorder(state)\n except TypeError:\n log.critical('Could not render SLS %s. Syntax error detected.', sls)\n else:\n state = {}\n return state, errors"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _handle_iorder(self, state):\n '''\n Take a state and apply the iorder system\n '''\n if self.opts['state_auto_order']:\n for name in state:\n for s_dec in state[name]:\n if not isinstance(s_dec, six.string_types):\n # PyDSL OrderedDict?\n continue\n\n if not isinstance(state[name], dict):\n # Include's or excludes as lists?\n continue\n if not isinstance(state[name][s_dec], list):\n # Bad syntax, let the verify seq pick it up later on\n continue\n\n found = False\n if s_dec.startswith('_'):\n continue\n\n for arg in state[name][s_dec]:\n if isinstance(arg, dict):\n if arg:\n if next(six.iterkeys(arg)) == 'order':\n found = True\n if not found:\n if not isinstance(state[name][s_dec], list):\n # quite certainly a syntax error, managed elsewhere\n continue\n state[name][s_dec].append(\n {'order': self.iorder}\n )\n self.iorder += 1\n return state", "response": "Take a state and apply the iorder system\nAttributeNames"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _handle_state_decls(self, state, sls, saltenv, errors):\n '''\n Add sls and saltenv components to the state\n '''\n for name in state:\n if not isinstance(state[name], dict):\n if name == '__extend__':\n continue\n if name == '__exclude__':\n continue\n\n if isinstance(state[name], six.string_types):\n # Is this is a short state, it needs to be padded\n if '.' in state[name]:\n comps = state[name].split('.')\n state[name] = {'__sls__': sls,\n '__env__': saltenv,\n comps[0]: [comps[1]]}\n continue\n errors.append(\n 'ID {0} in SLS {1} is not a dictionary'.format(name, sls)\n )\n continue\n skeys = set()\n for key in list(state[name]):\n if key.startswith('_'):\n continue\n if not isinstance(state[name][key], list):\n continue\n if '.' in key:\n comps = key.split('.')\n # Salt doesn't support state files such as:\n #\n # /etc/redis/redis.conf:\n # file.managed:\n # - source: salt://redis/redis.conf\n # - user: redis\n # - group: redis\n # - mode: 644\n # file.comment:\n # - regex: ^requirepass\n if comps[0] in skeys:\n errors.append(\n 'ID \\'{0}\\' in SLS \\'{1}\\' contains multiple state '\n 'declarations of the same type'.format(name, sls)\n )\n continue\n state[name][comps[0]] = state[name].pop(key)\n state[name][comps[0]].append(comps[1])\n skeys.add(comps[0])\n continue\n skeys.add(key)\n if '__sls__' not in state[name]:\n state[name]['__sls__'] = sls\n if '__env__' not in state[name]:\n state[name]['__env__'] = saltenv", "response": "Handle the state of the state files"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nhandle the extend dec out of the state and apply to the highstate global dec", "response": "def _handle_extend(self, state, sls, saltenv, errors):\n '''\n Take the extend dec out of state and apply to the highstate global\n dec\n '''\n if 'extend' in state:\n ext = state.pop('extend')\n if not isinstance(ext, dict):\n errors.append(('Extension value in SLS \\'{0}\\' is not a '\n 'dictionary').format(sls))\n return\n for name in ext:\n if not isinstance(ext[name], dict):\n errors.append(('Extension name \\'{0}\\' in SLS \\'{1}\\' is '\n 'not a dictionary'\n .format(name, sls)))\n continue\n if '__sls__' not in ext[name]:\n ext[name]['__sls__'] = sls\n if '__env__' not in ext[name]:\n ext[name]['__env__'] = saltenv\n for key in list(ext[name]):\n if key.startswith('_'):\n continue\n if not isinstance(ext[name][key], list):\n continue\n if '.' in key:\n comps = key.split('.')\n ext[name][comps[0]] = ext[name].pop(key)\n ext[name][comps[0]].append(comps[1])\n state.setdefault('__extend__', []).append(ext)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _handle_exclude(self, state, sls, saltenv, errors):\n '''\n Take the exclude dec out of the state and apply it to the highstate\n global dec\n '''\n if 'exclude' in state:\n exc = state.pop('exclude')\n if not isinstance(exc, list):\n err = ('Exclude Declaration in SLS {0} is not formed '\n 'as a list'.format(sls))\n errors.append(err)\n state.setdefault('__exclude__', []).extend(exc)", "response": "Handle the exclude declaration in the state and add it to the highstate\nAttributeNames global dec\nAttributeNames"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngathers the state files and render them into a single unified salt high data structure.", "response": "def render_highstate(self, matches):\n '''\n Gather the state files and render them into a single unified salt\n high data structure.\n '''\n highstate = self.building_highstate\n all_errors = []\n mods = set()\n statefiles = []\n for saltenv, states in six.iteritems(matches):\n for sls_match in states:\n if saltenv in self.avail:\n statefiles = fnmatch.filter(self.avail[saltenv], sls_match)\n elif '__env__' in self.avail:\n statefiles = fnmatch.filter(self.avail['__env__'], sls_match)\n else:\n all_errors.append(\n 'No matching salt environment for environment '\n '\\'{0}\\' found'.format(saltenv)\n )\n # if we did not found any sls in the fileserver listing, this\n # may be because the sls was generated or added later, we can\n # try to directly execute it, and if it fails, anyway it will\n # return the former error\n if not statefiles:\n statefiles = [sls_match]\n\n for sls in statefiles:\n r_env = '{0}:{1}'.format(saltenv, sls)\n if r_env in mods:\n continue\n state, errors = self.render_state(\n sls, saltenv, mods, matches)\n if state:\n self.merge_included_states(highstate, state, errors)\n for i, error in enumerate(errors[:]):\n if 'is not available' in error:\n # match SLS foobar in environment\n this_sls = 'SLS {0} in saltenv'.format(\n sls_match)\n if this_sls in error:\n errors[i] = (\n 'No matching sls found for \\'{0}\\' '\n 'in env \\'{1}\\''.format(sls_match, saltenv))\n all_errors.extend(errors)\n\n self.clean_duplicate_extends(highstate)\n return highstate, all_errors"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _check_pillar(self, force=False):\n '''\n Check the pillar for errors, refuse to run the state if there are\n errors in the pillar and return the pillar errors\n '''\n if force:\n return True\n if '_errors' in self.state.opts['pillar']:\n return False\n return True", "response": "Check the pillar for errors and return the pillar errors"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef matches_whitelist(self, matches, whitelist):\n '''\n Reads over the matches and returns a matches dict with just the ones\n that are in the whitelist\n '''\n if not whitelist:\n return matches\n ret_matches = {}\n if not isinstance(whitelist, list):\n whitelist = whitelist.split(',')\n for env in matches:\n for sls in matches[env]:\n if sls in whitelist:\n ret_matches[env] = ret_matches[env] if env in ret_matches else []\n ret_matches[env].append(sls)\n return ret_matches", "response": "Reads over the matches and returns a matches dict with just the ones\n that are in the whitelist\n "} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nexecutes the salt highstate for this minion", "response": "def call_highstate(self, exclude=None, cache=None, cache_name='highstate',\n force=False, whitelist=None, orchestration_jid=None):\n '''\n Run the sequence to execute the salt highstate for this minion\n '''\n # Check that top file exists\n tag_name = 'no_|-states_|-states_|-None'\n ret = {tag_name: {\n 'result': False,\n 'comment': 'No states found for this minion',\n 'name': 'No States',\n 'changes': {},\n '__run_num__': 0,\n }}\n cfn = os.path.join(\n self.opts['cachedir'],\n '{0}.cache.p'.format(cache_name)\n )\n\n if cache:\n if os.path.isfile(cfn):\n with salt.utils.files.fopen(cfn, 'rb') as fp_:\n high = self.serial.load(fp_)\n return self.state.call_high(high, orchestration_jid)\n # File exists so continue\n err = []\n try:\n top = self.get_top()\n except SaltRenderError as err:\n ret[tag_name]['comment'] = 'Unable to render top file: '\n ret[tag_name]['comment'] += six.text_type(err.error)\n return ret\n except Exception:\n trb = traceback.format_exc()\n err.append(trb)\n return err\n err += self.verify_tops(top)\n matches = self.top_matches(top)\n if not matches:\n msg = ('No Top file or master_tops data matches found. Please see '\n 'master log for details.')\n ret[tag_name]['comment'] = msg\n return ret\n matches = self.matches_whitelist(matches, whitelist)\n self.load_dynamic(matches)\n if not self._check_pillar(force):\n err += ['Pillar failed to render with the following messages:']\n err += self.state.opts['pillar']['_errors']\n else:\n high, errors = self.render_highstate(matches)\n if exclude:\n if isinstance(exclude, six.string_types):\n exclude = exclude.split(',')\n if '__exclude__' in high:\n high['__exclude__'].extend(exclude)\n else:\n high['__exclude__'] = exclude\n err += errors\n if err:\n return err\n if not high:\n return ret\n with salt.utils.files.set_umask(0o077):\n try:\n if salt.utils.platform.is_windows():\n # Make sure cache file isn't read-only\n self.state.functions['cmd.run'](\n ['attrib', '-R', cfn],\n python_shell=False,\n output_loglevel='quiet')\n with salt.utils.files.fopen(cfn, 'w+b') as fp_:\n try:\n self.serial.dump(high, fp_)\n except TypeError:\n # Can't serialize pydsl\n pass\n except (IOError, OSError):\n log.error('Unable to write to \"state.highstate\" cache file %s', cfn)\n\n return self.state.call_high(high, orchestration_jid)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn just the highstate or the errors", "response": "def compile_highstate(self):\n '''\n Return just the highstate or the errors\n '''\n err = []\n top = self.get_top()\n err += self.verify_tops(top)\n matches = self.top_matches(top)\n high, errors = self.render_highstate(matches)\n err += errors\n\n if err:\n return err\n\n return high"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncompile the state usage for the minion based on the top match data", "response": "def compile_state_usage(self):\n '''\n Return all used and unused states for the minion based on the top match data\n '''\n err = []\n top = self.get_top()\n err += self.verify_tops(top)\n\n if err:\n return err\n\n matches = self.top_matches(top)\n state_usage = {}\n\n for saltenv, states in self.avail.items():\n env_usage = {\n 'used': [],\n 'unused': [],\n 'count_all': 0,\n 'count_used': 0,\n 'count_unused': 0\n }\n\n env_matches = matches.get(saltenv)\n\n for state in states:\n env_usage['count_all'] += 1\n if state in env_matches:\n env_usage['count_used'] += 1\n env_usage['used'].append(state)\n else:\n env_usage['count_unused'] += 1\n env_usage['unused'].append(state)\n\n state_usage[saltenv] = env_usage\n\n return state_usage"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nload the modules into the state", "response": "def load_modules(self, data=None, proxy=None):\n '''\n Load the modules into the state\n '''\n log.info('Loading fresh modules for state activity')\n # Load a modified client interface that looks like the interface used\n # from the minion, but uses remote execution\n #\n self.functions = salt.client.FunctionWrapper(\n self.opts,\n self.opts['id']\n )\n # Load the states, but they should not be used in this class apart\n # from inspection\n self.utils = salt.loader.utils(self.opts)\n self.serializers = salt.loader.serializers(self.opts)\n self.states = salt.loader.states(self.opts, self.functions, self.utils, self.serializers)\n self.rend = salt.loader.render(self.opts, self.functions, states=self.states, context=self.state_con)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the state data from the master", "response": "def compile_master(self):\n '''\n Return the state data from the master\n '''\n load = {'grains': self.grains,\n 'opts': self.opts,\n 'cmd': '_master_state'}\n try:\n return self.channel.send(load, tries=3, timeout=72000)\n except SaltReqTimeoutError:\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\noutputting the data in lines very nice for running commands", "response": "def output(data, **kwargs): # pylint: disable=unused-argument\n '''\n Output the data in lines, very nice for running commands\n '''\n ret = ''\n if hasattr(data, 'keys'):\n for key in data:\n value = data[key]\n # Don't blow up on non-strings\n try:\n for line in value.splitlines():\n ret += '{0}: {1}\\n'.format(key, line)\n except AttributeError:\n ret += '{0}: {1}\\n'.format(key, value)\n else:\n try:\n ret += data + '\\n'\n except TypeError:\n # For non-dictionary, non-string data, just use print\n ret += '{0}\\n'.format(pprint.pformat(data))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef compare_and_update_config(config, update_config, changes, namespace=''):\n '''\n Recursively compare two configs, writing any needed changes to the\n update_config and capturing changes in the changes dict.\n '''\n if isinstance(config, dict):\n if not update_config:\n if config:\n # the updated config is more valid--report that we are using it\n changes[namespace] = {\n 'new': config,\n 'old': update_config,\n }\n return config\n elif not isinstance(update_config, dict):\n # new config is a dict, other isn't--new one wins\n changes[namespace] = {\n 'new': config,\n 'old': update_config,\n }\n return config\n else:\n # compare each key in the base config with the values in the\n # update_config, overwriting the values that are different but\n # keeping any that are not defined in config\n for key, value in six.iteritems(config):\n _namespace = key\n if namespace:\n _namespace = '{0}.{1}'.format(namespace, _namespace)\n update_config[key] = compare_and_update_config(\n value,\n update_config.get(key, None),\n changes,\n namespace=_namespace,\n )\n return update_config\n\n elif isinstance(config, list):\n if not update_config:\n if config:\n # the updated config is more valid--report that we are using it\n changes[namespace] = {\n 'new': config,\n 'old': update_config,\n }\n return config\n elif not isinstance(update_config, list):\n # new config is a list, other isn't--new one wins\n changes[namespace] = {\n 'new': config,\n 'old': update_config,\n }\n return config\n else:\n # iterate through config list, ensuring that each index in the\n # update_config list is the same\n for idx, item in enumerate(config):\n _namespace = '[{0}]'.format(idx)\n if namespace:\n _namespace = '{0}{1}'.format(namespace, _namespace)\n _update = None\n if len(update_config) > idx:\n _update = update_config[idx]\n if _update:\n update_config[idx] = compare_and_update_config(\n config[idx],\n _update,\n changes,\n namespace=_namespace,\n )\n else:\n changes[_namespace] = {\n 'new': config[idx],\n 'old': _update,\n }\n update_config.append(config[idx])\n\n if len(update_config) > len(config):\n # trim any items in update_config that are not in config\n for idx, old_item in enumerate(update_config):\n if idx < len(config):\n continue\n _namespace = '[{0}]'.format(idx)\n if namespace:\n _namespace = '{0}{1}'.format(namespace, _namespace)\n changes[_namespace] = {\n 'new': None,\n 'old': old_item,\n }\n del update_config[len(config):]\n return update_config\n\n else:\n if config != update_config:\n changes[namespace] = {\n 'new': config,\n 'old': update_config,\n }\n return config", "response": "Recursively compare two configs writing any needed changes to the update_config and capturing changes in the changes dict."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef managed(name,\n value=None,\n host=DEFAULT_HOST,\n port=DEFAULT_PORT,\n time=DEFAULT_TIME,\n min_compress_len=DEFAULT_MIN_COMPRESS_LEN):\n '''\n Manage a memcached key.\n\n name\n The key to manage\n\n value\n The value to set for that key\n\n host\n The memcached server IP address\n\n port\n The memcached server port\n\n\n .. code-block:: yaml\n\n foo:\n memcached.managed:\n - value: bar\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n try:\n cur = __salt__['memcached.get'](name, host, port)\n except CommandExecutionError as exc:\n ret['comment'] = six.text_type(exc)\n return ret\n\n if cur == value:\n ret['result'] = True\n ret['comment'] = 'Key \\'{0}\\' does not need to be updated'.format(name)\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n if cur is None:\n ret['comment'] = 'Key \\'{0}\\' would be added'.format(name)\n else:\n ret['comment'] = 'Value of key \\'{0}\\' would be changed'.format(name)\n return ret\n\n try:\n ret['result'] = __salt__['memcached.set'](\n name, value, host, port, time, min_compress_len\n )\n except (CommandExecutionError, SaltInvocationError) as exc:\n ret['comment'] = six.text_type(exc)\n else:\n if ret['result']:\n ret['comment'] = 'Successfully set key \\'{0}\\''.format(name)\n if cur is not None:\n ret['changes'] = {'old': cur, 'new': value}\n else:\n ret['changes'] = {'key added': name, 'value': value}\n else:\n ret['comment'] = 'Failed to set key \\'{0}\\''.format(name)\n return ret", "response": "Manage a memcached key."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef parse_hosts(hostsfile='/etc/hosts', hosts=None):\n '''\n Parse /etc/hosts file.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' dnsutil.parse_hosts\n '''\n if not hosts:\n try:\n with salt.utils.files.fopen(hostsfile, 'r') as fp_:\n hosts = salt.utils.stringutils.to_unicode(fp_.read())\n except Exception:\n return 'Error: hosts data was not found'\n\n hostsdict = {}\n for line in hosts.splitlines():\n if not line:\n continue\n if line.startswith('#'):\n continue\n comps = line.split()\n ip = comps[0]\n aliases = comps[1:]\n hostsdict.setdefault(ip, []).extend(aliases)\n\n return hostsdict", "response": "Parse the hosts file and return a dict of the hosts that are available in the hosts file."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef hosts_append(hostsfile='/etc/hosts', ip_addr=None, entries=None):\n '''\n Append a single line to the /etc/hosts file.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 ad1.yuk.co,ad2.yuk.co\n '''\n host_list = entries.split(',')\n hosts = parse_hosts(hostsfile=hostsfile)\n if ip_addr in hosts:\n for host in host_list:\n if host in hosts[ip_addr]:\n host_list.remove(host)\n\n if not host_list:\n return 'No additional hosts were added to {0}'.format(hostsfile)\n\n append_line = '\\n{0} {1}'.format(ip_addr, ' '.join(host_list))\n with salt.utils.files.fopen(hostsfile, 'a') as fp_:\n fp_.write(salt.utils.stringutils.to_str(append_line))\n\n return 'The following line was added to {0}:{1}'.format(hostsfile,\n append_line)", "response": "Append a single line to the hosts file."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef hosts_remove(hostsfile='/etc/hosts', entries=None):\n '''\n Remove a host from the /etc/hosts file. If doing so will leave a line\n containing only an IP address, then the line will be deleted. This function\n will leave comments and blank lines intact.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' dnsutil.hosts_remove /etc/hosts ad1.yuk.co\n salt '*' dnsutil.hosts_remove /etc/hosts ad2.yuk.co,ad1.yuk.co\n '''\n with salt.utils.files.fopen(hostsfile, 'r') as fp_:\n hosts = salt.utils.stringutils.to_unicode(fp_.read())\n\n host_list = entries.split(',')\n with salt.utils.files.fopen(hostsfile, 'w') as out_file:\n for line in hosts.splitlines():\n if not line or line.strip().startswith('#'):\n out_file.write(salt.utils.stringutils.to_str('{0}\\n'.format(line)))\n continue\n comps = line.split()\n for host in host_list:\n if host in comps[1:]:\n comps.remove(host)\n if len(comps) > 1:\n out_file.write(salt.utils.stringutils.to_str(' '.join(comps)))\n out_file.write(salt.utils.stringutils.to_str('\\n'))", "response": "Remove a host from the hosts file."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nparsing a zone file and returns a list of dicts.", "response": "def parse_zone(zonefile=None, zone=None):\n '''\n Parses a zone file. Can be passed raw zone data on the API level.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt ns1 dnsutil.parse_zone /var/lib/named/example.com.zone\n '''\n if zonefile:\n try:\n with salt.utils.files.fopen(zonefile, 'r') as fp_:\n zone = salt.utils.stringutils.to_unicode(fp_.read())\n except Exception:\n pass\n\n if not zone:\n return 'Error: Zone data was not found'\n\n zonedict = {}\n mode = 'single'\n for line in zone.splitlines():\n comps = line.split(';')\n line = comps[0].strip()\n if not line:\n continue\n comps = line.split()\n if line.startswith('$'):\n zonedict[comps[0].replace('$', '')] = comps[1]\n continue\n if '(' in line and ')' not in line:\n mode = 'multi'\n multi = ''\n if mode == 'multi':\n multi += ' {0}'.format(line)\n if ')' in line:\n mode = 'single'\n line = multi.replace('(', '').replace(')', '')\n else:\n continue\n if 'ORIGIN' in zonedict:\n comps = line.replace('@', zonedict['ORIGIN']).split()\n else:\n comps = line.split()\n if 'SOA' in line:\n if comps[1] != 'IN':\n comps.pop(1)\n zonedict['ORIGIN'] = comps[0]\n zonedict['NETWORK'] = comps[1]\n zonedict['SOURCE'] = comps[3]\n zonedict['CONTACT'] = comps[4].replace('.', '@', 1)\n zonedict['SERIAL'] = comps[5]\n zonedict['REFRESH'] = _to_seconds(comps[6])\n zonedict['RETRY'] = _to_seconds(comps[7])\n zonedict['EXPIRE'] = _to_seconds(comps[8])\n zonedict['MINTTL'] = _to_seconds(comps[9])\n continue\n if comps[0] == 'IN':\n comps.insert(0, zonedict['ORIGIN'])\n if not comps[0].endswith('.') and 'NS' not in line:\n comps[0] = '{0}.{1}'.format(comps[0], zonedict['ORIGIN'])\n if comps[2] == 'NS':\n zonedict.setdefault('NS', []).append(comps[3])\n elif comps[2] == 'MX':\n if 'MX' not in zonedict:\n zonedict.setdefault('MX', []).append({'priority': comps[3],\n 'host': comps[4]})\n elif comps[3] in ('A', 'AAAA'):\n zonedict.setdefault(comps[3], {})[comps[0]] = {\n 'TARGET': comps[4],\n 'TTL': comps[1],\n }\n else:\n zonedict.setdefault(comps[2], {})[comps[0]] = comps[3]\n return zonedict"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _to_seconds(timestr):\n '''\n Converts a time value to seconds.\n\n As per RFC1035 (page 45), max time is 1 week, so anything longer (or\n unreadable) will be set to one week (604800 seconds).\n '''\n timestr = timestr.upper()\n if 'H' in timestr:\n seconds = int(timestr.replace('H', '')) * 3600\n elif 'D' in timestr:\n seconds = int(timestr.replace('D', '')) * 86400\n elif 'W' in timestr:\n seconds = 604800\n else:\n try:\n seconds = int(timestr)\n except ValueError:\n seconds = 604800\n if seconds > 604800:\n seconds = 604800\n return seconds", "response": "Converts a time value to seconds."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the A record(s ) for host.", "response": "def A(host, nameserver=None):\n '''\n Return the A record(s) for ``host``.\n\n Always returns a list.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt ns1 dnsutil.A www.google.com\n '''\n if _has_dig():\n return __salt__['dig.A'](host, nameserver)\n elif nameserver is None:\n # fall back to the socket interface, if we don't care who resolves\n try:\n addresses = [sock[4][0] for sock in socket.getaddrinfo(host, None, socket.AF_INET, 0, socket.SOCK_RAW)]\n return addresses\n except socket.gaierror:\n return 'Unable to resolve {0}'.format(host)\n\n return 'This function requires dig, which is not currently available'"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the AAAA record for host.", "response": "def AAAA(host, nameserver=None):\n '''\n Return the AAAA record(s) for ``host``.\n\n Always returns a list.\n\n .. versionadded:: 2014.7.5\n\n CLI Example:\n\n .. code-block:: bash\n\n salt ns1 dnsutil.AAAA www.google.com\n '''\n if _has_dig():\n return __salt__['dig.AAAA'](host, nameserver)\n elif nameserver is None:\n # fall back to the socket interface, if we don't care who resolves\n try:\n addresses = [sock[4][0] for sock in socket.getaddrinfo(host, None, socket.AF_INET6, 0, socket.SOCK_RAW)]\n return addresses\n except socket.gaierror:\n return 'Unable to resolve {0}'.format(host)\n\n return 'This function requires dig, which is not currently available'"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef NS(domain, resolve=True, nameserver=None):\n '''\n Return a list of IPs of the nameservers for ``domain``\n\n If 'resolve' is False, don't resolve names.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt ns1 dnsutil.NS google.com\n\n '''\n if _has_dig():\n return __salt__['dig.NS'](domain, resolve, nameserver)\n\n return 'This function requires dig, which is not currently available'", "response": "Return a list of IPs of the nameserver for the given domain"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef MX(domain, resolve=False, nameserver=None):\n '''\n Return a list of lists for the MX of ``domain``.\n\n If the 'resolve' argument is True, resolve IPs for the servers.\n\n It's limited to one IP, because although in practice it's very rarely a\n round robin, it is an acceptable configuration and pulling just one IP lets\n the data be similar to the non-resolved version. If you think an MX has\n multiple IPs, don't use the resolver here, resolve them in a separate step.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt ns1 dnsutil.MX google.com\n '''\n if _has_dig():\n return __salt__['dig.MX'](domain, resolve, nameserver)\n\n return 'This function requires dig, which is not currently available'", "response": "Return a list of lists for the MX of the given domain."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef serial(zone='', update=False):\n '''\n Return, store and update a dns serial for your zone files.\n\n zone: a keyword for a specific zone\n\n update: store an updated version of the serial in a grain\n\n If ``update`` is False, the function will retrieve an existing serial or\n return the current date if no serial is stored. Nothing will be stored\n\n If ``update`` is True, the function will set the serial to the current date\n if none exist or if the existing serial is for a previous date. If a serial\n for greater than the current date is already stored, the function will\n increment it.\n\n This module stores the serial in a grain, you can explicitly set the\n stored value as a grain named ``dnsserial_<zone_name>``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt ns1 dnsutil.serial example.com\n '''\n grains = {}\n key = 'dnsserial'\n if zone:\n key += '_{0}'.format(zone)\n stored = __salt__['grains.get'](key=key)\n present = time.strftime('%Y%m%d01')\n if not update:\n return stored or present\n if stored and stored >= present:\n current = six.text_type(int(stored) + 1)\n else:\n current = present\n __salt__['grains.setval'](key=key, val=current)\n return current", "response": "Return a dns serial for your zone files."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nevaluates accepted keys and create a msgpack file which contains a list", "response": "def handle_key_cache(self):\n '''\n Evaluate accepted keys and create a msgpack file\n which contains a list\n '''\n if self.opts['key_cache'] == 'sched':\n keys = []\n #TODO DRY from CKMinions\n if self.opts['transport'] in ('zeromq', 'tcp'):\n acc = 'minions'\n else:\n acc = 'accepted'\n\n for fn_ in os.listdir(os.path.join(self.opts['pki_dir'], acc)):\n if not fn_.startswith('.') and os.path.isfile(os.path.join(self.opts['pki_dir'], acc, fn_)):\n keys.append(fn_)\n log.debug('Writing master key cache')\n # Write a temporary file securely\n if six.PY2:\n with salt.utils.atomicfile.atomic_open(os.path.join(self.opts['pki_dir'], acc, '.key_cache')) as cache_file:\n self.serial.dump(keys, cache_file)\n else:\n with salt.utils.atomicfile.atomic_open(os.path.join(self.opts['pki_dir'], acc, '.key_cache'), mode='wb') as cache_file:\n self.serial.dump(keys, cache_file)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nrotates the AES key rotation", "response": "def handle_key_rotate(self, now):\n '''\n Rotate the AES key rotation\n '''\n to_rotate = False\n dfn = os.path.join(self.opts['cachedir'], '.dfn')\n try:\n stats = os.stat(dfn)\n # Basic Windows permissions don't distinguish between\n # user/group/all. Check for read-only state instead.\n if salt.utils.platform.is_windows() and not os.access(dfn, os.W_OK):\n to_rotate = True\n # Cannot delete read-only files on Windows.\n os.chmod(dfn, stat.S_IRUSR | stat.S_IWUSR)\n elif stats.st_mode == 0o100400:\n to_rotate = True\n else:\n log.error('Found dropfile with incorrect permissions, ignoring...')\n os.remove(dfn)\n except os.error:\n pass\n\n if self.opts.get('publish_session'):\n if now - self.rotate >= self.opts['publish_session']:\n to_rotate = True\n\n if to_rotate:\n log.info('Rotating master AES key')\n for secret_key, secret_map in six.iteritems(SMaster.secrets):\n # should be unnecessary-- since no one else should be modifying\n with secret_map['secret'].get_lock():\n secret_map['secret'].value = salt.utils.stringutils.to_bytes(secret_map['reload']())\n self.event.fire_event({'rotate_{0}_key'.format(secret_key): True}, tag='key')\n self.rotate = now\n if self.opts.get('ping_on_rotate'):\n # Ping all minions to get them to pick up the new key\n log.debug('Pinging all connected minions '\n 'due to key rotation')\n salt.utils.master.ping_all_connected_minions(self.opts)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef handle_schedule(self):\n '''\n Evaluate the scheduler\n '''\n try:\n self.schedule.eval()\n # Check if scheduler requires lower loop interval than\n # the loop_interval setting\n if self.schedule.loop_interval < self.loop_interval:\n self.loop_interval = self.schedule.loop_interval\n except Exception as exc:\n log.error('Exception %s occurred in scheduled job', exc)", "response": "Evaluate the scheduler and update the loop_interval property"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef handle_presence(self, old_present):\n '''\n Fire presence events if enabled\n '''\n # On the first run it may need more time for the EventPublisher\n # to come up and be ready. Set the timeout to account for this.\n if self.presence_events and self.event.connect_pull(timeout=3):\n present = self.ckminions.connected_ids()\n new = present.difference(old_present)\n lost = old_present.difference(present)\n if new or lost:\n # Fire new minions present event\n data = {'new': list(new),\n 'lost': list(lost)}\n self.event.fire_event(data, tagify('change', 'presence'))\n data = {'present': list(present)}\n self.event.fire_event(data, tagify('present', 'presence'))\n old_present.clear()\n old_present.update(present)", "response": "Fire presence events if enabled\n is True"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef fill_buckets(self):\n '''\n Get the configured backends and the intervals for any backend which\n supports them, and set up the update \"buckets\". There will be one\n bucket for each thing being updated at a given interval.\n '''\n update_intervals = self.fileserver.update_intervals()\n self.buckets = {}\n for backend in self.fileserver.backends():\n fstr = '{0}.update'.format(backend)\n try:\n update_func = self.fileserver.servers[fstr]\n except KeyError:\n log.debug(\n 'No update function for the %s filserver backend',\n backend\n )\n continue\n if backend in update_intervals:\n # Variable intervals are supported for this backend\n for id_, interval in six.iteritems(update_intervals[backend]):\n if not interval:\n # Don't allow an interval of 0\n interval = DEFAULT_INTERVAL\n log.debug(\n 'An update_interval of 0 is not supported, '\n 'falling back to %s', interval\n )\n i_ptr = self.buckets.setdefault(interval, OrderedDict())\n # Backend doesn't technically need to be present in the\n # key, all we *really* need is the function reference, but\n # having it there makes it easier to provide meaningful\n # debug logging in the update threads.\n i_ptr.setdefault((backend, update_func), []).append(id_)\n else:\n # Variable intervals are not supported for this backend, so\n # fall back to the global interval for that fileserver. Since\n # this backend doesn't support variable updates, we have\n # nothing to pass to the backend's update func, so we'll just\n # set the value to None.\n try:\n interval_key = '{0}_update_interval'.format(backend)\n interval = self.opts[interval_key]\n except KeyError:\n interval = DEFAULT_INTERVAL\n log.warning(\n '%s key missing from configuration. Falling back to '\n 'default interval of %d seconds',\n interval_key, interval\n )\n self.buckets.setdefault(\n interval, OrderedDict())[(backend, update_func)] = None", "response": "Get the configured backends and intervals and set up the update buckets."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nthread target which handles all updates for a given interval", "response": "def update_fileserver(self, interval, backends):\n '''\n Threading target which handles all updates for a given wait interval\n '''\n def _do_update():\n log.debug(\n 'Performing fileserver updates for items with an update '\n 'interval of %d', interval\n )\n for backend, update_args in six.iteritems(backends):\n backend_name, update_func = backend\n try:\n if update_args:\n log.debug(\n 'Updating %s fileserver cache for the following '\n 'targets: %s', backend_name, update_args\n )\n args = (update_args,)\n else:\n log.debug('Updating %s fileserver cache', backend_name)\n args = ()\n\n update_func(*args)\n except Exception as exc:\n log.exception(\n 'Uncaught exception while updating %s fileserver '\n 'cache', backend_name\n )\n\n log.debug(\n 'Completed fileserver updates for items with an update '\n 'interval of %d, waiting %d seconds', interval, interval\n )\n\n condition = threading.Condition()\n _do_update()\n while True:\n with condition:\n condition.wait(interval)\n _do_update()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nstart the update threads", "response": "def run(self):\n '''\n Start the update threads\n '''\n salt.utils.process.appendproctitle(self.__class__.__name__)\n # Clean out the fileserver backend cache\n salt.daemons.masterapi.clean_fsbackend(self.opts)\n\n for interval in self.buckets:\n self.update_threads[interval] = threading.Thread(\n target=self.update_fileserver,\n args=(interval, self.buckets[interval]),\n )\n self.update_threads[interval].start()\n\n # Keep the process alive\n while True:\n time.sleep(60)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _pre_flight(self):\n '''\n Run pre flight checks. If anything in this method fails then the master\n should not start up.\n '''\n errors = []\n critical_errors = []\n\n try:\n os.chdir('/')\n except OSError as err:\n errors.append(\n 'Cannot change to root directory ({0})'.format(err)\n )\n\n if self.opts.get('fileserver_verify_config', True):\n # Avoid circular import\n import salt.fileserver\n fileserver = salt.fileserver.Fileserver(self.opts)\n if not fileserver.servers:\n errors.append(\n 'Failed to load fileserver backends, the configured backends '\n 'are: {0}'.format(', '.join(self.opts['fileserver_backend']))\n )\n else:\n # Run init() for all backends which support the function, to\n # double-check configuration\n try:\n fileserver.init()\n except salt.exceptions.FileserverConfigError as exc:\n critical_errors.append('{0}'.format(exc))\n\n if not self.opts['fileserver_backend']:\n errors.append('No fileserver backends are configured')\n\n # Check to see if we need to create a pillar cache dir\n if self.opts['pillar_cache'] and not os.path.isdir(os.path.join(self.opts['cachedir'], 'pillar_cache')):\n try:\n with salt.utils.files.set_umask(0o077):\n os.mkdir(os.path.join(self.opts['cachedir'], 'pillar_cache'))\n except OSError:\n pass\n\n if self.opts.get('git_pillar_verify_config', True):\n try:\n git_pillars = [\n x for x in self.opts.get('ext_pillar', [])\n if 'git' in x\n and not isinstance(x['git'], six.string_types)\n ]\n except TypeError:\n git_pillars = []\n critical_errors.append(\n 'Invalid ext_pillar configuration. It is likely that the '\n 'external pillar type was not specified for one or more '\n 'external pillars.'\n )\n if git_pillars:\n try:\n new_opts = copy.deepcopy(self.opts)\n import salt.pillar.git_pillar\n for repo in git_pillars:\n new_opts['ext_pillar'] = [repo]\n try:\n git_pillar = salt.utils.gitfs.GitPillar(\n new_opts,\n repo['git'],\n per_remote_overrides=salt.pillar.git_pillar.PER_REMOTE_OVERRIDES,\n per_remote_only=salt.pillar.git_pillar.PER_REMOTE_ONLY,\n global_only=salt.pillar.git_pillar.GLOBAL_ONLY)\n except salt.exceptions.FileserverConfigError as exc:\n critical_errors.append(exc.strerror)\n finally:\n del new_opts\n\n if errors or critical_errors:\n for error in errors:\n log.error(error)\n for error in critical_errors:\n log.critical(error)\n log.critical('Master failed pre flight checks, exiting\\n')\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)", "response": "Run pre flight checks."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nstart the master server.", "response": "def start(self):\n '''\n Turn on the master server components\n '''\n self._pre_flight()\n log.info('salt-master is starting as user \\'%s\\'', salt.utils.user.get_user())\n\n enable_sigusr1_handler()\n enable_sigusr2_handler()\n\n self.__set_max_open_files()\n\n # Reset signals to default ones before adding processes to the process\n # manager. We don't want the processes being started to inherit those\n # signal handlers\n with salt.utils.process.default_signals(signal.SIGINT, signal.SIGTERM):\n\n # Setup the secrets here because the PubServerChannel may need\n # them as well.\n SMaster.secrets['aes'] = {\n 'secret': multiprocessing.Array(\n ctypes.c_char,\n salt.utils.stringutils.to_bytes(\n salt.crypt.Crypticle.generate_key_string()\n )\n ),\n 'reload': salt.crypt.Crypticle.generate_key_string\n }\n log.info('Creating master process manager')\n # Since there are children having their own ProcessManager we should wait for kill more time.\n self.process_manager = salt.utils.process.ProcessManager(wait_for_kill=5)\n pub_channels = []\n log.info('Creating master publisher process')\n log_queue = salt.log.setup.get_multiprocessing_logging_queue()\n for _, opts in iter_transport_opts(self.opts):\n chan = salt.transport.server.PubServerChannel.factory(opts)\n chan.pre_fork(self.process_manager, kwargs={'log_queue': log_queue})\n pub_channels.append(chan)\n\n log.info('Creating master event publisher process')\n self.process_manager.add_process(salt.utils.event.EventPublisher, args=(self.opts,))\n\n if self.opts.get('reactor'):\n if isinstance(self.opts['engines'], list):\n rine = False\n for item in self.opts['engines']:\n if 'reactor' in item:\n rine = True\n break\n if not rine:\n self.opts['engines'].append({'reactor': {}})\n else:\n if 'reactor' not in self.opts['engines']:\n log.info('Enabling the reactor engine')\n self.opts['engines']['reactor'] = {}\n\n salt.engines.start_engines(self.opts, self.process_manager)\n\n # must be after channels\n log.info('Creating master maintenance process')\n self.process_manager.add_process(Maintenance, args=(self.opts,))\n\n if self.opts.get('event_return'):\n log.info('Creating master event return process')\n self.process_manager.add_process(salt.utils.event.EventReturn, args=(self.opts,))\n\n ext_procs = self.opts.get('ext_processes', [])\n for proc in ext_procs:\n log.info('Creating ext_processes process: %s', proc)\n try:\n mod = '.'.join(proc.split('.')[:-1])\n cls = proc.split('.')[-1]\n _tmp = __import__(mod, globals(), locals(), [cls], -1)\n cls = _tmp.__getattribute__(cls)\n self.process_manager.add_process(cls, args=(self.opts,))\n except Exception:\n log.error('Error creating ext_processes process: %s', proc)\n\n if HAS_HALITE and 'halite' in self.opts:\n log.info('Creating master halite process')\n self.process_manager.add_process(Halite, args=(self.opts['halite'],))\n\n # TODO: remove, or at least push into the transport stuff (pre-fork probably makes sense there)\n if self.opts['con_cache']:\n log.info('Creating master concache process')\n self.process_manager.add_process(salt.utils.master.ConnectedCache, args=(self.opts,))\n # workaround for issue #16315, race condition\n log.debug('Sleeping for two seconds to let concache rest')\n time.sleep(2)\n\n log.info('Creating master request server process')\n kwargs = {}\n if salt.utils.platform.is_windows():\n kwargs['log_queue'] = log_queue\n kwargs['log_queue_level'] = salt.log.setup.get_multiprocessing_logging_level()\n kwargs['secrets'] = SMaster.secrets\n\n self.process_manager.add_process(\n ReqServer,\n args=(self.opts, self.key, self.master_key),\n kwargs=kwargs,\n name='ReqServer')\n\n self.process_manager.add_process(\n FileserverUpdate,\n args=(self.opts,))\n\n # Fire up SSDP discovery publisher\n if self.opts['discovery']:\n if salt.utils.ssdp.SSDPDiscoveryServer.is_available():\n self.process_manager.add_process(salt.utils.ssdp.SSDPDiscoveryServer(\n port=self.opts['discovery']['port'],\n listen_ip=self.opts['interface'],\n answer={'mapping': self.opts['discovery'].get('mapping', {})}).run)\n else:\n log.error('Unable to load SSDP: asynchronous IO is not available.')\n if sys.version_info.major == 2:\n log.error('You are using Python 2, please install \"trollius\" module to enable SSDP discovery.')\n\n # Install the SIGINT/SIGTERM handlers if not done so far\n if signal.getsignal(signal.SIGINT) is signal.SIG_DFL:\n # No custom signal handling was added, install our own\n signal.signal(signal.SIGINT, self._handle_signals)\n\n if signal.getsignal(signal.SIGTERM) is signal.SIG_DFL:\n # No custom signal handling was added, install our own\n signal.signal(signal.SIGTERM, self._handle_signals)\n\n self.process_manager.run()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nbinding the reply server to the master process manager.", "response": "def __bind(self):\n '''\n Binds the reply server\n '''\n if self.log_queue is not None:\n salt.log.setup.set_multiprocessing_logging_queue(self.log_queue)\n if self.log_queue_level is not None:\n salt.log.setup.set_multiprocessing_logging_level(self.log_queue_level)\n salt.log.setup.setup_multiprocessing_logging(self.log_queue)\n if self.secrets is not None:\n SMaster.secrets = self.secrets\n\n dfn = os.path.join(self.opts['cachedir'], '.dfn')\n if os.path.isfile(dfn):\n try:\n if salt.utils.platform.is_windows() and not os.access(dfn, os.W_OK):\n # Cannot delete read-only files on Windows.\n os.chmod(dfn, stat.S_IRUSR | stat.S_IWUSR)\n os.remove(dfn)\n except os.error:\n pass\n\n # Wait for kill should be less then parent's ProcessManager.\n self.process_manager = salt.utils.process.ProcessManager(name='ReqServer_ProcessManager',\n wait_for_kill=1)\n\n req_channels = []\n tcp_only = True\n for transport, opts in iter_transport_opts(self.opts):\n chan = salt.transport.server.ReqServerChannel.factory(opts)\n chan.pre_fork(self.process_manager)\n req_channels.append(chan)\n if transport != 'tcp':\n tcp_only = False\n\n kwargs = {}\n if salt.utils.platform.is_windows():\n kwargs['log_queue'] = self.log_queue\n kwargs['log_queue_level'] = self.log_queue_level\n # Use one worker thread if only the TCP transport is set up on\n # Windows and we are using Python 2. There is load balancer\n # support on Windows for the TCP transport when using Python 3.\n if tcp_only and six.PY2 and int(self.opts['worker_threads']) != 1:\n log.warning('TCP transport supports only 1 worker on Windows '\n 'when using Python 2.')\n self.opts['worker_threads'] = 1\n\n # Reset signals to default ones before adding processes to the process\n # manager. We don't want the processes being started to inherit those\n # signal handlers\n with salt.utils.process.default_signals(signal.SIGINT, signal.SIGTERM):\n for ind in range(int(self.opts['worker_threads'])):\n name = 'MWorker-{0}'.format(ind)\n self.process_manager.add_process(MWorker,\n args=(self.opts,\n self.master_key,\n self.key,\n req_channels,\n name),\n kwargs=kwargs,\n name=name)\n self.process_manager.run()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __bind(self):\n '''\n Bind to the local port\n '''\n # using ZMQIOLoop since we *might* need zmq in there\n install_zmq()\n self.io_loop = ZMQDefaultLoop()\n self.io_loop.make_current()\n for req_channel in self.req_channels:\n req_channel.post_fork(self._handle_payload, io_loop=self.io_loop) # TODO: cleaner? Maybe lazily?\n try:\n self.io_loop.start()\n except (KeyboardInterrupt, SystemExit):\n # Tornado knows what to do\n pass", "response": "Bind to the local port\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nfire events with stat info if it s time", "response": "def _post_stats(self, stats):\n '''\n Fire events with stat info if it's time\n '''\n end_time = time.time()\n if end_time - self.stat_clock > self.opts['master_stats_event_iter']:\n # Fire the event with the stats and wipe the tracker\n self.aes_funcs.event.fire_event({'time': end_time - self.stat_clock, 'worker': self.name, 'stats': stats}, tagify(self.name, 'stats'))\n self.stats = collections.defaultdict(lambda: {'mean': 0, 'latency': 0, 'runs': 0})\n self.stat_clock = end_time"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _handle_clear(self, load):\n '''\n Process a cleartext command\n\n :param dict load: Cleartext payload\n :return: The result of passing the load to a function in ClearFuncs corresponding to\n the command specified in the load's 'cmd' key.\n '''\n log.trace('Clear payload received with command %s', load['cmd'])\n cmd = load['cmd']\n if cmd.startswith('__'):\n return False\n if self.opts['master_stats']:\n start = time.time()\n ret = getattr(self.clear_funcs, cmd)(load), {'fun': 'send_clear'}\n if self.opts['master_stats']:\n stats = salt.utils.event.update_stats(self.stats, start, load)\n self._post_stats(stats)\n return ret", "response": "Process a cleartext command and pass the load to a function in ClearFuncs corresponding to\n ."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nprocesses a command sent via an AES key.", "response": "def _handle_aes(self, data):\n '''\n Process a command sent via an AES key\n\n :param str load: Encrypted payload\n :return: The result of passing the load to a function in AESFuncs corresponding to\n the command specified in the load's 'cmd' key.\n '''\n if 'cmd' not in data:\n log.error('Received malformed command %s', data)\n return {}\n cmd = data['cmd']\n log.trace('AES payload received with command %s', data['cmd'])\n if cmd.startswith('__'):\n return False\n if self.opts['master_stats']:\n start = time.time()\n\n def run_func(data):\n return self.aes_funcs.run_func(data['cmd'], data)\n\n with StackContext(functools.partial(RequestContext,\n {'data': data,\n 'opts': self.opts})):\n ret = run_func(data)\n\n if self.opts['master_stats']:\n stats = salt.utils.event.update_stats(self.stats, start, data)\n self._post_stats(stats)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef run(self):\n '''\n Start a Master Worker\n '''\n salt.utils.process.appendproctitle(self.name)\n self.clear_funcs = ClearFuncs(\n self.opts,\n self.key,\n )\n self.aes_funcs = AESFuncs(self.opts)\n salt.utils.crypt.reinit_crypto()\n self.__bind()", "response": "Start a Master Worker"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nset the local file objects from the file server interface", "response": "def __setup_fileserver(self):\n '''\n Set the local file objects from the file server interface\n '''\n # Avoid circular import\n import salt.fileserver\n self.fs_ = salt.fileserver.Fileserver(self.opts)\n self._serve_file = self.fs_.serve_file\n self._file_find = self.fs_._find_file\n self._file_hash = self.fs_.file_hash\n self._file_hash_and_stat = self.fs_.file_hash_and_stat\n self._file_list = self.fs_.file_list\n self._file_list_emptydirs = self.fs_.file_list_emptydirs\n self._dir_list = self.fs_.dir_list\n self._symlink_list = self.fs_.symlink_list\n self._file_envs = self.fs_.file_envs"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __verify_minion(self, id_, token):\n '''\n Take a minion id and a string signed with the minion private key\n The string needs to verify as 'salt' with the minion public key\n\n :param str id_: A minion ID\n :param str token: A string signed with the minion private key\n\n :rtype: bool\n :return: Boolean indicating whether or not the token can be verified.\n '''\n if not salt.utils.verify.valid_id(self.opts, id_):\n return False\n pub_path = os.path.join(self.opts['pki_dir'], 'minions', id_)\n\n try:\n pub = salt.crypt.get_rsa_pub_key(pub_path)\n except (IOError, OSError):\n log.warning(\n 'Salt minion claiming to be %s attempted to communicate with '\n 'master, but key could not be read and verification was denied.',\n id_\n )\n return False\n except (ValueError, IndexError, TypeError) as err:\n log.error('Unable to load public key \"%s\": %s', pub_path, err)\n try:\n if salt.crypt.public_decrypt(pub, token) == b'salt':\n return True\n except ValueError as err:\n log.error('Unable to decrypt token: %s', err)\n\n log.error(\n 'Salt minion claiming to be %s has attempted to communicate with '\n 'the master and could not be verified', id_\n )\n return False", "response": "Verify a minion ID and a string signed with the minion private key"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nverifies that the passed information authorized a minion to execute a publish command in the load", "response": "def __verify_minion_publish(self, clear_load):\n '''\n Verify that the passed information authorized a minion to execute\n\n :param dict clear_load: A publication load from a minion\n\n :rtype: bool\n :return: A boolean indicating if the minion is allowed to publish the command in the load\n '''\n # Verify that the load is valid\n if 'peer' not in self.opts:\n return False\n if not isinstance(self.opts['peer'], dict):\n return False\n if any(key not in clear_load for key in ('fun', 'arg', 'tgt', 'ret', 'tok', 'id')):\n return False\n # If the command will make a recursive publish don't run\n if clear_load['fun'].startswith('publish.'):\n return False\n # Check the permissions for this minion\n if not self.__verify_minion(clear_load['id'], clear_load['tok']):\n # The minion is not who it says it is!\n # We don't want to listen to it!\n log.warning(\n 'Minion id %s is not who it says it is and is attempting '\n 'to issue a peer command', clear_load['id']\n )\n return False\n clear_load.pop('tok')\n perms = []\n for match in self.opts['peer']:\n if re.match(match, clear_load['id']):\n # This is the list of funcs/modules!\n if isinstance(self.opts['peer'][match], list):\n perms.extend(self.opts['peer'][match])\n if ',' in clear_load['fun']:\n # 'arg': [['cat', '/proc/cpuinfo'], [], ['foo']]\n clear_load['fun'] = clear_load['fun'].split(',')\n arg_ = []\n for arg in clear_load['arg']:\n arg_.append(arg.split())\n clear_load['arg'] = arg_\n\n # finally, check the auth of the load\n return self.ckminions.auth_check(\n perms,\n clear_load['fun'],\n clear_load['arg'],\n clear_load['tgt'],\n clear_load.get('tgt_type', 'glob'),\n publish_validate=True)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _master_tops(self, load):\n '''\n Return the results from an external node classifier if one is\n specified\n\n :param dict load: A payload received from a minion\n :return: The results from an external node classifier\n '''\n load = self.__verify_load(load, ('id', 'tok'))\n if load is False:\n return {}\n return self.masterapi._master_tops(load, skip_verify=True)", "response": "Return the results from an external node classifier if one is\n specified\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngathering the data from the specified minions", "response": "def _mine_get(self, load):\n '''\n Gathers the data from the specified minions' mine\n\n :param dict load: A payload received from a minion\n\n :rtype: dict\n :return: Mine data from the specified minions\n '''\n load = self.__verify_load(load, ('id', 'tgt', 'fun', 'tok'))\n if load is False:\n return {}\n else:\n return self.masterapi._mine_get(load, skip_verify=True)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _mine(self, load):\n '''\n Store the mine data\n\n :param dict load: A payload received from a minion\n\n :rtype: bool\n :return: True if the data has been stored in the mine\n '''\n load = self.__verify_load(load, ('id', 'data', 'tok'))\n if load is False:\n return {}\n return self.masterapi._mine(load, skip_verify=True)", "response": "Store the mine data in the mine data store"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _mine_delete(self, load):\n '''\n Allow the minion to delete a specific function from its own mine\n\n :param dict load: A payload received from a minion\n\n :rtype: bool\n :return: Boolean indicating whether or not the given function was deleted from the mine\n '''\n load = self.__verify_load(load, ('id', 'fun', 'tok'))\n if load is False:\n return {}\n else:\n return self.masterapi._mine_delete(load)", "response": "Allow the minion to delete a specific function from its own mine\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nallowing the minion to delete all of its own mine contents", "response": "def _mine_flush(self, load):\n '''\n Allow the minion to delete all of its own mine contents\n\n :param dict load: A payload received from a minion\n '''\n load = self.__verify_load(load, ('id', 'tok'))\n if load is False:\n return {}\n else:\n return self.masterapi._mine_flush(load, skip_verify=True)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _file_recv(self, load):\n '''\n Allows minions to send files to the master, files are sent to the\n master file cache\n '''\n if any(key not in load for key in ('id', 'path', 'loc')):\n return False\n if not isinstance(load['path'], list):\n return False\n if not self.opts['file_recv']:\n return False\n if not salt.utils.verify.valid_id(self.opts, load['id']):\n return False\n if 'loc' in load and load['loc'] < 0:\n log.error('Invalid file pointer: load[loc] < 0')\n return False\n\n if len(load['data']) + load.get('loc', 0) > self.opts['file_recv_max_size'] * 0x100000:\n log.error(\n 'file_recv_max_size limit of %d MB exceeded! %s will be '\n 'truncated. To successfully push this file, adjust '\n 'file_recv_max_size to an integer (in MB) large enough to '\n 'accommodate it.', self.opts['file_recv_max_size'], load['path']\n )\n return False\n if 'tok' not in load:\n log.error(\n 'Received incomplete call from %s for \\'%s\\', missing \\'%s\\'',\n load['id'], inspect_stack()['co_name'], 'tok'\n )\n return False\n if not self.__verify_minion(load['id'], load['tok']):\n # The minion is not who it says it is!\n # We don't want to listen to it!\n log.warning('Minion id %s is not who it says it is!', load['id'])\n return {}\n load.pop('tok')\n\n # Join path\n sep_path = os.sep.join(load['path'])\n\n # Path normalization should have been done by the sending\n # minion but we can't guarantee it. Re-do it here.\n normpath = os.path.normpath(sep_path)\n\n # Ensure that this safety check is done after the path\n # have been normalized.\n if os.path.isabs(normpath) or '../' in load['path']:\n # Can overwrite master files!!\n return False\n\n cpath = os.path.join(\n self.opts['cachedir'],\n 'minions',\n load['id'],\n 'files',\n normpath)\n # One last safety check here\n if not os.path.normpath(cpath).startswith(self.opts['cachedir']):\n log.warning(\n 'Attempt to write received file outside of master cache '\n 'directory! Requested path: %s. Access denied.', cpath\n )\n return False\n cdir = os.path.dirname(cpath)\n if not os.path.isdir(cdir):\n try:\n os.makedirs(cdir)\n except os.error:\n pass\n if os.path.isfile(cpath) and load['loc'] != 0:\n mode = 'ab'\n else:\n mode = 'wb'\n with salt.utils.files.fopen(cpath, mode) as fp_:\n if load['loc']:\n fp_.seek(load['loc'])\n\n fp_.write(salt.utils.stringutils.to_bytes(load['data']))\n return True", "response": "Internal method to send files to the master file cache"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _pillar(self, load):\n '''\n Return the pillar data for the minion\n\n :param dict load: Minion payload\n\n :rtype: dict\n :return: The pillar data for the minion\n '''\n if any(key not in load for key in ('id', 'grains')):\n return False\n if not salt.utils.verify.valid_id(self.opts, load['id']):\n return False\n load['grains']['id'] = load['id']\n\n pillar = salt.pillar.get_pillar(\n self.opts,\n load['grains'],\n load['id'],\n load.get('saltenv', load.get('env')),\n ext=load.get('ext'),\n pillar_override=load.get('pillar_override', {}),\n pillarenv=load.get('pillarenv'),\n extra_minion_data=load.get('extra_minion_data'))\n data = pillar.compile_pillar()\n self.fs_.update_opts()\n if self.opts.get('minion_data_cache', False):\n self.masterapi.cache.store('minions/{0}'.format(load['id']),\n 'data',\n {'grains': load['grains'],\n 'pillar': data})\n if self.opts.get('minion_data_cache_events') is True:\n self.event.fire_event({'Minion data cache refresh': load['id']}, tagify(load['id'], 'refresh', 'minion'))\n return data", "response": "Return the pillar data for the minion"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _minion_event(self, load):\n '''\n Receive an event from the minion and fire it on the master event\n interface\n\n :param dict load: The minion payload\n '''\n load = self.__verify_load(load, ('id', 'tok'))\n if load is False:\n return {}\n # Route to master event bus\n self.masterapi._minion_event(load)\n # Process locally\n self._handle_minion_event(load)", "response": "Handle an event from the minion and fire it on the master event bus"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nhandles a minion event from the minions cache", "response": "def _handle_minion_event(self, load):\n '''\n Act on specific events from minions\n '''\n id_ = load['id']\n if load.get('tag', '') == '_salt_error':\n log.error(\n 'Received minion error from [%s]: %s',\n id_, load['data']['message']\n )\n\n for event in load.get('events', []):\n event_data = event.get('data', {})\n if 'minions' in event_data:\n jid = event_data.get('jid')\n if not jid:\n continue\n minions = event_data['minions']\n try:\n salt.utils.job.store_minions(\n self.opts,\n jid,\n minions,\n mminion=self.mminion,\n syndic_id=id_)\n except (KeyError, salt.exceptions.SaltCacheError) as exc:\n log.error(\n 'Could not add minion(s) %s for job %s: %s',\n minions, jid, exc\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nhandle the return data sent from the master and returns it.", "response": "def _return(self, load):\n '''\n Handle the return data sent from the minions.\n\n Takes the return, verifies it and fires it on the master event bus.\n Typically, this event is consumed by the Salt CLI waiting on the other\n end of the event bus but could be heard by any listener on the bus.\n\n :param dict load: The minion payload\n '''\n if self.opts['require_minion_sign_messages'] and 'sig' not in load:\n log.critical(\n '_return: Master is requiring minions to sign their '\n 'messages, but there is no signature in this payload from '\n '%s.', load['id']\n )\n return False\n\n if 'sig' in load:\n log.trace('Verifying signed event publish from minion')\n sig = load.pop('sig')\n this_minion_pubkey = os.path.join(self.opts['pki_dir'], 'minions/{0}'.format(load['id']))\n serialized_load = salt.serializers.msgpack.serialize(load)\n if not salt.crypt.verify_signature(this_minion_pubkey, serialized_load, sig):\n log.info('Failed to verify event signature from minion %s.', load['id'])\n if self.opts['drop_messages_signature_fail']:\n log.critical(\n 'Drop_messages_signature_fail is enabled, dropping '\n 'message from %s', load['id']\n )\n return False\n else:\n log.info('But \\'drop_message_signature_fail\\' is disabled, so message is still accepted.')\n load['sig'] = sig\n\n try:\n salt.utils.job.store_job(\n self.opts, load, event=self.event, mminion=self.mminion)\n except salt.exceptions.SaltCacheError:\n log.error('Could not store job information for load: %s', load)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreceiving a syndic minion return and format it to look like returns from individual minions.", "response": "def _syndic_return(self, load):\n '''\n Receive a syndic minion return and format it to look like returns from\n individual minions.\n\n :param dict load: The minion payload\n '''\n loads = load.get('load')\n if not isinstance(loads, list):\n loads = [load] # support old syndics not aggregating returns\n for load in loads:\n # Verify the load\n if any(key not in load for key in ('return', 'jid', 'id')):\n continue\n # if we have a load, save it\n if load.get('load'):\n fstr = '{0}.save_load'.format(self.opts['master_job_cache'])\n self.mminion.returners[fstr](load['jid'], load['load'])\n\n # Register the syndic\n syndic_cache_path = os.path.join(self.opts['cachedir'], 'syndics', load['id'])\n if not os.path.exists(syndic_cache_path):\n path_name = os.path.split(syndic_cache_path)[0]\n if not os.path.exists(path_name):\n os.makedirs(path_name)\n with salt.utils.files.fopen(syndic_cache_path, 'w') as wfh:\n wfh.write('')\n\n # Format individual return loads\n for key, item in six.iteritems(load['return']):\n ret = {'jid': load['jid'],\n 'id': key}\n ret.update(item)\n if 'master_id' in load:\n ret['master_id'] = load['master_id']\n if 'fun' in load:\n ret['fun'] = load['fun']\n if 'arg' in load:\n ret['fun_args'] = load['arg']\n if 'out' in load:\n ret['out'] = load['out']\n if 'sig' in load:\n ret['sig'] = load['sig']\n self._return(ret)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nexecute a runner from a minion return the runner s function data", "response": "def minion_runner(self, clear_load):\n '''\n Execute a runner from a minion, return the runner's function data\n\n :param dict clear_load: The minion payload\n\n :rtype: dict\n :return: The runner function data\n '''\n load = self.__verify_load(clear_load, ('fun', 'arg', 'id', 'tok'))\n if load is False:\n return {}\n else:\n return self.masterapi.minion_runner(clear_load)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef pub_ret(self, load):\n '''\n Request the return data from a specific jid, only allowed\n if the requesting minion also initialted the execution.\n\n :param dict load: The minion payload\n\n :rtype: dict\n :return: Return data corresponding to a given JID\n '''\n load = self.__verify_load(load, ('jid', 'id', 'tok'))\n if load is False:\n return {}\n # Check that this minion can access this data\n auth_cache = os.path.join(\n self.opts['cachedir'],\n 'publish_auth')\n if not os.path.isdir(auth_cache):\n os.makedirs(auth_cache)\n jid_fn = os.path.join(auth_cache, six.text_type(load['jid']))\n with salt.utils.files.fopen(jid_fn, 'r') as fp_:\n if not load['id'] == fp_.read():\n return {}\n # Grab the latest and return\n return self.local.get_cache_returns(load['jid'])", "response": "Request the return data from a specific jid only allowed\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef minion_pub(self, clear_load):\n '''\n Publish a command initiated from a minion, this method executes minion\n restrictions so that the minion publication will only work if it is\n enabled in the config.\n\n The configuration on the master allows minions to be matched to\n salt functions, so the minions can only publish allowed salt functions\n\n The config will look like this:\n\n .. code-block:: bash\n\n peer:\n .*:\n - .*\n\n This configuration will enable all minions to execute all commands:\n\n .. code-block:: bash\n\n peer:\n foo.example.com:\n - test.*\n\n The above configuration will only allow the minion foo.example.com to\n execute commands from the test module.\n\n :param dict clear_load: The minion pay\n '''\n if not self.__verify_minion_publish(clear_load):\n return {}\n else:\n return self.masterapi.minion_pub(clear_load)", "response": "Publish a command initiated from a minion"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef minion_publish(self, clear_load):\n '''\n Publish a command initiated from a minion, this method executes minion\n restrictions so that the minion publication will only work if it is\n enabled in the config.\n\n The configuration on the master allows minions to be matched to\n salt functions, so the minions can only publish allowed salt functions\n\n The config will look like this:\n\n .. code-block:: bash\n\n peer:\n .*:\n - .*\n\n This configuration will enable all minions to execute all commands.\n peer:\n\n .. code-block:: bash\n\n foo.example.com:\n - test.*\n\n The above configuration will only allow the minion foo.example.com to\n execute commands from the test module.\n\n :param dict clear_load: The minion payload\n '''\n if not self.__verify_minion_publish(clear_load):\n return {}\n else:\n return self.masterapi.minion_publish(clear_load)", "response": "Publish a command initiated from a minion"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef revoke_auth(self, load):\n '''\n Allow a minion to request revocation of its own key\n\n :param dict load: The minion payload\n\n :rtype: dict\n :return: If the load is invalid, it may be returned. No key operation is performed.\n\n :rtype: bool\n :return: True if key was revoked, False if not\n '''\n load = self.__verify_load(load, ('id', 'tok'))\n\n if not self.opts.get('allow_minion_key_revoke', False):\n log.warning(\n 'Minion %s requested key revoke, but allow_minion_key_revoke '\n 'is set to False', load['id']\n )\n return load\n\n if load is False:\n return load\n else:\n return self.masterapi.revoke_auth(load)", "response": "Allow a minion to request revocation of its own key"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nwraps for running functions that are not encrypted with AES encryption", "response": "def run_func(self, func, load):\n '''\n Wrapper for running functions executed with AES encryption\n\n :param function func: The function to run\n :return: The result of the master function that was called\n '''\n # Don't honor private functions\n if func.startswith('__'):\n # TODO: return some error? Seems odd to return {}\n return {}, {'fun': 'send'}\n # Run the func\n if hasattr(self, func):\n try:\n start = time.time()\n ret = getattr(self, func)(load)\n log.trace(\n 'Master function call %s took %s seconds',\n func, time.time() - start\n )\n except Exception:\n ret = ''\n log.error('Error in function %s:\\n', func, exc_info=True)\n else:\n log.error(\n 'Received function %s which is unavailable on the master, '\n 'returning False', func\n )\n return False, {'fun': 'send'}\n # Don't encrypt the return value for the _return func\n # (we don't care about the return value, so why encrypt it?)\n if func == '_return':\n return ret, {'fun': 'send'}\n if func == '_pillar' and 'id' in load:\n if load.get('ver') != '2' and self.opts['pillar_version'] == 1:\n # Authorized to return old pillar proto\n return ret, {'fun': 'send'}\n return ret, {'fun': 'send_private', 'key': 'pillar', 'tgt': load['id']}\n # Encrypt the return\n return ret, {'fun': 'send'}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef runner(self, clear_load):\n '''\n Send a master control function back to the runner system\n '''\n # All runner ops pass through eauth\n auth_type, err_name, key, sensitive_load_keys = self._prep_auth_info(clear_load)\n\n # Authenticate\n auth_check = self.loadauth.check_authentication(clear_load, auth_type, key=key)\n error = auth_check.get('error')\n\n if error:\n # Authentication error occurred: do not continue.\n return {'error': error}\n\n # Authorize\n username = auth_check.get('username')\n if auth_type != 'user':\n runner_check = self.ckminions.runner_check(\n auth_check.get('auth_list', []),\n clear_load['fun'],\n clear_load.get('kwarg', {})\n )\n if not runner_check:\n return {'error': {'name': err_name,\n 'message': 'Authentication failure of type \"{0}\" occurred for '\n 'user {1}.'.format(auth_type, username)}}\n elif isinstance(runner_check, dict) and 'error' in runner_check:\n # A dictionary with an error name/message was handled by ckminions.runner_check\n return runner_check\n\n # No error occurred, consume sensitive settings from the clear_load if passed.\n for item in sensitive_load_keys:\n clear_load.pop(item, None)\n else:\n if 'user' in clear_load:\n username = clear_load['user']\n if salt.auth.AuthUser(username).is_sudo():\n username = self.opts.get('user', 'root')\n else:\n username = salt.utils.user.get_user()\n\n # Authorized. Do the job!\n try:\n fun = clear_load.pop('fun')\n runner_client = salt.runner.RunnerClient(self.opts)\n return runner_client.asynchronous(fun,\n clear_load.get('kwarg', {}),\n username)\n except Exception as exc:\n log.error('Exception occurred while introspecting %s: %s', fun, exc)\n return {'error': {'name': exc.__class__.__name__,\n 'args': exc.args,\n 'message': six.text_type(exc)}}", "response": "Send a master control function back to the runner system"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsends a master control function back to the wheel system", "response": "def wheel(self, clear_load):\n '''\n Send a master control function back to the wheel system\n '''\n # All wheel ops pass through eauth\n auth_type, err_name, key, sensitive_load_keys = self._prep_auth_info(clear_load)\n\n # Authenticate\n auth_check = self.loadauth.check_authentication(clear_load, auth_type, key=key)\n error = auth_check.get('error')\n\n if error:\n # Authentication error occurred: do not continue.\n return {'error': error}\n\n # Authorize\n username = auth_check.get('username')\n if auth_type != 'user':\n wheel_check = self.ckminions.wheel_check(\n auth_check.get('auth_list', []),\n clear_load['fun'],\n clear_load.get('kwarg', {})\n )\n if not wheel_check:\n return {'error': {'name': err_name,\n 'message': 'Authentication failure of type \"{0}\" occurred for '\n 'user {1}.'.format(auth_type, username)}}\n elif isinstance(wheel_check, dict) and 'error' in wheel_check:\n # A dictionary with an error name/message was handled by ckminions.wheel_check\n return wheel_check\n\n # No error occurred, consume sensitive settings from the clear_load if passed.\n for item in sensitive_load_keys:\n clear_load.pop(item, None)\n else:\n if 'user' in clear_load:\n username = clear_load['user']\n if salt.auth.AuthUser(username).is_sudo():\n username = self.opts.get('user', 'root')\n else:\n username = salt.utils.user.get_user()\n\n # Authorized. Do the job!\n try:\n jid = salt.utils.jid.gen_jid(self.opts)\n fun = clear_load.pop('fun')\n tag = tagify(jid, prefix='wheel')\n data = {'fun': \"wheel.{0}\".format(fun),\n 'jid': jid,\n 'tag': tag,\n 'user': username}\n\n self.event.fire_event(data, tagify([jid, 'new'], 'wheel'))\n ret = self.wheel_.call_func(fun, full_return=True, **clear_load)\n data['return'] = ret['return']\n data['success'] = ret['success']\n self.event.fire_event(data, tagify([jid, 'ret'], 'wheel'))\n return {'tag': tag,\n 'data': data}\n except Exception as exc:\n log.error('Exception occurred while introspecting %s: %s', fun, exc)\n data['return'] = 'Exception occurred in wheel {0}: {1}: {2}'.format(\n fun,\n exc.__class__.__name__,\n exc,\n )\n data['success'] = False\n self.event.fire_event(data, tagify([jid, 'ret'], 'wheel'))\n return {'tag': tag,\n 'data': data}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef mk_token(self, clear_load):\n '''\n Create and return an authentication token, the clear load needs to\n contain the eauth key and the needed authentication creds.\n '''\n token = self.loadauth.mk_token(clear_load)\n if not token:\n log.warning('Authentication failure of type \"eauth\" occurred.')\n return ''\n return token", "response": "Create and return an authentication token."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef publish(self, clear_load):\n '''\n This method sends out publications to the minions, it can only be used\n by the LocalClient.\n '''\n extra = clear_load.get('kwargs', {})\n\n publisher_acl = salt.acl.PublisherACL(self.opts['publisher_acl_blacklist'])\n\n if publisher_acl.user_is_blacklisted(clear_load['user']) or \\\n publisher_acl.cmd_is_blacklisted(clear_load['fun']):\n log.error(\n '%s does not have permissions to run %s. Please contact '\n 'your local administrator if you believe this is in '\n 'error.\\n', clear_load['user'], clear_load['fun']\n )\n return {'error': {'name': 'AuthorizationError',\n 'message': 'Authorization error occurred.'}}\n\n # Retrieve the minions list\n delimiter = clear_load.get('kwargs', {}).get('delimiter', DEFAULT_TARGET_DELIM)\n _res = self.ckminions.check_minions(\n clear_load['tgt'],\n clear_load.get('tgt_type', 'glob'),\n delimiter\n )\n minions = _res.get('minions', list())\n missing = _res.get('missing', list())\n ssh_minions = _res.get('ssh_minions', False)\n\n # Check for external auth calls and authenticate\n auth_type, err_name, key, sensitive_load_keys = self._prep_auth_info(extra)\n if auth_type == 'user':\n auth_check = self.loadauth.check_authentication(clear_load, auth_type, key=key)\n else:\n auth_check = self.loadauth.check_authentication(extra, auth_type)\n\n # Setup authorization list variable and error information\n auth_list = auth_check.get('auth_list', [])\n err_msg = 'Authentication failure of type \"{0}\" occurred.'.format(auth_type)\n\n if auth_check.get('error'):\n # Authentication error occurred: do not continue.\n log.warning(err_msg)\n return {'error': {'name': 'AuthenticationError',\n 'message': 'Authentication error occurred.'}}\n\n # All Token, Eauth, and non-root users must pass the authorization check\n if auth_type != 'user' or (auth_type == 'user' and auth_list):\n # Authorize the request\n authorized = self.ckminions.auth_check(\n auth_list,\n clear_load['fun'],\n clear_load['arg'],\n clear_load['tgt'],\n clear_load.get('tgt_type', 'glob'),\n minions=minions,\n # always accept find_job\n whitelist=['saltutil.find_job'],\n )\n\n if not authorized:\n # Authorization error occurred. Do not continue.\n if auth_type == 'eauth' and not auth_list and 'username' in extra and 'eauth' in extra:\n log.debug('Auth configuration for eauth \"%s\" and user \"%s\" is empty', extra['eauth'], extra['username'])\n log.warning(err_msg)\n return {'error': {'name': 'AuthorizationError',\n 'message': 'Authorization error occurred.'}}\n\n # Perform some specific auth_type tasks after the authorization check\n if auth_type == 'token':\n username = auth_check.get('username')\n clear_load['user'] = username\n log.debug('Minion tokenized user = \"%s\"', username)\n elif auth_type == 'eauth':\n # The username we are attempting to auth with\n clear_load['user'] = self.loadauth.load_name(extra)\n\n # If we order masters (via a syndic), don't short circuit if no minions\n # are found\n if not self.opts.get('order_masters'):\n # Check for no minions\n if not minions:\n return {\n 'enc': 'clear',\n 'load': {\n 'jid': None,\n 'minions': minions,\n 'error': 'Master could not resolve minions for target {0}'.format(clear_load['tgt'])\n }\n }\n if extra.get('batch', None):\n return self.publish_batch(clear_load, extra, minions, missing)\n\n jid = self._prep_jid(clear_load, extra)\n if jid is None:\n return {'enc': 'clear',\n 'load': {'error': 'Master failed to assign jid'}}\n payload = self._prep_pub(minions, jid, clear_load, extra, missing)\n\n # Send it!\n self._send_ssh_pub(payload, ssh_minions=ssh_minions)\n self._send_pub(payload)\n\n return {\n 'enc': 'clear',\n 'load': {\n 'jid': clear_load['jid'],\n 'minions': minions,\n 'missing': missing\n }\n }", "response": "Sends out publications to the minions"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _prep_jid(self, clear_load, extra):\n '''\n Return a jid for this publication\n '''\n # the jid in clear_load can be None, '', or something else. this is an\n # attempt to clean up the value before passing to plugins\n passed_jid = clear_load['jid'] if clear_load.get('jid') else None\n nocache = extra.get('nocache', False)\n\n # Retrieve the jid\n fstr = '{0}.prep_jid'.format(self.opts['master_job_cache'])\n try:\n # Retrieve the jid\n jid = self.mminion.returners[fstr](nocache=nocache,\n passed_jid=passed_jid)\n except (KeyError, TypeError):\n # The returner is not present\n msg = (\n 'Failed to allocate a jid. The requested returner \\'{0}\\' '\n 'could not be loaded.'.format(fstr.split('.')[0])\n )\n log.error(msg)\n return {'error': msg}\n return jid", "response": "Prepare a jid for this publication\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _send_pub(self, load):\n '''\n Take a load and send it across the network to connected minions\n '''\n for transport, opts in iter_transport_opts(self.opts):\n chan = salt.transport.server.PubServerChannel.factory(opts)\n chan.publish(load)", "response": "Send a load to all the minions that have been connected to"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nsend a load to the ssh server", "response": "def _send_ssh_pub(self, load, ssh_minions=False):\n '''\n Take a load and send it across the network to ssh minions\n '''\n if self.opts['enable_ssh_minions'] is True and ssh_minions is True:\n log.debug('Send payload to ssh minions')\n threading.Thread(target=self.ssh_client.cmd, kwargs=load).start()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\npreparing a publication for a given load and perform the necessary steps to prepare a publication.", "response": "def _prep_pub(self, minions, jid, clear_load, extra, missing):\n '''\n Take a given load and perform the necessary steps\n to prepare a publication.\n\n TODO: This is really only bound by temporal cohesion\n and thus should be refactored even further.\n '''\n clear_load['jid'] = jid\n delimiter = clear_load.get('kwargs', {}).get('delimiter', DEFAULT_TARGET_DELIM)\n\n # TODO Error reporting over the master event bus\n self.event.fire_event({'minions': minions}, clear_load['jid'])\n new_job_load = {\n 'jid': clear_load['jid'],\n 'tgt_type': clear_load['tgt_type'],\n 'tgt': clear_load['tgt'],\n 'user': clear_load['user'],\n 'fun': clear_load['fun'],\n 'arg': clear_load['arg'],\n 'minions': minions,\n 'missing': missing,\n }\n\n # Announce the job on the event bus\n self.event.fire_event(new_job_load, tagify([clear_load['jid'], 'new'], 'job'))\n\n if self.opts['ext_job_cache']:\n fstr = '{0}.save_load'.format(self.opts['ext_job_cache'])\n save_load_func = True\n\n # Get the returner's save_load arg_spec.\n try:\n arg_spec = salt.utils.args.get_function_argspec(self.mminion.returners[fstr])\n\n # Check if 'minions' is included in returner's save_load arg_spec.\n # This may be missing in custom returners, which we should warn about.\n if 'minions' not in arg_spec.args:\n log.critical(\n 'The specified returner used for the external job cache '\n '\\'%s\\' does not have a \\'minions\\' kwarg in the returner\\'s '\n 'save_load function.', self.opts['ext_job_cache']\n )\n except (AttributeError, KeyError):\n save_load_func = False\n log.critical(\n 'The specified returner used for the external job cache '\n '\"%s\" does not have a save_load function!',\n self.opts['ext_job_cache']\n )\n\n if save_load_func:\n try:\n self.mminion.returners[fstr](clear_load['jid'], clear_load, minions=minions)\n except Exception:\n log.critical(\n 'The specified returner threw a stack trace:\\n',\n exc_info=True\n )\n\n # always write out to the master job caches\n try:\n fstr = '{0}.save_load'.format(self.opts['master_job_cache'])\n self.mminion.returners[fstr](clear_load['jid'], clear_load, minions)\n except KeyError:\n log.critical(\n 'The specified returner used for the master job cache '\n '\"%s\" does not have a save_load function!',\n self.opts['master_job_cache']\n )\n except Exception:\n log.critical(\n 'The specified returner threw a stack trace:\\n',\n exc_info=True\n )\n # Set up the payload\n payload = {'enc': 'aes'}\n # Altering the contents of the publish load is serious!! Changes here\n # break compatibility with minion/master versions and even tiny\n # additions can have serious implications on the performance of the\n # publish commands.\n #\n # In short, check with Thomas Hatch before you even think about\n # touching this stuff, we can probably do what you want to do another\n # way that won't have a negative impact.\n load = {\n 'fun': clear_load['fun'],\n 'arg': clear_load['arg'],\n 'tgt': clear_load['tgt'],\n 'jid': clear_load['jid'],\n 'ret': clear_load['ret'],\n }\n # if you specified a master id, lets put that in the load\n if 'master_id' in self.opts:\n load['master_id'] = self.opts['master_id']\n # if someone passed us one, use that\n if 'master_id' in extra:\n load['master_id'] = extra['master_id']\n # Only add the delimiter to the pub data if it is non-default\n if delimiter != DEFAULT_TARGET_DELIM:\n load['delimiter'] = delimiter\n\n if 'id' in extra:\n load['id'] = extra['id']\n if 'tgt_type' in clear_load:\n load['tgt_type'] = clear_load['tgt_type']\n if 'to' in clear_load:\n load['to'] = clear_load['to']\n\n if 'kwargs' in clear_load:\n if 'ret_config' in clear_load['kwargs']:\n load['ret_config'] = clear_load['kwargs'].get('ret_config')\n\n if 'metadata' in clear_load['kwargs']:\n load['metadata'] = clear_load['kwargs'].get('metadata')\n\n if 'module_executors' in clear_load['kwargs']:\n load['module_executors'] = clear_load['kwargs'].get('module_executors')\n\n if 'executor_opts' in clear_load['kwargs']:\n load['executor_opts'] = clear_load['kwargs'].get('executor_opts')\n\n if 'ret_kwargs' in clear_load['kwargs']:\n load['ret_kwargs'] = clear_load['kwargs'].get('ret_kwargs')\n\n if 'user' in clear_load:\n log.info(\n 'User %s Published command %s with jid %s',\n clear_load['user'], clear_load['fun'], clear_load['jid']\n )\n load['user'] = clear_load['user']\n else:\n log.info(\n 'Published command %s with jid %s',\n clear_load['fun'], clear_load['jid']\n )\n log.debug('Published command details %s', load)\n return load"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef row_absent(name, db, table, where_sql, where_args=None):\n '''\n Makes sure the specified row is absent in db. If multiple rows\n match where_sql, then the state will fail.\n\n name\n Only used as the unique ID\n\n db\n The database file name\n\n table\n The table name to check\n\n where_sql\n The sql to select the row to check\n\n where_args\n The list parameters to substitute in where_sql\n '''\n changes = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n conn = None\n try:\n conn = sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)\n conn.row_factory = _dict_factory\n rows = None\n if where_args is None:\n rows = _query(conn,\n \"SELECT * FROM `\" + table + \"` WHERE \" + where_sql)\n else:\n rows = _query(conn,\n \"SELECT * FROM `\" + table + \"` WHERE \" + where_sql,\n where_args)\n\n if len(rows) > 1:\n changes['result'] = False\n changes['comment'] = \"More than one row matched the specified query\"\n elif len(rows) == 1:\n if __opts__['test']:\n changes['result'] = True\n changes['comment'] = \"Row will be removed in \" + table\n changes['changes']['old'] = rows[0]\n\n else:\n if where_args is None:\n cursor = conn.execute(\"DELETE FROM `\" +\n table + \"` WHERE \" + where_sql)\n else:\n cursor = conn.execute(\"DELETE FROM `\" +\n table + \"` WHERE \" + where_sql,\n where_args)\n conn.commit()\n if cursor.rowcount == 1:\n changes['result'] = True\n changes['comment'] = \"Row removed\"\n changes['changes']['old'] = rows[0]\n else:\n changes['result'] = False\n changes['comment'] = \"Unable to remove row\"\n else:\n changes['result'] = True\n changes['comment'] = 'Row is absent'\n\n except Exception as e:\n changes['result'] = False\n changes['comment'] = six.text_type(e)\n\n finally:\n if conn:\n conn.close()\n\n return changes", "response": "Make sure the specified row is absent in db."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks to make sure the given row exists and is present in the given database table and data. If update is True the row will be updated with data. Otherwise it will fail.", "response": "def row_present(name,\n db,\n table,\n data,\n where_sql,\n where_args=None,\n update=False):\n '''\n Checks to make sure the given row exists. If row exists and update is True\n then row will be updated with data. Otherwise it will leave existing\n row unmodified and check it against data. If the existing data\n doesn't match data_check the state will fail. If the row doesn't\n exist then it will insert data into the table. If more than one\n row matches, then the state will fail.\n\n name\n Only used as the unique ID\n\n db\n The database file name\n\n table\n The table name to check the data\n\n data\n The dictionary of key/value pairs to check against if\n row exists, insert into the table if it doesn't\n\n where_sql\n The sql to select the row to check\n\n where_args\n The list parameters to substitute in where_sql\n\n update\n True will replace the existing row with data\n When False and the row exists and data does not equal\n the row data then the state will fail\n '''\n changes = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n conn = None\n try:\n conn = sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)\n conn.row_factory = _dict_factory\n rows = None\n if where_args is None:\n rows = _query(conn, \"SELECT * FROM `\" +\n table + \"` WHERE \" + where_sql)\n else:\n rows = _query(conn, \"SELECT * FROM `\" +\n table + \"` WHERE \" + where_sql,\n where_args)\n\n if len(rows) > 1:\n changes['result'] = False\n changes['comment'] = 'More than one row matched the specified query'\n elif len(rows) == 1:\n for key, value in six.iteritems(data):\n if key in rows[0] and rows[0][key] != value:\n if update:\n if __opts__['test']:\n changes['result'] = True\n changes['comment'] = \"Row will be update in \" + table\n\n else:\n columns = []\n params = []\n for key, value in six.iteritems(data):\n columns.append(\"`\" + key + \"`=?\")\n params.append(value)\n\n if where_args is not None:\n params += where_args\n\n sql = \"UPDATE `\" + table + \"` SET \"\n sql += \",\".join(columns)\n sql += \" WHERE \"\n sql += where_sql\n cursor = conn.execute(sql, params)\n conn.commit()\n if cursor.rowcount == 1:\n changes['result'] = True\n changes['comment'] = \"Row updated\"\n changes['changes']['old'] = rows[0]\n changes['changes']['new'] = data\n else:\n changes['result'] = False\n changes['comment'] = \"Row update failed\"\n else:\n changes['result'] = False\n changes['comment'] = \"Existing data does\" + \\\n \"not match desired state\"\n break\n\n if changes['result'] is None:\n changes['result'] = True\n changes['comment'] = \"Row exists\"\n else:\n if __opts__['test']:\n changes['result'] = True\n changes['changes']['new'] = data\n changes['comment'] = \"Row will be inserted into \" + table\n else:\n columns = []\n value_stmt = []\n values = []\n for key, value in six.iteritems(data):\n value_stmt.append('?')\n values.append(value)\n columns.append(\"`\" + key + \"`\")\n\n sql = \"INSERT INTO `\" + table + \"` (\"\n sql += \",\".join(columns)\n sql += \") VALUES (\"\n sql += \",\".join(value_stmt)\n sql += \")\"\n cursor = conn.execute(sql, values)\n conn.commit()\n if cursor.rowcount == 1:\n changes['result'] = True\n changes['changes']['new'] = data\n changes['comment'] = 'Inserted row'\n else:\n changes['result'] = False\n changes['comment'] = \"Unable to insert data\"\n\n except Exception as e:\n changes['result'] = False\n changes['comment'] = six.text_type(e)\n\n finally:\n if conn:\n conn.close()\n\n return changes"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef table_absent(name, db):\n '''\n Make sure the specified table does not exist\n\n name\n The name of the table\n\n db\n The name of the database file\n '''\n changes = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n conn = None\n try:\n conn = sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)\n tables = _query(conn, \"SELECT sql FROM sqlite_master \" +\n \" WHERE type='table' AND name=?\", [name])\n\n if len(tables) == 1:\n if __opts__['test']:\n changes['result'] = True\n changes['comment'] = \"'\" + name + \"' will be dropped\"\n else:\n conn.execute(\"DROP TABLE \" + name)\n conn.commit()\n changes['changes']['old'] = tables[0][0]\n changes['result'] = True\n changes['comment'] = \"'\" + name + \"' was dropped\"\n elif not tables:\n changes['result'] = True\n changes['comment'] = \"'\" + name + \"' is already absent\"\n else:\n changes['result'] = False\n changes['comment'] = \"Multiple tables with the same name='\" + \\\n name + \"'\"\n\n except Exception as e:\n changes['result'] = False\n changes['comment'] = six.text_type(e)\n\n finally:\n if conn:\n conn.close()\n\n return changes", "response": "Make sure the specified table does not exist in the specified database"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef table_present(name, db, schema, force=False):\n '''\n Make sure the specified table exists with the specified schema\n\n name\n The name of the table\n\n db\n The name of the database file\n\n schema\n The dictionary containing the schema information\n\n force\n If the name of the table exists and force is set to False,\n the state will fail. If force is set to True, the existing\n table will be replaced with the new table\n '''\n changes = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n conn = None\n try:\n conn = sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)\n tables = _query(conn,\n \"SELECT sql FROM sqlite_master \" +\n \"WHERE type='table' AND name=?\", [name])\n\n if len(tables) == 1:\n sql = None\n if isinstance(schema, six.string_types):\n sql = schema.strip()\n else:\n sql = _get_sql_from_schema(name, schema)\n\n if sql != tables[0][0]:\n if force:\n if __opts__['test']:\n changes['result'] = True\n changes['changes']['old'] = tables[0][0]\n changes['changes']['new'] = sql\n changes['comment'] = \"'\" + name + \"' will be replaced\"\n else:\n conn.execute(\"DROP TABLE `\" + name + \"`\")\n conn.execute(sql)\n conn.commit()\n changes['result'] = True\n changes['changes']['old'] = tables[0][0]\n changes['changes']['new'] = sql\n changes['comment'] = \"Replaced '\" + name + \"'\"\n else:\n changes['result'] = False\n changes['comment'] = \"Expected schema=\" + sql + \\\n \"\\nactual schema=\" + tables[0][0]\n else:\n changes['result'] = True\n changes['comment'] = \"'\" + name + \\\n \"' exists with matching schema\"\n elif not tables:\n # Create the table\n sql = None\n if isinstance(schema, six.string_types):\n sql = schema\n else:\n sql = _get_sql_from_schema(name, schema)\n if __opts__['test']:\n changes['result'] = True\n changes['changes']['new'] = sql\n changes['comment'] = \"'\" + name + \"' will be created\"\n else:\n conn.execute(sql)\n conn.commit()\n changes['result'] = True\n changes['changes']['new'] = sql\n changes['comment'] = \"Created table '\" + name + \"'\"\n else:\n changes['result'] = False\n changes['comment'] = 'Multiple tables with the same name=' + name\n\n except Exception as e:\n changes['result'] = False\n changes['comment'] = str(e)\n\n finally:\n if conn:\n conn.close()\n\n return changes", "response": "Ensure the specified table exists with the specified schema."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nmatches based on range cluster Matches based on range cluster", "response": "def match(tgt, opts=None):\n '''\n Matches based on range cluster\n '''\n if not opts:\n opts = __opts__\n if HAS_RANGE:\n range_ = seco.range.Range(opts['range_server'])\n try:\n return opts['grains']['fqdn'] in range_.expand(tgt)\n except seco.range.RangeException as exc:\n log.debug('Range exception in compound match: %s', exc)\n return False\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef returner(ret):\n '''\n Return data to a Cassandra ColumnFamily\n '''\n\n consistency_level = getattr(pycassa.ConsistencyLevel,\n __opts__['cassandra.consistency_level'])\n\n pool = pycassa.ConnectionPool(__opts__['cassandra.keyspace'],\n __opts__['cassandra.servers'])\n ccf = pycassa.ColumnFamily(pool, __opts__['cassandra.column_family'],\n write_consistency_level=consistency_level)\n\n columns = {'fun': ret['fun'],\n 'id': ret['id']}\n if isinstance(ret['return'], dict):\n for key, value in six.iteritems(ret['return']):\n columns['return.{0}'.format(key)] = six.text_type(value)\n else:\n columns['return'] = six.text_type(ret['return'])\n\n log.debug(columns)\n ccf.insert(ret['jid'], columns)", "response": "Return data to a Cassandra ColumnFamily\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef prep_bootstrap(mpt):\n '''\n Update and get the random script to a random place\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' seed.prep_bootstrap /tmp\n\n '''\n # Verify that the boostrap script is downloaded\n bs_ = __salt__['config.gather_bootstrap_script']()\n fpd_ = os.path.join(mpt, 'tmp', \"{0}\".format(\n uuid.uuid4()))\n if not os.path.exists(fpd_):\n os.makedirs(fpd_)\n os.chmod(fpd_, 0o700)\n fp_ = os.path.join(fpd_, os.path.basename(bs_))\n # Copy script into tmp\n shutil.copy(bs_, fp_)\n tmppath = fpd_.replace(mpt, '')\n return fp_, tmppath", "response": "Update and get the random script to a random place\n CLI Example"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ngenerate a new minion key and a new minion key.", "response": "def apply_(path, id_=None, config=None, approve_key=True, install=True,\n prep_install=False, pub_key=None, priv_key=None, mount_point=None):\n '''\n Seed a location (disk image, directory, or block device) with the\n minion config, approve the minion's key, and/or install salt-minion.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion' seed.apply path id [config=config_data] \\\\\n [gen_key=(true|false)] [approve_key=(true|false)] \\\\\n [install=(true|false)]\n\n path\n Full path to the directory, device, or disk image on the target\n minion's file system.\n\n id\n Minion id with which to seed the path.\n\n config\n Minion configuration options. By default, the 'master' option is set to\n the target host's 'master'.\n\n approve_key\n Request a pre-approval of the generated minion key. Requires\n that the salt-master be configured to either auto-accept all keys or\n expect a signing request from the target host. Default: true.\n\n install\n Install salt-minion, if absent. Default: true.\n\n prep_install\n Prepare the bootstrap script, but don't run it. Default: false\n '''\n stats = __salt__['file.stats'](path, follow_symlinks=True)\n if not stats:\n return '{0} does not exist'.format(path)\n ftype = stats['type']\n path = stats['target']\n log.debug('Mounting %s at %s', ftype, path)\n try:\n os.makedirs(path)\n except OSError:\n # The directory already exists\n pass\n\n mpt = _mount(path, ftype, mount_point)\n\n if not mpt:\n return '{0} could not be mounted'.format(path)\n\n tmp = os.path.join(mpt, 'tmp')\n log.debug('Attempting to create directory %s', tmp)\n try:\n os.makedirs(tmp)\n except OSError:\n if not os.path.isdir(tmp):\n raise\n cfg_files = mkconfig(config, tmp=tmp, id_=id_, approve_key=approve_key,\n pub_key=pub_key, priv_key=priv_key)\n\n if _check_install(mpt):\n # salt-minion is already installed, just move the config and keys\n # into place\n log.info('salt-minion pre-installed on image, '\n 'configuring as %s', id_)\n minion_config = salt.config.minion_config(cfg_files['config'])\n pki_dir = minion_config['pki_dir']\n if not os.path.isdir(os.path.join(mpt, pki_dir.lstrip('/'))):\n __salt__['file.makedirs'](\n os.path.join(mpt, pki_dir.lstrip('/'), '')\n )\n os.rename(cfg_files['privkey'], os.path.join(\n mpt, pki_dir.lstrip('/'), 'minion.pem'))\n os.rename(cfg_files['pubkey'], os.path.join(\n mpt, pki_dir.lstrip('/'), 'minion.pub'))\n os.rename(cfg_files['config'], os.path.join(mpt, 'etc/salt/minion'))\n res = True\n elif install:\n log.info('Attempting to install salt-minion to %s', mpt)\n res = _install(mpt)\n elif prep_install:\n log.error('The prep_install option is no longer supported. Please use '\n 'the bootstrap script installed with Salt, located at %s.',\n salt.syspaths.BOOTSTRAP)\n res = False\n else:\n log.warning('No useful action performed on %s', mpt)\n res = False\n\n _umount(mpt, ftype)\n return res"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates keys and config and put them in a tmp directory.", "response": "def mkconfig(config=None,\n tmp=None,\n id_=None,\n approve_key=True,\n pub_key=None,\n priv_key=None):\n '''\n Generate keys and config and put them in a tmp directory.\n\n pub_key\n absolute path or file content of an optional preseeded salt key\n\n priv_key\n absolute path or file content of an optional preseeded salt key\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'minion' seed.mkconfig [config=config_data] [tmp=tmp_dir] \\\\\n [id_=minion_id] [approve_key=(true|false)]\n '''\n if tmp is None:\n tmp = tempfile.mkdtemp()\n if config is None:\n config = {}\n if 'master' not in config and __opts__['master'] != 'salt':\n config['master'] = __opts__['master']\n if id_:\n config['id'] = id_\n\n # Write the new minion's config to a tmp file\n tmp_config = os.path.join(tmp, 'minion')\n with salt.utils.files.fopen(tmp_config, 'w+') as fp_:\n fp_.write(salt.utils.cloud.salt_config_to_yaml(config))\n\n # Generate keys for the minion\n pubkeyfn = os.path.join(tmp, 'minion.pub')\n privkeyfn = os.path.join(tmp, 'minion.pem')\n preseeded = pub_key and priv_key\n if preseeded:\n log.debug('Writing minion.pub to %s', pubkeyfn)\n log.debug('Writing minion.pem to %s', privkeyfn)\n with salt.utils.files.fopen(pubkeyfn, 'w') as fic:\n fic.write(salt.utils.stringutils.to_str(_file_or_content(pub_key)))\n with salt.utils.files.fopen(privkeyfn, 'w') as fic:\n fic.write(salt.utils.stringutils.to_str(_file_or_content(priv_key)))\n os.chmod(pubkeyfn, 0o600)\n os.chmod(privkeyfn, 0o600)\n else:\n salt.crypt.gen_keys(tmp, 'minion', 2048)\n if approve_key and not preseeded:\n with salt.utils.files.fopen(pubkeyfn) as fp_:\n pubkey = salt.utils.stringutils.to_unicode(fp_.read())\n __salt__['pillar.ext']({'virtkey': [id_, pubkey]})\n\n return {'config': tmp_config, 'pubkey': pubkeyfn, 'privkey': privkeyfn}"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _install(mpt):\n '''\n Determine whether salt-minion is installed and, if not,\n install it.\n Return True if install is successful or already installed.\n '''\n _check_resolv(mpt)\n boot_, tmppath = (prep_bootstrap(mpt)\n or salt.syspaths.BOOTSTRAP)\n # Exec the chroot command\n cmd = 'if type salt-minion; then exit 0; '\n cmd += 'else sh {0} -c /tmp; fi'.format(os.path.join(tmppath, 'bootstrap-salt.sh'))\n return not __salt__['cmd.run_chroot'](mpt, cmd, python_shell=True)['retcode']", "response": "Install salt - minion if not installed."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _check_resolv(mpt):\n '''\n Check that the resolv.conf is present and populated\n '''\n resolv = os.path.join(mpt, 'etc/resolv.conf')\n replace = False\n if os.path.islink(resolv):\n resolv = os.path.realpath(resolv)\n if not os.path.isdir(os.path.dirname(resolv)):\n os.makedirs(os.path.dirname(resolv))\n if not os.path.isfile(resolv):\n replace = True\n if not replace:\n with salt.utils.files.fopen(resolv, 'rb') as fp_:\n conts = salt.utils.stringutils.to_unicode(fp_.read())\n if 'nameserver' not in conts:\n replace = True\n if 'nameserver 127.0.0.1' in conts:\n replace = True\n if replace:\n shutil.copy('/etc/resolv.conf', resolv)", "response": "Check that the resolv. conf is present and populated\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _netsh_file(content):\n '''\n helper function to get the results of ``netsh -f content.txt``\n\n Running ``netsh`` will drop you into a ``netsh`` prompt where you can issue\n ``netsh`` commands. You can put a series of commands in an external file and\n run them as if from a ``netsh`` prompt using the ``-f`` switch. That's what\n this function does.\n\n Args:\n\n content (str):\n The contents of the file that will be run by the ``netsh -f``\n command\n\n Returns:\n str: The text returned by the netsh command\n '''\n with tempfile.NamedTemporaryFile(mode='w',\n prefix='salt-',\n suffix='.netsh',\n delete=False) as fp:\n fp.write(content)\n try:\n log.debug('%s:\\n%s', fp.name, content)\n return salt.modules.cmdmod.run('netsh -f {0}'.format(fp.name), python_shell=True)\n finally:\n os.remove(fp.name)", "response": "helper function to get the results of netsh - f content. txt"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_settings(profile, section, store='local'):\n '''\n Get the firewall property from the specified profile in the specified store\n as returned by ``netsh advfirewall``.\n\n Args:\n\n profile (str):\n The firewall profile to query. Valid options are:\n\n - domain\n - public\n - private\n\n section (str):\n The property to query within the selected profile. Valid options\n are:\n\n - firewallpolicy : inbound/outbound behavior\n - logging : firewall logging settings\n - settings : firewall properties\n - state : firewalls state (on | off)\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n dict: A dictionary containing the properties for the specified profile\n\n Raises:\n CommandExecutionError: If an error occurs\n ValueError: If the parameters are incorrect\n '''\n # validate input\n if profile.lower() not in ('domain', 'public', 'private'):\n raise ValueError('Incorrect profile: {0}'.format(profile))\n if section.lower() not in ('state', 'firewallpolicy', 'settings', 'logging'):\n raise ValueError('Incorrect section: {0}'.format(section))\n if store.lower() not in ('local', 'lgpo'):\n raise ValueError('Incorrect store: {0}'.format(store))\n command = 'show {0}profile {1}'.format(profile, section)\n # run it\n results = _netsh_command(command=command, store=store)\n # sample output:\n # Domain Profile Settings:\n # ----------------------------------------------------------------------\n # LocalFirewallRules N/A (GPO-store only)\n # LocalConSecRules N/A (GPO-store only)\n # InboundUserNotification Disable\n # RemoteManagement Disable\n # UnicastResponseToMulticast Enable\n\n # if it's less than 3 lines it failed\n if len(results) < 3:\n raise CommandExecutionError('Invalid results: {0}'.format(results))\n ret = {}\n # Skip the first 2 lines. Add everything else to a dictionary\n for line in results[3:]:\n ret.update(dict(list(zip(*[iter(re.split(r\"\\s{2,}\", line))]*2))))\n\n # Remove spaces from the values so that `Not Configured` is detected\n # correctly\n for item in ret:\n ret[item] = ret[item].replace(' ', '')\n\n # special handling for firewallpolicy\n if section == 'firewallpolicy':\n inbound, outbound = ret['Firewall Policy'].split(',')\n return {'Inbound': inbound, 'Outbound': outbound}\n\n return ret", "response": "Get the firewall properties for the specified profile in the specified store."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_all_settings(profile, store='local'):\n '''\n Gets all the properties for the specified profile in the specified store\n\n Args:\n\n profile (str):\n The firewall profile to query. Valid options are:\n\n - domain\n - public\n - private\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n dict: A dictionary containing the specified settings\n '''\n ret = dict()\n ret.update(get_settings(profile=profile, section='state', store=store))\n ret.update(get_settings(profile=profile, section='firewallpolicy', store=store))\n ret.update(get_settings(profile=profile, section='settings', store=store))\n ret.update(get_settings(profile=profile, section='logging', store=store))\n return ret", "response": "Gets all the properties for the specified profile in the specified store"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets all properties for all profiles in the specified store", "response": "def get_all_profiles(store='local'):\n '''\n Gets all properties for all profiles in the specified store\n\n Args:\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n dict: A dictionary containing the specified settings for each profile\n '''\n return {\n 'Domain Profile': get_all_settings(profile='domain', store=store),\n 'Private Profile': get_all_settings(profile='private', store=store),\n 'Public Profile': get_all_settings(profile='public', store=store)\n }"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting the firewall settings for the specified profile and store.", "response": "def set_firewall_settings(profile,\n inbound=None,\n outbound=None,\n store='local'):\n '''\n Set the firewall inbound/outbound settings for the specified profile and\n store\n\n Args:\n\n profile (str):\n The firewall profile to configure. Valid options are:\n\n - domain\n - public\n - private\n\n inbound (str):\n The inbound setting. If ``None`` is passed, the setting will remain\n unchanged. Valid values are:\n\n - blockinbound\n - blockinboundalways\n - allowinbound\n - notconfigured\n\n Default is ``None``\n\n outbound (str):\n The outbound setting. If ``None`` is passed, the setting will remain\n unchanged. Valid values are:\n\n - allowoutbound\n - blockoutbound\n - notconfigured\n\n Default is ``None``\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n bool: ``True`` if successful\n\n Raises:\n CommandExecutionError: If an error occurs\n ValueError: If the parameters are incorrect\n '''\n # Input validation\n if profile.lower() not in ('domain', 'public', 'private'):\n raise ValueError('Incorrect profile: {0}'.format(profile))\n if inbound and inbound.lower() not in ('blockinbound',\n 'blockinboundalways',\n 'allowinbound',\n 'notconfigured'):\n raise ValueError('Incorrect inbound value: {0}'.format(inbound))\n if outbound and outbound.lower() not in ('allowoutbound',\n 'blockoutbound',\n 'notconfigured'):\n raise ValueError('Incorrect outbound value: {0}'.format(outbound))\n if not inbound and not outbound:\n raise ValueError('Must set inbound or outbound')\n\n # You have to specify inbound and outbound setting at the same time\n # If you're only specifying one, you have to get the current setting for the\n # other\n if not inbound or not outbound:\n ret = get_settings(profile=profile,\n section='firewallpolicy',\n store=store)\n if not inbound:\n inbound = ret['Inbound']\n if not outbound:\n outbound = ret['Outbound']\n\n command = 'set {0}profile firewallpolicy {1},{2}' \\\n ''.format(profile, inbound, outbound)\n\n results = _netsh_command(command=command, store=store)\n\n if results:\n raise CommandExecutionError('An error occurred: {0}'.format(results))\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_logging_settings(profile, setting, value, store='local'):\n '''\n Configure logging settings for the Windows firewall.\n\n Args:\n\n profile (str):\n The firewall profile to configure. Valid options are:\n\n - domain\n - public\n - private\n\n setting (str):\n The logging setting to configure. Valid options are:\n\n - allowedconnections\n - droppedconnections\n - filename\n - maxfilesize\n\n value (str):\n The value to apply to the setting. Valid values are dependent upon\n the setting being configured. Valid options are:\n\n allowedconnections:\n\n - enable\n - disable\n - notconfigured\n\n droppedconnections:\n\n - enable\n - disable\n - notconfigured\n\n filename:\n\n - Full path and name of the firewall log file\n - notconfigured\n\n maxfilesize:\n\n - 1 - 32767 (Kb)\n - notconfigured\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n bool: ``True`` if successful\n\n Raises:\n CommandExecutionError: If an error occurs\n ValueError: If the parameters are incorrect\n '''\n # Input validation\n if profile.lower() not in ('domain', 'public', 'private'):\n raise ValueError('Incorrect profile: {0}'.format(profile))\n if setting.lower() not in ('allowedconnections',\n 'droppedconnections',\n 'filename',\n 'maxfilesize'):\n raise ValueError('Incorrect setting: {0}'.format(setting))\n if setting.lower() in ('allowedconnections', 'droppedconnections'):\n if value.lower() not in ('enable', 'disable', 'notconfigured'):\n raise ValueError('Incorrect value: {0}'.format(value))\n # TODO: Consider adding something like the following to validate filename\n # https://stackoverflow.com/questions/9532499/check-whether-a-path-is-valid-in-python-without-creating-a-file-at-the-paths-ta\n if setting.lower() == 'maxfilesize':\n if value.lower() != 'notconfigured':\n # Must be a number between 1 and 32767\n try:\n int(value)\n except ValueError:\n raise ValueError('Incorrect value: {0}'.format(value))\n if not 1 <= int(value) <= 32767:\n raise ValueError('Incorrect value: {0}'.format(value))\n # Run the command\n command = 'set {0}profile logging {1} {2}'.format(profile, setting, value)\n results = _netsh_command(command=command, store=store)\n\n # A successful run should return an empty list\n if results:\n raise CommandExecutionError('An error occurred: {0}'.format(results))\n\n return True", "response": "Configure the logging settings for the Windows firewall."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconfiguring firewall settings. Args: profile (str): The firewall profile to configure. Valid options are: - domain - public - private setting (str): The firewall setting to configure. Valid options are: - localfirewallrules - localconsecrules - inboundusernotification - remotemanagement - unicastresponsetomulticast value (str): The value to apply to the setting. Valid options are - enable - disable - notconfigured store (str): The store to use. This is either the local firewall policy or the policy defined by local group policy. Valid options are: - lgpo - local Default is ``local`` Returns: bool: ``True`` if successful Raises: CommandExecutionError: If an error occurs ValueError: If the parameters are incorrect", "response": "def set_settings(profile, setting, value, store='local'):\n '''\n Configure firewall settings.\n\n Args:\n\n profile (str):\n The firewall profile to configure. Valid options are:\n\n - domain\n - public\n - private\n\n setting (str):\n The firewall setting to configure. Valid options are:\n\n - localfirewallrules\n - localconsecrules\n - inboundusernotification\n - remotemanagement\n - unicastresponsetomulticast\n\n value (str):\n The value to apply to the setting. Valid options are\n\n - enable\n - disable\n - notconfigured\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n bool: ``True`` if successful\n\n Raises:\n CommandExecutionError: If an error occurs\n ValueError: If the parameters are incorrect\n '''\n # Input validation\n if profile.lower() not in ('domain', 'public', 'private'):\n raise ValueError('Incorrect profile: {0}'.format(profile))\n if setting.lower() not in ('localfirewallrules',\n 'localconsecrules',\n 'inboundusernotification',\n 'remotemanagement',\n 'unicastresponsetomulticast'):\n raise ValueError('Incorrect setting: {0}'.format(setting))\n if value.lower() not in ('enable', 'disable', 'notconfigured'):\n raise ValueError('Incorrect value: {0}'.format(value))\n\n # Run the command\n command = 'set {0}profile settings {1} {2}'.format(profile, setting, value)\n results = _netsh_command(command=command, store=store)\n\n # A successful run should return an empty list\n if results:\n raise CommandExecutionError('An error occurred: {0}'.format(results))\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nconfiguring the firewall state of the current node.", "response": "def set_state(profile, state, store='local'):\n '''\n Configure the firewall state.\n\n Args:\n\n profile (str):\n The firewall profile to configure. Valid options are:\n\n - domain\n - public\n - private\n\n state (str):\n The firewall state. Valid options are:\n\n - on\n - off\n - notconfigured\n\n store (str):\n The store to use. This is either the local firewall policy or the\n policy defined by local group policy. Valid options are:\n\n - lgpo\n - local\n\n Default is ``local``\n\n Returns:\n bool: ``True`` if successful\n\n Raises:\n CommandExecutionError: If an error occurs\n ValueError: If the parameters are incorrect\n '''\n # Input validation\n if profile.lower() not in ('domain', 'public', 'private'):\n raise ValueError('Incorrect profile: {0}'.format(profile))\n if state.lower() not in ('on', 'off', 'notconfigured'):\n raise ValueError('Incorrect state: {0}'.format(state))\n\n # Run the command\n command = 'set {0}profile state {1}'.format(profile, state)\n results = _netsh_command(command=command, store=store)\n\n # A successful run should return an empty list\n if results:\n raise CommandExecutionError('An error occurred: {0}'.format(results))\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing the aliases file and return a list of line components", "response": "def __parse_aliases():\n '''\n Parse the aliases file, and return a list of line components:\n\n [\n (alias1, target1, comment1),\n (alias2, target2, comment2),\n ]\n '''\n afn = __get_aliases_filename()\n ret = []\n if not os.path.isfile(afn):\n return ret\n with salt.utils.files.fopen(afn, 'r') as ifile:\n for line in ifile:\n line = salt.utils.stringutils.to_unicode(line)\n match = __ALIAS_RE.match(line)\n if match:\n ret.append(match.groups())\n else:\n ret.append((None, None, line.strip()))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nwrite a new copy of the aliases file.", "response": "def __write_aliases_file(lines):\n '''\n Write a new copy of the aliases file. Lines is a list of lines\n as returned by __parse_aliases.\n '''\n afn = __get_aliases_filename()\n adir = os.path.dirname(afn)\n\n out = tempfile.NamedTemporaryFile(dir=adir, delete=False)\n\n if not __opts__.get('integration.test', False):\n if os.path.isfile(afn):\n afn_st = os.stat(afn)\n os.chmod(out.name, stat.S_IMODE(afn_st.st_mode))\n os.chown(out.name, afn_st.st_uid, afn_st.st_gid)\n else:\n os.chmod(out.name, 0o644)\n os.chown(out.name, 0, 0)\n\n for (line_alias, line_target, line_comment) in lines:\n if isinstance(line_target, list):\n line_target = ', '.join(line_target)\n if not line_comment:\n line_comment = ''\n if line_alias and line_target:\n write_line = '{0}: {1}{2}\\n'.format(\n line_alias, line_target, line_comment\n )\n else:\n write_line = '{0}\\n'.format(line_comment)\n if six.PY3:\n write_line = write_line.encode(__salt_system_encoding__)\n out.write(write_line)\n\n out.close()\n os.rename(out.name, afn)\n\n # Search $PATH for the newalises command\n newaliases = salt.utils.path.which('newaliases')\n if newaliases is not None:\n __salt__['cmd.run'](newaliases)\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the aliases found in the aliases file in this format", "response": "def list_aliases():\n '''\n Return the aliases found in the aliases file in this format::\n\n {'alias': 'target'}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aliases.list_aliases\n '''\n ret = dict((alias, target) for alias, target, comment in __parse_aliases() if alias)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef has_target(alias, target):\n '''\n Return true if the alias/target is set\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aliases.has_target alias target\n '''\n if target == '':\n raise SaltInvocationError('target can not be an empty string')\n aliases = list_aliases()\n if alias not in aliases:\n return False\n if isinstance(target, list):\n target = ', '.join(target)\n return target == aliases[alias]", "response": "Return true if the alias is set otherwise False"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nsetting the entry in the aliases file for the given alias to the given target.", "response": "def set_target(alias, target):\n '''\n Set the entry in the aliases file for the given alias, this will overwrite\n any previous entry for the given alias or create a new one if it does not\n exist.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aliases.set_target alias target\n '''\n\n if alias == '':\n raise SaltInvocationError('alias can not be an empty string')\n\n if target == '':\n raise SaltInvocationError('target can not be an empty string')\n\n if get_target(alias) == target:\n return True\n\n lines = __parse_aliases()\n out = []\n ovr = False\n for (line_alias, line_target, line_comment) in lines:\n if line_alias == alias:\n if not ovr:\n out.append((alias, target, line_comment))\n ovr = True\n else:\n out.append((line_alias, line_target, line_comment))\n if not ovr:\n out.append((alias, target, ''))\n\n __write_aliases_file(out)\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef rm_alias(alias):\n '''\n Remove an entry from the aliases file\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' aliases.rm_alias alias\n '''\n if not get_target(alias):\n return True\n\n lines = __parse_aliases()\n out = []\n for (line_alias, line_target, line_comment) in lines:\n if line_alias != alias:\n out.append((line_alias, line_target, line_comment))\n\n __write_aliases_file(out)\n return True", "response": "Remove an entry from the aliases file\n CLI Example : bash\nCTYPE rm_alias alias"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngathering the specified data from the minion data cache", "response": "def gather_cache(self):\n '''\n Gather the specified data from the minion data cache\n '''\n cache = {'grains': {}, 'pillar': {}}\n if self.grains or self.pillar:\n if self.opts.get('minion_data_cache'):\n minions = self.cache.list('minions')\n if not minions:\n return cache\n for minion in minions:\n total = self.cache.fetch('minions/{0}'.format(minion), 'data')\n\n if 'pillar' in total:\n if self.pillar_keys:\n for key in self.pillar_keys:\n if key in total['pillar']:\n cache['pillar'][minion][key] = total['pillar'][key]\n else:\n cache['pillar'][minion] = total['pillar']\n else:\n cache['pillar'][minion] = {}\n\n if 'grains' in total:\n if self.grain_keys:\n for key in self.grain_keys:\n if key in total['grains']:\n cache['grains'][minion][key] = total['grains'][key]\n else:\n cache['grains'][minion] = total['grains']\n else:\n cache['grains'][minion] = {}\n return cache"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ncompiles the top file and return the lowstate for the thorium runtime to iterate over", "response": "def get_chunks(self, exclude=None, whitelist=None):\n '''\n Compile the top file and return the lowstate for the thorium runtime\n to iterate over\n '''\n ret = {}\n err = []\n try:\n top = self.get_top()\n except SaltRenderError as err:\n return ret\n except Exception:\n trb = traceback.format_exc()\n err.append(trb)\n return err\n err += self.verify_tops(top)\n matches = self.top_matches(top)\n if not matches:\n msg = 'No Top file found!'\n raise SaltRenderError(msg)\n matches = self.matches_whitelist(matches, whitelist)\n high, errors = self.render_highstate(matches)\n if exclude:\n if isinstance(exclude, six.string_types):\n exclude = exclude.split(',')\n if '__exclude__' in high:\n high['__exclude__'].extend(exclude)\n else:\n high['__exclude__'] = exclude\n err += errors\n high, ext_errors = self.state.reconcile_extend(high)\n err += ext_errors\n err += self.state.verify_high(high)\n if err:\n raise SaltRenderError(err)\n return self.state.compile_high_data(high)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\niterate over the available events and return a list of events", "response": "def get_events(self):\n '''\n iterate over the available events and return a list of events\n '''\n ret = []\n while True:\n event = self.event.get_event(wait=1, full=True)\n if event is None:\n return ret\n ret.append(event)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef cache_file(source):\n '''\n Wrapper for cp.cache_file which raises an error if the file was unable to\n be cached.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion container_resource.cache_file salt://foo/bar/baz.txt\n '''\n try:\n # Don't just use cp.cache_file for this. Docker has its own code to\n # pull down images from the web.\n if source.startswith('salt://'):\n cached_source = __salt__['cp.cache_file'](source)\n if not cached_source:\n raise CommandExecutionError(\n 'Unable to cache {0}'.format(source)\n )\n return cached_source\n except AttributeError:\n raise SaltInvocationError('Invalid source file {0}'.format(source))\n return source", "response": "Wrapper for cp. cache_file which raises an error if the file was unable to be cached."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef run(name,\n cmd,\n container_type=None,\n exec_driver=None,\n output=None,\n no_start=False,\n stdin=None,\n python_shell=True,\n output_loglevel='debug',\n ignore_retcode=False,\n path=None,\n use_vt=False,\n keep_env=None):\n '''\n Common logic for running shell commands in containers\n\n path\n path to the container parent (for LXC only)\n default: /var/lib/lxc (system default)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion container_resource.run mycontainer 'ps aux' container_type=docker exec_driver=nsenter output=stdout\n '''\n valid_output = ('stdout', 'stderr', 'retcode', 'all')\n if output is None:\n cmd_func = 'cmd.run'\n elif output not in valid_output:\n raise SaltInvocationError(\n '\\'output\\' param must be one of the following: {0}'\n .format(', '.join(valid_output))\n )\n else:\n cmd_func = 'cmd.run_all'\n\n if keep_env is None or isinstance(keep_env, bool):\n to_keep = []\n elif not isinstance(keep_env, (list, tuple)):\n try:\n to_keep = keep_env.split(',')\n except AttributeError:\n log.warning('Invalid keep_env value, ignoring')\n to_keep = []\n else:\n to_keep = keep_env\n\n if exec_driver == 'lxc-attach':\n full_cmd = 'lxc-attach '\n if path:\n full_cmd += '-P {0} '.format(pipes.quote(path))\n if keep_env is not True:\n full_cmd += '--clear-env '\n if 'PATH' not in to_keep:\n full_cmd += '--set-var {0} '.format(PATH)\n # --clear-env results in a very restrictive PATH\n # (/bin:/usr/bin), use a good fallback.\n full_cmd += ' '.join(\n ['--set-var {0}={1}'.format(x, pipes.quote(os.environ[x]))\n for x in to_keep\n if x in os.environ]\n )\n full_cmd += ' -n {0} -- {1}'.format(pipes.quote(name), cmd)\n elif exec_driver == 'nsenter':\n pid = __salt__['{0}.pid'.format(container_type)](name)\n full_cmd = (\n 'nsenter --target {0} --mount --uts --ipc --net --pid -- '\n .format(pid)\n )\n if keep_env is not True:\n full_cmd += 'env -i '\n if 'PATH' not in to_keep:\n full_cmd += '{0} '.format(PATH)\n full_cmd += ' '.join(\n ['{0}={1}'.format(x, pipes.quote(os.environ[x]))\n for x in to_keep\n if x in os.environ]\n )\n full_cmd += ' {0}'.format(cmd)\n elif exec_driver == 'docker-exec':\n # We're using docker exec on the CLI as opposed to via docker-py, since\n # the Docker API doesn't return stdout and stderr separately.\n full_cmd = 'docker exec '\n if stdin:\n full_cmd += '-i '\n full_cmd += '{0} '.format(name)\n if keep_env is not True:\n full_cmd += 'env -i '\n if 'PATH' not in to_keep:\n full_cmd += '{0} '.format(PATH)\n full_cmd += ' '.join(\n ['{0}={1}'.format(x, pipes.quote(os.environ[x]))\n for x in to_keep\n if x in os.environ]\n )\n full_cmd += ' {0}'.format(cmd)\n\n if not use_vt:\n ret = __salt__[cmd_func](full_cmd,\n stdin=stdin,\n python_shell=python_shell,\n output_loglevel=output_loglevel,\n ignore_retcode=ignore_retcode)\n else:\n stdout, stderr = '', ''\n proc = salt.utils.vt.Terminal(\n full_cmd,\n shell=python_shell,\n log_stdin_level='quiet' if output_loglevel == 'quiet' else 'info',\n log_stdout_level=output_loglevel,\n log_stderr_level=output_loglevel,\n log_stdout=True,\n log_stderr=True,\n stream_stdout=False,\n stream_stderr=False\n )\n # Consume output\n try:\n while proc.has_unread_data:\n try:\n cstdout, cstderr = proc.recv()\n if cstdout:\n stdout += cstdout\n if cstderr:\n if output is None:\n stdout += cstderr\n else:\n stderr += cstderr\n time.sleep(0.5)\n except KeyboardInterrupt:\n break\n ret = stdout if output is None \\\n else {'retcode': proc.exitstatus,\n 'pid': 2,\n 'stdout': stdout,\n 'stderr': stderr}\n except salt.utils.vt.TerminalException:\n trace = traceback.format_exc()\n log.error(trace)\n ret = stdout if output is None \\\n else {'retcode': 127,\n 'pid': 2,\n 'stdout': stdout,\n 'stderr': stderr}\n finally:\n proc.terminate()\n\n return ret", "response": "Run a command in a container"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncopying a file from a local file to a local file", "response": "def copy_to(name,\n source,\n dest,\n container_type=None,\n path=None,\n exec_driver=None,\n overwrite=False,\n makedirs=False):\n '''\n Common logic for copying files to containers\n\n path\n path to the container parent (for LXC only)\n default: /var/lib/lxc (system default)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion container_resource.copy_to mycontainer /local/file/path /container/file/path container_type=docker exec_driver=nsenter\n '''\n # Get the appropriate functions\n state = __salt__['{0}.state'.format(container_type)]\n\n def run_all(*args, **akwargs):\n akwargs = copy.deepcopy(akwargs)\n if container_type in ['lxc'] and 'path' not in akwargs:\n akwargs['path'] = path\n return __salt__['{0}.run_all'.format(container_type)](\n *args, **akwargs)\n\n state_kwargs = {}\n cmd_kwargs = {'ignore_retcode': True}\n if container_type in ['lxc']:\n cmd_kwargs['path'] = path\n state_kwargs['path'] = path\n\n def _state(name):\n if state_kwargs:\n return state(name, **state_kwargs)\n else:\n return state(name)\n\n c_state = _state(name)\n if c_state != 'running':\n raise CommandExecutionError(\n 'Container \\'{0}\\' is not running'.format(name)\n )\n\n local_file = cache_file(source)\n source_dir, source_name = os.path.split(local_file)\n\n # Source file sanity checks\n if not os.path.isabs(local_file):\n raise SaltInvocationError('Source path must be absolute')\n elif not os.path.exists(local_file):\n raise SaltInvocationError(\n 'Source file {0} does not exist'.format(local_file)\n )\n elif not os.path.isfile(local_file):\n raise SaltInvocationError('Source must be a regular file')\n\n # Destination file sanity checks\n if not os.path.isabs(dest):\n raise SaltInvocationError('Destination path must be absolute')\n if run_all(name,\n 'test -d {0}'.format(pipes.quote(dest)),\n **cmd_kwargs)['retcode'] == 0:\n # Destination is a directory, full path to dest file will include the\n # basename of the source file.\n dest = os.path.join(dest, source_name)\n else:\n # Destination was not a directory. We will check to see if the parent\n # dir is a directory, and then (if makedirs=True) attempt to create the\n # parent directory.\n dest_dir, dest_name = os.path.split(dest)\n if run_all(name,\n 'test -d {0}'.format(pipes.quote(dest_dir)),\n **cmd_kwargs)['retcode'] != 0:\n if makedirs:\n result = run_all(name,\n 'mkdir -p {0}'.format(pipes.quote(dest_dir)),\n **cmd_kwargs)\n if result['retcode'] != 0:\n error = ('Unable to create destination directory {0} in '\n 'container \\'{1}\\''.format(dest_dir, name))\n if result['stderr']:\n error += ': {0}'.format(result['stderr'])\n raise CommandExecutionError(error)\n else:\n raise SaltInvocationError(\n 'Directory {0} does not exist on {1} container \\'{2}\\''\n .format(dest_dir, container_type, name)\n )\n if not overwrite and run_all(name,\n 'test -e {0}'.format(pipes.quote(dest)),\n **cmd_kwargs)['retcode'] == 0:\n raise CommandExecutionError(\n 'Destination path {0} already exists. Use overwrite=True to '\n 'overwrite it'.format(dest)\n )\n\n # Before we try to replace the file, compare checksums.\n source_md5 = __salt__['file.get_sum'](local_file, 'md5')\n if source_md5 == _get_md5(name, dest, run_all):\n log.debug('%s and %s:%s are the same file, skipping copy', source, name, dest)\n return True\n\n log.debug('Copying %s to %s container \\'%s\\' as %s',\n source, container_type, name, dest)\n\n # Using cat here instead of opening the file, reading it into memory,\n # and passing it as stdin to run(). This will keep down memory\n # usage for the minion and make the operation run quicker.\n if exec_driver == 'lxc-attach':\n lxcattach = 'lxc-attach'\n if path:\n lxcattach += ' -P {0}'.format(pipes.quote(path))\n copy_cmd = (\n 'cat \"{0}\" | {4} --clear-env --set-var {1} -n {2} -- '\n 'tee \"{3}\"'.format(local_file, PATH, name, dest, lxcattach)\n )\n elif exec_driver == 'nsenter':\n pid = __salt__['{0}.pid'.format(container_type)](name)\n copy_cmd = (\n 'cat \"{0}\" | {1} env -i {2} tee \"{3}\"'\n .format(local_file, _nsenter(pid), PATH, dest)\n )\n elif exec_driver == 'docker-exec':\n copy_cmd = (\n 'cat \"{0}\" | docker exec -i {1} env -i {2} tee \"{3}\"'\n .format(local_file, name, PATH, dest)\n )\n __salt__['cmd.run'](copy_cmd, python_shell=True, output_loglevel='quiet')\n return source_md5 == _get_md5(name, dest, run_all)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexecutes a function on the node", "response": "def execute(opts, data, func, args, kwargs):\n '''\n Directly calls the given function with arguments\n '''\n if data['fun'] == 'saltutil.find_job':\n return __executors__['direct_call.execute'](opts, data, func, args, kwargs)\n if data['fun'] in DOCKER_MOD_MAP:\n return __executors__['direct_call.execute'](opts, data, __salt__[DOCKER_MOD_MAP[data['fun']]], [opts['proxy']['name']] + args, kwargs)\n return __salt__['docker.call'](opts['proxy']['name'], data['fun'], *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nexecutes the salt command line", "response": "def run(self):\n '''\n Execute the salt command line\n '''\n import salt.client\n self.parse_args()\n\n if self.config['log_level'] not in ('quiet', ):\n # Setup file logging!\n self.setup_logfile_logger()\n verify_log(self.config)\n\n try:\n # We don't need to bail on config file permission errors\n # if the CLI process is run with the -a flag\n skip_perm_errors = self.options.eauth != ''\n\n self.local_client = salt.client.get_local_client(\n self.get_config_file_path(),\n skip_perm_errors=skip_perm_errors,\n auto_reconnect=True)\n except SaltClientError as exc:\n self.exit(2, '{0}\\n'.format(exc))\n return\n\n if self.options.batch or self.options.static:\n # _run_batch() will handle all output and\n # exit with the appropriate error condition\n # Execution will not continue past this point\n # in batch mode.\n self._run_batch()\n return\n\n if self.options.preview_target:\n minion_list = self._preview_target()\n self._output_ret(minion_list, self.config.get('output', 'nested'))\n return\n\n if self.options.timeout <= 0:\n self.options.timeout = self.local_client.opts['timeout']\n\n kwargs = {\n 'tgt': self.config['tgt'],\n 'fun': self.config['fun'],\n 'arg': self.config['arg'],\n 'timeout': self.options.timeout,\n 'show_timeout': self.options.show_timeout,\n 'show_jid': self.options.show_jid}\n\n if 'token' in self.config:\n import salt.utils.files\n try:\n with salt.utils.files.fopen(os.path.join(self.config['cachedir'], '.root_key'), 'r') as fp_:\n kwargs['key'] = fp_.readline()\n except IOError:\n kwargs['token'] = self.config['token']\n\n kwargs['delimiter'] = self.options.delimiter\n\n if self.selected_target_option:\n kwargs['tgt_type'] = self.selected_target_option\n else:\n kwargs['tgt_type'] = 'glob'\n\n # If batch_safe_limit is set, check minions matching target and\n # potentially switch to batch execution\n if self.options.batch_safe_limit > 1:\n if len(self._preview_target()) >= self.options.batch_safe_limit:\n salt.utils.stringutils.print_cli('\\nNOTICE: Too many minions targeted, switching to batch execution.')\n self.options.batch = self.options.batch_safe_size\n self._run_batch()\n return\n\n if getattr(self.options, 'return'):\n kwargs['ret'] = getattr(self.options, 'return')\n\n if getattr(self.options, 'return_config'):\n kwargs['ret_config'] = getattr(self.options, 'return_config')\n\n if getattr(self.options, 'return_kwargs'):\n kwargs['ret_kwargs'] = yamlify_arg(\n getattr(self.options, 'return_kwargs'))\n\n if getattr(self.options, 'module_executors'):\n kwargs['module_executors'] = yamlify_arg(getattr(self.options, 'module_executors'))\n\n if getattr(self.options, 'executor_opts'):\n kwargs['executor_opts'] = yamlify_arg(getattr(self.options, 'executor_opts'))\n\n if getattr(self.options, 'metadata'):\n kwargs['metadata'] = yamlify_arg(\n getattr(self.options, 'metadata'))\n\n # If using eauth and a token hasn't already been loaded into\n # kwargs, prompt the user to enter auth credentials\n if 'token' not in kwargs and 'key' not in kwargs and self.options.eauth:\n # This is expensive. Don't do it unless we need to.\n import salt.auth\n resolver = salt.auth.Resolver(self.config)\n res = resolver.cli(self.options.eauth)\n if self.options.mktoken and res:\n tok = resolver.token_cli(\n self.options.eauth,\n res\n )\n if tok:\n kwargs['token'] = tok.get('token', '')\n if not res:\n sys.stderr.write('ERROR: Authentication failed\\n')\n sys.exit(2)\n kwargs.update(res)\n kwargs['eauth'] = self.options.eauth\n\n if self.config['async']:\n jid = self.local_client.cmd_async(**kwargs)\n salt.utils.stringutils.print_cli('Executed command with job ID: {0}'.format(jid))\n return\n\n # local will be None when there was an error\n if not self.local_client:\n return\n\n retcodes = []\n errors = []\n\n try:\n if self.options.subset:\n cmd_func = self.local_client.cmd_subset\n kwargs['sub'] = self.options.subset\n kwargs['cli'] = True\n else:\n cmd_func = self.local_client.cmd_cli\n\n if self.options.progress:\n kwargs['progress'] = True\n self.config['progress'] = True\n ret = {}\n for progress in cmd_func(**kwargs):\n out = 'progress'\n try:\n self._progress_ret(progress, out)\n except LoaderError as exc:\n raise SaltSystemExit(exc)\n if 'return_count' not in progress:\n ret.update(progress)\n self._progress_end(out)\n self._print_returns_summary(ret)\n elif self.config['fun'] == 'sys.doc':\n ret = {}\n out = ''\n for full_ret in self.local_client.cmd_cli(**kwargs):\n ret_, out, retcode = self._format_ret(full_ret)\n ret.update(ret_)\n self._output_ret(ret, out, retcode=retcode)\n else:\n if self.options.verbose:\n kwargs['verbose'] = True\n ret = {}\n for full_ret in cmd_func(**kwargs):\n try:\n ret_, out, retcode = self._format_ret(full_ret)\n retcodes.append(retcode)\n self._output_ret(ret_, out, retcode=retcode)\n ret.update(full_ret)\n except KeyError:\n errors.append(full_ret)\n\n # Returns summary\n if self.config['cli_summary'] is True:\n if self.config['fun'] != 'sys.doc':\n if self.options.output is None:\n self._print_returns_summary(ret)\n self._print_errors_summary(errors)\n\n # NOTE: Return code is set here based on if all minions\n # returned 'ok' with a retcode of 0.\n # This is the final point before the 'salt' cmd returns,\n # which is why we set the retcode here.\n if not all(exit_code == salt.defaults.exitcodes.EX_OK for exit_code in retcodes):\n sys.stderr.write('ERROR: Minions returned with non-zero exit code\\n')\n sys.exit(salt.defaults.exitcodes.EX_GENERIC)\n\n except (AuthenticationError,\n AuthorizationError,\n SaltInvocationError,\n EauthAuthenticationError,\n SaltClientError) as exc:\n ret = six.text_type(exc)\n self._output_ret(ret, '', retcode=1)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nupdate the progress bar and print the progress events", "response": "def _progress_ret(self, progress, out):\n '''\n Print progress events\n '''\n import salt.output\n # Get the progress bar\n if not hasattr(self, 'progress_bar'):\n try:\n self.progress_bar = salt.output.get_progress(self.config, out, progress)\n except Exception:\n raise LoaderError('\\nWARNING: Install the `progressbar` python package. '\n 'Requested job was still run but output cannot be displayed.\\n')\n salt.output.update_progress(self.config, progress, self.progress_bar, out)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _output_ret(self, ret, out, retcode=0):\n '''\n Print the output from a single return to the terminal\n '''\n import salt.output\n # Handle special case commands\n if self.config['fun'] == 'sys.doc' and not isinstance(ret, Exception):\n self._print_docs(ret)\n else:\n # Determine the proper output method and run it\n salt.output.display_output(ret,\n out=out,\n opts=self.config,\n _retcode=retcode)\n if not ret:\n sys.stderr.write('ERROR: No return received\\n')\n sys.exit(2)", "response": "Print the output from a single return to the terminal"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ntake the full return data and format it to simple output", "response": "def _format_ret(self, full_ret):\n '''\n Take the full return data and format it to simple output\n '''\n ret = {}\n out = ''\n retcode = 0\n for key, data in six.iteritems(full_ret):\n ret[key] = data['ret']\n if 'out' in data:\n out = data['out']\n ret_retcode = self._get_retcode(data)\n if ret_retcode > retcode:\n retcode = ret_retcode\n return ret, out, retcode"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndetermine a retcode for a given return", "response": "def _get_retcode(self, 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 if isinstance(ret.get('retcode', 0), dict):\n return max(six.itervalues(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": "Can you tell what is the following Python 3 function doing\ndef _print_docs(self, ret):\n '''\n Print out the docstrings for all of the functions on the minions\n '''\n import salt.output\n docs = {}\n if not ret:\n self.exit(2, 'No minions found to gather docs from\\n')\n if isinstance(ret, six.string_types):\n self.exit(2, '{0}\\n'.format(ret))\n for host in ret:\n if isinstance(ret[host], six.string_types) \\\n and (ret[host].startswith(\"Minion did not return\")\n or ret[host] == 'VALUE_TRIMMED'):\n continue\n for fun in ret[host]:\n if fun not in docs and ret[host][fun]:\n docs[fun] = ret[host][fun]\n if self.options.output:\n for fun in sorted(docs):\n salt.output.display_output({fun: docs[fun]}, 'nested', self.config)\n else:\n for fun in sorted(docs):\n salt.utils.stringutils.print_cli('{0}:'.format(fun))\n salt.utils.stringutils.print_cli(docs[fun])\n salt.utils.stringutils.print_cli('')", "response": "Print out the docstrings for all of the functions on the minions\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef yaml_dquote(text):\n '''\n Make text into a double-quoted YAML string with correct escaping\n for special characters. Includes the opening and closing double\n quote characters.\n '''\n with io.StringIO() as ostream:\n yemitter = yaml.emitter.Emitter(ostream, width=six.MAXSIZE)\n yemitter.write_double_quoted(six.text_type(text))\n return ostream.getvalue()", "response": "Make text into a double - quoted YAML string with correct escaping for special characters."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nmake text into a single - quoted YAML string with correct escaping for special characters.", "response": "def yaml_squote(text):\n '''\n Make text into a single-quoted YAML string with correct escaping\n for special characters. Includes the opening and closing single\n quote characters.\n '''\n with io.StringIO() as ostream:\n yemitter = yaml.emitter.Emitter(ostream, width=six.MAXSIZE)\n yemitter.write_single_quoted(six.text_type(text))\n return ostream.getvalue()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef yaml_encode(data):\n '''\n A simple YAML encode that can take a single-element datatype and return\n a string representation.\n '''\n yrepr = yaml.representer.SafeRepresenter()\n ynode = yrepr.represent_data(data)\n if not isinstance(ynode, yaml.ScalarNode):\n raise TypeError(\n \"yaml_encode() only works with YAML scalar data;\"\n \" failed for {0}\".format(type(data))\n )\n\n tag = ynode.tag.rsplit(':', 1)[-1]\n ret = ynode.value\n\n if tag == \"str\":\n ret = yaml_dquote(ynode.value)\n\n return ret", "response": "A simple YAML encode that can take a single - element datatype and return\n a string representation."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _decrypt_object(obj, **kwargs):\n '''\n Recursively try to decrypt any object. If the object is a six.string_types\n (string or unicode), and it contains a valid NACLENC pretext, decrypt it,\n otherwise keep going until a string is found.\n '''\n if salt.utils.stringio.is_readable(obj):\n return _decrypt_object(obj.getvalue(), **kwargs)\n if isinstance(obj, six.string_types):\n if re.search(NACL_REGEX, obj) is not None:\n return __salt__['nacl.dec'](re.search(NACL_REGEX, obj).group(1), **kwargs)\n else:\n return obj\n elif isinstance(obj, dict):\n for key, value in six.iteritems(obj):\n obj[key] = _decrypt_object(value, **kwargs)\n return obj\n elif isinstance(obj, list):\n for key, value in enumerate(obj):\n obj[key] = _decrypt_object(value, **kwargs)\n return obj\n else:\n return obj", "response": "Recursively try to decrypt any object in the object tree"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nexecute queries against MySQL merge and return as a dict", "response": "def ext_pillar(minion_id,\n pillar,\n *args,\n **kwargs):\n '''\n Execute queries against MySQL, merge and return as a dict\n '''\n return MySQLExtPillar().fetch(minion_id, pillar, *args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_options(self):\n '''\n Returns options used for the MySQL connection.\n '''\n defaults = {'host': 'localhost',\n 'user': 'salt',\n 'pass': 'salt',\n 'db': 'salt',\n 'port': 3306,\n 'ssl': {}}\n _options = {}\n _opts = __opts__.get('mysql', {})\n for attr in defaults:\n if attr not in _opts:\n log.debug('Using default for MySQL %s', attr)\n _options[attr] = defaults[attr]\n continue\n _options[attr] = _opts[attr]\n return _options", "response": "Returns options used for the MySQL connection."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_cursor(self):\n '''\n Yield a MySQL cursor\n '''\n _options = self._get_options()\n conn = MySQLdb.connect(host=_options['host'],\n user=_options['user'],\n passwd=_options['pass'],\n db=_options['db'], port=_options['port'],\n ssl=_options['ssl'])\n cursor = conn.cursor()\n try:\n yield cursor\n except MySQLdb.DatabaseError as err:\n log.exception('Error in ext_pillar MySQL: %s', err.args)\n finally:\n conn.close()", "response": "Yield a MySQL cursor"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns an option based on key merging all matches.", "response": "def merge(value,\n default='',\n omit_opts=False,\n omit_master=False,\n omit_pillar=False):\n '''\n Retrieves an option based on key, merging all matches.\n\n Same as ``option()`` except that it merges all matches, rather than taking\n the first match.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' config.merge schedule\n '''\n ret = None\n if not omit_opts:\n if value in __opts__:\n ret = __opts__[value]\n if isinstance(ret, six.string_types):\n return ret\n if not omit_master:\n if value in __pillar__.get('master', {}):\n tmp = __pillar__['master'][value]\n if ret is None:\n ret = tmp\n if isinstance(ret, six.string_types):\n return ret\n elif isinstance(ret, dict) and isinstance(tmp, dict):\n tmp.update(ret)\n ret = tmp\n elif (isinstance(ret, (list, tuple)) and\n isinstance(tmp, (list, tuple))):\n ret = list(ret) + list(tmp)\n if not omit_pillar:\n if value in __pillar__:\n tmp = __pillar__[value]\n if ret is None:\n ret = tmp\n if isinstance(ret, six.string_types):\n return ret\n elif isinstance(ret, dict) and isinstance(tmp, dict):\n tmp.update(ret)\n ret = tmp\n elif (isinstance(ret, (list, tuple)) and\n isinstance(tmp, (list, tuple))):\n ret = list(ret) + list(tmp)\n if ret is None and value in DEFAULTS:\n return DEFAULTS[value]\n return ret or default"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting a named value from the master config", "response": "def get(key, default=''):\n '''\n .. versionadded: 0.14.0\n\n Attempt to retrieve the named value from opts, pillar, grains of the master\n config, if the named value is not available return the passed default.\n The default return is an empty string.\n\n The value can also represent a value in a nested dict using a \":\" delimiter\n for the dict. This means that if a dict looks like this::\n\n {'pkg': {'apache': 'httpd'}}\n\n To retrieve the value associated with the apache key in the pkg dict this\n key can be passed::\n\n pkg:apache\n\n This routine traverses these data stores in this order:\n\n - Local minion config (opts)\n - Minion's grains\n - Minion's pillar\n - Master config\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' config.get pkg:apache\n '''\n ret = salt.utils.data.traverse_dict_and_list(__opts__, key, '_|-')\n if ret != '_|-':\n return ret\n ret = salt.utils.data.traverse_dict_and_list(__grains__, key, '_|-')\n if ret != '_|-':\n return ret\n ret = salt.utils.data.traverse_dict_and_list(__pillar__, key, '_|-')\n if ret != '_|-':\n return ret\n ret = salt.utils.data.traverse_dict_and_list(__pillar__.get('master', {}), key, '_|-')\n if ret != '_|-':\n return ret\n return default"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nensuring that the action is present in Zabbix", "response": "def present(name, params, **kwargs):\n '''\n Creates Zabbix Action object or if differs update it according defined parameters\n\n :param name: Zabbix Action name\n :param params: Definition of the Zabbix Action\n :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)\n :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)\n :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)\n\n If there is a need to get a value from current zabbix online (e.g. id of a hostgroup you want to put a discovered\n system into), put a dictionary with two keys \"query_object\" and \"query_name\" instead of the value.\n In this example we want to get object id of hostgroup named \"Virtual machines\" and \"Databases\".\n\n .. code-block:: yaml\n\n zabbix-action-present:\n zabbix_action.present:\n - name: VMs\n - params:\n eventsource: 2\n status: 0\n filter:\n evaltype: 2\n conditions:\n - conditiontype: 24\n operator: 2\n value: 'virtual'\n - conditiontype: 24\n operator: 2\n value: 'kvm'\n operations:\n - operationtype: 2\n - operationtype: 4\n opgroup:\n - groupid:\n query_object: hostgroup\n query_name: Virtual machines\n - groupid:\n query_object: hostgroup\n query_name: Databases\n '''\n zabbix_id_mapper = __salt__['zabbix.get_zabbix_id_mapper']()\n\n dry_run = __opts__['test']\n ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}\n\n # Create input params substituting functions with their results\n params['name'] = name\n params['operations'] = params['operations'] if 'operations' in params else []\n if 'filter' in params:\n params['filter']['conditions'] = params['filter']['conditions'] if 'conditions' in params['filter'] else []\n\n input_params = __salt__['zabbix.substitute_params'](params, **kwargs)\n log.info('Zabbix Action: input params: %s', six.text_type(json.dumps(input_params, indent=4)))\n\n search = {'output': 'extend',\n 'selectOperations': 'extend',\n 'selectFilter': 'extend',\n 'filter': {\n 'name': name\n }}\n # GET Action object if exists\n action_get = __salt__['zabbix.run_query']('action.get', search, **kwargs)\n log.info('Zabbix Action: action.get result: %s', six.text_type(json.dumps(action_get, indent=4)))\n\n existing_obj = __salt__['zabbix.substitute_params'](action_get[0], **kwargs) \\\n if action_get and len(action_get) == 1 else False\n\n if existing_obj:\n diff_params = __salt__['zabbix.compare_params'](input_params, existing_obj)\n log.info('Zabbix Action: input params: {%s', six.text_type(json.dumps(input_params, indent=4)))\n log.info('Zabbix Action: Object comparison result. Differences: %s', six.text_type(diff_params))\n\n if diff_params:\n diff_params[zabbix_id_mapper['action']] = existing_obj[zabbix_id_mapper['action']]\n # diff_params['name'] = 'VMs' - BUG - https://support.zabbix.com/browse/ZBX-12078\n log.info('Zabbix Action: update params: %s', six.text_type(json.dumps(diff_params, indent=4)))\n\n if dry_run:\n ret['result'] = True\n ret['comment'] = 'Zabbix Action \"{0}\" would be fixed.'.format(name)\n ret['changes'] = {name: {'old': 'Zabbix Action \"{0}\" differs '\n 'in following parameters: {1}'.format(name, diff_params),\n 'new': 'Zabbix Action \"{0}\" would correspond to definition.'.format(name)}}\n else:\n action_update = __salt__['zabbix.run_query']('action.update', diff_params, **kwargs)\n log.info('Zabbix Action: action.update result: %s', six.text_type(action_update))\n if action_update:\n ret['result'] = True\n ret['comment'] = 'Zabbix Action \"{0}\" updated.'.format(name)\n ret['changes'] = {name: {'old': 'Zabbix Action \"{0}\" differed '\n 'in following parameters: {1}'.format(name, diff_params),\n 'new': 'Zabbix Action \"{0}\" fixed.'.format(name)}}\n\n else:\n ret['result'] = True\n ret['comment'] = 'Zabbix Action \"{0}\" already exists and corresponds to a definition.'.format(name)\n\n else:\n if dry_run:\n ret['result'] = True\n ret['comment'] = 'Zabbix Action \"{0}\" would be created.'.format(name)\n ret['changes'] = {name: {'old': 'Zabbix Action \"{0}\" does not exist.'.format(name),\n 'new': 'Zabbix Action \"{0}\" would be created according definition.'.format(name)}}\n else:\n # ACTION.CREATE\n action_create = __salt__['zabbix.run_query']('action.create', input_params, **kwargs)\n log.info('Zabbix Action: action.create result: %s', action_create)\n\n if action_create:\n ret['result'] = True\n ret['comment'] = 'Zabbix Action \"{0}\" created.'.format(name)\n ret['changes'] = {name: {'old': 'Zabbix Action \"{0}\" did not exist.'.format(name),\n 'new': 'Zabbix Action \"{0}\" created according definition.'.format(name)}}\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef available(name):\n '''\n Return True if the named service is available.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.available sshd\n '''\n cmd = '{0} get {1}'.format(_cmd(), name)\n if __salt__['cmd.retcode'](cmd) == 2:\n return False\n return True", "response": "Return True if the named service is available."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_all():\n '''\n Return all installed services.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.get_all\n '''\n ret = []\n service = _cmd()\n for svc in __salt__['cmd.run']('{0} ls all'.format(service)).splitlines():\n ret.append(svc)\n return sorted(ret)", "response": "Return all installed services."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the status of a service", "response": "def status(name, sig=None):\n '''\n Return the status for a service, returns a bool whether the service is\n running.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.status <service name>\n '''\n if sig:\n return bool(__salt__['status.pid'](sig))\n\n cmd = '{0} check {1}'.format(_cmd(), name)\n return not __salt__['cmd.retcode'](cmd)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef enable(name, **kwargs):\n '''\n Enable the named service to start at boot.\n\n flags : None\n Set optional flags to run the service with.\n\n service.flags can be used to change the default flags.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.enable <service name>\n salt '*' service.enable <service name> flags=<flags>\n '''\n stat_cmd = '{0} set {1} status on'.format(_cmd(), name)\n stat_retcode = __salt__['cmd.retcode'](stat_cmd)\n\n flag_retcode = None\n # only (re)set flags for services that have an rc.d(8) script\n if os.path.exists('/etc/rc.d/{0}'.format(name)):\n flags = _get_flags(**kwargs)\n flag_cmd = '{0} set {1} flags {2}'.format(_cmd(), name, flags)\n flag_retcode = __salt__['cmd.retcode'](flag_cmd)\n\n return not any([stat_retcode, flag_retcode])", "response": "Enable the named service to start at boot."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef enabled(name, **kwargs):\n '''\n Return True if the named service is enabled at boot and the provided\n flags match the configured ones (if any). Return False otherwise.\n\n name\n Service name\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.enabled <service name>\n salt '*' service.enabled <service name> flags=<flags>\n '''\n cmd = '{0} get {1} status'.format(_cmd(), name)\n if not __salt__['cmd.retcode'](cmd):\n # also consider a service disabled if the current flags are different\n # than the configured ones so we have a chance to update them\n flags = _get_flags(**kwargs)\n cur_flags = __salt__['cmd.run_stdout']('{0} get {1} flags'.format(_cmd(), name))\n if format(flags) == format(cur_flags):\n return True\n if not flags:\n def_flags = __salt__['cmd.run_stdout']('{0} getdef {1} flags'.format(_cmd(), name))\n if format(cur_flags) == format(def_flags):\n return True\n\n return False", "response": "Return True if the named service is enabled at boot and the provided\n flags match the configured ones. Return False otherwise."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _expand_users(device_users, common_users):\n\n '''Creates a longer list of accepted users on the device.'''\n\n expected_users = deepcopy(common_users)\n expected_users.update(device_users)\n\n return expected_users", "response": "Creates a longer list of accepted users on the device."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks if the input dictionary of users is valid.", "response": "def _check_users(users):\n\n '''Checks if the input dictionary of users is valid.'''\n\n messg = ''\n valid = True\n\n for user, user_details in six.iteritems(users):\n if not user_details:\n valid = False\n messg += 'Please provide details for username {user}.\\n'.format(user=user)\n continue\n if not (isinstance(user_details.get('level'), int) or 0 <= user_details.get('level') <= 15):\n # warn!\n messg += 'Level must be a integer between 0 and 15 for username {user}. Will assume 0.\\n'.format(user=user)\n\n return valid, messg"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncomputing the differences between the actual config and the expected config", "response": "def _compute_diff(configured, expected):\n\n '''Computes the differences between the actual config and the expected config'''\n\n diff = {\n 'add': {},\n 'update': {},\n 'remove': {}\n }\n\n configured_users = set(configured.keys())\n expected_users = set(expected.keys())\n\n add_usernames = expected_users - configured_users\n remove_usernames = configured_users - expected_users\n common_usernames = expected_users & configured_users\n\n add = dict((username, expected.get(username)) for username in add_usernames)\n remove = dict((username, configured.get(username)) for username in remove_usernames)\n update = {}\n\n for username in common_usernames:\n user_configuration = configured.get(username)\n user_expected = expected.get(username)\n if user_configuration == user_expected:\n continue\n update[username] = {}\n for field, field_value in six.iteritems(user_expected):\n if user_configuration.get(field) != field_value:\n update[username][field] = field_value\n\n diff.update({\n 'add': add,\n 'update': update,\n 'remove': remove\n })\n\n return diff"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nmanage the configuration of the users on the device, as specified in the state SLS file. Users not defined in that file will be remove whilst users not configured on the device, will be added. SLS Example: .. code-block:: yaml netusers_example: netusers.managed: - users: admin: level: 15 password: $1$knmhgPPv$g8745biu4rb.Zf.IT.F/U1 sshkeys: [] restricted: level: 1 password: $1$j34j5k4b$4d5SVjTiz1l.Zf.IT.F/K7 martin: level: 15 password: '' sshkeys: - ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4vwWHh0w JPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZKtCjO8LhbWCa+ X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87Oz1nKsKuNzm2csoUQlJ trmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwxM570s35Of/vV0zoOccj753sXn pvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t+wAAAIBURwSPZVElXe+9a43sF6M4ysT 7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0 bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v+zvMmv8KvQgHg jonathan: level: 15 password: '' sshkeys: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgGR9zPkHG ZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qaoqwpLB15GwLfEX Bx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006xeHh7rv7HtXF6zH3WId Uhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9xZBq6DBb+sESMS4s7nFcsruMo edb+BAc3aww0naeWpogjSt+We7y2N CLI Example: salt 'edge01.kix01' state.sls router.users Output example (raw python - can be reused in other modules): .. code-block:: python { 'netusers_|-netusers_example_|-netusers_example_|-managed': { 'comment': 'Configuration updated!', 'name': 'netusers_example', 'start_time': '10:57:08.678811', '__id__': 'netusers_example', 'duration': 1620.982, '__run_num__': 0, 'changes': { 'updated': { 'admin': { 'level': 15 }, 'restricted': { 'level': 1 }, 'martin': { 'sshkeys': [ 'ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4vwWHh0w JPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZKtCjO8LhbWCa+ X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87Oz1nKsKuNzm2csoUQlJ trmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwxM570s35Of/vV0zoOccj753sXn pvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t+wAAAIBURwSPZVElXe+9a43sF6M4ysT 7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0 bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v+zvMmv8KvQgHg' ] } }, 'added': { 'jonathan': { 'password': '', 'sshkeys': [ 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgGR9zPkHG ZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qaoqwpLB15GwLfEX Bx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006xeHh7rv7HtXF6zH3WId Uhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9xZBq6DBb+sESMS4s7nFcsruMo edb+BAc3aww0naeWpogjSt+We7y2N' ], 'level': 15 } }, 'removed': { } }, 'result': True } } CLI Output: .. code-block:: bash edge01.kix01: ---------- ID: netusers_example Function: netusers.managed Result: True Comment: Configuration updated! Started: 11:03:31.957725 Duration: 1220.435 ms Changes: ---------- added: ---------- jonathan: ---------- level: 15 password: sshkeys: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgG R9zPkHGZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qao qwpLB15GwLfEXBx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006 xeHh7rv7HtXF6zH3WIdUhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9 xZBq6DBb+sESMS4s7nFcsruMoedb+BAc3aww0naeWpogjSt+We7y2N removed: ---------- updated: ---------- martin: ---------- sshkeys: - ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4 vwWHh0wJPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZ KtCjO8LhbWCa+X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87 Oz1nKsKuNzm2csoUQlJtrmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwx M570s35Of/vV0zoOccj753sXnpvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t +wAAAIBURwSPZVElXe+9a43sF6M4ysT7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac 81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v +zvMmv8KvQgHg admin: ---------- level: 15 restricted: ---------- level: 1 Summary for edge01.kix01 ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 Total run time: 1.220 s", "response": "def managed(name, users=None, defaults=None):\n\n '''\n Manages the configuration of the users on the device, as specified in the state SLS file. Users not defined in that\n file will be remove whilst users not configured on the device, will be added.\n\n SLS Example:\n\n .. code-block:: yaml\n\n netusers_example:\n netusers.managed:\n - users:\n admin:\n level: 15\n password: $1$knmhgPPv$g8745biu4rb.Zf.IT.F/U1\n sshkeys: []\n restricted:\n level: 1\n password: $1$j34j5k4b$4d5SVjTiz1l.Zf.IT.F/K7\n martin:\n level: 15\n password: ''\n sshkeys:\n - ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4vwWHh0w\n JPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZKtCjO8LhbWCa+\n X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87Oz1nKsKuNzm2csoUQlJ\n trmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwxM570s35Of/vV0zoOccj753sXn\n pvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t+wAAAIBURwSPZVElXe+9a43sF6M4ysT\n 7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0\n bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v+zvMmv8KvQgHg\n jonathan:\n level: 15\n password: ''\n sshkeys:\n - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgGR9zPkHG\n ZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qaoqwpLB15GwLfEX\n Bx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006xeHh7rv7HtXF6zH3WId\n Uhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9xZBq6DBb+sESMS4s7nFcsruMo\n edb+BAc3aww0naeWpogjSt+We7y2N\n\n CLI Example:\n\n salt 'edge01.kix01' state.sls router.users\n\n Output example (raw python - can be reused in other modules):\n\n .. code-block:: python\n\n {\n 'netusers_|-netusers_example_|-netusers_example_|-managed': {\n 'comment': 'Configuration updated!',\n 'name': 'netusers_example',\n 'start_time': '10:57:08.678811',\n '__id__': 'netusers_example',\n 'duration': 1620.982,\n '__run_num__': 0,\n 'changes': {\n 'updated': {\n 'admin': {\n 'level': 15\n },\n 'restricted': {\n 'level': 1\n },\n 'martin': {\n 'sshkeys': [\n 'ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4vwWHh0w\n JPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZKtCjO8LhbWCa+\n X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87Oz1nKsKuNzm2csoUQlJ\n trmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwxM570s35Of/vV0zoOccj753sXn\n pvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t+wAAAIBURwSPZVElXe+9a43sF6M4ysT\n 7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0\n bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v+zvMmv8KvQgHg'\n ]\n }\n },\n 'added': {\n 'jonathan': {\n 'password': '',\n 'sshkeys': [\n 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgGR9zPkHG\n ZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qaoqwpLB15GwLfEX\n Bx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006xeHh7rv7HtXF6zH3WId\n Uhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9xZBq6DBb+sESMS4s7nFcsruMo\n edb+BAc3aww0naeWpogjSt+We7y2N'\n ],\n 'level': 15\n }\n },\n 'removed': {\n }\n },\n 'result': True\n }\n }\n\n CLI Output:\n\n .. code-block:: bash\n\n edge01.kix01:\n ----------\n ID: netusers_example\n Function: netusers.managed\n Result: True\n Comment: Configuration updated!\n Started: 11:03:31.957725\n Duration: 1220.435 ms\n Changes:\n ----------\n added:\n ----------\n jonathan:\n ----------\n level:\n 15\n password:\n sshkeys:\n - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcgxE6HZF/xjFtIt0thEDKPjFJxW9BpZtTVstYbDgG\n R9zPkHGZJT/j345jk345jk453jk43545j35nl3kln34n5kl4ghv3/JzWt/0Js5KZp/51KRNCs9O4t07qao\n qwpLB15GwLfEXBx9dW26zc4O+hi6754trxcfghvjbo98765drt/LYIEg0KSQPWyJEK1g31gacbxN7Ab006\n xeHh7rv7HtXF6zH3WIdUhq9rtdUag6kYnv6qvjG7sbCyHGYu5vZB7GytnNuVNbZuI+RdFvmHSnErV9HCu9\n xZBq6DBb+sESMS4s7nFcsruMoedb+BAc3aww0naeWpogjSt+We7y2N\n removed:\n ----------\n updated:\n ----------\n martin:\n ----------\n sshkeys:\n - ssh-dss AAAAB3NzaC1kc3MAAACBAK9dP3KariMlM/JmFW9rTSm5cXs4nR0+o6fTHP9o+bOLXMBTP8R4\n vwWHh0wJPjQmJYafAqZTnlgi0srGjyifFwPtODppDWLCgLe2M4LXnu3OMqknr54w344zPHP3iFwWxHrBrZ\n KtCjO8LhbWCa+X528+i87t6r5e4ersdfxgchvjbknlio87t6r5drcfhgjhbknio8976tycv7t86ftyiu87\n Oz1nKsKuNzm2csoUQlJtrmRfpjsOPNookmOz5wG0YxhwDmKeo6fWK+ATk1OiP+QT39fn4G77j8o+e4WAwx\n M570s35Of/vV0zoOccj753sXnpvJenvwpM2H6o3a9ALvehAJKWodAgZT7X8+iu786r5drtycghvjbiu78t\n +wAAAIBURwSPZVElXe+9a43sF6M4ysT7Xv+6wTsa8q86E3+RYyu8O2ObI2kwNLC3/HTgFniE/YqRG+WJac\n 81/VHWQNP822gns8RVrWKjqBktmQoEm7z5yy0bkjui78675dytcghvjkoi9y7t867ftcuvhbuu9t78gy/v\n +zvMmv8KvQgHg\n admin:\n ----------\n level:\n 15\n restricted:\n ----------\n level:\n 1\n Summary for edge01.kix01\n ------------\n Succeeded: 1 (changed=1)\n Failed: 0\n ------------\n Total states run: 1\n Total run time: 1.220 s\n '''\n\n result = False\n comment = ''\n changes = {}\n\n ret = {\n 'name': name,\n 'changes': changes,\n 'result': result,\n 'comment': comment\n }\n\n users = _ordered_dict_to_dict(users)\n defaults = _ordered_dict_to_dict(defaults)\n\n expected_users = _expand_users(users, defaults)\n valid, message = _check_users(expected_users)\n\n if not valid: # check and clean\n ret['comment'] = 'Please provide a valid configuration: {error}'.format(error=message)\n return ret\n\n # ----- Retrieve existing users configuration and determine differences ------------------------------------------->\n\n users_output = _retrieve_users()\n if not users_output.get('result'):\n ret['comment'] = 'Cannot retrieve users from the device: {reason}'.format(\n reason=users_output.get('comment')\n )\n return ret\n\n configured_users = users_output.get('out', {})\n\n if configured_users == expected_users:\n ret.update({\n 'comment': 'Users already configured as needed.',\n 'result': True\n })\n return ret\n\n diff = _compute_diff(configured_users, expected_users)\n\n users_to_add = diff.get('add', {})\n users_to_update = diff.get('update', {})\n users_to_remove = diff.get('remove', {})\n\n changes = {\n 'added': users_to_add,\n 'updated': users_to_update,\n 'removed': users_to_remove\n }\n\n ret.update({\n 'changes': changes\n })\n\n if __opts__['test'] is True:\n ret.update({\n 'result': None,\n 'comment': 'Testing mode: configuration was not changed!'\n })\n return ret\n\n # <---- Retrieve existing NTP peers and determine peers to be added/removed --------------------------------------->\n\n # ----- Call _set_users and _delete_users as needed --------------------------------------------------------------->\n\n expected_config_change = False\n successfully_changed = True\n\n if users_to_add:\n _set = _set_users(users_to_add)\n if _set.get('result'):\n expected_config_change = True\n else: # something went wrong...\n successfully_changed = False\n comment += 'Cannot configure new users: {reason}'.format(\n reason=_set.get('comment')\n )\n\n if users_to_update:\n _update = _update_users(users_to_update)\n if _update.get('result'):\n expected_config_change = True\n else: # something went wrong...\n successfully_changed = False\n comment += 'Cannot update the users configuration: {reason}'.format(\n reason=_update.get('comment')\n )\n\n if users_to_remove:\n _delete = _delete_users(users_to_remove)\n if _delete.get('result'):\n expected_config_change = True\n else: # something went wrong...\n successfully_changed = False\n comment += 'Cannot remove users: {reason}'.format(\n reason=_delete.get('comment')\n )\n\n # <---- Call _set_users and _delete_users as needed ----------------------------------------------------------------\n\n # ----- Try to commit changes ------------------------------------------------------------------------------------->\n\n if expected_config_change and successfully_changed:\n config_result, config_comment = __salt__['net.config_control']()\n result = config_result\n comment += config_comment\n\n # <---- Try to commit changes --------------------------------------------------------------------------------------\n\n if expected_config_change and result and not comment:\n comment = 'Configuration updated!'\n\n ret.update({\n 'result': result,\n 'comment': comment\n })\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nchecks if the client has sent a ready message and sends it to the server.", "response": "def received_message(self, message):\n '''\n Checks if the client has sent a ready message.\n A ready message causes ``send()`` to be called on the\n ``parent end`` of the pipe.\n\n Clients need to ensure that the pipe assigned to ``self.pipe`` is\n the ``parent end`` of a pipe.\n\n This ensures completion of the underlying websocket connection\n and can be used to synchronize parallel senders.\n '''\n if message.data.decode('utf-8') == 'websocket client ready':\n self.pipe.send(message)\n self.send('server received message', False)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nprinting an info on CLI with the title.", "response": "def cli_info(data, title='Info'):\n '''\n Prints an info on CLI with the title.\n Useful for infos, general errors etc.\n\n :param data:\n :param title:\n :return:\n '''\n\n wrapper = textwrap.TextWrapper()\n wrapper.initial_indent = ' ' * 4\n wrapper.subsequent_indent = wrapper.initial_indent\n\n return '{title}:\\n\\n{text}'.format(title=title, text=wrapper.fill(data))"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_users(profile='pagerduty', subdomain=None, api_key=None):\n '''\n List users belonging to this account\n\n CLI Example:\n\n salt myminion pagerduty.get_users\n '''\n\n return _list_items(\n 'users',\n 'id',\n profile=profile,\n subdomain=subdomain,\n api_key=api_key,\n )", "response": "List users belonging to this account"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nlisting all services belonging to this account", "response": "def get_services(profile='pagerduty', subdomain=None, api_key=None):\n '''\n List services belonging to this account\n\n CLI Example:\n\n salt myminion pagerduty.get_services\n '''\n\n return _list_items(\n 'services',\n 'id',\n profile=profile,\n subdomain=subdomain,\n api_key=api_key,\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nlist the schedules belonging to this account", "response": "def get_schedules(profile='pagerduty', subdomain=None, api_key=None):\n '''\n List schedules belonging to this account\n\n CLI Example:\n\n salt myminion pagerduty.get_schedules\n '''\n\n return _list_items(\n 'schedules',\n 'id',\n profile=profile,\n subdomain=subdomain,\n api_key=api_key,\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_escalation_policies(profile='pagerduty', subdomain=None, api_key=None):\n '''\n List escalation_policies belonging to this account\n\n CLI Example:\n\n salt myminion pagerduty.get_escalation_policies\n '''\n\n return _list_items(\n 'escalation_policies',\n 'id',\n profile=profile,\n subdomain=subdomain,\n api_key=api_key,\n )", "response": "List escalation_policies belonging to this account"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _list_items(action, key, profile=None, subdomain=None, api_key=None):\n '''\n List items belonging to an API call.\n\n This method should be in utils.pagerduty.\n '''\n items = _query(\n profile=profile,\n subdomain=subdomain,\n api_key=api_key,\n action=action\n )\n ret = {}\n for item in items[action]:\n ret[item[key]] = item\n return ret", "response": "List items belonging to an API call."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _query(method='GET', profile=None, url=None, path='api/v1',\n action=None, api_key=None, service=None, params=None,\n data=None, subdomain=None, verify_ssl=True):\n '''\n Query the PagerDuty API.\n\n This method should be in utils.pagerduty.\n\n '''\n\n if profile:\n creds = __salt__['config.option'](profile)\n else:\n creds = {\n 'pagerduty.api_key': api_key,\n 'pagerduty.subdomain': subdomain,\n }\n\n if url is None:\n url = 'https://{0}.pagerduty.com/{1}/{2}'.format(\n creds['pagerduty.subdomain'],\n path,\n action\n )\n\n if params is None:\n params = {}\n\n if data is None:\n data = {}\n\n headers = {\n 'Authorization': 'Token token={0}'.format(creds['pagerduty.api_key'])\n }\n\n if method != 'GET':\n headers['Content-type'] = 'application/json'\n\n result = requests.request(\n method,\n url,\n headers=headers,\n params=params,\n data=salt.utils.json.dumps(data),\n verify=verify_ssl\n )\n\n if result.text is None or result.text == '':\n return None\n result_json = result.json()\n # if this query supports pagination, loop and fetch all results, merge them together\n if 'total' in result_json and 'offset' in result_json and 'limit' in result_json:\n offset = result_json['offset']\n limit = result_json['limit']\n total = result_json['total']\n while offset + limit < total:\n offset = offset + limit\n limit = 100\n data['offset'] = offset\n data['limit'] = limit\n next_page_results = requests.request(method,\n url,\n headers=headers,\n params=params,\n data=data, # Already serialized above, don't do it again\n verify=verify_ssl).json()\n offset = next_page_results['offset']\n limit = next_page_results['limit']\n # merge results\n for k, v in result_json.items():\n if isinstance(v, list):\n result_json[k] += next_page_results[k]\n return result_json", "response": "Query the PagerDuty API."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget any single pagerduty resource by key.", "response": "def get_resource(resource_name, key, identifier_fields, profile='pagerduty', subdomain=None, api_key=None):\n '''\n Get any single pagerduty resource by key.\n\n We allow flexible lookup by any of a list of identifier_fields.\n So, for example, you can look up users by email address or name by calling:\n\n get_resource('users', key, ['name', 'email'], ...)\n\n This method is mainly used to translate state sls into pagerduty id's for dependent objects.\n For example, a pagerduty escalation policy contains one or more schedules, which must be passed\n by their pagerduty id. We look up the schedules by name (using this method), and then translate\n the names into id's.\n\n This method is implemented by getting all objects of the resource type (cached into __context__),\n then brute force searching through the list and trying to match any of the identifier_fields.\n The __context__ cache is purged after any create, update or delete to the resource.\n '''\n # cache the expensive 'get all resources' calls into __context__ so that we do them once per salt run\n if 'pagerduty_util.resource_cache' not in __context__:\n __context__['pagerduty_util.resource_cache'] = {}\n if resource_name not in __context__['pagerduty_util.resource_cache']:\n if resource_name == 'services':\n action = resource_name + '?include[]=escalation_policy'\n else:\n action = resource_name\n __context__['pagerduty_util.resource_cache'][resource_name] = _query(action=action,\n profile=profile,\n subdomain=subdomain,\n api_key=api_key)[resource_name]\n for resource in __context__['pagerduty_util.resource_cache'][resource_name]:\n for field in identifier_fields:\n if resource[field] == key:\n # PagerDuty's /schedules endpoint returns less data than /schedules/:id.\n # so, now that we found the schedule, we need to get all the data for it.\n if resource_name == 'schedules':\n full_resource_info = _query(action='{0}/{1}'.format(resource_name, resource['id']),\n profile=profile,\n subdomain=subdomain,\n api_key=api_key)\n return full_resource_info\n return resource\n return None"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating or update a PagerDuty resource.", "response": "def create_or_update_resource(resource_name, identifier_fields, data, diff=None, profile='pagerduty', subdomain=None, api_key=None):\n '''\n create or update any pagerduty resource\n Helper method for present().\n\n Determining if two resources are the same is different for different PD resource, so this method accepts a diff function.\n The diff function will be invoked as diff(state_information, object_returned_from_pagerduty), and\n should return a dict of data to pass to the PagerDuty update API method, or None if no update\n is to be performed. If no diff method is provided, the default behavor is to scan the keys in the state_information,\n comparing the matching values in the object_returned_from_pagerduty, and update any values that differ.\n\n examples:\n create_or_update_resource(\"user\", [\"id\",\"name\",\"email\"])\n create_or_update_resource(\"escalation_policies\", [\"id\",\"name\"], diff=my_diff_function)\n\n '''\n # try to locate the resource by any of the identifier_fields that are specified in data\n resource = None\n for field in identifier_fields:\n if field in data:\n resource = get_resource(resource_name, data[field], identifier_fields, profile, subdomain, api_key)\n if resource is not None:\n break\n\n if resource is None:\n if __opts__['test']:\n return 'would create'\n # flush the resource_cache, because we're modifying a resource\n del __context__['pagerduty_util.resource_cache'][resource_name]\n # create\n return _query(method='POST', action=resource_name, data=data, profile=profile, subdomain=subdomain, api_key=api_key)\n else:\n # update\n data_to_update = {}\n # if differencing function is provided, use it\n if diff:\n data_to_update = diff(data, resource)\n # else default to naive key-value walk of the dicts\n else:\n for k, v in data.items():\n if k.startswith('_'):\n continue\n resource_value = resource.get(k, None)\n if resource_value is not None and resource_value != v:\n data_to_update[k] = v\n if data_to_update:\n if __opts__['test']:\n return 'would update'\n # flush the resource_cache, because we're modifying a resource\n del __context__['pagerduty_util.resource_cache'][resource_name]\n resource_id = _get_resource_id(resource)\n return _query(method='PUT', action='{0}/{1}'.format(resource_name, resource_id), data=data_to_update,\n profile=profile, subdomain=subdomain, api_key=api_key)\n else:\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndelete a resource by id or name or email", "response": "def delete_resource(resource_name, key, identifier_fields, profile='pagerduty', subdomain=None, api_key=None):\n '''\n delete any pagerduty resource\n\n Helper method for absent()\n\n example:\n delete_resource(\"users\", key, [\"id\",\"name\",\"email\"]) # delete by id or name or email\n\n '''\n resource = get_resource(resource_name, key, identifier_fields, profile, subdomain, api_key)\n if resource:\n if __opts__['test']:\n return 'would delete'\n # flush the resource_cache, because we're modifying a resource\n del __context__['pagerduty_util.resource_cache'][resource_name]\n resource_id = _get_resource_id(resource)\n return _query(method='DELETE', action='{0}/{1}'.format(resource_name, resource_id), profile=profile, subdomain=subdomain, api_key=api_key)\n else:\n return True"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef downloaded(name, artifact, target_dir='/tmp', target_file=None):\n '''\n Ensures that the artifact from nexus exists at given location. If it doesn't exist, then\n it will be downloaded. If it already exists then the checksum of existing file is checked\n against checksum in nexus. If it is different then the step will fail.\n\n artifact\n Details of the artifact to be downloaded from nexus. Various options are:\n\n - nexus_url: URL of the nexus instance\n - repository: Repository in nexus\n - artifact_id: Artifact ID\n - group_id: Group ID\n - packaging: Packaging\n - classifier: Classifier\n - version: Version\n One of the following:\n - Version to download\n - ``latest`` - Download the latest release of this artifact\n - ``latest_snapshot`` - Download the latest snapshot for this artifact\n\n - username: nexus username\n - password: nexus password\n\n target_dir\n Directory where the artifact should be downloaded. By default it is downloaded to /tmp directory.\n\n target_file\n Target file to download artifact to. By default file name is resolved by nexus.\n\n An example to download an artifact to a specific file:\n\n .. code-block:: yaml\n\n jboss_module_downloaded:\n nexus.downloaded:\n - artifact:\n nexus_url: http://nexus.intranet.example.com/repository\n repository: 'libs-release-local'\n artifact_id: 'module'\n group_id: 'com.company.module'\n packaging: 'jar'\n classifier: 'sources'\n version: '1.0'\n - target_file: /opt/jboss7/modules/com/company/lib/module.jar\n\n Download artifact to the folder (automatically resolves file name):\n\n .. code-block:: yaml\n\n maven_artifact_downloaded:\n nexus.downloaded:\n - artifact:\n nexus_url: http://nexus.intranet.example.com/repository\n repository: 'maven-releases'\n artifact_id: 'module'\n group_id: 'com.company.module'\n packaging: 'zip'\n classifier: 'dist'\n version: '1.0'\n - target_dir: /opt/maven/modules/com/company/release\n\n '''\n log.debug(\" ======================== STATE: nexus.downloaded (name: %s) \", name)\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n\n try:\n fetch_result = __fetch_from_nexus(artifact, target_dir, target_file)\n except Exception as exc:\n ret['result'] = False\n ret['comment'] = six.text_type(exc)\n return ret\n\n log.debug(\"fetch_result=%s\", fetch_result)\n\n ret['result'] = fetch_result['status']\n ret['comment'] = fetch_result['comment']\n ret['changes'] = fetch_result['changes']\n log.debug(\"ret=%s\", ret)\n\n return ret", "response": "Download an artifact from nexus to given location."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the currently configured jobs.", "response": "def _jobs():\n '''\n Return the currently configured jobs.\n '''\n response = salt.utils.http.query(\n \"{0}/scheduler/jobs\".format(_base_url()),\n decode_type='json',\n decode=True,\n )\n jobs = {}\n for job in response['dict']:\n jobs[job.pop('name')] = job\n return jobs"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nupdates the specified job with the given configuration.", "response": "def update_job(name, config):\n '''\n Update the specified job with the given configuration.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt chronos-minion-id chronos.update_job my-job '<config yaml>'\n '''\n if 'name' not in config:\n config['name'] = name\n data = salt.utils.json.dumps(config)\n try:\n response = salt.utils.http.query(\n \"{0}/scheduler/iso8601\".format(_base_url()),\n method='POST',\n data=data,\n header_dict={\n 'Content-Type': 'application/json',\n },\n )\n log.debug('update response: %s', response)\n return {'success': True}\n except Exception as ex:\n log.error('unable to update chronos job: %s', get_error_message(ex))\n return {\n 'exception': {\n 'message': get_error_message(ex),\n }\n }"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nremoving the specified job from the server.", "response": "def rm_job(name):\n '''\n Remove the specified job from the server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt chronos-minion-id chronos.rm_job my-job\n '''\n response = salt.utils.http.query(\n \"{0}/scheduler/job/{1}\".format(_base_url(), name),\n method='DELETE',\n )\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN Return iSCSI IQN", "response": "def iscsi_iqn():\n '''\n Return iSCSI IQN\n '''\n grains = {}\n grains['iscsi_iqn'] = False\n if salt.utils.platform.is_linux():\n grains['iscsi_iqn'] = _linux_iqn()\n elif salt.utils.platform.is_windows():\n grains['iscsi_iqn'] = _windows_iqn()\n elif salt.utils.platform.is_aix():\n grains['iscsi_iqn'] = _aix_iqn()\n return grains"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _linux_iqn():\n '''\n Return iSCSI IQN from a Linux host.\n '''\n ret = []\n\n initiator = '/etc/iscsi/initiatorname.iscsi'\n try:\n with salt.utils.files.fopen(initiator, 'r') as _iscsi:\n for line in _iscsi:\n line = line.strip()\n if line.startswith('InitiatorName='):\n ret.append(line.split('=', 1)[1])\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 iSCSI IQN from a Linux host."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _aix_iqn():\n '''\n Return iSCSI IQN from an AIX host.\n '''\n ret = []\n\n aix_cmd = 'lsattr -E -l iscsi0 | grep initiator_name'\n\n aix_ret = salt.modules.cmdmod.run(aix_cmd)\n if aix_ret[0].isalpha():\n try:\n ret.append(aix_ret.split()[1].rstrip())\n except IndexError:\n pass\n return ret", "response": "Return iSCSI IQN from an AIX host."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _windows_iqn():\n '''\n Return iSCSI IQN from a Windows host.\n '''\n ret = []\n\n wmic = salt.utils.path.which('wmic')\n\n if not wmic:\n return ret\n\n namespace = r'\\\\root\\WMI'\n path = 'MSiSCSIInitiator_MethodClass'\n get = 'iSCSINodeName'\n\n cmd_ret = salt.modules.cmdmod.run_all(\n '{0} /namespace:{1} path {2} get {3} /format:table'\n ''.format(wmic, namespace, path, get))\n\n for line in cmd_ret['stdout'].splitlines():\n if line.startswith('iqn.'):\n line = line.rstrip()\n ret.append(line.rstrip())\n\n return ret", "response": "Return iSCSI IQN from a Windows host."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_jid(returner, jid):\n '''\n Return the information for a specified job id\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' ret.get_jid redis 20421104181954700505\n '''\n returners = salt.loader.returners(__opts__, __salt__)\n return returners['{0}.get_jid'.format(returner)](jid)", "response": "Return the information for a specified job id\n "} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns info about last time fun was called on each minion", "response": "def get_fun(returner, fun):\n '''\n Return info about last time fun was called on each minion\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' ret.get_fun mysql network.interfaces\n '''\n returners = salt.loader.returners(__opts__, __salt__)\n return returners['{0}.get_fun'.format(returner)](fun)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a list of all job ids", "response": "def get_jids(returner):\n '''\n Return a list of all job ids\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' ret.get_jids mysql\n '''\n returners = salt.loader.returners(__opts__, __salt__)\n return returners['{0}.get_jids'.format(returner)]()"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of all minions", "response": "def get_minions(returner):\n '''\n Return a list of all minions\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' ret.get_minions mysql\n '''\n returners = salt.loader.returners(__opts__, __salt__)\n return returners['{0}.get_minions'.format(returner)]()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _enforce_txt_record_maxlen(key, value):\n '''\n Enforces the TXT record maximum length of 255 characters.\n TXT record length includes key, value, and '='.\n\n :param str key: Key of the TXT record\n :param str value: Value of the TXT record\n\n :rtype: str\n :return: The value of the TXT record. It may be truncated if it exceeds\n the maximum permitted length. In case of truncation, '...' is\n appended to indicate that the entire value is not present.\n '''\n # Add 1 for '=' seperator between key and value\n if len(key) + len(value) + 1 > 255:\n # 255 - 3 ('...') - 1 ('=') = 251\n return value[:251 - len(key)] + '...'\n return value", "response": "Enforces the TXT record maximum length of 255 characters."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef beacon(config):\n '''\n Broadcast values via zeroconf\n\n If the announced values are static, it is advised to set run_once: True\n (do not poll) on the beacon configuration.\n\n The following are required configuration settings:\n\n - ``servicetype`` - The service type to announce\n - ``port`` - The port of the service to announce\n - ``txt`` - The TXT record of the service being announced as a dict. Grains\n can be used to define TXT values using one of following two formats:\n\n - ``grains.<grain_name>``\n - ``grains.<grain_name>[i]`` where i is an integer representing the\n index of the grain to use. If the grain is not a list, the index is\n ignored.\n\n The following are optional configuration settings:\n\n - ``servicename`` - Set the name of the service. Will use the hostname from\n the minion's ``host`` grain if this value is not set.\n - ``reset_on_change`` - If ``True`` and there is a change in TXT records\n detected, it will stop announcing the service and then restart announcing\n the service. This interruption in service announcement may be desirable\n if the client relies on changes in the browse records to update its cache\n of TXT records. Defaults to ``False``.\n - ``reset_wait`` - The number of seconds to wait after announcement stops\n announcing and before it restarts announcing in the case where there is a\n change in TXT records detected and ``reset_on_change`` is ``True``.\n Defaults to ``0``.\n - ``copy_grains`` - If ``True``, Salt will copy the grains passed into the\n beacon when it backs them up to check for changes on the next iteration.\n Normally, instead of copy, it would use straight value assignment. This\n will allow detection of changes to grains where the grains are modified\n in-place instead of completely replaced. In-place grains changes are not\n currently done in the main Salt code but may be done due to a custom\n plug-in. Defaults to ``False``.\n\n Example Config\n\n .. code-block:: yaml\n\n beacons:\n bonjour_announce:\n - run_once: True\n - servicetype: _demo._tcp\n - port: 1234\n - txt:\n ProdName: grains.productname\n SerialNo: grains.serialnumber\n Comments: 'this is a test'\n '''\n ret = []\n changes = {}\n txt = {}\n\n global LAST_GRAINS\n global SD_REF\n\n _config = {}\n list(map(_config.update, config))\n\n if 'servicename' in _config:\n servicename = _config['servicename']\n else:\n servicename = __grains__['host']\n # Check for hostname change\n if LAST_GRAINS and LAST_GRAINS['host'] != servicename:\n changes['servicename'] = servicename\n\n if LAST_GRAINS and _config.get('reset_on_change', False):\n # Check for IP address change in the case when we reset on change\n if LAST_GRAINS.get('ipv4', []) != __grains__.get('ipv4', []):\n changes['ipv4'] = __grains__.get('ipv4', [])\n if LAST_GRAINS.get('ipv6', []) != __grains__.get('ipv6', []):\n changes['ipv6'] = __grains__.get('ipv6', [])\n\n for item in _config['txt']:\n changes_key = 'txt.' + salt.utils.stringutils.to_unicode(item)\n if _config['txt'][item].startswith('grains.'):\n grain = _config['txt'][item][7:]\n grain_index = None\n square_bracket = grain.find('[')\n if square_bracket != -1 and grain[-1] == ']':\n grain_index = int(grain[square_bracket+1:-1])\n grain = grain[:square_bracket]\n\n grain_value = __grains__.get(grain, '')\n if isinstance(grain_value, list):\n if grain_index is not None:\n grain_value = grain_value[grain_index]\n else:\n grain_value = ','.join(grain_value)\n txt[item] = _enforce_txt_record_maxlen(item, grain_value)\n if LAST_GRAINS and (LAST_GRAINS.get(grain, '') != __grains__.get(grain, '')):\n changes[changes_key] = txt[item]\n else:\n txt[item] = _enforce_txt_record_maxlen(item, _config['txt'][item])\n\n if not LAST_GRAINS:\n changes[changes_key] = txt[item]\n\n if changes:\n txt_record = pybonjour.TXTRecord(items=txt)\n if not LAST_GRAINS:\n changes['servicename'] = servicename\n changes['servicetype'] = _config['servicetype']\n changes['port'] = _config['port']\n changes['ipv4'] = __grains__.get('ipv4', [])\n changes['ipv6'] = __grains__.get('ipv6', [])\n SD_REF = pybonjour.DNSServiceRegister(\n name=servicename,\n regtype=_config['servicetype'],\n port=_config['port'],\n txtRecord=txt_record,\n callBack=_register_callback)\n atexit.register(_close_sd_ref)\n ready = select.select([SD_REF], [], [])\n if SD_REF in ready[0]:\n pybonjour.DNSServiceProcessResult(SD_REF)\n elif _config.get('reset_on_change', False) or 'servicename' in changes:\n # A change in 'servicename' requires a reset because we can only\n # directly update TXT records\n SD_REF.close()\n SD_REF = None\n reset_wait = _config.get('reset_wait', 0)\n if reset_wait > 0:\n time.sleep(reset_wait)\n SD_REF = pybonjour.DNSServiceRegister(\n name=servicename,\n regtype=_config['servicetype'],\n port=_config['port'],\n txtRecord=txt_record,\n callBack=_register_callback)\n ready = select.select([SD_REF], [], [])\n if SD_REF in ready[0]:\n pybonjour.DNSServiceProcessResult(SD_REF)\n else:\n txt_record_raw = six.text_type(txt_record).encode('utf-8')\n pybonjour.DNSServiceUpdateRecord(\n SD_REF,\n RecordRef=None,\n flags=0,\n rdata=txt_record_raw)\n\n ret.append({'tag': 'result', 'changes': changes})\n\n if _config.get('copy_grains', False):\n LAST_GRAINS = __grains__.copy()\n else:\n LAST_GRAINS = __grains__\n\n return ret", "response": "This function is used to announce the beacon values via zeroconf\n ."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef doc(*args):\n '''\n Return the docstrings for all modules. Optionally, specify a module or a\n function to narrow the selection.\n\n The strings are aggregated into a single document on the master for easy\n reading.\n\n Multiple modules/functions can be specified.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.doc\n salt '*' sys.doc sys\n salt '*' sys.doc sys.doc\n salt '*' sys.doc network.traceroute user.info\n\n Modules can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.doc 'sys.*'\n salt '*' sys.doc 'sys.list_*'\n '''\n docs = {}\n if not args:\n for fun in __salt__:\n docs[fun] = __salt__[fun].__doc__\n return _strip_rst(docs)\n\n for module in args:\n _use_fnmatch = False\n if '*' in module:\n target_mod = module\n _use_fnmatch = True\n elif module:\n # allow both \"sys\" and \"sys.\" to match sys, without also matching\n # sysctl\n target_mod = module + '.' if not module.endswith('.') else module\n else:\n target_mod = ''\n if _use_fnmatch:\n for fun in fnmatch.filter(__salt__, target_mod):\n docs[fun] = __salt__[fun].__doc__\n else:\n\n for fun in __salt__:\n if fun == module or fun.startswith(target_mod):\n docs[fun] = __salt__[fun].__doc__\n return _strip_rst(docs)", "response": "Return the docstrings for all modules or functions. Optionally specify a module or function to narrow the selection."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef state_doc(*args):\n '''\n Return the docstrings for all states. Optionally, specify a state or a\n function to narrow the selection.\n\n The strings are aggregated into a single document on the master for easy\n reading.\n\n Multiple states/functions can be specified.\n\n .. versionadded:: 2014.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.state_doc\n salt '*' sys.state_doc service\n salt '*' sys.state_doc service.running\n salt '*' sys.state_doc service.running ipables.append\n\n State names can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.state_doc 'service.*' 'iptables.*'\n\n '''\n st_ = salt.state.State(__opts__)\n\n docs = {}\n if not args:\n for fun in st_.states:\n state = fun.split('.')[0]\n if state not in docs:\n if hasattr(st_.states[fun], '__globals__'):\n docs[state] = st_.states[fun].__globals__['__doc__']\n docs[fun] = st_.states[fun].__doc__\n return _strip_rst(docs)\n\n for module in args:\n _use_fnmatch = False\n if '*' in module:\n target_mod = module\n _use_fnmatch = True\n elif module:\n # allow both \"sys\" and \"sys.\" to match sys, without also matching\n # sysctl\n target_mod = module + '.' if not module.endswith('.') else module\n else:\n target_mod = ''\n if _use_fnmatch:\n for fun in fnmatch.filter(st_.states, target_mod):\n state = fun.split('.')[0]\n if hasattr(st_.states[fun], '__globals__'):\n docs[state] = st_.states[fun].__globals__['__doc__']\n docs[fun] = st_.states[fun].__doc__\n else:\n for fun in st_.states:\n if fun == module or fun.startswith(target_mod):\n state = module.split('.')[0]\n if state not in docs:\n if hasattr(st_.states[fun], '__globals__'):\n docs[state] = st_.states[fun].__globals__['__doc__']\n docs[fun] = st_.states[fun].__doc__\n return _strip_rst(docs)", "response": "Return the docstrings for all states or functions."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef runner_doc(*args):\n '''\n Return the docstrings for all runners. Optionally, specify a runner or a\n function to narrow the selection.\n\n The strings are aggregated into a single document on the master for easy\n reading.\n\n Multiple runners/functions can be specified.\n\n .. versionadded:: 2014.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.runner_doc\n salt '*' sys.runner_doc cache\n salt '*' sys.runner_doc cache.grains\n salt '*' sys.runner_doc cache.grains mine.get\n\n Runner names can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.runner_doc 'cache.clear_*'\n\n '''\n run_ = salt.runner.Runner(__opts__)\n docs = {}\n if not args:\n for fun in run_.functions:\n docs[fun] = run_.functions[fun].__doc__\n return _strip_rst(docs)\n\n for module in args:\n _use_fnmatch = False\n if '*' in module:\n target_mod = module\n _use_fnmatch = True\n elif module:\n # allow both \"sys\" and \"sys.\" to match sys, without also matching\n # sysctl\n target_mod = module + '.' if not module.endswith('.') else module\n else:\n target_mod = ''\n if _use_fnmatch:\n for fun in fnmatch.filter(run_.functions, target_mod):\n docs[fun] = run_.functions[fun].__doc__\n else:\n for fun in run_.functions:\n if fun == module or fun.startswith(target_mod):\n docs[fun] = run_.functions[fun].__doc__\n return _strip_rst(docs)", "response": "Return the docstrings for all runners and functions."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef returner_doc(*args):\n '''\n Return the docstrings for all returners. Optionally, specify a returner or a\n function to narrow the selection.\n\n The strings are aggregated into a single document on the master for easy\n reading.\n\n Multiple returners/functions can be specified.\n\n .. versionadded:: 2014.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.returner_doc\n salt '*' sys.returner_doc sqlite3\n salt '*' sys.returner_doc sqlite3.get_fun\n salt '*' sys.returner_doc sqlite3.get_fun etcd.get_fun\n\n Returner names can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.returner_doc 'sqlite3.get_*'\n\n '''\n\n returners_ = salt.loader.returners(__opts__, [])\n docs = {}\n if not args:\n for fun in returners_:\n docs[fun] = returners_[fun].__doc__\n return _strip_rst(docs)\n\n for module in args:\n _use_fnmatch = False\n if '*' in module:\n target_mod = module\n _use_fnmatch = True\n elif module:\n # allow both \"sys\" and \"sys.\" to match sys, without also matching\n # sysctl\n target_mod = module + '.' if not module.endswith('.') else module\n else:\n target_mod = ''\n if _use_fnmatch:\n for fun in returners_:\n if fun == module or fun.startswith(target_mod):\n docs[fun] = returners_[fun].__doc__\n else:\n for fun in six.iterkeys(returners_):\n if fun == module or fun.startswith(target_mod):\n docs[fun] = returners_[fun].__doc__\n return _strip_rst(docs)", "response": "Return the docstrings for all returners or functions. Optionally specify a returner or a function to narrow the selection."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef renderer_doc(*args):\n '''\n Return the docstrings for all renderers. Optionally, specify a renderer or a\n function to narrow the selection.\n\n The strings are aggregated into a single document on the master for easy\n reading.\n\n Multiple renderers can be specified.\n\n .. versionadded:: 2015.5.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.renderer_doc\n salt '*' sys.renderer_doc cheetah\n salt '*' sys.renderer_doc jinja json\n\n Renderer names can be specified as globs.\n\n .. code-block:: bash\n\n salt '*' sys.renderer_doc 'c*' 'j*'\n\n '''\n renderers_ = salt.loader.render(__opts__, [])\n docs = {}\n if not args:\n for func in six.iterkeys(renderers_):\n docs[func] = renderers_[func].__doc__\n return _strip_rst(docs)\n\n for module in args:\n if '*' in module or '.' in module:\n for func in fnmatch.filter(renderers_, module):\n docs[func] = renderers_[func].__doc__\n else:\n moduledot = module + '.'\n for func in six.iterkeys(renderers_):\n if func.startswith(moduledot):\n docs[func] = renderers_[func].__doc__\n return _strip_rst(docs)", "response": "Return the docstrings for all renderers."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef utils_doc(*args):\n '''\n .. versionadded:: Neon\n\n Return the docstrings for all utils modules. Optionally, specify a module\n or a function to narrow the selection.\n\n The strings are aggregated into a single document on the master for easy\n reading.\n\n Multiple modules/functions can be specified.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.utils_doc\n salt '*' sys.utils_doc data stringutils\n salt '*' sys.utils_doc stringutils.to_unicode\n salt '*' sys.utils_doc data.encode data.decode\n '''\n docs = {}\n if not args:\n for fun in __utils__:\n docs[fun] = __utils__[fun].__doc__\n return _strip_rst(docs)\n\n for module in args:\n _use_fnmatch = False\n if '*' in module:\n target_mod = module\n _use_fnmatch = True\n elif module:\n # allow both \"sys\" and \"sys.\" to match sys, without also matching\n # sysctl\n target_mod = module + '.' if not module.endswith('.') else module\n else:\n target_mod = ''\n if _use_fnmatch:\n for fun in fnmatch.filter(__utils__, target_mod):\n docs[fun] = __utils__[fun].__doc__\n else:\n\n for fun in __utils__:\n if fun == module or fun.startswith(target_mod):\n docs[fun] = __utils__[fun].__doc__\n return _strip_rst(docs)", "response": "Return the docstrings for all utils modules or functions."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nlist the functions for all modules or modules in which to list.", "response": "def list_functions(*args, **kwargs): # pylint: disable=unused-argument\n '''\n List the functions for all modules. Optionally, specify a module or modules\n from which to list.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.list_functions\n salt '*' sys.list_functions sys\n salt '*' sys.list_functions sys user\n\n Function names can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.list_functions 'sys.list_*'\n\n .. versionadded:: ?\n\n .. code-block:: bash\n\n salt '*' sys.list_functions 'module.specific_function'\n\n '''\n # ## NOTE: **kwargs is used here to prevent a traceback when garbage\n # ## arguments are tacked on to the end.\n\n if not args:\n # We're being asked for all functions\n return sorted(__salt__)\n\n names = set()\n for module in args:\n if '*' in module or '.' in module:\n for func in fnmatch.filter(__salt__, module):\n names.add(func)\n else:\n # \"sys\" should just match sys without also matching sysctl\n moduledot = module + '.'\n for func in __salt__:\n if func.startswith(moduledot):\n names.add(func)\n return sorted(names)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_modules(*args):\n '''\n List the modules loaded on the minion\n\n .. versionadded:: 2015.5.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.list_modules\n\n Module names can be specified as globs.\n\n .. code-block:: bash\n\n salt '*' sys.list_modules 's*'\n\n '''\n modules = set()\n if not args:\n for func in __salt__:\n modules.add(func.split('.')[0])\n return sorted(modules)\n\n for module in args:\n if '*' in module:\n for func in fnmatch.filter(__salt__, module):\n modules.add(func.split('.')[0])\n else:\n for func in __salt__:\n mod_test = func.split('.')[0]\n if mod_test == module:\n modules.add(mod_test)\n return sorted(modules)", "response": "List the modules loaded on the minion"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the argument specification of functions in Salt state modules.", "response": "def state_argspec(module=''):\n '''\n Return the argument specification of functions in Salt state\n modules.\n\n .. versionadded:: 2015.5.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.state_argspec pkg.installed\n salt '*' sys.state_argspec file\n salt '*' sys.state_argspec\n\n State names can be specified as globs.\n\n .. code-block:: bash\n\n salt '*' sys.state_argspec 'pkg.*'\n\n '''\n st_ = salt.state.State(__opts__)\n return salt.utils.args.argspec_report(st_.states, module)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the argument specification of functions in Salt returner modules.", "response": "def returner_argspec(module=''):\n '''\n Return the argument specification of functions in Salt returner\n modules.\n\n .. versionadded:: 2015.5.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.returner_argspec xmpp\n salt '*' sys.returner_argspec xmpp smtp\n salt '*' sys.returner_argspec\n\n Returner names can be specified as globs.\n\n .. code-block:: bash\n\n salt '*' sys.returner_argspec 'sqlite3.*'\n\n '''\n returners_ = salt.loader.returners(__opts__, [])\n return salt.utils.args.argspec_report(returners_, module)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the argument specification of functions in Salt runner modules.", "response": "def runner_argspec(module=''):\n '''\n Return the argument specification of functions in Salt runner\n modules.\n\n .. versionadded:: 2015.5.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.runner_argspec state\n salt '*' sys.runner_argspec http\n salt '*' sys.runner_argspec\n\n Runner names can be specified as globs.\n\n .. code-block:: bash\n\n salt '*' sys.runner_argspec 'winrepo.*'\n '''\n run_ = salt.runner.Runner(__opts__)\n return salt.utils.args.argspec_report(run_.functions, module)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_state_functions(*args, **kwargs): # pylint: disable=unused-argument\n '''\n List the functions for all state modules. Optionally, specify a state\n module or modules from which to list.\n\n .. versionadded:: 2014.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.list_state_functions\n salt '*' sys.list_state_functions file\n salt '*' sys.list_state_functions pkg user\n\n State function names can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.list_state_functions 'file.*'\n salt '*' sys.list_state_functions 'file.s*'\n\n .. versionadded:: ?\n\n .. code-block:: bash\n\n salt '*' sys.list_state_functions 'module.specific_function'\n\n '''\n # NOTE: **kwargs is used here to prevent a traceback when garbage\n # arguments are tacked on to the end.\n\n st_ = salt.state.State(__opts__)\n if not args:\n # We're being asked for all functions\n return sorted(st_.states)\n\n names = set()\n for module in args:\n if '*' in module or '.' in module:\n for func in fnmatch.filter(st_.states, module):\n names.add(func)\n else:\n # \"sys\" should just match sys without also matching sysctl\n moduledot = module + '.'\n for func in st_.states:\n if func.startswith(moduledot):\n names.add(func)\n return sorted(names)", "response": "List the functions for all state modules. Optionally specify a state module or modules from which to list."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_state_modules(*args):\n '''\n List the modules loaded on the minion\n\n .. versionadded:: 2014.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.list_state_modules\n\n State module names can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.list_state_modules 'mysql_*'\n\n '''\n st_ = salt.state.State(__opts__)\n modules = set()\n\n if not args:\n for func in st_.states:\n log.debug('func %s', func)\n modules.add(func.split('.')[0])\n return sorted(modules)\n\n for module in args:\n if '*' in module:\n for func in fnmatch.filter(st_.states, module):\n modules.add(func.split('.')[0])\n else:\n for func in st_.states:\n mod_test = func.split('.')[0]\n if mod_test == module:\n modules.add(mod_test)\n return sorted(modules)", "response": "List the modules loaded on the minion"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef list_runners(*args):\n '''\n List the runners loaded on the minion\n\n .. versionadded:: 2014.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.list_runners\n\n Runner names can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.list_runners 'm*'\n\n '''\n run_ = salt.runner.Runner(__opts__)\n runners = set()\n if not args:\n for func in run_.functions:\n runners.add(func.split('.')[0])\n return sorted(runners)\n\n for module in args:\n if '*' in module:\n for func in fnmatch.filter(run_.functions, module):\n runners.add(func.split('.')[0])\n else:\n for func in run_.functions:\n mod_test = func.split('.')[0]\n if mod_test == module:\n runners.add(mod_test)\n return sorted(runners)", "response": "List the runners loaded on the minion"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef list_runner_functions(*args, **kwargs): # pylint: disable=unused-argument\n '''\n List the functions for all runner modules. Optionally, specify a runner\n module or modules from which to list.\n\n .. versionadded:: 2014.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.list_runner_functions\n salt '*' sys.list_runner_functions state\n salt '*' sys.list_runner_functions state virt\n\n Runner function names can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.list_runner_functions 'state.*' 'virt.*'\n\n '''\n # ## NOTE: **kwargs is used here to prevent a traceback when garbage\n # ## arguments are tacked on to the end.\n\n run_ = salt.runner.Runner(__opts__)\n if not args:\n # We're being asked for all functions\n return sorted(run_.functions)\n\n names = set()\n for module in args:\n if '*' in module or '.' in module:\n for func in fnmatch.filter(run_.functions, module):\n names.add(func)\n else:\n # \"sys\" should just match sys without also matching sysctl\n moduledot = module + '.'\n for func in run_.functions:\n if func.startswith(moduledot):\n names.add(func)\n return sorted(names)", "response": "List the functions for all runner modules. Optionally specify a runner module or modules from which to list."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nlisting the returners loaded on the minion", "response": "def list_returners(*args):\n '''\n List the returners loaded on the minion\n\n .. versionadded:: 2014.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.list_returners\n\n Returner names can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.list_returners 's*'\n\n '''\n returners_ = salt.loader.returners(__opts__, [])\n returners = set()\n\n if not args:\n for func in six.iterkeys(returners_):\n returners.add(func.split('.')[0])\n return sorted(returners)\n\n for module in args:\n if '*' in module:\n for func in fnmatch.filter(returners_, module):\n returners.add(func.split('.')[0])\n else:\n for func in returners_:\n mod_test = func.split('.')[0]\n if mod_test == module:\n returners.add(mod_test)\n return sorted(returners)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef list_returner_functions(*args, **kwargs): # pylint: disable=unused-argument\n '''\n List the functions for all returner modules. Optionally, specify a returner\n module or modules from which to list.\n\n .. versionadded:: 2014.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.list_returner_functions\n salt '*' sys.list_returner_functions mysql\n salt '*' sys.list_returner_functions mysql etcd\n\n Returner names can be specified as globs.\n\n .. versionadded:: 2015.5.0\n\n .. code-block:: bash\n\n salt '*' sys.list_returner_functions 'sqlite3.get_*'\n\n '''\n # NOTE: **kwargs is used here to prevent a traceback when garbage\n # arguments are tacked on to the end.\n\n returners_ = salt.loader.returners(__opts__, [])\n if not args:\n # We're being asked for all functions\n return sorted(returners_)\n\n names = set()\n for module in args:\n if '*' in module or '.' in module:\n for func in fnmatch.filter(returners_, module):\n names.add(func)\n else:\n # \"sys\" should just match sys without also matching sysctl\n moduledot = module + '.'\n for func in returners_:\n if func.startswith(moduledot):\n names.add(func)\n return sorted(names)", "response": "List the functions for all returner modules. Optionally specify a returner module or modules from which to list."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_renderers(*args):\n '''\n List the renderers loaded on the minion\n\n .. versionadded:: 2015.5.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.list_renderers\n\n Render names can be specified as globs.\n\n .. code-block:: bash\n\n salt '*' sys.list_renderers 'yaml*'\n\n '''\n renderers_ = salt.loader.render(__opts__, [])\n renderers = set()\n\n if not args:\n for rend in six.iterkeys(renderers_):\n renderers.add(rend)\n return sorted(renderers)\n\n for module in args:\n for rend in fnmatch.filter(renderers_, module):\n renderers.add(rend)\n return sorted(renderers)", "response": "List the available renderers on the minion"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef state_schema(module=''):\n '''\n Return a JSON Schema for the given state function(s)\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sys.state_schema\n salt '*' sys.state_schema pkg.installed\n '''\n specs = state_argspec(module)\n\n schemas = []\n for state_mod, state_spec in specs.items():\n schemas.append(_argspec_to_schema(state_mod, state_spec))\n\n return schemas", "response": "Return a JSON Schema for the given state function"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _run_psql(cmd, runas=None, password=None, host=None, port=None, user=None):\n '''\n Helper function to call psql, because the password requirement\n makes this too much code to be repeated in each function below\n '''\n kwargs = {\n 'reset_system_locale': False,\n 'clean_env': True,\n }\n if runas is None:\n if not host:\n host = __salt__['config.option']('postgres.host')\n if not host or host.startswith('/'):\n if 'FreeBSD' in __grains__['os_family']:\n runas = 'pgsql'\n elif 'OpenBSD' in __grains__['os_family']:\n runas = '_postgresql'\n else:\n runas = 'postgres'\n\n if user is None:\n user = runas\n\n if runas:\n kwargs['runas'] = runas\n\n if password is None:\n password = __salt__['config.option']('postgres.pass')\n if password is not None:\n pgpassfile = salt.utils.files.mkstemp(text=True)\n with salt.utils.files.fopen(pgpassfile, 'w') as fp_:\n fp_.write(salt.utils.stringutils.to_str('{0}:{1}:*:{2}:{3}'.format(\n 'localhost' if not host or host.startswith('/') else host,\n port if port else '*',\n user if user else '*',\n password,\n )))\n __salt__['file.chown'](pgpassfile, runas, '')\n kwargs['env'] = {'PGPASSFILE': pgpassfile}\n\n ret = __salt__['cmd.run_all'](cmd, python_shell=False, **kwargs)\n\n if ret.get('retcode', 0) != 0:\n log.error('Error connecting to Postgresql server')\n if password is not None and not __salt__['file.remove'](pgpassfile):\n log.warning('Remove PGPASSFILE failed')\n\n return ret", "response": "Helper function to call psql"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _run_initdb(name,\n auth='password',\n user=None,\n password=None,\n encoding='UTF8',\n locale=None,\n runas=None,\n waldir=None,\n checksums=False):\n '''\n Helper function to call initdb\n '''\n if runas is None:\n if 'FreeBSD' in __grains__['os_family']:\n runas = 'pgsql'\n elif 'OpenBSD' in __grains__['os_family']:\n runas = '_postgresql'\n else:\n runas = 'postgres'\n\n if user is None:\n user = runas\n _INITDB_BIN = _find_pg_binary('initdb')\n if not _INITDB_BIN:\n raise CommandExecutionError('initdb executable not found.')\n cmd = [\n _INITDB_BIN,\n '--pgdata={0}'.format(name),\n '--username={0}'.format(user),\n '--auth={0}'.format(auth),\n '--encoding={0}'.format(encoding),\n ]\n\n if locale is not None:\n cmd.append('--locale={0}'.format(locale))\n\n # intentionally use short option, as the long option name has been\n # renamed from \"xlogdir\" to \"waldir\" in PostgreSQL 10\n if waldir is not None:\n cmd.append('-X')\n cmd.append(waldir)\n\n if checksums:\n cmd.append('--data-checksums')\n\n if password is not None:\n pgpassfile = salt.utils.files.mkstemp(text=True)\n with salt.utils.files.fopen(pgpassfile, 'w') as fp_:\n fp_.write(salt.utils.stringutils.to_str('{0}'.format(password)))\n __salt__['file.chown'](pgpassfile, runas, '')\n cmd.extend([\n '--pwfile={0}'.format(pgpassfile),\n ])\n\n kwargs = dict(runas=runas, clean_env=True)\n cmdstr = ' '.join([pipes.quote(c) for c in cmd])\n ret = __salt__['cmd.run_all'](cmdstr, python_shell=False, **kwargs)\n\n if ret.get('retcode', 0) != 0:\n log.error('Error initilizing the postgres data directory')\n\n if password is not None and not __salt__['file.remove'](pgpassfile):\n log.warning('Removal of PGPASSFILE failed')\n\n return ret", "response": "Helper function to call initdb"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the version of a Postgres server.", "response": "def version(user=None, host=None, port=None, maintenance_db=None,\n password=None, runas=None):\n '''\n Return the version of a Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.version\n '''\n query = 'SELECT setting FROM pg_catalog.pg_settings ' \\\n 'WHERE name = \\'server_version\\''\n cmd = _psql_cmd('-c', query,\n '-t',\n host=host,\n user=user,\n port=port,\n maintenance_db=maintenance_db,\n password=password)\n ret = _run_psql(\n cmd, runas=runas, password=password, host=host, port=port, user=user)\n\n for line in salt.utils.itertools.split(ret['stdout'], '\\n'):\n # Just return the first line\n return line"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the server version properly parsed and casted for internal use.", "response": "def _parsed_version(user=None, host=None, port=None, maintenance_db=None,\n password=None, runas=None):\n '''\n Returns the server version properly parsed and int casted for internal use.\n\n If the Postgres server does not respond, None will be returned.\n '''\n\n psql_version = version(\n user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas,\n )\n\n if psql_version:\n return _LooseVersion(psql_version)\n else:\n log.warning('Attempt to parse version of Postgres server failed. '\n 'Is the server responding?')\n return None"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _connection_defaults(user=None, host=None, port=None, maintenance_db=None):\n '''\n Returns a tuple of (user, host, port, db) with config, pillar, or default\n values assigned to missing values.\n '''\n if not user:\n user = __salt__['config.option']('postgres.user')\n if not host:\n host = __salt__['config.option']('postgres.host')\n if not port:\n port = __salt__['config.option']('postgres.port')\n if not maintenance_db:\n maintenance_db = __salt__['config.option']('postgres.maintenance_db')\n\n return (user, host, port, maintenance_db)", "response": "Return a tuple of user host port and db defaults assigned to missing values."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _psql_cmd(*args, **kwargs):\n '''\n Return string with fully composed psql command.\n\n Accepts optional keyword arguments: user, host, port and maintenance_db,\n as well as any number of positional arguments to be added to the end of\n the command.\n '''\n (user, host, port, maintenance_db) = _connection_defaults(\n kwargs.get('user'),\n kwargs.get('host'),\n kwargs.get('port'),\n kwargs.get('maintenance_db'))\n _PSQL_BIN = _find_pg_binary('psql')\n cmd = [_PSQL_BIN,\n '--no-align',\n '--no-readline',\n '--no-psqlrc',\n '--no-password'] # Never prompt, handled in _run_psql.\n if user:\n cmd += ['--username', user]\n if host:\n cmd += ['--host', host]\n if port:\n cmd += ['--port', six.text_type(port)]\n if not maintenance_db:\n maintenance_db = 'postgres'\n cmd.extend(['--dbname', maintenance_db])\n cmd.extend(args)\n return cmd", "response": "Returns fully composed psql command."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef psql_query(query, user=None, host=None, port=None, maintenance_db=None,\n password=None, runas=None, write=False):\n '''\n Run an SQL-Query and return the results as a list. This command\n only supports SELECT statements. This limitation can be worked around\n with a query like this:\n\n WITH updated AS (UPDATE pg_authid SET rolconnlimit = 2000 WHERE\n rolname = 'rolename' RETURNING rolconnlimit) SELECT * FROM updated;\n\n query\n The query string.\n\n user\n Database username, if different from config or default.\n\n host\n Database host, if different from config or default.\n\n port\n Database port, if different from the config or default.\n\n maintenance_db\n The database to run the query against.\n\n password\n User password, if different from the config or default.\n\n runas\n User to run the command as.\n\n write\n Mark query as READ WRITE transaction.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.psql_query 'select * from pg_stat_activity'\n '''\n ret = []\n\n csv_query = 'COPY ({0}) TO STDOUT WITH CSV HEADER'.format(\n query.strip().rstrip(';'))\n\n # Mark transaction as R/W to achieve write will be allowed\n # Commit is necessary due to transaction\n if write:\n csv_query = 'START TRANSACTION READ WRITE; {0}; COMMIT TRANSACTION;'.format(csv_query)\n\n # always use the same datestyle settings to allow parsing dates\n # regardless what server settings are configured\n cmdret = _psql_prepare_and_run(['-v', 'datestyle=ISO,MDY',\n '-c', csv_query],\n runas=runas,\n host=host, user=user, port=port,\n maintenance_db=maintenance_db,\n password=password)\n if cmdret['retcode'] > 0:\n return ret\n\n csv_file = StringIO(cmdret['stdout'])\n header = {}\n for row in csv.reader(csv_file,\n delimiter=salt.utils.stringutils.to_str(','),\n quotechar=salt.utils.stringutils.to_str('\"')):\n if not row:\n continue\n if not header:\n header = row\n continue\n ret.append(dict(zip(header, row)))\n\n # Remove 'COMMIT' message if query is inside R/W transction\n if write:\n ret = ret[0:-1]\n\n return ret", "response": "Run an SQL - Query and return the results as a list."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn dictionary with information about databases of a Postgres server.", "response": "def db_list(user=None, host=None, port=None, maintenance_db=None,\n password=None, runas=None):\n '''\n Return dictionary with information about databases of a Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.db_list\n '''\n\n ret = {}\n\n query = (\n 'SELECT datname as \"Name\", pga.rolname as \"Owner\", '\n 'pg_encoding_to_char(encoding) as \"Encoding\", '\n 'datcollate as \"Collate\", datctype as \"Ctype\", '\n 'datacl as \"Access privileges\", spcname as \"Tablespace\" '\n 'FROM pg_database pgd, pg_roles pga, pg_tablespace pgts '\n 'WHERE pga.oid = pgd.datdba AND pgts.oid = pgd.dattablespace'\n )\n\n rows = psql_query(query, runas=runas, host=host, user=user,\n port=port, maintenance_db=maintenance_db,\n password=password)\n\n for row in rows:\n ret[row['Name']] = row\n ret[row['Name']].pop('Name')\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nchecking if a database exists on the Postgres server.", "response": "def db_exists(name, user=None, host=None, port=None, maintenance_db=None,\n password=None, runas=None):\n '''\n Checks if a database exists on the Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.db_exists 'dbname'\n '''\n\n databases = db_list(user=user, host=host, port=port,\n maintenance_db=maintenance_db,\n password=password, runas=runas)\n return name in databases"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates a Postgres database.", "response": "def db_create(name,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n tablespace=None,\n encoding=None,\n lc_collate=None,\n lc_ctype=None,\n owner=None,\n template=None,\n runas=None):\n '''\n Adds a databases to the Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.db_create 'dbname'\n\n salt '*' postgres.db_create 'dbname' template=template_postgis\n\n '''\n\n # Base query to create a database\n query = 'CREATE DATABASE \"{0}\"'.format(name)\n\n # \"With\"-options to create a database\n with_args = salt.utils.odict.OrderedDict([\n ('TABLESPACE', _quote_ddl_value(tablespace, '\"')),\n # owner needs to be enclosed in double quotes so postgres\n # doesn't get thrown by dashes in the name\n ('OWNER', _quote_ddl_value(owner, '\"')),\n ('TEMPLATE', template),\n ('ENCODING', _quote_ddl_value(encoding)),\n ('LC_COLLATE', _quote_ddl_value(lc_collate)),\n ('LC_CTYPE', _quote_ddl_value(lc_ctype)),\n ])\n with_chunks = []\n for key, value in with_args.items():\n if value is not None:\n with_chunks += [key, '=', value]\n # Build a final query\n if with_chunks:\n with_chunks.insert(0, ' WITH')\n query += ' '.join(with_chunks)\n\n # Execute the command\n ret = _psql_prepare_and_run(['-c', query],\n user=user, host=host, port=port,\n maintenance_db=maintenance_db,\n password=password, runas=runas)\n return ret['retcode'] == 0"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nchange tablespace or owner of database.", "response": "def db_alter(name, user=None, host=None, port=None, maintenance_db=None,\n password=None, tablespace=None, owner=None, owner_recurse=False,\n runas=None):\n '''\n Change tablespace or/and owner of database.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.db_alter dbname owner=otheruser\n '''\n if not any((tablespace, owner)):\n return True # Nothing todo?\n\n if owner and owner_recurse:\n ret = owner_to(name, owner,\n user=user,\n host=host,\n port=port,\n password=password,\n runas=runas)\n else:\n queries = []\n if owner:\n queries.append('ALTER DATABASE \"{0}\" OWNER TO \"{1}\"'.format(\n name, owner\n ))\n if tablespace:\n queries.append('ALTER DATABASE \"{0}\" SET TABLESPACE \"{1}\"'.format(\n name, tablespace\n ))\n for query in queries:\n ret = _psql_prepare_and_run(['-c', query],\n user=user, host=host, port=port,\n maintenance_db=maintenance_db,\n password=password, runas=runas)\n\n if ret['retcode'] != 0:\n return False\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef tablespace_list(user=None, host=None, port=None, maintenance_db=None,\n password=None, runas=None):\n '''\n Return dictionary with information about tablespaces of a Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.tablespace_list\n\n .. versionadded:: 2015.8.0\n '''\n\n ret = {}\n\n query = (\n 'SELECT spcname as \"Name\", pga.rolname as \"Owner\", spcacl as \"ACL\", '\n 'spcoptions as \"Opts\", pg_tablespace_location(pgts.oid) as \"Location\" '\n 'FROM pg_tablespace pgts, pg_roles pga WHERE pga.oid = pgts.spcowner'\n )\n\n rows = __salt__['postgres.psql_query'](query, runas=runas, host=host,\n user=user, port=port,\n maintenance_db=maintenance_db,\n password=password)\n\n for row in rows:\n ret[row['Name']] = row\n ret[row['Name']].pop('Name')\n\n return ret", "response": "Return a dictionary with information about tablespaces of a Postgres server."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef tablespace_exists(name, user=None, host=None, port=None, maintenance_db=None,\n password=None, runas=None):\n '''\n Checks if a tablespace exists on the Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.tablespace_exists 'dbname'\n\n .. versionadded:: 2015.8.0\n '''\n\n tablespaces = tablespace_list(user=user, host=host, port=port,\n maintenance_db=maintenance_db,\n password=password, runas=runas)\n return name in tablespaces", "response": "Checks if a tablespace exists on the Postgres server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef tablespace_create(name, location, options=None, owner=None, user=None,\n host=None, port=None, maintenance_db=None, password=None,\n runas=None):\n '''\n Adds a tablespace to the Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.tablespace_create tablespacename '/path/datadir'\n\n .. versionadded:: 2015.8.0\n '''\n owner_query = ''\n options_query = ''\n if owner:\n owner_query = 'OWNER \"{0}\"'.format(owner)\n # should come out looking like: 'OWNER postgres'\n if options:\n optionstext = ['{0} = {1}'.format(k, v) for k, v in six.iteritems(options)]\n options_query = 'WITH ( {0} )'.format(', '.join(optionstext))\n # should come out looking like: 'WITH ( opt1 = 1.0, opt2 = 4.0 )'\n query = 'CREATE TABLESPACE \"{0}\" {1} LOCATION \\'{2}\\' {3}'.format(name,\n owner_query,\n location,\n options_query)\n\n # Execute the command\n ret = _psql_prepare_and_run(['-c', query],\n user=user, host=host, port=port,\n maintenance_db=maintenance_db,\n password=password, runas=runas)\n return ret['retcode'] == 0", "response": "Adds a tablespace to the Postgres server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef tablespace_alter(name, user=None, host=None, port=None, maintenance_db=None,\n password=None, new_name=None, new_owner=None,\n set_option=None, reset_option=None, runas=None):\n '''\n Change tablespace name, owner, or options.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.tablespace_alter tsname new_owner=otheruser\n salt '*' postgres.tablespace_alter index_space new_name=fast_raid\n salt '*' postgres.tablespace_alter test set_option=\"{'seq_page_cost': '1.1'}\"\n salt '*' postgres.tablespace_alter tsname reset_option=seq_page_cost\n\n .. versionadded:: 2015.8.0\n '''\n if not any([new_name, new_owner, set_option, reset_option]):\n return True # Nothing todo?\n\n queries = []\n\n if new_name:\n queries.append('ALTER TABLESPACE \"{0}\" RENAME TO \"{1}\"'.format(\n name, new_name))\n if new_owner:\n queries.append('ALTER TABLESPACE \"{0}\" OWNER TO \"{1}\"'.format(\n name, new_owner))\n if set_option:\n queries.append('ALTER TABLESPACE \"{0}\" SET ({1} = {2})'.format(\n name, set_option.keys()[0], set_option.values()[0]))\n if reset_option:\n queries.append('ALTER TABLESPACE \"{0}\" RESET ({1})'.format(\n name, reset_option))\n\n for query in queries:\n ret = _psql_prepare_and_run(['-c', query],\n user=user, host=host, port=port,\n maintenance_db=maintenance_db,\n password=password, runas=runas)\n if ret['retcode'] != 0:\n return False\n\n return True", "response": "Change tablespace name owner or options."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nremoving a tablespace from the Postgres server.", "response": "def tablespace_remove(name, user=None, host=None, port=None,\n maintenance_db=None, password=None, runas=None):\n '''\n Removes a tablespace from the Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.tablespace_remove tsname\n\n .. versionadded:: 2015.8.0\n '''\n query = 'DROP TABLESPACE \"{0}\"'.format(name)\n ret = _psql_prepare_and_run(['-c', query],\n user=user,\n host=host,\n port=port,\n runas=runas,\n maintenance_db=maintenance_db,\n password=password)\n return ret['retcode'] == 0"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef user_list(user=None, host=None, port=None, maintenance_db=None,\n password=None, runas=None, return_password=False):\n '''\n Return a dict with information about users of a Postgres server.\n\n Set return_password to True to get password hash in the result.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.user_list\n '''\n\n ret = {}\n\n ver = _parsed_version(user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n if ver:\n if ver >= _LooseVersion('9.1'):\n replication_column = 'pg_roles.rolreplication'\n else:\n replication_column = 'NULL'\n if ver >= _LooseVersion('9.5'):\n rolcatupdate_column = 'NULL'\n else:\n rolcatupdate_column = 'pg_roles.rolcatupdate'\n else:\n log.error('Could not retrieve Postgres version. Is Postgresql server running?')\n return False\n\n # will return empty string if return_password = False\n _x = lambda s: s if return_password else ''\n\n query = (''.join([\n 'SELECT '\n 'pg_roles.rolname as \"name\",'\n 'pg_roles.rolsuper as \"superuser\", '\n 'pg_roles.rolinherit as \"inherits privileges\", '\n 'pg_roles.rolcreaterole as \"can create roles\", '\n 'pg_roles.rolcreatedb as \"can create databases\", '\n '{0} as \"can update system catalogs\", '\n 'pg_roles.rolcanlogin as \"can login\", '\n '{1} as \"replication\", '\n 'pg_roles.rolconnlimit as \"connections\", '\n '(SELECT array_agg(pg_roles2.rolname)'\n ' FROM pg_catalog.pg_auth_members'\n ' JOIN pg_catalog.pg_roles pg_roles2 ON (pg_auth_members.roleid = pg_roles2.oid)'\n ' WHERE pg_auth_members.member = pg_roles.oid) as \"groups\",'\n 'pg_roles.rolvaliduntil::timestamp(0) as \"expiry time\", '\n 'pg_roles.rolconfig as \"defaults variables\" '\n , _x(', COALESCE(pg_shadow.passwd, pg_authid.rolpassword) as \"password\" '),\n 'FROM pg_roles '\n , _x('LEFT JOIN pg_authid ON pg_roles.oid = pg_authid.oid ')\n , _x('LEFT JOIN pg_shadow ON pg_roles.oid = pg_shadow.usesysid')\n ]).format(rolcatupdate_column, replication_column))\n\n rows = psql_query(query,\n runas=runas,\n host=host,\n user=user,\n port=port,\n maintenance_db=maintenance_db,\n password=password)\n\n def get_bool(rowdict, key):\n '''\n Returns the boolean value of the key, instead of 't' and 'f' strings.\n '''\n if rowdict[key] == 't':\n return True\n elif rowdict[key] == 'f':\n return False\n else:\n return None\n\n for row in rows:\n retrow = {}\n for key in ('superuser', 'inherits privileges', 'can create roles',\n 'can create databases', 'can update system catalogs',\n 'can login', 'replication', 'connections'):\n retrow[key] = get_bool(row, key)\n for date_key in ('expiry time',):\n try:\n retrow[date_key] = datetime.datetime.strptime(\n row[date_key], '%Y-%m-%d %H:%M:%S')\n except ValueError:\n retrow[date_key] = None\n retrow['defaults variables'] = row['defaults variables']\n if return_password:\n retrow['password'] = row['password']\n # use csv reader to handle quoted roles correctly\n retrow['groups'] = list(csv.reader([row['groups'].strip('{}')]))[0]\n ret[row['name']] = retrow\n\n return ret", "response": "Return a dict with information about users of a Postgres server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef role_get(name, user=None, host=None, port=None, maintenance_db=None,\n password=None, runas=None, return_password=False):\n '''\n Return a dict with information about users of a Postgres server.\n\n Set return_password to True to get password hash in the result.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.role_get postgres\n '''\n all_users = user_list(user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas,\n return_password=return_password)\n try:\n return all_users.get(name, None)\n except AttributeError:\n log.error('Could not retrieve Postgres role. Is Postgres running?')\n return None", "response": "Get a Postgres role"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks if a user exists on the Postgres server.", "response": "def user_exists(name,\n user=None, host=None, port=None, maintenance_db=None,\n password=None,\n runas=None):\n '''\n Checks if a user exists on the Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.user_exists 'username'\n '''\n return bool(\n role_get(name,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas,\n return_password=False))"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _maybe_encrypt_password(role,\n password,\n encrypted=_DEFAULT_PASSWORDS_ENCRYPTION):\n '''\n pgsql passwords are md5 hashes of the string: 'md5{password}{rolename}'\n '''\n if password is not None:\n password = six.text_type(password)\n if encrypted and password and not password.startswith('md5'):\n password = \"md5{0}\".format(\n hashlib.md5(salt.utils.stringutils.to_bytes('{0}{1}'.format(password, role))).hexdigest())\n return password", "response": "Helper function to encrypt the password if encrypted is set to True."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _role_create(name,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n createdb=None,\n createroles=None,\n encrypted=None,\n superuser=None,\n login=None,\n connlimit=None,\n inherit=None,\n replication=None,\n rolepassword=None,\n valid_until=None,\n typ_='role',\n groups=None,\n runas=None):\n '''\n Creates a Postgres role. Users and Groups are both roles in postgres.\n However, users can login, groups cannot.\n '''\n\n # check if role exists\n if user_exists(name, user, host, port, maintenance_db,\n password=password, runas=runas):\n log.info('%s \\'%s\\' already exists', typ_.capitalize(), name)\n return False\n\n sub_cmd = 'CREATE ROLE \"{0}\" WITH'.format(name)\n sub_cmd = '{0} {1}'.format(sub_cmd, _role_cmd_args(\n name,\n typ_=typ_,\n encrypted=encrypted,\n login=login,\n connlimit=connlimit,\n inherit=inherit,\n createdb=createdb,\n createroles=createroles,\n superuser=superuser,\n groups=groups,\n replication=replication,\n rolepassword=rolepassword,\n valid_until=valid_until\n ))\n ret = _psql_prepare_and_run(['-c', sub_cmd],\n runas=runas, host=host, user=user, port=port,\n maintenance_db=maintenance_db,\n password=password)\n\n return ret['retcode'] == 0", "response": "Create a Postgres role."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a Postgres user.", "response": "def user_create(username,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n createdb=None,\n createroles=None,\n inherit=None,\n login=None,\n connlimit=None,\n encrypted=None,\n superuser=None,\n replication=None,\n rolepassword=None,\n valid_until=None,\n groups=None,\n runas=None):\n '''\n Creates a Postgres user.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' postgres.user_create 'username' user='user' \\\\\n host='hostname' port='port' password='password' \\\\\n rolepassword='rolepassword' valid_until='valid_until'\n '''\n return _role_create(username,\n typ_='user',\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n createdb=createdb,\n createroles=createroles,\n inherit=inherit,\n login=login,\n connlimit=connlimit,\n encrypted=encrypted,\n superuser=superuser,\n replication=replication,\n rolepassword=rolepassword,\n valid_until=valid_until,\n groups=groups,\n runas=runas)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _role_update(name,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n createdb=None,\n typ_='role',\n createroles=None,\n inherit=None,\n login=None,\n connlimit=None,\n encrypted=None,\n superuser=None,\n replication=None,\n rolepassword=None,\n valid_until=None,\n groups=None,\n runas=None):\n '''\n Updates a postgres role.\n '''\n role = role_get(name,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas,\n return_password=False)\n\n # check if user exists\n if not bool(role):\n log.info(\n '%s \\'%s\\' could not be found', typ_.capitalize(), name\n )\n return False\n\n sub_cmd = 'ALTER ROLE \"{0}\" WITH'.format(name)\n sub_cmd = '{0} {1}'.format(sub_cmd, _role_cmd_args(\n name,\n encrypted=encrypted,\n login=login,\n connlimit=connlimit,\n inherit=inherit,\n createdb=createdb,\n createroles=createroles,\n superuser=superuser,\n groups=groups,\n replication=replication,\n rolepassword=rolepassword,\n valid_until=valid_until,\n db_role=role\n ))\n ret = _psql_prepare_and_run(['-c', sub_cmd],\n runas=runas, host=host, user=user, port=port,\n maintenance_db=maintenance_db,\n password=password)\n\n return ret['retcode'] == 0", "response": "Update a postgres role."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _role_remove(name, user=None, host=None, port=None, maintenance_db=None,\n password=None, runas=None):\n '''\n Removes a role from the Postgres Server\n '''\n\n # check if user exists\n if not user_exists(name, user, host, port, maintenance_db,\n password=password, runas=runas):\n log.info('User \\'%s\\' does not exist', name)\n return False\n\n # user exists, proceed\n sub_cmd = 'DROP ROLE \"{0}\"'.format(name)\n _psql_prepare_and_run(\n ['-c', sub_cmd],\n runas=runas, host=host, user=user, port=port,\n maintenance_db=maintenance_db, password=password)\n\n if not user_exists(name, user, host, port, maintenance_db,\n password=password, runas=runas):\n return True\n else:\n log.info('Failed to delete user \\'%s\\'.', name)\n return False", "response": "Removes a role from the Postgres Server\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlisting installed postgresql extensions", "response": "def installed_extensions(user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n runas=None):\n '''\n List installed postgresql extensions\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.installed_extensions\n\n '''\n exts = []\n query = (\n 'select a.*, b.nspname as schema_name '\n 'from pg_extension a, pg_namespace b where a.extnamespace = b.oid;'\n )\n ret = psql_query(query, user=user, host=host, port=port,\n maintenance_db=maintenance_db,\n password=password, runas=runas)\n exts = {}\n for row in ret:\n if 'extversion' in row and 'extname' in row:\n exts[row['extname']] = row\n return exts"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting info about an available postgresql extension", "response": "def get_available_extension(name,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n runas=None):\n '''\n Get info about an available postgresql extension\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.get_available_extension plpgsql\n\n '''\n return available_extensions(user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas).get(name, None)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_installed_extension(name,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n runas=None):\n '''\n Get info about an installed postgresql extension\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.get_installed_extension plpgsql\n\n '''\n return installed_extensions(user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas).get(name, None)", "response": "Get info about an installed postgresql extension"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ntesting if a specific extension is available", "response": "def is_available_extension(name,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n runas=None):\n '''\n Test if a specific extension is available\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.is_available_extension\n\n '''\n exts = available_extensions(user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n if name.lower() in [\n a.lower()\n for a in exts\n ]:\n return True\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntest if a specific extension is installed in the current environment", "response": "def is_installed_extension(name,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n runas=None):\n '''\n Test if a specific extension is installed\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.is_installed_extension\n\n '''\n installed_ext = get_installed_extension(\n name,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n return bool(installed_ext)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate metadata for an extension.", "response": "def create_metadata(name,\n ext_version=None,\n schema=None,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n runas=None):\n '''\n Get lifecycle information about an extension\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.create_metadata adminpack\n\n '''\n installed_ext = get_installed_extension(\n name,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n ret = [_EXTENSION_NOT_INSTALLED]\n if installed_ext:\n ret = [_EXTENSION_INSTALLED]\n if (\n ext_version is not None\n and _pg_is_older_ext_ver(\n installed_ext.get('extversion', ext_version),\n ext_version\n )\n ):\n ret.append(_EXTENSION_TO_UPGRADE)\n if (\n schema is not None\n and installed_ext.get('extrelocatable', 'f') == 't'\n and installed_ext.get('schema_name', schema) != schema\n ):\n ret.append(_EXTENSION_TO_MOVE)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndrops an installed postgresql extension.", "response": "def drop_extension(name,\n if_exists=None,\n restrict=None,\n cascade=None,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n runas=None):\n '''\n Drop an installed postgresql extension\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.drop_extension 'adminpack'\n\n '''\n if cascade is None:\n cascade = True\n if if_exists is None:\n if_exists = False\n if restrict is None:\n restrict = False\n args = ['DROP EXTENSION']\n if if_exists:\n args.append('IF EXISTS')\n args.append(name)\n if cascade:\n args.append('CASCADE')\n if restrict:\n args.append('RESTRICT')\n args.append(';')\n cmd = ' '.join(args)\n if is_installed_extension(name,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas):\n _psql_prepare_and_run(\n ['-c', cmd],\n runas=runas, host=host, user=user, port=port,\n maintenance_db=maintenance_db, password=password)\n ret = not is_installed_extension(name,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n if not ret:\n log.info('Failed to drop ext: %s', name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_extension(name,\n if_not_exists=None,\n schema=None,\n ext_version=None,\n from_version=None,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n runas=None):\n '''\n Install a postgresql extension\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.create_extension 'adminpack'\n\n '''\n if if_not_exists is None:\n if_not_exists = True\n mtdata = create_metadata(name,\n ext_version=ext_version,\n schema=schema,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n installed = _EXTENSION_NOT_INSTALLED not in mtdata\n installable = is_available_extension(name,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n if installable:\n if not installed:\n args = ['CREATE EXTENSION']\n if if_not_exists:\n args.append('IF NOT EXISTS')\n args.append('\"{0}\"'.format(name))\n sargs = []\n if schema:\n sargs.append('SCHEMA \"{0}\"'.format(schema))\n if ext_version:\n sargs.append('VERSION {0}'.format(ext_version))\n if from_version:\n sargs.append('FROM {0}'.format(from_version))\n if sargs:\n args.append('WITH')\n args.extend(sargs)\n args.append(';')\n cmd = ' '.join(args).strip()\n else:\n args = []\n if schema and _EXTENSION_TO_MOVE in mtdata:\n args.append('ALTER EXTENSION \"{0}\" SET SCHEMA \"{1}\";'.format(\n name, schema))\n if ext_version and _EXTENSION_TO_UPGRADE in mtdata:\n args.append('ALTER EXTENSION \"{0}\" UPDATE TO {1};'.format(\n name, ext_version))\n cmd = ' '.join(args).strip()\n if cmd:\n _psql_prepare_and_run(\n ['-c', cmd],\n runas=runas, host=host, user=user, port=port,\n maintenance_db=maintenance_db, password=password)\n mtdata = create_metadata(name,\n ext_version=ext_version,\n schema=schema,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n ret = True\n for i in _EXTENSION_FLAGS:\n if (i in mtdata) and (i != _EXTENSION_INSTALLED):\n ret = False\n if not ret:\n log.info('Failed to create ext: %s', name)\n return ret", "response": "Create an extension in the postgresql database."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef user_remove(username,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n runas=None):\n '''\n Removes a user from the Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.user_remove 'username'\n '''\n return _role_remove(username,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)", "response": "Removes a user from the Postgres server."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a Postgres group.", "response": "def group_create(groupname,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n createdb=None,\n createroles=None,\n encrypted=None,\n login=None,\n inherit=None,\n superuser=None,\n replication=None,\n rolepassword=None,\n groups=None,\n runas=None):\n '''\n Creates a Postgres group. A group is postgres is similar to a user, but\n cannot login.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.group_create 'groupname' user='user' \\\\\n host='hostname' port='port' password='password' \\\\\n rolepassword='rolepassword'\n '''\n return _role_create(groupname,\n user=user,\n typ_='group',\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n createdb=createdb,\n createroles=createroles,\n encrypted=encrypted,\n login=login,\n inherit=inherit,\n superuser=superuser,\n replication=replication,\n rolepassword=rolepassword,\n groups=groups,\n runas=runas)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef group_remove(groupname,\n user=None,\n host=None,\n port=None,\n maintenance_db=None,\n password=None,\n runas=None):\n '''\n Removes a group from the Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.group_remove 'groupname'\n '''\n return _role_remove(groupname,\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)", "response": "Removes a group from the Postgres server."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets the owner of all schemas functions tables views and sequences to", "response": "def owner_to(dbname,\n ownername,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n Set the owner of all schemas, functions, tables, views and sequences to\n the given username.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.owner_to 'dbname' 'username'\n '''\n\n sqlfile = tempfile.NamedTemporaryFile()\n sqlfile.write('begin;\\n')\n sqlfile.write(\n 'alter database \"{0}\" owner to \"{1}\";\\n'.format(\n dbname, ownername\n )\n )\n\n queries = (\n # schemas\n ('alter schema {n} owner to {owner};',\n 'select quote_ident(schema_name) as n from '\n 'information_schema.schemata;'),\n # tables and views\n ('alter table {n} owner to {owner};',\n 'select quote_ident(table_schema)||\\'.\\'||quote_ident(table_name) as '\n 'n from information_schema.tables where table_schema not in '\n '(\\'pg_catalog\\', \\'information_schema\\');'),\n # functions\n ('alter function {n} owner to {owner};',\n 'select p.oid::regprocedure::text as n from pg_catalog.pg_proc p '\n 'join pg_catalog.pg_namespace ns on p.pronamespace=ns.oid where '\n 'ns.nspname not in (\\'pg_catalog\\', \\'information_schema\\') '\n ' and not p.proisagg;'),\n # aggregate functions\n ('alter aggregate {n} owner to {owner};',\n 'select p.oid::regprocedure::text as n from pg_catalog.pg_proc p '\n 'join pg_catalog.pg_namespace ns on p.pronamespace=ns.oid where '\n 'ns.nspname not in (\\'pg_catalog\\', \\'information_schema\\') '\n 'and p.proisagg;'),\n # sequences\n ('alter sequence {n} owner to {owner};',\n 'select quote_ident(sequence_schema)||\\'.\\'||'\n 'quote_ident(sequence_name) as n from information_schema.sequences;')\n )\n\n for fmt, query in queries:\n ret = psql_query(query, user=user, host=host, port=port,\n maintenance_db=dbname, password=password, runas=runas)\n for row in ret:\n sqlfile.write(fmt.format(owner=ownername, n=row['n']) + '\\n')\n\n sqlfile.write('commit;\\n')\n sqlfile.flush()\n os.chmod(sqlfile.name, 0o644) # ensure psql can read the file\n\n # run the generated sqlfile in the db\n cmdret = _psql_prepare_and_run(['-f', sqlfile.name],\n user=user,\n runas=runas,\n host=host,\n port=port,\n password=password,\n maintenance_db=dbname)\n return cmdret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef schema_create(dbname, name, owner=None,\n user=None,\n db_user=None, db_password=None,\n db_host=None, db_port=None):\n '''\n Creates a Postgres schema.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.schema_create dbname name owner='owner' \\\\\n user='user' \\\\\n db_user='user' db_password='password'\n db_host='hostname' db_port='port'\n '''\n\n # check if schema exists\n if schema_exists(dbname, name, user=user,\n db_user=db_user, db_password=db_password,\n db_host=db_host, db_port=db_port):\n log.info('\\'%s\\' already exists in \\'%s\\'', name, dbname)\n return False\n\n sub_cmd = 'CREATE SCHEMA \"{0}\"'.format(name)\n if owner is not None:\n sub_cmd = '{0} AUTHORIZATION \"{1}\"'.format(sub_cmd, owner)\n\n ret = _psql_prepare_and_run(['-c', sub_cmd],\n user=db_user, password=db_password,\n port=db_port, host=db_host,\n maintenance_db=dbname, runas=user)\n\n return ret['retcode'] == 0", "response": "Create a Postgres schema."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nremoving a schema from the Postgres server.", "response": "def schema_remove(dbname, name,\n user=None,\n db_user=None, db_password=None,\n db_host=None, db_port=None):\n '''\n Removes a schema from the Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.schema_remove dbname schemaname\n\n dbname\n Database name we work on\n\n schemaname\n The schema's name we'll remove\n\n user\n System user all operations should be performed on behalf of\n\n db_user\n database username if different from config or default\n\n db_password\n user password if any password for a specified user\n\n db_host\n Database host if different from config or default\n\n db_port\n Database port if different from config or default\n\n '''\n\n # check if schema exists\n if not schema_exists(dbname, name, user=None,\n db_user=db_user, db_password=db_password,\n db_host=db_host, db_port=db_port):\n log.info('Schema \\'%s\\' does not exist in \\'%s\\'', name, dbname)\n return False\n\n # schema exists, proceed\n sub_cmd = 'DROP SCHEMA \"{0}\"'.format(name)\n _psql_prepare_and_run(\n ['-c', sub_cmd],\n runas=user,\n maintenance_db=dbname,\n host=db_host, user=db_user, port=db_port, password=db_password)\n\n if not schema_exists(dbname, name, user,\n db_user=db_user, db_password=db_password,\n db_host=db_host, db_port=db_port):\n return True\n else:\n log.info('Failed to delete schema \\'%s\\'.', name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking if a schema exists on the Postgres server.", "response": "def schema_exists(dbname, name, user=None,\n db_user=None, db_password=None,\n db_host=None, db_port=None):\n '''\n Checks if a schema exists on the Postgres server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.schema_exists dbname schemaname\n\n dbname\n Database name we query on\n\n name\n Schema name we look for\n\n user\n The system user the operation should be performed on behalf of\n\n db_user\n database username if different from config or default\n\n db_password\n user password if any password for a specified user\n\n db_host\n Database host if different from config or default\n\n db_port\n Database port if different from config or default\n\n '''\n return bool(\n schema_get(dbname, name, user=user,\n db_user=db_user,\n db_host=db_host,\n db_port=db_port,\n db_password=db_password))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef schema_get(dbname, name, user=None,\n db_user=None, db_password=None,\n db_host=None, db_port=None):\n '''\n Return a dict with information about schemas in a database.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.schema_get dbname name\n\n dbname\n Database name we query on\n\n name\n Schema name we look for\n\n user\n The system user the operation should be performed on behalf of\n\n db_user\n database username if different from config or default\n\n db_password\n user password if any password for a specified user\n\n db_host\n Database host if different from config or default\n\n db_port\n Database port if different from config or default\n '''\n all_schemas = schema_list(dbname, user=user,\n db_user=db_user,\n db_host=db_host,\n db_port=db_port,\n db_password=db_password)\n try:\n return all_schemas.get(name, None)\n except AttributeError:\n log.error('Could not retrieve Postgres schema. Is Postgres running?')\n return False", "response": "Get a specific schema from a database."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a dict with information about schemas in a Postgres database.", "response": "def schema_list(dbname, user=None,\n db_user=None, db_password=None,\n db_host=None, db_port=None):\n '''\n Return a dict with information about schemas in a Postgres database.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.schema_list dbname\n\n dbname\n Database name we query on\n\n user\n The system user the operation should be performed on behalf of\n\n db_user\n database username if different from config or default\n\n db_password\n user password if any password for a specified user\n\n db_host\n Database host if different from config or default\n\n db_port\n Database port if different from config or default\n '''\n\n ret = {}\n\n query = (''.join([\n 'SELECT '\n 'pg_namespace.nspname as \"name\",'\n 'pg_namespace.nspacl as \"acl\", '\n 'pg_roles.rolname as \"owner\" '\n 'FROM pg_namespace '\n 'LEFT JOIN pg_roles ON pg_roles.oid = pg_namespace.nspowner '\n ]))\n\n rows = psql_query(query, runas=user,\n host=db_host,\n user=db_user,\n port=db_port,\n maintenance_db=dbname,\n password=db_password)\n\n for row in rows:\n retrow = {}\n for key in ('owner', 'acl'):\n retrow[key] = row[key]\n ret[row['name']] = retrow\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a list of languages in a database.", "response": "def language_list(\n maintenance_db,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n .. versionadded:: 2016.3.0\n\n Return a list of languages in a database.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.language_list dbname\n\n maintenance_db\n The database to check\n\n user\n database username if different from config or default\n\n password\n user password if any password for a specified user\n\n host\n Database host if different from config or default\n\n port\n Database port if different from config or default\n\n runas\n System user all operations should be performed on behalf of\n '''\n\n ret = {}\n query = 'SELECT lanname AS \"Name\" FROM pg_language'\n\n rows = psql_query(\n query,\n runas=runas,\n host=host,\n user=user,\n port=port,\n maintenance_db=maintenance_db,\n password=password)\n\n for row in rows:\n ret[row['Name']] = row['Name']\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking if a language exists in a database.", "response": "def language_exists(\n name,\n maintenance_db,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n .. versionadded:: 2016.3.0\n\n Checks if language exists in a database.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.language_exists plpgsql dbname\n\n name\n Language to check for\n\n maintenance_db\n The database to check in\n\n user\n database username if different from config or default\n\n password\n user password if any password for a specified user\n\n host\n Database host if different from config or default\n\n port\n Database port if different from config or default\n\n runas\n System user all operations should be performed on behalf of\n\n '''\n\n languages = language_list(\n maintenance_db, user=user, host=host,\n port=port, password=password,\n runas=runas)\n\n return name in languages"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef language_create(name,\n maintenance_db,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n .. versionadded:: 2016.3.0\n\n Installs a language into a database\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.language_create plpgsql dbname\n\n name\n Language to install\n\n maintenance_db\n The database to install the language in\n\n user\n database username if different from config or default\n\n password\n user password if any password for a specified user\n\n host\n Database host if different from config or default\n\n port\n Database port if different from config or default\n\n runas\n System user all operations should be performed on behalf of\n '''\n\n if language_exists(name, maintenance_db):\n log.info('Language %s already exists in %s', name, maintenance_db)\n return False\n\n query = 'CREATE LANGUAGE {0}'.format(name)\n\n ret = _psql_prepare_and_run(['-c', query],\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n\n return ret['retcode'] == 0", "response": "Creates a new language in a database"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ngenerate the SQL required for the default privileges list.", "response": "def _make_default_privileges_list_query(name, object_type, prepend):\n '''\n Generate the SQL required for specific object type\n '''\n if object_type == 'table':\n query = (' '.join([\n 'SELECT defacl.defaclacl AS name',\n 'FROM pg_default_acl defacl',\n 'JOIN pg_authid aid',\n 'ON defacl.defaclrole = aid.oid ',\n 'JOIN pg_namespace nsp ',\n 'ON nsp.oid = defacl.defaclnamespace',\n \"WHERE nsp.nspname = '{0}'\",\n \"AND defaclobjtype ='r'\",\n 'ORDER BY nspname',\n ])).format(prepend)\n elif object_type == 'sequence':\n query = (' '.join([\n 'SELECT defacl.defaclacl AS name',\n 'FROM pg_default_acl defacl',\n 'JOIN pg_authid aid',\n 'ON defacl.defaclrole = aid.oid ',\n 'JOIN pg_namespace nsp ',\n 'ON nsp.oid = defacl.defaclnamespace',\n \"WHERE nsp.nspname = '{0}'\",\n \"AND defaclobjtype ='S'\",\n 'ORDER BY nspname',\n ])).format(prepend, name)\n elif object_type == 'schema':\n query = (' '.join([\n 'SELECT nspacl AS name',\n 'FROM pg_catalog.pg_namespace',\n \"WHERE nspname = '{0}'\",\n 'ORDER BY nspname',\n ])).format(name)\n elif object_type == 'function':\n query = (' '.join([\n 'SELECT defacl.defaclacl AS name',\n 'FROM pg_default_acl defacl',\n 'JOIN pg_authid aid',\n 'ON defacl.defaclrole = aid.oid ',\n 'JOIN pg_namespace nsp ',\n 'ON nsp.oid = defacl.defaclnamespace',\n \"WHERE nsp.nspname = '{0}'\",\n \"AND defaclobjtype ='f'\",\n 'ORDER BY nspname',\n ])).format(prepend, name)\n elif object_type == 'group':\n query = (' '.join([\n 'SELECT rolname, admin_option',\n 'FROM pg_catalog.pg_auth_members m',\n 'JOIN pg_catalog.pg_roles r',\n 'ON m.member=r.oid',\n 'WHERE m.roleid IN',\n '(SELECT oid',\n 'FROM pg_catalog.pg_roles',\n \"WHERE rolname='{0}')\",\n 'ORDER BY rolname',\n ])).format(name)\n\n return query"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _make_privileges_list_query(name, object_type, prepend):\n '''\n Generate the SQL required for specific object type\n '''\n if object_type == 'table':\n query = (' '.join([\n 'SELECT relacl AS name',\n 'FROM pg_catalog.pg_class c',\n 'JOIN pg_catalog.pg_namespace n',\n 'ON n.oid = c.relnamespace',\n \"WHERE nspname = '{0}'\",\n \"AND relname = '{1}'\",\n \"AND relkind = 'r'\",\n 'ORDER BY relname',\n ])).format(prepend, name)\n elif object_type == 'sequence':\n query = (' '.join([\n 'SELECT relacl AS name',\n 'FROM pg_catalog.pg_class c',\n 'JOIN pg_catalog.pg_namespace n',\n 'ON n.oid = c.relnamespace',\n \"WHERE nspname = '{0}'\",\n \"AND relname = '{1}'\",\n \"AND relkind = 'S'\",\n 'ORDER BY relname',\n ])).format(prepend, name)\n elif object_type == 'schema':\n query = (' '.join([\n 'SELECT nspacl AS name',\n 'FROM pg_catalog.pg_namespace',\n \"WHERE nspname = '{0}'\",\n 'ORDER BY nspname',\n ])).format(name)\n elif object_type == 'function':\n query = (' '.join([\n 'SELECT proacl AS name',\n 'FROM pg_catalog.pg_proc p',\n 'JOIN pg_catalog.pg_namespace n',\n 'ON n.oid = p.pronamespace',\n \"WHERE nspname = '{0}'\",\n \"AND p.oid::regprocedure::text = '{1}'\",\n 'ORDER BY proname, proargtypes',\n ])).format(prepend, name)\n elif object_type == 'tablespace':\n query = (' '.join([\n 'SELECT spcacl AS name',\n 'FROM pg_catalog.pg_tablespace',\n \"WHERE spcname = '{0}'\",\n 'ORDER BY spcname',\n ])).format(name)\n elif object_type == 'language':\n query = (' '.join([\n 'SELECT lanacl AS name',\n 'FROM pg_catalog.pg_language',\n \"WHERE lanname = '{0}'\",\n 'ORDER BY lanname',\n ])).format(name)\n elif object_type == 'database':\n query = (' '.join([\n 'SELECT datacl AS name',\n 'FROM pg_catalog.pg_database',\n \"WHERE datname = '{0}'\",\n 'ORDER BY datname',\n ])).format(name)\n elif object_type == 'group':\n query = (' '.join([\n 'SELECT rolname, admin_option',\n 'FROM pg_catalog.pg_auth_members m',\n 'JOIN pg_catalog.pg_roles r',\n 'ON m.member=r.oid',\n 'WHERE m.roleid IN',\n '(SELECT oid',\n 'FROM pg_catalog.pg_roles',\n \"WHERE rolname='{0}')\",\n 'ORDER BY rolname',\n ])).format(name)\n\n return query", "response": "Generates the SQL required for the object type of the object."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_object_owner(name,\n object_type,\n prepend='public',\n maintenance_db=None,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n Return the owner of a postgres object\n '''\n if object_type == 'table':\n query = (' '.join([\n 'SELECT tableowner AS name',\n 'FROM pg_tables',\n \"WHERE schemaname = '{0}'\",\n \"AND tablename = '{1}'\"\n ])).format(prepend, name)\n elif object_type == 'sequence':\n query = (' '.join([\n 'SELECT rolname AS name',\n 'FROM pg_catalog.pg_class c',\n 'JOIN pg_roles r',\n 'ON c.relowner = r.oid',\n 'JOIN pg_catalog.pg_namespace n',\n 'ON n.oid = c.relnamespace',\n \"WHERE relkind='S'\",\n \"AND nspname='{0}'\",\n \"AND relname = '{1}'\",\n ])).format(prepend, name)\n elif object_type == 'schema':\n query = (' '.join([\n 'SELECT rolname AS name',\n 'FROM pg_namespace n',\n 'JOIN pg_roles r',\n 'ON n.nspowner = r.oid',\n \"WHERE nspname = '{0}'\",\n ])).format(name)\n elif object_type == 'function':\n query = (' '.join([\n 'SELECT proname AS name',\n 'FROM pg_catalog.pg_proc p',\n 'JOIN pg_catalog.pg_namespace n',\n 'ON n.oid = p.pronamespace',\n \"WHERE nspname = '{0}'\",\n \"AND p.oid::regprocedure::text = '{1}'\",\n 'ORDER BY proname, proargtypes',\n ])).format(prepend, name)\n elif object_type == 'tablespace':\n query = (' '.join([\n 'SELECT rolname AS name',\n 'FROM pg_tablespace t',\n 'JOIN pg_roles r',\n 'ON t.spcowner = r.oid',\n \"WHERE spcname = '{0}'\",\n ])).format(name)\n elif object_type == 'language':\n query = (' '.join([\n 'SELECT rolname AS name',\n 'FROM pg_language l',\n 'JOIN pg_roles r',\n 'ON l.lanowner = r.oid',\n \"WHERE lanname = '{0}'\",\n ])).format(name)\n elif object_type == 'database':\n query = (' '.join([\n 'SELECT rolname AS name',\n 'FROM pg_database d',\n 'JOIN pg_roles r',\n 'ON d.datdba = r.oid',\n \"WHERE datname = '{0}'\",\n ])).format(name)\n\n rows = psql_query(\n query,\n runas=runas,\n host=host,\n user=user,\n port=port,\n maintenance_db=maintenance_db,\n password=password)\n try:\n ret = rows[0]['name']\n except IndexError:\n ret = None\n\n return ret", "response": "Get the owner of a postgres object."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nvalidate the supplied default privileges and object_type", "response": "def _validate_default_privileges(object_type, defprivs, defprivileges):\n '''\n Validate the supplied privileges\n '''\n if object_type != 'group':\n _defperms = [_DEFAULT_PRIVILEGES_MAP[defperm]\n for defperm in _DEFAULT_PRIVILEGE_TYPE_MAP[object_type]]\n _defperms.append('ALL')\n\n if object_type not in _DEFAULT_PRIVILEGES_OBJECTS:\n raise SaltInvocationError(\n 'Invalid object_type: {0} provided'.format(object_type))\n\n if not set(defprivs).issubset(set(_defperms)):\n raise SaltInvocationError(\n 'Invalid default privilege(s): {0} provided for object {1}'.format(\n defprivileges, object_type))\n else:\n if defprivileges:\n raise SaltInvocationError(\n 'The default privileges option should not '\n 'be set for object_type group')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _mod_defpriv_opts(object_type, defprivileges):\n '''\n Format options\n '''\n object_type = object_type.lower()\n defprivileges = '' if defprivileges is None else defprivileges\n _defprivs = re.split(r'\\s?,\\s?', defprivileges.upper())\n\n return object_type, defprivileges, _defprivs", "response": "Format options for defprivileges and object_type"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nprocess a single defpriv part.", "response": "def _process_defpriv_part(defperms):\n '''\n Process part\n '''\n _tmp = {}\n previous = None\n for defperm in defperms:\n if previous is None:\n _tmp[_DEFAULT_PRIVILEGES_MAP[defperm]] = False\n previous = _DEFAULT_PRIVILEGES_MAP[defperm]\n else:\n if defperm == '*':\n _tmp[previous] = True\n else:\n _tmp[_DEFAULT_PRIVILEGES_MAP[defperm]] = False\n previous = _DEFAULT_PRIVILEGES_MAP[defperm]\n return _tmp"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _validate_privileges(object_type, privs, privileges):\n '''\n Validate the supplied privileges\n '''\n if object_type != 'group':\n _perms = [_PRIVILEGES_MAP[perm]\n for perm in _PRIVILEGE_TYPE_MAP[object_type]]\n _perms.append('ALL')\n\n if object_type not in _PRIVILEGES_OBJECTS:\n raise SaltInvocationError(\n 'Invalid object_type: {0} provided'.format(object_type))\n\n if not set(privs).issubset(set(_perms)):\n raise SaltInvocationError(\n 'Invalid privilege(s): {0} provided for object {1}'.format(\n privileges, object_type))\n else:\n if privileges:\n raise SaltInvocationError(\n 'The privileges option should not '\n 'be set for object_type group')", "response": "Validate the supplied privileges\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _mod_priv_opts(object_type, privileges):\n '''\n Format options\n '''\n object_type = object_type.lower()\n privileges = '' if privileges is None else privileges\n _privs = re.split(r'\\s?,\\s?', privileges.upper())\n\n return object_type, privileges, _privs", "response": "Format options for private objects"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nprocesses the private part of a node.", "response": "def _process_priv_part(perms):\n '''\n Process part\n '''\n _tmp = {}\n previous = None\n for perm in perms:\n if previous is None:\n _tmp[_PRIVILEGES_MAP[perm]] = False\n previous = _PRIVILEGES_MAP[perm]\n else:\n if perm == '*':\n _tmp[previous] = True\n else:\n _tmp[_PRIVILEGES_MAP[perm]] = False\n previous = _PRIVILEGES_MAP[perm]\n return _tmp"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a list of default privileges for the specified object.", "response": "def default_privileges_list(\n name,\n object_type,\n prepend='public',\n maintenance_db=None,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n .. versionadded:: 2019.0.0\n\n Return a list of default privileges for the specified object.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.default_privileges_list table_name table maintenance_db=db_name\n\n name\n Name of the object for which the permissions should be returned\n\n object_type\n The object type, which can be one of the following:\n\n - table\n - sequence\n - schema\n - group\n - function\n\n prepend\n Table and Sequence object types live under a schema so this should be\n provided if the object is not under the default `public` schema\n\n maintenance_db\n The database to connect to\n\n user\n database username if different from config or default\n\n password\n user password if any password for a specified user\n\n host\n Database host if different from config or default\n\n port\n Database port if different from config or default\n\n runas\n System user all operations should be performed on behalf of\n '''\n object_type = object_type.lower()\n query = _make_default_privileges_list_query(name, object_type, prepend)\n\n if object_type not in _DEFAULT_PRIVILEGES_OBJECTS:\n raise SaltInvocationError(\n 'Invalid object_type: {0} provided'.format(object_type))\n\n rows = psql_query(\n query,\n runas=runas,\n host=host,\n user=user,\n port=port,\n maintenance_db=maintenance_db,\n password=password)\n\n ret = {}\n\n for row in rows:\n if object_type != 'group':\n result = row['name']\n result = result.strip('{}')\n parts = result.split(',')\n for part in parts:\n perms_part, _ = part.split('/')\n rolename, defperms = perms_part.split('=')\n if rolename == '':\n rolename = 'public'\n _tmp = _process_defpriv_part(defperms)\n ret[rolename] = _tmp\n else:\n if row['admin_option'] == 't':\n admin_option = True\n else:\n admin_option = False\n\n ret[row['rolname']] = admin_option\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a list of privileges for the specified object.", "response": "def privileges_list(\n name,\n object_type,\n prepend='public',\n maintenance_db=None,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n .. versionadded:: 2016.3.0\n\n Return a list of privileges for the specified object.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.privileges_list table_name table maintenance_db=db_name\n\n name\n Name of the object for which the permissions should be returned\n\n object_type\n The object type, which can be one of the following:\n\n - table\n - sequence\n - schema\n - tablespace\n - language\n - database\n - group\n - function\n\n prepend\n Table and Sequence object types live under a schema so this should be\n provided if the object is not under the default `public` schema\n\n maintenance_db\n The database to connect to\n\n user\n database username if different from config or default\n\n password\n user password if any password for a specified user\n\n host\n Database host if different from config or default\n\n port\n Database port if different from config or default\n\n runas\n System user all operations should be performed on behalf of\n '''\n object_type = object_type.lower()\n query = _make_privileges_list_query(name, object_type, prepend)\n\n if object_type not in _PRIVILEGES_OBJECTS:\n raise SaltInvocationError(\n 'Invalid object_type: {0} provided'.format(object_type))\n\n rows = psql_query(\n query,\n runas=runas,\n host=host,\n user=user,\n port=port,\n maintenance_db=maintenance_db,\n password=password)\n\n ret = {}\n\n for row in rows:\n if object_type != 'group':\n result = row['name']\n result = result.strip('{}')\n parts = result.split(',')\n for part in parts:\n perms_part, _ = part.split('/')\n rolename, perms = perms_part.split('=')\n if rolename == '':\n rolename = 'public'\n _tmp = _process_priv_part(perms)\n ret[rolename] = _tmp\n else:\n if row['admin_option'] == 't':\n admin_option = True\n else:\n admin_option = False\n\n ret[row['rolname']] = admin_option\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef has_default_privileges(name,\n object_name,\n object_type,\n defprivileges=None,\n grant_option=None,\n prepend='public',\n maintenance_db=None,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n .. versionadded:: 2019.0.0\n\n Check if a role has the specified privileges on an object\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.has_default_privileges user_name table_name table \\\\\n SELECT,INSERT maintenance_db=db_name\n\n name\n Name of the role whose privileges should be checked on object_type\n\n object_name\n Name of the object on which the check is to be performed\n\n object_type\n The object type, which can be one of the following:\n\n - table\n - sequence\n - schema\n - group\n - function\n\n privileges\n Comma separated list of privileges to check, from the list below:\n\n - INSERT\n - CREATE\n - TRUNCATE\n - TRIGGER\n - SELECT\n - USAGE\n - UPDATE\n - EXECUTE\n - REFERENCES\n - DELETE\n - ALL\n\n grant_option\n If grant_option is set to True, the grant option check is performed\n\n prepend\n Table and Sequence object types live under a schema so this should be\n provided if the object is not under the default `public` schema\n\n maintenance_db\n The database to connect to\n\n user\n database username if different from config or default\n\n password\n user password if any password for a specified user\n\n host\n Database host if different from config or default\n\n port\n Database port if different from config or default\n\n runas\n System user all operations should be performed on behalf of\n '''\n object_type, defprivileges, _defprivs = _mod_defpriv_opts(object_type, defprivileges)\n\n _validate_default_privileges(object_type, _defprivs, defprivileges)\n\n if object_type != 'group':\n owner = _get_object_owner(object_name, object_type, prepend=prepend,\n maintenance_db=maintenance_db, user=user, host=host, port=port,\n password=password, runas=runas)\n if owner is not None and name == owner:\n return True\n\n _defprivileges = default_privileges_list(object_name, object_type, prepend=prepend,\n maintenance_db=maintenance_db, user=user, host=host, port=port,\n password=password, runas=runas)\n\n if name in _defprivileges:\n if object_type == 'group':\n if grant_option:\n retval = _defprivileges[name]\n else:\n retval = True\n return retval\n else:\n _defperms = _DEFAULT_PRIVILEGE_TYPE_MAP[object_type]\n if grant_option:\n defperms = dict((_DEFAULT_PRIVILEGES_MAP[defperm], True) for defperm in _defperms)\n retval = defperms == _defprivileges[name]\n else:\n defperms = [_DEFAULT_PRIVILEGES_MAP[defperm] for defperm in _defperms]\n if 'ALL' in _defprivs:\n retval = sorted(defperms) == sorted(_defprivileges[name].keys())\n else:\n retval = set(_defprivs).issubset(\n set(_defprivileges[name].keys()))\n return retval\n\n return False", "response": "Check if a role has the specified default privileges."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncheck if a role has the specified privileges on an object.", "response": "def has_privileges(name,\n object_name,\n object_type,\n privileges=None,\n grant_option=None,\n prepend='public',\n maintenance_db=None,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n .. versionadded:: 2016.3.0\n\n Check if a role has the specified privileges on an object\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.has_privileges user_name table_name table \\\\\n SELECT,INSERT maintenance_db=db_name\n\n name\n Name of the role whose privileges should be checked on object_type\n\n object_name\n Name of the object on which the check is to be performed\n\n object_type\n The object type, which can be one of the following:\n\n - table\n - sequence\n - schema\n - tablespace\n - language\n - database\n - group\n - function\n\n privileges\n Comma separated list of privileges to check, from the list below:\n\n - INSERT\n - CREATE\n - TRUNCATE\n - CONNECT\n - TRIGGER\n - SELECT\n - USAGE\n - TEMPORARY\n - UPDATE\n - EXECUTE\n - REFERENCES\n - DELETE\n - ALL\n\n grant_option\n If grant_option is set to True, the grant option check is performed\n\n prepend\n Table and Sequence object types live under a schema so this should be\n provided if the object is not under the default `public` schema\n\n maintenance_db\n The database to connect to\n\n user\n database username if different from config or default\n\n password\n user password if any password for a specified user\n\n host\n Database host if different from config or default\n\n port\n Database port if different from config or default\n\n runas\n System user all operations should be performed on behalf of\n '''\n object_type, privileges, _privs = _mod_priv_opts(object_type, privileges)\n\n _validate_privileges(object_type, _privs, privileges)\n\n if object_type != 'group':\n owner = _get_object_owner(object_name, object_type, prepend=prepend,\n maintenance_db=maintenance_db, user=user, host=host, port=port,\n password=password, runas=runas)\n if owner is not None and name == owner:\n return True\n\n _privileges = privileges_list(object_name, object_type, prepend=prepend,\n maintenance_db=maintenance_db, user=user, host=host, port=port,\n password=password, runas=runas)\n\n if name in _privileges:\n if object_type == 'group':\n if grant_option:\n retval = _privileges[name]\n else:\n retval = True\n return retval\n else:\n _perms = _PRIVILEGE_TYPE_MAP[object_type]\n if grant_option:\n perms = dict((_PRIVILEGES_MAP[perm], True) for perm in _perms)\n retval = perms == _privileges[name]\n else:\n perms = [_PRIVILEGES_MAP[perm] for perm in _perms]\n if 'ALL' in _privs:\n retval = sorted(perms) == sorted(_privileges[name].keys())\n else:\n retval = set(_privs).issubset(\n set(_privileges[name].keys()))\n return retval\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngrants default privileges to an object.", "response": "def default_privileges_grant(name,\n object_name,\n object_type,\n defprivileges=None,\n grant_option=None,\n prepend='public',\n maintenance_db=None,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n .. versionadded:: 2019.0.0\n\n Grant default privileges on a postgres object\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.default_privileges_grant user_name table_name table \\\\\n SELECT,UPDATE maintenance_db=db_name\n\n name\n Name of the role to which default privileges should be granted\n\n object_name\n Name of the object on which the grant is to be performed\n\n object_type\n The object type, which can be one of the following:\n\n - table\n - sequence\n - schema\n - group\n - function\n\n privileges\n Comma separated list of privileges to grant, from the list below:\n\n - INSERT\n - CREATE\n - TRUNCATE\n - TRIGGER\n - SELECT\n - USAGE\n - UPDATE\n - EXECUTE\n - REFERENCES\n - DELETE\n - ALL\n\n grant_option\n If grant_option is set to True, the recipient of the default privilege can\n in turn grant it to others\n\n prepend\n Table and Sequence object types live under a schema so this should be\n provided if the object is not under the default `public` schema\n\n maintenance_db\n The database to connect to\n\n user\n database username if different from config or default\n\n password\n user password if any password for a specified user\n\n host\n Database host if different from config or default\n\n port\n Database port if different from config or default\n\n runas\n System user all operations should be performed on behalf of\n '''\n object_type, pdefrivileges, _defprivs = _mod_defpriv_opts(object_type, defprivileges)\n\n _validate_default_privileges(object_type, _defprivs, defprivileges)\n\n if has_default_privileges(name, object_name, object_type, defprivileges,\n prepend=prepend, maintenance_db=maintenance_db, user=user,\n host=host, port=port, password=password, runas=runas):\n log.info('The object: %s of type: %s already has default privileges: %s set',\n object_name, object_type, defprivileges)\n return False\n\n _grants = ','.join(_defprivs)\n\n if object_type in ['table', 'sequence']:\n on_part = '{0}.\"{1}\"'.format(prepend, object_name)\n elif object_type == 'function':\n on_part = '{0}'.format(object_name)\n else:\n on_part = '\"{0}\"'.format(object_name)\n\n if grant_option:\n if object_type == 'group':\n query = ' ALTER DEFAULT PRIVILEGES GRANT {0} TO \"{1}\" WITH ADMIN OPTION'.format(\n object_name, name)\n elif (object_type in ('table', 'sequence', 'function') and\n object_name.upper() == 'ALL'):\n query = 'ALTER DEFAULT PRIVILEGES IN SCHEMA {2} GRANT {0} ON {1}S TO ' \\\n '\"{3}\" WITH GRANT OPTION'.format(\n _grants, object_type.upper(), prepend, name)\n else:\n query = 'ALTER DEFAULT PRIVILEGES IN SCHEMA {2} GRANT {0} ON {1}S TO \"{3}\" WITH GRANT OPTION'.format(\n _grants, object_type.upper(), on_part, name)\n else:\n if object_type == 'group':\n query = 'ALTER DEFAULT PRIVILEGES GRANT {0} TO \"{1}\"'.format(object_name, name)\n elif (object_type in ('table', 'sequence') and\n object_name.upper() == 'ALL'):\n query = 'ALTER DEFAULT PRIVILEGES IN SCHEMA {2} GRANT {0} ON {1}S TO \"{3}\"'.format(\n _grants, object_type.upper(), prepend, name)\n else:\n query = ' ALTER DEFAULT PRIVILEGES IN SCHEMA {2} GRANT {0} ON {1}S TO \"{3}\"'.format(\n _grants, object_type.upper(), prepend, name)\n\n ret = _psql_prepare_and_run(['-c', query],\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n\n return ret['retcode'] == 0"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nrevoke default privileges on a postgres object.", "response": "def default_privileges_revoke(name,\n object_name,\n object_type,\n defprivileges=None,\n prepend='public',\n maintenance_db=None,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n .. versionadded:: 2019.0.0\n\n Revoke default privileges on a postgres object\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.default_privileges_revoke user_name table_name table \\\\\n SELECT,UPDATE maintenance_db=db_name\n\n name\n Name of the role whose default privileges should be revoked\n\n object_name\n Name of the object on which the revoke is to be performed\n\n object_type\n The object type, which can be one of the following:\n\n - table\n - sequence\n - schema\n - group\n - function\n\n privileges\n Comma separated list of privileges to revoke, from the list below:\n\n - INSERT\n - CREATE\n - TRUNCATE\n - TRIGGER\n - SELECT\n - USAGE\n - UPDATE\n - EXECUTE\n - REFERENCES\n - DELETE\n - ALL\n\n maintenance_db\n The database to connect to\n\n user\n database username if different from config or default\n\n password\n user password if any password for a specified user\n\n host\n Database host if different from config or default\n\n port\n Database port if different from config or default\n\n runas\n System user all operations should be performed on behalf of\n '''\n object_type, defprivileges, _defprivs = _mod_defpriv_opts(object_type, defprivileges)\n\n _validate_default_privileges(object_type, _defprivs, defprivileges)\n\n if not has_default_privileges(name, object_name, object_type, defprivileges,\n prepend=prepend, maintenance_db=maintenance_db, user=user,\n host=host, port=port, password=password, runas=runas):\n log.info('The object: %s of type: %s does not'\n ' have default privileges: %s set', object_name, object_type, defprivileges)\n return False\n\n _grants = ','.join(_defprivs)\n\n if object_type in ['table', 'sequence']:\n on_part = '{0}.{1}'.format(prepend, object_name)\n else:\n on_part = object_name\n\n if object_type == 'group':\n query = 'ALTER DEFAULT PRIVILEGES REVOKE {0} FROM {1}'.format(object_name, name)\n else:\n query = 'ALTER DEFAULT PRIVILEGES IN SCHEMA {2} REVOKE {0} ON {1}S FROM {3}'.format(\n _grants, object_type.upper(), prepend, name)\n\n ret = _psql_prepare_and_run(['-c', query],\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n\n return ret['retcode'] == 0"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nrevoke the specified privileges on a postgres object.", "response": "def privileges_revoke(name,\n object_name,\n object_type,\n privileges=None,\n prepend='public',\n maintenance_db=None,\n user=None,\n host=None,\n port=None,\n password=None,\n runas=None):\n '''\n .. versionadded:: 2016.3.0\n\n Revoke privileges on a postgres object\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.privileges_revoke user_name table_name table \\\\\n SELECT,UPDATE maintenance_db=db_name\n\n name\n Name of the role whose privileges should be revoked\n\n object_name\n Name of the object on which the revoke is to be performed\n\n object_type\n The object type, which can be one of the following:\n\n - table\n - sequence\n - schema\n - tablespace\n - language\n - database\n - group\n - function\n\n privileges\n Comma separated list of privileges to revoke, from the list below:\n\n - INSERT\n - CREATE\n - TRUNCATE\n - CONNECT\n - TRIGGER\n - SELECT\n - USAGE\n - TEMPORARY\n - UPDATE\n - EXECUTE\n - REFERENCES\n - DELETE\n - ALL\n\n maintenance_db\n The database to connect to\n\n user\n database username if different from config or default\n\n password\n user password if any password for a specified user\n\n host\n Database host if different from config or default\n\n port\n Database port if different from config or default\n\n runas\n System user all operations should be performed on behalf of\n '''\n object_type, privileges, _privs = _mod_priv_opts(object_type, privileges)\n\n _validate_privileges(object_type, _privs, privileges)\n\n if not has_privileges(name, object_name, object_type, privileges,\n prepend=prepend, maintenance_db=maintenance_db, user=user,\n host=host, port=port, password=password, runas=runas):\n log.info('The object: %s of type: %s does not'\n ' have privileges: %s set', object_name, object_type, privileges)\n return False\n\n _grants = ','.join(_privs)\n\n if object_type in ['table', 'sequence']:\n on_part = '{0}.{1}'.format(prepend, object_name)\n else:\n on_part = object_name\n\n if object_type == 'group':\n query = 'REVOKE {0} FROM {1}'.format(object_name, name)\n else:\n query = 'REVOKE {0} ON {1} {2} FROM {3}'.format(\n _grants, object_type.upper(), on_part, name)\n\n ret = _psql_prepare_and_run(['-c', query],\n user=user,\n host=host,\n port=port,\n maintenance_db=maintenance_db,\n password=password,\n runas=runas)\n\n return ret['retcode'] == 0"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ninitializes a postgres data directory.", "response": "def datadir_init(name,\n auth='password',\n user=None,\n password=None,\n encoding='UTF8',\n locale=None,\n waldir=None,\n checksums=False,\n runas=None):\n '''\n .. versionadded:: 2016.3.0\n\n Initializes a postgres data directory\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.datadir_init '/var/lib/pgsql/data'\n\n name\n The name of the directory to initialize\n\n auth\n The default authentication method for local connections\n\n password\n The password to set for the postgres user\n\n user\n The database superuser name\n\n encoding\n The default encoding for new databases\n\n locale\n The default locale for new databases\n\n waldir\n The transaction log (WAL) directory (default is to keep WAL\n inside the data directory)\n\n .. versionadded:: 2019.2.0\n\n checksums\n If True, the cluster will be created with data page checksums.\n\n .. note:: Data page checksums are supported since PostgreSQL 9.3.\n\n .. versionadded:: 2019.2.0\n\n runas\n The system user the operation should be performed on behalf of\n\n '''\n if datadir_exists(name):\n log.info('%s already exists', name)\n return False\n\n ret = _run_initdb(\n name,\n auth=auth,\n user=user,\n password=password,\n encoding=encoding,\n locale=locale,\n runas=runas)\n return ret['retcode'] == 0"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking if postgres data directory exists", "response": "def datadir_exists(name):\n '''\n .. versionadded:: 2016.3.0\n\n Checks if postgres data directory has been initialized\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' postgres.datadir_exists '/var/lib/pgsql/data'\n\n name\n Name of the directory to check\n '''\n _version_file = os.path.join(name, 'PG_VERSION')\n _config_file = os.path.join(name, 'postgresql.conf')\n\n return os.path.isfile(_version_file) and os.path.isfile(_config_file)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef exec_action(module, action, module_parameter=None, action_parameter=None, state_only=False):\n '''\n Execute an arbitrary action on a module.\n\n module\n name of the module to be executed\n\n action\n name of the module's action to be run\n\n module_parameter\n additional params passed to the defined module\n\n action_parameter\n additional params passed to the defined action\n\n state_only\n don't return any output but only the success/failure of the operation\n\n CLI Example (updating the ``php`` implementation used for ``apache2``):\n\n .. code-block:: bash\n\n salt '*' eselect.exec_action php update action_parameter='apache2'\n '''\n out = __salt__['cmd.run'](\n 'eselect --brief --colour=no {0} {1} {2} {3}'.format(\n module, module_parameter or '', action, action_parameter or ''),\n python_shell=False\n )\n out = out.strip().split('\\n')\n\n if out[0].startswith('!!! Error'):\n return False\n\n if state_only:\n return True\n\n if not out:\n return False\n\n if len(out) == 1 and not out[0].strip():\n return False\n\n return out", "response": "Execute an arbitrary action on a module"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlisting available ``eselect`` modules. CLI Example: .. code-block:: bash salt '*' eselect.get_modules", "response": "def get_modules():\n '''\n List available ``eselect`` modules.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' eselect.get_modules\n '''\n modules = []\n module_list = exec_action('modules', 'list', action_parameter='--only-names')\n if not module_list:\n return None\n\n for module in module_list:\n if module not in ['help', 'usage', 'version']:\n modules.append(module)\n return modules"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_target_list(module, action_parameter=None):\n '''\n List available targets for the given module.\n\n module\n name of the module to be queried for its targets\n\n action_parameter\n additional params passed to the defined action\n\n .. versionadded:: 2016.11.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' eselect.get_target_list kernel\n '''\n exec_output = exec_action(module, 'list', action_parameter=action_parameter)\n if not exec_output:\n return None\n\n target_list = []\n if isinstance(exec_output, list):\n for item in exec_output:\n target_list.append(item.split(None, 1)[0])\n return target_list\n\n return None", "response": "Returns a list of available targets for the given module"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_current_target(module, module_parameter=None, action_parameter=None):\n '''\n Get the currently selected target for the given module.\n\n module\n name of the module to be queried for its current target\n\n module_parameter\n additional params passed to the defined module\n\n action_parameter\n additional params passed to the 'show' action\n\n CLI Example (current target of system-wide ``java-vm``):\n\n .. code-block:: bash\n\n salt '*' eselect.get_current_target java-vm action_parameter='system'\n\n CLI Example (current target of ``kernel`` symlink):\n\n .. code-block:: bash\n\n salt '*' eselect.get_current_target kernel\n '''\n result = exec_action(module, 'show', module_parameter=module_parameter, action_parameter=action_parameter)[0]\n if not result:\n return None\n\n if result == '(unset)':\n return None\n\n return result", "response": "Get the currently selected target for the given module."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nset the target for the given module.", "response": "def set_target(module, target, module_parameter=None, action_parameter=None):\n '''\n Set the target for the given module.\n Target can be specified by index or name.\n\n module\n name of the module for which a target should be set\n\n target\n name of the target to be set for this module\n\n module_parameter\n additional params passed to the defined module\n\n action_parameter\n additional params passed to the defined action\n\n CLI Example (setting target of system-wide ``java-vm``):\n\n .. code-block:: bash\n\n salt '*' eselect.set_target java-vm icedtea-bin-7 action_parameter='system'\n\n CLI Example (setting target of ``kernel`` symlink):\n\n .. code-block:: bash\n\n salt '*' eselect.set_target kernel linux-3.17.5-gentoo\n '''\n if action_parameter:\n action_parameter = '{0} {1}'.format(action_parameter, target)\n else:\n action_parameter = target\n\n # get list of available modules\n if module not in get_modules():\n log.error('Module %s not available', module)\n return False\n\n exec_result = exec_action(module, 'set', module_parameter=module_parameter, action_parameter=action_parameter, state_only=True)\n if exec_result:\n return exec_result\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef calc(name, num, oper, minimum=0, maximum=0, ref=None):\n '''\n Perform a calculation on the ``num`` most recent values. Requires a list.\n Valid values for ``oper`` are:\n\n - add: Add last ``num`` values together\n - mul: Multiple last ``num`` values together\n - mean: Calculate mean of last ``num`` values\n - median: Calculate median of last ``num`` values\n - median_low: Calculate low median of last ``num`` values\n - median_high: Calculate high median of last ``num`` values\n - median_grouped: Calculate grouped median of last ``num`` values\n - mode: Calculate mode of last ``num`` values\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n calc.calc:\n - name: myregentry\n - num: 5\n - oper: mean\n '''\n ret = {'name': name,\n 'changes': {},\n 'comment': '',\n 'result': True}\n if name not in __reg__:\n ret['comment'] = '{0} not found in register'.format(name)\n ret['result'] = False\n\n def opadd(vals):\n sum = 0\n for val in vals:\n sum = sum + val\n return sum\n\n def opmul(vals):\n prod = 0\n for val in vals:\n prod = prod * val\n return prod\n\n ops = {\n 'add': opadd,\n 'mul': opmul,\n 'mean': statistics.mean,\n 'median': statistics.median,\n 'median_low': statistics.median_low,\n 'median_high': statistics.median_high,\n 'median_grouped': statistics.median_grouped,\n 'mode': statistics.mode,\n }\n\n count = 0\n vals = []\n __reg__[name]['val'].reverse()\n for regitem in __reg__[name]['val']:\n count += 1\n if count > num:\n break\n if ref is None:\n vals.append(regitem)\n else:\n vals.append(regitem[ref])\n\n answer = ops[oper](vals)\n\n if minimum > 0 and answer < minimum:\n ret['result'] = False\n\n if 0 < maximum < answer:\n ret['result'] = False\n\n ret['changes'] = {\n 'Number of values': len(vals),\n 'Operator': oper,\n 'Answer': answer,\n }\n return ret", "response": "Calculates the most recent values of the specified register entry."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef add(name, num, minimum=0, maximum=0, ref=None):\n '''\n Adds together the ``num`` most recent values. Requires a list.\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n calc.add:\n - name: myregentry\n - num: 5\n '''\n return calc(\n name=name,\n num=num,\n oper='add',\n minimum=minimum,\n maximum=maximum,\n ref=ref\n )", "response": "Adds together the num most recent values. Requires a list."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncalculates the mean of the num most recent values. Requires a list.", "response": "def mean(name, num, minimum=0, maximum=0, ref=None):\n '''\n Calculates the mean of the ``num`` most recent values. Requires a list.\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n calc.mean:\n - name: myregentry\n - num: 5\n '''\n return calc(\n name=name,\n num=num,\n oper='mean',\n minimum=minimum,\n maximum=maximum,\n ref=ref\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncalculate the median of the num most recent values. Requires a list.", "response": "def median(name, num, minimum=0, maximum=0, ref=None):\n '''\n Calculates the mean of the ``num`` most recent values. Requires a list.\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n calc.median:\n - name: myregentry\n - num: 5\n '''\n return calc(\n name=name,\n num=num,\n oper='median',\n minimum=minimum,\n maximum=maximum,\n ref=ref\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef median_low(name, num, minimum=0, maximum=0, ref=None):\n '''\n Calculates the low mean of the ``num`` most recent values. Requires a list.\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n calc.median_low:\n - name: myregentry\n - num: 5\n '''\n return calc(\n name=name,\n num=num,\n oper='median_low',\n minimum=minimum,\n maximum=maximum,\n ref=ref\n )", "response": "Calculates the low mean of the num most recent values. Requires a list. Requires a list."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncalculate the high mean of the num most recent values. Requires a list.", "response": "def median_high(name, num, minimum=0, maximum=0, ref=None):\n '''\n Calculates the high mean of the ``num`` most recent values. Requires a list.\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n calc.median_high:\n - name: myregentry\n - num: 5\n '''\n return calc(\n name=name,\n num=num,\n oper='median_high',\n minimum=minimum,\n maximum=maximum,\n ref=ref\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef mode(name, num, minimum=0, maximum=0, ref=None):\n '''\n Calculates the mode of the ``num`` most recent values. Requires a list.\n\n USAGE:\n\n .. code-block:: yaml\n\n foo:\n calc.mode:\n - name: myregentry\n - num: 5\n '''\n return calc(\n name=name,\n num=num,\n oper='mode',\n minimum=minimum,\n maximum=maximum,\n ref=ref\n )", "response": "Calculates the mode of the num most recent values. Requires a list."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_libvirt_enum_string(prefix, value):\n '''\n Convert the libvirt enum integer value into a human readable string.\n\n :param prefix: start of the libvirt attribute to look for.\n :param value: integer to convert to string\n '''\n attributes = [attr[len(prefix):] for attr in libvirt.__dict__ if attr.startswith(prefix)]\n\n # Filter out the values starting with a common base as they match another enum\n prefixes = [_compute_subprefix(p) for p in attributes]\n counts = {p: prefixes.count(p) for p in prefixes}\n sub_prefixes = [p for p, count in counts.items() if count > 1 or (p.endswith('_') and p[:-1] in prefixes)]\n filtered = [attr for attr in attributes if _compute_subprefix(attr) not in sub_prefixes]\n\n for candidate in filtered:\n if value == getattr(libvirt, ''.join((prefix, candidate))):\n name = candidate.lower().replace('_', ' ')\n return name\n return 'unknown'", "response": "Convert the libvirt enum integer value into a human readable string."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_domain_event_detail(event, detail):\n '''\n Convert event and detail numeric values into a tuple of human readable strings\n '''\n event_name = _get_libvirt_enum_string('VIR_DOMAIN_EVENT_', event)\n if event_name == 'unknown':\n return event_name, 'unknown'\n\n prefix = 'VIR_DOMAIN_EVENT_{0}_'.format(event_name.upper())\n detail_name = _get_libvirt_enum_string(prefix, detail)\n\n return event_name, detail_name", "response": "Convert event and detail numeric values into a tuple of human readable strings\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _salt_send_event(opaque, conn, data):\n '''\n Convenience function adding common data to the event and sending it\n on the salt event bus.\n\n :param opaque: the opaque data that is passed to the callback.\n This is a dict with 'prefix', 'object' and 'event' keys.\n :param conn: libvirt connection\n :param data: additional event data dict to send\n '''\n tag_prefix = opaque['prefix']\n object_type = opaque['object']\n event_type = opaque['event']\n\n # Prepare the connection URI to fit in the tag\n # qemu+ssh://user@host:1234/system -> qemu+ssh/user@host:1234/system\n uri = urlparse(conn.getURI())\n uri_tag = [uri.scheme]\n if uri.netloc:\n uri_tag.append(uri.netloc)\n path = uri.path.strip('/')\n if path:\n uri_tag.append(path)\n uri_str = \"/\".join(uri_tag)\n\n # Append some common data\n all_data = {\n 'uri': conn.getURI()\n }\n all_data.update(data)\n\n tag = '/'.join((tag_prefix, uri_str, object_type, event_type))\n\n # Actually send the event in salt\n if __opts__.get('__role') == 'master':\n salt.utils.event.get_master_event(\n __opts__,\n __opts__['sock_dir']).fire_event(all_data, tag)\n else:\n __salt__['event.send'](tag, all_data)", "response": "Helper function to send an event to the salt event bus"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _domain_event_lifecycle_cb(conn, domain, event, detail, opaque):\n '''\n Domain lifecycle events handler\n '''\n event_str, detail_str = _get_domain_event_detail(event, detail)\n\n _salt_send_domain_event(opaque, conn, domain, opaque['event'], {\n 'event': event_str,\n 'detail': detail_str\n })", "response": "Domain lifecycle events handler"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _domain_event_watchdog_cb(conn, domain, action, opaque):\n '''\n Domain watchdog events handler\n '''\n _salt_send_domain_event(opaque, conn, domain, opaque['event'], {\n 'action': _get_libvirt_enum_string('VIR_DOMAIN_EVENT_WATCHDOG_', action)\n })", "response": "Domain watchdog events handler"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _domain_event_disk_change_cb(conn, domain, old_src, new_src, dev, reason, opaque):\n '''\n Domain disk change events handler\n '''\n _salt_send_domain_event(opaque, conn, domain, opaque['event'], {\n 'oldSrcPath': old_src,\n 'newSrcPath': new_src,\n 'dev': dev,\n 'reason': _get_libvirt_enum_string('VIR_DOMAIN_EVENT_DISK_', reason)\n })", "response": "Domain disk change events handler"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _domain_event_tray_change_cb(conn, domain, dev, reason, opaque):\n '''\n Domain tray change events handler\n '''\n _salt_send_domain_event(opaque, conn, domain, opaque['event'], {\n 'dev': dev,\n 'reason': _get_libvirt_enum_string('VIR_DOMAIN_EVENT_TRAY_CHANGE_', reason)\n })", "response": "Domain tray change events handler"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _domain_event_pmwakeup_cb(conn, domain, reason, opaque):\n '''\n Domain wakeup events handler\n '''\n _salt_send_domain_event(opaque, conn, domain, opaque['event'], {\n 'reason': 'unknown' # currently unused\n })", "response": "Domain wakeup events handler\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _domain_event_pmsuspend_cb(conn, domain, reason, opaque):\n '''\n Domain suspend events handler\n '''\n _salt_send_domain_event(opaque, conn, domain, opaque['event'], {\n 'reason': 'unknown' # currently unused\n })", "response": "Domain suspend events handler\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _domain_event_balloon_change_cb(conn, domain, actual, opaque):\n '''\n Domain balloon change events handler\n '''\n _salt_send_domain_event(opaque, conn, domain, opaque['event'], {\n 'actual': actual\n })", "response": "Domain balloon change events handler"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _domain_event_pmsuspend_disk_cb(conn, domain, reason, opaque):\n '''\n Domain disk suspend events handler\n '''\n _salt_send_domain_event(opaque, conn, domain, opaque['event'], {\n 'reason': 'unknown' # currently unused\n })", "response": "Domain disk suspend disk events handler"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _domain_event_job_completed_cb(conn, domain, params, opaque):\n '''\n Domain job completion events handler\n '''\n _salt_send_domain_event(opaque, conn, domain, opaque['event'], {\n 'params': params\n })", "response": "Domain job completion events handler"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _domain_event_metadata_change_cb(conn, domain, mtype, nsuri, opaque):\n '''\n Domain metadata change events handler\n '''\n _salt_send_domain_event(opaque, conn, domain, opaque['event'], {\n 'type': _get_libvirt_enum_string('VIR_DOMAIN_METADATA_', mtype),\n 'nsuri': nsuri\n })", "response": "Domain metadata change events handler"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _network_event_lifecycle_cb(conn, net, event, detail, opaque):\n '''\n Network lifecycle events handler\n '''\n\n _salt_send_event(opaque, conn, {\n 'network': {\n 'name': net.name(),\n 'uuid': net.UUIDString()\n },\n 'event': _get_libvirt_enum_string('VIR_NETWORK_EVENT_', event),\n 'detail': 'unknown' # currently unused\n })", "response": "Network lifecycle events handler\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _pool_event_refresh_cb(conn, pool, opaque):\n '''\n Storage pool refresh events handler\n '''\n _salt_send_event(opaque, conn, {\n 'pool': {\n 'name': pool.name(),\n 'uuid': pool.UUIDString()\n },\n 'event': opaque['event']\n })", "response": "Storage pool refresh events handler\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _nodedev_event_update_cb(conn, dev, opaque):\n '''\n Node device update events handler\n '''\n _salt_send_event(opaque, conn, {\n 'nodedev': {\n 'name': dev.name()\n },\n 'event': opaque['event']\n })", "response": "Node device update events handler\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _secret_event_value_changed_cb(conn, secret, opaque):\n '''\n Secret value change events handler\n '''\n _salt_send_event(opaque, conn, {\n 'secret': {\n 'uuid': secret.UUIDString()\n },\n 'event': opaque['event']\n })", "response": "Handle the secret value change events handler\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _append_callback_id(ids, obj, callback_id):\n '''\n Helper function adding a callback ID to the IDs dict.\n The callback ids dict maps an object to event callback ids.\n\n :param ids: dict of callback IDs to update\n :param obj: one of the keys of REGISTER_FUNCTIONS\n :param callback_id: the result of _register_callback\n '''\n if obj not in ids:\n ids[obj] = []\n ids[obj].append(callback_id)", "response": "Helper function adding a callback ID to the IDs dict."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef start(uri=None,\n tag_prefix='salt/engines/libvirt_events',\n filters=None):\n '''\n Listen to libvirt events and forward them to salt.\n\n :param uri: libvirt URI to listen on.\n Defaults to None to pick the first available local hypervisor\n :param tag_prefix: the begining of the salt event tag to use.\n Defaults to 'salt/engines/libvirt_events'\n :param filters: the list of event of listen on. Defaults to 'all'\n '''\n if filters is None:\n filters = ['all']\n try:\n libvirt.virEventRegisterDefaultImpl()\n\n cnx = libvirt.openReadOnly(uri)\n log.debug('Opened libvirt uri: %s', cnx.getURI())\n\n callback_ids = {}\n all_filters = \"all\" in filters\n\n for obj, event_defs in CALLBACK_DEFS.items():\n for event, real_id in event_defs:\n event_filter = \"/\".join((obj, event))\n if event_filter not in filters and obj not in filters and not all_filters:\n continue\n registered_id = _register_callback(cnx, tag_prefix,\n obj, event, real_id)\n if registered_id:\n _append_callback_id(callback_ids, obj, registered_id)\n\n exit_loop = False\n while not exit_loop:\n exit_loop = libvirt.virEventRunDefaultImpl() < 0\n log.debug('=== in the loop exit_loop %s ===', exit_loop)\n\n except Exception as err: # pylint: disable=broad-except\n log.exception(err)\n finally:\n _callbacks_cleanup(cnx, callback_ids)\n _cleanup(cnx)", "response": "Start a new local hypervisor."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _number(text):\n '''\n Convert a string to a number.\n Returns an integer if the string represents an integer, a floating\n point number if the string is a real number, or the string unchanged\n otherwise.\n '''\n if text.isdigit():\n return int(text)\n try:\n return float(text)\n except ValueError:\n return text", "response": "Convert a string to a number."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_boot_time_aix():\n '''\n Return the number of seconds since boot time on AIX\n\n t=$(LC_ALL=POSIX ps -o etime= -p 1)\n d=0 h=0\n case $t in *-*) d=${t%%-*}; t=${t#*-};; esac\n case $t in *:*:*) h=${t%%:*}; t=${t#*:};; esac\n s=$((d*86400 + h*3600 + ${t%%:*}*60 + ${t#*:}))\n\n t is 7-20:46:46\n '''\n boot_secs = 0\n res = __salt__['cmd.run_all']('ps -o etime= -p 1')\n if res['retcode'] > 0:\n raise CommandExecutionError('Unable to find boot_time for pid 1.')\n bt_time = res['stdout']\n days = bt_time.split('-')\n hms = days[1].split(':')\n boot_secs = _number(days[0]) * 86400 + _number(hms[0]) * 3600 + _number(hms[1]) * 60 + _number(hms[2])\n return boot_secs", "response": "Return the number of seconds since boot time on AIX"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _aix_loadavg():\n '''\n Return the load average on AIX\n '''\n # 03:42PM up 9 days, 20:41, 2 users, load average: 0.28, 0.47, 0.69\n uptime = __salt__['cmd.run']('uptime')\n ldavg = uptime.split('load average')\n load_avg = ldavg[1].split()\n return {'1-min': load_avg[1].strip(','),\n '5-min': load_avg[2].strip(','),\n '15-min': load_avg[3]}", "response": "Return the load average on AIX\n "} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the process data for AIX CTYPE", "response": "def procs():\n '''\n Return the process data\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.procs\n '''\n # Get the user, pid and cmd\n ret = {}\n uind = 0\n pind = 0\n cind = 0\n plines = __salt__['cmd.run'](__grains__['ps'], python_shell=True).splitlines()\n guide = plines.pop(0).split()\n if 'USER' in guide:\n uind = guide.index('USER')\n elif 'UID' in guide:\n uind = guide.index('UID')\n if 'PID' in guide:\n pind = guide.index('PID')\n if 'COMMAND' in guide:\n cind = guide.index('COMMAND')\n elif 'CMD' in guide:\n cind = guide.index('CMD')\n for line in plines:\n if not line:\n continue\n comps = line.split()\n ret[comps[pind]] = {'user': comps[uind],\n 'cmd': ' '.join(comps[cind:])}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning a custom composite of status data and info for this minion", "response": "def custom():\n '''\n Return a custom composite of status data and info for this minion,\n based on the minion config file. An example config like might be::\n\n status.cpustats.custom: [ 'cpu', 'ctxt', 'btime', 'processes' ]\n\n Where status refers to status.py, cpustats is the function\n where we get our data, and custom is this function It is followed\n by a list of keys that we want returned.\n\n This function is meant to replace all_status(), which returns\n anything and everything, which we probably don't want.\n\n By default, nothing is returned. Warning: Depending on what you\n include, there can be a LOT here!\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.custom\n '''\n ret = {}\n conf = __salt__['config.dot_vals']('status')\n for key, val in six.iteritems(conf):\n func = '{0}()'.format(key.split('.')[1])\n vals = eval(func) # pylint: disable=W0123\n\n for item in val:\n ret[item] = vals[item]\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef uptime():\n '''\n Return the uptime for this system.\n\n .. versionchanged:: 2015.8.9\n The uptime function was changed to return a dictionary of easy-to-read\n key/value pairs containing uptime information, instead of the output\n from a ``cmd.run`` call.\n\n .. versionchanged:: 2016.11.0\n Support for OpenBSD, FreeBSD, NetBSD, MacOS, and Solaris\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.uptime\n '''\n curr_seconds = time.time()\n\n # Get uptime in seconds\n if salt.utils.platform.is_linux():\n ut_path = \"/proc/uptime\"\n if not os.path.exists(ut_path):\n raise CommandExecutionError(\"File {ut_path} was not found.\".format(ut_path=ut_path))\n with salt.utils.files.fopen(ut_path) as rfh:\n seconds = int(float(rfh.read().split()[0]))\n elif salt.utils.platform.is_sunos():\n # note: some flavors/versions report the host uptime inside a zone\n # https://support.oracle.com/epmos/faces/BugDisplay?id=15611584\n res = __salt__['cmd.run_all']('kstat -p unix:0:system_misc:boot_time')\n if res['retcode'] > 0:\n raise CommandExecutionError('The boot_time kstat was not found.')\n seconds = int(curr_seconds - int(res['stdout'].split()[-1]))\n elif salt.utils.platform.is_openbsd() or salt.utils.platform.is_netbsd():\n bt_data = __salt__['sysctl.get']('kern.boottime')\n if not bt_data:\n raise CommandExecutionError('Cannot find kern.boottime system parameter')\n seconds = int(curr_seconds - int(bt_data))\n elif salt.utils.platform.is_freebsd() or salt.utils.platform.is_darwin():\n # format: { sec = 1477761334, usec = 664698 } Sat Oct 29 17:15:34 2016\n bt_data = __salt__['sysctl.get']('kern.boottime')\n if not bt_data:\n raise CommandExecutionError('Cannot find kern.boottime system parameter')\n data = bt_data.split(\"{\")[-1].split(\"}\")[0].strip().replace(' ', '')\n uptime = dict([(k, int(v,)) for k, v in [p.strip().split('=') for p in data.split(',')]])\n seconds = int(curr_seconds - uptime['sec'])\n elif salt.utils.platform.is_aix():\n seconds = _get_boot_time_aix()\n else:\n return __salt__['cmd.run']('uptime')\n\n # Setup datetime and timedelta objects\n boot_time = datetime.datetime.utcfromtimestamp(curr_seconds - seconds)\n curr_time = datetime.datetime.utcfromtimestamp(curr_seconds)\n up_time = curr_time - boot_time\n\n # Construct return information\n ut_ret = {\n 'seconds': seconds,\n 'since_iso': boot_time.isoformat(),\n 'since_t': int(curr_seconds - seconds),\n 'days': up_time.days,\n 'time': '{0}:{1}'.format(up_time.seconds // 3600, up_time.seconds % 3600 // 60),\n }\n\n if salt.utils.path.which('who'):\n who_cmd = 'who' if salt.utils.platform.is_openbsd() else 'who -s' # OpenBSD does not support -s\n ut_ret['users'] = len(__salt__['cmd.run'](who_cmd).split(os.linesep))\n\n return ut_ret", "response": "Return the uptime for this system."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef loadavg():\n '''\n Return the load averages for this minion\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.loadavg\n\n :raises CommandExecutionError: If the system cannot report loadaverages to Python\n '''\n if __grains__['kernel'] == 'AIX':\n return _aix_loadavg()\n\n try:\n load_avg = os.getloadavg()\n except AttributeError:\n # Some UNIX-based operating systems do not have os.getloadavg()\n raise salt.exceptions.CommandExecutionError('status.loadavag is not available on your platform')\n return {'1-min': load_avg[0],\n '5-min': load_avg[1],\n '15-min': load_avg[2]}", "response": "Return the load averages for this minion"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the CPU stats for this minion", "response": "def cpustats():\n '''\n Return the CPU stats for this minion\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n .. versionchanged:: 2018.3.0\n Added support for OpenBSD\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.cpustats\n '''\n def linux_cpustats():\n '''\n linux specific implementation of cpustats\n '''\n ret = {}\n try:\n with salt.utils.files.fopen('/proc/stat', 'r') as fp_:\n stats = salt.utils.stringutils.to_unicode(fp_.read())\n except IOError:\n pass\n else:\n for line in stats.splitlines():\n if not line:\n continue\n comps = line.split()\n if comps[0] == 'cpu':\n ret[comps[0]] = {'idle': _number(comps[4]),\n 'iowait': _number(comps[5]),\n 'irq': _number(comps[6]),\n 'nice': _number(comps[2]),\n 'softirq': _number(comps[7]),\n 'steal': _number(comps[8]),\n 'system': _number(comps[3]),\n 'user': _number(comps[1])}\n elif comps[0] == 'intr':\n ret[comps[0]] = {'total': _number(comps[1]),\n 'irqs': [_number(x) for x in comps[2:]]}\n elif comps[0] == 'softirq':\n ret[comps[0]] = {'total': _number(comps[1]),\n 'softirqs': [_number(x) for x in comps[2:]]}\n else:\n ret[comps[0]] = _number(comps[1])\n return ret\n\n def freebsd_cpustats():\n '''\n freebsd specific implementation of cpustats\n '''\n vmstat = __salt__['cmd.run']('vmstat -P').splitlines()\n vm0 = vmstat[0].split()\n cpu0loc = vm0.index('cpu0')\n vm1 = vmstat[1].split()\n usloc = vm1.index('us')\n vm2 = vmstat[2].split()\n cpuctr = 0\n ret = {}\n for cpu in vm0[cpu0loc:]:\n ret[cpu] = {'us': _number(vm2[usloc + 3 * cpuctr]),\n 'sy': _number(vm2[usloc + 1 + 3 * cpuctr]),\n 'id': _number(vm2[usloc + 2 + 3 * cpuctr]), }\n cpuctr += 1\n return ret\n\n def sunos_cpustats():\n '''\n sunos specific implementation of cpustats\n '''\n mpstat = __salt__['cmd.run']('mpstat 1 2').splitlines()\n fields = mpstat[0].split()\n ret = {}\n for cpu in mpstat:\n if cpu.startswith('CPU'):\n continue\n cpu = cpu.split()\n ret[_number(cpu[0])] = {}\n for i in range(1, len(fields)-1):\n ret[_number(cpu[0])][fields[i]] = _number(cpu[i])\n return ret\n\n def aix_cpustats():\n '''\n AIX specific implementation of cpustats\n '''\n ret = {}\n ret['mpstat'] = []\n procn = None\n fields = []\n for line in __salt__['cmd.run']('mpstat -a').splitlines():\n if not line:\n continue\n procn = len(ret['mpstat'])\n if line.startswith('System'):\n comps = line.split(':')\n ret['mpstat'].append({})\n ret['mpstat'][procn]['system'] = {}\n cpu_comps = comps[1].split()\n for i in range(0, len(cpu_comps)):\n cpu_vals = cpu_comps[i].split('=')\n ret['mpstat'][procn]['system'][cpu_vals[0]] = cpu_vals[1]\n\n if line.startswith('cpu'):\n fields = line.split()\n continue\n\n if fields:\n cpustat = line.split()\n ret[_number(cpustat[0])] = {}\n for i in range(1, len(fields)-1):\n ret[_number(cpustat[0])][fields[i]] = _number(cpustat[i])\n\n return ret\n\n def openbsd_cpustats():\n '''\n openbsd specific implementation of cpustats\n '''\n systat = __salt__['cmd.run']('systat -s 2 -B cpu').splitlines()\n fields = systat[3].split()\n ret = {}\n for cpu in systat[4:]:\n cpu_line = cpu.split()\n cpu_idx = cpu_line[0]\n ret[cpu_idx] = {}\n\n for idx, field in enumerate(fields[1:]):\n ret[cpu_idx][field] = cpu_line[idx+1]\n\n return ret\n\n # dict that return a function that does the right thing per platform\n get_version = {\n 'Linux': linux_cpustats,\n 'FreeBSD': freebsd_cpustats,\n 'OpenBSD': openbsd_cpustats,\n 'SunOS': sunos_cpustats,\n 'AIX': aix_cpustats,\n }\n\n errmsg = 'This method is unsupported on the current operating system!'\n return get_version.get(__grains__['kernel'], lambda: errmsg)()"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the memory info for this minion", "response": "def meminfo():\n '''\n Return the memory info for this minion\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n .. versionchanged:: 2018.3.0\n Added support for OpenBSD\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.meminfo\n '''\n def linux_meminfo():\n '''\n linux specific implementation of meminfo\n '''\n ret = {}\n try:\n with salt.utils.files.fopen('/proc/meminfo', 'r') as fp_:\n stats = salt.utils.stringutils.to_unicode(fp_.read())\n except IOError:\n pass\n else:\n for line in stats.splitlines():\n if not line:\n continue\n comps = line.split()\n comps[0] = comps[0].replace(':', '')\n ret[comps[0]] = {\n 'value': comps[1],\n }\n if len(comps) > 2:\n ret[comps[0]]['unit'] = comps[2]\n return ret\n\n def freebsd_meminfo():\n '''\n freebsd specific implementation of meminfo\n '''\n sysctlvm = __salt__['cmd.run']('sysctl vm').splitlines()\n sysctlvm = [x for x in sysctlvm if x.startswith('vm')]\n sysctlvm = [x.split(':') for x in sysctlvm]\n sysctlvm = [[y.strip() for y in x] for x in sysctlvm]\n sysctlvm = [x for x in sysctlvm if x[1]] # If x[1] not empty\n\n ret = {}\n for line in sysctlvm:\n ret[line[0]] = line[1]\n # Special handling for vm.total as it's especially important\n sysctlvmtot = __salt__['cmd.run']('sysctl -n vm.vmtotal').splitlines()\n sysctlvmtot = [x for x in sysctlvmtot if x]\n ret['vm.vmtotal'] = sysctlvmtot\n return ret\n\n def aix_meminfo():\n '''\n AIX specific implementation of meminfo\n '''\n ret = {}\n ret['svmon'] = []\n ret['vmstat'] = []\n procn = None\n fields = []\n pagesize_flag = False\n for line in __salt__['cmd.run']('svmon -G').splitlines():\n # Note: svmon is per-system\n # size inuse free pin virtual mmode\n #memory 1048576 1039740 8836 285078 474993 Ded\n #pg space 917504 2574\n #\n # work pers clnt other\n #pin 248379 0 2107 34592\n #in use 474993 0 564747\n #\n #PageSize PoolSize inuse pgsp pin virtual\n #s 4 KB - 666956 2574 60726 102209\n #m 64 KB - 23299 0 14022 23299\n if not line:\n continue\n\n if re.match(r'\\s', line):\n # assume fields line\n fields = line.split()\n continue\n\n if line.startswith('memory') or line.startswith('pin'):\n procn = len(ret['svmon'])\n ret['svmon'].append({})\n comps = line.split()\n ret['svmon'][procn][comps[0]] = {}\n for i in range(0, len(fields)):\n if len(comps) > i + 1:\n ret['svmon'][procn][comps[0]][fields[i]] = comps[i+1]\n continue\n\n if line.startswith('pg space') or line.startswith('in use'):\n procn = len(ret['svmon'])\n ret['svmon'].append({})\n comps = line.split()\n pg_space = '{0} {1}'.format(comps[0], comps[1])\n ret['svmon'][procn][pg_space] = {}\n for i in range(0, len(fields)):\n if len(comps) > i + 2:\n ret['svmon'][procn][pg_space][fields[i]] = comps[i+2]\n continue\n\n if line.startswith('PageSize'):\n fields = line.split()\n pagesize_flag = False\n continue\n\n if pagesize_flag:\n procn = len(ret['svmon'])\n ret['svmon'].append({})\n comps = line.split()\n ret['svmon'][procn][comps[0]] = {}\n for i in range(0, len(fields)):\n if len(comps) > i:\n ret['svmon'][procn][comps[0]][fields[i]] = comps[i]\n continue\n\n for line in __salt__['cmd.run']('vmstat -v').splitlines():\n # Note: vmstat is per-system\n if not line:\n continue\n\n procn = len(ret['vmstat'])\n ret['vmstat'].append({})\n comps = line.lstrip().split(' ', 1)\n ret['vmstat'][procn][comps[1]] = comps[0]\n\n return ret\n\n def openbsd_meminfo():\n '''\n openbsd specific implementation of meminfo\n '''\n vmstat = __salt__['cmd.run']('vmstat').splitlines()\n # We're only interested in memory and page values which are printed\n # as subsequent fields.\n fields = ['active virtual pages', 'free list size', 'page faults',\n 'pages reclaimed', 'pages paged in', 'pages paged out',\n 'pages freed', 'pages scanned']\n data = vmstat[2].split()[2:10]\n ret = dict(zip(fields, data))\n return ret\n\n # dict that return a function that does the right thing per platform\n get_version = {\n 'Linux': linux_meminfo,\n 'FreeBSD': freebsd_meminfo,\n 'OpenBSD': openbsd_meminfo,\n 'AIX': aix_meminfo,\n }\n\n errmsg = 'This method is unsupported on the current operating system!'\n return get_version.get(__grains__['kernel'], lambda: errmsg)()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the CPU info for this minion", "response": "def cpuinfo():\n '''\n .. versionchanged:: 2016.3.2\n Return the CPU info for this minion\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n .. versionchanged:: 2018.3.0\n Added support for NetBSD and OpenBSD\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.cpuinfo\n '''\n def linux_cpuinfo():\n '''\n linux specific cpuinfo implementation\n '''\n ret = {}\n try:\n with salt.utils.files.fopen('/proc/cpuinfo', 'r') as fp_:\n stats = salt.utils.stringutils.to_unicode(fp_.read())\n except IOError:\n pass\n else:\n for line in stats.splitlines():\n if not line:\n continue\n comps = line.split(':')\n comps[0] = comps[0].strip()\n if comps[0] == 'flags':\n ret[comps[0]] = comps[1].split()\n else:\n ret[comps[0]] = comps[1].strip()\n return ret\n\n def bsd_cpuinfo():\n '''\n bsd specific cpuinfo implementation\n '''\n bsd_cmd = 'sysctl hw.model hw.ncpu'\n ret = {}\n if __grains__['kernel'].lower() in ['netbsd', 'openbsd']:\n sep = '='\n else:\n sep = ':'\n\n for line in __salt__['cmd.run'](bsd_cmd).splitlines():\n if not line:\n continue\n comps = line.split(sep)\n comps[0] = comps[0].strip()\n ret[comps[0]] = comps[1].strip()\n return ret\n\n def sunos_cpuinfo():\n '''\n sunos specific cpuinfo implementation\n '''\n ret = {}\n ret['isainfo'] = {}\n for line in __salt__['cmd.run']('isainfo -x').splitlines():\n # Note: isainfo is per-system and not per-cpu\n # Output Example:\n #amd64: rdrand f16c vmx avx xsave pclmulqdq aes sse4.2 sse4.1 ssse3 popcnt tscp cx16 sse3 sse2 sse fxsr mmx cmov amd_sysc cx8 tsc fpu\n #i386: rdrand f16c vmx avx xsave pclmulqdq aes sse4.2 sse4.1 ssse3 popcnt tscp ahf cx16 sse3 sse2 sse fxsr mmx cmov sep cx8 tsc fpu\n if not line:\n continue\n comps = line.split(':')\n comps[0] = comps[0].strip()\n ret['isainfo'][comps[0]] = sorted(comps[1].strip().split())\n ret['psrinfo'] = []\n procn = None\n for line in __salt__['cmd.run']('psrinfo -v -p').splitlines():\n # Output Example:\n #The physical processor has 6 cores and 12 virtual processors (0-5 12-17)\n # The core has 2 virtual processors (0 12)\n # The core has 2 virtual processors (1 13)\n # The core has 2 virtual processors (2 14)\n # The core has 2 virtual processors (3 15)\n # The core has 2 virtual processors (4 16)\n # The core has 2 virtual processors (5 17)\n # x86 (GenuineIntel 306E4 family 6 model 62 step 4 clock 2100 MHz)\n # Intel(r) Xeon(r) CPU E5-2620 v2 @ 2.10GHz\n #The physical processor has 6 cores and 12 virtual processors (6-11 18-23)\n # The core has 2 virtual processors (6 18)\n # The core has 2 virtual processors (7 19)\n # The core has 2 virtual processors (8 20)\n # The core has 2 virtual processors (9 21)\n # The core has 2 virtual processors (10 22)\n # The core has 2 virtual processors (11 23)\n # x86 (GenuineIntel 306E4 family 6 model 62 step 4 clock 2100 MHz)\n # Intel(r) Xeon(r) CPU E5-2620 v2 @ 2.10GHz\n #\n # Output Example 2:\n #The physical processor has 4 virtual processors (0-3)\n # x86 (GenuineIntel 406D8 family 6 model 77 step 8 clock 2400 MHz)\n # Intel(r) Atom(tm) CPU C2558 @ 2.40GHz\n if not line:\n continue\n if line.startswith('The physical processor'):\n procn = len(ret['psrinfo'])\n line = line.split()\n ret['psrinfo'].append({})\n if 'cores' in line:\n ret['psrinfo'][procn]['topology'] = {}\n ret['psrinfo'][procn]['topology']['cores'] = _number(line[4])\n ret['psrinfo'][procn]['topology']['threads'] = _number(line[7])\n elif 'virtual' in line:\n ret['psrinfo'][procn]['topology'] = {}\n ret['psrinfo'][procn]['topology']['threads'] = _number(line[4])\n elif line.startswith(' ' * 6): # 3x2 space indent\n ret['psrinfo'][procn]['name'] = line.strip()\n elif line.startswith(' ' * 4): # 2x2 space indent\n line = line.strip().split()\n ret['psrinfo'][procn]['vendor'] = line[1][1:]\n ret['psrinfo'][procn]['family'] = _number(line[4])\n ret['psrinfo'][procn]['model'] = _number(line[6])\n ret['psrinfo'][procn]['step'] = _number(line[8])\n ret['psrinfo'][procn]['clock'] = \"{0} {1}\".format(line[10], line[11][:-1])\n return ret\n\n def aix_cpuinfo():\n '''\n AIX specific cpuinfo implementation\n '''\n ret = {}\n ret['prtconf'] = []\n ret['lparstat'] = []\n procn = None\n for line in __salt__['cmd.run']('prtconf | grep -i \"Processor\"', python_shell=True).splitlines():\n # Note: prtconf is per-system and not per-cpu\n # Output Example:\n #prtconf | grep -i \"Processor\"\n #Processor Type: PowerPC_POWER7\n #Processor Implementation Mode: POWER 7\n #Processor Version: PV_7_Compat\n #Number Of Processors: 2\n #Processor Clock Speed: 3000 MHz\n # Model Implementation: Multiple Processor, PCI bus\n # + proc0 Processor\n # + proc4 Processor\n if not line:\n continue\n procn = len(ret['prtconf'])\n if line.startswith('Processor') or line.startswith('Number'):\n ret['prtconf'].append({})\n comps = line.split(':')\n comps[0] = comps[0].rstrip()\n ret['prtconf'][procn][comps[0]] = comps[1]\n else:\n continue\n\n for line in __salt__['cmd.run']('prtconf | grep \"CPU\"', python_shell=True).splitlines():\n # Note: prtconf is per-system and not per-cpu\n # Output Example:\n #CPU Type: 64-bit\n if not line:\n continue\n procn = len(ret['prtconf'])\n if line.startswith('CPU'):\n ret['prtconf'].append({})\n comps = line.split(':')\n comps[0] = comps[0].rstrip()\n ret['prtconf'][procn][comps[0]] = comps[1]\n else:\n continue\n\n for line in __salt__['cmd.run']('lparstat -i | grep CPU', python_shell=True).splitlines():\n # Note: lparstat is per-system and not per-cpu\n # Output Example:\n #Online Virtual CPUs : 2\n #Maximum Virtual CPUs : 2\n #Minimum Virtual CPUs : 1\n #Maximum Physical CPUs in system : 32\n #Active Physical CPUs in system : 32\n #Active CPUs in Pool : 32\n #Shared Physical CPUs in system : 32\n #Physical CPU Percentage : 25.00%\n #Desired Virtual CPUs : 2\n if not line:\n continue\n\n procn = len(ret['lparstat'])\n ret['lparstat'].append({})\n comps = line.split(':')\n comps[0] = comps[0].rstrip()\n ret['lparstat'][procn][comps[0]] = comps[1]\n\n return ret\n\n # dict that returns a function that does the right thing per platform\n get_version = {\n 'Linux': linux_cpuinfo,\n 'FreeBSD': bsd_cpuinfo,\n 'NetBSD': bsd_cpuinfo,\n 'OpenBSD': bsd_cpuinfo,\n 'SunOS': sunos_cpuinfo,\n 'AIX': aix_cpuinfo,\n }\n\n errmsg = 'This method is unsupported on the current operating system!'\n return get_version.get(__grains__['kernel'], lambda: errmsg)()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef diskstats():\n '''\n .. versionchanged:: 2016.3.2\n Return the disk stats for this minion\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.diskstats\n '''\n def linux_diskstats():\n '''\n linux specific implementation of diskstats\n '''\n ret = {}\n try:\n with salt.utils.files.fopen('/proc/diskstats', 'r') as fp_:\n stats = salt.utils.stringutils.to_unicode(fp_.read())\n except IOError:\n pass\n else:\n for line in stats.splitlines():\n if not line:\n continue\n comps = line.split()\n ret[comps[2]] = {\n 'major': _number(comps[0]),\n 'minor': _number(comps[1]),\n 'device': _number(comps[2]),\n 'reads_issued': _number(comps[3]),\n 'reads_merged': _number(comps[4]),\n 'sectors_read': _number(comps[5]),\n 'ms_spent_reading': _number(comps[6]),\n 'writes_completed': _number(comps[7]),\n 'writes_merged': _number(comps[8]),\n 'sectors_written': _number(comps[9]),\n 'ms_spent_writing': _number(comps[10]),\n 'io_in_progress': _number(comps[11]),\n 'ms_spent_in_io': _number(comps[12]),\n 'weighted_ms_spent_in_io': _number(comps[13])\n }\n return ret\n\n def generic_diskstats():\n '''\n generic implementation of diskstats\n note: freebsd and sunos\n '''\n ret = {}\n iostat = __salt__['cmd.run']('iostat -xzd').splitlines()\n header = iostat[1]\n for line in iostat[2:]:\n comps = line.split()\n ret[comps[0]] = {}\n for metric, value in zip(header.split()[1:], comps[1:]):\n ret[comps[0]][metric] = _number(value)\n return ret\n\n def aix_diskstats():\n '''\n AIX specific implementation of diskstats\n '''\n ret = {}\n procn = None\n fields = []\n disk_name = ''\n disk_mode = ''\n for line in __salt__['cmd.run']('iostat -dDV').splitlines():\n # Note: iostat -dDV is per-system\n #\n #System configuration: lcpu=8 drives=1 paths=2 vdisks=2\n #\n #hdisk0 xfer: %tm_act bps tps bread bwrtn\n # 0.0 0.8 0.0 0.0 0.8\n # read: rps avgserv minserv maxserv timeouts fails\n # 0.0 2.5 0.3 12.4 0 0\n # write: wps avgserv minserv maxserv timeouts fails\n # 0.0 0.3 0.2 0.7 0 0\n # queue: avgtime mintime maxtime avgwqsz avgsqsz sqfull\n # 0.3 0.0 5.3 0.0 0.0 0.0\n #--------------------------------------------------------------------------------\n if not line or line.startswith('System') or line.startswith('-----------'):\n continue\n\n if not re.match(r'\\s', line):\n #have new disk\n dsk_comps = line.split(':')\n dsk_firsts = dsk_comps[0].split()\n disk_name = dsk_firsts[0]\n disk_mode = dsk_firsts[1]\n fields = dsk_comps[1].split()\n ret[disk_name] = []\n\n procn = len(ret[disk_name])\n ret[disk_name].append({})\n ret[disk_name][procn][disk_mode] = {}\n continue\n\n if ':' in line:\n comps = line.split(':')\n fields = comps[1].split()\n disk_mode = comps[0].lstrip()\n procn = len(ret[disk_name])\n ret[disk_name].append({})\n ret[disk_name][procn][disk_mode] = {}\n else:\n comps = line.split()\n for i in range(0, len(fields)):\n if len(comps) > i:\n ret[disk_name][procn][disk_mode][fields[i]] = comps[i]\n\n return ret\n\n # dict that return a function that does the right thing per platform\n get_version = {\n 'Linux': linux_diskstats,\n 'FreeBSD': generic_diskstats,\n 'SunOS': generic_diskstats,\n 'AIX': aix_diskstats,\n }\n\n errmsg = 'This method is unsupported on the current operating system!'\n return get_version.get(__grains__['kernel'], lambda: errmsg)()", "response": "Return the disk stats for this minion"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the disk usage for this minion", "response": "def diskusage(*args):\n '''\n Return the disk usage for this minion\n\n Usage::\n\n salt '*' status.diskusage [paths and/or filesystem types]\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.diskusage # usage for all filesystems\n salt '*' status.diskusage / /tmp # usage for / and /tmp\n salt '*' status.diskusage ext? # usage for ext[234] filesystems\n salt '*' status.diskusage / ext? # usage for / and all ext filesystems\n '''\n selected = set()\n fstypes = set()\n if not args:\n # select all filesystems\n fstypes.add('*')\n else:\n for arg in args:\n if arg.startswith('/'):\n # select path\n selected.add(arg)\n else:\n # select fstype\n fstypes.add(arg)\n\n if fstypes:\n # determine which mount points host the specified fstypes\n regex = re.compile(\n '|'.join(\n fnmatch.translate(fstype).format('(%s)') for fstype in fstypes\n )\n )\n # ifile source of data varies with OS, otherwise all the same\n if __grains__['kernel'] == 'Linux':\n try:\n with salt.utils.files.fopen('/proc/mounts', 'r') as fp_:\n ifile = salt.utils.stringutils.to_unicode(fp_.read()).splitlines()\n except OSError:\n return {}\n elif __grains__['kernel'] in ('FreeBSD', 'SunOS'):\n ifile = __salt__['cmd.run']('mount -p').splitlines()\n else:\n raise CommandExecutionError('status.diskusage not yet supported on this platform')\n\n for line in ifile:\n comps = line.split()\n if __grains__['kernel'] == 'SunOS':\n if len(comps) >= 4:\n mntpt = comps[2]\n fstype = comps[3]\n if regex.match(fstype):\n selected.add(mntpt)\n else:\n if len(comps) >= 3:\n mntpt = comps[1]\n fstype = comps[2]\n if regex.match(fstype):\n selected.add(mntpt)\n\n # query the filesystems disk usage\n ret = {}\n for path in selected:\n fsstats = os.statvfs(path)\n blksz = fsstats.f_bsize\n available = fsstats.f_bavail * blksz\n total = fsstats.f_blocks * blksz\n ret[path] = {\"available\": available, \"total\": total}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef vmstats():\n '''\n .. versionchanged:: 2016.3.2\n Return the virtual memory stats for this minion\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.vmstats\n '''\n def linux_vmstats():\n '''\n linux specific implementation of vmstats\n '''\n ret = {}\n try:\n with salt.utils.files.fopen('/proc/vmstat', 'r') as fp_:\n stats = salt.utils.stringutils.to_unicode(fp_.read())\n except IOError:\n pass\n else:\n for line in stats.splitlines():\n if not line:\n continue\n comps = line.split()\n ret[comps[0]] = _number(comps[1])\n return ret\n\n def generic_vmstats():\n '''\n generic implementation of vmstats\n note: works on FreeBSD, SunOS and OpenBSD (possibly others)\n '''\n ret = {}\n for line in __salt__['cmd.run']('vmstat -s').splitlines():\n comps = line.split()\n if comps[0].isdigit():\n ret[' '.join(comps[1:])] = _number(comps[0].strip())\n return ret\n\n # dict that returns a function that does the right thing per platform\n get_version = {\n 'Linux': linux_vmstats,\n 'FreeBSD': generic_vmstats,\n 'OpenBSD': generic_vmstats,\n 'SunOS': generic_vmstats,\n 'AIX': generic_vmstats,\n }\n\n errmsg = 'This method is unsupported on the current operating system!'\n return get_version.get(__grains__['kernel'], lambda: errmsg)()", "response": "Return the virtual memory stats for this minion"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef nproc():\n '''\n Return the number of processing units available on this system\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n .. versionchanged:: 2018.3.0\n Added support for Darwin, FreeBSD and OpenBSD\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.nproc\n '''\n def linux_nproc():\n '''\n linux specific implementation of nproc\n '''\n try:\n return _number(__salt__['cmd.run']('nproc').strip())\n except ValueError:\n return 0\n\n def generic_nproc():\n '''\n generic implementation of nproc\n '''\n ncpu_data = __salt__['sysctl.get']('hw.ncpu')\n if not ncpu_data:\n # We need at least one CPU to run\n return 1\n else:\n return _number(ncpu_data)\n\n # dict that returns a function that does the right thing per platform\n get_version = {\n 'Linux': linux_nproc,\n 'Darwin': generic_nproc,\n 'FreeBSD': generic_nproc,\n 'OpenBSD': generic_nproc,\n 'AIX': _aix_nproc,\n }\n\n errmsg = 'This method is unsupported on the current operating system!'\n return get_version.get(__grains__['kernel'], lambda: errmsg)()", "response": "Return the number of processing units available on this system"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning the network stats for this minion", "response": "def netstats():\n '''\n Return the network stats for this minion\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n .. versionchanged:: 2018.3.0\n Added support for OpenBSD\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.netstats\n '''\n def linux_netstats():\n '''\n linux specific netstats implementation\n '''\n ret = {}\n try:\n with salt.utils.files.fopen('/proc/net/netstat', 'r') as fp_:\n stats = salt.utils.stringutils.to_unicode(fp_.read())\n except IOError:\n pass\n else:\n headers = ['']\n for line in stats.splitlines():\n if not line:\n continue\n comps = line.split()\n if comps[0] == headers[0]:\n index = len(headers) - 1\n row = {}\n for field in range(index):\n if field < 1:\n continue\n else:\n row[headers[field]] = _number(comps[field])\n rowname = headers[0].replace(':', '')\n ret[rowname] = row\n else:\n headers = comps\n return ret\n\n def freebsd_netstats():\n return bsd_netstats()\n\n def bsd_netstats():\n '''\n bsd specific netstats implementation\n '''\n ret = {}\n for line in __salt__['cmd.run']('netstat -s').splitlines():\n if line.startswith('\\t\\t'):\n continue # Skip, too detailed\n if not line.startswith('\\t'):\n key = line.split()[0].replace(':', '')\n ret[key] = {}\n else:\n comps = line.split()\n if comps[0].isdigit():\n ret[key][' '.join(comps[1:])] = comps[0]\n return ret\n\n def sunos_netstats():\n '''\n sunos specific netstats implementation\n '''\n ret = {}\n for line in __salt__['cmd.run']('netstat -s').splitlines():\n line = line.replace('=', ' = ').split()\n if len(line) > 6:\n line.pop(0)\n if '=' in line:\n if len(line) >= 3:\n if line[2].isdigit() or line[2][0] == '-':\n line[2] = _number(line[2])\n ret[line[0]] = line[2]\n if len(line) >= 6:\n if line[5].isdigit() or line[5][0] == '-':\n line[5] = _number(line[5])\n ret[line[3]] = line[5]\n return ret\n\n def aix_netstats():\n '''\n AIX specific netstats implementation\n '''\n ret = {}\n fields = []\n procn = None\n proto_name = None\n for line in __salt__['cmd.run']('netstat -s').splitlines():\n if not line:\n continue\n\n if not re.match(r'\\s', line) and ':' in line:\n comps = line.split(':')\n proto_name = comps[0]\n ret[proto_name] = []\n procn = len(ret[proto_name])\n ret[proto_name].append({})\n continue\n else:\n comps = line.split()\n comps[0] = comps[0].strip()\n if comps[0].isdigit():\n ret[proto_name][procn][' '.join(comps[1:])] = _number(comps[0])\n else:\n continue\n\n return ret\n\n # dict that returns a function that does the right thing per platform\n get_version = {\n 'Linux': linux_netstats,\n 'FreeBSD': bsd_netstats,\n 'OpenBSD': bsd_netstats,\n 'SunOS': sunos_netstats,\n 'AIX': aix_netstats,\n }\n\n errmsg = 'This method is unsupported on the current operating system!'\n return get_version.get(__grains__['kernel'], lambda: errmsg)()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef w(): # pylint: disable=C0103\n '''\n Return a list of logged in users for this minion, using the w command\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.w\n '''\n def linux_w():\n '''\n Linux specific implementation for w\n '''\n user_list = []\n users = __salt__['cmd.run']('w -fh').splitlines()\n for row in users:\n if not row:\n continue\n comps = row.split()\n rec = {'idle': comps[3],\n 'jcpu': comps[4],\n 'login': comps[2],\n 'pcpu': comps[5],\n 'tty': comps[1],\n 'user': comps[0],\n 'what': ' '.join(comps[6:])}\n user_list.append(rec)\n return user_list\n\n def bsd_w():\n '''\n Generic BSD implementation for w\n '''\n user_list = []\n users = __salt__['cmd.run']('w -h').splitlines()\n for row in users:\n if not row:\n continue\n comps = row.split()\n rec = {'from': comps[2],\n 'idle': comps[4],\n 'login': comps[3],\n 'tty': comps[1],\n 'user': comps[0],\n 'what': ' '.join(comps[5:])}\n user_list.append(rec)\n return user_list\n\n # dict that returns a function that does the right thing per platform\n get_version = {\n 'Darwin': bsd_w,\n 'FreeBSD': bsd_w,\n 'Linux': linux_w,\n 'OpenBSD': bsd_w,\n }\n\n errmsg = 'This method is unsupported on the current operating system!'\n return get_version.get(__grains__['kernel'], lambda: errmsg)()", "response": "Return a list of logged in users for this minion using the w command."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a composite of all status data and info for this minion.", "response": "def all_status():\n '''\n Return a composite of all status data and info for this minion.\n Warning: There is a LOT here!\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.all_status\n '''\n return {'cpuinfo': cpuinfo(),\n 'cpustats': cpustats(),\n 'diskstats': diskstats(),\n 'diskusage': diskusage(),\n 'loadavg': loadavg(),\n 'meminfo': meminfo(),\n 'netdev': netdev(),\n 'netstats': netstats(),\n 'uptime': uptime(),\n 'vmstats': vmstats(),\n 'w': w()}"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the PID of the process running or not.", "response": "def pid(sig):\n '''\n Return the PID or an empty string if the process is running or not.\n Pass a signature to use to find the process via ps. Note you can pass\n a Python-compatible regular expression to return all pids of\n processes matching the regexp.\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.pid <sig>\n '''\n\n cmd = __grains__['ps']\n output = __salt__['cmd.run_stdout'](cmd, python_shell=True)\n\n pids = ''\n for line in output.splitlines():\n if 'status.pid' in line:\n continue\n if re.search(sig, line):\n if pids:\n pids += '\\n'\n pids += line.split()[1]\n\n return pids"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef version():\n '''\n Return the system version for this minion\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n .. versionchanged:: 2018.3.0\n Added support for OpenBSD\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.version\n '''\n def linux_version():\n '''\n linux specific implementation of version\n '''\n try:\n with salt.utils.files.fopen('/proc/version', 'r') as fp_:\n return salt.utils.stringutils.to_unicode(fp_.read()).strip()\n except IOError:\n return {}\n\n def bsd_version():\n '''\n bsd specific implementation of version\n '''\n return __salt__['cmd.run']('sysctl -n kern.version')\n\n # dict that returns a function that does the right thing per platform\n get_version = {\n 'Linux': linux_version,\n 'FreeBSD': bsd_version,\n 'OpenBSD': bsd_version,\n 'AIX': lambda: __salt__['cmd.run']('oslevel -s'),\n }\n\n errmsg = 'This method is unsupported on the current operating system!'\n return get_version.get(__grains__['kernel'], lambda: errmsg)()", "response": "Return the system version for this minion"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef master(master=None, connected=True):\n '''\n .. versionadded:: 2014.7.0\n\n Return the connection status with master. Fire an event if the\n connection to master is not as expected. This function is meant to be\n run via a scheduled job from the minion. If master_ip is an FQDN/Hostname,\n it must be resolvable to a valid IPv4 address.\n\n .. versionchanged:: 2016.11.4\n Added support for AIX\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.master\n '''\n master_ips = None\n\n if master:\n master_ips = salt.utils.network.host_to_ips(master)\n\n if not master_ips:\n return\n\n master_connection_status = False\n port = __salt__['config.get']('publish_port', default=4505)\n connected_ips = salt.utils.network.remote_port_tcp(port)\n\n # Get connection status for master\n for master_ip in master_ips:\n if master_ip in connected_ips:\n master_connection_status = True\n break\n\n # Connection to master is not as expected\n if master_connection_status is not connected:\n event = salt.utils.event.get_event('minion', opts=__opts__, listen=False)\n if master_connection_status:\n event.fire_event({'master': master}, salt.minion.master_event(type='connected'))\n else:\n event.fire_event({'master': master}, salt.minion.master_event(type='disconnected'))\n\n return master_connection_status", "response": "Return the connection status with master."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef ping_master(master):\n '''\n .. versionadded:: 2016.3.0\n\n Sends ping request to the given master. Fires '__master_failback' event on success.\n Returns bool result.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.ping_master localhost\n '''\n if master is None or master == '':\n return False\n\n opts = copy.deepcopy(__opts__)\n opts['master'] = master\n if 'master_ip' in opts: # avoid 'master ip changed' warning\n del opts['master_ip']\n opts.update(salt.minion.prep_ip_port(opts))\n try:\n opts.update(salt.minion.resolve_dns(opts, fallback=False))\n except Exception:\n return False\n\n timeout = opts.get('auth_timeout', 60)\n load = {'cmd': 'ping'}\n\n result = False\n channel = salt.transport.client.ReqChannel.factory(opts, crypt='clear')\n try:\n payload = channel.send(load, tries=0, timeout=timeout)\n result = True\n except Exception as e:\n pass\n\n if result:\n event = salt.utils.event.get_event('minion', opts=__opts__, listen=False)\n event.fire_event({'master': master}, salt.minion.master_event(type='failback'))\n\n return result", "response": "Ping the master and return the result."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nforces proxy minion reconnection when not alive.", "response": "def proxy_reconnect(proxy_name, opts=None):\n '''\n Forces proxy minion reconnection when not alive.\n\n proxy_name\n The virtual name of the proxy module.\n\n opts: None\n Opts dictionary. Not intended for CLI usage.\n\n CLI Example:\n\n salt '*' status.proxy_reconnect rest_sample\n '''\n\n if not opts:\n opts = __opts__\n\n if 'proxy' not in opts:\n return False # fail\n\n proxy_keepalive_fn = proxy_name+'.alive'\n if proxy_keepalive_fn not in __proxy__:\n return False # fail\n\n is_alive = __proxy__[proxy_keepalive_fn](opts)\n if not is_alive:\n minion_id = opts.get('proxyid', '') or opts.get('id', '')\n log.info('%s (%s proxy) is down. Restarting.', minion_id, proxy_name)\n __proxy__[proxy_name+'.shutdown'](opts) # safely close connection\n __proxy__[proxy_name+'.init'](opts) # reopen connection\n log.debug('Restarted %s (%s proxy)!', minion_id, proxy_name)\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nturn the mess of OrderedDicts and Lists into a list of dicts for use in the CRL module.", "response": "def _revoked_to_list(revs):\n '''\n Turn the mess of OrderedDicts and Lists into a list of dicts for\n use in the CRL module.\n '''\n list_ = []\n\n for rev in revs:\n for rev_name, props in six.iteritems(\n rev): # pylint: disable=unused-variable\n dict_ = {}\n for prop in props:\n for propname, val in six.iteritems(prop):\n if isinstance(val, datetime.datetime):\n val = val.strftime('%Y-%m-%d %H:%M:%S')\n dict_[propname] = val\n list_.append(dict_)\n\n return list_"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef private_key_managed(name,\n bits=2048,\n passphrase=None,\n cipher='aes_128_cbc',\n new=False,\n overwrite=False,\n verbose=True,\n **kwargs):\n '''\n Manage a private key's existence.\n\n name:\n Path to the private key\n\n bits:\n Key length in bits. Default 2048.\n\n passphrase:\n Passphrase for encrypting the private key.\n\n cipher:\n Cipher for encrypting the private key.\n\n new:\n Always create a new key. Defaults to False.\n Combining new with :mod:`prereq <salt.states.requsities.preqreq>`, or when used as part of a\n `managed_private_key` can allow key rotation whenever a new certificiate is generated.\n\n overwrite:\n Overwrite an existing private key if the provided passphrase cannot decrypt it.\n\n verbose:\n Provide visual feedback on stdout, dots while key is generated.\n Default is True.\n\n .. versionadded:: 2016.11.0\n\n kwargs:\n Any kwargs supported by file.managed are supported.\n\n Example:\n\n The jinja templating in this example ensures a private key is generated if the file doesn't exist\n and that a new private key is generated whenever the certificate that uses it is to be renewed.\n\n .. code-block:: jinja\n\n /etc/pki/www.key:\n x509.private_key_managed:\n - bits: 4096\n - new: True\n {% if salt['file.file_exists']('/etc/pki/www.key') -%}\n - prereq:\n - x509: /etc/pki/www.crt\n {%- endif %}\n '''\n file_args, kwargs = _get_file_args(name, **kwargs)\n new_key = False\n if _check_private_key(\n name, bits=bits, passphrase=passphrase, new=new, overwrite=overwrite):\n file_args['contents'] = __salt__['x509.get_pem_entry'](\n name, pem_type='RSA PRIVATE KEY')\n else:\n new_key = True\n file_args['contents'] = __salt__['x509.create_private_key'](\n text=True, bits=bits, passphrase=passphrase, cipher=cipher, verbose=verbose)\n\n # Ensure the key contents are a string before passing it along\n file_args['contents'] = salt.utils.stringutils.to_str(file_args['contents'])\n\n ret = __states__['file.managed'](**file_args)\n if ret['changes'] and new_key:\n ret['changes'] = {'new': 'New private key generated'}\n\n return ret", "response": "Manage a private key."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef csr_managed(name,\n **kwargs):\n '''\n Manage a Certificate Signing Request\n\n name:\n Path to the CSR\n\n properties:\n The properties to be added to the certificate request, including items like subject, extensions\n and public key. See above for valid properties.\n\n kwargs:\n Any arguments supported by :py:func:`file.managed <salt.states.file.managed>` are supported.\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 Example:\n\n .. code-block:: yaml\n\n /etc/pki/mycert.csr:\n x509.csr_managed:\n - private_key: /etc/pki/mycert.key\n - CN: www.example.com\n - C: US\n - ST: Utah\n - L: Salt Lake City\n - keyUsage: 'critical dataEncipherment'\n\n /etc/pki/mycert.csr:\n x509.csr_managed:\n - private_key: /etc/pki/mycert.key\n - CN: www.example.com\n - C: US\n - ST: Utah\n - L: Salt Lake City\n - keyUsage: 'critical dataEncipherment'\n - DomainController: 'ASN1:UTF8String:SomeOneSomeWhere'\n - ext_mapping:\n '1.3.6.1.4.1.311.20.2': 'DomainController'\n\n '''\n try:\n old = __salt__['x509.read_csr'](name)\n except salt.exceptions.SaltInvocationError:\n old = '{0} is not a valid csr.'.format(name)\n\n file_args, kwargs = _get_file_args(name, **kwargs)\n file_args['contents'] = __salt__['x509.create_csr'](text=True, **kwargs)\n\n ret = __states__['file.managed'](**file_args)\n if ret['changes']:\n new = __salt__['x509.read_csr'](file_args['contents'])\n if old != new:\n ret['changes'] = {\"Old\": old, \"New\": new}\n\n return ret", "response": "Manage a Certificate Signing Request"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nmanage a certificate in a new file.", "response": "def certificate_managed(name,\n days_remaining=90,\n managed_private_key=None,\n append_certs=None,\n **kwargs):\n '''\n Manage a Certificate\n\n name\n Path to the certificate\n\n days_remaining : 90\n The minimum number of days remaining when the certificate should be\n recreated. A value of 0 disables automatic renewal.\n\n managed_private_key\n Manages the private key corresponding to the certificate. All of the\n arguments supported by :py:func:`x509.private_key_managed\n <salt.states.x509.private_key_managed>` are supported. If `name` is not\n speicified or is the same as the name of the certificate, the private\n key and certificate will be written together in the same file.\n\n append_certs:\n A list of certificates to be appended to the managed file.\n\n kwargs:\n Any arguments supported by :py:func:`x509.create_certificate\n <salt.modules.x509.create_certificate>` or :py:func:`file.managed\n <salt.states.file.managed>` are supported.\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 Examples:\n\n .. code-block:: yaml\n\n /etc/pki/ca.crt:\n x509.certificate_managed:\n - signing_private_key: /etc/pki/ca.key\n - CN: ca.example.com\n - C: US\n - ST: Utah\n - L: Salt Lake City\n - basicConstraints: \"critical CA:true\"\n - keyUsage: \"critical cRLSign, keyCertSign\"\n - subjectKeyIdentifier: hash\n - authorityKeyIdentifier: keyid,issuer:always\n - days_valid: 3650\n - days_remaining: 0\n - backup: True\n\n\n .. code-block:: yaml\n\n /etc/ssl/www.crt:\n x509.certificate_managed:\n - ca_server: pki\n - signing_policy: www\n - public_key: /etc/ssl/www.key\n - CN: www.example.com\n - days_valid: 90\n - days_remaining: 30\n - backup: True\n\n '''\n if 'path' in kwargs:\n name = kwargs.pop('path')\n\n file_args, kwargs = _get_file_args(name, **kwargs)\n\n rotate_private_key = False\n new_private_key = False\n if managed_private_key:\n private_key_args = {\n 'name': name,\n 'new': False,\n 'overwrite': False,\n 'bits': 2048,\n 'passphrase': None,\n 'cipher': 'aes_128_cbc',\n 'verbose': True\n }\n private_key_args.update(managed_private_key)\n kwargs['public_key_passphrase'] = private_key_args['passphrase']\n\n if private_key_args['new']:\n rotate_private_key = True\n private_key_args['new'] = False\n\n if _check_private_key(private_key_args['name'],\n bits=private_key_args['bits'],\n passphrase=private_key_args['passphrase'],\n new=private_key_args['new'],\n overwrite=private_key_args['overwrite']):\n private_key = __salt__['x509.get_pem_entry'](\n private_key_args['name'], pem_type='RSA PRIVATE KEY')\n else:\n new_private_key = True\n private_key = __salt__['x509.create_private_key'](text=True, bits=private_key_args['bits'],\n passphrase=private_key_args['passphrase'],\n cipher=private_key_args['cipher'],\n verbose=private_key_args['verbose'])\n\n kwargs['public_key'] = private_key\n\n current_days_remaining = 0\n current_comp = {}\n\n if os.path.isfile(name):\n try:\n current = __salt__['x509.read_certificate'](certificate=name)\n current_comp = copy.deepcopy(current)\n if 'serial_number' not in kwargs:\n current_comp.pop('Serial Number')\n if 'signing_cert' not in kwargs:\n try:\n current_comp['X509v3 Extensions']['authorityKeyIdentifier'] = (\n re.sub(r'serial:([0-9A-F]{2}:)*[0-9A-F]{2}', 'serial:--',\n current_comp['X509v3 Extensions']['authorityKeyIdentifier']))\n except KeyError:\n pass\n current_comp.pop('Not Before')\n current_comp.pop('MD5 Finger Print')\n current_comp.pop('SHA1 Finger Print')\n current_comp.pop('SHA-256 Finger Print')\n current_notafter = current_comp.pop('Not After')\n current_days_remaining = (\n datetime.datetime.strptime(current_notafter, '%Y-%m-%d %H:%M:%S') -\n datetime.datetime.now()).days\n if days_remaining == 0:\n days_remaining = current_days_remaining - 1\n except salt.exceptions.SaltInvocationError:\n current = '{0} is not a valid Certificate.'.format(name)\n else:\n current = '{0} does not exist.'.format(name)\n\n if 'ca_server' in kwargs and 'signing_policy' not in kwargs:\n raise salt.exceptions.SaltInvocationError(\n 'signing_policy must be specified if ca_server is.')\n\n new = __salt__['x509.create_certificate'](testrun=False, text=True, **kwargs)\n new = __salt__['x509.read_certificate'](certificate=new)\n newcert = __salt__['x509.create_certificate'](testrun=True, **kwargs)\n\n if isinstance(new, dict):\n new_comp = copy.deepcopy(new)\n if 'serial_number' not in kwargs:\n new_comp.pop('Serial Number')\n if 'signing_cert' not in kwargs:\n try:\n new_comp['X509v3 Extensions']['authorityKeyIdentifier'] = (\n re.sub(r'serial:([0-9A-F]{2}:)*[0-9A-F]{2}', 'serial:--',\n new_comp['X509v3 Extensions']['authorityKeyIdentifier']))\n except KeyError:\n pass\n new_comp.pop('Not Before')\n new_comp.pop('Not After')\n new_comp.pop('MD5 Finger Print')\n new_comp.pop('SHA1 Finger Print')\n new_comp.pop('SHA-256 Finger Print')\n new_issuer_public_key = new_issuer_public_key = newcert.pop('Issuer Public Key')\n else:\n new_comp = new\n\n new_certificate = False\n if (current_comp == new_comp and\n current_days_remaining > days_remaining and\n __salt__['x509.verify_signature'](name, new_issuer_public_key)):\n certificate = __salt__['x509.get_pem_entry'](\n name, pem_type='CERTIFICATE')\n else:\n if rotate_private_key and not new_private_key:\n new_private_key = True\n private_key = __salt__['x509.create_private_key'](\n text=True, bits=private_key_args['bits'], verbose=private_key_args['verbose'])\n kwargs['public_key'] = private_key\n new_certificate = True\n certificate = __salt__['x509.create_certificate'](text=True, **kwargs)\n\n file_args['contents'] = ''\n private_ret = {}\n if managed_private_key:\n if private_key_args['name'] == name:\n file_args['contents'] = private_key\n else:\n private_file_args = copy.deepcopy(file_args)\n unique_private_file_args, _ = _get_file_args(**private_key_args)\n private_file_args.update(unique_private_file_args)\n private_file_args['contents'] = private_key\n private_ret = __states__['file.managed'](**private_file_args)\n if not private_ret['result']:\n return private_ret\n\n file_args['contents'] += salt.utils.stringutils.to_str(certificate)\n\n if not append_certs:\n append_certs = []\n for append_cert in append_certs:\n file_args[\n 'contents'] += __salt__['x509.get_pem_entry'](append_cert, pem_type='CERTIFICATE')\n\n file_args['show_changes'] = False\n ret = __states__['file.managed'](**file_args)\n\n if ret['changes']:\n ret['changes'] = {'Certificate': ret['changes']}\n else:\n ret['changes'] = {}\n if private_ret and private_ret['changes']:\n ret['changes']['Private Key'] = private_ret['changes']\n if new_private_key:\n ret['changes']['Private Key'] = 'New private key generated'\n if new_certificate:\n ret['changes']['Certificate'] = {\n 'Old': current,\n 'New': __salt__['x509.read_certificate'](certificate=certificate)}\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nmanaging a Certificate Revocation List for a given certificate.", "response": "def crl_managed(name,\n signing_private_key,\n signing_private_key_passphrase=None,\n signing_cert=None,\n revoked=None,\n days_valid=100,\n digest=\"\",\n days_remaining=30,\n include_expired=False,\n **kwargs):\n '''\n Manage a Certificate Revocation List\n\n name\n Path to the certificate\n\n signing_private_key\n The private key that will be used to sign this crl. This is\n usually your CA's private key.\n\n signing_private_key_passphrase\n Passphrase to decrypt the private key.\n\n signing_cert\n The certificate of the authority that will be used to sign this crl.\n This is usually your CA's certificate.\n\n revoked\n A list of certificates to revoke. Must include either a serial number or a\n the certificate itself. Can optionally include the revocation date and\n notAfter date from the certificate. See example below for details.\n\n days_valid : 100\n The number of days the certificate should be valid for.\n\n digest\n The digest to use for signing the CRL. This has no effect on versions\n of pyOpenSSL less than 0.14.\n\n days_remaining : 30\n The crl should be automatically recreated if there are less than\n ``days_remaining`` days until the crl expires. Set to 0 to disable\n automatic renewal.\n\n include_expired : False\n If ``True``, include expired certificates in the CRL.\n\n kwargs\n Any arguments supported by :py:func:`file.managed <salt.states.file.managed>` are supported.\n\n Example:\n\n .. code-block:: yaml\n\n /etc/pki/ca.crl:\n x509.crl_managed:\n - signing_private_key: /etc/pki/myca.key\n - signing_cert: /etc/pki/myca.crt\n - revoked:\n - compromized_Web_key:\n - certificate: /etc/pki/certs/badweb.crt\n - revocation_date: 2015-03-01 00:00:00\n - reason: keyCompromise\n - terminated_vpn_user:\n - serial_number: D6:D2:DC:D8:4D:5C:C0:F4\n - not_after: 2016-01-01 00:00:00\n - revocation_date: 2015-02-25 00:00:00\n - reason: cessationOfOperation\n '''\n if revoked is None:\n revoked = []\n\n revoked = _revoked_to_list(revoked)\n\n current_days_remaining = 0\n current_comp = {}\n\n if os.path.isfile(name):\n try:\n current = __salt__['x509.read_crl'](crl=name)\n current_comp = current.copy()\n current_comp.pop('Last Update')\n current_notafter = current_comp.pop('Next Update')\n current_days_remaining = (\n datetime.datetime.strptime(current_notafter, '%Y-%m-%d %H:%M:%S') -\n datetime.datetime.now()).days\n if days_remaining == 0:\n days_remaining = current_days_remaining - 1\n except salt.exceptions.SaltInvocationError:\n current = '{0} is not a valid CRL.'.format(name)\n else:\n current = '{0} does not exist.'.format(name)\n\n new_crl = __salt__['x509.create_crl'](text=True, signing_private_key=signing_private_key,\n signing_private_key_passphrase=signing_private_key_passphrase,\n signing_cert=signing_cert, revoked=revoked, days_valid=days_valid,\n digest=digest, include_expired=include_expired)\n\n new = __salt__['x509.read_crl'](crl=new_crl)\n new_comp = new.copy()\n new_comp.pop('Last Update')\n new_comp.pop('Next Update')\n\n file_args, kwargs = _get_file_args(name, **kwargs)\n new_crl_created = False\n if (current_comp == new_comp and\n current_days_remaining > days_remaining and\n __salt__['x509.verify_crl'](name, signing_cert)):\n file_args['contents'] = __salt__[\n 'x509.get_pem_entry'](name, pem_type='X509 CRL')\n else:\n new_crl_created = True\n file_args['contents'] = new_crl\n\n ret = __states__['file.managed'](**file_args)\n if new_crl_created:\n ret['changes'] = {'Old': current, 'New': __salt__[\n 'x509.read_crl'](crl=new_crl)}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef pem_managed(name,\n text,\n backup=False,\n **kwargs):\n '''\n Manage the contents of a PEM file directly with the content in text, ensuring correct formatting.\n\n name:\n The path to the file to manage\n\n text:\n The PEM formatted text to write.\n\n kwargs:\n Any arguments supported by :py:func:`file.managed <salt.states.file.managed>` are supported.\n '''\n file_args, kwargs = _get_file_args(name, **kwargs)\n file_args['contents'] = salt.utils.stringutils.to_str(__salt__['x509.get_pem_entry'](text=text))\n\n return __states__['file.managed'](**file_args)", "response": "Manage the contents of a PEM file directly with the content in text ensuring correct formatting."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef date_cast(date):\n '''\n Casts any object into a datetime.datetime object\n\n date\n any datetime, time string representation...\n '''\n if date is None:\n return datetime.datetime.now()\n elif isinstance(date, datetime.datetime):\n return date\n\n # fuzzy date\n try:\n if isinstance(date, six.string_types):\n try:\n if HAS_TIMELIB:\n # py3: yes, timelib.strtodatetime wants bytes, not str :/\n return timelib.strtodatetime(\n salt.utils.stringutils.to_bytes(date))\n except ValueError:\n pass\n\n # not parsed yet, obviously a timestamp?\n if date.isdigit():\n date = int(date)\n else:\n date = float(date)\n\n return datetime.datetime.fromtimestamp(date)\n except Exception:\n if HAS_TIMELIB:\n raise ValueError('Unable to parse {0}'.format(date))\n\n raise RuntimeError(\n 'Unable to parse {0}. Consider installing timelib'.format(date))", "response": "Casts any object into a datetime. datetime object date\nElems any datetime string representation..."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure that the named user is present in the specified database.", "response": "def present(name, login=None, domain=None, database=None, roles=None, options=None, **kwargs):\n '''\n Checks existance of the named user.\n If not present, creates the user with the specified roles and options.\n\n name\n The name of the user to manage\n login\n If not specified, will be created WITHOUT LOGIN\n domain\n Creates a Windows authentication user.\n Needs to be NetBIOS domain or hostname\n database\n The database of the user (not the login)\n roles\n Add this user to all the roles in the list\n options\n Can be a list of strings, a dictionary, or a list of dictionaries\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n if domain and not login:\n ret['result'] = False\n ret['comment'] = 'domain cannot be set without login'\n return ret\n if __salt__['mssql.user_exists'](name, domain=domain, database=database, **kwargs):\n ret['comment'] = 'User {0} is already present (Not going to try to set its roles or options)'.format(name)\n return ret\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'User {0} is set to be added'.format(name)\n return ret\n\n user_created = __salt__['mssql.user_create'](name, login=login,\n domain=domain,\n database=database,\n roles=roles,\n options=_normalize_options(options),\n **kwargs)\n if user_created is not True: # Non-empty strings are also evaluated to True, so we cannot use if not user_created:\n ret['result'] = False\n ret['comment'] += 'User {0} failed to be added: {1}'.format(name, user_created)\n return ret\n ret['comment'] += 'User {0} has been added'.format(name)\n ret['changes'][name] = 'Present'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nensures that the named user is absent", "response": "def absent(name, **kwargs):\n '''\n Ensure that the named user is absent\n\n name\n The username of the user to remove\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n if not __salt__['mssql.user_exists'](name):\n ret['comment'] = 'User {0} is not present'.format(name)\n return ret\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'User {0} is set to be removed'.format(name)\n return ret\n if __salt__['mssql.user_remove'](name, **kwargs):\n ret['comment'] = 'User {0} has been removed'.format(name)\n ret['changes'][name] = 'Absent'\n return ret\n # else:\n ret['result'] = False\n ret['comment'] = 'User {0} failed to be removed'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a function that dumps the object and loads it again", "response": "def _strip_odict(wrapped):\n '''\n dump to json and load it again, replaces OrderedDicts with regular ones\n '''\n @functools.wraps(wrapped)\n def strip(*args):\n return salt.utils.json.loads(salt.utils.json.dumps(wrapped(*args)))\n return strip"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nload the map at the specified path and returns the specified value from the specified map.", "response": "def load_map(path, value):\n '''\n Loads the map at the specified path, and returns the specified value from\n that map.\n\n CLI Example:\n\n .. code-block:: bash\n\n # Assuming the map is loaded in your formula SLS as follows:\n #\n # {% from \"myformula/map.jinja\" import myformula with context %}\n #\n # the following syntax can be used to load the map and check the\n # results:\n salt myminion jinja.load_map myformula/map.jinja myformula\n '''\n tmplstr = textwrap.dedent('''\\\n {{% from \"{path}\" import {value} with context %}}\n {{{{ {value} | tojson }}}}\n '''.format(path=path, value=value))\n return salt.template.compile_template_str(\n tmplstr,\n salt.loader.render(__opts__, __salt__),\n __opts__['renderer'],\n __opts__['renderer_blacklist'],\n __opts__['renderer_whitelist'])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting a value from the pillar data.", "response": "def get(key,\n default=KeyError,\n merge=False,\n merge_nested_lists=None,\n delimiter=DEFAULT_TARGET_DELIM,\n pillarenv=None,\n saltenv=None):\n '''\n .. versionadded:: 0.14\n\n Attempt to retrieve the named value from :ref:`in-memory pillar data\n <pillar-in-memory>`. If the pillar key is not present in the in-memory\n pillar, then the value specified in the ``default`` option (described\n below) will be returned.\n\n If the merge parameter is set to ``True``, the default will be recursively\n merged into the returned pillar data.\n\n The value can also represent a value in a nested dict using a \":\" delimiter\n for the dict. This means that if a dict in pillar looks like this::\n\n {'pkg': {'apache': 'httpd'}}\n\n To retrieve the value associated with the ``apache`` key in the ``pkg``\n dict this key can be passed as::\n\n pkg:apache\n\n key\n The pillar key to get value from\n\n default\n The value specified by this option will be returned if the desired\n pillar key does not exist.\n\n If a default value is specified, then it will be an empty string,\n unless :conf_minion:`pillar_raise_on_missing` is set to ``True``, in\n which case an error will be raised.\n\n merge : ``False``\n If ``True``, the retrieved values will be merged into the passed\n default. When the default and the retrieved value are both\n dictionaries, the dictionaries will be recursively merged.\n\n .. versionadded:: 2014.7.0\n .. versionchanged:: 2016.3.7,2016.11.4,2017.7.0\n If the default and the retrieved value are not of the same type,\n then merging will be skipped and the retrieved value will be\n returned. Earlier releases raised an error in these cases.\n\n merge_nested_lists\n If set to ``False``, lists nested within the retrieved pillar\n dictionary will *overwrite* lists in ``default``. If set to ``True``,\n nested lists will be *merged* into lists in ``default``. If unspecified\n (the default), this option is inherited from the\n :conf_minion:`pillar_merge_lists` minion config option.\n\n .. note::\n This option is ignored when ``merge`` is set to ``False``.\n\n .. versionadded:: 2016.11.6\n\n delimiter\n Specify an alternate delimiter to use when traversing a nested dict.\n This is useful for when the desired key contains a colon. See CLI\n example below for usage.\n\n .. versionadded:: 2014.7.0\n\n pillarenv\n If specified, this function will query the master to generate fresh\n pillar data on the fly, specifically from the requested pillar\n environment. Note that this can produce different pillar data than\n executing this function without an environment, as its normal behavior\n is just to return a value from minion's pillar data in memory (which\n can be sourced from more than one pillar environment).\n\n Using this argument will not affect the pillar data in memory. It will\n however be slightly slower and use more resources on the master due to\n the need for the master to generate and send the minion fresh pillar\n data. This tradeoff in performance however allows for the use case\n where pillar data is desired only from a single environment.\n\n .. versionadded:: 2017.7.0\n\n saltenv\n Included only for compatibility with\n :conf_minion:`pillarenv_from_saltenv`, and is otherwise ignored.\n\n .. versionadded:: 2017.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pillar.get pkg:apache\n salt '*' pillar.get abc::def|ghi delimiter='|'\n '''\n if not __opts__.get('pillar_raise_on_missing'):\n if default is KeyError:\n default = ''\n opt_merge_lists = __opts__.get('pillar_merge_lists', False) if \\\n merge_nested_lists is None else merge_nested_lists\n pillar_dict = __pillar__ \\\n if all(x is None for x in (saltenv, pillarenv)) \\\n else items(saltenv=saltenv, pillarenv=pillarenv)\n\n if merge:\n if isinstance(default, dict):\n ret = salt.utils.data.traverse_dict_and_list(\n pillar_dict,\n key,\n {},\n delimiter)\n if isinstance(ret, collections.Mapping):\n default = copy.deepcopy(default)\n return salt.utils.dictupdate.update(\n default,\n ret,\n merge_lists=opt_merge_lists)\n else:\n log.error(\n 'pillar.get: Default (%s) is a dict, but the returned '\n 'pillar value (%s) is of type \\'%s\\'. Merge will be '\n 'skipped.', default, ret, type(ret).__name__\n )\n elif isinstance(default, list):\n ret = salt.utils.data.traverse_dict_and_list( # pylint: disable=redefined-variable-type\n pillar_dict,\n key,\n [],\n delimiter)\n if isinstance(ret, list):\n default = copy.deepcopy(default)\n default.extend([x for x in ret if x not in default])\n return default\n else:\n log.error(\n 'pillar.get: Default (%s) is a list, but the returned '\n 'pillar value (%s) is of type \\'%s\\'. Merge will be '\n 'skipped.', default, ret, type(ret).__name__\n )\n else:\n log.error(\n 'pillar.get: Default (%s) is of type \\'%s\\', must be a dict '\n 'or list to merge. Merge will be skipped.',\n default, type(default).__name__\n )\n\n ret = salt.utils.data.traverse_dict_and_list(\n pillar_dict,\n key,\n default,\n delimiter)\n if ret is KeyError:\n raise KeyError('Pillar key not found: {0}'.format(key))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncalls the master for a fresh pillar and generates the pillar data on the fly Contrast with :py:func:`raw` which returns the pillar data that is currently loaded into the minion. pillar If specified, allows for a dictionary of pillar data to be made available to pillar and ext_pillar rendering. these pillar variables will also override any variables of the same name in pillar or ext_pillar. .. versionadded:: 2015.5.0 pillar_enc If specified, the data passed in the ``pillar`` argument will be passed through this renderer to decrypt it. .. note:: This will decrypt on the minion side, so the specified renderer must be set up on the minion for this to work. Alternatively, pillar data can be decrypted master-side. For more information, see the :ref:`Pillar Encryption <pillar-encryption>` documentation. Pillar data that is decrypted master-side, is not decrypted until the end of pillar compilation though, so minion-side decryption will be necessary if the encrypted pillar data must be made available in an decrypted state pillar/ext_pillar rendering. .. versionadded:: 2017.7.0 pillarenv Pass a specific pillar environment from which to compile pillar data. If not specified, then the minion's :conf_minion:`pillarenv` option is not used, and if that also is not specified then all configured pillar environments will be merged into a single pillar dictionary and returned. .. versionadded:: 2016.11.2 saltenv Included only for compatibility with :conf_minion:`pillarenv_from_saltenv`, and is otherwise ignored. CLI Example: .. code-block:: bash salt '*' pillar.items", "response": "def items(*args, **kwargs):\n '''\n Calls the master for a fresh pillar and generates the pillar data on the\n fly\n\n Contrast with :py:func:`raw` which returns the pillar data that is\n currently loaded into the minion.\n\n pillar\n If specified, allows for a dictionary of pillar data to be made\n available to pillar and ext_pillar rendering. these pillar variables\n will also override any variables of the same name in pillar or\n ext_pillar.\n\n .. versionadded:: 2015.5.0\n\n pillar_enc\n If specified, the data passed in the ``pillar`` argument will be passed\n through this renderer to decrypt it.\n\n .. note::\n This will decrypt on the minion side, so the specified renderer\n must be set up on the minion for this to work. Alternatively,\n pillar data can be decrypted master-side. For more information, see\n the :ref:`Pillar Encryption <pillar-encryption>` documentation.\n Pillar data that is decrypted master-side, is not decrypted until\n the end of pillar compilation though, so minion-side decryption\n will be necessary if the encrypted pillar data must be made\n available in an decrypted state pillar/ext_pillar rendering.\n\n .. versionadded:: 2017.7.0\n\n pillarenv\n Pass a specific pillar environment from which to compile pillar data.\n If not specified, then the minion's :conf_minion:`pillarenv` option is\n not used, and if that also is not specified then all configured pillar\n environments will be merged into a single pillar dictionary and\n returned.\n\n .. versionadded:: 2016.11.2\n\n saltenv\n Included only for compatibility with\n :conf_minion:`pillarenv_from_saltenv`, and is otherwise ignored.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pillar.items\n '''\n # Preserve backwards compatibility\n if args:\n return item(*args)\n\n pillarenv = kwargs.get('pillarenv')\n if pillarenv is None:\n if __opts__.get('pillarenv_from_saltenv', False):\n pillarenv = kwargs.get('saltenv') or __opts__['saltenv']\n else:\n pillarenv = __opts__['pillarenv']\n\n pillar_override = kwargs.get('pillar')\n pillar_enc = kwargs.get('pillar_enc')\n\n if pillar_override and pillar_enc:\n try:\n pillar_override = salt.utils.crypt.decrypt(\n pillar_override,\n pillar_enc,\n translate_newlines=True,\n opts=__opts__,\n valid_rend=__opts__['decrypt_pillar_renderers'])\n except Exception as exc:\n raise CommandExecutionError(\n 'Failed to decrypt pillar override: {0}'.format(exc)\n )\n\n pillar = salt.pillar.get_pillar(\n __opts__,\n __grains__,\n __opts__['id'],\n pillar_override=pillar_override,\n pillarenv=pillarenv)\n\n return pillar.compile_pillar()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef item(*args, **kwargs):\n '''\n .. versionadded:: 0.16.2\n\n Return one or more pillar entries from the :ref:`in-memory pillar data\n <pillar-in-memory>`.\n\n delimiter\n Delimiter used to traverse nested dictionaries.\n\n .. note::\n This is different from :py:func:`pillar.get\n <salt.modules.pillar.get>` in that no default value can be\n specified. :py:func:`pillar.get <salt.modules.pillar.get>` should\n probably still be used in most cases to retrieve nested pillar\n values, as it is a bit more flexible. One reason to use this\n function instead of :py:func:`pillar.get <salt.modules.pillar.get>`\n however is when it is desirable to retrieve the values of more than\n one key, since :py:func:`pillar.get <salt.modules.pillar.get>` can\n only retrieve one key at a time.\n\n .. versionadded:: 2015.8.0\n\n pillarenv\n If specified, this function will query the master to generate fresh\n pillar data on the fly, specifically from the requested pillar\n environment. Note that this can produce different pillar data than\n executing this function without an environment, as its normal behavior\n is just to return a value from minion's pillar data in memory (which\n can be sourced from more than one pillar environment).\n\n Using this argument will not affect the pillar data in memory. It will\n however be slightly slower and use more resources on the master due to\n the need for the master to generate and send the minion fresh pillar\n data. This tradeoff in performance however allows for the use case\n where pillar data is desired only from a single environment.\n\n .. versionadded:: 2017.7.6,2018.3.1\n\n saltenv\n Included only for compatibility with\n :conf_minion:`pillarenv_from_saltenv`, and is otherwise ignored.\n\n .. versionadded:: 2017.7.6,2018.3.1\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pillar.item foo\n salt '*' pillar.item foo:bar\n salt '*' pillar.item foo bar baz\n '''\n ret = {}\n default = kwargs.get('default', '')\n delimiter = kwargs.get('delimiter', DEFAULT_TARGET_DELIM)\n pillarenv = kwargs.get('pillarenv', None)\n saltenv = kwargs.get('saltenv', None)\n\n pillar_dict = __pillar__ \\\n if all(x is None for x in (saltenv, pillarenv)) \\\n else items(saltenv=saltenv, pillarenv=pillarenv)\n\n try:\n for arg in args:\n ret[arg] = salt.utils.data.traverse_dict_and_list(\n pillar_dict,\n arg,\n default,\n delimiter)\n except KeyError:\n pass\n\n return ret", "response": "Return one or more pillar entries from the master."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngenerate and apply external pillar and external external_pillar to the base base.", "response": "def ext(external, pillar=None):\n '''\n .. versionchanged:: 2016.3.6,2016.11.3,2017.7.0\n The supported ext_pillar types are now tunable using the\n :conf_master:`on_demand_ext_pillar` config option. Earlier releases\n used a hard-coded default.\n\n Generate the pillar and apply an explicit external pillar\n\n\n external\n A single ext_pillar to add to the ext_pillar configuration. This must\n be passed as a single section from the ext_pillar configuration (see\n CLI examples below). For more complicated ``ext_pillar``\n configurations, it can be helpful to use the Python shell to load YAML\n configuration into a dictionary, and figure out\n\n .. code-block:: python\n\n >>> import salt.utils.yaml\n >>> ext_pillar = salt.utils.yaml.safe_load(\"\"\"\n ... ext_pillar:\n ... - git:\n ... - issue38440 https://github.com/terminalmage/git_pillar:\n ... - env: base\n ... \"\"\")\n >>> ext_pillar\n {'ext_pillar': [{'git': [{'mybranch https://github.com/myuser/myrepo': [{'env': 'base'}]}]}]}\n >>> ext_pillar['ext_pillar'][0]\n {'git': [{'mybranch https://github.com/myuser/myrepo': [{'env': 'base'}]}]}\n\n In the above example, the value to pass would be\n ``{'git': [{'mybranch https://github.com/myuser/myrepo': [{'env': 'base'}]}]}``.\n Note that this would need to be quoted when passing on the CLI (as in\n the CLI examples below).\n\n pillar : None\n If specified, allows for a dictionary of pillar data to be made\n available to pillar and ext_pillar rendering. These pillar variables\n will also override any variables of the same name in pillar or\n ext_pillar.\n\n .. versionadded:: 2015.5.0\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pillar.ext '{libvirt: _}'\n salt '*' pillar.ext \"{'git': ['master https://github.com/myuser/myrepo']}\"\n salt '*' pillar.ext \"{'git': [{'mybranch https://github.com/myuser/myrepo': [{'env': 'base'}]}]}\"\n '''\n if isinstance(external, six.string_types):\n external = salt.utils.yaml.safe_load(external)\n pillar_obj = salt.pillar.get_pillar(\n __opts__,\n __grains__,\n __opts__['id'],\n __opts__['saltenv'],\n ext=external,\n pillar_override=pillar)\n\n ret = pillar_obj.compile_pillar()\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef file_exists(path, saltenv=None):\n '''\n .. versionadded:: 2016.3.0\n\n This is a master-only function. Calling from the minion is not supported.\n\n Use the given path and search relative to the pillar environments to see if\n a file exists at that path.\n\n If the ``saltenv`` argument is given, restrict search to that environment\n only.\n\n Will only work with ``pillar_roots``, not external pillars.\n\n Returns True if the file is found, and False otherwise.\n\n path\n The path to the file in question. Will be treated as a relative path\n\n saltenv\n Optional argument to restrict the search to a specific saltenv\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pillar.file_exists foo/bar.sls\n '''\n pillar_roots = __opts__.get('pillar_roots')\n if not pillar_roots:\n raise CommandExecutionError('No pillar_roots found. Are you running '\n 'this on the master?')\n\n if saltenv:\n if saltenv in pillar_roots:\n pillar_roots = {saltenv: pillar_roots[saltenv]}\n else:\n return False\n\n for env in pillar_roots:\n for pillar_dir in pillar_roots[env]:\n full_path = os.path.join(pillar_dir, path)\n if __salt__['file.file_exists'](full_path):\n return True\n\n return False", "response": "Return True if the file exists at the given path."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the system from the configuration", "response": "def _get_system():\n '''\n Get Pure Storage FlashArray configuration\n\n 1) From the minion config\n pure_tags:\n fa:\n san_ip: management vip or hostname for the FlashArray\n api_token: A valid api token for the FlashArray being managed\n 2) From environment (PUREFA_IP and PUREFA_API)\n 3) From the pillar (PUREFA_IP and PUREFA_API)\n\n '''\n agent = {'base': USER_AGENT_BASE,\n 'class': __name__,\n 'version': VERSION,\n 'platform': platform.platform()\n }\n\n user_agent = '{base} {class}/{version} ({platform})'.format(**agent)\n\n try:\n array = __opts__['pure_tags']['fa'].get('san_ip')\n api = __opts__['pure_tags']['fa'].get('api_token')\n if array and api:\n system = purestorage.FlashArray(array, api_token=api, user_agent=user_agent)\n except (KeyError, NameError, TypeError):\n try:\n san_ip = os.environ.get('PUREFA_IP')\n api_token = os.environ.get('PUREFA_API')\n system = purestorage.FlashArray(san_ip,\n api_token=api_token,\n user_agent=user_agent)\n except (ValueError, KeyError, NameError):\n try:\n system = purestorage.FlashArray(__pillar__['PUREFA_IP'],\n api_token=__pillar__['PUREFA_API'],\n user_agent=user_agent)\n except (KeyError, NameError):\n raise CommandExecutionError('No Pure Storage FlashArray credentials found.')\n\n try:\n system.get()\n except Exception:\n raise CommandExecutionError('Pure Storage FlashArray authentication failed.')\n return system"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_snapshot(name, suffix, array):\n '''Private function to check snapshot'''\n snapshot = name + '.' + suffix\n try:\n for snap in array.get_volume(name, snap=True):\n if snap['name'] == snapshot:\n return snapshot\n except purestorage.PureError:\n return None", "response": "Private function to check snapshot"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_hgroup(name, array):\n '''Private function to check hostgroup'''\n hostgroup = None\n for temp in array.list_hgroups():\n if temp['name'] == name:\n hostgroup = temp\n break\n return hostgroup", "response": "Private function to check hostgroup"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef present(\n name,\n subscriptions=None,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Ensure the SNS topic exists.\n\n name\n Name of the SNS topic.\n\n subscriptions\n List of SNS subscriptions.\n\n Each subscription is a dictionary with a protocol and endpoint key:\n\n .. code-block:: python\n\n [\n {'protocol': 'https', 'endpoint': 'https://www.example.com/sns-endpoint'},\n {'protocol': 'sqs', 'endpoint': 'arn:aws:sqs:us-west-2:123456789012:MyQueue'}\n ]\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n\n is_present = __salt__['boto_sns.exists'](\n name, region=region, key=key, keyid=keyid, profile=profile\n )\n if is_present:\n ret['result'] = True\n ret['comment'] = 'AWS SNS topic {0} present.'.format(name)\n else:\n if __opts__['test']:\n msg = 'AWS SNS topic {0} is set to be created.'.format(name)\n ret['comment'] = msg\n ret['result'] = None\n return ret\n\n created = __salt__['boto_sns.create'](\n name, region=region, key=key, keyid=keyid, profile=profile\n )\n if created:\n msg = 'AWS SNS topic {0} created.'.format(name)\n ret['comment'] = msg\n ret['changes']['old'] = None\n ret['changes']['new'] = {'topic': name, 'subscriptions': []}\n ret['result'] = True\n else:\n ret['comment'] = 'Failed to create {0} AWS SNS topic'.format(name)\n ret['result'] = False\n return ret\n\n if not subscriptions:\n return ret\n\n # Get current subscriptions\n _subscriptions = __salt__['boto_sns.get_all_subscriptions_by_topic'](\n name, region=region, key=key, keyid=keyid, profile=profile\n )\n\n # Convert subscriptions into a data strucure we can compare against\n _subscriptions = [\n {'protocol': s['Protocol'], 'endpoint': s['Endpoint']}\n for s in _subscriptions\n ]\n\n for subscription in subscriptions:\n # If the subscription contains inline digest auth, AWS will *** the\n # password. So we need to do the same with ours if the regex matches\n # Example: https://user:****@my.endpoiint.com/foo/bar\n _endpoint = subscription['endpoint']\n matches = re.search(\n r'https://(?P<user>\\w+):(?P<pass>\\w+)@',\n _endpoint)\n\n # We are using https and have auth creds - the password will be starred out,\n # so star out our password so we can still match it\n if matches is not None:\n subscription['endpoint'] = _endpoint.replace(\n matches.groupdict()['pass'],\n '****')\n\n if subscription not in _subscriptions:\n # Ensure the endpoint is set back to it's original value,\n # incase we starred out a password\n subscription['endpoint'] = _endpoint\n\n if __opts__['test']:\n msg = ' AWS SNS subscription {0}:{1} to be set on topic {2}.'\\\n .format(\n subscription['protocol'],\n subscription['endpoint'],\n name)\n ret['comment'] += msg\n ret['result'] = None\n continue\n\n created = __salt__['boto_sns.subscribe'](\n name, subscription['protocol'], subscription['endpoint'],\n region=region, key=key, keyid=keyid, profile=profile)\n if created:\n msg = ' AWS SNS subscription {0}:{1} set on topic {2}.'\\\n .format(subscription['protocol'],\n subscription['endpoint'],\n name)\n ret['comment'] += msg\n ret['changes'].setdefault('old', None)\n ret['changes']\\\n .setdefault('new', {})\\\n .setdefault('subscriptions', [])\\\n .append(subscription)\n ret['result'] = True\n else:\n ret['result'] = False\n return ret\n else:\n msg = ' AWS SNS subscription {0}:{1} already set on topic {2}.'\\\n .format(\n subscription['protocol'],\n subscription['endpoint'],\n name)\n ret['comment'] += msg\n return ret", "response": "Ensure the SNS topic exists and is created."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures the named SNS topic is absent.", "response": "def absent(\n name,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n unsubscribe=False):\n '''\n Ensure the named sns topic is deleted.\n\n name\n Name of the SNS topic.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n\n unsubscribe\n If True, unsubscribe all subcriptions to the SNS topic before\n deleting the SNS topic\n\n .. versionadded:: 2016.11.0\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n\n is_present = __salt__['boto_sns.exists'](\n name, region=region, key=key, keyid=keyid, profile=profile\n )\n\n if is_present:\n subscriptions = __salt__['boto_sns.get_all_subscriptions_by_topic'](\n name, region=region, key=key, keyid=keyid, profile=profile\n ) if unsubscribe else []\n failed_unsubscribe_subscriptions = []\n\n if __opts__.get('test'):\n ret['comment'] = (\n 'AWS SNS topic {0} is set to be removed. '\n '{1} subscription(s) will be removed.'.format(name, len(subscriptions))\n )\n ret['result'] = None\n return ret\n\n for subscription in subscriptions:\n unsubscribed = __salt__['boto_sns.unsubscribe'](\n name, subscription['SubscriptionArn'], region=region,\n key=key, keyid=keyid, profile=profile\n )\n if unsubscribed is False:\n failed_unsubscribe_subscriptions.append(subscription)\n\n deleted = __salt__['boto_sns.delete'](\n name, region=region, key=key, keyid=keyid, profile=profile)\n if deleted:\n ret['comment'] = 'AWS SNS topic {0} deleted.'.format(name)\n ret['changes']['new'] = None\n if unsubscribe is False:\n ret['changes']['old'] = {'topic': name}\n else:\n ret['changes']['old'] = {'topic': name, 'subscriptions': subscriptions}\n if failed_unsubscribe_subscriptions:\n ret['changes']['new'] = {'subscriptions': failed_unsubscribe_subscriptions}\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to delete {0} AWS SNS topic.'.format(name)\n else:\n ret['comment'] = 'AWS SNS topic {0} does not exist.'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef avail_locations(call=None):\n '''\n List all available 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 ret = {}\n conn = get_conn(service='SoftLayer_Product_Package')\n\n locations = conn.getLocations(id=50)\n for location in locations:\n ret[location['id']] = {\n 'id': location['id'],\n 'name': location['name'],\n 'location': location['longName'],\n }\n\n available = conn.getAvailableLocations(id=50)\n for location in available:\n if location.get('isAvailable', 0) is 0:\n continue\n ret[location['locationId']]['available'] = True\n\n return ret", "response": "Return a list of available locations for the current user"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef avail_images(call=None):\n '''\n Return a dict of all available VM images on the cloud provider.\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_images function must be called with '\n '-f or --function, or with the --list-images option'\n )\n\n ret = {}\n conn = get_conn(service='SoftLayer_Product_Package')\n for category in conn.getCategories(id=50):\n if category['categoryCode'] != 'os':\n continue\n for group in category['groups']:\n for price in group['prices']:\n ret[price['id']] = price['item'].copy()\n del ret[price['id']]['id']\n return ret", "response": "Return a dict of all available VM images on the cloud provider."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a single VM from a data dict.", "response": "def 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 'softlayer_hw',\n vm_['profile'],\n vm_=vm_) is False:\n return False\n except AttributeError:\n pass\n\n name = vm_['name']\n hostname = name\n domain = config.get_cloud_config_value(\n 'domain', vm_, __opts__, default=None\n )\n if domain is None:\n SaltCloudSystemExit(\n 'A domain name is required for the SoftLayer driver.'\n )\n\n if vm_.get('use_fqdn'):\n name = '.'.join([name, domain])\n vm_['name'] = name\n\n __utils__['cloud.fire_event'](\n 'event',\n 'starting create',\n 'salt/cloud/{0}/creating'.format(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 log.info('Creating Cloud VM %s', name)\n conn = get_conn(service='SoftLayer_Product_Order')\n kwargs = {\n 'complexType': 'SoftLayer_Container_Product_Order_Hardware_Server',\n 'quantity': 1,\n 'hardware': [{\n 'hostname': hostname,\n 'domain': domain,\n }],\n # Baremetal Package\n 'packageId': 50,\n 'prices': [\n # Size Ex: 1921: 2 x 2.0 GHz Core Bare Metal Instance - 2 GB Ram\n {'id': vm_['size']},\n # HDD Ex: 19: 250GB SATA II\n {'id': vm_['hdd']},\n # Image Ex: 13963: CentOS 6.0 - Minimal Install (64 bit)\n {'id': vm_['image']},\n\n # The following items are currently required\n # Reboot / Remote Console\n {'id': '905'},\n # 1 IP Address\n {'id': '21'},\n # Host Ping Monitoring\n {'id': '55'},\n # Email and Ticket Notifications\n {'id': '57'},\n # Automated Notification Response\n {'id': '58'},\n # Unlimited SSL VPN Users & 1 PPTP VPN User per account\n {'id': '420'},\n # Nessus Vulnerability Assessment & Reporting\n {'id': '418'},\n ],\n }\n\n optional_products = config.get_cloud_config_value(\n 'optional_products', vm_, __opts__, default=[]\n )\n for product in optional_products:\n kwargs['prices'].append({'id': product})\n\n # Default is 273 (100 Mbps Public & Private Networks)\n port_speed = config.get_cloud_config_value(\n 'port_speed', vm_, __opts__, default=273\n )\n kwargs['prices'].append({'id': port_speed})\n\n # Default is 1800 (0 GB Bandwidth)\n bandwidth = config.get_cloud_config_value(\n 'bandwidth', vm_, __opts__, default=1800\n )\n kwargs['prices'].append({'id': bandwidth})\n\n post_uri = config.get_cloud_config_value(\n 'post_uri', vm_, __opts__, default=None\n )\n if post_uri:\n kwargs['prices'].append({'id': post_uri})\n\n vlan_id = config.get_cloud_config_value(\n 'vlan', vm_, __opts__, default=False\n )\n if vlan_id:\n kwargs['primaryNetworkComponent'] = {\n 'networkVlan': {\n 'id': vlan_id,\n }\n }\n\n location = get_location(vm_)\n if location:\n kwargs['location'] = location\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(name),\n args={\n 'kwargs': __utils__['cloud.filter_event']('requesting', kwargs, list(kwargs)),\n },\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n try:\n response = conn.placeOrder(kwargs)\n # Leaving the following line in, commented, for easy debugging\n #response = conn.verifyOrder(kwargs)\n except Exception as exc:\n log.error(\n 'Error creating %s on SoftLayer\\n\\n'\n 'The following exception was thrown when trying to '\n 'run the initial deployment: \\n%s', name, exc,\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n return False\n\n def wait_for_ip():\n '''\n Wait for the IP address to become available\n '''\n nodes = list_nodes_full()\n if 'primaryIpAddress' in nodes[hostname]:\n return nodes[hostname]['primaryIpAddress']\n time.sleep(1)\n return False\n\n ip_address = salt.utils.cloud.wait_for_fun(\n wait_for_ip,\n timeout=config.get_cloud_config_value(\n 'wait_for_fun_timeout', vm_, __opts__, default=15 * 60),\n )\n\n ssh_connect_timeout = config.get_cloud_config_value(\n # 15 minutes\n 'ssh_connect_timeout', vm_, __opts__, 900\n )\n if not salt.utils.cloud.wait_for_port(ip_address,\n timeout=ssh_connect_timeout):\n raise SaltCloudSystemExit(\n 'Failed to authenticate against remote ssh'\n )\n\n pass_conn = get_conn(service='SoftLayer_Account')\n mask = {\n 'virtualGuests': {\n 'powerState': '',\n 'operatingSystem': {\n 'passwords': ''\n },\n },\n }\n\n def get_passwd():\n '''\n Wait for the password to become available\n '''\n node_info = pass_conn.getVirtualGuests(id=response['id'], mask=mask)\n for node in node_info:\n if node['id'] == response['id'] \\\n and 'passwords' in node['operatingSystem'] \\\n and node['operatingSystem']['passwords']:\n return node['operatingSystem']['passwords'][0]['password']\n time.sleep(5)\n return False\n\n passwd = salt.utils.cloud.wait_for_fun(\n get_passwd,\n timeout=config.get_cloud_config_value(\n 'wait_for_fun_timeout', vm_, __opts__, default=15 * 60),\n )\n response['password'] = passwd\n response['public_ip'] = ip_address\n\n ssh_username = config.get_cloud_config_value(\n 'ssh_username', vm_, __opts__, default='root'\n )\n\n vm_['ssh_host'] = ip_address\n vm_['password'] = passwd\n ret = __utils__['cloud.bootstrap'](vm_, __opts__)\n\n ret.update(response)\n\n __utils__['cloud.fire_event'](\n 'event',\n 'created instance',\n 'salt/cloud/{0}/created'.format(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"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_nodes_full(mask='mask[id, hostname, primaryIpAddress, \\\n primaryBackendIpAddress, processorPhysicalCoreAmount, memoryCount]',\n call=None):\n '''\n Return a list of the VMs that are on the provider\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes_full function must be called with -f or --function.'\n )\n\n ret = {}\n conn = get_conn(service='SoftLayer_Account')\n response = conn.getHardware(mask=mask)\n\n for node in response:\n ret[node['hostname']] = node\n __utils__['cloud.cache_node_list'](ret, __active_provider_name__.split(':')[0], __opts__)\n return ret", "response": "Return a list of the VMs that are on the provider"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nshowing the unofficial pricing for a particular profile", "response": "def show_pricing(kwargs=None, call=None):\n '''\n Show pricing for a particular profile. This is only an estimate, based on\n unofficial pricing sources.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f show_pricing my-softlayerhw-config profile=my-profile\n\n If pricing sources have not been cached, they will be downloaded. Once they\n have been cached, they will not be updated automatically. To manually update\n all prices, use the following command:\n\n .. code-block:: bash\n\n salt-cloud -f update_pricing <provider>\n\n .. versionadded:: 2015.8.0\n '''\n profile = __opts__['profiles'].get(kwargs['profile'], {})\n if not profile:\n return {'Error': 'The requested profile was not found'}\n\n # Make sure the profile belongs to Softlayer HW\n provider = profile.get('provider', '0:0')\n comps = provider.split(':')\n if len(comps) < 2 or comps[1] != 'softlayer_hw':\n return {'Error': 'The requested profile does not belong to Softlayer HW'}\n\n raw = {}\n ret = {}\n ret['per_hour'] = 0\n conn = get_conn(service='SoftLayer_Product_Item_Price')\n for item in profile:\n if item in ('profile', 'provider', 'location'):\n continue\n price = conn.getObject(id=profile[item])\n raw[item] = price\n ret['per_hour'] += decimal.Decimal(price.get('hourlyRecurringFee', 0))\n\n ret['per_day'] = ret['per_hour'] * 24\n ret['per_week'] = ret['per_day'] * 7\n ret['per_month'] = ret['per_day'] * 30\n ret['per_year'] = ret['per_week'] * 52\n\n if kwargs.get('raw', False):\n ret['_raw'] = raw\n\n return {profile['profile']: ret}"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a dict of all prices on the cloud provider.", "response": "def show_all_prices(call=None, kwargs=None):\n '''\n Return a dict of all prices on the cloud provider.\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The show_all_prices function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n conn = get_conn(service='SoftLayer_Product_Package')\n if 'code' not in kwargs:\n return conn.getCategories(id=50)\n\n ret = {}\n for category in conn.getCategories(id=50):\n if category['categoryCode'] != kwargs['code']:\n continue\n for group in category['groups']:\n for price in group['prices']:\n ret[price['id']] = price['item'].copy()\n del ret[price['id']]['id']\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef show_all_categories(call=None):\n '''\n Return a dict of all available categories on the cloud provider.\n\n .. versionadded:: 2016.3.0\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The show_all_categories function must be called with -f or --function.'\n )\n\n conn = get_conn(service='SoftLayer_Product_Package')\n categories = []\n\n for category in conn.getCategories(id=50):\n categories.append(category['categoryCode'])\n\n return {'category_codes': categories}", "response": "Return a dict of all available categories on the cloud provider."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _connect(**kwargs):\n '''\n wrap authentication credentials here\n '''\n connargs = dict()\n\n def _connarg(name, key=None, get_opts=True):\n '''\n Add key to connargs, only if name exists in our kwargs or,\n if get_opts is true, as mysql.<name> in __opts__ or __pillar__\n\n If get_opts is true, evaluate in said order - kwargs, opts\n then pillar. To avoid collision with other functions,\n kwargs-based connection arguments are prefixed with 'connection_'\n (i.e. 'connection_host', 'connection_user', etc.).\n '''\n if key is None:\n key = name\n\n if name in kwargs:\n connargs[key] = kwargs[name]\n elif get_opts:\n prefix = 'connection_'\n if name.startswith(prefix):\n try:\n name = name[len(prefix):]\n except IndexError:\n return\n val = __salt__['config.option']('mysql.{0}'.format(name), None)\n if val is not None:\n connargs[key] = val\n\n # If a default file is explicitly passed to kwargs, don't grab the\n # opts/pillar settings, as it can override info in the defaults file\n if 'connection_default_file' in kwargs:\n get_opts = False\n else:\n get_opts = True\n\n _connarg('connection_host', 'host', get_opts)\n _connarg('connection_user', 'user', get_opts)\n _connarg('connection_pass', 'passwd', get_opts)\n _connarg('connection_port', 'port', get_opts)\n _connarg('connection_db', 'db', get_opts)\n _connarg('connection_conv', 'conv', get_opts)\n _connarg('connection_unix_socket', 'unix_socket', get_opts)\n _connarg('connection_default_file', 'read_default_file', get_opts)\n _connarg('connection_default_group', 'read_default_group', get_opts)\n # MySQLdb states that this is required for charset usage\n # but in fact it's more than it's internally activated\n # when charset is used, activating use_unicode here would\n # retrieve utf8 strings as unicode() objects in salt\n # and we do not want that.\n #_connarg('connection_use_unicode', 'use_unicode')\n connargs['use_unicode'] = False\n _connarg('connection_charset', 'charset')\n # Ensure MySQldb knows the format we use for queries with arguments\n MySQLdb.paramstyle = 'pyformat'\n\n if connargs.get('passwd', True) is None: # If present but set to None. (Extreme edge case.)\n log.warning('MySQL password of None found. Attempting passwordless login.')\n connargs.pop('passwd')\n try:\n dbc = MySQLdb.connect(**connargs)\n except OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return None\n\n dbc.autocommit(True)\n return dbc", "response": "Connect to the MySQL server"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef quote_identifier(identifier, for_grants=False):\n r'''\n Return an identifier name (column, table, database, etc) escaped for MySQL\n\n This means surrounded by \"`\" character and escaping this character inside.\n It also means doubling the '%' character for MySQLdb internal usage.\n\n :param identifier: the table, column or database identifier\n\n :param for_grants: is False by default, when using database names on grant\n queries you should set it to True to also escape \"_\" and \"%\" characters as\n requested by MySQL. Note that theses characters should only be escaped when\n requesting grants on the database level (`my\\_\\%db`.*) but not for table\n level grants (`my_%db`.`foo`)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.quote_identifier 'foo`bar'\n '''\n if for_grants:\n return '`' + identifier.replace('`', '``').replace('_', r'\\_') \\\n .replace('%', r'%%') + '`'\n else:\n return '`' + identifier.replace('`', '``').replace('%', '%%') + '`'", "response": "r Quote an identifier for MySQLdb internal usage."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nrunning an arbitrary SQL query and return the results.", "response": "def query(database, query, **connection_args):\n '''\n Run an arbitrary SQL query and return the results or\n the number of affected rows.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.query mydb \"UPDATE mytable set myfield=1 limit 1\"\n\n Return data:\n\n .. code-block:: python\n\n {'query time': {'human': '39.0ms', 'raw': '0.03899'}, 'rows affected': 1L}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.query mydb \"SELECT id,name,cash from users limit 3\"\n\n Return data:\n\n .. code-block:: python\n\n {'columns': ('id', 'name', 'cash'),\n 'query time': {'human': '1.0ms', 'raw': '0.001'},\n 'results': ((1L, 'User 1', Decimal('110.000000')),\n (2L, 'User 2', Decimal('215.636756')),\n (3L, 'User 3', Decimal('0.040000'))),\n 'rows returned': 3L}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.query mydb 'INSERT into users values (null,\"user 4\", 5)'\n\n Return data:\n\n .. code-block:: python\n\n {'query time': {'human': '25.6ms', 'raw': '0.02563'}, 'rows affected': 1L}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.query mydb 'DELETE from users where id = 4 limit 1'\n\n Return data:\n\n .. code-block:: python\n\n {'query time': {'human': '39.0ms', 'raw': '0.03899'}, 'rows affected': 1L}\n\n Jinja Example: Run a query on ``mydb`` and use row 0, column 0's data.\n\n .. code-block:: jinja\n\n {{ salt['mysql.query']('mydb', 'SELECT info from mytable limit 1')['results'][0][0] }}\n '''\n # Doesn't do anything about sql warnings, e.g. empty values on an insert.\n # I don't think it handles multiple queries at once, so adding \"commit\"\n # might not work.\n\n # The following 3 lines stops MySQLdb from converting the MySQL results\n # into Python objects. It leaves them as strings.\n orig_conv = MySQLdb.converters.conversions\n conv_iter = iter(orig_conv)\n conv = dict(zip(conv_iter, [str] * len(orig_conv)))\n\n # some converters are lists, do not break theses\n conv_mysqldb = {'MYSQLDB': True}\n if conv_mysqldb.get(MySQLdb.__package__.upper()):\n conv[FIELD_TYPE.BLOB] = [\n (FLAG.BINARY, str),\n ]\n conv[FIELD_TYPE.STRING] = [\n (FLAG.BINARY, str),\n ]\n conv[FIELD_TYPE.VAR_STRING] = [\n (FLAG.BINARY, str),\n ]\n conv[FIELD_TYPE.VARCHAR] = [\n (FLAG.BINARY, str),\n ]\n\n connection_args.update({'connection_db': database, 'connection_conv': conv})\n dbc = _connect(**connection_args)\n if dbc is None:\n return {}\n cur = dbc.cursor()\n start = time.time()\n log.debug('Using db: %s to run query %s', database, query)\n try:\n affected = _execute(cur, query)\n except OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n results = cur.fetchall()\n elapsed = (time.time() - start)\n if elapsed < 0.200:\n elapsed_h = str(round(elapsed * 1000, 1)) + 'ms'\n else:\n elapsed_h = str(round(elapsed, 2)) + 's'\n\n ret = {}\n ret['query time'] = {'human': elapsed_h, 'raw': str(round(elapsed, 5))}\n select_keywords = [\"SELECT\", \"SHOW\", \"DESC\"]\n select_query = False\n for keyword in select_keywords:\n if query.upper().strip().startswith(keyword):\n select_query = True\n break\n if select_query:\n ret['rows returned'] = affected\n columns = ()\n for column in cur.description:\n columns += (column[0],)\n ret['columns'] = columns\n ret['results'] = results\n return ret\n else:\n ret['rows affected'] = affected\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef file_query(database, file_name, **connection_args):\n '''\n Run an arbitrary SQL query from the specified file and return the\n the number of affected rows.\n\n .. versionadded:: 2017.7.0\n\n database\n\n database to run script inside\n\n file_name\n\n File name of the script. This can be on the minion, or a file that is reachable by the fileserver\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.file_query mydb file_name=/tmp/sqlfile.sql\n salt '*' mysql.file_query mydb file_name=salt://sqlfile.sql\n\n Return data:\n\n .. code-block:: python\n\n {'query time': {'human': '39.0ms', 'raw': '0.03899'}, 'rows affected': 1L}\n\n '''\n if any(file_name.startswith(proto) for proto in ('salt://', 'http://', 'https://', 'swift://', 's3://')):\n file_name = __salt__['cp.cache_file'](file_name)\n\n if os.path.exists(file_name):\n with salt.utils.files.fopen(file_name, 'r') as ifile:\n contents = salt.utils.stringutils.to_unicode(ifile.read())\n else:\n log.error('File \"%s\" does not exist', file_name)\n return False\n\n query_string = \"\"\n ret = {'rows returned': 0, 'columns': [], 'results': [], 'rows affected': 0, 'query time': {'raw': 0}}\n for line in contents.splitlines():\n if re.match(r'--', line): # ignore sql comments\n continue\n if not re.search(r'[^-;]+;', line): # keep appending lines that don't end in ;\n query_string = query_string + line\n else:\n query_string = query_string + line # append lines that end with ; and run query\n query_result = query(database, query_string, **connection_args)\n query_string = \"\"\n\n if query_result is False:\n # Fail out on error\n return False\n\n if 'query time' in query_result:\n ret['query time']['raw'] += float(query_result['query time']['raw'])\n if 'rows returned' in query_result:\n ret['rows returned'] += query_result['rows returned']\n if 'columns' in query_result:\n ret['columns'].append(query_result['columns'])\n if 'results' in query_result:\n ret['results'].append(query_result['results'])\n if 'rows affected' in query_result:\n ret['rows affected'] += query_result['rows affected']\n ret['query time']['human'] = six.text_type(round(float(ret['query time']['raw']), 2)) + 's'\n ret['query time']['raw'] = round(float(ret['query time']['raw']), 5)\n\n # Remove empty keys in ret\n ret = {k: v for k, v in six.iteritems(ret) if v}\n\n return ret", "response": "Run an arbitrary SQL query from the specified file and return the number of affected rows."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef status(**connection_args):\n '''\n Return the status of a MySQL server using the output from the ``SHOW\n STATUS`` query.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.status\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return {}\n cur = dbc.cursor()\n qry = 'SHOW STATUS'\n try:\n _execute(cur, qry)\n except OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return {}\n\n ret = {}\n for _ in range(cur.rowcount):\n row = cur.fetchone()\n ret[row[0]] = row[1]\n return ret", "response": "Return the status of a MySQL server"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning the version of a MySQL server.", "response": "def version(**connection_args):\n '''\n Return the version of a MySQL server using the output from the ``SELECT\n VERSION()`` query.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.version\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return ''\n cur = dbc.cursor()\n qry = 'SELECT VERSION()'\n try:\n _execute(cur, qry)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return ''\n\n try:\n return salt.utils.data.decode(cur.fetchone()[0])\n except IndexError:\n return ''"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef slave_lag(**connection_args):\n '''\n Return the number of seconds that a slave SQL server is lagging behind the\n master, if the host is not a slave it will return -1. If the server is\n configured to be a slave for replication but slave IO is not running then\n -2 will be returned. If there was an error connecting to the database or\n checking the slave status, -3 will be returned.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.slave_lag\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return -3\n cur = dbc.cursor(MySQLdb.cursors.DictCursor)\n qry = 'show slave status'\n try:\n _execute(cur, qry)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return -3\n\n results = cur.fetchone()\n if cur.rowcount == 0:\n # Server is not a slave if master is not defined. Return empty tuple\n # in this case. Could probably check to see if Slave_IO_Running and\n # Slave_SQL_Running are both set to 'Yes' as well to be really really\n # sure that it is a slave.\n return -1\n else:\n if results['Slave_IO_Running'] == 'Yes':\n return results['Seconds_Behind_Master']\n else:\n # Replication is broken if you get here.\n return -2", "response": "Return the number of seconds that a slave SQL server is lagging behind the master."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef free_slave(**connection_args):\n '''\n Frees a slave from its master. This is a WIP, do not use.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.free_slave\n '''\n slave_db = _connect(**connection_args)\n if slave_db is None:\n return ''\n slave_cur = slave_db.cursor(MySQLdb.cursors.DictCursor)\n slave_cur.execute('show slave status')\n slave_status = slave_cur.fetchone()\n master = {'host': slave_status['Master_Host']}\n\n try:\n # Try to connect to the master and flush logs before promoting to\n # master. This may fail if the master is no longer available.\n # I am also assuming that the admin password is the same on both\n # servers here, and only overriding the host option in the connect\n # function.\n master_db = _connect(**master)\n if master_db is None:\n return ''\n master_cur = master_db.cursor()\n master_cur.execute('flush logs')\n master_db.close()\n except MySQLdb.OperationalError:\n pass\n\n slave_cur.execute('stop slave')\n slave_cur.execute('reset master')\n slave_cur.execute('change master to MASTER_HOST=''')\n slave_cur.execute('show slave status')\n results = slave_cur.fetchone()\n\n if results is None:\n return 'promoted'\n else:\n return 'failed'", "response": "Free a slave from its master."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of databases of a MySQL server using the output from the SHOW DATABASES query.", "response": "def db_list(**connection_args):\n '''\n Return a list of databases of a MySQL server using the output\n from the ``SHOW DATABASES`` query.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.db_list\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return []\n cur = dbc.cursor()\n qry = 'SHOW DATABASES'\n try:\n _execute(cur, qry)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return []\n\n ret = []\n results = cur.fetchall()\n for dbs in results:\n ret.append(dbs[0])\n\n log.debug(ret)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef alter_db(name, character_set=None, collate=None, **connection_args):\n '''\n Modify database using ``ALTER DATABASE %(dbname)s CHARACTER SET %(charset)s\n COLLATE %(collation)s;`` query.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.alter_db testdb charset='latin1'\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return []\n cur = dbc.cursor()\n existing = db_get(name, **connection_args)\n qry = 'ALTER DATABASE `{0}` CHARACTER SET {1} COLLATE {2};'.format(\n name.replace('%', r'\\%').replace('_', r'\\_'),\n character_set or existing.get('character_set'),\n collate or existing.get('collate'))\n args = {}\n _execute(cur, qry, args)", "response": "Modify database using mysql. alter_db"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of databases of a MySQL server using the output from the MySQL server.", "response": "def db_get(name, **connection_args):\n '''\n Return a list of databases of a MySQL server using the output\n from the ``SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM\n INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='dbname';`` query.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.db_get test\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return []\n cur = dbc.cursor()\n qry = ('SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM '\n 'INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=%(dbname)s;')\n args = {\"dbname\": name}\n _execute(cur, qry, args)\n if cur.rowcount:\n rows = cur.fetchall()\n return {'character_set': rows[0][0],\n 'collate': rows[0][1]}\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef db_tables(name, **connection_args):\n '''\n Shows the tables in the given MySQL database (if exists)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.db_tables 'database'\n '''\n if not db_exists(name, **connection_args):\n log.info('Database \\'%s\\' does not exist', name)\n return False\n\n dbc = _connect(**connection_args)\n if dbc is None:\n return []\n cur = dbc.cursor()\n s_name = quote_identifier(name)\n # identifiers cannot be used as values\n qry = 'SHOW TABLES IN {0}'.format(s_name)\n try:\n _execute(cur, qry)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return []\n\n ret = []\n results = cur.fetchall()\n for table in results:\n ret.append(table[0])\n log.debug(ret)\n return ret", "response": "Show the tables in a MySQL database."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nchecking if a MySQL database exists on the MySQL server.", "response": "def db_exists(name, **connection_args):\n '''\n Checks if a database exists on the MySQL server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.db_exists 'dbname'\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n cur = dbc.cursor()\n # Warn: here db identifier is not backtyped but should be\n # escaped as a string value. Note also that LIKE special characters\n # '_' and '%' should also be escaped.\n args = {\"dbname\": name}\n qry = \"SHOW DATABASES LIKE %(dbname)s;\"\n try:\n _execute(cur, qry, args)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n cur.fetchall()\n return cur.rowcount == 1"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a new MySQL database.", "response": "def db_create(name, character_set=None, collate=None, **connection_args):\n '''\n Adds a databases to the MySQL server.\n\n name\n The name of the database to manage\n\n character_set\n The character set, if left empty the MySQL default will be used\n\n collate\n The collation, if left empty the MySQL default will be used\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.db_create 'dbname'\n salt '*' mysql.db_create 'dbname' 'utf8' 'utf8_general_ci'\n '''\n # check if db exists\n if db_exists(name, **connection_args):\n log.info('DB \\'%s\\' already exists', name)\n return False\n\n # db doesn't exist, proceed\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n cur = dbc.cursor()\n s_name = quote_identifier(name)\n # identifiers cannot be used as values\n qry = 'CREATE DATABASE IF NOT EXISTS {0}'.format(s_name)\n args = {}\n if character_set is not None:\n qry += ' CHARACTER SET %(character_set)s'\n args['character_set'] = character_set\n if collate is not None:\n qry += ' COLLATE %(collate)s'\n args['collate'] = collate\n qry += ';'\n\n try:\n if _execute(cur, qry, args):\n log.info('DB \\'%s\\' created', name)\n return True\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nremoving a MySQL database from the MySQL server.", "response": "def db_remove(name, **connection_args):\n '''\n Removes a databases from the MySQL server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.db_remove 'dbname'\n '''\n # check if db exists\n if not db_exists(name, **connection_args):\n log.info('DB \\'%s\\' does not exist', name)\n return False\n\n if name in ('mysql', 'information_scheme'):\n log.info('DB \\'%s\\' may not be removed', name)\n return False\n\n # db does exists, proceed\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n cur = dbc.cursor()\n s_name = quote_identifier(name)\n # identifiers cannot be used as values\n qry = 'DROP DATABASE {0};'.format(s_name)\n try:\n _execute(cur, qry)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n\n if not db_exists(name, **connection_args):\n log.info('Database \\'%s\\' has been removed', name)\n return True\n\n log.info('Database \\'%s\\' has not been removed', name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef user_list(**connection_args):\n '''\n Return a list of users on a MySQL server\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.user_list\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return []\n cur = dbc.cursor(MySQLdb.cursors.DictCursor)\n try:\n qry = 'SELECT User,Host FROM mysql.user'\n _execute(cur, qry)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return []\n results = cur.fetchall()\n log.debug(results)\n return results", "response": "Return a list of users on a MySQL server"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef user_exists(user,\n host='localhost',\n password=None,\n password_hash=None,\n passwordless=False,\n unix_socket=False,\n password_column=None,\n **connection_args):\n '''\n Checks if a user exists on the MySQL server. A login can be checked to see\n if passwordless login is permitted by omitting ``password`` and\n ``password_hash``, and using ``passwordless=True``.\n\n .. versionadded:: 0.16.2\n The ``passwordless`` option was added.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.user_exists 'username' 'hostname' 'password'\n salt '*' mysql.user_exists 'username' 'hostname' password_hash='hash'\n salt '*' mysql.user_exists 'username' passwordless=True\n salt '*' mysql.user_exists 'username' password_column='authentication_string'\n '''\n run_verify = False\n server_version = salt.utils.data.decode(version(**connection_args))\n if not server_version:\n last_err = __context__['mysql.error']\n err = 'MySQL Error: Unable to fetch current server version. Last error was: \"{}\"'.format(last_err)\n log.error(err)\n return False\n compare_version = '10.2.0' if 'MariaDB' in server_version else '8.0.11'\n dbc = _connect(**connection_args)\n # Did we fail to connect with the user we are checking\n # Its password might have previously change with the same command/state\n if dbc is None \\\n and __context__['mysql.error'] \\\n .startswith(\"MySQL Error 1045: Access denied for user '{0}'@\".format(user)) \\\n and password:\n # Clear the previous error\n __context__['mysql.error'] = None\n connection_args['connection_pass'] = password\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n\n if not password_column:\n password_column = __password_column(**connection_args)\n\n cur = dbc.cursor()\n qry = ('SELECT User,Host FROM mysql.user WHERE User = %(user)s AND '\n 'Host = %(host)s')\n args = {}\n args['user'] = user\n args['host'] = host\n\n if salt.utils.data.is_true(passwordless):\n if salt.utils.data.is_true(unix_socket):\n qry += ' AND plugin=%(unix_socket)s'\n args['unix_socket'] = 'unix_socket'\n else:\n qry += ' AND ' + password_column + ' = \\'\\''\n elif password:\n if salt.utils.versions.version_cmp(server_version, compare_version) >= 0:\n run_verify = True\n else:\n _password = password\n qry += ' AND ' + password_column + ' = PASSWORD(%(password)s)'\n args['password'] = six.text_type(_password)\n elif password_hash:\n qry += ' AND ' + password_column + ' = %(password)s'\n args['password'] = password_hash\n\n if run_verify:\n if not verify_login(user, password, **connection_args):\n return False\n try:\n _execute(cur, qry, args)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n\n return cur.rowcount == 1", "response": "Check if a user exists on the MySQL server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef user_info(user, host='localhost', **connection_args):\n '''\n Get full info on a MySQL user\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.user_info root localhost\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n\n cur = dbc.cursor(MySQLdb.cursors.DictCursor)\n qry = ('SELECT * FROM mysql.user WHERE User = %(user)s AND '\n 'Host = %(host)s')\n args = {}\n args['user'] = user\n args['host'] = host\n\n try:\n _execute(cur, qry, args)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n result = cur.fetchone()\n log.debug(result)\n return result", "response": "Return full info on a MySQL user"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ncreates a MySQL user and return a user record.", "response": "def user_create(user,\n host='localhost',\n password=None,\n password_hash=None,\n allow_passwordless=False,\n unix_socket=False,\n password_column=None,\n **connection_args):\n '''\n Creates a MySQL user\n\n host\n Host for which this user/password combo applies\n\n password\n The password to use for the new user. Will take precedence over the\n ``password_hash`` option if both are specified.\n\n password_hash\n The password in hashed form. Be sure to quote the password because YAML\n doesn't like the ``*``. A password hash can be obtained from the mysql\n command-line client like so::\n\n mysql> SELECT PASSWORD('mypass');\n +-------------------------------------------+\n | PASSWORD('mypass') |\n +-------------------------------------------+\n | *6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4 |\n +-------------------------------------------+\n 1 row in set (0.00 sec)\n\n allow_passwordless\n If ``True``, then ``password`` and ``password_hash`` can be omitted (or\n set to ``None``) to permit a passwordless login.\n\n unix_socket\n If ``True`` and allow_passwordless is ``True`` then will be used unix_socket auth plugin.\n\n .. versionadded:: 0.16.2\n The ``allow_passwordless`` option was added.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' mysql.user_create 'username' 'hostname' 'password'\n salt '*' mysql.user_create 'username' 'hostname' password_hash='hash'\n salt '*' mysql.user_create 'username' 'hostname' allow_passwordless=True\n '''\n server_version = salt.utils.data.decode(version(**connection_args))\n if not server_version:\n last_err = __context__['mysql.error']\n err = 'MySQL Error: Unable to fetch current server version. Last error was: \"{}\"'.format(last_err)\n log.error(err)\n return False\n compare_version = '10.2.0' if 'MariaDB' in server_version else '8.0.11'\n if user_exists(user, host, **connection_args):\n log.info('User \\'%s\\'@\\'%s\\' already exists', user, host)\n return False\n\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n\n if not password_column:\n password_column = __password_column(**connection_args)\n\n cur = dbc.cursor()\n qry = 'CREATE USER %(user)s@%(host)s'\n args = {}\n args['user'] = user\n args['host'] = host\n if password is not None:\n qry += ' IDENTIFIED BY %(password)s'\n args['password'] = six.text_type(password)\n elif password_hash is not None:\n if salt.utils.versions.version_cmp(server_version, compare_version) >= 0:\n qry += ' IDENTIFIED BY %(password)s'\n else:\n qry += ' IDENTIFIED BY PASSWORD %(password)s'\n args['password'] = password_hash\n elif salt.utils.data.is_true(allow_passwordless):\n if salt.utils.data.is_true(unix_socket):\n if host == 'localhost':\n qry += ' IDENTIFIED VIA unix_socket'\n else:\n log.error(\n 'Auth via unix_socket can be set only for host=localhost'\n )\n else:\n log.error('password or password_hash must be specified, unless '\n 'allow_passwordless=True')\n return False\n\n try:\n _execute(cur, qry, args)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n\n if user_exists(user, host, password, password_hash, password_column=password_column, **connection_args):\n msg = 'User \\'{0}\\'@\\'{1}\\' has been created'.format(user, host)\n if not any((password, password_hash)):\n msg += ' with passwordless login'\n log.info(msg)\n return True\n\n log.info('User \\'%s\\'@\\'%s\\' was not created', user, host)\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef user_chpass(user,\n host='localhost',\n password=None,\n password_hash=None,\n allow_passwordless=False,\n unix_socket=None,\n password_column=None,\n **connection_args):\n '''\n Change password for a MySQL user\n\n host\n Host for which this user/password combo applies\n\n password\n The password to set for the new user. Will take precedence over the\n ``password_hash`` option if both are specified.\n\n password_hash\n The password in hashed form. Be sure to quote the password because YAML\n doesn't like the ``*``. A password hash can be obtained from the mysql\n command-line client like so::\n\n mysql> SELECT PASSWORD('mypass');\n +-------------------------------------------+\n | PASSWORD('mypass') |\n +-------------------------------------------+\n | *6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4 |\n +-------------------------------------------+\n 1 row in set (0.00 sec)\n\n allow_passwordless\n If ``True``, then ``password`` and ``password_hash`` can be omitted (or\n set to ``None``) to permit a passwordless login.\n\n .. versionadded:: 0.16.2\n The ``allow_passwordless`` option was added.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' mysql.user_chpass frank localhost newpassword\n salt '*' mysql.user_chpass frank localhost password_hash='hash'\n salt '*' mysql.user_chpass frank localhost allow_passwordless=True\n '''\n server_version = salt.utils.data.decode(version(**connection_args))\n if not server_version:\n last_err = __context__['mysql.error']\n err = 'MySQL Error: Unable to fetch current server version. Last error was: \"{}\"'.format(last_err)\n log.error(err)\n return False\n compare_version = '10.2.0' if 'MariaDB' in server_version else '8.0.11'\n args = {}\n if password is not None:\n if salt.utils.versions.version_cmp(server_version, compare_version) >= 0:\n password_sql = '%(password)s'\n else:\n password_sql = 'PASSWORD(%(password)s)'\n args['password'] = password\n elif password_hash is not None:\n password_sql = '%(password)s'\n args['password'] = password_hash\n elif not salt.utils.data.is_true(allow_passwordless):\n log.error('password or password_hash must be specified, unless '\n 'allow_passwordless=True')\n return False\n else:\n password_sql = '\\'\\''\n\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n\n if not password_column:\n password_column = __password_column(**connection_args)\n\n cur = dbc.cursor()\n args['user'] = user\n args['host'] = host\n if salt.utils.versions.version_cmp(server_version, compare_version) >= 0:\n qry = \"ALTER USER %(user)s@%(host)s IDENTIFIED BY %(password)s;\"\n else:\n qry = ('UPDATE mysql.user SET ' + password_column + '=' + password_sql +\n ' WHERE User=%(user)s AND Host = %(host)s;')\n if salt.utils.data.is_true(allow_passwordless) and \\\n salt.utils.data.is_true(unix_socket):\n if host == 'localhost':\n args['unix_socket'] = 'auth_socket'\n if salt.utils.versions.version_cmp(server_version, compare_version) >= 0:\n qry = \"ALTER USER %(user)s@%(host)s IDENTIFIED WITH %(unix_socket)s AS %(user)s;\"\n else:\n qry = ('UPDATE mysql.user SET ' + password_column + '='\n + password_sql + ', plugin=%(unix_socket)s' +\n ' WHERE User=%(user)s AND Host = %(host)s;')\n else:\n log.error('Auth via unix_socket can be set only for host=localhost')\n try:\n result = _execute(cur, qry, args)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n\n if salt.utils.versions.version_cmp(server_version, compare_version) >= 0:\n _execute(cur, 'FLUSH PRIVILEGES;')\n log.info(\n 'Password for user \\'%s\\'@\\'%s\\' has been %s',\n user, host,\n 'changed' if any((password, password_hash)) else 'cleared'\n )\n return True\n else:\n if result:\n _execute(cur, 'FLUSH PRIVILEGES;')\n log.info(\n 'Password for user \\'%s\\'@\\'%s\\' has been %s',\n user, host,\n 'changed' if any((password, password_hash)) else 'cleared'\n )\n return True\n\n log.info(\n 'Password for user \\'%s\\'@\\'%s\\' was not %s',\n user, host,\n 'changed' if any((password, password_hash)) else 'cleared'\n )\n return False", "response": "Change password for a MySQL user"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nremoving MySQL user from the cache.", "response": "def user_remove(user,\n host='localhost',\n **connection_args):\n '''\n Delete MySQL user\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.user_remove frank localhost\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n\n cur = dbc.cursor()\n qry = 'DROP USER %(user)s@%(host)s'\n args = {}\n args['user'] = user\n args['host'] = host\n try:\n _execute(cur, qry, args)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n\n if not user_exists(user, host, **connection_args):\n log.info('User \\'%s\\'@\\'%s\\' has been removed', user, host)\n return True\n\n log.info('User \\'%s\\'@\\'%s\\' has NOT been removed', user, host)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef db_check(name,\n table=None,\n **connection_args):\n '''\n Repairs the full database or just a given table\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.db_check dbname\n salt '*' mysql.db_check dbname dbtable\n '''\n ret = []\n if table is None:\n # we need to check all tables\n tables = db_tables(name, **connection_args)\n for table in tables:\n log.info('Checking table \\'%s\\' in db \\'%s\\'..', name, table)\n ret.append(__check_table(name, table, **connection_args))\n else:\n log.info('Checking table \\'%s\\' in db \\'%s\\'..', name, table)\n ret = __check_table(name, table, **connection_args)\n return ret", "response": "Check the database for tables in a given table."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nrepair the full database or just a given table", "response": "def db_repair(name,\n table=None,\n **connection_args):\n '''\n Repairs the full database or just a given table\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.db_repair dbname\n '''\n ret = []\n if table is None:\n # we need to repair all tables\n tables = db_tables(name, **connection_args)\n for table in tables:\n log.info('Repairing table \\'%s\\' in db \\'%s\\'..', name, table)\n ret.append(__repair_table(name, table, **connection_args))\n else:\n log.info('Repairing table \\'%s\\' in db \\'%s\\'..', name, table)\n ret = __repair_table(name, table, **connection_args)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\noptimizing the full database or just a given table", "response": "def db_optimize(name,\n table=None,\n **connection_args):\n '''\n Optimizes the full database or just a given table\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.db_optimize dbname\n '''\n ret = []\n if table is None:\n # we need to optimize all tables\n tables = db_tables(name, **connection_args)\n for table in tables:\n log.info('Optimizing table \\'%s\\' in db \\'%s\\'..', name, table)\n ret.append(__optimize_table(name, table, **connection_args))\n else:\n log.info('Optimizing table \\'%s\\' in db \\'%s\\'..', name, table)\n ret = __optimize_table(name, table, **connection_args)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngenerates a grant query that could set the given grants WorkItem", "response": "def __grant_generate(grant,\n database,\n user,\n host='localhost',\n grant_option=False,\n escape=True,\n ssl_option=False):\n '''\n Validate grants and build the query that could set the given grants\n\n Note that this query contains arguments for user and host but not for\n grants or database.\n '''\n # TODO: Re-order the grant so it is according to the\n # SHOW GRANTS for xxx@yyy query (SELECT comes first, etc)\n grant = re.sub(r'\\s*,\\s*', ', ', grant).upper()\n\n grant = __grant_normalize(grant)\n\n db_part = database.rpartition('.')\n dbc = db_part[0]\n table = db_part[2]\n\n if escape:\n if dbc != '*':\n # _ and % are authorized on GRANT queries and should get escaped\n # on the db name, but only if not requesting a table level grant\n dbc = quote_identifier(dbc, for_grants=(table == '*'))\n if table != '*':\n table = quote_identifier(table)\n # identifiers cannot be used as values, and same thing for grants\n qry = 'GRANT {0} ON {1}.{2} TO %(user)s@%(host)s'.format(grant, dbc, table)\n args = {}\n args['user'] = user\n args['host'] = host\n if ssl_option and isinstance(ssl_option, list):\n qry += __ssl_option_sanitize(ssl_option)\n if salt.utils.data.is_true(grant_option):\n qry += ' WITH GRANT OPTION'\n log.debug('Grant Query generated: %s args %s', qry, repr(args))\n return {'qry': qry, 'args': args}"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef user_grants(user,\n host='localhost', **connection_args):\n '''\n Shows the grants for the given MySQL user (if it exists)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.user_grants 'frank' 'localhost'\n '''\n if not user_exists(user, host, **connection_args):\n log.info('User \\'%s\\'@\\'%s\\' does not exist', user, host)\n return False\n\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n cur = dbc.cursor()\n qry = 'SHOW GRANTS FOR %(user)s@%(host)s'\n args = {}\n args['user'] = user\n args['host'] = host\n try:\n _execute(cur, qry, args)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n\n ret = []\n results = salt.utils.data.decode(cur.fetchall())\n for grant in results:\n tmp = grant[0].split(' IDENTIFIED BY')[0]\n if 'WITH GRANT OPTION' in grant[0] and 'WITH GRANT OPTION' not in tmp:\n tmp = '{0} WITH GRANT OPTION'.format(tmp)\n ret.append(tmp)\n log.debug(ret)\n return ret", "response": "Show the grants for a MySQL user"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nchecking to see if a grant exists in the database.", "response": "def grant_exists(grant,\n database,\n user,\n host='localhost',\n grant_option=False,\n escape=True,\n **connection_args):\n '''\n Checks to see if a grant exists in the database\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.grant_exists \\\n 'SELECT,INSERT,UPDATE,...' 'database.*' 'frank' 'localhost'\n '''\n\n server_version = salt.utils.data.decode(version(**connection_args))\n if not server_version:\n last_err = __context__['mysql.error']\n err = 'MySQL Error: Unable to fetch current server version. Last error was: \"{}\"'.format(last_err)\n log.error(err)\n return False\n if 'ALL' in grant:\n if salt.utils.versions.version_cmp(server_version, '8.0') >= 0 and \\\n 'MariaDB' not in server_version:\n grant = ','.join([i for i in __all_privileges__])\n else:\n grant = 'ALL PRIVILEGES'\n\n try:\n target = __grant_generate(\n grant, database, user, host, grant_option, escape\n )\n except Exception:\n log.error('Error during grant generation.')\n return False\n\n grants = user_grants(user, host, **connection_args)\n\n if grants is False:\n log.error('Grant does not exist or may not be ordered properly. In some cases, '\n 'this could also indicate a connection error. Check your configuration.')\n return False\n\n # Combine grants that match the same database\n _grants = {}\n for grant in grants:\n grant_token = _grant_to_tokens(grant)\n if grant_token['database'] not in _grants:\n _grants[grant_token['database']] = {'user': grant_token['user'],\n 'database': grant_token['database'],\n 'host': grant_token['host'],\n 'grant': grant_token['grant']}\n else:\n _grants[grant_token['database']]['grant'].extend(grant_token['grant'])\n\n target_tokens = _grant_to_tokens(target)\n for database, grant_tokens in _grants.items():\n try:\n _grant_tokens = {}\n _target_tokens = {}\n\n _grant_matches = [True if i in grant_tokens['grant']\n else False for i in target_tokens['grant']]\n\n for item in ['user', 'database', 'host']:\n _grant_tokens[item] = grant_tokens[item].replace('\"', '').replace('\\\\', '').replace('`', '')\n _target_tokens[item] = target_tokens[item].replace('\"', '').replace('\\\\', '').replace('`', '')\n\n if _grant_tokens['user'] == _target_tokens['user'] and \\\n _grant_tokens['database'] == _target_tokens['database'] and \\\n _grant_tokens['host'] == _target_tokens['host'] and \\\n all(_grant_matches):\n return True\n else:\n log.debug('grants mismatch \\'%s\\'<>\\'%s\\'', grant_tokens, target_tokens)\n\n except Exception as exc: # Fallback to strict parsing\n log.exception(exc)\n if grants is not False and target in grants:\n log.debug('Grant exists.')\n return True\n\n log.debug('Grant does not exist, or is perhaps not ordered properly?')\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef grant_add(grant,\n database,\n user,\n host='localhost',\n grant_option=False,\n escape=True,\n ssl_option=False,\n **connection_args):\n '''\n Adds a grant to the MySQL server.\n\n For database, make sure you specify database.table or database.*\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.grant_add \\\n 'SELECT,INSERT,UPDATE,...' 'database.*' 'frank' 'localhost'\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n cur = dbc.cursor()\n\n # Avoid spaces problems\n grant = grant.strip()\n try:\n qry = __grant_generate(grant, database, user, host, grant_option, escape, ssl_option)\n except Exception:\n log.error('Error during grant generation')\n return False\n try:\n _execute(cur, qry['qry'], qry['args'])\n except (MySQLdb.OperationalError, MySQLdb.ProgrammingError) as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n if grant_exists(\n grant, database, user, host, grant_option, escape,\n **connection_args):\n log.info(\n 'Grant \\'%s\\' on \\'%s\\' for user \\'%s\\' has been added',\n grant, database, user\n )\n return True\n\n log.info(\n 'Grant \\'%s\\' on \\'%s\\' for user \\'%s\\' has NOT been added',\n grant, database, user\n )\n return False", "response": "Add a grant to the MySQL server."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nremove a grant from the MySQL server.", "response": "def grant_revoke(grant,\n database,\n user,\n host='localhost',\n grant_option=False,\n escape=True,\n **connection_args):\n '''\n Removes a grant from the MySQL server.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.grant_revoke \\\n 'SELECT,INSERT,UPDATE' 'database.*' 'frank' 'localhost'\n '''\n dbc = _connect(**connection_args)\n if dbc is None:\n return False\n cur = dbc.cursor()\n\n grant = __grant_normalize(grant)\n\n if salt.utils.data.is_true(grant_option):\n grant += ', GRANT OPTION'\n\n db_part = database.rpartition('.')\n dbc = db_part[0]\n table = db_part[2]\n if dbc != '*':\n # _ and % are authorized on GRANT queries and should get escaped\n # on the db name, but only if not requesting a table level grant\n s_database = quote_identifier(dbc, for_grants=(table == '*'))\n if dbc == '*':\n # add revoke for *.*\n # before the modification query send to mysql will looks like\n # REVOKE SELECT ON `*`.* FROM %(user)s@%(host)s\n s_database = dbc\n if table != '*':\n table = quote_identifier(table)\n # identifiers cannot be used as values, same thing for grants\n qry = 'REVOKE {0} ON {1}.{2} FROM %(user)s@%(host)s;'.format(\n grant,\n s_database,\n table\n )\n args = {}\n args['user'] = user\n args['host'] = host\n\n try:\n _execute(cur, qry, args)\n except MySQLdb.OperationalError as exc:\n err = 'MySQL Error {0}: {1}'.format(*exc.args)\n __context__['mysql.error'] = err\n log.error(err)\n return False\n\n if not grant_exists(grant,\n database,\n user,\n host,\n grant_option,\n escape,\n **connection_args):\n log.info(\n 'Grant \\'%s\\' on \\'%s\\' for user \\'%s\\' has been '\n 'revoked', grant, database, user)\n return True\n\n log.info(\n 'Grant \\'%s\\' on \\'%s\\' for user \\'%s\\' has NOT been '\n 'revoked', grant, database, user)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef processlist(**connection_args):\n '''\n Retrieves the processlist from the MySQL server via\n \"SHOW FULL PROCESSLIST\".\n\n Returns: a list of dicts, with each dict representing a process:\n\n .. code-block:: python\n\n {'Command': 'Query',\n 'Host': 'localhost',\n 'Id': 39,\n 'Info': 'SHOW FULL PROCESSLIST',\n 'Rows_examined': 0,\n 'Rows_read': 1,\n 'Rows_sent': 0,\n 'State': None,\n 'Time': 0,\n 'User': 'root',\n 'db': 'mysql'}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.processlist\n\n '''\n ret = []\n\n dbc = _connect(**connection_args)\n if dbc is None:\n return []\n cur = dbc.cursor()\n _execute(cur, 'SHOW FULL PROCESSLIST')\n hdr = [c[0] for c in cur.description]\n for _ in range(cur.rowcount):\n row = cur.fetchone()\n idx_r = {}\n for idx_j in range(len(hdr)):\n idx_r[hdr[idx_j]] = row[idx_j]\n ret.append(idx_r)\n cur.close()\n return ret", "response": "Returns the processlist from the MySQL server via the SHOW FULL PROCESSLIST command."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nperforms the query that is passed to it and returns the results in a dict.", "response": "def __do_query_into_hash(conn, sql_str):\n '''\n Perform the query that is passed to it (sql_str).\n\n Returns:\n results in a dict.\n\n '''\n mod = sys._getframe().f_code.co_name\n log.debug('%s<--(%s)', mod, sql_str)\n\n rtn_results = []\n\n try:\n cursor = conn.cursor()\n except MySQLdb.MySQLError:\n log.error('%s: Can\\'t get cursor for SQL->%s', mod, sql_str)\n cursor.close()\n log.debug('%s-->', mod)\n return rtn_results\n\n try:\n _execute(cursor, sql_str)\n except MySQLdb.MySQLError:\n log.error('%s: try to execute : SQL->%s', mod, sql_str)\n cursor.close()\n log.debug('%s-->', mod)\n return rtn_results\n\n qrs = cursor.fetchall()\n\n for row_data in qrs:\n col_cnt = 0\n row = {}\n for col_data in cursor.description:\n col_name = col_data[0]\n row[col_name] = row_data[col_cnt]\n col_cnt += 1\n\n rtn_results.append(row)\n\n cursor.close()\n log.debug('%s-->', mod)\n return rtn_results"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets the master status from the minion.", "response": "def get_master_status(**connection_args):\n '''\n Retrieves the master status from the minion.\n\n Returns::\n\n {'host.domain.com': {'Binlog_Do_DB': '',\n 'Binlog_Ignore_DB': '',\n 'File': 'mysql-bin.000021',\n 'Position': 107}}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.get_master_status\n\n '''\n mod = sys._getframe().f_code.co_name\n log.debug('%s<--', mod)\n conn = _connect(**connection_args)\n if conn is None:\n return []\n rtnv = __do_query_into_hash(conn, \"SHOW MASTER STATUS\")\n conn.close()\n\n # check for if this minion is not a master\n if not rtnv:\n rtnv.append([])\n\n log.debug('%s-->%s', mod, len(rtnv[0]))\n return rtnv[0]"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef verify_login(user, password=None, **connection_args):\n '''\n Attempt to login using the provided credentials.\n If successful, return true. Otherwise, return False.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mysql.verify_login root password\n '''\n # Override the connection args for username and password\n connection_args['connection_user'] = user\n connection_args['connection_pass'] = password\n\n dbc = _connect(**connection_args)\n if dbc is None:\n # Clear the mysql.error if unable to connect\n # if the connection fails, we simply return False\n if 'mysql.error' in __context__:\n del __context__['mysql.error']\n return False\n return True", "response": "Verify that the user and password are valid."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nexecute a wheel function through the master network interface.", "response": "def master_call(self, **kwargs):\n '''\n Execute a wheel function through the master network interface (eauth).\n '''\n load = kwargs\n load['cmd'] = 'wheel'\n interface = self.opts['interface']\n if interface == '0.0.0.0':\n interface = '127.0.0.1'\n master_uri = 'tcp://{}:{}'.format(\n salt.utils.zeromq.ip_bracket(interface),\n six.text_type(self.opts['ret_port'])\n )\n channel = salt.transport.client.ReqChannel.factory(self.opts,\n crypt='clear',\n master_uri=master_uri,\n usage='master_call')\n try:\n ret = channel.send(load)\n finally:\n channel.close()\n if isinstance(ret, collections.Mapping):\n if 'error' in ret:\n salt.utils.error.raise_error(**ret['error'])\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nexecute a function asynchronously ; eauth is respected", "response": "def cmd_async(self, low):\n '''\n Execute a function asynchronously; eauth is respected\n\n This function requires that :conf_master:`external_auth` is configured\n and the user is authorized\n\n .. code-block:: python\n\n >>> wheel.cmd_async({\n 'fun': 'key.finger',\n 'match': 'jerry',\n 'eauth': 'auto',\n 'username': 'saltdev',\n 'password': 'saltdev',\n })\n {'jid': '20131219224744416681', 'tag': 'salt/wheel/20131219224744416681'}\n '''\n fun = low.pop('fun')\n return self.asynchronous(fun, low)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexecutes a function .. code-block:: python >>> wheel.cmd('key.finger', ['jerry']) {'minions': {'jerry': '5d:f6:79:43:5e:d4:42:3f:57:b8:45:a8:7e:a4:6e:ca'}}", "response": "def cmd(self, fun, arg=None, pub_data=None, kwarg=None, print_event=True, full_return=False):\n '''\n Execute a function\n\n .. code-block:: python\n\n >>> wheel.cmd('key.finger', ['jerry'])\n {'minions': {'jerry': '5d:f6:79:43:5e:d4:42:3f:57:b8:45:a8:7e:a4:6e:ca'}}\n '''\n return super(WheelClient, self).cmd(fun,\n arg,\n pub_data,\n kwarg,\n print_event,\n full_return)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _add_var(var, value):\n '''\n Add a new var to the make.conf. If using layman, the source line\n for the layman make.conf needs to be at the very end of the\n config. This ensures that the new var will be above the source\n line.\n '''\n makeconf = _get_makeconf()\n layman = 'source /var/lib/layman/make.conf'\n fullvar = '{0}=\"{1}\"'.format(var, value)\n if __salt__['file.contains'](makeconf, layman):\n # TODO perhaps make this a function in the file module?\n cmd = ['sed', '-i', r'/{0}/ i\\{1}'.format(\n layman.replace('/', '\\\\/'),\n fullvar),\n makeconf]\n __salt__['cmd.run'](cmd)\n else:\n __salt__['file.append'](makeconf, fullvar)", "response": "Add a new var to the make. conf"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsets a variable in the make. conf", "response": "def set_var(var, value):\n '''\n Set a variable in the make.conf\n\n Return a dict containing the new value for variable::\n\n {'<variable>': {'old': '<old-value>',\n 'new': '<new-value>'}}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' makeconf.set_var 'LINGUAS' 'en'\n '''\n makeconf = _get_makeconf()\n\n old_value = get_var(var)\n\n # If var already in file, replace its value\n if old_value is not None:\n __salt__['file.sed'](\n makeconf, '^{0}=.*'.format(var), '{0}=\"{1}\"'.format(var, value)\n )\n else:\n _add_var(var, value)\n\n new_value = get_var(var)\n return {var: {'old': old_value, 'new': new_value}}"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef remove_var(var):\n '''\n Remove a variable from the make.conf\n\n Return a dict containing the new value for the variable::\n\n {'<variable>': {'old': '<old-value>',\n 'new': '<new-value>'}}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' makeconf.remove_var 'LINGUAS'\n '''\n makeconf = _get_makeconf()\n\n old_value = get_var(var)\n\n # If var is in file\n if old_value is not None:\n __salt__['file.sed'](makeconf, '^{0}=.*'.format(var), '')\n\n new_value = get_var(var)\n return {var: {'old': old_value, 'new': new_value}}", "response": "Remove a variable from the make. conf\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef append_var(var, value):\n '''\n Add to or create a new variable in the make.conf\n\n Return a dict containing the new value for variable::\n\n {'<variable>': {'old': '<old-value>',\n 'new': '<new-value>'}}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' makeconf.append_var 'LINGUAS' 'en'\n '''\n makeconf = _get_makeconf()\n\n old_value = get_var(var)\n\n # If var already in file, add to its value\n if old_value is not None:\n appended_value = '{0} {1}'.format(old_value, value)\n __salt__['file.sed'](makeconf, '^{0}=.*'.format(var),\n '{0}=\"{1}\"'.format(var, appended_value))\n else:\n _add_var(var, value)\n\n new_value = get_var(var)\n return {var: {'old': old_value, 'new': new_value}}", "response": "Append a new variable to the make. conf file"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove a value from a variable in the make. conf", "response": "def trim_var(var, value):\n '''\n Remove a value from a variable in the make.conf\n\n Return a dict containing the new value for variable::\n\n {'<variable>': {'old': '<old-value>',\n 'new': '<new-value>'}}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' makeconf.trim_var 'LINGUAS' 'en'\n '''\n makeconf = _get_makeconf()\n\n old_value = get_var(var)\n\n # If var in file, trim value from its value\n if old_value is not None:\n __salt__['file.sed'](makeconf, value, '', limit=var)\n\n new_value = get_var(var)\n return {var: {'old': old_value, 'new': new_value}}"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the value of a variable in make. conf WorkItem", "response": "def get_var(var):\n '''\n Get the value of a variable in make.conf\n\n Return the value of the variable or None if the variable is not in\n make.conf\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' makeconf.get_var 'LINGUAS'\n '''\n makeconf = _get_makeconf()\n # Open makeconf\n with salt.utils.files.fopen(makeconf) as fn_:\n conf_file = salt.utils.data.decode(fn_.readlines())\n for line in conf_file:\n if line.startswith(var):\n ret = line.split('=', 1)[1]\n if '\"' in ret:\n ret = ret.split('\"')[1]\n elif '#' in ret:\n ret = ret.split('#')[0]\n ret = ret.strip()\n return ret\n return None"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef var_contains(var, value):\n '''\n Verify if variable contains a value in make.conf\n\n Return True if value is set for var\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' makeconf.var_contains 'LINGUAS' 'en'\n '''\n setval = get_var(var)\n # Remove any escaping that was needed to past through salt\n value = value.replace('\\\\', '')\n if setval is None:\n return False\n return value in setval.split()", "response": "Verify if a variable contains a value in make. conf\n Return True if the variable contains a value in make. conf\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _carbon(host, port):\n '''\n Context manager to ensure the clean creation and destruction of a socket.\n\n host\n The IP or hostname of the carbon server\n port\n The port that carbon is listening on\n '''\n carbon_sock = None\n\n try:\n carbon_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,\n socket.IPPROTO_TCP)\n\n carbon_sock.connect((host, port))\n except socket.error as err:\n log.error('Error connecting to %s:%s, %s', host, port, err)\n raise\n else:\n log.debug('Connected to carbon')\n yield carbon_sock\n finally:\n if carbon_sock is not None:\n # Shut down and close socket\n log.debug('Destroying carbon socket')\n\n carbon_sock.shutdown(socket.SHUT_RDWR)\n carbon_sock.close()", "response": "Context manager to ensure the creation and destruction of a socket."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nformats metrics for carbon pickle protocol", "response": "def _send_picklemetrics(metrics):\n '''\n Format metrics for the carbon pickle protocol\n '''\n\n metrics = [(metric_name, (timestamp, value))\n for (metric_name, value, timestamp) in metrics]\n\n data = cPickle.dumps(metrics, -1)\n payload = struct.pack(b'!L', len(data)) + data\n\n return payload"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _send_textmetrics(metrics):\n '''\n Format metrics for the carbon plaintext protocol\n '''\n\n data = [' '.join(map(six.text_type, metric)) for metric in metrics] + ['']\n\n return '\\n'.join(data)", "response": "Formats the metrics for the carbon plaintext protocol\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _walk(path, value, metrics, timestamp, skip):\n '''\n Recursively include metrics from *value*.\n\n path\n The dot-separated path of the metric.\n value\n A dictionary or value from a dictionary. If a dictionary, ``_walk``\n will be called again with the each key/value pair as a new set of\n metrics.\n metrics\n The list of metrics that will be sent to carbon, formatted as::\n\n (path, value, timestamp)\n skip\n Whether or not to skip metrics when there's an error casting the value\n to a float. Defaults to `False`.\n '''\n log.trace(\n 'Carbon return walking path: %s, value: %s, metrics: %s, '\n 'timestamp: %s', path, value, metrics, timestamp\n )\n if isinstance(value, collections.Mapping):\n for key, val in six.iteritems(value):\n _walk('{0}.{1}'.format(path, key), val, metrics, timestamp, skip)\n elif isinstance(value, list):\n for item in value:\n _walk('{0}.{1}'.format(path, item), item, metrics, timestamp, skip)\n\n else:\n try:\n val = float(value)\n metrics.append((path, val, timestamp))\n except (TypeError, ValueError):\n msg = 'Error in carbon returner, when trying to convert metric: ' \\\n '{0}, with val: {1}'.format(path, value)\n if skip:\n log.debug(msg)\n else:\n log.info(msg)\n raise", "response": "Recursively include metrics from value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _send(saltdata, metric_base, opts):\n '''\n Send the data to carbon\n '''\n\n host = opts.get('host')\n port = opts.get('port')\n skip = opts.get('skip')\n metric_base_pattern = opts.get('carbon.metric_base_pattern')\n mode = opts.get('mode').lower() if 'mode' in opts else 'text'\n\n log.debug('Carbon minion configured with host: %s:%s', host, port)\n log.debug('Using carbon protocol: %s', mode)\n\n if not (host and port):\n log.error('Host or port not defined')\n return\n\n # TODO: possible to use time return from salt job to be slightly more precise?\n # convert the jid to unix timestamp?\n # {'fun': 'test.version', 'jid': '20130113193949451054', 'return': '0.11.0', 'id': 'salt'}\n timestamp = int(time.time())\n\n handler = _send_picklemetrics if mode == 'pickle' else _send_textmetrics\n metrics = []\n log.trace('Carbon returning walking data: %s', saltdata)\n _walk(metric_base, saltdata, metrics, timestamp, skip)\n data = handler(metrics)\n log.trace('Carbon inserting data: %s', data)\n\n with _carbon(host, port) as sock:\n total_sent_bytes = 0\n while total_sent_bytes < len(data):\n sent_bytes = sock.send(data[total_sent_bytes:])\n if sent_bytes == 0:\n log.error('Bytes sent 0, Connection reset?')\n return\n\n log.debug('Sent %s bytes to carbon', sent_bytes)\n total_sent_bytes += sent_bytes", "response": "Send the data to a carbon object"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef event_return(events):\n '''\n Return event data to remote carbon server\n\n Provide a list of events to be stored in carbon\n '''\n opts = _get_options({}) # Pass in empty ret, since this is a list of events\n opts['skip'] = True\n for event in events:\n log.trace('Carbon returner received event: %s', event)\n metric_base = event['tag']\n saltdata = event['data'].get('data')\n _send(saltdata, metric_base, opts)", "response": "Return event data to remote carbon server"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef returner(ret):\n '''\n Return data to a remote carbon server using the text metric protocol\n\n Each metric will look like::\n\n [module].[function].[minion_id].[metric path [...]].[metric name]\n\n '''\n opts = _get_options(ret)\n metric_base = ret['fun']\n # Strip the hostname from the carbon base if we are returning from virt\n # module since then we will get stable metric bases even if the VM is\n # migrate from host to host\n if not metric_base.startswith('virt.'):\n metric_base += '.' + ret['id'].replace('.', '_')\n\n saltdata = ret['return']\n _send(saltdata, metric_base, opts)", "response": "Return data to a remote carbon server using the text metric protocol\n Each metric will look like ::\n [ module. function. minion_id. metric path. metric name."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncheck to see if a stack exists.", "response": "def exists(name, region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if a stack exists.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cfn.exists mystack region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n # Returns an object if stack exists else an exception\n exists = conn.describe_stacks(name)\n log.debug('Stack %s exists.', name)\n return True\n except BotoServerError as e:\n log.debug('boto_cfn.exists raised an exception', exc_info=True)\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef describe(name, region=None, key=None, keyid=None, profile=None):\n '''\n Describe a stack.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cfn.describe mystack region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n # Returns an object if stack exists else an exception\n r = conn.describe_stacks(name)\n if r:\n stack = r[0]\n log.debug('Found VPC: %s', stack.stack_id)\n keys = ('stack_id', 'description', 'stack_status', 'stack_status_reason', 'tags')\n\n ret = dict([(k, getattr(stack, k)) for k in keys if hasattr(stack, k)])\n o = getattr(stack, 'outputs')\n p = getattr(stack, 'parameters')\n outputs = {}\n parameters = {}\n for i in o:\n outputs[i.key] = i.value\n ret['outputs'] = outputs\n for j in p:\n parameters[j.key] = j.value\n ret['parameters'] = parameters\n\n return {'stack': ret}\n\n log.debug('Stack %s exists.', name)\n return True\n except BotoServerError as e:\n log.warning('Could not describe stack %s.\\n%s', name, e)\n return False", "response": "Describe a stack.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cfn.describe mystack region=us-east-1"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a CFN stack.", "response": "def create(name, template_body=None, template_url=None, parameters=None, notification_arns=None, disable_rollback=None,\n timeout_in_minutes=None, capabilities=None, tags=None, on_failure=None, stack_policy_body=None,\n stack_policy_url=None, region=None, key=None, keyid=None, profile=None):\n '''\n Create a CFN stack.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cfn.create mystack template_url='https://s3.amazonaws.com/bucket/template.cft' \\\n region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n return conn.create_stack(name, template_body, template_url, parameters, notification_arns, disable_rollback,\n timeout_in_minutes, capabilities, tags, on_failure, stack_policy_body, stack_policy_url)\n except BotoServerError as e:\n msg = 'Failed to create stack {0}.\\n{1}'.format(name, e)\n log.error(msg)\n log.debug(e)\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef update_stack(name, template_body=None, template_url=None, parameters=None, notification_arns=None,\n disable_rollback=False, timeout_in_minutes=None, capabilities=None, tags=None,\n use_previous_template=None, stack_policy_during_update_body=None, stack_policy_during_update_url=None,\n stack_policy_body=None, stack_policy_url=None, region=None, key=None, keyid=None, profile=None):\n '''\n Update a CFN stack.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cfn.update_stack mystack template_url='https://s3.amazonaws.com/bucket/template.cft' \\\n region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n update = conn.update_stack(name, template_body, template_url, parameters, notification_arns,\n disable_rollback, timeout_in_minutes, capabilities, tags, use_previous_template,\n stack_policy_during_update_body, stack_policy_during_update_url,\n stack_policy_body, stack_policy_url)\n log.debug('Updated result is : %s.', update)\n return update\n except BotoServerError as e:\n msg = 'Failed to update stack {0}.'.format(name)\n log.debug(e)\n log.error(msg)\n return six.text_type(e)", "response": "Update a CFN stack."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete(name, region=None, key=None, keyid=None, profile=None):\n '''\n Delete a CFN stack.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cfn.delete mystack region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n return conn.delete_stack(name)\n except BotoServerError as e:\n msg = 'Failed to create stack {0}.'.format(name)\n log.error(msg)\n log.debug(e)\n return six.text_type(e)", "response": "Delete a CFN stack."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_template(name, region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if attributes are set on a CFN stack.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cfn.get_template mystack\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n template = conn.get_template(name)\n log.info('Retrieved template for stack %s', name)\n return template\n except BotoServerError as e:\n log.debug(e)\n msg = 'Template {0} does not exist'.format(name)\n log.error(msg)\n return six.text_type(e)", "response": "Get a CFN template"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef validate_template(template_body=None, template_url=None, region=None, key=None, keyid=None, profile=None):\n '''\n Validate cloudformation template\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cfn.validate_template mystack-template\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n # Returns an object if json is validated and an exception if its not\n return conn.validate_template(template_body, template_url)\n except BotoServerError as e:\n log.debug(e)\n msg = 'Error while trying to validate template {0}.'.format(template_body)\n log.error(msg)\n return six.text_type(e)", "response": "Validate cloudformation template\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cfn.validate_template mystack-template"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef run(name, action):\n '''\n Run the specified service with an action.\n\n .. versionadded:: 2015.8.1\n\n name\n Service name.\n\n action\n Action name (like start, stop, reload, restart).\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.run apache2 reload\n salt '*' service.run postgresql initdb\n '''\n cmd = os.path.join(\n _GRAINMAP.get(__grains__.get('os'), '/etc/init.d'),\n name\n ) + ' ' + action\n return not __salt__['cmd.retcode'](cmd, python_shell=False)", "response": "Run the specified service with an action."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef status(name, sig=None):\n '''\n Return the status for a service.\n If the name contains globbing, a dict mapping service name to PID or empty\n string is returned.\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): Signature to use to find the service via ps\n\n Returns:\n string: PID if running, empty otherwise\n dict: Maps service name to PID if running, empty string otherwise\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.status <service name> [service signature]\n '''\n if sig:\n return __salt__['status.pid'](sig)\n\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 results[service] = __salt__['status.pid'](service)\n if contains_globbing:\n return results\n return results[name]", "response": "Return the status for a service."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a list of all available services CLI Example : Returns a list of all available services", "response": "def get_all():\n '''\n Return a list of all available services\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.get_all\n '''\n if not os.path.isdir(_GRAINMAP.get(__grains__.get('os'), '/etc/init.d')):\n return []\n return sorted(os.listdir(_GRAINMAP.get(__grains__.get('os'), '/etc/init.d')))"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _auth(profile=None):\n '''\n Set up neutron credentials\n '''\n credentials = __salt__['config.option'](profile)\n kwargs = {\n 'username': credentials['keystone.user'],\n 'password': credentials['keystone.password'],\n 'tenant_name': credentials['keystone.tenant'],\n 'auth_url': credentials['keystone.auth_url'],\n 'region_name': credentials.get('keystone.region_name', None),\n 'service_type': credentials['keystone.service_type'],\n }\n\n return suoneu.SaltNeutron(**kwargs)", "response": "Return a new neutron client"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef ext_pillar(minion_id,\n pillar, # pylint: disable=W0613\n conf):\n '''\n Check neutron for all data\n '''\n comps = conf.split()\n\n profile = None\n if comps[0]:\n profile = comps[0]\n\n conn = _auth(profile)\n ret = {}\n networks = conn.list_networks()\n for network in networks['networks']:\n ret[network['name']] = network\n\n if len(comps) < 2:\n comps.append('networks')\n return {comps[1]: ret}", "response": "Check neutron for all data"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef present(name, containment='NONE', options=None, **kwargs):\n '''\n Ensure that the named database is present with the specified options\n\n name\n The name of the database to manage\n containment\n Defaults to NONE\n options\n Can be a list of strings, a dictionary, or a list of dictionaries\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n if __salt__['mssql.db_exists'](name, **kwargs):\n ret['comment'] = 'Database {0} is already present (Not going to try to set its options)'.format(name)\n return ret\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Database {0} is set to be added'.format(name)\n return ret\n\n db_created = __salt__['mssql.db_create'](name, containment=containment, new_database_options=_normalize_options(options), **kwargs)\n if db_created is not True: # Non-empty strings are also evaluated to True, so we cannot use if not db_created:\n ret['result'] = False\n ret['comment'] += 'Database {0} failed to be created: {1}'.format(name, db_created)\n return ret\n ret['comment'] += 'Database {0} has been added'.format(name)\n ret['changes'][name] = 'Present'\n return ret", "response": "Ensure that the named database is present with the specified options"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_file_client(opts, pillar=False):\n '''\n Read in the ``file_client`` option and return the correct type of file\n server\n '''\n client = opts.get('file_client', 'remote')\n if pillar and client == 'local':\n client = 'pillar'\n return {\n 'remote': RemoteClient,\n 'local': FSClient,\n 'pillar': PillarClient,\n }.get(client, RemoteClient)(opts)", "response": "Read in the file_client option and return the correct type of file\n server\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef decode_dict_keys_to_str(src):\n '''\n Convert top level keys from bytes to strings if possible.\n This is necessary because Python 3 makes a distinction\n between these types.\n '''\n if not six.PY3 or not isinstance(src, dict):\n return src\n\n output = {}\n for key, val in six.iteritems(src):\n if isinstance(key, bytes):\n try:\n key = key.decode()\n except UnicodeError:\n pass\n output[key] = val\n return output", "response": "Convert top level keys from bytes to strings if possible."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _check_proto(self, path):\n '''\n Make sure that this path is intended for the salt master and trim it\n '''\n if not path.startswith('salt://'):\n raise MinionError('Unsupported path: {0}'.format(path))\n file_path, saltenv = salt.utils.url.parse(path)\n return file_path", "response": "Check that the path is intended for the salt master and trim it"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _file_local_list(self, dest):\n '''\n Helper util to return a list of files in a directory\n '''\n if os.path.isdir(dest):\n destdir = dest\n else:\n destdir = os.path.dirname(dest)\n\n filelist = set()\n\n for root, dirs, files in salt.utils.path.os_walk(destdir, followlinks=True):\n for name in files:\n path = os.path.join(root, name)\n filelist.add(path)\n\n return filelist", "response": "Helper util to return a list of files in a directory dest"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the local location to cache the file", "response": "def _cache_loc(self, path, saltenv='base', cachedir=None):\n '''\n Return the local location to cache the file, cache dirs will be made\n '''\n cachedir = self.get_cachedir(cachedir)\n dest = salt.utils.path.join(cachedir,\n 'files',\n saltenv,\n path)\n destdir = os.path.dirname(dest)\n with salt.utils.files.set_umask(0o077):\n # remove destdir if it is a regular file to avoid an OSError when\n # running os.makedirs below\n if os.path.isfile(destdir):\n os.remove(destdir)\n\n # ensure destdir exists\n try:\n os.makedirs(destdir)\n except OSError as exc:\n if exc.errno != errno.EEXIST: # ignore if it was there already\n raise\n\n yield dest"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a file from the local files or master depending on the implementation AttributeNames", "response": "def get_file(self,\n path,\n dest='',\n makedirs=False,\n saltenv='base',\n gzip=None,\n cachedir=None):\n '''\n Copies a file from the local files or master depending on\n implementation\n '''\n raise NotImplementedError"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef cache_file(self, path, saltenv='base', cachedir=None, source_hash=None):\n '''\n Pull a file down from the file server and store it in the minion\n file cache\n '''\n return self.get_url(\n path, '', True, saltenv, cachedir=cachedir, source_hash=source_hash)", "response": "Get a file from the file server and store it in the minion s cache_file dictionary"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef cache_files(self, paths, saltenv='base', cachedir=None):\n '''\n Download a list of files stored on the master and put them in the\n minion file cache\n '''\n ret = []\n if isinstance(paths, six.string_types):\n paths = paths.split(',')\n for path in paths:\n ret.append(self.cache_file(path, saltenv, cachedir=cachedir))\n return ret", "response": "Download a list of files stored on the master and put them in the master file cache\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndownloading and cache all files on a master in a specified environment", "response": "def cache_master(self, saltenv='base', cachedir=None):\n '''\n Download and cache all files on a master in a specified environment\n '''\n ret = []\n for path in self.file_list(saltenv):\n ret.append(\n self.cache_file(\n salt.utils.url.create(path), saltenv, cachedir=cachedir)\n )\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef cache_dir(self, path, saltenv='base', include_empty=False,\n include_pat=None, exclude_pat=None, cachedir=None):\n '''\n Download all of the files in a subdir of the master\n '''\n ret = []\n\n path = self._check_proto(salt.utils.data.decode(path))\n # We want to make sure files start with this *directory*, use\n # '/' explicitly because the master (that's generating the\n # list of files) only runs on POSIX\n if not path.endswith('/'):\n path = path + '/'\n\n log.info(\n 'Caching directory \\'%s\\' for environment \\'%s\\'', path, saltenv\n )\n # go through the list of all files finding ones that are in\n # the target directory and caching them\n for fn_ in self.file_list(saltenv):\n fn_ = salt.utils.data.decode(fn_)\n if fn_.strip() and fn_.startswith(path):\n if salt.utils.stringutils.check_include_exclude(\n fn_, include_pat, exclude_pat):\n fn_ = self.cache_file(\n salt.utils.url.create(fn_), saltenv, cachedir=cachedir)\n if fn_:\n ret.append(fn_)\n\n if include_empty:\n # Break up the path into a list containing the bottom-level\n # directory (the one being recursively copied) and the directories\n # preceding it\n # separated = string.rsplit(path, '/', 1)\n # if len(separated) != 2:\n # # No slashes in path. (So all files in saltenv will be copied)\n # prefix = ''\n # else:\n # prefix = separated[0]\n cachedir = self.get_cachedir(cachedir)\n\n dest = salt.utils.path.join(cachedir, 'files', saltenv)\n for fn_ in self.file_list_emptydirs(saltenv):\n fn_ = salt.utils.data.decode(fn_)\n if fn_.startswith(path):\n minion_dir = '{0}/{1}'.format(dest, fn_)\n if not os.path.isdir(minion_dir):\n os.makedirs(minion_dir)\n ret.append(minion_dir)\n return ret", "response": "Download all of the files in a directory and cache them"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncache a local file on the minion in the localfiles cache", "response": "def cache_local_file(self, path, **kwargs):\n '''\n Cache a local file on the minion in the localfiles cache\n '''\n dest = os.path.join(self.opts['cachedir'], 'localfiles',\n path.lstrip('/'))\n destdir = os.path.dirname(dest)\n\n if not os.path.isdir(destdir):\n os.makedirs(destdir)\n\n shutil.copyfile(path, dest)\n return dest"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef file_local_list(self, saltenv='base'):\n '''\n List files in the local minion files and localfiles caches\n '''\n filesdest = os.path.join(self.opts['cachedir'], 'files', saltenv)\n localfilesdest = os.path.join(self.opts['cachedir'], 'localfiles')\n\n fdest = self._file_local_list(filesdest)\n ldest = self._file_local_list(localfilesdest)\n return sorted(fdest.union(ldest))", "response": "List files in the local minion files and localfiles caches\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the full path to a file if it is cached locally on the minion otherwise returns a blank string", "response": "def is_cached(self, path, saltenv='base', cachedir=None):\n '''\n Returns the full path to a file if it is cached locally on the minion\n otherwise returns a blank string\n '''\n if path.startswith('salt://'):\n path, senv = salt.utils.url.parse(path)\n if senv:\n saltenv = senv\n\n escaped = True if salt.utils.url.is_escaped(path) else False\n\n # also strip escape character '|'\n localsfilesdest = os.path.join(\n self.opts['cachedir'], 'localfiles', path.lstrip('|/'))\n filesdest = os.path.join(\n self.opts['cachedir'], 'files', saltenv, path.lstrip('|/'))\n extrndest = self._extrn_path(path, saltenv, cachedir=cachedir)\n\n if os.path.exists(filesdest):\n return salt.utils.url.escape(filesdest) if escaped else filesdest\n elif os.path.exists(localsfilesdest):\n return salt.utils.url.escape(localsfilesdest) \\\n if escaped \\\n else localsfilesdest\n elif os.path.exists(extrndest):\n return extrndest\n\n return ''"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef cache_dest(self, url, saltenv='base', cachedir=None):\n '''\n Return the expected cache location for the specified URL and\n environment.\n '''\n proto = urlparse(url).scheme\n\n if proto == '':\n # Local file path\n return url\n\n if proto == 'salt':\n url, senv = salt.utils.url.parse(url)\n if senv:\n saltenv = senv\n return salt.utils.path.join(\n self.opts['cachedir'],\n 'files',\n saltenv,\n url.lstrip('|/'))\n\n return self._extrn_path(url, saltenv, cachedir=cachedir)", "response": "Return the expected cache location for the specified URL and environment."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_states(self, saltenv):\n '''\n Return a list of all available sls modules on the master for a given\n environment\n '''\n states = set()\n for path in self.file_list(saltenv):\n if salt.utils.platform.is_windows():\n path = path.replace('\\\\', '/')\n if path.endswith('.sls'):\n # is an sls module!\n if path.endswith('/init.sls'):\n states.add(path.replace('/', '.')[:-9])\n else:\n states.add(path.replace('/', '.')[:-4])\n return sorted(states)", "response": "Return a list of all sls modules on the master for a given environment"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets a state file from the master and store it in the local minion cache ; return the location of the file", "response": "def get_state(self, sls, saltenv, cachedir=None):\n '''\n Get a state file from the master and store it in the local minion\n cache; return the location of the file\n '''\n if '.' in sls:\n sls = sls.replace('.', '/')\n sls_url = salt.utils.url.create(sls + '.sls')\n init_url = salt.utils.url.create(sls + '/init.sls')\n for path in [sls_url, init_url]:\n dest = self.cache_file(path, saltenv, cachedir=cachedir)\n if dest:\n return {'source': path, 'dest': dest}\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget a directory recursively from the salt - master", "response": "def get_dir(self, path, dest='', saltenv='base', gzip=None,\n cachedir=None):\n '''\n Get a directory recursively from the salt-master\n '''\n ret = []\n # Strip trailing slash\n path = self._check_proto(path).rstrip('/')\n # Break up the path into a list containing the bottom-level directory\n # (the one being recursively copied) and the directories preceding it\n separated = path.rsplit('/', 1)\n if len(separated) != 2:\n # No slashes in path. (This means all files in saltenv will be\n # copied)\n prefix = ''\n else:\n prefix = separated[0]\n\n # Copy files from master\n for fn_ in self.file_list(saltenv, prefix=path):\n # Prevent files in \"salt://foobar/\" (or salt://foo.sh) from\n # matching a path of \"salt://foo\"\n try:\n if fn_[len(path)] != '/':\n continue\n except IndexError:\n continue\n # Remove the leading directories from path to derive\n # the relative path on the minion.\n minion_relpath = fn_[len(prefix):].lstrip('/')\n ret.append(\n self.get_file(\n salt.utils.url.create(fn_),\n '{0}/{1}'.format(dest, minion_relpath),\n True, saltenv, gzip\n )\n )\n # Replicate empty dirs from master\n try:\n for fn_ in self.file_list_emptydirs(saltenv, prefix=path):\n # Prevent an empty dir \"salt://foobar/\" from matching a path of\n # \"salt://foo\"\n try:\n if fn_[len(path)] != '/':\n continue\n except IndexError:\n continue\n # Remove the leading directories from path to derive\n # the relative path on the minion.\n minion_relpath = fn_[len(prefix):].lstrip('/')\n minion_mkdir = '{0}/{1}'.format(dest, minion_relpath)\n if not os.path.isdir(minion_mkdir):\n os.makedirs(minion_mkdir)\n ret.append(minion_mkdir)\n except TypeError:\n pass\n ret.sort()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets a single file from a URL.", "response": "def get_url(self, url, dest, makedirs=False, saltenv='base',\n no_cache=False, cachedir=None, source_hash=None):\n '''\n Get a single file from a URL.\n '''\n url_data = urlparse(url)\n url_scheme = url_data.scheme\n url_path = os.path.join(\n url_data.netloc, url_data.path).rstrip(os.sep)\n\n # If dest is a directory, rewrite dest with filename\n if dest is not None \\\n and (os.path.isdir(dest) or dest.endswith(('/', '\\\\'))):\n if url_data.query or len(url_data.path) > 1 and not url_data.path.endswith('/'):\n strpath = url.split('/')[-1]\n else:\n strpath = 'index.html'\n\n if salt.utils.platform.is_windows():\n strpath = salt.utils.path.sanitize_win_path(strpath)\n\n dest = os.path.join(dest, strpath)\n\n if url_scheme and url_scheme.lower() in string.ascii_lowercase:\n url_path = ':'.join((url_scheme, url_path))\n url_scheme = 'file'\n\n if url_scheme in ('file', ''):\n # Local filesystem\n if not os.path.isabs(url_path):\n raise CommandExecutionError(\n 'Path \\'{0}\\' is not absolute'.format(url_path)\n )\n if dest is None:\n with salt.utils.files.fopen(url_path, 'rb') as fp_:\n data = fp_.read()\n return data\n return url_path\n\n if url_scheme == 'salt':\n result = self.get_file(url, dest, makedirs, saltenv, cachedir=cachedir)\n if result and dest is None:\n with salt.utils.files.fopen(result, 'rb') as fp_:\n data = fp_.read()\n return data\n return result\n\n if dest:\n destdir = os.path.dirname(dest)\n if not os.path.isdir(destdir):\n if makedirs:\n os.makedirs(destdir)\n else:\n return ''\n elif not no_cache:\n dest = self._extrn_path(url, saltenv, cachedir=cachedir)\n if source_hash is not None:\n try:\n source_hash = source_hash.split('=')[-1]\n form = salt.utils.files.HASHES_REVMAP[len(source_hash)]\n if salt.utils.hashutils.get_hash(dest, form) == source_hash:\n log.debug(\n 'Cached copy of %s (%s) matches source_hash %s, '\n 'skipping download', url, dest, source_hash\n )\n return dest\n except (AttributeError, KeyError, IOError, OSError):\n pass\n destdir = os.path.dirname(dest)\n if not os.path.isdir(destdir):\n os.makedirs(destdir)\n\n if url_data.scheme == 's3':\n try:\n def s3_opt(key, default=None):\n '''\n Get value of s3.<key> from Minion config or from Pillar\n '''\n if 's3.' + key in self.opts:\n return self.opts['s3.' + key]\n try:\n return self.opts['pillar']['s3'][key]\n except (KeyError, TypeError):\n return default\n self.utils['s3.query'](method='GET',\n bucket=url_data.netloc,\n path=url_data.path[1:],\n return_bin=False,\n local_file=dest,\n action=None,\n key=s3_opt('key'),\n keyid=s3_opt('keyid'),\n service_url=s3_opt('service_url'),\n verify_ssl=s3_opt('verify_ssl', True),\n location=s3_opt('location'),\n path_style=s3_opt('path_style', False),\n https_enable=s3_opt('https_enable', True))\n return dest\n except Exception as exc:\n raise MinionError(\n 'Could not fetch from {0}. Exception: {1}'.format(url, exc)\n )\n if url_data.scheme == 'ftp':\n try:\n ftp = ftplib.FTP()\n ftp.connect(url_data.hostname, url_data.port)\n ftp.login(url_data.username, url_data.password)\n remote_file_path = url_data.path.lstrip('/')\n with salt.utils.files.fopen(dest, 'wb') as fp_:\n ftp.retrbinary('RETR {0}'.format(remote_file_path), fp_.write)\n ftp.quit()\n return dest\n except Exception as exc:\n raise MinionError('Could not retrieve {0} from FTP server. Exception: {1}'.format(url, exc))\n\n if url_data.scheme == 'swift':\n try:\n def swift_opt(key, default):\n '''\n Get value of <key> from Minion config or from Pillar\n '''\n if key in self.opts:\n return self.opts[key]\n try:\n return self.opts['pillar'][key]\n except (KeyError, TypeError):\n return default\n\n swift_conn = SaltSwift(swift_opt('keystone.user', None),\n swift_opt('keystone.tenant', None),\n swift_opt('keystone.auth_url', None),\n swift_opt('keystone.password', None))\n\n swift_conn.get_object(url_data.netloc,\n url_data.path[1:],\n dest)\n return dest\n except Exception:\n raise MinionError('Could not fetch from {0}'.format(url))\n\n get_kwargs = {}\n if url_data.username is not None \\\n and url_data.scheme in ('http', 'https'):\n netloc = url_data.netloc\n at_sign_pos = netloc.rfind('@')\n if at_sign_pos != -1:\n netloc = netloc[at_sign_pos + 1:]\n fixed_url = urlunparse(\n (url_data.scheme, netloc, url_data.path,\n url_data.params, url_data.query, url_data.fragment))\n get_kwargs['auth'] = (url_data.username, url_data.password)\n else:\n fixed_url = url\n\n destfp = None\n try:\n # Tornado calls streaming_callback on redirect response bodies.\n # But we need streaming to support fetching large files (> RAM\n # avail). Here we are working around this by disabling recording\n # the body for redirections. The issue is fixed in Tornado 4.3.0\n # so on_header callback could be removed when we'll deprecate\n # Tornado<4.3.0. See #27093 and #30431 for details.\n\n # Use list here to make it writable inside the on_header callback.\n # Simple bool doesn't work here: on_header creates a new local\n # variable instead. This could be avoided in Py3 with 'nonlocal'\n # statement. There is no Py2 alternative for this.\n #\n # write_body[0] is used by the on_chunk callback to tell it whether\n # or not we need to write the body of the request to disk. For\n # 30x redirects we set this to False because we don't want to\n # write the contents to disk, as we will need to wait until we\n # get to the redirected URL.\n #\n # write_body[1] will contain a tornado.httputil.HTTPHeaders\n # instance that we will use to parse each header line. We\n # initialize this to False, and after we parse the status line we\n # will replace it with the HTTPHeaders instance. If/when we have\n # found the encoding used in the request, we set this value to\n # False to signify that we are done parsing.\n #\n # write_body[2] is where the encoding will be stored\n write_body = [None, False, None]\n\n def on_header(hdr):\n if write_body[1] is not False and write_body[2] is None:\n if not hdr.strip() and 'Content-Type' not in write_body[1]:\n # If write_body[0] is True, then we are not following a\n # redirect (initial response was a 200 OK). So there is\n # no need to reset write_body[0].\n if write_body[0] is not True:\n # We are following a redirect, so we need to reset\n # write_body[0] so that we properly follow it.\n write_body[0] = None\n # We don't need the HTTPHeaders object anymore\n write_body[1] = False\n return\n # Try to find out what content type encoding is used if\n # this is a text file\n write_body[1].parse_line(hdr) # pylint: disable=no-member\n if 'Content-Type' in write_body[1]:\n content_type = write_body[1].get('Content-Type') # pylint: disable=no-member\n if not content_type.startswith('text'):\n write_body[1] = write_body[2] = False\n else:\n encoding = 'utf-8'\n fields = content_type.split(';')\n for field in fields:\n if 'encoding' in field:\n encoding = field.split('encoding=')[-1]\n write_body[2] = encoding\n # We have found our encoding. Stop processing headers.\n write_body[1] = False\n\n # If write_body[0] is False, this means that this\n # header is a 30x redirect, so we need to reset\n # write_body[0] to None so that we parse the HTTP\n # status code from the redirect target. Additionally,\n # we need to reset write_body[2] so that we inspect the\n # headers for the Content-Type of the URL we're\n # following.\n if write_body[0] is write_body[1] is False:\n write_body[0] = write_body[2] = None\n\n # Check the status line of the HTTP request\n if write_body[0] is None:\n try:\n hdr = parse_response_start_line(hdr)\n except HTTPInputError:\n # Not the first line, do nothing\n return\n write_body[0] = hdr.code not in [301, 302, 303, 307]\n write_body[1] = HTTPHeaders()\n\n if no_cache:\n result = []\n\n def on_chunk(chunk):\n if write_body[0]:\n if write_body[2]:\n chunk = chunk.decode(write_body[2])\n result.append(chunk)\n else:\n dest_tmp = u\"{0}.part\".format(dest)\n # We need an open filehandle to use in the on_chunk callback,\n # that's why we're not using a with clause here.\n destfp = salt.utils.files.fopen(dest_tmp, 'wb') # pylint: disable=resource-leakage\n\n def on_chunk(chunk):\n if write_body[0]:\n destfp.write(chunk)\n\n query = salt.utils.http.query(\n fixed_url,\n stream=True,\n streaming_callback=on_chunk,\n header_callback=on_header,\n username=url_data.username,\n password=url_data.password,\n opts=self.opts,\n **get_kwargs\n )\n if 'handle' not in query:\n raise MinionError('Error: {0} reading {1}'.format(query['error'], url))\n if no_cache:\n if write_body[2]:\n return ''.join(result)\n return b''.join(result)\n else:\n destfp.close()\n destfp = None\n salt.utils.files.rename(dest_tmp, dest)\n return dest\n except HTTPError as exc:\n raise MinionError('HTTP error {0} reading {1}: {3}'.format(\n exc.code,\n url,\n *BaseHTTPServer.BaseHTTPRequestHandler.responses[exc.code]))\n except URLError as exc:\n raise MinionError('Error reading {0}: {1}'.format(url, exc.reason))\n finally:\n if destfp is not None:\n destfp.close()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget a template from the cache file and return the path to the dest file", "response": "def get_template(\n self,\n url,\n dest,\n template='jinja',\n makedirs=False,\n saltenv='base',\n cachedir=None,\n **kwargs):\n '''\n Cache a file then process it as a template\n '''\n if 'env' in kwargs:\n # \"env\" is not supported; Use \"saltenv\".\n kwargs.pop('env')\n\n kwargs['saltenv'] = saltenv\n url_data = urlparse(url)\n sfn = self.cache_file(url, saltenv, cachedir=cachedir)\n if not sfn or not os.path.exists(sfn):\n return ''\n if template in salt.utils.templates.TEMPLATE_REGISTRY:\n data = salt.utils.templates.TEMPLATE_REGISTRY[template](\n sfn,\n **kwargs\n )\n else:\n log.error(\n 'Attempted to render template with unavailable engine %s',\n template\n )\n return ''\n if not data['result']:\n # Failed to render the template\n log.error('Failed to render template with error: %s', data['data'])\n return ''\n if not dest:\n # No destination passed, set the dest as an extrn_files cache\n dest = self._extrn_path(url, saltenv, cachedir=cachedir)\n # If Salt generated the dest name, create any required dirs\n makedirs = True\n\n destdir = os.path.dirname(dest)\n if not os.path.isdir(destdir):\n if makedirs:\n os.makedirs(destdir)\n else:\n salt.utils.files.safe_rm(data['data'])\n return ''\n shutil.move(data['data'], dest)\n return dest"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the extrn_filepath for a given url", "response": "def _extrn_path(self, url, saltenv, cachedir=None):\n '''\n Return the extrn_filepath for a given url\n '''\n url_data = urlparse(url)\n if salt.utils.platform.is_windows():\n netloc = salt.utils.path.sanitize_win_path(url_data.netloc)\n else:\n netloc = url_data.netloc\n\n # Strip user:pass from URLs\n netloc = netloc.split('@')[-1]\n\n if cachedir is None:\n cachedir = self.opts['cachedir']\n elif not os.path.isabs(cachedir):\n cachedir = os.path.join(self.opts['cachedir'], cachedir)\n\n if url_data.query:\n file_name = '-'.join([url_data.path, url_data.query])\n else:\n file_name = url_data.path\n\n if len(file_name) > MAX_FILENAME_LENGTH:\n file_name = salt.utils.hashutils.sha256_digest(file_name)\n\n return salt.utils.path.join(\n cachedir,\n 'extrn_files',\n saltenv,\n netloc,\n file_name\n )"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlocate the file path", "response": "def _find_file(self, path, saltenv='base'):\n '''\n Locate the file path\n '''\n fnd = {'path': '',\n 'rel': ''}\n\n if salt.utils.url.is_escaped(path):\n # The path arguments are escaped\n path = salt.utils.url.unescape(path)\n for root in self.opts['pillar_roots'].get(saltenv, []):\n full = os.path.join(root, path)\n if os.path.isfile(full):\n fnd['path'] = full\n fnd['rel'] = path\n return fnd\n return fnd"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_file(self,\n path,\n dest='',\n makedirs=False,\n saltenv='base',\n gzip=None,\n cachedir=None):\n '''\n Copies a file from the local files directory into :param:`dest`\n gzip compression settings are ignored for local files\n '''\n path = self._check_proto(path)\n fnd = self._find_file(path, saltenv)\n fnd_path = fnd.get('path')\n if not fnd_path:\n return ''\n\n return fnd_path", "response": "Gets a file from the local files directory into the specified destination directory."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of files in the given environment with optional relative prefix path to limit directory traversal", "response": "def file_list(self, saltenv='base', prefix=''):\n '''\n Return a list of files in the given environment\n with optional relative prefix path to limit directory traversal\n '''\n ret = []\n prefix = prefix.strip('/')\n for path in self.opts['pillar_roots'].get(saltenv, []):\n for root, dirs, files in salt.utils.path.os_walk(\n os.path.join(path, prefix), followlinks=True\n ):\n # Don't walk any directories that match file_ignore_regex or glob\n dirs[:] = [d for d in dirs if not salt.fileserver.is_file_ignored(self.opts, d)]\n for fname in files:\n relpath = os.path.relpath(os.path.join(root, fname), path)\n ret.append(salt.utils.data.decode(relpath))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef __get_file_path(self, path, saltenv='base'):\n '''\n Return either a file path or the result of a remote find_file call.\n '''\n try:\n path = self._check_proto(path)\n except MinionError as err:\n # Local file path\n if not os.path.isfile(path):\n log.warning(\n 'specified file %s is not present to generate hash: %s',\n path, err\n )\n return None\n else:\n return path\n return self._find_file(path, saltenv)", "response": "Return either a file path or the result of a remote find_file call."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef hash_file(self, path, saltenv='base'):\n '''\n Return the hash of a file, to get the hash of a file in the pillar_roots\n prepend the path with salt://<file on server> otherwise, prepend the\n file with / for a local file.\n '''\n ret = {}\n fnd = self.__get_file_path(path, saltenv)\n if fnd is None:\n return ret\n\n try:\n # Remote file path (self._find_file() invoked)\n fnd_path = fnd['path']\n except TypeError:\n # Local file path\n fnd_path = fnd\n\n hash_type = self.opts.get('hash_type', 'md5')\n ret['hsum'] = salt.utils.hashutils.get_hash(fnd_path, form=hash_type)\n ret['hash_type'] = hash_type\n return ret", "response": "Return the hash of a file"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns the hash of a file and the stat result of the file.", "response": "def hash_and_stat_file(self, path, saltenv='base'):\n '''\n Return the hash of a file, to get the hash of a file in the pillar_roots\n prepend the path with salt://<file on server> otherwise, prepend the\n file with / for a local file.\n\n Additionally, return the stat result of the file, or None if no stat\n results were found.\n '''\n ret = {}\n fnd = self.__get_file_path(path, saltenv)\n if fnd is None:\n return ret, None\n\n try:\n # Remote file path (self._find_file() invoked)\n fnd_path = fnd['path']\n fnd_stat = fnd.get('stat')\n except TypeError:\n # Local file path\n fnd_path = fnd\n try:\n fnd_stat = list(os.stat(fnd_path))\n except Exception:\n fnd_stat = None\n\n hash_type = self.opts.get('hash_type', 'md5')\n ret['hsum'] = salt.utils.hashutils.get_hash(fnd_path, form=hash_type)\n ret['hash_type'] = hash_type\n return ret, fnd_stat"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef envs(self):\n '''\n Return the available environments\n '''\n ret = []\n for saltenv in self.opts['pillar_roots']:\n ret.append(saltenv)\n return ret", "response": "Return the available environments"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreset the channel, in the event of an interruption", "response": "def _refresh_channel(self):\n '''\n Reset the channel, in the event of an interruption\n '''\n self.channel = salt.transport.client.ReqChannel.factory(self.opts)\n return self.channel"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets a single file from the salt - master and store it in the specified location.", "response": "def get_file(self,\n path,\n dest='',\n makedirs=False,\n saltenv='base',\n gzip=None,\n cachedir=None):\n '''\n Get a single file from the salt-master\n path must be a salt server location, aka, salt://path/to/file, if\n dest is omitted, then the downloaded file will be placed in the minion\n cache\n '''\n path, senv = salt.utils.url.split_env(path)\n if senv:\n saltenv = senv\n\n if not salt.utils.platform.is_windows():\n hash_server, stat_server = self.hash_and_stat_file(path, saltenv)\n try:\n mode_server = stat_server[0]\n except (IndexError, TypeError):\n mode_server = None\n else:\n hash_server = self.hash_file(path, saltenv)\n mode_server = None\n\n # Check if file exists on server, before creating files and\n # directories\n if hash_server == '':\n log.debug(\n 'Could not find file \\'%s\\' in saltenv \\'%s\\'',\n path, saltenv\n )\n return False\n\n # If dest is a directory, rewrite dest with filename\n if dest is not None \\\n and (os.path.isdir(dest) or dest.endswith(('/', '\\\\'))):\n dest = os.path.join(dest, os.path.basename(path))\n log.debug(\n 'In saltenv \\'%s\\', \\'%s\\' is a directory. Changing dest to '\n '\\'%s\\'', saltenv, os.path.dirname(dest), dest\n )\n\n # Hash compare local copy with master and skip download\n # if no difference found.\n dest2check = dest\n if not dest2check:\n rel_path = self._check_proto(path)\n\n log.debug(\n 'In saltenv \\'%s\\', looking at rel_path \\'%s\\' to resolve '\n '\\'%s\\'', saltenv, rel_path, path\n )\n with self._cache_loc(\n rel_path, saltenv, cachedir=cachedir) as cache_dest:\n dest2check = cache_dest\n\n log.debug(\n 'In saltenv \\'%s\\', ** considering ** path \\'%s\\' to resolve '\n '\\'%s\\'', saltenv, dest2check, path\n )\n\n if dest2check and os.path.isfile(dest2check):\n if not salt.utils.platform.is_windows():\n hash_local, stat_local = \\\n self.hash_and_stat_file(dest2check, saltenv)\n try:\n mode_local = stat_local[0]\n except (IndexError, TypeError):\n mode_local = None\n else:\n hash_local = self.hash_file(dest2check, saltenv)\n mode_local = None\n\n if hash_local == hash_server:\n return dest2check\n\n log.debug(\n 'Fetching file from saltenv \\'%s\\', ** attempting ** \\'%s\\'',\n saltenv, path\n )\n d_tries = 0\n transport_tries = 0\n path = self._check_proto(path)\n load = {'path': path,\n 'saltenv': saltenv,\n 'cmd': '_serve_file'}\n if gzip:\n gzip = int(gzip)\n load['gzip'] = gzip\n\n fn_ = None\n if dest:\n destdir = os.path.dirname(dest)\n if not os.path.isdir(destdir):\n if makedirs:\n try:\n os.makedirs(destdir)\n except OSError as exc:\n if exc.errno != errno.EEXIST: # ignore if it was there already\n raise\n else:\n return False\n # We need an open filehandle here, that's why we're not using a\n # with clause:\n fn_ = salt.utils.files.fopen(dest, 'wb+') # pylint: disable=resource-leakage\n else:\n log.debug('No dest file found')\n\n while True:\n if not fn_:\n load['loc'] = 0\n else:\n load['loc'] = fn_.tell()\n data = self.channel.send(load, raw=True)\n if six.PY3:\n # Sometimes the source is local (eg when using\n # 'salt.fileserver.FSChan'), in which case the keys are\n # already strings. Sometimes the source is remote, in which\n # case the keys are bytes due to raw mode. Standardize on\n # strings for the top-level keys to simplify things.\n data = decode_dict_keys_to_str(data)\n try:\n if not data['data']:\n if not fn_ and data['dest']:\n # This is a 0 byte file on the master\n with self._cache_loc(\n data['dest'],\n saltenv,\n cachedir=cachedir) as cache_dest:\n dest = cache_dest\n with salt.utils.files.fopen(cache_dest, 'wb+') as ofile:\n ofile.write(data['data'])\n if 'hsum' in data and d_tries < 3:\n # Master has prompted a file verification, if the\n # verification fails, re-download the file. Try 3 times\n d_tries += 1\n hsum = salt.utils.hashutils.get_hash(dest, salt.utils.stringutils.to_str(data.get('hash_type', b'md5')))\n if hsum != data['hsum']:\n log.warning(\n 'Bad download of file %s, attempt %d of 3',\n path, d_tries\n )\n continue\n break\n if not fn_:\n with self._cache_loc(\n data['dest'],\n saltenv,\n cachedir=cachedir) as cache_dest:\n dest = cache_dest\n # If a directory was formerly cached at this path, then\n # remove it to avoid a traceback trying to write the file\n if os.path.isdir(dest):\n salt.utils.files.rm_rf(dest)\n fn_ = salt.utils.atomicfile.atomic_open(dest, 'wb+')\n if data.get('gzip', None):\n data = salt.utils.gzip_util.uncompress(data['data'])\n else:\n data = data['data']\n if six.PY3 and isinstance(data, str):\n data = data.encode()\n fn_.write(data)\n except (TypeError, KeyError) as exc:\n try:\n data_type = type(data).__name__\n except AttributeError:\n # Shouldn't happen, but don't let this cause a traceback.\n data_type = six.text_type(type(data))\n transport_tries += 1\n log.warning(\n 'Data transport is broken, got: %s, type: %s, '\n 'exception: %s, attempt %d of 3',\n data, data_type, exc, transport_tries\n )\n self._refresh_channel()\n if transport_tries > 3:\n log.error(\n 'Data transport is broken, got: %s, type: %s, '\n 'exception: %s, retry attempts exhausted',\n data, data_type, exc\n )\n break\n\n if fn_:\n fn_.close()\n log.info(\n 'Fetching file from saltenv \\'%s\\', ** done ** \\'%s\\'',\n saltenv, path\n )\n else:\n log.debug(\n 'In saltenv \\'%s\\', we are ** missing ** the file \\'%s\\'',\n saltenv, path\n )\n\n return dest"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nlist the dirs on the master", "response": "def dir_list(self, saltenv='base', prefix=''):\n '''\n List the dirs on the master\n '''\n load = {'saltenv': saltenv,\n 'prefix': prefix,\n 'cmd': '_dir_list'}\n return salt.utils.data.decode(self.channel.send(load)) if six.PY2 \\\n else self.channel.send(load)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef __hash_and_stat_file(self, path, saltenv='base'):\n '''\n Common code for hashing and stating files\n '''\n try:\n path = self._check_proto(path)\n except MinionError as err:\n if not os.path.isfile(path):\n log.warning(\n 'specified file %s is not present to generate hash: %s',\n path, err\n )\n return {}, None\n else:\n ret = {}\n hash_type = self.opts.get('hash_type', 'md5')\n ret['hsum'] = salt.utils.hashutils.get_hash(path, form=hash_type)\n ret['hash_type'] = hash_type\n return ret\n load = {'path': path,\n 'saltenv': saltenv,\n 'cmd': '_file_hash'}\n return self.channel.send(load)", "response": "Common code for hashing and stating files"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a list of the files in the specified environment", "response": "def list_env(self, saltenv='base'):\n '''\n Return a list of the files in the file server's specified environment\n '''\n load = {'saltenv': saltenv,\n 'cmd': '_file_list'}\n return salt.utils.data.decode(self.channel.send(load)) if six.PY2 \\\n else self.channel.send(load)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns a list of available environments", "response": "def envs(self):\n '''\n Return a list of available environments\n '''\n load = {'cmd': '_file_envs'}\n return salt.utils.data.decode(self.channel.send(load)) if six.PY2 \\\n else self.channel.send(load)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the master_tops system_till_master_tops is set to None", "response": "def master_tops(self):\n '''\n Return the metadata derived from the master_tops system\n '''\n log.debug(\n 'The _ext_nodes master function has been renamed to _master_tops. '\n 'To ensure compatibility when using older Salt masters we will '\n 'continue to invoke the function as _ext_nodes until the '\n 'Magnesium release.'\n )\n # TODO: Change back to _master_tops\n # for Magnesium release\n load = {'cmd': '_ext_nodes',\n 'id': self.opts['id'],\n 'opts': self.opts}\n if self.auth:\n load['tok'] = self.auth.gen_token(b'salt')\n return salt.utils.data.decode(self.channel.send(load)) if six.PY2 \\\n else self.channel.send(load)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking destination for the archives.", "response": "def check_destination(self, location, group):\n '''\n Check destination for the archives.\n :return:\n '''\n # Pre-create destination, since rsync will\n # put one file named as group\n try:\n destination = os.path.join(location, group)\n if os.path.exists(destination) and not os.path.isdir(destination):\n raise salt.exceptions.SaltException('Destination \"{}\" should be directory!'.format(destination))\n if not os.path.exists(destination):\n os.makedirs(destination)\n log.debug('Created destination directory for archives: %s', destination)\n else:\n log.debug('Archives destination directory %s already exists', destination)\n except OSError as err:\n log.error(err)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef collected(self, group, filename=None, host=None, location=None, move=True, all=True):\n '''\n Sync archives to a central place.\n\n :param name:\n :param group:\n :param filename:\n :param host:\n :param location:\n :param move:\n :param all:\n :return:\n '''\n ret = {\n 'name': 'support.collected',\n 'changes': {},\n 'result': True,\n 'comment': '',\n }\n location = location or tempfile.gettempdir()\n self.check_destination(location, group)\n ret['changes'] = __salt__['support.sync'](group, name=filename, host=host,\n location=location, move=move, all=all)\n\n return ret", "response": "Sync archives to a central place."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ntake minion support config data.", "response": "def taken(self, profile='default', pillar=None, archive=None, output='nested'):\n '''\n Takes minion support config data.\n\n :param profile:\n :param pillar:\n :param archive:\n :param output:\n :return:\n '''\n ret = {\n 'name': 'support.taken',\n 'changes': {},\n 'result': True,\n }\n\n result = __salt__['support.run'](profile=profile, pillar=pillar, archive=archive, output=output)\n if result.get('archive'):\n ret['comment'] = 'Information about this system has been saved to {} file.'.format(result['archive'])\n ret['changes']['archive'] = result['archive']\n ret['changes']['messages'] = {}\n for key in ['info', 'error', 'warning']:\n if result.get('messages', {}).get(key):\n ret['changes']['messages'][key] = result['messages'][key]\n else:\n ret['comment'] = ''\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_disk(name, size):\n '''\n Create a VMM disk with the specified `name` and `size`.\n\n size:\n Size in megabytes, or use a specifier such as M, G, T.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' vmctl.create_disk /path/to/disk.img size=10G\n '''\n ret = False\n cmd = 'vmctl create {0} -s {1}'.format(name, size)\n\n result = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n\n if result['retcode'] == 0:\n ret = True\n else:\n raise CommandExecutionError(\n 'Problem encountered creating disk image',\n info={'errors': [result['stderr']], 'changes': ret}\n )\n\n return ret", "response": "Create a VMM disk with the specified name and size."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef load(path):\n '''\n Load additional configuration from the specified file.\n\n path\n Path to the configuration file.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' vmctl.load path=/etc/vm.switches.conf\n '''\n ret = False\n cmd = 'vmctl load {0}'.format(path)\n result = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n if result['retcode'] == 0:\n ret = True\n else:\n raise CommandExecutionError(\n 'Problem encountered running vmctl',\n info={'errors': [result['stderr']], 'changes': ret}\n )\n\n return ret", "response": "Load additional configuration from the specified file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreload the current configuration file from the default configuration file.", "response": "def reload():\n '''\n Remove all stopped VMs and reload configuration from the default configuration file.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' vmctl.reload\n '''\n ret = False\n cmd = 'vmctl reload'\n result = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n if result['retcode'] == 0:\n ret = True\n else:\n raise CommandExecutionError(\n 'Problem encountered running vmctl',\n info={'errors': [result['stderr']], 'changes': ret}\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreset the running state of a single or multiple virtual machines or subsystems.", "response": "def reset(all=False, vms=False, switches=False):\n '''\n Reset the running state of VMM or a subsystem.\n\n all:\n Reset the running state.\n\n switches:\n Reset the configured switches.\n\n vms:\n Reset and terminate all VMs.\n\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' vmctl.reset all=True\n '''\n ret = False\n cmd = ['vmctl', 'reset']\n\n if all:\n cmd.append('all')\n elif vms:\n cmd.append('vms')\n elif switches:\n cmd.append('switches')\n\n result = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n if result['retcode'] == 0:\n ret = True\n else:\n raise CommandExecutionError(\n 'Problem encountered running vmctl',\n info={'errors': [result['stderr']], 'changes': ret}\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstart a new virtual machine.", "response": "def start(name=None, id=None, bootpath=None, disk=None, disks=None, local_iface=False,\n memory=None, nics=0, switch=None):\n '''\n Starts a VM defined by the specified parameters.\n When both a name and id are provided, the id is ignored.\n\n name:\n Name of the defined VM.\n\n id:\n VM id.\n\n bootpath:\n Path to a kernel or BIOS image to load.\n\n disk:\n Path to a single disk to use.\n\n disks:\n List of multiple disks to use.\n\n local_iface:\n Whether to add a local network interface. See \"LOCAL INTERFACES\"\n in the vmctl(8) manual page for more information.\n\n memory:\n Memory size of the VM specified in megabytes.\n\n switch:\n Add a network interface that is attached to the specified\n virtual switch on the host.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' vmctl.start 2 # start VM with id 2\n salt '*' vmctl.start name=web1 bootpath='/bsd.rd' nics=2 memory=512M disk='/disk.img'\n '''\n ret = {'changes': False, 'console': None}\n cmd = ['vmctl', 'start']\n\n if not (name or id):\n raise SaltInvocationError('Must provide either \"name\" or \"id\"')\n elif name:\n cmd.append(name)\n else:\n cmd.append(id)\n name = _id_to_name(id)\n\n if nics > 0:\n cmd.append('-i {0}'.format(nics))\n\n # Paths cannot be appended as otherwise the inserted whitespace is treated by\n # vmctl as being part of the path.\n if bootpath:\n cmd.extend(['-b', bootpath])\n\n if memory:\n cmd.append('-m {0}'.format(memory))\n\n if switch:\n cmd.append('-n {0}'.format(switch))\n\n if local_iface:\n cmd.append('-L')\n\n if disk and disks:\n raise SaltInvocationError('Must provide either \"disks\" or \"disk\"')\n\n if disk:\n cmd.extend(['-d', disk])\n\n if disks:\n cmd.extend(['-d', x] for x in disks)\n\n # Before attempting to define a new VM, make sure it doesn't already exist.\n # Otherwise return to indicate nothing was changed.\n if len(cmd) > 3:\n vmstate = status(name)\n if vmstate:\n ret['comment'] = 'VM already exists and cannot be redefined'\n return ret\n\n result = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n\n if result['retcode'] == 0:\n ret['changes'] = True\n m = re.match(r'.*successfully, tty (\\/dev.*)', result['stderr'])\n if m:\n ret['console'] = m.groups()[0]\n else:\n m = re.match(r'.*Operation already in progress$', result['stderr'])\n if m:\n ret['changes'] = False\n else:\n raise CommandExecutionError(\n 'Problem encountered running vmctl',\n info={'errors': [result['stderr']], 'changes': ret}\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef status(name=None, id=None):\n '''\n List VMs running on the host, or only the VM specified by ``id``. When\n both a name and id are provided, the id is ignored.\n\n name:\n Name of the defined VM.\n\n id:\n VM id.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' vmctl.status # to list all VMs\n salt '*' vmctl.status name=web1 # to get a single VM\n '''\n ret = {}\n cmd = ['vmctl', 'status']\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 encountered running vmctl',\n info={'error': [result['stderr']], 'changes': ret}\n )\n\n # Grab the header and save it with the lowercase names.\n header = result['stdout'].splitlines()[0].split()\n header = list([x.lower() for x in header])\n\n # A VM can be in one of the following states (from vmm.c:vcpu_state_decode())\n # - stopped\n # - running\n # - requesting termination\n # - terminated\n # - unknown\n\n for line in result['stdout'].splitlines()[1:]:\n data = line.split()\n vm = dict(list(zip(header, data)))\n vmname = vm.pop('name')\n if vm['pid'] == '-':\n # If the VM has no PID it's not running.\n vm['state'] = 'stopped'\n elif vmname and data[-2] == '-':\n # When a VM does have a PID and the second to last field is a '-', it's\n # transitioning to another state. A VM name itself cannot contain a\n # '-' so it's safe to split on '-'.\n vm['state'] = data[-1]\n else:\n vm['state'] = 'running'\n\n # When the status is requested of a single VM (by name) which is stopping,\n # vmctl doesn't print the status line. So we'll parse the full list and\n # return when we've found the requested VM.\n if id and int(vm['id']) == id:\n return {vmname: vm}\n elif name and vmname == name:\n return {vmname: vm}\n else:\n ret[vmname] = vm\n\n # Assert we've not come this far when an id or name have been provided. That\n # means the requested VM does not exist.\n if id or name:\n return {}\n\n return ret", "response": "Return a list of VMs running on the host or only the VM specified by id."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstop the specified virtual machine.", "response": "def stop(name=None, id=None):\n '''\n Stop (terminate) the VM identified by the given id or name.\n When both a name and id are provided, the id is ignored.\n\n name:\n Name of the defined VM.\n\n id:\n VM id.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' vmctl.stop name=alpine\n '''\n ret = {}\n cmd = ['vmctl', 'stop']\n\n if not (name or id):\n raise SaltInvocationError('Must provide either \"name\" or \"id\"')\n elif name:\n cmd.append(name)\n else:\n cmd.append(id)\n\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('^vmctl: sent request to terminate vm.*', result['stderr']):\n ret['changes'] = True\n else:\n ret['changes'] = False\n else:\n raise CommandExecutionError(\n 'Problem encountered running vmctl',\n info={'errors': [result['stderr']], 'changes': ret}\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nrunning all the munin plugins", "response": "def run_all():\n '''\n Run all the munin plugins\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' munin.run_all\n '''\n plugins = list_plugins()\n ret = {}\n for plugin in plugins:\n ret.update(run(plugin))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list_plugins():\n '''\n List all the munin plugins\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' munin.list_plugins\n '''\n pluginlist = os.listdir(PLUGINDIR)\n ret = []\n for plugin in pluginlist:\n # Check if execute bit\n statf = os.path.join(PLUGINDIR, plugin)\n try:\n executebit = stat.S_IXUSR & os.stat(statf)[stat.ST_MODE]\n except OSError:\n pass\n if executebit:\n ret.append(plugin)\n return ret", "response": "List all the munin plugins in order of creation"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_installed():\n '''\n Return a list of all installed kernels.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' kernelpkg.list_installed\n '''\n result = __salt__['pkg.version'](_package_name(), versions_as_list=True)\n if result is None:\n return []\n\n if six.PY2:\n return sorted(result, cmp=_cmp_version)\n else:\n return sorted(result, key=functools.cmp_to_key(_cmp_version))", "response": "Return a list of all installed kernels."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupgrading the kernel and optionally reboot the system.", "response": "def upgrade(reboot=False, at_time=None):\n '''\n Upgrade the kernel and optionally reboot the system.\n\n reboot : False\n Request a reboot if a new kernel is available.\n\n at_time : immediate\n Schedule the reboot at some point in the future. This argument\n is ignored if ``reboot=False``. See\n :py:func:`~salt.modules.system.reboot` for more details\n on this argument.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' kernelpkg.upgrade\n salt '*' kernelpkg.upgrade reboot=True at_time=1\n\n .. note::\n An immediate reboot often shuts down the system before the minion has a\n chance to return, resulting in errors. A minimal delay (1 minute) is\n useful to ensure the result is delivered to the master.\n '''\n result = __salt__['pkg.upgrade'](name=_package_name())\n _needs_reboot = needs_reboot()\n\n ret = {\n 'upgrades': result,\n 'active': active(),\n 'latest_installed': latest_installed(),\n 'reboot_requested': reboot,\n 'reboot_required': _needs_reboot\n }\n\n if reboot and _needs_reboot:\n log.warning('Rebooting system due to kernel upgrade')\n __salt__['system.reboot'](at_time=at_time)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nremove a specific version of a kernel.", "response": "def remove(release):\n '''\n Remove a specific version of the kernel.\n\n release\n The release number of an installed kernel. This must be the entire release\n number as returned by :py:func:`~salt.modules.kernelpkg_linux_yum.list_installed`,\n not the package name.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' kernelpkg.remove 3.10.0-327.el7\n '''\n if release not in list_installed():\n raise CommandExecutionError('Kernel release \\'{0}\\' is not installed'.format(release))\n\n if release == active():\n raise CommandExecutionError('Active kernel cannot be removed')\n\n target = '{0}-{1}'.format(_package_name(), release)\n log.info('Removing kernel package %s', target)\n old = __salt__['pkg.list_pkgs']()\n\n # Build the command string\n cmd = []\n if salt.utils.systemd.has_scope(__context__) \\\n and __salt__['config.get']('systemd.scope', True):\n cmd.extend(['systemd-run', '--scope'])\n cmd.extend([_yum(), '-y', 'remove', target])\n\n # Execute the command\n out = __salt__['cmd.run_all'](\n cmd,\n output_loglevel='trace',\n python_shell=False\n )\n\n # Look for the changes in installed packages\n __context__.pop('pkg.list_pkgs', None)\n new = __salt__['pkg.list_pkgs']()\n ret = salt.utils.data.compare_dicts(old, new)\n\n # Look for command execution errors\n if out['retcode'] != 0:\n raise CommandExecutionError(\n 'Error occurred removing package(s)',\n info={'errors': [out['stderr']], 'changes': ret}\n )\n\n return {'removed': [target]}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nenforce the TTL to a specific key delete it if it is past TTL", "response": "def _enforce_ttl_key(self, key):\n '''\n Enforce the TTL to a specific key, delete if its past TTL\n '''\n if key not in self._key_cache_time or self._ttl == 0:\n return\n if time.time() - self._key_cache_time[key] > self._ttl:\n del self._key_cache_time[key]\n dict.__delitem__(self, key)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _enforce_ttl_key(self, key):\n '''\n Enforce the TTL to a specific key, delete if its past TTL\n '''\n if key not in self._key_cache_time or self._ttl == 0:\n return\n if time.time() - self._key_cache_time[key] > self._ttl:\n del self._key_cache_time[key]\n self._dict.__delitem__(key)", "response": "Enforces the TTL to a specific key delete it if it is past TTL\n "} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreads in from disk and store the cache in self. _dict.", "response": "def _read(self):\n '''\n Read in from disk\n '''\n if msgpack is None:\n log.error('Cache cannot be read from the disk: msgpack is missing')\n elif not os.path.exists(self._path):\n log.debug('Cache path does not exist for reading: %s', self._path)\n else:\n try:\n with salt.utils.files.fopen(self._path, 'rb') as fp_:\n cache = salt.utils.data.decode(msgpack.load(fp_, encoding=__salt_system_encoding__))\n if \"CacheDisk_cachetime\" in cache: # new format\n self._dict = cache[\"CacheDisk_data\"]\n self._key_cache_time = cache[\"CacheDisk_cachetime\"]\n else: # old format\n self._dict = cache\n timestamp = os.path.getmtime(self._path)\n for key in self._dict:\n self._key_cache_time[key] = timestamp\n if log.isEnabledFor(logging.DEBUG):\n log.debug('Disk cache retrieved: %s', cache)\n except (IOError, OSError) as err:\n log.error('Error while reading disk cache from %s: %s', self._path, err)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nstore the contents of the entire cache to disk", "response": "def store(self):\n '''\n Write content of the entire cache to disk\n '''\n if msgpack is None:\n log.error('Cache cannot be stored on disk: msgpack is missing')\n else:\n # TODO Dir hashing?\n try:\n with salt.utils.files.fopen(self._path, 'wb+') as fp_:\n cache = {\n \"CacheDisk_data\": self._dict,\n \"CacheDisk_cachetime\": self._key_cache_time\n }\n msgpack.dump(cache, fp_, use_bin_type=True)\n except (IOError, OSError) as err:\n log.error('Error storing cache data to the disk: %s', err)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef put_cache(self, minions):\n '''\n published the given minions to the ConCache\n '''\n self.cupd_out.send(self.serial.dumps(minions))", "response": "Publish the given minions to the ConCache\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_cached(self):\n '''\n queries the ConCache for a list of currently connected minions\n '''\n msg = self.serial.dumps('minions')\n self.creq_out.send(msg)\n min_list = self.serial.loads(self.creq_out.recv())\n return min_list", "response": "queries the ConCache for a list of currently connected minions\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef sweep(self):\n '''\n Sweep the cache and remove the outdated or least frequently\n used entries\n '''\n if self.max_age < time.time() - self.timestamp:\n self.clear()\n self.timestamp = time.time()\n else:\n paterns = list(self.cache.values())\n paterns.sort()\n for idx in range(self.clear_size):\n del self.cache[paterns[idx][2]]", "response": "Sweep the cache and remove the outdated or least frequently used entries\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get(self, pattern):\n '''\n Get a compiled regular expression object based on pattern and\n cache it when it is not in the cache already\n '''\n try:\n self.cache[pattern][0] += 1\n return self.cache[pattern][1]\n except KeyError:\n pass\n if len(self.cache) > self.size:\n self.sweep()\n regex = re.compile('{0}{1}{2}'.format(\n self.prepend, pattern, self.append))\n self.cache[pattern] = [1, regex, pattern, time.time()]\n return regex", "response": "Get a compiled regular expression object based on pattern and cache it if it is not in the cache and return it"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef cache_context(self, context):\n '''\n Cache the given context to disk\n '''\n if not os.path.isdir(os.path.dirname(self.cache_path)):\n os.mkdir(os.path.dirname(self.cache_path))\n with salt.utils.files.fopen(self.cache_path, 'w+b') as cache:\n self.serial.dump(context, cache)", "response": "Cache the given context to disk\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_cache_context(self):\n '''\n Retrieve a context cache from disk\n '''\n with salt.utils.files.fopen(self.cache_path, 'rb') as cache:\n return salt.utils.data.decode(self.serial.load(cache))", "response": "Retrieve a context cache from disk"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstrip reStructuredText directives in docstrings Strip/replace reStructuredText directives in docstrings", "response": "def strip_rst(docs):\n '''\n Strip/replace reStructuredText directives in docstrings\n '''\n for func, docstring in six.iteritems(docs):\n log.debug('Stripping docstring for %s', func)\n if not docstring:\n continue\n docstring_new = docstring if six.PY3 else salt.utils.data.encode(docstring)\n for regex, repl in (\n (r' *.. code-block:: \\S+\\n{1,2}', ''),\n ('.. note::', 'Note:'),\n ('.. warning::', 'Warning:'),\n ('.. versionadded::', 'New in version'),\n ('.. versionchanged::', 'Changed in version')):\n if six.PY2:\n regex = salt.utils.data.encode(regex)\n repl = salt.utils.data.encode(repl)\n try:\n docstring_new = re.sub(regex, repl, docstring_new)\n except Exception:\n log.debug(\n 'Exception encountered while matching regex %r to '\n 'docstring for function %s', regex, func,\n exc_info=True\n )\n if six.PY2:\n docstring_new = salt.utils.data.decode(docstring_new)\n if docstring != docstring_new:\n docs[func] = docstring_new\n return docs"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing a docstring into its parts.", "response": "def parse_docstring(docstring):\n '''\n Parse a docstring into its parts.\n\n Currently only parses dependencies, can be extended to parse whatever is\n needed.\n\n Parses into a dictionary:\n {\n 'full': full docstring,\n 'deps': list of dependencies (empty list if none)\n }\n '''\n # First try with regex search for :depends:\n ret = {'full': docstring}\n regex = r'([ \\t]*):depends:[ \\t]+- (\\w+)[^\\n]*\\n(\\1[ \\t]+- (\\w+)[^\\n]*\\n)*'\n match = re.search(regex, docstring, re.M)\n if match:\n deps = []\n regex = r'- (\\w+)'\n for line in match.group(0).strip().splitlines():\n deps.append(re.search(regex, line).group(1))\n ret['deps'] = deps\n return ret\n # Try searching for a one-liner instead\n else:\n txt = 'Required python modules: '\n data = docstring.splitlines()\n dep_list = list(x for x in data if x.strip().startswith(txt))\n if not dep_list:\n ret['deps'] = []\n return ret\n deps = dep_list[0].replace(txt, '').strip().split(', ')\n ret['deps'] = deps\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures a cache cluster exists and contains the specified attributes.", "response": "def present(\n name,\n engine=None,\n cache_node_type=None,\n num_cache_nodes=None,\n preferred_availability_zone=None,\n port=None,\n cache_parameter_group_name=None,\n cache_security_group_names=None,\n replication_group_id=None,\n auto_minor_version_upgrade=True,\n security_group_ids=None,\n cache_subnet_group_name=None,\n engine_version=None,\n notification_topic_arn=None,\n preferred_maintenance_window=None,\n wait=None,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Ensure the cache cluster exists.\n\n name\n Name of the cache cluster (cache cluster id).\n\n engine\n The name of the cache engine to be used for this cache cluster. Valid\n values are memcached or redis.\n\n cache_node_type\n The compute and memory capacity of the nodes in the cache cluster.\n cache.t1.micro, cache.m1.small, etc. See: https://boto.readthedocs.io/en/latest/ref/elasticache.html#boto.elasticache.layer1.ElastiCacheConnection.create_cache_cluster\n\n num_cache_nodes\n The number of cache nodes that the cache cluster will have.\n\n preferred_availability_zone\n The EC2 Availability Zone in which the cache cluster will be created.\n All cache nodes belonging to a cache cluster are placed in the\n preferred availability zone.\n\n port\n The port number on which each of the cache nodes will accept\n connections.\n\n cache_parameter_group_name\n The name of the cache parameter group to associate with this cache\n cluster. If this argument is omitted, the default cache parameter group\n for the specified engine will be used.\n\n cache_security_group_names\n A list of cache security group names to associate with this cache\n cluster. Use this parameter only when you are creating a cluster\n outside of a VPC.\n\n replication_group_id\n The replication group to which this cache cluster should belong. If\n this parameter is specified, the cache cluster will be added to the\n specified replication group as a read replica; otherwise, the cache\n cluster will be a standalone primary that is not part of any\n replication group.\n\n auto_minor_version_upgrade\n Determines whether minor engine upgrades will be applied automatically\n to the cache cluster during the maintenance window. A value of True\n allows these upgrades to occur; False disables automatic upgrades.\n\n security_group_ids\n One or more VPC security groups associated with the cache cluster. Use\n this parameter only when you are creating a cluster in a VPC.\n\n cache_subnet_group_name\n The name of the cache subnet group to be used for the cache cluster.\n Use this parameter only when you are creating a cluster in a VPC.\n\n engine_version\n The version number of the cache engine to be used for this cluster.\n\n notification_topic_arn\n The Amazon Resource Name (ARN) of the Amazon Simple Notification\n Service (SNS) topic to which notifications will be sent. The Amazon SNS\n topic owner must be the same as the cache cluster owner.\n\n preferred_maintenance_window\n The weekly time range (in UTC) during which system maintenance can\n occur. Example: sun:05:00-sun:09:00\n\n wait\n Boolean. Wait for confirmation from boto that the cluster is in the\n available state.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n if cache_security_group_names and cache_subnet_group_name:\n _subnet_group = __salt__['boto_elasticache.get_cache_subnet_group'](\n cache_subnet_group_name, region, key, keyid, profile\n )\n vpc_id = _subnet_group['vpc_id']\n if not security_group_ids:\n security_group_ids = []\n _security_group_ids = __salt__['boto_secgroup.convert_to_group_ids'](\n groups=cache_security_group_names,\n vpc_id=vpc_id,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile\n )\n security_group_ids.extend(_security_group_ids)\n cache_security_group_names = None\n config = __salt__['boto_elasticache.get_config'](name, region, key, keyid,\n profile)\n if config is None:\n msg = 'Failed to retrieve cache cluster info from AWS.'\n ret['comment'] = msg\n ret['result'] = None\n return ret\n elif not config:\n if __opts__['test']:\n msg = 'Cache cluster {0} is set to be created.'.format(name)\n ret['comment'] = msg\n ret['result'] = None\n return ret\n created = __salt__['boto_elasticache.create'](\n name=name, num_cache_nodes=num_cache_nodes,\n cache_node_type=cache_node_type, engine=engine,\n replication_group_id=replication_group_id,\n engine_version=engine_version,\n cache_parameter_group_name=cache_parameter_group_name,\n cache_subnet_group_name=cache_subnet_group_name,\n cache_security_group_names=cache_security_group_names,\n security_group_ids=security_group_ids,\n preferred_availability_zone=preferred_availability_zone,\n preferred_maintenance_window=preferred_maintenance_window,\n port=port, notification_topic_arn=notification_topic_arn,\n auto_minor_version_upgrade=auto_minor_version_upgrade,\n wait=wait, region=region, key=key, keyid=keyid, profile=profile)\n if created:\n ret['changes']['old'] = None\n config = __salt__['boto_elasticache.get_config'](name, region, key,\n keyid, profile)\n ret['changes']['new'] = config\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to create {0} cache cluster.'.format(name)\n return ret\n # TODO: support modification of existing elasticache clusters\n else:\n ret['comment'] = 'Cache cluster {0} is present.'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring a cache subnet group exists.", "response": "def subnet_group_present(name, subnet_ids=None, subnet_names=None,\n description=None, tags=None, region=None,\n key=None, keyid=None, profile=None):\n '''\n Ensure ElastiCache subnet group exists.\n\n .. versionadded:: 2015.8.0\n\n name\n The name for the ElastiCache subnet group. This value is stored as a lowercase string.\n\n subnet_ids\n A list of VPC subnet IDs for the cache subnet group. Exclusive with subnet_names.\n\n subnet_names\n A list of VPC subnet names for the cache subnet group. Exclusive with subnet_ids.\n\n description\n Subnet group description.\n\n tags\n A list of tags.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string) that\n contains a dict with region, key and keyid.\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n exists = __salt__['boto_elasticache.subnet_group_exists'](name=name, tags=tags, region=region, key=key,\n keyid=keyid, profile=profile)\n if not exists:\n if __opts__['test']:\n ret['comment'] = 'Subnet group {0} is set to be created.'.format(name)\n ret['result'] = None\n return ret\n created = __salt__['boto_elasticache.create_subnet_group'](name=name, subnet_ids=subnet_ids,\n subnet_names=subnet_names,\n description=description, tags=tags,\n region=region, key=key, keyid=keyid,\n profile=profile)\n if not created:\n ret['result'] = False\n ret['comment'] = 'Failed to create {0} subnet group.'.format(name)\n return ret\n ret['changes']['old'] = None\n ret['changes']['new'] = name\n ret['comment'] = 'Subnet group {0} created.'.format(name)\n return ret\n ret['comment'] = 'Subnet group present.'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a new replication group.", "response": "def creategroup(name, primary_cluster_id, replication_group_description, wait=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure the a replication group is create.\n\n name\n Name of replication group\n\n wait\n Waits for the group to be available\n\n primary_cluster_id\n Name of the master cache node\n\n replication_group_description\n Description for the group\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n '''\n ret = {'name': name, 'result': None, 'comment': '', 'changes': {}}\n is_present = __salt__['boto_elasticache.group_exists'](name, region, key, keyid,\n profile)\n if not is_present:\n if __opts__['test']:\n ret['comment'] = 'Replication {0} is set to be created.'.format(\n name)\n ret['result'] = None\n created = __salt__['boto_elasticache.create_replication_group'](name, primary_cluster_id,\n replication_group_description,\n wait, region, key, keyid, profile)\n if created:\n config = __salt__['boto_elasticache.describe_replication_group'](name, region, key, keyid, profile)\n ret['changes']['old'] = None\n ret['changes']['new'] = config\n ret['result'] = True\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to create {0} replication group.'.format(name)\n else:\n ret['comment'] = '{0} replication group exists .'.format(name)\n ret['result'] = True\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _clear_dict(endpoint_props):\n '''\n Eliminates None entries from the features of the endpoint dict.\n '''\n return dict(\n (prop_name, prop_val)\n for prop_name, prop_val in six.iteritems(endpoint_props)\n if prop_val is not None\n )", "response": "Clears the dictionary of all None entries from the features of the endpoint dict."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _ignore_keys(endpoint_props):\n '''\n Ignores some keys that might be different without any important info.\n These keys are defined under _DO_NOT_COMPARE_FIELDS.\n '''\n return dict(\n (prop_name, prop_val)\n for prop_name, prop_val in six.iteritems(endpoint_props)\n if prop_name not in _DO_NOT_COMPARE_FIELDS\n )", "response": "Ignores some keys that might be different without any important info."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _unique(list_of_dicts):\n '''\n Returns an unique list of dictionaries given a list that may contain duplicates.\n '''\n unique_list = []\n for ele in list_of_dicts:\n if ele not in unique_list:\n unique_list.append(ele)\n return unique_list", "response": "Returns an unique list of dictionaries given a list that may contain duplicates."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nclears the dictionary of _ignore_keys and _clear_dict.", "response": "def _clear_ignore(endpoint_props):\n '''\n Both _clear_dict and _ignore_keys in a single iteration.\n '''\n return dict(\n (prop_name, prop_val)\n for prop_name, prop_val in six.iteritems(endpoint_props)\n if prop_name not in _DO_NOT_COMPARE_FIELDS and prop_val is not None\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _find_match(ele, lst):\n '''\n Find a matching element in a list.\n '''\n for _ele in lst:\n for match_key in _MATCH_KEYS:\n if _ele.get(match_key) == ele.get(match_key):\n return ele", "response": "Find a matching element in a list."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a dict with fields that differ between two dicts.", "response": "def _update_on_fields(prev_ele, new_ele):\n '''\n Return a dict with fields that differ between two dicts.\n '''\n fields_update = dict(\n (prop_name, prop_val)\n for prop_name, prop_val in six.iteritems(new_ele)\n if new_ele.get(prop_name) != prev_ele.get(prop_name) or prop_name in _MATCH_KEYS\n )\n if len(set(fields_update.keys()) | set(_MATCH_KEYS)) > len(set(_MATCH_KEYS)):\n if 'id' not in fields_update:\n # in case of update, the ID is necessary\n # if not specified in the pillar,\n # will try to get it from the prev_ele\n fields_update['id'] = prev_ele['id']\n return fields_update"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncomputing the differences between the expected and configured endpoints.", "response": "def _compute_diff(expected_endpoints, configured_endpoints):\n '''\n Compares configured endpoints with the expected configuration and returns the differences.\n '''\n new_endpoints = []\n update_endpoints = []\n remove_endpoints = []\n\n ret = _compute_diff_ret()\n\n # noth configured => configure with expected endpoints\n if not configured_endpoints:\n ret.update({\n 'add': expected_endpoints\n })\n return ret\n\n # noting expected => remove everything\n if not expected_endpoints:\n ret.update({\n 'remove': configured_endpoints\n })\n return ret\n\n expected_endpoints_clear = _clear_ignore_list(expected_endpoints)\n configured_endpoints_clear = _clear_ignore_list(configured_endpoints)\n\n for expected_endpoint_clear in expected_endpoints_clear:\n if expected_endpoint_clear not in configured_endpoints_clear:\n # none equal => add or update\n matching_ele = _find_match(expected_endpoint_clear, configured_endpoints_clear)\n if not matching_ele:\n # new element => add\n new_endpoints.append(expected_endpoint_clear)\n else:\n # element matched, but some fields are different\n update_fields = _update_on_fields(matching_ele, expected_endpoint_clear)\n if update_fields:\n update_endpoints.append(update_fields)\n for configured_endpoint_clear in configured_endpoints_clear:\n if configured_endpoint_clear not in expected_endpoints_clear:\n matching_ele = _find_match(configured_endpoint_clear, expected_endpoints_clear)\n if not matching_ele:\n # no match found => remove\n remove_endpoints.append(configured_endpoint_clear)\n\n return {\n 'add': new_endpoints,\n 'update': update_endpoints,\n 'remove': remove_endpoints\n }"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a new entry under a specific endpoint.", "response": "def create(name,\n endpoint='incidents',\n api_url=None,\n page_id=None,\n api_key=None,\n api_version=None,\n **kwargs):\n '''\n Insert a new entry under a specific endpoint.\n\n endpoint: incidents\n Insert under this specific endpoint.\n\n page_id\n Page ID. Can also be specified in the config file.\n\n api_key\n API key. Can also be specified in the config file.\n\n api_version: 1\n API version. Can also be specified in the config file.\n\n api_url\n Custom API URL in case the user has a StatusPage service running in a custom environment.\n\n kwargs\n Other params.\n\n SLS Example:\n\n .. code-block:: yaml\n\n create-my-component:\n statuspage.create:\n - endpoint: components\n - name: my component\n - group_id: 993vgplshj12\n '''\n ret = _default_ret(name)\n endpoint_sg = endpoint[:-1] # singular\n if __opts__['test']:\n ret['comment'] = 'The following {endpoint} would be created:'.format(endpoint=endpoint_sg)\n ret['result'] = None\n ret['changes'][endpoint] = {}\n for karg, warg in six.iteritems(kwargs):\n if warg is None or karg.startswith('__'):\n continue\n ret['changes'][endpoint][karg] = warg\n return ret\n sp_create = __salt__['statuspage.create'](endpoint=endpoint,\n api_url=api_url,\n page_id=page_id,\n api_key=api_key,\n api_version=api_version,\n **kwargs)\n if not sp_create.get('result'):\n ret['comment'] = 'Unable to create {endpoint}: {msg}'.format(endpoint=endpoint_sg,\n msg=sp_create.get('comment'))\n else:\n ret['comment'] = '{endpoint} created!'.format(endpoint=endpoint_sg)\n ret['result'] = True\n ret['changes'] = sp_create.get('out')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating an enpoint entry in a specific endpoint.", "response": "def update(name,\n endpoint='incidents',\n id=None,\n api_url=None,\n page_id=None,\n api_key=None,\n api_version=None,\n **kwargs):\n '''\n Update attribute(s) of a specific endpoint.\n\n id\n The unique ID of the enpoint entry.\n\n endpoint: incidents\n Endpoint name.\n\n page_id\n Page ID. Can also be specified in the config file.\n\n api_key\n API key. Can also be specified in the config file.\n\n api_version: 1\n API version. Can also be specified in the config file.\n\n api_url\n Custom API URL in case the user has a StatusPage service running in a custom environment.\n\n SLS Example:\n\n .. code-block:: yaml\n\n update-my-incident:\n statuspage.update:\n - id: dz959yz2nd4l\n - status: resolved\n '''\n ret = _default_ret(name)\n endpoint_sg = endpoint[:-1] # singular\n if not id:\n log.error('Invalid %s ID', endpoint_sg)\n ret['comment'] = 'Please specify a valid {endpoint} ID'.format(endpoint=endpoint_sg)\n return ret\n if __opts__['test']:\n ret['comment'] = '{endpoint} #{id} would be updated:'.format(endpoint=endpoint_sg, id=id)\n ret['result'] = None\n ret['changes'][endpoint] = {}\n for karg, warg in six.iteritems(kwargs):\n if warg is None or karg.startswith('__'):\n continue\n ret['changes'][endpoint][karg] = warg\n return ret\n sp_update = __salt__['statuspage.update'](endpoint=endpoint,\n id=id,\n api_url=api_url,\n page_id=page_id,\n api_key=api_key,\n api_version=api_version,\n **kwargs)\n if not sp_update.get('result'):\n ret['comment'] = 'Unable to update {endpoint} #{id}: {msg}'.format(endpoint=endpoint_sg,\n id=id,\n msg=sp_update.get('comment'))\n else:\n ret['comment'] = '{endpoint} #{id} updated!'.format(endpoint=endpoint_sg, id=id)\n ret['result'] = True\n ret['changes'] = sp_update.get('out')"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndeletes an entry from an incidents status page", "response": "def delete(name,\n endpoint='incidents',\n id=None,\n api_url=None,\n page_id=None,\n api_key=None,\n api_version=None):\n '''\n Remove an entry from an endpoint.\n\n endpoint: incidents\n Request a specific endpoint.\n\n page_id\n Page ID. Can also be specified in the config file.\n\n api_key\n API key. Can also be specified in the config file.\n\n api_version: 1\n API version. Can also be specified in the config file.\n\n api_url\n Custom API URL in case the user has a StatusPage service running in a custom environment.\n\n SLS Example:\n\n .. code-block:: yaml\n\n delete-my-component:\n statuspage.delete:\n - endpoint: components\n - id: ftgks51sfs2d\n '''\n ret = _default_ret(name)\n endpoint_sg = endpoint[:-1] # singular\n if not id:\n log.error('Invalid %s ID', endpoint_sg)\n ret['comment'] = 'Please specify a valid {endpoint} ID'.format(endpoint=endpoint_sg)\n return ret\n if __opts__['test']:\n ret['comment'] = '{endpoint} #{id} would be removed!'.format(endpoint=endpoint_sg, id=id)\n ret['result'] = None\n sp_delete = __salt__['statuspage.delete'](endpoint=endpoint,\n id=id,\n api_url=api_url,\n page_id=page_id,\n api_key=api_key,\n api_version=api_version)\n if not sp_delete.get('result'):\n ret['comment'] = 'Unable to delete {endpoint} #{id}: {msg}'.format(endpoint=endpoint_sg,\n id=id,\n msg=sp_delete.get('comment'))\n else:\n ret['comment'] = '{endpoint} #{id} deleted!'.format(endpoint=endpoint_sg, id=id)\n ret['result'] = True"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef managed(name,\n config,\n api_url=None,\n page_id=None,\n api_key=None,\n api_version=None,\n pace=_PACE,\n allow_empty=False):\n '''\n Manage the StatusPage configuration.\n\n config\n Dictionary with the expected configuration of the StatusPage.\n The main level keys of this dictionary represent the endpoint name.\n If a certain endpoint does not exist in this structure, it will be ignored / not configured.\n\n page_id\n Page ID. Can also be specified in the config file.\n\n api_key\n API key. Can also be specified in the config file.\n\n api_version: 1\n API version. Can also be specified in the config file.\n\n api_url\n Custom API URL in case the user has a StatusPage service running in a custom environment.\n\n pace: 1\n Max requests per second allowed by the API.\n\n allow_empty: False\n Allow empty config.\n\n SLS example:\n\n .. code-block:: yaml\n\n my-statuspage-config:\n statuspage.managed:\n - config:\n components:\n - name: component1\n group_id: uy4g37rf\n - name: component2\n group_id: 3n4uyu4gf\n incidents:\n - name: incident1\n status: resolved\n impact: major\n backfilled: false\n - name: incident2\n status: investigating\n impact: minor\n '''\n complete_diff = {}\n ret = _default_ret(name)\n if not config and not allow_empty:\n ret.update({\n 'result': False,\n 'comment': 'Cannot remove everything. To allow this, please set the option `allow_empty` as True.'\n })\n return ret\n is_empty = True\n for endpoint_name, endpoint_expected_config in six.iteritems(config):\n if endpoint_expected_config:\n is_empty = False\n endpoint_existing_config_ret = __salt__['statuspage.retrieve'](endpoint=endpoint_name,\n api_url=api_url,\n page_id=page_id,\n api_key=api_key,\n api_version=api_version)\n if not endpoint_existing_config_ret.get('result'):\n ret.update({\n 'comment': endpoint_existing_config_ret.get('comment')\n })\n return ret # stop at first error\n endpoint_existing_config = endpoint_existing_config_ret.get('out')\n complete_diff[endpoint_name] = _compute_diff(endpoint_expected_config, endpoint_existing_config)\n if is_empty and not allow_empty:\n ret.update({\n 'result': False,\n 'comment': 'Cannot remove everything. To allow this, please set the option `allow_empty` as True.'\n })\n return ret\n any_changes = False\n for endpoint_name, endpoint_diff in six.iteritems(complete_diff):\n if endpoint_diff.get('add') or endpoint_diff.get('update') or endpoint_diff.get('remove'):\n any_changes = True\n if not any_changes:\n ret.update({\n 'result': True,\n 'comment': 'No changes required.',\n 'changes': {}\n })\n return ret\n ret.update({\n 'changes': complete_diff\n })\n if __opts__.get('test'):\n ret.update({\n 'comment': 'Testing mode. Would apply the following changes:',\n 'result': None\n })\n return ret\n for endpoint_name, endpoint_diff in six.iteritems(complete_diff):\n endpoint_sg = endpoint_name[:-1] # singular\n for new_endpoint in endpoint_diff.get('add'):\n log.debug('Defining new %s %s',\n endpoint_sg,\n new_endpoint\n )\n adding = __salt__['statuspage.create'](endpoint=endpoint_name,\n api_url=api_url,\n page_id=page_id,\n api_key=api_key,\n api_version=api_version,\n **new_endpoint)\n if not adding.get('result'):\n ret.update({\n 'comment': adding.get('comment')\n })\n return ret\n if pace:\n time.sleep(1/pace)\n for update_endpoint in endpoint_diff.get('update'):\n if 'id' not in update_endpoint:\n continue\n endpoint_id = update_endpoint.pop('id')\n log.debug('Updating %s #%s: %s',\n endpoint_sg,\n endpoint_id,\n update_endpoint\n )\n updating = __salt__['statuspage.update'](endpoint=endpoint_name,\n id=endpoint_id,\n api_url=api_url,\n page_id=page_id,\n api_key=api_key,\n api_version=api_version,\n **update_endpoint)\n if not updating.get('result'):\n ret.update({\n 'comment': updating.get('comment')\n })\n return ret\n if pace:\n time.sleep(1/pace)\n for remove_endpoint in endpoint_diff.get('remove'):\n if 'id' not in remove_endpoint:\n continue\n endpoint_id = remove_endpoint.pop('id')\n log.debug('Removing %s #%s',\n endpoint_sg,\n endpoint_id\n )\n removing = __salt__['statuspage.delete'](endpoint=endpoint_name,\n id=endpoint_id,\n api_url=api_url,\n page_id=page_id,\n api_key=api_key,\n api_version=api_version)\n if not removing.get('result'):\n ret.update({\n 'comment': removing.get('comment')\n })\n return ret\n if pace:\n time.sleep(1/pace)\n ret.update({\n 'result': True,\n 'comment': 'StatusPage updated.'\n })\n return ret", "response": "Manage the status page configuration."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nensure a user is present in the system.", "response": "def present(email, profile=\"splunk\", **kwargs):\n '''\n Ensure a user is present\n\n .. code-block:: yaml\n\n ensure example test user 1:\n splunk.user_present:\n - realname: 'Example TestUser1'\n - name: 'exampleuser'\n - email: 'example@domain.com'\n - roles: ['user']\n\n The following parameters are required:\n\n email\n This is the email of the user in splunk\n '''\n\n name = kwargs.get('name')\n\n ret = {\n 'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''\n }\n\n target = __salt__['splunk.get_user'](email, profile=profile, user_details=True)\n\n if not target:\n if __opts__['test']:\n ret['comment'] = 'User {0} will be created'.format(name)\n return ret\n\n # create the user\n result = __salt__['splunk.create_user'](\n email, profile=profile, **kwargs\n )\n if result:\n ret['changes'].setdefault('old', None)\n ret['changes'].setdefault('new', 'User {0} exists'.format(name))\n ret['result'] = True\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to create {0}'.format(name)\n\n return ret\n else:\n ret['comment'] = 'User {0} set to be updated.'.format(name)\n if __opts__['test']:\n ret['result'] = None\n return ret\n\n # found a user... updating\n result = __salt__['splunk.update_user'](\n email, profile, **kwargs\n )\n\n if isinstance(result, bool) and result:\n # no update\n ret['result'] = None\n ret['comment'] = \"No changes\"\n else:\n diff = {}\n for field in ['name', 'realname', 'roles', 'defaultApp', 'tz', 'capabilities']:\n if field == 'roles':\n diff['roles'] = list(set(target.get(field, [])).symmetric_difference(set(result.get(field, []))))\n elif target.get(field) != result.get(field):\n diff[field] = result.get(field)\n\n newvalues = result\n ret['result'] = True\n ret['changes']['diff'] = diff\n ret['changes']['old'] = target\n ret['changes']['new'] = newvalues\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures a splunk user is absent.", "response": "def absent(email, profile=\"splunk\", **kwargs):\n '''\n Ensure a splunk user is absent\n\n .. code-block:: yaml\n\n ensure example test user 1:\n splunk.absent:\n - email: 'example@domain.com'\n - name: 'exampleuser'\n\n The following parameters are required:\n\n email\n This is the email of the user in splunk\n name\n This is the splunk username used to identify the user.\n\n '''\n user_identity = kwargs.get('name')\n\n ret = {\n 'name': user_identity,\n 'changes': {},\n 'result': None,\n 'comment': 'User {0} is absent.'.format(user_identity)\n }\n\n target = __salt__['splunk.get_user'](email, profile=profile)\n\n if not target:\n ret['comment'] = 'User {0} does not exist'.format(user_identity)\n ret['result'] = True\n return ret\n\n if __opts__['test']:\n ret['comment'] = \"User {0} is all set to be deleted\".format(user_identity)\n ret['result'] = None\n return ret\n\n result = __salt__['splunk.delete_user'](email, profile=profile)\n\n if result:\n ret['comment'] = 'Deleted user {0}'.format(user_identity)\n ret['changes'].setdefault('old', 'User {0} exists'.format(user_identity))\n ret['changes'].setdefault('new', 'User {0} deleted'.format(user_identity))\n ret['result'] = True\n\n else:\n ret['comment'] = 'Failed to delete {0}'.format(user_identity)\n ret['result'] = False\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef system(name):\n '''\n Set the keyboard layout for the system\n\n name\n The keyboard layout to use\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n if __salt__['keyboard.get_sys']() == name:\n ret['result'] = True\n ret['comment'] = 'System layout {0} already set'.format(name)\n return ret\n if __opts__['test']:\n ret['comment'] = 'System layout {0} needs to be set'.format(name)\n return ret\n if __salt__['keyboard.set_sys'](name):\n ret['changes'] = {'layout': name}\n ret['result'] = True\n ret['comment'] = 'Set system keyboard layout {0}'.format(name)\n return ret\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to set system keyboard layout'\n return ret", "response": "Set the keyboard layout for the system"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsets the keyboard layout for XOrg", "response": "def xorg(name):\n '''\n Set the keyboard layout for XOrg\n\n layout\n The keyboard layout to use\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n if __salt__['keyboard.get_x']() == name:\n ret['result'] = True\n ret['comment'] = 'XOrg layout {0} already set'.format(name)\n return ret\n if __opts__['test']:\n ret['comment'] = 'XOrg layout {0} needs to be set'.format(name)\n return ret\n if __salt__['keyboard.set_x'](name):\n ret['changes'] = {'layout': name}\n ret['result'] = True\n ret['comment'] = 'Set XOrg keyboard layout {0}'.format(name)\n return ret\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to set XOrg keyboard layout'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _status_wait(service_name, end_time, service_states):\n '''\n Helper function that will wait for the status of the service to match the\n provided status before an end time expires. Used for service stop and start\n\n .. versionadded:: 2017.7.9,2018.3.4\n\n Args:\n service_name (str):\n The name of the service\n\n end_time (float):\n A future time. e.g. time.time() + 10\n\n service_states (list):\n Services statuses to wait for as returned by info()\n\n Returns:\n dict: A dictionary containing information about the service.\n\n :codeauthor: Damon Atkins <https://github.com/damon-atkins>\n '''\n info_results = info(service_name)\n\n while info_results['Status'] in service_states and time.time() < end_time:\n # From Microsoft: Do not wait longer than the wait hint. A good interval\n # is one-tenth of the wait hint but not less than 1 second and not more\n # than 10 seconds.\n # https://docs.microsoft.com/en-us/windows/desktop/services/starting-a-service\n # https://docs.microsoft.com/en-us/windows/desktop/services/stopping-a-service\n # Wait hint is in ms\n wait_time = info_results['Status_WaitHint']\n # Convert to seconds or 0\n wait_time = wait_time / 1000 if wait_time else 0\n if wait_time < 1:\n wait_time = 1\n elif wait_time > 10:\n wait_time = 10\n\n time.sleep(wait_time)\n info_results = info(service_name)\n\n return info_results", "response": "Helper function that will wait for the status of the service to match the end time. Used for service start and stop."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _cmd_quote(cmd):\n r'''\n Helper function to properly format the path to the binary for the service\n Must be wrapped in double quotes to account for paths that have spaces. For\n example:\n\n ``\"C:\\Program Files\\Path\\to\\bin.exe\"``\n\n Args:\n cmd (str): Full path to the binary\n\n Returns:\n str: Properly quoted path to the binary\n '''\n # Remove all single and double quotes from the beginning and the end\n pattern = re.compile('^(\\\\\"|\\').*|.*(\\\\\"|\\')$')\n while pattern.match(cmd) is not None:\n cmd = cmd.strip('\"').strip('\\'')\n # Ensure the path to the binary is wrapped in double quotes to account for\n # spaces in the path\n cmd = '\"{0}\"'.format(cmd)\n return cmd", "response": "r Removes single and double quotes from the command to ensure that the path to the binary is wrapped in double quotes."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_enabled():\n '''\n Return a list of enabled services. Enabled is defined as a service that is\n marked to Auto Start.\n\n Returns:\n list: A list of enabled services\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.get_enabled\n '''\n raw_services = _get_services()\n services = set()\n for service in raw_services:\n if info(service['ServiceName'])['StartType'] in ['Auto']:\n services.add(service['ServiceName'])\n\n return sorted(services)", "response": "Return a list of enabled services."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef available(name):\n '''\n Check if a service is available on the system.\n\n Args:\n name (str): The name of the service to check\n\n Returns:\n bool: ``True`` if the service is available, ``False`` otherwise\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.available <service name>\n '''\n for service in get_all():\n if name.lower() == service.lower():\n return True\n\n return False", "response": "Check if a service is available on the system."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_services():\n '''\n Returns a list of all services on the system.\n '''\n handle_scm = win32service.OpenSCManager(\n None, None, win32service.SC_MANAGER_ENUMERATE_SERVICE)\n\n try:\n services = win32service.EnumServicesStatusEx(handle_scm)\n except AttributeError:\n services = win32service.EnumServicesStatus(handle_scm)\n finally:\n win32service.CloseServiceHandle(handle_scm)\n\n return services", "response": "Returns a list of all services on the system."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_all():\n '''\n Return all installed services\n\n Returns:\n list: Returns a list of all services on the system.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.get_all\n '''\n services = _get_services()\n\n ret = set()\n for service in services:\n ret.add(service['ServiceName'])\n\n return sorted(ret)", "response": "Returns a list of all installed services on the system"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_service_name(*args):\n '''\n The Display Name is what is displayed in Windows when services.msc is\n executed. Each Display Name has an associated Service Name which is the\n actual name of the service. This function allows you to discover the\n Service Name by returning a dictionary of Display Names and Service Names,\n or filter by adding arguments of Display Names.\n\n If no args are passed, return a dict of all services where the keys are the\n service Display Names and the values are the Service Names.\n\n If arguments are passed, create a dict of Display Names and Service Names\n\n Returns:\n dict: A dictionary of display names and service names\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' service.get_service_name\n salt '*' service.get_service_name 'Google Update Service (gupdate)' 'DHCP Client'\n '''\n raw_services = _get_services()\n\n services = dict()\n for raw_service in raw_services:\n if args:\n if raw_service['DisplayName'] in args or \\\n raw_service['ServiceName'] in args or \\\n raw_service['ServiceName'].lower() in args:\n services[raw_service['DisplayName']] = raw_service['ServiceName']\n else:\n services[raw_service['DisplayName']] = raw_service['ServiceName']\n\n return services", "response": "This function returns a dictionary of Display Names and Service Names."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets information about a service on the system.", "response": "def info(name):\n '''\n Get information about a service on the system\n\n Args:\n name (str): The name of the service. This is not the display name. Use\n ``get_service_name`` to find the service name.\n\n Returns:\n dict: A dictionary containing information about the service.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.info spooler\n '''\n try:\n handle_scm = win32service.OpenSCManager(\n None, None, win32service.SC_MANAGER_CONNECT)\n except pywintypes.error as exc:\n raise CommandExecutionError(\n 'Failed to connect to the SCM: {0}'.format(exc.strerror))\n\n try:\n handle_svc = win32service.OpenService(\n handle_scm, name,\n win32service.SERVICE_ENUMERATE_DEPENDENTS |\n win32service.SERVICE_INTERROGATE |\n win32service.SERVICE_QUERY_CONFIG |\n win32service.SERVICE_QUERY_STATUS)\n except pywintypes.error as exc:\n raise CommandExecutionError(\n 'Failed To Open {0}: {1}'.format(name, exc.strerror))\n\n try:\n config_info = win32service.QueryServiceConfig(handle_svc)\n status_info = win32service.QueryServiceStatusEx(handle_svc)\n\n try:\n description = win32service.QueryServiceConfig2(\n handle_svc, win32service.SERVICE_CONFIG_DESCRIPTION)\n except pywintypes.error:\n description = 'Failed to get description'\n\n delayed_start = win32service.QueryServiceConfig2(\n handle_svc, win32service.SERVICE_CONFIG_DELAYED_AUTO_START_INFO)\n finally:\n win32service.CloseServiceHandle(handle_scm)\n win32service.CloseServiceHandle(handle_svc)\n\n ret = dict()\n try:\n sid = win32security.LookupAccountName(\n '', 'NT Service\\\\{0}'.format(name))[0]\n ret['sid'] = win32security.ConvertSidToStringSid(sid)\n except pywintypes.error:\n ret['sid'] = 'Failed to get SID'\n\n ret['BinaryPath'] = config_info[3]\n ret['LoadOrderGroup'] = config_info[4]\n ret['TagID'] = config_info[5]\n ret['Dependencies'] = config_info[6]\n ret['ServiceAccount'] = config_info[7]\n ret['DisplayName'] = config_info[8]\n ret['Description'] = description\n ret['Status_ServiceCode'] = status_info['ServiceSpecificExitCode']\n ret['Status_CheckPoint'] = status_info['CheckPoint']\n ret['Status_WaitHint'] = status_info['WaitHint']\n ret['StartTypeDelayed'] = delayed_start\n\n flags = list()\n for bit in SERVICE_TYPE:\n if isinstance(bit, int):\n if config_info[0] & bit:\n flags.append(SERVICE_TYPE[bit])\n\n ret['ServiceType'] = flags if flags else config_info[0]\n\n flags = list()\n for bit in SERVICE_CONTROLS:\n if status_info['ControlsAccepted'] & bit:\n flags.append(SERVICE_CONTROLS[bit])\n\n ret['ControlsAccepted'] = flags if flags else status_info['ControlsAccepted']\n\n try:\n ret['Status_ExitCode'] = SERVICE_ERRORS[status_info['Win32ExitCode']]\n except KeyError:\n ret['Status_ExitCode'] = status_info['Win32ExitCode']\n\n try:\n ret['StartType'] = SERVICE_START_TYPE[config_info[1]]\n except KeyError:\n ret['StartType'] = config_info[1]\n\n try:\n ret['ErrorControl'] = SERVICE_ERROR_CONTROL[config_info[2]]\n except KeyError:\n ret['ErrorControl'] = config_info[2]\n\n try:\n ret['Status'] = SERVICE_STATE[status_info['CurrentState']]\n except KeyError:\n ret['Status'] = status_info['CurrentState']\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef start(name, timeout=90, with_deps=False, with_parents=False):\n '''\n Start the specified service.\n\n .. warning::\n You cannot start a disabled service in Windows. If the service is\n disabled, it will be changed to ``Manual`` start.\n\n Args:\n name (str): The name of the service to start\n\n timeout (int):\n The time in seconds to wait for the service to start before\n returning. Default is 90 seconds\n\n .. versionadded:: 2017.7.9,2018.3.4\n\n with_deps (bool):\n If enabled start the given service and the services the current\n service depends on.\n\n with_parents (bool):\n If enabled and in case other running services depend on the to be start\n service, this flag indicates that those other services will be started\n as well.\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``. Also returns ``True``\n if the service is already started\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.start <service name>\n '''\n # Set the service to manual if disabled\n if disabled(name):\n modify(name, start_type='Manual')\n\n ret = set()\n\n # Using a list here to maintain order\n services = ServiceDependencies(name, get_all, info)\n start = services.start_order(with_deps=with_deps, with_parents=with_parents)\n log.debug(\"Starting services %s\", start)\n for name in start:\n try:\n win32serviceutil.StartService(name)\n except pywintypes.error as exc:\n if exc.winerror != 1056:\n raise CommandExecutionError(\n 'Failed To Start {0}: {1}'.format(name, exc.strerror))\n log.debug('Service \"%s\" is running', name)\n\n srv_status = _status_wait(service_name=name,\n end_time=time.time() + int(timeout),\n service_states=['Start Pending', 'Stopped'])\n ret.add(srv_status['Status'] == 'Running')\n return False not in ret", "response": "Start a service in the specified language."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef stop(name, timeout=90, with_deps=False, with_parents=False):\n '''\n Stop the specified service\n\n Args:\n name (str): The name of the service to stop\n\n timeout (int):\n The time in seconds to wait for the service to stop before\n returning. Default is 90 seconds\n\n .. versionadded:: 2017.7.9,2018.3.4\n\n with_deps (bool):\n If enabled stop the given service and the services\n the current service depends on.\n\n with_parents (bool):\n If enabled and in case other running services depend on the to be stopped\n service, this flag indicates that those other services will be stopped\n as well.\n If disabled, the service stop will fail in case other running services\n depend on the to be stopped service.\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``. Also returns ``True``\n if the service is already stopped\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.stop <service name>\n '''\n ret = set()\n\n services = ServiceDependencies(name, get_all, info)\n stop = services.stop_order(with_deps=with_deps, with_parents=with_parents)\n log.debug(\"Stopping services %s\", stop)\n for name in stop:\n try:\n win32serviceutil.StopService(name)\n except pywintypes.error as exc:\n if exc.winerror != 1062:\n raise CommandExecutionError(\n 'Failed To Stop {0}: {1}'.format(name, exc.strerror))\n log.debug('Service \"%s\" is not running', name)\n\n srv_status = _status_wait(service_name=name,\n end_time=time.time() + int(timeout),\n service_states=['Running', 'Stop Pending'])\n ret.add(srv_status['Status'] == 'Stopped')\n return False not in ret", "response": "Stop a service in the specified language."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nrestarts the named service.", "response": "def restart(name, timeout=90, with_deps=False, with_parents=False):\n '''\n Restart the named service. This issues a stop command followed by a start.\n\n Args:\n name: The name of the service to restart.\n\n .. note::\n If the name passed is ``salt-minion`` a scheduled task is\n created and executed to restart the salt-minion service.\n\n timeout (int):\n The time in seconds to wait for the service to stop and start before\n returning. Default is 90 seconds\n\n .. note::\n The timeout is cumulative meaning it is applied to the stop and\n then to the start command. A timeout of 90 could take up to 180\n seconds if the service is long in stopping and starting\n\n .. versionadded:: 2017.7.9,2018.3.4\n\n with_deps (bool):\n If enabled restart the given service and the services\n the current service depends on.\n\n with_parents (bool):\n If enabled and in case other running services depend on the to be\n restarted service, this flag indicates that those other services\n will be restarted as well.\n If disabled, the service restart will fail in case other running\n services depend on the to be restarted service.\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.restart <service name>\n '''\n if 'salt-minion' in name:\n create_win_salt_restart_task()\n return execute_salt_restart_task()\n\n ret = set()\n ret.add(stop(name=name, timeout=timeout, with_deps=with_deps, with_parents=with_parents))\n ret.add(start(name=name, timeout=timeout, with_deps=with_deps, with_parents=with_parents))\n return False not in ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a task in Windows task scheduler to enable restarting the salt - minion", "response": "def create_win_salt_restart_task():\n '''\n Create a task in Windows task scheduler to enable restarting the salt-minion\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.create_win_salt_restart_task()\n '''\n cmd = 'cmd'\n args = '/c ping -n 3 127.0.0.1 && net stop salt-minion && net start ' \\\n 'salt-minion'\n return __salt__['task.create_task'](name='restart-salt-minion',\n user_name='System',\n force=True,\n action_type='Execute',\n cmd=cmd,\n arguments=args,\n trigger_type='Once',\n start_date='1975-01-01',\n start_time='01:00')"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef status(name, *args, **kwargs):\n '''\n Return the status for a service.\n If the name contains globbing, a dict mapping service name to True/False\n values is returned.\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\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 <service name>\n '''\n\n results = {}\n all_services = get_all()\n contains_globbing = bool(re.search(r'\\*|\\?|\\[.+\\]', name))\n if contains_globbing:\n services = fnmatch.filter(all_services, name)\n else:\n services = [name]\n for service in services:\n results[service] = info(service)['Status'] in ['Running', 'Stop Pending']\n if contains_globbing:\n return results\n return results[name]", "response": "Return the status for a service."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nmodifies a service s parameters.", "response": "def modify(name,\n bin_path=None,\n exe_args=None,\n display_name=None,\n description=None,\n service_type=None,\n start_type=None,\n start_delayed=None,\n error_control=None,\n load_order_group=None,\n dependencies=None,\n account_name=None,\n account_password=None,\n run_interactive=None):\n # pylint: disable=anomalous-backslash-in-string\n '''\n Modify a service's parameters. Changes will not be made for parameters that\n are not passed.\n\n .. versionadded:: 2016.11.0\n\n Args:\n name (str):\n The name of the service. Can be found using the\n ``service.get_service_name`` function\n\n bin_path (str):\n The path to the service executable. Backslashes must be escaped, eg:\n ``C:\\\\path\\\\to\\\\binary.exe``\n\n exe_args (str):\n Any arguments required by the service executable\n\n display_name (str):\n The name to display in the service manager\n\n description (str):\n The description to display for the service\n\n service_type (str):\n Specifies the service type. Default is ``own``. Valid options are as\n follows:\n\n - kernel: Driver service\n - filesystem: File system driver service\n - adapter: Adapter driver service (reserved)\n - recognizer: Recognizer driver service (reserved)\n - own (default): Service runs in its own process\n - share: Service shares a process with one or more other services\n\n start_type (str):\n Specifies the service start type. Valid options are as follows:\n\n - boot: Device driver that is loaded by the boot loader\n - system: Device driver that is started during kernel initialization\n - auto: Service that automatically starts\n - manual: Service must be started manually\n - disabled: Service cannot be started\n\n start_delayed (bool):\n Set the service to Auto(Delayed Start). Only valid if the start_type\n is set to ``Auto``. If service_type is not passed, but the service\n is already set to ``Auto``, then the flag will be set.\n\n error_control (str):\n The severity of the error, and action taken, if this service fails\n to start. Valid options are as follows:\n\n - normal: Error is logged and a message box is displayed\n - severe: Error is logged and computer attempts a restart with the\n last known good configuration\n - critical: Error is logged, computer attempts to restart with the\n last known good configuration, system halts on failure\n - ignore: Error is logged and startup continues, no notification is\n given to the user\n\n load_order_group (str):\n The name of the load order group to which this service belongs\n\n dependencies (list):\n A list of services or load ordering groups that must start before\n this service\n\n account_name (str):\n The name of the account under which the service should run. For\n ``own`` type services this should be in the ``domain\\\\username``\n format. The following are examples of valid built-in service\n accounts:\n\n - NT Authority\\\\LocalService\n - NT Authority\\\\NetworkService\n - NT Authority\\\\LocalSystem\n - .\\LocalSystem\n\n account_password (str):\n The password for the account name specified in ``account_name``. For\n the above built-in accounts, this can be None. Otherwise a password\n must be specified.\n\n run_interactive (bool):\n If this setting is True, the service will be allowed to interact\n with the user. Not recommended for services that run with elevated\n privileges.\n\n Returns:\n dict: a dictionary of changes made\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.modify spooler start_type=disabled\n\n '''\n # pylint: enable=anomalous-backslash-in-string\n # https://msdn.microsoft.com/en-us/library/windows/desktop/ms681987(v=vs.85).aspx\n # https://msdn.microsoft.com/en-us/library/windows/desktop/ms681988(v-vs.85).aspx\n handle_scm = win32service.OpenSCManager(\n None, None, win32service.SC_MANAGER_CONNECT)\n\n try:\n handle_svc = win32service.OpenService(\n handle_scm,\n name,\n win32service.SERVICE_CHANGE_CONFIG |\n win32service.SERVICE_QUERY_CONFIG)\n except pywintypes.error as exc:\n raise CommandExecutionError(\n 'Failed To Open {0}: {1}'.format(name, exc.strerror))\n\n config_info = win32service.QueryServiceConfig(handle_svc)\n\n changes = dict()\n\n # Input Validation\n if bin_path is not None:\n # shlex.quote the path to the binary\n bin_path = _cmd_quote(bin_path)\n if exe_args is not None:\n bin_path = '{0} {1}'.format(bin_path, exe_args)\n changes['BinaryPath'] = bin_path\n\n if service_type is not None:\n if service_type.lower() in SERVICE_TYPE:\n service_type = SERVICE_TYPE[service_type.lower()]\n if run_interactive:\n service_type = service_type | \\\n win32service.SERVICE_INTERACTIVE_PROCESS\n else:\n raise CommandExecutionError(\n 'Invalid Service Type: {0}'.format(service_type))\n else:\n if run_interactive is True:\n service_type = config_info[0] | \\\n win32service.SERVICE_INTERACTIVE_PROCESS\n elif run_interactive is False:\n service_type = config_info[0] ^ \\\n win32service.SERVICE_INTERACTIVE_PROCESS\n else:\n service_type = win32service.SERVICE_NO_CHANGE\n\n if service_type is not win32service.SERVICE_NO_CHANGE:\n flags = list()\n for bit in SERVICE_TYPE:\n if isinstance(bit, int) and service_type & bit:\n flags.append(SERVICE_TYPE[bit])\n\n changes['ServiceType'] = flags if flags else service_type\n\n if start_type is not None:\n if start_type.lower() in SERVICE_START_TYPE:\n start_type = SERVICE_START_TYPE[start_type.lower()]\n else:\n raise CommandExecutionError(\n 'Invalid Start Type: {0}'.format(start_type))\n changes['StartType'] = SERVICE_START_TYPE[start_type]\n else:\n start_type = win32service.SERVICE_NO_CHANGE\n\n if error_control is not None:\n if error_control.lower() in SERVICE_ERROR_CONTROL:\n error_control = SERVICE_ERROR_CONTROL[error_control.lower()]\n else:\n raise CommandExecutionError(\n 'Invalid Error Control: {0}'.format(error_control))\n changes['ErrorControl'] = SERVICE_ERROR_CONTROL[error_control]\n else:\n error_control = win32service.SERVICE_NO_CHANGE\n\n if account_name is not None:\n changes['ServiceAccount'] = account_name\n if account_name in ['LocalSystem', 'LocalService', 'NetworkService']:\n account_password = ''\n\n if account_password is not None:\n changes['ServiceAccountPassword'] = 'XXX-REDACTED-XXX'\n\n if load_order_group is not None:\n changes['LoadOrderGroup'] = load_order_group\n\n if dependencies is not None:\n changes['Dependencies'] = dependencies\n\n if display_name is not None:\n changes['DisplayName'] = display_name\n\n win32service.ChangeServiceConfig(handle_svc,\n service_type,\n start_type,\n error_control,\n bin_path,\n load_order_group,\n 0,\n dependencies,\n account_name,\n account_password,\n display_name)\n\n if description is not None:\n win32service.ChangeServiceConfig2(\n handle_svc, win32service.SERVICE_CONFIG_DESCRIPTION, description)\n changes['Description'] = description\n\n if start_delayed is not None:\n # You can only set delayed start for services that are set to auto start\n # Start type 2 is Auto\n # Start type -1 is no change\n if (start_type == -1 and config_info[1] == 2) or start_type == 2:\n win32service.ChangeServiceConfig2(\n handle_svc, win32service.SERVICE_CONFIG_DELAYED_AUTO_START_INFO,\n start_delayed)\n changes['StartTypeDelayed'] = start_delayed\n else:\n changes['Warning'] = 'start_delayed: Requires start_type \"auto\"'\n\n win32service.CloseServiceHandle(handle_scm)\n win32service.CloseServiceHandle(handle_svc)\n\n return changes"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nenable the named service to start at boot", "response": "def enable(name, start_type='auto', start_delayed=False, **kwargs):\n '''\n Enable the named service to start at boot\n\n Args:\n name (str): The name of the service to enable.\n\n start_type (str): Specifies the service start type. Valid options are as\n follows:\n\n - boot: Device driver that is loaded by the boot loader\n - system: Device driver that is started during kernel initialization\n - auto: Service that automatically starts\n - manual: Service must be started manually\n - disabled: Service cannot be started\n\n start_delayed (bool): Set the service to Auto(Delayed Start). Only valid\n if the start_type is set to ``Auto``. If service_type is not passed,\n but the service is already set to ``Auto``, then the flag will be\n set.\n\n Returns:\n bool: ``True`` if successful, ``False`` otherwise\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.enable <service name>\n '''\n\n modify(name, start_type=start_type, start_delayed=start_delayed)\n svcstat = info(name)\n if start_type.lower() == 'auto':\n return svcstat['StartType'].lower() == start_type.lower() and svcstat['StartTypeDelayed'] == start_delayed\n else:\n return svcstat['StartType'].lower() == start_type.lower()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a new service in the service manager.", "response": "def create(name,\n bin_path,\n exe_args=None,\n display_name=None,\n description=None,\n service_type='own',\n start_type='manual',\n start_delayed=False,\n error_control='normal',\n load_order_group=None,\n dependencies=None,\n account_name='.\\\\LocalSystem',\n account_password=None,\n run_interactive=False,\n **kwargs):\n '''\n Create the named service.\n\n .. versionadded:: 2015.8.0\n\n Args:\n\n name (str):\n Specifies the service name. This is not the display_name\n\n bin_path (str):\n Specifies the path to the service binary file. Backslashes must be\n escaped, eg: ``C:\\\\path\\\\to\\\\binary.exe``\n\n exe_args (str):\n Any additional arguments required by the service binary.\n\n display_name (str):\n The name to be displayed in the service manager. If not passed, the\n ``name`` will be used\n\n description (str):\n A description of the service\n\n service_type (str):\n Specifies the service type. Default is ``own``. Valid options are as\n follows:\n\n - kernel: Driver service\n - filesystem: File system driver service\n - adapter: Adapter driver service (reserved)\n - recognizer: Recognizer driver service (reserved)\n - own (default): Service runs in its own process\n - share: Service shares a process with one or more other services\n\n start_type (str):\n Specifies the service start type. Valid options are as follows:\n\n - boot: Device driver that is loaded by the boot loader\n - system: Device driver that is started during kernel initialization\n - auto: Service that automatically starts\n - manual (default): Service must be started manually\n - disabled: Service cannot be started\n\n start_delayed (bool):\n Set the service to Auto(Delayed Start). Only valid if the start_type\n is set to ``Auto``. If service_type is not passed, but the service\n is already set to ``Auto``, then the flag will be set. Default is\n ``False``\n\n error_control (str):\n The severity of the error, and action taken, if this service fails\n to start. Valid options are as follows:\n\n - normal (normal): Error is logged and a message box is displayed\n - severe: Error is logged and computer attempts a restart with the\n last known good configuration\n - critical: Error is logged, computer attempts to restart with the\n last known good configuration, system halts on failure\n - ignore: Error is logged and startup continues, no notification is\n given to the user\n\n load_order_group (str):\n The name of the load order group to which this service belongs\n\n dependencies (list):\n A list of services or load ordering groups that must start before\n this service\n\n account_name (str):\n The name of the account under which the service should run. For\n ``own`` type services this should be in the ``domain\\\\username``\n format. The following are examples of valid built-in service\n accounts:\n\n - NT Authority\\\\LocalService\n - NT Authority\\\\NetworkService\n - NT Authority\\\\LocalSystem\n - .\\\\LocalSystem\n\n account_password (str):\n The password for the account name specified in ``account_name``. For\n the above built-in accounts, this can be None. Otherwise a password\n must be specified.\n\n run_interactive (bool):\n If this setting is True, the service will be allowed to interact\n with the user. Not recommended for services that run with elevated\n privileges.\n\n Returns:\n dict: A dictionary containing information about the new service\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.create <service name> <path to exe> display_name='<display name>'\n '''\n if display_name is None:\n display_name = name\n\n # Test if the service already exists\n if name in get_all():\n raise CommandExecutionError('Service Already Exists: {0}'.format(name))\n\n # shlex.quote the path to the binary\n bin_path = _cmd_quote(bin_path)\n if exe_args is not None:\n bin_path = '{0} {1}'.format(bin_path, exe_args)\n\n if service_type.lower() in SERVICE_TYPE:\n service_type = SERVICE_TYPE[service_type.lower()]\n if run_interactive:\n service_type = service_type | \\\n win32service.SERVICE_INTERACTIVE_PROCESS\n else:\n raise CommandExecutionError(\n 'Invalid Service Type: {0}'.format(service_type))\n\n if start_type.lower() in SERVICE_START_TYPE:\n start_type = SERVICE_START_TYPE[start_type.lower()]\n else:\n raise CommandExecutionError(\n 'Invalid Start Type: {0}'.format(start_type))\n\n if error_control.lower() in SERVICE_ERROR_CONTROL:\n error_control = SERVICE_ERROR_CONTROL[error_control.lower()]\n else:\n raise CommandExecutionError(\n 'Invalid Error Control: {0}'.format(error_control))\n\n if start_delayed:\n if start_type != 2:\n raise CommandExecutionError(\n 'Invalid Parameter: start_delayed requires start_type \"auto\"')\n\n if account_name in ['LocalSystem', '.\\\\LocalSystem',\n 'LocalService', '.\\\\LocalService',\n 'NetworkService', '.\\\\NetworkService']:\n account_password = ''\n\n # Connect to Service Control Manager\n handle_scm = win32service.OpenSCManager(\n None, None, win32service.SC_MANAGER_ALL_ACCESS)\n\n # Create the service\n handle_svc = win32service.CreateService(handle_scm,\n name,\n display_name,\n win32service.SERVICE_ALL_ACCESS,\n service_type,\n start_type,\n error_control,\n bin_path,\n load_order_group,\n 0,\n dependencies,\n account_name,\n account_password)\n\n if description is not None:\n win32service.ChangeServiceConfig2(\n handle_svc, win32service.SERVICE_CONFIG_DESCRIPTION, description)\n\n if start_delayed is not None:\n # You can only set delayed start for services that are set to auto start\n # Start type 2 is Auto\n if start_type == 2:\n win32service.ChangeServiceConfig2(\n handle_svc, win32service.SERVICE_CONFIG_DELAYED_AUTO_START_INFO,\n start_delayed)\n\n win32service.CloseServiceHandle(handle_scm)\n win32service.CloseServiceHandle(handle_svc)\n\n return info(name)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef delete(name, timeout=90):\n '''\n Delete the named service\n\n Args:\n\n name (str): The name of the service to delete\n\n timeout (int):\n The time in seconds to wait for the service to be deleted before\n returning. This is necessary because a service must be stopped\n before it can be deleted. Default is 90 seconds\n\n .. versionadded:: 2017.7.9,2018.3.4\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``. Also returns ``True``\n if the service is not present\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.delete <service name>\n '''\n handle_scm = win32service.OpenSCManager(\n None, None, win32service.SC_MANAGER_CONNECT)\n\n try:\n handle_svc = win32service.OpenService(\n handle_scm, name, win32service.SERVICE_ALL_ACCESS)\n except pywintypes.error as exc:\n win32service.CloseServiceHandle(handle_scm)\n if exc.winerror != 1060:\n raise CommandExecutionError(\n 'Failed to open {0}. {1}'.format(name, exc.strerror))\n log.debug('Service \"%s\" is not present', name)\n return True\n\n try:\n win32service.DeleteService(handle_svc)\n except pywintypes.error as exc:\n raise CommandExecutionError(\n 'Failed to delete {0}. {1}'.format(name, exc.strerror))\n finally:\n log.debug('Cleaning up')\n win32service.CloseServiceHandle(handle_scm)\n win32service.CloseServiceHandle(handle_svc)\n\n end_time = time.time() + int(timeout)\n while name in get_all() and time.time() < end_time:\n time.sleep(1)\n\n return name not in get_all()", "response": "Delete the named service in the system."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_conn():\n '''\n Return a conn object for the passed VM data\n '''\n driver = get_driver(Provider.CLOUDSTACK)\n\n verify_ssl_cert = config.get_cloud_config_value('verify_ssl_cert',\n get_configured_provider(),\n __opts__,\n default=True,\n search_global=False)\n\n if verify_ssl_cert is False:\n try:\n import libcloud.security\n libcloud.security.VERIFY_SSL_CERT = False\n except (ImportError, AttributeError):\n raise SaltCloudSystemExit(\n 'Could not disable SSL certificate verification. '\n 'Not loading module.'\n )\n\n return driver(\n key=config.get_cloud_config_value(\n 'apikey', get_configured_provider(), __opts__, search_global=False\n ),\n secret=config.get_cloud_config_value(\n 'secretkey', get_configured_provider(), __opts__,\n search_global=False\n ),\n secure=config.get_cloud_config_value(\n 'secure', get_configured_provider(), __opts__,\n default=True, search_global=False\n ),\n host=config.get_cloud_config_value(\n 'host', get_configured_provider(), __opts__, search_global=False\n ),\n path=config.get_cloud_config_value(\n 'path', get_configured_provider(), __opts__, search_global=False\n ),\n port=config.get_cloud_config_value(\n 'port', get_configured_provider(), __opts__,\n default=None, search_global=False\n )\n )", "response": "Return a conn object for the passed VM data"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_location(conn, vm_):\n '''\n Return the node location to use\n '''\n locations = conn.list_locations()\n # Default to Dallas if not otherwise set\n loc = config.get_cloud_config_value('location', vm_, __opts__, default=2)\n for location in locations:\n if six.text_type(loc) in (six.text_type(location.id), six.text_type(location.name)):\n return location", "response": "Return the node location to use"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_security_groups(conn, vm_):\n '''\n Return a list of security groups to use, defaulting to ['default']\n '''\n securitygroup_enabled = config.get_cloud_config_value(\n 'securitygroup_enabled', vm_, __opts__, default=True\n )\n if securitygroup_enabled:\n return config.get_cloud_config_value(\n 'securitygroup', vm_, __opts__, default=['default']\n )\n else:\n return False", "response": "Return a list of security groups to use"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the keypair to use", "response": "def get_keypair(vm_):\n '''\n Return the keypair to use\n '''\n keypair = config.get_cloud_config_value('keypair', vm_, __opts__)\n\n if keypair:\n return keypair\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets the IP address of the VM", "response": "def get_ip(data):\n '''\n Return the IP address of the VM\n If the VM has public IP as defined by libcloud module then use it\n Otherwise try to extract the private IP and use that one.\n '''\n try:\n ip = data.public_ips[0]\n except Exception:\n ip = data.private_ips[0]\n return ip"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the networkid to use only valid for Advanced Zone", "response": "def get_networkid(vm_):\n '''\n Return the networkid to use, only valid for Advanced Zone\n '''\n networkid = config.get_cloud_config_value('networkid', vm_, __opts__)\n\n if networkid is not None:\n return networkid\n else:\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the project to use.", "response": "def get_project(conn, vm_):\n '''\n Return the project to use.\n '''\n try:\n projects = conn.ex_list_projects()\n except AttributeError:\n # with versions <0.15 of libcloud this is causing an AttributeError.\n log.warning('Cannot get projects, you may need to update libcloud to 0.15 or later')\n return False\n projid = config.get_cloud_config_value('projectid', vm_, __opts__)\n\n if not projid:\n return False\n\n for project in projects:\n if six.text_type(projid) in (six.text_type(project.id), six.text_type(project.name)):\n return project\n\n log.warning(\"Couldn't find project %s in projects\", projid)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncreate a single VM from a data dict", "response": "def 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 'cloudstack',\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 sock_dir=__opts__['sock_dir'],\n args=__utils__['cloud.filter_event']('creating', vm_, ['name', 'profile', 'provider', 'driver']),\n transport=__opts__['transport']\n )\n\n log.info('Creating Cloud VM %s', vm_['name'])\n conn = get_conn()\n # pylint: disable=not-callable\n kwargs = {\n 'name': vm_['name'],\n 'image': get_image(conn, vm_),\n 'size': get_size(conn, vm_),\n 'location': get_location(conn, vm_),\n }\n # pylint: enable=not-callable\n\n sg = get_security_groups(conn, vm_)\n if sg is not False:\n kwargs['ex_security_groups'] = sg\n\n if get_keypair(vm_) is not False:\n kwargs['ex_keyname'] = get_keypair(vm_)\n\n if get_networkid(vm_) is not False:\n kwargs['networkids'] = get_networkid(vm_)\n kwargs['networks'] = ( # The only attr that is used is 'id'.\n CloudStackNetwork(None, None, None,\n kwargs['networkids'],\n None, None),\n )\n\n if get_project(conn, vm_) is not False:\n kwargs['project'] = get_project(conn, vm_)\n\n event_data = kwargs.copy()\n event_data['image'] = kwargs['image'].name\n event_data['size'] = kwargs['size'].name\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(vm_['name']),\n sock_dir=__opts__['sock_dir'],\n args={\n 'kwargs': __utils__['cloud.filter_event'](\n 'requesting',\n event_data,\n ['name', 'profile', 'provider', 'driver', 'image', 'size'],\n ),\n },\n transport=__opts__['transport']\n )\n\n displayname = cloudstack_displayname(vm_)\n if displayname:\n kwargs['ex_displayname'] = displayname\n else:\n kwargs['ex_displayname'] = kwargs['name']\n\n volumes = {}\n ex_blockdevicemappings = block_device_mappings(vm_)\n if ex_blockdevicemappings:\n for ex_blockdevicemapping in ex_blockdevicemappings:\n if 'VirtualName' not in ex_blockdevicemapping:\n ex_blockdevicemapping['VirtualName'] = '{0}-{1}'.format(vm_['name'], len(volumes))\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting volume',\n 'salt/cloud/{0}/requesting'.format(ex_blockdevicemapping['VirtualName']),\n sock_dir=__opts__['sock_dir'],\n args={'kwargs': {'name': ex_blockdevicemapping['VirtualName'],\n 'device': ex_blockdevicemapping['DeviceName'],\n 'size': ex_blockdevicemapping['VolumeSize']}},\n )\n try:\n volumes[ex_blockdevicemapping['DeviceName']] = conn.create_volume(\n ex_blockdevicemapping['VolumeSize'],\n ex_blockdevicemapping['VirtualName']\n )\n except Exception as exc:\n log.error(\n 'Error creating volume %s on CLOUDSTACK\\n\\n'\n 'The following exception was thrown by libcloud when trying to '\n 'requesting a volume: \\n%s',\n ex_blockdevicemapping['VirtualName'], exc,\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n return False\n else:\n ex_blockdevicemapping = {}\n try:\n data = conn.create_node(**kwargs)\n except Exception as exc:\n log.error(\n 'Error creating %s on CLOUDSTACK\\n\\n'\n 'The following exception was thrown by libcloud when trying to '\n 'run the initial deployment: \\n%s',\n vm_['name'], exc,\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n return False\n\n for device_name in six.iterkeys(volumes):\n try:\n conn.attach_volume(data, volumes[device_name], device_name)\n except Exception as exc:\n log.error(\n 'Error attaching volume %s on CLOUDSTACK\\n\\n'\n 'The following exception was thrown by libcloud when trying to '\n 'attach a volume: \\n%s',\n ex_blockdevicemapping.get('VirtualName', 'UNKNOWN'), exc,\n # Show the traceback if the debug logging level is enabled\n exc_info=log.isEnabledFor(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 vm_['ssh_host'] = get_ip(data)\n vm_['password'] = data.extra['password']\n vm_['key_filename'] = get_key()\n ret = __utils__['cloud.bootstrap'](vm_, __opts__)\n\n ret.update(data.__dict__)\n\n if 'password' in data.extra:\n del data.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 __utils__['cloud.fire_event'](\n 'event',\n 'created instance',\n 'salt/cloud/{0}/created'.format(vm_['name']),\n sock_dir=__opts__['sock_dir'],\n args=__utils__['cloud.filter_event']('created', vm_, ['name', 'profile', 'provider', 'driver']),\n transport=__opts__['transport']\n )\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef destroy(name, conn=None, call=None):\n '''\n Delete a single VM, and all of its volumes\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 sock_dir=__opts__['sock_dir'],\n args={'name': name},\n )\n\n if not conn:\n conn = get_conn() # pylint: disable=E0602\n\n node = get_node(conn, name)\n if node is None:\n log.error('Unable to find the VM %s', name)\n volumes = conn.list_volumes(node)\n if volumes is None:\n log.error('Unable to find volumes of the VM %s', name)\n # TODO add an option like 'delete_sshkeys' below\n for volume in volumes:\n if volume.extra['volume_type'] != 'DATADISK':\n log.info(\n 'Ignoring volume type %s: %s',\n volume.extra['volume_type'], volume.name\n )\n continue\n log.info('Detaching volume: %s', volume.name)\n __utils__['cloud.fire_event'](\n 'event',\n 'detaching volume',\n 'salt/cloud/{0}/detaching'.format(volume.name),\n sock_dir=__opts__['sock_dir'],\n args={'name': volume.name},\n )\n if not conn.detach_volume(volume):\n log.error('Failed to Detach volume: %s', volume.name)\n return False\n log.info('Detached volume: %s', volume.name)\n __utils__['cloud.fire_event'](\n 'event',\n 'detached volume',\n 'salt/cloud/{0}/detached'.format(volume.name),\n sock_dir=__opts__['sock_dir'],\n args={'name': volume.name},\n )\n\n log.info('Destroying volume: %s', volume.name)\n __utils__['cloud.fire_event'](\n 'event',\n 'destroying volume',\n 'salt/cloud/{0}/destroying'.format(volume.name),\n sock_dir=__opts__['sock_dir'],\n args={'name': volume.name},\n )\n if not conn.destroy_volume(volume):\n log.error('Failed to Destroy volume: %s', volume.name)\n return False\n log.info('Destroyed volume: %s', volume.name)\n __utils__['cloud.fire_event'](\n 'event',\n 'destroyed volume',\n 'salt/cloud/{0}/destroyed'.format(volume.name),\n sock_dir=__opts__['sock_dir'],\n args={'name': volume.name},\n )\n log.info('Destroying VM: %s', name)\n ret = conn.destroy_node(node)\n if not ret:\n log.error('Failed to Destroy VM: %s', name)\n return False\n log.info('Destroyed VM: %s', name)\n # Fire destroy action\n event = salt.utils.event.SaltEvent('master', __opts__['sock_dir'])\n __utils__['cloud.fire_event'](\n 'event',\n 'destroyed instance',\n 'salt/cloud/{0}/destroyed'.format(name),\n sock_dir=__opts__['sock_dir'],\n args={'name': name},\n )\n if __opts__['delete_sshkeys'] is True:\n salt.utils.cloud.remove_sshkey(node.public_ips[0])\n return True", "response": "Delete a single VM and all of its volumes"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn system rc configuration variables", "response": "def get(**kwargs):\n '''\n Return system rc configuration variables\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sysrc.get includeDefaults=True\n '''\n\n cmd = 'sysrc -v'\n\n if 'file' in kwargs:\n cmd += ' -f '+kwargs['file']\n\n if 'jail' in kwargs:\n cmd += ' -j '+kwargs['jail']\n\n if 'name' in kwargs:\n cmd += ' '+kwargs['name']\n elif kwargs.get('includeDefaults', False):\n cmd += ' -A'\n else:\n cmd += ' -a'\n\n sysrcs = __salt__['cmd.run'](cmd)\n if \"sysrc: unknown variable\" in sysrcs:\n # raise CommandExecutionError(sysrcs)\n return None\n\n ret = {}\n for sysrc in sysrcs.split(\"\\n\"):\n line_components = sysrc.split(': ')\n rcfile = line_components[0]\n if len(line_components) > 2:\n var = line_components[1]\n val = line_components[2]\n else:\n var = line_components[1].rstrip(':')\n val = ''\n if rcfile not in ret:\n ret[rcfile] = {}\n ret[rcfile][var] = val\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nset system rc configuration variables", "response": "def set_(name, value, **kwargs):\n '''\n Set system rc configuration variables\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sysrc.set name=sshd_flags value=\"-p 2222\"\n '''\n\n cmd = 'sysrc -v'\n\n if 'file' in kwargs:\n cmd += ' -f '+kwargs['file']\n\n if 'jail' in kwargs:\n cmd += ' -j '+kwargs['jail']\n\n # This is here because the YAML parser likes to convert the string literals\n # YES, NO, Yes, No, True, False, etc. to boolean types. However, in this case,\n # we will check to see if that happened and replace it with \"YES\" or \"NO\" because\n # those items are accepted in sysrc.\n if type(value) == bool:\n if value:\n value = \"YES\"\n else:\n value = \"NO\"\n\n # This is here for the same reason, except for numbers\n if type(value) == int:\n value = str(value)\n\n cmd += ' '+name+\"=\\\"\"+value+\"\\\"\"\n\n sysrcs = __salt__['cmd.run'](cmd)\n\n ret = {}\n for sysrc in sysrcs.split(\"\\n\"):\n rcfile = sysrc.split(': ')[0]\n var = sysrc.split(': ')[1]\n oldval = sysrc.split(': ')[2].strip().split(\"->\")[0]\n newval = sysrc.split(': ')[2].strip().split(\"->\")[1]\n if rcfile not in ret:\n ret[rcfile] = {}\n ret[rcfile][var] = newval\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef remove(name, **kwargs):\n '''\n Remove system rc configuration variables\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' sysrc.remove name=sshd_enable\n '''\n\n cmd = 'sysrc -v'\n\n if 'file' in kwargs:\n cmd += ' -f '+kwargs['file']\n\n if 'jail' in kwargs:\n cmd += ' -j '+kwargs['jail']\n\n cmd += ' -x '+name\n\n sysrcs = __salt__['cmd.run'](cmd)\n if \"sysrc: unknown variable\" in sysrcs:\n raise CommandExecutionError(sysrcs)\n else:\n return name+\" removed\"", "response": "Remove system rc configuration variables"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a single VM from a data dict", "response": "def 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(\n __opts__,\n __active_provider_name__ or 'dimensiondata',\n vm_['profile']) 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 log.info('Creating Cloud VM %s', vm_['name'])\n conn = get_conn()\n\n location = conn.ex_get_location_by_id(vm_['location'])\n images = conn.list_images(location=location)\n image = [x for x in images if x.id == vm_['image']][0]\n network_domains = conn.ex_list_network_domains(location=location)\n try:\n network_domain = [y for y in network_domains\n if y.name == vm_['network_domain']][0]\n except IndexError:\n network_domain = conn.ex_create_network_domain(\n location=location,\n name=vm_['network_domain'],\n plan='ADVANCED',\n description=''\n )\n\n try:\n vlan = [y for y in conn.ex_list_vlans(\n location=location,\n network_domain=network_domain)\n if y.name == vm_['vlan']][0]\n except (IndexError, KeyError):\n # Use the first VLAN in the network domain\n vlan = conn.ex_list_vlans(\n location=location,\n network_domain=network_domain)[0]\n\n kwargs = {\n 'name': vm_['name'],\n 'image': image,\n 'ex_description': vm_['description'],\n 'ex_network_domain': network_domain,\n 'ex_vlan': vlan,\n 'ex_is_started': vm_['is_started']\n }\n\n event_data = _to_event_data(kwargs)\n\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(vm_['name']),\n args=__utils__['cloud.filter_event']('requesting', event_data, list(event_data)),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n # Initial password (excluded from event payload)\n initial_password = NodeAuthPassword(vm_['auth'])\n kwargs['auth'] = initial_password\n\n try:\n data = conn.create_node(**kwargs)\n except Exception as exc:\n log.error(\n 'Error creating %s on DIMENSIONDATA\\n\\n'\n 'The following exception was thrown by libcloud when trying to '\n 'run the initial deployment: \\n%s',\n vm_['name'], exc,\n exc_info_on_loglevel=logging.DEBUG\n )\n return False\n\n try:\n data = __utils__['cloud.wait_for_ip'](\n _query_node_data,\n update_args=(vm_, data),\n timeout=config.get_cloud_config_value(\n 'wait_for_ip_timeout', vm_, __opts__, default=25 * 60),\n interval=config.get_cloud_config_value(\n 'wait_for_ip_interval', vm_, __opts__, default=30),\n max_failures=config.get_cloud_config_value(\n 'wait_for_ip_max_failures', vm_, __opts__, default=60),\n )\n except (SaltCloudExecutionTimeout, SaltCloudExecutionFailure) as exc:\n try:\n # It might be already up, let's destroy it!\n destroy(vm_['name']) # pylint: disable=not-callable\n except SaltCloudSystemExit:\n pass\n finally:\n raise SaltCloudSystemExit(six.text_type(exc))\n\n log.debug('VM is now running')\n if ssh_interface(vm_) == 'private_ips':\n ip_address = preferred_ip(vm_, data.private_ips)\n else:\n ip_address = preferred_ip(vm_, data.public_ips)\n log.debug('Using IP address %s', ip_address)\n\n if __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 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(\n 'No IP addresses could be found.'\n )\n\n vm_['salt_host'] = salt_ip_address\n vm_['ssh_host'] = ip_address\n vm_['password'] = vm_['auth']\n\n ret = __utils__['cloud.bootstrap'](vm_, __opts__)\n\n ret.update(data.__dict__)\n\n if 'password' in data.extra:\n del data.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 __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"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nstopping a VM in DimensionData.", "response": "def stop(name, call=None):\n '''\n Stop a VM in DimensionData.\n\n name:\n The name of the VM to stop.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -a stop vm_name\n '''\n conn = get_conn()\n node = get_node(conn, name) # pylint: disable=not-callable\n log.debug('Node of Cloud VM: %s', node)\n\n status = conn.ex_shutdown_graceful(node)\n log.debug('Status of Cloud VM: %s', status)\n\n return status"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nstopping a VM in DimensionData.", "response": "def start(name, call=None):\n '''\n Stop a VM in DimensionData.\n\n :param str name:\n The name of the VM to stop.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -a stop vm_name\n '''\n\n conn = get_conn()\n node = get_node(conn, name) # pylint: disable=not-callable\n log.debug('Node of Cloud VM: %s', node)\n\n status = conn.ex_start_node(node)\n log.debug('Status of Cloud VM: %s', status)\n\n return status"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_conn():\n '''\n Return a conn object for the passed VM data\n '''\n vm_ = get_configured_provider()\n driver = get_driver(Provider.DIMENSIONDATA)\n\n region = config.get_cloud_config_value(\n 'region', vm_, __opts__\n )\n\n user_id = config.get_cloud_config_value(\n 'user_id', vm_, __opts__\n )\n key = config.get_cloud_config_value(\n 'key', vm_, __opts__\n )\n\n if key is not None:\n log.debug('DimensionData authenticating using password')\n\n return driver(\n user_id,\n key,\n region=region\n )", "response": "Return a conn object for the passed VM data\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_lb_conn(dd_driver=None):\n '''\n Return a load-balancer conn object\n '''\n vm_ = get_configured_provider()\n\n region = config.get_cloud_config_value(\n 'region', vm_, __opts__\n )\n\n user_id = config.get_cloud_config_value(\n 'user_id', vm_, __opts__\n )\n key = config.get_cloud_config_value(\n 'key', vm_, __opts__\n )\n if not dd_driver:\n raise SaltCloudSystemExit(\n 'Missing dimensiondata_driver for get_lb_conn method.'\n )\n return get_driver_lb(Provider_lb.DIMENSIONDATA)(user_id, key, region=region)", "response": "Return a load - balancer conn object"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _to_event_data(obj):\n '''\n Convert the specified object into a form that can be serialised by msgpack as event data.\n\n :param obj: The object to convert.\n '''\n\n if obj is None:\n return None\n if isinstance(obj, bool):\n return obj\n if isinstance(obj, int):\n return obj\n if isinstance(obj, float):\n return obj\n if isinstance(obj, str):\n return obj\n if isinstance(obj, bytes):\n return obj\n if isinstance(obj, dict):\n return obj\n\n if isinstance(obj, NodeDriver): # Special case for NodeDriver (cyclic references)\n return obj.name\n\n if isinstance(obj, list):\n return [_to_event_data(item) for item in obj]\n\n event_data = {}\n for attribute_name in dir(obj):\n if attribute_name.startswith('_'):\n continue\n\n attribute_value = getattr(obj, attribute_name)\n\n if callable(attribute_value): # Strip out methods\n continue\n\n event_data[attribute_name] = _to_event_data(attribute_value)\n\n return event_data", "response": "Convert the specified object into a form that can be serialised by msgpack as event data."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef package_install(name, **kwargs):\n '''\n Install a \"package\" on the REST server\n '''\n DETAILS = _load_state()\n if kwargs.get('version', False):\n version = kwargs['version']\n else:\n version = '1.0'\n DETAILS['packages'][name] = version\n _save_state(DETAILS)\n return {name: version}", "response": "Install a package on the REST server"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef uptodate():\n '''\n Call the REST endpoint to see if the packages on the \"server\" are up to date.\n '''\n DETAILS = _load_state()\n for p in DETAILS['packages']:\n version_float = float(DETAILS['packages'][p])\n version_float = version_float + 1.0\n DETAILS['packages'][p] = six.text_type(version_float)\n return DETAILS['packages']", "response": "Return a list of all packages that are up to date."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef package_remove(name):\n '''\n Remove a \"package\" on the REST server\n '''\n DETAILS = _load_state()\n DETAILS['packages'].pop(name)\n _save_state(DETAILS)\n return DETAILS['packages']", "response": "Remove a package on the REST server"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nclosing the archive. :return:", "response": "def close(self):\n '''\n Closes the archive.\n :return:\n '''\n if self.__arch is None:\n raise salt.exceptions.SaltException('Archive already closed')\n self._flush_content()\n self.__arch.close()\n self.__arch = None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _flush_content(self):\n '''\n Flush content to the archive\n :return:\n '''\n if self.__current_section is not None:\n buff = BytesIO()\n buff._dirty = False\n for action_return in self.__current_section:\n for title, ret_data in action_return.items():\n if isinstance(ret_data, file):\n self.out.put(ret_data.name, indent=4)\n self.__arch.add(ret_data.name, arcname=ret_data.name)\n else:\n buff.write(salt.utils.stringutils.to_bytes(title + '\\n'))\n buff.write(salt.utils.stringutils.to_bytes(('-' * len(title)) + '\\n\\n'))\n buff.write(salt.utils.stringutils.to_bytes(ret_data))\n buff.write(salt.utils.stringutils.to_bytes('\\n\\n\\n'))\n buff._dirty = True\n if buff._dirty:\n buff.seek(0)\n tar_info = tarfile.TarInfo(name=\"{}/{}\".format(self.__default_root, self.__current_section_name))\n if not hasattr(buff, 'getbuffer'): # Py2's BytesIO is older\n buff.getbuffer = buff.getvalue\n tar_info.size = len(buff.getbuffer())\n self.__arch.addfile(tarinfo=tar_info, fileobj=buff)", "response": "Flush the content of the archive to the archive"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add(self, name):\n '''\n Start a new section.\n :param name:\n :return:\n '''\n if self.__current_section:\n self._flush_content()\n self.discard_current(name)", "response": "Start a new section."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _printout(self, data, output):\n '''\n Use salt outputter to printout content.\n\n :return:\n '''\n opts = {'extension_modules': '', 'color': False}\n try:\n printout = salt.output.get_printout(output, opts)(data)\n if printout is not None:\n return printout.rstrip()\n except (KeyError, AttributeError, TypeError) as err:\n log.debug(err, exc_info=True)\n try:\n printout = salt.output.get_printout('nested', opts)(data)\n if printout is not None:\n return printout.rstrip()\n except (KeyError, AttributeError, TypeError) as err:\n log.debug(err, exc_info=True)\n printout = salt.output.get_printout('raw', opts)(data)\n if printout is not None:\n return printout.rstrip()\n\n return salt.output.try_printout(data, output, opts)", "response": "Use salt outputter to printout content."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nwrite a new entry to the current opened section.", "response": "def write(self, title, data, output=None):\n '''\n Add a data to the current opened section.\n :return:\n '''\n if not isinstance(data, (dict, list, tuple)):\n data = {'raw-content': str(data)}\n output = output or self.__default_outputter\n\n if output != 'null':\n try:\n if isinstance(data, dict) and 'return' in data:\n data = data['return']\n content = self._printout(data, output)\n except Exception: # Fall-back to just raw YAML\n content = None\n else:\n content = None\n\n if content is None:\n data = json.loads(json.dumps(data))\n if isinstance(data, dict) and data.get('return'):\n data = data.get('return')\n content = yaml.safe_dump(data, default_flow_style=False, indent=4)\n\n self.__current_section.append({title: content})"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef link(self, title, path):\n '''\n Add a static file on the file system.\n\n :param title:\n :param path:\n :return:\n '''\n # The filehandler needs to be explicitly passed here, so PyLint needs to accept that.\n # pylint: disable=W8470\n if not isinstance(path, file):\n path = salt.utils.files.fopen(path)\n self.__current_section.append({title: path})", "response": "Add a static file on the file system."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _setup_fun_config(self, fun_conf):\n '''\n Setup function configuration.\n\n :param conf:\n :return:\n '''\n conf = copy.deepcopy(self.config)\n conf['file_client'] = 'local'\n conf['fun'] = ''\n conf['arg'] = []\n conf['kwarg'] = {}\n conf['cache_jobs'] = False\n conf['print_metadata'] = False\n conf.update(fun_conf)\n conf['fun'] = conf['fun'].split(':')[-1] # Discard typing prefix\n\n return conf", "response": "Setup function configuration.\n\n :param conf:\n :return:"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets & setup runner.", "response": "def _get_runner(self, conf):\n '''\n Get & setup runner.\n\n :param conf:\n :return:\n '''\n conf = self._setup_fun_config(copy.deepcopy(conf))\n if not getattr(self, '_runner', None):\n self._runner = salt.cli.support.localrunner.LocalRunner(conf)\n else:\n self._runner.opts = conf\n return self._runner"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets & setup caller from the factory.", "response": "def _get_caller(self, conf):\n '''\n Get & setup caller from the factory.\n\n :param conf:\n :return:\n '''\n conf = self._setup_fun_config(copy.deepcopy(conf))\n if not getattr(self, '_caller', None):\n self._caller = salt.cli.caller.Caller.factory(conf)\n else:\n self._caller.opts = conf\n return self._caller"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nexecute local runner :param run_conf: :return:", "response": "def _local_run(self, run_conf):\n '''\n Execute local runner\n\n :param run_conf:\n :return:\n '''\n try:\n ret = self._get_runner(run_conf).run()\n except SystemExit:\n ret = 'Runner is not available at this moment'\n self.out.error(ret)\n except Exception as ex:\n ret = 'Unhandled exception occurred: {}'.format(ex)\n log.debug(ex, exc_info=True)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _internal_function_call(self, call_conf):\n '''\n Call internal function.\n\n :param call_conf:\n :return:\n '''\n def stub(*args, **kwargs):\n message = 'Function {} is not available'.format(call_conf['fun'])\n self.out.error(message)\n log.debug(\n 'Attempt to run \"%s\" with %s arguments and %s parameters.',\n call_conf['fun'], call_conf['arg'], call_conf['kwargs']\n )\n return message\n\n return getattr(salt.cli.support.intfunc,\n call_conf['fun'], stub)(self.collector,\n *call_conf['arg'],\n **call_conf['kwargs'])", "response": "Internal function to call internal function."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nparse action and turn into a calling point.", "response": "def _get_action(self, action_meta):\n '''\n Parse action and turn into a calling point.\n :param action_meta:\n :return:\n '''\n conf = {\n 'fun': list(action_meta.keys())[0],\n 'arg': [],\n 'kwargs': {},\n }\n if not len(conf['fun'].split('.')) - 1:\n conf['salt.int.intfunc'] = True\n\n action_meta = action_meta[conf['fun']]\n info = action_meta.get('info', 'Action for {}'.format(conf['fun']))\n for arg in action_meta.get('args') or []:\n if not isinstance(arg, dict):\n conf['arg'].append(arg)\n else:\n conf['kwargs'].update(arg)\n\n return info, action_meta.get('output'), conf"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef collect_internal_data(self):\n '''\n Dumps current running pillars, configuration etc.\n :return:\n '''\n section = 'configuration'\n self.out.put(section)\n self.collector.add(section)\n self.out.put('Saving config', indent=2)\n self.collector.write('General Configuration', self.config)\n self.out.put('Saving pillars', indent=2)\n self.collector.write('Active Pillars', self._local_call({'fun': 'pillar.items'}))\n\n section = 'highstate'\n self.out.put(section)\n self.collector.add(section)\n self.out.put('Saving highstate', indent=2)\n self.collector.write('Rendered highstate', self._local_call({'fun': 'state.show_highstate'}))", "response": "Dumps current running pillars configuration etc."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nextract return data from the results.", "response": "def _extract_return(self, data):\n '''\n Extracts return data from the results.\n\n :param data:\n :return:\n '''\n if isinstance(data, dict):\n data = data.get('return', data)\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef collect_local_data(self, profile=None, profile_source=None):\n '''\n Collects master system data.\n :return:\n '''\n def call(func, *args, **kwargs):\n '''\n Call wrapper for templates\n :param func:\n :return:\n '''\n return self._extract_return(self._local_call({'fun': func, 'arg': args, 'kwarg': kwargs}))\n\n def run(func, *args, **kwargs):\n '''\n Runner wrapper for templates\n :param func:\n :return:\n '''\n return self._extract_return(self._local_run({'fun': func, 'arg': args, 'kwarg': kwargs}))\n\n scenario = profile_source or salt.cli.support.get_profile(profile or self.config['support_profile'], call, run)\n for category_name in scenario:\n self.out.put(category_name)\n self.collector.add(category_name)\n for action in scenario[category_name]:\n if not action:\n continue\n action_name = next(iter(action))\n if not isinstance(action[action_name], six.string_types):\n info, output, conf = self._get_action(action)\n action_type = self._get_action_type(action) # run:<something> for runners\n if action_type == self.RUNNER_TYPE:\n self.out.put('Running {}'.format(info.lower()), indent=2)\n self.collector.write(info, self._local_run(conf), output=output)\n elif action_type == self.CALL_TYPE:\n if not conf.get('salt.int.intfunc'):\n self.out.put('Collecting {}'.format(info.lower()), indent=2)\n self.collector.write(info, self._local_call(conf), output=output)\n else:\n self.collector.discard_current()\n self._internal_function_call(conf)\n else:\n self.out.error('Unknown action type \"{}\" for action: {}'.format(action_type, action))\n else:\n # TODO: This needs to be moved then to the utils.\n # But the code is not yet there (other PRs)\n self.out.msg('\\n'.join(salt.cli.support.console.wrap(action[action_name])), ident=2)", "response": "Collect master system data."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget action type. :param action: :return:", "response": "def _get_action_type(self, action):\n '''\n Get action type.\n :param action:\n :return:\n '''\n action_name = next(iter(action or {'': None}))\n if ':' not in action_name:\n action_name = '{}:{}'.format(self.CALL_TYPE, action_name)\n\n return action_name.split(':')[0] or None"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _check_existing_archive(self):\n '''\n Check if archive exists or not. If exists and --force was not specified,\n bail out. Otherwise remove it and move on.\n\n :return:\n '''\n if os.path.exists(self.config['support_archive']):\n if self.config['support_archive_force_overwrite']:\n self.out.warning('Overwriting existing archive: {}'.format(self.config['support_archive']))\n try:\n os.unlink(self.config['support_archive'])\n except Exception as err:\n log.debug(err)\n self.out.error('{} while trying to overwrite existing archive.'.format(err))\n ret = True\n else:\n self.out.warning('File {} already exists.'.format(self.config['support_archive']))\n ret = False\n else:\n ret = True\n\n return ret", "response": "Check if an existing archive exists or not."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nrun the SPM command", "response": "def run(self, args):\n '''\n Run the SPM command\n '''\n command = args[0]\n try:\n if command == 'install':\n self._install(args)\n elif command == 'local':\n self._local(args)\n elif command == 'repo':\n self._repo(args)\n elif command == 'remove':\n self._remove(args)\n elif command == 'build':\n self._build(args)\n elif command == 'update_repo':\n self._download_repo_metadata(args)\n elif command == 'create_repo':\n self._create_repo(args)\n elif command == 'files':\n self._list_files(args)\n elif command == 'info':\n self._info(args)\n elif command == 'list':\n self._list(args)\n elif command == 'close':\n self._close()\n else:\n raise SPMInvocationError('Invalid command \\'{0}\\''.format(command))\n except SPMException as exc:\n self.ui.error(six.text_type(exc))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _list(self, args):\n '''\n Process local commands\n '''\n args.pop(0)\n command = args[0]\n if command == 'packages':\n self._list_packages(args)\n elif command == 'files':\n self._list_files(args)\n elif command == 'repos':\n self._repo_list(args)\n else:\n raise SPMInvocationError('Invalid list command \\'{0}\\''.format(command))", "response": "List local files packages or repos"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _repo_packages(self, args, search=False):\n '''\n List packages for one or more configured repos\n '''\n packages = []\n repo_metadata = self._get_repo_metadata()\n for repo in repo_metadata:\n for pkg in repo_metadata[repo]['packages']:\n if args[1] in pkg:\n version = repo_metadata[repo]['packages'][pkg]['info']['version']\n release = repo_metadata[repo]['packages'][pkg]['info']['release']\n packages.append((pkg, version, release, repo))\n for pkg in sorted(packages):\n self.ui.status(\n '{0}\\t{1}-{2}\\t{3}'.format(pkg[0], pkg[1], pkg[2], pkg[3])\n )\n return packages", "response": "List packages for one or more configured repos\n "} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nlists configured repos This can be called either as a ``repo`` command or a ``list`` command", "response": "def _repo_list(self, args):\n '''\n List configured repos\n\n This can be called either as a ``repo`` command or a ``list`` command\n '''\n repo_metadata = self._get_repo_metadata()\n for repo in repo_metadata:\n self.ui.status(repo)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ninstall a package from a repo", "response": "def _install(self, args):\n '''\n Install a package from a repo\n '''\n if len(args) < 2:\n raise SPMInvocationError('A package must be specified')\n\n caller_opts = self.opts.copy()\n caller_opts['file_client'] = 'local'\n self.caller = salt.client.Caller(mopts=caller_opts)\n self.client = salt.client.get_local_client(self.opts['conf_file'])\n cache = salt.cache.Cache(self.opts)\n\n packages = args[1:]\n file_map = {}\n optional = []\n recommended = []\n to_install = []\n for pkg in packages:\n if pkg.endswith('.spm'):\n if self._pkgfiles_fun('path_exists', pkg):\n comps = pkg.split('-')\n comps = os.path.split('-'.join(comps[:-2]))\n pkg_name = comps[-1]\n\n formula_tar = tarfile.open(pkg, 'r:bz2')\n formula_ref = formula_tar.extractfile('{0}/FORMULA'.format(pkg_name))\n formula_def = salt.utils.yaml.safe_load(formula_ref)\n\n file_map[pkg_name] = pkg\n to_, op_, re_ = self._check_all_deps(\n pkg_name=pkg_name,\n pkg_file=pkg,\n formula_def=formula_def\n )\n to_install.extend(to_)\n optional.extend(op_)\n recommended.extend(re_)\n formula_tar.close()\n else:\n raise SPMInvocationError('Package file {0} not found'.format(pkg))\n else:\n to_, op_, re_ = self._check_all_deps(pkg_name=pkg)\n to_install.extend(to_)\n optional.extend(op_)\n recommended.extend(re_)\n\n optional = set(filter(len, optional))\n if optional:\n self.ui.status('The following dependencies are optional:\\n\\t{0}\\n'.format(\n '\\n\\t'.join(optional)\n ))\n recommended = set(filter(len, recommended))\n if recommended:\n self.ui.status('The following dependencies are recommended:\\n\\t{0}\\n'.format(\n '\\n\\t'.join(recommended)\n ))\n\n to_install = set(filter(len, to_install))\n msg = 'Installing packages:\\n\\t{0}\\n'.format('\\n\\t'.join(to_install))\n if not self.opts['assume_yes']:\n self.ui.confirm(msg)\n\n repo_metadata = self._get_repo_metadata()\n\n dl_list = {}\n for package in to_install:\n if package in file_map:\n self._install_indv_pkg(package, file_map[package])\n else:\n for repo in repo_metadata:\n repo_info = repo_metadata[repo]\n if package in repo_info['packages']:\n dl_package = False\n repo_ver = repo_info['packages'][package]['info']['version']\n repo_rel = repo_info['packages'][package]['info']['release']\n repo_url = repo_info['info']['url']\n if package in dl_list:\n # Check package version, replace if newer version\n if repo_ver == dl_list[package]['version']:\n # Version is the same, check release\n if repo_rel > dl_list[package]['release']:\n dl_package = True\n elif repo_rel == dl_list[package]['release']:\n # Version and release are the same, give\n # preference to local (file://) repos\n if dl_list[package]['source'].startswith('file://'):\n if not repo_url.startswith('file://'):\n dl_package = True\n elif repo_ver > dl_list[package]['version']:\n dl_package = True\n else:\n dl_package = True\n\n if dl_package is True:\n # Put together download directory\n cache_path = os.path.join(\n self.opts['spm_cache_dir'],\n repo\n )\n\n # Put together download paths\n dl_url = '{0}/{1}'.format(\n repo_info['info']['url'],\n repo_info['packages'][package]['filename']\n )\n out_file = os.path.join(\n cache_path,\n repo_info['packages'][package]['filename']\n )\n dl_list[package] = {\n 'version': repo_ver,\n 'release': repo_rel,\n 'source': dl_url,\n 'dest_dir': cache_path,\n 'dest_file': out_file,\n }\n\n for package in dl_list:\n dl_url = dl_list[package]['source']\n cache_path = dl_list[package]['dest_dir']\n out_file = dl_list[package]['dest_file']\n\n # Make sure download directory exists\n if not os.path.exists(cache_path):\n os.makedirs(cache_path)\n\n # Download the package\n if dl_url.startswith('file://'):\n dl_url = dl_url.replace('file://', '')\n shutil.copyfile(dl_url, out_file)\n else:\n with salt.utils.files.fopen(out_file, 'w') as outf:\n outf.write(self._query_http(dl_url, repo_info['info']))\n\n # First we download everything, then we install\n for package in dl_list:\n out_file = dl_list[package]['dest_file']\n # Kick off the install\n self._install_indv_pkg(package, out_file)\n return"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ninstalls a package from a file", "response": "def _local_install(self, args, pkg_name=None):\n '''\n Install a package from a file\n '''\n if len(args) < 2:\n raise SPMInvocationError('A package file must be specified')\n\n self._install(args)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _check_all_deps(self, pkg_name=None, pkg_file=None, formula_def=None):\n '''\n Starting with one package, check all packages for dependencies\n '''\n if pkg_file and not os.path.exists(pkg_file):\n raise SPMInvocationError('Package file {0} not found'.format(pkg_file))\n\n self.repo_metadata = self._get_repo_metadata()\n if not formula_def:\n for repo in self.repo_metadata:\n if not isinstance(self.repo_metadata[repo]['packages'], dict):\n continue\n if pkg_name in self.repo_metadata[repo]['packages']:\n formula_def = self.repo_metadata[repo]['packages'][pkg_name]['info']\n\n if not formula_def:\n raise SPMInvocationError('Unable to read formula for {0}'.format(pkg_name))\n\n # Check to see if the package is already installed\n pkg_info = self._pkgdb_fun('info', pkg_name, self.db_conn)\n pkgs_to_install = []\n if pkg_info is None or self.opts['force']:\n pkgs_to_install.append(pkg_name)\n elif pkg_info is not None and not self.opts['force']:\n raise SPMPackageError(\n 'Package {0} already installed, not installing again'.format(formula_def['name'])\n )\n\n optional_install = []\n recommended_install = []\n if 'dependencies' in formula_def or 'optional' in formula_def or 'recommended' in formula_def:\n self.avail_pkgs = {}\n for repo in self.repo_metadata:\n if not isinstance(self.repo_metadata[repo]['packages'], dict):\n continue\n for pkg in self.repo_metadata[repo]['packages']:\n self.avail_pkgs[pkg] = repo\n\n needs, unavail, optional, recommended = self._resolve_deps(formula_def)\n\n if unavail:\n raise SPMPackageError(\n 'Cannot install {0}, the following dependencies are needed:\\n\\n{1}'.format(\n formula_def['name'], '\\n'.join(unavail))\n )\n\n if optional:\n optional_install.extend(optional)\n for dep_pkg in optional:\n pkg_info = self._pkgdb_fun('info', formula_def['name'])\n msg = dep_pkg\n if isinstance(pkg_info, dict):\n msg = '{0} [Installed]'.format(dep_pkg)\n optional_install.append(msg)\n\n if recommended:\n recommended_install.extend(recommended)\n for dep_pkg in recommended:\n pkg_info = self._pkgdb_fun('info', formula_def['name'])\n msg = dep_pkg\n if isinstance(pkg_info, dict):\n msg = '{0} [Installed]'.format(dep_pkg)\n recommended_install.append(msg)\n\n if needs:\n pkgs_to_install.extend(needs)\n for dep_pkg in needs:\n pkg_info = self._pkgdb_fun('info', formula_def['name'])\n msg = dep_pkg\n if isinstance(pkg_info, dict):\n msg = '{0} [Installed]'.format(dep_pkg)\n\n return pkgs_to_install, optional_install, recommended_install", "response": "Checks to see if a package is installed and if so checks if all dependencies are needed."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ninstalls one individual package", "response": "def _install_indv_pkg(self, pkg_name, pkg_file):\n '''\n Install one individual package\n '''\n self.ui.status('... installing {0}'.format(pkg_name))\n formula_tar = tarfile.open(pkg_file, 'r:bz2')\n formula_ref = formula_tar.extractfile('{0}/FORMULA'.format(pkg_name))\n formula_def = salt.utils.yaml.safe_load(formula_ref)\n\n for field in ('version', 'release', 'summary', 'description'):\n if field not in formula_def:\n raise SPMPackageError('Invalid package: the {0} was not found'.format(field))\n\n pkg_files = formula_tar.getmembers()\n\n # First pass: check for files that already exist\n existing_files = self._pkgfiles_fun('check_existing', pkg_name, pkg_files, formula_def)\n\n if existing_files and not self.opts['force']:\n raise SPMPackageError('Not installing {0} due to existing files:\\n\\n{1}'.format(\n pkg_name, '\\n'.join(existing_files))\n )\n\n # We've decided to install\n self._pkgdb_fun('register_pkg', pkg_name, formula_def, self.db_conn)\n\n # Run the pre_local_state script, if present\n if 'pre_local_state' in formula_def:\n high_data = self._render(formula_def['pre_local_state'], formula_def)\n ret = self.caller.cmd('state.high', data=high_data)\n if 'pre_tgt_state' in formula_def:\n log.debug('Executing pre_tgt_state script')\n high_data = self._render(formula_def['pre_tgt_state']['data'], formula_def)\n tgt = formula_def['pre_tgt_state']['tgt']\n ret = self.client.run_job(\n tgt=formula_def['pre_tgt_state']['tgt'],\n fun='state.high',\n tgt_type=formula_def['pre_tgt_state'].get('tgt_type', 'glob'),\n timout=self.opts['timeout'],\n data=high_data,\n )\n\n # No defaults for this in config.py; default to the current running\n # user and group\n if salt.utils.platform.is_windows():\n uname = gname = salt.utils.win_functions.get_current_user()\n uname_sid = salt.utils.win_functions.get_sid_from_name(uname)\n uid = self.opts.get('spm_uid', uname_sid)\n gid = self.opts.get('spm_gid', uname_sid)\n else:\n uid = self.opts.get('spm_uid', os.getuid())\n gid = self.opts.get('spm_gid', os.getgid())\n uname = pwd.getpwuid(uid)[0]\n gname = grp.getgrgid(gid)[0]\n\n # Second pass: install the files\n for member in pkg_files:\n member.uid = uid\n member.gid = gid\n member.uname = uname\n member.gname = gname\n\n out_path = self._pkgfiles_fun('install_file',\n pkg_name,\n formula_tar,\n member,\n formula_def,\n self.files_conn)\n if out_path is not False:\n if member.isdir():\n digest = ''\n else:\n self._verbose('Installing file {0} to {1}'.format(member.name, out_path), log.trace)\n file_hash = hashlib.sha1()\n digest = self._pkgfiles_fun('hash_file',\n os.path.join(out_path, member.name),\n file_hash,\n self.files_conn)\n self._pkgdb_fun('register_file',\n pkg_name,\n member,\n out_path,\n digest,\n self.db_conn)\n\n # Run the post_local_state script, if present\n if 'post_local_state' in formula_def:\n log.debug('Executing post_local_state script')\n high_data = self._render(formula_def['post_local_state'], formula_def)\n self.caller.cmd('state.high', data=high_data)\n if 'post_tgt_state' in formula_def:\n log.debug('Executing post_tgt_state script')\n high_data = self._render(formula_def['post_tgt_state']['data'], formula_def)\n tgt = formula_def['post_tgt_state']['tgt']\n ret = self.client.run_job(\n tgt=formula_def['post_tgt_state']['tgt'],\n fun='state.high',\n tgt_type=formula_def['post_tgt_state'].get('tgt_type', 'glob'),\n timout=self.opts['timeout'],\n data=high_data,\n )\n\n formula_tar.close()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _resolve_deps(self, formula_def):\n '''\n Return a list of packages which need to be installed, to resolve all\n dependencies\n '''\n pkg_info = self.pkgdb['{0}.info'.format(self.db_prov)](formula_def['name'])\n if not isinstance(pkg_info, dict):\n pkg_info = {}\n\n can_has = {}\n cant_has = []\n if 'dependencies' in formula_def and formula_def['dependencies'] is None:\n formula_def['dependencies'] = ''\n for dep in formula_def.get('dependencies', '').split(','):\n dep = dep.strip()\n if not dep:\n continue\n if self.pkgdb['{0}.info'.format(self.db_prov)](dep):\n continue\n\n if dep in self.avail_pkgs:\n can_has[dep] = self.avail_pkgs[dep]\n else:\n cant_has.append(dep)\n\n optional = formula_def.get('optional', '').split(',')\n recommended = formula_def.get('recommended', '').split(',')\n\n inspected = []\n to_inspect = can_has.copy()\n while to_inspect:\n dep = next(six.iterkeys(to_inspect))\n del to_inspect[dep]\n\n # Don't try to resolve the same package more than once\n if dep in inspected:\n continue\n inspected.append(dep)\n\n repo_contents = self.repo_metadata.get(can_has[dep], {})\n repo_packages = repo_contents.get('packages', {})\n dep_formula = repo_packages.get(dep, {}).get('info', {})\n\n also_can, also_cant, opt_dep, rec_dep = self._resolve_deps(dep_formula)\n can_has.update(also_can)\n cant_has = sorted(set(cant_has + also_cant))\n optional = sorted(set(optional + opt_dep))\n recommended = sorted(set(recommended + rec_dep))\n\n return can_has, cant_has, optional, recommended", "response": "Returns a list of packages which need to be installed to resolve all\n dependencies"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ntraverse through all repo files and apply the functionality provided in the callback to them", "response": "def _traverse_repos(self, callback, repo_name=None):\n '''\n Traverse through all repo files and apply the functionality provided in\n the callback to them\n '''\n repo_files = []\n if os.path.exists(self.opts['spm_repos_config']):\n repo_files.append(self.opts['spm_repos_config'])\n\n for (dirpath, dirnames, filenames) in salt.utils.path.os_walk('{0}.d'.format(self.opts['spm_repos_config'])):\n for repo_file in filenames:\n if not repo_file.endswith('.repo'):\n continue\n repo_files.append(repo_file)\n\n for repo_file in repo_files:\n repo_path = '{0}.d/{1}'.format(self.opts['spm_repos_config'], repo_file)\n with salt.utils.files.fopen(repo_path) as rph:\n repo_data = salt.utils.yaml.safe_load(rph)\n for repo in repo_data:\n if repo_data[repo].get('enabled', True) is False:\n continue\n if repo_name is not None and repo != repo_name:\n continue\n callback(repo, repo_data[repo])"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _query_http(self, dl_path, repo_info):\n '''\n Download files via http\n '''\n query = None\n response = None\n\n try:\n if 'username' in repo_info:\n try:\n if 'password' in repo_info:\n query = http.query(\n dl_path, text=True,\n username=repo_info['username'],\n password=repo_info['password']\n )\n else:\n raise SPMException('Auth defined, but password is not set for username: \\'{0}\\''\n .format(repo_info['username']))\n except SPMException as exc:\n self.ui.error(six.text_type(exc))\n else:\n query = http.query(dl_path, text=True)\n except SPMException as exc:\n self.ui.error(six.text_type(exc))\n\n try:\n if query:\n if 'SPM-METADATA' in dl_path:\n response = salt.utils.yaml.safe_load(query.get('text', '{}'))\n else:\n response = query.get('text')\n else:\n raise SPMException('Response is empty, please check for Errors above.')\n except SPMException as exc:\n self.ui.error(six.text_type(exc))\n\n return response", "response": "Download files via http and return the response."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _download_repo_metadata(self, args):\n '''\n Connect to all repos and download metadata\n '''\n cache = salt.cache.Cache(self.opts, self.opts['spm_cache_dir'])\n\n def _update_metadata(repo, repo_info):\n dl_path = '{0}/SPM-METADATA'.format(repo_info['url'])\n if dl_path.startswith('file://'):\n dl_path = dl_path.replace('file://', '')\n with salt.utils.files.fopen(dl_path, 'r') as rpm:\n metadata = salt.utils.yaml.safe_load(rpm)\n else:\n metadata = self._query_http(dl_path, repo_info)\n\n cache.store('.', repo, metadata)\n\n repo_name = args[1] if len(args) > 1 else None\n self._traverse_repos(_update_metadata, repo_name)", "response": "Connect to all repos and download metadata\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget the repo metadata", "response": "def _get_repo_metadata(self):\n '''\n Return cached repo metadata\n '''\n cache = salt.cache.Cache(self.opts, self.opts['spm_cache_dir'])\n metadata = {}\n\n def _read_metadata(repo, repo_info):\n if cache.updated('.', repo) is None:\n log.warning('Updating repo metadata')\n self._download_repo_metadata({})\n\n metadata[repo] = {\n 'info': repo_info,\n 'packages': cache.fetch('.', repo),\n }\n\n self._traverse_repos(_read_metadata)\n return metadata"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncreating a repository for all the SPM files in that directory and create a list of all the SPM - METADATA files.", "response": "def _create_repo(self, args):\n '''\n Scan a directory and create an SPM-METADATA file which describes\n all of the SPM files in that directory.\n '''\n if len(args) < 2:\n raise SPMInvocationError('A path to a directory must be specified')\n\n if args[1] == '.':\n repo_path = os.getcwdu()\n else:\n repo_path = args[1]\n\n old_files = []\n repo_metadata = {}\n for (dirpath, dirnames, filenames) in salt.utils.path.os_walk(repo_path):\n for spm_file in filenames:\n if not spm_file.endswith('.spm'):\n continue\n spm_path = '{0}/{1}'.format(repo_path, spm_file)\n if not tarfile.is_tarfile(spm_path):\n continue\n comps = spm_file.split('-')\n spm_name = '-'.join(comps[:-2])\n spm_fh = tarfile.open(spm_path, 'r:bz2')\n formula_handle = spm_fh.extractfile('{0}/FORMULA'.format(spm_name))\n formula_conf = salt.utils.yaml.safe_load(formula_handle.read())\n\n use_formula = True\n if spm_name in repo_metadata:\n # This package is already in the repo; use the latest\n cur_info = repo_metadata[spm_name]['info']\n new_info = formula_conf\n if int(new_info['version']) == int(cur_info['version']):\n # Version is the same, check release\n if int(new_info['release']) < int(cur_info['release']):\n # This is an old release; don't use it\n use_formula = False\n elif int(new_info['version']) < int(cur_info['version']):\n # This is an old version; don't use it\n use_formula = False\n\n if use_formula is True:\n # Ignore/archive/delete the old version\n log.debug(\n '%s %s-%s had been added, but %s-%s will replace it',\n spm_name, cur_info['version'], cur_info['release'],\n new_info['version'], new_info['release']\n )\n old_files.append(repo_metadata[spm_name]['filename'])\n else:\n # Ignore/archive/delete the new version\n log.debug(\n '%s %s-%s has been found, but is older than %s-%s',\n spm_name, new_info['version'], new_info['release'],\n cur_info['version'], cur_info['release']\n )\n old_files.append(spm_file)\n\n if use_formula is True:\n log.debug(\n 'adding %s-%s-%s to the repo',\n formula_conf['name'], formula_conf['version'],\n formula_conf['release']\n )\n repo_metadata[spm_name] = {\n 'info': formula_conf.copy(),\n }\n repo_metadata[spm_name]['filename'] = spm_file\n\n metadata_filename = '{0}/SPM-METADATA'.format(repo_path)\n with salt.utils.files.fopen(metadata_filename, 'w') as mfh:\n salt.utils.yaml.safe_dump(\n repo_metadata,\n mfh,\n indent=4,\n canonical=False,\n default_flow_style=False,\n )\n\n log.debug('Wrote %s', metadata_filename)\n\n for file_ in old_files:\n if self.opts['spm_repo_dups'] == 'ignore':\n # ignore old packages, but still only add the latest\n log.debug('%s will be left in the directory', file_)\n elif self.opts['spm_repo_dups'] == 'archive':\n # spm_repo_archive_path is where old packages are moved\n if not os.path.exists('./archive'):\n try:\n os.makedirs('./archive')\n log.debug('%s has been archived', file_)\n except IOError:\n log.error('Unable to create archive directory')\n try:\n shutil.move(file_, './archive')\n except (IOError, OSError):\n log.error('Unable to archive %s', file_)\n elif self.opts['spm_repo_dups'] == 'delete':\n # delete old packages from the repo\n try:\n os.remove(file_)\n log.debug('%s has been deleted', file_)\n except IOError:\n log.error('Unable to delete %s', file_)\n except OSError:\n # The file has already been deleted\n pass"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _remove(self, args):\n '''\n Remove a package\n '''\n if len(args) < 2:\n raise SPMInvocationError('A package must be specified')\n\n packages = args[1:]\n msg = 'Removing packages:\\n\\t{0}'.format('\\n\\t'.join(packages))\n\n if not self.opts['assume_yes']:\n self.ui.confirm(msg)\n\n for package in packages:\n self.ui.status('... removing {0}'.format(package))\n\n if not self._pkgdb_fun('db_exists', self.opts['spm_db']):\n raise SPMDatabaseError('No database at {0}, cannot remove {1}'.format(self.opts['spm_db'], package))\n\n # Look at local repo index\n pkg_info = self._pkgdb_fun('info', package, self.db_conn)\n if pkg_info is None:\n raise SPMInvocationError('Package {0} not installed'.format(package))\n\n # Find files that have not changed and remove them\n files = self._pkgdb_fun('list_files', package, self.db_conn)\n dirs = []\n for filerow in files:\n if self._pkgfiles_fun('path_isdir', filerow[0]):\n dirs.append(filerow[0])\n continue\n file_hash = hashlib.sha1()\n digest = self._pkgfiles_fun('hash_file', filerow[0], file_hash, self.files_conn)\n if filerow[1] == digest:\n self._verbose('Removing file {0}'.format(filerow[0]), log.trace)\n self._pkgfiles_fun('remove_file', filerow[0], self.files_conn)\n else:\n self._verbose('Not removing file {0}'.format(filerow[0]), log.trace)\n self._pkgdb_fun('unregister_file', filerow[0], package, self.db_conn)\n\n # Clean up directories\n for dir_ in sorted(dirs, reverse=True):\n self._pkgdb_fun('unregister_file', dir_, package, self.db_conn)\n try:\n self._verbose('Removing directory {0}'.format(dir_), log.trace)\n os.rmdir(dir_)\n except OSError:\n # Leave directories in place that still have files in them\n self._verbose('Cannot remove directory {0}, probably not empty'.format(dir_), log.trace)\n\n self._pkgdb_fun('unregister_pkg', package, self.db_conn)", "response": "Remove a package from the database."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _local_info(self, args):\n '''\n List info for a package file\n '''\n if len(args) < 2:\n raise SPMInvocationError('A package filename must be specified')\n\n pkg_file = args[1]\n\n if not os.path.exists(pkg_file):\n raise SPMInvocationError('Package file {0} not found'.format(pkg_file))\n\n comps = pkg_file.split('-')\n comps = '-'.join(comps[:-2]).split('/')\n name = comps[-1]\n\n formula_tar = tarfile.open(pkg_file, 'r:bz2')\n formula_ref = formula_tar.extractfile('{0}/FORMULA'.format(name))\n formula_def = salt.utils.yaml.safe_load(formula_ref)\n\n self.ui.status(self._get_info(formula_def))\n formula_tar.close()", "response": "Get info for a local package file"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _info(self, args):\n '''\n List info for a package\n '''\n if len(args) < 2:\n raise SPMInvocationError('A package must be specified')\n\n package = args[1]\n\n pkg_info = self._pkgdb_fun('info', package, self.db_conn)\n if pkg_info is None:\n raise SPMPackageError('package {0} not installed'.format(package))\n self.ui.status(self._get_info(pkg_info))", "response": "Show info for a package"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nlists files for a package file", "response": "def _local_list_files(self, args):\n '''\n List files for a package file\n '''\n if len(args) < 2:\n raise SPMInvocationError('A package filename must be specified')\n\n pkg_file = args[1]\n if not os.path.exists(pkg_file):\n raise SPMPackageError('Package file {0} not found'.format(pkg_file))\n formula_tar = tarfile.open(pkg_file, 'r:bz2')\n pkg_files = formula_tar.getmembers()\n\n for member in pkg_files:\n self.ui.status(member.name)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _list_packages(self, args):\n '''\n List files for an installed package\n '''\n packages = self._pkgdb_fun('list_packages', self.db_conn)\n for package in packages:\n if self.opts['verbose']:\n status_msg = ','.join(package)\n else:\n status_msg = package[0]\n self.ui.status(status_msg)", "response": "List packages for an installed package\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _list_files(self, args):\n '''\n List files for an installed package\n '''\n if len(args) < 2:\n raise SPMInvocationError('A package name must be specified')\n\n package = args[-1]\n\n files = self._pkgdb_fun('list_files', package, self.db_conn)\n if files is None:\n raise SPMPackageError('package {0} not installed'.format(package))\n else:\n for file_ in files:\n if self.opts['verbose']:\n status_msg = ','.join(file_)\n else:\n status_msg = file_[0]\n self.ui.status(status_msg)", "response": "List files for an installed package"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nbuild a package and return the object.", "response": "def _build(self, args):\n '''\n Build a package\n '''\n if len(args) < 2:\n raise SPMInvocationError('A path to a formula must be specified')\n\n self.abspath = args[1].rstrip('/')\n comps = self.abspath.split('/')\n self.relpath = comps[-1]\n\n formula_path = '{0}/FORMULA'.format(self.abspath)\n if not os.path.exists(formula_path):\n raise SPMPackageError('Formula file {0} not found'.format(formula_path))\n with salt.utils.files.fopen(formula_path) as fp_:\n formula_conf = salt.utils.yaml.safe_load(fp_)\n\n for field in ('name', 'version', 'release', 'summary', 'description'):\n if field not in formula_conf:\n raise SPMPackageError('Invalid package: a {0} must be defined'.format(field))\n\n out_path = '{0}/{1}-{2}-{3}.spm'.format(\n self.opts['spm_build_dir'],\n formula_conf['name'],\n formula_conf['version'],\n formula_conf['release'],\n )\n\n if not os.path.exists(self.opts['spm_build_dir']):\n os.mkdir(self.opts['spm_build_dir'])\n\n self.formula_conf = formula_conf\n\n formula_tar = tarfile.open(out_path, 'w:bz2')\n\n if 'files' in formula_conf:\n # This allows files to be added to the SPM file in a specific order.\n # It also allows for files to be tagged as a certain type, as with\n # RPM files. This tag is ignored here, but is used when installing\n # the SPM file.\n if isinstance(formula_conf['files'], list):\n formula_dir = tarfile.TarInfo(formula_conf['name'])\n formula_dir.type = tarfile.DIRTYPE\n formula_tar.addfile(formula_dir)\n for file_ in formula_conf['files']:\n for ftype in FILE_TYPES:\n if file_.startswith('{0}|'.format(ftype)):\n file_ = file_.lstrip('{0}|'.format(ftype))\n formula_tar.add(\n os.path.join(os.getcwd(), file_),\n os.path.join(formula_conf['name'], file_),\n )\n else:\n # If no files are specified, then the whole directory will be added.\n try:\n formula_tar.add(formula_path, formula_conf['name'], filter=self._exclude)\n formula_tar.add(self.abspath, formula_conf['name'], filter=self._exclude)\n except TypeError:\n formula_tar.add(formula_path, formula_conf['name'], exclude=self._exclude)\n formula_tar.add(self.abspath, formula_conf['name'], exclude=self._exclude)\n formula_tar.close()\n\n self.ui.status('Built package {0}'.format(out_path))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nexclude based on opts Exclude based on opts", "response": "def _exclude(self, member):\n '''\n Exclude based on opts\n '''\n if isinstance(member, string_types):\n return None\n\n for item in self.opts['spm_build_exclude']:\n if member.name.startswith('{0}/{1}'.format(self.formula_conf['name'], item)):\n return None\n elif member.name.startswith('{0}/{1}'.format(self.abspath, item)):\n return None\n return member"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nrenders a [pre|post]_local_state or [pre|post]_tgt_state script", "response": "def _render(self, data, formula_def):\n '''\n Render a [pre|post]_local_state or [pre|post]_tgt_state script\n '''\n # FORMULA can contain a renderer option\n renderer = formula_def.get('renderer', self.opts.get('renderer', 'jinja|yaml'))\n rend = salt.loader.render(self.opts, {})\n blacklist = self.opts.get('renderer_blacklist')\n whitelist = self.opts.get('renderer_whitelist')\n template_vars = formula_def.copy()\n template_vars['opts'] = self.opts.copy()\n return compile_template(\n ':string:',\n rend,\n renderer,\n blacklist,\n whitelist,\n input_data=data,\n **template_vars\n )"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nchecks to see if a queue exists.", "response": "def exists(name, region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if a queue exists.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_sqs.exists myqueue region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n conn.get_queue_url(QueueName=name)\n except botocore.exceptions.ClientError as e:\n missing_code = 'AWS.SimpleQueueService.NonExistentQueue'\n if e.response.get('Error', {}).get('Code') == missing_code:\n return {'result': False}\n return {'error': __utils__['boto3.get_error'](e)}\n return {'result': True}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create(\n name,\n attributes=None,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n):\n '''\n Create an SQS queue.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_sqs.create myqueue region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n if attributes is None:\n attributes = {}\n attributes = _preprocess_attributes(attributes)\n\n try:\n conn.create_queue(QueueName=name, Attributes=attributes)\n except botocore.exceptions.ClientError as e:\n return {'error': __utils__['boto3.get_error'](e)}\n return {'result': True}", "response": "Create an SQS queue."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete(name, region=None, key=None, keyid=None, profile=None):\n '''\n Delete an SQS queue.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_sqs.delete myqueue region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n url = conn.get_queue_url(QueueName=name)['QueueUrl']\n conn.delete_queue(QueueUrl=url)\n except botocore.exceptions.ClientError as e:\n return {'error': __utils__['boto3.get_error'](e)}\n return {'result': True}", "response": "Delete an SQS queue."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of the names of all visible queues.", "response": "def list_(prefix='', region=None, key=None, keyid=None, profile=None):\n '''\n Return a list of the names of all visible queues.\n\n .. versionadded:: 2016.11.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_sqs.list region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n def extract_name(queue_url):\n # Note: this logic taken from boto, so should be safe\n return _urlparse(queue_url).path.split('/')[2]\n\n try:\n r = conn.list_queues(QueueNamePrefix=prefix)\n # The 'QueueUrls' attribute is missing if there are no queues\n urls = r.get('QueueUrls', [])\n return {'result': [extract_name(url) for url in urls]}\n except botocore.exceptions.ClientError as e:\n return {'error': __utils__['boto3.get_error'](e)}"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets the attributes currently set on an SQS queue.", "response": "def get_attributes(name, region=None, key=None, keyid=None, profile=None):\n '''\n Return attributes currently set on an SQS queue.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_sqs.get_attributes myqueue\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n url = conn.get_queue_url(QueueName=name)['QueueUrl']\n r = conn.get_queue_attributes(QueueUrl=url, AttributeNames=['All'])\n return {'result': r['Attributes']}\n except botocore.exceptions.ClientError as e:\n return {'error': __utils__['boto3.get_error'](e)}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset attributes on an SQS queue.", "response": "def set_attributes(\n name,\n attributes,\n region=None,\n key=None,\n keyid=None,\n profile=None,\n):\n '''\n Set attributes on an SQS queue.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_sqs.set_attributes myqueue '{ReceiveMessageWaitTimeSeconds: 20}' region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n attributes = _preprocess_attributes(attributes)\n\n try:\n url = conn.get_queue_url(QueueName=name)['QueueUrl']\n conn.set_queue_attributes(QueueUrl=url, Attributes=attributes)\n except botocore.exceptions.ClientError as e:\n return {'error': __utils__['boto3.get_error'](e)}\n return {'result': True}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupdating the state of a template in the network.", "response": "def _update_config(template_name,\n template_source=None,\n template_hash=None,\n template_hash_name=None,\n template_user='root',\n template_group='root',\n template_mode='755',\n template_attrs='--------------e----',\n saltenv=None,\n template_engine='jinja',\n skip_verify=False,\n defaults=None,\n test=False,\n commit=True,\n debug=False,\n replace=False,\n **template_vars):\n '''\n Call the necessary functions in order to execute the state.\n For the moment this only calls the ``net.load_template`` function from the\n :mod:`Network-related basic features execution module <salt.modules.napalm_network>`, but this may change in time.\n '''\n\n return __salt__['net.load_template'](template_name,\n template_source=template_source,\n template_hash=template_hash,\n template_hash_name=template_hash_name,\n template_user=template_user,\n template_group=template_group,\n template_mode=template_mode,\n template_attrs=template_attrs,\n saltenv=saltenv,\n template_engine=template_engine,\n skip_verify=skip_verify,\n defaults=defaults,\n test=test,\n commit=commit,\n debug=debug,\n replace=replace,\n **template_vars)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreplaces occurrences of a pattern in a configuration source.", "response": "def replace_pattern(name,\n pattern,\n repl,\n count=0,\n flags=8,\n bufsize=1,\n append_if_not_found=False,\n prepend_if_not_found=False,\n not_found_content=None,\n search_only=False,\n show_changes=True,\n backslash_literal=False,\n source='running',\n path=None,\n test=False,\n replace=True,\n debug=False,\n commit=True):\n '''\n .. versionadded:: 2019.2.0\n\n Replace occurrences of a pattern in the configuration source. If\n ``show_changes`` is ``True``, then a diff of what changed will be returned,\n otherwise a ``True`` will be returned when changes are made, and ``False``\n when no changes are made.\n This is a pure Python implementation that wraps Python's :py:func:`~re.sub`.\n\n pattern\n A regular expression, to be matched using Python's\n :py:func:`~re.search`.\n\n repl\n The replacement text.\n\n count: ``0``\n Maximum number of pattern occurrences to be replaced. If count is a\n positive integer ``n``, only ``n`` occurrences will be replaced,\n otherwise all occurrences will be replaced.\n\n flags (list or int): ``8``\n A list of flags defined in the ``re`` module documentation from the\n Python standard library. Each list item should be a string that will\n correlate to the human-friendly flag name. E.g., ``['IGNORECASE',\n 'MULTILINE']``. Optionally, ``flags`` may be an int, with a value\n corresponding to the XOR (``|``) of all the desired flags. Defaults to\n 8 (which supports 'MULTILINE').\n\n bufsize (int or str): ``1``\n How much of the configuration to buffer into memory at once. The\n default value ``1`` processes one line at a time. The special value\n ``file`` may be specified which will read the entire file into memory\n before processing.\n\n append_if_not_found: ``False``\n If set to ``True``, and pattern is not found, then the content will be\n appended to the file.\n\n prepend_if_not_found: ``False``\n If set to ``True`` and pattern is not found, then the content will be\n prepended to the file.\n\n not_found_content\n Content to use for append/prepend if not found. If None (default), uses\n ``repl``. Useful when ``repl`` uses references to group in pattern.\n\n search_only: ``False``\n If set to true, this no changes will be performed on the file, and this\n function will simply return ``True`` if the pattern was matched, and\n ``False`` if not.\n\n show_changes: ``True``\n If ``True``, return a diff of changes made. Otherwise, return ``True``\n if changes were made, and ``False`` if not.\n\n backslash_literal: ``False``\n Interpret backslashes as literal backslashes for the repl and not\n escape characters. This will help when using append/prepend so that\n the backslashes are not interpreted for the repl on the second run of\n the state.\n\n source: ``running``\n The configuration source. Choose from: ``running``, ``candidate``, or\n ``startup``. Default: ``running``.\n\n path\n Save the temporary configuration to a specific path, then read from\n there.\n\n test: ``False``\n Dry run? If set as ``True``, will apply the config, discard and return\n the changes. Default: ``False`` and will commit the changes on the\n device.\n\n commit: ``True``\n Commit the configuration changes? Default: ``True``.\n\n debug: ``False``\n Debug mode. Will insert a new key in the output dictionary, as\n ``loaded_config`` containing the raw configuration loaded on the device.\n\n replace: ``True``\n Load and replace the configuration. Default: ``True``.\n\n If an equal sign (``=``) appears in an argument to a Salt command it is\n interpreted as a keyword argument in the format ``key=val``. That\n processing can be bypassed in order to pass an equal sign through to the\n remote shell command by manually specifying the kwarg:\n\n State SLS Example:\n\n .. code-block:: yaml\n\n update_policy_name:\n netconfig.replace_pattern:\n - pattern: OLD-POLICY-NAME\n - repl: new-policy-name\n - debug: true\n '''\n ret = salt.utils.napalm.default_ret(name)\n # the user can override the flags the equivalent CLI args\n # which have higher precedence\n test = __salt__['config.merge']('test', test)\n debug = __salt__['config.merge']('debug', debug)\n commit = __salt__['config.merge']('commit', commit)\n replace = __salt__['config.merge']('replace', replace) # this might be a bit risky\n replace_ret = __salt__['net.replace_pattern'](pattern,\n repl,\n count=count,\n flags=flags,\n bufsize=bufsize,\n append_if_not_found=append_if_not_found,\n prepend_if_not_found=prepend_if_not_found,\n not_found_content=not_found_content,\n search_only=search_only,\n show_changes=show_changes,\n backslash_literal=backslash_literal,\n source=source,\n path=path,\n test=test,\n replace=replace,\n debug=debug,\n commit=commit)\n return salt.utils.napalm.loaded_ret(ret, replace_ret, test, debug)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nsaves the configuration to a file on the local file system.", "response": "def saved(name,\n source='running',\n user=None,\n group=None,\n mode=None,\n attrs=None,\n makedirs=False,\n dir_mode=None,\n replace=True,\n backup='',\n show_changes=True,\n create=True,\n tmp_dir='',\n tmp_ext='',\n encoding=None,\n encoding_errors='strict',\n allow_empty=False,\n follow_symlinks=True,\n check_cmd=None,\n win_owner=None,\n win_perms=None,\n win_deny_perms=None,\n win_inheritance=True,\n win_perms_reset=False,\n **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Save the configuration to a file on the local file system.\n\n name\n Absolute path to file where to save the configuration.\n To push the files to the Master, use\n :mod:`cp.push <salt.modules.cp.push>` Execution function.\n\n source: ``running``\n The configuration source. Choose from: ``running``, ``candidate``,\n ``startup``. Default: ``running``.\n\n user\n The user to own the file, this defaults to the user salt is running as\n on the minion\n\n group\n The group ownership set for the file, this defaults to the group salt\n is running as on the minion. On Windows, this is ignored\n\n mode\n The permissions to set on this file, e.g. ``644``, ``0775``, or\n ``4664``.\n The default mode for new files and directories corresponds to the\n umask of the salt process. The mode of existing files and directories\n will only be changed if ``mode`` is specified.\n\n .. note::\n This option is **not** supported on Windows.\n attrs\n The attributes to have on this file, e.g. ``a``, ``i``. The attributes\n can be any or a combination of the following characters:\n ``aAcCdDeijPsStTu``.\n\n .. note::\n This option is **not** supported on Windows.\n\n makedirs: ``False``\n If set to ``True``, then the parent directories will be created to\n facilitate the creation of the named file. If ``False``, and the parent\n directory of the destination file doesn't exist, the state will fail.\n\n dir_mode\n If directories are to be created, passing this option specifies the\n permissions for those directories. If this is not set, directories\n will be assigned permissions by adding the execute bit to the mode of\n the files.\n\n The default mode for new files and directories corresponds umask of salt\n process. For existing files and directories it's not enforced.\n\n replace: ``True``\n If set to ``False`` and the file already exists, the file will not be\n modified even if changes would otherwise be made. Permissions and\n ownership will still be enforced, however.\n\n backup\n Overrides the default backup mode for this specific file. See\n :ref:`backup_mode documentation <file-state-backups>` for more details.\n\n show_changes: ``True``\n Output a unified diff of the old file and the new file. If ``False``\n return a boolean if any changes were made.\n\n create: ``True``\n If set to ``False``, then the file will only be managed if the file\n already exists on the system.\n\n encoding\n If specified, then the specified encoding will be used. Otherwise, the\n file will be encoded using the system locale (usually UTF-8). See\n https://docs.python.org/3/library/codecs.html#standard-encodings for\n the list of available encodings.\n\n encoding_errors: ``'strict'``\n Error encoding scheme. Default is ```'strict'```.\n See https://docs.python.org/2/library/codecs.html#codec-base-classes\n for the list of available schemes.\n\n allow_empty: ``True``\n If set to ``False``, then the state will fail if the contents specified\n by ``contents_pillar`` or ``contents_grains`` are empty.\n\n follow_symlinks: ``True``\n If the desired path is a symlink follow it and make changes to the\n file to which the symlink points.\n\n check_cmd\n The specified command will be run with an appended argument of a\n *temporary* file containing the new managed contents. If the command\n exits with a zero status the new managed contents will be written to\n the managed destination. If the command exits with a nonzero exit\n code, the state will fail and no changes will be made to the file.\n\n tmp_dir\n Directory for temp file created by ``check_cmd``. Useful for checkers\n dependent on config file location (e.g. daemons restricted to their\n own config directories by an apparmor profile).\n\n tmp_ext\n Suffix for temp file created by ``check_cmd``. Useful for checkers\n dependent on config file extension (e.g. the init-checkconf upstart\n config checker).\n\n win_owner: ``None``\n The owner of the directory. If this is not passed, user will be used. If\n user is not passed, the account under which Salt is running will be\n used.\n\n win_perms: ``None``\n A dictionary containing permissions to grant and their propagation. For\n example: ``{'Administrators': {'perms': 'full_control'}}`` Can be a\n single basic perm or a list of advanced perms. ``perms`` must be\n specified. ``applies_to`` does not apply to file objects.\n\n win_deny_perms: ``None``\n A dictionary containing permissions to deny and their propagation. For\n example: ``{'Administrators': {'perms': 'full_control'}}`` Can be a\n single basic perm or a list of advanced perms. ``perms`` must be\n specified. ``applies_to`` does not apply to file objects.\n\n win_inheritance: ``True``\n True to inherit permissions from the parent directory, False not to\n inherit permission.\n\n win_perms_reset: ``False``\n If ``True`` the existing DACL will be cleared and replaced with the\n settings defined in this function. If ``False``, new entries will be\n appended to the existing DACL. Default is ``False``.\n\n State SLS Example:\n\n .. code-block:: yaml\n\n /var/backups/{{ opts.id }}/{{ salt.status.time('%s') }}.cfg:\n netconfig.saved:\n - source: running\n - makedirs: true\n\n The state SLS above would create a backup config grouping the files by the\n Minion ID, in chronological files. For example, if the state is executed at\n on the 3rd of August 2018, at 5:15PM, on the Minion ``core1.lon01``, the\n configuration would saved in the file:\n ``/var/backups/core01.lon01/1533316558.cfg``\n '''\n ret = __salt__['net.config'](source=source)\n if not ret['result']:\n return {\n 'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ret['comment']\n }\n return __states__['file.managed'](name,\n user=user,\n group=group,\n mode=mode,\n attrs=attrs,\n makedirs=makedirs,\n dir_mode=dir_mode,\n replace=replace,\n backup=backup,\n show_changes=show_changes,\n create=create,\n contents=ret['out'][source],\n tmp_dir=tmp_dir,\n tmp_ext=tmp_ext,\n encoding=encoding,\n encoding_errors=encoding_errors,\n allow_empty=allow_empty,\n follow_symlinks=follow_symlinks,\n check_cmd=check_cmd,\n win_owner=win_owner,\n win_perms=win_perms,\n win_deny_perms=win_deny_perms,\n win_inheritance=win_inheritance,\n win_perms_reset=win_perms_reset,\n **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef managed(name,\n template_name=None,\n template_source=None,\n template_hash=None,\n template_hash_name=None,\n saltenv='base',\n template_engine='jinja',\n skip_verify=False,\n context=None,\n defaults=None,\n test=False,\n commit=True,\n debug=False,\n replace=False,\n commit_in=None,\n commit_at=None,\n revert_in=None,\n revert_at=None,\n **template_vars):\n '''\n Manages the configuration on network devices.\n\n By default this state will commit the changes on the device. If there are no changes required, it does not commit\n and the field ``already_configured`` from the output dictionary will be set as ``True`` to notify that.\n\n To avoid committing the configuration, set the argument ``test`` to ``True`` (or via the CLI argument ``test=True``)\n and will discard (dry run).\n\n To preserve the changes, set ``commit`` to ``False`` (either as CLI argument, either as state parameter).\n However, this is recommended to be used only in exceptional cases when there are applied few consecutive states\n and/or configuration changes. Otherwise the user might forget that the config DB is locked and the candidate config\n buffer is not cleared/merged in the running config.\n\n To replace the config, set ``replace`` to ``True``. This option is recommended to be used with caution!\n\n template_name\n Identifies path to the template source. The template can be either stored on the local machine,\n either remotely.\n The recommended location is under the ``file_roots`` as specified in the master config file.\n For example, let's suppose the ``file_roots`` is configured as:\n\n .. code-block:: yaml\n\n file_roots:\n base:\n - /etc/salt/states\n\n Placing the template under ``/etc/salt/states/templates/example.jinja``, it can be used as\n ``salt://templates/example.jinja``.\n Alternatively, for local files, the user can specify the absolute path.\n If remotely, the source can be retrieved via ``http``, ``https`` or ``ftp``.\n\n Examples:\n\n - ``salt://my_template.jinja``\n - ``/absolute/path/to/my_template.jinja``\n - ``http://example.com/template.cheetah``\n - ``https:/example.com/template.mako``\n - ``ftp://example.com/template.py``\n\n .. versionchanged:: 2019.2.0\n This argument can now support a list of templates to be rendered.\n The resulting configuration text is loaded at once, as a single\n configuration chunk.\n\n template_source: None\n Inline config template to be rendered and loaded on the device.\n\n template_hash: None\n Hash of the template file. Format: ``{hash_type: 'md5', 'hsum': <md5sum>}``\n\n template_hash_name: None\n When ``template_hash`` refers to a remote file, this specifies the filename to look for in that file.\n\n saltenv: base\n Specifies the template environment. This will influence the relative imports inside the templates.\n\n template_engine: jinja\n The following templates engines are supported:\n\n - :mod:`cheetah<salt.renderers.cheetah>`\n - :mod:`genshi<salt.renderers.genshi>`\n - :mod:`jinja<salt.renderers.jinja>`\n - :mod:`mako<salt.renderers.mako>`\n - :mod:`py<salt.renderers.py>`\n - :mod:`wempy<salt.renderers.wempy>`\n\n skip_verify: False\n If ``True``, hash verification of remote file sources (``http://``, ``https://``, ``ftp://``) will be skipped,\n and the ``source_hash`` argument will be ignored.\n\n .. versionchanged:: 2017.7.1\n\n test: False\n Dry run? If set to ``True``, will apply the config, discard and return the changes. Default: ``False``\n (will commit the changes on the device).\n\n commit: True\n Commit? Default: ``True``.\n\n debug: False\n Debug mode. Will insert a new key under the output dictionary, as ``loaded_config`` containing the raw\n result after the template was rendered.\n\n .. note::\n This argument cannot be used directly on the command line. Instead,\n it can be passed through the ``pillar`` variable when executing\n either of the :py:func:`state.sls <salt.modules.state.sls>` or\n :py:func:`state.apply <salt.modules.state.apply>` (see below for an\n example).\n\n commit_in: ``None``\n Commit the changes in a specific number of minutes / hours. Example of\n accepted formats: ``5`` (commit in 5 minutes), ``2m`` (commit in 2\n minutes), ``1h`` (commit the changes in 1 hour)`, ``5h30m`` (commit\n the changes in 5 hours and 30 minutes).\n\n .. note::\n This feature works on any platforms, as it does not rely on the\n native features of the network operating system.\n\n .. note::\n After the command is executed and the ``diff`` is not satisfactory,\n or for any other reasons you have to discard the commit, you are\n able to do so using the\n :py:func:`net.cancel_commit <salt.modules.napalm_network.cancel_commit>`\n execution function, using the commit ID returned by this function.\n\n .. warning::\n Using this feature, Salt will load the exact configuration you\n expect, however the diff may change in time (i.e., if an user\n applies a manual configuration change, or a different process or\n command changes the configuration in the meanwhile).\n\n .. versionadded:: 2019.2.0\n\n commit_at: ``None``\n Commit the changes at a specific time. Example of accepted formats:\n ``1am`` (will commit the changes at the next 1AM), ``13:20`` (will\n commit at 13:20), ``1:20am``, etc.\n\n .. note::\n This feature works on any platforms, as it does not rely on the\n native features of the network operating system.\n\n .. note::\n After the command is executed and the ``diff`` is not satisfactory,\n or for any other reasons you have to discard the commit, you are\n able to do so using the\n :py:func:`net.cancel_commit <salt.modules.napalm_network.cancel_commit>`\n execution function, using the commit ID returned by this function.\n\n .. warning::\n Using this feature, Salt will load the exact configuration you\n expect, however the diff may change in time (i.e., if an user\n applies a manual configuration change, or a different process or\n command changes the configuration in the meanwhile).\n\n .. versionadded:: 2019.2.0\n\n revert_in: ``None``\n Commit and revert the changes in a specific number of minutes / hours.\n Example of accepted formats: ``5`` (revert in 5 minutes), ``2m`` (revert\n in 2 minutes), ``1h`` (revert the changes in 1 hour)`, ``5h30m`` (revert\n the changes in 5 hours and 30 minutes).\n\n .. note::\n To confirm the commit, and prevent reverting the changes, you will\n have to execute the\n :mod:`net.confirm_commit <salt.modules.napalm_network.confirm_commit>`\n function, using the commit ID returned by this function.\n\n .. warning::\n This works on any platform, regardless if they have or don't have\n native capabilities to confirming a commit. However, please be\n *very* cautious when using this feature: on Junos (as it is the only\n NAPALM core platform supporting this natively) it executes a commit\n confirmed as you would do from the command line.\n All the other platforms don't have this capability natively,\n therefore the revert is done via Salt. That means, your device needs\n to be reachable at the moment when Salt will attempt to revert your\n changes. Be cautious when pushing configuration changes that would\n prevent you reach the device.\n\n Similarly, if an user or a different process apply other\n configuration changes in the meanwhile (between the moment you\n commit and till the changes are reverted), these changes would be\n equally reverted, as Salt cannot be aware of them.\n\n .. versionadded:: 2019.2.0\n\n revert_at: ``None``\n Commit and revert the changes at a specific time. Example of accepted\n formats: ``1am`` (will commit and revert the changes at the next 1AM),\n ``13:20`` (will commit and revert at 13:20), ``1:20am``, etc.\n\n .. note::\n To confirm the commit, and prevent reverting the changes, you will\n have to execute the\n :mod:`net.confirm_commit <salt.modules.napalm_network.confirm_commit>`\n function, using the commit ID returned by this function.\n\n .. warning::\n This works on any platform, regardless if they have or don't have\n native capabilities to confirming a commit. However, please be\n *very* cautious when using this feature: on Junos (as it is the only\n NAPALM core platform supporting this natively) it executes a commit\n confirmed as you would do from the command line.\n All the other platforms don't have this capability natively,\n therefore the revert is done via Salt. That means, your device needs\n to be reachable at the moment when Salt will attempt to revert your\n changes. Be cautious when pushing configuration changes that would\n prevent you reach the device.\n\n Similarly, if an user or a different process apply other\n configuration changes in the meanwhile (between the moment you\n commit and till the changes are reverted), these changes would be\n equally reverted, as Salt cannot be aware of them.\n\n .. versionadded:: 2019.2.0\n\n replace: False\n Load and replace the configuration. Default: ``False`` (will apply load merge).\n\n context: None\n Overrides default context variables passed to the template.\n\n .. versionadded:: 2019.2.0\n\n defaults: None\n Default variables/context passed to the template.\n\n template_vars\n Dictionary with the arguments/context to be used when the template is rendered. Do not explicitly specify this\n argument. This represents any other variable that will be sent to the template rendering system. Please\n see an example below! In both ``ntp_peers_example_using_pillar`` and ``ntp_peers_example``, ``peers`` is sent as\n template variable.\n\n .. note::\n It is more recommended to use the ``context`` argument instead, to\n avoid any conflicts with other arguments.\n\n SLS Example (e.g.: under salt://router/config.sls) :\n\n .. code-block:: yaml\n\n whole_config_example:\n netconfig.managed:\n - template_name: salt://path/to/complete_config.jinja\n - debug: True\n - replace: True\n bgp_config_example:\n netconfig.managed:\n - template_name: /absolute/path/to/bgp_neighbors.mako\n - template_engine: mako\n prefix_lists_example:\n netconfig.managed:\n - template_name: prefix_lists.cheetah\n - debug: True\n - template_engine: cheetah\n ntp_peers_example:\n netconfig.managed:\n - template_name: http://bit.ly/2gKOj20\n - skip_verify: False\n - debug: True\n - peers:\n - 192.168.0.1\n - 192.168.0.1\n ntp_peers_example_using_pillar:\n netconfig.managed:\n - template_name: http://bit.ly/2gKOj20\n - peers: {{ pillar.get('ntp.peers', []) }}\n\n Multi template example:\n\n .. code-block:: yaml\n\n hostname_and_ntp:\n netconfig.managed:\n - template_name:\n - https://bit.ly/2OhSgqP\n - https://bit.ly/2M6C4Lx\n - https://bit.ly/2OIWVTs\n - debug: true\n - context:\n hostname: {{ opts.id }}\n servers:\n - 172.17.17.1\n - 172.17.17.2\n peers:\n - 192.168.0.1\n - 192.168.0.2\n\n Usage examples:\n\n .. code-block:: bash\n\n $ sudo salt 'juniper.device' state.sls router.config test=True\n\n $ sudo salt -N all-routers state.sls router.config pillar=\"{'debug': True}\"\n\n ``router.config`` depends on the location of the SLS file (see above). Running this command, will be executed all\n five steps from above. These examples above are not meant to be used in a production environment, their sole purpose\n is to provide usage examples.\n\n Output example:\n\n .. code-block:: bash\n\n $ sudo salt 'juniper.device' state.sls router.config test=True\n juniper.device:\n ----------\n ID: ntp_peers_example_using_pillar\n Function: netconfig.managed\n Result: None\n Comment: Testing mode: Configuration discarded.\n Started: 12:01:40.744535\n Duration: 8755.788 ms\n Changes:\n ----------\n diff:\n [edit system ntp]\n peer 192.168.0.1 { ... }\n + peer 172.17.17.1;\n + peer 172.17.17.3;\n\n Summary for juniper.device\n ------------\n Succeeded: 1 (unchanged=1, changed=1)\n Failed: 0\n ------------\n Total states run: 1\n Total run time: 8.756 s\n\n Raw output example (useful when the output is reused in other states/execution modules):\n\n .. code-block:: bash\n\n $ sudo salt --out=pprint 'juniper.device' state.sls router.config test=True debug=True\n\n .. code-block:: python\n\n {\n 'juniper.device': {\n 'netconfig_|-ntp_peers_example_using_pillar_|-ntp_peers_example_using_pillar_|-managed': {\n '__id__': 'ntp_peers_example_using_pillar',\n '__run_num__': 0,\n 'already_configured': False,\n 'changes': {\n 'diff': '[edit system ntp] peer 192.168.0.1 { ... }+ peer 172.17.17.1;+ peer 172.17.17.3;'\n },\n 'comment': 'Testing mode: Configuration discarded.',\n 'duration': 7400.759,\n 'loaded_config': 'system { ntp { peer 172.17.17.1; peer 172.17.17.3; } }',\n 'name': 'ntp_peers_example_using_pillar',\n 'result': None,\n 'start_time': '12:09:09.811445'\n }\n }\n }\n '''\n ret = salt.utils.napalm.default_ret(name)\n\n # the user can override the flags the equivalent CLI args\n # which have higher precedence\n test = __salt__['config.merge']('test', test)\n debug = __salt__['config.merge']('debug', debug)\n commit = __salt__['config.merge']('commit', commit)\n replace = __salt__['config.merge']('replace', replace) # this might be a bit risky\n skip_verify = __salt__['config.merge']('skip_verify', skip_verify)\n commit_in = __salt__['config.merge']('commit_in', commit_in)\n commit_at = __salt__['config.merge']('commit_at', commit_at)\n revert_in = __salt__['config.merge']('revert_in', revert_in)\n revert_at = __salt__['config.merge']('revert_at', revert_at)\n\n config_update_ret = _update_config(template_name=template_name,\n template_source=template_source,\n template_hash=template_hash,\n template_hash_name=template_hash_name,\n saltenv=saltenv,\n template_engine=template_engine,\n skip_verify=skip_verify,\n context=context,\n defaults=defaults,\n test=test,\n commit=commit,\n commit_in=commit_in,\n commit_at=commit_at,\n revert_in=revert_in,\n revert_at=revert_at,\n debug=debug,\n replace=replace,\n **template_vars)\n\n return salt.utils.napalm.loaded_ret(ret, config_update_ret, test, debug)", "response": "This state is used to manage the configuration of a single candidate on a network device."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef commit_cancelled(name):\n '''\n .. versionadded:: 2019.2.0\n\n Cancel a commit scheduled to be executed via the ``commit_in`` and\n ``commit_at`` arguments from the\n :py:func:`net.load_template <salt.modules.napalm_network.load_template>` or\n :py:func:`net.load_config <salt.modules.napalm_network.load_config>`\n execution functions. The commit ID is displayed when the commit is scheduled\n via the functions named above.\n\n State SLS Example:\n\n .. code-block:: yaml\n\n '20180726083540640360':\n netconfig.commit_cancelled\n '''\n cancelled = {\n 'name': name,\n 'result': None,\n 'changes': {},\n 'comment': ''\n }\n if __opts__['test']:\n cancelled['comment'] = 'It would cancel commit #{}'.format(name)\n return cancelled\n ret = __salt__['net.cancel_commit'](name)\n cancelled.update(ret)\n return cancelled", "response": "Cancel a scheduled commit on the specified node"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nconfirms a commit of a node", "response": "def commit_confirmed(name):\n '''\n .. versionadded:: 2019.2.0\n\n Confirm a commit scheduled to be reverted via the ``revert_in`` and\n ``revert_at`` arguments from the\n :mod:`net.load_template <salt.modules.napalm_network.load_template>` or\n :mod:`net.load_config <salt.modules.napalm_network.load_config>`\n execution functions. The commit ID is displayed when the commit confirmed\n is scheduled via the functions named above.\n\n State SLS Example:\n\n .. code-block:: yaml\n\n '20180726083540640360':\n netconfig.commit_confirmed\n '''\n confirmed = {\n 'name': name,\n 'result': None,\n 'changes': {},\n 'comment': ''\n }\n if __opts__['test']:\n confirmed['comment'] = 'It would confirm commit #{}'.format(name)\n return confirmed\n ret = __salt__['net.confirm_commit'](name)\n confirmed.update(ret)\n return confirmed"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nserialize Python data to a Python string representation", "response": "def serialize(obj, **options):\n '''\n Serialize Python data to a Python string representation (via pprint.format)\n\n :param obj: the data structure to serialize\n :param options: options given to pprint.format\n '''\n\n #round-trip this through JSON to avoid OrderedDict types\n # there's probably a more performant way to do this...\n # TODO remove json round-trip when all dataset will use\n # serializers\n return pprint.pformat(\n salt.utils.json.loads(\n salt.utils.json.dumps(obj, _json_module=_json),\n _json_module=_json\n ),\n **options\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ninitializes the server connection", "response": "def init(opts):\n '''\n Required.\n Can be used to initialize the server connection.\n '''\n try:\n DETAILS['server'] = SSHConnection(host=__opts__['proxy']['host'],\n username=__opts__['proxy']['username'],\n password=__opts__['proxy']['password'])\n out, err = DETAILS['server'].sendline('help')\n DETAILS['initialized'] = True\n\n except TerminalException as e:\n log.error(e)\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the grains from the proxied device", "response": "def grains():\n '''\n Get the grains from the proxied device\n '''\n\n if not DETAILS.get('grains_cache', {}):\n cmd = 'info'\n\n # Send the command to execute\n out, err = DETAILS['server'].sendline(cmd)\n\n # \"scrape\" the output and return the right fields as a dict\n DETAILS['grains_cache'] = parse(out)\n\n return DETAILS['grains_cache']"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef ping():\n '''\n Required.\n Ping the device on the other end of the connection\n '''\n try:\n out, err = DETAILS['server'].sendline('help')\n return True\n except TerminalException as e:\n log.error(e)\n return False", "response": "Ping the device on the other end of the connection"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse(out):\n '''\n Extract json from out.\n\n Parameter\n out: Type string. The data returned by the\n ssh command.\n '''\n jsonret = []\n in_json = False\n for ln_ in out.split('\\n'):\n if '{' in ln_:\n in_json = True\n if in_json:\n jsonret.append(ln_)\n if '}' in ln_:\n in_json = False\n return salt.utils.json.loads('\\n'.join(jsonret))", "response": "Extract json from out.\n Parameter\nAttributeNames out Type string. The data returned by the the\nAttributeNames ssh command."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef package_install(name, **kwargs):\n '''\n Install a \"package\" on the ssh server\n '''\n cmd = 'pkg_install ' + name\n if kwargs.get('version', False):\n cmd += ' ' + kwargs['version']\n\n # Send the command to execute\n out, err = DETAILS['server'].sendline(cmd)\n\n # \"scrape\" the output and return the right fields as a dict\n return parse(out)", "response": "Install a package on the ssh server"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove a package on the ssh server", "response": "def package_remove(name):\n '''\n Remove a \"package\" on the ssh server\n '''\n cmd = 'pkg_remove ' + name\n\n # Send the command to execute\n out, err = DETAILS['server'].sendline(cmd)\n\n # \"scrape\" the output and return the right fields as a dict\n return parse(out)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef service_start(name):\n '''\n Start a \"service\" on the ssh server\n\n .. versionadded:: 2015.8.2\n '''\n cmd = 'start ' + name\n\n # Send the command to execute\n out, err = DETAILS['server'].sendline(cmd)\n\n # \"scrape\" the output and return the right fields as a dict\n return parse(out)", "response": "Start a service on the ssh server"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef service_stop(name):\n '''\n Stop a \"service\" on the ssh server\n\n .. versionadded:: 2015.8.2\n '''\n cmd = 'stop ' + name\n\n # Send the command to execute\n out, err = DETAILS['server'].sendline(cmd)\n\n # \"scrape\" the output and return the right fields as a dict\n return parse(out)", "response": "Stop a service on the ssh server and return the output as a dict"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef service_restart(name):\n '''\n Restart a \"service\" on the ssh server\n\n .. versionadded:: 2015.8.2\n '''\n cmd = 'restart ' + name\n\n # Send the command to execute\n out, err = DETAILS['server'].sendline(cmd)\n\n # \"scrape\" the output and return the right fields as a dict\n return parse(out)", "response": "Restart a service on the ssh server"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef run_command(jboss_config, command, fail_on_error=True):\n '''\n Execute a command against jboss instance through the CLI interface.\n\n jboss_config\n Configuration dictionary with properties specified above.\n command\n Command to execute against jboss instance\n fail_on_error (default=True)\n Is true, raise CommandExecutionError exception if execution fails.\n If false, 'success' property of the returned dictionary is set to False\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7_cli.run_command '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}' my_command\n '''\n cli_command_result = __call_cli(jboss_config, command)\n\n if cli_command_result['retcode'] == 0:\n cli_command_result['success'] = True\n else:\n if fail_on_error:\n raise CommandExecutionError('''Command execution failed, return code={retcode}, stdout='{stdout}', stderr='{stderr}' '''.format(**cli_command_result))\n else:\n cli_command_result['success'] = False\n\n return cli_command_result", "response": "Execute a command against the jboss instance."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef run_operation(jboss_config, operation, fail_on_error=True, retries=1):\n '''\n Execute an operation against jboss instance through the CLI interface.\n\n jboss_config\n Configuration dictionary with properties specified above.\n operation\n An operation to execute against jboss instance\n\n fail_on_error (default=True)\n Is true, raise CommandExecutionError exception if execution fails.\n If false, 'success' property of the returned dictionary is set to False\n retries:\n Number of retries in case of \"JBAS012144: Could not connect to remote\" error.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' jboss7_cli.run_operation '{\"cli_path\": \"integration.modules.sysmod.SysModuleTest.test_valid_docs\", \"controller\": \"10.11.12.13:9999\", \"cli_user\": \"jbossadm\", \"cli_password\": \"jbossadm\"}' my_operation\n '''\n cli_command_result = __call_cli(jboss_config, operation, retries)\n\n if cli_command_result['retcode'] == 0:\n if _is_cli_output(cli_command_result['stdout']):\n cli_result = _parse(cli_command_result['stdout'])\n cli_result['success'] = cli_result['outcome'] == 'success'\n else:\n raise CommandExecutionError('Operation has returned unparseable output: {0}'.format(cli_command_result['stdout']))\n else:\n if _is_cli_output(cli_command_result['stdout']):\n cli_result = _parse(cli_command_result['stdout'])\n cli_result['success'] = False\n match = re.search(r'^(JBAS\\d+):', cli_result['failure-description'])\n cli_result['err_code'] = match.group(1)\n cli_result['stdout'] = cli_command_result['stdout']\n else:\n if fail_on_error:\n raise CommandExecutionError('''Command execution failed, return code={retcode}, stdout='{stdout}', stderr='{stderr}' '''.format(**cli_command_result))\n else:\n cli_result = {\n 'success': False,\n 'stdout': cli_command_result['stdout'],\n 'stderr': cli_command_result['stderr'],\n 'retcode': cli_command_result['retcode']\n }\n return cli_result", "response": "Execute an operation against the jboss instance."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _delete_resource(name, name_param, desc, res_type, wait=0, status_param=None,\n status_gone='deleted', region=None, key=None, keyid=None, profile=None,\n **args):\n '''\n Delete a generic Elasticache resource.\n '''\n try:\n wait = int(wait)\n except Exception:\n raise SaltInvocationError(\"Bad value ('{0}') passed for 'wait' param - must be an \"\n \"int or boolean.\".format(wait))\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if name_param in args:\n log.info(\n \"'name: %s' param being overridden by explicitly provided '%s: %s'\",\n name, name_param, args[name_param]\n )\n name = args[name_param]\n else:\n args[name_param] = name\n args = dict([(k, v) for k, v in args.items() if not k.startswith('_')])\n try:\n func = 'delete_'+res_type\n f = getattr(conn, func)\n if wait:\n func = 'describe_'+res_type+'s'\n s = globals()[func]\n except (AttributeError, KeyError) as e:\n raise SaltInvocationError(\"No function '{0}()' found: {1}\".format(func, e.message))\n try:\n\n f(**args)\n if not wait:\n log.info('%s %s deletion requested.', desc.title(), name)\n return True\n log.info('Waiting up to %s seconds for %s %s to be deleted.', wait, desc, name)\n orig_wait = wait\n while wait > 0:\n r = s(name=name, conn=conn)\n if not r or r[0].get(status_param) == status_gone:\n log.info('%s %s deleted.', desc.title(), name)\n return True\n sleep = wait if wait % 60 == wait else 60\n log.info('Sleeping %s seconds for %s %s to be deleted.',\n sleep, desc, name)\n time.sleep(sleep)\n wait -= sleep\n log.error('%s %s not deleted after %s seconds!', desc.title(), name, orig_wait)\n\n return False\n except botocore.exceptions.ClientError as e:\n log.error('Failed to delete %s %s: %s', desc, name, e)\n return False", "response": "Delete a generic Elasticache resource."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nreturns details about all Elasticache cache clusters.", "response": "def describe_cache_clusters(name=None, conn=None, region=None, key=None,\n keyid=None, profile=None, **args):\n '''\n Return details about all (or just one) Elasticache cache clusters.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.describe_cache_clusters\n salt myminion boto3_elasticache.describe_cache_clusters myelasticache\n '''\n return _describe_resource(name=name, name_param='CacheClusterId', res_type='cache_cluster',\n info_node='CacheClusters', conn=conn, region=region, key=key,\n keyid=keyid, profile=profile, **args)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef cache_cluster_exists(name, conn=None, region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if a cache cluster exists.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.cache_cluster_exists myelasticache\n '''\n return bool(describe_cache_clusters(name=name, conn=conn, region=region, key=key, keyid=keyid, profile=profile))", "response": "Check to see if a cache cluster exists."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_cache_cluster(name, wait=600, security_groups=None,\n region=None, key=None, keyid=None, profile=None, **args):\n '''\n Create a cache cluster.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.create_cache_cluster name=myCacheCluster \\\n Engine=redis \\\n CacheNodeType=cache.t2.micro \\\n NumCacheNodes=1 \\\n SecurityGroupIds='[sg-11223344]' \\\n CacheSubnetGroupName=myCacheSubnetGroup\n '''\n if security_groups:\n if not isinstance(security_groups, list):\n security_groups = [security_groups]\n sgs = __salt__['boto_secgroup.convert_to_group_ids'](groups=security_groups, region=region,\n key=key, keyid=keyid, profile=profile)\n if 'SecurityGroupIds' not in args:\n args['SecurityGroupIds'] = []\n args['SecurityGroupIds'] += sgs\n args = dict([(k, v) for k, v in args.items() if not k.startswith('_')])\n return _create_resource(name, name_param='CacheClusterId', desc='cache cluster',\n res_type='cache_cluster', wait=wait, status_param='CacheClusterStatus',\n region=region, key=key, keyid=keyid, profile=profile, **args)", "response": "Create a cache cluster."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking to see if a replication group exists.", "response": "def replication_group_exists(name, region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if a replication group exists.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.replication_group_exists myelasticache\n '''\n return bool(describe_replication_groups(name=name, region=region, key=key, keyid=keyid,\n profile=profile))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ndeleting an ElastiCache replication group optionally taking a snapshot first.", "response": "def delete_replication_group(name, wait=600, region=None, key=None, keyid=None, profile=None, **args):\n '''\n Delete an ElastiCache replication group, optionally taking a snapshot first.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.delete_replication_group my-replication-group\n '''\n return _delete_resource(name, name_param='ReplicationGroupId', desc='replication group',\n res_type='replication_group', wait=wait, status_param='Status',\n region=region, key=key, keyid=keyid, profile=profile, **args)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nchecking to see if an ElastiCache subnet group exists.", "response": "def cache_subnet_group_exists(name, region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if an ElastiCache subnet group exists.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.cache_subnet_group_exists my-subnet-group\n '''\n return bool(describe_cache_subnet_groups(name=name, region=region, key=key, keyid=keyid, profile=profile))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_cache_subnet_groups(region=None, key=None, keyid=None, profile=None):\n '''\n Return a list of all cache subnet group names\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.list_cache_subnet_groups region=us-east-1\n '''\n return [g['CacheSubnetGroupName'] for g in\n describe_cache_subnet_groups(None, region, key, keyid, profile)]", "response": "Return a list of all cache subnet groups in a region"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate an ElastiCache subnet group.", "response": "def create_cache_subnet_group(name, subnets=None, region=None, key=None, keyid=None, profile=None, **args):\n '''\n Create an ElastiCache subnet group\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.create_cache_subnet_group name=my-subnet-group \\\n CacheSubnetGroupDescription=\"description\" \\\n subnets='[myVPCSubnet1,myVPCSubnet2]'\n '''\n if subnets:\n if 'SubnetIds' not in args:\n args['SubnetIds'] = []\n if not isinstance(subnets, list):\n subnets = [subnets]\n for subnet in subnets:\n if subnet.startswith('subnet-'):\n # Moderately safe assumption... :) Will be caught further down if incorrect.\n args['SubnetIds'] += [subnet]\n continue\n sn = __salt__['boto_vpc.describe_subnets'](subnet_names=subnet, region=region, key=key,\n keyid=keyid, profile=profile).get('subnets')\n if not sn:\n raise SaltInvocationError(\n 'Could not resolve Subnet Name {0} to an ID.'.format(subnet))\n if len(sn) == 1:\n args['SubnetIds'] += [sn[0]['id']]\n elif len(sn) > 1:\n raise CommandExecutionError(\n 'Subnet Name {0} returned more than one ID.'.format(subnet))\n args = dict([(k, v) for k, v in args.items() if not k.startswith('_')])\n return _create_resource(name, name_param='CacheSubnetGroupName', desc='cache subnet group',\n res_type='cache_subnet_group',\n region=region, key=key, keyid=keyid, profile=profile, **args)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef cache_security_group_exists(name, region=None, key=None, keyid=None, profile=None):\n '''\n Check to see if an ElastiCache security group exists.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.cache_security_group_exists mysecuritygroup\n '''\n return bool(describe_cache_security_groups(name=name, region=region, key=key, keyid=keyid,\n profile=profile))", "response": "Check to see if an ElastiCache security group exists."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a cache security group.", "response": "def create_cache_security_group(name, region=None, key=None, keyid=None, profile=None, **args):\n '''\n Create a cache security group.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.create_cache_security_group mycachesecgrp Description='My Cache Security Group'\n '''\n return _create_resource(name, name_param='CacheSecurityGroupName', desc='cache security group',\n res_type='cache_security_group',\n region=region, key=key, keyid=keyid, profile=profile, **args)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndeletes a cache security group.", "response": "def delete_cache_security_group(name, region=None, key=None, keyid=None, profile=None, **args):\n '''\n Delete a cache security group.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.delete_cache_security_group myelasticachesg\n '''\n return _delete_resource(name, name_param='CacheSecurityGroupName',\n desc='cache security group', res_type='cache_security_group',\n region=region, key=key, keyid=keyid, profile=profile, **args)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nauthorizes network ingress from an ec2 security group to a cache security group.", "response": "def authorize_cache_security_group_ingress(name, region=None, key=None, keyid=None, profile=None, **args):\n '''\n Authorize network ingress from an ec2 security group to a cache security group.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.authorize_cache_security_group_ingress \\\n mycachesecgrp \\\n EC2SecurityGroupName=someEC2sg \\\n EC2SecurityGroupOwnerId=SOMEOWNERID\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if 'CacheSecurityGroupName' in args:\n log.info(\n \"'name: %s' param being overridden by explicitly provided \"\n \"'CacheSecurityGroupName: %s'\",\n name, args['CacheSecurityGroupName']\n )\n name = args['CacheSecurityGroupName']\n else:\n args['CacheSubnetGroupName'] = name\n args = dict([(k, v) for k, v in args.items() if not k.startswith('_')])\n try:\n conn.authorize_cache_security_group_ingress(**args)\n log.info('Authorized %s to cache security group %s.',\n args['EC2SecurityGroupName'], name)\n return True\n except botocore.exceptions.ClientError as e:\n log.error('Failed to update security group %s: %s', name, e)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef describe_cache_parameter_groups(name=None, conn=None, region=None, key=None, keyid=None,\n profile=None):\n '''\n Return details about all (or just one) Elasticache cache clusters.\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.describe_cache_parameter_groups\n salt myminion boto3_elasticache.describe_cache_parameter_groups myParameterGroup\n '''\n return _describe_resource(name=name, name_param='CacheParameterGroupName',\n res_type='cache_parameter_group', info_node='CacheParameterGroups',\n conn=conn, region=region, key=key, keyid=keyid, profile=profile)", "response": "Return details about all Elasticache cache parameter groups."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef describe_cache_parameters(name=None, conn=None, region=None, key=None, keyid=None,\n profile=None, **args):\n '''\n Returns the detailed parameter list for a particular cache parameter group.\n\n name\n The name of a specific cache parameter group to return details for.\n\n CacheParameterGroupName\n The name of a specific cache parameter group to return details for. Generally not\n required, as `name` will be used if not provided.\n\n Source\n Optionally, limit the parameter types to return.\n Valid values:\n - user\n - system\n - engine-default\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.describe_cache_parameters name=myParamGroup Source=user\n '''\n ret = {}\n generic = _describe_resource(name=name, name_param='CacheParameterGroupName',\n res_type='cache_parameter', info_node='Parameters',\n conn=conn, region=region, key=key, keyid=keyid, profile=profile,\n **args)\n specific = _describe_resource(name=name, name_param='CacheParameterGroupName',\n res_type='cache_parameter',\n info_node='CacheNodeTypeSpecificParameters', conn=conn,\n region=region, key=key, keyid=keyid, profile=profile, **args)\n ret.update({'Parameters': generic}) if generic else None\n ret.update({'CacheNodeTypeSpecificParameters': specific}) if specific else None\n return ret", "response": "Returns the detailed parameter list for a specific cache parameter group."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef modify_cache_parameter_group(name, region=None, key=None, keyid=None, profile=None,\n **args):\n '''\n Update a cache parameter group in place.\n\n Note that due to a design limitation in AWS, this function is not atomic -- a maximum of 20\n params may be modified in one underlying boto call. This means that if more than 20 params\n need to be changed, the update is performed in blocks of 20, which in turns means that if a\n later sub-call fails after an earlier one has succeeded, the overall update will be left\n partially applied.\n\n CacheParameterGroupName\n The name of the cache parameter group to modify.\n\n ParameterNameValues\n A [list] of {dicts}, each composed of a parameter name and a value, for the parameter\n update. At least one parameter/value pair is required.\n\n .. code-block:: yaml\n\n ParameterNameValues:\n - ParameterName: timeout\n # Amazon requires ALL VALUES to be strings...\n ParameterValue: \"30\"\n - ParameterName: appendonly\n # The YAML parser will turn a bare `yes` into a bool, which Amazon will then throw on...\n ParameterValue: \"yes\"\n\n Example:\n\n .. code-block:: bash\n\n salt myminion boto3_elasticache.modify_cache_parameter_group \\\n CacheParameterGroupName=myParamGroup \\\n ParameterNameValues='[ { ParameterName: timeout,\n ParameterValue: \"30\" },\n { ParameterName: appendonly,\n ParameterValue: \"yes\" } ]'\n '''\n args = dict([(k, v) for k, v in args.items() if not k.startswith('_')])\n try:\n Params = args['ParameterNameValues']\n except ValueError as e:\n raise SaltInvocationError('Invalid `ParameterNameValues` structure passed.')\n while Params:\n args.update({'ParameterNameValues': Params[:20]})\n Params = Params[20:]\n if not _modify_resource(name, name_param='CacheParameterGroupName',\n desc='cache parameter group', res_type='cache_parameter_group',\n region=region, key=key, keyid=keyid, profile=profile, **args):\n return False\n return True", "response": "Modify a cache parameter group in place."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nchecking if portname is valid and whether or not the directory exists in the availabe ports tree.", "response": "def _check_portname(name):\n '''\n Check if portname is valid and whether or not the directory exists in the\n ports tree.\n '''\n if not isinstance(name, string_types) or '/' not in name:\n raise SaltInvocationError(\n 'Invalid port name \\'{0}\\' (category required)'.format(name)\n )\n\n path = os.path.join('/usr/ports', name)\n if not os.path.isdir(path):\n raise SaltInvocationError('Path \\'{0}\\' does not exist'.format(path))\n\n return path"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nretrieve the path to the dir containing OPTIONS file for a given port", "response": "def _options_dir(name):\n '''\n Retrieve the path to the dir containing OPTIONS file for a given port\n '''\n _check_portname(name)\n _root = '/var/db/ports'\n\n # New path: /var/db/ports/category_portname\n new_dir = os.path.join(_root, name.replace('/', '_'))\n # Old path: /var/db/ports/portname\n old_dir = os.path.join(_root, name.split('/')[-1])\n\n if os.path.isdir(old_dir):\n return old_dir\n return new_dir"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _options_file_exists(name):\n '''\n Returns True/False based on whether or not the options file for the\n specified port exists.\n '''\n return os.path.isfile(os.path.join(_options_dir(name), 'options'))", "response": "Returns True if the options file for the specified port exists."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _write_options(name, configuration):\n '''\n Writes a new OPTIONS file\n '''\n _check_portname(name)\n\n pkg = next(iter(configuration))\n conf_ptr = configuration[pkg]\n\n dirname = _options_dir(name)\n if not os.path.isdir(dirname):\n try:\n os.makedirs(dirname)\n except OSError as exc:\n raise CommandExecutionError(\n 'Unable to make {0}: {1}'.format(dirname, exc)\n )\n\n with salt.utils.files.fopen(os.path.join(dirname, 'options'), 'w') as fp_:\n sorted_options = list(conf_ptr)\n sorted_options.sort()\n fp_.write(salt.utils.stringutils.to_str(\n '# This file was auto-generated by Salt (http://saltstack.com)\\n'\n '# Options for {0}\\n'\n '_OPTIONS_READ={0}\\n'\n '_FILE_COMPLETE_OPTIONS_LIST={1}\\n'\n .format(pkg, ' '.join(sorted_options))\n ))\n opt_tmpl = 'OPTIONS_FILE_{0}SET+={1}\\n'\n for opt in sorted_options:\n fp_.write(salt.utils.stringutils.to_str(\n opt_tmpl.format(\n '' if conf_ptr[opt] == 'on' else 'UN',\n opt\n )\n ))", "response": "Writes a new OPTIONS file for the port with the given name and configuration."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nnormalize the on or off values to be used in writing the options file", "response": "def _normalize(val):\n '''\n Fix Salt's yaml-ification of on/off, and otherwise normalize the on/off\n values to be used in writing the options file\n '''\n if isinstance(val, bool):\n return 'on' if val else 'off'\n return six.text_type(val).lower()"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef install(name, clean=True):\n '''\n Install a port from the ports tree. Installs using ``BATCH=yes`` for\n non-interactive building. To set config options for a given port, use\n :mod:`ports.config <salt.modules.freebsdports.config>`.\n\n clean : True\n If ``True``, cleans after installation. Equivalent to running ``make\n install clean BATCH=yes``.\n\n .. note::\n\n It may be helpful to run this function using the ``-t`` option to set a\n higher timeout, since compiling a port may cause the Salt command to\n exceed the default timeout.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt -t 1200 '*' ports.install security/nmap\n '''\n portpath = _check_portname(name)\n old = __salt__['pkg.list_pkgs']()\n if old.get(name.rsplit('/')[-1]):\n deinstall(name)\n cmd = ['make', 'install']\n if clean:\n cmd.append('clean')\n cmd.append('BATCH=yes')\n result = __salt__['cmd.run_all'](\n cmd,\n cwd=portpath,\n reset_system_locale=False,\n python_shell=False\n )\n if result['retcode'] != 0:\n __context__['ports.install_error'] = result['stderr']\n __context__.pop('pkg.list_pkgs', None)\n new = __salt__['pkg.list_pkgs']()\n ret = salt.utils.data.compare_dicts(old, new)\n if not ret and result['retcode'] == 0:\n # No change in package list, but the make install was successful.\n # Assume that the installation was a recompile with new options, and\n # set return dict so that changes are detected by the ports.installed\n # state.\n ret = {name: {'old': old.get(name, ''),\n 'new': new.get(name, '')}}\n return ret", "response": "Install a port from the ports tree."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef rmconfig(name):\n '''\n Clear the cached options for the specified port; run a ``make rmconfig``\n\n name\n The name of the port to clear\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' ports.rmconfig security/nmap\n '''\n portpath = _check_portname(name)\n return __salt__['cmd.run'](\n ['make', 'rmconfig'],\n cwd=portpath,\n python_shell=False\n )", "response": "Clears the cached options for the specified port ; run a make rmconfig on the specified port"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nshow the configuration options for a given port.", "response": "def showconfig(name, default=False, dict_return=False):\n '''\n Show the configuration options for a given port.\n\n default : False\n Show the default options for a port (not necessarily the same as the\n current configuration)\n\n dict_return : False\n Instead of returning the output of ``make showconfig``, return the data\n in an dictionary\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' ports.showconfig security/nmap\n salt '*' ports.showconfig security/nmap default=True\n '''\n portpath = _check_portname(name)\n\n if default and _options_file_exists(name):\n saved_config = showconfig(name, default=False, dict_return=True)\n rmconfig(name)\n if _options_file_exists(name):\n raise CommandExecutionError('Unable to get default configuration')\n default_config = showconfig(name, default=False,\n dict_return=dict_return)\n _write_options(name, saved_config)\n return default_config\n\n try:\n result = __salt__['cmd.run_all'](\n ['make', 'showconfig'],\n cwd=portpath,\n python_shell=False\n )\n output = result['stdout'].splitlines()\n if result['retcode'] != 0:\n error = result['stderr']\n else:\n error = ''\n except TypeError:\n error = result\n\n if error:\n msg = ('Error running \\'make showconfig\\' for {0}: {1}'\n .format(name, error))\n log.error(msg)\n raise SaltInvocationError(msg)\n\n if not dict_return:\n return '\\n'.join(output)\n\n if (not output) or ('configuration options' not in output[0]):\n return {}\n\n try:\n pkg = output[0].split()[-1].rstrip(':')\n except (IndexError, AttributeError, TypeError) as exc:\n log.error('Unable to get pkg-version string: %s', exc)\n return {}\n\n ret = {pkg: {}}\n output = output[1:]\n for line in output:\n try:\n opt, val, desc = re.match(\n r'\\s+([^=]+)=(off|on): (.+)', line\n ).groups()\n except AttributeError:\n continue\n ret[pkg][opt] = val\n\n if not ret[pkg]:\n return {}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nmodifies the configuration options for a given port. Multiple options can be specified.", "response": "def config(name, reset=False, **kwargs):\n '''\n Modify configuration options for a given port. Multiple options can be\n specified. To see the available options for a port, use\n :mod:`ports.showconfig <salt.modules.freebsdports.showconfig>`.\n\n name\n The port name, in ``category/name`` format\n\n reset : False\n If ``True``, runs a ``make rmconfig`` for the port, clearing its\n configuration before setting the desired options\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' ports.config security/nmap IPV6=off\n '''\n portpath = _check_portname(name)\n\n if reset:\n rmconfig(name)\n\n configuration = showconfig(name, dict_return=True)\n\n if not configuration:\n raise CommandExecutionError(\n 'Unable to get port configuration for \\'{0}\\''.format(name)\n )\n\n # Get top-level key for later reference\n pkg = next(iter(configuration))\n conf_ptr = configuration[pkg]\n\n opts = dict(\n (six.text_type(x), _normalize(kwargs[x]))\n for x in kwargs\n if not x.startswith('_')\n )\n\n bad_opts = [x for x in opts if x not in conf_ptr]\n if bad_opts:\n raise SaltInvocationError(\n 'The following opts are not valid for port {0}: {1}'\n .format(name, ', '.join(bad_opts))\n )\n\n bad_vals = [\n '{0}={1}'.format(x, y) for x, y in six.iteritems(opts)\n if y not in ('on', 'off')\n ]\n if bad_vals:\n raise SaltInvocationError(\n 'The following key/value pairs are invalid: {0}'\n .format(', '.join(bad_vals))\n )\n\n conf_ptr.update(opts)\n _write_options(name, configuration)\n\n new_config = showconfig(name, dict_return=True)\n try:\n new_config = new_config[next(iter(new_config))]\n except (StopIteration, TypeError):\n return False\n\n return all(conf_ptr[x] == new_config.get(x) for x in conf_ptr)"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nupdates the ports tree with the current state of the current node.", "response": "def update(extract=False):\n '''\n Update the ports tree\n\n extract : False\n If ``True``, runs a ``portsnap extract`` after fetching, should be used\n for first-time installation of the ports tree.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' ports.update\n '''\n result = __salt__['cmd.run_all'](\n _portsnap() + ['fetch'],\n python_shell=False\n )\n if not result['retcode'] == 0:\n raise CommandExecutionError(\n 'Unable to fetch ports snapshot: {0}'.format(result['stderr'])\n )\n\n ret = []\n try:\n patch_count = re.search(\n r'Fetching (\\d+) patches', result['stdout']\n ).group(1)\n except AttributeError:\n patch_count = 0\n\n try:\n new_port_count = re.search(\n r'Fetching (\\d+) new ports or files', result['stdout']\n ).group(1)\n except AttributeError:\n new_port_count = 0\n\n ret.append('Applied {0} new patches'.format(patch_count))\n ret.append('Fetched {0} new ports or files'.format(new_port_count))\n\n if extract:\n result = __salt__['cmd.run_all'](\n _portsnap() + ['extract'],\n python_shell=False\n )\n if not result['retcode'] == 0:\n raise CommandExecutionError(\n 'Unable to extract ports snapshot {0}'.format(result['stderr'])\n )\n\n result = __salt__['cmd.run_all'](\n _portsnap() + ['update'],\n python_shell=False\n )\n if not result['retcode'] == 0:\n raise CommandExecutionError(\n 'Unable to apply ports snapshot: {0}'.format(result['stderr'])\n )\n\n __context__.pop('ports.list_all', None)\n return '\\n'.join(ret)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_all():\n '''\n Lists all ports available.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' ports.list_all\n\n .. warning::\n\n Takes a while to run, and returns a **LOT** of output\n '''\n if 'ports.list_all' not in __context__:\n __context__['ports.list_all'] = []\n for path, dirs, files in salt.utils.path.os_walk('/usr/ports'):\n stripped = path[len('/usr/ports'):]\n if stripped.count('/') != 2 or stripped.endswith('/CVS'):\n continue\n __context__['ports.list_all'].append(stripped[1:])\n return __context__['ports.list_all']", "response": "Lists all ports available."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nsearch for matches in the ports tree.", "response": "def search(name):\n '''\n Search for matches in the ports tree. Globs are supported, and the category\n is optional\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' ports.search 'security/*'\n salt '*' ports.search 'security/n*'\n salt '*' ports.search nmap\n\n .. warning::\n\n Takes a while to run\n '''\n name = six.text_type(name)\n all_ports = list_all()\n if '/' in name:\n if name.count('/') > 1:\n raise SaltInvocationError(\n 'Invalid search string \\'{0}\\'. Port names cannot have more '\n 'than one slash'\n )\n else:\n return fnmatch.filter(all_ports, name)\n else:\n ret = []\n for port in all_ports:\n if fnmatch.fnmatch(port.rsplit('/')[-1], name):\n ret.append(port)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nconvert a list of cli_table objects to list of dictionaries.", "response": "def _clitable_to_dict(objects, fsm_handler):\n '''\n Converts TextFSM cli_table object to list of dictionaries.\n '''\n objs = []\n log.debug('Cli Table:')\n log.debug(objects)\n log.debug('FSM handler:')\n log.debug(fsm_handler)\n for row in objects:\n temp_dict = {}\n for index, element in enumerate(row):\n temp_dict[fsm_handler.header[index].lower()] = element\n objs.append(temp_dict)\n log.debug('Extraction result:')\n log.debug(objs)\n return objs"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreading the body from the request and returns it.", "response": "def read_body(environ):\n '''\n Pull the body from the request and return it\n '''\n length = environ.get('CONTENT_LENGTH', '0')\n length = 0 if length == '' else int(length)\n\n return environ['wsgi.input'].read(length)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_json(environ):\n '''\n Return the request body as JSON\n '''\n content_type = environ.get('CONTENT_TYPE', '')\n if content_type != 'application/json':\n raise HTTPError(406, 'JSON required')\n\n try:\n return salt.utils.json.loads(read_body(environ))\n except ValueError as exc:\n raise HTTPError(400, exc)", "response": "Return the request body as JSON"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ntake the response data and returns a tuple of headers suitable for passing to start_response", "response": "def get_headers(data, extra_headers=None):\n '''\n Takes the response data as well as any additional headers and returns a\n tuple of tuples of headers suitable for passing to start_response()\n '''\n response_headers = {\n 'Content-Length': str(len(data)),\n }\n\n if extra_headers:\n response_headers.update(extra_headers)\n\n return list(response_headers.items())"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nyields the lowstate data from the client", "response": "def run_chunk(environ, lowstate):\n '''\n Expects a list of lowstate dictionaries that are executed and returned in\n order\n '''\n client = environ['SALT_APIClient']\n\n for chunk in lowstate:\n yield client.run(chunk)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndoes any path dispatching here and return a JSON - serializable data - suitable for the response", "response": "def dispatch(environ):\n '''\n Do any path/method dispatching here and return a JSON-serializable data\n structure appropriate for the response\n '''\n method = environ['REQUEST_METHOD'].upper()\n\n if method == 'GET':\n return (\"They found me. I don't know how, but they found me. \"\n \"Run for it, Marty!\")\n elif method == 'POST':\n data = get_json(environ)\n return run_chunk(environ, data)\n else:\n raise HTTPError(405, 'Method Not Allowed')"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nmakes Salt s opts dict and APIClient available in the WSGI environment", "response": "def saltenviron(environ):\n '''\n Make Salt's opts dict and the APIClient available in the WSGI environ\n '''\n if '__opts__' not in locals():\n import salt.config\n __opts__ = salt.config.client_config(\n os.environ.get('SALT_MASTER_CONFIG', '/etc/salt/master'))\n\n environ['SALT_OPTS'] = __opts__\n environ['SALT_APIClient'] = salt.netapi.NetapiClient(__opts__)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nprocesses the request and return a JSON response. Catch errors and return the appropriate HTTP code.", "response": "def application(environ, start_response):\n '''\n Process the request and return a JSON response. Catch errors and return the\n appropriate HTTP code.\n '''\n # Instantiate APIClient once for the whole app\n saltenviron(environ)\n\n # Call the dispatcher\n try:\n resp = list(dispatch(environ))\n code = 200\n except HTTPError as exc:\n code = exc.code\n resp = str(exc)\n except salt.exceptions.EauthAuthenticationError as exc:\n code = 401\n resp = str(exc)\n except Exception as exc:\n code = 500\n resp = str(exc)\n\n # Convert the response to JSON\n try:\n ret = salt.utils.json.dumps({'return': resp})\n except TypeError as exc:\n code = 500\n ret = str(exc) # future lint: disable=blacklisted-function\n\n # Return the response\n start_response(H[code], get_headers(ret, {\n 'Content-Type': 'application/json',\n }))\n return (ret,)"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef start():\n '''\n Start simple_server()\n '''\n from wsgiref.simple_server import make_server\n\n # When started outside of salt-api __opts__ will not be injected\n if '__opts__' not in globals():\n globals()['__opts__'] = get_opts()\n\n if __virtual__() is False:\n raise SystemExit(1)\n\n mod_opts = __opts__.get(__virtualname__, {})\n\n # pylint: disable=C0103\n httpd = make_server('localhost', mod_opts['port'], application)\n\n try:\n httpd.serve_forever()\n except KeyboardInterrupt:\n raise SystemExit(0)", "response": "Start the base server for the current salt - api"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef split(orig, sep=None):\n '''\n Generator function for iterating through large strings, particularly useful\n as a replacement for str.splitlines().\n\n See http://stackoverflow.com/a/3865367\n '''\n exp = re.compile(r'\\s+' if sep is None else re.escape(sep))\n pos = 0\n length = len(orig)\n while True:\n match = exp.search(orig, pos)\n if not match:\n if pos < length or sep is not None:\n val = orig[pos:]\n if val:\n # Only yield a value if the slice was not an empty string,\n # because if it is then we've reached the end. This keeps\n # us from yielding an extra blank value at the end.\n yield val\n break\n if pos < match.start() or sep is not None:\n yield orig[pos:match.start()]\n pos = match.end()", "response": "Generator function for iterating through large strings particularly useful\n as a replacement for str. splitlines().\n yields a list of strings each containing a single value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef read_file(fh_, chunk_size=1048576):\n '''\n Generator that reads chunk_size bytes at a time from a file/filehandle and\n yields it.\n '''\n try:\n if chunk_size != int(chunk_size):\n raise ValueError\n except ValueError:\n raise ValueError('chunk_size must be an integer')\n try:\n while True:\n try:\n chunk = fh_.read(chunk_size)\n except AttributeError:\n # Open the file and re-attempt the read\n fh_ = salt.utils.files.fopen(fh_, 'rb') # pylint: disable=W8470\n chunk = fh_.read(chunk_size)\n if not chunk:\n break\n yield chunk\n finally:\n try:\n fh_.close()\n except AttributeError:\n pass", "response": "Generator that reads chunk_size bytes at a time from a filehandle and yields it."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef fnmatch_multiple(candidates, pattern):\n '''\n Convenience function which runs fnmatch.fnmatch() on each element of passed\n iterable. The first matching candidate is returned, or None if there is no\n matching candidate.\n '''\n # Make sure that candidates is iterable to avoid a TypeError when we try to\n # iterate over its items.\n try:\n candidates_iter = iter(candidates)\n except TypeError:\n return None\n\n for candidate in candidates_iter:\n try:\n if fnmatch.fnmatch(candidate, pattern):\n return candidate\n except TypeError:\n pass\n return None", "response": "Convenience function which runs fnmatch. fnmatch on each element of passed\n iterable."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef translate_input(translator,\n skip_translate=None,\n ignore_collisions=False,\n validate_ip_addrs=True,\n **kwargs):\n '''\n Translate CLI/SLS input into the format the API expects. The ``translator``\n argument must be a module containing translation functions, within\n salt.utils.docker.translate. A ``skip_translate`` kwarg can be passed to\n control which arguments are translated. It can be either a comma-separated\n list or an iterable containing strings (e.g. a list or tuple), and members\n of that tuple will have their translation skipped. Optionally,\n skip_translate can be set to True to skip *all* translation.\n '''\n kwargs = copy.deepcopy(salt.utils.args.clean_kwargs(**kwargs))\n invalid = {}\n collisions = []\n\n if skip_translate is True:\n # Skip all translation\n return kwargs\n else:\n if not skip_translate:\n skip_translate = ()\n else:\n try:\n skip_translate = _split(skip_translate)\n except AttributeError:\n pass\n if not hasattr(skip_translate, '__iter__'):\n log.error('skip_translate is not an iterable, ignoring')\n skip_translate = ()\n\n try:\n # Using list(kwargs) here because if there are any invalid arguments we\n # will be popping them from the kwargs.\n for key in list(kwargs):\n real_key = translator.ALIASES.get(key, key)\n if real_key in skip_translate:\n continue\n\n # ipam_pools is designed to be passed as a list of actual\n # dictionaries, but if each of the dictionaries passed has a single\n # element, it will be incorrectly repacked.\n if key != 'ipam_pools' and salt.utils.data.is_dictlist(kwargs[key]):\n kwargs[key] = salt.utils.data.repack_dictlist(kwargs[key])\n\n try:\n kwargs[key] = getattr(translator, real_key)(\n kwargs[key],\n validate_ip_addrs=validate_ip_addrs,\n skip_translate=skip_translate)\n except AttributeError:\n log.debug('No translation function for argument \\'%s\\'', key)\n continue\n except SaltInvocationError as exc:\n kwargs.pop(key)\n invalid[key] = exc.strerror\n\n try:\n translator._merge_keys(kwargs)\n except AttributeError:\n pass\n\n # Convert CLI versions of commands to their docker-py counterparts\n for key in translator.ALIASES:\n if key in kwargs:\n new_key = translator.ALIASES[key]\n value = kwargs.pop(key)\n if new_key in kwargs:\n collisions.append(new_key)\n else:\n kwargs[new_key] = value\n\n try:\n translator._post_processing(kwargs, skip_translate, invalid)\n except AttributeError:\n pass\n\n except Exception as exc:\n error_message = exc.__str__()\n log.error(\n 'Error translating input: \\'%s\\'', error_message, exc_info=True)\n else:\n error_message = None\n\n error_data = {}\n if error_message is not None:\n error_data['error_message'] = error_message\n if invalid:\n error_data['invalid'] = invalid\n if collisions and not ignore_collisions:\n for item in collisions:\n error_data.setdefault('collisions', []).append(\n '\\'{0}\\' is an alias for \\'{1}\\', they cannot both be used'\n .format(translator.ALIASES_REVMAP[item], item)\n )\n if error_data:\n raise CommandExecutionError(\n 'Failed to translate input', info=error_data)\n\n return kwargs", "response": "Translate CLI input into the format that the API expects."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nbuilds an IPAM config dictionary for the given IPAM pools and options.", "response": "def create_ipam_config(*pools, **kwargs):\n '''\n Builds an IP address management (IPAM) config dictionary\n '''\n kwargs = salt.utils.args.clean_kwargs(**kwargs)\n\n try:\n # docker-py 2.0 and newer\n pool_args = salt.utils.args.get_function_argspec(\n docker.types.IPAMPool.__init__).args\n create_pool = docker.types.IPAMPool\n create_config = docker.types.IPAMConfig\n except AttributeError:\n # docker-py < 2.0\n pool_args = salt.utils.args.get_function_argspec(\n docker.utils.create_ipam_pool).args\n create_pool = docker.utils.create_ipam_pool\n create_config = docker.utils.create_ipam_config\n\n for primary_key, alias_key in (('driver', 'ipam_driver'),\n ('options', 'ipam_opts')):\n\n if alias_key in kwargs:\n alias_val = kwargs.pop(alias_key)\n if primary_key in kwargs:\n log.warning(\n 'docker.create_ipam_config: Both \\'%s\\' and \\'%s\\' '\n 'passed. Ignoring \\'%s\\'',\n alias_key, primary_key, alias_key\n )\n else:\n kwargs[primary_key] = alias_val\n\n if salt.utils.data.is_dictlist(kwargs.get('options')):\n kwargs['options'] = salt.utils.data.repack_dictlist(kwargs['options'])\n\n # Get all of the IPAM pool args that were passed as individual kwargs\n # instead of in the *pools tuple\n pool_kwargs = {}\n for key in list(kwargs):\n if key in pool_args:\n pool_kwargs[key] = kwargs.pop(key)\n\n pool_configs = []\n if pool_kwargs:\n pool_configs.append(create_pool(**pool_kwargs))\n pool_configs.extend([create_pool(**pool) for pool in pools])\n\n if pool_configs:\n # Sanity check the IPAM pools. docker-py's type/function for creating\n # an IPAM pool will allow you to create a pool with a gateway, IP\n # range, or map of aux addresses, even when no subnet is passed.\n # However, attempting to use this IPAM pool when creating the network\n # will cause the Docker Engine to throw an error.\n if any('Subnet' not in pool for pool in pool_configs):\n raise SaltInvocationError('A subnet is required in each IPAM pool')\n else:\n kwargs['pool_configs'] = pool_configs\n\n ret = create_config(**kwargs)\n pool_dicts = ret.get('Config')\n if pool_dicts:\n # When you inspect a network with custom IPAM configuration, only\n # arguments which were explictly passed are reflected. By contrast,\n # docker-py will include keys for arguments which were not passed in\n # but set the value to None. Thus, for ease of comparison, the below\n # loop will remove all keys with a value of None from the generated\n # pool configs.\n for idx, _ in enumerate(pool_dicts):\n for key in list(pool_dicts[idx]):\n if pool_dicts[idx][key] is None:\n del pool_dicts[idx][key]\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nstart the logstash engine", "response": "def start(host, port=5959, tag='salt/engine/logstash', proto='udp'):\n '''\n Listen to salt events and forward them to logstash\n '''\n\n if proto == 'tcp':\n logstashHandler = logstash.TCPLogstashHandler\n elif proto == 'udp':\n logstashHandler = logstash.UDPLogstashHandler\n\n logstash_logger = logging.getLogger('python-logstash-logger')\n logstash_logger.setLevel(logging.INFO)\n logstash_logger.addHandler(logstashHandler(host, port, version=1))\n\n if __opts__.get('id').endswith('_master'):\n event_bus = salt.utils.event.get_master_event(\n __opts__,\n __opts__['sock_dir'],\n listen=True)\n else:\n event_bus = salt.utils.event.get_event(\n 'minion',\n transport=__opts__['transport'],\n opts=__opts__,\n sock_dir=__opts__['sock_dir'],\n listen=True)\n log.debug('Logstash engine started')\n\n while True:\n event = event_bus.get_event()\n if event:\n logstash_logger.info(tag, extra=event)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nensuring a service exists and is up - to - date and is up - to - date.", "response": "def present(name, auth=None, **kwargs):\n '''\n Ensure an service exists and is up-to-date\n\n name\n Name of the group\n\n type\n Service type\n\n enabled\n Boolean to control if service is enabled\n\n description\n An arbitrary description of the service\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n kwargs = __utils__['args.clean_kwargs'](**kwargs)\n\n __salt__['keystoneng.setup_clouds'](auth)\n\n service = __salt__['keystoneng.service_get'](name=name)\n\n if service is None:\n if __opts__['test'] is True:\n ret['result'] = None\n ret['changes'] = kwargs\n ret['comment'] = 'Service will be created.'\n return ret\n\n kwargs['name'] = name\n service = __salt__['keystoneng.service_create'](**kwargs)\n ret['changes'] = service\n ret['comment'] = 'Created service'\n return ret\n\n changes = __salt__['keystoneng.compare_changes'](service, **kwargs)\n if changes:\n if __opts__['test'] is True:\n ret['result'] = None\n ret['changes'] = changes\n ret['comment'] = 'Service will be updated.'\n return ret\n\n kwargs['name'] = service\n __salt__['keystoneng.service_update'](**kwargs)\n ret['changes'].update(changes)\n ret['comment'] = 'Updated service'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef absent(name, auth=None):\n '''\n Ensure service does not exist\n\n name\n Name of the service\n '''\n\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n __salt__['keystoneng.setup_clouds'](auth)\n\n service = __salt__['keystoneng.service_get'](name=name)\n\n if service:\n if __opts__['test'] is True:\n ret['result'] = None\n ret['changes'] = {'id': service.id}\n ret['comment'] = 'Service will be deleted.'\n return ret\n\n __salt__['keystoneng.service_delete'](name=service)\n ret['changes']['id'] = service.id\n ret['comment'] = 'Deleted service'\n\n return ret", "response": "Ensure a service is absent"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_conn():\n '''\n Return a conn object for the passed VM data\n '''\n certificate_path = config.get_cloud_config_value(\n 'certificate_path',\n get_configured_provider(), __opts__, search_global=False\n )\n subscription_id = salt.utils.stringutils.to_str(\n config.get_cloud_config_value(\n 'subscription_id',\n get_configured_provider(), __opts__, search_global=False\n )\n )\n management_host = config.get_cloud_config_value(\n 'management_host',\n get_configured_provider(),\n __opts__,\n search_global=False,\n default='management.core.windows.net'\n )\n return azure.servicemanagement.ServiceManagementService(\n subscription_id, certificate_path, management_host\n )", "response": "Return a conn object for the passed VM data\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns a list of available locations for Azure resource tables", "response": "def avail_locations(conn=None, call=None):\n '''\n List available locations for Azure\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 not conn:\n conn = get_conn()\n\n ret = {}\n locations = conn.list_locations()\n for location in locations:\n ret[location.name] = {\n 'name': location.name,\n 'display_name': location.display_name,\n 'available_services': location.available_services,\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of available images for Azure resource types", "response": "def avail_images(conn=None, call=None):\n '''\n List available images for Azure\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_images function must be called with '\n '-f or --function, or with the --list-images option'\n )\n\n if not conn:\n conn = get_conn()\n\n ret = {}\n for item in conn.list_os_images():\n ret[item.name] = object_to_dict(item)\n for item in conn.list_vm_images():\n ret[item.name] = object_to_dict(item)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef avail_sizes(call=None):\n '''\n Return a list of sizes from Azure\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The avail_sizes function must be called with '\n '-f or --function, or with the --list-sizes option'\n )\n\n conn = get_conn()\n data = conn.list_role_sizes()\n ret = {}\n for item in data.role_sizes:\n ret[item.name] = object_to_dict(item)\n return ret", "response": "Return a list of sizes from Azure\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_nodes_full(conn=None, call=None):\n '''\n List VMs on this Azure account, with full information\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_nodes_full function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n ret = {}\n services = list_hosted_services(conn=conn, call=call)\n for service in services:\n for deployment in services[service]['deployments']:\n deploy_dict = services[service]['deployments'][deployment]\n deploy_dict_no_role_info = copy.deepcopy(deploy_dict)\n del deploy_dict_no_role_info['role_list']\n del deploy_dict_no_role_info['role_instance_list']\n roles = deploy_dict['role_list']\n for role in roles:\n role_instances = deploy_dict['role_instance_list']\n ret[role] = roles[role]\n ret[role].update(role_instances[role])\n ret[role]['id'] = role\n ret[role]['hosted_service'] = service\n if role_instances[role]['power_state'] == 'Started':\n ret[role]['state'] = 'running'\n elif role_instances[role]['power_state'] == 'Stopped':\n ret[role]['state'] = 'stopped'\n else:\n ret[role]['state'] = 'pending'\n ret[role]['private_ips'] = []\n ret[role]['public_ips'] = []\n ret[role]['deployment'] = deploy_dict_no_role_info\n ret[role]['url'] = deploy_dict['url']\n ip_address = role_instances[role]['ip_address']\n if ip_address:\n if salt.utils.cloud.is_public_ip(ip_address):\n ret[role]['public_ips'].append(ip_address)\n else:\n ret[role]['private_ips'].append(ip_address)\n ret[role]['size'] = role_instances[role]['instance_size']\n ret[role]['image'] = roles[role]['role_info']['os_virtual_hard_disk']['source_image_name']\n return ret", "response": "List all VMs on this Azure account with full information."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nlisting the available hosted services on this Azure account with full information.", "response": "def list_hosted_services(conn=None, call=None):\n '''\n List VMs on this Azure account, with full information\n '''\n if call == 'action':\n raise SaltCloudSystemExit(\n 'The list_hosted_services function must be called with '\n '-f or --function'\n )\n\n if not conn:\n conn = get_conn()\n\n ret = {}\n services = conn.list_hosted_services()\n for service in services:\n props = service.hosted_service_properties\n ret[service.service_name] = {\n 'name': service.service_name,\n 'url': service.url,\n 'affinity_group': props.affinity_group,\n 'date_created': props.date_created,\n 'date_last_modified': props.date_last_modified,\n 'description': props.description,\n 'extended_properties': props.extended_properties,\n 'label': props.label,\n 'location': props.location,\n 'status': props.status,\n 'deployments': {},\n }\n deployments = conn.get_hosted_service_properties(\n service_name=service.service_name, embed_detail=True\n )\n for deployment in deployments.deployments:\n ret[service.service_name]['deployments'][deployment.name] = {\n 'configuration': deployment.configuration,\n 'created_time': deployment.created_time,\n 'deployment_slot': deployment.deployment_slot,\n 'extended_properties': deployment.extended_properties,\n 'input_endpoint_list': deployment.input_endpoint_list,\n 'label': deployment.label,\n 'last_modified_time': deployment.last_modified_time,\n 'locked': deployment.locked,\n 'name': deployment.name,\n 'persistent_vm_downtime_info': deployment.persistent_vm_downtime_info,\n 'private_id': deployment.private_id,\n 'role_instance_list': {},\n 'role_list': {},\n 'rollback_allowed': deployment.rollback_allowed,\n 'sdk_version': deployment.sdk_version,\n 'status': deployment.status,\n 'upgrade_domain_count': deployment.upgrade_domain_count,\n 'upgrade_status': deployment.upgrade_status,\n 'url': deployment.url,\n }\n for role_instance in deployment.role_instance_list:\n ret[service.service_name]['deployments'][deployment.name]['role_instance_list'][role_instance.role_name] = {\n 'fqdn': role_instance.fqdn,\n 'instance_error_code': role_instance.instance_error_code,\n 'instance_fault_domain': role_instance.instance_fault_domain,\n 'instance_name': role_instance.instance_name,\n 'instance_size': role_instance.instance_size,\n 'instance_state_details': role_instance.instance_state_details,\n 'instance_status': role_instance.instance_status,\n 'instance_upgrade_domain': role_instance.instance_upgrade_domain,\n 'ip_address': role_instance.ip_address,\n 'power_state': role_instance.power_state,\n 'role_name': role_instance.role_name,\n }\n for role in deployment.role_list:\n ret[service.service_name]['deployments'][deployment.name]['role_list'][role.role_name] = {\n 'role_name': role.role_name,\n 'os_version': role.os_version,\n }\n role_info = conn.get_role(\n service_name=service.service_name,\n deployment_name=deployment.name,\n role_name=role.role_name,\n )\n ret[service.service_name]['deployments'][deployment.name]['role_list'][role.role_name]['role_info'] = {\n 'availability_set_name': role_info.availability_set_name,\n 'configuration_sets': role_info.configuration_sets,\n 'data_virtual_hard_disks': role_info.data_virtual_hard_disks,\n 'os_version': role_info.os_version,\n 'role_name': role_info.role_name,\n 'role_size': role_info.role_size,\n 'role_type': role_info.role_type,\n }\n ret[service.service_name]['deployments'][deployment.name]['role_list'][role.role_name]['role_info']['os_virtual_hard_disk'] = {\n 'disk_label': role_info.os_virtual_hard_disk.disk_label,\n 'disk_name': role_info.os_virtual_hard_disk.disk_name,\n 'host_caching': role_info.os_virtual_hard_disk.host_caching,\n 'media_link': role_info.os_virtual_hard_disk.media_link,\n 'os': role_info.os_virtual_hard_disk.os,\n 'source_image_name': role_info.os_virtual_hard_disk.source_image_name,\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would 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 nodes = list_nodes_full()\n # Find under which cloud service the name is listed, if any\n if name not in nodes:\n return {}\n if 'name' not in nodes[name]:\n nodes[name]['name'] = nodes[name]['id']\n try:\n __utils__['cloud.cache_node'](nodes[name], __active_provider_name__, __opts__)\n except TypeError:\n log.warning('Unable to show cache node data; this may be because the node has been deleted')\n return nodes[name]"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a single VM from a data dict", "response": "def 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 'azure',\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 log.info('Creating Cloud VM %s', vm_['name'])\n conn = get_conn()\n\n label = vm_.get('label', vm_['name'])\n service_name = vm_.get('service_name', vm_['name'])\n service_kwargs = {\n 'service_name': service_name,\n 'label': label,\n 'description': vm_.get('desc', vm_['name']),\n }\n\n loc_error = False\n if 'location' in vm_:\n if 'affinity_group' in vm_:\n loc_error = True\n else:\n service_kwargs['location'] = vm_['location']\n elif 'affinity_group' in vm_:\n service_kwargs['affinity_group'] = vm_['affinity_group']\n else:\n loc_error = True\n\n if loc_error:\n raise SaltCloudSystemExit(\n 'Either a location or affinity group must be specified, but not both'\n )\n\n ssh_port = config.get_cloud_config_value('port', vm_, __opts__,\n default=22, search_global=True)\n\n ssh_endpoint = azure.servicemanagement.ConfigurationSetInputEndpoint(\n name='SSH',\n protocol='TCP',\n port=ssh_port,\n local_port=22,\n )\n\n network_config = azure.servicemanagement.ConfigurationSet()\n network_config.input_endpoints.input_endpoints.append(ssh_endpoint)\n network_config.configuration_set_type = 'NetworkConfiguration'\n\n if 'win_username' in vm_:\n system_config = azure.servicemanagement.WindowsConfigurationSet(\n computer_name=vm_['name'],\n admin_username=vm_['win_username'],\n admin_password=vm_['win_password'],\n )\n\n smb_port = '445'\n if 'smb_port' in vm_:\n smb_port = vm_['smb_port']\n\n smb_endpoint = azure.servicemanagement.ConfigurationSetInputEndpoint(\n name='SMB',\n protocol='TCP',\n port=smb_port,\n local_port=smb_port,\n )\n\n network_config.input_endpoints.input_endpoints.append(smb_endpoint)\n\n # Domain and WinRM configuration not yet supported by Salt Cloud\n system_config.domain_join = None\n system_config.win_rm = None\n\n else:\n system_config = azure.servicemanagement.LinuxConfigurationSet(\n host_name=vm_['name'],\n user_name=vm_['ssh_username'],\n user_password=vm_['ssh_password'],\n disable_ssh_password_authentication=False,\n )\n\n # TODO: Might need to create a storage account\n media_link = vm_['media_link']\n # TODO: Probably better to use more than just the name in the media_link\n media_link += '/{0}.vhd'.format(vm_['name'])\n os_hd = azure.servicemanagement.OSVirtualHardDisk(vm_['image'], media_link)\n\n vm_kwargs = {\n 'service_name': service_name,\n 'deployment_name': service_name,\n 'deployment_slot': vm_['slot'],\n 'label': label,\n 'role_name': vm_['name'],\n 'system_config': system_config,\n 'os_virtual_hard_disk': os_hd,\n 'role_size': vm_['size'],\n 'network_config': network_config,\n }\n\n if 'virtual_network_name' in vm_:\n vm_kwargs['virtual_network_name'] = vm_['virtual_network_name']\n if 'subnet_name' in vm_:\n network_config.subnet_names.append(vm_['subnet_name'])\n\n log.debug('vm_kwargs: %s', vm_kwargs)\n\n event_kwargs = {'service_kwargs': service_kwargs.copy(),\n 'vm_kwargs': vm_kwargs.copy()}\n del event_kwargs['vm_kwargs']['system_config']\n del event_kwargs['vm_kwargs']['os_virtual_hard_disk']\n del event_kwargs['vm_kwargs']['network_config']\n __utils__['cloud.fire_event'](\n 'event',\n 'requesting instance',\n 'salt/cloud/{0}/requesting'.format(vm_['name']),\n args=__utils__['cloud.filter_event']('requesting', event_kwargs, list(event_kwargs)),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n log.debug('vm_kwargs: %s', vm_kwargs)\n\n # Azure lets you open winrm on a new VM\n # Can open up specific ports in Azure; but not on Windows\n try:\n conn.create_hosted_service(**service_kwargs)\n except AzureConflictHttpError:\n log.debug('Cloud service already exists')\n except Exception as exc:\n error = 'The hosted service name is invalid.'\n if error in six.text_type(exc):\n log.error(\n 'Error creating %s on Azure.\\n\\n'\n 'The hosted service name is invalid. The name can contain '\n 'only letters, numbers, and hyphens. The name must start with '\n 'a letter and must end with a letter or a number.',\n vm_['name'],\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n else:\n log.error(\n 'Error creating %s on Azure\\n\\n'\n 'The following exception was thrown when trying to '\n 'run the initial deployment: \\n%s',\n vm_['name'], exc,\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n return False\n try:\n result = conn.create_virtual_machine_deployment(**vm_kwargs)\n log.debug('Request ID for machine: %s', result.request_id)\n _wait_for_async(conn, result.request_id)\n except AzureConflictHttpError:\n log.debug('Conflict error. The deployment may already exist, trying add_role')\n # Deleting two useless keywords\n del vm_kwargs['deployment_slot']\n del vm_kwargs['label']\n del vm_kwargs['virtual_network_name']\n result = conn.add_role(**vm_kwargs)\n _wait_for_async(conn, result.request_id)\n except Exception as exc:\n error = 'The hosted service name is invalid.'\n if error in six.text_type(exc):\n log.error(\n 'Error creating %s on Azure.\\n\\n'\n 'The VM name is invalid. The name can contain '\n 'only letters, numbers, and hyphens. The name must start with '\n 'a letter and must end with a letter or a number.',\n vm_['name'],\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n else:\n log.error(\n 'Error creating %s on Azure.\\n\\n'\n 'The Virtual Machine could not be created. If you '\n 'are using an already existing Cloud Service, '\n 'make sure you set up the `port` variable corresponding '\n 'to the SSH port exists and that the port number is not '\n 'already in use.\\nThe following exception was thrown when trying to '\n 'run the initial deployment: \\n%s',\n vm_['name'], exc,\n # Show the traceback if the debug logging level is enabled\n exc_info_on_loglevel=logging.DEBUG\n )\n return False\n\n def wait_for_hostname():\n '''\n Wait for the IP address to become available\n '''\n try:\n conn.get_role(service_name, service_name, vm_['name'])\n data = show_instance(vm_['name'], call='action')\n if 'url' in data and data['url'] != six.text_type(''):\n return data['url']\n except AzureMissingResourceHttpError:\n pass\n time.sleep(1)\n return False\n\n hostname = salt.utils.cloud.wait_for_fun(\n wait_for_hostname,\n timeout=config.get_cloud_config_value(\n 'wait_for_fun_timeout', vm_, __opts__, default=15 * 60),\n )\n\n if not hostname:\n log.error('Failed to get a value for the hostname.')\n return False\n\n vm_['ssh_host'] = hostname.replace('http://', '').replace('/', '')\n vm_['password'] = config.get_cloud_config_value(\n 'ssh_password', vm_, __opts__\n )\n ret = __utils__['cloud.bootstrap'](vm_, __opts__)\n\n # Attaching volumes\n volumes = config.get_cloud_config_value(\n 'volumes', vm_, __opts__, search_global=True\n )\n if volumes:\n __utils__['cloud.fire_event'](\n 'event',\n 'attaching volumes',\n 'salt/cloud/{0}/attaching_volumes'.format(vm_['name']),\n args=__utils__['cloud.filter_event']('attaching_volumes', vm_, ['volumes']),\n sock_dir=__opts__['sock_dir'],\n transport=__opts__['transport']\n )\n\n log.info('Create and attach volumes to node %s', vm_['name'])\n created = create_attach_volumes(\n vm_['name'],\n {\n 'volumes': volumes,\n 'service_name': service_name,\n 'deployment_name': vm_['name'],\n 'media_link': media_link,\n 'role_name': vm_['name'],\n 'del_all_vols_on_destroy': vm_.get('set_del_all_vols_on_destroy', False)\n },\n call='action'\n )\n ret['Attached Volumes'] = created\n\n data = show_instance(vm_['name'], call='action')\n log.info('Created Cloud VM \\'%s\\'', vm_)\n log.debug('\\'%s\\' VM creation details:\\n%s', vm_['name'], pprint.pformat(data))\n\n ret.update(data)\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"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_attach_volumes(name, kwargs, call=None, wait_to_finish=True):\n '''\n Create and attach volumes to created node\n '''\n if call != 'action':\n raise SaltCloudSystemExit(\n 'The create_attach_volumes action must be called with '\n '-a or --action.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if isinstance(kwargs['volumes'], six.string_types):\n volumes = salt.utils.yaml.safe_load(kwargs['volumes'])\n else:\n volumes = kwargs['volumes']\n\n # From the Azure .NET SDK doc\n #\n # The Create Data Disk operation adds a data disk to a virtual\n # machine. There are three ways to create the data disk using the\n # Add Data Disk operation.\n # Option 1 - Attach an empty data disk to\n # the role by specifying the disk label and location of the disk\n # image. Do not include the DiskName and SourceMediaLink elements in\n # the request body. Include the MediaLink element and reference a\n # blob that is in the same geographical region as the role. You can\n # also omit the MediaLink element. In this usage, Azure will create\n # the data disk in the storage account configured as default for the\n # role.\n # Option 2 - Attach an existing data disk that is in the image\n # repository. Do not include the DiskName and SourceMediaLink\n # elements in the request body. Specify the data disk to use by\n # including the DiskName element. Note: If included the in the\n # response body, the MediaLink and LogicalDiskSizeInGB elements are\n # ignored.\n # Option 3 - Specify the location of a blob in your storage\n # account that contain a disk image to use. Include the\n # SourceMediaLink element. Note: If the MediaLink element\n # isincluded, it is ignored. (see\n # http://msdn.microsoft.com/en-us/library/windowsazure/jj157199.aspx\n # for more information)\n #\n # Here only option 1 is implemented\n conn = get_conn()\n ret = []\n for volume in volumes:\n if \"disk_name\" in volume:\n log.error(\"You cannot specify a disk_name. Only new volumes are allowed\")\n return False\n # Use the size keyword to set a size, but you can use the\n # azure name too. If neither is set, the disk has size 100GB\n volume.setdefault(\"logical_disk_size_in_gb\", volume.get(\"size\", 100))\n volume.setdefault(\"host_caching\", \"ReadOnly\")\n volume.setdefault(\"lun\", 0)\n # The media link is vm_name-disk-[0-15].vhd\n volume.setdefault(\"media_link\",\n kwargs[\"media_link\"][:-4] + \"-disk-{0}.vhd\".format(volume[\"lun\"]))\n volume.setdefault(\"disk_label\",\n kwargs[\"role_name\"] + \"-disk-{0}\".format(volume[\"lun\"]))\n volume_dict = {\n 'volume_name': volume[\"lun\"],\n 'disk_label': volume[\"disk_label\"]\n }\n\n # Preparing the volume dict to be passed with **\n kwargs_add_data_disk = [\"lun\", \"host_caching\", \"media_link\",\n \"disk_label\", \"disk_name\",\n \"logical_disk_size_in_gb\",\n \"source_media_link\"]\n for key in set(volume.keys()) - set(kwargs_add_data_disk):\n del volume[key]\n\n attach = conn.add_data_disk(kwargs[\"service_name\"], kwargs[\"deployment_name\"], kwargs[\"role_name\"],\n **volume)\n log.debug(attach)\n\n # If attach is None then everything is fine\n if attach:\n msg = (\n '{0} attached to {1} (aka {2})'.format(\n volume_dict['volume_name'],\n kwargs['role_name'],\n name,\n )\n )\n log.info(msg)\n ret.append(msg)\n else:\n log.error('Error attaching %s on Azure', volume_dict)\n return ret", "response": "Create and attach volumes to a virtual machine."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _wait_for_async(conn, request_id):\n '''\n Helper function for azure tests\n '''\n count = 0\n log.debug('Waiting for asynchronous operation to complete')\n result = conn.get_operation_status(request_id)\n while result.status == 'InProgress':\n count = count + 1\n if count > 120:\n raise ValueError('Timed out waiting for asynchronous operation to complete.')\n time.sleep(5)\n result = conn.get_operation_status(request_id)\n\n if result.status != 'Succeeded':\n raise AzureException('Operation failed. {message} ({code})'\n .format(message=result.error.message,\n code=result.error.code))", "response": "Helper function for azure tests"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndestroys a VM and its associated resources.", "response": "def destroy(name, conn=None, call=None, kwargs=None):\n '''\n Destroy a VM\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -d myminion\n salt-cloud -a destroy myminion service_name=myservice\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 if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n instance_data = show_instance(name, call='action')\n service_name = instance_data['deployment']['name']\n disk_name = instance_data['role_info']['os_virtual_hard_disk']['disk_name']\n\n ret = {}\n # TODO: Add the ability to delete or not delete a hosted service when\n # deleting a VM\n try:\n log.debug('Deleting role')\n result = conn.delete_role(service_name, service_name, name)\n delete_type = 'delete_role'\n except AzureException:\n log.debug('Failed to delete role, deleting deployment')\n try:\n result = conn.delete_deployment(service_name, service_name)\n except AzureConflictHttpError as exc:\n log.error(exc.message)\n raise SaltCloudSystemExit('{0}: {1}'.format(name, exc.message))\n delete_type = 'delete_deployment'\n _wait_for_async(conn, result.request_id)\n ret[name] = {\n delete_type: {'request_id': result.request_id},\n }\n if __opts__.get('update_cachedir', False) is True:\n __utils__['cloud.delete_minion_cachedir'](name, __active_provider_name__.split(':')[0], __opts__)\n\n cleanup_disks = config.get_cloud_config_value(\n 'cleanup_disks',\n get_configured_provider(), __opts__, search_global=False, default=False,\n )\n if cleanup_disks:\n cleanup_vhds = kwargs.get('delete_vhd', config.get_cloud_config_value(\n 'cleanup_vhds',\n get_configured_provider(), __opts__, search_global=False, default=False,\n ))\n log.debug('Deleting disk %s', disk_name)\n if cleanup_vhds:\n log.debug('Deleting vhd')\n\n def wait_for_destroy():\n '''\n Wait for the VM to be deleted\n '''\n try:\n data = delete_disk(kwargs={'name': disk_name, 'delete_vhd': cleanup_vhds}, call='function')\n return data\n except AzureConflictHttpError:\n log.debug('Waiting for VM to be destroyed...')\n time.sleep(5)\n return False\n\n data = salt.utils.cloud.wait_for_fun(\n wait_for_destroy,\n timeout=config.get_cloud_config_value(\n 'wait_for_fun_timeout', {}, __opts__, default=15 * 60),\n )\n ret[name]['delete_disk'] = {\n 'name': disk_name,\n 'delete_vhd': cleanup_vhds,\n 'data': data\n }\n\n # Services can't be cleaned up unless disks are too\n cleanup_services = config.get_cloud_config_value(\n 'cleanup_services',\n get_configured_provider(), __opts__, search_global=False, default=False\n )\n if cleanup_services:\n log.debug('Deleting service %s', service_name)\n\n def wait_for_disk_delete():\n '''\n Wait for the disk to be deleted\n '''\n try:\n data = delete_service(kwargs={'name': service_name}, call='function')\n return data\n except AzureConflictHttpError:\n log.debug('Waiting for disk to be deleted...')\n time.sleep(5)\n return False\n\n data = salt.utils.cloud.wait_for_fun(\n wait_for_disk_delete,\n timeout=config.get_cloud_config_value(\n 'wait_for_fun_timeout', {}, __opts__, default=15 * 60),\n )\n ret[name]['delete_services'] = {\n 'name': service_name,\n 'data': data\n }\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nlisting all storage services on this Azure account with full information.", "response": "def list_storage_services(conn=None, call=None):\n '''\n List VMs on this Azure account, with full information\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n ('The list_storage_services function must be called '\n 'with -f or --function.')\n )\n\n if not conn:\n conn = get_conn()\n\n ret = {}\n accounts = conn.list_storage_accounts()\n for service in accounts.storage_services:\n ret[service.service_name] = {\n 'capabilities': service.capabilities,\n 'service_name': service.service_name,\n 'storage_service_properties': service.storage_service_properties,\n 'extended_properties': service.extended_properties,\n 'storage_service_keys': service.storage_service_keys,\n 'url': service.url,\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the status of an operation on the specified resource ID", "response": "def get_operation_status(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Get Operation Status, based on a request ID\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f get_operation_status my-azure id=0123456789abcdef0123456789abcdef\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_instance function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'id' not in kwargs:\n raise SaltCloudSystemExit('A request ID must be specified as \"id\"')\n\n if not conn:\n conn = get_conn()\n\n data = conn.get_operation_status(kwargs['id'])\n ret = {\n 'http_status_code': data.http_status_code,\n 'id': kwargs['id'],\n 'status': data.status\n }\n if hasattr(data.error, 'code'):\n ret['error'] = {\n 'code': data.error.code,\n 'message': data.error.message,\n }\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list_storage(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List storage accounts associated with the account\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f list_storage my-azure\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_storage function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n data = conn.list_storage_accounts()\n pprint.pprint(dir(data))\n ret = {}\n for item in data.storage_services:\n ret[item.service_name] = object_to_dict(item)\n return ret", "response": "List storage accounts associated with the account\nTaxonomy CLI Example"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef show_storage(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List storage service properties\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_storage my-azure name=my_storage\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_storage function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n data = conn.get_storage_account_properties(\n kwargs['name'],\n )\n return object_to_dict(data)", "response": "Show the properties of a specific storage account"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef show_storage_keys(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Show storage account keys\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_storage_keys my-azure name=my_storage\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_storage_keys function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n try:\n data = conn.get_storage_account_keys(\n kwargs['name'],\n )\n except AzureMissingResourceHttpError as exc:\n storage_data = show_storage(kwargs={'name': kwargs['name']}, call='function')\n if storage_data['storage_service_properties']['status'] == 'Creating':\n raise SaltCloudSystemExit('The storage account keys have not yet been created.')\n else:\n raise SaltCloudSystemExit('{0}: {1}'.format(kwargs['name'], exc.message))\n return object_to_dict(data)", "response": "Show the storage account keys for the specified storage account."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef create_storage(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Create a new storage account\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f create_storage my-azure name=my_storage label=my_storage location='West US'\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_storage function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if not conn:\n conn = get_conn()\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if 'description' not in kwargs:\n raise SaltCloudSystemExit('A description must be specified as \"description\"')\n\n if 'label' not in kwargs:\n raise SaltCloudSystemExit('A label must be specified as \"label\"')\n\n if 'location' not in kwargs and 'affinity_group' not in kwargs:\n raise SaltCloudSystemExit('Either a location or an affinity_group '\n 'must be specified (but not both)')\n\n try:\n data = conn.create_storage_account(\n service_name=kwargs['name'],\n label=kwargs['label'],\n description=kwargs.get('description', None),\n location=kwargs.get('location', None),\n affinity_group=kwargs.get('affinity_group', None),\n extended_properties=kwargs.get('extended_properties', None),\n geo_replication_enabled=kwargs.get('geo_replication_enabled', None),\n account_type=kwargs.get('account_type', 'Standard_GRS'),\n )\n return {'Success': 'The storage account was successfully created'}\n except AzureConflictHttpError:\n raise SaltCloudSystemExit('There was a conflict. This usually means that the storage account already exists.')", "response": "Creates a new storage account in Azure"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef update_storage(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Update a storage account's properties\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f update_storage my-azure name=my_storage label=my_storage\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_storage function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n data = conn.update_storage_account(\n service_name=kwargs['name'],\n label=kwargs.get('label', None),\n description=kwargs.get('description', None),\n extended_properties=kwargs.get('extended_properties', None),\n geo_replication_enabled=kwargs.get('geo_replication_enabled', None),\n account_type=kwargs.get('account_type', 'Standard_GRS'),\n )\n return show_storage(kwargs={'name': kwargs['name']}, call='function')", "response": "Update a storage account s properties"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nregenerate storage account keys.", "response": "def regenerate_storage_keys(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Regenerate storage account keys. Requires a key_type (\"primary\" or\n \"secondary\") to be specified.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f regenerate_storage_keys my-azure name=my_storage key_type=primary\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_storage function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if 'key_type' not in kwargs or kwargs['key_type'] not in ('primary', 'secondary'):\n raise SaltCloudSystemExit('A key_type must be specified (\"primary\" or \"secondary\")')\n\n try:\n data = conn.regenerate_storage_account_keys(\n service_name=kwargs['name'],\n key_type=kwargs['key_type'],\n )\n return show_storage_keys(kwargs={'name': kwargs['name']}, call='function')\n except AzureConflictHttpError:\n raise SaltCloudSystemExit('There was a conflict. This usually means that the storage account already exists.')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef delete_storage(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Delete a specific storage account\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f delete_storage my-azure name=my_storage\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The delete_storage function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if not conn:\n conn = get_conn()\n\n try:\n data = conn.delete_storage_account(kwargs['name'])\n return {'Success': 'The storage account was successfully deleted'}\n except AzureMissingResourceHttpError as exc:\n raise SaltCloudSystemExit('{0}: {1}'.format(kwargs['name'], exc.message))", "response": "Delete a specific storage account"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_services(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List hosted services associated with the account\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f list_services my-azure\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_services function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n data = conn.list_hosted_services()\n ret = {}\n for item in data.hosted_services:\n ret[item.service_name] = object_to_dict(item)\n ret[item.service_name]['name'] = item.service_name\n return ret", "response": "List the hosted services associated with the account\nTaxonomy CLI Example"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef show_service(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List hosted service properties\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_service my-azure name=my_service\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_service function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n data = conn.get_hosted_service_properties(\n kwargs['name'],\n kwargs.get('details', False)\n )\n ret = object_to_dict(data)\n return ret", "response": "Show the properties of a hosted service."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a new hosted service in Azure", "response": "def create_service(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Create a new hosted service\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f create_service my-azure name=my_service label=my_service location='West US'\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The create_service function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if 'label' not in kwargs:\n raise SaltCloudSystemExit('A label must be specified as \"label\"')\n\n if 'location' not in kwargs and 'affinity_group' not in kwargs:\n raise SaltCloudSystemExit('Either a location or an affinity_group '\n 'must be specified (but not both)')\n\n try:\n data = conn.create_hosted_service(\n kwargs['name'],\n kwargs['label'],\n kwargs.get('description', None),\n kwargs.get('location', None),\n kwargs.get('affinity_group', None),\n kwargs.get('extended_properties', None),\n )\n return {'Success': 'The service was successfully created'}\n except AzureConflictHttpError:\n raise SaltCloudSystemExit('There was a conflict. This usually means that the service already exists.')"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete_service(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Delete a specific service associated with the account\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f delete_service my-azure name=my_service\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The delete_service function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if not conn:\n conn = get_conn()\n\n try:\n conn.delete_hosted_service(kwargs['name'])\n return {'Success': 'The service was successfully deleted'}\n except AzureMissingResourceHttpError as exc:\n raise SaltCloudSystemExit('{0}: {1}'.format(kwargs['name'], exc.message))", "response": "Delete a specific service associated with the Azure account"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists the disks associated with the account Taxonomy CLI Example", "response": "def list_disks(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List disks associated with the account\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f list_disks my-azure\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_disks function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n data = conn.list_disks()\n ret = {}\n for item in data.disks:\n ret[item.name] = object_to_dict(item)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef show_disk(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Return information about a disk\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_disk my-azure name=my_disk\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The get_disk function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n data = conn.get_disk(kwargs['name'])\n return object_to_dict(data)", "response": "Show information about a disk in Azure"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef cleanup_unattached_disks(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Cleans up all disks associated with the account, which are not attached.\n *** CAUTION *** This is a destructive function with no undo button, and no\n \"Are you sure?\" confirmation!\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f cleanup_unattached_disks my-azure name=my_disk\n salt-cloud -f cleanup_unattached_disks my-azure name=my_disk delete_vhd=True\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The delete_disk function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n disks = list_disks(kwargs=kwargs, conn=conn, call='function')\n for disk in disks:\n if disks[disk]['attached_to'] is None:\n del_kwargs = {\n 'name': disks[disk]['name'],\n 'delete_vhd': kwargs.get('delete_vhd', False)\n }\n log.info(\n 'Deleting disk %s, deleting VHD: %s',\n del_kwargs['name'], del_kwargs['delete_vhd']\n )\n data = delete_disk(kwargs=del_kwargs, call='function')\n return True", "response": "Cleans up all unattached disks associated with the account."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ndeleting a specific disk associated with the account", "response": "def delete_disk(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Delete a specific disk associated with the account\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f delete_disk my-azure name=my_disk\n salt-cloud -f delete_disk my-azure name=my_disk delete_vhd=True\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The delete_disk function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if not conn:\n conn = get_conn()\n\n try:\n data = conn.delete_disk(kwargs['name'], kwargs.get('delete_vhd', False))\n return {'Success': 'The disk was successfully deleted'}\n except AzureMissingResourceHttpError as exc:\n raise SaltCloudSystemExit('{0}: {1}'.format(kwargs['name'], exc.message))"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nupdate a disk in Azure", "response": "def update_disk(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Update a disk's properties\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f update_disk my-azure name=my_disk label=my_disk\n salt-cloud -f update_disk my-azure name=my_disk new_name=another_disk\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_disk function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n old_data = show_disk(kwargs={'name': kwargs['name']}, call='function')\n data = conn.update_disk(\n disk_name=kwargs['name'],\n has_operating_system=kwargs.get('has_operating_system', old_data['has_operating_system']),\n label=kwargs.get('label', old_data['label']),\n media_link=kwargs.get('media_link', old_data['media_link']),\n name=kwargs.get('new_name', old_data['name']),\n os=kwargs.get('os', old_data['os']),\n )\n return show_disk(kwargs={'name': kwargs['name']}, call='function')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlist the certificates associated with a service.", "response": "def list_service_certificates(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List certificates associated with the service\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f list_service_certificates my-azure name=my_service\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_service_certificates function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A service name must be specified as \"name\"')\n\n if not conn:\n conn = get_conn()\n\n data = conn.list_service_certificates(service_name=kwargs['name'])\n ret = {}\n for item in data.certificates:\n ret[item.thumbprint] = object_to_dict(item)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nshowing information about a service certificate in Azure", "response": "def show_service_certificate(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Return information about a service certificate\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_service_certificate my-azure name=my_service_certificate \\\\\n thumbalgorithm=sha1 thumbprint=0123456789ABCDEF\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The get_service_certificate function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A service name must be specified as \"name\"')\n\n if 'thumbalgorithm' not in kwargs:\n raise SaltCloudSystemExit('A thumbalgorithm must be specified as \"thumbalgorithm\"')\n\n if 'thumbprint' not in kwargs:\n raise SaltCloudSystemExit('A thumbprint must be specified as \"thumbprint\"')\n\n data = conn.get_service_certificate(\n kwargs['name'],\n kwargs['thumbalgorithm'],\n kwargs['thumbprint'],\n )\n return object_to_dict(data)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd a new service certificate to the Azure service.", "response": "def add_service_certificate(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Add a new service certificate\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f add_service_certificate my-azure name=my_service_certificate \\\\\n data='...CERT_DATA...' certificate_format=sha1 password=verybadpass\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The add_service_certificate function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if 'data' not in kwargs:\n raise SaltCloudSystemExit('Certificate data must be specified as \"data\"')\n\n if 'certificate_format' not in kwargs:\n raise SaltCloudSystemExit('A certificate_format must be specified as \"certificate_format\"')\n\n if 'password' not in kwargs:\n raise SaltCloudSystemExit('A password must be specified as \"password\"')\n\n try:\n data = conn.add_service_certificate(\n kwargs['name'],\n kwargs['data'],\n kwargs['certificate_format'],\n kwargs['password'],\n )\n return {'Success': 'The service certificate was successfully added'}\n except AzureConflictHttpError:\n raise SaltCloudSystemExit('There was a conflict. This usually means that the '\n 'service certificate already exists.')"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete_service_certificate(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Delete a specific certificate associated with the service\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f delete_service_certificate my-azure name=my_service_certificate \\\\\n thumbalgorithm=sha1 thumbprint=0123456789ABCDEF\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The delete_service_certificate function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if 'thumbalgorithm' not in kwargs:\n raise SaltCloudSystemExit('A thumbalgorithm must be specified as \"thumbalgorithm\"')\n\n if 'thumbprint' not in kwargs:\n raise SaltCloudSystemExit('A thumbprint must be specified as \"thumbprint\"')\n\n if not conn:\n conn = get_conn()\n\n try:\n data = conn.delete_service_certificate(\n kwargs['name'],\n kwargs['thumbalgorithm'],\n kwargs['thumbprint'],\n )\n return {'Success': 'The service certificate was successfully deleted'}\n except AzureMissingResourceHttpError as exc:\n raise SaltCloudSystemExit('{0}: {1}'.format(kwargs['name'], exc.message))", "response": "Delete a specific service certificate associated with the specified Azure service."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nlist management certificates associated with the subscription", "response": "def list_management_certificates(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List management certificates associated with the subscription\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f list_management_certificates my-azure name=my_management\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_management_certificates function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n data = conn.list_management_certificates()\n ret = {}\n for item in data.subscription_certificates:\n ret[item.subscription_certificate_thumbprint] = object_to_dict(item)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef show_management_certificate(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Return information about a management_certificate\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f get_management_certificate my-azure name=my_management_certificate \\\\\n thumbalgorithm=sha1 thumbprint=0123456789ABCDEF\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The get_management_certificate function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'thumbprint' not in kwargs:\n raise SaltCloudSystemExit('A thumbprint must be specified as \"thumbprint\"')\n\n data = conn.get_management_certificate(kwargs['thumbprint'])\n return object_to_dict(data)", "response": "Show information about a management certificate"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef add_management_certificate(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Add a new management certificate\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f add_management_certificate my-azure public_key='...PUBKEY...' \\\\\n thumbprint=0123456789ABCDEF data='...CERT_DATA...'\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The add_management_certificate function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'public_key' not in kwargs:\n raise SaltCloudSystemExit('A public_key must be specified as \"public_key\"')\n\n if 'thumbprint' not in kwargs:\n raise SaltCloudSystemExit('A thumbprint must be specified as \"thumbprint\"')\n\n if 'data' not in kwargs:\n raise SaltCloudSystemExit('Certificate data must be specified as \"data\"')\n\n try:\n conn.add_management_certificate(\n kwargs['name'],\n kwargs['thumbprint'],\n kwargs['data'],\n )\n return {'Success': 'The management certificate was successfully added'}\n except AzureConflictHttpError:\n raise SaltCloudSystemExit('There was a conflict. '\n 'This usually means that the management certificate already exists.')", "response": "Adds a new management certificate to the specified resource."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nlists virtual networks associated with the deployment", "response": "def list_virtual_networks(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List input endpoints associated with the deployment\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f list_virtual_networks my-azure service=myservice deployment=mydeployment\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_virtual_networks function must be called with -f or --function.'\n )\n\n path = 'services/networking/virtualnetwork'\n data = query(path)\n return data"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlist all input endpoints associated with a service on a deployment", "response": "def list_input_endpoints(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List input endpoints associated with the deployment\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f list_input_endpoints my-azure service=myservice deployment=mydeployment\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_input_endpoints function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'service' not in kwargs:\n raise SaltCloudSystemExit('A service name must be specified as \"service\"')\n\n if 'deployment' not in kwargs:\n raise SaltCloudSystemExit('A deployment name must be specified as \"deployment\"')\n\n path = 'services/hostedservices/{0}/deployments/{1}'.format(\n kwargs['service'],\n kwargs['deployment'],\n )\n\n data = query(path)\n if data is None:\n raise SaltCloudSystemExit(\n 'There was an error listing endpoints with the {0} service on the {1} deployment.'.format(\n kwargs['service'],\n kwargs['deployment']\n )\n )\n\n ret = {}\n for item in data:\n if 'Role' in item:\n role = item['Role']\n if not isinstance(role, dict):\n return ret\n input_endpoint = role['ConfigurationSets']['ConfigurationSet'].get('InputEndpoints', {}).get('InputEndpoint')\n if not input_endpoint:\n continue\n if not isinstance(input_endpoint, list):\n input_endpoint = [input_endpoint]\n for endpoint in input_endpoint:\n ret[endpoint['Name']] = endpoint\n return ret\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef show_input_endpoint(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Show an input endpoint associated with the deployment\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_input_endpoint my-azure service=myservice \\\\\n deployment=mydeployment name=SSH\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_input_endpoint function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('An endpoint name must be specified as \"name\"')\n\n data = list_input_endpoints(kwargs=kwargs, call='function')\n return data.get(kwargs['name'], None)", "response": "Show an input endpoint associated with the specified deployment."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nupdating an input endpoint associated with a deployment.", "response": "def update_input_endpoint(kwargs=None, conn=None, call=None, activity='update'):\n '''\n .. versionadded:: 2015.8.0\n\n Update an input endpoint associated with the deployment. Please note that\n there may be a delay before the changes show up.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f update_input_endpoint my-azure service=myservice \\\\\n deployment=mydeployment role=myrole name=HTTP local_port=80 \\\\\n port=80 protocol=tcp enable_direct_server_return=False \\\\\n timeout_for_tcp_idle_connection=4\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The update_input_endpoint function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'service' not in kwargs:\n raise SaltCloudSystemExit('A service name must be specified as \"service\"')\n\n if 'deployment' not in kwargs:\n raise SaltCloudSystemExit('A deployment name must be specified as \"deployment\"')\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('An endpoint name must be specified as \"name\"')\n\n if 'role' not in kwargs:\n raise SaltCloudSystemExit('An role name must be specified as \"role\"')\n\n if activity != 'delete':\n if 'port' not in kwargs:\n raise SaltCloudSystemExit('An endpoint port must be specified as \"port\"')\n\n if 'protocol' not in kwargs:\n raise SaltCloudSystemExit('An endpoint protocol (tcp or udp) must be specified as \"protocol\"')\n\n if 'local_port' not in kwargs:\n kwargs['local_port'] = kwargs['port']\n\n if 'enable_direct_server_return' not in kwargs:\n kwargs['enable_direct_server_return'] = False\n kwargs['enable_direct_server_return'] = six.text_type(kwargs['enable_direct_server_return']).lower()\n\n if 'timeout_for_tcp_idle_connection' not in kwargs:\n kwargs['timeout_for_tcp_idle_connection'] = 4\n\n old_endpoints = list_input_endpoints(kwargs, call='function')\n\n endpoints_xml = ''\n endpoint_xml = '''\n <InputEndpoint>\n <LocalPort>{local_port}</LocalPort>\n <Name>{name}</Name>\n <Port>{port}</Port>\n <Protocol>{protocol}</Protocol>\n <EnableDirectServerReturn>{enable_direct_server_return}</EnableDirectServerReturn>\n <IdleTimeoutInMinutes>{timeout_for_tcp_idle_connection}</IdleTimeoutInMinutes>\n </InputEndpoint>'''\n\n if activity == 'add':\n old_endpoints[kwargs['name']] = kwargs\n old_endpoints[kwargs['name']]['Name'] = kwargs['name']\n\n for endpoint in old_endpoints:\n if old_endpoints[endpoint]['Name'] == kwargs['name']:\n if activity != 'delete':\n this_endpoint_xml = endpoint_xml.format(**kwargs)\n endpoints_xml += this_endpoint_xml\n else:\n this_endpoint_xml = endpoint_xml.format(\n local_port=old_endpoints[endpoint]['LocalPort'],\n name=old_endpoints[endpoint]['Name'],\n port=old_endpoints[endpoint]['Port'],\n protocol=old_endpoints[endpoint]['Protocol'],\n enable_direct_server_return=old_endpoints[endpoint]['EnableDirectServerReturn'],\n timeout_for_tcp_idle_connection=old_endpoints[endpoint].get('IdleTimeoutInMinutes', 4),\n )\n endpoints_xml += this_endpoint_xml\n\n request_xml = '''<PersistentVMRole xmlns=\"http://schemas.microsoft.com/windowsazure\"\nxmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n <ConfigurationSets>\n <ConfigurationSet>\n <ConfigurationSetType>NetworkConfiguration</ConfigurationSetType>\n <InputEndpoints>{0}\n </InputEndpoints>\n </ConfigurationSet>\n </ConfigurationSets>\n <OSVirtualHardDisk>\n </OSVirtualHardDisk>\n</PersistentVMRole>'''.format(endpoints_xml)\n\n path = 'services/hostedservices/{0}/deployments/{1}/roles/{2}'.format(\n kwargs['service'],\n kwargs['deployment'],\n kwargs['role'],\n )\n query(\n path=path,\n method='PUT',\n header_dict={'Content-Type': 'application/xml'},\n data=request_xml,\n decode=False,\n )\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nadd an input endpoint to the current deployment.", "response": "def add_input_endpoint(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Add an input endpoint to the deployment. Please note that\n there may be a delay before the changes show up.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f add_input_endpoint my-azure service=myservice \\\\\n deployment=mydeployment role=myrole name=HTTP local_port=80 \\\\\n port=80 protocol=tcp enable_direct_server_return=False \\\\\n timeout_for_tcp_idle_connection=4\n '''\n return update_input_endpoint(\n kwargs=kwargs,\n conn=conn,\n call='function',\n activity='add',\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete_input_endpoint(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Delete an input endpoint from the deployment. Please note that\n there may be a delay before the changes show up.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f delete_input_endpoint my-azure service=myservice \\\\\n deployment=mydeployment role=myrole name=HTTP\n '''\n return update_input_endpoint(\n kwargs=kwargs,\n conn=conn,\n call='function',\n activity='delete',\n )", "response": "Delete an input endpoint from the specified deployment"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef show_deployment(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Return information about a deployment\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_deployment my-azure name=my_deployment\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The get_deployment function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'service_name' not in kwargs:\n raise SaltCloudSystemExit('A service name must be specified as \"service_name\"')\n\n if 'deployment_name' not in kwargs:\n raise SaltCloudSystemExit('A deployment name must be specified as \"deployment_name\"')\n\n data = conn.get_deployment_by_name(\n service_name=kwargs['service_name'],\n deployment_name=kwargs['deployment_name'],\n )\n return object_to_dict(data)", "response": "Show information about a deployment in Azure"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_affinity_groups(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List input endpoints associated with the deployment\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f list_affinity_groups my-azure\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_affinity_groups function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n data = conn.list_affinity_groups()\n ret = {}\n for item in data.affinity_groups:\n ret[item.name] = object_to_dict(item)\n return ret", "response": "List the affinity groups associated with the deployment"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef show_affinity_group(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Show an affinity group associated with the account\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_affinity_group my-azure service=myservice \\\\\n deployment=mydeployment name=SSH\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_affinity_group function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('An affinity group name must be specified as \"name\"')\n\n data = conn.get_affinity_group_properties(affinity_group_name=kwargs['name'])\n return object_to_dict(data)", "response": "Show an affinity group associated with the account"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef create_affinity_group(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Create a new affinity group\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f create_affinity_group my-azure name=my_affinity_group\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The create_affinity_group function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if 'label' not in kwargs:\n raise SaltCloudSystemExit('A label must be specified as \"label\"')\n\n if 'location' not in kwargs:\n raise SaltCloudSystemExit('A location must be specified as \"location\"')\n\n try:\n conn.create_affinity_group(\n kwargs['name'],\n kwargs['label'],\n kwargs['location'],\n kwargs.get('description', None),\n )\n return {'Success': 'The affinity group was successfully created'}\n except AzureConflictHttpError:\n raise SaltCloudSystemExit('There was a conflict. This usually means that the affinity group already exists.')", "response": "Creates a new affinity group"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nupdates an affinity group", "response": "def update_affinity_group(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Update an affinity group's properties\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f update_affinity_group my-azure name=my_group label=my_group\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The update_affinity_group function must be called with -f or --function.'\n )\n\n if not conn:\n conn = get_conn()\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if 'label' not in kwargs:\n raise SaltCloudSystemExit('A label must be specified as \"label\"')\n\n conn.update_affinity_group(\n affinity_group_name=kwargs['name'],\n label=kwargs['label'],\n description=kwargs.get('description', None),\n )\n return show_affinity_group(kwargs={'name': kwargs['name']}, call='function')"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ndelete a specific affinity group", "response": "def delete_affinity_group(kwargs=None, conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Delete a specific affinity group associated with the account\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f delete_affinity_group my-azure name=my_affinity_group\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The delete_affinity_group function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('A name must be specified as \"name\"')\n\n if not conn:\n conn = get_conn()\n\n try:\n conn.delete_affinity_group(kwargs['name'])\n return {'Success': 'The affinity group was successfully deleted'}\n except AzureMissingResourceHttpError as exc:\n raise SaltCloudSystemExit('{0}: {1}'.format(kwargs['name'], exc.message))"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning a storage_conn object for the storage account and storage key", "response": "def get_storage_conn(storage_account=None, storage_key=None, conn_kwargs=None):\n '''\n .. versionadded:: 2015.8.0\n\n Return a storage_conn object for the storage account\n '''\n if conn_kwargs is None:\n conn_kwargs = {}\n\n if not storage_account:\n storage_account = config.get_cloud_config_value(\n 'storage_account',\n get_configured_provider(), __opts__, search_global=False,\n default=conn_kwargs.get('storage_account', None)\n )\n if not storage_key:\n storage_key = config.get_cloud_config_value(\n 'storage_key',\n get_configured_provider(), __opts__, search_global=False,\n default=conn_kwargs.get('storage_key', None)\n )\n return azure.storage.BlobService(storage_account, storage_key)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating the URL to access a blob.", "response": "def make_blob_url(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Creates the URL to access a blob\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f make_blob_url my-azure container=mycontainer blob=myblob\n\n container:\n Name of the container.\n blob:\n Name of the blob.\n account:\n Name of the storage account. If not specified, derives the host base\n from the provider configuration.\n protocol:\n Protocol to use: 'http' or 'https'. If not specified, derives the host\n base from the provider configuration.\n host_base:\n Live host base URL. If not specified, derives the host base from the\n provider configuration.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The make_blob_url function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'container' not in kwargs:\n raise SaltCloudSystemExit('A container name must be specified as \"container\"')\n\n if 'blob' not in kwargs:\n raise SaltCloudSystemExit('A blob name must be specified as \"blob\"')\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n data = storage_conn.make_blob_url(\n kwargs['container'],\n kwargs['blob'],\n kwargs.get('account', None),\n kwargs.get('protocol', None),\n kwargs.get('host_base', None),\n )\n ret = {}\n for item in data.containers:\n ret[item.name] = object_to_dict(item)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_storage_containers(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List containers associated with the storage account\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f list_storage_containers my-azure\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_storage_containers function must be called with -f or --function.'\n )\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n data = storage_conn.list_containers()\n ret = {}\n for item in data.containers:\n ret[item.name] = object_to_dict(item)\n return ret", "response": "List the storage containers associated with the storage account"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncreate a storage container in Azure.", "response": "def create_storage_container(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Create a storage container\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f create_storage_container my-azure name=mycontainer\n\n name:\n Name of container to create.\n meta_name_values:\n Optional. A dict with name_value pairs to associate with the\n container as metadata. Example:{'Category':'test'}\n blob_public_access:\n Optional. Possible values include: container, blob\n fail_on_exist:\n Specify whether to throw an exception when the container exists.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The create_storage_container function must be called with -f or --function.'\n )\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n try:\n storage_conn.create_container(\n container_name=kwargs['name'],\n x_ms_meta_name_values=kwargs.get('meta_name_values', None),\n x_ms_blob_public_access=kwargs.get('blob_public_access', None),\n fail_on_exist=kwargs.get('fail_on_exist', False),\n )\n return {'Success': 'The storage container was successfully created'}\n except AzureConflictHttpError:\n raise SaltCloudSystemExit('There was a conflict. This usually means that the storage container already exists.')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef show_storage_container(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Show a container associated with the storage account\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_storage_container my-azure name=myservice\n\n name:\n Name of container to show.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_storage_container function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('An storage container name must be specified as \"name\"')\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n data = storage_conn.get_container_properties(\n container_name=kwargs['name'],\n x_ms_lease_id=kwargs.get('lease_id', None),\n )\n return data", "response": "Show a container associated with the storage account"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef show_storage_container_metadata(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Show a storage container's metadata\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_storage_container_metadata my-azure name=myservice\n\n name:\n Name of container to show.\n lease_id:\n If specified, show_storage_container_metadata only succeeds if the\n container's lease is active and matches this ID.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_storage_container function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('An storage container name must be specified as \"name\"')\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n data = storage_conn.get_container_metadata(\n container_name=kwargs['name'],\n x_ms_lease_id=kwargs.get('lease_id', None),\n )\n return data", "response": "Show a storage container s metadata."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef set_storage_container_metadata(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Set a storage container's metadata\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f set_storage_container my-azure name=mycontainer \\\\\n x_ms_meta_name_values='{\"my_name\": \"my_value\"}'\n\n name:\n Name of existing container.\n meta_name_values:\n A dict containing name, value for metadata.\n Example: {'category':'test'}\n lease_id:\n If specified, set_storage_container_metadata only succeeds if the\n container's lease is active and matches this ID.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The create_storage_container function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('An storage container name must be specified as \"name\"')\n\n x_ms_meta_name_values = salt.utils.yaml.safe_load(\n kwargs.get('meta_name_values', '')\n )\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n try:\n storage_conn.set_container_metadata(\n container_name=kwargs['name'],\n x_ms_meta_name_values=x_ms_meta_name_values,\n x_ms_lease_id=kwargs.get('lease_id', None),\n )\n return {'Success': 'The storage container was successfully updated'}\n except AzureConflictHttpError:\n raise SaltCloudSystemExit('There was a conflict.')", "response": "Set a storage container s metadata."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nshows a storage container s acl", "response": "def show_storage_container_acl(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Show a storage container's acl\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_storage_container_acl my-azure name=myservice\n\n name:\n Name of existing container.\n lease_id:\n If specified, show_storage_container_acl only succeeds if the\n container's lease is active and matches this ID.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_storage_container function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('An storage container name must be specified as \"name\"')\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n data = storage_conn.get_container_acl(\n container_name=kwargs['name'],\n x_ms_lease_id=kwargs.get('lease_id', None),\n )\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef set_storage_container_acl(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Set a storage container's acl\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f set_storage_container my-azure name=mycontainer\n\n name:\n Name of existing container.\n signed_identifiers:\n SignedIdentifers instance\n blob_public_access:\n Optional. Possible values include: container, blob\n lease_id:\n If specified, set_storage_container_acl only succeeds if the\n container's lease is active and matches this ID.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The create_storage_container function must be called with -f or --function.'\n )\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n try:\n data = storage_conn.set_container_acl(\n container_name=kwargs['name'],\n signed_identifiers=kwargs.get('signed_identifiers', None),\n x_ms_blob_public_access=kwargs.get('blob_public_access', None),\n x_ms_lease_id=kwargs.get('lease_id', None),\n )\n return {'Success': 'The storage container was successfully updated'}\n except AzureConflictHttpError:\n raise SaltCloudSystemExit('There was a conflict.')", "response": "Set a storage container s acl"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef delete_storage_container(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Delete a container associated with the storage account\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f delete_storage_container my-azure name=mycontainer\n\n name:\n Name of container to create.\n fail_not_exist:\n Specify whether to throw an exception when the container exists.\n lease_id:\n If specified, delete_storage_container only succeeds if the\n container's lease is active and matches this ID.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The delete_storage_container function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('An storage container name must be specified as \"name\"')\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n data = storage_conn.delete_container(\n container_name=kwargs['name'],\n fail_not_exist=kwargs.get('fail_not_exist', None),\n x_ms_lease_id=kwargs.get('lease_id', None),\n )\n return data", "response": "Delete a storage container associated with the Azure Storage account."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef lease_storage_container(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Lease a container associated with the storage account\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f lease_storage_container my-azure name=mycontainer\n\n name:\n Name of container to create.\n lease_action:\n Required. Possible values: acquire|renew|release|break|change\n lease_id:\n Required if the container has an active lease.\n lease_duration:\n Specifies the duration of the lease, in seconds, or negative one\n (-1) for a lease that never expires. A non-infinite lease can be\n between 15 and 60 seconds. A lease duration cannot be changed\n using renew or change. For backwards compatibility, the default is\n 60, and the value is only used on an acquire operation.\n lease_break_period:\n Optional. For a break operation, this is the proposed duration of\n seconds that the lease should continue before it is broken, between\n 0 and 60 seconds. This break period is only used if it is shorter\n than the time remaining on the lease. If longer, the time remaining\n on the lease is used. A new lease will not be available before the\n break period has expired, but the lease may be held for longer than\n the break period. If this header does not appear with a break\n operation, a fixed-duration lease breaks after the remaining lease\n period elapses, and an infinite lease breaks immediately.\n proposed_lease_id:\n Optional for acquire, required for change. Proposed lease ID, in a\n GUID string format.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The lease_storage_container function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('An storage container name must be specified as \"name\"')\n\n lease_actions = ('acquire', 'renew', 'release', 'break', 'change')\n\n if kwargs.get('lease_action', None) not in lease_actions:\n raise SaltCloudSystemExit(\n 'A lease_action must be one of: {0}'.format(\n ', '.join(lease_actions)\n )\n )\n\n if kwargs['lease_action'] != 'acquire' and 'lease_id' not in kwargs:\n raise SaltCloudSystemExit(\n 'A lease ID must be specified for the \"{0}\" lease action '\n 'as \"lease_id\"'.format(kwargs['lease_action'])\n )\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n data = storage_conn.lease_container(\n container_name=kwargs['name'],\n x_ms_lease_action=kwargs['lease_action'],\n x_ms_lease_id=kwargs.get('lease_id', None),\n x_ms_lease_duration=kwargs.get('lease_duration', 60),\n x_ms_lease_break_period=kwargs.get('lease_break_period', None),\n x_ms_proposed_lease_id=kwargs.get('proposed_lease_id', None),\n )\n\n return data", "response": "Creates a new container in Azure storage account."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nlisting the blobs associated with a container.", "response": "def list_blobs(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n List blobs associated with the container\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f list_blobs my-azure container=mycontainer\n\n container:\n The name of the storage container\n prefix:\n Optional. Filters the results to return only blobs whose names\n begin with the specified prefix.\n marker:\n Optional. A string value that identifies the portion of the list\n to be returned with the next list operation. The operation returns\n a marker value within the response body if the list returned was\n not complete. The marker value may then be used in a subsequent\n call to request the next set of list items. The marker value is\n opaque to the client.\n maxresults:\n Optional. Specifies the maximum number of blobs to return,\n including all BlobPrefix elements. If the request does not specify\n maxresults or specifies a value greater than 5,000, the server will\n return up to 5,000 items. Setting maxresults to a value less than\n or equal to zero results in error response code 400 (Bad Request).\n include:\n Optional. Specifies one or more datasets to include in the\n response. To specify more than one of these options on the URI,\n you must separate each option with a comma. Valid values are:\n\n snapshots:\n Specifies that snapshots should be included in the\n enumeration. Snapshots are listed from oldest to newest in\n the response.\n metadata:\n Specifies that blob metadata be returned in the response.\n uncommittedblobs:\n Specifies that blobs for which blocks have been uploaded,\n but which have not been committed using Put Block List\n (REST API), be included in the response.\n copy:\n Version 2012-02-12 and newer. Specifies that metadata\n related to any current or previous Copy Blob operation\n should be included in the response.\n delimiter:\n Optional. When the request includes this parameter, the operation\n returns a BlobPrefix element in the response body that acts as a\n placeholder for all blobs whose names begin with the same\n substring up to the appearance of the delimiter character. The\n delimiter may be a single character or a string.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The list_blobs function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'container' not in kwargs:\n raise SaltCloudSystemExit('An storage container name must be specified as \"container\"')\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n return salt.utils.msazure.list_blobs(storage_conn=storage_conn, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nshow a blob s service properties", "response": "def show_blob_service_properties(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Show a blob's service properties\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_blob_service_properties my-azure\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_blob_service_properties function must be called with -f or --function.'\n )\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n data = storage_conn.get_blob_service_properties(\n timeout=kwargs.get('timeout', None),\n )\n return data"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_blob_service_properties(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Sets the properties of a storage account's Blob service, including\n Windows Azure Storage Analytics. You can also use this operation to\n set the default request version for all incoming requests that do not\n have a version specified.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f set_blob_service_properties my-azure\n\n properties:\n a StorageServiceProperties object.\n timeout:\n Optional. The timeout parameter is expressed in seconds.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The set_blob_service_properties function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'properties' not in kwargs:\n raise SaltCloudSystemExit('The blob service properties name must be specified as \"properties\"')\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n data = storage_conn.get_blob_service_properties(\n storage_service_properties=kwargs['properties'],\n timeout=kwargs.get('timeout', None),\n )\n return data", "response": "Set the properties of a storage account s Blob service including Windows Azure Storage Analytics."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nshow the properties of a blob in Azure.", "response": "def show_blob_properties(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Returns all user-defined metadata, standard HTTP properties, and\n system properties for the blob.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f show_blob_properties my-azure container=mycontainer blob=myblob\n\n container:\n Name of existing container.\n blob:\n Name of existing blob.\n lease_id:\n Required if the blob has an active lease.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The show_blob_properties function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'container' not in kwargs:\n raise SaltCloudSystemExit('The container name must be specified as \"container\"')\n\n if 'blob' not in kwargs:\n raise SaltCloudSystemExit('The blob name must be specified as \"blob\"')\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n try:\n data = storage_conn.get_blob_properties(\n container_name=kwargs['container'],\n blob_name=kwargs['blob'],\n x_ms_lease_id=kwargs.get('lease_id', None),\n )\n except AzureMissingResourceHttpError:\n raise SaltCloudSystemExit('The specified blob does not exist.')\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsetting a blob s properties", "response": "def set_blob_properties(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Set a blob's properties\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-cloud -f set_blob_properties my-azure\n\n container:\n Name of existing container.\n blob:\n Name of existing blob.\n blob_cache_control:\n Optional. Modifies the cache control string for the blob.\n blob_content_type:\n Optional. Sets the blob's content type.\n blob_content_md5:\n Optional. Sets the blob's MD5 hash.\n blob_content_encoding:\n Optional. Sets the blob's content encoding.\n blob_content_language:\n Optional. Sets the blob's content language.\n lease_id:\n Required if the blob has an active lease.\n blob_content_disposition:\n Optional. Sets the blob's Content-Disposition header.\n The Content-Disposition response header field conveys additional\n information about how to process the response payload, and also can\n be used to attach additional metadata. For example, if set to\n attachment, it indicates that the user-agent should not display the\n response, but instead show a Save As dialog with a filename other\n than the blob name specified.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The set_blob_properties function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'container' not in kwargs:\n raise SaltCloudSystemExit('The blob container name must be specified as \"container\"')\n\n if 'blob' not in kwargs:\n raise SaltCloudSystemExit('The blob name must be specified as \"blob\"')\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n data = storage_conn.get_blob_properties(\n container_name=kwargs['container'],\n blob_name=kwargs['blob'],\n x_ms_blob_cache_control=kwargs.get('blob_cache_control', None),\n x_ms_blob_content_type=kwargs.get('blob_content_type', None),\n x_ms_blob_content_md5=kwargs.get('blob_content_md5', None),\n x_ms_blob_content_encoding=kwargs.get('blob_content_encoding', None),\n x_ms_blob_content_language=kwargs.get('blob_content_language', None),\n x_ms_lease_id=kwargs.get('lease_id', None),\n x_ms_blob_content_disposition=kwargs.get('blob_content_disposition', None),\n )\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef put_blob(kwargs=None, storage_conn=None, call=None):\n '''\n .. versionadded:: 2015.8.0\n\n Upload a blob\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt-cloud -f put_blob my-azure container=base name=top.sls blob_path=/srv/salt/top.sls\n salt-cloud -f put_blob my-azure container=base name=content.txt blob_content='Some content'\n\n container:\n Name of existing container.\n name:\n Name of existing blob.\n blob_path:\n The path on the local machine of the file to upload as a blob. Either\n this or blob_content must be specified.\n blob_content:\n The actual content to be uploaded as a blob. Either this or blob_path\n must me specified.\n cache_control:\n Optional. The Blob service stores this value but does not use or\n modify it.\n content_language:\n Optional. Specifies the natural languages used by this resource.\n content_md5:\n Optional. An MD5 hash of the blob content. This hash is used to\n verify the integrity of the blob during transport. When this header\n is specified, the storage service checks the hash that has arrived\n with the one that was sent. If the two hashes do not match, the\n operation will fail with error code 400 (Bad Request).\n blob_content_type:\n Optional. Set the blob's content type.\n blob_content_encoding:\n Optional. Set the blob's content encoding.\n blob_content_language:\n Optional. Set the blob's content language.\n blob_content_md5:\n Optional. Set the blob's MD5 hash.\n blob_cache_control:\n Optional. Sets the blob's cache control.\n meta_name_values:\n A dict containing name, value for metadata.\n lease_id:\n Required if the blob has an active lease.\n '''\n if call != 'function':\n raise SaltCloudSystemExit(\n 'The put_blob function must be called with -f or --function.'\n )\n\n if kwargs is None:\n kwargs = {}\n\n if 'container' not in kwargs:\n raise SaltCloudSystemExit('The blob container name must be specified as \"container\"')\n\n if 'name' not in kwargs:\n raise SaltCloudSystemExit('The blob name must be specified as \"name\"')\n\n if 'blob_path' not in kwargs and 'blob_content' not in kwargs:\n raise SaltCloudSystemExit(\n 'Either a path to a file needs to be passed in as \"blob_path\" or '\n 'the contents of a blob as \"blob_content.\"'\n )\n\n if not storage_conn:\n storage_conn = get_storage_conn(conn_kwargs=kwargs)\n\n return salt.utils.msazure.put_blob(storage_conn=storage_conn, **kwargs)", "response": "Uploads a file to Azure Blob service."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nperforms a query directly against the Azure REST API", "response": "def query(path, method='GET', data=None, params=None, header_dict=None, decode=True):\n '''\n Perform a query directly against the Azure REST API\n '''\n certificate_path = config.get_cloud_config_value(\n 'certificate_path',\n get_configured_provider(), __opts__, search_global=False\n )\n subscription_id = salt.utils.stringutils.to_str(\n config.get_cloud_config_value(\n 'subscription_id',\n get_configured_provider(), __opts__, search_global=False\n )\n )\n management_host = config.get_cloud_config_value(\n 'management_host',\n get_configured_provider(),\n __opts__,\n search_global=False,\n default='management.core.windows.net'\n )\n backend = config.get_cloud_config_value(\n 'backend',\n get_configured_provider(), __opts__, search_global=False\n )\n url = 'https://{management_host}/{subscription_id}/{path}'.format(\n management_host=management_host,\n subscription_id=subscription_id,\n path=path,\n )\n\n if header_dict is None:\n header_dict = {}\n\n header_dict['x-ms-version'] = '2014-06-01'\n\n result = salt.utils.http.query(\n url,\n method=method,\n params=params,\n data=data,\n header_dict=header_dict,\n port=443,\n text=True,\n cert=certificate_path,\n backend=backend,\n decode=decode,\n decode_type='xml',\n )\n if 'dict' in result:\n return result['dict']\n return"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef start(name):\n '''\n Starts service via daemontools\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' daemontools.start <service name>\n '''\n __salt__['file.remove']('{0}/down'.format(_service_path(name)))\n cmd = 'svc -u {0}'.format(_service_path(name))\n return not __salt__['cmd.retcode'](cmd, python_shell=False)", "response": "Starts a daemon service via daemontools daemontools. start"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef stop(name):\n '''\n Stops service via daemontools\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' daemontools.stop <service name>\n '''\n __salt__['file.touch']('{0}/down'.format(_service_path(name)))\n cmd = 'svc -d {0}'.format(_service_path(name))\n return not __salt__['cmd.retcode'](cmd, python_shell=False)", "response": "Stops the specified service via daemontools stop"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsends a TERM to service via daemontools daemontools. term", "response": "def term(name):\n '''\n Send a TERM to service via daemontools\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' daemontools.term <service name>\n '''\n cmd = 'svc -t {0}'.format(_service_path(name))\n return not __salt__['cmd.retcode'](cmd, python_shell=False)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the status of a service via daemontools", "response": "def status(name, sig=None):\n '''\n Return the status for a service via daemontools, return pid if running\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' daemontools.status <service name>\n '''\n cmd = 'svstat {0}'.format(_service_path(name))\n out = __salt__['cmd.run_stdout'](cmd, python_shell=False)\n try:\n pid = re.search(r'\\(pid (\\d+)\\)', out).group(1)\n except AttributeError:\n pid = ''\n return pid"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nreturning True if the named service is enabled False otherwise", "response": "def enabled(name, **kwargs):\n '''\n Return True if the named service is enabled, false otherwise\n A service is considered enabled if in your service directory:\n - an executable ./run file exist\n - a file named \"down\" does not exist\n\n .. versionadded:: 2015.5.7\n\n name\n Service name\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' daemontools.enabled <service name>\n '''\n if not available(name):\n log.error('Service %s not found', name)\n return False\n\n run_file = os.path.join(SERVICE_DIR, name, 'run')\n down_file = os.path.join(SERVICE_DIR, name, 'down')\n\n return (\n os.path.isfile(run_file) and\n os.access(run_file, os.X_OK) and not\n os.path.isfile(down_file)\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_keys(hive, key=None, use_32bit_registry=False):\n '''\n Enumerates the subkeys in a registry key or hive.\n\n Args:\n\n hive (str):\n The name of the hive. Can be one of the following:\n\n - HKEY_LOCAL_MACHINE or HKLM\n - HKEY_CURRENT_USER or HKCU\n - HKEY_USER or HKU\n - HKEY_CLASSES_ROOT or HKCR\n - HKEY_CURRENT_CONFIG or HKCC\n\n key (str):\n The key (looks like a path) to the value name. If a key is not\n passed, the keys under the hive will be returned.\n\n use_32bit_registry (bool):\n Accesses the 32bit portion of the registry on 64 bit installations.\n On 32bit machines this is ignored.\n\n Returns:\n list: A list of keys/subkeys under the hive or key.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' reg.list_keys HKLM 'SOFTWARE'\n '''\n return __utils__['reg.list_keys'](hive=hive,\n key=key,\n use_32bit_registry=use_32bit_registry)", "response": "List the keys under a given hive or key."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_values(hive, key=None, use_32bit_registry=False, include_default=True):\n r'''\n Enumerates the values in a registry key or hive.\n\n Args:\n\n hive (str):\n The name of the hive. Can be one of the following:\n\n - HKEY_LOCAL_MACHINE or HKLM\n - HKEY_CURRENT_USER or HKCU\n - HKEY_USER or HKU\n - HKEY_CLASSES_ROOT or HKCR\n - HKEY_CURRENT_CONFIG or HKCC\n\n key (str):\n The key (looks like a path) to the value name. If a key is not\n passed, the values under the hive will be returned.\n\n use_32bit_registry (bool):\n Accesses the 32bit portion of the registry on 64 bit installations.\n On 32bit machines this is ignored.\n\n include_default (bool):\n Toggle whether to include the '(Default)' value.\n\n Returns:\n list: A list of values under the hive or key.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' reg.list_values HKLM 'SYSTEM\\\\CurrentControlSet\\\\Services\\\\Tcpip'\n '''\n return __utils__['reg.list_values'](hive=hive,\n key=key,\n use_32bit_registry=use_32bit_registry,\n include_default=include_default)", "response": "r Returns a list of values under the specified registry key or hive."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef read_value(hive, key, vname=None, use_32bit_registry=False):\n r'''\n Reads a registry value entry or the default value for a key. To read the\n default value, don't pass ``vname``\n\n Args:\n\n hive (str): The name of the hive. Can be one of the following:\n\n - HKEY_LOCAL_MACHINE or HKLM\n - HKEY_CURRENT_USER or HKCU\n - HKEY_USER or HKU\n - HKEY_CLASSES_ROOT or HKCR\n - HKEY_CURRENT_CONFIG or HKCC\n\n key (str):\n The key (looks like a path) to the value name.\n\n vname (str):\n The value name. These are the individual name/data pairs under the\n key. If not passed, the key (Default) value will be returned.\n\n use_32bit_registry (bool):\n Accesses the 32bit portion of the registry on 64bit installations.\n On 32bit machines this is ignored.\n\n Returns:\n dict: A dictionary containing the passed settings as well as the\n value_data if successful. If unsuccessful, sets success to False.\n\n bool: Returns False if the key is not found\n\n If vname is not passed:\n\n - Returns the first unnamed value (Default) as a string.\n - Returns none if first unnamed value is empty.\n\n CLI Example:\n\n The following will get the value of the ``version`` value name in the\n ``HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Salt`` key\n\n .. code-block:: bash\n\n salt '*' reg.read_value HKEY_LOCAL_MACHINE 'SOFTWARE\\Salt' 'version'\n\n CLI Example:\n\n The following will get the default value of the\n ``HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Salt`` key\n\n .. code-block:: bash\n\n salt '*' reg.read_value HKEY_LOCAL_MACHINE 'SOFTWARE\\Salt'\n '''\n return __utils__['reg.read_value'](hive=hive,\n key=key,\n vname=vname,\n use_32bit_registry=use_32bit_registry)", "response": "r Reads a value from the registry entry or the default value for a key."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset a value in the KeyReservedModule registry.", "response": "def set_value(hive,\n key,\n vname=None,\n vdata=None,\n vtype='REG_SZ',\n use_32bit_registry=False,\n volatile=False):\n '''\n Sets a value in the registry. If ``vname`` is passed, it will be the value\n for that value name, otherwise it will be the default value for the\n specified key\n\n Args:\n\n hive (str):\n The name of the hive. Can be one of the following\n\n - HKEY_LOCAL_MACHINE or HKLM\n - HKEY_CURRENT_USER or HKCU\n - HKEY_USER or HKU\n - HKEY_CLASSES_ROOT or HKCR\n - HKEY_CURRENT_CONFIG or HKCC\n\n key (str):\n The key (looks like a path) to the value name.\n\n vname (str):\n The value name. These are the individual name/data pairs under the\n key. If not passed, the key (Default) value will be set.\n\n vdata (str, int, list, bytes):\n The value you'd like to set. If a value name (vname) is passed, this\n will be the data for that value name. If not, this will be the\n (Default) value for the key.\n\n The type of data this parameter expects is determined by the value\n type specified in ``vtype``. The correspondence is as follows:\n\n - REG_BINARY: Binary data (str in Py2, bytes in Py3)\n - REG_DWORD: int\n - REG_EXPAND_SZ: str\n - REG_MULTI_SZ: list of str\n - REG_QWORD: int\n - REG_SZ: str\n\n .. note::\n When setting REG_BINARY, string data will be converted to\n binary.\n\n .. note::\n The type for the (Default) value is always REG_SZ and cannot be\n changed.\n\n .. note::\n This parameter is optional. If ``vdata`` is not passed, the Key\n will be created with no associated item/value pairs.\n\n vtype (str):\n The value type. The possible values of the vtype parameter are\n indicated above in the description of the vdata parameter.\n\n use_32bit_registry (bool):\n Sets the 32bit portion of the registry on 64bit installations. On\n 32bit machines this is ignored.\n\n volatile (bool):\n When this parameter has a value of True, the registry key will be\n made volatile (i.e. it will not persist beyond a system reset or\n shutdown). This parameter only has an effect when a key is being\n created and at no other time.\n\n Returns:\n bool: True if successful, otherwise False\n\n CLI Example:\n\n This will set the version value to 2015.5.2 in the SOFTWARE\\\\Salt key in\n the HKEY_LOCAL_MACHINE hive\n\n .. code-block:: bash\n\n salt '*' reg.set_value HKEY_LOCAL_MACHINE 'SOFTWARE\\\\Salt' 'version' '2015.5.2'\n\n CLI Example:\n\n This function is strict about the type of vdata. For instance this\n example will fail because vtype has a value of REG_SZ and vdata has a\n type of int (as opposed to str as expected).\n\n .. code-block:: bash\n\n salt '*' reg.set_value HKEY_LOCAL_MACHINE 'SOFTWARE\\\\Salt' 'str_data' 1.2\n\n CLI Example:\n\n In this next example vdata is properly quoted and should succeed.\n\n .. code-block:: bash\n\n salt '*' reg.set_value HKEY_LOCAL_MACHINE 'SOFTWARE\\\\Salt' 'str_data' vtype=REG_SZ vdata=\"'1.2'\"\n\n CLI Example:\n\n This is an example of using vtype REG_BINARY.\n\n .. code-block:: bash\n\n salt '*' reg.set_value HKEY_LOCAL_MACHINE 'SOFTWARE\\\\Salt' 'bin_data' vtype=REG_BINARY vdata='Salty Data'\n\n CLI Example:\n\n An example of using vtype REG_MULTI_SZ is as follows:\n\n .. code-block:: bash\n\n salt '*' reg.set_value HKEY_LOCAL_MACHINE 'SOFTWARE\\\\Salt' 'list_data' vtype=REG_MULTI_SZ vdata='[\"Salt\", \"is\", \"great\"]'\n '''\n return __utils__['reg.set_value'](hive=hive,\n key=key,\n vname=vname,\n vdata=vdata,\n vtype=vtype,\n use_32bit_registry=use_32bit_registry,\n volatile=volatile)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef delete_key_recursive(hive, key, use_32bit_registry=False):\n r'''\n .. versionadded:: 2015.5.4\n\n Delete a registry key to include all subkeys and value/data pairs.\n\n Args:\n\n hive (str):\n The name of the hive. Can be one of the following\n\n - HKEY_LOCAL_MACHINE or HKLM\n - HKEY_CURRENT_USER or HKCU\n - HKEY_USER or HKU\n - HKEY_CLASSES_ROOT or HKCR\n - HKEY_CURRENT_CONFIG or HKCC\n\n key (str):\n The key to remove (looks like a path)\n\n use_32bit_registry (bool):\n Deletes the 32bit portion of the registry on 64bit\n installations. On 32bit machines this is ignored.\n\n Returns:\n dict: A dictionary listing the keys that deleted successfully as well as\n those that failed to delete.\n\n CLI Example:\n\n The following example will remove ``delete_me`` and all its subkeys from the\n ``SOFTWARE`` key in ``HKEY_LOCAL_MACHINE``:\n\n .. code-block:: bash\n\n salt '*' reg.delete_key_recursive HKLM SOFTWARE\\\\delete_me\n '''\n return __utils__['reg.delete_key_recursive'](hive=hive,\n key=key,\n use_32bit_registry=use_32bit_registry)", "response": "Delete a key in a given hive and all its subkeys and value pairs."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nimport the registry settings from a Windows REG file.", "response": "def import_file(source, use_32bit_registry=False):\n '''\n Import registry settings from a Windows ``REG`` file by invoking ``REG.EXE``.\n\n .. versionadded:: 2018.3.0\n\n Args:\n\n source (str):\n The full path of the ``REG`` file. This can be either a local file\n path or a URL type supported by salt (e.g. ``salt://salt_master_path``)\n\n use_32bit_registry (bool):\n If the value of this parameter is ``True`` then the ``REG`` file\n will be imported into the Windows 32 bit registry. Otherwise the\n Windows 64 bit registry will be used.\n\n Returns:\n bool: True if successful, otherwise an error is raised\n\n Raises:\n ValueError: If the value of ``source`` is an invalid path or otherwise\n causes ``cp.cache_file`` to return ``False``\n CommandExecutionError: If ``reg.exe`` exits with a non-0 exit code\n\n CLI Example:\n\n .. code-block:: bash\n\n salt machine1 reg.import_file salt://win/printer_config/110_Canon/postinstall_config.reg\n\n '''\n cache_path = __salt__['cp.cache_file'](source)\n if not cache_path:\n error_msg = \"File/URL '{0}' probably invalid.\".format(source)\n raise ValueError(error_msg)\n if use_32bit_registry:\n word_sz_txt = \"32\"\n else:\n word_sz_txt = \"64\"\n cmd = 'reg import \"{0}\" /reg:{1}'.format(cache_path, word_sz_txt)\n cmd_ret_dict = __salt__['cmd.run_all'](cmd, python_shell=True)\n retcode = cmd_ret_dict['retcode']\n if retcode != 0:\n raise CommandExecutionError(\n 'reg.exe import failed',\n info=cmd_ret_dict\n )\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nexecutes a command and read the output as YAML and return the result as a dictionary.", "response": "def ext_pillar(minion_id,\n pillar, # pylint: disable=W0613\n use_grain=False,\n minion_ids=None,\n tag_match_key=None,\n tag_match_value='asis',\n tag_list_key=None,\n tag_list_sep=';'):\n '''\n Execute a command and read the output as YAML\n '''\n valid_tag_match_value = ['uqdn', 'asis']\n\n # meta-data:instance-id\n grain_instance_id = __grains__.get('meta-data', {}).get('instance-id', None)\n if not grain_instance_id:\n # dynamic:instance-identity:document:instanceId\n grain_instance_id = \\\n __grains__.get('dynamic', {}).get('instance-identity', {}).get('document', {}).get('instance-id', None)\n if grain_instance_id and re.search(r'^i-([0-9a-z]{17}|[0-9a-z]{8})$', grain_instance_id) is None:\n log.error('External pillar %s, instance-id \\'%s\\' is not valid for '\n '\\'%s\\'', __name__, grain_instance_id, minion_id)\n grain_instance_id = None # invalid instance id found, remove it from use.\n\n # Check AWS Tag restrictions .i.e. letters, spaces, and numbers and + - = . _ : / @\n if tag_match_key and re.match(r'[\\w=.:/@-]+$', tag_match_key) is None:\n log.error('External pillar %s, tag_match_key \\'%s\\' is not valid ',\n __name__, tag_match_key if isinstance(tag_match_key, six.text_type) else 'non-string')\n return {}\n\n if tag_match_key and tag_match_value not in valid_tag_match_value:\n log.error('External pillar %s, tag_value \\'%s\\' is not valid must be one '\n 'of %s', __name__, tag_match_value, ' '.join(valid_tag_match_value))\n return {}\n\n if not tag_match_key:\n base_msg = ('External pillar %s, querying EC2 tags for minion id \\'%s\\' '\n 'against instance-id', __name__, minion_id)\n else:\n base_msg = ('External pillar %s, querying EC2 tags for minion id \\'%s\\' '\n 'against instance-id or \\'%s\\' against \\'%s\\'', __name__, minion_id, tag_match_key, tag_match_value)\n\n log.debug(base_msg)\n find_filter = None\n find_id = None\n\n if re.search(r'^i-([0-9a-z]{17}|[0-9a-z]{8})$', minion_id) is not None:\n find_filter = None\n find_id = minion_id\n elif tag_match_key:\n if tag_match_value == 'uqdn':\n find_filter = {'tag:{0}'.format(tag_match_key): minion_id.split('.', 1)[0]}\n else:\n find_filter = {'tag:{0}'.format(tag_match_key): minion_id}\n if grain_instance_id:\n # we have an untrusted grain_instance_id, use it to narrow the search\n # even more. Combination will be unique even if uqdn is set.\n find_filter.update({'instance-id': grain_instance_id})\n # Add this if running state is not dependant on EC2Config\n # find_filter.update('instance-state-name': 'running')\n\n # no minion-id is instance-id and no suitable filter, try use_grain if enabled\n if not find_filter and not find_id and use_grain:\n if not grain_instance_id:\n log.debug('Minion-id is not in AWS instance-id formation, and there '\n 'is no instance-id grain for minion %s', minion_id)\n return {}\n if minion_ids is not None and minion_id not in minion_ids:\n log.debug('Minion-id is not in AWS instance ID format, and minion_ids '\n 'is set in the ec2_pillar configuration, but minion %s is '\n 'not in the list of allowed minions %s', minion_id, minion_ids)\n return {}\n find_id = grain_instance_id\n\n if not (find_filter or find_id):\n log.debug('External pillar %s, querying EC2 tags for minion id \\'%s\\' against '\n 'instance-id or \\'%s\\' against \\'%s\\' noughthing to match against',\n __name__, minion_id, tag_match_key, tag_match_value)\n return {}\n\n myself = boto.utils.get_instance_metadata(timeout=0.1, num_retries=1)\n if not myself:\n log.info(\"%s: salt master not an EC2 instance, skipping\", __name__)\n return {}\n\n # Get the Master's instance info, primarily the region\n (_, region) = _get_instance_info()\n\n # If the Minion's region is available, use it instead\n if use_grain:\n region = __grains__.get('ec2', {}).get('region', region)\n\n try:\n conn = boto.ec2.connect_to_region(region)\n except boto.exception.AWSConnectionError as exc:\n log.error('%s: invalid AWS credentials, %s', __name__, exc)\n return {}\n\n if conn is None:\n log.error('%s: Could not connect to region %s', __name__, region)\n return {}\n\n try:\n if find_id:\n instance_data = conn.get_only_instances(instance_ids=[find_id], dry_run=False)\n else:\n # filters and max_results can not be used togther.\n instance_data = conn.get_only_instances(filters=find_filter, dry_run=False)\n\n except boto.exception.EC2ResponseError as exc:\n log.error('%s failed with \\'%s\\'', base_msg, exc)\n return {}\n\n if not instance_data:\n log.debug('%s no match using \\'%s\\'', base_msg, find_id if find_id else find_filter)\n return {}\n\n # Find a active instance, i.e. ignore terminated and stopped instances\n active_inst = []\n for inst in range(0, len(instance_data)):\n if instance_data[inst].state not in ['terminated', 'stopped']:\n active_inst.append(inst)\n\n valid_inst = len(active_inst)\n if not valid_inst:\n log.debug('%s match found but not active \\'%s\\'', base_msg, find_id if find_id else find_filter)\n return {}\n\n if valid_inst > 1:\n log.error('%s multiple matches, ignored, using \\'%s\\'', base_msg, find_id if find_id else find_filter)\n return {}\n\n instance = instance_data[active_inst[0]]\n if instance.tags:\n ec2_tags = instance.tags\n ec2_tags_list = {}\n log.debug('External pillar %s, for minion id \\'%s\\', tags: %s', __name__, minion_id, instance.tags)\n if tag_list_key and isinstance(tag_list_key, list):\n for item in tag_list_key:\n if item in ec2_tags:\n ec2_tags_list[item] = ec2_tags[item].split(tag_list_sep)\n del ec2_tags[item] # make sure its only in ec2_tags_list\n else:\n ec2_tags_list[item] = [] # always return a result\n\n return {'ec2_tags': ec2_tags, 'ec2_tags_list': ec2_tags_list}\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef ext_pillar(minion_id, pillar, conf):\n '''\n Return an existing set of certificates\n '''\n cache = salt.cache.Cache(__opts__, syspaths.CACHE_DIR)\n\n ret = {}\n dns_names = cache.fetch('venafi/minions', minion_id)\n for dns_name in dns_names:\n data = cache.fetch('venafi/domains', dns_name)\n ret[dns_name] = data\n del ret[dns_name]['csr']\n return {'venafi': ret}", "response": "Return an existing set of certificates\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the status of a service via s6", "response": "def status(name, sig=None):\n '''\n Return the status for a service via s6, return pid if running\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' s6.status <service name>\n '''\n cmd = 's6-svstat {0}'.format(_service_path(name))\n out = __salt__['cmd.run_stdout'](cmd)\n try:\n pid = re.search(r'up \\(pid (\\d+)\\)', out).group(1)\n except AttributeError:\n pid = ''\n return pid"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of all available services", "response": "def get_all():\n '''\n Return a list of all available services\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' s6.get_all\n '''\n if not SERVICE_DIR:\n raise CommandExecutionError(\"Could not find service directory.\")\n service_list = [dirname for dirname\n in os.listdir(SERVICE_DIR)\n if not dirname.startswith('.')]\n return sorted(service_list)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef add_host(mac, name=None, ip=None, ddns=False, group=None,\n supersede_host=False):\n '''\n Add a host object for the given mac.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt dhcp-server omapi.add_host ab:ab:ab:ab:ab:ab name=host1\n\n Add ddns-hostname and a fixed-ip statements:\n\n .. code-block:: bash\n\n salt dhcp-server omapi.add_host ab:ab:ab:ab:ab:ab name=host1 ip=10.1.1.1 ddns=true\n '''\n statements = ''\n o = _conn()\n msg = omapi.OmapiMessage.open(b'host')\n msg.message.append((b'create', struct.pack(b'!I', 1)))\n msg.message.append((b'exclusive', struct.pack(b'!I', 1)))\n msg.obj.append((b'hardware-address', omapi.pack_mac(mac)))\n msg.obj.append((b'hardware-type', struct.pack(b'!I', 1)))\n if ip:\n msg.obj.append((b'ip-address', omapi.pack_ip(ip)))\n if name:\n msg.obj.append((b'name', salt.utils.stringutils.to_bytes(name)))\n if group:\n msg.obj.append((b'group', salt.utils.stringutils.to_bytes(group)))\n if supersede_host:\n statements += 'option host-name \"{0}\"; '.format(name)\n if ddns and name:\n statements += 'ddns-hostname \"{0}\"; '.format(name)\n if statements:\n msg.obj.append((b'statements', salt.utils.stringutils.to_bytes(statements)))\n response = o.query_server(msg)\n if response.opcode != omapi.OMAPI_OP_UPDATE:\n return False\n return True", "response": "Adds a host object for the given mac."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ndelete the host with the given mac or name.", "response": "def delete_host(mac=None, name=None):\n '''\n Delete the host with the given mac or name.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt dhcp-server omapi.delete_host name=host1\n salt dhcp-server omapi.delete_host mac=ab:ab:ab:ab:ab:ab\n '''\n if not (mac or name):\n raise TypeError('At least one argument is required')\n o = _conn()\n msg = omapi.OmapiMessage.open(b'host')\n if mac:\n msg.obj.append((b'hardware-address', omapi.pack_mac(mac)))\n msg.obj.append((b'hardware-type', struct.pack(b'!I', 1)))\n if name:\n msg.obj.append((b'name', salt.utils.stringutils.to_bytes(name)))\n response = o.query_server(msg)\n if response.opcode != omapi.OMAPI_OP_UPDATE:\n return None\n if response.handle == 0:\n return False\n response = o.query_server(omapi.OmapiMessage.delete(response.handle))\n if response.opcode != omapi.OMAPI_OP_STATUS:\n return False\n return True"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the auth object", "response": "def _auth():\n '''\n Return the auth object\n '''\n if 'auth' not in __context__:\n try:\n __context__['auth'] = salt.crypt.SAuth(__opts__)\n except SaltClientError:\n log.error('Could not authenticate with master.'\n 'Mine data will not be transmitted.')\n return __context__['auth']"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nexecuting the configured functions and send the data back up to the master.", "response": "def update(clear=False, mine_functions=None):\n '''\n Execute the configured functions and send the data back up to the master.\n The functions to be executed are merged from the master config, pillar and\n minion config under the option `mine_functions`:\n\n .. code-block:: yaml\n\n mine_functions:\n network.ip_addrs:\n - eth0\n disk.usage: []\n\n This function accepts the following arguments:\n\n clear: False\n Boolean flag specifying whether updating will clear the existing\n mines, or will update. Default: `False` (update).\n\n mine_functions\n Update the mine data on certain functions only.\n This feature can be used when updating the mine for functions\n that require refresh at different intervals than the rest of\n the functions specified under `mine_functions` in the\n minion/master config or pillar.\n A potential use would be together with the `scheduler`, for example:\n\n .. code-block:: yaml\n\n schedule:\n lldp_mine_update:\n function: mine.update\n kwargs:\n mine_functions:\n net.lldp: []\n hours: 12\n\n In the example above, the mine for `net.lldp` would be refreshed\n every 12 hours, while `network.ip_addrs` would continue to be updated\n as specified in `mine_interval`.\n\n The function cache will be populated with information from executing these\n functions\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mine.update\n '''\n m_data = {}\n if not mine_functions:\n m_data = __salt__['config.merge']('mine_functions', {})\n # If we don't have any mine functions configured, then we should just bail out\n if not m_data:\n return\n elif mine_functions and isinstance(mine_functions, list):\n m_data = dict((fun, {}) for fun in mine_functions)\n elif mine_functions and isinstance(mine_functions, dict):\n m_data = mine_functions\n else:\n return\n\n data = {}\n for func in m_data:\n try:\n if m_data[func] and isinstance(m_data[func], dict):\n mine_func = m_data[func].pop('mine_function', func)\n if not _mine_function_available(mine_func):\n continue\n data[func] = __salt__[mine_func](**m_data[func])\n elif m_data[func] and isinstance(m_data[func], list):\n mine_func = func\n if isinstance(m_data[func][0], dict) and 'mine_function' in m_data[func][0]:\n mine_func = m_data[func][0]['mine_function']\n m_data[func].pop(0)\n\n if not _mine_function_available(mine_func):\n continue\n data[func] = __salt__[mine_func](*m_data[func])\n else:\n if not _mine_function_available(func):\n continue\n data[func] = __salt__[func]()\n except Exception:\n trace = traceback.format_exc()\n log.error('Function %s in mine_functions failed to execute', func)\n log.debug('Error: %s', trace)\n continue\n if __opts__['file_client'] == 'local':\n if not clear:\n old = __salt__['data.get']('mine_cache')\n if isinstance(old, dict):\n old.update(data)\n data = old\n return __salt__['data.update']('mine_cache', data)\n load = {\n 'cmd': '_mine',\n 'data': data,\n 'id': __opts__['id'],\n 'clear': clear,\n }\n return _mine_send(load, __opts__)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef send(func, *args, **kwargs):\n '''\n Send a specific function to the mine.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mine.send network.ip_addrs eth0\n salt '*' mine.send eth0_ip_addrs mine_function=network.ip_addrs eth0\n '''\n kwargs = salt.utils.args.clean_kwargs(**kwargs)\n mine_func = kwargs.pop('mine_function', func)\n if mine_func not in __salt__:\n return False\n data = {}\n arg_data = salt.utils.args.arg_lookup(__salt__[mine_func])\n func_data = copy.deepcopy(kwargs)\n for ind, _ in enumerate(arg_data.get('args', [])):\n try:\n func_data[arg_data['args'][ind]] = args[ind]\n except IndexError:\n # Safe error, arg may be in kwargs\n pass\n f_call = salt.utils.args.format_call(\n __salt__[mine_func],\n func_data,\n expected_extra_kws=MINE_INTERNAL_KEYWORDS)\n for arg in args:\n if arg not in f_call['args']:\n f_call['args'].append(arg)\n try:\n if 'kwargs' in f_call:\n data[func] = __salt__[mine_func](*f_call['args'], **f_call['kwargs'])\n else:\n data[func] = __salt__[mine_func](*f_call['args'])\n except Exception as exc:\n log.error('Function %s in mine.send failed to execute: %s',\n mine_func, exc)\n return False\n if __opts__['file_client'] == 'local':\n old = __salt__['data.get']('mine_cache')\n if isinstance(old, dict):\n old.update(data)\n data = old\n return __salt__['data.update']('mine_cache', data)\n load = {\n 'cmd': '_mine',\n 'data': data,\n 'id': __opts__['id'],\n }\n return _mine_send(load, __opts__)", "response": "Send a specific function to the mine."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get(tgt,\n fun,\n tgt_type='glob',\n exclude_minion=False):\n '''\n Get data from the mine based on the target, function and tgt_type\n\n Targets can be matched based on any standard matching system that can be\n matched on the master via these keywords:\n\n - glob\n - pcre\n - grain\n - grain_pcre\n - compound\n - pillar\n - pillar_pcre\n\n Note that all pillar matches, whether using the compound matching system or\n the pillar matching system, will be exact matches, with globbing disabled.\n\n exclude_minion\n Excludes the current minion from the result set\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mine.get '*' network.interfaces\n salt '*' mine.get '*' network.interfaces,network.ipaddrs\n salt '*' mine.get '*' '[\"network.interfaces\", \"network.ipaddrs\"]'\n salt '*' mine.get 'os:Fedora' network.interfaces grain\n salt '*' mine.get 'G@os:Fedora and S@192.168.5.0/24' network.ipaddrs compound\n\n .. seealso:: Retrieving Mine data from Pillar and Orchestrate\n\n This execution module is intended to be executed on minions.\n Master-side operations such as Pillar or Orchestrate that require Mine\n data should use the :py:mod:`Mine Runner module <salt.runners.mine>`\n instead; it can be invoked from a Pillar SLS file using the\n :py:func:`saltutil.runner <salt.modules.saltutil.runner>` module. For\n example:\n\n .. code-block:: jinja\n\n {% set minion_ips = salt.saltutil.runner('mine.get',\n tgt='*',\n fun='network.ip_addrs',\n tgt_type='glob') %}\n '''\n if __opts__['file_client'] == 'local':\n ret = {}\n is_target = {'glob': __salt__['match.glob'],\n 'pcre': __salt__['match.pcre'],\n 'list': __salt__['match.list'],\n 'grain': __salt__['match.grain'],\n 'grain_pcre': __salt__['match.grain_pcre'],\n 'ipcidr': __salt__['match.ipcidr'],\n 'compound': __salt__['match.compound'],\n 'pillar': __salt__['match.pillar'],\n 'pillar_pcre': __salt__['match.pillar_pcre'],\n }[tgt_type](tgt)\n if is_target:\n data = __salt__['data.get']('mine_cache')\n\n if isinstance(data, dict):\n if isinstance(fun, six.string_types):\n functions = list(set(fun.split(',')))\n _ret_dict = len(functions) > 1\n elif isinstance(fun, list):\n functions = fun\n _ret_dict = True\n else:\n return {}\n\n if not _ret_dict and functions and functions[0] in data:\n ret[__opts__['id']] = data.get(functions)\n elif _ret_dict:\n for fun in functions:\n if fun in data:\n ret.setdefault(fun, {})[__opts__['id']] = data.get(fun)\n\n return ret\n load = {\n 'cmd': '_mine_get',\n 'id': __opts__['id'],\n 'tgt': tgt,\n 'fun': fun,\n 'tgt_type': tgt_type,\n }\n ret = _mine_get(load, __opts__)\n if exclude_minion:\n if __opts__['id'] in ret:\n del ret[__opts__['id']]\n return ret", "response": "Return the data from the master based on the target function and tgt_type"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nremoves specific function contents of minion. Returns True on success.", "response": "def delete(fun):\n '''\n Remove specific function contents of minion. Returns True on success.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mine.delete 'network.interfaces'\n '''\n if __opts__['file_client'] == 'local':\n data = __salt__['data.get']('mine_cache')\n if isinstance(data, dict) and fun in data:\n del data[fun]\n return __salt__['data.update']('mine_cache', data)\n load = {\n 'cmd': '_mine_delete',\n 'id': __opts__['id'],\n 'fun': fun,\n }\n return _mine_send(load, __opts__)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets all docker info for a given set of network interfaces and subnet addresses.", "response": "def get_docker(interfaces=None, cidrs=None, with_container_id=False):\n '''\n .. versionchanged:: 2017.7.8,2018.3.3\n When :conf_minion:`docker.update_mine` is set to ``False`` for a given\n minion, no mine data will be populated for that minion, and thus none\n will be returned for it.\n .. versionchanged:: 2019.2.0\n :conf_minion:`docker.update_mine` now defaults to ``False``\n\n Get all mine data for :py:func:`docker.ps <salt.modules.dockermod.ps_>` and\n run an aggregation routine. The ``interfaces`` parameter allows for\n specifying the network interfaces from which to select IP addresses. The\n ``cidrs`` parameter allows for specifying a list of subnets which the IP\n address must match.\n\n with_container_id\n Boolean, to expose container_id in the list of results\n\n .. versionadded:: 2015.8.2\n\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mine.get_docker\n salt '*' mine.get_docker interfaces='eth0'\n salt '*' mine.get_docker interfaces='[\"eth0\", \"eth1\"]'\n salt '*' mine.get_docker cidrs='107.170.147.0/24'\n salt '*' mine.get_docker cidrs='[\"107.170.147.0/24\", \"172.17.42.0/24\"]'\n salt '*' mine.get_docker interfaces='[\"eth0\", \"eth1\"]' cidrs='[\"107.170.147.0/24\", \"172.17.42.0/24\"]'\n '''\n # Enforce that interface and cidr are lists\n if interfaces:\n interface_ = []\n interface_.extend(interfaces if isinstance(interfaces, list) else [interfaces])\n interfaces = interface_\n if cidrs:\n cidr_ = []\n cidr_.extend(cidrs if isinstance(cidrs, list) else [cidrs])\n cidrs = cidr_\n\n # Get docker info\n cmd = 'docker.ps'\n docker_hosts = get('*', cmd)\n\n proxy_lists = {}\n\n # Process docker info\n for containers in six.itervalues(docker_hosts):\n host = containers.pop('host')\n host_ips = []\n\n # Prepare host_ips list\n if not interfaces:\n for info in six.itervalues(host['interfaces']):\n if 'inet' in info:\n for ip_ in info['inet']:\n host_ips.append(ip_['address'])\n else:\n for interface in interfaces:\n if interface in host['interfaces']:\n if 'inet' in host['interfaces'][interface]:\n for item in host['interfaces'][interface]['inet']:\n host_ips.append(item['address'])\n host_ips = list(set(host_ips))\n\n # Filter out ips from host_ips with cidrs\n if cidrs:\n good_ips = []\n for cidr in cidrs:\n for ip_ in host_ips:\n if salt.utils.network.in_subnet(cidr, [ip_]):\n good_ips.append(ip_)\n host_ips = list(set(good_ips))\n\n # Process each container\n for container in six.itervalues(containers):\n container_id = container['Info']['Id']\n if container['Image'] not in proxy_lists:\n proxy_lists[container['Image']] = {}\n for dock_port in container['Ports']:\n # IP exists only if port is exposed\n ip_address = dock_port.get('IP')\n # If port is 0.0.0.0, then we must get the docker host IP\n if ip_address == '0.0.0.0':\n for ip_ in host_ips:\n containers = proxy_lists[container['Image']].setdefault('ipv4', {}).setdefault(dock_port['PrivatePort'], [])\n container_network_footprint = '{0}:{1}'.format(ip_, dock_port['PublicPort'])\n if with_container_id:\n value = (container_network_footprint, container_id)\n else:\n value = container_network_footprint\n if value not in containers:\n containers.append(value)\n elif ip_address:\n containers = proxy_lists[container['Image']].setdefault('ipv4', {}).setdefault(dock_port['PrivatePort'], [])\n container_network_footprint = '{0}:{1}'.format(dock_port['IP'], dock_port['PublicPort'])\n if with_container_id:\n value = (container_network_footprint, container_id)\n else:\n value = container_network_footprint\n if value not in containers:\n containers.append(value)\n\n return proxy_lists"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef valid():\n '''\n List valid entries in mine configuration.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mine.valid\n '''\n m_data = __salt__['config.merge']('mine_functions', {})\n # If we don't have any mine functions configured, then we should just bail out\n if not m_data:\n return\n\n data = {}\n for func in m_data:\n if m_data[func] and isinstance(m_data[func], dict):\n mine_func = m_data[func].pop('mine_function', func)\n if not _mine_function_available(mine_func):\n continue\n data[func] = {mine_func: m_data[func]}\n elif m_data[func] and isinstance(m_data[func], list):\n mine_func = func\n if isinstance(m_data[func][0], dict) and 'mine_function' in m_data[func][0]:\n mine_func = m_data[func][0]['mine_function']\n m_data[func].pop(0)\n\n if not _mine_function_available(mine_func):\n continue\n data[func] = {mine_func: m_data[func]}\n else:\n if not _mine_function_available(func):\n continue\n data[func] = m_data[func]\n\n return data", "response": "List valid entries in mine configuration."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef returner(ret):\n '''\n Return data to a Pg server\n '''\n try:\n with _get_serv(ret, commit=True) as cur:\n sql = '''INSERT INTO salt_returns\n (fun, jid, return, id, success, full_ret, alter_time)\n VALUES (%s, %s, %s, %s, %s, %s, to_timestamp(%s))'''\n\n cur.execute(sql, (ret['fun'], ret['jid'],\n psycopg2.extras.Json(ret['return']),\n ret['id'],\n ret.get('success', False),\n psycopg2.extras.Json(ret),\n time.time()))\n except salt.exceptions.SaltMasterError:\n log.critical('Could not store return with pgjsonb returner. PostgreSQL server unavailable.')", "response": "Return data to a Pg server\n ArcGIS server"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef event_return(events):\n '''\n Return event to Pg server\n\n Requires that configuration be enabled via 'event_return'\n option in master config.\n '''\n with _get_serv(events, commit=True) as cur:\n for event in events:\n tag = event.get('tag', '')\n data = event.get('data', '')\n sql = '''INSERT INTO salt_events (tag, data, master_id, alter_time)\n VALUES (%s, %s, %s, to_timestamp(%s))'''\n cur.execute(sql, (tag, psycopg2.extras.Json(data),\n __opts__['id'], time.time()))", "response": "Return event to Pg server"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef save_load(jid, load, minions=None):\n '''\n Save the load to the specified jid id\n '''\n with _get_serv(commit=True) as cur:\n try:\n cur.execute(PG_SAVE_LOAD_SQL,\n {'jid': jid, 'load': psycopg2.extras.Json(load)})\n except psycopg2.IntegrityError:\n # https://github.com/saltstack/salt/issues/22171\n # Without this try/except we get tons of duplicate entry errors\n # which result in job returns not being stored properly\n pass", "response": "Save the load to the specified jid id\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_load(jid):\n '''\n Return the load data that marks a specified jid\n '''\n with _get_serv(ret=None, commit=True) as cur:\n\n sql = '''SELECT load FROM jids WHERE jid = %s;'''\n cur.execute(sql, (jid,))\n data = cur.fetchone()\n if data:\n return data[0]\n return {}", "response": "Return the load data that marks a specified jid\n Return the load data that marks a specified jid\n Return the load data that marks a specified jid\n Return the load data that marks a specified jid\n Return the empty dict if no load data is found"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_fun(fun):\n '''\n Return a dict of the last function called for all minions\n '''\n with _get_serv(ret=None, commit=True) as cur:\n\n sql = '''SELECT s.id,s.jid, s.full_ret\n FROM salt_returns s\n JOIN ( SELECT MAX(`jid`) as jid\n from salt_returns GROUP BY fun, id) max\n ON s.jid = max.jid\n WHERE s.fun = %s\n '''\n\n cur.execute(sql, (fun,))\n data = cur.fetchall()\n\n ret = {}\n if data:\n for minion, _, full_ret in data:\n ret[minion] = full_ret\n return ret", "response": "Return a dict of the last function called for all minions\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\npurge jobs older than this date.", "response": "def _purge_jobs(timestamp):\n '''\n Purge records from the returner tables.\n :param job_age_in_seconds: Purge jobs older than this\n :return:\n '''\n with _get_serv() as cursor:\n try:\n sql = 'delete from jids where jid in (select distinct jid from salt_returns where alter_time < %s)'\n cursor.execute(sql, (timestamp,))\n cursor.execute('COMMIT')\n except psycopg2.DatabaseError as err:\n error = err.args\n sys.stderr.write(six.text_type(error))\n cursor.execute(\"ROLLBACK\")\n raise err\n\n try:\n sql = 'delete from salt_returns where alter_time < %s'\n cursor.execute(sql, (timestamp,))\n cursor.execute('COMMIT')\n except psycopg2.DatabaseError as err:\n error = err.args\n sys.stderr.write(six.text_type(error))\n cursor.execute(\"ROLLBACK\")\n raise err\n\n try:\n sql = 'delete from salt_events where alter_time < %s'\n cursor.execute(sql, (timestamp,))\n cursor.execute('COMMIT')\n except psycopg2.DatabaseError as err:\n error = err.args\n sys.stderr.write(six.text_type(error))\n cursor.execute(\"ROLLBACK\")\n raise err\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\narchive jobs from the backup tables.", "response": "def _archive_jobs(timestamp):\n '''\n Copy rows to a set of backup tables, then purge rows.\n :param timestamp: Archive rows older than this timestamp\n :return:\n '''\n source_tables = ['jids',\n 'salt_returns',\n 'salt_events']\n\n with _get_serv() as cursor:\n target_tables = {}\n for table_name in source_tables:\n try:\n tmp_table_name = table_name + '_archive'\n sql = 'create table IF NOT exists {0} (LIKE {1})'.format(tmp_table_name, table_name)\n cursor.execute(sql)\n cursor.execute('COMMIT')\n target_tables[table_name] = tmp_table_name\n except psycopg2.DatabaseError as err:\n error = err.args\n sys.stderr.write(six.text_type(error))\n cursor.execute(\"ROLLBACK\")\n raise err\n\n try:\n sql = 'insert into {0} select * from {1} where jid in (select distinct jid from salt_returns where alter_time < %s)'.format(target_tables['jids'], 'jids')\n cursor.execute(sql, (timestamp,))\n cursor.execute('COMMIT')\n except psycopg2.DatabaseError as err:\n error = err.args\n sys.stderr.write(six.text_type(error))\n cursor.execute(\"ROLLBACK\")\n raise err\n except Exception as e:\n log.error(e)\n raise\n\n try:\n sql = 'insert into {0} select * from {1} where alter_time < %s'.format(target_tables['salt_returns'], 'salt_returns')\n cursor.execute(sql, (timestamp,))\n cursor.execute('COMMIT')\n except psycopg2.DatabaseError as err:\n error = err.args\n sys.stderr.write(six.text_type(error))\n cursor.execute(\"ROLLBACK\")\n raise err\n\n try:\n sql = 'insert into {0} select * from {1} where alter_time < %s'.format(target_tables['salt_events'], 'salt_events')\n cursor.execute(sql, (timestamp,))\n cursor.execute('COMMIT')\n except psycopg2.DatabaseError as err:\n error = err.args\n sys.stderr.write(six.text_type(error))\n cursor.execute(\"ROLLBACK\")\n raise err\n\n return _purge_jobs(timestamp)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncall in the master s event loop every loop_interval.", "response": "def clean_old_jobs():\n '''\n Called in the master's event loop every loop_interval. Archives and/or\n deletes the events and job details from the database.\n :return:\n '''\n if __opts__.get('keep_jobs', False) and int(__opts__.get('keep_jobs', 0)) > 0:\n try:\n with _get_serv() as cur:\n sql = \"select (NOW() - interval '{0}' hour) as stamp;\".format(__opts__['keep_jobs'])\n cur.execute(sql)\n rows = cur.fetchall()\n stamp = rows[0][0]\n\n if __opts__.get('archive_jobs', False):\n _archive_jobs(stamp)\n else:\n _purge_jobs(stamp)\n except Exception as e:\n log.error(e)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn information about the CPU.", "response": "def cpustats():\n '''\n Return information about the CPU.\n\n Returns\n dict: A dictionary containing information about the CPU stats\n\n CLI Example:\n\n .. code-block:: bash\n\n salt * status.cpustats\n '''\n # Tries to gather information similar to that returned by a Linux machine\n # Avoid using WMI as there's a lot of overhead\n\n # Time related info\n user, system, idle, interrupt, dpc = psutil.cpu_times()\n cpu = {'user': user,\n 'system': system,\n 'idle': idle,\n 'irq': interrupt,\n 'dpc': dpc}\n # Count related info\n ctx_switches, interrupts, soft_interrupts, sys_calls = psutil.cpu_stats()\n intr = {'irqs': {'irqs': [],\n 'total': interrupts}}\n soft_irq = {'softirqs': [],\n 'total': soft_interrupts}\n return {'btime': psutil.boot_time(),\n 'cpu': cpu,\n 'ctxt': ctx_switches,\n 'intr': intr,\n 'processes': len(psutil.pids()),\n 'softirq': soft_irq,\n 'syscalls': sys_calls}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef meminfo():\n '''\n Return information about physical and virtual memory on the system\n\n Returns:\n dict: A dictionary of information about memory on the system\n\n CLI Example:\n\n .. code-block:: bash\n\n salt * status.meminfo\n '''\n # Get physical memory\n vm_total, vm_available, vm_percent, vm_used, vm_free = psutil.virtual_memory()\n # Get swap memory\n swp_total, swp_used, swp_free, swp_percent, _, _ = psutil.swap_memory()\n\n def get_unit_value(memory):\n symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')\n prefix = {}\n for i, s in enumerate(symbols):\n prefix[s] = 1 << (i + 1) * 10\n for s in reversed(symbols):\n if memory >= prefix[s]:\n value = float(memory) / prefix[s]\n return {'unit': s,\n 'value': value}\n return {'unit': 'B',\n 'value': memory}\n\n return {'VmallocTotal': get_unit_value(vm_total),\n 'VmallocUsed': get_unit_value(vm_used),\n 'VmallocFree': get_unit_value(vm_free),\n 'VmallocAvail': get_unit_value(vm_available),\n 'SwapTotal': get_unit_value(swp_total),\n 'SwapUsed': get_unit_value(swp_used),\n 'SwapFree': get_unit_value(swp_free)}", "response": "Return information about physical and virtual memory on the system\notope"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef vmstats():\n '''\n Return information about the virtual memory on the machine\n\n Returns:\n dict: A dictionary of virtual memory stats\n\n CLI Example:\n\n .. code-block:: bash\n\n salt * status.vmstats\n '''\n # Setup the SPI Structure\n spi = SYSTEM_PERFORMANCE_INFORMATION()\n retlen = ctypes.c_ulong()\n\n # 2 means to query System Performance Information and return it in a\n # SYSTEM_PERFORMANCE_INFORMATION Structure\n ctypes.windll.ntdll.NtQuerySystemInformation(\n 2, ctypes.byref(spi), ctypes.sizeof(spi), ctypes.byref(retlen))\n\n # Return each defined field in a dict\n ret = {}\n for field in spi._fields_:\n ret.update({field[0]: getattr(spi, field[0])})\n\n return ret", "response": "Return information about the virtual memory on the machine"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn the disk usage for this minion", "response": "def diskusage(human_readable=False, path=None):\n '''\n .. versionadded:: 2015.8.0\n\n Return the disk usage for this minion\n\n human_readable : False\n If ``True``, usage will be in KB/MB/GB etc.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.diskusage path=c:/salt\n '''\n if not path:\n path = 'c:/'\n\n disk_stats = psutil.disk_usage(path)\n\n total_val = disk_stats.total\n used_val = disk_stats.used\n free_val = disk_stats.free\n percent = disk_stats.percent\n\n if human_readable:\n total_val = _byte_calc(total_val)\n used_val = _byte_calc(used_val)\n free_val = _byte_calc(free_val)\n\n return {'total': total_val,\n 'used': used_val,\n 'free': free_val,\n 'percent': percent}"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the process data for all processes in the system", "response": "def procs(count=False):\n '''\n Return the process data\n\n count : False\n If ``True``, this function will simply return the number of processes.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.procs\n salt '*' status.procs count\n '''\n with salt.utils.winapi.Com():\n wmi_obj = wmi.WMI()\n processes = wmi_obj.win32_process()\n\n #this short circuit's the function to get a short simple proc count.\n if count:\n return len(processes)\n\n #a propper run of the function, creating a nonsensically long out put.\n process_info = {}\n for proc in processes:\n process_info[proc.ProcessId] = _get_process_info(proc)\n\n return process_info"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef saltmem(human_readable=False):\n '''\n .. versionadded:: 2015.8.0\n\n Returns the amount of memory that salt is using\n\n human_readable : False\n return the value in a nicely formatted number\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.saltmem\n salt '*' status.saltmem human_readable=True\n '''\n # psutil.Process defaults to current process (`os.getpid()`)\n p = psutil.Process()\n\n # Use oneshot to get a snapshot\n with p.oneshot():\n mem = p.memory_info().rss\n\n if human_readable:\n return _byte_calc(mem)\n\n return mem", "response": "Return the amount of memory that salt is using"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef uptime(human_readable=False):\n '''\n .. versionadded:: 2015.8.0\n\n Return the system uptime for the machine\n\n Args:\n\n human_readable (bool):\n Return uptime in human readable format if ``True``, otherwise\n return seconds. Default is ``False``\n\n .. note::\n Human readable format is ``days, hours:min:sec``. Days will only\n be displayed if more than 0\n\n Returns:\n str:\n The uptime in seconds or human readable format depending on the\n value of ``human_readable``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.uptime\n salt '*' status.uptime human_readable=True\n '''\n # Get startup time\n startup_time = datetime.datetime.fromtimestamp(psutil.boot_time())\n\n # Subtract startup time from current time to get the uptime of the system\n uptime = datetime.datetime.now() - startup_time\n\n return six.text_type(uptime) if human_readable else uptime.total_seconds()", "response": "Return the system uptime for the specified resource."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning process information as a dict", "response": "def _get_process_info(proc):\n '''\n Return process information\n '''\n cmd = salt.utils.stringutils.to_unicode(proc.CommandLine or '')\n name = salt.utils.stringutils.to_unicode(proc.Name)\n info = dict(\n cmd=cmd,\n name=name,\n **_get_process_owner(proc)\n )\n return info"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a new master entry for the current state of the master.", "response": "def master(master=None, connected=True):\n '''\n .. versionadded:: 2015.5.0\n\n Fire an event if the minion gets disconnected from its master. This\n function is meant to be run via a scheduled job from the minion. If\n master_ip is an FQDN/Hostname, is must be resolvable to a valid IPv4\n address.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' status.master\n '''\n\n def _win_remotes_on(port):\n '''\n Windows specific helper function.\n Returns set of ipv4 host addresses of remote established connections\n on local or remote tcp port.\n\n Parses output of shell 'netstat' to get connections\n\n PS C:> netstat -n -p TCP\n\n Active Connections\n\n Proto Local Address Foreign Address State\n TCP 10.1.1.26:3389 10.1.1.1:4505 ESTABLISHED\n TCP 10.1.1.26:56862 10.1.1.10:49155 TIME_WAIT\n TCP 10.1.1.26:56868 169.254.169.254:80 CLOSE_WAIT\n TCP 127.0.0.1:49197 127.0.0.1:49198 ESTABLISHED\n TCP 127.0.0.1:49198 127.0.0.1:49197 ESTABLISHED\n '''\n remotes = set()\n try:\n data = subprocess.check_output(['netstat', '-n', '-p', 'TCP']) # pylint: disable=minimum-python-version\n except subprocess.CalledProcessError:\n log.error('Failed netstat')\n raise\n\n lines = salt.utils.stringutils.to_unicode(data).split('\\n')\n for line in lines:\n if 'ESTABLISHED' not in line:\n continue\n chunks = line.split()\n remote_host, remote_port = chunks[2].rsplit(':', 1)\n if int(remote_port) != port:\n continue\n remotes.add(remote_host)\n return remotes\n\n # the default publishing port\n port = 4505\n master_ips = None\n\n if master:\n master_ips = _host_to_ips(master)\n\n if not master_ips:\n return\n\n if __salt__['config.get']('publish_port') != '':\n port = int(__salt__['config.get']('publish_port'))\n\n master_connection_status = False\n connected_ips = _win_remotes_on(port)\n\n # Get connection status for master\n for master_ip in master_ips:\n if master_ip in connected_ips:\n master_connection_status = True\n break\n\n # Connection to master is not as expected\n if master_connection_status is not connected:\n event = salt.utils.event.get_event('minion', opts=__opts__, listen=False)\n if master_connection_status:\n event.fire_event({'master': master}, salt.minion.master_event(type='connected'))\n else:\n event.fire_event({'master': master}, salt.minion.master_event(type='disconnected'))\n\n return master_connection_status"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _check_config_exists(config_file=None):\n '''\n Verify the config file is present\n '''\n if config_file is None:\n config_file = _config_file()\n if not os.path.isfile(config_file):\n return False\n return True", "response": "Verify the config file is present and is a valid resource."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef is_jail(name):\n '''\n Return True if jail exists False if not\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' poudriere.is_jail <jail name>\n '''\n jails = list_jails()\n for jail in jails:\n if jail.split()[0] == name:\n return True\n return False", "response": "Check if a jail exists"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmake pkgng aware version of the current hierarchy", "response": "def make_pkgng_aware(jname):\n '''\n Make jail ``jname`` pkgng aware\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' poudriere.make_pkgng_aware <jail name>\n '''\n ret = {'changes': {}}\n cdir = _config_dir()\n # ensure cdir is there\n if not os.path.isdir(cdir):\n os.makedirs(cdir)\n if os.path.isdir(cdir):\n ret['changes'] = 'Created poudriere make file dir {0}'.format(cdir)\n else:\n return 'Could not create or find required directory {0}'.format(\n cdir)\n\n # Added args to file\n __salt__['file.write']('{0}-make.conf'.format(os.path.join(cdir, jname)), 'WITH_PKGNG=yes')\n\n if os.path.isfile(os.path.join(cdir, jname) + '-make.conf'):\n ret['changes'] = 'Created {0}'.format(\n os.path.join(cdir, '{0}-make.conf'.format(jname))\n )\n return ret\n else:\n return 'Looks like file {0} could not be created'.format(\n os.path.join(cdir, jname + '-make.conf')\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef parse_config(config_file=None):\n '''\n Returns a dict of poudriere main configuration definitions\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' poudriere.parse_config\n '''\n if config_file is None:\n config_file = _config_file()\n ret = {}\n if _check_config_exists(config_file):\n with salt.utils.files.fopen(config_file) as ifile:\n for line in ifile:\n key, val = salt.utils.stringutils.to_unicode(line).split('=')\n ret[key] = val\n return ret\n\n return 'Could not find {0} on file system'.format(config_file)", "response": "Parses the poudriere main configuration file"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a new poudriere jail", "response": "def create_jail(name, arch, version=\"9.0-RELEASE\"):\n '''\n Creates a new poudriere jail if one does not exist\n\n *NOTE* creating a new jail will take some time the master is not hanging\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' poudriere.create_jail 90amd64 amd64\n '''\n # Config file must be on system to create a poudriere jail\n _check_config_exists()\n\n # Check if the jail is there\n if is_jail(name):\n return '{0} already exists'.format(name)\n\n cmd = 'poudriere jails -c -j {0} -v {1} -a {2}'.format(name, version, arch)\n __salt__['cmd.run'](cmd)\n\n # Make jail pkgng aware\n make_pkgng_aware(name)\n\n # Make sure the jail was created\n if is_jail(name):\n return 'Created jail {0}'.format(name)\n\n return 'Issue creating jail {0}'.format(name)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef update_jail(name):\n '''\n Run freebsd-update on `name` poudriere jail\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' poudriere.update_jail freebsd:10:x86:64\n '''\n if is_jail(name):\n cmd = 'poudriere jail -u -j {0}'.format(name)\n ret = __salt__['cmd.run'](cmd)\n return ret\n else:\n return 'Could not find jail {0}'.format(name)", "response": "Update the freebsd - update on the specified jail"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef delete_jail(name):\n '''\n Deletes poudriere jail with `name`\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' poudriere.delete_jail 90amd64\n '''\n if is_jail(name):\n cmd = 'poudriere jail -d -j {0}'.format(name)\n __salt__['cmd.run'](cmd)\n\n # Make sure jail is gone\n if is_jail(name):\n return 'Looks like there was an issue deleteing jail \\\n {0}'.format(name)\n else:\n # Could not find jail.\n return 'Looks like jail {0} has not been created'.format(name)\n\n # clean up pkgng make info in config dir\n make_file = os.path.join(_config_dir(), '{0}-make.conf'.format(name))\n if os.path.isfile(make_file):\n try:\n os.remove(make_file)\n except (IOError, OSError):\n return ('Deleted jail \"{0}\" but was unable to remove jail make '\n 'file').format(name)\n __salt__['file.remove'](make_file)\n\n return 'Deleted jail {0}'.format(name)", "response": "Deletes poudriere jail with name"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nupdating the ports tree either the default or the ports_tree specified", "response": "def update_ports_tree(ports_tree):\n '''\n Updates the ports tree, either the default or the `ports_tree` specified\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' poudriere.update_ports_tree staging\n '''\n _check_config_exists()\n if ports_tree:\n cmd = 'poudriere ports -u -p {0}'.format(ports_tree)\n else:\n cmd = 'poudriere ports -u'\n ret = __salt__['cmd.run'](cmd)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef bulk_build(jail, pkg_file, keep=False):\n '''\n Run bulk build on poudriere server.\n\n Return number of pkg builds, failures, and errors, on error dump to CLI\n\n CLI Example:\n\n .. code-block:: bash\n\n salt -N buildbox_group poudriere.bulk_build 90amd64 /root/pkg_list\n\n '''\n # make sure `pkg file` and jail is on file system\n if not os.path.isfile(pkg_file):\n return 'Could not find file {0} on filesystem'.format(pkg_file)\n if not is_jail(jail):\n return 'Could not find jail {0}'.format(jail)\n\n # Generate command\n if keep:\n cmd = 'poudriere bulk -k -f {0} -j {1}'.format(pkg_file, jail)\n else:\n cmd = 'poudriere bulk -f {0} -j {1}'.format(pkg_file, jail)\n\n # Bulk build this can take some time, depending on pkg_file ... hours\n res = __salt__['cmd.run'](cmd)\n lines = res.splitlines()\n for line in lines:\n if \"packages built\" in line:\n return line\n return ('There may have been an issue building packages dumping output: '\n '{0}').format(res)", "response": "Run bulk build on poudriere server."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _update_nilrt_restart_state():\n '''\n NILRT systems determine whether to reboot after various package operations\n including but not limited to kernel module installs/removals by checking\n specific file md5sums & timestamps. These files are touched/modified by\n the post-install/post-remove functions of their respective packages.\n\n The opkg module uses this function to store/update those file timestamps\n and checksums to be used later by the restartcheck module.\n\n '''\n __salt__['cmd.shell']('stat -c %Y /lib/modules/$(uname -r)/modules.dep >{0}/modules.dep.timestamp'\n .format(NILRT_RESTARTCHECK_STATE_PATH))\n __salt__['cmd.shell']('md5sum /lib/modules/$(uname -r)/modules.dep >{0}/modules.dep.md5sum'\n .format(NILRT_RESTARTCHECK_STATE_PATH))\n\n # We can't assume nisysapi.ini always exists like modules.dep\n nisysapi_path = '/usr/local/natinst/share/nisysapi.ini'\n if os.path.exists(nisysapi_path):\n __salt__['cmd.shell']('stat -c %Y {0} >{1}/nisysapi.ini.timestamp'\n .format(nisysapi_path, NILRT_RESTARTCHECK_STATE_PATH))\n __salt__['cmd.shell']('md5sum {0} >{1}/nisysapi.ini.md5sum'\n .format(nisysapi_path, NILRT_RESTARTCHECK_STATE_PATH))\n\n # Expert plugin files get added to a conf.d dir, so keep track of the total\n # no. of files, their timestamps and content hashes\n nisysapi_conf_d_path = \"/usr/lib/{0}/nisysapi/conf.d/experts/\".format(\n 'arm-linux-gnueabi' if 'arm' in __grains__.get('cpuarch') else 'x86_64-linux-gnu'\n )\n\n if os.path.exists(nisysapi_conf_d_path):\n with salt.utils.files.fopen('{0}/sysapi.conf.d.count'.format(\n NILRT_RESTARTCHECK_STATE_PATH), 'w') as fcount:\n fcount.write(str(len(os.listdir(nisysapi_conf_d_path))))\n\n for fexpert in os.listdir(nisysapi_conf_d_path):\n __salt__['cmd.shell']('stat -c %Y {0}/{1} >{2}/{1}.timestamp'\n .format(nisysapi_conf_d_path,\n fexpert,\n NILRT_RESTARTCHECK_STATE_PATH))\n __salt__['cmd.shell']('md5sum {0}/{1} >{2}/{1}.md5sum'\n .format(nisysapi_conf_d_path,\n fexpert,\n NILRT_RESTARTCHECK_STATE_PATH))", "response": "Update the state of the NILRT restartcheck module."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_restartcheck_result(errors):\n '''\n Return restartcheck result and append errors (if any) to ``errors``\n '''\n rs_result = __salt__['restartcheck.restartcheck'](verbose=False)\n if isinstance(rs_result, dict) and 'comment' in rs_result:\n errors.append(rs_result['comment'])\n return rs_result", "response": "Return restartcheck result and append errors to errors"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nprocessing restartcheck output to see if system restarts were requested and take appropriate action.", "response": "def _process_restartcheck_result(rs_result, **kwargs):\n '''\n Check restartcheck output to see if system/service restarts were requested\n and take appropriate action.\n '''\n if 'No packages seem to need to be restarted' in rs_result:\n return\n reboot_required = False\n for rstr in rs_result:\n if 'System restart required' in rstr:\n _update_nilrt_restart_state()\n __salt__['system.set_reboot_required_witnessed']()\n reboot_required = True\n if kwargs.get('always_restart_services', True) or not reboot_required:\n for rstr in rs_result:\n if 'System restart required' not in rstr:\n service = os.path.join('/etc/init.d', rstr)\n if os.path.exists(service):\n __salt__['cmd.run']([service, 'restart'])"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nupdate the opkg database to latest packages based upon repositories", "response": "def refresh_db(failhard=False, **kwargs): # pylint: disable=unused-argument\n '''\n Updates the opkg database to latest packages based upon repositories\n\n Returns a dict, with the keys being package databases and the values being\n the result of the update attempt. Values can be one of the following:\n\n - ``True``: Database updated successfully\n - ``False``: Problem updating database\n\n failhard\n If False, return results of failed lines as ``False`` for the package\n database that encountered the error.\n If True, raise an error with a list of the package databases that\n encountered errors.\n\n .. versionadded:: 2018.3.0\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 ret = {}\n error_repos = []\n cmd = ['opkg', 'update']\n # opkg returns a non-zero retcode when there is a failure to refresh\n # from one or more repos. Due to this, ignore the retcode.\n call = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False,\n ignore_retcode=True,\n redirect_stderr=True)\n\n out = call['stdout']\n prev_line = ''\n for line in salt.utils.itertools.split(out, '\\n'):\n if 'Inflating' in line:\n key = line.strip().split()[1][:-1]\n ret[key] = True\n elif 'Updated source' in line:\n # Use the previous line.\n key = prev_line.strip().split()[1][:-1]\n ret[key] = True\n elif 'Failed to download' in line:\n key = line.strip().split()[5].split(',')[0]\n ret[key] = False\n error_repos.append(key)\n prev_line = line\n\n if failhard and error_repos:\n raise CommandExecutionError(\n 'Error getting repos: {0}'.format(', '.join(error_repos))\n )\n\n # On a non-zero exit code where no failed repos were found, raise an\n # exception because this appears to be a different kind of error.\n if call['retcode'] != 0 and not error_repos:\n raise CommandExecutionError(out)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _build_install_command_list(cmd_prefix, to_install, to_downgrade, to_reinstall):\n '''\n Builds a list of install commands to be executed in sequence in order to process\n each of the to_install, to_downgrade, and to_reinstall lists.\n '''\n cmds = []\n if to_install:\n cmd = copy.deepcopy(cmd_prefix)\n cmd.extend(to_install)\n cmds.append(cmd)\n if to_downgrade:\n cmd = copy.deepcopy(cmd_prefix)\n cmd.append('--force-downgrade')\n cmd.extend(to_downgrade)\n cmds.append(cmd)\n if to_reinstall:\n cmd = copy.deepcopy(cmd_prefix)\n cmd.append('--force-reinstall')\n cmd.extend(to_reinstall)\n cmds.append(cmd)\n\n return cmds", "response": "Builds a list of install commands to be executed in sequence in order to process\n each of the to_install to_downgrade and to_reinstall lists."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _parse_reported_packages_from_install_output(output):\n '''\n Parses the output of \"opkg install\" to determine what packages would have been\n installed by an operation run with the --noaction flag.\n\n We are looking for lines like:\n Installing <package> (<version>) on <target>\n or\n Upgrading <package> from <oldVersion> to <version> on root\n '''\n reported_pkgs = {}\n install_pattern = re.compile(r'Installing\\s(?P<package>.*?)\\s\\((?P<version>.*?)\\)\\son\\s(?P<target>.*?)')\n upgrade_pattern = re.compile(r'Upgrading\\s(?P<package>.*?)\\sfrom\\s(?P<oldVersion>.*?)\\sto\\s(?P<version>.*?)\\son\\s(?P<target>.*?)')\n for line in salt.utils.itertools.split(output, '\\n'):\n match = install_pattern.match(line)\n if match is None:\n match = upgrade_pattern.match(line)\n if match:\n reported_pkgs[match.group('package')] = match.group('version')\n\n return reported_pkgs", "response": "Parses the output of opkg install to determine what packages would have been installed on the root\n ."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _execute_install_command(cmd, parse_output, errors, parsed_packages):\n '''\n Executes a command for the install operation.\n If the command fails, its error output will be appended to the errors list.\n If the command succeeds and parse_output is true, updated packages will be appended\n to the parsed_packages dictionary.\n '''\n out = __salt__['cmd.run_all'](\n cmd,\n output_loglevel='trace',\n python_shell=False\n )\n if out['retcode'] != 0:\n if out['stderr']:\n errors.append(out['stderr'])\n else:\n errors.append(out['stdout'])\n elif parse_output:\n parsed_packages.update(_parse_reported_packages_from_install_output(out['stdout']))", "response": "Executes a command for the install operation."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ninstalling the passed package from the IPK database.", "response": "def install(name=None,\n refresh=False,\n pkgs=None,\n sources=None,\n reinstall=False,\n **kwargs):\n '''\n Install the passed package, add refresh=True to update the opkg database.\n\n name\n The name of the package to be installed. Note that this parameter is\n ignored if either \"pkgs\" or \"sources\" is passed. Additionally, please\n note that this option can only be used to install packages from a\n software repository. To install a package file manually, use the\n \"sources\" option.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install <package name>\n\n refresh\n Whether or not to refresh the package database before installing.\n\n version\n Install a specific version of the package, e.g. 1.2.3~0ubuntu0. Ignored\n if \"pkgs\" or \"sources\" is passed.\n\n .. versionadded:: 2017.7.0\n\n reinstall : False\n Specifying reinstall=True will use ``opkg install --force-reinstall``\n rather than simply ``opkg install`` for requested packages that are\n already installed.\n\n If a version is specified with the requested package, then ``opkg\n install --force-reinstall`` will only be used if the installed version\n matches the requested version.\n\n .. versionadded:: 2017.7.0\n\n\n Multiple Package Installation Options:\n\n pkgs\n A list of packages to install from a software repository. Must be\n passed as a python list.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install pkgs='[\"foo\", \"bar\"]'\n salt '*' pkg.install pkgs='[\"foo\", {\"bar\": \"1.2.3-0ubuntu0\"}]'\n\n sources\n A list of IPK packages to install. Must be passed as a list of dicts,\n with the keys being package names, and the values being the source URI\n or local path to the package. Dependencies are automatically resolved\n and marked as auto-installed.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install sources='[{\"foo\": \"salt://foo.deb\"},{\"bar\": \"salt://bar.deb\"}]'\n\n install_recommends\n Whether to install the packages marked as recommended. Default is True.\n\n only_upgrade\n Only upgrade the packages (disallow downgrades), if they are already\n installed. Default is False.\n\n .. versionadded:: 2017.7.0\n\n always_restart_services\n Whether to restart services even if a reboot is required. Default is True.\n\n Returns a dict containing the new package names and versions::\n\n {'<package>': {'old': '<old-version>',\n 'new': '<new-version>'}}\n '''\n refreshdb = salt.utils.data.is_true(refresh)\n\n try:\n pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](\n name, pkgs, sources, **kwargs\n )\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n old = list_pkgs()\n cmd_prefix = ['opkg', 'install']\n to_install = []\n to_reinstall = []\n to_downgrade = []\n\n _append_noaction_if_testmode(cmd_prefix, **kwargs)\n if not pkg_params:\n return {}\n elif pkg_type == 'file':\n if reinstall:\n cmd_prefix.append('--force-reinstall')\n if not kwargs.get('only_upgrade', False):\n cmd_prefix.append('--force-downgrade')\n to_install.extend(pkg_params)\n elif pkg_type == 'repository':\n if not kwargs.get('install_recommends', True):\n cmd_prefix.append('--no-install-recommends')\n for pkgname, pkgversion in six.iteritems(pkg_params):\n if (name and pkgs is None and kwargs.get('version') and\n len(pkg_params) == 1):\n # Only use the 'version' param if 'name' was not specified as a\n # comma-separated list\n version_num = kwargs['version']\n else:\n version_num = pkgversion\n\n if version_num is None:\n # Don't allow downgrades if the version\n # number is not specified.\n if reinstall and pkgname in old:\n to_reinstall.append(pkgname)\n else:\n to_install.append(pkgname)\n else:\n pkgstr = '{0}={1}'.format(pkgname, version_num)\n cver = old.get(pkgname, '')\n if reinstall and cver and salt.utils.versions.compare(\n ver1=version_num,\n oper='==',\n ver2=cver,\n cmp_func=version_cmp):\n to_reinstall.append(pkgstr)\n elif not cver or salt.utils.versions.compare(\n ver1=version_num,\n oper='>=',\n ver2=cver,\n cmp_func=version_cmp):\n to_install.append(pkgstr)\n else:\n if not kwargs.get('only_upgrade', False):\n to_downgrade.append(pkgstr)\n else:\n # This should cause the command to fail.\n to_install.append(pkgstr)\n\n cmds = _build_install_command_list(cmd_prefix, to_install, to_downgrade, to_reinstall)\n\n if not cmds:\n return {}\n\n if refreshdb:\n refresh_db()\n\n errors = []\n is_testmode = _is_testmode(**kwargs)\n test_packages = {}\n for cmd in cmds:\n _execute_install_command(cmd, is_testmode, errors, test_packages)\n\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n if is_testmode:\n new = copy.deepcopy(new)\n new.update(test_packages)\n\n ret = salt.utils.data.compare_dicts(old, new)\n\n if pkg_type == 'file' and reinstall:\n # For file-based packages, prepare 'to_reinstall' to have a list\n # of all the package names that may have been reinstalled.\n # This way, we could include reinstalled packages in 'ret'.\n for pkgfile in to_install:\n # Convert from file name to package name.\n cmd = ['opkg', 'info', pkgfile]\n out = __salt__['cmd.run_all'](\n cmd,\n output_loglevel='trace',\n python_shell=False\n )\n if out['retcode'] == 0:\n # Just need the package name.\n pkginfo_dict = _process_info_installed_output(\n out['stdout'], []\n )\n if pkginfo_dict:\n to_reinstall.append(list(pkginfo_dict.keys())[0])\n\n for pkgname in to_reinstall:\n if pkgname not in ret or pkgname in old:\n ret.update({pkgname: {'old': old.get(pkgname, ''),\n 'new': new.get(pkgname, '')}})\n\n rs_result = _get_restartcheck_result(errors)\n\n if errors:\n raise CommandExecutionError(\n 'Problem encountered installing package(s)',\n info={'errors': errors, 'changes': ret}\n )\n\n _process_restartcheck_result(rs_result, **kwargs)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _parse_reported_packages_from_remove_output(output):\n '''\n Parses the output of \"opkg remove\" to determine what packages would have been\n removed by an operation run with the --noaction flag.\n\n We are looking for lines like\n Removing <package> (<version>) from <Target>...\n '''\n reported_pkgs = {}\n remove_pattern = re.compile(r'Removing\\s(?P<package>.*?)\\s\\((?P<version>.*?)\\)\\sfrom\\s(?P<target>.*?)...')\n for line in salt.utils.itertools.split(output, '\\n'):\n match = remove_pattern.match(line)\n if match:\n reported_pkgs[match.group('package')] = ''\n\n return reported_pkgs", "response": "Parses the output of opkg remove to determine what packages would have been removed from the target."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nremoving packages using opkg remove.", "response": "def remove(name=None, pkgs=None, **kwargs): # pylint: disable=unused-argument\n '''\n Remove packages using ``opkg remove``.\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 remove_dependencies\n Remove package and all dependencies\n\n .. versionadded:: 2019.2.0\n\n auto_remove_deps\n Remove packages that were installed automatically to satisfy dependencies\n\n .. versionadded:: 2019.2.0\n\n Returns a dict containing the changes.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.remove <package name>\n salt '*' pkg.remove <package1>,<package2>,<package3>\n salt '*' pkg.remove pkgs='[\"foo\", \"bar\"]'\n salt '*' pkg.remove pkgs='[\"foo\", \"bar\"]' remove_dependencies=True auto_remove_deps=True\n '''\n try:\n pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[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 = ['opkg', 'remove']\n _append_noaction_if_testmode(cmd, **kwargs)\n if kwargs.get('remove_dependencies', False):\n cmd.append('--force-removal-of-dependent-packages')\n if kwargs.get('auto_remove_deps', False):\n cmd.append('--autoremove')\n cmd.extend(targets)\n\n out = __salt__['cmd.run_all'](\n cmd,\n output_loglevel='trace',\n python_shell=False\n )\n if out['retcode'] != 0:\n if out['stderr']:\n errors = [out['stderr']]\n else:\n errors = [out['stdout']]\n else:\n errors = []\n\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n if _is_testmode(**kwargs):\n reportedPkgs = _parse_reported_packages_from_remove_output(out['stdout'])\n new = {k: v for k, v in new.items() if k not in reportedPkgs}\n ret = salt.utils.data.compare_dicts(old, new)\n\n rs_result = _get_restartcheck_result(errors)\n\n if errors:\n raise CommandExecutionError(\n 'Problem encountered removing package(s)',\n info={'errors': errors, 'changes': ret}\n )\n\n _process_restartcheck_result(rs_result, **kwargs)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nupgrading all packages via opkg upgrade", "response": "def upgrade(refresh=True, **kwargs): # pylint: disable=unused-argument\n '''\n Upgrades all packages via ``opkg upgrade``\n\n Returns a dictionary containing the changes:\n\n .. code-block:: python\n\n {'<package>': {'old': '<old-version>',\n 'new': '<new-version>'}}\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 errors = []\n\n if salt.utils.data.is_true(refresh):\n refresh_db()\n\n old = list_pkgs()\n\n cmd = ['opkg', 'upgrade']\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 errors.append(result)\n\n rs_result = _get_restartcheck_result(errors)\n\n if errors:\n raise CommandExecutionError(\n 'Problem encountered upgrading packages',\n info={'errors': errors, 'changes': ret}\n )\n\n _process_restartcheck_result(rs_result, **kwargs)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef hold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W0613\n '''\n Set package in 'hold' state, meaning it will not be upgraded.\n\n name\n The name of the package, e.g., 'tmux'\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.hold <package name>\n\n pkgs\n A list of packages to hold. Must be passed as a python list.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.hold pkgs='[\"foo\", \"bar\"]'\n '''\n if not name and not pkgs and not sources:\n raise SaltInvocationError(\n 'One of name, pkgs, or sources must be specified.'\n )\n if pkgs and sources:\n raise SaltInvocationError(\n 'Only one of pkgs or sources can be specified.'\n )\n\n targets = []\n if pkgs:\n targets.extend(pkgs)\n elif sources:\n for source in sources:\n targets.append(next(iter(source)))\n else:\n targets.append(name)\n\n ret = {}\n for target in targets:\n if isinstance(target, dict):\n target = next(iter(target))\n\n ret[target] = {'name': target,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n state = _get_state(target)\n if not state:\n ret[target]['comment'] = ('Package {0} not currently held.'\n .format(target))\n elif state != 'hold':\n if 'test' in __opts__ and __opts__['test']:\n ret[target].update(result=None)\n ret[target]['comment'] = ('Package {0} is set to be held.'\n .format(target))\n else:\n result = _set_state(target, 'hold')\n ret[target].update(changes=result[target], result=True)\n ret[target]['comment'] = ('Package {0} is now being held.'\n .format(target))\n else:\n ret[target].update(result=True)\n ret[target]['comment'] = ('Package {0} is already set to be held.'\n .format(target))\n return ret", "response": "Hold a package in the sequence of packages."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the state of the pkg", "response": "def _get_state(pkg):\n '''\n View package state from the opkg database\n\n Return the state of pkg\n '''\n cmd = ['opkg', 'status']\n cmd.append(pkg)\n out = __salt__['cmd.run'](cmd, python_shell=False)\n state_flag = ''\n for line in salt.utils.itertools.split(out, '\\n'):\n if line.startswith('Status'):\n _status, _state_want, state_flag, _state_status = line.split()\n\n return state_flag"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _set_state(pkg, state):\n '''\n Change package state on the opkg database\n\n The state can be any of:\n\n - hold\n - noprune\n - user\n - ok\n - installed\n - unpacked\n\n This command is commonly used to mark a specific package to be held from\n being upgraded, that is, to be kept at a certain version.\n\n Returns a dict containing the package name, and the new and old\n versions.\n '''\n ret = {}\n valid_states = ('hold', 'noprune', 'user', 'ok', 'installed', 'unpacked')\n if state not in valid_states:\n raise SaltInvocationError('Invalid state: {0}'.format(state))\n oldstate = _get_state(pkg)\n cmd = ['opkg', 'flag']\n cmd.append(state)\n cmd.append(pkg)\n _out = __salt__['cmd.run'](cmd, python_shell=False)\n\n # Missing return value check due to opkg issue 160\n ret[pkg] = {'old': oldstate,\n 'new': state}\n return ret", "response": "Change the state of a specific package on the opkg database"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_upgrades(refresh=True, **kwargs): # pylint: disable=unused-argument\n '''\n List all available package upgrades.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_upgrades\n '''\n ret = {}\n if salt.utils.data.is_true(refresh):\n refresh_db()\n\n cmd = ['opkg', 'list-upgradable']\n call = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n\n if call['retcode'] != 0:\n comment = ''\n if 'stderr' in call:\n comment += call['stderr']\n if 'stdout' in call:\n comment += call['stdout']\n raise CommandExecutionError(comment)\n else:\n out = call['stdout']\n\n for line in out.splitlines():\n name, _oldversion, newversion = line.split(' - ')\n ret[name] = newversion\n\n return ret", "response": "List all available package upgrades."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _convert_to_standard_attr(attr):\n '''\n Helper function for _process_info_installed_output()\n\n Converts an opkg attribute name to a standard attribute\n name which is used across 'pkg' modules.\n '''\n ret_attr = ATTR_MAP.get(attr, None)\n if ret_attr is None:\n # All others convert to lowercase\n return attr.lower()\n return ret_attr", "response": "Helper function for _process_info_installed_output() converts an opkg attribute name to a standard attribute name which is used across pkg modules."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _process_info_installed_output(out, filter_attrs):\n '''\n Helper function for info_installed()\n\n Processes stdout output from a single invocation of\n 'opkg status'.\n '''\n ret = {}\n name = None\n attrs = {}\n attr = None\n\n for line in salt.utils.itertools.split(out, '\\n'):\n if line and line[0] == ' ':\n # This is a continuation of the last attr\n if filter_attrs is None or attr in filter_attrs:\n line = line.strip()\n if attrs[attr]:\n # If attr is empty, don't add leading newline\n attrs[attr] += '\\n'\n attrs[attr] += line\n continue\n line = line.strip()\n if not line:\n # Separator between different packages\n if name:\n ret[name] = attrs\n name = None\n attrs = {}\n attr = None\n continue\n key, value = line.split(':', 1)\n value = value.lstrip()\n attr = _convert_to_standard_attr(key)\n if attr == 'name':\n name = value\n elif filter_attrs is None or attr in filter_attrs:\n attrs[attr] = value\n\n if name:\n ret[name] = attrs\n return ret", "response": "Helper function for info_installed() processes the output from a single invocation of info_installed opkg status."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nreturn the information of the named packages installed on the system.", "response": "def info_installed(*names, **kwargs):\n '''\n Return the information of the named package(s), installed on the system.\n\n .. versionadded:: 2017.7.0\n\n :param names:\n Names of the packages to get information about. If none are specified,\n will return information for all installed packages.\n\n :param attr:\n Comma-separated package attributes. If no 'attr' is specified, all available attributes returned.\n\n Valid attributes are:\n arch, conffiles, conflicts, depends, description, filename, group,\n install_date_time_t, md5sum, packager, provides, recommends,\n replaces, size, source, suggests, url, version\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pkg.info_installed\n salt '*' pkg.info_installed attr=version,packager\n salt '*' pkg.info_installed <package1>\n salt '*' pkg.info_installed <package1> <package2> <package3> ...\n salt '*' pkg.info_installed <package1> attr=version,packager\n salt '*' pkg.info_installed <package1> <package2> <package3> ... attr=version,packager\n '''\n attr = kwargs.pop('attr', None)\n if attr is None:\n filter_attrs = None\n elif isinstance(attr, six.string_types):\n filter_attrs = set(attr.split(','))\n else:\n filter_attrs = set(attr)\n\n ret = {}\n if names:\n # Specific list of names of installed packages\n for name in names:\n cmd = ['opkg', 'status', name]\n call = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n if call['retcode'] != 0:\n comment = ''\n if call['stderr']:\n comment += call['stderr']\n else:\n comment += call['stdout']\n\n raise CommandExecutionError(comment)\n ret.update(_process_info_installed_output(call['stdout'], filter_attrs))\n else:\n # All installed packages\n cmd = ['opkg', 'status']\n call = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n if call['retcode'] != 0:\n comment = ''\n if call['stderr']:\n comment += call['stderr']\n else:\n comment += call['stdout']\n\n raise CommandExecutionError(comment)\n ret.update(_process_info_installed_output(call['stdout'], filter_attrs))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncomparing two packages and return the version of the first one.", "response": "def version_cmp(pkg1, pkg2, ignore_epoch=False, **kwargs): # pylint: disable=unused-argument\n '''\n Do a cmp-style comparison on two packages. Return -1 if pkg1 < pkg2, 0 if\n pkg1 == pkg2, and 1 if pkg1 > pkg2. Return None if there was a problem\n making the comparison.\n\n ignore_epoch : False\n Set to ``True`` to ignore the epoch when comparing versions\n\n .. versionadded:: 2016.3.4\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.version_cmp '0.2.4-0' '0.2.4.1-0'\n '''\n normalize = lambda x: six.text_type(x).split(':', 1)[-1] if ignore_epoch else six.text_type(x)\n pkg1 = normalize(pkg1)\n pkg2 = normalize(pkg2)\n\n output = __salt__['cmd.run_stdout'](['opkg', '--version'],\n output_loglevel='trace',\n python_shell=False)\n opkg_version = output.split(' ')[2].strip()\n if salt.utils.versions.LooseVersion(opkg_version) >= \\\n salt.utils.versions.LooseVersion('0.3.4'):\n cmd_compare = ['opkg', 'compare-versions']\n elif salt.utils.path.which('opkg-compare-versions'):\n cmd_compare = ['opkg-compare-versions']\n else:\n log.warning('Unable to find a compare-versions utility installed. Either upgrade opkg to '\n 'version > 0.3.4 (preferred) or install the older opkg-compare-versions script.')\n return None\n\n for oper, ret in ((\"<<\", -1), (\"=\", 0), (\">>\", 1)):\n cmd = cmd_compare[:]\n cmd.append(_cmd_quote(pkg1))\n cmd.append(oper)\n cmd.append(_cmd_quote(pkg2))\n retcode = __salt__['cmd.retcode'](cmd,\n output_loglevel='trace',\n ignore_retcode=True,\n python_shell=False)\n if retcode == 0:\n return ret\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsets the option to repo", "response": "def _set_repo_option(repo, option):\n '''\n Set the option to repo\n '''\n if not option:\n return\n opt = option.split('=')\n if len(opt) != 2:\n return\n if opt[0] == 'trusted':\n repo['trusted'] = opt[1] == 'yes'\n else:\n repo[opt[0]] = opt[1]"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _set_repo_options(repo, options):\n '''\n Set the options to the repo.\n '''\n delimiters = \"[\", \"]\"\n pattern = '|'.join(map(re.escape, delimiters))\n for option in options:\n splitted = re.split(pattern, option)\n for opt in splitted:\n _set_repo_option(repo, opt)", "response": "Set the options to the repo."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _create_repo(line, filename):\n '''\n Create repo\n '''\n repo = {}\n if line.startswith('#'):\n repo['enabled'] = False\n line = line[1:]\n else:\n repo['enabled'] = True\n cols = salt.utils.args.shlex_split(line.strip())\n repo['compressed'] = not cols[0] in 'src'\n repo['name'] = cols[1]\n repo['uri'] = cols[2]\n repo['file'] = os.path.join(OPKG_CONFDIR, filename)\n if len(cols) > 3:\n _set_repo_options(repo, cols[3:])\n return repo", "response": "Create repo dict from line"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _read_repos(conf_file, repos, filename, regex):\n '''\n Read repos from configuration file\n '''\n for line in conf_file:\n line = salt.utils.stringutils.to_unicode(line)\n if not regex.search(line):\n continue\n repo = _create_repo(line, filename)\n\n # do not store duplicated uri's\n if repo['uri'] not in repos:\n repos[repo['uri']] = [repo]", "response": "Read repos from configuration file"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_repos(**kwargs): # pylint: disable=unused-argument\n '''\n Lists all repos on ``/etc/opkg/*.conf``\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_repos\n '''\n repos = {}\n regex = re.compile(REPO_REGEXP)\n for filename in os.listdir(OPKG_CONFDIR):\n if not filename.endswith(\".conf\"):\n continue\n with salt.utils.files.fopen(os.path.join(OPKG_CONFDIR, filename)) as conf_file:\n _read_repos(conf_file, repos, filename, regex)\n return repos", "response": "Lists all repos on the OPKG. conf dir"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_repo(repo, **kwargs): # pylint: disable=unused-argument\n '''\n Display a repo from the ``/etc/opkg/*.conf``\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.get_repo repo\n '''\n repos = list_repos()\n\n if repos:\n for source in six.itervalues(repos):\n for sub in source:\n if sub['name'] == repo:\n return sub\n return {}", "response": "Display a repo from the system"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _del_repo_from_file(repo, filepath):\n '''\n Remove a repo from filepath\n '''\n with salt.utils.files.fopen(filepath) as fhandle:\n output = []\n regex = re.compile(REPO_REGEXP)\n for line in fhandle:\n line = salt.utils.stringutils.to_unicode(line)\n if regex.search(line):\n if line.startswith('#'):\n line = line[1:]\n cols = salt.utils.args.shlex_split(line.strip())\n if repo != cols[1]:\n output.append(salt.utils.stringutils.to_str(line))\n with salt.utils.files.fopen(filepath, 'w') as fhandle:\n fhandle.writelines(output)", "response": "Remove a repo from filepath\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _set_trusted_option_if_needed(repostr, trusted):\n '''\n Set trusted option to repo if needed\n '''\n if trusted is True:\n repostr += ' [trusted=yes]'\n elif trusted is False:\n repostr += ' [trusted=no]'\n return repostr", "response": "Sets trusted option to repo if needed\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _add_new_repo(repo, properties):\n '''\n Add a new repo entry\n '''\n repostr = '# ' if not properties.get('enabled') else ''\n repostr += 'src/gz ' if properties.get('compressed') else 'src '\n if ' ' in repo:\n repostr += '\"' + repo + '\" '\n else:\n repostr += repo + ' '\n repostr += properties.get('uri')\n repostr = _set_trusted_option_if_needed(repostr, properties.get('trusted'))\n repostr += '\\n'\n conffile = os.path.join(OPKG_CONFDIR, repo + '.conf')\n\n with salt.utils.files.fopen(conffile, 'a') as fhandle:\n fhandle.write(salt.utils.stringutils.to_str(repostr))", "response": "Add a new repo entry\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _mod_repo_in_file(repo, repostr, filepath):\n '''\n Replace a repo entry in filepath with repostr\n '''\n with salt.utils.files.fopen(filepath) as fhandle:\n output = []\n for line in fhandle:\n cols = salt.utils.args.shlex_split(\n salt.utils.stringutils.to_unicode(line).strip()\n )\n if repo not in cols:\n output.append(line)\n else:\n output.append(salt.utils.stringutils.to_str(repostr + '\\n'))\n with salt.utils.files.fopen(filepath, 'w') as fhandle:\n fhandle.writelines(output)", "response": "Replace a repo entry in filepath with repostr"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef del_repo(repo, **kwargs): # pylint: disable=unused-argument\n '''\n Delete a repo from ``/etc/opkg/*.conf``\n\n If the file does not contain any other repo configuration, the file itself\n will be deleted.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.del_repo repo\n '''\n refresh = salt.utils.data.is_true(kwargs.get('refresh', True))\n repos = list_repos()\n if repos:\n deleted_from = dict()\n for repository in repos:\n source = repos[repository][0]\n if source['name'] == repo:\n deleted_from[source['file']] = 0\n _del_repo_from_file(repo, source['file'])\n\n if deleted_from:\n ret = ''\n for repository in repos:\n source = repos[repository][0]\n if source['file'] in deleted_from:\n deleted_from[source['file']] += 1\n for repo_file, count in six.iteritems(deleted_from):\n msg = 'Repo \\'{0}\\' has been removed from {1}.\\n'\n if count == 1 and os.path.isfile(repo_file):\n msg = ('File {1} containing repo \\'{0}\\' has been '\n 'removed.\\n')\n try:\n os.remove(repo_file)\n except OSError:\n pass\n ret += msg.format(repo, repo_file)\n if refresh:\n refresh_db()\n return ret\n\n return \"Repo {0} doesn't exist in the opkg repo lists\".format(repo)", "response": "Delete a repo from the opkg repo lists"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef mod_repo(repo, **kwargs):\n '''\n Modify one or more values for a repo. If the repo does not exist, it will\n be created, so long as uri is defined.\n\n The following options are available to modify a repo definition:\n\n repo\n alias by which opkg refers to the repo.\n uri\n the URI to the repo.\n compressed\n defines (True or False) if the index file is compressed\n enabled\n enable or disable (True or False) repository\n but do not remove if disabled.\n refresh\n enable or disable (True or False) auto-refresh of the repositories\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.mod_repo repo uri=http://new/uri\n salt '*' pkg.mod_repo repo enabled=False\n '''\n repos = list_repos()\n found = False\n uri = ''\n if 'uri' in kwargs:\n uri = kwargs['uri']\n\n for repository in repos:\n source = repos[repository][0]\n if source['name'] == repo:\n found = True\n repostr = ''\n if 'enabled' in kwargs and not kwargs['enabled']:\n repostr += '# '\n if 'compressed' in kwargs:\n repostr += 'src/gz ' if kwargs['compressed'] else 'src'\n else:\n repostr += 'src/gz' if source['compressed'] else 'src'\n repo_alias = kwargs['alias'] if 'alias' in kwargs else repo\n if ' ' in repo_alias:\n repostr += ' \"{0}\"'.format(repo_alias)\n else:\n repostr += ' {0}'.format(repo_alias)\n repostr += ' {0}'.format(kwargs['uri'] if 'uri' in kwargs else source['uri'])\n trusted = kwargs.get('trusted')\n repostr = _set_trusted_option_if_needed(repostr, trusted) if trusted is not None else \\\n _set_trusted_option_if_needed(repostr, source.get('trusted'))\n _mod_repo_in_file(repo, repostr, source['file'])\n elif uri and source['uri'] == uri:\n raise CommandExecutionError(\n 'Repository \\'{0}\\' already exists as \\'{1}\\'.'.format(uri, source['name']))\n\n if not found:\n # Need to add a new repo\n if 'uri' not in kwargs:\n raise CommandExecutionError(\n 'Repository \\'{0}\\' not found and no URI passed to create one.'.format(repo))\n properties = {'uri': kwargs['uri']}\n # If compressed is not defined, assume True\n properties['compressed'] = kwargs['compressed'] if 'compressed' in kwargs else True\n # If enabled is not defined, assume True\n properties['enabled'] = kwargs['enabled'] if 'enabled' in kwargs else True\n properties['trusted'] = kwargs.get('trusted')\n _add_new_repo(repo, properties)\n\n if 'refresh' in kwargs:\n refresh_db()", "response": "Modify one or more values for a repo."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nlists the files that belong to a package. Not specifying any packages will return a list of _every_ file on the system's package database (not generally recommended). CLI Examples: .. code-block:: bash salt '*' pkg.file_list httpd salt '*' pkg.file_list httpd postfix salt '*' pkg.file_list", "response": "def file_list(*packages, **kwargs): # pylint: disable=unused-argument\n '''\n List the files that belong to a package. Not specifying any packages will\n return a list of _every_ file on the system's package database (not\n generally recommended).\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.file_list httpd\n salt '*' pkg.file_list httpd postfix\n salt '*' pkg.file_list\n '''\n output = file_dict(*packages)\n files = []\n for package in list(output['packages'].values()):\n files.extend(package)\n return {'errors': output['errors'], 'files': files}"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a dict of all files in a package.", "response": "def file_dict(*packages, **kwargs): # pylint: disable=unused-argument\n '''\n List the files that belong to a package, grouped by package. Not\n specifying any packages will return a list of _every_ file on the system's\n package database (not generally recommended).\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.file_list httpd\n salt '*' pkg.file_list httpd postfix\n salt '*' pkg.file_list\n '''\n errors = []\n ret = {}\n cmd_files = ['opkg', 'files']\n\n if not packages:\n packages = list(list_pkgs().keys())\n\n for package in packages:\n files = []\n cmd = cmd_files[:]\n cmd.append(package)\n out = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n for line in out['stdout'].splitlines():\n if line.startswith('/'):\n files.append(line)\n elif line.startswith(' * '):\n errors.append(line[3:])\n break\n else:\n continue\n if files:\n ret[package] = files\n\n return {'errors': errors, 'packages': ret}"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the name of the package that owns the file. Multiple file paths can be passed.", "response": "def owner(*paths, **kwargs): # pylint: disable=unused-argument\n '''\n Return the name of the package that owns the file. Multiple file paths can\n be passed. Like :mod:`pkg.version <salt.modules.opkg.version`, if a single\n path is passed, a string will be returned, and if multiple paths are passed,\n a dictionary of file/package name pairs will be returned.\n\n If the file is not owned by a package, or is not present on the minion,\n then an empty string will be returned for that path.\n\n CLI Example:\n\n salt '*' pkg.owner /usr/bin/apachectl\n salt '*' pkg.owner /usr/bin/apachectl /usr/bin/basename\n '''\n if not paths:\n return ''\n ret = {}\n cmd_search = ['opkg', 'search']\n for path in paths:\n cmd = cmd_search[:]\n cmd.append(path)\n output = __salt__['cmd.run_stdout'](cmd,\n output_loglevel='trace',\n python_shell=False)\n if output:\n ret[path] = output.split(' - ')[0].strip()\n else:\n ret[path] = ''\n if len(ret) == 1:\n return next(six.itervalues(ret))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _send_email(name, email):\n \"send a email to inform user of account creation\"\n config = __salt__['config.option']('splunk')\n email_object = config.get('email')\n if email_object:\n cc = email_object.get('cc')\n subject = email_object.get('subject')\n message = email_object.get('message').format(name, name, _generate_password(email), name)\n\n try:\n mail_process = subprocess.Popen(['mail', '-s', subject, '-c', cc, email], stdin=subprocess.PIPE)\n except Exception as e:\n log.error(\"unable to send email to %s: %s\", email, e)\n\n mail_process.communicate(message)\n\n log.info(\"sent account creation email to %s\", email)", "response": "send a email to inform user of account creation"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_splunk(profile):\n '''\n Return the splunk client, cached into __context__ for performance\n '''\n config = __salt__['config.option'](profile)\n\n key = \"splunk.{0}:{1}:{2}:{3}\".format(\n config.get('host'),\n config.get('port'),\n config.get('username'),\n config.get('password')\n )\n\n if key not in __context__:\n __context__[key] = splunklib.client.connect(\n host=config.get('host'),\n port=config.get('port'),\n username=config.get('username'),\n password=config.get('password'))\n\n return __context__[key]", "response": "Return the splunk client cached into __context__ for performance\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_users(profile=\"splunk\"):\n '''\n List all users in the splunk DB\n\n CLI Example:\n\n salt myminion splunk.list_users\n '''\n\n config = __salt__['config.option'](profile)\n key = \"splunk.users.{0}\".format(\n config.get('host')\n )\n\n if key not in __context__:\n _populate_cache(profile)\n\n return __context__[key]", "response": "List all users in the splunk DB\n CLI Example : splunk. list_users"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget a splunk user by name or email", "response": "def get_user(email, profile=\"splunk\", **kwargs):\n '''\n Get a splunk user by name/email\n\n CLI Example:\n\n salt myminion splunk.get_user 'user@example.com' user_details=false\n salt myminion splunk.get_user 'user@example.com' user_details=true\n '''\n\n user_map = list_users(profile)\n user_found = email.lower() in user_map.keys()\n\n if not kwargs.get('user_details', False) and user_found:\n # The user is in splunk group, just return\n return True\n elif kwargs.get('user_details', False) and user_found:\n user = user_map[email.lower()]\n\n response = {}\n for field in ['defaultApp', 'realname', 'name', 'email']:\n response[field] = user[field]\n\n response['roles'] = []\n for role in user.role_entities:\n response['roles'].append(role.name)\n\n return response\n\n return False"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a splunk user by name email", "response": "def create_user(email, profile=\"splunk\", **kwargs):\n '''\n create a splunk user by name/email\n\n CLI Example:\n\n salt myminion splunk.create_user user@example.com roles=['user'] realname=\"Test User\" name=testuser\n '''\n\n client = _get_splunk(profile)\n\n email = email.lower()\n\n user = list_users(profile).get(email)\n\n if user:\n log.error(\"User is already present %s\", email)\n return False\n\n property_map = {}\n\n for field in ALLOWED_FIELDS_FOR_MODIFICATION:\n if kwargs.get(field):\n property_map[field] = kwargs.get(field)\n\n try:\n # create\n for req_field in REQUIRED_FIELDS_FOR_CREATE:\n if not property_map.get(req_field):\n log.error(\"Missing required params %s\",\n ', '.join([six.text_type(k) for k in REQUIRED_FIELDS_FOR_CREATE]))\n return False\n\n newuser = client.users.create(username=property_map['name'],\n password=_generate_password(email),\n roles=property_map['roles'],\n email=email,\n realname=property_map['realname'])\n\n _send_email(newuser.name, newuser.email)\n\n response = {}\n for field in ['email', 'password', 'realname', 'roles']:\n response[field] = newuser[field]\n\n except Exception as e:\n log.error(\"Caught exception %s\", e)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef update_user(email, profile=\"splunk\", **kwargs):\n '''\n Create a splunk user by email\n\n CLI Example:\n\n salt myminion splunk.update_user example@domain.com roles=['user'] realname=\"Test User\"\n '''\n\n client = _get_splunk(profile)\n\n email = email.lower()\n\n user = list_users(profile).get(email)\n\n if not user:\n log.error('Failed to retrieve user %s', email)\n return False\n\n property_map = {}\n\n for field in ALLOWED_FIELDS_FOR_MODIFICATION:\n if kwargs.get(field):\n property_map[field] = kwargs.get(field)\n\n # update\n kwargs = {}\n roles = [role.name for role in user.role_entities]\n\n for k, v in property_map.items():\n resource_value = user[k]\n if resource_value is not None:\n # you can't update the username in update api call\n if k.lower() == 'name':\n continue\n if k.lower() == 'roles':\n if isinstance(v, six.string_types):\n v = v.split(',')\n if set(roles) != set(v):\n kwargs['roles'] = list(set(v))\n elif resource_value != v:\n kwargs[k] = v\n\n if kwargs:\n user.update(**kwargs).refresh()\n\n fields_modified = {}\n for field in ALLOWED_FIELDS_FOR_MODIFICATION:\n fields_modified[field] = user[field]\n\n else:\n #succeeded, no change\n return True", "response": "Update a splunk user by email"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndelete a splunk user by email", "response": "def delete_user(email, profile=\"splunk\"):\n '''\n Delete a splunk user by email\n\n CLI Example:\n\n salt myminion splunk_user.delete 'user@example.com'\n '''\n\n client = _get_splunk(profile)\n\n user = list_users(profile).get(email)\n\n if user:\n try:\n client.users.delete(user.name)\n except (AuthenticationError, HTTPError) as e:\n log.info('Exception: %s', e)\n return False\n else:\n return False\n\n return user.name not in client.users"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the default shell to use on this system", "response": "def shell():\n '''\n Return the default shell to use on this system\n '''\n # Provides:\n # shell\n if salt.utils.platform.is_windows():\n env_var = 'COMSPEC'\n default = r'C:\\Windows\\system32\\cmd.exe'\n else:\n env_var = 'SHELL'\n default = '/bin/sh'\n\n return {'shell': os.environ.get(env_var, default)}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef config():\n '''\n Return the grains set in the grains file\n '''\n if 'conf_file' not in __opts__:\n return {}\n if os.path.isdir(__opts__['conf_file']):\n if salt.utils.platform.is_proxy():\n gfn = os.path.join(\n __opts__['conf_file'],\n 'proxy.d',\n __opts__['id'],\n 'grains'\n )\n else:\n gfn = os.path.join(\n __opts__['conf_file'],\n 'grains'\n )\n else:\n if salt.utils.platform.is_proxy():\n gfn = os.path.join(\n os.path.dirname(__opts__['conf_file']),\n 'proxy.d',\n __opts__['id'],\n 'grains'\n )\n else:\n gfn = os.path.join(\n os.path.dirname(__opts__['conf_file']),\n 'grains'\n )\n if os.path.isfile(gfn):\n log.debug('Loading static grains from %s', gfn)\n with salt.utils.files.fopen(gfn, 'rb') as fp_:\n try:\n return salt.utils.data.decode(salt.utils.yaml.safe_load(fp_))\n except Exception:\n log.warning(\"Bad syntax in grains file! Skipping.\")\n return {}\n return {}", "response": "Return the grains set in the grains file\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_output_volume():\n '''\n Get the output volume (range 0 to 100)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' desktop.get_output_volume\n '''\n cmd = 'osascript -e \"get output volume of (get volume settings)\"'\n call = __salt__['cmd.run_all'](\n cmd,\n output_loglevel='debug',\n python_shell=False\n )\n _check_cmd(call)\n\n return call.get('stdout')", "response": "Get the output volume of the current node"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_output_volume(volume):\n '''\n Set the volume of sound.\n\n volume\n The level of volume. Can range from 0 to 100.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' desktop.set_output_volume <volume>\n '''\n cmd = 'osascript -e \"set volume output volume {0}\"'.format(volume)\n call = __salt__['cmd.run_all'](\n cmd,\n output_loglevel='debug',\n python_shell=False\n )\n _check_cmd(call)\n\n return get_output_volume()", "response": "Set the output volume of sound."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlaunching the screensaver. CLI Example: .. code-block:: bash salt '*' desktop.screensaver", "response": "def screensaver():\n '''\n Launch the screensaver.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' desktop.screensaver\n '''\n cmd = 'open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app'\n call = __salt__['cmd.run_all'](\n cmd,\n output_loglevel='debug',\n python_shell=False\n )\n _check_cmd(call)\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef say(*words):\n '''\n Say some words.\n\n words\n The words to execute the say command with.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' desktop.say <word0> <word1> ... <wordN>\n '''\n cmd = 'say {0}'.format(' '.join(words))\n call = __salt__['cmd.run_all'](\n cmd,\n output_loglevel='debug',\n python_shell=False\n )\n _check_cmd(call)\n\n return True", "response": "Say some words with the specified words."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncheck the output of the cmd. run_all function call.", "response": "def _check_cmd(call):\n '''\n Check the output of the cmd.run_all function call.\n '''\n if call['retcode'] != 0:\n comment = ''\n std_err = call.get('stderr')\n std_out = call.get('stdout')\n if std_err:\n comment += std_err\n if std_out:\n comment += std_out\n\n raise CommandExecutionError('Error running command: {0}'.format(comment))\n\n return call"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef build_counter_list(counter_list):\n r'''\n Create a list of Counter objects to be used in the pdh query\n\n Args:\n counter_list (list):\n A list of tuples containing counter information. Each tuple should\n contain the object, instance, and counter name. For example, to\n get the ``% Processor Time`` counter for all Processors on the\n system (``\\Processor(*)\\% Processor Time``) you would pass a tuple\n like this:\n\n ```\n counter_list = [('Processor', '*', '% Processor Time')]\n ```\n\n If there is no ``instance`` for the counter, pass ``None``\n\n Multiple counters can be passed like so:\n\n ```\n counter_list = [('Processor', '*', '% Processor Time'),\n ('System', None, 'Context Switches/sec')]\n ```\n\n .. note::\n Invalid counters are ignored\n\n Returns:\n list: A list of Counter objects\n '''\n counters = []\n index = 0\n for obj, instance, counter_name in counter_list:\n try:\n counter = Counter.build_counter(obj, instance, index, counter_name)\n index += 1\n counters.append(counter)\n except CommandExecutionError as exc:\n # Not a valid counter\n log.debug(exc.strerror)\n continue\n return counters", "response": "r Builds a list of Counter objects for the given list of objects."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting the values for all counters available to a Counter object.", "response": "def get_all_counters(obj, instance_list=None):\n '''\n Get the values for all counters available to a Counter object\n\n Args:\n\n obj (str):\n The name of the counter object. You can get a list of valid names\n using the ``list_objects`` function\n\n instance_list (list):\n A list of instances to return. Use this to narrow down the counters\n that are returned.\n\n .. note::\n ``_Total`` is returned as ``*``\n '''\n counters, instances_avail = win32pdh.EnumObjectItems(None, None, obj, -1, 0)\n\n if instance_list is None:\n instance_list = instances_avail\n\n if not isinstance(instance_list, list):\n instance_list = [instance_list]\n\n counter_list = []\n for counter in counters:\n for instance in instance_list:\n instance = '*' if instance.lower() == '_total' else instance\n counter_list.append((obj, instance, counter))\n else: # pylint: disable=useless-else-on-loop\n counter_list.append((obj, None, counter))\n\n return get_counters(counter_list) if counter_list else {}"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets the values for the passes list of counters", "response": "def get_counters(counter_list):\n '''\n Get the values for the passes list of counters\n\n Args:\n counter_list (list):\n A list of counters to lookup\n\n Returns:\n dict: A dictionary of counters and their values\n '''\n if not isinstance(counter_list, list):\n raise CommandExecutionError('counter_list must be a list of tuples')\n\n try:\n # Start a Query instances\n query = win32pdh.OpenQuery()\n\n # Build the counters\n counters = build_counter_list(counter_list)\n\n # Add counters to the Query\n for counter in counters:\n counter.add_to_query(query)\n\n # https://docs.microsoft.com/en-us/windows/desktop/perfctrs/collecting-performance-data\n win32pdh.CollectQueryData(query)\n # The sleep here is required for counters that require more than 1\n # reading\n time.sleep(1)\n win32pdh.CollectQueryData(query)\n ret = {}\n\n for counter in counters:\n try:\n ret.update({counter.path: counter.value()})\n except pywintypes.error as exc:\n if exc.strerror == 'No data to return.':\n # Some counters are not active and will throw an error if\n # there is no data to return\n continue\n else:\n raise\n\n finally:\n win32pdh.CloseQuery(query)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nbuild a Counter object from the specified object instance and counter.", "response": "def build_counter(obj, instance, instance_index, counter):\n r'''\n Makes a fully resolved counter path. Counter names are formatted like\n this:\n\n ``\\Processor(*)\\% Processor Time``\n\n The above breaks down like this:\n\n obj = 'Processor'\n instance = '*'\n counter = '% Processor Time'\n\n Args:\n\n obj (str):\n The top level object\n\n instance (str):\n The instance of the object\n\n instance_index (int):\n The index of the instance. Can usually be 0\n\n counter (str):\n The name of the counter\n\n Returns:\n Counter: A Counter object with the path if valid\n\n Raises:\n CommandExecutionError: If the path is invalid\n '''\n path = win32pdh.MakeCounterPath(\n (None, obj, instance, None, instance_index, counter), 0)\n if win32pdh.ValidatePath(path) is 0:\n return Counter(path, obj, instance, instance_index, counter)\n raise CommandExecutionError('Invalid counter specified: {0}'.format(path))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add_to_query(self, query):\n '''\n Add the current path to the query\n\n Args:\n query (obj):\n The handle to the query to add the counter\n '''\n self.handle = win32pdh.AddCounter(query, self.path)", "response": "Add the current path to the query"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets information about the counter.", "response": "def get_info(self):\n '''\n Get information about the counter\n\n .. note::\n GetCounterInfo sometimes crashes in the wrapper code. Fewer crashes\n if this is called after sampling data.\n '''\n if not self.info:\n ci = win32pdh.GetCounterInfo(self.handle, 0)\n self.info = {\n 'type': ci[0],\n 'version': ci[1],\n 'scale': ci[2],\n 'default_scale': ci[3],\n 'user_data': ci[4],\n 'query_user_data': ci[5],\n 'full_path': ci[6],\n 'machine_name': ci[7][0],\n 'object_name': ci[7][1],\n 'instance_name': ci[7][2],\n 'parent_instance': ci[7][3],\n 'instance_index': ci[7][4],\n 'counter_name': ci[7][5],\n 'explain_text': ci[8]\n }\n return self.info"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning the counter value of the current object", "response": "def value(self):\n '''\n Return the counter value\n\n Returns:\n long: The counter value\n '''\n (counter_type, value) = win32pdh.GetFormattedCounterValue(\n self.handle, win32pdh.PDH_FMT_DOUBLE)\n self.type = counter_type\n return value"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn the names of the flags that are set in the Type field It can be used to format the counter.", "response": "def type_string(self):\n '''\n Returns the names of the flags that are set in the Type field\n\n It can be used to format the counter.\n '''\n type = self.get_info()['type']\n type_list = []\n for member in dir(self):\n if member.startswith(\"PERF_\"):\n bit = getattr(self, member)\n if bit and bit & type:\n type_list.append(member[5:])\n return type_list"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef exists(name, index=None):\n '''\n Add the directory to the system PATH at index location\n\n index\n Position where the directory should be placed in the PATH. This is\n 0-indexed, so 0 means to prepend at the very start of the PATH.\n\n .. note::\n If the index is not specified, and the directory needs to be added\n to the PATH, then the directory will be appended to the PATH, and\n this state will not enforce its location within the PATH.\n\n Examples:\n\n .. code-block:: yaml\n\n 'C:\\\\python27':\n win_path.exists\n\n 'C:\\\\sysinternals':\n win_path.exists:\n - index: 0\n\n 'C:\\\\mystuff':\n win_path.exists:\n - index: -1\n '''\n try:\n name = os.path.normpath(salt.utils.stringutils.to_unicode(name))\n except TypeError:\n name = six.text_type(name)\n\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n\n if index is not None and not isinstance(index, six.integer_types):\n ret['comment'] = 'Index must be an integer'\n ret['result'] = False\n return ret\n\n def _get_path_lowercase():\n return [x.lower() for x in __salt__['win_path.get_path']()]\n\n def _index(path=None):\n if path is None:\n path = _get_path_lowercase()\n try:\n pos = path.index(name.lower())\n except ValueError:\n return None\n else:\n if index is not None and index < 0:\n # Since a negative index was used, convert the index to a\n # negative index to make the changes dict easier to read, as\n # well as making comparisons manageable.\n return -(len(path) - pos)\n else:\n return pos\n\n def _changes(old, new):\n return {'index': {'old': old, 'new': new}}\n\n pre_path = _get_path_lowercase()\n num_dirs = len(pre_path)\n\n if index is not None:\n if index > num_dirs:\n ret.setdefault('warnings', []).append(\n 'There are only {0} directories in the PATH, using an index '\n 'of {0} instead of {1}.'.format(num_dirs, index)\n )\n index = num_dirs\n elif index <= -num_dirs:\n ret.setdefault('warnings', []).append(\n 'There are only {0} directories in the PATH, using an index '\n 'of 0 instead of {1}.'.format(num_dirs, index)\n )\n index = 0\n\n old_index = _index(pre_path)\n comments = []\n\n if old_index is not None:\n # Directory exists in PATH\n\n if index is None:\n # We're not enforcing the index, and the directory is in the PATH.\n # There's nothing to do here.\n comments.append('{0} already exists in the PATH.'.format(name))\n return _format_comments(ret, comments)\n else:\n if index == old_index:\n comments.append(\n '{0} already exists in the PATH at index {1}.'.format(\n name, index\n )\n )\n return _format_comments(ret, comments)\n else:\n if __opts__['test']:\n ret['result'] = None\n comments.append(\n '{0} would be moved from index {1} to {2}.'.format(\n name, old_index, index\n )\n )\n ret['changes'] = _changes(old_index, index)\n return _format_comments(ret, comments)\n\n else:\n # Directory does not exist in PATH\n if __opts__['test']:\n ret['result'] = None\n comments.append(\n '{0} would be added to the PATH{1}.'.format(\n name,\n ' at index {0}'.format(index) if index is not None else ''\n )\n )\n ret['changes'] = _changes(old_index, index)\n return _format_comments(ret, comments)\n\n try:\n ret['result'] = __salt__['win_path.add'](name, index=index, rehash=False)\n except Exception as exc:\n comments.append('Encountered error: {0}.'.format(exc))\n ret['result'] = False\n\n if ret['result']:\n ret['result'] = __salt__['win_path.rehash']()\n if not ret['result']:\n comments.append(\n 'Updated registry with new PATH, but failed to rehash.'\n )\n\n new_index = _index()\n\n if ret['result']:\n # If we have not already determined a False result based on the return\n # from either win_path.add or win_path.rehash, check the new_index.\n ret['result'] = new_index is not None \\\n if index is None \\\n else index == new_index\n\n if index is not None and old_index is not None:\n comments.append(\n '{0} {1} from index {2} to {3}.'.format(\n 'Moved' if ret['result'] else 'Failed to move',\n name,\n old_index,\n index\n )\n )\n else:\n comments.append(\n '{0} {1} to the PATH{2}.'.format(\n 'Added' if ret['result'] else 'Failed to add',\n name,\n ' at index {0}'.format(index) if index is not None else ''\n )\n )\n\n if old_index != new_index:\n ret['changes'] = _changes(old_index, new_index)\n\n return _format_comments(ret, comments)", "response": "Return a list of the names of the named file in the system PATH."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a unique ID for this proxy minion", "response": "def id(opts):\n '''\n Return a unique ID for this proxy minion. This ID MUST NOT CHANGE.\n If it changes while the proxy is running the salt-master will get\n really confused and may stop talking to this minion\n '''\n r = salt.utils.http.query(opts['proxy']['url']+'id', decode_type='json', decode=True)\n return r['dict']['id'].encode('ascii', 'ignore')"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef grains():\n '''\n Get the grains from the proxied device\n '''\n if not DETAILS.get('grains_cache', {}):\n r = salt.utils.http.query(DETAILS['url']+'info', decode_type='json', decode=True)\n DETAILS['grains_cache'] = r['dict']\n return DETAILS['grains_cache']", "response": "Get the grains from the proxied device\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nstart a service on the REST server", "response": "def service_start(name):\n '''\n Start a \"service\" on the REST server\n '''\n r = salt.utils.http.query(DETAILS['url']+'service/start/'+name, decode_type='json', decode=True)\n return r['dict']"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef service_list():\n '''\n List \"services\" on the REST server\n '''\n r = salt.utils.http.query(DETAILS['url']+'service/list', decode_type='json', decode=True)\n return r['dict']", "response": "List services on the REST server"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ninstalls a package on the REST server", "response": "def package_install(name, **kwargs):\n '''\n Install a \"package\" on the REST server\n '''\n cmd = DETAILS['url']+'package/install/'+name\n if kwargs.get('version', False):\n cmd += '/'+kwargs['version']\n else:\n cmd += '/1.0'\n r = salt.utils.http.query(cmd, decode_type='json', decode=True)\n return r['dict']"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks if the server is up and return the object ID", "response": "def ping():\n '''\n Is the REST server up?\n '''\n r = salt.utils.http.query(DETAILS['url']+'ping', decode_type='json', decode=True)\n try:\n return r['dict'].get('ret', False)\n except Exception:\n return False"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef exists(name, attributes):\n '''\n Make sure the given attributes exist on the file/directory\n\n name\n The path to the file/directory\n\n attributes\n The attributes that should exist on the file/directory, this is accepted as\n an array, with key and value split with an equals sign, if you want to specify\n a hex value then add 0x to the beginning of the value.\n\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}}\n\n if not os.path.exists(name):\n ret['result'] = False\n ret['comment'] = \"File or directory doesn't exist\"\n return ret\n\n current_attrs = __salt__['xattr.list'](name)\n current_ids = current_attrs.keys()\n\n for attr in attributes:\n attr_id, attr_val = attr.split(\"=\")\n attr_hex = attr_val.startswith(\"0x\")\n\n if attr_hex:\n # Remove spaces and new lines so we can match these\n current_attrs[attr_id] = __salt__['xattr.read'](name, attr_id, hex=True).replace(\" \", \"\").replace(\"\\n\", \"\")\n attr_val = attr_val[2:].replace(\" \", \"\")\n\n if attr_id not in current_attrs:\n value_matches = False\n else:\n value_matches = ((current_attrs[attr_id] == attr_val) or\n (attr_hex and current_attrs[attr_id] == attr_val))\n\n if attr_id in current_ids and value_matches:\n continue\n else:\n ret['changes'][attr_id] = attr_val\n __salt__['xattr.write'](name, attr_id, attr_val, attr_hex)\n\n if not ret['changes']:\n ret['comment'] = 'All values existed correctly.'\n\n return ret", "response": "Make sure the given attributes exist on the file or directory."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ndeletes the given attributes from the given file or directory.", "response": "def delete(name, attributes):\n '''\n Make sure the given attributes are deleted from the file/directory\n\n name\n The path to the file/directory\n\n attributes\n The attributes that should be removed from the file/directory, this is accepted as\n an array.\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}}\n\n if not os.path.exists(name):\n ret['result'] = False\n ret['comment'] = \"File or directory doesn't exist\"\n return ret\n\n current_attrs = __salt__['xattr.list'](name)\n current_ids = current_attrs.keys()\n\n for attr in attributes:\n\n if attr in current_ids:\n __salt__['xattr.delete'](name, attr)\n ret['changes'][attr] = 'delete'\n\n if not ret['changes']:\n ret['comment'] = 'All attributes were already deleted.'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncheck if a given path is writeable by the current user.", "response": "def is_writeable(path, check_parent=False):\n '''\n Check if a given path is writeable by the current user.\n\n :param path: The path to check\n :param check_parent: If the path to check does not exist, check for the\n ability to write to the parent directory instead\n :returns: True or False\n '''\n\n if os.access(path, os.F_OK) and os.access(path, os.W_OK):\n # The path exists and is writeable\n return True\n\n if os.access(path, os.F_OK) and not os.access(path, os.W_OK):\n # The path exists and is not writeable\n return False\n\n # The path does not exists or is not writeable\n\n if check_parent is False:\n # We're not allowed to check the parent directory of the provided path\n return False\n\n # Lets get the parent directory of the provided path\n parent_dir = os.path.dirname(path)\n\n if not os.access(parent_dir, os.F_OK):\n # Parent directory does not exit\n return False\n\n # Finally, return if we're allowed to write in the parent directory of the\n # provided path\n return os.access(parent_dir, os.W_OK)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef is_readable(path):\n '''\n Check if a given path is readable by the current user.\n\n :param path: The path to check\n :returns: True or False\n '''\n\n if os.access(path, os.F_OK) and os.access(path, os.R_OK):\n # The path exists and is readable\n return True\n\n # The path does not exist\n return False", "response": "Check if a given path is readable by the current user."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef parseData(self, data, host, port, options):\n '''\n This function will parse the raw syslog data, dynamically create the\n topic according to the topic specified by the user (if specified) and\n decide whether to send the syslog data as an event on the master bus,\n based on the constraints given by the user.\n\n :param data: The raw syslog event data which is to be parsed.\n :param host: The IP of the host from where syslog is forwarded.\n :param port: Port of the junos device from which the data is sent\n :param options: kwargs provided by the user in the configuration file.\n :return: The result dictionary which contains the data and the topic,\n if the event is to be sent on the bus.\n\n '''\n data = self.obj.parse(data)\n data['hostip'] = host\n log.debug(\n 'Junos Syslog - received %s from %s, sent from port %s',\n data, host, port\n )\n\n send_this_event = True\n for key in options:\n if key in data:\n if isinstance(options[key], (six.string_types, int)):\n if six.text_type(options[key]) != six.text_type(data[key]):\n send_this_event = False\n break\n elif isinstance(options[key], list):\n for opt in options[key]:\n if six.text_type(opt) == six.text_type(data[key]):\n break\n else:\n send_this_event = False\n break\n else:\n raise Exception(\n 'Arguments in config not specified properly')\n else:\n raise Exception(\n 'Please check the arguments given to junos engine in the\\\n configuration file')\n\n if send_this_event:\n if 'event' in data:\n topic = 'jnpr/syslog'\n\n for i in range(2, len(self.title)):\n topic += '/' + six.text_type(data[self.title[i]])\n log.debug(\n 'Junos Syslog - sending this event on the bus: %s from %s',\n data, host\n )\n result = {'send': True, 'data': data, 'topic': topic}\n return result\n else:\n raise Exception(\n 'The incoming event data could not be parsed properly.')\n else:\n result = {'send': False}\n return result", "response": "This function will parse the raw syslog data and create the topic according to the topic specified by the user."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef send_event_to_salt(self, result):\n '''\n This function identifies whether the engine is running on the master\n or the minion and sends the data to the master event bus accordingly.\n\n :param result: It's a dictionary which has the final data and topic.\n\n '''\n if result['send']:\n data = result['data']\n topic = result['topic']\n # If the engine is run on master, get the event bus and send the\n # parsed event.\n if __opts__['__role'] == 'master':\n event.get_master_event(__opts__,\n __opts__['sock_dir']\n ).fire_event(data, topic)\n # If the engine is run on minion, use the fire_master execution\n # module to send event on the master bus.\n else:\n __salt__['event.fire_master'](data=data, tag=topic)", "response": "Send the data to the master event bus according to the result."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef sdb_get(uri, opts, utils=None):\n '''\n Get a value from a db, using a uri in the form of ``sdb://<profile>/<key>``. If\n the uri provided does not start with ``sdb://``, then it will be returned as-is.\n '''\n if not isinstance(uri, string_types) or not uri.startswith('sdb://'):\n return uri\n\n if utils is None:\n utils = salt.loader.utils(opts)\n\n sdlen = len('sdb://')\n indx = uri.find('/', sdlen)\n\n if (indx == -1) or not uri[(indx + 1):]:\n return uri\n\n profile = opts.get(uri[sdlen:indx], {})\n if not profile:\n profile = opts.get('pillar', {}).get(uri[sdlen:indx], {})\n if 'driver' not in profile:\n return uri\n\n fun = '{0}.get'.format(profile['driver'])\n query = uri[indx+1:]\n\n loaded_db = salt.loader.sdb(opts, fun, utils=utils)\n return loaded_db[fun](query, profile=profile)", "response": "Get a value from a db using a uri in the form of sdb://<profile>/<key >."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget or set the hash of the given uri.", "response": "def sdb_get_or_set_hash(uri,\n opts,\n length=8,\n chars='abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)',\n utils=None):\n '''\n Check if value exists in sdb. If it does, return, otherwise generate a\n random string and store it. This can be used for storing secrets in a\n centralized place.\n '''\n if not isinstance(uri, string_types) or not uri.startswith('sdb://'):\n return False\n\n if utils is None:\n utils = salt.loader.utils(opts)\n\n ret = sdb_get(uri, opts, utils=utils)\n\n if ret is None:\n val = ''.join([random.SystemRandom().choice(chars) for _ in range(length)])\n sdb_set(uri, val, opts, utils)\n\n return ret or val"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nlisting all users. profile Configuration profile used to connect to the Grafana instance. Default is 'grafana'. CLI Example: .. code-block:: bash salt '*' grafana4.get_users", "response": "def get_users(profile='grafana'):\n '''\n List all users.\n\n profile\n Configuration profile used to connect to the Grafana instance.\n Default is 'grafana'.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' grafana4.get_users\n '''\n if isinstance(profile, string_types):\n profile = __salt__['config.option'](profile)\n response = requests.get(\n '{0}/api/users'.format(profile['grafana_url']),\n auth=_get_auth(profile),\n headers=_get_headers(profile),\n timeout=profile.get('grafana_timeout', 3),\n )\n if response.status_code >= 400:\n response.raise_for_status()\n return response.json()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget a single user.", "response": "def get_user(login, profile='grafana'):\n '''\n Show a single user.\n\n login\n Login of the user.\n\n profile\n Configuration profile used to connect to the Grafana instance.\n Default is 'grafana'.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' grafana4.get_user <login>\n '''\n data = get_users(profile)\n for user in data:\n if user['login'] == login:\n return user\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef update_user(userid, profile='grafana', orgid=None, **kwargs):\n '''\n Update an existing user.\n\n userid\n Id of the user.\n\n login\n Optional - Login of the user.\n\n email\n Optional - Email of the user.\n\n name\n Optional - Full name of the user.\n\n orgid\n Optional - Default Organization of the user.\n\n profile\n Configuration profile used to connect to the Grafana instance.\n Default is 'grafana'.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' grafana4.update_user <user_id> login=<login> email=<email>\n '''\n if isinstance(profile, string_types):\n profile = __salt__['config.option'](profile)\n response = requests.put(\n '{0}/api/users/{1}'.format(profile['grafana_url'], userid),\n json=kwargs,\n auth=_get_auth(profile),\n headers=_get_headers(profile),\n timeout=profile.get('grafana_timeout', 3),\n )\n if response.status_code >= 400:\n response.raise_for_status()\n if orgid:\n response2 = requests.post(\n '{0}/api/users/{1}/using/{2}'.format(profile['grafana_url'], userid, orgid),\n auth=_get_auth(profile),\n headers=_get_headers(profile),\n timeout=profile.get('grafana_timeout', 3),\n )\n if response2.status_code >= 400:\n response2.raise_for_status()\n return response.json()", "response": "Update an existing user."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nswitching the current organization.", "response": "def switch_org(orgname, profile='grafana'):\n '''\n Switch the current organization.\n\n name\n Name of the organization to switch to.\n\n profile\n Configuration profile used to connect to the Grafana instance.\n Default is 'grafana'.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' grafana4.switch_org <name>\n '''\n if isinstance(profile, string_types):\n profile = __salt__['config.option'](profile)\n org = get_org(orgname, profile)\n response = requests.post(\n '{0}/api/user/using/{1}'.format(profile['grafana_url'], org['id']),\n auth=_get_auth(profile),\n headers=_get_headers(profile),\n timeout=profile.get('grafana_timeout', 3),\n )\n if response.status_code >= 400:\n response.raise_for_status()\n return org"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef update_org_user(userid, orgname=None, profile='grafana', **kwargs):\n '''\n Update user role in the organization.\n\n userid\n Id of the user.\n\n loginOrEmail\n Login or email of the user.\n\n role\n Role of the user for this organization. Should be one of:\n - Admin\n - Editor\n - Read Only Editor\n - Viewer\n\n orgname\n Name of the organization in which users are updated.\n\n profile\n Configuration profile used to connect to the Grafana instance.\n Default is 'grafana'.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' grafana4.update_org_user <user_id> <orgname> loginOrEmail=<loginOrEmail> role=<role>\n '''\n if isinstance(profile, string_types):\n profile = __salt__['config.option'](profile)\n if orgname:\n switch_org(orgname, profile)\n response = requests.patch(\n '{0}/api/org/users/{1}'.format(profile['grafana_url'], userid),\n json=kwargs,\n auth=_get_auth(profile),\n headers=_get_headers(profile),\n timeout=profile.get('grafana_timeout', 3),\n )\n if response.status_code >= 400:\n response.raise_for_status()\n return response.json()", "response": "Update user role in the organization."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting a single datasource in an organisation.", "response": "def get_datasource(name, orgname=None, profile='grafana'):\n '''\n Show a single datasource in an organisation.\n\n name\n Name of the datasource.\n\n orgname\n Name of the organization.\n\n profile\n Configuration profile used to connect to the Grafana instance.\n Default is 'grafana'.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' grafana4.get_datasource <name> <orgname>\n '''\n data = get_datasources(orgname=orgname, profile=profile)\n for datasource in data:\n if datasource['name'] == name:\n return datasource\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndeletes a datasource. datasourceid Id of the datasource. profile Configuration profile used to connect to the Grafana instance. Default is 'grafana'. CLI Example: .. code-block:: bash salt '*' grafana4.delete_datasource <datasource_id>", "response": "def delete_datasource(datasourceid, orgname=None, profile='grafana'):\n '''\n Delete a datasource.\n\n datasourceid\n Id of the datasource.\n\n profile\n Configuration profile used to connect to the Grafana instance.\n Default is 'grafana'.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' grafana4.delete_datasource <datasource_id>\n '''\n if isinstance(profile, string_types):\n profile = __salt__['config.option'](profile)\n response = requests.delete(\n '{0}/api/datasources/{1}'.format(profile['grafana_url'], datasourceid),\n auth=_get_auth(profile),\n headers=_get_headers(profile),\n timeout=profile.get('grafana_timeout', 3),\n )\n if response.status_code >= 400:\n response.raise_for_status()\n return response.json()"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nread in the libvirt keys and return the new ones", "response": "def ext_pillar(minion_id,\n pillar, # pylint: disable=W0613\n command): # pylint: disable=W0613\n '''\n Read in the generated libvirt keys\n '''\n key_dir = os.path.join(\n __opts__['pki_dir'],\n 'libvirt',\n minion_id)\n cacert = os.path.join(__opts__['pki_dir'],\n 'libvirt',\n 'cacert.pem')\n if not os.path.isdir(key_dir):\n # No keys have been generated\n gen_hyper_keys(minion_id,\n pillar.get('ext_pillar_virt.country', 'US'),\n pillar.get('ext_pillar_virt.st', 'Utah'),\n pillar.get('ext_pillar_virt.locality',\n 'Salt Lake City'),\n pillar.get('ext_pillar_virt.organization', 'Salted'),\n pillar.get('ext_pillar_virt.expiration_days', '365')\n )\n ret = {}\n for key in os.listdir(key_dir):\n if not key.endswith('.pem'):\n continue\n fn_ = os.path.join(key_dir, key)\n with salt.utils.files.fopen(fn_, 'r') as fp_:\n ret['libvirt.{0}'.format(key)] = \\\n salt.utils.stringutils.to_unicode(fp_.read())\n with salt.utils.files.fopen(cacert, 'r') as fp_:\n ret['libvirt.cacert.pem'] = \\\n salt.utils.stringutils.to_unicode(fp_.read())\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngenerates the keys to be used by libvirt hypervisors", "response": "def gen_hyper_keys(minion_id,\n country='US',\n state='Utah',\n locality='Salt Lake City',\n organization='Salted',\n expiration_days='365'):\n '''\n Generate the keys to be used by libvirt hypervisors, this routine gens\n the keys and applies them to the pillar for the hypervisor minions\n '''\n key_dir = os.path.join(\n __opts__['pki_dir'],\n 'libvirt')\n if not os.path.isdir(key_dir):\n os.makedirs(key_dir)\n cakey = os.path.join(key_dir, 'cakey.pem')\n cacert = os.path.join(key_dir, 'cacert.pem')\n cainfo = os.path.join(key_dir, 'ca.info')\n if not os.path.isfile(cainfo):\n with salt.utils.files.fopen(cainfo, 'w+') as fp_:\n fp_.write('cn = salted\\nca\\ncert_signing_key')\n if not os.path.isfile(cakey):\n subprocess.call(\n 'certtool --generate-privkey > {0}'.format(cakey),\n shell=True)\n if not os.path.isfile(cacert):\n cmd = ('certtool --generate-self-signed --load-privkey {0} '\n '--template {1} --outfile {2}').format(cakey, cainfo, cacert)\n subprocess.call(cmd, shell=True)\n sub_dir = os.path.join(key_dir, minion_id)\n if not os.path.isdir(sub_dir):\n os.makedirs(sub_dir)\n priv = os.path.join(sub_dir, 'serverkey.pem')\n cert = os.path.join(sub_dir, 'servercert.pem')\n srvinfo = os.path.join(sub_dir, 'server.info')\n cpriv = os.path.join(sub_dir, 'clientkey.pem')\n ccert = os.path.join(sub_dir, 'clientcert.pem')\n clientinfo = os.path.join(sub_dir, 'client.info')\n if not os.path.isfile(srvinfo):\n with salt.utils.files.fopen(srvinfo, 'w+') as fp_:\n infodat = salt.utils.stringutils.to_str(\n 'organization = salted\\ncn = {0}\\ntls_www_server'\n '\\nencryption_key\\nsigning_key'\n '\\ndigitalSignature\\nexpiration_days = {1}'.format(\n __grains__['fqdn'], expiration_days\n )\n )\n fp_.write(infodat)\n if not os.path.isfile(priv):\n subprocess.call(\n 'certtool --generate-privkey > {0}'.format(priv),\n shell=True)\n if not os.path.isfile(cert):\n cmd = ('certtool --generate-certificate --load-privkey {0} '\n '--load-ca-certificate {1} --load-ca-privkey {2} '\n '--template {3} --outfile {4}'\n ).format(priv, cacert, cakey, srvinfo, cert)\n subprocess.call(cmd, shell=True)\n if not os.path.isfile(clientinfo):\n with salt.utils.files.fopen(clientinfo, 'w+') as fp_:\n infodat = salt.utils.stringutils.to_str(\n 'country = {0}\\nstate = {1}\\nlocality = {2}\\n'\n 'organization = {3}\\ncn = {4}\\n'\n 'tls_www_client\\nencryption_key\\nsigning_key\\n'\n 'digitalSignature'.format(\n country,\n state,\n locality,\n organization,\n __grains__['fqdn']\n )\n )\n fp_.write(infodat)\n if not os.path.isfile(cpriv):\n subprocess.call(\n 'certtool --generate-privkey > {0}'.format(cpriv),\n shell=True)\n if not os.path.isfile(ccert):\n cmd = ('certtool --generate-certificate --load-privkey {0} '\n '--load-ca-certificate {1} --load-ca-privkey {2} '\n '--template {3} --outfile {4}'\n ).format(cpriv, cacert, cakey, clientinfo, ccert)\n subprocess.call(cmd, shell=True)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nensuring a key pair is present.", "response": "def key_present(name, save_private=None, upload_public=None, region=None,\n key=None, keyid=None, profile=None):\n '''\n Ensure key pair is present.\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n exists = __salt__['boto_ec2.get_key'](name, region, key, keyid, profile)\n log.debug('exists is %s', exists)\n if upload_public is not None and 'salt://' in upload_public:\n try:\n upload_public = __salt__['cp.get_file_str'](upload_public)\n except IOError as e:\n log.debug(e)\n ret['comment'] = 'File {0} not found.'.format(upload_public)\n ret['result'] = False\n return ret\n if not exists:\n if __opts__['test']:\n ret['comment'] = 'The key {0} is set to be created.'.format(name)\n ret['result'] = None\n return ret\n if save_private and not upload_public:\n created = __salt__['boto_ec2.create_key'](\n name, save_private, region, key, keyid, profile\n )\n if created:\n ret['result'] = True\n ret['comment'] = 'The key {0} is created.'.format(name)\n ret['changes']['new'] = created\n else:\n ret['result'] = False\n ret['comment'] = 'Could not create key {0} '.format(name)\n elif not save_private and upload_public:\n imported = __salt__['boto_ec2.import_key'](name, upload_public,\n region, key, keyid,\n profile)\n if imported:\n ret['result'] = True\n ret['comment'] = 'The key {0} is created.'.format(name)\n ret['changes']['old'] = None\n ret['changes']['new'] = imported\n else:\n ret['result'] = False\n ret['comment'] = 'Could not create key {0} '.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'You can either upload or download a private key '\n else:\n ret['result'] = True\n ret['comment'] = 'The key name {0} already exists'.format(name)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef key_absent(name, region=None, key=None, keyid=None, profile=None):\n '''\n Deletes a key pair\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n exists = __salt__['boto_ec2.get_key'](name, region, key, keyid, profile)\n if exists:\n if __opts__['test']:\n ret['comment'] = 'The key {0} is set to be deleted.'.format(name)\n ret['result'] = None\n return ret\n deleted = __salt__['boto_ec2.delete_key'](name, region,\n key, keyid,\n profile)\n log.debug('exists is %s', deleted)\n if deleted:\n ret['result'] = True\n ret['comment'] = 'The key {0} is deleted.'.format(name)\n ret['changes']['old'] = name\n else:\n ret['result'] = False\n ret['comment'] = 'Could not delete key {0} '.format(name)\n else:\n ret['result'] = True\n ret['comment'] = 'The key name {0} does not exist'.format(name)\n return ret", "response": "Ensures that the named key pair is absent."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef eni_present(\n name,\n subnet_id=None,\n subnet_name=None,\n private_ip_address=None,\n description=None,\n groups=None,\n source_dest_check=True,\n allocate_eip=None,\n arecords=None,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Ensure the EC2 ENI exists.\n\n .. versionadded:: 2016.3.0\n\n name\n Name tag associated with the ENI.\n\n subnet_id\n The VPC subnet ID the ENI will exist within.\n\n subnet_name\n The VPC subnet name the ENI will exist within.\n\n private_ip_address\n The private ip address to use for this ENI. If this is not specified\n AWS will automatically assign a private IP address to the ENI. Must be\n specified at creation time; will be ignored afterward.\n\n description\n Description of the key.\n\n groups\n A list of security groups to apply to the ENI.\n\n source_dest_check\n Boolean specifying whether source/destination checking is enabled on\n the ENI.\n\n allocate_eip\n allocate and associate an EIP to the ENI. Could be 'standard' to\n allocate Elastic IP to EC2 region or 'vpc' to get it for a\n particular VPC\n\n .. versionchanged:: 2016.11.0\n\n arecords\n A list of arecord dicts with attributes needed for the DNS add_record state.\n By default the boto_route53.add_record state will be used, which requires: name, zone, ttl, and identifier.\n See the boto_route53 state for information about these attributes.\n Other DNS modules can be called by specifying the provider keyword.\n By default, the private ENI IP address will be used, set 'public: True' in the arecord dict to use the ENI's public IP address\n\n .. versionadded:: 2016.3.0\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n '''\n if not salt.utils.data.exactly_one((subnet_id, subnet_name)):\n raise SaltInvocationError('One (but not both) of subnet_id or '\n 'subnet_name must be provided.')\n if not groups:\n raise SaltInvocationError('groups is a required argument.')\n if not isinstance(groups, list):\n raise SaltInvocationError('groups must be a list.')\n if not isinstance(source_dest_check, bool):\n raise SaltInvocationError('source_dest_check must be a bool.')\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n r = __salt__['boto_ec2.get_network_interface'](\n name=name, region=region, key=key, keyid=keyid, profile=profile\n )\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Error when attempting to find eni: {0}.'.format(\n r['error']['message']\n )\n return ret\n if not r['result']:\n if __opts__['test']:\n ret['comment'] = 'ENI is set to be created.'\n if allocate_eip:\n ret['comment'] = ' '.join([ret['comment'], 'An EIP is set to be allocated/assocaited to the ENI.'])\n if arecords:\n ret['comment'] = ' '.join([ret['comment'], 'A records are set to be created.'])\n ret['result'] = None\n return ret\n result_create = __salt__['boto_ec2.create_network_interface'](\n name, subnet_id=subnet_id, subnet_name=subnet_name, private_ip_address=private_ip_address,\n description=description, groups=groups, region=region, key=key,\n keyid=keyid, profile=profile\n )\n if 'error' in result_create:\n ret['result'] = False\n ret['comment'] = 'Failed to create ENI: {0}'.format(\n result_create['error']['message']\n )\n return ret\n r['result'] = result_create['result']\n ret['comment'] = 'Created ENI {0}'.format(name)\n ret['changes']['id'] = r['result']['id']\n else:\n _ret = _eni_attribute(\n r['result'], 'description', description, region, key, keyid,\n profile\n )\n ret['changes'] = dictupdate.update(ret['changes'], _ret['changes'])\n ret['comment'] = _ret['comment']\n if not _ret['result']:\n ret['result'] = _ret['result']\n if ret['result'] is False:\n return ret\n _ret = _eni_groups(\n r['result'], groups, region, key, keyid, profile\n )\n ret['changes'] = dictupdate.update(ret['changes'], _ret['changes'])\n ret['comment'] = ' '.join([ret['comment'], _ret['comment']])\n if not _ret['result']:\n ret['result'] = _ret['result']\n if ret['result'] is False:\n return ret\n # Actions that need to occur whether creating or updating\n _ret = _eni_attribute(\n r['result'], 'source_dest_check', source_dest_check, region, key,\n keyid, profile\n )\n ret['changes'] = dictupdate.update(ret['changes'], _ret['changes'])\n ret['comment'] = ' '.join([ret['comment'], _ret['comment']])\n if not _ret['result']:\n ret['result'] = _ret['result']\n return ret\n if allocate_eip:\n if 'allocationId' not in r['result']:\n if __opts__['test']:\n ret['comment'] = ' '.join([ret['comment'], 'An EIP is set to be allocated and assocaited to the ENI.'])\n else:\n domain = 'vpc' if allocate_eip == 'vpc' else None\n eip_alloc = __salt__['boto_ec2.allocate_eip_address'](domain=domain,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile)\n if eip_alloc:\n _ret = __salt__['boto_ec2.associate_eip_address'](instance_id=None,\n instance_name=None,\n public_ip=None,\n allocation_id=eip_alloc['allocation_id'],\n network_interface_id=r['result']['id'],\n private_ip_address=None,\n allow_reassociation=False,\n region=region,\n key=key,\n keyid=keyid,\n profile=profile)\n if not _ret:\n _ret = __salt__['boto_ec2.release_eip_address'](public_ip=None,\n allocation_id=eip_alloc['allocation_id'],\n region=region,\n key=key,\n keyid=keyid,\n profile=profile)\n ret['result'] = False\n msg = 'Failed to assocaite the allocated EIP address with the ENI. The EIP {0}'.format('was successfully released.' if _ret else 'was NOT RELEASED.')\n ret['comment'] = ' '.join([ret['comment'], msg])\n return ret\n else:\n ret['result'] = False\n ret['comment'] = ' '.join([ret['comment'], 'Failed to allocate an EIP address'])\n return ret\n else:\n ret['comment'] = ' '.join([ret['comment'], 'An EIP is already allocated/assocaited to the ENI'])\n if arecords:\n for arecord in arecords:\n if 'name' not in arecord:\n msg = 'The arecord must contain a \"name\" property.'\n raise SaltInvocationError(msg)\n log.debug('processing arecord %s', arecord)\n _ret = None\n dns_provider = 'boto_route53'\n arecord['record_type'] = 'A'\n public_ip_arecord = False\n if 'public' in arecord:\n public_ip_arecord = arecord.pop('public')\n if public_ip_arecord:\n if 'publicIp' in r['result']:\n arecord['value'] = r['result']['publicIp']\n elif 'public_ip' in eip_alloc:\n arecord['value'] = eip_alloc['public_ip']\n else:\n msg = 'Unable to add an A record for the public IP address, a public IP address does not seem to be allocated to this ENI.'\n raise CommandExecutionError(msg)\n else:\n arecord['value'] = r['result']['private_ip_address']\n if 'provider' in arecord:\n dns_provider = arecord.pop('provider')\n if dns_provider == 'boto_route53':\n if 'profile' not in arecord:\n arecord['profile'] = profile\n if 'key' not in arecord:\n arecord['key'] = key\n if 'keyid' not in arecord:\n arecord['keyid'] = keyid\n if 'region' not in arecord:\n arecord['region'] = region\n _ret = __states__['.'.join([dns_provider, 'present'])](**arecord)\n log.debug('ret from dns_provider.present = %s', _ret)\n ret['changes'] = dictupdate.update(ret['changes'], _ret['changes'])\n ret['comment'] = ' '.join([ret['comment'], _ret['comment']])\n if not _ret['result']:\n ret['result'] = _ret['result']\n if ret['result'] is False:\n return ret\n return ret", "response": "Ensure the ENI is present within the VPC."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nensuring the ENI is absent.", "response": "def eni_absent(\n name,\n release_eip=False,\n region=None,\n key=None,\n keyid=None,\n profile=None):\n '''\n Ensure the EC2 ENI is absent.\n\n .. versionadded:: 2016.3.0\n\n name\n Name tag associated with the ENI.\n\n release_eip\n True/False - release any EIP associated with the ENI\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n '''\n ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}\n r = __salt__['boto_ec2.get_network_interface'](\n name=name, region=region, key=key, keyid=keyid, profile=profile\n )\n if 'error' in r:\n ret['result'] = False\n ret['comment'] = 'Error when attempting to find eni: {0}.'.format(\n r['error']['message']\n )\n return ret\n if not r['result']:\n if __opts__['test']:\n ret['comment'] = 'ENI is set to be deleted.'\n ret['result'] = None\n return ret\n else:\n if __opts__['test']:\n ret['comment'] = 'ENI is set to be deleted.'\n if release_eip and 'allocationId' in r['result']:\n ret['comment'] = ' '.join([ret['comment'], 'Allocated/associated EIP is set to be released'])\n ret['result'] = None\n return ret\n if 'id' in r['result']['attachment']:\n result_detach = __salt__['boto_ec2.detach_network_interface'](\n name=name, force=True, region=region, key=key,\n keyid=keyid, profile=profile\n )\n if 'error' in result_detach:\n ret['result'] = False\n ret['comment'] = 'Failed to detach ENI: {0}'.format(\n result_detach['error']['message']\n )\n return ret\n # TODO: Ensure the detach occurs before continuing\n result_delete = __salt__['boto_ec2.delete_network_interface'](\n name=name, region=region, key=key,\n keyid=keyid, profile=profile\n )\n if 'error' in result_delete:\n ret['result'] = False\n ret['comment'] = 'Failed to delete ENI: {0}'.format(\n result_delete['error']['message']\n )\n return ret\n ret['comment'] = 'Deleted ENI {0}'.format(name)\n ret['changes']['id'] = None\n if release_eip and 'allocationId' in r['result']:\n _ret = __salt__['boto_ec2.release_eip_address'](public_ip=None,\n allocation_id=r['result']['allocationId'],\n region=region,\n key=key,\n keyid=keyid,\n profile=profile)\n if not _ret:\n ret['comment'] = ' '.join([ret['comment'], 'Failed to release EIP allocated to the ENI.'])\n ret['result'] = False\n return ret\n else:\n ret['comment'] = ' '.join([ret['comment'], 'EIP released.'])\n ret['changes']['eip released'] = True\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ncreate a snapshot from the given instance", "response": "def snapshot_created(name, ami_name, instance_name, wait_until_available=True, wait_timeout_seconds=300, **kwargs):\n '''\n Create a snapshot from the given instance\n\n .. versionadded:: 2016.3.0\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n\n if not __salt__['boto_ec2.create_image'](ami_name=ami_name, instance_name=instance_name, **kwargs):\n ret['comment'] = 'Failed to create new AMI {ami_name}'.format(ami_name=ami_name)\n ret['result'] = False\n return ret\n\n ret['comment'] = 'Created new AMI {ami_name}'.format(ami_name=ami_name)\n ret['changes']['new'] = {ami_name: ami_name}\n if not wait_until_available:\n return ret\n\n starttime = time()\n while True:\n images = __salt__['boto_ec2.find_images'](ami_name=ami_name, return_objs=True, **kwargs)\n if images and images[0].state == 'available':\n break\n if time() - starttime > wait_timeout_seconds:\n if images:\n ret['comment'] = 'AMI still in state {state} after timeout'.format(state=images[0].state)\n else:\n ret['comment'] = 'AMI with name {ami_name} not found after timeout.'.format(ami_name=ami_name)\n ret['result'] = False\n return ret\n sleep(5)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure an EC2 instance is present in the given state.", "response": "def instance_present(name, instance_name=None, instance_id=None, image_id=None,\n image_name=None, tags=None, key_name=None,\n security_groups=None, user_data=None, instance_type=None,\n placement=None, kernel_id=None, ramdisk_id=None,\n vpc_id=None, vpc_name=None, monitoring_enabled=None,\n subnet_id=None, subnet_name=None, private_ip_address=None,\n block_device_map=None, disable_api_termination=None,\n instance_initiated_shutdown_behavior=None,\n placement_group=None, client_token=None,\n security_group_ids=None, security_group_names=None,\n additional_info=None, tenancy=None,\n instance_profile_arn=None, instance_profile_name=None,\n ebs_optimized=None, network_interfaces=None,\n network_interface_name=None,\n network_interface_id=None,\n attributes=None, target_state=None, public_ip=None,\n allocation_id=None, allocate_eip=False, region=None,\n key=None, keyid=None, profile=None):\n ### TODO - implement 'target_state={running, stopped}'\n '''\n Ensure an EC2 instance is running with the given attributes and state.\n\n name\n (string) - The name of the state definition. Recommended that this\n match the instance_name attribute (generally the FQDN of the instance).\n instance_name\n (string) - The name of the instance, generally its FQDN. Exclusive with\n 'instance_id'.\n instance_id\n (string) - The ID of the instance (if known). Exclusive with\n 'instance_name'.\n image_id\n (string) \u2013 The ID of the AMI image to run.\n image_name\n (string) \u2013 The name of the AMI image to run.\n tags\n (dict) - Tags to apply to the instance.\n key_name\n (string) \u2013 The name of the key pair with which to launch instances.\n security_groups\n (list of strings) \u2013 The names of the EC2 classic security groups with\n which to associate instances\n user_data\n (string) \u2013 The Base64-encoded MIME user data to be made available to the\n instance(s) in this reservation.\n instance_type\n (string) \u2013 The EC2 instance size/type. Note that only certain types are\n compatible with HVM based AMIs.\n placement\n (string) \u2013 The Availability Zone to launch the instance into.\n kernel_id\n (string) \u2013 The ID of the kernel with which to launch the instances.\n ramdisk_id\n (string) \u2013 The ID of the RAM disk with which to launch the instances.\n vpc_id\n (string) - The ID of a VPC to attach the instance to.\n vpc_name\n (string) - The name of a VPC to attach the instance to.\n monitoring_enabled\n (bool) \u2013 Enable detailed CloudWatch monitoring on the instance.\n subnet_id\n (string) \u2013 The ID of the subnet within which to launch the instances for\n VPC.\n subnet_name\n (string) \u2013 The name of the subnet within which to launch the instances\n for VPC.\n private_ip_address\n (string) \u2013 If you\u2019re using VPC, you can optionally use this parameter to\n assign the instance a specific available IP address from the subnet\n (e.g., 10.0.0.25).\n block_device_map\n (boto.ec2.blockdevicemapping.BlockDeviceMapping) \u2013 A BlockDeviceMapping\n data structure describing the EBS volumes associated with the Image.\n disable_api_termination\n (bool) \u2013 If True, the instances will be locked and will not be able to\n be terminated via the API.\n instance_initiated_shutdown_behavior\n (string) \u2013 Specifies whether the instance stops or terminates on\n instance-initiated shutdown. Valid values are:\n\n - 'stop'\n - 'terminate'\n\n placement_group\n (string) \u2013 If specified, this is the name of the placement group in\n which the instance(s) will be launched.\n client_token\n (string) \u2013 Unique, case-sensitive identifier you provide to ensure\n idempotency of the request. Maximum 64 ASCII characters.\n security_group_ids\n (list of strings) \u2013 The IDs of the VPC security groups with which to\n associate instances.\n security_group_names\n (list of strings) \u2013 The names of the VPC security groups with which to\n associate instances.\n additional_info\n (string) \u2013 Specifies additional information to make available to the\n instance(s).\n tenancy\n (string) \u2013 The tenancy of the instance you want to launch. An instance\n with a tenancy of \u2018dedicated\u2019 runs on single-tenant hardware and can\n only be launched into a VPC. Valid values are:\u201ddefault\u201d or \u201cdedicated\u201d.\n NOTE: To use dedicated tenancy you MUST specify a VPC subnet-ID as well.\n instance_profile_arn\n (string) \u2013 The Amazon resource name (ARN) of the IAM Instance Profile\n (IIP) to associate with the instances.\n instance_profile_name\n (string) \u2013 The name of the IAM Instance Profile (IIP) to associate with\n the instances.\n ebs_optimized\n (bool) \u2013 Whether the instance is optimized for EBS I/O. This\n optimization provides dedicated throughput to Amazon EBS and a tuned\n configuration stack to provide optimal EBS I/O performance. This\n optimization isn\u2019t available with all instance types.\n network_interfaces\n (boto.ec2.networkinterface.NetworkInterfaceCollection) \u2013 A\n NetworkInterfaceCollection data structure containing the ENI\n specifications for the instance.\n network_interface_name\n (string) - The name of Elastic Network Interface to attach\n\n .. versionadded:: 2016.11.0\n\n network_interface_id\n (string) - The id of Elastic Network Interface to attach\n\n .. versionadded:: 2016.11.0\n\n attributes\n (dict) - Instance attributes and value to be applied to the instance.\n Available options are:\n\n - instanceType - A valid instance type (m1.small)\n - kernel - Kernel ID (None)\n - ramdisk - Ramdisk ID (None)\n - userData - Base64 encoded String (None)\n - disableApiTermination - Boolean (true)\n - instanceInitiatedShutdownBehavior - stop|terminate\n - blockDeviceMapping - List of strings - ie: [\u2018/dev/sda=false\u2019]\n - sourceDestCheck - Boolean (true)\n - groupSet - Set of Security Groups or IDs\n - ebsOptimized - Boolean (false)\n - sriovNetSupport - String - ie: \u2018simple\u2019\n\n target_state\n (string) - The desired target state of the instance. Available options\n are:\n\n - running\n - stopped\n\n Note that this option is currently UNIMPLEMENTED.\n public_ip:\n (string) - The IP of a previously allocated EIP address, which will be\n attached to the instance. EC2 Classic instances ONLY - for VCP pass in\n an allocation_id instead.\n allocation_id:\n (string) - The ID of a previously allocated EIP address, which will be\n attached to the instance. VPC instances ONLY - for Classic pass in\n a public_ip instead.\n allocate_eip:\n (bool) - Allocate and attach an EIP on-the-fly for this instance. Note\n you'll want to releaase this address when terminating the instance,\n either manually or via the 'release_eip' flag to 'instance_absent'.\n region\n (string) - Region to connect to.\n key\n (string) - Secret key to be used.\n keyid\n (string) - Access key to be used.\n profile\n (variable) - A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n\n .. versionadded:: 2016.3.0\n '''\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n _create = False\n running_states = ('pending', 'rebooting', 'running', 'stopping', 'stopped')\n changed_attrs = {}\n\n if not salt.utils.data.exactly_one((image_id, image_name)):\n raise SaltInvocationError('Exactly one of image_id OR '\n 'image_name must be provided.')\n if (public_ip or allocation_id or allocate_eip) and not salt.utils.data.exactly_one((public_ip, allocation_id, allocate_eip)):\n raise SaltInvocationError('At most one of public_ip, allocation_id OR '\n 'allocate_eip may be provided.')\n\n if instance_id:\n exists = __salt__['boto_ec2.exists'](instance_id=instance_id, region=region, key=key,\n keyid=keyid, profile=profile, in_states=running_states)\n if not exists:\n _create = True\n else:\n instances = __salt__['boto_ec2.find_instances'](name=instance_name if instance_name else name,\n region=region, key=key, keyid=keyid, profile=profile,\n in_states=running_states)\n if not instances:\n _create = True\n elif len(instances) > 1:\n log.debug('Multiple instances matching criteria found - cannot determine a singular instance-id')\n instance_id = None # No way to know, we'll just have to bail later....\n else:\n instance_id = instances[0]\n\n if _create:\n if __opts__['test']:\n ret['comment'] = 'The instance {0} is set to be created.'.format(name)\n ret['result'] = None\n return ret\n if image_name:\n args = {'ami_name': image_name, 'region': region, 'key': key,\n 'keyid': keyid, 'profile': profile}\n image_ids = __salt__['boto_ec2.find_images'](**args)\n if image_ids:\n image_id = image_ids[0]\n else:\n image_id = image_name\n r = __salt__['boto_ec2.run'](image_id, instance_name if instance_name else name,\n tags=tags, key_name=key_name,\n security_groups=security_groups, user_data=user_data,\n instance_type=instance_type, placement=placement,\n kernel_id=kernel_id, ramdisk_id=ramdisk_id, vpc_id=vpc_id,\n vpc_name=vpc_name, monitoring_enabled=monitoring_enabled,\n subnet_id=subnet_id, subnet_name=subnet_name,\n private_ip_address=private_ip_address,\n block_device_map=block_device_map,\n disable_api_termination=disable_api_termination,\n instance_initiated_shutdown_behavior=instance_initiated_shutdown_behavior,\n placement_group=placement_group, client_token=client_token,\n security_group_ids=security_group_ids,\n security_group_names=security_group_names,\n additional_info=additional_info, tenancy=tenancy,\n instance_profile_arn=instance_profile_arn,\n instance_profile_name=instance_profile_name,\n ebs_optimized=ebs_optimized, network_interfaces=network_interfaces,\n network_interface_name=network_interface_name,\n network_interface_id=network_interface_id,\n region=region, key=key, keyid=keyid, profile=profile)\n if not r or 'instance_id' not in r:\n ret['result'] = False\n ret['comment'] = 'Failed to create instance {0}.'.format(instance_name if instance_name else name)\n return ret\n\n instance_id = r['instance_id']\n ret['changes'] = {'old': {}, 'new': {}}\n ret['changes']['old']['instance_id'] = None\n ret['changes']['new']['instance_id'] = instance_id\n\n # To avoid issues we only allocate new EIPs at instance creation.\n # This might miss situations where an instance is initially created\n # created without and one is added later, but the alternative is the\n # risk of EIPs allocated at every state run.\n if allocate_eip:\n if __opts__['test']:\n ret['comment'] = 'New EIP would be allocated.'\n ret['result'] = None\n return ret\n domain = 'vpc' if vpc_id or vpc_name else None\n r = __salt__['boto_ec2.allocate_eip_address'](\n domain=domain, region=region, key=key, keyid=keyid,\n profile=profile)\n if not r:\n ret['result'] = False\n ret['comment'] = 'Failed to allocate new EIP.'\n return ret\n allocation_id = r['allocation_id']\n log.info(\"New EIP with address %s allocated.\", r['public_ip'])\n else:\n log.info(\"EIP not requested.\")\n\n if public_ip or allocation_id:\n # This can take a bit to show up, give it a chance to...\n tries = 10\n secs = 3\n for t in range(tries):\n r = __salt__['boto_ec2.get_eip_address_info'](\n addresses=public_ip, allocation_ids=allocation_id,\n region=region, key=key, keyid=keyid, profile=profile)\n if r:\n break\n else:\n log.info(\n 'Waiting up to %s secs for new EIP %s to become available',\n tries * secs, public_ip or allocation_id\n )\n time.sleep(secs)\n if not r:\n ret['result'] = False\n ret['comment'] = 'Failed to lookup EIP {0}.'.format(public_ip or allocation_id)\n return ret\n ip = r[0]['public_ip']\n if r[0].get('instance_id'):\n if r[0]['instance_id'] != instance_id:\n ret['result'] = False\n ret['comment'] = ('EIP {0} is already associated with instance '\n '{1}.'.format(public_ip if public_ip else\n allocation_id, r[0]['instance_id']))\n return ret\n else:\n if __opts__['test']:\n ret['comment'] = 'Instance {0} to be updated.'.format(name)\n ret['result'] = None\n return ret\n r = __salt__['boto_ec2.associate_eip_address'](\n instance_id=instance_id, public_ip=public_ip,\n allocation_id=allocation_id, region=region, key=key,\n keyid=keyid, profile=profile)\n if r:\n if 'new' not in ret['changes']:\n ret['changes']['new'] = {}\n ret['changes']['new']['public_ip'] = ip\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to attach EIP to instance {0}.'.format(\n instance_name if instance_name else name)\n return ret\n\n if attributes:\n for k, v in six.iteritems(attributes):\n curr = __salt__['boto_ec2.get_attribute'](k, instance_id=instance_id, region=region, key=key,\n keyid=keyid, profile=profile)\n curr = {} if not isinstance(curr, dict) else curr\n if curr.get(k) == v:\n continue\n else:\n if __opts__['test']:\n changed_attrs[k] = 'The instance attribute {0} is set to be changed from \\'{1}\\' to \\'{2}\\'.'.format(\n k, curr.get(k), v)\n continue\n try:\n r = __salt__['boto_ec2.set_attribute'](attribute=k, attribute_value=v,\n instance_id=instance_id, region=region,\n key=key, keyid=keyid, profile=profile)\n except SaltInvocationError as e:\n ret['result'] = False\n ret['comment'] = 'Failed to set attribute {0} to {1} on instance {2}.'.format(k, v, instance_name)\n return ret\n ret['changes'] = ret['changes'] if ret['changes'] else {'old': {}, 'new': {}}\n ret['changes']['old'][k] = curr.get(k)\n ret['changes']['new'][k] = v\n\n if __opts__['test']:\n if changed_attrs:\n ret['changes']['new'] = changed_attrs\n ret['result'] = None\n else:\n ret['comment'] = 'Instance {0} is in the correct state'.format(instance_name if instance_name else name)\n ret['result'] = True\n\n if tags and instance_id is not None:\n tags = dict(tags)\n curr_tags = dict(__salt__['boto_ec2.get_all_tags'](filters={'resource-id': instance_id},\n region=region, key=key, keyid=keyid, profile=profile).get(instance_id, {}))\n current = set(curr_tags.keys())\n desired = set(tags.keys())\n remove = list(current - desired) # Boto explicitly requires a list here and can't cope with a set...\n add = dict([(t, tags[t]) for t in desired - current])\n replace = dict([(t, tags[t]) for t in tags if tags.get(t) != curr_tags.get(t)])\n # Tag keys are unique despite the bizarre semantics uses which make it LOOK like they could be duplicative.\n add.update(replace)\n if add or remove:\n if __opts__['test']:\n ret['changes']['old'] = ret['changes']['old'] if 'old' in ret['changes'] else {}\n ret['changes']['new'] = ret['changes']['new'] if 'new' in ret['changes'] else {}\n ret['changes']['old']['tags'] = curr_tags\n ret['changes']['new']['tags'] = tags\n ret['comment'] += ' Tags would be updated on instance {0}.'.format(instance_name if\n instance_name else name)\n else:\n if remove:\n if not __salt__['boto_ec2.delete_tags'](resource_ids=instance_id, tags=remove,\n region=region, key=key, keyid=keyid,\n profile=profile):\n msg = \"Error while deleting tags on instance {0}\".format(instance_name if\n instance_name else name)\n log.error(msg)\n ret['comment'] += ' ' + msg\n ret['result'] = False\n return ret\n if add:\n if not __salt__['boto_ec2.create_tags'](resource_ids=instance_id, tags=add,\n region=region, key=key, keyid=keyid,\n profile=profile):\n msg = \"Error while creating tags on instance {0}\".format(instance_name if\n instance_name else name)\n log.error(msg)\n ret['comment'] += ' ' + msg\n ret['result'] = False\n return ret\n ret['changes']['old'] = ret['changes']['old'] if 'old' in ret['changes'] else {}\n ret['changes']['new'] = ret['changes']['new'] if 'new' in ret['changes'] else {}\n ret['changes']['old']['tags'] = curr_tags\n ret['changes']['new']['tags'] = tags\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef instance_absent(name, instance_name=None, instance_id=None,\n release_eip=False, region=None, key=None, keyid=None,\n profile=None, filters=None):\n '''\n Ensure an EC2 instance does not exist (is stopped and removed).\n\n .. versionchanged:: 2016.11.0\n\n name\n (string) - The name of the state definition.\n instance_name\n (string) - The name of the instance.\n instance_id\n (string) - The ID of the instance.\n release_eip\n (bool) - Release any associated EIPs during termination.\n region\n (string) - Region to connect to.\n key\n (string) - Secret key to be used.\n keyid\n (string) - Access key to be used.\n profile\n (variable) - A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n filters\n (dict) - A dict of additional filters to use in matching the instance to\n delete.\n\n YAML example fragment:\n\n .. code-block:: yaml\n\n - filters:\n vpc-id: vpc-abcdef12\n '''\n ### TODO - Implement 'force' option?? Would automagically turn off\n ### 'disableApiTermination', as needed, before trying to delete.\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n running_states = ('pending', 'rebooting', 'running', 'stopping', 'stopped')\n\n if not instance_id:\n try:\n instance_id = __salt__['boto_ec2.get_id'](name=instance_name if instance_name else name,\n region=region, key=key, keyid=keyid,\n profile=profile, in_states=running_states,\n filters=filters)\n except CommandExecutionError as e:\n ret['result'] = None\n ret['comment'] = (\"Couldn't determine current status of instance \"\n \"{0}.\".format(instance_name or name))\n return ret\n\n instances = __salt__['boto_ec2.find_instances'](instance_id=instance_id, region=region,\n key=key, keyid=keyid, profile=profile,\n return_objs=True, filters=filters)\n if not instances:\n ret['result'] = True\n ret['comment'] = 'Instance {0} is already gone.'.format(instance_id)\n return ret\n instance = instances[0]\n\n ### Honor 'disableApiTermination' - if you want to override it, first use set_attribute() to turn it off\n no_can_do = __salt__['boto_ec2.get_attribute']('disableApiTermination', instance_id=instance_id,\n region=region, key=key, keyid=keyid, profile=profile)\n if no_can_do.get('disableApiTermination') is True:\n ret['result'] = False\n ret['comment'] = 'Termination of instance {0} via the API is disabled.'.format(instance_id)\n return ret\n\n if __opts__['test']:\n ret['comment'] = 'The instance {0} is set to be deleted.'.format(name)\n ret['result'] = None\n return ret\n\n r = __salt__['boto_ec2.terminate'](instance_id=instance_id, name=instance_name, region=region,\n key=key, keyid=keyid, profile=profile)\n if not r:\n ret['result'] = False\n ret['comment'] = 'Failed to terminate instance {0}.'.format(instance_id)\n return ret\n\n ret['changes']['old'] = {'instance_id': instance_id}\n ret['changes']['new'] = None\n\n if release_eip:\n ip = getattr(instance, 'ip_address', None)\n if ip:\n base_args = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n public_ip = None\n alloc_id = None\n assoc_id = None\n if getattr(instance, 'vpc_id', None):\n r = __salt__['boto_ec2.get_eip_address_info'](addresses=ip, **base_args)\n if r and 'allocation_id' in r[0]:\n alloc_id = r[0]['allocation_id']\n assoc_id = r[0].get('association_id')\n else:\n # I /believe/ this situation is impossible but let's hedge our bets...\n ret['result'] = False\n ret['comment'] = \"Can't determine AllocationId for address {0}.\".format(ip)\n return ret\n else:\n public_ip = instance.ip_address\n\n if assoc_id:\n # Race here - sometimes the terminate above will already have dropped this\n if not __salt__['boto_ec2.disassociate_eip_address'](association_id=assoc_id,\n **base_args):\n log.warning(\"Failed to disassociate EIP %s.\", ip)\n\n if __salt__['boto_ec2.release_eip_address'](allocation_id=alloc_id, public_ip=public_ip,\n **base_args):\n log.info(\"Released EIP address %s\", public_ip or r[0]['public_ip'])\n ret['changes']['old']['public_ip'] = public_ip or r[0]['public_ip']\n else:\n ret['result'] = False\n ret['comment'] = \"Failed to release EIP {0}.\".format(ip)\n return ret\n\n return ret", "response": "Ensure an EC2 instance is absent."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef volume_absent(name, volume_name=None, volume_id=None, instance_name=None,\n instance_id=None, device=None, region=None, key=None, keyid=None, profile=None):\n '''\n Ensure the EC2 volume is detached and absent.\n\n .. versionadded:: 2016.11.0\n\n name\n State definition name.\n\n volume_name\n Name tag associated with the volume. For safety, if this matches more than\n one volume, the state will refuse to apply.\n\n volume_id\n Resource ID of the volume.\n\n instance_name\n Only remove volume if it is attached to instance with this Name tag.\n Exclusive with 'instance_id'. Requires 'device'.\n\n instance_id\n Only remove volume if it is attached to this instance.\n Exclusive with 'instance_name'. Requires 'device'.\n\n device\n Match by device rather than ID. Requires one of 'instance_name' or\n 'instance_id'.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n\n '''\n\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n filters = {}\n running_states = ('pending', 'rebooting', 'running', 'stopping', 'stopped')\n\n if not salt.utils.data.exactly_one((volume_name, volume_id, instance_name, instance_id)):\n raise SaltInvocationError(\"Exactly one of 'volume_name', 'volume_id', \"\n \"'instance_name', or 'instance_id' must be provided.\")\n if (instance_name or instance_id) and not device:\n raise SaltInvocationError(\"Parameter 'device' is required when either \"\n \"'instance_name' or 'instance_id' is specified.\")\n if volume_id:\n filters.update({'volume-id': volume_id})\n if volume_name:\n filters.update({'tag:Name': volume_name})\n if instance_name:\n instance_id = __salt__['boto_ec2.get_id'](\n name=instance_name, region=region, key=key, keyid=keyid,\n profile=profile, in_states=running_states)\n if not instance_id:\n ret['comment'] = ('Instance with Name {0} not found. Assuming '\n 'associated volumes gone.'.format(instance_name))\n return ret\n if instance_id:\n filters.update({'attachment.instance-id': instance_id})\n if device:\n filters.update({'attachment.device': device})\n\n args = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n\n vols = __salt__['boto_ec2.get_all_volumes'](filters=filters, **args)\n if not vols:\n ret['comment'] = 'Volume matching criteria not found, assuming already absent'\n return ret\n if len(vols) > 1:\n msg = \"More than one volume matched criteria, can't continue in state {0}\".format(name)\n log.error(msg)\n ret['comment'] = msg\n ret['result'] = False\n return ret\n vol = vols[0]\n log.info('Matched Volume ID %s', vol)\n\n if __opts__['test']:\n ret['comment'] = 'The volume {0} is set to be deleted.'.format(vol)\n ret['result'] = None\n return ret\n if __salt__['boto_ec2.delete_volume'](volume_id=vol, force=True, **args):\n ret['comment'] = 'Volume {0} deleted.'.format(vol)\n ret['changes'] = {'old': {'volume_id': vol}, 'new': {'volume_id': None}}\n else:\n ret['comment'] = 'Error deleting volume {0}.'.format(vol)\n ret['result'] = False\n return ret", "response": "Ensure the EC2 volume is detached and absent."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn a new state definition with the EC2 volumes tagged by the given filters.", "response": "def volumes_tagged(name, tag_maps, authoritative=False, region=None, key=None,\n keyid=None, profile=None):\n '''\n Ensure EC2 volume(s) matching the given filters have the defined tags.\n\n .. versionadded:: 2016.11.0\n\n name\n State definition name.\n\n tag_maps\n List of dicts of filters and tags, where 'filters' is a dict suitable for passing\n to the 'filters' argument of boto_ec2.get_all_volumes(), and 'tags' is a dict of\n tags to be set on volumes as matched by the given filters. The filter syntax is\n extended to permit passing either a list of volume_ids or an instance_name (with\n instance_name being the Name tag of the instance to which the desired volumes are\n mapped). Each mapping in the list is applied separately, so multiple sets of\n volumes can be all tagged differently with one call to this function.\n\n YAML example fragment:\n\n .. code-block:: yaml\n\n - filters:\n attachment.instance_id: i-abcdef12\n tags:\n Name: dev-int-abcdef12.aws-foo.com\n - filters:\n attachment.device: /dev/sdf\n tags:\n ManagedSnapshots: true\n BillingGroup: bubba.hotep@aws-foo.com\n - filters:\n instance_name: prd-foo-01.aws-foo.com\n tags:\n Name: prd-foo-01.aws-foo.com\n BillingGroup: infra-team@aws-foo.com\n - filters:\n volume_ids: [ vol-12345689, vol-abcdef12 ]\n tags:\n BillingGroup: infra-team@aws-foo.com\n\n authoritative\n Should un-declared tags currently set on matched volumes be deleted? Boolean.\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n\n '''\n\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}\n }\n args = {'tag_maps': tag_maps, 'authoritative': authoritative,\n 'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n\n if __opts__['test']:\n args['dry_run'] = True\n r = __salt__['boto_ec2.set_volumes_tags'](**args)\n if r['success']:\n if r.get('changes'):\n ret['comment'] = 'Tags would be updated.'\n ret['changes'] = r['changes']\n ret['result'] = None\n else:\n ret['comment'] = 'Error validating requested volume tags.'\n ret['result'] = False\n return ret\n r = __salt__['boto_ec2.set_volumes_tags'](**args)\n if r['success']:\n if r.get('changes'):\n ret['comment'] = 'Tags applied.'\n ret['changes'] = r['changes']\n else:\n ret['comment'] = 'Error updating requested volume tags.'\n ret['result'] = False\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef volume_present(name, volume_name=None, volume_id=None, instance_name=None,\n instance_id=None, device=None, size=None, snapshot_id=None,\n volume_type=None, iops=None, encrypted=False, kms_key_id=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Ensure the EC2 volume is present and attached.\n\n ..\n\n name\n State definition name.\n\n volume_name\n The Name tag value for the volume. If no volume with that matching name tag is found,\n a new volume will be created. If multiple volumes are matched, the state will fail.\n\n volume_id\n Resource ID of the volume. Exclusive with 'volume_name'.\n\n instance_name\n Attach volume to instance with this Name tag.\n Exclusive with 'instance_id'.\n\n instance_id\n Attach volume to instance with this ID.\n Exclusive with 'instance_name'.\n\n device\n The device on the instance through which the volume is exposed (e.g. /dev/sdh)\n\n size\n The size of the new volume, in GiB. If you're creating the volume from a snapshot\n and don't specify a volume size, the default is the snapshot size. Optionally specified\n at volume creation time; will be ignored afterward. Requires 'volume_name'.\n\n snapshot_id\n The snapshot ID from which the new Volume will be created. Optionally specified\n at volume creation time; will be ignored afterward. Requires 'volume_name'.\n\n volume_type\n The type of the volume. Optionally specified at volume creation time; will be ignored afterward.\n Requires 'volume_name'.\n Valid volume types for AWS can be found here:\n http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html\n\n iops\n The provisioned IOPS you want to associate with this volume. Optionally specified\n at volume creation time; will be ignored afterward. Requires 'volume_name'.\n\n encrypted\n Specifies whether the volume should be encrypted. Optionally specified\n at volume creation time; will be ignored afterward. Requires 'volume_name'.\n\n kms_key_id\n If encrypted is True, this KMS Key ID may be specified to encrypt volume with this key.\n Optionally specified at volume creation time; will be ignored afterward.\n Requires 'volume_name'.\n e.g.: arn:aws:kms:us-east-1:012345678910:key/abcd1234-a123-456a-a12b-a123b4cd56ef\n\n region\n Region to connect to.\n\n key\n Secret key to be used.\n\n keyid\n Access key to be used.\n\n profile\n A dict with region, key and keyid, or a pillar key (string)\n that contains a dict with region, key and keyid.\n\n '''\n\n ret = {'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {}}\n old_dict = {}\n new_dict = {}\n running_states = ('running', 'stopped')\n\n if not salt.utils.data.exactly_one((volume_name, volume_id)):\n raise SaltInvocationError(\"Exactly one of 'volume_name', 'volume_id', \"\n \" must be provided.\")\n if not salt.utils.data.exactly_one((instance_name, instance_id)):\n raise SaltInvocationError(\"Exactly one of 'instance_name', or 'instance_id'\"\n \" must be provided.\")\n if device is None:\n raise SaltInvocationError(\"Parameter 'device' is required.\")\n args = {'region': region, 'key': key, 'keyid': keyid, 'profile': profile}\n if instance_name:\n instance_id = __salt__['boto_ec2.get_id'](\n name=instance_name, in_states=running_states, **args)\n if not instance_id:\n raise SaltInvocationError('Instance with Name {0} not found.'.format(instance_name))\n\n instances = __salt__['boto_ec2.find_instances'](instance_id=instance_id, return_objs=True, **args)\n instance = instances[0]\n if volume_name:\n filters = {}\n filters.update({'tag:Name': volume_name})\n vols = __salt__['boto_ec2.get_all_volumes'](filters=filters, **args)\n if len(vols) > 1:\n msg = \"More than one volume matched volume name {0}, can't continue in state {1}\".format(volume_name,\n name)\n raise SaltInvocationError(msg)\n if not vols:\n if __opts__['test']:\n ret['comment'] = ('The volume with name {0} is set to be created and attached'\n ' on {1}({2}).'.format(volume_name, instance_id, device))\n ret['result'] = None\n return ret\n _rt = __salt__['boto_ec2.create_volume'](zone_name=instance.placement,\n size=size,\n snapshot_id=snapshot_id,\n volume_type=volume_type,\n iops=iops,\n encrypted=encrypted,\n kms_key_id=kms_key_id,\n wait_for_creation=True, **args)\n if 'result' in _rt:\n volume_id = _rt['result']\n else:\n raise SaltInvocationError('Error creating volume with name {0}.'.format(volume_name))\n _rt = __salt__['boto_ec2.set_volumes_tags'](tag_maps=[{\n 'filters': {'volume_ids': [volume_id]},\n 'tags': {'Name': volume_name}\n }], **args)\n if _rt['success'] is False:\n raise SaltInvocationError('Error updating requested volume '\n '{0} with name {1}. {2}'.format(volume_id,\n volume_name,\n _rt['comment']))\n old_dict['volume_id'] = None\n new_dict['volume_id'] = volume_id\n else:\n volume_id = vols[0]\n vols = __salt__['boto_ec2.get_all_volumes'](volume_ids=[volume_id], return_objs=True, **args)\n if not vols:\n raise SaltInvocationError('Volume {0} do not exist'.format(volume_id))\n vol = vols[0]\n if vol.zone != instance.placement:\n raise SaltInvocationError(('Volume {0} in {1} cannot attach to instance'\n ' {2} in {3}.').format(volume_id,\n vol.zone,\n instance_id,\n instance.placement))\n attach_data = vol.attach_data\n if attach_data is not None and attach_data.instance_id is not None:\n if instance_id == attach_data.instance_id and device == attach_data.device:\n ret['comment'] = 'The volume {0} is attached on {1}({2}).'.format(volume_id,\n instance_id, device)\n return ret\n else:\n if __opts__['test']:\n ret['comment'] = ('The volume {0} is set to be detached'\n ' from {1}({2} and attached on {3}({4}).').format(attach_data.instance_id,\n attach_data.devic,\n volume_id,\n instance_id,\n device)\n ret['result'] = None\n return ret\n if __salt__['boto_ec2.detach_volume'](volume_id=volume_id, wait_for_detachement=True, **args):\n ret['comment'] = 'Volume {0} is detached from {1}({2}).'.format(volume_id,\n attach_data.instance_id,\n attach_data.device)\n old_dict['instance_id'] = attach_data.instance_id\n old_dict['device'] = attach_data.device\n else:\n raise SaltInvocationError(('The volume {0} is already attached on instance {1}({2}).'\n ' Failed to detach').format(volume_id,\n attach_data.instance_id,\n attach_data.device))\n else:\n old_dict['instance_id'] = instance_id\n old_dict['device'] = None\n if __opts__['test']:\n ret['comment'] = 'The volume {0} is set to be attached on {1}({2}).'.format(volume_id,\n instance_id,\n device)\n ret['result'] = None\n return ret\n if __salt__['boto_ec2.attach_volume'](volume_id=volume_id, instance_id=instance_id,\n device=device, **args):\n ret['comment'] = ' '.join([\n ret['comment'],\n 'Volume {0} is attached on {1}({2}).'.format(volume_id, instance_id, device)])\n new_dict['instance_id'] = instance_id\n new_dict['device'] = device\n ret['changes'] = {'old': old_dict, 'new': new_dict}\n else:\n ret['comment'] = 'Error attaching volume {0} to instance {1}({2}).'.format(volume_id,\n instance_id,\n device)\n ret['result'] = False\n return ret", "response": "Ensure the EC2 volume is present and attached."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef private_ips_present(name, network_interface_name=None, network_interface_id=None,\n private_ip_addresses=None, allow_reassignment=False, region=None, key=None,\n keyid=None, profile=None):\n '''\n Ensure an ENI has secondary private ip addresses associated with it\n\n name\n (String) - State definition name\n network_interface_id\n (String) - The EC2 network interface id, example eni-123456789\n private_ip_addresses\n (List or String) - The secondary private ip address(es) that should be present on the ENI.\n allow_reassignment\n (Boolean) - If true, will reassign a secondary private ip address associated with another\n ENI. If false, state will fail if the secondary private ip address is associated with\n another ENI.\n region\n (string) - Region to connect to.\n key\n (string) - Secret key to be used.\n keyid\n (string) - Access key to be used.\n profile\n (variable) - A dict with region, key and keyid, or a pillar key (string) that contains a\n dict with region, key and keyid.\n '''\n\n if not salt.utils.data.exactly_one((network_interface_name, network_interface_id)):\n raise SaltInvocationError(\"Exactly one of 'network_interface_name', \"\n \"'network_interface_id' must be provided\")\n\n if not private_ip_addresses:\n raise SaltInvocationError(\"You must provide the private_ip_addresses to associate with the \"\n \"ENI\")\n\n ret = {\n 'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {'old': [], 'new': []}\n }\n\n get_eni_args = {\n 'name': network_interface_name,\n 'network_interface_id': network_interface_id,\n 'region': region,\n 'key': key,\n 'keyid': keyid,\n 'profile': profile\n }\n\n eni = __salt__['boto_ec2.get_network_interface'](**get_eni_args)\n\n # Check if there are any new secondary private ips to add to the eni\n if eni and eni.get('result', {}).get('private_ip_addresses'):\n for eni_pip in eni['result']['private_ip_addresses']:\n ret['changes']['old'].append(eni_pip['private_ip_address'])\n\n ips_to_add = []\n for private_ip in private_ip_addresses:\n if private_ip not in ret['changes']['old']:\n ips_to_add.append(private_ip)\n\n if ips_to_add:\n if not __opts__['test']:\n # Assign secondary private ips to ENI\n assign_ips_args = {\n 'network_interface_id': network_interface_id,\n 'private_ip_addresses': ips_to_add,\n 'allow_reassignment': allow_reassignment,\n 'region': region,\n 'key': key,\n 'keyid': keyid,\n 'profile': profile\n }\n\n __salt__['boto_ec2.assign_private_ip_addresses'](**assign_ips_args)\n\n # Verify secondary private ips were properly assigned to ENI\n eni = __salt__['boto_ec2.get_network_interface'](**get_eni_args)\n if eni and eni.get('result', {}).get('private_ip_addresses', None):\n for eni_pip in eni['result']['private_ip_addresses']:\n ret['changes']['new'].append(eni_pip['private_ip_address'])\n\n ips_not_added = []\n for private_ip in private_ip_addresses:\n if private_ip not in ret['changes']['new']:\n ips_not_added.append(private_ip)\n\n # Display results\n if ips_not_added:\n ret['result'] = False\n ret['comment'] = ('ips on eni: {0}\\n'\n 'attempted to add: {1}\\n'\n 'could not add the following ips: {2}\\n'.format(\n '\\n\\t- ' + '\\n\\t- '.join(ret['changes']['new']),\n '\\n\\t- ' + '\\n\\t- '.join(ips_to_add),\n '\\n\\t- ' + '\\n\\t- '.join(ips_not_added)))\n else:\n ret['comment'] = \"added ips: {0}\".format(\n '\\n\\t- ' + '\\n\\t- '.join(ips_to_add))\n\n # Verify there were changes\n if ret['changes']['old'] == ret['changes']['new']:\n ret['changes'] = {}\n\n else:\n # Testing mode, show that there were ips to add\n ret['comment'] = ('ips on eni: {0}\\n'\n 'ips that would be added: {1}\\n'.format(\n '\\n\\t- ' + '\\n\\t- '.join(ret['changes']['old']),\n '\\n\\t- ' + '\\n\\t- '.join(ips_to_add)))\n ret['changes'] = {}\n ret['result'] = None\n\n else:\n ret['comment'] = 'ips on eni: {0}'.format(\n '\\n\\t- ' + '\\n\\t- '.join(ret['changes']['old']))\n\n # there were no changes since we did not attempt to remove ips\n ret['changes'] = {}\n\n return ret", "response": "Ensure a secondary private ip address is present on the ENI."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nensuring that the secondary private ips are absent on the ENI.", "response": "def private_ips_absent(name, network_interface_name=None, network_interface_id=None,\n private_ip_addresses=None, region=None, key=None, keyid=None, profile=None):\n\n '''\n Ensure an ENI does not have secondary private ip addresses associated with it\n\n name\n (String) - State definition name\n network_interface_id\n (String) - The EC2 network interface id, example eni-123456789\n private_ip_addresses\n (List or String) - The secondary private ip address(es) that should be absent on the ENI.\n region\n (string) - Region to connect to.\n key\n (string) - Secret key to be used.\n keyid\n (string) - Access key to be used.\n profile\n (variable) - A dict with region, key and keyid, or a pillar key (string) that contains a\n dict with region, key and keyid.\n '''\n\n if not salt.utils.data.exactly_one((network_interface_name, network_interface_id)):\n raise SaltInvocationError(\"Exactly one of 'network_interface_name', \"\n \"'network_interface_id' must be provided\")\n\n if not private_ip_addresses:\n raise SaltInvocationError(\"You must provide the private_ip_addresses to unassociate with \"\n \"the ENI\")\n if not isinstance(private_ip_addresses, list):\n private_ip_addresses = [private_ip_addresses]\n\n ret = {\n 'name': name,\n 'result': True,\n 'comment': '',\n 'changes': {'new': [], 'old': []}\n }\n\n get_eni_args = {\n 'name': network_interface_name,\n 'network_interface_id': network_interface_id,\n 'region': region,\n 'key': key,\n 'keyid': keyid,\n 'profile': profile\n }\n\n eni = __salt__['boto_ec2.get_network_interface'](**get_eni_args)\n\n # Check if there are any old private ips to remove from the eni\n primary_private_ip = None\n if eni and eni.get('result', {}).get('private_ip_addresses'):\n for eni_pip in eni['result']['private_ip_addresses']:\n ret['changes']['old'].append(eni_pip['private_ip_address'])\n if eni_pip['primary']:\n primary_private_ip = eni_pip['private_ip_address']\n\n ips_to_remove = []\n for private_ip in private_ip_addresses:\n if private_ip in ret['changes']['old']:\n ips_to_remove.append(private_ip)\n if private_ip == primary_private_ip:\n ret['result'] = False\n ret['comment'] = ('You cannot unassign the primary private ip address ({0}) on an '\n 'eni\\n'\n 'ips on eni: {1}\\n'\n 'attempted to remove: {2}\\n'.format(\n primary_private_ip,\n '\\n\\t- ' + '\\n\\t- '.join(ret['changes']['old']),\n '\\n\\t- ' + '\\n\\t- '.join(private_ip_addresses)))\n ret['changes'] = {}\n return ret\n\n if ips_to_remove:\n if not __opts__['test']:\n # Unassign secondary private ips to ENI\n assign_ips_args = {\n 'network_interface_id': network_interface_id,\n 'private_ip_addresses': ips_to_remove,\n 'region': region,\n 'key': key,\n 'keyid': keyid,\n 'profile': profile\n }\n\n __salt__['boto_ec2.unassign_private_ip_addresses'](**assign_ips_args)\n\n # Verify secondary private ips were properly unassigned from ENI\n eni = __salt__['boto_ec2.get_network_interface'](**get_eni_args)\n if eni and eni.get('result', {}).get('private_ip_addresses', None):\n for eni_pip in eni['result']['private_ip_addresses']:\n ret['changes']['new'].append(eni_pip['private_ip_address'])\n ips_not_removed = []\n for private_ip in private_ip_addresses:\n if private_ip in ret['changes']['new']:\n ips_not_removed.append(private_ip)\n\n if ips_not_removed:\n ret['result'] = False\n ret['comment'] = ('ips on eni: {0}\\n'\n 'attempted to remove: {1}\\n'\n 'could not remove the following ips: {2}\\n'.format(\n '\\n\\t- ' + '\\n\\t- '.join(ret['changes']['new']),\n '\\n\\t- ' + '\\n\\t- '.join(ips_to_remove),\n '\\n\\t- ' + '\\n\\t- '.join(ips_not_removed)))\n else:\n ret['comment'] = \"removed ips: {0}\".format('\\n\\t- ' + '\\n\\t- '.join(ips_to_remove))\n\n # Verify there were changes\n if ret['changes']['old'] == ret['changes']['new']:\n ret['changes'] = {}\n\n else:\n # Testing mode, show that there were ips to remove\n ret['comment'] = ('ips on eni: {0}\\n'\n 'ips that would be removed: {1}\\n'.format(\n '\\n\\t- ' + '\\n\\t- '.join(ret['changes']['old']),\n '\\n\\t- ' + '\\n\\t- '.join(ips_to_remove)))\n ret['changes'] = {}\n ret['result'] = None\n\n else:\n ret['comment'] = 'ips on network interface: {0}'.format(\n '\\n\\t- ' + '\\n\\t- '.join(ret['changes']['old']))\n\n # there were no changes since we did not attempt to remove ips\n ret['changes'] = {}\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nadds default values to the kwargs if they are not already set.", "response": "def _post_processing(kwargs, skip_translate, invalid): # pylint: disable=unused-argument\n '''\n Additional network-specific post-translation processing\n '''\n # If any defaults were not expicitly passed, add them\n for item in DEFAULTS:\n if item not in kwargs:\n kwargs[item] = DEFAULTS[item]"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget a LocalClient object based on the config file.", "response": "def get_local_client(\n c_path=os.path.join(syspaths.CONFIG_DIR, 'master'),\n mopts=None,\n skip_perm_errors=False,\n io_loop=None,\n auto_reconnect=False):\n '''\n .. versionadded:: 2014.7.0\n\n Read in the config and return the correct LocalClient object based on\n the configured transport\n\n :param IOLoop io_loop: io_loop used for events.\n Pass in an io_loop if you want asynchronous\n operation for obtaining events. Eg use of\n set_event_handler() API. Otherwise, operation\n will be synchronous.\n '''\n if mopts:\n opts = mopts\n else:\n # Late import to prevent circular import\n import salt.config\n opts = salt.config.client_config(c_path)\n\n # TODO: AIO core is separate from transport\n return LocalClient(\n mopts=opts,\n skip_perm_errors=skip_perm_errors,\n io_loop=io_loop,\n auto_reconnect=auto_reconnect)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef __read_master_key(self):\n '''\n Read in the rotating master authentication key\n '''\n key_user = self.salt_user\n if key_user == 'root':\n if self.opts.get('user', 'root') != 'root':\n key_user = self.opts.get('user', 'root')\n if key_user.startswith('sudo_'):\n key_user = self.opts.get('user', 'root')\n if salt.utils.platform.is_windows():\n # The username may contain '\\' if it is in Windows\n # 'DOMAIN\\username' format. Fix this for the keyfile path.\n key_user = key_user.replace('\\\\', '_')\n keyfile = os.path.join(self.opts['cachedir'],\n '.{0}_key'.format(key_user))\n try:\n # Make sure all key parent directories are accessible\n salt.utils.verify.check_path_traversal(self.opts['cachedir'],\n key_user,\n self.skip_perm_errors)\n with salt.utils.files.fopen(keyfile, 'r') as key:\n return salt.utils.stringutils.to_unicode(key.read())\n except (OSError, IOError, SaltClientError):\n # Fall back to eauth\n return ''", "response": "Read in the rotating master authentication key"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting a seco. range into a list target", "response": "def _convert_range_to_list(self, tgt):\n '''\n convert a seco.range range into a list target\n '''\n range_ = seco.range.Range(self.opts['range_server'])\n try:\n return range_.expand(tgt)\n except seco.range.RangeException as err:\n print('Range server exception: {0}'.format(err))\n return []"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_timeout(self, timeout):\n '''\n Return the timeout to use\n '''\n if timeout is None:\n return self.opts['timeout']\n if isinstance(timeout, int):\n return timeout\n if isinstance(timeout, six.string_types):\n try:\n return int(timeout)\n except ValueError:\n return self.opts['timeout']\n # Looks like the timeout is invalid, use config\n return self.opts['timeout']", "response": "Return the timeout to use\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef gather_job_info(self, jid, tgt, tgt_type, listen=True, **kwargs):\n '''\n Return the information about a given job\n '''\n log.debug('Checking whether jid %s is still running', jid)\n timeout = int(kwargs.get('gather_job_timeout', self.opts['gather_job_timeout']))\n\n pub_data = self.run_job(tgt,\n 'saltutil.find_job',\n arg=[jid],\n tgt_type=tgt_type,\n timeout=timeout,\n listen=listen,\n **kwargs\n )\n\n if 'jid' in pub_data:\n self.event.subscribe(pub_data['jid'])\n\n return pub_data", "response": "Return the information about a given job"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef cmd_async(\n self,\n tgt,\n fun,\n arg=(),\n tgt_type='glob',\n ret='',\n jid='',\n kwarg=None,\n **kwargs):\n '''\n Asynchronously send a command to connected minions\n\n The function signature is the same as :py:meth:`cmd` with the\n following exceptions.\n\n :returns: A job ID or 0 on failure.\n\n .. code-block:: python\n\n >>> local.cmd_async('*', 'test.sleep', [300])\n '20131219215921857715'\n '''\n pub_data = self.run_job(tgt,\n fun,\n arg,\n tgt_type,\n ret,\n jid=jid,\n kwarg=kwarg,\n listen=False,\n **kwargs)\n try:\n return pub_data['jid']\n except KeyError:\n return 0", "response": "Asynchronously send a command to connected minions"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef cmd_subset(\n self,\n tgt,\n fun,\n arg=(),\n tgt_type='glob',\n ret='',\n kwarg=None,\n sub=3,\n cli=False,\n progress=False,\n full_return=False,\n **kwargs):\n '''\n Execute a command on a random subset of the targeted systems\n\n The function signature is the same as :py:meth:`cmd` with the\n following exceptions.\n\n :param sub: The number of systems to execute on\n :param cli: When this is set to True, a generator is returned,\n otherwise a dictionary of the minion returns is returned\n\n .. code-block:: python\n\n >>> SLC.cmd_subset('*', 'test.ping', sub=1)\n {'jerry': True}\n '''\n minion_ret = self.cmd(tgt,\n 'sys.list_functions',\n tgt_type=tgt_type,\n **kwargs)\n minions = list(minion_ret)\n random.shuffle(minions)\n f_tgt = []\n for minion in minions:\n if fun in minion_ret[minion]:\n f_tgt.append(minion)\n if len(f_tgt) >= sub:\n break\n func = self.cmd\n if cli:\n func = self.cmd_cli\n return func(\n f_tgt,\n fun,\n arg,\n tgt_type='list',\n ret=ret,\n kwarg=kwarg,\n progress=progress,\n full_return=full_return,\n **kwargs)", "response": "Execute a command on a random subset of the targeted systems."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nexecuting a command on subsets of minions at a time", "response": "def cmd_batch(\n self,\n tgt,\n fun,\n arg=(),\n tgt_type='glob',\n ret='',\n kwarg=None,\n batch='10%',\n **kwargs):\n '''\n Iteratively execute a command on subsets of minions at a time\n\n The function signature is the same as :py:meth:`cmd` with the\n following exceptions.\n\n :param batch: The batch identifier of systems to execute on\n\n :returns: A generator of minion returns\n\n .. code-block:: python\n\n >>> returns = local.cmd_batch('*', 'state.highstate', batch='10%')\n >>> for ret in returns:\n ... print(ret)\n {'jerry': {...}}\n {'dave': {...}}\n {'stewart': {...}}\n '''\n # Late import - not used anywhere else in this file\n import salt.cli.batch\n opts = salt.cli.batch.batch_get_opts(\n tgt, fun, batch, self.opts,\n arg=arg, tgt_type=tgt_type, ret=ret, kwarg=kwarg, **kwargs)\n\n eauth = salt.cli.batch.batch_get_eauth(kwargs)\n\n arg = salt.utils.args.parse_input(arg, kwargs=kwarg)\n opts = {'tgt': tgt,\n 'fun': fun,\n 'arg': arg,\n 'tgt_type': tgt_type,\n 'ret': ret,\n 'batch': batch,\n 'failhard': kwargs.get('failhard', False),\n 'raw': kwargs.get('raw', False)}\n\n if 'timeout' in kwargs:\n opts['timeout'] = kwargs['timeout']\n if 'gather_job_timeout' in kwargs:\n opts['gather_job_timeout'] = kwargs['gather_job_timeout']\n if 'batch_wait' in kwargs:\n opts['batch_wait'] = int(kwargs['batch_wait'])\n\n eauth = {}\n if 'eauth' in kwargs:\n eauth['eauth'] = kwargs.pop('eauth')\n if 'username' in kwargs:\n eauth['username'] = kwargs.pop('username')\n if 'password' in kwargs:\n eauth['password'] = kwargs.pop('password')\n if 'token' in kwargs:\n eauth['token'] = kwargs.pop('token')\n\n for key, val in six.iteritems(self.opts):\n if key not in opts:\n opts[key] = val\n\n batch = salt.cli.batch.Batch(opts, eauth=eauth, quiet=True)\n for ret in batch.run():\n yield ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nexecutes a command on the specified minions of a specific type.", "response": "def cmd(self,\n tgt,\n fun,\n arg=(),\n timeout=None,\n tgt_type='glob',\n ret='',\n jid='',\n full_return=False,\n kwarg=None,\n **kwargs):\n '''\n Synchronously execute a command on targeted minions\n\n The cmd method will execute and wait for the timeout period for all\n minions to reply, then it will return all minion data at once.\n\n .. code-block:: python\n\n >>> import salt.client\n >>> local = salt.client.LocalClient()\n >>> local.cmd('*', 'cmd.run', ['whoami'])\n {'jerry': 'root'}\n\n With extra keyword arguments for the command function to be run:\n\n .. code-block:: python\n\n local.cmd('*', 'test.arg', ['arg1', 'arg2'], kwarg={'foo': 'bar'})\n\n Compound commands can be used for multiple executions in a single\n publish. Function names and function arguments are provided in separate\n lists but the index values must correlate and an empty list must be\n used if no arguments are required.\n\n .. code-block:: python\n\n >>> local.cmd('*', [\n 'grains.items',\n 'sys.doc',\n 'cmd.run',\n ],\n [\n [],\n [],\n ['uptime'],\n ])\n\n :param tgt: Which minions to target for the execution. Default is shell\n glob. Modified by the ``tgt_type`` option.\n :type tgt: string or list\n\n :param fun: The module and function to call on the specified minions of\n the form ``module.function``. For example ``test.ping`` or\n ``grains.items``.\n\n Compound commands\n Multiple functions may be called in a single publish by\n passing a list of commands. This can dramatically lower\n overhead and speed up the application communicating with Salt.\n\n This requires that the ``arg`` param is a list of lists. The\n ``fun`` list and the ``arg`` list must correlate by index\n meaning a function that does not take arguments must still have\n a corresponding empty list at the expected index.\n :type fun: string or list of strings\n\n :param arg: A list of arguments to pass to the remote function. If the\n function takes no arguments ``arg`` may be omitted except when\n executing a compound command.\n :type arg: list or list-of-lists\n\n :param timeout: Seconds to wait after the last minion returns but\n before all minions return.\n\n :param tgt_type: The type of ``tgt``. Allowed values:\n\n * ``glob`` - Bash glob completion - Default\n * ``pcre`` - Perl style regular expression\n * ``list`` - Python list of hosts\n * ``grain`` - Match based on a grain comparison\n * ``grain_pcre`` - Grain comparison with a regex\n * ``pillar`` - Pillar data comparison\n * ``pillar_pcre`` - Pillar data comparison with a regex\n * ``nodegroup`` - Match on nodegroup\n * ``range`` - Use a Range server for matching\n * ``compound`` - Pass a compound match string\n * ``ipcidr`` - Match based on Subnet (CIDR notation) or IPv4 address.\n\n .. versionchanged:: 2017.7.0\n Renamed from ``expr_form`` to ``tgt_type``\n\n :param ret: The returner to use. The value passed can be single\n returner, or a comma delimited list of returners to call in order\n on the minions\n\n :param kwarg: A dictionary with keyword arguments for the function.\n\n :param full_return: Output the job return only (default) or the full\n return including exit code and other job metadata.\n\n :param kwargs: Optional keyword arguments.\n Authentication credentials may be passed when using\n :conf_master:`external_auth`.\n\n For example: ``local.cmd('*', 'test.ping', username='saltdev',\n password='saltdev', eauth='pam')``.\n Or: ``local.cmd('*', 'test.ping',\n token='5871821ea51754fdcea8153c1c745433')``\n\n :returns: A dictionary with the result of the execution, keyed by\n minion ID. A compound command will return a sub-dictionary keyed by\n function name.\n '''\n was_listening = self.event.cpub\n\n try:\n pub_data = self.run_job(tgt,\n fun,\n arg,\n tgt_type,\n ret,\n timeout,\n jid,\n kwarg=kwarg,\n listen=True,\n **kwargs)\n\n if not pub_data:\n return pub_data\n\n ret = {}\n for fn_ret in self.get_cli_event_returns(\n pub_data['jid'],\n pub_data['minions'],\n self._get_timeout(timeout),\n tgt,\n tgt_type,\n **kwargs):\n\n if fn_ret:\n for mid, data in six.iteritems(fn_ret):\n ret[mid] = (data if full_return\n else data.get('ret', {}))\n\n for failed in list(set(pub_data['minions']) - set(ret)):\n ret[failed] = False\n return ret\n finally:\n if not was_listening:\n self.event.close_pub()"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef cmd_cli(\n self,\n tgt,\n fun,\n arg=(),\n timeout=None,\n tgt_type='glob',\n ret='',\n verbose=False,\n kwarg=None,\n progress=False,\n **kwargs):\n '''\n Used by the :command:`salt` CLI. This method returns minion returns as\n they come back and attempts to block until all minions return.\n\n The function signature is the same as :py:meth:`cmd` with the\n following exceptions.\n\n :param verbose: Print extra information about the running command\n :returns: A generator\n '''\n was_listening = self.event.cpub\n\n if fun.startswith('state.'):\n ref = {'compound': '-C',\n 'glob': '',\n 'grain': '-G',\n 'grain_pcre': '-P',\n 'ipcidr': '-S',\n 'list': '-L',\n 'nodegroup': '-N',\n 'pcre': '-E',\n 'pillar': '-I',\n 'pillar_pcre': '-J'}\n if HAS_RANGE:\n ref['range'] = '-R'\n if ref[tgt_type].startswith('-'):\n self.target_data = \"{0} '{1}'\".format(\n ref[tgt_type],\n ','.join(tgt) if isinstance(tgt, list) else tgt)\n else:\n self.target_data = ','.join(tgt) if isinstance(tgt, list) else tgt\n else:\n self.target_data = ''\n\n try:\n self.pub_data = self.run_job(\n tgt,\n fun,\n arg,\n tgt_type,\n ret,\n timeout,\n kwarg=kwarg,\n listen=True,\n **kwargs)\n\n if not self.pub_data:\n yield self.pub_data\n else:\n try:\n for fn_ret in self.get_cli_event_returns(\n self.pub_data['jid'],\n self.pub_data['minions'],\n self._get_timeout(timeout),\n tgt,\n tgt_type,\n verbose,\n progress,\n **kwargs):\n\n if not fn_ret:\n continue\n\n yield fn_ret\n except KeyboardInterrupt:\n exit_msg = (\n '\\nExiting gracefully on Ctrl-c'\n '\\n'\n 'This job\\'s jid is: {0}\\n'\n 'The minions may not have all finished running and any '\n 'remaining minions will return upon completion.\\n\\n'\n 'To look up the return data for this job later, run the '\n 'following command:\\n'\n 'salt-run jobs.lookup_jid {0}'.format(self.pub_data['jid']))\n if self.target_data:\n exit_msg += (\n '\\n\\n'\n 'To set up the state run to safely exit, run the following command:\\n'\n 'salt {0} state.soft_kill {1}'.format(self.target_data, self.pub_data['jid']))\n raise SystemExit(exit_msg)\n finally:\n if not was_listening:\n self.event.close_pub()", "response": "This method is used by the CLI to run the command and return the output of the command."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef cmd_iter(\n self,\n tgt,\n fun,\n arg=(),\n timeout=None,\n tgt_type='glob',\n ret='',\n kwarg=None,\n **kwargs):\n '''\n Yields the individual minion returns as they come in\n\n The function signature is the same as :py:meth:`cmd` with the\n following exceptions.\n\n Normally :py:meth:`cmd_iter` does not yield results for minions that\n are not connected. If you want it to return results for disconnected\n minions set `expect_minions=True` in `kwargs`.\n\n :return: A generator yielding the individual minion returns\n\n .. code-block:: python\n\n >>> ret = local.cmd_iter('*', 'test.ping')\n >>> for i in ret:\n ... print(i)\n {'jerry': {'ret': True}}\n {'dave': {'ret': True}}\n {'stewart': {'ret': True}}\n '''\n was_listening = self.event.cpub\n\n try:\n pub_data = self.run_job(\n tgt,\n fun,\n arg,\n tgt_type,\n ret,\n timeout,\n kwarg=kwarg,\n listen=True,\n **kwargs)\n\n if not pub_data:\n yield pub_data\n else:\n if kwargs.get('yield_pub_data'):\n yield pub_data\n for fn_ret in self.get_iter_returns(pub_data['jid'],\n pub_data['minions'],\n timeout=self._get_timeout(timeout),\n tgt=tgt,\n tgt_type=tgt_type,\n **kwargs):\n if not fn_ret:\n continue\n yield fn_ret\n self._clean_up_subscriptions(pub_data['jid'])\n finally:\n if not was_listening:\n self.event.close_pub()", "response": "A generator that yields the individual minion returns as they come in."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef cmd_iter_no_block(\n self,\n tgt,\n fun,\n arg=(),\n timeout=None,\n tgt_type='glob',\n ret='',\n kwarg=None,\n show_jid=False,\n verbose=False,\n **kwargs):\n '''\n Yields the individual minion returns as they come in, or None\n when no returns are available.\n\n The function signature is the same as :py:meth:`cmd` with the\n following exceptions.\n\n :returns: A generator yielding the individual minion returns, or None\n when no returns are available. This allows for actions to be\n injected in between minion returns.\n\n .. code-block:: python\n\n >>> ret = local.cmd_iter_no_block('*', 'test.ping')\n >>> for i in ret:\n ... print(i)\n None\n {'jerry': {'ret': True}}\n {'dave': {'ret': True}}\n None\n {'stewart': {'ret': True}}\n '''\n was_listening = self.event.cpub\n\n try:\n pub_data = self.run_job(\n tgt,\n fun,\n arg,\n tgt_type,\n ret,\n timeout,\n kwarg=kwarg,\n listen=True,\n **kwargs)\n\n if not pub_data:\n yield pub_data\n else:\n for fn_ret in self.get_iter_returns(pub_data['jid'],\n pub_data['minions'],\n timeout=timeout,\n tgt=tgt,\n tgt_type=tgt_type,\n block=False,\n **kwargs):\n if fn_ret and any([show_jid, verbose]):\n for minion in fn_ret:\n fn_ret[minion]['jid'] = pub_data['jid']\n yield fn_ret\n\n self._clean_up_subscriptions(pub_data['jid'])\n finally:\n if not was_listening:\n self.event.close_pub()", "response": "A generator that yields the individual minion returns as they come in or None when no returns are available."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef cmd_full_return(\n self,\n tgt,\n fun,\n arg=(),\n timeout=None,\n tgt_type='glob',\n ret='',\n verbose=False,\n kwarg=None,\n **kwargs):\n '''\n Execute a salt command and return\n '''\n was_listening = self.event.cpub\n\n try:\n pub_data = self.run_job(\n tgt,\n fun,\n arg,\n tgt_type,\n ret,\n timeout,\n kwarg=kwarg,\n listen=True,\n **kwargs)\n\n if not pub_data:\n return pub_data\n\n return (self.get_cli_static_event_returns(pub_data['jid'],\n pub_data['minions'],\n timeout,\n tgt,\n tgt_type,\n verbose))\n finally:\n if not was_listening:\n self.event.close_pub()", "response": "Execute a salt command and return a dict of data"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting the return data for a specified job", "response": "def get_cli_returns(\n self,\n jid,\n minions,\n timeout=None,\n tgt='*',\n tgt_type='glob',\n verbose=False,\n show_jid=False,\n **kwargs):\n '''\n Starts a watcher looking at the return data for a specified JID\n\n :returns: all of the information for the JID\n '''\n if verbose:\n msg = 'Executing job with jid {0}'.format(jid)\n print(msg)\n print('-' * len(msg) + '\\n')\n elif show_jid:\n print('jid: {0}'.format(jid))\n if timeout is None:\n timeout = self.opts['timeout']\n fret = {}\n # make sure the minions is a set (since we do set operations on it)\n minions = set(minions)\n\n found = set()\n # start this before the cache lookup-- in case new stuff comes in\n event_iter = self.get_event_iter_returns(jid, minions, timeout=timeout)\n\n # get the info from the cache\n ret = self.get_cache_returns(jid)\n if ret != {}:\n found.update(set(ret))\n yield ret\n\n # if you have all the returns, stop\n if len(found.intersection(minions)) >= len(minions):\n raise StopIteration()\n\n # otherwise, get them from the event system\n for event in event_iter:\n if event != {}:\n found.update(set(event))\n yield event\n if len(found.intersection(minions)) >= len(minions):\n self._clean_up_subscriptions(jid)\n raise StopIteration()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_returns_no_block(\n self,\n tag,\n match_type=None):\n '''\n Raw function to just return events of jid excluding timeout logic\n\n Yield either the raw event data or None\n\n Pass a list of additional regular expressions as `tags_regex` to search\n the event bus for non-return data, such as minion lists returned from\n syndics.\n '''\n\n while True:\n raw = self.event.get_event(wait=0.01, tag=tag, match_type=match_type, full=True,\n no_block=True, auto_reconnect=self.auto_reconnect)\n yield raw", "response": "Get the raw event data of a specific tag and block until it is available"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget the return data for a specific job", "response": "def get_iter_returns(\n self,\n jid,\n minions,\n timeout=None,\n tgt='*',\n tgt_type='glob',\n expect_minions=False,\n block=True,\n **kwargs):\n '''\n Watch the event system and return job data as it comes in\n\n :returns: all of the information for the JID\n '''\n if not isinstance(minions, set):\n if isinstance(minions, six.string_types):\n minions = set([minions])\n elif isinstance(minions, (list, tuple)):\n minions = set(list(minions))\n\n if timeout is None:\n timeout = self.opts['timeout']\n gather_job_timeout = int(kwargs.get('gather_job_timeout', self.opts['gather_job_timeout']))\n start = int(time.time())\n\n # timeouts per minion, id_ -> timeout time\n minion_timeouts = {}\n\n found = set()\n missing = set()\n # Check to see if the jid is real, if not return the empty dict\n try:\n if self.returners['{0}.get_load'.format(self.opts['master_job_cache'])](jid) == {}:\n log.warning('jid does not exist')\n yield {}\n # stop the iteration, since the jid is invalid\n raise StopIteration()\n except Exception as exc:\n log.warning('Returner unavailable: %s', exc, exc_info_on_loglevel=logging.DEBUG)\n # Wait for the hosts to check in\n last_time = False\n # iterator for this job's return\n if self.opts['order_masters']:\n # If we are a MoM, we need to gather expected minions from downstreams masters.\n ret_iter = self.get_returns_no_block('(salt/job|syndic/.*)/{0}'.format(jid), 'regex')\n else:\n ret_iter = self.get_returns_no_block('salt/job/{0}'.format(jid))\n # iterator for the info of this job\n jinfo_iter = []\n # open event jids that need to be un-subscribed from later\n open_jids = set()\n timeout_at = time.time() + timeout\n gather_syndic_wait = time.time() + self.opts['syndic_wait']\n # are there still minions running the job out there\n # start as True so that we ping at least once\n minions_running = True\n log.debug(\n 'get_iter_returns for jid %s sent to %s will timeout at %s',\n jid, minions, datetime.fromtimestamp(timeout_at).time()\n )\n while True:\n # Process events until timeout is reached or all minions have returned\n for raw in ret_iter:\n # if we got None, then there were no events\n if raw is None:\n break\n if 'minions' in raw.get('data', {}):\n minions.update(raw['data']['minions'])\n if 'missing' in raw.get('data', {}):\n missing.update(raw['data']['missing'])\n continue\n if 'return' not in raw['data']:\n continue\n if kwargs.get('raw', False):\n found.add(raw['data']['id'])\n yield raw\n else:\n found.add(raw['data']['id'])\n ret = {raw['data']['id']: {'ret': raw['data']['return']}}\n if 'out' in raw['data']:\n ret[raw['data']['id']]['out'] = raw['data']['out']\n if 'retcode' in raw['data']:\n ret[raw['data']['id']]['retcode'] = raw['data']['retcode']\n if 'jid' in raw['data']:\n ret[raw['data']['id']]['jid'] = raw['data']['jid']\n if kwargs.get('_cmd_meta', False):\n ret[raw['data']['id']].update(raw['data'])\n log.debug('jid %s return from %s', jid, raw['data']['id'])\n yield ret\n\n # if we have all of the returns (and we aren't a syndic), no need for anything fancy\n if len(found.intersection(minions)) >= len(minions) and not self.opts['order_masters']:\n # All minions have returned, break out of the loop\n log.debug('jid %s found all minions %s', jid, found)\n break\n elif len(found.intersection(minions)) >= len(minions) and self.opts['order_masters']:\n if len(found) >= len(minions) and len(minions) > 0 and time.time() > gather_syndic_wait:\n # There were some minions to find and we found them\n # However, this does not imply that *all* masters have yet responded with expected minion lists.\n # Therefore, continue to wait up to the syndic_wait period (calculated in gather_syndic_wait) to see\n # if additional lower-level masters deliver their lists of expected\n # minions.\n break\n # If we get here we may not have gathered the minion list yet. Keep waiting\n # for all lower-level masters to respond with their minion lists\n\n # let start the timeouts for all remaining minions\n\n for id_ in minions - found:\n # if we have a new minion in the list, make sure it has a timeout\n if id_ not in minion_timeouts:\n minion_timeouts[id_] = time.time() + timeout\n\n # if the jinfo has timed out and some minions are still running the job\n # re-do the ping\n if time.time() > timeout_at and minions_running:\n # since this is a new ping, no one has responded yet\n jinfo = self.gather_job_info(jid, list(minions - found), 'list', **kwargs)\n minions_running = False\n # if we weren't assigned any jid that means the master thinks\n # we have nothing to send\n if 'jid' not in jinfo:\n jinfo_iter = []\n else:\n jinfo_iter = self.get_returns_no_block('salt/job/{0}'.format(jinfo['jid']))\n timeout_at = time.time() + gather_job_timeout\n # if you are a syndic, wait a little longer\n if self.opts['order_masters']:\n timeout_at += self.opts.get('syndic_wait', 1)\n\n # check for minions that are running the job still\n for raw in jinfo_iter:\n # if there are no more events, lets stop waiting for the jinfo\n if raw is None:\n break\n try:\n if raw['data']['retcode'] > 0:\n log.error('saltutil returning errors on minion %s', raw['data']['id'])\n minions.remove(raw['data']['id'])\n break\n except KeyError as exc:\n # This is a safe pass. We're just using the try/except to\n # avoid having to deep-check for keys.\n missing_key = exc.__str__().strip('\\'\"')\n if missing_key == 'retcode':\n log.debug('retcode missing from client return')\n else:\n log.debug(\n 'Passing on saltutil error. Key \\'%s\\' missing '\n 'from client return. This may be an error in '\n 'the client.', missing_key\n )\n # Keep track of the jid events to unsubscribe from later\n open_jids.add(jinfo['jid'])\n\n # TODO: move to a library??\n if 'minions' in raw.get('data', {}):\n minions.update(raw['data']['minions'])\n continue\n if 'syndic' in raw.get('data', {}):\n minions.update(raw['syndic'])\n continue\n if 'return' not in raw.get('data', {}):\n continue\n\n # if the job isn't running there anymore... don't count\n if raw['data']['return'] == {}:\n continue\n\n # if the minion throws an exception containing the word \"return\"\n # the master will try to handle the string as a dict in the next\n # step. Check if we have a string, log the issue and continue.\n if isinstance(raw['data']['return'], six.string_types):\n log.error(\"unexpected return from minion: %s\", raw)\n continue\n\n if 'return' in raw['data']['return'] and \\\n raw['data']['return']['return'] == {}:\n continue\n\n # if we didn't originally target the minion, lets add it to the list\n if raw['data']['id'] not in minions:\n minions.add(raw['data']['id'])\n # update this minion's timeout, as long as the job is still running\n minion_timeouts[raw['data']['id']] = time.time() + timeout\n # a minion returned, so we know its running somewhere\n minions_running = True\n\n # if we have hit gather_job_timeout (after firing the job) AND\n # if we have hit all minion timeouts, lets call it\n now = time.time()\n # if we have finished waiting, and no minions are running the job\n # then we need to see if each minion has timedout\n done = (now > timeout_at) and not minions_running\n if done:\n # if all minions have timeod out\n for id_ in minions - found:\n if now < minion_timeouts[id_]:\n done = False\n break\n if done:\n break\n\n # don't spin\n if block:\n time.sleep(0.01)\n else:\n yield\n\n # If there are any remaining open events, clean them up.\n if open_jids:\n for jid in open_jids:\n self.event.unsubscribe(jid)\n\n if expect_minions:\n for minion in list((minions - found)):\n yield {minion: {'failed': True}}\n\n # Filter out any minions marked as missing for which we received\n # returns (prevents false events sent due to higher-level masters not\n # knowing about lower-level minions).\n missing -= found\n\n # Report on missing minions\n if missing:\n for minion in missing:\n yield {minion: {'failed': True}}"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ngets the returns for the command line interface via the event system", "response": "def get_returns(\n self,\n jid,\n minions,\n timeout=None):\n '''\n Get the returns for the command line interface via the event system\n '''\n minions = set(minions)\n if timeout is None:\n timeout = self.opts['timeout']\n start = int(time.time())\n timeout_at = start + timeout\n log.debug(\n 'get_returns for jid %s sent to %s will timeout at %s',\n jid, minions, datetime.fromtimestamp(timeout_at).time()\n )\n\n found = set()\n ret = {}\n # Check to see if the jid is real, if not return the empty dict\n try:\n if self.returners['{0}.get_load'.format(self.opts['master_job_cache'])](jid) == {}:\n log.warning('jid does not exist')\n return ret\n except Exception as exc:\n raise SaltClientError('Master job cache returner [{0}] failed to verify jid. '\n 'Exception details: {1}'.format(self.opts['master_job_cache'], exc))\n\n # Wait for the hosts to check in\n while True:\n time_left = timeout_at - int(time.time())\n wait = max(1, time_left)\n raw = self.event.get_event(wait, jid, auto_reconnect=self.auto_reconnect)\n if raw is not None and 'return' in raw:\n found.add(raw['id'])\n ret[raw['id']] = raw['return']\n if len(found.intersection(minions)) >= len(minions):\n # All minions have returned, break out of the loop\n log.debug('jid %s found all minions', jid)\n break\n continue\n # Then event system timeout was reached and nothing was returned\n if len(found.intersection(minions)) >= len(minions):\n # All minions have returned, break out of the loop\n log.debug('jid %s found all minions', jid)\n break\n if int(time.time()) > timeout_at:\n log.info(\n 'jid %s minions %s did not return in time',\n jid, (minions - found)\n )\n break\n time.sleep(0.01)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef get_full_returns(self, jid, minions, timeout=None):\n '''\n This method starts off a watcher looking at the return data for\n a specified jid, it returns all of the information for the jid\n '''\n # TODO: change this from ret to return... or the other way.\n # Its inconsistent, we should pick one\n\n ret = {}\n # create the iterator-- since we want to get anyone in the middle\n event_iter = self.get_event_iter_returns(jid, minions, timeout=timeout)\n\n try:\n data = self.returners['{0}.get_jid'.format(self.opts['master_job_cache'])](jid)\n except Exception as exc:\n raise SaltClientError('Returner {0} could not fetch jid data. '\n 'Exception details: {1}'.format(\n self.opts['master_job_cache'],\n exc))\n for minion in data:\n m_data = {}\n if 'return' in data[minion]:\n m_data['ret'] = data[minion].get('return')\n else:\n m_data['ret'] = data[minion].get('return')\n if 'out' in data[minion]:\n m_data['out'] = data[minion]['out']\n if minion in ret:\n ret[minion].update(m_data)\n else:\n ret[minion] = m_data\n\n # if we have all the minion returns, lets just return\n if len(set(ret).intersection(minions)) >= len(minions):\n return ret\n\n # otherwise lets use the listener we created above to get the rest\n for event_ret in event_iter:\n # if nothing in the event_ret, skip\n if event_ret == {}:\n time.sleep(0.02)\n continue\n for minion, m_data in six.iteritems(event_ret):\n if minion in ret:\n ret[minion].update(m_data)\n else:\n ret[minion] = m_data\n\n # are we done yet?\n if len(set(ret).intersection(minions)) >= len(minions):\n return ret\n\n # otherwise we hit the timeout, return what we have\n return ret", "response": "This method returns all of the information for a specific jid"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef get_cache_returns(self, jid):\n '''\n Execute a single pass to gather the contents of the job cache\n '''\n ret = {}\n\n try:\n data = self.returners['{0}.get_jid'.format(self.opts['master_job_cache'])](jid)\n except Exception as exc:\n raise SaltClientError('Could not examine master job cache. '\n 'Error occurred in {0} returner. '\n 'Exception details: {1}'.format(self.opts['master_job_cache'],\n exc))\n for minion in data:\n m_data = {}\n if 'return' in data[minion]:\n m_data['ret'] = data[minion].get('return')\n else:\n m_data['ret'] = data[minion].get('return')\n if 'out' in data[minion]:\n m_data['out'] = data[minion]['out']\n if minion in ret:\n ret[minion].update(m_data)\n else:\n ret[minion] = m_data\n\n return ret", "response": "Execute a single pass to gather the contents of the job cache"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_cli_static_event_returns(\n self,\n jid,\n minions,\n timeout=None,\n tgt='*',\n tgt_type='glob',\n verbose=False,\n show_timeout=False,\n show_jid=False):\n '''\n Get the returns for the command line interface via the event system\n '''\n log.trace('entered - function get_cli_static_event_returns()')\n minions = set(minions)\n if verbose:\n msg = 'Executing job with jid {0}'.format(jid)\n print(msg)\n print('-' * len(msg) + '\\n')\n elif show_jid:\n print('jid: {0}'.format(jid))\n\n if timeout is None:\n timeout = self.opts['timeout']\n\n start = int(time.time())\n timeout_at = start + timeout\n found = set()\n ret = {}\n # Check to see if the jid is real, if not return the empty dict\n try:\n if self.returners['{0}.get_load'.format(self.opts['master_job_cache'])](jid) == {}:\n log.warning('jid does not exist')\n return ret\n except Exception as exc:\n raise SaltClientError('Load could not be retrieved from '\n 'returner {0}. Exception details: {1}'.format(\n self.opts['master_job_cache'],\n exc))\n # Wait for the hosts to check in\n while True:\n # Process events until timeout is reached or all minions have returned\n time_left = timeout_at - int(time.time())\n # Wait 0 == forever, use a minimum of 1s\n wait = max(1, time_left)\n jid_tag = 'salt/job/{0}'.format(jid)\n raw = self.event.get_event(wait, jid_tag, auto_reconnect=self.auto_reconnect)\n if raw is not None and 'return' in raw:\n if 'minions' in raw.get('data', {}):\n minions.update(raw['data']['minions'])\n continue\n found.add(raw['id'])\n ret[raw['id']] = {'ret': raw['return']}\n ret[raw['id']]['success'] = raw.get('success', False)\n if 'out' in raw:\n ret[raw['id']]['out'] = raw['out']\n if len(found.intersection(minions)) >= len(minions):\n # All minions have returned, break out of the loop\n break\n continue\n # Then event system timeout was reached and nothing was returned\n if len(found.intersection(minions)) >= len(minions):\n # All minions have returned, break out of the loop\n break\n if int(time.time()) > timeout_at:\n if verbose or show_timeout:\n if self.opts.get('minion_data_cache', False) \\\n or tgt_type in ('glob', 'pcre', 'list'):\n if len(found) < len(minions):\n fail = sorted(list(minions.difference(found)))\n for minion in fail:\n ret[minion] = {\n 'out': 'no_return',\n 'ret': 'Minion did not return'\n }\n break\n time.sleep(0.01)\n\n self._clean_up_subscriptions(jid)\n return ret", "response": "Get the returns for the command line interface via the event system"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the returns for the command line interface via the event system", "response": "def get_cli_event_returns(\n self,\n jid,\n minions,\n timeout=None,\n tgt='*',\n tgt_type='glob',\n verbose=False,\n progress=False,\n show_timeout=False,\n show_jid=False,\n **kwargs):\n '''\n Get the returns for the command line interface via the event system\n '''\n log.trace('func get_cli_event_returns()')\n\n if verbose:\n msg = 'Executing job with jid {0}'.format(jid)\n print(msg)\n print('-' * len(msg) + '\\n')\n elif show_jid:\n print('jid: {0}'.format(jid))\n\n # lazy load the connected minions\n connected_minions = None\n return_count = 0\n\n for ret in self.get_iter_returns(jid,\n minions,\n timeout=timeout,\n tgt=tgt,\n tgt_type=tgt_type,\n # (gtmanfred) expect_minions is popped here incase it is passed from a client\n # call. If this is not popped, then it would be passed twice to\n # get_iter_returns.\n expect_minions=(kwargs.pop('expect_minions', False) or verbose or show_timeout),\n **kwargs\n ):\n log.debug('return event: %s', ret)\n return_count = return_count + 1\n if progress:\n for id_, min_ret in six.iteritems(ret):\n if not min_ret.get('failed') is True:\n yield {'minion_count': len(minions), 'return_count': return_count}\n # replace the return structure for missing minions\n for id_, min_ret in six.iteritems(ret):\n if min_ret.get('failed') is True:\n if connected_minions is None:\n connected_minions = salt.utils.minions.CkMinions(self.opts).connected_ids()\n if self.opts['minion_data_cache'] \\\n and salt.cache.factory(self.opts).contains('minions/{0}'.format(id_), 'data') \\\n and connected_minions \\\n and id_ not in connected_minions:\n\n yield {\n id_: {\n 'out': 'no_return',\n 'ret': 'Minion did not return. [Not connected]',\n 'retcode': salt.defaults.exitcodes.EX_GENERIC\n }\n }\n else:\n # don't report syndics as unresponsive minions\n if not os.path.exists(os.path.join(self.opts['syndic_dir'], id_)):\n yield {\n id_: {\n 'out': 'no_return',\n 'ret': 'Minion did not return. [No response]'\n '\\nThe minions may not have all finished running and any '\n 'remaining minions will return upon completion. To look '\n 'up the return data for this job later, run the following '\n 'command:\\n\\n'\n 'salt-run jobs.lookup_jid {0}'.format(jid),\n 'retcode': salt.defaults.exitcodes.EX_GENERIC\n }\n }\n else:\n yield {id_: min_ret}\n\n self._clean_up_subscriptions(jid)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ngetting the return data from the event system for the specified jid", "response": "def get_event_iter_returns(self, jid, minions, timeout=None):\n '''\n Gather the return data from the event system, break hard when timeout\n is reached.\n '''\n log.trace('entered - function get_event_iter_returns()')\n if timeout is None:\n timeout = self.opts['timeout']\n\n timeout_at = time.time() + timeout\n\n found = set()\n # Check to see if the jid is real, if not return the empty dict\n if self.returners['{0}.get_load'.format(self.opts['master_job_cache'])](jid) == {}:\n log.warning('jid does not exist')\n yield {}\n # stop the iteration, since the jid is invalid\n raise StopIteration()\n # Wait for the hosts to check in\n while True:\n raw = self.event.get_event(timeout, auto_reconnect=self.auto_reconnect)\n if raw is None or time.time() > timeout_at:\n # Timeout reached\n break\n if 'minions' in raw.get('data', {}):\n continue\n try:\n found.add(raw['id'])\n ret = {raw['id']: {'ret': raw['return']}}\n except KeyError:\n # Ignore other erroneous messages\n continue\n if 'out' in raw:\n ret[raw['id']]['out'] = raw['out']\n yield ret\n time.sleep(0.02)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nprepare the payload for the pub command", "response": "def _prep_pub(self,\n tgt,\n fun,\n arg,\n tgt_type,\n ret,\n jid,\n timeout,\n **kwargs):\n '''\n Set up the payload_kwargs to be sent down to the master\n '''\n if tgt_type == 'nodegroup':\n if tgt not in self.opts['nodegroups']:\n conf_file = self.opts.get(\n 'conf_file', 'the master config file'\n )\n raise SaltInvocationError(\n 'Node group {0} unavailable in {1}'.format(\n tgt, conf_file\n )\n )\n tgt = salt.utils.minions.nodegroup_comp(tgt,\n self.opts['nodegroups'])\n tgt_type = 'compound'\n\n # Convert a range expression to a list of nodes and change expression\n # form to list\n if tgt_type == 'range' and HAS_RANGE:\n tgt = self._convert_range_to_list(tgt)\n tgt_type = 'list'\n\n # If an external job cache is specified add it to the ret list\n if self.opts.get('ext_job_cache'):\n if ret:\n ret += ',{0}'.format(self.opts['ext_job_cache'])\n else:\n ret = self.opts['ext_job_cache']\n\n # format the payload - make a function that does this in the payload\n # module\n\n # Generate the standard keyword args to feed to format_payload\n payload_kwargs = {'cmd': 'publish',\n 'tgt': tgt,\n 'fun': fun,\n 'arg': arg,\n 'key': self.key,\n 'tgt_type': tgt_type,\n 'ret': ret,\n 'jid': jid}\n\n # if kwargs are passed, pack them.\n if kwargs:\n payload_kwargs['kwargs'] = kwargs\n\n # If we have a salt user, add it to the payload\n if self.opts['syndic_master'] and 'user' in kwargs:\n payload_kwargs['user'] = kwargs['user']\n elif self.salt_user:\n payload_kwargs['user'] = self.salt_user\n\n # If we're a syndication master, pass the timeout\n if self.opts['order_masters']:\n payload_kwargs['to'] = timeout\n\n return payload_kwargs"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef pub_async(self,\n tgt,\n fun,\n arg=(),\n tgt_type='glob',\n ret='',\n jid='',\n timeout=5,\n io_loop=None,\n listen=True,\n **kwargs):\n '''\n Take the required arguments and publish the given command.\n Arguments:\n tgt:\n The tgt is a regex or a glob used to match up the ids on\n the minions. Salt works by always publishing every command\n to all of the minions and then the minions determine if\n the command is for them based on the tgt value.\n fun:\n The function name to be called on the remote host(s), this\n must be a string in the format \"<modulename>.<function name>\"\n arg:\n The arg option needs to be a tuple of arguments to pass\n to the calling function, if left blank\n Returns:\n jid:\n A string, as returned by the publisher, which is the job\n id, this will inform the client where to get the job results\n minions:\n A set, the targets that the tgt passed should match.\n '''\n # Make sure the publisher is running by checking the unix socket\n if (self.opts.get('ipc_mode', '') != 'tcp' and\n not os.path.exists(os.path.join(self.opts['sock_dir'],\n 'publish_pull.ipc'))):\n log.error(\n 'Unable to connect to the salt master publisher at %s',\n self.opts['sock_dir']\n )\n raise SaltClientError\n\n payload_kwargs = self._prep_pub(\n tgt,\n fun,\n arg,\n tgt_type,\n ret,\n jid,\n timeout,\n **kwargs)\n\n master_uri = 'tcp://' + salt.utils.zeromq.ip_bracket(self.opts['interface']) + \\\n ':' + six.text_type(self.opts['ret_port'])\n channel = salt.transport.client.AsyncReqChannel.factory(self.opts,\n io_loop=io_loop,\n crypt='clear',\n master_uri=master_uri)\n\n try:\n # Ensure that the event subscriber is connected.\n # If not, we won't get a response, so error out\n if listen and not self.event.connect_pub(timeout=timeout):\n raise SaltReqTimeoutError()\n payload = yield channel.send(payload_kwargs, timeout=timeout)\n except SaltReqTimeoutError:\n raise SaltReqTimeoutError(\n 'Salt request timed out. The master is not responding. You '\n 'may need to run your command with `--async` in order to '\n 'bypass the congested event bus. With `--async`, the CLI tool '\n 'will print the job id (jid) and exit immediately without '\n 'listening for responses. You can then use '\n '`salt-run jobs.lookup_jid` to look up the results of the job '\n 'in the job cache later.'\n )\n\n if not payload:\n # The master key could have changed out from under us! Regen\n # and try again if the key has changed\n key = self.__read_master_key()\n if key == self.key:\n raise tornado.gen.Return(payload)\n self.key = key\n payload_kwargs['key'] = self.key\n payload = yield channel.send(payload_kwargs)\n\n error = payload.pop('error', None)\n if error is not None:\n if isinstance(error, dict):\n err_name = error.get('name', '')\n err_msg = error.get('message', '')\n if err_name == 'AuthenticationError':\n raise AuthenticationError(err_msg)\n elif err_name == 'AuthorizationError':\n raise AuthorizationError(err_msg)\n\n raise PublishError(error)\n\n if not payload:\n raise tornado.gen.Return(payload)\n\n # We have the payload, let's get rid of the channel fast(GC'ed faster)\n channel.close()\n\n raise tornado.gen.Return({'jid': payload['load']['jid'],\n 'minions': payload['load']['minions']})", "response": "Publish a command to all of the minions on the master."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nloading the functions available on the minion", "response": "def __load_functions(self):\n '''\n Find out what functions are available on the minion\n '''\n return set(self.local.cmd(self.minion,\n 'sys.list_functions').get(self.minion, []))"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a function that executes the arguments passed via the local client and returns the result of the remote call.", "response": "def run_key(self, key):\n '''\n Return a function that executes the arguments passed via the local\n client\n '''\n def func(*args, **kwargs):\n '''\n Run a remote call\n '''\n args = list(args)\n for _key, _val in kwargs:\n args.append('{0}={1}'.format(_key, _val))\n return self.local.cmd(self.minion, key, args)\n return func"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ncall an execution module with the given arguments and keyword arguments and return the result.", "response": "def cmd(self, fun, *args, **kwargs):\n '''\n Call an execution module with the given arguments and keyword arguments\n\n .. versionchanged:: 2015.8.0\n Added the ``cmd`` method for consistency with the other Salt clients.\n The existing ``function`` and ``sminion.functions`` interfaces still\n exist but have been removed from the docs.\n\n .. code-block:: python\n\n caller.cmd('test.arg', 'Foo', 'Bar', baz='Baz')\n\n caller.cmd('event.send', 'myco/myevent/something',\n data={'foo': 'Foo'}, with_env=['GIT_COMMIT'], with_grains=True)\n '''\n return self.sminion.functions[fun](*args, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef function(self, fun, *args, **kwargs):\n '''\n Call a single salt function\n '''\n func = self.sminion.functions[fun]\n args, kwargs = salt.minion.load_args_and_kwargs(\n func,\n salt.utils.args.parse_input(args, kwargs=kwargs),)\n return func(*args, **kwargs)", "response": "Call a single salt function"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef cmd(self, fun, *args, **kwargs):\n '''\n Call an execution module with the given arguments and keyword arguments\n\n .. code-block:: python\n\n caller.cmd('test.arg', 'Foo', 'Bar', baz='Baz')\n\n caller.cmd('event.send', 'myco/myevent/something',\n data={'foo': 'Foo'}, with_env=['GIT_COMMIT'], with_grains=True)\n '''\n func = self.sminion.functions[fun]\n data = {\n 'arg': args,\n 'fun': fun\n }\n data.update(kwargs)\n executors = getattr(self.sminion, 'module_executors', []) or \\\n self.opts.get('module_executors', ['direct_call'])\n if isinstance(executors, six.string_types):\n executors = [executors]\n for name in executors:\n fname = '{0}.execute'.format(name)\n if fname not in self.sminion.executors:\n raise SaltInvocationError(\"Executor '{0}' is not available\".format(name))\n return_data = self.sminion.executors[fname](self.opts, data, func, args, kwargs)\n if return_data is not None:\n break\n return return_data", "response": "Execute a function on the specified module"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the value of the setting for the provided class.", "response": "def _get_wmi_setting(wmi_class_name, setting, server):\n '''\n Get the value of the setting for the provided class.\n '''\n with salt.utils.winapi.Com():\n try:\n connection = wmi.WMI(namespace=_WMI_NAMESPACE)\n wmi_class = getattr(connection, wmi_class_name)\n\n objs = wmi_class([setting], Name=server)[0]\n ret = getattr(objs, setting)\n except wmi.x_wmi as error:\n _LOG.error('Encountered WMI error: %s', error.com_error)\n except (AttributeError, IndexError) as error:\n _LOG.error('Error getting %s: %s', wmi_class_name, error)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _set_wmi_setting(wmi_class_name, setting, value, server):\n '''\n Set the value of the setting for the provided class.\n '''\n with salt.utils.winapi.Com():\n try:\n connection = wmi.WMI(namespace=_WMI_NAMESPACE)\n wmi_class = getattr(connection, wmi_class_name)\n\n objs = wmi_class(Name=server)[0]\n except wmi.x_wmi as error:\n _LOG.error('Encountered WMI error: %s', error.com_error)\n except (AttributeError, IndexError) as error:\n _LOG.error('Error getting %s: %s', wmi_class_name, error)\n\n try:\n setattr(objs, setting, value)\n return True\n except wmi.x_wmi as error:\n _LOG.error('Encountered WMI error: %s', error.com_error)\n except AttributeError as error:\n _LOG.error('Error setting %s: %s', setting, error)\n return False", "response": "Set the value of the setting for the provided class."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting all available log format names and ids.", "response": "def get_log_format_types():\n '''\n Get all available log format names and ids.\n\n :return: A dictionary of the log format names and ids.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' win_smtp_server.get_log_format_types\n '''\n ret = dict()\n prefix = 'logging/'\n\n with salt.utils.winapi.Com():\n try:\n connection = wmi.WMI(namespace=_WMI_NAMESPACE)\n objs = connection.IISLogModuleSetting()\n\n # Remove the prefix from the name.\n for obj in objs:\n name = six.text_type(obj.Name).replace(prefix, '', 1)\n ret[name] = six.text_type(obj.LogModuleId)\n except wmi.x_wmi as error:\n _LOG.error('Encountered WMI error: %s', error.com_error)\n except (AttributeError, IndexError) as error:\n _LOG.error('Error getting IISLogModuleSetting: %s', error)\n\n if not ret:\n _LOG.error('Unable to get log format types.')\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_servers():\n '''\n Get the SMTP virtual server names.\n\n :return: A list of the SMTP virtual servers.\n :rtype: list\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' win_smtp_server.get_servers\n '''\n ret = list()\n\n with salt.utils.winapi.Com():\n try:\n connection = wmi.WMI(namespace=_WMI_NAMESPACE)\n objs = connection.IIsSmtpServerSetting()\n\n for obj in objs:\n ret.append(six.text_type(obj.Name))\n except wmi.x_wmi as error:\n _LOG.error('Encountered WMI error: %s', error.com_error)\n except (AttributeError, IndexError) as error:\n _LOG.error('Error getting IIsSmtpServerSetting: %s', error)\n\n _LOG.debug('Found SMTP servers: %s', ret)\n return ret", "response": "Get the SMTP virtual server names."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_server_setting(settings, server=_DEFAULT_SERVER):\n '''\n Get the value of the setting for the SMTP virtual server.\n\n :param str settings: A list of the setting names.\n :param str server: The SMTP server name.\n\n :return: A dictionary of the provided settings and their values.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' win_smtp_server.get_server_setting settings=\"['MaxRecipients']\"\n '''\n ret = dict()\n\n if not settings:\n _LOG.warning('No settings provided.')\n return ret\n\n with salt.utils.winapi.Com():\n try:\n connection = wmi.WMI(namespace=_WMI_NAMESPACE)\n objs = connection.IIsSmtpServerSetting(settings, Name=server)[0]\n\n for setting in settings:\n ret[setting] = six.text_type(getattr(objs, setting))\n except wmi.x_wmi as error:\n _LOG.error('Encountered WMI error: %s', error.com_error)\n except (AttributeError, IndexError) as error:\n _LOG.error('Error getting IIsSmtpServerSetting: %s', error)\n return ret", "response": "Get the value of the setting for the SMTP virtual server."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nset the value of the SMTP virtual server setting for the SMTP virtual server.", "response": "def set_server_setting(settings, server=_DEFAULT_SERVER):\n '''\n Set the value of the setting for the SMTP virtual server.\n\n .. note::\n\n The setting names are case-sensitive.\n\n :param str settings: A dictionary of the setting names and their values.\n :param str server: The SMTP server name.\n\n :return: A boolean representing whether all changes succeeded.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' win_smtp_server.set_server_setting settings=\"{'MaxRecipients': '500'}\"\n '''\n if not settings:\n _LOG.warning('No settings provided')\n return False\n\n # Some fields are formatted like '{data}'. Salt tries to convert these to dicts\n # automatically on input, so convert them back to the proper format.\n settings = _normalize_server_settings(**settings)\n\n current_settings = get_server_setting(settings=settings.keys(), server=server)\n\n if settings == current_settings:\n _LOG.debug('Settings already contain the provided values.')\n return True\n\n # Note that we must fetch all properties of IIsSmtpServerSetting below, since\n # filtering for specific properties and then attempting to set them will cause\n # an error like: wmi.x_wmi Unexpected COM Error -2147352567\n with salt.utils.winapi.Com():\n try:\n connection = wmi.WMI(namespace=_WMI_NAMESPACE)\n objs = connection.IIsSmtpServerSetting(Name=server)[0]\n except wmi.x_wmi as error:\n _LOG.error('Encountered WMI error: %s', error.com_error)\n except (AttributeError, IndexError) as error:\n _LOG.error('Error getting IIsSmtpServerSetting: %s', error)\n\n for setting in settings:\n if six.text_type(settings[setting]) != six.text_type(current_settings[setting]):\n try:\n setattr(objs, setting, settings[setting])\n except wmi.x_wmi as error:\n _LOG.error('Encountered WMI error: %s', error.com_error)\n except AttributeError as error:\n _LOG.error('Error setting %s: %s', setting, error)\n\n # Get the settings post-change so that we can verify tht all properties\n # were modified successfully. Track the ones that weren't.\n new_settings = get_server_setting(settings=settings.keys(), server=server)\n failed_settings = dict()\n\n for setting in settings:\n if six.text_type(settings[setting]) != six.text_type(new_settings[setting]):\n failed_settings[setting] = settings[setting]\n if failed_settings:\n _LOG.error('Failed to change settings: %s', failed_settings)\n return False\n\n _LOG.debug('Settings configured successfully: %s', settings.keys())\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the active log format for the SMTP virtual server.", "response": "def get_log_format(server=_DEFAULT_SERVER):\n '''\n Get the active log format for the SMTP virtual server.\n\n :param str server: The SMTP server name.\n\n :return: A string of the log format name.\n :rtype: str\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' win_smtp_server.get_log_format\n '''\n log_format_types = get_log_format_types()\n format_id = _get_wmi_setting('IIsSmtpServerSetting', 'LogPluginClsid', server)\n\n # Since IIsSmtpServerSetting stores the log type as an id, we need\n # to get the mapping from IISLogModuleSetting and extract the name.\n for key in log_format_types:\n if six.text_type(format_id) == log_format_types[key]:\n return key\n _LOG.warning('Unable to determine log format.')\n return None"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsets the active log format for the SMTP virtual server.", "response": "def set_log_format(log_format, server=_DEFAULT_SERVER):\n '''\n Set the active log format for the SMTP virtual server.\n\n :param str log_format: The log format name.\n :param str server: The SMTP server name.\n\n :return: A boolean representing whether the change succeeded.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' win_smtp_server.set_log_format 'Microsoft IIS Log File Format'\n '''\n setting = 'LogPluginClsid'\n log_format_types = get_log_format_types()\n format_id = log_format_types.get(log_format, None)\n\n if not format_id:\n message = (\"Invalid log format '{0}' specified. Valid formats:\"\n ' {1}').format(log_format, log_format_types.keys())\n raise SaltInvocationError(message)\n\n _LOG.debug(\"Id for '%s' found: %s\", log_format, format_id)\n\n current_log_format = get_log_format(server)\n\n if log_format == current_log_format:\n _LOG.debug('%s already contains the provided format.', setting)\n return True\n\n _set_wmi_setting('IIsSmtpServerSetting', setting, format_id, server)\n\n new_log_format = get_log_format(server)\n ret = log_format == new_log_format\n\n if ret:\n _LOG.debug(\"Setting %s configured successfully: %s\", setting, log_format)\n else:\n _LOG.error(\"Unable to configure %s with value: %s\", setting, log_format)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_connection_ip_list(as_wmi_format=False, server=_DEFAULT_SERVER):\n '''\n Get the IPGrant list for the SMTP virtual server.\n\n :param bool as_wmi_format: Returns the connection IPs as a list in the format WMI expects.\n :param str server: The SMTP server name.\n\n :return: A dictionary of the IP and subnet pairs.\n :rtype: dict\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' win_smtp_server.get_connection_ip_list\n '''\n ret = dict()\n setting = 'IPGrant'\n reg_separator = r',\\s*'\n\n if as_wmi_format:\n ret = list()\n\n addresses = _get_wmi_setting('IIsIPSecuritySetting', setting, server)\n\n # WMI returns the addresses as a tuple of unicode strings, each representing\n # an address/subnet pair. Remove extra spaces that may be present.\n for unnormalized_address in addresses:\n ip_address, subnet = re.split(reg_separator, unnormalized_address)\n if as_wmi_format:\n ret.append('{0}, {1}'.format(ip_address, subnet))\n else:\n ret[ip_address] = subnet\n\n if not ret:\n _LOG.debug('%s is empty.', setting)\n return ret", "response": "Get the IPGrant list for the SMTP virtual server."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef set_connection_ip_list(addresses=None, grant_by_default=False, server=_DEFAULT_SERVER):\n '''\n Set the IPGrant list for the SMTP virtual server.\n\n :param str addresses: A dictionary of IP + subnet pairs.\n :param bool grant_by_default: Whether the addresses should be a blacklist or whitelist.\n :param str server: The SMTP server name.\n\n :return: A boolean representing whether the change succeeded.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' win_smtp_server.set_connection_ip_list addresses=\"{'127.0.0.1': '255.255.255.255'}\"\n '''\n setting = 'IPGrant'\n formatted_addresses = list()\n\n # It's okay to accept an empty list for set_connection_ip_list,\n # since an empty list may be desirable.\n if not addresses:\n addresses = dict()\n _LOG.debug('Empty %s specified.', setting)\n\n # Convert addresses to the 'ip_address, subnet' format used by\n # IIsIPSecuritySetting.\n for address in addresses:\n formatted_addresses.append('{0}, {1}'.format(address.strip(),\n addresses[address].strip()))\n\n current_addresses = get_connection_ip_list(as_wmi_format=True, server=server)\n\n # Order is not important, so compare to the current addresses as unordered sets.\n if set(formatted_addresses) == set(current_addresses):\n _LOG.debug('%s already contains the provided addresses.', setting)\n return True\n\n # First we should check GrantByDefault, and change it if necessary.\n current_grant_by_default = _get_wmi_setting('IIsIPSecuritySetting', 'GrantByDefault', server)\n\n if grant_by_default != current_grant_by_default:\n _LOG.debug('Setting GrantByDefault to: %s', grant_by_default)\n _set_wmi_setting('IIsIPSecuritySetting', 'GrantByDefault', grant_by_default, server)\n\n _set_wmi_setting('IIsIPSecuritySetting', setting, formatted_addresses, server)\n\n new_addresses = get_connection_ip_list(as_wmi_format=True, server=server)\n ret = set(formatted_addresses) == set(new_addresses)\n\n if ret:\n _LOG.debug('%s configured successfully: %s', setting, formatted_addresses)\n return ret\n _LOG.error('Unable to configure %s with value: %s', setting, formatted_addresses)\n return ret", "response": "Set the IPGrant list for the SMTP virtual server."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the RelayIpList list for the SMTP virtual server.", "response": "def get_relay_ip_list(server=_DEFAULT_SERVER):\n '''\n Get the RelayIpList list for the SMTP virtual server.\n\n :param str server: The SMTP server name.\n\n :return: A list of the relay IPs.\n :rtype: list\n\n .. note::\n\n A return value of None corresponds to the restrictive 'Only the list below' GUI parameter\n with an empty access list, and setting an empty list/tuple corresponds to the more\n permissive 'All except the list below' GUI parameter.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' win_smtp_server.get_relay_ip_list\n '''\n ret = list()\n setting = 'RelayIpList'\n\n lines = _get_wmi_setting('IIsSmtpServerSetting', setting, server)\n\n if not lines:\n _LOG.debug('%s is empty: %s', setting, lines)\n if lines is None:\n lines = [None]\n return list(lines)\n\n # WMI returns the addresses as a tuple of individual octets, so we\n # need to group them and reassemble them into IP addresses.\n i = 0\n while i < len(lines):\n octets = [six.text_type(x) for x in lines[i: i + 4]]\n address = '.'.join(octets)\n ret.append(address)\n i += 4\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef set_relay_ip_list(addresses=None, server=_DEFAULT_SERVER):\n '''\n Set the RelayIpList list for the SMTP virtual server.\n\n Due to the unusual way that Windows stores the relay IPs, it is advisable to retrieve\n the existing list you wish to set from a pre-configured server.\n\n For example, setting '127.0.0.1' as an allowed relay IP through the GUI would generate\n an actual relay IP list similar to the following:\n\n .. code-block:: cfg\n\n ['24.0.0.128', '32.0.0.128', '60.0.0.128', '68.0.0.128', '1.0.0.0', '76.0.0.0',\n '0.0.0.0', '0.0.0.0', '1.0.0.0', '1.0.0.0', '2.0.0.0', '2.0.0.0', '4.0.0.0',\n '0.0.0.0', '76.0.0.128', '0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0',\n '255.255.255.255', '127.0.0.1']\n\n .. note::\n\n Setting the list to None corresponds to the restrictive 'Only the list below' GUI parameter\n with an empty access list configured, and setting an empty list/tuple corresponds to the\n more permissive 'All except the list below' GUI parameter.\n\n :param str addresses: A list of the relay IPs. The order of the list is important.\n :param str server: The SMTP server name.\n\n :return: A boolean representing whether the change succeeded.\n :rtype: bool\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' win_smtp_server.set_relay_ip_list addresses=\"['192.168.1.1', '172.16.1.1']\"\n '''\n setting = 'RelayIpList'\n formatted_addresses = list()\n\n current_addresses = get_relay_ip_list(server)\n\n if list(addresses) == current_addresses:\n _LOG.debug('%s already contains the provided addresses.', setting)\n return True\n\n if addresses:\n # The WMI input data needs to be in the format used by RelayIpList. Order\n # is also important due to the way RelayIpList orders the address list.\n\n if addresses[0] is None:\n formatted_addresses = None\n else:\n for address in addresses:\n for octet in address.split('.'):\n formatted_addresses.append(octet)\n\n _LOG.debug('Formatted %s addresses: %s', setting, formatted_addresses)\n\n _set_wmi_setting('IIsSmtpServerSetting', setting, formatted_addresses, server)\n\n new_addresses = get_relay_ip_list(server)\n\n ret = list(addresses) == new_addresses\n\n if ret:\n _LOG.debug('%s configured successfully: %s', setting, addresses)\n return ret\n _LOG.error('Unable to configure %s with value: %s', setting, addresses)\n return ret", "response": "Sets the RelayIpList list for the SMTP virtual server."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nprepares the connection with the remote network device and clean up the key value pairs removing the args used for the connection cleanup.", "response": "def _prepare_connection(**kwargs):\n '''\n Prepare the connection with the remote network device, and clean up the key\n value pairs, removing the args used for the connection init.\n '''\n init_args = {}\n fun_kwargs = {}\n netmiko_kwargs = __salt__['config.get']('netmiko', {})\n netmiko_kwargs.update(kwargs) # merge the CLI args with the opts/pillar\n netmiko_init_args, _, _, netmiko_defaults = inspect.getargspec(BaseConnection.__init__)\n check_self = netmiko_init_args.pop(0)\n for karg, warg in six.iteritems(netmiko_kwargs):\n if karg not in netmiko_init_args:\n if warg is not None:\n fun_kwargs[karg] = warg\n continue\n if warg is not None:\n init_args[karg] = warg\n conn = ConnectHandler(**init_args)\n return conn, fun_kwargs"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef multi_call(*methods, **kwargs):\n '''\n Invoke multiple Netmiko methods at once, and return their output, as list.\n\n methods\n A list of dictionaries with the following keys:\n\n - ``name``: the name of the Netmiko method to be executed.\n - ``args``: list of arguments to be sent to the Netmiko method.\n - ``kwargs``: dictionary of arguments to be sent to the Netmiko method.\n\n kwargs\n Key-value dictionary with the connection details (when not running\n under a Proxy Minion).\n '''\n kwargs = clean_kwargs(**kwargs)\n if 'netmiko.conn' in __proxy__:\n conn = __proxy__['netmiko.conn']()\n else:\n conn, kwargs = _prepare_connection(**kwargs)\n ret = []\n for method in methods:\n # Explicit unpacking\n method_name = method['name']\n method_args = method.get('args', [])\n method_kwargs = method.get('kwargs', [])\n ret.append(getattr(conn, method_name)(*method_args, **method_kwargs))\n if 'netmiko.conn' not in __proxy__:\n conn.disconnect()\n return ret", "response": "Invoke multiple Netmiko methods at once and return their output as list."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nsends a configuration command to the SSH channel.", "response": "def send_config(config_file=None,\n config_commands=None,\n template_engine='jinja',\n commit=False,\n context=None,\n defaults=None,\n saltenv='base',\n **kwargs):\n '''\n Send configuration commands down the SSH channel.\n Return the configuration lines sent to the device.\n\n The function is flexible to send the configuration from a local or remote\n file, or simply the commands as list.\n\n config_file\n The source file with the configuration commands to be sent to the\n device.\n\n The file can also be a template that can be rendered using the template\n engine of choice.\n\n This can be specified using the absolute path to the file, or using one\n of the following URL schemes:\n\n - ``salt://``, to fetch the file from the Salt fileserver.\n - ``http://`` or ``https://``\n - ``ftp://``\n - ``s3://``\n - ``swift://``\n\n config_commands\n Multiple configuration commands to be sent to the device.\n\n .. note::\n\n This argument is ignored when ``config_file`` is specified.\n\n template_engine: ``jinja``\n The template engine to use when rendering the source file. Default:\n ``jinja``. To simply fetch the file without attempting to render, set\n this argument to ``None``.\n\n commit: ``False``\n Commit the configuration changes before exiting the config mode. This\n option is by default disabled, as many platforms don't have this\n capability natively.\n\n context\n Variables to add to the template context.\n\n defaults\n Default values of the context_dict.\n\n exit_config_mode: ``True``\n Determines whether or not to exit config mode after complete.\n\n delay_factor: ``1``\n Factor to adjust delays.\n\n max_loops: ``150``\n Controls wait time in conjunction with delay_factor (default: ``150``).\n\n strip_prompt: ``False``\n Determines whether or not to strip the prompt (default: ``False``).\n\n strip_command: ``False``\n Determines whether or not to strip the command (default: ``False``).\n\n config_mode_command\n The command to enter into config mode.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' netmiko.send_config config_commands=\"['interface GigabitEthernet3', 'no ip address']\"\n salt '*' netmiko.send_config config_commands=\"['snmp-server location {{ grains.location }}']\"\n salt '*' netmiko.send_config config_file=salt://config.txt\n salt '*' netmiko.send_config config_file=https://bit.ly/2sgljCB device_type='cisco_ios' ip='1.2.3.4' username='example'\n '''\n if config_file:\n file_str = __salt__['cp.get_file_str'](config_file, saltenv=saltenv)\n if file_str is False:\n raise CommandExecutionError('Source file {} not found'.format(config_file))\n elif config_commands:\n if isinstance(config_commands, (six.string_types, six.text_type)):\n config_commands = [config_commands]\n file_str = '\\n'.join(config_commands)\n # unify all the commands in a single file, to render them in a go\n if template_engine:\n file_str = __salt__['file.apply_template_on_contents'](file_str,\n template_engine,\n context,\n defaults,\n saltenv)\n # whatever the source of the commands would be, split them line by line\n config_commands = [line for line in file_str.splitlines() if line.strip()]\n kwargs = clean_kwargs(**kwargs)\n if 'netmiko.conn' in __proxy__:\n conn = __proxy__['netmiko.conn']()\n else:\n conn, kwargs = _prepare_connection(**kwargs)\n if commit:\n kwargs['exit_config_mode'] = False # don't exit config mode after\n # loading the commands, wait for explicit commit\n ret = conn.send_config_set(config_commands=config_commands, **kwargs)\n if commit:\n ret += conn.commit()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef connect(image):\n '''\n Activate nbd for an image file.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' qemu_nbd.connect /tmp/image.raw\n '''\n if not os.path.isfile(image):\n log.warning('Could not connect image: %s does not exist', image)\n return ''\n\n if salt.utils.path.which('sfdisk'):\n fdisk = 'sfdisk -d'\n else:\n fdisk = 'fdisk -l'\n __salt__['cmd.run']('modprobe nbd max_part=63')\n for nbd in glob.glob('/dev/nbd?'):\n if __salt__['cmd.retcode']('{0} {1}'.format(fdisk, nbd)):\n while True:\n # Sometimes nbd does not \"take hold\", loop until we can verify\n __salt__['cmd.run'](\n 'qemu-nbd -c {0} {1}'.format(nbd, image),\n python_shell=False,\n )\n if not __salt__['cmd.retcode']('{0} {1}'.format(fdisk, nbd)):\n break\n return nbd\n log.warning('Could not connect image: %s', image)\n return ''", "response": "Activate nbd for an image file."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef mount(nbd, root=None):\n '''\n Pass in the nbd connection device location, mount all partitions and return\n a dict of mount points\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' qemu_nbd.mount /dev/nbd0\n '''\n __salt__['cmd.run'](\n 'partprobe {0}'.format(nbd),\n python_shell=False,\n )\n ret = {}\n if root is None:\n root = os.path.join(\n tempfile.gettempdir(),\n 'nbd',\n os.path.basename(nbd)\n )\n for part in glob.glob('{0}p*'.format(nbd)):\n m_pt = os.path.join(root, os.path.basename(part))\n time.sleep(1)\n mnt = __salt__['mount.mount'](m_pt, part, True)\n if mnt is not True:\n continue\n ret[m_pt] = part\n return ret", "response": "Mount all partitions of the nbd device and return a dict of mount points"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nmounting the named image via qemu - nbd and return the mounted roots", "response": "def init(image, root=None):\n '''\n Mount the named image via qemu-nbd and return the mounted roots\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' qemu_nbd.init /srv/image.qcow2\n '''\n nbd = connect(image)\n if not nbd:\n return ''\n return mount(nbd, root)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nclear the image from nbd", "response": "def clear(mnt):\n '''\n Pass in the mnt dict returned from nbd_mount to unmount and disconnect\n the image from nbd. If all of the partitions are unmounted return an\n empty dict, otherwise return a dict containing the still mounted\n partitions\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' qemu_nbd.clear '{\"/mnt/foo\": \"/dev/nbd0p1\"}'\n '''\n ret = {}\n nbds = set()\n for m_pt, dev in six.iteritems(mnt):\n mnt_ret = __salt__['mount.umount'](m_pt)\n if mnt_ret is not True:\n ret[m_pt] = dev\n nbds.add(dev[:dev.rindex('p')])\n if ret:\n return ret\n for nbd in nbds:\n __salt__['cmd.run']('qemu-nbd -d {0}'.format(nbd), python_shell=False)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nhandling an interrupt from the salt master.", "response": "def _handle_interrupt(exc, original_exc, hardfail=False, trace=''):\n '''\n if hardfailing:\n If we got the original stacktrace, log it\n If all cases, raise the original exception\n but this is logically part the initial\n stack.\n else just let salt exit gracefully\n\n '''\n if hardfail:\n if trace:\n log.error(trace)\n raise original_exc\n else:\n raise exc"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nstarts the salt master.", "response": "def salt_master():\n '''\n Start the salt master.\n '''\n import salt.cli.daemons\n# REMOVEME after Python 2.7 support is dropped (also the six import)\n if six.PY2:\n from salt.utils.versions import warn_until\n # Message borrowed from pip's deprecation warning\n warn_until('Sodium',\n 'Python 2.7 will reach the end of its life on January 1st,'\n ' 2020. Please upgrade your Python as Python 2.7 won\\'t be'\n ' maintained after that date. Salt will drop support for'\n ' Python 2.7 in the Sodium release or later.')\n# END REMOVEME\n master = salt.cli.daemons.Master()\n master.start()"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nstarts a minion process", "response": "def minion_process():\n '''\n Start a minion process\n '''\n import salt.utils.platform\n import salt.utils.process\n import salt.cli.daemons\n\n # salt_minion spawns this function in a new process\n\n salt.utils.process.appendproctitle('KeepAlive')\n\n def handle_hup(manager, sig, frame):\n manager.minion.reload()\n\n lock = threading.RLock()\n\n def suicide_when_without_parent(parent_pid):\n '''\n Have the minion suicide if the parent process is gone\n\n NOTE: small race issue where the parent PID could be replace\n with another process with same PID!\n '''\n while lock.acquire(blocking=False):\n lock.release()\n time.sleep(5)\n try:\n # check pid alive (Unix only trick!)\n if os.getuid() == 0 and not salt.utils.platform.is_windows():\n os.kill(parent_pid, 0)\n except OSError as exc:\n # forcibly exit, regular sys.exit raises an exception-- which\n # isn't sufficient in a thread\n log.error('Minion process encountered exception: %s', exc)\n os._exit(salt.defaults.exitcodes.EX_GENERIC)\n\n try:\n if not salt.utils.platform.is_windows():\n thread = threading.Thread(target=suicide_when_without_parent, args=(os.getppid(),))\n thread.start()\n\n minion = salt.cli.daemons.Minion()\n signal.signal(signal.SIGHUP,\n functools.partial(handle_hup,\n minion))\n minion.start()\n except (SaltClientError, SaltReqTimeoutError, SaltSystemExit) as exc:\n lock.acquire(blocking=True)\n log.warning('Fatal functionality error caught by minion handler:\\n', exc_info=True)\n log.warning('** Restarting minion **')\n delay = 60\n if minion is not None and hasattr(minion, 'config'):\n delay = minion.config.get('random_reauth_delay', 60)\n delay = randint(1, delay)\n log.info('waiting random_reauth_delay %ss', delay)\n time.sleep(delay)\n sys.exit(salt.defaults.exitcodes.SALT_KEEPALIVE)\n finally:\n lock.acquire(blocking=True)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef salt_minion():\n '''\n Start the salt minion in a subprocess.\n Auto restart minion on error.\n '''\n import signal\n\n import salt.utils.platform\n import salt.utils.process\n salt.utils.process.notify_systemd()\n\n import salt.cli.daemons\n import multiprocessing\n if '' in sys.path:\n sys.path.remove('')\n\n if salt.utils.platform.is_windows():\n minion = salt.cli.daemons.Minion()\n minion.start()\n return\n# REMOVEME after Python 2.7 support is dropped (also the six import)\n elif six.PY2:\n from salt.utils.versions import warn_until\n # Message borrowed from pip's deprecation warning\n warn_until('Sodium',\n 'Python 2.7 will reach the end of its life on January 1st,'\n ' 2020. Please upgrade your Python as Python 2.7 won\\'t be'\n ' maintained after that date. Salt will drop support for'\n ' Python 2.7 in the Sodium release or later.')\n# END REMOVEME\n\n if '--disable-keepalive' in sys.argv:\n sys.argv.remove('--disable-keepalive')\n minion = salt.cli.daemons.Minion()\n minion.start()\n return\n\n def escalate_signal_to_process(pid, signum, sigframe): # pylint: disable=unused-argument\n '''\n Escalate the signal received to the multiprocessing process that\n is actually running the minion\n '''\n # escalate signal\n os.kill(pid, signum)\n\n # keep one minion subprocess running\n prev_sigint_handler = signal.getsignal(signal.SIGINT)\n prev_sigterm_handler = signal.getsignal(signal.SIGTERM)\n while True:\n try:\n process = multiprocessing.Process(target=minion_process)\n process.start()\n signal.signal(signal.SIGTERM,\n functools.partial(escalate_signal_to_process,\n process.pid))\n signal.signal(signal.SIGINT,\n functools.partial(escalate_signal_to_process,\n process.pid))\n signal.signal(signal.SIGHUP,\n functools.partial(escalate_signal_to_process,\n process.pid))\n except Exception: # pylint: disable=broad-except\n # if multiprocessing does not work\n minion = salt.cli.daemons.Minion()\n minion.start()\n break\n\n process.join()\n\n # Process exited or was terminated. Since we're going to try to restart\n # it, we MUST, reset signal handling to the previous handlers\n signal.signal(signal.SIGINT, prev_sigint_handler)\n signal.signal(signal.SIGTERM, prev_sigterm_handler)\n\n if not process.exitcode == salt.defaults.exitcodes.SALT_KEEPALIVE:\n sys.exit(process.exitcode)\n # ontop of the random_reauth_delay already preformed\n # delay extra to reduce flooding and free resources\n # NOTE: values are static but should be fine.\n time.sleep(2 + randint(1, 10))\n # need to reset logging because new minion objects\n # cause extra log handlers to accumulate\n rlogger = logging.getLogger()\n for handler in rlogger.handlers:\n rlogger.removeHandler(handler)\n logging.basicConfig()", "response": "Start a salt minion in a subprocess."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef proxy_minion_process(queue):\n '''\n Start a proxy minion process\n '''\n import salt.cli.daemons\n import salt.utils.platform\n # salt_minion spawns this function in a new process\n\n lock = threading.RLock()\n\n def suicide_when_without_parent(parent_pid):\n '''\n Have the minion suicide if the parent process is gone\n\n NOTE: there is a small race issue where the parent PID could be replace\n with another process with the same PID!\n '''\n while lock.acquire(blocking=False):\n lock.release()\n time.sleep(5)\n try:\n # check pid alive (Unix only trick!)\n os.kill(parent_pid, 0)\n except OSError:\n # forcibly exit, regular sys.exit raises an exception-- which\n # isn't sufficient in a thread\n os._exit(999)\n\n try:\n if not salt.utils.platform.is_windows():\n thread = threading.Thread(target=suicide_when_without_parent, args=(os.getppid(),))\n thread.start()\n\n restart = False\n proxyminion = None\n status = salt.defaults.exitcodes.EX_OK\n proxyminion = salt.cli.daemons.ProxyMinion()\n proxyminion.start()\n except (Exception, SaltClientError, SaltReqTimeoutError, SaltSystemExit) as exc:\n log.error('Proxy Minion failed to start: ', exc_info=True)\n restart = True\n # status is superfluous since the process will be restarted\n status = salt.defaults.exitcodes.SALT_KEEPALIVE\n except SystemExit as exc:\n restart = False\n status = exc.code\n finally:\n lock.acquire(blocking=True)\n\n if restart is True:\n log.warning('** Restarting proxy minion **')\n delay = 60\n if proxyminion is not None:\n if hasattr(proxyminion, 'config'):\n delay = proxyminion.config.get('random_reauth_delay', 60)\n random_delay = randint(1, delay)\n log.info('Sleeping random_reauth_delay of %s seconds', random_delay)\n # preform delay after minion resources have been cleaned\n queue.put(random_delay)\n else:\n queue.put(0)\n sys.exit(status)", "response": "Start a proxy minion process"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nstarts a proxy minion.", "response": "def salt_proxy():\n '''\n Start a proxy minion.\n '''\n import salt.cli.daemons\n import salt.utils.platform\n import multiprocessing\n if '' in sys.path:\n sys.path.remove('')\n\n if salt.utils.platform.is_windows():\n proxyminion = salt.cli.daemons.ProxyMinion()\n proxyminion.start()\n return\n\n if '--disable-keepalive' in sys.argv:\n sys.argv.remove('--disable-keepalive')\n proxyminion = salt.cli.daemons.ProxyMinion()\n proxyminion.start()\n return\n\n # keep one minion subprocess running\n while True:\n try:\n queue = multiprocessing.Queue()\n except Exception:\n # This breaks in containers\n proxyminion = salt.cli.daemons.ProxyMinion()\n proxyminion.start()\n return\n process = multiprocessing.Process(target=proxy_minion_process, args=(queue,))\n process.start()\n try:\n process.join()\n try:\n restart_delay = queue.get(block=False)\n except Exception:\n if process.exitcode == 0:\n # Minion process ended naturally, Ctrl+C or --version\n break\n restart_delay = 60\n if restart_delay == 0:\n # Minion process ended naturally, Ctrl+C, --version, etc.\n sys.exit(process.exitcode)\n # delay restart to reduce flooding and allow network resources to close\n time.sleep(restart_delay)\n except KeyboardInterrupt:\n break\n # need to reset logging because new minion objects\n # cause extra log handlers to accumulate\n rlogger = logging.getLogger()\n for handler in rlogger.handlers:\n rlogger.removeHandler(handler)\n logging.basicConfig()"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nstarts the salt syndic.", "response": "def salt_syndic():\n '''\n Start the salt syndic.\n '''\n import salt.utils.process\n salt.utils.process.notify_systemd()\n\n import salt.cli.daemons\n pid = os.getpid()\n try:\n syndic = salt.cli.daemons.Syndic()\n syndic.start()\n except KeyboardInterrupt:\n os.kill(pid, 15)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nmanage the authentication keys with salt - key.", "response": "def salt_key():\n '''\n Manage the authentication keys with salt-key.\n '''\n import salt.cli.key\n try:\n client = salt.cli.key.SaltKey()\n _install_signal_handlers(client)\n client.run()\n except Exception as err:\n sys.stderr.write(\"Error: {0}\\n\".format(err))"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef salt_cp():\n '''\n Publish commands to the salt system from the command line on the\n master.\n '''\n import salt.cli.cp\n client = salt.cli.cp.SaltCPCli()\n _install_signal_handlers(client)\n client.run()", "response": "Publish commands to the salt system from the command line on the\n master."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nexecute a salt convenience routine.", "response": "def salt_run():\n '''\n Execute a salt convenience routine.\n '''\n import salt.cli.run\n if '' in sys.path:\n sys.path.remove('')\n client = salt.cli.run.SaltRun()\n _install_signal_handlers(client)\n client.run()"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nexecute the salt - ssh system", "response": "def salt_ssh():\n '''\n Execute the salt-ssh system\n '''\n import salt.cli.ssh\n if '' in sys.path:\n sys.path.remove('')\n try:\n client = salt.cli.ssh.SaltSSH()\n _install_signal_handlers(client)\n client.run()\n except SaltClientError as err:\n trace = traceback.format_exc()\n try:\n hardcrash = client.options.hard_crash\n except (AttributeError, KeyError):\n hardcrash = False\n _handle_interrupt(\n SystemExit(err),\n err,\n hardcrash, trace=trace)"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\npublishes commands to the salt system from the command line on the master.", "response": "def salt_main():\n '''\n Publish commands to the salt system from the command line on the\n master.\n '''\n import salt.cli.salt\n if '' in sys.path:\n sys.path.remove('')\n client = salt.cli.salt.SaltCMD()\n _install_signal_handlers(client)\n client.run()"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nextends a salt - style node with a salt - style extension", "response": "def salt_extend(extension, name, description, salt_dir, merge):\n '''\n Quickstart for developing on the saltstack installation\n\n .. versionadded:: 2016.11.0\n '''\n import salt.utils.extend\n salt.utils.extend.run(extension=extension,\n name=name,\n description=description,\n salt_dir=salt_dir,\n merge=merge)"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nruns Salt Support that collects system data logs etc for debug and support purposes.", "response": "def salt_support():\n '''\n Run Salt Support that collects system data, logs etc for debug and support purposes.\n :return:\n '''\n\n import salt.cli.support.collector\n if '' in sys.path:\n sys.path.remove('')\n client = salt.cli.support.collector.SaltSupport()\n _install_signal_handlers(client)\n client.run()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef ext_pillar(minion_id,\n pillar, # pylint: disable=W0613\n command):\n '''\n Execute an unmodified puppet_node_classifier and read the output as YAML\n '''\n try:\n data = salt.utils.yaml.safe_load(__salt__['cmd.run']('{0} {1}'.format(command, minion_id)))\n return data['parameters']\n except Exception:\n log.critical('YAML data from %s failed to parse', command)\n return {}", "response": "Execute an unmodified puppet_node_classifier and read the output as YAML\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _pshell_json(cmd, cwd=None):\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'.format(cmd)\n log.debug('PowerShell: %s', cmd)\n ret = __salt__['cmd.run_all'](cmd, shell='powershell', cwd=cwd)\n\n if 'pid' in ret:\n del ret['pid']\n\n if ret.get('stderr', ''):\n error = ret['stderr'].splitlines()[0]\n raise CommandExecutionError(error, info=ret)\n\n if 'retcode' not in ret or ret['retcode'] != 0:\n # run_all logs an error to log.error, fail hard back to the user\n raise CommandExecutionError(\n 'Issue executing PowerShell {0}'.format(cmd), info=ret)\n\n # Sometimes Powershell returns an empty string, which isn't valid JSON\n if ret['stdout'] == '':\n ret['stdout'] = '{}'\n\n try:\n ret = salt.utils.json.loads(ret['stdout'], strict=False)\n except ValueError:\n raise CommandExecutionError(\n 'No JSON results from PowerShell', info=ret)\n\n return ret", "response": "Execute the desired powershell command and ensure that it returns data\n in JSON format and load that into python\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ninstalling a KB from a. msu file.", "response": "def install(path, restart=False):\n '''\n Install a KB from a .msu file.\n\n Args:\n\n path (str):\n The full path to the msu file to install\n\n restart (bool):\n ``True`` to force a restart if required by the installation. Adds\n the ``/forcerestart`` switch to the ``wusa.exe`` command. ``False``\n will add the ``/norestart`` switch instead. Default is ``False``\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``\n\n Raise:\n CommandExecutionError: If the package is already installed or an error\n is encountered\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' wusa.install C:/temp/KB123456.msu\n '''\n # Build the command\n cmd = ['wusa.exe', path, '/quiet']\n if restart:\n cmd.append('/forcerestart')\n else:\n cmd.append('/norestart')\n\n # Run the command\n ret_code = __salt__['cmd.retcode'](cmd, ignore_retcode=True)\n\n # Check the ret_code\n file_name = os.path.basename(path)\n errors = {2359302: '{0} is already installed'.format(file_name),\n 87: 'Unknown error'}\n if ret_code in errors:\n raise CommandExecutionError(errors[ret_code])\n elif ret_code:\n raise CommandExecutionError('Unknown error: {0}'.format(ret_code))\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list():\n '''\n Get a list of updates installed on the machine\n\n Returns:\n list: A list of installed updates\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' wusa.list\n '''\n kbs = []\n ret = _pshell_json('Get-HotFix | Select HotFixID')\n for item in ret:\n kbs.append(item['HotFixID'])\n return kbs", "response": "Get a list of updates installed on the machine\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef managed(name,\n data,\n **kwargs):\n '''\n Manage the device configuration given the input data structured\n according to the YANG models.\n\n data\n YANG structured data.\n\n models\n A list of models to be used when generating the config.\n\n profiles: ``None``\n Use certain profiles to generate the config.\n If not specified, will use the platform default profile(s).\n\n compliance_report: ``False``\n Return the compliance report in the comment.\n\n .. versionadded:: 2017.7.3\n\n test: ``False``\n Dry run? If set as ``True``, will apply the config, discard\n and return the changes. Default: ``False`` and will commit\n the changes on the device.\n\n commit: ``True``\n Commit? Default: ``True``.\n\n debug: ``False``\n Debug mode. Will insert a new key under the output dictionary,\n as ``loaded_config`` containing the raw configuration loaded on the device.\n\n replace: ``False``\n Should replace the config with the new generate one?\n\n State SLS example:\n\n .. code-block:: jinja\n\n {%- set expected_config = pillar.get('openconfig_interfaces_cfg') -%}\n interfaces_config:\n napalm_yang.managed:\n - data: {{ expected_config | json }}\n - models:\n - models.openconfig_interfaces\n - debug: true\n\n Pillar example:\n\n .. code-block:: yaml\n\n openconfig_interfaces_cfg:\n _kwargs:\n filter: true\n interfaces:\n interface:\n Et1:\n config:\n mtu: 9000\n Et2:\n config:\n description: \"description example\"\n '''\n models = kwargs.get('models', None)\n if isinstance(models, tuple) and isinstance(models[0], list):\n models = models[0]\n ret = salt.utils.napalm.default_ret(name)\n test = kwargs.get('test', False) or __opts__.get('test', False)\n debug = kwargs.get('debug', False) or __opts__.get('debug', False)\n commit = kwargs.get('commit', True) or __opts__.get('commit', True)\n replace = kwargs.get('replace', False) or __opts__.get('replace', False)\n return_compliance_report = kwargs.get('compliance_report', False) or __opts__.get('compliance_report', False)\n profiles = kwargs.get('profiles', [])\n temp_file = __salt__['temp.file']()\n log.debug('Creating temp file: %s', temp_file)\n if 'to_dict' not in data:\n data = {'to_dict': data}\n data = [data]\n with salt.utils.files.fopen(temp_file, 'w') as file_handle:\n salt.utils.yaml.safe_dump(\n salt.utils.json.loads(salt.utils.json.dumps(data)),\n file_handle,\n encoding='utf-8'\n )\n device_config = __salt__['napalm_yang.parse'](*models,\n config=True,\n profiles=profiles)\n log.debug('Parsed the config from the device:')\n log.debug(device_config)\n compliance_report = __salt__['napalm_yang.compliance_report'](device_config,\n *models,\n filepath=temp_file)\n log.debug('Compliance report:')\n log.debug(compliance_report)\n complies = compliance_report.get('complies', False)\n if complies:\n ret.update({\n 'result': True,\n 'comment': 'Already configured as required.'\n })\n log.debug('All good here.')\n return ret\n log.debug('Does not comply, trying to generate and load config')\n data = data[0]['to_dict']\n if '_kwargs' in data:\n data.pop('_kwargs')\n loaded_changes = __salt__['napalm_yang.load_config'](data,\n *models,\n profiles=profiles,\n test=test,\n debug=debug,\n commit=commit,\n replace=replace)\n log.debug('Loaded config result:')\n log.debug(loaded_changes)\n __salt__['file.remove'](temp_file)\n loaded_changes['compliance_report'] = compliance_report\n return salt.utils.napalm.loaded_ret(ret,\n loaded_changes,\n test,\n debug,\n opts=__opts__,\n compliance_report=return_compliance_report)", "response": "Manage the device configuration given the input data and the YANG data."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconfigures the network device.", "response": "def configured(name,\n data,\n **kwargs):\n '''\n Configure the network device, given the input data structured\n according to the YANG models.\n\n .. note::\n The main difference between this function and ``managed``\n is that the later generates and loads the configuration\n only when there are differences between the existing\n configuration on the device and the expected\n configuration. Depending on the platform and hardware\n capabilities, one could be more optimal than the other.\n Additionally, the output of the ``managed`` is different,\n in such a way that the ``pchange`` field in the output\n contains structured data, rather than text.\n\n data\n YANG structured data.\n\n models\n A list of models to be used when generating the config.\n\n profiles: ``None``\n Use certain profiles to generate the config.\n If not specified, will use the platform default profile(s).\n\n test: ``False``\n Dry run? If set as ``True``, will apply the config, discard\n and return the changes. Default: ``False`` and will commit\n the changes on the device.\n\n commit: ``True``\n Commit? Default: ``True``.\n\n debug: ``False``\n Debug mode. Will insert a new key under the output dictionary,\n as ``loaded_config`` containing the raw configuration loaded on the device.\n\n replace: ``False``\n Should replace the config with the new generate one?\n\n State SLS example:\n\n .. code-block:: jinja\n\n {%- set expected_config = pillar.get('openconfig_interfaces_cfg') -%}\n interfaces_config:\n napalm_yang.configured:\n - data: {{ expected_config | json }}\n - models:\n - models.openconfig_interfaces\n - debug: true\n\n Pillar example:\n\n .. code-block:: yaml\n\n openconfig_interfaces_cfg:\n _kwargs:\n filter: true\n interfaces:\n interface:\n Et1:\n config:\n mtu: 9000\n Et2:\n config:\n description: \"description example\"\n '''\n models = kwargs.get('models', None)\n if isinstance(models, tuple) and isinstance(models[0], list):\n models = models[0]\n ret = salt.utils.napalm.default_ret(name)\n test = kwargs.get('test', False) or __opts__.get('test', False)\n debug = kwargs.get('debug', False) or __opts__.get('debug', False)\n commit = kwargs.get('commit', True) or __opts__.get('commit', True)\n replace = kwargs.get('replace', False) or __opts__.get('replace', False)\n profiles = kwargs.get('profiles', [])\n if '_kwargs' in data:\n data.pop('_kwargs')\n loaded_changes = __salt__['napalm_yang.load_config'](data,\n *models,\n profiles=profiles,\n test=test,\n debug=debug,\n commit=commit,\n replace=replace)\n return salt.utils.napalm.loaded_ret(ret, loaded_changes, test, debug)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_datacenter_name():\n '''\n Returns the datacenter name configured on the proxy\n\n Supported proxies: esxcluster, esxdatacenter\n '''\n\n proxy_type = __salt__['vsphere.get_proxy_type']()\n details = None\n if proxy_type == 'esxcluster':\n details = __salt__['esxcluster.get_details']()\n elif proxy_type == 'esxdatacenter':\n details = __salt__['esxdatacenter.get_details']()\n if not details:\n raise salt.exceptions.CommandExecutionError(\n 'details for proxy type \\'{0}\\' not loaded'.format(proxy_type))\n return details['datacenter']", "response": "Returns the datacenter name configured on the proxy\n Supported proxies are esxcluster esxdatacenter"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconfigures a DVS in the provided datacenter.", "response": "def dvs_configured(name, dvs):\n '''\n Configures a DVS.\n\n Creates a new DVS, if it doesn't exist in the provided datacenter or\n reconfigures it if configured differently.\n\n dvs\n DVS dict representations (see module sysdocs)\n '''\n datacenter_name = _get_datacenter_name()\n dvs_name = dvs['name'] if dvs.get('name') else name\n log.info('Running state %s for DVS \\'%s\\' in datacenter \\'%s\\'',\n name, dvs_name, datacenter_name)\n changes_required = False\n ret = {'name': name, 'changes': {}, 'result': None, 'comment': None}\n comments = []\n changes = {}\n changes_required = False\n\n try:\n #TODO dvs validation\n si = __salt__['vsphere.get_service_instance_via_proxy']()\n dvss = __salt__['vsphere.list_dvss'](dvs_names=[dvs_name],\n service_instance=si)\n if not dvss:\n changes_required = True\n if __opts__['test']:\n comments.append('State {0} will create a new DVS '\n '\\'{1}\\' in datacenter \\'{2}\\''\n ''.format(name, dvs_name, datacenter_name))\n log.info(comments[-1])\n else:\n dvs['name'] = dvs_name\n __salt__['vsphere.create_dvs'](dvs_dict=dvs,\n dvs_name=dvs_name,\n service_instance=si)\n comments.append('Created a new DVS \\'{0}\\' in datacenter '\n '\\'{1}\\''.format(dvs_name, datacenter_name))\n log.info(comments[-1])\n changes.update({'dvs': {'new': dvs}})\n else:\n # DVS already exists. Checking various aspects of the config\n props = ['description', 'contact_email', 'contact_name',\n 'lacp_api_version', 'link_discovery_protocol',\n 'max_mtu', 'network_resource_control_version',\n 'network_resource_management_enabled']\n log.trace('DVS \\'%s\\' found in datacenter \\'%s\\'. Checking '\n 'for any updates in %s', dvs_name, datacenter_name, props)\n props_to_original_values = {}\n props_to_updated_values = {}\n current_dvs = dvss[0]\n for prop in props:\n if prop in dvs and dvs[prop] != current_dvs.get(prop):\n props_to_original_values[prop] = current_dvs.get(prop)\n props_to_updated_values[prop] = dvs[prop]\n\n # Simple infrastructure traffic resource control compare doesn't\n # work because num_shares is optional if share_level is not custom\n # We need to do a dedicated compare for this property\n infra_prop = 'infrastructure_traffic_resource_pools'\n original_infra_res_pools = []\n updated_infra_res_pools = []\n if infra_prop in dvs:\n if not current_dvs.get(infra_prop):\n updated_infra_res_pools = dvs[infra_prop]\n else:\n for idx in range(len(dvs[infra_prop])):\n if 'num_shares' not in dvs[infra_prop][idx] and \\\n current_dvs[infra_prop][idx]['share_level'] != \\\n 'custom' and \\\n 'num_shares' in current_dvs[infra_prop][idx]:\n\n del current_dvs[infra_prop][idx]['num_shares']\n if dvs[infra_prop][idx] != \\\n current_dvs[infra_prop][idx]:\n\n original_infra_res_pools.append(\n current_dvs[infra_prop][idx])\n updated_infra_res_pools.append(\n dict(dvs[infra_prop][idx]))\n if updated_infra_res_pools:\n props_to_original_values[\n 'infrastructure_traffic_resource_pools'] = \\\n original_infra_res_pools\n props_to_updated_values[\n 'infrastructure_traffic_resource_pools'] = \\\n updated_infra_res_pools\n if props_to_updated_values:\n if __opts__['test']:\n changes_string = ''\n for p in props_to_updated_values:\n if p == 'infrastructure_traffic_resource_pools':\n changes_string += \\\n '\\tinfrastructure_traffic_resource_pools:\\n'\n for idx in range(len(props_to_updated_values[p])):\n d = props_to_updated_values[p][idx]\n s = props_to_original_values[p][idx]\n changes_string += \\\n ('\\t\\t{0} from \\'{1}\\' to \\'{2}\\'\\n'\n ''.format(d['key'], s, d))\n else:\n changes_string += \\\n ('\\t{0} from \\'{1}\\' to \\'{2}\\'\\n'\n ''.format(p, props_to_original_values[p],\n props_to_updated_values[p]))\n comments.append(\n 'State dvs_configured will update DVS \\'{0}\\' '\n 'in datacenter \\'{1}\\':\\n{2}'\n ''.format(dvs_name, datacenter_name, changes_string))\n log.info(comments[-1])\n else:\n __salt__['vsphere.update_dvs'](\n dvs_dict=props_to_updated_values,\n dvs=dvs_name,\n service_instance=si)\n comments.append('Updated DVS \\'{0}\\' in datacenter \\'{1}\\''\n ''.format(dvs_name, datacenter_name))\n log.info(comments[-1])\n changes.update({'dvs': {'new': props_to_updated_values,\n 'old': props_to_original_values}})\n __salt__['vsphere.disconnect'](si)\n except salt.exceptions.CommandExecutionError as exc:\n log.exception('Encountered error')\n if si:\n __salt__['vsphere.disconnect'](si)\n if not __opts__['test']:\n ret['result'] = False\n ret.update({'comment': six.text_type(exc),\n 'result': False if not __opts__['test'] else None})\n return ret\n if not comments:\n # We have no changes\n ret.update({'comment': ('DVS \\'{0}\\' in datacenter \\'{1}\\' is '\n 'correctly configured. Nothing to be done.'\n ''.format(dvs_name, datacenter_name)),\n 'result': True})\n else:\n ret.update({\n 'comment': '\\n'.join(comments),\n 'changes': changes,\n 'result': None if __opts__['test'] else True,\n })\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_diff_dict(dict1, dict2):\n '''\n Returns a dictionary with the diffs between two dictionaries\n\n It will ignore any key that doesn't exist in dict2\n '''\n ret_dict = {}\n for p in dict2.keys():\n if p not in dict1:\n ret_dict.update({p: {'val1': None, 'val2': dict2[p]}})\n elif dict1[p] != dict2[p]:\n if isinstance(dict1[p], dict) and isinstance(dict2[p], dict):\n sub_diff_dict = _get_diff_dict(dict1[p], dict2[p])\n if sub_diff_dict:\n ret_dict.update({p: sub_diff_dict})\n else:\n ret_dict.update({p: {'val1': dict1[p], 'val2': dict2[p]}})\n return ret_dict", "response": "Returns a dictionary with the diffs between two dictionaries\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a dictionary with the values stored in val2 of a diff dict.", "response": "def _get_val2_dict_from_diff_dict(diff_dict):\n '''\n Returns a dictionaries with the values stored in val2 of a diff dict.\n '''\n ret_dict = {}\n for p in diff_dict.keys():\n if not isinstance(diff_dict[p], dict):\n raise ValueError('Unexpected diff difct \\'{0}\\''.format(diff_dict))\n if 'val2' in diff_dict[p].keys():\n ret_dict.update({p: diff_dict[p]['val2']})\n else:\n ret_dict.update(\n {p: _get_val2_dict_from_diff_dict(diff_dict[p])})\n return ret_dict"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nconfiguring the portgroups on a DVS", "response": "def portgroups_configured(name, dvs, portgroups):\n '''\n Configures portgroups on a DVS.\n\n Creates/updates/removes portgroups in a provided DVS\n\n dvs\n Name of the DVS\n\n portgroups\n Portgroup dict representations (see module sysdocs)\n '''\n datacenter = _get_datacenter_name()\n log.info('Running state %s on DVS \\'%s\\', datacenter \\'%s\\'',\n name, dvs, datacenter)\n changes_required = False\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': None}\n comments = []\n changes = {}\n changes_required = False\n\n try:\n #TODO portroups validation\n si = __salt__['vsphere.get_service_instance_via_proxy']()\n current_pgs = __salt__['vsphere.list_dvportgroups'](\n dvs=dvs, service_instance=si)\n expected_pg_names = []\n for pg in portgroups:\n pg_name = pg['name']\n expected_pg_names.append(pg_name)\n del pg['name']\n log.info('Checking pg \\'%s\\'', pg_name)\n filtered_current_pgs = \\\n [p for p in current_pgs if p.get('name') == pg_name]\n if not filtered_current_pgs:\n changes_required = True\n if __opts__['test']:\n comments.append('State {0} will create a new portgroup '\n '\\'{1}\\' in DVS \\'{2}\\', datacenter '\n '\\'{3}\\''.format(name, pg_name, dvs,\n datacenter))\n else:\n __salt__['vsphere.create_dvportgroup'](\n portgroup_dict=pg, portgroup_name=pg_name, dvs=dvs,\n service_instance=si)\n comments.append('Created a new portgroup \\'{0}\\' in DVS '\n '\\'{1}\\', datacenter \\'{2}\\''\n ''.format(pg_name, dvs, datacenter))\n log.info(comments[-1])\n changes.update({pg_name: {'new': pg}})\n else:\n # Porgroup already exists. Checking the config\n log.trace('Portgroup \\'%s\\' found in DVS \\'%s\\', datacenter '\n '\\'%s\\'. Checking for any updates.',\n pg_name, dvs, datacenter)\n current_pg = filtered_current_pgs[0]\n diff_dict = _get_diff_dict(current_pg, pg)\n\n if diff_dict:\n changes_required = True\n if __opts__['test']:\n changes_strings = \\\n _get_changes_from_diff_dict(diff_dict)\n log.trace('changes_strings = %s', changes_strings)\n comments.append(\n 'State {0} will update portgroup \\'{1}\\' in '\n 'DVS \\'{2}\\', datacenter \\'{3}\\':\\n{4}'\n ''.format(name, pg_name, dvs, datacenter,\n '\\n'.join(['\\t{0}'.format(c) for c in\n changes_strings])))\n else:\n __salt__['vsphere.update_dvportgroup'](\n portgroup_dict=pg, portgroup=pg_name, dvs=dvs,\n service_instance=si)\n comments.append('Updated portgroup \\'{0}\\' in DVS '\n '\\'{1}\\', datacenter \\'{2}\\''\n ''.format(pg_name, dvs, datacenter))\n log.info(comments[-1])\n changes.update(\n {pg_name: {'new':\n _get_val2_dict_from_diff_dict(diff_dict),\n 'old':\n _get_val1_dict_from_diff_dict(diff_dict)}})\n # Add the uplink portgroup to the expected pg names\n uplink_pg = __salt__['vsphere.list_uplink_dvportgroup'](\n dvs=dvs, service_instance=si)\n expected_pg_names.append(uplink_pg['name'])\n # Remove any extra portgroups\n for current_pg in current_pgs:\n if current_pg['name'] not in expected_pg_names:\n changes_required = True\n if __opts__['test']:\n comments.append('State {0} will remove '\n 'the portgroup \\'{1}\\' from DVS \\'{2}\\', '\n 'datacenter \\'{3}\\''\n ''.format(name, current_pg['name'], dvs,\n datacenter))\n else:\n __salt__['vsphere.remove_dvportgroup'](\n portgroup=current_pg['name'], dvs=dvs,\n service_instance=si)\n comments.append('Removed the portgroup \\'{0}\\' from DVS '\n '\\'{1}\\', datacenter \\'{2}\\''\n ''.format(current_pg['name'], dvs,\n datacenter))\n log.info(comments[-1])\n changes.update({current_pg['name']:\n {'old': current_pg}})\n __salt__['vsphere.disconnect'](si)\n except salt.exceptions.CommandExecutionError as exc:\n log.exception('Encountered error')\n if si:\n __salt__['vsphere.disconnect'](si)\n if not __opts__['test']:\n ret['result'] = False\n ret.update({'comment': exc.strerror,\n 'result': False if not __opts__['test'] else None})\n return ret\n if not changes_required:\n # We have no changes\n ret.update({'comment': ('All portgroups in DVS \\'{0}\\', datacenter '\n '\\'{1}\\' exist and are correctly configured. '\n 'Nothing to be done.'.format(dvs, datacenter)),\n 'result': True})\n else:\n ret.update({\n 'comment': '\\n'.join(comments),\n 'changes': changes,\n 'result': None if __opts__['test'] else True,\n })\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef uplink_portgroup_configured(name, dvs, uplink_portgroup):\n '''\n Configures the uplink portgroup on a DVS. The state assumes there is only\n one uplink portgroup.\n\n dvs\n Name of the DVS\n\n upling_portgroup\n Uplink portgroup dict representations (see module sysdocs)\n\n '''\n datacenter = _get_datacenter_name()\n log.info('Running %s on DVS \\'%s\\', datacenter \\'%s\\'', name, dvs, datacenter)\n changes_required = False\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': None}\n comments = []\n changes = {}\n changes_required = False\n\n try:\n #TODO portroups validation\n si = __salt__['vsphere.get_service_instance_via_proxy']()\n current_uplink_portgroup = __salt__['vsphere.list_uplink_dvportgroup'](\n dvs=dvs, service_instance=si)\n log.trace('current_uplink_portgroup = %s', current_uplink_portgroup)\n diff_dict = _get_diff_dict(current_uplink_portgroup, uplink_portgroup)\n if diff_dict:\n changes_required = True\n if __opts__['test']:\n changes_strings = \\\n _get_changes_from_diff_dict(diff_dict)\n log.trace('changes_strings = %s', changes_strings)\n comments.append(\n 'State {0} will update the '\n 'uplink portgroup in DVS \\'{1}\\', datacenter '\n '\\'{2}\\':\\n{3}'\n ''.format(name, dvs, datacenter,\n '\\n'.join(['\\t{0}'.format(c) for c in\n changes_strings])))\n else:\n __salt__['vsphere.update_dvportgroup'](\n portgroup_dict=uplink_portgroup,\n portgroup=current_uplink_portgroup['name'],\n dvs=dvs,\n service_instance=si)\n comments.append('Updated the uplink portgroup in DVS '\n '\\'{0}\\', datacenter \\'{1}\\''\n ''.format(dvs, datacenter))\n log.info(comments[-1])\n changes.update(\n {'uplink_portgroup':\n {'new': _get_val2_dict_from_diff_dict(diff_dict),\n 'old': _get_val1_dict_from_diff_dict(diff_dict)}})\n __salt__['vsphere.disconnect'](si)\n except salt.exceptions.CommandExecutionError as exc:\n log.exception('Encountered error')\n if si:\n __salt__['vsphere.disconnect'](si)\n if not __opts__['test']:\n ret['result'] = False\n ret.update({'comment': exc.strerror,\n 'result': False if not __opts__['test'] else None})\n return ret\n if not changes_required:\n # We have no changes\n ret.update({'comment': ('Uplink portgroup in DVS \\'{0}\\', datacenter '\n '\\'{1}\\' is correctly configured. '\n 'Nothing to be done.'.format(dvs, datacenter)),\n 'result': True})\n else:\n ret.update({\n 'comment': '\\n'.join(comments),\n 'changes': changes,\n 'result': None if __opts__['test'] else True,\n })\n return ret", "response": "Configure the uplink portgroup on a DVS."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nensuring that the named user is present with the specified privileges.", "response": "def present(name,\n createdb=None,\n createroles=None,\n encrypted=None,\n superuser=None,\n replication=None,\n inherit=None,\n login=None,\n password=None,\n default_password=None,\n refresh_password=None,\n valid_until=None,\n groups=None,\n user=None,\n maintenance_db=None,\n db_password=None,\n db_host=None,\n db_port=None,\n db_user=None):\n '''\n Ensure that the named user is present with the specified privileges\n Please note that the user/group notion in postgresql is just abstract, we\n have roles, where users can be seens as roles with the LOGIN privilege\n and groups the others.\n\n name\n The name of the system user to manage.\n\n createdb\n Is the user allowed to create databases?\n\n createroles\n Is the user allowed to create other users?\n\n encrypted\n Should the password be encrypted in the system catalog?\n\n login\n Should the group have login perm\n\n inherit\n Should the group inherit permissions\n\n superuser\n Should the new user be a \"superuser\"\n\n replication\n Should the new user be allowed to initiate streaming replication\n\n password\n The system user's password. It can be either a plain string or a\n md5 postgresql hashed password::\n\n 'md5{MD5OF({password}{role}}'\n\n If encrypted is None or True, the password will be automatically\n encrypted to the previous\n format if it is not already done.\n\n default_password\n The password used only when creating the user, unless password is set.\n\n .. versionadded:: 2016.3.0\n\n refresh_password\n Password refresh flag\n\n Boolean attribute to specify whether to password comparison check\n should be performed.\n\n If refresh_password is ``True``, the password will be automatically\n updated without extra password change check.\n\n This behaviour makes it possible to execute in environments without\n superuser access available, e.g. Amazon RDS for PostgreSQL\n\n valid_until\n A date and time after which the role's password is no longer valid.\n\n groups\n A string of comma separated groups the user should be in\n\n user\n System user all operations should be performed on behalf of\n\n .. versionadded:: 0.17.0\n\n db_user\n Postgres database username, if different from config or default.\n\n db_password\n Postgres user's password, if any password, for a specified db_user.\n\n db_host\n Postgres database host, if different from config or default.\n\n db_port\n Postgres database port, if different from config or default.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'User {0} is already present'.format(name)}\n\n # default to encrypted passwords\n if encrypted is not False:\n encrypted = postgres._DEFAULT_PASSWORDS_ENCRYPTION\n # maybe encrypt if it's not already and necessary\n password = postgres._maybe_encrypt_password(name,\n password,\n encrypted=encrypted)\n\n if default_password is not None:\n default_password = postgres._maybe_encrypt_password(name,\n default_password,\n encrypted=encrypted)\n\n db_args = {\n 'maintenance_db': maintenance_db,\n 'runas': user,\n 'host': db_host,\n 'user': db_user,\n 'port': db_port,\n 'password': db_password,\n }\n\n # check if user exists\n mode = 'create'\n user_attr = __salt__['postgres.role_get'](\n name, return_password=not refresh_password, **db_args)\n if user_attr is not None:\n mode = 'update'\n\n cret = None\n update = {}\n if mode == 'update':\n user_groups = user_attr.get('groups', [])\n if (\n createdb is not None\n and user_attr['can create databases'] != createdb\n ):\n update['createdb'] = createdb\n if (\n inherit is not None\n and user_attr['inherits privileges'] != inherit\n ):\n update['inherit'] = inherit\n if login is not None and user_attr['can login'] != login:\n update['login'] = login\n if (\n createroles is not None\n and user_attr['can create roles'] != createroles\n ):\n update['createroles'] = createroles\n if (\n replication is not None\n and user_attr['replication'] != replication\n ):\n update['replication'] = replication\n if superuser is not None and user_attr['superuser'] != superuser:\n update['superuser'] = superuser\n if password is not None and (refresh_password or user_attr['password'] != password):\n update['password'] = True\n if valid_until is not None:\n valid_until_dt = __salt__['postgres.psql_query'](\n 'SELECT \\'{0}\\'::timestamp(0) as dt;'.format(\n valid_until.replace('\\'', '\\'\\'')),\n **db_args)[0]['dt']\n try:\n valid_until_dt = datetime.datetime.strptime(\n valid_until_dt, '%Y-%m-%d %H:%M:%S')\n except ValueError:\n valid_until_dt = None\n if valid_until_dt != user_attr['expiry time']:\n update['valid_until'] = valid_until\n if groups is not None:\n lgroups = groups\n if isinstance(groups, (six.string_types, six.text_type)):\n lgroups = lgroups.split(',')\n if isinstance(lgroups, list):\n missing_groups = [a for a in lgroups if a not in user_groups]\n if missing_groups:\n update['groups'] = missing_groups\n\n if mode == 'create' and password is None:\n password = default_password\n\n if mode == 'create' or (mode == 'update' and update):\n if __opts__['test']:\n if update:\n ret['changes'][name] = update\n ret['result'] = None\n ret['comment'] = 'User {0} is set to be {1}d'.format(name, mode)\n return ret\n cret = __salt__['postgres.user_{0}'.format(mode)](\n username=name,\n createdb=createdb,\n createroles=createroles,\n encrypted=encrypted,\n superuser=superuser,\n login=login,\n inherit=inherit,\n replication=replication,\n rolepassword=password,\n valid_until=valid_until,\n groups=groups,\n **db_args)\n else:\n cret = None\n\n if cret:\n ret['comment'] = 'The user {0} has been {1}d'.format(name, mode)\n if update:\n ret['changes'][name] = update\n else:\n ret['changes'][name] = 'Present'\n elif cret is not None:\n ret['comment'] = 'Failed to create user {0}'.format(name)\n ret['result'] = False\n else:\n ret['result'] = True\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nensures that the named property is present in the specified zone.", "response": "def property_present(name, property, value):\n '''\n Ensure property has a certain value\n\n name : string\n name of the zone\n property : string\n name of property\n value : string\n value of property\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n ## sanitize input\n value = _parse_value(value)\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name in zones:\n ## zone exists\n zonecfg = __salt__['zonecfg.info'](name, show_all=True)\n if property not in zonecfg or zonecfg[property] != _parse_value(value):\n if __opts__['test']:\n ret['result'] = True\n else:\n # update property\n zonecfg_res = __salt__['zonecfg.set_property'](name, property, value)\n ret['result'] = zonecfg_res['status']\n if 'messages' in zonecfg_res:\n ret['comment'] = zonecfg_res['message']\n if ret['result']:\n ret['changes'][property] = _parse_value(value)\n if ret['comment'] == '':\n ret['comment'] = 'The property {0} is was updated to {1}.'.format(property, value)\n elif ret['comment'] == '':\n if ret['comment'] == '':\n ret['comment'] = 'The property {0} is was not updated to {1}!'.format(property, value)\n else:\n ret['result'] = True\n ret['comment'] = 'The property {0} is already set to {1}.'.format(property, value)\n else:\n ## zone does not exist\n ret['result'] = False\n ret['comment'] = 'The zone {0} is not in the configured, installed, or booted state.'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nensuring the specified property is absent in the specified resource tree.", "response": "def property_absent(name, property):\n '''\n Ensure property is absent\n\n name : string\n name of the zone\n property : string\n name of property\n\n .. note::\n This does a zoneacfg clear call. So the property may be reset to a default value!\n Does has the side effect of always having to be called.\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name in zones:\n ## zone exists\n zonecfg = __salt__['zonecfg.info'](name, show_all=True)\n if property in zonecfg:\n if __opts__['test']:\n ret['result'] = True\n else:\n # clear property\n zonecfg_res = __salt__['zonecfg.clear_property'](name, property)\n zonecfg_new = __salt__['zonecfg.info'](name, show_all=True)\n ret['result'] = zonecfg_res['status']\n if 'messages' in zonecfg_res:\n ret['comment'] = zonecfg_res['message']\n if ret['result']:\n if property not in zonecfg_new:\n ret['changes'][property] = None\n elif zonecfg[property] != zonecfg_new[property]:\n ret['changes'][property] = zonecfg_new[property]\n if ret['comment'] == '':\n ret['comment'] = 'The property {0} was cleared!'.format(property)\n elif ret['comment'] == '':\n if ret['comment'] == '':\n ret['comment'] = 'The property {0} did not get cleared!'.format(property)\n else:\n ret['result'] = True\n ret['comment'] = 'The property {0} does not exist!'.format(property)\n else:\n ## zone does not exist\n ret['result'] = False\n ret['comment'] = 'The zone {0} is not in the configured, installed, or booted state.'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nensures a resource exists with provided properties", "response": "def resource_present(name, resource_type, resource_selector_property, resource_selector_value, **kwargs):\n '''\n Ensure resource exists with provided properties\n\n name : string\n name of the zone\n resource_type : string\n type of resource\n resource_selector_property : string\n unique resource identifier\n resource_selector_value : string\n value for resource selection\n kwargs : string|int|...\n resource properties\n\n .. warning::\n Both resource_selector_property and resource_selector_value must be\n provided, some properties like ``name`` are already reserved by salt in\n states.\n\n .. note::\n You can set both resource_selector_property and resource_selector_value\n to None for resources that do not require them.\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n # sanitize input\n kwargs = salt.utils.args.clean_kwargs(**kwargs)\n resource_selector_value = _parse_value(resource_selector_value)\n for k, v in kwargs.items():\n kwargs[k] = _parse_value(kwargs[k])\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name in zones:\n ## zone exists\n zonecfg = __salt__['zonecfg.info'](name, show_all=True)\n\n ## update kwargs\n zonecfg_kwargs = {}\n zonecfg_kwargs.update(kwargs)\n zonecfg_kwargs['zone'] = name\n zonecfg_kwargs['resource_type'] = resource_type\n zonecfg_kwargs['resource_selector'] = resource_selector_property\n if resource_selector_property:\n zonecfg_kwargs[resource_selector_property] = resource_selector_value\n\n ## check update or add\n if resource_type in zonecfg:\n for resource in zonecfg[resource_type]:\n if not resource_selector_property or resource[resource_selector_property] == resource_selector_value:\n ret['result'] = True\n if resource_selector_property:\n ret['comment'] = 'the {0} resource {1} is up to date.'.format(\n resource_type,\n resource_selector_value,\n )\n else:\n ret['comment'] = 'the {0} resource is up to date.'.format(\n resource_type,\n )\n\n ## check if update reauired\n for key in kwargs:\n log.debug('zone.resource_preent - key=%s value=%s current_value=%s',\n key,\n resource[key] if key in resource else None,\n _parse_value(kwargs[key]),\n )\n # note: something odd with ncpus property, we fix it here for now\n if key == 'ncpus' and key in kwargs:\n kwargs[key] = '{0:.2f}'.format(float(kwargs[key]))\n\n if key not in resource:\n ret['result'] = None\n elif resource[key] != _parse_value(kwargs[key]):\n ret['result'] = None\n\n ## do update\n if ret['result'] is None:\n if __opts__['test']:\n ret['result'] = True\n else:\n ## update resource\n zonecfg_res = __salt__['zonecfg.update_resource'](**zonecfg_kwargs)\n ret['result'] = zonecfg_res['status']\n if 'message' in zonecfg_res:\n ret['comment'] = zonecfg_res['message']\n\n if ret['result']:\n ret['changes'][resource_type] = {}\n if resource_selector_property:\n ret['changes'][resource_type][resource_selector_value] = {}\n for key in kwargs if ret['result'] else []:\n if resource_selector_property:\n ret['changes'][resource_type][resource_selector_value][key] = _parse_value(kwargs[key])\n else:\n ret['changes'][resource_type][key] = _parse_value(kwargs[key])\n if ret['comment'] == '':\n if resource_selector_property:\n ret['comment'] = 'The {0} resource {1} was updated.'.format(\n resource_type,\n resource_selector_value,\n )\n else:\n ret['comment'] = 'The {0} resource was updated.'.format(\n resource_type,\n )\n elif ret['comment'] == '':\n if resource_selector_property:\n ret['comment'] = 'The {0} resource {1} was not updated.'.format(\n resource_type,\n resource_selector_value,\n )\n else:\n ret['comment'] = 'The {0} resource was not updated.'.format(\n resource_type,\n )\n if ret['result'] is None:\n ## add\n if __opts__['test']:\n ret['result'] = True\n else:\n ## add resource\n if 'resource_selector' in zonecfg_kwargs:\n del zonecfg_kwargs['resource_selector']\n zonecfg_res = __salt__['zonecfg.add_resource'](**zonecfg_kwargs)\n ret['result'] = zonecfg_res['status']\n if 'message' in zonecfg_res:\n ret['comment'] = zonecfg_res['message']\n\n if ret['result']:\n ret['changes'][resource_type] = {}\n if resource_selector_property:\n ret['changes'][resource_type][resource_selector_value] = {}\n for key in kwargs if ret['result'] else []:\n if resource_selector_property:\n ret['changes'][resource_type][resource_selector_value][key] = _parse_value(kwargs[key])\n else:\n ret['changes'][resource_type][key] = _parse_value(kwargs[key])\n if ret['comment'] == '':\n ret['comment'] = 'The {0} resource {1} was added.'.format(\n resource_type,\n resource_selector_value,\n )\n elif ret['comment'] == '':\n ret['comment'] = 'The {0} resource {1} was not added.'.format(\n resource_type,\n resource_selector_value,\n )\n else:\n ## zone does not exist\n ret['result'] = False\n ret['comment'] = 'The zone {0} is not in the configured, installed, or booted state.'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures a resource is absent in the specified zone.", "response": "def resource_absent(name, resource_type, resource_selector_property, resource_selector_value):\n '''\n Ensure resource is absent\n\n name : string\n name of the zone\n resource_type : string\n type of resource\n resource_selector_property : string\n unique resource identifier\n resource_selector_value : string\n value for resource selection\n\n .. warning::\n Both resource_selector_property and resource_selector_value must be provided, some properties\n like ```name``` are already reserved by salt in there states.\n\n .. note::\n You can set both resource_selector_property and resource_selector_value to None for\n resources that do not require them.\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n # sanitize input\n if resource_selector_property:\n resource_selector_value = _parse_value(resource_selector_value)\n else:\n resource_selector_value = None\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name in zones:\n ## zone exists\n zonecfg = __salt__['zonecfg.info'](name, show_all=True)\n if resource_type in zonecfg:\n for resource in zonecfg[resource_type]:\n if __opts__['test']:\n ret['result'] = True\n elif not resource_selector_property:\n zonecfg_res = __salt__['zonecfg.remove_resource'](\n zone=name,\n resource_type=resource_type,\n resource_key=None,\n resource_value=None,\n )\n ret['result'] = zonecfg_res['status']\n if zonecfg_res['status']:\n ret['changes'][resource_type] = 'removed'\n if ret['comment'] == '':\n ret['comment'] = 'The {0} resource was removed.'.format(\n resource_type,\n )\n elif 'messages' in zonecfg_res:\n ret['comment'] = zonecfg_res['message']\n else:\n ret['comment'] = 'The {0} resource was not removed.'.format(\n resource_type,\n )\n elif resource[resource_selector_property] == resource_selector_value:\n zonecfg_res = __salt__['zonecfg.remove_resource'](\n zone=name,\n resource_type=resource_type,\n resource_key=resource_selector_property,\n resource_value=resource_selector_value,\n )\n ret['result'] = zonecfg_res['status']\n if zonecfg_res['status']:\n ret['changes'][resource_type] = {}\n ret['changes'][resource_type][resource_selector_value] = 'removed'\n if ret['comment'] == '':\n ret['comment'] = 'The {0} resource {1} was removed.'.format(\n resource_type,\n resource_selector_value,\n )\n elif 'messages' in zonecfg_res:\n ret['comment'] = zonecfg_res['message']\n else:\n ret['comment'] = 'The {0} resource {1} was not removed.'.format(\n resource_type,\n resource_selector_value,\n )\n\n # resource already absent\n if ret['result'] is None:\n ret['result'] = True\n ret['comment'] = 'The {0} resource {1} was absent.'.format(\n resource_type,\n resource_selector_value,\n )\n else:\n ## zone does not exist\n ret['result'] = False\n ret['comment'] = 'The zone {0} is not in the configured, installed, or booted state.'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef booted(name, single=False):\n '''\n Ensure zone is booted\n\n name : string\n name of the zone\n single : boolean\n boot in single usermode\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n zones = __salt__['zoneadm.list'](installed=True)\n if name in zones:\n ## zone exists\n if zones[name]['state'] == 'running':\n ## zone is running\n ret['result'] = True\n ret['comment'] = 'Zone {0} already booted'.format(name)\n else:\n ## try and boot the zone\n if not __opts__['test']:\n zoneadm_res = __salt__['zoneadm.boot'](name, single)\n if __opts__['test'] or zoneadm_res['status']:\n ret['result'] = True\n ret['changes'][name] = 'booted'\n ret['comment'] = 'Zone {0} booted'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to boot {0}'.format(name)\n else:\n ## zone does not exist\n ret['comment'] = []\n ret['comment'].append(\n 'The zone {0} is not in the installed or booted state.'.format(name)\n )\n for zone in zones:\n if zones[zone]['uuid'] == name:\n ret['comment'].append(\n 'The zone {0} has a uuid of {1}, please use the zone name instead!'.format(\n zone,\n name,\n )\n )\n\n ret['result'] = False\n ret['comment'] = \"\\n\".join(ret['comment'])\n\n return ret", "response": "Ensure the zone is booted in a single usermode"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef halted(name, graceful=True):\n '''\n Ensure zone is halted\n\n name : string\n name of the zone\n graceful : boolean\n use shutdown instead of halt if true\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n zones = __salt__['zoneadm.list'](installed=True)\n if name in zones:\n ## zone exists\n if zones[name]['state'] != 'running':\n ## zone is not running\n ret['result'] = True\n ret['comment'] = 'Zone {0} already halted'.format(name)\n else:\n ## try and halt the zone\n if not __opts__['test']:\n zoneadm_res = __salt__['zoneadm.shutdown'](name) if graceful else __salt__['zoneadm.halt'](name)\n if __opts__['test'] or zoneadm_res['status']:\n ret['result'] = True\n ret['changes'][name] = 'halted'\n ret['comment'] = 'Zone {0} halted'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to halt {0}'.format(name)\n else:\n ## zone does not exist\n ret['comment'] = []\n ret['comment'].append(\n 'The zone {0} is not in the installed state.'.format(name)\n )\n for zone in zones:\n if zones[zone]['uuid'] == name:\n ret['comment'].append(\n 'The zone {0} has a uuid of {1}, please use the zone name instead!'.format(\n zone,\n name,\n )\n )\n ## note: a non existing zone is not running, we do not consider this a failure\n ret['result'] = True\n ret['comment'] = \"\\n\".join(ret['comment'])\n\n return ret", "response": "Ensure the zone is halted"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nexport a zone configuration to a file", "response": "def export(name, path, replace=False):\n '''\n Export a zones configuration\n\n name : string\n name of the zone\n path : string\n path of file to export too.\n replace : boolean\n replace the file if it exists\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name in zones:\n ## zone exists\n if __opts__['test']:\n ## pretend we did the correct thing\n ret['result'] = True\n ret['comment'] = 'Zone configartion for {0} exported to {1}'.format(\n name,\n path,\n )\n ret['changes'][name] = 'exported'\n if __salt__['file.file_exists'](path) and not replace:\n ret['result'] = False\n ret['changes'] = {}\n ret['comment'] = 'File {0} exists, zone configuration for {1} not exported.'.format(\n path,\n name,\n )\n else:\n ## export and update file\n cfg_tmp = salt.utils.files.mkstemp()\n __salt__['zonecfg.export'](name, cfg_tmp)\n if not __salt__['file.file_exists'](path):\n ## move cfg_tmp to path\n try:\n __salt__['file.move'](cfg_tmp, path)\n except CommandExecutionError:\n if __salt__['file.file_exists'](cfg_tmp):\n __salt__['file.remove'](cfg_tmp)\n ret['result'] = False\n ret['comment'] = 'Unable to export zone configuration for {0} to {1}!'.format(\n name,\n path,\n )\n else:\n ret['result'] = True\n ret['comment'] = 'Zone configuration for {0} was exported to {1}.'.format(\n name,\n path,\n )\n ret['changes'][name] = 'exported'\n else:\n cfg_diff = __salt__['file.get_diff'](path, cfg_tmp)\n if not cfg_diff:\n ret['result'] = True\n ret['comment'] = 'Zone configuration for {0} was already exported to {1}.'.format(\n name,\n path\n )\n if __salt__['file.file_exists'](cfg_tmp):\n __salt__['file.remove'](cfg_tmp)\n else:\n if replace:\n try:\n __salt__['file.move'](cfg_tmp, path)\n except CommandExecutionError:\n if __salt__['file.file_exists'](cfg_tmp):\n __salt__['file.remove'](cfg_tmp)\n ret['result'] = False\n ret['comment'] = 'Unable to be re-export zone configuration for {0} to {1}!'.format(\n name,\n path,\n )\n else:\n ret['result'] = True\n ret['comment'] = 'Zone configuration for {0} was re-exported to {1}.'.format(\n name,\n path,\n )\n ret['changes'][name] = 'exported'\n else:\n ret['result'] = False\n ret['comment'] = 'Zone configuration for {0} is different from the one exported to {1}!'.format(\n name,\n path\n )\n if __salt__['file.file_exists'](cfg_tmp):\n __salt__['file.remove'](cfg_tmp)\n else:\n ## zone does not exist\n ret['comment'] = []\n ret['comment'].append(\n 'The zone {0} does not exist.'.format(name)\n )\n for zone in zones:\n if zones[zone]['uuid'] == name:\n ret['comment'].append(\n 'The zone {0} has a uuid of {1}, please use the zone name instead!'.format(\n name,\n path,\n )\n )\n\n ret['result'] = False\n ret['comment'] = \"\\n\".join(ret['comment'])\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef import_(name, path, mode='import', nodataset=False, brand_opts=None):\n '''\n Import a zones configuration\n\n name : string\n name of the zone\n path : string\n path of the configuration file to import\n mode : string\n either import, install, or attach\n nodataset : boolean\n do not create a ZFS file system\n brand_opts : boolean\n brand specific options to pass\n\n .. note::\n The mode argument can be set to ``import``, ``install``, or ``attach``.\n ``import``: will only import the configuration\n ``install``: will import and then try to install the zone\n ``attach``: will import and then try to attach of the zone\n\n .. code-block:: yaml\n\n omipkg1:\n zone.import:\n - path: /foo/bar/baz\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name not in zones:\n if __opts__['test']:\n ret['result'] = True\n ret['comment'] = 'Zone {0} was imported from {1}.'.format(\n name,\n path,\n )\n ret['changes'][name] = 'imported'\n else:\n if __salt__['file.file_exists'](path):\n res_import = __salt__['zonecfg.import'](name, path)\n if not res_import['status']:\n ret['result'] = False\n ret['comment'] = 'Unable to import zone configuration for {0}!'.format(name)\n else:\n ret['result'] = True\n ret['changes'][name] = 'imported'\n ret['comment'] = 'Zone {0} was imported from {1}.'.format(\n name,\n path,\n )\n if mode.lower() == 'attach':\n res_attach = __salt__['zoneadm.attach'](name, False, brand_opts)\n ret['result'] = res_attach['status']\n if res_attach['status']:\n ret['changes'][name] = 'attached'\n ret['comment'] = 'Zone {0} was attached from {1}.'.format(\n name,\n path,\n )\n else:\n ret['comment'] = []\n ret['comment'].append('Failed to attach zone {0} from {1}!'.format(\n name,\n path,\n ))\n if 'message' in res_attach:\n ret['comment'].append(res_attach['message'])\n ret['comment'] = \"\\n\".join(ret['comment'])\n if mode.lower() == 'install':\n res_install = __salt__['zoneadm.install'](name, nodataset, brand_opts)\n ret['result'] = res_install['status']\n if res_install['status']:\n ret['changes'][name] = 'installed'\n ret['comment'] = 'Zone {0} was installed from {1}.'.format(\n name,\n path,\n )\n else:\n ret['comment'] = []\n ret['comment'].append('Failed to install zone {0} from {1}!'.format(\n name,\n path,\n ))\n if 'message' in res_install:\n ret['comment'].append(res_install['message'])\n ret['comment'] = \"\\n\".join(ret['comment'])\n else:\n ret['result'] = False\n ret['comment'] = 'The file {0} does not exists, unable to import!'.format(path)\n else:\n ## zone exist\n ret['result'] = True\n ret['comment'] = 'Zone {0} already exists, not importing configuration.'.format(name)\n\n return ret", "response": "Import a zone from a configuration file"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nensures a zone with certain properties and resources exists.", "response": "def present(name, brand, zonepath, properties=None, resources=None):\n '''\n Ensure a zone with certain properties and resources\n\n name : string\n name of the zone\n brand : string\n brand of the zone\n zonepath : string\n path of the zone\n properties : list of key-value pairs\n dict of properties\n resources : list of key-value pairs\n dict of resources\n\n .. note::\n If the zone does not exist it will not be installed.\n You can use the ```zone.installed``` state for this.\n\n .. note::\n Default resource selectors:\n - fs: dir\n - net: mac-addr\n - device: match\n - rctl: name\n - attr: name\n - dataset: name\n - admin: user\n\n .. warning::\n Properties and resource will not be removed when they\n are absent from the state!\n\n For properties, simple set them to ```None```.\n\n For resources, add the ```resource_prune``` property\n and set it to ```True```. Also specify the\n ```resource_selector_property``` if the default is not\n the one you want.\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': []}\n\n ## sanitize defaults\n if not properties:\n properties = []\n if not resources:\n resources = []\n properties.append(OrderedDict({\"brand\": brand}))\n properties.append(OrderedDict({\"zonepath\": zonepath}))\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n\n ## test mode only has limited support\n if __opts__['test']:\n ret['result'] = None\n ret['comment'].append('Cannot determine of changes would happen to the zone {0}.'.format(name))\n\n ## create zone if needed\n if name not in zones:\n if __opts__['test']:\n ## we pretend we created the zone\n res_create = {'status': True}\n ret['comment'] = []\n else:\n ## create and install\n res_create = __salt__['zonecfg.create'](name, brand, zonepath)\n if res_create['status']:\n ret['result'] = True\n ret['changes'][name] = 'created'\n ret['comment'].append('The zone {0} was created.'.format(name))\n\n if not __opts__['test']:\n ret['result'] = True\n if isinstance(properties, list):\n for prop in properties:\n if not isinstance(prop, OrderedDict) or len(prop) != 1:\n log.warning('zone.present - failed to parse property: %s', prop)\n continue\n for key, value in prop.items():\n res = None\n if not value:\n res = property_absent(name, key)\n elif value:\n res = property_present(name, key, value)\n if res:\n ret['result'] = ret['result'] if res['result'] else False\n ret['comment'].append(res['comment'])\n if res['changes']:\n if 'property' not in ret['changes']:\n ret['changes']['property'] = {}\n ret['changes']['property'] = merge_dict(ret['changes']['property'], res['changes'])\n if isinstance(resources, list):\n for resource in resources:\n if not isinstance(prop, OrderedDict) or len(prop) != 1:\n log.warning('zone.present - failed to parse resource: %s', resource)\n continue\n for key, value in resource.items():\n zonecfg = __salt__['zonecfg.info'](name, show_all=True)\n resource_cfg = {}\n resource_cfg['resource_type'] = key\n if isinstance(value, list):\n for respv in value:\n resource_cfg.update(dict(respv))\n\n resource_prune = False\n resource_selector_property = None\n if 'resource_prune' in resource_cfg:\n resource_prune = resource_cfg['resource_prune']\n del resource_cfg['resource_prune']\n if 'resource_selector_property' in resource_cfg:\n resource_selector_property = resource_cfg['resource_selector_property']\n del resource_cfg['resource_selector_property']\n if not resource_selector_property and key in _zonecfg_resource_default_selectors:\n resource_selector_property = _zonecfg_resource_default_selectors[key]\n\n res = None\n if resource_prune:\n res = resource_absent(\n name,\n resource_cfg['resource_type'],\n resource_selector_property=resource_selector_property,\n resource_selector_value=resource_cfg[resource_selector_property] if resource_selector_property else None,\n )\n else:\n resource_cfg['resource_selector_property'] = resource_selector_property\n if resource_selector_property in resource_cfg:\n resource_cfg['resource_selector_value'] = resource_cfg[resource_selector_property]\n else:\n resource_cfg['resource_selector_value'] = None\n resource_cfg['name'] = name # we do this last because name can also be a attrib value\n res = resource_present(**resource_cfg)\n if res:\n ret['result'] = ret['result'] if res['result'] else False\n ret['comment'].append(res['comment'])\n if res['changes']:\n if 'resource' not in ret['changes']:\n ret['changes']['resource'] = {}\n ret['changes']['resource'] = merge_dict(ret['changes']['resource'], res['changes'])\n\n if isinstance(ret['comment'], list):\n ret['comment'] = \"\\n\".join(ret['comment'])\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nensuring a zone is absent.", "response": "def absent(name, uninstall=False):\n '''\n Ensure a zone is absent\n\n name : string\n name of the zone\n uninstall : boolean\n when true, uninstall instead of detaching the zone first.\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name in zones:\n if __opts__['test']:\n ret['result'] = True\n ret['changes'][name] = 'removed'\n ret['comment'] = 'Zone {0} was removed.'.format(name)\n else:\n ret['result'] = True\n if uninstall and zones[name]['state'] in ['running', 'installed']:\n res_halt = __salt__['zoneadm.halt'](name)\n res_uninstall = __salt__['zoneadm.uninstall'](name)\n ret['result'] = res_uninstall['status']\n if ret['result']:\n ret['changes'][name] = 'uninstalled'\n ret['comment'] = 'The zone {0} was uninstalled.'.format(name)\n else:\n ret['comment'] = []\n ret['comment'].append('Failed to uninstall zone {0}!'.format(name))\n if 'message' in res_uninstall:\n ret['comment'].append(res_uninstall['message'])\n ret['comment'] = \"\\n\".join(ret['comment'])\n elif zones[name]['state'] == 'installed':\n res_detach = __salt__['zoneadm.detach'](name)\n ret['result'] = res_detach['status']\n if ret['result']:\n ret['changes'][name] = 'detached'\n ret['comment'] = 'The zone {0} was detached.'.format(name)\n else:\n ret['comment'] = []\n ret['comment'].append('Failed to detach zone {0}!'.format(name))\n if 'message' in res_detach:\n ret['comment'].append(res_detach['message'])\n ret['comment'] = \"\\n\".join(ret['comment'])\n if ret['result']:\n res_delete = __salt__['zonecfg.delete'](name)\n ret['result'] = res_delete['status']\n if ret['result']:\n ret['changes'][name] = 'deleted'\n ret['comment'] = 'The zone {0} was delete.'.format(name)\n else:\n ret['comment'] = []\n ret['comment'].append('Failed to delete zone {0}!'.format(name))\n if 'message' in res_delete:\n ret['comment'].append(res_delete['message'])\n ret['comment'] = \"\\n\".join(ret['comment'])\n else:\n ret['result'] = True\n ret['comment'] = 'Zone {0} does not exist.'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nensure zone is attached to the WorkItem", "response": "def attached(name, force=False):\n '''\n Ensure zone is attached\n\n name : string\n name of the zone\n force : boolean\n force attach the zone\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name in zones:\n if zones[name]['state'] == 'configured':\n if __opts__['test']:\n res_attach = {'status': True}\n else:\n res_attach = __salt__['zoneadm.attach'](name, force)\n ret['result'] = res_attach['status']\n if ret['result']:\n ret['changes'][name] = 'attached'\n ret['comment'] = 'The zone {0} was attached.'.format(name)\n else:\n ret['comment'] = []\n ret['comment'].append('Failed to attach zone {0}!'.format(name))\n if 'message' in res_attach:\n ret['comment'].append(res_attach['message'])\n ret['comment'] = \"\\n\".join(ret['comment'])\n else:\n ret['result'] = True\n ret['comment'] = 'zone {0} already attached.'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'zone {0} is not configured!'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nensure zone is detached", "response": "def detached(name):\n '''\n Ensure zone is detached\n\n name : string\n name of the zone\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name in zones:\n if zones[name]['state'] != 'configured':\n if __opts__['test']:\n res_detach = {'status': True}\n else:\n res_detach = __salt__['zoneadm.detach'](name)\n ret['result'] = res_detach['status']\n if ret['result']:\n ret['changes'][name] = 'detached'\n ret['comment'] = 'The zone {0} was detached.'.format(name)\n else:\n ret['comment'] = []\n ret['comment'].append('Failed to detach zone {0}!'.format(name))\n if 'message' in res_detach:\n ret['comment'].append(res_detach['message'])\n ret['comment'] = \"\\n\".join(ret['comment'])\n else:\n ret['result'] = True\n ret['comment'] = 'zone {0} already detached.'.format(name)\n else:\n ## note: a non existing zone is not attached, we do not consider this a failure\n ret['result'] = True\n ret['comment'] = 'zone {0} is not configured!'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef installed(name, nodataset=False, brand_opts=None):\n '''\n Ensure zone is installed\n\n name : string\n name of the zone\n nodataset : boolean\n do not create a ZFS file system\n brand_opts : boolean\n brand specific options to pass\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name in zones:\n if zones[name]['state'] == 'configured':\n if __opts__['test']:\n res_install = {'status': True}\n else:\n res_install = __salt__['zoneadm.install'](name, nodataset, brand_opts)\n ret['result'] = res_install['status']\n if ret['result']:\n ret['changes'][name] = 'installed'\n ret['comment'] = 'The zone {0} was installed.'.format(name)\n else:\n ret['comment'] = []\n ret['comment'].append('Failed to install zone {0}!'.format(name))\n if 'message' in res_install:\n ret['comment'].append(res_install['message'])\n ret['comment'] = \"\\n\".join(ret['comment'])\n else:\n ret['result'] = True\n ret['comment'] = 'zone {0} already installed.'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'zone {0} is not configured!'.format(name)\n\n return ret", "response": "Ensure zone is installed"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef uninstalled(name):\n '''\n Ensure zone is uninstalled\n\n name : string\n name of the zone\n\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n zones = __salt__['zoneadm.list'](installed=True, configured=True)\n if name in zones:\n if zones[name]['state'] != 'configured':\n if __opts__['test']:\n res_uninstall = {'status': True}\n else:\n res_uninstall = __salt__['zoneadm.uninstall'](name)\n ret['result'] = res_uninstall['status']\n if ret['result']:\n ret['changes'][name] = 'uninstalled'\n ret['comment'] = 'The zone {0} was uninstalled.'.format(name)\n else:\n ret['comment'] = []\n ret['comment'].append('Failed to uninstall zone {0}!'.format(name))\n if 'message' in res_uninstall:\n ret['comment'].append(res_uninstall['message'])\n ret['comment'] = \"\\n\".join(ret['comment'])\n else:\n ret['result'] = True\n ret['comment'] = 'zone {0} already uninstalled.'.format(name)\n else:\n ## note: a non existing zone is not installed, we do not consider this a failure\n ret['result'] = True\n ret['comment'] = 'zone {0} is not configured!'.format(name)\n\n return ret", "response": "Ensure zone is uninstalled"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nvalidating the beacon configuration.", "response": "def validate(config):\n '''\n Validate the beacon configuration\n '''\n _config = {}\n list(map(_config.update, config))\n\n if not isinstance(config, list):\n return False, ('Configuration for avahi_announce '\n 'beacon must be a list.')\n\n elif not all(x in _config for x in ('servicetype',\n 'port',\n 'txt')):\n return False, ('Configuration for avahi_announce beacon '\n 'must contain servicetype, port and txt items.')\n return True, 'Valid beacon configuration.'"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nbroadcast values via zeroconf If the announced values are static, it is advised to set run_once: True (do not poll) on the beacon configuration. The following are required configuration settings: - ``servicetype`` - The service type to announce - ``port`` - The port of the service to announce - ``txt`` - The TXT record of the service being announced as a dict. Grains can be used to define TXT values using one of following two formats: - ``grains.<grain_name>`` - ``grains.<grain_name>[i]`` where i is an integer representing the index of the grain to use. If the grain is not a list, the index is ignored. The following are optional configuration settings: - ``servicename`` - Set the name of the service. Will use the hostname from the minion's ``host`` grain if this value is not set. - ``reset_on_change`` - If ``True`` and there is a change in TXT records detected, it will stop announcing the service and then restart announcing the service. This interruption in service announcement may be desirable if the client relies on changes in the browse records to update its cache of TXT records. Defaults to ``False``. - ``reset_wait`` - The number of seconds to wait after announcement stops announcing and before it restarts announcing in the case where there is a change in TXT records detected and ``reset_on_change`` is ``True``. Defaults to ``0``. - ``copy_grains`` - If ``True``, Salt will copy the grains passed into the beacon when it backs them up to check for changes on the next iteration. Normally, instead of copy, it would use straight value assignment. This will allow detection of changes to grains where the grains are modified in-place instead of completely replaced. In-place grains changes are not currently done in the main Salt code but may be done due to a custom plug-in. Defaults to ``False``. Example Config .. code-block:: yaml beacons: avahi_announce: - run_once: True - servicetype: _demo._tcp - port: 1234 - txt: ProdName: grains.productname SerialNo: grains.serialnumber Comments: 'this is a test'", "response": "def beacon(config):\n '''\n Broadcast values via zeroconf\n\n If the announced values are static, it is advised to set run_once: True\n (do not poll) on the beacon configuration.\n\n The following are required configuration settings:\n\n - ``servicetype`` - The service type to announce\n - ``port`` - The port of the service to announce\n - ``txt`` - The TXT record of the service being announced as a dict. Grains\n can be used to define TXT values using one of following two formats:\n\n - ``grains.<grain_name>``\n - ``grains.<grain_name>[i]`` where i is an integer representing the\n index of the grain to use. If the grain is not a list, the index is\n ignored.\n\n The following are optional configuration settings:\n\n - ``servicename`` - Set the name of the service. Will use the hostname from\n the minion's ``host`` grain if this value is not set.\n - ``reset_on_change`` - If ``True`` and there is a change in TXT records\n detected, it will stop announcing the service and then restart announcing\n the service. This interruption in service announcement may be desirable\n if the client relies on changes in the browse records to update its cache\n of TXT records. Defaults to ``False``.\n - ``reset_wait`` - The number of seconds to wait after announcement stops\n announcing and before it restarts announcing in the case where there is a\n change in TXT records detected and ``reset_on_change`` is ``True``.\n Defaults to ``0``.\n - ``copy_grains`` - If ``True``, Salt will copy the grains passed into the\n beacon when it backs them up to check for changes on the next iteration.\n Normally, instead of copy, it would use straight value assignment. This\n will allow detection of changes to grains where the grains are modified\n in-place instead of completely replaced. In-place grains changes are not\n currently done in the main Salt code but may be done due to a custom\n plug-in. Defaults to ``False``.\n\n Example Config\n\n .. code-block:: yaml\n\n beacons:\n avahi_announce:\n - run_once: True\n - servicetype: _demo._tcp\n - port: 1234\n - txt:\n ProdName: grains.productname\n SerialNo: grains.serialnumber\n Comments: 'this is a test'\n '''\n ret = []\n changes = {}\n txt = {}\n\n global LAST_GRAINS\n\n _config = {}\n list(map(_config.update, config))\n\n if 'servicename' in _config:\n servicename = _config['servicename']\n else:\n servicename = __grains__['host']\n # Check for hostname change\n if LAST_GRAINS and LAST_GRAINS['host'] != servicename:\n changes['servicename'] = servicename\n\n if LAST_GRAINS and _config.get('reset_on_change', False):\n # Check for IP address change in the case when we reset on change\n if LAST_GRAINS.get('ipv4', []) != __grains__.get('ipv4', []):\n changes['ipv4'] = __grains__.get('ipv4', [])\n if LAST_GRAINS.get('ipv6', []) != __grains__.get('ipv6', []):\n changes['ipv6'] = __grains__.get('ipv6', [])\n\n for item in _config['txt']:\n changes_key = 'txt.' + salt.utils.stringutils.to_unicode(item)\n if _config['txt'][item].startswith('grains.'):\n grain = _config['txt'][item][7:]\n grain_index = None\n square_bracket = grain.find('[')\n if square_bracket != -1 and grain[-1] == ']':\n grain_index = int(grain[square_bracket+1:-1])\n grain = grain[:square_bracket]\n\n grain_value = __grains__.get(grain, '')\n if isinstance(grain_value, list):\n if grain_index is not None:\n grain_value = grain_value[grain_index]\n else:\n grain_value = ','.join(grain_value)\n txt[item] = _enforce_txt_record_maxlen(item, grain_value)\n if LAST_GRAINS and (LAST_GRAINS.get(grain, '') != __grains__.get(grain, '')):\n changes[changes_key] = txt[item]\n else:\n txt[item] = _enforce_txt_record_maxlen(item, _config['txt'][item])\n\n if not LAST_GRAINS:\n changes[changes_key] = txt[item]\n\n if changes:\n if not LAST_GRAINS:\n changes['servicename'] = servicename\n changes['servicetype'] = _config['servicetype']\n changes['port'] = _config['port']\n changes['ipv4'] = __grains__.get('ipv4', [])\n changes['ipv6'] = __grains__.get('ipv6', [])\n GROUP.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),\n servicename, _config['servicetype'], '', '',\n dbus.UInt16(_config['port']), avahi.dict_to_txt_array(txt))\n GROUP.Commit()\n elif _config.get('reset_on_change', False) or 'servicename' in changes:\n # A change in 'servicename' requires a reset because we can only\n # directly update TXT records\n GROUP.Reset()\n reset_wait = _config.get('reset_wait', 0)\n if reset_wait > 0:\n time.sleep(reset_wait)\n GROUP.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),\n servicename, _config['servicetype'], '', '',\n dbus.UInt16(_config['port']), avahi.dict_to_txt_array(txt))\n GROUP.Commit()\n else:\n GROUP.UpdateServiceTxt(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),\n servicename, _config['servicetype'], '',\n avahi.dict_to_txt_array(txt))\n\n ret.append({'tag': 'result', 'changes': changes})\n\n if _config.get('copy_grains', False):\n LAST_GRAINS = __grains__.copy()\n else:\n LAST_GRAINS = __grains__\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nenforcing that the WAR will be deployed and started in the context path, while making use of WAR versions in the filename. .. note:: For more info about Tomcats file paths and context naming, please see http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Naming name The context path to deploy (incl. forward slash) the WAR to. war Absolute path to WAR file (should be accessible by the user running Tomcat) or a path supported by the ``salt.modules.cp.get_url`` function. force : False Force deployment even if the version strings are the same. Disabled by default. url : http://localhost:8080/manager The URL of the Tomcat Web Application Manager. timeout : 180 Timeout for HTTP requests to the Tomcat Manager. temp_war_location : None Use another location to temporarily copy the WAR file to. By default the system's temp directory is used. version : '' Specify the WAR version. If this argument is provided, it overrides the version encoded in the WAR file name, if one is present. .. versionadded:: 2015.8.6 Use ``False`` or blank value to prevent guessing the version and keeping it blank. .. versionadded:: 2016.11.0 Example: .. code-block:: yaml jenkins: tomcat.war_deployed: - name: /salt-powered-jenkins - war: salt://jenkins-1.2.4.war - require: - service: application-service .. note:: Be aware that in the above example the WAR ``jenkins-1.2.4.war`` will be deployed to the context path ``salt-powered-jenkins##1.2.4``. To avoid this either specify a version yourself, or set version to ``False``.", "response": "def war_deployed(name,\n war,\n force=False,\n url='http://localhost:8080/manager',\n timeout=180,\n temp_war_location=None,\n version=True):\n '''\n Enforce that the WAR will be deployed and started in the context path,\n while making use of WAR versions in the filename.\n\n .. note::\n\n For more info about Tomcats file paths and context naming, please see\n http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Naming\n\n name\n The context path to deploy (incl. forward slash) the WAR to.\n war\n Absolute path to WAR file (should be accessible by the user running\n Tomcat) or a path supported by the ``salt.modules.cp.get_url`` function.\n force : False\n Force deployment even if the version strings are the same.\n Disabled by default.\n url : http://localhost:8080/manager\n The URL of the Tomcat Web Application Manager.\n timeout : 180\n Timeout for HTTP requests to the Tomcat Manager.\n temp_war_location : None\n Use another location to temporarily copy the WAR file to.\n By default the system's temp directory is used.\n version : ''\n Specify the WAR version. If this argument is provided, it overrides\n the version encoded in the WAR file name, if one is present.\n\n .. versionadded:: 2015.8.6\n\n Use ``False`` or blank value to prevent guessing the version and keeping it blank.\n\n .. versionadded:: 2016.11.0\n\n Example:\n\n .. code-block:: yaml\n\n jenkins:\n tomcat.war_deployed:\n - name: /salt-powered-jenkins\n - war: salt://jenkins-1.2.4.war\n - require:\n - service: application-service\n\n .. note::\n\n Be aware that in the above example the WAR ``jenkins-1.2.4.war`` will\n be deployed to the context path ``salt-powered-jenkins##1.2.4``. To avoid this\n either specify a version yourself, or set version to ``False``.\n\n '''\n # Prepare\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n\n # if version is defined or False, we don't want to overwrite\n if version is True:\n version = __salt__['tomcat.extract_war_version'](war) or ''\n elif not version:\n version = ''\n\n webapps = __salt__['tomcat.ls'](url, timeout)\n deploy = False\n undeploy = False\n status = True\n\n # Gathered/specified new WAR version string\n specified_ver = 'version {0}'.format(version) if version else 'no version'\n\n # Determine what to do\n try:\n # Printed version strings, here to throw exception if no webapps[name]\n current_ver = 'version ' + webapps[name]['version'] \\\n if webapps[name]['version'] else 'no version'\n # `endswith` on the supposed string will cause Exception if empty\n if (not webapps[name]['version'].endswith(version)\n or (version == '' and webapps[name]['version'] != version)\n or force):\n deploy = True\n undeploy = True\n ret['changes']['undeploy'] = ('undeployed {0} with {1}'.\n format(name, current_ver))\n ret['changes']['deploy'] = ('will deploy {0} with {1}'.\n format(name, specified_ver))\n else:\n deploy = False\n ret['comment'] = ('{0} with {1} is already deployed'.\n format(name, specified_ver))\n if webapps[name]['mode'] != 'running':\n ret['changes']['start'] = 'starting {0}'.format(name)\n status = False\n else:\n return ret\n except Exception:\n deploy = True\n ret['changes']['deploy'] = ('deployed {0} with {1}'.\n format(name, specified_ver))\n\n # Test\n if __opts__['test']:\n ret['result'] = None\n return ret\n\n # make sure the webapp is up if deployed\n if deploy is False:\n if status is False:\n ret['comment'] = __salt__['tomcat.start'](name, url,\n timeout=timeout)\n ret['result'] = ret['comment'].startswith('OK')\n return ret\n\n # Undeploy\n if undeploy:\n un = __salt__['tomcat.undeploy'](name, url, timeout=timeout)\n if un.startswith('FAIL'):\n ret['result'] = False\n ret['comment'] = un\n return ret\n\n # Deploy\n deploy_res = __salt__['tomcat.deploy_war'](war,\n name,\n 'yes',\n url,\n __env__,\n timeout,\n temp_war_location=temp_war_location,\n version=version)\n\n # Return\n if deploy_res.startswith('OK'):\n ret['result'] = True\n ret['comment'] = six.text_type(__salt__['tomcat.ls'](url, timeout)[name])\n ret['changes']['deploy'] = ('deployed {0} with {1}'.\n format(name, specified_ver))\n else:\n ret['result'] = False\n ret['comment'] = deploy_res\n ret['changes'].pop('deploy')\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef wait(name, url='http://localhost:8080/manager', timeout=180):\n '''\n Wait for the Tomcat Manager to load.\n\n Notice that if tomcat is not running we won't wait for it start and the\n state will fail. This state can be required in the tomcat.war_deployed\n state to make sure tomcat is running and that the manager is running as\n well and ready for deployment.\n\n url : http://localhost:8080/manager\n The URL of the server with the Tomcat Manager webapp.\n timeout : 180\n Timeout for HTTP request to the Tomcat Manager.\n\n Example:\n\n .. code-block:: yaml\n\n tomcat-service:\n service.running:\n - name: tomcat\n - enable: True\n\n wait-for-tomcatmanager:\n tomcat.wait:\n - timeout: 300\n - require:\n - service: tomcat-service\n\n jenkins:\n tomcat.war_deployed:\n - name: /ran\n - war: salt://jenkins-1.2.4.war\n - require:\n - tomcat: wait-for-tomcatmanager\n '''\n\n result = __salt__['tomcat.status'](url, timeout)\n ret = {'name': name,\n 'result': result,\n 'changes': {},\n 'comment': ('tomcat manager is ready' if result\n else 'tomcat manager is not ready')\n }\n\n return ret", "response": "Wait for the Tomcat Manager to load."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nenforcing that the WAR is undeployed from the Tomcat Manager webapp.", "response": "def undeployed(name,\n url='http://localhost:8080/manager',\n timeout=180):\n '''\n Enforce that the WAR will be undeployed from the server\n\n name\n The context path to undeploy.\n url : http://localhost:8080/manager\n The URL of the server with the Tomcat Manager webapp.\n timeout : 180\n Timeout for HTTP request to the Tomcat Manager.\n\n Example:\n\n .. code-block:: yaml\n\n jenkins:\n tomcat.undeployed:\n - name: /ran\n - require:\n - service: application-service\n '''\n\n # Prepare\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n\n if not __salt__['tomcat.status'](url, timeout):\n ret['comment'] = 'Tomcat Manager does not respond'\n ret['result'] = False\n return ret\n\n try:\n version = __salt__['tomcat.ls'](url, timeout)[name]['version']\n ret['changes'] = {'undeploy': version}\n except KeyError:\n return ret\n\n # Test\n if __opts__['test']:\n ret['result'] = None\n return ret\n\n undeploy = __salt__['tomcat.undeploy'](name, url, timeout=timeout)\n if undeploy.startswith('FAIL'):\n ret['result'] = False\n ret['comment'] = undeploy\n return ret\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef send_msg(recipient, message, jid=None, password=None, profile=None):\n '''\n Send a message to an XMPP recipient. Designed for use in states.\n\n CLI Examples:\n\n .. code-block:: bash\n\n xmpp.send_msg 'admins@xmpp.example.com' 'This is a salt module test' \\\n profile='my-xmpp-account'\n xmpp.send_msg 'admins@xmpp.example.com' 'This is a salt module test' \\\n jid='myuser@xmpp.example.com/salt' password='verybadpass'\n '''\n if profile:\n creds = __salt__['config.option'](profile)\n jid = creds.get('xmpp.jid')\n password = creds.get('xmpp.password')\n\n xmpp = SendMsgBot(jid, password, recipient, message)\n xmpp.register_plugin('xep_0030') # Service Discovery\n xmpp.register_plugin('xep_0199') # XMPP Ping\n\n if xmpp.connect():\n xmpp.process(block=True)\n return True\n return False", "response": "Send a message to an XMPP recipient."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef send_msg_multi(message,\n recipients=None,\n rooms=None,\n jid=None,\n password=None,\n nick=\"SaltStack Bot\",\n profile=None):\n '''\n Send a message to an XMPP recipient, support send message to\n multiple recipients or chat room.\n\n CLI Examples:\n\n .. code-block:: bash\n\n xmpp.send_msg recipients=['admins@xmpp.example.com'] \\\n rooms=['secret@conference.xmpp.example.com'] \\\n 'This is a salt module test' \\\n profile='my-xmpp-account'\n xmpp.send_msg recipients=['admins@xmpp.example.com'] \\\n rooms=['secret@conference.xmpp.example.com'] \\\n 'This is a salt module test' \\\n jid='myuser@xmpp.example.com/salt' password='verybadpass'\n\n '''\n\n # Remove: [WARNING ] Use of send mask waiters is deprecated.\n for handler in logging.root.handlers:\n handler.addFilter(SleekXMPPMUC())\n\n if profile:\n creds = __salt__['config.option'](profile)\n jid = creds.get('xmpp.jid')\n password = creds.get('xmpp.password')\n\n xmpp = SendMsgBot.create_multi(\n jid, password, message, recipients=recipients, rooms=rooms, nick=nick)\n\n if rooms:\n xmpp.register_plugin('xep_0045') # MUC plugin\n if xmpp.connect():\n try:\n xmpp.process(block=True)\n return True\n except XMPPError as err:\n log.error(\"Could not send message, error: %s\", err)\n else:\n log.error(\"Could not connect to XMPP server\")\n return False", "response": "Send a message to an XMPP recipient or chat room."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a SendMsgBot object that accepts multiple recipients and rooms.", "response": "def create_multi(cls, jid, password, msg, recipients=None, rooms=None,\n nick=\"SaltStack Bot\"):\n '''\n Alternate constructor that accept multiple recipients and rooms\n '''\n obj = SendMsgBot(jid, password, None, msg)\n obj.recipients = [] if recipients is None else recipients\n obj.rooms = [] if rooms is None else rooms\n obj.nick = nick\n return obj"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nemit a dict name \"texts\" whose value is a list of texts. .. code-block:: yaml beacons: twilio_txt_msg: - account_sid: \"<account sid>\" - auth_token: \"<auth token>\" - twilio_number: \"+15555555555\" - interval: 10", "response": "def beacon(config):\n '''\n Emit a dict name \"texts\" whose value is a list\n of texts.\n\n .. code-block:: yaml\n\n beacons:\n twilio_txt_msg:\n - account_sid: \"<account sid>\"\n - auth_token: \"<auth token>\"\n - twilio_number: \"+15555555555\"\n - interval: 10\n\n '''\n log.trace('twilio_txt_msg beacon starting')\n\n _config = {}\n list(map(_config.update, config))\n\n ret = []\n if not all([_config['account_sid'],\n _config['auth_token'],\n _config['twilio_number']]):\n return ret\n output = {}\n output['texts'] = []\n client = TwilioRestClient(_config['account_sid'], _config['auth_token'])\n messages = client.messages.list(to=_config['twilio_number'])\n num_messages = len(messages)\n log.trace('Num messages: %d', num_messages)\n if not num_messages:\n log.trace('Twilio beacon has no texts')\n return ret\n\n for message in messages:\n item = {}\n item['id'] = six.text_type(message.sid)\n item['body'] = six.text_type(message.body)\n item['from'] = six.text_type(message.from_)\n item['sent'] = six.text_type(message.date_sent)\n item['images'] = []\n\n if int(message.num_media):\n media = client.media(message.sid).list()\n if media:\n for pic in media:\n item['images'].append(six.text_type(pic.uri))\n output['texts'].append(item)\n message.delete()\n ret.append(output)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nchecking to see if the given rule exists.", "response": "def exists(Name, region=None, key=None, keyid=None, profile=None):\n '''\n Given a rule name, check to see if the given rule exists.\n\n Returns True if the given rule exists and returns False if the given\n rule does not exist.\n\n CLI example::\n\n salt myminion boto_cloudwatch_event.exists myevent region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n events = conn.list_rules(NamePrefix=Name)\n if not events:\n return {'exists': False}\n for rule in events.get('Rules', []):\n if rule.get('Name', None) == Name:\n return {'exists': True}\n return {'exists': False}\n except ClientError as e:\n err = __utils__['boto3.get_error'](e)\n return {'error': err}"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef create_or_update(Name,\n ScheduleExpression=None,\n EventPattern=None,\n Description=None,\n RoleArn=None,\n State=None,\n region=None, key=None, keyid=None, profile=None):\n '''\n Given a valid config, create an event rule.\n\n Returns {created: true} if the rule was created and returns\n {created: False} if the rule was not created.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudwatch_event.create_or_update my_rule\n\n '''\n\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n kwargs = {}\n for arg in ('ScheduleExpression', 'EventPattern', 'State',\n 'Description', 'RoleArn'):\n if locals()[arg] is not None:\n kwargs[arg] = locals()[arg]\n rule = conn.put_rule(Name=Name,\n **kwargs)\n if rule:\n log.info('The newly created event rule is %s', rule.get('RuleArn'))\n\n return {'created': True, 'arn': rule.get('RuleArn')}\n else:\n log.warning('Event rule was not created')\n return {'created': False}\n except ClientError as e:\n return {'created': False, 'error': __utils__['boto3.get_error'](e)}", "response": "Given a valid config create an event rule."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef describe(Name,\n region=None, key=None, keyid=None, profile=None):\n '''\n Given a rule name describe its properties.\n\n Returns a dictionary of interesting properties.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudwatch_event.describe myrule\n\n '''\n\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n rule = conn.describe_rule(Name=Name)\n if rule:\n keys = ('Name', 'Arn', 'EventPattern',\n 'ScheduleExpression', 'State',\n 'Description',\n 'RoleArn')\n return {'rule': dict([(k, rule.get(k)) for k in keys])}\n else:\n return {'rule': None}\n except ClientError as e:\n err = __utils__['boto3.get_error'](e)\n if e.response.get('Error', {}).get('Code') == 'RuleNotFoundException':\n return {'error': \"Rule {0} not found\".format(Rule)}\n return {'error': __utils__['boto3.get_error'](e)}", "response": "Given a rule name describe its properties."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef list_rules(region=None, key=None, keyid=None, profile=None):\n '''\n List, with details, all Cloudwatch Event rules visible in the current scope.\n\n CLI example::\n\n salt myminion boto_cloudwatch_event.list_rules region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n try:\n ret = []\n NextToken = ''\n while NextToken is not None:\n args = {'NextToken': NextToken} if NextToken else {}\n r = conn.list_rules(**args)\n ret += r.get('Rules', [])\n NextToken = r.get('NextToken')\n return ret\n except ClientError as e:\n return {'error': __utils__['boto3.get_error'](e)}", "response": "List all Cloudwatch Event rules visible in the current scope."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef list_targets(Rule,\n region=None, key=None, keyid=None, profile=None):\n '''\n Given a rule name list the targets of that rule.\n\n Returns a dictionary of interesting properties.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudwatch_event.list_targets myrule\n\n '''\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n targets = conn.list_targets_by_rule(Rule=Rule)\n ret = []\n if targets and 'Targets' in targets:\n keys = ('Id', 'Arn', 'Input',\n 'InputPath')\n for target in targets.get('Targets'):\n ret.append(dict([(k, target.get(k)) for k in keys if k in target]))\n return {'targets': ret}\n else:\n return {'targets': None}\n except ClientError as e:\n err = __utils__['boto3.get_error'](e)\n if e.response.get('Error', {}).get('Code') == 'RuleNotFoundException':\n return {'error': \"Rule {0} not found\".format(Rule)}\n return {'error': __utils__['boto3.get_error'](e)}", "response": "Given a rule name list the targets of that rule."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef put_targets(Rule, Targets,\n region=None, key=None, keyid=None, profile=None):\n '''\n Add the given targets to the given rule\n\n Returns a dictionary describing any failures.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion boto_cloudwatch_event.put_targets myrule [{'Id': 'target1', 'Arn': 'arn:***'}]\n\n '''\n try:\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n if isinstance(Targets, six.string_types):\n Targets = salt.utils.json.loads(Targets)\n failures = conn.put_targets(Rule=Rule, Targets=Targets)\n if failures and failures.get('FailedEntryCount', 0) > 0:\n return {'failures': failures.get('FailedEntries')}\n else:\n return {'failures': None}\n except ClientError as e:\n err = __utils__['boto3.get_error'](e)\n if e.response.get('Error', {}).get('Code') == 'RuleNotFoundException':\n return {'error': \"Rule {0} not found\".format(Rule)}\n return {'error': __utils__['boto3.get_error'](e)}", "response": "Adds the given targets to the given rule returning a dictionary describing any failures."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_enabled_disabled(enabled_prop=\"true\"):\n '''\n DRY: Get all service FMRIs and their enabled property\n '''\n ret = set()\n cmd = '/usr/bin/svcprop -c -p general/enabled \"*\"'\n lines = __salt__['cmd.run_stdout'](cmd, python_shell=False).splitlines()\n for line in lines:\n comps = line.split()\n if not comps:\n continue\n if comps[2] == enabled_prop:\n ret.add(comps[0].split(\"/:properties\")[0])\n return sorted(ret)", "response": "Get all service FMRIs and their disabled properties"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef available(name):\n '''\n Returns ``True`` if the specified service is available, otherwise returns\n ``False``.\n\n We look up the name with the svcs command to get back the FMRI\n This allows users to use simpler service names\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.available net-snmp\n '''\n cmd = '/usr/bin/svcs -H -o FMRI {0}'.format(name)\n name = __salt__['cmd.run'](cmd, python_shell=False)\n return name in get_all()", "response": "Returns True if the specified service is available otherwise returns False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn True if the specified service is not available.", "response": "def missing(name):\n '''\n The inverse of service.available.\n Returns ``True`` if the specified service is not available, otherwise returns\n ``False``.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.missing net-snmp\n '''\n cmd = '/usr/bin/svcs -H -o FMRI {0}'.format(name)\n name = __salt__['cmd.run'](cmd, python_shell=False)\n return name not in get_all()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_all():\n '''\n Return all installed services\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.get_all\n '''\n ret = set()\n cmd = '/usr/bin/svcs -aH -o FMRI,STATE -s FMRI'\n lines = __salt__['cmd.run'](cmd).splitlines()\n for line in lines:\n comps = line.split()\n if not comps:\n continue\n ret.add(comps[0])\n return sorted(ret)", "response": "Return all installed services\n CLI Example :\n Return all installed services\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nrestarting the named service CLI Example : bash restart service name", "response": "def restart(name):\n '''\n Restart the named service\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.restart <service name>\n '''\n cmd = '/usr/sbin/svcadm restart {0}'.format(name)\n if not __salt__['cmd.retcode'](cmd, python_shell=False):\n # calling restart doesn't clear maintenance\n # or tell us that the service is in the 'online' state\n return start(name)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef reload_(name):\n '''\n Reload the named service\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.reload <service name>\n '''\n cmd = '/usr/sbin/svcadm refresh {0}'.format(name)\n if not __salt__['cmd.retcode'](cmd, python_shell=False):\n # calling reload doesn't clear maintenance\n # or tell us that the service is in the 'online' state\n return start(name)\n return False", "response": "Reload the named service\n CLI Example : bash\n reload service\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef status(name, sig=None):\n '''\n Return the status for a service.\n If the name contains globbing, a dict mapping service name to True/False\n values is returned.\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 <service name>\n '''\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 cmd = '/usr/bin/svcs -H -o STATE {0}'.format(service)\n line = __salt__['cmd.run'](cmd, python_shell=False)\n results[service] = line == 'online'\n if contains_globbing:\n return results\n return results[name]", "response": "Return the status of a service."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nenable the named service to start at boot CLI Example : bash service. enable", "response": "def enable(name, **kwargs):\n '''\n Enable the named service to start at boot\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.enable <service name>\n '''\n cmd = '/usr/sbin/svcadm enable {0}'.format(name)\n return not __salt__['cmd.retcode'](cmd, python_shell=False)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef disable(name, **kwargs):\n '''\n Disable the named service to start at boot\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.disable <service name>\n '''\n cmd = '/usr/sbin/svcadm disable {0}'.format(name)\n return not __salt__['cmd.retcode'](cmd, python_shell=False)", "response": "Disable the named service to start at boot\n CLI Example : bash\n service. disable"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef enabled(name, **kwargs):\n '''\n Check to see if the named service is enabled to start on boot\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' service.enabled <service name>\n '''\n # The property that reveals whether a service is enabled\n # can only be queried using the full FMRI\n # We extract the FMRI and then do the query\n fmri_cmd = '/usr/bin/svcs -H -o FMRI {0}'.format(name)\n fmri = __salt__['cmd.run'](fmri_cmd, python_shell=False)\n cmd = '/usr/sbin/svccfg -s {0} listprop general/enabled'.format(fmri)\n comps = __salt__['cmd.run'](cmd, python_shell=False).split()\n if comps[2] == 'true':\n return True\n else:\n return False", "response": "Check to see if the named service is enabled to start on boot\n CLI Example : bash\n service. enabled <service name >"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef upgrade_available(name, **kwargs):\n '''\n Check whether or not an upgrade is available for a given package\n\n Args:\n name (str): The name of a single package\n\n Kwargs:\n refresh (bool): Refresh package metadata. Default ``True``\n saltenv (str): The salt environment. Default ``base``\n\n Returns:\n bool: True if new version available, otherwise False\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade_available <package name>\n '''\n saltenv = kwargs.get('saltenv', 'base')\n # Refresh before looking for the latest version available,\n # same default as latest_version\n refresh = salt.utils.data.is_true(kwargs.get('refresh', True))\n\n # if latest_version returns blank, the latest version is already installed or\n # their is no package definition. This is a salt standard which could be improved.\n return latest_version(name, saltenv=saltenv, refresh=refresh) != ''", "response": "Check whether or not an upgrade is available for a given package."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nlist all available upgrades on this system", "response": "def list_upgrades(refresh=True, **kwargs):\n '''\n List all available package upgrades on this system\n\n Args:\n refresh (bool): Refresh package metadata. Default ``True``\n\n Kwargs:\n saltenv (str): Salt environment. Default ``base``\n\n Returns:\n dict: A dictionary of packages with available upgrades\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_upgrades\n '''\n saltenv = kwargs.get('saltenv', 'base')\n refresh = salt.utils.data.is_true(refresh)\n _refresh_db_conditional(saltenv, force=refresh)\n\n installed_pkgs = list_pkgs(refresh=False, saltenv=saltenv)\n available_pkgs = get_repo_data(saltenv).get('repo')\n pkgs = {}\n for pkg in installed_pkgs:\n if pkg in available_pkgs:\n # latest_version() will be blank if the latest version is installed.\n # or the package name is wrong. Given we check available_pkgs, this\n # should not be the case of wrong package name.\n # Note: latest_version() is an expensive way to do this as it\n # calls list_pkgs each time.\n latest_ver = latest_version(pkg, refresh=False, saltenv=saltenv)\n if latest_ver:\n pkgs[pkg] = latest_ver\n\n return pkgs"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of available versions of the specified package.", "response": "def list_available(*names, **kwargs):\n '''\n Return a list of available versions of the specified package.\n\n Args:\n names (str): One or more package names\n\n Kwargs:\n\n saltenv (str): The salt environment to use. Default ``base``.\n\n refresh (bool): Refresh package metadata. Default ``False``.\n\n return_dict_always (bool):\n Default ``False`` dict when a single package name is queried.\n\n Returns:\n dict: The package name with its available versions\n\n .. code-block:: cfg\n\n {'<package name>': ['<version>', '<version>', ]}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_available <package name> return_dict_always=True\n salt '*' pkg.list_available <package name01> <package name02>\n '''\n if not names:\n return ''\n\n saltenv = kwargs.get('saltenv', 'base')\n refresh = salt.utils.data.is_true(kwargs.get('refresh', False))\n _refresh_db_conditional(saltenv, force=refresh)\n return_dict_always = \\\n salt.utils.data.is_true(kwargs.get('return_dict_always', False))\n if len(names) == 1 and not return_dict_always:\n pkginfo = _get_package_info(names[0], saltenv=saltenv)\n if not pkginfo:\n return ''\n versions = sorted(\n list(pkginfo.keys()),\n key=cmp_to_key(_reverse_cmp_pkg_versions)\n )\n else:\n versions = {}\n for name in names:\n pkginfo = _get_package_info(name, saltenv=saltenv)\n if not pkginfo:\n continue\n verlist = sorted(\n list(pkginfo.keys()) if pkginfo else [],\n key=cmp_to_key(_reverse_cmp_pkg_versions)\n )\n versions[name] = verlist\n return versions"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a string representing the version of the specified packages.", "response": "def version(*names, **kwargs):\n '''\n Returns a string representing the package version or an empty string if not\n installed. If more than one package name is specified, a dict of\n name/version pairs is returned.\n\n Args:\n name (str): One or more package names\n\n Kwargs:\n saltenv (str): The salt environment to use. Default ``base``.\n refresh (bool): Refresh package metadata. Default ``False``.\n\n Returns:\n str: version string when a single package is specified.\n dict: The package name(s) with the installed versions.\n\n .. code-block:: cfg\n\n {['<version>', '<version>', ]} OR\n {'<package name>': ['<version>', '<version>', ]}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.version <package name>\n salt '*' pkg.version <package name01> <package name02>\n\n '''\n # Standard is return empty string even if not a valid name\n # TODO: Look at returning an error across all platforms with\n # CommandExecutionError(msg,info={'errors': errors })\n # available_pkgs = get_repo_data(saltenv).get('repo')\n # for name in names:\n # if name in available_pkgs:\n # ret[name] = installed_pkgs.get(name, '')\n\n saltenv = kwargs.get('saltenv', 'base')\n installed_pkgs = list_pkgs(saltenv=saltenv, refresh=kwargs.get('refresh', False))\n\n if len(names) == 1:\n return installed_pkgs.get(names[0], '')\n\n ret = {}\n for name in names:\n ret[name] = installed_pkgs.get(name, '')\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists the packages currently installed on the master.", "response": "def list_pkgs(versions_as_list=False,\n include_components=True,\n include_updates=True,\n **kwargs):\n '''\n List the packages currently installed.\n\n .. note::\n To view installed software as displayed in the Add/Remove Programs, set\n ``include_components`` and ``include_updates`` to False.\n\n Args:\n\n versions_as_list (bool):\n Returns the versions as a list\n\n include_components (bool):\n Include sub components of installed software. Default is ``True``\n\n include_updates (bool):\n Include software updates and Windows updates. Default is ``True``\n\n Kwargs:\n\n saltenv (str):\n The salt environment to use. Default ``base``\n\n refresh (bool):\n Refresh package metadata. Default ``False``\n\n Returns:\n dict: A dictionary of installed software with versions installed\n\n .. code-block:: cfg\n\n {'<package_name>': '<version>'}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_pkgs\n salt '*' pkg.list_pkgs versions_as_list=True\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 saltenv = kwargs.get('saltenv', 'base')\n refresh = salt.utils.data.is_true(kwargs.get('refresh', False))\n _refresh_db_conditional(saltenv, force=refresh)\n\n ret = {}\n name_map = _get_name_map(saltenv)\n for pkg_name, val_list in six.iteritems(\n _get_reg_software(include_components=include_components,\n include_updates=include_updates)):\n if pkg_name in name_map:\n key = name_map[pkg_name]\n for val in val_list:\n if val == 'Not Found':\n # Look up version from winrepo\n pkg_info = _get_package_info(key, saltenv=saltenv)\n if not pkg_info:\n continue\n for pkg_ver in pkg_info.keys():\n if pkg_info[pkg_ver]['full_name'] == pkg_name:\n val = pkg_ver\n __salt__['pkg_resource.add_pkg'](ret, key, val)\n else:\n key = pkg_name\n for val in val_list:\n __salt__['pkg_resource.add_pkg'](ret, key, val)\n\n __salt__['pkg_resource.sort_pkglist'](ret)\n if not versions_as_list:\n __salt__['pkg_resource.stringify'](ret)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef refresh_db(**kwargs):\n r'''\n Generates the local software metadata database (`winrepo.p`) on the minion.\n The database is stored in a serialized format located by default at the\n following location:\n\n ``C:\\salt\\var\\cache\\salt\\minion\\files\\base\\win\\repo-ng\\winrepo.p``\n\n This module performs the following steps to generate the software metadata\n database:\n\n - Fetch the package definition files (.sls) from `winrepo_source_dir`\n (default `salt://win/repo-ng`) and cache them in\n `<cachedir>\\files\\<saltenv>\\<winrepo_source_dir>`\n (default: ``C:\\salt\\var\\cache\\salt\\minion\\files\\base\\win\\repo-ng``)\n - Call :py:func:`pkg.genrepo <salt.modules.win_pkg.genrepo>` to parse the\n package definition files and generate the repository metadata database\n file (`winrepo.p`)\n - Return the report received from\n :py:func:`pkg.genrepo <salt.modules.win_pkg.genrepo>`\n\n The default winrepo directory on the master is `/srv/salt/win/repo-ng`. All\n files that end with `.sls` in this and all subdirectories will be used to\n generate the repository metadata database (`winrepo.p`).\n\n .. note::\n - Hidden directories (directories beginning with '`.`', such as\n '`.git`') will be ignored.\n\n .. note::\n There is no need to call `pkg.refresh_db` every time you work with the\n pkg module. Automatic refresh will occur based on the following minion\n configuration settings:\n\n - `winrepo_cache_expire_min`\n - `winrepo_cache_expire_max`\n\n However, if the package definition files have changed, as would be the\n case if you are developing a new package definition, this function\n should be called to ensure the minion has the latest information about\n packages available to it.\n\n .. warning::\n Directories and files fetched from <winrepo_source_dir>\n (`/srv/salt/win/repo-ng`) will be processed in alphabetical order. If\n two or more software definition files contain the same name, the last\n one processed replaces all data from the files processed before it.\n\n For more information see\n :ref:`Windows Software Repository <windows-package-manager>`\n\n Arguments:\n\n saltenv (str): Salt environment. Default: ``base``\n\n verbose (bool):\n Return a verbose data structure which includes 'success_list', a\n list of all sls files and the package names contained within.\n Default is 'False'\n\n failhard (bool):\n If ``True``, an error will be raised if any repo SLS files fails to\n process. If ``False``, no error will be raised, and a dictionary\n containing the full results will be returned.\n\n Returns:\n dict: A dictionary containing the results of the database refresh.\n\n .. note::\n A result with a `total: 0` generally means that the files are in the\n wrong location on the master. Try running the following command on the\n minion: `salt-call -l debug pkg.refresh saltenv=base`\n\n .. warning::\n When calling this command from a state using `module.run` be sure to\n pass `failhard: False`. Otherwise the state will report failure if it\n encounters a bad software definition file.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.refresh_db\n salt '*' pkg.refresh_db saltenv=base\n '''\n # Remove rtag file to keep multiple refreshes from happening in pkg states\n salt.utils.pkg.clear_rtag(__opts__)\n saltenv = kwargs.pop('saltenv', 'base')\n verbose = salt.utils.data.is_true(kwargs.pop('verbose', False))\n failhard = salt.utils.data.is_true(kwargs.pop('failhard', True))\n __context__.pop('winrepo.data', None)\n repo_details = _get_repo_details(saltenv)\n\n log.debug(\n 'Refreshing pkg metadata db for saltenv \\'%s\\' (age of existing '\n 'metadata is %s)',\n saltenv, datetime.timedelta(seconds=repo_details.winrepo_age)\n )\n\n # Clear minion repo-ng cache see #35342 discussion\n log.info('Removing all *.sls files under \\'%s\\'', repo_details.local_dest)\n failed = []\n for root, _, files in salt.utils.path.os_walk(repo_details.local_dest, followlinks=False):\n for name in files:\n if name.endswith('.sls'):\n full_filename = os.path.join(root, name)\n try:\n os.remove(full_filename)\n except OSError as exc:\n if exc.errno != errno.ENOENT:\n log.error('Failed to remove %s: %s', full_filename, exc)\n failed.append(full_filename)\n if failed:\n raise CommandExecutionError(\n 'Failed to clear one or more winrepo cache files',\n info={'failed': failed}\n )\n\n # Cache repo-ng locally\n log.info('Fetching *.sls files from %s', repo_details.winrepo_source_dir)\n __salt__['cp.cache_dir'](\n path=repo_details.winrepo_source_dir,\n saltenv=saltenv,\n include_pat='*.sls',\n exclude_pat=r'E@\\/\\..*?\\/' # Exclude all hidden directories (.git)\n )\n return genrepo(saltenv=saltenv, verbose=verbose, failhard=failhard)", "response": "r A function that generates the local software metadata database on the master and returns the updated version of the database."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_repo_details(saltenv):\n '''\n Return repo details for the specified saltenv as a namedtuple\n '''\n contextkey = 'winrepo._get_repo_details.{0}'.format(saltenv)\n\n if contextkey in __context__:\n (winrepo_source_dir, local_dest, winrepo_file) = __context__[contextkey]\n else:\n winrepo_source_dir = __opts__['winrepo_source_dir']\n dirs = [__opts__['cachedir'], 'files', saltenv]\n url_parts = _urlparse(winrepo_source_dir)\n dirs.append(url_parts.netloc)\n dirs.extend(url_parts.path.strip('/').split('/'))\n local_dest = os.sep.join(dirs)\n\n winrepo_file = os.path.join(local_dest, 'winrepo.p') # Default\n # Check for a valid windows file name\n if not re.search(r'[\\/:*?\"<>|]',\n __opts__['winrepo_cachefile'],\n flags=re.IGNORECASE):\n winrepo_file = os.path.join(\n local_dest,\n __opts__['winrepo_cachefile']\n )\n else:\n log.error(\n 'minion configuration option \\'winrepo_cachefile\\' has been '\n 'ignored as its value (%s) is invalid. Please ensure this '\n 'option is set to a valid filename.',\n __opts__['winrepo_cachefile']\n )\n\n # Do some safety checks on the repo_path as its contents can be removed,\n # this includes check for bad coding\n system_root = os.environ.get('SystemRoot', r'C:\\Windows')\n if not salt.utils.path.safe_path(\n path=local_dest,\n allow_path='\\\\'.join([system_root, 'TEMP'])):\n\n raise CommandExecutionError(\n 'Attempting to delete files from a possibly unsafe location: '\n '{0}'.format(local_dest)\n )\n\n __context__[contextkey] = (winrepo_source_dir, local_dest, winrepo_file)\n\n try:\n os.makedirs(local_dest)\n except OSError as exc:\n if exc.errno != errno.EEXIST:\n raise CommandExecutionError(\n 'Failed to create {0}: {1}'.format(local_dest, exc)\n )\n\n winrepo_age = -1\n try:\n stat_result = os.stat(winrepo_file)\n mtime = stat_result.st_mtime\n winrepo_age = time.time() - mtime\n except OSError as exc:\n if exc.errno != errno.ENOENT:\n raise CommandExecutionError(\n 'Failed to get age of {0}: {1}'.format(winrepo_file, exc)\n )\n except AttributeError:\n # Shouldn't happen but log if it does\n log.warning('st_mtime missing from stat result %s', stat_result)\n except TypeError:\n # Shouldn't happen but log if it does\n log.warning('mtime of %s (%s) is an invalid type', winrepo_file, mtime)\n\n repo_details = collections.namedtuple(\n 'RepoDetails',\n ('winrepo_source_dir', 'local_dest', 'winrepo_file', 'winrepo_age')\n )\n return repo_details(winrepo_source_dir, local_dest, winrepo_file, winrepo_age)", "response": "Return the repo details for the specified saltenv as a namedtuple"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef genrepo(**kwargs):\n '''\n Generate package metadata db based on files within the winrepo_source_dir\n\n Kwargs:\n\n saltenv (str): Salt environment. Default: ``base``\n\n verbose (bool):\n Return verbose data structure which includes 'success_list', a list\n of all sls files and the package names contained within.\n Default ``False``.\n\n failhard (bool):\n If ``True``, an error will be raised if any repo SLS files failed\n to process. If ``False``, no error will be raised, and a dictionary\n containing the full results will be returned.\n\n .. note::\n - Hidden directories (directories beginning with '`.`', such as\n '`.git`') will be ignored.\n\n Returns:\n dict: A dictionary of the results of the command\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run pkg.genrepo\n salt -G 'os:windows' pkg.genrepo verbose=true failhard=false\n salt -G 'os:windows' pkg.genrepo saltenv=base\n '''\n saltenv = kwargs.pop('saltenv', 'base')\n verbose = salt.utils.data.is_true(kwargs.pop('verbose', False))\n failhard = salt.utils.data.is_true(kwargs.pop('failhard', True))\n\n ret = {}\n successful_verbose = {}\n total_files_processed = 0\n ret['repo'] = {}\n ret['errors'] = {}\n repo_details = _get_repo_details(saltenv)\n\n for root, _, files in salt.utils.path.os_walk(repo_details.local_dest, followlinks=False):\n\n # Skip hidden directories (.git)\n if re.search(r'[\\\\/]\\..*', root):\n log.debug('Skipping files in directory: %s', root)\n continue\n\n short_path = os.path.relpath(root, repo_details.local_dest)\n if short_path == '.':\n short_path = ''\n\n for name in files:\n if name.endswith('.sls'):\n total_files_processed += 1\n _repo_process_pkg_sls(\n os.path.join(root, name),\n os.path.join(short_path, name),\n ret,\n successful_verbose\n )\n serial = salt.payload.Serial(__opts__)\n\n with salt.utils.files.fopen(repo_details.winrepo_file, 'wb') as repo_cache:\n repo_cache.write(serial.dumps(ret))\n # For some reason we can not save ret into __context__['winrepo.data'] as this breaks due to utf8 issues\n successful_count = len(successful_verbose)\n error_count = len(ret['errors'])\n if verbose:\n results = {\n 'total': total_files_processed,\n 'success': successful_count,\n 'failed': error_count,\n 'success_list': successful_verbose,\n 'failed_list': ret['errors']\n }\n else:\n if error_count > 0:\n results = {\n 'total': total_files_processed,\n 'success': successful_count,\n 'failed': error_count,\n 'failed_list': ret['errors']\n }\n else:\n results = {\n 'total': total_files_processed,\n 'success': successful_count,\n 'failed': error_count\n }\n\n if error_count > 0 and failhard:\n raise CommandExecutionError(\n 'Error occurred while generating repo db',\n info=results\n )\n else:\n return results", "response": "Generate package metadata db based on files within the winrepo_source_dir"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nextracting the hash sum whether it is in a remote hash file or just a string.", "response": "def _get_source_sum(source_hash, file_path, saltenv):\n '''\n Extract the hash sum, whether it is in a remote hash file, or just a string.\n '''\n ret = dict()\n schemes = ('salt', 'http', 'https', 'ftp', 'swift', 's3', 'file')\n invalid_hash_msg = (\"Source hash '{0}' format is invalid. It must be in \"\n \"the format <hash type>=<hash>\").format(source_hash)\n source_hash = six.text_type(source_hash)\n source_hash_scheme = _urlparse(source_hash).scheme\n\n if source_hash_scheme in schemes:\n # The source_hash is a file on a server\n cached_hash_file = __salt__['cp.cache_file'](source_hash, saltenv)\n\n if not cached_hash_file:\n raise CommandExecutionError(('Source hash file {0} not'\n ' found').format(source_hash))\n\n ret = __salt__['file.extract_hash'](cached_hash_file, '', file_path)\n if ret is None:\n raise SaltInvocationError(invalid_hash_msg)\n else:\n # The source_hash is a hash string\n items = source_hash.split('=', 1)\n\n if len(items) != 2:\n invalid_hash_msg = ('{0}, or it must be a supported protocol'\n ': {1}').format(invalid_hash_msg,\n ', '.join(schemes))\n raise SaltInvocationError(invalid_hash_msg)\n\n ret['hash_type'], ret['hsum'] = [item.strip().lower() for item in items]\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_msiexec(use_msiexec):\n '''\n Return if msiexec.exe will be used and the command to invoke it.\n '''\n if use_msiexec is False:\n return False, ''\n if isinstance(use_msiexec, six.string_types):\n if os.path.isfile(use_msiexec):\n return True, use_msiexec\n else:\n log.warning(\n \"msiexec path '%s' not found. Using system registered \"\n \"msiexec instead\", use_msiexec\n )\n use_msiexec = True\n if use_msiexec is True:\n return True, 'msiexec'", "response": "Return if msiexec. exe will be used and the command to invoke it."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef upgrade(**kwargs):\n '''\n Upgrade all software. Currently not implemented\n\n Kwargs:\n saltenv (str): The salt environment to use. Default ``base``.\n refresh (bool): Refresh package metadata. Default ``True``.\n\n .. note::\n This feature is not yet implemented for Windows.\n\n Returns:\n dict: Empty dict, until implemented\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade\n '''\n log.warning('pkg.upgrade not implemented on Windows yet')\n refresh = salt.utils.data.is_true(kwargs.get('refresh', True))\n saltenv = kwargs.get('saltenv', 'base')\n log.warning('pkg.upgrade not implemented on Windows yet refresh:%s saltenv:%s', refresh, saltenv)\n # Uncomment the below once pkg.upgrade has been implemented\n\n # if salt.utils.data.is_true(refresh):\n # refresh_db()\n return {}", "response": "Upgrade all software. Currently not implemented on Windows yet."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nremove the passed package from the specified package list or all packages in the system", "response": "def remove(name=None, pkgs=None, **kwargs):\n '''\n Remove the passed package(s) from the system using winrepo\n\n .. versionadded:: 0.16.0\n\n Args:\n name (str):\n The name(s) of the package(s) to be uninstalled. Can be a\n single package or a comma delimited list of packages, no spaces.\n\n pkgs (list):\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 Kwargs:\n\n version (str):\n The version of the package to be uninstalled. If this option is\n used to to uninstall multiple packages, then this version will be\n applied to all targeted packages. Recommended using only when\n uninstalling a single package. If this parameter is omitted, the\n latest version will be uninstalled.\n\n saltenv (str): Salt environment. Default ``base``\n refresh (bool): Refresh package metadata. Default ``False``\n\n Returns:\n dict: Returns a dict containing the changes.\n\n If the package is removed by ``pkg.remove``:\n\n {'<package>': {'old': '<old-version>',\n 'new': '<new-version>'}}\n\n If the package is already uninstalled:\n\n {'<package>': {'current': 'not installed'}}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.remove <package name>\n salt '*' pkg.remove <package1>,<package2>,<package3>\n salt '*' pkg.remove pkgs='[\"foo\", \"bar\"]'\n '''\n saltenv = kwargs.get('saltenv', 'base')\n refresh = salt.utils.data.is_true(kwargs.get('refresh', False))\n # no need to call _refresh_db_conditional as list_pkgs will do it\n ret = {}\n\n # Make sure name or pkgs is passed\n if not name and not pkgs:\n return 'Must pass a single package or a list of packages'\n\n # Get package parameters\n pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs, **kwargs)[0]\n\n # Get a list of currently installed software for comparison at the end\n old = list_pkgs(saltenv=saltenv, refresh=refresh, versions_as_list=True)\n\n # Loop through each package\n changed = [] # list of changed package names\n for pkgname, version_num in six.iteritems(pkg_params):\n\n # Load package information for the package\n pkginfo = _get_package_info(pkgname, saltenv=saltenv)\n\n # Make sure pkginfo was found\n if not pkginfo:\n msg = 'Unable to locate package {0}'.format(pkgname)\n log.error(msg)\n ret[pkgname] = msg\n continue\n\n # Check to see if package is installed on the system\n if pkgname not in old:\n log.debug('%s %s not installed', pkgname, version_num if version_num else '')\n ret[pkgname] = {'current': 'not installed'}\n continue\n\n removal_targets = []\n # Only support a single version number\n if version_num is not None:\n # Using the salt cmdline with version=5.3 might be interpreted\n # as a float it must be converted to a string in order for\n # string matching to work.\n version_num = six.text_type(version_num)\n\n # At least one version of the software is installed.\n if version_num is None:\n for ver_install in old[pkgname]:\n if ver_install not in pkginfo and 'latest' in pkginfo:\n log.debug('%s %s using package latest entry to to remove', pkgname, version_num)\n removal_targets.append('latest')\n else:\n removal_targets.append(ver_install)\n else:\n if version_num in pkginfo:\n # we known how to remove this version\n if version_num in old[pkgname]:\n removal_targets.append(version_num)\n else:\n log.debug('%s %s not installed', pkgname, version_num)\n ret[pkgname] = {'current': '{0} not installed'.format(version_num)}\n continue\n elif 'latest' in pkginfo:\n # we do not have version entry, assume software can self upgrade and use latest\n log.debug('%s %s using package latest entry to to remove', pkgname, version_num)\n removal_targets.append('latest')\n\n if not removal_targets:\n log.error('%s %s no definition to remove this version', pkgname, version_num)\n ret[pkgname] = {\n 'current': '{0} no definition, cannot removed'.format(version_num)\n }\n continue\n\n for target in removal_targets:\n # Get the uninstaller\n uninstaller = pkginfo[target].get('uninstaller', '')\n cache_dir = pkginfo[target].get('cache_dir', False)\n uninstall_flags = pkginfo[target].get('uninstall_flags', '')\n\n # If no uninstaller found, use the installer with uninstall flags\n if not uninstaller and uninstall_flags:\n uninstaller = pkginfo[target].get('installer', '')\n\n # If still no uninstaller found, fail\n if not uninstaller:\n log.error(\n 'No installer or uninstaller configured for package %s',\n pkgname,\n )\n ret[pkgname] = {'no uninstaller defined': target}\n continue\n\n # Where is the uninstaller\n if uninstaller.startswith(('salt:', 'http:', 'https:', 'ftp:')):\n\n # Check for the 'cache_dir' parameter in the .sls file\n # If true, the entire directory will be cached instead of the\n # individual file. This is useful for installations that are not\n # single files\n\n if cache_dir and uninstaller.startswith('salt:'):\n path, _ = os.path.split(uninstaller)\n __salt__['cp.cache_dir'](path,\n saltenv,\n False,\n None,\n 'E@init.sls$')\n\n # Check to see if the uninstaller is cached\n cached_pkg = __salt__['cp.is_cached'](uninstaller, saltenv)\n if not cached_pkg:\n # It's not cached. Cache it, mate.\n cached_pkg = __salt__['cp.cache_file'](uninstaller, saltenv)\n\n # Check if the uninstaller was cached successfully\n if not cached_pkg:\n log.error('Unable to cache %s', uninstaller)\n ret[pkgname] = {'unable to cache': uninstaller}\n continue\n\n # Compare the hash of the cached installer to the source only if\n # the file is hosted on salt:\n # TODO cp.cache_file does cache and hash checking? So why do it again?\n if uninstaller.startswith('salt:'):\n if __salt__['cp.hash_file'](uninstaller, saltenv) != \\\n __salt__['cp.hash_file'](cached_pkg):\n try:\n cached_pkg = __salt__['cp.cache_file'](\n uninstaller, saltenv)\n except MinionError as exc:\n return '{0}: {1}'.format(exc, uninstaller)\n\n # Check if the installer was cached successfully\n if not cached_pkg:\n log.error('Unable to cache %s', uninstaller)\n ret[pkgname] = {'unable to cache': uninstaller}\n continue\n else:\n # Run the uninstaller directly\n # (not hosted on salt:, https:, etc.)\n cached_pkg = os.path.expandvars(uninstaller)\n\n # Fix non-windows slashes\n cached_pkg = cached_pkg.replace('/', '\\\\')\n cache_path, _ = os.path.split(cached_pkg)\n\n # os.path.expandvars is not required as we run everything through cmd.exe /s /c\n\n if kwargs.get('extra_uninstall_flags'):\n uninstall_flags = '{0} {1}'.format(\n uninstall_flags, kwargs.get('extra_uninstall_flags', ''))\n\n # Compute msiexec string\n use_msiexec, msiexec = _get_msiexec(pkginfo[target].get('msiexec', False))\n cmd_shell = os.getenv('ComSpec', '{0}\\\\system32\\\\cmd.exe'.format(os.getenv('WINDIR')))\n\n # Build cmd and arguments\n # cmd and arguments must be separated for use with the task scheduler\n if use_msiexec:\n # Check if uninstaller is set to {guid}, if not we assume its a remote msi file.\n # which has already been downloaded.\n arguments = '\"{0}\" /X \"{1}\"'.format(msiexec, cached_pkg)\n else:\n arguments = '\"{0}\"'.format(cached_pkg)\n\n if uninstall_flags:\n arguments = '{0} {1}'.format(arguments, uninstall_flags)\n\n # Uninstall the software\n changed.append(pkgname)\n # Check Use Scheduler Option\n if pkginfo[target].get('use_scheduler', False):\n # Create Scheduled Task\n __salt__['task.create_task'](name='update-salt-software',\n user_name='System',\n force=True,\n action_type='Execute',\n cmd=cmd_shell,\n arguments='/s /c \"{0}\"'.format(arguments),\n start_in=cache_path,\n trigger_type='Once',\n start_date='1975-01-01',\n start_time='01:00',\n ac_only=False,\n stop_if_on_batteries=False)\n # Run Scheduled Task\n if not __salt__['task.run_wait'](name='update-salt-software'):\n log.error('Failed to remove %s', pkgname)\n log.error('Scheduled Task failed to run')\n ret[pkgname] = {'uninstall status': 'failed'}\n else:\n # Launch the command\n result = __salt__['cmd.run_all'](\n '\"{0}\" /s /c \"{1}\"'.format(cmd_shell, arguments),\n output_loglevel='trace',\n python_shell=False,\n redirect_stderr=True)\n if not result['retcode']:\n ret[pkgname] = {'uninstall status': 'success'}\n changed.append(pkgname)\n elif result['retcode'] == 3010:\n # 3010 is ERROR_SUCCESS_REBOOT_REQUIRED\n report_reboot_exit_codes = kwargs.pop(\n 'report_reboot_exit_codes', True)\n if report_reboot_exit_codes:\n __salt__['system.set_reboot_required_witnessed']()\n ret[pkgname] = {'uninstall status': 'success, reboot required'}\n changed.append(pkgname)\n elif result['retcode'] == 1641:\n # 1641 is ERROR_SUCCESS_REBOOT_INITIATED\n ret[pkgname] = {'uninstall status': 'success, reboot initiated'}\n changed.append(pkgname)\n else:\n log.error('Failed to remove %s', pkgname)\n log.error('retcode %s', result['retcode'])\n log.error('uninstaller output: %s', result['stdout'])\n ret[pkgname] = {'uninstall status': 'failed'}\n\n # Get a new list of installed software\n new = list_pkgs(saltenv=saltenv, refresh=False)\n\n # Take the \"old\" package list and convert the values to strings in\n # preparation for the comparison below.\n __salt__['pkg_resource.stringify'](old)\n\n # Check for changes in the registry\n difference = salt.utils.data.compare_dicts(old, new)\n found_chgs = all(name in difference for name in changed)\n end_t = time.time() + 3 # give it 3 seconds to catch up.\n while not found_chgs and time.time() < end_t:\n time.sleep(0.5)\n new = list_pkgs(saltenv=saltenv, refresh=False)\n difference = salt.utils.data.compare_dicts(old, new)\n found_chgs = all(name in difference for name in changed)\n\n if not found_chgs:\n log.warning('Expected changes for package removal may not have occured')\n\n # Compare the software list before and after\n # Add the difference to ret\n ret.update(difference)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_repo_data(saltenv='base'):\n '''\n Returns the existing package metadata db. Will create it, if it does not\n exist, however will not refresh it.\n\n Args:\n saltenv (str): Salt environment. Default ``base``\n\n Returns:\n dict: A dict containing contents of metadata db.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.get_repo_data\n '''\n # we only call refresh_db if it does not exist, as we want to return\n # the existing data even if its old, other parts of the code call this,\n # but they will call refresh if they need too.\n repo_details = _get_repo_details(saltenv)\n\n if repo_details.winrepo_age == -1:\n # no repo meta db\n log.debug('No winrepo.p cache file. Refresh pkg db now.')\n refresh_db(saltenv=saltenv)\n\n if 'winrepo.data' in __context__:\n log.trace('get_repo_data returning results from __context__')\n return __context__['winrepo.data']\n else:\n log.trace('get_repo_data called reading from disk')\n\n try:\n serial = salt.payload.Serial(__opts__)\n with salt.utils.files.fopen(repo_details.winrepo_file, 'rb') as repofile:\n try:\n repodata = salt.utils.data.decode(serial.loads(repofile.read()) or {})\n __context__['winrepo.data'] = repodata\n return repodata\n except Exception as exc:\n log.exception(exc)\n return {}\n except IOError as exc:\n log.error('Not able to read repo file')\n log.exception(exc)\n return {}", "response": "Returns the existing package metadata db. Will create it if it does not exist. Will refresh it if it does not exist."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a reverse map of full pkg names to the names recognized by winrepo.", "response": "def _get_name_map(saltenv='base'):\n '''\n Return a reverse map of full pkg names to the names recognized by winrepo.\n '''\n u_name_map = {}\n name_map = get_repo_data(saltenv).get('name_map', {})\n\n if not six.PY2:\n return name_map\n\n for k in name_map:\n u_name_map[k] = name_map[k]\n return u_name_map"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef compare_versions(ver1='', oper='==', ver2=''):\n '''\n Compare software package versions\n\n Args:\n ver1 (str): A software version to compare\n oper (str): The operand to use to compare\n ver2 (str): A software version to compare\n\n Returns:\n bool: True if the comparison is valid, otherwise False\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.compare_versions 1.2 >= 1.3\n '''\n if not ver1:\n raise SaltInvocationError('compare_version, ver1 is blank')\n if not ver2:\n raise SaltInvocationError('compare_version, ver2 is blank')\n\n # Support version being the special meaning of 'latest'\n if ver1 == 'latest':\n ver1 = six.text_type(sys.maxsize)\n if ver2 == 'latest':\n ver2 = six.text_type(sys.maxsize)\n # Support version being the special meaning of 'Not Found'\n if ver1 == 'Not Found':\n ver1 = '0.0.0.0.0'\n if ver2 == 'Not Found':\n ver2 = '0.0.0.0.0'\n\n return salt.utils.versions.compare(ver1, oper, ver2, ignore_epoch=True)", "response": "Compare two software package versions"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nread the contents of the file and returns the object.", "response": "def _read(self, fp, fpname):\n '''\n Makes the following changes from the RawConfigParser:\n\n 1. Strip leading tabs from non-section-header lines.\n 2. Treat 8 spaces at the beginning of a line as a tab.\n 3. Treat lines beginning with a tab as options.\n 4. Drops support for continuation lines.\n 5. Multiple values for a given option are stored as a list.\n 6. Keys and values are decoded to the system encoding.\n '''\n cursect = None # None, or a dictionary\n optname = None\n lineno = 0\n e = None # None, or an exception\n while True:\n line = salt.utils.stringutils.to_unicode(fp.readline())\n if not line:\n break\n lineno = lineno + 1\n # comment or blank line?\n if line.strip() == '' or line[0] in '#;':\n continue\n if line.split(None, 1)[0].lower() == 'rem' and line[0] in 'rR':\n # no leading whitespace\n continue\n # Replace space indentation with a tab. Allows parser to work\n # properly in cases where someone has edited the git config by hand\n # and indented using spaces instead of tabs.\n if line.startswith(self.SPACEINDENT):\n line = '\\t' + line[len(self.SPACEINDENT):]\n # is it a section header?\n mo = self.SECTCRE.match(line)\n if mo:\n sectname = mo.group('header')\n if sectname in self._sections:\n cursect = self._sections[sectname]\n elif sectname == self.DEFAULTSECT:\n cursect = self._defaults\n else:\n cursect = self._dict()\n self._sections[sectname] = cursect\n # So sections can't start with a continuation line\n optname = None\n # no section header in the file?\n elif cursect is None:\n raise MissingSectionHeaderError( # pylint: disable=undefined-variable\n salt.utils.stringutils.to_str(fpname),\n lineno,\n salt.utils.stringutils.to_str(line))\n # an option line?\n else:\n mo = self._optcre.match(line.lstrip())\n if mo:\n optname, vi, optval = mo.group('option', 'vi', 'value')\n optname = self.optionxform(optname.rstrip())\n if optval is None:\n optval = ''\n if optval:\n if vi in ('=', ':') and ';' in optval:\n # ';' is a comment delimiter only if it follows\n # a spacing character\n pos = optval.find(';')\n if pos != -1 and optval[pos-1].isspace():\n optval = optval[:pos]\n optval = optval.strip()\n # Empty strings should be considered as blank strings\n if optval in ('\"\"', \"''\"):\n optval = ''\n self._add_option(cursect, optname, optval)\n else:\n # a non-fatal parsing error occurred. set up the\n # exception but keep going. the exception will be\n # raised at the end of the file and will contain a\n # list of all bogus lines\n if not e:\n e = ParsingError(fpname) # pylint: disable=undefined-variable\n e.append(lineno, repr(line))\n # if any parsing errors occurred, raise an exception\n if e:\n raise e"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _string_check(self, value, allow_list=False):\n '''\n Based on the string-checking code from the SafeConfigParser's set()\n function, this enforces string values for config options.\n '''\n if self._optcre is self.OPTCRE or value:\n is_list = isinstance(value, list)\n if is_list and not allow_list:\n raise TypeError('option value cannot be a list unless allow_list is True')\n elif not is_list:\n value = [value]\n if not all(isinstance(x, six.string_types) for x in value):\n raise TypeError('option values must be strings')", "response": "Checks that the option value is a string."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get(self, section, option, as_list=False):\n '''\n Adds an optional \"as_list\" argument to ensure a list is returned. This\n is helpful when iterating over an option which may or may not be a\n multivar.\n '''\n ret = super(GitConfigParser, self).get(section, option)\n if as_list and not isinstance(ret, list):\n ret = [ret]\n return ret", "response": "Returns the value of a\n option."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\noverriding the default set method to change the default value for the option.", "response": "def set(self, section, option, value=''):\n '''\n This is overridden from the RawConfigParser merely to change the\n default value for the 'value' argument.\n '''\n self._string_check(value)\n super(GitConfigParser, self).set(section, option, value)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nremoving an option with a value matching the expression. Works on single - level items and multivars.", "response": "def remove_option_regexp(self, section, option, expr):\n '''\n Remove an option with a value matching the expression. Works on single\n values and multivars.\n '''\n if not section or section == self.DEFAULTSECT:\n sectdict = self._defaults\n else:\n try:\n sectdict = self._sections[section]\n except KeyError:\n raise NoSectionError( # pylint: disable=undefined-variable\n salt.utils.stringutils.to_str(section))\n option = self.optionxform(option)\n if option not in sectdict:\n return False\n regexp = re.compile(expr)\n if isinstance(sectdict[option], list):\n new_list = [x for x in sectdict[option] if not regexp.search(x)]\n # Revert back to a list if we removed all but one item\n if len(new_list) == 1:\n new_list = new_list[0]\n existed = new_list != sectdict[option]\n if existed:\n del sectdict[option]\n sectdict[option] = new_list\n del new_list\n else:\n existed = bool(regexp.search(sectdict[option]))\n if existed:\n del sectdict[option]\n return existed"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nwriting the contents of the raw config file to the file - like object fp_.", "response": "def write(self, fp_):\n '''\n Makes the following changes from the RawConfigParser:\n\n 1. Prepends options with a tab character.\n 2. Does not write a blank line between sections.\n 3. When an option's value is a list, a line for each option is written.\n This allows us to support multivars like a remote's \"fetch\" option.\n 4. Drops support for continuation lines.\n '''\n convert = salt.utils.stringutils.to_bytes \\\n if 'b' in fp_.mode \\\n else salt.utils.stringutils.to_str\n if self._defaults:\n fp_.write(convert('[%s]\\n' % self.DEFAULTSECT))\n for (key, value) in six.iteritems(self._defaults):\n value = salt.utils.stringutils.to_unicode(value).replace('\\n', '\\n\\t')\n fp_.write(convert('%s = %s\\n' % (key, value)))\n for section in self._sections:\n fp_.write(convert('[%s]\\n' % section))\n for (key, value) in six.iteritems(self._sections[section]):\n if (value is not None) or (self._optcre == self.OPTCRE):\n if not isinstance(value, list):\n value = [value]\n for item in value:\n fp_.write(convert('\\t%s\\n' % ' = '.join((key, item)).rstrip()))"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreads a single pillar file into a Pillar object", "response": "def ext_pillar(\n mid,\n pillar,\n path,\n idkey='id',\n namespace=None,\n fieldnames=None,\n restkey=None,\n restval=None,\n dialect='excel'):\n '''\n Read a CSV into Pillar\n\n :param str path: Absolute path to a CSV file.\n :param str idkey: (Optional) The column name of minion IDs.\n :param str namespace: (Optional) A pillar key to namespace the values under.\n :param list fieldnames: (Optional) if the first row of the CSV is not\n column names they may be specified here instead.\n '''\n with salt.utils.files.fopen(path, 'rb') as f:\n sheet = csv.DictReader(f, fieldnames,\n restkey=restkey, restval=restval, dialect=dialect)\n\n for row in sheet:\n if row[idkey] == mid:\n if namespace:\n return {namespace: row}\n else:\n return row\n\n return {}"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef set_(filename, section, parameter, value):\n '''\n Set a value in an OpenStack configuration file.\n\n filename\n The full path to the configuration file\n\n section\n The section in which the parameter will be set\n\n parameter\n The parameter to change\n\n value\n The value to set\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call openstack_config.set /etc/keystone/keystone.conf sql connection foo\n '''\n\n filename = _quote(filename)\n section = _quote(section)\n parameter = _quote(parameter)\n value = _quote(six.text_type(value))\n\n result = __salt__['cmd.run_all'](\n 'openstack-config --set {0} {1} {2} {3}'.format(\n filename, section, parameter, value\n ),\n python_shell=False,\n )\n\n if result['retcode'] == 0:\n return result['stdout']\n else:\n raise salt.exceptions.CommandExecutionError(result['stderr'])", "response": "Set a value in an OpenStack configuration file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get(filename, section, parameter):\n '''\n Get a value from an OpenStack configuration file.\n\n filename\n The full path to the configuration file\n\n section\n The section from which to search for the parameter\n\n parameter\n The parameter to return\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-call openstack_config.get /etc/keystone/keystone.conf sql connection\n\n '''\n\n filename = _quote(filename)\n section = _quote(section)\n parameter = _quote(parameter)\n\n result = __salt__['cmd.run_all'](\n 'openstack-config --get {0} {1} {2}'.format(\n filename, section, parameter\n ),\n python_shell=False,\n )\n\n if result['retcode'] == 0:\n return result['stdout']\n else:\n raise salt.exceptions.CommandExecutionError(result['stderr'])", "response": "Get a value from an OpenStack configuration file."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nensuring an ACE is present in the ACEs folder.", "response": "def present(name, objectType, user, permission, acetype, propagation):\n '''\n Ensure an ACE is present\n '''\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n tRet = __salt__['win_dacl.check_ace'](name, objectType, user, permission, acetype, propagation, True)\n if tRet['result']:\n if not tRet['Exists']:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'The ACE is set to be added.'\n ret['changes']['Added ACEs'] = ((\n '{0} {1} {2} on {3}'\n ).format(user, acetype, permission, propagation))\n return ret\n addRet = __salt__['win_dacl.add_ace'](name, objectType, user, permission, acetype, propagation)\n if addRet['result']:\n ret['result'] = True\n ret['changes'] = dict(ret['changes'], **addRet['changes'])\n else:\n ret['result'] = False\n ret['comment'] = ' '.join([ret['comment'], addRet['comment']])\n else:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'The ACE is present.'\n else:\n ret['result'] = False\n ret['comment'] = tRet['comment']\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef inherit(name, objectType, clear_existing_acl=False):\n '''\n Ensure an object is inheriting ACLs from its parent\n '''\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n tRet = __salt__['win_dacl.check_inheritance'](name, objectType)\n if tRet['result']:\n if not tRet['Inheritance']:\n if __opts__['test']:\n ret['result'] = None\n ret['changes']['Inheritance'] = \"Enabled\"\n ret['comment'] = 'Inheritance is set to be enabled.'\n ret['changes']['Existing ACLs'] = (\n 'Are set to be removed' if clear_existing_acl else 'Are set to be kept')\n return ret\n eRet = __salt__['win_dacl.enable_inheritance'](name, objectType, clear_existing_acl)\n if eRet['result']:\n ret['result'] = True\n ret['changes'] = dict(ret['changes'], **eRet['changes'])\n else:\n ret['result'] = False\n ret['comment'] = ' '.join([ret['comment'], eRet['comment']])\n else:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Inheritance is enabled.'\n else:\n ret['result'] = False\n ret['comment'] = tRet['comment']\n return ret", "response": "Ensure an object is inheriting ACLs from its parent\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef disinherit(name, objectType, copy_inherited_acl=True):\n '''\n Ensure an object is not inheriting ACLs from its parent\n '''\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n tRet = __salt__['win_dacl.check_inheritance'](name, objectType)\n if tRet['result']:\n if tRet['Inheritance']:\n if __opts__['test']:\n ret['result'] = None\n ret['changes']['Inheritance'] = \"Disabled\"\n ret['comment'] = 'Inheritance is set to be disabled.'\n ret['changes']['Inherited ACLs'] = (\n 'Are set to be kept' if copy_inherited_acl else 'Are set to be removed')\n return ret\n eRet = __salt__['win_dacl.disable_inheritance'](name, objectType, copy_inherited_acl)\n ret['result'] = eRet['result']\n if eRet['result']:\n ret['changes'] = dict(ret['changes'], **eRet['changes'])\n else:\n ret['comment'] = ' '.join([ret['comment'], eRet['comment']])\n else:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Inheritance is disabled.'\n else:\n ret['result'] = False\n ret['comment'] = tRet['comment']\n return ret", "response": "Ensure an object is not inheriting ACLs from its parent\nubes."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nupdating the progress bar with the return count.", "response": "def output(ret, bar, **kwargs): # pylint: disable=unused-argument\n '''\n Update the progress bar\n '''\n if 'return_count' in ret:\n val = ret['return_count']\n # Avoid to fail if targets are behind a syndic. In this case actual return count will be\n # higher than targeted by MoM itself.\n # TODO: implement a way to get the proper target minions count and remove this workaround.\n # Details are in #44239.\n if val > bar.maxval:\n bar.maxval = val\n bar.update(val)\n return ''"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ninitializes and return a progress bar iter", "response": "def progress_iter(progress):\n '''\n Initialize and return a progress bar iter\n '''\n widgets = [progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.Timer(), ' Returns: [', progressbar.Counter(), '/{0}]'.format(progress['minion_count'])]\n bar = progressbar.ProgressBar(widgets=widgets, maxval=progress['minion_count'])\n bar.start()\n return bar"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nconverts a preseed file to an SLS file", "response": "def mksls(src, dst=None):\n '''\n Convert a preseed file to an SLS file\n '''\n ps_opts = {}\n with salt.utils.files.fopen(src, 'r') as fh_:\n for line in fh_:\n line = salt.utils.stringutils.to_unicode(line)\n if line.startswith('#'):\n continue\n if not line.strip():\n continue\n\n comps = shlex.split(line)\n if comps[0] not in ps_opts.keys():\n ps_opts[comps[0]] = {}\n cmds = comps[1].split('/')\n\n pointer = ps_opts[comps[0]]\n for cmd in cmds:\n pointer = pointer.setdefault(cmd, {})\n\n pointer['type'] = comps[2]\n if len(comps) > 3:\n pointer['argument'] = comps[3]\n\n sls = {}\n\n # Set language\n # ( This looks like it maps to something else )\n sls[ps_opts['d-i']['languagechooser']['language-name-fb']['argument']] = {\n 'locale': ['system']\n }\n\n # Set keyboard\n # ( This looks like it maps to something else )\n sls[ps_opts['d-i']['kbd-chooser']['method']['argument']] = {\n 'keyboard': ['system']\n }\n\n # Set timezone\n timezone = ps_opts['d-i']['time']['zone']['argument']\n sls[timezone] = {'timezone': ['system']}\n if ps_opts['d-i']['tzconfig']['gmt']['argument'] == 'true':\n sls[timezone]['timezone'].append('utc')\n\n # Set network\n if 'netcfg' in ps_opts['d-i'].keys():\n iface = ps_opts['d-i']['netcfg']['choose_interface']['argument']\n sls[iface] = {}\n sls[iface]['enabled'] = True\n if ps_opts['d-i']['netcfg']['confirm_static'] == 'true':\n sls[iface]['proto'] = 'static'\n elif ps_opts['d-i']['netcfg']['disable_dhcp'] == 'false':\n sls[iface]['proto'] = 'dhcp'\n sls[iface]['netmask'] = ps_opts['d-i']['netcfg']['get_netmask']['argument']\n sls[iface]['domain'] = ps_opts['d-i']['netcfg']['get_domain']['argument']\n sls[iface]['gateway'] = ps_opts['d-i']['netcfg']['get_gateway']['argument']\n sls[iface]['hostname'] = ps_opts['d-i']['netcfg']['get_hostname']['argument']\n sls[iface]['ipaddress'] = ps_opts['d-i']['netcfg']['get_ipaddress']['argument']\n sls[iface]['nameservers'] = ps_opts['d-i']['netcfg']['get_nameservers']['argument']\n\n if dst is not None:\n with salt.utils.files.fopen(dst, 'w') as fh_:\n salt.utils.yaml.safe_dump(sls, fh_, default_flow_style=False)\n else:\n return salt.utils.yaml.safe_dump(sls, default_flow_style=False)"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _remove_unicode_encoding(xml_file):\n '''\n attempts to remove the \"encoding='unicode'\" from an xml file\n as lxml does not support that on a windows node currently\n see issue #38100\n '''\n with salt.utils.files.fopen(xml_file, 'rb') as f:\n xml_content = f.read()\n modified_xml = re.sub(r' encoding=[\\'\"]+unicode[\\'\"]+', '', xml_content.decode('utf-16'), count=1)\n xmltree = lxml.etree.parse(six.StringIO(modified_xml))\n return xmltree", "response": "Remove the encoding = unicode from an xml file"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _buildElementNsmap(using_elements):\n '''\n build a namespace map for an ADMX element\n '''\n thisMap = {}\n for e in using_elements:\n thisMap[e.attrib['prefix']] = e.attrib['namespace']\n return thisMap", "response": "build a namespace map for an ADMX element"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_audit_defaults(option=None):\n '''\n Loads audit.csv defaults into a dict in __context__ called\n 'lgpo.audit_defaults'. The dictionary includes fieldnames and all\n configurable policies as keys. The values are used to create/modify the\n ``audit.csv`` file. The first entry is `fieldnames` used to create the\n header for the csv file. The rest of the entries are the audit policy names.\n Sample data follows:\n\n {\n 'fieldnames': ['Machine Name',\n 'Policy Target',\n 'Subcategory',\n 'Subcategory GUID',\n 'Inclusion Setting',\n 'Exclusion Setting',\n 'Setting Value'],\n 'Audit Sensitive Privilege Use': {'Auditpol Name': 'Sensitive Privilege Use',\n 'Exclusion Setting': '',\n 'Inclusion Setting': 'No Auditing',\n 'Machine Name': 'WIN-8FGT3E045SE',\n 'Policy Target': 'System',\n 'Setting Value': '0',\n 'Subcategory': u'Audit Sensitive Privilege Use',\n 'Subcategory GUID': '{0CCE9228-69AE-11D9-BED3-505054503030}'},\n 'Audit Special Logon': {'Auditpol Name': 'Special Logon',\n 'Exclusion Setting': '',\n 'Inclusion Setting': 'No Auditing',\n 'Machine Name': 'WIN-8FGT3E045SE',\n 'Policy Target': 'System',\n 'Setting Value': '0',\n 'Subcategory': u'Audit Special Logon',\n 'Subcategory GUID': '{0CCE921B-69AE-11D9-BED3-505054503030}'},\n 'Audit System Integrity': {'Auditpol Name': 'System Integrity',\n 'Exclusion Setting': '',\n 'Inclusion Setting': 'No Auditing',\n 'Machine Name': 'WIN-8FGT3E045SE',\n 'Policy Target': 'System',\n 'Setting Value': '0',\n 'Subcategory': u'Audit System Integrity',\n 'Subcategory GUID': '{0CCE9212-69AE-11D9-BED3-505054503030}'},\n ...\n }\n\n .. note::\n `Auditpol Name` designates the value to use when setting the value with\n the auditpol command\n\n Args:\n option (str): The item from the dictionary to return. If ``None`` the\n entire dictionary is returned. Default is ``None``\n\n Returns:\n dict: If ``None`` or one of the audit settings is passed\n list: If ``fieldnames`` is passed\n '''\n if 'lgpo.audit_defaults' not in __context__:\n # Get available setting names and GUIDs\n # This is used to get the fieldnames and GUIDs for individual policies\n log.debug('Loading auditpol defaults into __context__')\n dump = __utils__['auditpol.get_auditpol_dump']()\n reader = csv.DictReader(dump)\n audit_defaults = {'fieldnames': reader.fieldnames}\n for row in reader:\n row['Machine Name'] = ''\n row['Auditpol Name'] = row['Subcategory']\n # Special handling for snowflake scenarios where the audit.csv names\n # don't match the auditpol names\n if row['Subcategory'] == 'Central Policy Staging':\n row['Subcategory'] = 'Audit Central Access Policy Staging'\n elif row['Subcategory'] == 'Plug and Play Events':\n row['Subcategory'] = 'Audit PNP Activity'\n elif row['Subcategory'] == 'Token Right Adjusted Events':\n row['Subcategory'] = 'Audit Token Right Adjusted'\n else:\n row['Subcategory'] = 'Audit {0}'.format(row['Subcategory'])\n audit_defaults[row['Subcategory']] = row\n\n __context__['lgpo.audit_defaults'] = audit_defaults\n\n if option:\n return __context__['lgpo.audit_defaults'][option]\n else:\n return __context__['lgpo.audit_defaults']", "response": "Load audit. csv defaults into a dictionary in the context of the module."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _findOptionValueAdvAudit(option):\n '''\n Get the Advanced Auditing policy as configured in\n ``C:\\\\Windows\\\\Security\\\\Audit\\\\audit.csv``\n\n Args:\n option (str): The name of the setting as it appears in audit.csv\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``\n '''\n if 'lgpo.adv_audit_data' not in __context__:\n system_root = os.environ.get('SystemRoot', 'C:\\\\Windows')\n f_audit = os.path.join(system_root, 'security', 'audit', 'audit.csv')\n f_audit_gpo = os.path.join(system_root, 'System32', 'GroupPolicy',\n 'Machine', 'Microsoft', 'Windows NT',\n 'Audit', 'audit.csv')\n\n # Make sure there is an existing audit.csv file on the machine\n if not __salt__['file.file_exists'](f_audit):\n if __salt__['file.file_exists'](f_audit_gpo):\n # If the GPO audit.csv exists, we'll use that one\n __salt__['file.copy'](f_audit_gpo, f_audit)\n else:\n field_names = _get_audit_defaults('fieldnames')\n # If the file doesn't exist anywhere, create it with default\n # fieldnames\n __salt__['file.makedirs'](f_audit)\n __salt__['file.write'](f_audit, ','.join(field_names))\n\n audit_settings = {}\n with salt.utils.files.fopen(f_audit, mode='r') as csv_file:\n reader = csv.DictReader(csv_file)\n\n for row in reader:\n audit_settings.update(\n {row['Subcategory']: row['Setting Value']})\n\n __context__['lgpo.adv_audit_data'] = audit_settings\n\n return __context__['lgpo.adv_audit_data'].get(option, None)", "response": "Find the value of an option as configured in audit. csv and return it as a dict."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nset the Advanced Audit settings in the two. csv files on Windows.", "response": "def _set_audit_file_data(option, value):\n '''\n Helper function that sets the Advanced Audit settings in the two .csv files\n on Windows. Those files are located at:\n C:\\\\Windows\\\\Security\\\\Audit\\\\audit.csv\n C:\\\\Windows\\\\System32\\\\GroupPolicy\\\\Machine\\\\Microsoft\\\\Windows NT\\\\Audit\\\\audit.csv\n\n Args:\n option (str): The name of the option to set\n value (str): The value to set. ['None', '0', '1', '2', '3']\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``\n '''\n # Set up some paths here\n system_root = os.environ.get('SystemRoot', 'C:\\\\Windows')\n f_audit = os.path.join(system_root, 'security', 'audit', 'audit.csv')\n f_audit_gpo = os.path.join(system_root, 'System32', 'GroupPolicy',\n 'Machine', 'Microsoft', 'Windows NT',\n 'Audit', 'audit.csv')\n f_temp = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.csv',\n prefix='audit')\n\n # Lookup dict for \"Inclusion Setting\" field\n auditpol_values = {'None': 'No Auditing',\n '0': 'No Auditing',\n '1': 'Success',\n '2': 'Failure',\n '3': 'Success and Failure'}\n\n try:\n # Open the existing audit.csv and load the csv `reader`\n with salt.utils.files.fopen(f_audit, mode='r') as csv_file:\n reader = csv.DictReader(csv_file)\n\n # Open the temporary .csv and load the csv `writer`\n with salt.utils.files.fopen(f_temp.name, mode='w') as tmp_file:\n writer = csv.DictWriter(tmp_file, fieldnames=reader.fieldnames)\n\n # Write the header values (labels)\n writer.writeheader()\n\n value_written = False\n # Loop through the current audit.csv and write the changes to\n # the temp csv file for existing settings\n for row in reader:\n # If the row matches the value we're setting, update it with\n # the new value\n if row['Subcategory'] == option:\n if not value == 'None':\n # The value is not None, make the change\n row['Inclusion Setting'] = auditpol_values[value]\n row['Setting Value'] = value\n log.debug('LGPO: Setting {0} to {1}'\n ''.format(option, value))\n writer.writerow(row)\n else:\n # value is None, remove it by not writing it to the\n # temp file\n log.debug('LGPO: Removing {0}'.format(option))\n value_written = True\n # If it's not the value we're setting, just write it\n else:\n writer.writerow(row)\n\n # If a value was not written, it is a new setting not found in\n # the existing audit.cvs file. Add the new setting with values\n # from the defaults\n if not value_written:\n if not value == 'None':\n # value is not None, write the new value\n log.debug('LGPO: Setting {0} to {1}'\n ''.format(option, value))\n defaults = _get_audit_defaults(option)\n writer.writerow({\n 'Machine Name': defaults['Machine Name'],\n 'Policy Target': defaults['Policy Target'],\n 'Subcategory': defaults['Subcategory'],\n 'Subcategory GUID': defaults['Subcategory GUID'],\n 'Inclusion Setting': auditpol_values[value],\n 'Exclusion Setting': defaults['Exclusion Setting'],\n 'Setting Value': value})\n value_written = True\n\n if value_written:\n # Copy the temporary csv file over the existing audit.csv in both\n # locations if a value was written\n __salt__['file.copy'](f_temp.name, f_audit, remove_existing=True)\n __salt__['file.makedirs'](f_audit_gpo)\n __salt__['file.copy'](f_temp.name, f_audit_gpo, remove_existing=True)\n finally:\n f_temp.close()\n __salt__['file.remove'](f_temp.name)\n\n return value_written"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _setOptionValueAdvAudit(option, value):\n '''\n Helper function to update the Advanced Audit policy on the machine. This\n function modifies the two ``audit.csv`` files in the following locations:\n\n C:\\\\Windows\\\\Security\\\\Audit\\\\audit.csv\n C:\\\\Windows\\\\System32\\\\GroupPolicy\\\\Machine\\\\Microsoft\\\\Windows NT\\\\Audit\\\\audit.csv\n\n Then it applies those settings using ``auditpol``\n\n After that, it updates ``__context__`` with the new setting\n\n Args:\n option (str): The name of the option to set\n value (str): The value to set. ['None', '0', '1', '2', '3']\n\n Returns:\n bool: ``True`` if successful, otherwise ``False``\n '''\n # Set the values in both audit.csv files\n if not _set_audit_file_data(option=option, value=value):\n raise CommandExecutionError('Failed to set audit.csv option: {0}'\n ''.format(option))\n # Apply the settings locally\n if not _set_auditpol_data(option=option, value=value):\n # Only log this error, it will be in effect the next time the machine\n # updates its policy\n log.debug('Failed to apply audit setting: {0}'.format(option))\n\n # Update __context__\n if value is None:\n log.debug('LGPO: Removing Advanced Audit data: {0}'.format(option))\n __context__['lgpo.adv_audit_data'].pop(option)\n else:\n log.debug('LGPO: Updating Advanced Audit data: {0}: {1}'\n ''.format(option, value))\n __context__['lgpo.adv_audit_data'][option] = value\n\n return True", "response": "Helper function to set the value of an option in the audit. csv file and the locally set the value of the audit. pol file."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _load_secedit_data():\n '''\n Helper function that loads secedit data. It runs `secedit /export /cfg\n <file_name>` which creates a file that contains the secedit data.\n\n Returns:\n str: The contents of the file generated by the secedit command\n '''\n try:\n f_exp = os.path.join(__opts__['cachedir'],\n 'secedit-{0}.txt'.format(UUID))\n __salt__['cmd.run'](['secedit', '/export', '/cfg', f_exp])\n with io.open(f_exp, encoding='utf-16') as fp:\n secedit_data = fp.readlines()\n return secedit_data\n finally:\n if __salt__['file.file_exists'](f_exp):\n __salt__['file.remove'](f_exp)", "response": "Helper function that loads the secedit data. It runs secedit and returns the contents of the file generated by the secedit command\n "} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _write_secedit_data(inf_data):\n '''\n Helper function to write secedit data to the database\n '''\n # Set file names\n f_sdb = os.path.join(__opts__['cachedir'], 'secedit-{0}.sdb'.format(UUID))\n f_inf = os.path.join(__opts__['cachedir'], 'secedit-{0}.inf'.format(UUID))\n\n try:\n # Write the changes to the inf file\n __salt__['file.write'](f_inf, inf_data)\n # Run secedit to make the change\n cmd = ['secedit', '/configure', '/db', f_sdb, '/cfg', f_inf]\n retcode = __salt__['cmd.retcode'](cmd)\n # Success\n if retcode == 0:\n # Pop secedit data so it will always be current\n __context__.pop('lgpo.secedit_data')\n return True\n # Failure\n return False\n finally:\n # Cleanup our scratch files\n if __salt__['file.file_exists'](f_sdb):\n __salt__['file.remove'](f_sdb)\n if __salt__['file.file_exists'](f_inf):\n __salt__['file.remove'](f_inf)", "response": "Helper function to write secedit data to the database"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _delAccountRights(sidObject, user_right):\n '''\n helper function to remove an account right from a user\n '''\n try:\n _polHandle = win32security.LsaOpenPolicy(None, win32security.POLICY_ALL_ACCESS)\n user_rights_list = [user_right]\n _ret = win32security.LsaRemoveAccountRights(_polHandle, sidObject, False, user_rights_list)\n return True\n except Exception as e:\n log.exception('Error attempting to delete account right')\n return False", "response": "helper function to remove an account right from a user\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _getRightsAssignments(user_right):\n '''\n helper function to return all the user rights assignments/users\n '''\n sids = []\n polHandle = win32security.LsaOpenPolicy(None, win32security.POLICY_ALL_ACCESS)\n sids = win32security.LsaEnumerateAccountsWithUserRight(polHandle, user_right)\n return sids", "response": "helper function to return all the user rights assignments"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _getAdmlPresentationRefId(adml_data, ref_id):\n '''\n helper function to check for a presentation label for a policy element\n '''\n search_results = adml_data.xpath('//*[@*[local-name() = \"refId\"] = \"{0}\"]'.format(ref_id))\n prepended_text = ''\n if search_results:\n for result in search_results:\n the_localname = etree.QName(result.tag).localname\n presentation_element = PRESENTATION_ANCESTOR_XPATH(result)\n if presentation_element:\n presentation_element = presentation_element[0]\n if TEXT_ELEMENT_XPATH(presentation_element):\n for p_item in presentation_element.getchildren():\n if p_item == result:\n break\n else:\n if etree.QName(p_item.tag).localname == 'text':\n if prepended_text:\n prepended_text = ' '.join((text for text in (prepended_text, getattr(p_item, 'text', '').rstrip()) if text))\n else:\n prepended_text = getattr(p_item, 'text', '').rstrip()\n else:\n prepended_text = ''\n if prepended_text.endswith('.'):\n prepended_text = ''\n if the_localname == 'textBox' \\\n or the_localname == 'comboBox':\n label_items = result.xpath('.//*[local-name() = \"label\"]')\n for label_item in label_items:\n if label_item.text:\n return (prepended_text + ' ' + label_item.text.rstrip().rstrip(':')).lstrip()\n elif the_localname == 'decimalTextBox' \\\n or the_localname == 'longDecimalTextBox' \\\n or the_localname == 'dropdownList' \\\n or the_localname == 'listBox' \\\n or the_localname == 'checkBox' \\\n or the_localname == 'text' \\\n or the_localname == 'multiTextBox':\n if result.text:\n return (prepended_text + ' ' + result.text.rstrip().rstrip(':')).lstrip()\n return None", "response": "helper function to check for a presentation label for a policy element"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _checkListItem(policy_element, policy_name, policy_key, xpath_object, policy_file_data, test_items=True):\n '''\n helper function to process an enabled/disabled/true/falseList set\n\n if test_items is True, it will determine if the policy is enabled or\n disabled returning True if all items are configured in the registry.pol file\n and false if they are not\n\n if test_items is False, the expected strings for the items will be returned\n as a list\n\n returns True if the enabled/disabledList is 100% configured in the\n registry.pol file, otherwise returns False\n '''\n xpath_string = ('.//*[local-name() = \"decimal\" or local-name() = \"delete\"'\n ' or local-name() = \"longDecimal\" or local-name() = \"string\"]')\n value_item_child_xpath = etree.XPath(xpath_string)\n expected_strings = []\n for list_element in xpath_object(policy_element):\n configured_items = 0\n required_items = 0\n for item in list_element.getchildren():\n required_items = required_items + 1\n if 'key' in item.attrib:\n item_key = item.attrib['key']\n else:\n item_key = policy_key\n if 'valueName' in item.attrib:\n item_valuename = item.attrib['valueName']\n else:\n log.error('%s item with attributes %s in policy %s does not '\n 'have the required \"valueName\" attribute',\n etree.QName(list_element).localname,\n item.attrib, policy_element.attrib)\n break\n for value_item in value_item_child_xpath(item):\n search_string = _processValueItem(value_item,\n item_key,\n item_valuename,\n policy_element,\n item)\n if test_items:\n if _regexSearchRegPolData(re.escape(search_string), policy_file_data):\n configured_items = configured_items + 1\n log.debug('found the search string in the pol file,'\n '%s of %s items for policy %s are '\n 'configured in registry.pol',\n configured_items, required_items,\n policy_name)\n else:\n expected_strings.append(search_string)\n if test_items:\n if required_items > 0 and required_items == configured_items:\n log.debug('%s all items are set', policy_name)\n return True\n if test_items:\n return False\n else:\n return expected_strings", "response": "helper function to process a list element and check if it is enabled or disabled"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _buildKnownDataSearchString(reg_key, reg_valueName, reg_vtype, reg_data,\n check_deleted=False):\n '''\n helper function similar to _processValueItem to build a search string for a\n known key/value/type/data\n '''\n registry = Registry()\n this_element_value = None\n expected_string = b''\n encoded_semicolon = ';'.encode('utf-16-le')\n encoded_null = chr(0).encode('utf-16-le')\n if reg_key:\n reg_key = reg_key.encode('utf-16-le')\n if reg_valueName:\n reg_valueName = reg_valueName.encode('utf-16-le')\n if reg_data and not check_deleted:\n if reg_vtype == 'REG_DWORD':\n this_element_value = struct.pack(b'I', int(reg_data))\n elif reg_vtype == \"REG_QWORD\":\n this_element_value = struct.pack(b'Q', int(reg_data))\n elif reg_vtype == 'REG_SZ':\n this_element_value = b''.join([reg_data.encode('utf-16-le'),\n encoded_null])\n if check_deleted:\n reg_vtype = 'REG_SZ'\n expected_string = b''.join(['['.encode('utf-16-le'),\n reg_key,\n encoded_null,\n encoded_semicolon,\n '**del.'.encode('utf-16-le'),\n reg_valueName,\n encoded_null,\n encoded_semicolon,\n chr(registry.vtype[reg_vtype]).encode('utf-32-le'),\n encoded_semicolon,\n six.unichr(len(' {0}'.format(chr(0)).encode('utf-16-le'))).encode('utf-32-le'),\n encoded_semicolon,\n ' '.encode('utf-16-le'),\n encoded_null,\n ']'.encode('utf-16-le')])\n else:\n expected_string = b''.join(['['.encode('utf-16-le'),\n reg_key,\n encoded_null,\n encoded_semicolon,\n reg_valueName,\n encoded_null,\n encoded_semicolon,\n chr(registry.vtype[reg_vtype]).encode('utf-32-le'),\n encoded_semicolon,\n six.unichr(len(this_element_value)).encode('utf-32-le'),\n encoded_semicolon,\n this_element_value,\n ']'.encode('utf-16-le')])\n return expected_string", "response": "helper function similar to _processValueItem to build a search string for a resource item that is known to be used by the registry."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nrewrites of _getAllAdminTemplateSettingsFromRegPolFile where instead of looking only at the contents of the file, we're going to loop through every policy and look in the registry.pol file to determine if it is enabled/disabled/not configured", "response": "def _checkAllAdmxPolicies(policy_class,\n adml_language='en-US',\n return_full_policy_names=False,\n hierarchical_return=False,\n return_not_configured=False):\n '''\n rewrite of _getAllAdminTemplateSettingsFromRegPolFile where instead of\n looking only at the contents of the file, we're going to loop through every\n policy and look in the registry.pol file to determine if it is\n enabled/disabled/not configured\n '''\n log.debug('POLICY CLASS == %s', policy_class)\n module_policy_data = _policy_info()\n policy_file_data = _read_regpol_file(module_policy_data.admx_registry_classes[policy_class]['policy_path'])\n admx_policies = []\n policy_vals = {}\n hierarchy = {}\n full_names = {}\n admx_policy_definitions = _get_policy_definitions(language=adml_language)\n adml_policy_resources = _get_policy_resources(language=adml_language)\n if policy_file_data:\n log.debug('POLICY CLASS %s has file data', policy_class)\n policy_filedata_split = re.sub(\n salt.utils.stringutils.to_bytes(r'\\]{0}$'.format(chr(0))),\n b'',\n re.sub(salt.utils.stringutils.to_bytes(r'^\\[{0}'.format(chr(0))),\n b'',\n re.sub(re.escape(module_policy_data.reg_pol_header.encode('utf-16-le')),\n b'',\n policy_file_data))).split(']['.encode('utf-16-le'))\n for policy_item in policy_filedata_split:\n policy_item_key = policy_item.split('{0};'.format(chr(0)).encode('utf-16-le'))[0].decode('utf-16-le').lower()\n if policy_item_key:\n for admx_item in REGKEY_XPATH(admx_policy_definitions, keyvalue=policy_item_key):\n if etree.QName(admx_item).localname == 'policy':\n if admx_item not in admx_policies:\n admx_policies.append(admx_item)\n else:\n for policy_item in POLICY_ANCESTOR_XPATH(admx_item):\n if policy_item not in admx_policies:\n admx_policies.append(policy_item)\n\n log.debug('%s policies to examine', len(admx_policies))\n if return_not_configured:\n log.debug('returning non configured policies')\n not_configured_policies = ALL_CLASS_POLICY_XPATH(admx_policy_definitions, registry_class=policy_class)\n for policy_item in admx_policies:\n if policy_item in not_configured_policies:\n not_configured_policies.remove(policy_item)\n\n for not_configured_policy in not_configured_policies:\n not_configured_policy_namespace = not_configured_policy.nsmap[not_configured_policy.prefix]\n if not_configured_policy_namespace not in policy_vals:\n policy_vals[not_configured_policy_namespace] = {}\n policy_vals[not_configured_policy_namespace][not_configured_policy.attrib['name']] = 'Not Configured'\n if return_full_policy_names:\n if not_configured_policy_namespace not in full_names:\n full_names[not_configured_policy_namespace] = {}\n full_names[not_configured_policy_namespace][not_configured_policy.attrib['name']] = _getFullPolicyName(\n policy_item=not_configured_policy,\n policy_name=not_configured_policy.attrib['name'],\n return_full_policy_names=return_full_policy_names,\n adml_language=adml_language)\n log.debug('building hierarchy for non-configured item %s',\n not_configured_policy.attrib['name'])\n if not_configured_policy_namespace not in hierarchy:\n hierarchy[not_configured_policy_namespace] = {}\n hierarchy[not_configured_policy_namespace][not_configured_policy.attrib['name']] = _build_parent_list(\n policy_definition=not_configured_policy,\n return_full_policy_names=return_full_policy_names,\n adml_language=adml_language)\n for admx_policy in admx_policies:\n this_valuename = None\n this_policy_setting = 'Not Configured'\n element_only_enabled_disabled = True\n explicit_enable_disable_value_setting = False\n\n if 'key' in admx_policy.attrib:\n this_key = admx_policy.attrib['key']\n else:\n log.error('policy item %s does not have the required \"key\" '\n 'attribute', admx_policy.attrib)\n break\n if 'valueName' in admx_policy.attrib:\n this_valuename = admx_policy.attrib['valueName']\n if 'name' in admx_policy.attrib:\n this_policyname = admx_policy.attrib['name']\n else:\n log.error('policy item %s does not have the required \"name\" '\n 'attribute', admx_policy.attrib)\n break\n this_policynamespace = admx_policy.nsmap[admx_policy.prefix]\n if ENABLED_VALUE_XPATH(admx_policy) and this_policy_setting == 'Not Configured':\n # some policies have a disabled list but not an enabled list\n # added this to address those issues\n if DISABLED_LIST_XPATH(admx_policy) or DISABLED_VALUE_XPATH(admx_policy):\n element_only_enabled_disabled = False\n explicit_enable_disable_value_setting = True\n if _checkValueItemParent(admx_policy,\n this_policyname,\n this_key,\n this_valuename,\n ENABLED_VALUE_XPATH,\n policy_file_data):\n this_policy_setting = 'Enabled'\n log.debug('%s is enabled by detected ENABLED_VALUE_XPATH', this_policyname)\n if this_policynamespace not in policy_vals:\n policy_vals[this_policynamespace] = {}\n policy_vals[this_policynamespace][this_policyname] = this_policy_setting\n if DISABLED_VALUE_XPATH(admx_policy) and this_policy_setting == 'Not Configured':\n # some policies have a disabled list but not an enabled list\n # added this to address those issues\n if ENABLED_LIST_XPATH(admx_policy) or ENABLED_VALUE_XPATH(admx_policy):\n element_only_enabled_disabled = False\n explicit_enable_disable_value_setting = True\n if _checkValueItemParent(admx_policy,\n this_policyname,\n this_key,\n this_valuename,\n DISABLED_VALUE_XPATH,\n policy_file_data):\n this_policy_setting = 'Disabled'\n log.debug('%s is disabled by detected DISABLED_VALUE_XPATH', this_policyname)\n if this_policynamespace not in policy_vals:\n policy_vals[this_policynamespace] = {}\n policy_vals[this_policynamespace][this_policyname] = this_policy_setting\n if ENABLED_LIST_XPATH(admx_policy) and this_policy_setting == 'Not Configured':\n if DISABLED_LIST_XPATH(admx_policy) or DISABLED_VALUE_XPATH(admx_policy):\n element_only_enabled_disabled = False\n explicit_enable_disable_value_setting = True\n if _checkListItem(admx_policy, this_policyname, this_key, ENABLED_LIST_XPATH, policy_file_data):\n this_policy_setting = 'Enabled'\n log.debug('%s is enabled by detected ENABLED_LIST_XPATH', this_policyname)\n if this_policynamespace not in policy_vals:\n policy_vals[this_policynamespace] = {}\n policy_vals[this_policynamespace][this_policyname] = this_policy_setting\n if DISABLED_LIST_XPATH(admx_policy) and this_policy_setting == 'Not Configured':\n if ENABLED_LIST_XPATH(admx_policy) or ENABLED_VALUE_XPATH(admx_policy):\n element_only_enabled_disabled = False\n explicit_enable_disable_value_setting = True\n if _checkListItem(admx_policy, this_policyname, this_key, DISABLED_LIST_XPATH, policy_file_data):\n this_policy_setting = 'Disabled'\n log.debug('%s is disabled by detected DISABLED_LIST_XPATH', this_policyname)\n if this_policynamespace not in policy_vals:\n policy_vals[this_policynamespace] = {}\n policy_vals[this_policynamespace][this_policyname] = this_policy_setting\n\n if not explicit_enable_disable_value_setting and this_valuename:\n # the policy has a key/valuename but no explicit enabled/Disabled\n # Value or List\n # these seem to default to a REG_DWORD 1 = \"Enabled\" **del. = \"Disabled\"\n if _regexSearchRegPolData(re.escape(_buildKnownDataSearchString(this_key,\n this_valuename,\n 'REG_DWORD',\n '1')),\n policy_file_data):\n this_policy_setting = 'Enabled'\n log.debug('%s is enabled by no explicit enable/disable list or value', this_policyname)\n if this_policynamespace not in policy_vals:\n policy_vals[this_policynamespace] = {}\n policy_vals[this_policynamespace][this_policyname] = this_policy_setting\n elif _regexSearchRegPolData(re.escape(_buildKnownDataSearchString(this_key,\n this_valuename,\n 'REG_DWORD',\n None,\n check_deleted=True)),\n policy_file_data):\n this_policy_setting = 'Disabled'\n log.debug('%s is disabled by no explicit enable/disable list or value', this_policyname)\n if this_policynamespace not in policy_vals:\n policy_vals[this_policynamespace] = {}\n policy_vals[this_policynamespace][this_policyname] = this_policy_setting\n\n if ELEMENTS_XPATH(admx_policy):\n if element_only_enabled_disabled or this_policy_setting == 'Enabled':\n # TODO does this need to be modified based on the 'required' attribute?\n required_elements = {}\n configured_elements = {}\n policy_disabled_elements = 0\n for elements_item in ELEMENTS_XPATH(admx_policy):\n for child_item in elements_item.getchildren():\n this_element_name = _getFullPolicyName(\n policy_item=child_item,\n policy_name=child_item.attrib['id'],\n return_full_policy_names=return_full_policy_names,\n adml_language=adml_language)\n required_elements[this_element_name] = None\n child_key = this_key\n child_valuename = this_valuename\n if 'key' in child_item.attrib:\n child_key = child_item.attrib['key']\n if 'valueName' in child_item.attrib:\n child_valuename = child_item.attrib['valueName']\n\n if etree.QName(child_item).localname == 'boolean':\n # https://msdn.microsoft.com/en-us/library/dn605978(v=vs.85).aspx\n if child_item.getchildren():\n if TRUE_VALUE_XPATH(child_item) and this_element_name not in configured_elements:\n if _checkValueItemParent(child_item,\n this_policyname,\n child_key,\n child_valuename,\n TRUE_VALUE_XPATH,\n policy_file_data):\n configured_elements[this_element_name] = True\n log.debug('element %s is configured true',\n child_item.attrib['id'])\n if FALSE_VALUE_XPATH(child_item) and this_element_name not in configured_elements:\n if _checkValueItemParent(child_item,\n this_policyname,\n child_key,\n child_valuename,\n FALSE_VALUE_XPATH,\n policy_file_data):\n configured_elements[this_element_name] = False\n policy_disabled_elements = policy_disabled_elements + 1\n log.debug('element %s is configured false',\n child_item.attrib['id'])\n # WARNING - no standard ADMX files use true/falseList\n # so this hasn't actually been tested\n if TRUE_LIST_XPATH(child_item) and this_element_name not in configured_elements:\n log.debug('checking trueList')\n if _checkListItem(child_item,\n this_policyname,\n this_key,\n TRUE_LIST_XPATH,\n policy_file_data):\n configured_elements[this_element_name] = True\n log.debug('element %s is configured true',\n child_item.attrib['id'])\n if FALSE_LIST_XPATH(child_item) and this_element_name not in configured_elements:\n log.debug('checking falseList')\n if _checkListItem(child_item,\n this_policyname,\n this_key,\n FALSE_LIST_XPATH,\n policy_file_data):\n configured_elements[this_element_name] = False\n policy_disabled_elements = policy_disabled_elements + 1\n log.debug('element %s is configured false',\n child_item.attrib['id'])\n else:\n if _regexSearchRegPolData(re.escape(_processValueItem(child_item,\n child_key,\n child_valuename,\n admx_policy,\n elements_item,\n check_deleted=True)),\n policy_file_data):\n configured_elements[this_element_name] = False\n policy_disabled_elements = policy_disabled_elements + 1\n log.debug('element %s is configured false', child_item.attrib['id'])\n elif _regexSearchRegPolData(re.escape(_processValueItem(child_item,\n child_key,\n child_valuename,\n admx_policy,\n elements_item,\n check_deleted=False)),\n policy_file_data):\n configured_elements[this_element_name] = True\n log.debug('element %s is configured true',\n child_item.attrib['id'])\n elif etree.QName(child_item).localname == 'decimal' \\\n or etree.QName(child_item).localname == 'text' \\\n or etree.QName(child_item).localname == 'longDecimal' \\\n or etree.QName(child_item).localname == 'multiText':\n # https://msdn.microsoft.com/en-us/library/dn605987(v=vs.85).aspx\n if _regexSearchRegPolData(re.escape(_processValueItem(child_item,\n child_key,\n child_valuename,\n admx_policy,\n elements_item,\n check_deleted=True)),\n policy_file_data):\n configured_elements[this_element_name] = 'Disabled'\n policy_disabled_elements = policy_disabled_elements + 1\n log.debug('element %s is disabled',\n child_item.attrib['id'])\n elif _regexSearchRegPolData(re.escape(_processValueItem(child_item,\n child_key,\n child_valuename,\n admx_policy,\n elements_item,\n check_deleted=False)),\n policy_file_data):\n configured_value = _getDataFromRegPolData(_processValueItem(child_item,\n child_key,\n child_valuename,\n admx_policy,\n elements_item,\n check_deleted=False),\n policy_file_data)\n configured_elements[this_element_name] = configured_value\n log.debug('element %s is enabled, value == %s',\n child_item.attrib['id'],\n configured_value)\n elif etree.QName(child_item).localname == 'enum':\n if _regexSearchRegPolData(re.escape(_processValueItem(child_item,\n child_key,\n child_valuename,\n admx_policy,\n elements_item,\n check_deleted=True)),\n policy_file_data):\n log.debug('enum element %s is disabled',\n child_item.attrib['id'])\n configured_elements[this_element_name] = 'Disabled'\n policy_disabled_elements = policy_disabled_elements + 1\n else:\n for enum_item in child_item.getchildren():\n if _checkValueItemParent(enum_item,\n child_item.attrib['id'],\n child_key,\n child_valuename,\n VALUE_XPATH,\n policy_file_data):\n if VALUE_LIST_XPATH(enum_item):\n log.debug('enum item has a valueList')\n if _checkListItem(enum_item,\n this_policyname,\n child_key,\n VALUE_LIST_XPATH,\n policy_file_data):\n log.debug('all valueList items exist in file')\n configured_elements[this_element_name] = _getAdmlDisplayName(\n adml_policy_resources,\n enum_item.attrib['displayName'])\n break\n else:\n configured_elements[this_element_name] = _getAdmlDisplayName(\n adml_policy_resources,\n enum_item.attrib['displayName'])\n break\n elif etree.QName(child_item).localname == 'list':\n return_value_name = False\n if 'explicitValue' in child_item.attrib \\\n and child_item.attrib['explicitValue'].lower() == 'true':\n log.debug('explicitValue list, we will return value names')\n return_value_name = True\n if _regexSearchRegPolData(re.escape(_processValueItem(child_item,\n child_key,\n child_valuename,\n admx_policy,\n elements_item,\n check_deleted=False)\n ) + salt.utils.stringutils.to_bytes(r'(?!\\*\\*delvals\\.)'),\n policy_file_data):\n configured_value = _getDataFromRegPolData(_processValueItem(child_item,\n child_key,\n child_valuename,\n admx_policy,\n elements_item,\n check_deleted=False),\n policy_file_data,\n return_value_name=return_value_name)\n configured_elements[this_element_name] = configured_value\n log.debug('element %s is enabled values: %s',\n child_item.attrib['id'],\n configured_value)\n elif _regexSearchRegPolData(re.escape(_processValueItem(child_item,\n child_key,\n child_valuename,\n admx_policy,\n elements_item,\n check_deleted=True)),\n policy_file_data):\n configured_elements[this_element_name] = \"Disabled\"\n policy_disabled_elements = policy_disabled_elements + 1\n log.debug('element %s is disabled', child_item.attrib['id'])\n if element_only_enabled_disabled:\n if required_elements \\\n and len(configured_elements) == len(required_elements):\n if policy_disabled_elements == len(required_elements):\n log.debug('%s is disabled by all enum elements', this_policyname)\n if this_policynamespace not in policy_vals:\n policy_vals[this_policynamespace] = {}\n policy_vals[this_policynamespace][this_policyname] = 'Disabled'\n else:\n if this_policynamespace not in policy_vals:\n policy_vals[this_policynamespace] = {}\n policy_vals[this_policynamespace][this_policyname] = configured_elements\n log.debug('%s is enabled by enum elements', this_policyname)\n else:\n if this_policy_setting == 'Enabled':\n if this_policynamespace not in policy_vals:\n policy_vals[this_policynamespace] = {}\n policy_vals[this_policynamespace][this_policyname] = configured_elements\n if return_full_policy_names and this_policynamespace in policy_vals and this_policyname in policy_vals[this_policynamespace]:\n if this_policynamespace not in full_names:\n full_names[this_policynamespace] = {}\n full_names[this_policynamespace][this_policyname] = _getFullPolicyName(\n policy_item=admx_policy,\n policy_name=admx_policy.attrib['name'],\n return_full_policy_names=return_full_policy_names,\n adml_language=adml_language)\n if this_policynamespace in policy_vals and this_policyname in policy_vals[this_policynamespace]:\n if this_policynamespace not in hierarchy:\n hierarchy[this_policynamespace] = {}\n hierarchy[this_policynamespace][this_policyname] = _build_parent_list(\n policy_definition=admx_policy,\n return_full_policy_names=return_full_policy_names,\n adml_language=adml_language)\n if policy_vals and return_full_policy_names and not hierarchical_return:\n unpathed_dict = {}\n pathed_dict = {}\n for policy_namespace in list(policy_vals):\n for policy_item in list(policy_vals[policy_namespace]):\n if full_names[policy_namespace][policy_item] in policy_vals[policy_namespace]:\n # add this item with the path'd full name\n full_path_list = hierarchy[policy_namespace][policy_item]\n full_path_list.reverse()\n full_path_list.append(full_names[policy_namespace][policy_item])\n policy_vals['\\\\'.join(full_path_list)] = policy_vals[policy_namespace].pop(policy_item)\n pathed_dict[full_names[policy_namespace][policy_item]] = True\n else:\n policy_vals[policy_namespace][full_names[policy_namespace][policy_item]] = policy_vals[policy_namespace].pop(policy_item)\n if policy_namespace not in unpathed_dict:\n unpathed_dict[policy_namespace] = {}\n unpathed_dict[policy_namespace][full_names[policy_namespace][policy_item]] = policy_item\n # go back and remove any \"unpathed\" policies that need a full path\n for path_needed in unpathed_dict[policy_namespace]:\n # remove the item with the same full name and re-add it w/a path'd version\n full_path_list = hierarchy[policy_namespace][unpathed_dict[policy_namespace][path_needed]]\n full_path_list.reverse()\n full_path_list.append(path_needed)\n log.debug('full_path_list == %s', full_path_list)\n policy_vals['\\\\'.join(full_path_list)] = policy_vals[policy_namespace].pop(path_needed)\n for policy_namespace in list(policy_vals):\n if policy_vals[policy_namespace] == {}:\n policy_vals.pop(policy_namespace)\n if policy_vals and hierarchical_return:\n if hierarchy:\n for policy_namespace in hierarchy:\n for hierarchy_item in hierarchy[policy_namespace]:\n if hierarchy_item in policy_vals[policy_namespace]:\n tdict = {}\n first_item = True\n for item in hierarchy[policy_namespace][hierarchy_item]:\n newdict = {}\n if first_item:\n h_policy_name = hierarchy_item\n if return_full_policy_names:\n h_policy_name = full_names[policy_namespace][hierarchy_item]\n newdict[item] = {h_policy_name: policy_vals[policy_namespace].pop(hierarchy_item)}\n first_item = False\n else:\n newdict[item] = tdict\n tdict = newdict\n if tdict:\n policy_vals = dictupdate.update(policy_vals, tdict)\n if policy_namespace in policy_vals and policy_vals[policy_namespace] == {}:\n policy_vals.pop(policy_namespace)\n policy_vals = {\n module_policy_data.admx_registry_classes[policy_class]['lgpo_section']: {\n 'Administrative Templates': policy_vals\n }\n }\n return policy_vals"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _build_parent_list(policy_definition,\n return_full_policy_names,\n adml_language):\n '''\n helper function to build a list containing parent elements of the ADMX\n policy\n '''\n parent_list = []\n policy_namespace = list(policy_definition.nsmap.keys())[0]\n parent_category = policy_definition.xpath(\n '{0}:parentCategory/@ref'.format(policy_namespace),\n namespaces=policy_definition.nsmap)\n admx_policy_definitions = _get_policy_definitions(language=adml_language)\n if parent_category:\n parent_category = parent_category[0]\n nsmap_xpath = '/policyDefinitions/policyNamespaces/{0}:*' \\\n ''.format(policy_namespace)\n this_namespace_map = _buildElementNsmap(\n admx_policy_definitions.xpath(\n nsmap_xpath, namespaces=policy_definition.nsmap))\n this_namespace_map = dictupdate.update(this_namespace_map,\n policy_definition.nsmap)\n parent_list = _admx_policy_parent_walk(\n path=parent_list,\n policy_namespace=policy_namespace,\n parent_category=parent_category,\n policy_nsmap=this_namespace_map,\n return_full_policy_names=return_full_policy_names,\n adml_language=adml_language)\n return parent_list", "response": "helper function to build a list containing parent elements of the ADMX policy containing the current ones of the ADMX element."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _write_regpol_data(data_to_write,\n policy_file_path,\n gpt_ini_path,\n gpt_extension,\n gpt_extension_guid):\n '''\n helper function to actually write the data to a Registry.pol file\n\n also updates/edits the gpt.ini file to include the ADM policy extensions\n to let the computer know user and/or machine registry policy files need\n to be processed\n\n data_to_write: data to write into the user/machine registry.pol file\n policy_file_path: path to the registry.pol file\n gpt_ini_path: path to gpt.ini file\n gpt_extension: gpt extension list name from _policy_info class for this registry class gpt_extension_location\n gpt_extension_guid: admx registry extension guid for the class\n '''\n try:\n reg_pol_header = u'\\u5250\\u6765\\x01\\x00'\n if not os.path.exists(policy_file_path):\n __salt__['file.makedirs'](policy_file_path)\n with salt.utils.files.fopen(policy_file_path, 'wb') as pol_file:\n if not data_to_write.startswith(reg_pol_header.encode('utf-16-le')):\n pol_file.write(reg_pol_header.encode('utf-16-le'))\n pol_file.write(data_to_write)\n try:\n gpt_ini_data = ''\n if os.path.exists(gpt_ini_path):\n with salt.utils.files.fopen(gpt_ini_path, 'r') as gpt_file:\n gpt_ini_data = gpt_file.read()\n if not _regexSearchRegPolData(r'\\[General\\]\\r\\n', gpt_ini_data):\n gpt_ini_data = '[General]\\r\\n' + gpt_ini_data\n if _regexSearchRegPolData(r'{0}='.format(re.escape(gpt_extension)),\n gpt_ini_data):\n # ensure the line contains the ADM guid\n gpt_ext_loc = re.search(r'^{0}=.*\\r\\n'.format(re.escape(gpt_extension)),\n gpt_ini_data,\n re.IGNORECASE | re.MULTILINE)\n gpt_ext_str = gpt_ini_data[gpt_ext_loc.start():gpt_ext_loc.end()]\n if not _regexSearchRegPolData(r'{0}'.format(re.escape(gpt_extension_guid)),\n gpt_ext_str):\n gpt_ext_str = gpt_ext_str.split('=')\n gpt_ext_str[1] = gpt_extension_guid + gpt_ext_str[1]\n gpt_ext_str = '='.join(gpt_ext_str)\n gpt_ini_data = gpt_ini_data[0:gpt_ext_loc.start()] + gpt_ext_str + gpt_ini_data[gpt_ext_loc.end():]\n else:\n general_location = re.search(r'^\\[General\\]\\r\\n',\n gpt_ini_data,\n re.IGNORECASE | re.MULTILINE)\n gpt_ini_data = '{0}{1}={2}\\r\\n{3}'.format(\n gpt_ini_data[general_location.start():general_location.end()],\n gpt_extension,\n gpt_extension_guid,\n gpt_ini_data[general_location.end():])\n # https://technet.microsoft.com/en-us/library/cc978247.aspx\n if _regexSearchRegPolData(r'Version=', gpt_ini_data):\n version_loc = re.search(r'^Version=.*\\r\\n',\n gpt_ini_data,\n re.IGNORECASE | re.MULTILINE)\n version_str = gpt_ini_data[version_loc.start():version_loc.end()]\n version_str = version_str.split('=')\n version_nums = struct.unpack(b'>2H', struct.pack(b'>I', int(version_str[1])))\n if gpt_extension.lower() == 'gPCMachineExtensionNames'.lower():\n version_nums = (version_nums[0], version_nums[1] + 1)\n elif gpt_extension.lower() == 'gPCUserExtensionNames'.lower():\n version_nums = (version_nums[0] + 1, version_nums[1])\n version_num = struct.unpack(b'>I', struct.pack(b'>2H', *version_nums))[0]\n gpt_ini_data = '{0}{1}={2}\\r\\n{3}'.format(\n gpt_ini_data[0:version_loc.start()],\n 'Version',\n version_num,\n gpt_ini_data[version_loc.end():])\n else:\n general_location = re.search(r'^\\[General\\]\\r\\n',\n gpt_ini_data,\n re.IGNORECASE | re.MULTILINE)\n if gpt_extension.lower() == 'gPCMachineExtensionNames'.lower():\n version_nums = (0, 1)\n elif gpt_extension.lower() == 'gPCUserExtensionNames'.lower():\n version_nums = (1, 0)\n gpt_ini_data = '{0}{1}={2}\\r\\n{3}'.format(\n gpt_ini_data[general_location.start():general_location.end()],\n 'Version',\n int(\"{0}{1}\".format(six.text_type(version_nums[0]).zfill(4),\n six.text_type(version_nums[1]).zfill(4)),\n 16),\n gpt_ini_data[general_location.end():])\n if gpt_ini_data:\n with salt.utils.files.fopen(gpt_ini_path, 'w') as gpt_file:\n gpt_file.write(gpt_ini_data)\n # TODO: This needs to be more specific\n except Exception as e:\n msg = 'An error occurred attempting to write to {0}, the exception was {1}'.format(\n gpt_ini_path, e)\n log.exception(msg)\n raise CommandExecutionError(msg)\n # TODO: This needs to be more specific\n except Exception as e:\n msg = 'An error occurred attempting to write to {0}, the exception was {1}'.format(policy_file_path, e)\n log.exception(msg)\n raise CommandExecutionError(msg)", "response": "Helper function to actually write the data into a Registry. pol file"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _policyFileReplaceOrAppendList(string_list, policy_data):\n '''\n helper function to take a list of strings for registry.pol file data and\n update existing strings or append the strings\n '''\n if not policy_data:\n policy_data = b''\n # we are going to clean off the special pre-fixes, so we get only the valuename\n specialValueRegex = salt.utils.stringutils.to_bytes(r'(\\*\\*Del\\.|\\*\\*DelVals\\.){0,1}')\n for this_string in string_list:\n list_item_key = this_string.split(b'\\00;')[0].lstrip(b'[')\n list_item_value_name = re.sub(specialValueRegex,\n b'',\n this_string.split(b'\\00;')[1],\n flags=re.IGNORECASE)\n log.debug('item value name is %s', list_item_value_name)\n data_to_replace = _regexSearchKeyValueCombo(policy_data,\n list_item_key,\n list_item_value_name)\n if data_to_replace:\n log.debug('replacing %s with %s', data_to_replace, this_string)\n policy_data = policy_data.replace(data_to_replace, this_string)\n else:\n log.debug('appending %s', this_string)\n policy_data = b''.join([policy_data, this_string])\n return policy_data", "response": "helper function to take a list of strings for registry. pol file data and update existing strings or append the strings\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _policyFileReplaceOrAppend(this_string, policy_data, append_only=False):\n '''\n helper function to take a ADMX policy string for registry.pol file data and\n update existing string or append the string to the data\n '''\n # we are going to clean off the special pre-fixes, so we get only the valuename\n if not policy_data:\n policy_data = b''\n specialValueRegex = salt.utils.stringutils.to_bytes(r'(\\*\\*Del\\.|\\*\\*DelVals\\.){0,1}')\n item_key = None\n item_value_name = None\n data_to_replace = None\n if not append_only:\n item_key = this_string.split(b'\\00;')[0].lstrip(b'[')\n item_value_name = re.sub(specialValueRegex,\n b'',\n this_string.split(b'\\00;')[1],\n flags=re.IGNORECASE)\n log.debug('item value name is %s', item_value_name)\n data_to_replace = _regexSearchKeyValueCombo(policy_data, item_key, item_value_name)\n if data_to_replace:\n log.debug('replacing %s with %s', data_to_replace, this_string)\n policy_data = policy_data.replace(data_to_replace, this_string)\n else:\n log.debug('appending %s', this_string)\n policy_data = b''.join([policy_data, this_string])\n\n return policy_data", "response": "helper function to take a string for registry. pol file data and update existing string or append the string to the data\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _lookup_admin_template(policy_name,\n policy_class,\n adml_language='en-US'):\n '''\n (success_flag, policy_xml_item, policy_name_list, message)\n '''\n policy_aliases = []\n admx_policy_definitions = _get_policy_definitions(language=adml_language)\n adml_policy_resources = _get_policy_resources(language=adml_language)\n admx_search_results = ADMX_SEARCH_XPATH(admx_policy_definitions,\n policy_name=policy_name,\n registry_class=policy_class)\n if admx_search_results:\n if len(admx_search_results) == 1:\n the_policy = admx_search_results[0]\n policy_display_name = _getFullPolicyName(\n policy_item=the_policy,\n policy_name=the_policy.attrib['name'],\n return_full_policy_names=True,\n adml_language=adml_language)\n policy_aliases.append(policy_display_name)\n policy_aliases.append(the_policy.attrib['name'])\n full_path_list = _build_parent_list(policy_definition=the_policy,\n return_full_policy_names=True,\n adml_language=adml_language)\n full_path_list.reverse()\n full_path_list.append(policy_display_name)\n policy_aliases.append('\\\\'.join(full_path_list))\n return True, the_policy, policy_aliases, None\n else:\n msg = 'ADMX policy name/id \"{0}\" is used in multiple ADMX files'\n return False, None, [], msg\n else:\n adml_search_results = ADML_SEARCH_XPATH(adml_policy_resources,\n policy_name=policy_name)\n hierarchy = []\n hierarchy_policy_name = policy_name\n if not adml_search_results:\n if '\\\\' in policy_name:\n hierarchy = policy_name.split('\\\\')\n policy_name = hierarchy.pop()\n adml_search_results = ADML_SEARCH_XPATH(adml_policy_resources,\n policy_name=policy_name)\n if adml_search_results:\n multiple_adml_entries = False\n suggested_policies = ''\n adml_to_remove = []\n if len(adml_search_results) > 1:\n log.debug('multiple ADML entries found matching the policy name %s', policy_name)\n multiple_adml_entries = True\n for adml_search_result in adml_search_results:\n if not getattr(adml_search_result, 'text', '').strip() == policy_name:\n adml_to_remove.append(adml_search_result)\n else:\n if hierarchy:\n log.debug('we have hierarchy of %s', hierarchy)\n display_name_searchval = '$({0}.{1})'.format(\n adml_search_result.tag.split('}')[1],\n adml_search_result.attrib['id'])\n #policy_search_string = '//{0}:policy[@*[local-name() = \"displayName\"] = \"{1}\" and (@*[local-name() = \"class\"] = \"Both\" or @*[local-name() = \"class\"] = \"{2}\") ]'.format(\n policy_search_string = '//{0}:policy[@displayName = \"{1}\" and (@class = \"Both\" or @class = \"{2}\") ]'.format(\n adml_search_result.prefix,\n display_name_searchval,\n policy_class)\n admx_results = []\n these_admx_search_results = admx_policy_definitions.xpath(policy_search_string, namespaces=adml_search_result.nsmap)\n if not these_admx_search_results:\n log.debug('No admx was found for the adml entry %s, it will be removed', display_name_searchval)\n adml_to_remove.append(adml_search_result)\n for search_result in these_admx_search_results:\n log.debug('policy_name == %s', policy_name)\n this_hierarchy = _build_parent_list(\n policy_definition=search_result,\n return_full_policy_names=True,\n adml_language=adml_language)\n this_hierarchy.reverse()\n if hierarchy != this_hierarchy:\n msg = 'hierarchy %s does not match this item\\'s hierarchy of %s'\n log.debug(msg, hierarchy, this_hierarchy)\n if len(these_admx_search_results) == 1:\n log.debug('only 1 admx was found and it does not match this adml, it is safe to remove from the list')\n adml_to_remove.append(adml_search_result)\n else:\n log.debug('hierarchy %s matches item\\'s hierarchy of %s', hierarchy, this_hierarchy)\n log.debug('search_result %s added to results', search_result)\n admx_results.append(search_result)\n if len(admx_results) == 1:\n admx_search_results.append(admx_results[0])\n else:\n # verify the ADMX correlated to this ADML is in the same class\n # that we are looking for\n display_name_searchval = '$({0}.{1})'.format(\n adml_search_result.tag.split('}')[1],\n adml_search_result.attrib['id'])\n these_admx_search_results = ADMX_DISPLAYNAME_SEARCH_XPATH(\n admx_policy_definitions,\n display_name=display_name_searchval,\n registry_class=policy_class)\n if not these_admx_search_results:\n adml_to_remove.append(adml_search_result)\n for adml in adml_to_remove:\n if adml in adml_search_results:\n adml_search_results.remove(adml)\n if len(adml_search_results) == 1 and multiple_adml_entries:\n multiple_adml_entries = False\n for adml_search_result in adml_search_results:\n log.debug('found an ADML entry matching the string! %s -- %s',\n adml_search_result.tag,\n adml_search_result.attrib)\n display_name_searchval = '$({0}.{1})'.format(\n adml_search_result.tag.split('}')[1],\n adml_search_result.attrib['id'])\n log.debug('searching for displayName == %s', display_name_searchval)\n if not admx_search_results:\n log.debug('search for an admx entry matching display_name %s and registry_class %s', display_name_searchval, policy_class)\n admx_search_results = ADMX_DISPLAYNAME_SEARCH_XPATH(\n admx_policy_definitions,\n display_name=display_name_searchval,\n registry_class=policy_class)\n if admx_search_results:\n log.debug('processing admx_search_results of %s', admx_search_results)\n log.debug('multiple_adml_entries is %s', multiple_adml_entries)\n if (len(admx_search_results) == 1 or hierarchy) and not multiple_adml_entries:\n found = False\n for search_result in admx_search_results:\n found = False\n if hierarchy:\n this_hierarchy = _build_parent_list(\n policy_definition=search_result,\n return_full_policy_names=True,\n adml_language=adml_language)\n this_hierarchy.reverse()\n log.debug('testing %s == %s', hierarchy, this_hierarchy)\n if hierarchy == this_hierarchy:\n found = True\n else:\n found = True\n if found:\n log.debug('found the ADMX policy matching '\n 'the display name %s -- %s',\n search_result, policy_name)\n if 'name' in search_result.attrib:\n policy_display_name = _getFullPolicyName(\n policy_item=search_result,\n policy_name=search_result.attrib['name'],\n return_full_policy_names=True,\n adml_language=adml_language)\n policy_aliases.append(policy_display_name)\n policy_aliases.append(search_result.attrib['name'])\n full_path_list = _build_parent_list(\n policy_definition=search_result,\n return_full_policy_names=True,\n adml_language=adml_language)\n full_path_list.reverse()\n full_path_list.append(policy_display_name)\n policy_aliases.append('\\\\'.join(full_path_list))\n return True, search_result, policy_aliases, None\n else:\n msg = ('ADMX policy with the display name {0} does not'\n 'have the required name attribtue')\n msg = msg.format(policy_name)\n return False, None, [], msg\n if not found:\n msg = 'Unable to correlate {0} to any policy'.format(hierarchy_policy_name)\n return False, None, [], msg\n else:\n for possible_policy in admx_search_results:\n this_parent_list = _build_parent_list(\n policy_definition=possible_policy,\n return_full_policy_names=True,\n adml_language=adml_language)\n this_parent_list.reverse()\n this_parent_list.append(policy_name)\n if suggested_policies:\n suggested_policies = ', '.join([suggested_policies,\n '\\\\'.join(this_parent_list)])\n else:\n suggested_policies = '\\\\'.join(this_parent_list)\n if suggested_policies:\n msg = ('ADML policy name \"{0}\" is used as the display name'\n ' for multiple policies.'\n ' These policies matched: {1}'\n '. You can utilize these long names to'\n ' specify the correct policy')\n return False, None, [], \\\n msg.format(policy_name, suggested_policies)\n return False, None, [], \\\n 'Unable to find {0} policy {1}'.format(policy_class, policy_name)", "response": "Internal function to search for an admin template for a specific policy."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get(policy_class=None, return_full_policy_names=True,\n hierarchical_return=False, adml_language='en-US',\n return_not_configured=False):\n '''\n Get a policy value\n\n Args:\n\n policy_class (str):\n Some policies are both user and computer, by default all policies\n will be pulled, but this can be used to retrieve only a specific\n policy class User/USER/user = retrieve user policies\n Machine/MACHINE/machine/Computer/COMPUTER/computer = retrieve\n machine/computer policies\n\n return_full_policy_names (bool):\n True/False to return the policy name as it is seen in the\n ``gpedit.msc`` GUI or to only return the policy key/id.\n\n hierarchical_return (bool):\n True/False to return the policy data in the hierarchy as seen in the\n ``gpedit.msc`` GUI. The default of False will return data split only\n into User/Computer configuration sections\n\n adml_language (str):\n The ADML language to use for processing display/descriptive names\n and enumeration values of ADMX template data, defaults to en-US\n\n return_not_configured (bool):\n Include Administrative Template policies that are 'Not Configured'\n in the return data\n\n Returns:\n dict: A dictionary containing the policy values for the specified class\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' lgpo.get machine return_full_policy_names=True\n '''\n\n vals = {}\n modal_returns = {}\n _policydata = _policy_info()\n\n if policy_class is None or policy_class.lower() == 'both':\n policy_class = _policydata.policies.keys()\n elif policy_class.lower() not in [z.lower() for z in _policydata.policies.keys()]:\n msg = 'The policy_class {0} is not an available policy class, please ' \\\n 'use one of the following: {1}, Both'\n raise SaltInvocationError(\n msg.format(policy_class, ', '.join(_policydata.policies.keys())))\n else:\n policy_class = [policy_class.title()]\n\n # handle policies statically defined in this module\n for p_class in policy_class:\n this_class_policy_names = _policydata.policies[p_class]['policies']\n class_vals = {}\n for policy_name in this_class_policy_names:\n _pol = None\n if policy_name in _policydata.policies[p_class]['policies']:\n _pol = _policydata.policies[p_class]['policies'][policy_name]\n else:\n for policy in _policydata.policies[p_class]['policies']:\n if _policydata.policies[p_class]['policies'][policy]['Policy'].upper() == policy_name.upper():\n _pol = _policydata.policies[p_class]['policies'][policy]\n policy_name = policy\n if _pol:\n vals_key_name = policy_name\n if 'Registry' in _pol:\n # get value from registry\n class_vals[policy_name] = __utils__['reg.read_value'](\n _pol['Registry']['Hive'],\n _pol['Registry']['Path'],\n _pol['Registry']['Value'])['vdata']\n log.debug(\n 'Value %r found for reg policy %s',\n class_vals[policy_name], policy_name\n )\n elif 'Secedit' in _pol:\n # get value from secedit\n _val = _get_secedit_value(option=_pol['Secedit']['Option'])\n class_vals[policy_name] = _val\n elif 'NetSH' in _pol:\n # get value from netsh\n class_vals[policy_name] = _findOptionValueNetSH(\n profile=_pol['NetSH']['Profile'],\n option=_pol['NetSH']['Option'])\n elif 'AdvAudit' in _pol:\n # get value from auditpol\n class_vals[policy_name] = _findOptionValueAdvAudit(\n option=_pol['AdvAudit']['Option'])\n elif 'NetUserModal' in _pol:\n # get value from UserNetMod\n if _pol['NetUserModal']['Modal'] not in modal_returns:\n modal_returns[_pol['NetUserModal']['Modal']] = win32net.NetUserModalsGet(\n None,\n _pol['NetUserModal']['Modal'])\n class_vals[policy_name] = modal_returns[_pol['NetUserModal']['Modal']][_pol['NetUserModal']['Option']]\n elif 'LsaRights' in _pol:\n class_vals[policy_name] = _getRightsAssignments(_pol['LsaRights']['Option'])\n elif 'ScriptIni' in _pol:\n log.debug('Working with ScriptIni setting %s', policy_name)\n class_vals[policy_name] = _getScriptSettingsFromIniFile(_pol)\n if policy_name in class_vals:\n class_vals[policy_name] = _transform_value(\n value=class_vals[policy_name],\n policy=_policydata.policies[p_class]['policies'][policy_name],\n transform_type='Get')\n if return_full_policy_names:\n class_vals[_pol['Policy']] = class_vals.pop(policy_name)\n vals_key_name = _pol['Policy']\n if hierarchical_return:\n if 'lgpo_section' in _pol:\n firstItem = True\n tdict = {}\n for level in reversed(_pol['lgpo_section']):\n newdict = {}\n if firstItem:\n newdict[level] = {vals_key_name: class_vals.pop(vals_key_name)}\n firstItem = False\n else:\n newdict[level] = tdict\n tdict = newdict\n if tdict:\n class_vals = dictupdate.update(class_vals, tdict)\n else:\n msg = 'The specified policy {0} is not currently available ' \\\n 'to be configured via this module'\n raise SaltInvocationError(msg.format(policy_name))\n class_vals = dictupdate.update(\n class_vals,\n _checkAllAdmxPolicies(policy_class=p_class,\n adml_language=adml_language,\n return_full_policy_names=return_full_policy_names,\n hierarchical_return=hierarchical_return,\n return_not_configured=return_not_configured))\n if _policydata.policies[p_class]['lgpo_section'] not in class_vals:\n temp_dict = {\n _policydata.policies[p_class]['lgpo_section']: class_vals}\n class_vals = temp_dict\n vals = dictupdate.update(vals, class_vals)\n\n return vals", "response": "Get a specific class of a specific policy value"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_computer_policy(name,\n setting,\n cumulative_rights_assignments=True,\n adml_language='en-US'):\n '''\n Set a single computer policy\n\n Args:\n name (str):\n The name of the policy to configure\n\n setting (str):\n The setting to configure the named policy with\n\n cumulative_rights_assignments (bool): Determine how user rights\n assignment policies are configured. If True, user right assignment\n specifications are simply added to the existing policy. If False,\n only the users specified will get the right (any existing will have\n the right revoked)\n\n adml_language (str): The language files to use for looking up\n Administrative Template policy data (i.e. how the policy is\n displayed in the GUI). Defaults to 'en-US' (U.S. English).\n\n Returns:\n bool: True if successful, otherwise False\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' lgpo.set_computer_policy LockoutDuration 1440\n '''\n pol = {}\n pol[name] = setting\n ret = set_(computer_policy=pol,\n user_policy=None,\n cumulative_rights_assignments=cumulative_rights_assignments,\n adml_language=adml_language)\n return ret", "response": "Set a single computer policy with the specified name and setting."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nsetting a local server policy. Args: computer_policy (dict): A dictionary of \"policyname: value\" pairs of computer policies to set. 'value' should be how it is displayed in the gpedit GUI, i.e. if a setting can be 'Enabled'/'Disabled', then that should be passed Administrative Template data may require dicts within dicts, to specify each element of the Administrative Template policy. Administrative Templates policies are always cumulative. Policy names can be specified in a number of ways based on the type of policy: Windows Settings Policies: These policies can be specified using the GUI display name or the key name from the _policy_info class in this module. The GUI display name is also contained in the _policy_info class in this module. Administrative Template Policies: These can be specified using the policy name as displayed in the GUI (case sensitive). Some policies have the same name, but a different location (for example, \"Access data sources across domains\"). These can be differentiated by the \"path\" in the GUI (for example, \"Windows Components\\\\Internet Explorer\\\\Internet Control Panel\\\\Security Page\\\\Internet Zone\\\\Access data sources across domains\"). Additionally, policies can be specified using the \"name\" and \"id\" attributes from the ADMX files. For Administrative Templates that have policy elements, each element can be specified using the text string as seen in the GUI or using the ID attribute from the ADMX file. Due to the way some of the GUI text is laid out, some policy element names could include descriptive text that appears lbefore the policy element in the GUI. Use the get_policy_info function for the policy name to view the element ID/names that the module will accept. user_policy (dict): The same setup as the computer_policy, except with data to configure the local user policy. cumulative_rights_assignments (bool): Determine how user rights assignment policies are configured. If True, user right assignment specifications are simply added to the existing policy If False, only the users specified will get the right (any existing will have the right revoked) adml_language (str): The language files to use for looking up Administrative Template policy data (i.e. how the policy is displayed in the GUI). Defaults to 'en-US' (U.S. English). Returns: bool: True is successful, otherwise False CLI Example: .. code-block:: bash salt '*' lgpo.set computer_policy=\"{'LockoutDuration': 2, 'RestrictAnonymous': 'Enabled', 'AuditProcessTracking': 'Succes, Failure'}\"", "response": "def set_(computer_policy=None,\n user_policy=None,\n cumulative_rights_assignments=True,\n adml_language='en-US'):\n '''\n Set a local server policy.\n\n Args:\n\n computer_policy (dict):\n A dictionary of \"policyname: value\" pairs of computer policies to\n set. 'value' should be how it is displayed in the gpedit GUI, i.e.\n if a setting can be 'Enabled'/'Disabled', then that should be passed\n\n Administrative Template data may require dicts within dicts, to\n specify each element of the Administrative Template policy.\n Administrative Templates policies are always cumulative.\n\n Policy names can be specified in a number of ways based on the type\n of policy:\n\n Windows Settings Policies:\n\n These policies can be specified using the GUI display name\n or the key name from the _policy_info class in this module.\n The GUI display name is also contained in the _policy_info\n class in this module.\n\n Administrative Template Policies:\n\n These can be specified using the policy name as displayed in\n the GUI (case sensitive). Some policies have the same name,\n but a different location (for example, \"Access data sources\n across domains\"). These can be differentiated by the \"path\"\n in the GUI (for example, \"Windows Components\\\\Internet\n Explorer\\\\Internet Control Panel\\\\Security Page\\\\Internet\n Zone\\\\Access data sources across domains\").\n\n Additionally, policies can be specified using the \"name\" and\n \"id\" attributes from the ADMX files.\n\n For Administrative Templates that have policy elements, each\n element can be specified using the text string as seen in\n the GUI or using the ID attribute from the ADMX file. Due to\n the way some of the GUI text is laid out, some policy\n element names could include descriptive text that appears\n lbefore the policy element in the GUI.\n\n Use the get_policy_info function for the policy name to view\n the element ID/names that the module will accept.\n\n user_policy (dict):\n The same setup as the computer_policy, except with data to configure\n the local user policy.\n\n cumulative_rights_assignments (bool):\n Determine how user rights assignment policies are configured.\n\n If True, user right assignment specifications are simply added to\n the existing policy\n\n If False, only the users specified will get the right (any existing\n will have the right revoked)\n\n adml_language (str):\n The language files to use for looking up Administrative Template\n policy data (i.e. how the policy is displayed in the GUI). Defaults\n to 'en-US' (U.S. English).\n\n Returns:\n bool: True is successful, otherwise False\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' lgpo.set computer_policy=\"{'LockoutDuration': 2, 'RestrictAnonymous': 'Enabled', 'AuditProcessTracking': 'Succes, Failure'}\"\n '''\n\n if computer_policy and not isinstance(computer_policy, dict):\n msg = 'computer_policy must be specified as a dict'\n raise SaltInvocationError(msg)\n if user_policy and not isinstance(user_policy, dict):\n msg = 'user_policy must be specified as a dict'\n raise SaltInvocationError(msg)\n policies = {}\n policies['User'] = user_policy\n policies['Machine'] = computer_policy\n if policies:\n adml_policy_resources = _get_policy_resources(language=adml_language)\n for p_class in policies:\n _secedits = {}\n _netshs = {}\n _advaudits = {}\n _modal_sets = {}\n _admTemplateData = {}\n _regedits = {}\n _lsarights = {}\n _policydata = _policy_info()\n if policies[p_class]:\n for policy_name in policies[p_class]:\n _pol = None\n policy_key_name = policy_name\n if policy_name in _policydata.policies[p_class]['policies']:\n _pol = _policydata.policies[p_class]['policies'][policy_name]\n else:\n for policy in _policydata.policies[p_class]['policies']:\n if _policydata.policies[p_class]['policies'][policy]['Policy'].upper() == \\\n policy_name.upper():\n _pol = _policydata.policies[p_class]['policies'][policy]\n policy_key_name = policy\n if _pol:\n # transform and validate the setting\n _value = _transform_value(\n value=policies[p_class][policy_name],\n policy=_policydata.policies[p_class]['policies'][policy_key_name],\n transform_type='Put')\n if not _validateSetting(\n value=_value,\n policy=_policydata.policies[p_class]['policies'][policy_key_name]):\n msg = 'The specified value {0} is not an acceptable setting for policy {1}.'\n raise SaltInvocationError(msg.format(policies[p_class][policy_name], policy_name))\n if 'Registry' in _pol:\n # set value in registry\n log.debug('%s is a registry policy', policy_name)\n _regedits[policy_name] = {'policy': _pol, 'value': _value}\n elif 'Secedit' in _pol:\n # set value with secedit\n log.debug('%s is a Secedit policy', policy_name)\n if _pol['Secedit']['Section'] not in _secedits:\n _secedits[_pol['Secedit']['Section']] = []\n _secedits[_pol['Secedit']['Section']].append(\n ' '.join([_pol['Secedit']['Option'],\n '=', six.text_type(_value)]))\n elif 'NetSH' in _pol:\n # set value with netsh\n log.debug('%s is a NetSH policy', policy_name)\n _netshs.setdefault(policy_name, {\n 'profile': _pol['NetSH']['Profile'],\n 'section': _pol['NetSH']['Section'],\n 'option': _pol['NetSH']['Option'],\n 'value': six.text_type(_value)\n })\n elif 'AdvAudit' in _pol:\n # set value with advaudit\n _advaudits.setdefault(policy_name, {\n 'option': _pol['AdvAudit']['Option'],\n 'value': six.text_type(_value)\n })\n elif 'NetUserModal' in _pol:\n # set value via NetUserModal\n log.debug('%s is a NetUserModal policy', policy_name)\n if _pol['NetUserModal']['Modal'] not in _modal_sets:\n _modal_sets[_pol['NetUserModal']['Modal']] = {}\n _modal_sets[_pol['NetUserModal']['Modal']][_pol['NetUserModal']['Option']] = _value\n elif 'LsaRights' in _pol:\n log.debug('%s is a LsaRights policy', policy_name)\n _lsarights[policy_name] = {'policy': _pol, 'value': _value}\n else:\n _value = policies[p_class][policy_name]\n log.debug('searching for \"%s\" in admx data', policy_name)\n success, the_policy, policy_name_list, msg = _lookup_admin_template(\n policy_name=policy_name,\n policy_class=p_class,\n adml_language=adml_language)\n if success:\n policy_name = the_policy.attrib['name']\n policy_namespace = the_policy.nsmap[the_policy.prefix]\n if policy_namespace not in _admTemplateData:\n _admTemplateData[policy_namespace] = {}\n _admTemplateData[policy_namespace][policy_name] = _value\n else:\n raise SaltInvocationError(msg)\n if policy_namespace and policy_name in _admTemplateData[policy_namespace] and the_policy is not None:\n log.debug('setting == %s', six.text_type(_admTemplateData[policy_namespace][policy_name]).lower())\n log.debug(six.text_type(_admTemplateData[policy_namespace][policy_name]).lower())\n if six.text_type(_admTemplateData[policy_namespace][policy_name]).lower() != 'disabled' \\\n and six.text_type(_admTemplateData[policy_namespace][policy_name]).lower() != 'not configured':\n if ELEMENTS_XPATH(the_policy):\n if isinstance(_admTemplateData[policy_namespace][policy_name], dict):\n for elements_item in ELEMENTS_XPATH(the_policy):\n for child_item in elements_item.getchildren():\n # check each element\n log.debug('checking element %s', child_item.attrib['id'])\n temp_element_name = None\n this_element_name = _getFullPolicyName(\n policy_item=child_item,\n policy_name=child_item.attrib['id'],\n return_full_policy_names=True,\n adml_language=adml_language)\n log.debug('id attribute == \"%s\" this_element_name == \"%s\"', child_item.attrib['id'], this_element_name)\n if this_element_name in _admTemplateData[policy_namespace][policy_name]:\n temp_element_name = this_element_name\n elif child_item.attrib['id'] in _admTemplateData[policy_namespace][policy_name]:\n temp_element_name = child_item.attrib['id']\n else:\n msg = ('Element \"{0}\" must be included'\n ' in the policy configuration for policy {1}')\n raise SaltInvocationError(msg.format(this_element_name, policy_name))\n if 'required' in child_item.attrib \\\n and child_item.attrib['required'].lower() == 'true':\n if not _admTemplateData[policy_namespace][policy_name][temp_element_name]:\n msg = 'Element \"{0}\" requires a value to be specified'\n raise SaltInvocationError(msg.format(temp_element_name))\n if etree.QName(child_item).localname == 'boolean':\n if not isinstance(\n _admTemplateData[policy_namespace][policy_name][temp_element_name],\n bool):\n msg = 'Element {0} requires a boolean True or False'\n raise SaltInvocationError(msg.format(temp_element_name))\n elif etree.QName(child_item).localname == 'decimal' or \\\n etree.QName(child_item).localname == 'longDecimal':\n min_val = 0\n max_val = 9999\n if 'minValue' in child_item.attrib:\n min_val = int(child_item.attrib['minValue'])\n if 'maxValue' in child_item.attrib:\n max_val = int(child_item.attrib['maxValue'])\n if int(_admTemplateData[policy_namespace][policy_name][temp_element_name]) \\\n < min_val or \\\n int(_admTemplateData[policy_namespace][policy_name][temp_element_name]) \\\n > max_val:\n msg = 'Element \"{0}\" value must be between {1} and {2}'\n raise SaltInvocationError(msg.format(temp_element_name,\n min_val,\n max_val))\n elif etree.QName(child_item).localname == 'enum':\n # make sure the value is in the enumeration\n found = False\n for enum_item in child_item.getchildren():\n if _admTemplateData[policy_namespace][policy_name][temp_element_name] == \\\n _getAdmlDisplayName(\n adml_policy_resources,\n enum_item.attrib['displayName']).strip():\n found = True\n break\n if not found:\n msg = 'Element \"{0}\" does not have a valid value'\n raise SaltInvocationError(msg.format(temp_element_name))\n elif etree.QName(child_item).localname == 'list':\n if 'explicitValue' in child_item.attrib \\\n and child_item.attrib['explicitValue'].lower() == \\\n 'true':\n if not isinstance(\n _admTemplateData[policy_namespace][policy_name][temp_element_name],\n dict):\n msg = ('Each list item of element \"{0}\" '\n 'requires a dict value')\n msg = msg.format(temp_element_name)\n raise SaltInvocationError(msg)\n elif not isinstance(\n _admTemplateData[policy_namespace][policy_name][temp_element_name],\n list):\n msg = 'Element \"{0}\" requires a list value'\n msg = msg.format(temp_element_name)\n raise SaltInvocationError(msg)\n elif etree.QName(child_item).localname == 'multiText':\n if not isinstance(\n _admTemplateData[policy_namespace][policy_name][temp_element_name],\n list):\n msg = 'Element \"{0}\" requires a list value'\n msg = msg.format(temp_element_name)\n raise SaltInvocationError(msg)\n _admTemplateData[policy_namespace][policy_name][child_item.attrib['id']] = \\\n _admTemplateData[policy_namespace][policy_name].pop(temp_element_name)\n else:\n msg = 'The policy \"{0}\" has elements which must be configured'\n msg = msg.format(policy_name)\n raise SaltInvocationError(msg)\n else:\n if six.text_type(_admTemplateData[policy_namespace][policy_name]).lower() != 'enabled':\n msg = ('The policy {0} must either be \"Enabled\", '\n '\"Disabled\", or \"Not Configured\"')\n msg = msg.format(policy_name)\n raise SaltInvocationError(msg)\n if _regedits:\n for regedit in _regedits:\n log.debug('%s is a Registry policy', regedit)\n # if the value setting is None or \"(value not set)\", we will delete the value from the registry\n if _regedits[regedit]['value'] is not None and _regedits[regedit]['value'] != '(value not set)':\n _ret = __utils__['reg.set_value'](\n _regedits[regedit]['policy']['Registry']['Hive'],\n _regedits[regedit]['policy']['Registry']['Path'],\n _regedits[regedit]['policy']['Registry']['Value'],\n _regedits[regedit]['value'],\n _regedits[regedit]['policy']['Registry']['Type'])\n else:\n _ret = __utils__['reg.read_value'](\n _regedits[regedit]['policy']['Registry']['Hive'],\n _regedits[regedit]['policy']['Registry']['Path'],\n _regedits[regedit]['policy']['Registry']['Value'])\n if _ret['success'] and _ret['vdata'] != '(value not set)':\n _ret = __utils__['reg.delete_value'](\n _regedits[regedit]['policy']['Registry']['Hive'],\n _regedits[regedit]['policy']['Registry']['Path'],\n _regedits[regedit]['policy']['Registry']['Value'])\n if not _ret:\n msg = ('Error while attempting to set policy {0} via the registry.'\n ' Some changes may not be applied as expected')\n raise CommandExecutionError(msg.format(regedit))\n if _lsarights:\n for lsaright in _lsarights:\n _existingUsers = None\n if not cumulative_rights_assignments:\n _existingUsers = _getRightsAssignments(\n _lsarights[lsaright]['policy']['LsaRights']['Option'])\n if _lsarights[lsaright]['value']:\n for acct in _lsarights[lsaright]['value']:\n _ret = _addAccountRights(acct, _lsarights[lsaright]['policy']['LsaRights']['Option'])\n if not _ret:\n msg = 'An error occurred attempting to configure the user right {0}.'\n raise SaltInvocationError(msg.format(lsaright))\n if _existingUsers:\n for acct in _existingUsers:\n if acct not in _lsarights[lsaright]['value']:\n _ret = _delAccountRights(\n acct, _lsarights[lsaright]['policy']['LsaRights']['Option'])\n if not _ret:\n msg = ('An error occurred attempting to remove previously'\n 'configured users with right {0}.')\n raise SaltInvocationError(msg.format(lsaright))\n if _secedits:\n # we've got secedits to make\n log.debug(_secedits)\n ini_data = '\\r\\n'.join(['[Unicode]', 'Unicode=yes'])\n _seceditSections = ['System Access', 'Event Audit', 'Registry Values', 'Privilege Rights']\n for _seceditSection in _seceditSections:\n if _seceditSection in _secedits:\n ini_data = '\\r\\n'.join([ini_data, ''.join(['[', _seceditSection, ']']),\n '\\r\\n'.join(_secedits[_seceditSection])])\n ini_data = '\\r\\n'.join([ini_data, '[Version]',\n 'signature=\"$CHICAGO$\"',\n 'Revision=1'])\n log.debug('ini_data == %s', ini_data)\n if not _write_secedit_data(ini_data):\n msg = ('Error while attempting to set policies via '\n 'secedit. Some changes may not be applied as '\n 'expected')\n raise CommandExecutionError(msg)\n if _netshs:\n # we've got netsh settings to make\n for setting in _netshs:\n log.debug('Setting firewall policy: %s', setting)\n log.debug(_netshs[setting])\n _setOptionValueNetSH(**_netshs[setting])\n\n if _advaudits:\n # We've got AdvAudit settings to make\n for setting in _advaudits:\n log.debug('Setting Advanced Audit policy: {0}'.format(setting))\n log.debug(_advaudits[setting])\n _setOptionValueAdvAudit(**_advaudits[setting])\n\n if _modal_sets:\n # we've got modalsets to make\n log.debug(_modal_sets)\n for _modal_set in _modal_sets:\n try:\n _existingModalData = win32net.NetUserModalsGet(None, _modal_set)\n _newModalSetData = dictupdate.update(_existingModalData, _modal_sets[_modal_set])\n log.debug('NEW MODAL SET = %s', _newModalSetData)\n _ret = win32net.NetUserModalsSet(None, _modal_set, _newModalSetData)\n # TODO: This needs to be more specific\n except Exception:\n msg = 'An unhandled exception occurred while attempting to set policy via NetUserModalSet'\n log.exception(msg)\n raise CommandExecutionError(msg)\n if _admTemplateData:\n _ret = False\n log.debug('going to write some adm template data :: %s', _admTemplateData)\n _ret = _writeAdminTemplateRegPolFile(_admTemplateData,\n adml_language=adml_language,\n registry_class=p_class)\n if not _ret:\n msg = ('Error while attempting to write Administrative Template Policy data.'\n ' Some changes may not be applied as expected')\n raise CommandExecutionError(msg)\n return True\n else:\n msg = 'You have to specify something!'\n raise SaltInvocationError(msg)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _seconds_to_days(cls, val, **kwargs):\n '''\n converts a number of seconds to days\n '''\n zero_value = kwargs.get('zero_value', 0)\n if val is not None:\n if val == zero_value:\n return 0\n return val / 86400\n else:\n return 'Not Defined'", "response": "converts a number of seconds to days"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _days_to_seconds(cls, val, **kwargs):\n '''\n converts a number of days to seconds\n '''\n zero_value = kwargs.get('zero_value', 0)\n if val is not None:\n if val == 0:\n return zero_value\n return val * 86400\n else:\n return 'Not Defined'", "response": "converts a number of days to seconds"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nconvert a binary 0 / 1 to Disabled or Enabled.", "response": "def _binary_enable_zero_disable_one_conversion(cls, val, **kwargs):\n '''\n converts a binary 0/1 to Disabled/Enabled\n '''\n try:\n if val is not None:\n if ord(val) == 0:\n return 'Disabled'\n elif ord(val) == 1:\n return 'Enabled'\n else:\n return 'Invalid Value: {0!r}'.format(val) # pylint: disable=repr-flag-used-in-string\n else:\n return 'Not Defined'\n except TypeError:\n return 'Invalid Value'"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _binary_enable_zero_disable_one_reverse_conversion(cls, val, **kwargs):\n '''\n converts Enabled/Disabled to unicode char to write to a REG_BINARY value\n '''\n if val is not None:\n if val.upper() == 'DISABLED':\n return chr(0)\n elif val.upper() == 'ENABLED':\n return chr(1)\n else:\n return None\n else:\n return None", "response": "Convert Enabled to unicode char to write to a REG_BINARY value."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _dasd_conversion(cls, val, **kwargs):\n '''\n converts 0/1/2 for dasd reg key\n '''\n if val is not None:\n if val == '0' or val == 0 or val == '':\n return 'Administrators'\n elif val == '1' or val == 1:\n return 'Administrators and Power Users'\n elif val == '2' or val == 2:\n return 'Administrators and Interactive Users'\n else:\n return 'Not Defined'\n else:\n return 'Not Defined'", "response": "Convert a string value to a string for dasd reg key."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconverts DASD String values to the reg_sz value.", "response": "def _dasd_reverse_conversion(cls, val, **kwargs):\n '''\n converts DASD String values to the reg_sz value\n '''\n if val is not None:\n if val.upper() == 'ADMINISTRATORS':\n # \"\" also shows 'administrators' in the GUI\n return '0'\n elif val.upper() == 'ADMINISTRATORS AND POWER USERS':\n return '1'\n elif val.upper() == 'ADMINISTRATORS AND INTERACTIVE USERS':\n return '2'\n elif val.upper() == 'NOT DEFINED':\n # a setting of anything other than nothing, 0, 1, 2 or if it\n # doesn't exist show 'not defined'\n return '9999'\n else:\n return 'Invalid Value'\n else:\n return 'Not Defined'"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _in_range_inclusive(cls, val, **kwargs):\n '''\n checks that a value is in an inclusive range\n The value for 0 used by Max Password Age is actually 0xffffffff\n '''\n minimum = kwargs.get('min', 0)\n maximum = kwargs.get('max', 1)\n zero_value = kwargs.get('zero_value', 0)\n\n if isinstance(val, six.string_types):\n if val.lower() == 'not defined':\n return True\n else:\n try:\n val = int(val)\n except ValueError:\n return False\n if val is not None:\n if minimum <= val <= maximum or val == zero_value:\n return True\n else:\n return False\n else:\n return False", "response": "checks that a value is in an inclusive range\nAttributeNames"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nconvert the binary value in the registry for driver signing into the correct string representation", "response": "def _driver_signing_reg_conversion(cls, val, **kwargs):\n '''\n converts the binary value in the registry for driver signing into the\n correct string representation\n '''\n log.debug('we have %s for the driver signing value', val)\n if val is not None:\n # since this is from secedit, it should be 3,<value>\n _val = val.split(',')\n if len(_val) == 2:\n if _val[1] == '0':\n return 'Silently Succeed'\n elif _val[1] == '1':\n return 'Warn but allow installation'\n elif _val[1] == '2':\n return 'Do not allow installation'\n elif _val[1] == 'Not Defined':\n return 'Not Defined'\n else:\n return 'Invalid Value'\n else:\n return 'Not Defined'\n else:\n return 'Not Defined'"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nconverting the string value seen in the GUI to the correct registry value for secedit", "response": "def _driver_signing_reg_reverse_conversion(cls, val, **kwargs):\n '''\n converts the string value seen in the GUI to the correct registry value\n for secedit\n '''\n if val is not None:\n if val.upper() == 'SILENTLY SUCCEED':\n return ','.join(['3', '0'])\n elif val.upper() == 'WARN BUT ALLOW INSTALLATION':\n return ','.join(['3', chr(1)])\n elif val.upper() == 'DO NOT ALLOW INSTALLATION':\n return ','.join(['3', chr(2)])\n else:\n return 'Invalid Value'\n else:\n return 'Not Defined'"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _sidConversion(cls, val, **kwargs):\n '''\n converts a list of pysid objects to string representations\n '''\n if isinstance(val, six.string_types):\n val = val.split(',')\n usernames = []\n for _sid in val:\n try:\n userSid = win32security.LookupAccountSid('', _sid)\n if userSid[1]:\n userSid = '{1}\\\\{0}'.format(userSid[0], userSid[1])\n else:\n userSid = '{0}'.format(userSid[0])\n # TODO: This needs to be more specific\n except Exception:\n userSid = win32security.ConvertSidToStringSid(_sid)\n log.warning('Unable to convert SID \"%s\" to a friendly name. The SID will be disaplayed instead of a user/group name.', userSid)\n usernames.append(userSid)\n return usernames", "response": "Convert a list of pysid objects to string representations\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _usernamesToSidObjects(cls, val, **kwargs):\n '''\n converts a list of usernames to sid objects\n '''\n if not val:\n return val\n if isinstance(val, six.string_types):\n val = val.split(',')\n sids = []\n for _user in val:\n try:\n sid = win32security.LookupAccountName('', _user)[0]\n sids.append(sid)\n # This needs to be more specific\n except Exception as e:\n log.exception('Handle this explicitly')\n raise CommandExecutionError((\n 'There was an error obtaining the SID of user \"{0}\". Error '\n 'returned: {1}'\n ).format(_user, e))\n return sids", "response": "Convert a list of usernames to sid objects"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _powershell_script_order_conversion(cls, val, **kwargs):\n '''\n converts true/false/None to the GUI representation of the powershell\n startup/shutdown script order\n '''\n log.debug('script order value = %s', val)\n if val is None or val == 'None':\n return 'Not Configured'\n elif val == 'true':\n return 'Run Windows PowerShell scripts first'\n elif val == 'false':\n return 'Run Windows PowerShell scripts last'\n else:\n return 'Invalid Value'", "response": "Convert a string value to the GUI representation of the powershell reservation script order."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _powershell_script_order_reverse_conversion(cls, val, **kwargs):\n '''\n converts powershell script GUI strings representations to\n True/False/None\n '''\n if val.upper() == 'Run Windows PowerShell scripts first'.upper():\n return 'true'\n elif val.upper() == 'Run Windows PowerShell scripts last'.upper():\n return 'false'\n elif val is 'Not Configured':\n return None\n else:\n return 'Invalid Value'", "response": "Convert powershell script GUI strings representations to True False and None to return True False or None to return False or None to return None"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _dict_lookup(cls, item, **kwargs):\n '''\n Retrieves the key or value from a dict based on the item\n kwarg lookup dict to search for item\n kwarg value_lookup bool to determine if item should be compared to keys\n or values\n '''\n log.debug('item == %s', item)\n value_lookup = kwargs.get('value_lookup', False)\n if 'lookup' in kwargs:\n for k, v in six.iteritems(kwargs['lookup']):\n if value_lookup:\n if six.text_type(v).lower() == six.text_type(item).lower():\n log.debug('returning key %s', k)\n return k\n else:\n if six.text_type(k).lower() == six.text_type(item).lower():\n log.debug('returning value %s', v)\n return v\n return 'Invalid Value'", "response": "Returns the key or value of the item in a dict based on the item\n kwarg lookup dict to search for the item\nCOOKIEID kwarg value_lookup bool to determine if the item should be compared to keys\nCOOKIEID or values\nCOOKIEID"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _multi_string_put_transform(cls, item, **kwargs):\n '''\n transform for a REG_MULTI_SZ to properly handle \"Not Defined\"\n '''\n if isinstance(item, list):\n return item\n elif isinstance(item, six.string_types):\n if item.lower() == 'not defined':\n return None\n else:\n return item.split(',')\n else:\n return 'Invalid Value'", "response": "transform for a REG_MULTI_SZ to properly handle Not Defined"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nconverts a snapshot response to a dictionary.", "response": "def _snapshot_to_data(snapshot):\n '''\n Returns snapshot data from a D-Bus response.\n\n A snapshot D-Bus response is a dbus.Struct containing the\n information related to a snapshot:\n\n [id, type, pre_snapshot, timestamp, user, description,\n cleanup_algorithm, userdata]\n\n id: dbus.UInt32\n type: dbus.UInt16\n pre_snapshot: dbus.UInt32\n timestamp: dbus.Int64\n user: dbus.UInt32\n description: dbus.String\n cleaup_algorithm: dbus.String\n userdata: dbus.Dictionary\n '''\n data = {}\n\n data['id'] = snapshot[0]\n data['type'] = ['single', 'pre', 'post'][snapshot[1]]\n if data['type'] == 'post':\n data['pre'] = snapshot[2]\n\n if snapshot[3] != -1:\n data['timestamp'] = snapshot[3]\n else:\n data['timestamp'] = int(time.time())\n\n data['user'] = getpwuid(snapshot[4])[0]\n data['description'] = snapshot[5]\n data['cleanup'] = snapshot[6]\n\n data['userdata'] = {}\n for key, value in snapshot[7].items():\n data['userdata'][key] = value\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _dbus_exception_to_reason(exc, args):\n '''\n Returns a error message from a snapper DBusException\n '''\n error = exc.get_dbus_name()\n if error == 'error.unknown_config':\n return \"Unknown configuration '{0}'\".format(args['config'])\n elif error == 'error.illegal_snapshot':\n return 'Invalid snapshot'\n else:\n return exc.get_dbus_name()", "response": "Returns a message from a snapper DBusException"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nlists available snapshots in a node", "response": "def list_snapshots(config='root'):\n '''\n List available snapshots\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' snapper.list_snapshots config=myconfig\n '''\n try:\n snapshots = snapper.ListSnapshots(config)\n return [_snapshot_to_data(s) for s in snapshots]\n except dbus.DBusException as exc:\n raise CommandExecutionError(\n 'Error encountered while listing snapshots: {0}'\n .format(_dbus_exception_to_reason(exc, locals()))\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef get_snapshot(number=0, config='root'):\n '''\n Get detailed information about a given snapshot\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' snapper.get_snapshot 1\n '''\n try:\n snapshot = snapper.GetSnapshot(config, int(number))\n return _snapshot_to_data(snapshot)\n except dbus.DBusException as exc:\n raise CommandExecutionError(\n 'Error encountered while retrieving snapshot: {0}'\n .format(_dbus_exception_to_reason(exc, locals()))\n )", "response": "Get detailed information about a given snapshot"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_configs():\n '''\n List all available configs\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' snapper.list_configs\n '''\n try:\n configs = snapper.ListConfigs()\n return dict((config[0], config[2]) for config in configs)\n except dbus.DBusException as exc:\n raise CommandExecutionError(\n 'Error encountered while listing configurations: {0}'\n .format(_dbus_exception_to_reason(exc, locals()))\n )", "response": "List all available configs"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef set_config(name='root', **kwargs):\n '''\n Set configuration values\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' snapper.set_config SYNC_ACL=True\n\n Keys are case insensitive as they will be always uppercased to snapper\n convention. The above example is equivalent to:\n\n .. code-block:: bash\n\n salt '*' snapper.set_config sync_acl=True\n '''\n try:\n data = dict((k.upper(), _config_filter(v)) for k, v in\n kwargs.items() if not k.startswith('__'))\n snapper.SetConfig(name, data)\n except dbus.DBusException as exc:\n raise CommandExecutionError(\n 'Error encountered while setting configuration {0}: {1}'\n .format(name, _dbus_exception_to_reason(exc, locals()))\n )\n return True", "response": "Set the configuration values for the current user."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_last_snapshot(config='root'):\n '''\n Returns the last existing created snapshot\n '''\n snapshot_list = sorted(list_snapshots(config), key=lambda x: x['id'])\n return snapshot_list[-1]", "response": "Returns the last existing snapshot in the availabe node"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nconverts a numeric dbus snapper status into a string", "response": "def status_to_string(dbus_status):\n '''\n Converts a numeric dbus snapper status into a string\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' snapper.status_to_string <dbus_status>\n '''\n status_tuple = (\n dbus_status & 0b000000001, dbus_status & 0b000000010, dbus_status & 0b000000100,\n dbus_status & 0b000001000, dbus_status & 0b000010000, dbus_status & 0b000100000,\n dbus_status & 0b001000000, dbus_status & 0b010000000, dbus_status & 0b100000000\n )\n\n return [DBUS_STATUS_MAP[status] for status in status_tuple if status]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_config(name='root'):\n '''\n Retrieves all values from a given configuration\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' snapper.get_config\n '''\n try:\n config = snapper.GetConfig(name)\n return config\n except dbus.DBusException as exc:\n raise CommandExecutionError(\n 'Error encountered while retrieving configuration: {0}'\n .format(_dbus_exception_to_reason(exc, locals()))\n )", "response": "Retrieves all values from a given configuration"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef create_config(name=None,\n subvolume=None,\n fstype=None,\n template=None,\n extra_opts=None):\n '''\n Creates a new Snapper configuration\n\n name\n Name of the new Snapper configuration.\n subvolume\n Path to the related subvolume.\n fstype\n Filesystem type of the subvolume.\n template\n Configuration template to use. (Default: default)\n extra_opts\n Extra Snapper configuration opts dictionary. It will override the values provided\n by the given template (if any).\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' snapper.create_config name=myconfig subvolume=/foo/bar/ fstype=btrfs\n salt '*' snapper.create_config name=myconfig subvolume=/foo/bar/ fstype=btrfs template=\"default\"\n salt '*' snapper.create_config name=myconfig subvolume=/foo/bar/ fstype=btrfs extra_opts='{\"NUMBER_CLEANUP\": False}'\n '''\n def raise_arg_error(argname):\n raise CommandExecutionError(\n 'You must provide a \"{0}\" for the new configuration'.format(argname)\n )\n\n if not name:\n raise_arg_error(\"name\")\n if not subvolume:\n raise_arg_error(\"subvolume\")\n if not fstype:\n raise_arg_error(\"fstype\")\n if not template:\n template = \"\"\n\n try:\n snapper.CreateConfig(name, subvolume, fstype, template)\n if extra_opts:\n set_config(name, **extra_opts)\n return get_config(name)\n except dbus.DBusException as exc:\n raise CommandExecutionError(\n 'Error encountered while creating the new configuration: {0}'\n .format(_dbus_exception_to_reason(exc, locals()))\n )", "response": "Creates a new configuration for a specific subvolume."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a new snapshot in the salt - job system.", "response": "def create_snapshot(config='root', snapshot_type='single', pre_number=None,\n description=None, cleanup_algorithm='number', userdata=None,\n **kwargs):\n '''\n Creates an snapshot\n\n config\n Configuration name.\n snapshot_type\n Specifies the type of the new snapshot. Possible values are\n single, pre and post.\n pre_number\n For post snapshots the number of the pre snapshot must be\n provided.\n description\n Description for the snapshot. If not given, the salt job will be used.\n cleanup_algorithm\n Set the cleanup algorithm for the snapshot.\n\n number\n Deletes old snapshots when a certain number of snapshots\n is reached.\n timeline\n Deletes old snapshots but keeps a number of hourly,\n daily, weekly, monthly and yearly snapshots.\n empty-pre-post\n Deletes pre/post snapshot pairs with empty diffs.\n userdata\n Set userdata for the snapshot (key-value pairs).\n\n Returns the number of the created snapshot.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' snapper.create_snapshot\n '''\n if not userdata:\n userdata = {}\n\n jid = kwargs.get('__pub_jid')\n if description is None and jid is not None:\n description = 'salt job {0}'.format(jid)\n\n if jid is not None:\n userdata['salt_jid'] = jid\n\n new_nr = None\n try:\n if snapshot_type == 'single':\n new_nr = snapper.CreateSingleSnapshot(config, description,\n cleanup_algorithm, userdata)\n elif snapshot_type == 'pre':\n new_nr = snapper.CreatePreSnapshot(config, description,\n cleanup_algorithm, userdata)\n elif snapshot_type == 'post':\n if pre_number is None:\n raise CommandExecutionError(\n \"pre snapshot number 'pre_number' needs to be\"\n \"specified for snapshots of the 'post' type\")\n new_nr = snapper.CreatePostSnapshot(config, pre_number, description,\n cleanup_algorithm, userdata)\n else:\n raise CommandExecutionError(\n \"Invalid snapshot type '{0}'\".format(snapshot_type))\n except dbus.DBusException as exc:\n raise CommandExecutionError(\n 'Error encountered while listing changed files: {0}'\n .format(_dbus_exception_to_reason(exc, locals()))\n )\n return new_nr"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ndeleting a snapshot from the specified list of snapshots.", "response": "def delete_snapshot(snapshots_ids=None, config=\"root\"):\n '''\n Deletes an snapshot\n\n config\n Configuration name. (Default: root)\n\n snapshots_ids\n List of the snapshots IDs to be deleted.\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' snapper.delete_snapshot 54\n salt '*' snapper.delete_snapshot config=root 54\n salt '*' snapper.delete_snapshot config=root snapshots_ids=[54,55,56]\n '''\n if not snapshots_ids:\n raise CommandExecutionError('Error: No snapshot ID has been provided')\n try:\n current_snapshots_ids = [x['id'] for x in list_snapshots(config)]\n if not isinstance(snapshots_ids, list):\n snapshots_ids = [snapshots_ids]\n if not set(snapshots_ids).issubset(set(current_snapshots_ids)):\n raise CommandExecutionError(\n \"Error: Snapshots '{0}' not found\".format(\", \".join(\n [six.text_type(x) for x in set(snapshots_ids).difference(\n set(current_snapshots_ids))]))\n )\n snapper.DeleteSnapshots(config, snapshots_ids)\n return {config: {\"ids\": snapshots_ids, \"status\": \"deleted\"}}\n except dbus.DBusException as exc:\n raise CommandExecutionError(_dbus_exception_to_reason(exc, locals()))"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef modify_snapshot(snapshot_id=None,\n description=None,\n userdata=None,\n cleanup=None,\n config=\"root\"):\n '''\n Modify attributes of an existing snapshot.\n\n config\n Configuration name. (Default: root)\n\n snapshot_id\n ID of the snapshot to be modified.\n\n cleanup\n Change the cleanup method of the snapshot. (str)\n\n description\n Change the description of the snapshot. (str)\n\n userdata\n Change the userdata dictionary of the snapshot. (dict)\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' snapper.modify_snapshot 54 description=\"my snapshot description\"\n salt '*' snapper.modify_snapshot 54 description=\"my snapshot description\"\n salt '*' snapper.modify_snapshot 54 userdata='{\"foo\": \"bar\"}'\n salt '*' snapper.modify_snapshot snapshot_id=54 cleanup=\"number\"\n '''\n if not snapshot_id:\n raise CommandExecutionError('Error: No snapshot ID has been provided')\n\n snapshot = get_snapshot(config=config, number=snapshot_id)\n try:\n # Updating only the explicitly provided attributes by the user\n updated_opts = {\n 'description': description if description is not None else snapshot['description'],\n 'cleanup': cleanup if cleanup is not None else snapshot['cleanup'],\n 'userdata': userdata if userdata is not None else snapshot['userdata'],\n }\n snapper.SetSnapshot(config,\n snapshot_id,\n updated_opts['description'],\n updated_opts['cleanup'],\n updated_opts['userdata'])\n return get_snapshot(config=config, number=snapshot_id)\n except dbus.DBusException as exc:\n raise CommandExecutionError(_dbus_exception_to_reason(exc, locals()))", "response": "Modify attributes of an existing snapshot."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn numerical interval based on optionals num_pre num_post values", "response": "def _get_num_interval(config, num_pre, num_post):\n '''\n Returns numerical interval based on optionals num_pre, num_post values\n '''\n post = int(num_post) if num_post else 0\n pre = int(num_pre) if num_pre is not None else _get_last_snapshot(config)['id']\n return pre, post"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _is_text_file(filename):\n '''\n Checks if a file is a text file\n '''\n type_of_file = os.popen('file -bi {0}'.format(filename), 'r').read()\n return type_of_file.startswith('text')", "response": "Checks if a file is a text file"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nruns a function from an execution module creating pre and post snapshots and associates the salt job id with those snapshots.", "response": "def run(function, *args, **kwargs):\n '''\n Runs a function from an execution module creating pre and post snapshots\n and associating the salt job id with those snapshots for easy undo and\n cleanup.\n\n function\n Salt function to call.\n\n config\n Configuration name. (default: \"root\")\n\n description\n A description for the snapshots. (default: None)\n\n userdata\n Data to include in the snapshot metadata. (default: None)\n\n cleanup_algorithm\n Snapper cleanup algorithm. (default: \"number\")\n\n `*args`\n args for the function to call. (default: None)\n\n `**kwargs`\n kwargs for the function to call (default: None)\n\n This would run append text to /etc/motd using the file.append\n module, and will create two snapshots, pre and post with the associated\n metadata. The jid will be available as salt_jid in the userdata of the\n snapshot.\n\n You can immediately see the changes\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' snapper.run file.append args='[\"/etc/motd\", \"some text\"]'\n '''\n config = kwargs.pop(\"config\", \"root\")\n description = kwargs.pop(\"description\", \"snapper.run[{0}]\".format(function))\n cleanup_algorithm = kwargs.pop(\"cleanup_algorithm\", \"number\")\n userdata = kwargs.pop(\"userdata\", {})\n\n func_kwargs = dict((k, v) for k, v in kwargs.items() if not k.startswith('__'))\n kwargs = dict((k, v) for k, v in kwargs.items() if k.startswith('__'))\n\n pre_nr = __salt__['snapper.create_snapshot'](\n config=config,\n snapshot_type='pre',\n description=description,\n cleanup_algorithm=cleanup_algorithm,\n userdata=userdata,\n **kwargs)\n\n if function not in __salt__:\n raise CommandExecutionError(\n 'function \"{0}\" does not exist'.format(function)\n )\n\n try:\n ret = __salt__[function](*args, **func_kwargs)\n except CommandExecutionError as exc:\n ret = \"\\n\".join([six.text_type(exc), __salt__[function].__doc__])\n\n __salt__['snapper.create_snapshot'](\n config=config,\n snapshot_type='post',\n pre_number=pre_nr,\n description=description,\n cleanup_algorithm=cleanup_algorithm,\n userdata=userdata,\n **kwargs)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nreturning a comparison between two snapshots AttributeNames", "response": "def status(config='root', num_pre=None, num_post=None):\n '''\n Returns a comparison between two snapshots\n\n config\n Configuration name.\n\n num_pre\n first snapshot ID to compare. Default is last snapshot\n\n num_post\n last snapshot ID to compare. Default is 0 (current state)\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' snapper.status\n salt '*' snapper.status num_pre=19 num_post=20\n '''\n try:\n pre, post = _get_num_interval(config, num_pre, num_post)\n snapper.CreateComparison(config, int(pre), int(post))\n files = snapper.GetFiles(config, int(pre), int(post))\n status_ret = {}\n SUBVOLUME = list_configs()[config]['SUBVOLUME']\n for file in files:\n # In case of SUBVOLUME is included in filepath we remove it\n # to prevent from filepath starting with double '/'\n _filepath = file[0][len(SUBVOLUME):] if file[0].startswith(SUBVOLUME) else file[0]\n status_ret[os.path.normpath(SUBVOLUME + _filepath)] = {'status': status_to_string(file[1])}\n return status_ret\n except dbus.DBusException as exc:\n raise CommandExecutionError(\n 'Error encountered while listing changed files: {0}'\n .format(_dbus_exception_to_reason(exc, locals()))\n )"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef undo(config='root', files=None, num_pre=None, num_post=None):\n '''\n Undo all file changes that happened between num_pre and num_post, leaving\n the files into the state of num_pre.\n\n .. warning::\n If one of the files has changes after num_post, they will be overwritten\n The snapshots are used to determine the file list, but the current\n version of the files will be overwritten by the versions in num_pre.\n\n You to undo changes between num_pre and the current version of the\n files use num_post=0.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' snapper.undo\n '''\n pre, post = _get_num_interval(config, num_pre, num_post)\n\n changes = status(config, pre, post)\n changed = set(changes.keys())\n requested = set(files or changed)\n\n if not requested.issubset(changed):\n raise CommandExecutionError(\n 'Given file list contains files that are not present'\n 'in the changed filelist: {0}'.format(changed - requested))\n\n cmdret = __salt__['cmd.run']('snapper -c {0} undochange {1}..{2} {3}'.format(\n config, pre, post, ' '.join(requested)))\n\n try:\n components = cmdret.split(' ')\n ret = {}\n for comp in components:\n key, val = comp.split(':')\n ret[key] = val\n return ret\n except ValueError as exc:\n raise CommandExecutionError(\n 'Error while processing Snapper response: {0}'.format(cmdret))", "response": "Undo all file changes between num_pre and num_post."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _get_jid_snapshots(jid, config='root'):\n '''\n Returns pre/post snapshots made by a given Salt jid\n\n Looks for 'salt_jid' entries into snapshots userdata which are created\n when 'snapper.run' is executed.\n '''\n jid_snapshots = [x for x in list_snapshots(config) if x['userdata'].get(\"salt_jid\") == jid]\n pre_snapshot = [x for x in jid_snapshots if x['type'] == \"pre\"]\n post_snapshot = [x for x in jid_snapshots if x['type'] == \"post\"]\n\n if not pre_snapshot or not post_snapshot:\n raise CommandExecutionError(\"Jid '{0}' snapshots not found\".format(jid))\n\n return (\n pre_snapshot[0]['id'],\n post_snapshot[0]['id']\n )", "response": "Returns pre and post snapshots made by a given jid"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nundo the changes applied by a salt job jid", "response": "def undo_jid(jid, config='root'):\n '''\n Undo the changes applied by a salt job\n\n jid\n The job id to lookup\n\n config\n Configuration name.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' snapper.undo_jid jid=20160607130930720112\n '''\n pre_snapshot, post_snapshot = _get_jid_snapshots(jid, config=config)\n return undo(config, num_pre=pre_snapshot, num_post=post_snapshot)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the differences between two snapshots for a specific configuration name.", "response": "def diff(config='root', filename=None, num_pre=None, num_post=None):\n '''\n Returns the differences between two snapshots\n\n config\n Configuration name.\n\n filename\n if not provided the showing differences between snapshots for\n all \"text\" files\n\n num_pre\n first snapshot ID to compare. Default is last snapshot\n\n num_post\n last snapshot ID to compare. Default is 0 (current state)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' snapper.diff\n salt '*' snapper.diff filename=/var/log/snapper.log num_pre=19 num_post=20\n '''\n try:\n pre, post = _get_num_interval(config, num_pre, num_post)\n\n files = changed_files(config, pre, post)\n if filename:\n files = [filename] if filename in files else []\n\n SUBVOLUME = list_configs()[config]['SUBVOLUME']\n pre_mount = snapper.MountSnapshot(config, pre, False) if pre else SUBVOLUME\n post_mount = snapper.MountSnapshot(config, post, False) if post else SUBVOLUME\n\n files_diff = dict()\n for filepath in [filepath for filepath in files if not os.path.isdir(filepath)]:\n\n _filepath = filepath\n if filepath.startswith(SUBVOLUME):\n _filepath = filepath[len(SUBVOLUME):]\n\n # Just in case, removing possible double '/' from the final file paths\n pre_file = os.path.normpath(pre_mount + \"/\" + _filepath).replace(\"//\", \"/\")\n post_file = os.path.normpath(post_mount + \"/\" + _filepath).replace(\"//\", \"/\")\n\n if os.path.isfile(pre_file):\n pre_file_exists = True\n with salt.utils.files.fopen(pre_file) as rfh:\n pre_file_content = [salt.utils.stringutils.to_unicode(_l)\n for _l in rfh.readlines()]\n else:\n pre_file_content = []\n pre_file_exists = False\n\n if os.path.isfile(post_file):\n post_file_exists = True\n with salt.utils.files.fopen(post_file) as rfh:\n post_file_content = [salt.utils.stringutils.to_unicode(_l)\n for _l in rfh.readlines()]\n else:\n post_file_content = []\n post_file_exists = False\n\n if _is_text_file(pre_file) or _is_text_file(post_file):\n files_diff[filepath] = {\n 'comment': \"text file changed\",\n 'diff': ''.join(difflib.unified_diff(pre_file_content,\n post_file_content,\n fromfile=pre_file,\n tofile=post_file))}\n\n if pre_file_exists and not post_file_exists:\n files_diff[filepath]['comment'] = \"text file deleted\"\n if not pre_file_exists and post_file_exists:\n files_diff[filepath]['comment'] = \"text file created\"\n\n elif not _is_text_file(pre_file) and not _is_text_file(post_file):\n # This is a binary file\n files_diff[filepath] = {'comment': \"binary file changed\"}\n if pre_file_exists:\n files_diff[filepath]['old_sha256_digest'] = __salt__['hashutil.sha256_digest'](''.join(pre_file_content))\n if post_file_exists:\n files_diff[filepath]['new_sha256_digest'] = __salt__['hashutil.sha256_digest'](''.join(post_file_content))\n if post_file_exists and not pre_file_exists:\n files_diff[filepath]['comment'] = \"binary file created\"\n if pre_file_exists and not post_file_exists:\n files_diff[filepath]['comment'] = \"binary file deleted\"\n\n if pre:\n snapper.UmountSnapshot(config, pre, False)\n if post:\n snapper.UmountSnapshot(config, post, False)\n return files_diff\n except dbus.DBusException as exc:\n raise CommandExecutionError(\n 'Error encountered while showing differences between snapshots: {0}'\n .format(_dbus_exception_to_reason(exc, locals()))\n )"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef diff_jid(jid, config='root'):\n '''\n Returns the changes applied by a `jid`\n\n jid\n The job id to lookup\n\n config\n Configuration name.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' snapper.diff_jid jid=20160607130930720112\n '''\n pre_snapshot, post_snapshot = _get_jid_snapshots(jid, config=config)\n return diff(config, num_pre=pre_snapshot, num_post=post_snapshot)", "response": "Returns the changes applied by a jid"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ncreates a baseline snapshot", "response": "def create_baseline(tag=\"baseline\", config='root'):\n '''\n Creates a snapshot marked as baseline\n\n tag\n Tag name for the baseline\n\n config\n Configuration name.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' snapper.create_baseline\n salt '*' snapper.create_baseline my_custom_baseline\n '''\n return __salt__['snapper.create_snapshot'](config=config,\n snapshot_type='single',\n description=\"baseline snapshot\",\n cleanup_algorithm=\"number\",\n userdata={\"baseline_tag\": tag})"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef setenv(name,\n value,\n false_unsets=False,\n clear_all=False,\n update_minion=False,\n permanent=False):\n '''\n Set the salt process environment variables.\n\n name\n The environment key to set. Must be a string.\n\n value\n Either a string or dict. When string, it will be the value\n set for the environment key of 'name' above.\n When a dict, each key/value pair represents an environment\n variable to set.\n\n false_unsets\n If a key's value is False and false_unsets is True, then the\n key will be removed from the salt processes environment dict\n entirely. If a key's value is False and false_unsets is not\n True, then the key's value will be set to an empty string.\n Default: False\n\n clear_all\n USE WITH CAUTION! This option can unset environment variables\n needed for salt to function properly.\n If clear_all is True, then any environment variables not\n defined in the environ dict will be deleted.\n Default: False\n\n update_minion\n If True, apply these environ changes to the main salt-minion\n process. If False, the environ changes will only affect the\n current salt subprocess.\n Default: False\n\n permanent\n On Windows minions this will set the environment variable in the\n registry so that it is always added as a environment variable when\n applications open. If you want to set the variable to HKLM instead of\n HKCU just pass in \"HKLM\" for this parameter. On all other minion types\n this will be ignored. Note: This will only take affect on applications\n opened after this has been set.\n\n Example:\n\n .. code-block:: yaml\n\n a_string_env:\n environ.setenv:\n - name: foo\n - value: bar\n - update_minion: True\n\n a_dict_env:\n environ.setenv:\n - name: does_not_matter\n - value:\n foo: bar\n baz: quux\n '''\n\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n environ = {}\n if isinstance(value, six.string_types) or value is False:\n environ[name] = value\n elif isinstance(value, dict):\n environ = value\n else:\n ret['result'] = False\n ret['comment'] = 'Environ value must be string, dict or False'\n return ret\n\n if clear_all is True:\n # Any keys not in 'environ' dict supplied by user will be unset\n to_unset = [key for key in os.environ if key not in environ]\n for key in to_unset:\n if false_unsets is not True:\n # This key value will change to ''\n ret['changes'].update({key: ''})\n else:\n # We're going to delete the key\n ret['changes'].update({key: None})\n\n current_environ = dict(os.environ)\n already_set = []\n for key, val in six.iteritems(environ):\n if val is False:\n # We unset this key from the environment if\n # false_unsets is True. Otherwise we want to set\n # the value to ''\n def key_exists():\n if salt.utils.platform.is_windows():\n permanent_hive = 'HKCU'\n permanent_key = 'Environment'\n if permanent == 'HKLM':\n permanent_hive = 'HKLM'\n permanent_key = r'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment'\n\n out = __utils__['reg.read_value'](permanent_hive, permanent_key, _norm_key(key))\n return out['success'] is True\n else:\n return False\n if current_environ.get(_norm_key(key), None) is None and not key_exists():\n # The key does not exist in environment\n if false_unsets is not True:\n # This key will be added with value ''\n ret['changes'].update({key: ''})\n else:\n # The key exists.\n if false_unsets is not True:\n # Check to see if the value will change\n if current_environ.get(_norm_key(key), None) != '':\n # This key value will change to ''\n ret['changes'].update({key: ''})\n else:\n # We're going to delete the key\n ret['changes'].update({key: None})\n elif current_environ.get(_norm_key(key), '') == val:\n already_set.append(key)\n else:\n ret['changes'].update({key: val})\n\n if __opts__['test']:\n if ret['changes']:\n ret['comment'] = 'Environ values will be changed'\n else:\n ret['comment'] = 'Environ values are already set with the correct values'\n return ret\n\n if ret['changes']:\n environ_ret = __salt__['environ.setenv'](environ,\n false_unsets,\n clear_all,\n update_minion,\n permanent)\n if not environ_ret:\n ret['result'] = False\n ret['comment'] = 'Failed to set environ variables'\n return ret\n ret['result'] = True\n ret['changes'] = environ_ret\n ret['comment'] = 'Environ values were set'\n else:\n ret['comment'] = 'Environ values were already set with the correct values'\n return ret", "response": "Set the salt process environment variables."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ncreates a znc compatible hashed password", "response": "def _makepass(password, hasher='sha256'):\n '''\n Create a znc compatible hashed password\n '''\n # Setup the hasher\n if hasher == 'sha256':\n h = hashlib.sha256(password)\n elif hasher == 'md5':\n h = hashlib.md5(password)\n else:\n return NotImplemented\n\n c = \"abcdefghijklmnopqrstuvwxyz\" \\\n \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\" \\\n \"0123456789!?.,:;/*-+_()\"\n r = {\n 'Method': h.name,\n 'Salt': ''.join(random.SystemRandom().choice(c) for x in range(20)),\n }\n\n # Salt the password hash\n h.update(r['Salt'])\n r['Hash'] = h.hexdigest()\n\n return r"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nbuilds a single or multiple modules using znc - buildmod", "response": "def buildmod(*modules):\n '''\n Build module using znc-buildmod\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' znc.buildmod module.cpp [...]\n '''\n # Check if module files are missing\n missing = [module for module in modules if not os.path.exists(module)]\n if missing:\n return 'Error: The file ({0}) does not exist.'.format(', '.join(missing))\n\n cmd = ['znc-buildmod']\n cmd.extend(modules)\n out = __salt__['cmd.run'](cmd, python_shell=False).splitlines()\n return out[-1]"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef version():\n '''\n Return server version from znc --version\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' znc.version\n '''\n cmd = ['znc', '--version']\n out = __salt__['cmd.run'](cmd, python_shell=False).splitlines()\n ret = out[0].split(' - ')\n return ret[0]", "response": "Return server version from znc -- version\n CLI Example :"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a MSSQL connection.", "response": "def _get_conn(ret=None):\n '''\n Return a MSSQL connection.\n '''\n _options = _get_options(ret)\n dsn = _options.get('dsn')\n user = _options.get('user')\n passwd = _options.get('passwd')\n\n return pyodbc.connect('DSN={0};UID={1};PWD={2}'.format(\n dsn,\n user,\n passwd))"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns data to an odbc server", "response": "def returner(ret):\n '''\n Return data to an odbc server\n '''\n conn = _get_conn(ret)\n cur = conn.cursor()\n sql = '''INSERT INTO salt_returns\n (fun, jid, retval, id, success, full_ret)\n VALUES (?, ?, ?, ?, ?, ?)'''\n cur.execute(\n sql, (\n ret['fun'],\n ret['jid'],\n salt.utils.json.dumps(ret['return']),\n ret['id'],\n ret['success'],\n salt.utils.json.dumps(ret)\n )\n )\n _close_conn(conn)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the information returned when the specified job id was executed", "response": "def get_jid(jid):\n '''\n Return the information returned when the specified job id was executed\n '''\n conn = _get_conn(ret=None)\n cur = conn.cursor()\n sql = '''SELECT id, full_ret FROM salt_returns WHERE jid = ?'''\n\n cur.execute(sql, (jid,))\n data = cur.fetchall()\n ret = {}\n if data:\n for minion, full_ret in data:\n ret[minion] = salt.utils.json.loads(full_ret)\n _close_conn(conn)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_fun(fun):\n '''\n Return a dict of the last function called for all minions\n '''\n conn = _get_conn(ret=None)\n cur = conn.cursor()\n sql = '''SELECT s.id,s.jid, s.full_ret\n FROM salt_returns s\n JOIN ( SELECT MAX(jid) AS jid FROM salt_returns GROUP BY fun, id) max\n ON s.jid = max.jid\n WHERE s.fun = ?\n '''\n\n cur.execute(sql, (fun,))\n data = cur.fetchall()\n\n ret = {}\n if data:\n for minion, _, retval in data:\n ret[minion] = salt.utils.json.loads(retval)\n _close_conn(conn)\n return ret", "response": "Return a dict of the last function called for all minions\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a list of all job ids", "response": "def get_jids():\n '''\n Return a list of all job ids\n '''\n conn = _get_conn(ret=None)\n cur = conn.cursor()\n sql = '''SELECT distinct jid, load FROM jids'''\n\n cur.execute(sql)\n data = cur.fetchall()\n ret = {}\n for jid, load in data:\n ret[jid] = salt.utils.jid.format_jid_instance(jid, salt.utils.json.loads(load))\n _close_conn(conn)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _load_config():\n '''\n Loads and parses /usbkey/config\n '''\n config = {}\n\n if os.path.isfile('/usbkey/config'):\n with salt.utils.files.fopen('/usbkey/config', 'r') as config_file:\n for optval in config_file:\n optval = salt.utils.stringutils.to_unicode(optval)\n if optval[0] == '#':\n continue\n if '=' not in optval:\n continue\n optval = optval.split('=')\n config[optval[0].lower()] = optval[1].strip().strip('\"')\n log.debug('smartos.config - read /usbkey/config: %s', config)\n return config", "response": "Loads and parses the config file for the current system"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _write_config(config):\n '''\n writes /usbkey/config\n '''\n try:\n with salt.utils.atomicfile.atomic_open('/usbkey/config', 'w') as config_file:\n config_file.write(\"#\\n# This file was generated by salt\\n#\\n\")\n for prop in salt.utils.odict.OrderedDict(sorted(config.items())):\n if ' ' in six.text_type(config[prop]):\n if not config[prop].startswith('\"') or not config[prop].endswith('\"'):\n config[prop] = '\"{0}\"'.format(config[prop])\n config_file.write(\n salt.utils.stringutils.to_str(\n \"{0}={1}\\n\".format(prop, config[prop])\n )\n )\n log.debug('smartos.config - wrote /usbkey/config: %s', config)\n except IOError:\n return False\n\n return True", "response": "Writes the given config dictionary to the config file"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _parse_vmconfig(config, instances):\n '''\n Parse vm_present vm config\n '''\n vmconfig = None\n\n if isinstance(config, (salt.utils.odict.OrderedDict)):\n vmconfig = salt.utils.odict.OrderedDict()\n for prop in config:\n if prop not in instances:\n vmconfig[prop] = config[prop]\n else:\n if not isinstance(config[prop], (salt.utils.odict.OrderedDict)):\n continue\n vmconfig[prop] = []\n for instance in config[prop]:\n instance_config = config[prop][instance]\n instance_config[instances[prop]] = instance\n ## some property are lowercase\n if 'mac' in instance_config:\n instance_config['mac'] = instance_config['mac'].lower()\n vmconfig[prop].append(instance_config)\n else:\n log.error('smartos.vm_present::parse_vmconfig - failed to parse')\n\n return vmconfig", "response": "Parse vm_present vm config"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets the changes in the current state", "response": "def _get_instance_changes(current, state):\n '''\n get modified properties\n '''\n # get keys\n current_keys = set(current.keys())\n state_keys = set(state.keys())\n\n # compare configs\n changed = salt.utils.data.compare_dicts(current, state)\n for change in salt.utils.data.compare_dicts(current, state):\n if change in changed and changed[change]['old'] == \"\":\n del changed[change]\n if change in changed and changed[change]['new'] == \"\":\n del changed[change]\n\n return changed"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nensure that the specified configuration property is present in the given USB key hierarchy.", "response": "def config_present(name, value):\n '''\n Ensure configuration property is set to value in /usbkey/config\n\n name : string\n name of property\n value : string\n value of property\n\n '''\n name = name.lower()\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n # load confiration\n config = _load_config()\n\n # handle bool and None value\n if isinstance(value, (bool)):\n value = 'true' if value else 'false'\n if not value:\n value = \"\"\n\n if name in config:\n if six.text_type(config[name]) == six.text_type(value):\n # we're good\n ret['result'] = True\n ret['comment'] = 'property {0} already has value \"{1}\"'.format(name, value)\n else:\n # update property\n ret['result'] = True\n ret['comment'] = 'updated property {0} with value \"{1}\"'.format(name, value)\n ret['changes'][name] = value\n config[name] = value\n else:\n # add property\n ret['result'] = True\n ret['comment'] = 'added property {0} with value \"{1}\"'.format(name, value)\n ret['changes'][name] = value\n config[name] = value\n\n # apply change if needed\n if not __opts__['test'] and ret['changes']:\n ret['result'] = _write_config(config)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef config_absent(name):\n '''\n Ensure configuration property is absent in /usbkey/config\n\n name : string\n name of property\n\n '''\n name = name.lower()\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n # load configuration\n config = _load_config()\n\n if name in config:\n # delete property\n ret['result'] = True\n ret['comment'] = 'property {0} deleted'.format(name)\n ret['changes'][name] = None\n del config[name]\n else:\n # we're good\n ret['result'] = True\n ret['comment'] = 'property {0} is absent'.format(name)\n\n # apply change if needed\n if not __opts__['test'] and ret['changes']:\n ret['result'] = _write_config(config)\n\n return ret", "response": "Ensure configuration property is absent in / usbkey / config"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef source_present(name, source_type='imgapi'):\n '''\n Ensure an image source is present on the computenode\n\n name : string\n source url\n source_type : string\n source type (imgapi or docker)\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if name in __salt__['imgadm.sources']():\n # source is present\n ret['result'] = True\n ret['comment'] = 'image source {0} is present'.format(name)\n else:\n # add new source\n if __opts__['test']:\n res = {}\n ret['result'] = True\n else:\n res = __salt__['imgadm.source_add'](name, source_type)\n ret['result'] = (name in res)\n\n if ret['result']:\n ret['comment'] = 'image source {0} added'.format(name)\n ret['changes'][name] = 'added'\n else:\n ret['comment'] = 'image source {0} not added'.format(name)\n if 'Error' in res:\n ret['comment'] = '{0}: {1}'.format(ret['comment'], res['Error'])\n\n return ret", "response": "Ensure an image source is present on the computenode\n "} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef source_absent(name):\n '''\n Ensure an image source is absent on the computenode\n\n name : string\n source url\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if name not in __salt__['imgadm.sources']():\n # source is absent\n ret['result'] = True\n ret['comment'] = 'image source {0} is absent'.format(name)\n else:\n # remove source\n if __opts__['test']:\n res = {}\n ret['result'] = True\n else:\n res = __salt__['imgadm.source_delete'](name)\n ret['result'] = (name not in res)\n\n if ret['result']:\n ret['comment'] = 'image source {0} deleted'.format(name)\n ret['changes'][name] = 'deleted'\n else:\n ret['comment'] = 'image source {0} not deleted'.format(name)\n if 'Error' in res:\n ret['comment'] = '{0}: {1}'.format(ret['comment'], res['Error'])\n\n return ret", "response": "Ensure an image source is absent on the computenode\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef image_present(name):\n '''\n Ensure image is present on the computenode\n\n name : string\n uuid of image\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if _is_docker_uuid(name) and __salt__['imgadm.docker_to_uuid'](name):\n # docker image was imported\n ret['result'] = True\n ret['comment'] = 'image {0} ({1}) is present'.format(\n name,\n __salt__['imgadm.docker_to_uuid'](name),\n )\n elif name in __salt__['imgadm.list']():\n # image was already imported\n ret['result'] = True\n ret['comment'] = 'image {0} is present'.format(name)\n else:\n # add image\n if _is_docker_uuid(name):\n # NOTE: we cannot query available docker images\n available_images = [name]\n else:\n available_images = __salt__['imgadm.avail']()\n\n if name in available_images:\n if __opts__['test']:\n ret['result'] = True\n res = {}\n if _is_docker_uuid(name):\n res['00000000-0000-0000-0000-000000000000'] = name\n else:\n res[name] = available_images[name]\n else:\n res = __salt__['imgadm.import'](name)\n if _is_uuid(name):\n ret['result'] = (name in res)\n elif _is_docker_uuid(name):\n ret['result'] = __salt__['imgadm.docker_to_uuid'](name) is not None\n if ret['result']:\n ret['comment'] = 'image {0} imported'.format(name)\n ret['changes'] = res\n else:\n ret['comment'] = 'image {0} was unable to be imported'.format(name)\n else:\n ret['result'] = False\n ret['comment'] = 'image {0} does not exists'.format(name)\n\n return ret", "response": "Ensure image is present on the computenode\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nensure image is absent on the computenode and remove it if it is not used by a vm.", "response": "def image_absent(name):\n '''\n Ensure image is absent on the computenode\n\n name : string\n uuid of image\n\n .. note::\n\n computenode.image_absent will only remove the image if it is not used\n by a vm.\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n uuid = None\n if _is_uuid(name):\n uuid = name\n if _is_docker_uuid(name):\n uuid = __salt__['imgadm.docker_to_uuid'](name)\n\n if not uuid or uuid not in __salt__['imgadm.list']():\n # image not imported\n ret['result'] = True\n ret['comment'] = 'image {0} is absent'.format(name)\n else:\n # check if image in use by vm\n if uuid in __salt__['vmadm.list'](order='image_uuid'):\n ret['result'] = False\n ret['comment'] = 'image {0} currently in use by a vm'.format(name)\n else:\n # delete image\n if __opts__['test']:\n ret['result'] = True\n else:\n image = __salt__['imgadm.get'](uuid)\n image_count = 0\n if image['manifest']['name'] == 'docker-layer':\n # NOTE: docker images are made of multiple layers, loop over them\n while image:\n image_count += 1\n __salt__['imgadm.delete'](image['manifest']['uuid'])\n if 'origin' in image['manifest']:\n image = __salt__['imgadm.get'](image['manifest']['origin'])\n else:\n image = None\n else:\n # NOTE: normal images can just be delete\n __salt__['imgadm.delete'](uuid)\n\n ret['result'] = uuid not in __salt__['imgadm.list']()\n if image_count:\n ret['comment'] = 'image {0} and {1} children deleted'.format(name, image_count)\n else:\n ret['comment'] = 'image {0} deleted'.format(name)\n ret['changes'][name] = None\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndeleting images not in use or installed via image_present .. warning:: Only images installed via image_present states that are included via the top file will be detected.", "response": "def image_vacuum(name):\n '''\n Delete images not in use or installed via image_present\n\n .. warning::\n\n Only image_present states that are included via the\n top file will be detected.\n '''\n name = name.lower()\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n # list of images to keep\n images = []\n\n # retrieve image_present state data for host\n for state in __salt__['state.show_lowstate']():\n # don't throw exceptions when not highstate run\n if 'state' not in state:\n continue\n\n # skip if not from this state module\n if state['state'] != __virtualname__:\n continue\n # skip if not image_present\n if state['fun'] not in ['image_present']:\n continue\n # keep images installed via image_present\n if 'name' in state:\n if _is_uuid(state['name']):\n images.append(state['name'])\n elif _is_docker_uuid(state['name']):\n state['name'] = __salt__['imgadm.docker_to_uuid'](state['name'])\n if not state['name']:\n continue\n images.append(state['name'])\n\n # retrieve images in use by vms\n for image_uuid in __salt__['vmadm.list'](order='image_uuid'):\n if image_uuid not in images:\n images.append(image_uuid)\n\n # purge unused images\n ret['result'] = True\n for image_uuid in __salt__['imgadm.list']():\n if image_uuid in images:\n continue\n\n image = __salt__['imgadm.get'](image_uuid)\n if image['manifest']['name'] == 'docker-layer':\n # NOTE: docker images are made of multiple layers, loop over them\n while image:\n image_uuid = image['manifest']['uuid']\n if image_uuid in __salt__['imgadm.delete'](image_uuid):\n ret['changes'][image_uuid] = None\n else:\n ret['result'] = False\n ret['comment'] = 'failed to delete images'\n if 'origin' in image['manifest']:\n image = __salt__['imgadm.get'](image['manifest']['origin'])\n else:\n image = None\n else:\n # NOTE: normal images can just be delete\n if image_uuid in __salt__['imgadm.delete'](image_uuid):\n ret['changes'][image_uuid] = None\n else:\n ret['result'] = False\n ret['comment'] = 'failed to delete images'\n\n if ret['result'] and not ret['changes']:\n ret['comment'] = 'no images deleted'\n elif ret['result'] and ret['changes']:\n ret['comment'] = 'images deleted'\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef vm_present(name, vmconfig, config=None):\n '''\n Ensure vm is present on the computenode\n\n name : string\n hostname of vm\n vmconfig : dict\n options to set for the vm\n config : dict\n fine grain control over vm_present\n\n .. note::\n\n The following configuration properties can be toggled in the config parameter.\n - kvm_reboot (true) - reboots of kvm zones if needed for a config update\n - auto_import (false) - automatic importing of missing images\n - auto_lx_vars (true) - copy kernel_version and docker:* variables from image\n - reprovision (false) - reprovision on image_uuid changes\n - enforce_tags (true) - false = add tags only, true = add, update, and remove tags\n - enforce_routes (true) - false = add tags only, true = add, update, and remove routes\n - enforce_internal_metadata (true) - false = add metadata only, true = add, update, and remove metadata\n - enforce_customer_metadata (true) - false = add metadata only, true = add, update, and remove metadata\n\n .. note::\n\n State ID is used as hostname. Hostnames must be unique.\n\n .. note::\n\n If hostname is provided in vmconfig this will take president over the State ID.\n This allows multiple states to be applied to the same vm.\n\n .. note::\n\n The following instances should have a unique ID.\n - nic : mac\n - filesystem: target\n - disk : path or diskN for zvols\n\n e.g. disk0 will be the first disk added, disk1 the 2nd,...\n\n .. versionchanged:: 2019.2.0\n\n Added support for docker image uuids, added auto_lx_vars configuration, documented some missing configuration options.\n\n '''\n name = name.lower()\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n # config defaults\n state_config = config if config else {}\n config = {\n 'kvm_reboot': True,\n 'auto_import': False,\n 'auto_lx_vars': True,\n 'reprovision': False,\n 'enforce_tags': True,\n 'enforce_routes': True,\n 'enforce_internal_metadata': True,\n 'enforce_customer_metadata': True,\n }\n config.update(state_config)\n log.debug('smartos.vm_present::%s::config - %s', name, config)\n\n # map special vmconfig parameters\n # collections have set/remove handlers\n # instances have add/update/remove handlers and a unique id\n vmconfig_type = {\n 'collection': [\n 'tags',\n 'customer_metadata',\n 'internal_metadata',\n 'routes'\n ],\n 'instance': {\n 'nics': 'mac',\n 'disks': 'path',\n 'filesystems': 'target'\n },\n 'create_only': [\n 'filesystems'\n ]\n }\n vmconfig_docker_keep = [\n 'docker:id',\n 'docker:restartcount',\n ]\n vmconfig_docker_array = [\n 'docker:env',\n 'docker:cmd',\n 'docker:entrypoint',\n ]\n\n # parse vmconfig\n vmconfig = _parse_vmconfig(vmconfig, vmconfig_type['instance'])\n log.debug('smartos.vm_present::%s::vmconfig - %s', name, vmconfig)\n\n # set hostname if needed\n if 'hostname' not in vmconfig:\n vmconfig['hostname'] = name\n\n # prepare image_uuid\n if 'image_uuid' in vmconfig:\n # NOTE: lookup uuid from docker uuid (normal uuid's are passed throuhg unmodified)\n # we must do this again if we end up importing a missing image later!\n docker_uuid = __salt__['imgadm.docker_to_uuid'](vmconfig['image_uuid'])\n vmconfig['image_uuid'] = docker_uuid if docker_uuid else vmconfig['image_uuid']\n\n # NOTE: import image (if missing and allowed)\n if vmconfig['image_uuid'] not in __salt__['imgadm.list']():\n if config['auto_import']:\n if not __opts__['test']:\n res = __salt__['imgadm.import'](vmconfig['image_uuid'])\n vmconfig['image_uuid'] = __salt__['imgadm.docker_to_uuid'](vmconfig['image_uuid'])\n if vmconfig['image_uuid'] not in res:\n ret['result'] = False\n ret['comment'] = 'failed to import image {0}'.format(vmconfig['image_uuid'])\n else:\n ret['result'] = False\n ret['comment'] = 'image {0} not installed'.format(vmconfig['image_uuid'])\n\n # prepare disk.*.image_uuid\n for disk in vmconfig['disks'] if 'disks' in vmconfig else []:\n if 'image_uuid' in disk and disk['image_uuid'] not in __salt__['imgadm.list']():\n if config['auto_import']:\n if not __opts__['test']:\n res = __salt__['imgadm.import'](disk['image_uuid'])\n if disk['image_uuid'] not in res:\n ret['result'] = False\n ret['comment'] = 'failed to import image {0}'.format(disk['image_uuid'])\n else:\n ret['result'] = False\n ret['comment'] = 'image {0} not installed'.format(disk['image_uuid'])\n\n # docker json-array handling\n if 'internal_metadata' in vmconfig:\n for var in vmconfig_docker_array:\n if var not in vmconfig['internal_metadata']:\n continue\n if isinstance(vmconfig['internal_metadata'][var], list):\n vmconfig['internal_metadata'][var] = json.dumps(\n vmconfig['internal_metadata'][var]\n )\n\n # copy lx variables\n if vmconfig['brand'] == 'lx' and config['auto_lx_vars']:\n # NOTE: we can only copy the lx vars after the image has bene imported\n vmconfig = _copy_lx_vars(vmconfig)\n\n # quick abort if things look wrong\n # NOTE: use explicit check for false, otherwise None also matches!\n if ret['result'] is False:\n return ret\n\n # check if vm exists\n if vmconfig['hostname'] in __salt__['vmadm.list'](order='hostname'):\n # update vm\n ret['result'] = True\n\n # expand vmconfig\n vmconfig = {\n 'state': vmconfig,\n 'current': __salt__['vmadm.get'](vmconfig['hostname'], key='hostname'),\n 'changed': {},\n 'reprovision_uuid': None\n }\n\n # prepare reprovision\n if 'image_uuid' in vmconfig['state']:\n vmconfig['reprovision_uuid'] = vmconfig['state']['image_uuid']\n vmconfig['state']['image_uuid'] = vmconfig['current']['image_uuid']\n\n # disks need some special care\n if 'disks' in vmconfig['state']:\n new_disks = []\n for disk in vmconfig['state']['disks']:\n path = False\n if 'disks' in vmconfig['current']:\n for cdisk in vmconfig['current']['disks']:\n if cdisk['path'].endswith(disk['path']):\n path = cdisk['path']\n break\n if not path:\n del disk['path']\n else:\n disk['path'] = path\n new_disks.append(disk)\n vmconfig['state']['disks'] = new_disks\n\n # process properties\n for prop in vmconfig['state']:\n # skip special vmconfig_types\n if prop in vmconfig_type['instance'] or \\\n prop in vmconfig_type['collection'] or \\\n prop in vmconfig_type['create_only']:\n continue\n\n # skip unchanged properties\n if prop in vmconfig['current']:\n if isinstance(vmconfig['current'][prop], (list)) or isinstance(vmconfig['current'][prop], (dict)):\n if vmconfig['current'][prop] == vmconfig['state'][prop]:\n continue\n else:\n if \"{0}\".format(vmconfig['current'][prop]) == \"{0}\".format(vmconfig['state'][prop]):\n continue\n\n # add property to changeset\n vmconfig['changed'][prop] = vmconfig['state'][prop]\n\n # process collections\n for collection in vmconfig_type['collection']:\n # skip create only collections\n if collection in vmconfig_type['create_only']:\n continue\n\n # enforcement\n enforce = config['enforce_{0}'.format(collection)]\n log.debug('smartos.vm_present::enforce_%s = %s', collection, enforce)\n\n # dockerinit handling\n if collection == 'internal_metadata' and vmconfig['state'].get('docker', False):\n if 'internal_metadata' not in vmconfig['state']:\n vmconfig['state']['internal_metadata'] = {}\n\n # preserve some docker specific metadata (added and needed by dockerinit)\n for var in vmconfig_docker_keep:\n val = vmconfig['current'].get(collection, {}).get(var, None)\n if val is not None:\n vmconfig['state']['internal_metadata'][var] = val\n\n # process add and update for collection\n if collection in vmconfig['state'] and vmconfig['state'][collection] is not None:\n for prop in vmconfig['state'][collection]:\n # skip unchanged properties\n if prop in vmconfig['current'][collection] and \\\n vmconfig['current'][collection][prop] == vmconfig['state'][collection][prop]:\n continue\n\n # skip update if not enforcing\n if not enforce and prop in vmconfig['current'][collection]:\n continue\n\n # create set_ dict\n if 'set_{0}'.format(collection) not in vmconfig['changed']:\n vmconfig['changed']['set_{0}'.format(collection)] = {}\n\n # add property to changeset\n vmconfig['changed']['set_{0}'.format(collection)][prop] = vmconfig['state'][collection][prop]\n\n # process remove for collection\n if enforce and collection in vmconfig['current'] and vmconfig['current'][collection] is not None:\n for prop in vmconfig['current'][collection]:\n # skip if exists in state\n if collection in vmconfig['state'] and vmconfig['state'][collection] is not None:\n if prop in vmconfig['state'][collection]:\n continue\n\n # create remove_ array\n if 'remove_{0}'.format(collection) not in vmconfig['changed']:\n vmconfig['changed']['remove_{0}'.format(collection)] = []\n\n # remove property\n vmconfig['changed']['remove_{0}'.format(collection)].append(prop)\n\n # process instances\n for instance in vmconfig_type['instance']:\n # skip create only instances\n if instance in vmconfig_type['create_only']:\n continue\n\n # add or update instances\n if instance in vmconfig['state'] and vmconfig['state'][instance] is not None:\n for state_cfg in vmconfig['state'][instance]:\n add_instance = True\n\n # find instance with matching ids\n for current_cfg in vmconfig['current'][instance]:\n if vmconfig_type['instance'][instance] not in state_cfg:\n continue\n\n if state_cfg[vmconfig_type['instance'][instance]] == current_cfg[vmconfig_type['instance'][instance]]:\n # ids have matched, disable add instance\n add_instance = False\n\n changed = _get_instance_changes(current_cfg, state_cfg)\n update_cfg = {}\n\n # handle changes\n for prop in changed:\n update_cfg[prop] = state_cfg[prop]\n\n # handle new properties\n for prop in state_cfg:\n # skip empty props like ips, options,..\n if isinstance(state_cfg[prop], (list)) and not state_cfg[prop]:\n continue\n\n if prop not in current_cfg:\n update_cfg[prop] = state_cfg[prop]\n\n # update instance\n if update_cfg:\n # create update_ array\n if 'update_{0}'.format(instance) not in vmconfig['changed']:\n vmconfig['changed']['update_{0}'.format(instance)] = []\n\n update_cfg[vmconfig_type['instance'][instance]] = state_cfg[vmconfig_type['instance'][instance]]\n vmconfig['changed']['update_{0}'.format(instance)].append(update_cfg)\n\n if add_instance:\n # create add_ array\n if 'add_{0}'.format(instance) not in vmconfig['changed']:\n vmconfig['changed']['add_{0}'.format(instance)] = []\n\n # add instance\n vmconfig['changed']['add_{0}'.format(instance)].append(state_cfg)\n\n # remove instances\n if instance in vmconfig['current'] and vmconfig['current'][instance] is not None:\n for current_cfg in vmconfig['current'][instance]:\n remove_instance = True\n\n # find instance with matching ids\n if instance in vmconfig['state'] and vmconfig['state'][instance] is not None:\n for state_cfg in vmconfig['state'][instance]:\n if vmconfig_type['instance'][instance] not in state_cfg:\n continue\n\n if state_cfg[vmconfig_type['instance'][instance]] == current_cfg[vmconfig_type['instance'][instance]]:\n # keep instance if matched\n remove_instance = False\n\n if remove_instance:\n # create remove_ array\n if 'remove_{0}'.format(instance) not in vmconfig['changed']:\n vmconfig['changed']['remove_{0}'.format(instance)] = []\n\n # remove instance\n vmconfig['changed']['remove_{0}'.format(instance)].append(\n current_cfg[vmconfig_type['instance'][instance]]\n )\n\n # update vm if we have pending changes\n kvm_needs_start = False\n if not __opts__['test'] and vmconfig['changed']:\n # stop kvm if disk updates and kvm_reboot\n if vmconfig['current']['brand'] == 'kvm' and config['kvm_reboot']:\n if 'add_disks' in vmconfig['changed'] or \\\n 'update_disks' in vmconfig['changed'] or \\\n 'remove_disks' in vmconfig['changed']:\n if vmconfig['state']['hostname'] in __salt__['vmadm.list'](order='hostname', search='state=running'):\n kvm_needs_start = True\n __salt__['vmadm.stop'](vm=vmconfig['state']['hostname'], key='hostname')\n\n # do update\n rret = __salt__['vmadm.update'](vm=vmconfig['state']['hostname'], key='hostname', **vmconfig['changed'])\n if not isinstance(rret, (bool)) and 'Error' in rret:\n ret['result'] = False\n ret['comment'] = \"{0}\".format(rret['Error'])\n else:\n ret['result'] = True\n ret['changes'][vmconfig['state']['hostname']] = vmconfig['changed']\n\n if ret['result']:\n if __opts__['test']:\n ret['changes'][vmconfig['state']['hostname']] = vmconfig['changed']\n\n if vmconfig['state']['hostname'] in ret['changes'] and ret['changes'][vmconfig['state']['hostname']]:\n ret['comment'] = 'vm {0} updated'.format(vmconfig['state']['hostname'])\n if config['kvm_reboot'] and vmconfig['current']['brand'] == 'kvm' and not __opts__['test']:\n if vmconfig['state']['hostname'] in __salt__['vmadm.list'](order='hostname', search='state=running'):\n __salt__['vmadm.reboot'](vm=vmconfig['state']['hostname'], key='hostname')\n if kvm_needs_start:\n __salt__['vmadm.start'](vm=vmconfig['state']['hostname'], key='hostname')\n else:\n ret['changes'] = {}\n ret['comment'] = 'vm {0} is up to date'.format(vmconfig['state']['hostname'])\n\n # reprovision (if required and allowed)\n if 'image_uuid' in vmconfig['current'] and vmconfig['reprovision_uuid'] != vmconfig['current']['image_uuid']:\n if config['reprovision']:\n rret = __salt__['vmadm.reprovision'](\n vm=vmconfig['state']['hostname'],\n key='hostname',\n image=vmconfig['reprovision_uuid']\n )\n if not isinstance(rret, (bool)) and 'Error' in rret:\n ret['result'] = False\n ret['comment'] = 'vm {0} updated, reprovision failed'.format(\n vmconfig['state']['hostname']\n )\n else:\n ret['comment'] = 'vm {0} updated and reprovisioned'.format(vmconfig['state']['hostname'])\n if vmconfig['state']['hostname'] not in ret['changes']:\n ret['changes'][vmconfig['state']['hostname']] = {}\n ret['changes'][vmconfig['state']['hostname']]['image_uuid'] = vmconfig['reprovision_uuid']\n else:\n log.warning('smartos.vm_present::%s::reprovision - '\n 'image_uuid in state does not match current, '\n 'reprovision not allowed',\n name)\n else:\n ret['comment'] = 'vm {0} failed to be updated'.format(vmconfig['state']['hostname'])\n if not isinstance(rret, (bool)) and 'Error' in rret:\n ret['comment'] = \"{0}\".format(rret['Error'])\n else:\n # check required image installed\n ret['result'] = True\n\n # disks need some special care\n if 'disks' in vmconfig:\n new_disks = []\n for disk in vmconfig['disks']:\n if 'path' in disk:\n del disk['path']\n new_disks.append(disk)\n vmconfig['disks'] = new_disks\n\n # create vm\n if ret['result']:\n uuid = __salt__['vmadm.create'](**vmconfig) if not __opts__['test'] else True\n if not isinstance(uuid, (bool)) and 'Error' in uuid:\n ret['result'] = False\n ret['comment'] = \"{0}\".format(uuid['Error'])\n else:\n ret['result'] = True\n ret['changes'][vmconfig['hostname']] = vmconfig\n ret['comment'] = 'vm {0} created'.format(vmconfig['hostname'])\n\n return ret", "response": "Ensure vm is present on the computenode"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nensuring vm is absent on the computenode", "response": "def vm_absent(name, archive=False):\n '''\n Ensure vm is absent on the computenode\n\n name : string\n hostname of vm\n archive : boolean\n toggle archiving of vm on removal\n\n .. note::\n\n State ID is used as hostname. Hostnames must be unique.\n\n '''\n name = name.lower()\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if name not in __salt__['vmadm.list'](order='hostname'):\n # we're good\n ret['result'] = True\n ret['comment'] = 'vm {0} is absent'.format(name)\n else:\n # delete vm\n if not __opts__['test']:\n # set archive to true if needed\n if archive:\n __salt__['vmadm.update'](vm=name, key='hostname', archive_on_delete=True)\n\n ret['result'] = __salt__['vmadm.delete'](name, key='hostname')\n else:\n ret['result'] = True\n\n if not isinstance(ret['result'], bool) and ret['result'].get('Error'):\n ret['result'] = False\n ret['comment'] = 'failed to delete vm {0}'.format(name)\n else:\n ret['comment'] = 'vm {0} deleted'.format(name)\n ret['changes'][name] = None\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nensure vm is in the running state on the computenode", "response": "def vm_running(name):\n '''\n Ensure vm is in the running state on the computenode\n\n name : string\n hostname of vm\n\n .. note::\n\n State ID is used as hostname. Hostnames must be unique.\n\n '''\n name = name.lower()\n ret = {'name': name,\n 'changes': {},\n 'result': None,\n 'comment': ''}\n\n if name in __salt__['vmadm.list'](order='hostname', search='state=running'):\n # we're good\n ret['result'] = True\n ret['comment'] = 'vm {0} already running'.format(name)\n else:\n # start the vm\n ret['result'] = True if __opts__['test'] else __salt__['vmadm.start'](name, key='hostname')\n if not isinstance(ret['result'], bool) and ret['result'].get('Error'):\n ret['result'] = False\n ret['comment'] = 'failed to start {0}'.format(name)\n else:\n ret['changes'][name] = 'running'\n ret['comment'] = 'vm {0} started'.format(name)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _repack_options(options):\n '''\n Repack the options data\n '''\n return dict(\n [\n (six.text_type(x), _normalize(y))\n for x, y in six.iteritems(salt.utils.data.repack_dictlist(options))\n ]\n )", "response": "Repack the options data\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_option_list(options):\n '''\n Returns the key/value pairs in the passed dict in a commaspace-delimited\n list in the format \"key=value\".\n '''\n return ', '.join(['{0}={1}'.format(x, y) for x, y in six.iteritems(options)])", "response": "Returns the key = value pairs in the passed dict in a commaspace - delimited\n list in the format key = value."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nverifies that the port is installed and that it has the specified options.", "response": "def installed(name, options=None):\n '''\n Verify that the desired port is installed, and that it was compiled with\n the desired options.\n\n options\n Make sure that the desired non-default options are set\n\n .. warning::\n\n Any build options not passed here assume the default values for the\n port, and are not just differences from the existing cached options\n from a previous ``make config``.\n\n Example usage:\n\n .. code-block:: yaml\n\n security/nmap:\n ports.installed:\n - options:\n - IPV6: off\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': '{0} is already installed'.format(name)}\n try:\n current_options = __salt__['ports.showconfig'](name, default=False,\n dict_return=True)\n default_options = __salt__['ports.showconfig'](name, default=True,\n dict_return=True)\n # unpack the options from the top-level return dict\n if current_options:\n current_options = current_options[next(iter(current_options))]\n if default_options:\n default_options = default_options[next(iter(default_options))]\n except (SaltInvocationError, CommandExecutionError) as exc:\n ret['result'] = False\n ret['comment'] = ('Unable to get configuration for {0}. Port name may '\n 'be invalid, or ports tree may need to be updated. '\n 'Error message: {1}'.format(name, exc))\n return ret\n\n options = _repack_options(options) if options is not None else {}\n desired_options = copy.deepcopy(default_options)\n desired_options.update(options)\n ports_pre = [\n x['origin'] for x in\n six.itervalues(__salt__['pkg.list_pkgs'](with_origin=True))\n ]\n\n if current_options == desired_options and name in ports_pre:\n # Port is installed as desired\n if options:\n ret['comment'] += ' ' + _build_option_string(options)\n return ret\n\n if not default_options:\n if options:\n ret['result'] = False\n ret['comment'] = ('{0} does not have any build options, yet '\n 'options were specified'.format(name))\n return ret\n else:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = '{0} will be installed'.format(name)\n return ret\n else:\n bad_opts = [x for x in options if x not in default_options]\n if bad_opts:\n ret['result'] = False\n ret['comment'] = ('The following options are not available for '\n '{0}: {1}'.format(name, ', '.join(bad_opts)))\n return ret\n\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = '{0} will be installed '.format(name)\n ret['comment'] += _build_option_string(options)\n return ret\n\n if options:\n if not __salt__['ports.config'](name, reset=True, **options):\n ret['result'] = False\n ret['comment'] = 'Unable to set options for {0}'.format(name)\n return ret\n else:\n __salt__['ports.rmconfig'](name)\n if _options_file_exists(name):\n ret['result'] = False\n ret['comment'] = 'Unable to clear options for {0}'.format(name)\n return ret\n\n ret['changes'] = __salt__['ports.install'](name)\n ports_post = [\n x['origin'] for x in\n six.itervalues(__salt__['pkg.list_pkgs'](with_origin=True))\n ]\n err = sys.modules[\n __salt__['test.ping'].__module__\n ].__context__.pop('ports.install_error', None)\n if err or name not in ports_post:\n ret['result'] = False\n if ret['result']:\n ret['comment'] = 'Successfully installed {0}'.format(name)\n if default_options:\n ret['comment'] += ' ' + _build_option_string(options)\n else:\n ret['comment'] = 'Failed to install {0}'.format(name)\n if err:\n ret['comment'] += '. Error message:\\n{0}'.format(err)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nvalidating the configuration passed to the API.", "response": "def _validate(dns_proto, dns_servers, ip_proto, ip_addrs, gateway):\n '''\n Ensure that the configuration passed is formatted correctly and contains\n valid IP addresses, etc.\n '''\n errors = []\n # Validate DNS configuration\n if dns_proto == 'dhcp':\n if dns_servers is not None:\n errors.append(\n 'The dns_servers param cannot be set if unless dns_proto is '\n 'set to \\'static\\''\n )\n else:\n if str(dns_servers).lower() in ['none', '[]']:\n pass\n elif not isinstance(dns_servers, list):\n errors.append(\n 'The dns_servers param must be formatted as a list'\n )\n else:\n bad_ips = [x for x in dns_servers\n if not salt.utils.validate.net.ipv4_addr(x)]\n if bad_ips:\n errors.append('Invalid DNS server IPs: {0}'\n .format(', '.join(bad_ips)))\n\n # Validate IP configuration\n if ip_proto == 'dhcp':\n if ip_addrs is not None:\n errors.append(\n 'The ip_addrs param cannot be set if unless ip_proto is set '\n 'to \\'static\\''\n )\n if gateway is not None:\n errors.append(\n 'A gateway IP cannot be set if unless ip_proto is set to '\n '\\'static\\''\n )\n else:\n if not ip_addrs:\n errors.append(\n 'The ip_addrs param is required to set static IPs'\n )\n elif not isinstance(ip_addrs, list):\n errors.append(\n 'The ip_addrs param must be formatted as a list'\n )\n else:\n bad_ips = [x for x in ip_addrs\n if not salt.utils.validate.net.ipv4_addr(x)]\n if bad_ips:\n errors.append('The following static IPs are invalid: '\n '{0}'.format(', '.join(bad_ips)))\n\n # Validate default gateway\n if gateway is not None:\n if not salt.utils.validate.net.ipv4_addr(gateway):\n errors.append('Gateway IP {0} is invalid'.format(gateway))\n\n return errors"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _changes(cur, dns_proto, dns_servers, ip_proto, ip_addrs, gateway):\n '''\n Compares the current interface against the desired configuration and\n returns a dictionary describing the changes that need to be made.\n '''\n changes = {}\n cur_dns_proto = (\n 'static' if 'Statically Configured DNS Servers' in cur\n else 'dhcp'\n )\n if cur_dns_proto == 'static':\n if isinstance(cur['Statically Configured DNS Servers'], list):\n cur_dns_servers = cur['Statically Configured DNS Servers']\n else:\n cur_dns_servers = [cur['Statically Configured DNS Servers']]\n if set(dns_servers or ['None']) != set(cur_dns_servers):\n changes['dns_servers'] = dns_servers\n elif 'DNS servers configured through DHCP' in cur:\n cur_dns_servers = cur['DNS servers configured through DHCP']\n if dns_proto == 'static':\n # If we're currently set to 'dhcp' but moving to 'static', specify the changes.\n if set(dns_servers or ['None']) != set(cur_dns_servers):\n changes['dns_servers'] = dns_servers\n\n cur_ip_proto = 'static' if cur['DHCP enabled'] == 'No' else 'dhcp'\n cur_ip_addrs = _addrdict_to_ip_addrs(cur.get('ip_addrs', []))\n cur_gateway = cur.get('Default Gateway')\n\n if dns_proto != cur_dns_proto:\n changes['dns_proto'] = dns_proto\n if ip_proto != cur_ip_proto:\n changes['ip_proto'] = ip_proto\n if set(ip_addrs or []) != set(cur_ip_addrs):\n if ip_proto == 'static':\n changes['ip_addrs'] = ip_addrs\n if gateway != cur_gateway:\n if ip_proto == 'static':\n changes['gateway'] = gateway\n return changes", "response": "Returns a dictionary describing the changes made to the current configuration and the current interface."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef managed(name,\n dns_proto=None,\n dns_servers=None,\n ip_proto=None,\n ip_addrs=None,\n gateway=None,\n enabled=True,\n **kwargs):\n '''\n Ensure that the named interface is configured properly.\n\n Args:\n\n name (str):\n The name of the interface to manage\n\n dns_proto (str): None\n Set to ``static`` and use the ``dns_servers`` parameter to provide a\n list of DNS nameservers. set to ``dhcp`` to use DHCP to get the DNS\n servers.\n\n dns_servers (list): None\n A list of static DNS servers. To clear the list of DNS servers pass\n an empty list (``[]``). ``None`` will make no changes.\n\n ip_proto (str): None\n Set to ``static`` and use the ``ip_addrs`` and (optionally)\n ``gateway`` parameters to provide a list of static IP addresses and\n the default gateway. Set to ``dhcp`` to use DHCP.\n\n ip_addrs (list): None\n A list of static IP addresses with netmask flag, ie: 192.168.0.11/24\n\n gateway (str): None\n The gateway to set for the interface\n\n enabled (bool): True\n Set to ``False`` to ensure that this interface is disabled.\n\n Returns:\n dict: A dictionary of old and new settings\n\n Example:\n\n .. code-block:: yaml\n\n Ethernet1:\n network.managed:\n - dns_proto: static\n - dns_servers:\n - 8.8.8.8\n - 8.8.8.4\n - ip_proto: static\n - ip_addrs:\n - 192.168.0.100/24\n\n Clear DNS entries example:\n\n .. code-block:: yaml\n\n Ethernet1:\n network.managed:\n - dns_proto: static\n - dns_servers: []\n - ip_proto: dhcp\n '''\n ret = {\n 'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'Interface \\'{0}\\' is up to date'.format(name)\n }\n\n dns_proto = six.text_type(dns_proto).lower()\n ip_proto = six.text_type(ip_proto).lower()\n\n errors = []\n if dns_proto not in __VALID_PROTO:\n ret['result'] = False\n errors.append('dns_proto must be one of the following: {0}'\n .format(', '.join(__VALID_PROTO)))\n\n if ip_proto not in __VALID_PROTO:\n errors.append('ip_proto must be one of the following: {0}'\n .format(', '.join(__VALID_PROTO)))\n\n if errors:\n ret['result'] = False\n ret['comment'] = '\\n'.join(errors)\n return ret\n\n try:\n currently_enabled = __salt__['ip.is_enabled'](name)\n except CommandExecutionError:\n currently_enabled = False\n\n if not enabled:\n if currently_enabled:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = ('Interface \\'{0}\\' will be disabled'\n .format(name))\n else:\n ret['result'] = __salt__['ip.disable'](name)\n if not ret['result']:\n ret['comment'] = ('Failed to disable interface \\'{0}\\''\n .format(name))\n else:\n ret['comment'] += ' (already disabled)'\n return ret\n else:\n if not currently_enabled:\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = ('Interface \\'{0}\\' will be enabled'\n .format(name))\n else:\n if not __salt__['ip.enable'](name):\n ret['result'] = False\n ret['comment'] = ('Failed to enable interface \\'{0}\\' to '\n 'make changes'.format(name))\n return ret\n\n errors = _validate(dns_proto, dns_servers, ip_proto, ip_addrs, gateway)\n if errors:\n ret['result'] = False\n ret['comment'] = ('The following SLS configuration errors were '\n 'detected:\\n- {0}'.format('\\n- '.join(errors)))\n return ret\n\n old = __salt__['ip.get_interface'](name)\n if not old:\n ret['result'] = False\n ret['comment'] = ('Unable to get current configuration for '\n 'interface \\'{0}\\''.format(name))\n return ret\n\n changes = _changes(old,\n dns_proto,\n dns_servers,\n ip_proto,\n ip_addrs,\n gateway)\n\n # If dns_servers is the default `None` make no changes\n # To clear the list, pass an empty dict\n if str(dns_servers).lower() == 'none':\n changes.pop('dns_servers', None)\n\n if not changes:\n return ret\n\n if __opts__['test']:\n comments = []\n if 'dns_proto' in changes:\n comments.append('DNS protocol will be changed to: {0}'\n .format(changes['dns_proto']))\n if dns_proto == 'static' and 'dns_servers' in changes:\n if not changes['dns_servers']:\n comments.append('The list of DNS servers will be cleared')\n else:\n comments.append(\n 'DNS servers will be set to the following: {0}'\n .format(', '.join(changes['dns_servers']))\n )\n if 'ip_proto' in changes:\n comments.append('IP protocol will be changed to: {0}'\n .format(changes['ip_proto']))\n if ip_proto == 'static':\n if 'ip_addrs' in changes:\n comments.append(\n 'IP addresses will be set to the following: {0}'\n .format(', '.join(changes['ip_addrs']))\n )\n if 'gateway' in changes:\n if changes['gateway'] is None:\n comments.append('Default gateway will be removed')\n else:\n comments.append(\n 'Default gateway will be set to {0}'\n .format(changes['gateway'])\n )\n\n ret['result'] = None\n ret['comment'] = ('The following changes will be made to '\n 'interface \\'{0}\\':\\n- {1}'\n .format(name, '\\n- '.join(comments)))\n return ret\n\n if changes.get('dns_proto') == 'dhcp':\n __salt__['ip.set_dhcp_dns'](name)\n\n elif 'dns_servers' in changes:\n if not changes['dns_servers']:\n # To clear the list of DNS servers you have to pass []. Later\n # changes gets passed like *args and a single empty list is\n # converted to an empty tuple. So, you have to add [] here\n changes['dns_servers'] = [[]]\n\n __salt__['ip.set_static_dns'](name, *changes['dns_servers'])\n\n if changes.get('ip_proto') == 'dhcp':\n __salt__['ip.set_dhcp_ip'](name)\n elif changes.get('ip_addrs') or changes.get('gateway') or changes.get('ip_proto') == 'static':\n if changes.get('gateway') and not changes.get('ip_addrs'):\n changes['ip_addrs'] = ip_addrs\n if changes.get('ip_proto') == 'static' and not changes.get('ip_addrs'):\n changes['ip_addrs'] = ip_addrs\n for idx in range(len(changes['ip_addrs'])):\n if idx == 0:\n __salt__['ip.set_static_ip'](\n name,\n changes['ip_addrs'][idx],\n gateway=gateway,\n append=False\n )\n else:\n __salt__['ip.set_static_ip'](\n name,\n changes['ip_addrs'][idx],\n gateway=None,\n append=True\n )\n\n new = __salt__['ip.get_interface'](name)\n ret['changes'] = salt.utils.data.compare_dicts(old, new)\n if _changes(new, dns_proto, dns_servers, ip_proto, ip_addrs, gateway):\n ret['result'] = False\n ret['comment'] = ('Failed to set desired configuration settings '\n 'for interface \\'{0}\\''.format(name))\n else:\n ret['comment'] = ('Successfully updated configuration for '\n 'interface \\'{0}\\''.format(name))\n return ret", "response": "This function manage the named interface with the specified DNS servers and IP addresses."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nsets up libcrypto argtypes and initialize the library", "response": "def _init_libcrypto():\n '''\n Set up libcrypto argtypes and initialize the library\n '''\n libcrypto = _load_libcrypto()\n\n try:\n libcrypto.OPENSSL_init_crypto()\n except AttributeError:\n # Support for OpenSSL < 1.1 (OPENSSL_API_COMPAT < 0x10100000L)\n libcrypto.OPENSSL_no_config()\n libcrypto.OPENSSL_add_all_algorithms_noconf()\n\n libcrypto.RSA_new.argtypes = ()\n libcrypto.RSA_new.restype = c_void_p\n libcrypto.RSA_free.argtypes = (c_void_p, )\n libcrypto.RSA_size.argtype = (c_void_p)\n libcrypto.BIO_new_mem_buf.argtypes = (c_char_p, c_int)\n libcrypto.BIO_new_mem_buf.restype = c_void_p\n libcrypto.BIO_free.argtypes = (c_void_p, )\n libcrypto.PEM_read_bio_RSAPrivateKey.argtypes = (c_void_p, c_void_p, c_void_p, c_void_p)\n libcrypto.PEM_read_bio_RSAPrivateKey.restype = c_void_p\n libcrypto.PEM_read_bio_RSA_PUBKEY.argtypes = (c_void_p, c_void_p, c_void_p, c_void_p)\n libcrypto.PEM_read_bio_RSA_PUBKEY.restype = c_void_p\n libcrypto.RSA_private_encrypt.argtypes = (c_int, c_char_p, c_char_p, c_void_p, c_int)\n libcrypto.RSA_public_decrypt.argtypes = (c_int, c_char_p, c_char_p, c_void_p, c_int)\n\n return libcrypto"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef sign(self, msg):\n '''\n Sign a message (digest) using the private key\n\n :param str msg: The message (digest) to sign\n :rtype: str\n :return: The signature, or an empty string if the encryption failed\n '''\n # Allocate a buffer large enough for the signature. Freed by ctypes.\n buf = create_string_buffer(libcrypto.RSA_size(self._rsa))\n msg = salt.utils.stringutils.to_bytes(msg)\n size = libcrypto.RSA_private_encrypt(len(msg), msg, buf, self._rsa, RSA_X931_PADDING)\n if size < 0:\n raise ValueError('Unable to encrypt message')\n return buf[0:size]", "response": "Sign a message using the private key."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nverifies the message from the signature using the public key.", "response": "def verify(self, signed):\n '''\n Recover the message (digest) from the signature using the public key\n\n :param str signed: The signature created with the private key\n :rtype: str\n :return: The message (digest) recovered from the signature, or an empty\n string if the decryption failed\n '''\n # Allocate a buffer large enough for the signature. Freed by ctypes.\n buf = create_string_buffer(libcrypto.RSA_size(self._rsa))\n signed = salt.utils.stringutils.to_bytes(signed)\n size = libcrypto.RSA_public_decrypt(len(signed), signed, buf, self._rsa, RSA_X931_PADDING)\n if size < 0:\n raise ValueError('Unable to decrypt message')\n return buf[0:size]"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef install(pkg=None,\n pkgs=None,\n user=None,\n install_global=False,\n env=None):\n '''\n Install a cabal package.\n\n pkg\n A package name in format accepted by cabal-install. See:\n https://wiki.haskell.org/Cabal-Install\n\n pkgs\n A list of packages names in same format as ``pkg``\n\n user\n The user to run cabal install with\n\n install_global\n Install package globally instead of locally\n\n env\n Environment variables to set when invoking cabal. Uses the\n same ``env`` format as the :py:func:`cmd.run\n <salt.modules.cmdmod.run>` execution function\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' cabal.install shellcheck\n salt '*' cabal.install shellcheck-0.3.5\n '''\n\n cmd = ['cabal install']\n\n if install_global:\n cmd.append('--global')\n\n if pkg:\n cmd.append('\"{0}\"'.format(pkg))\n elif pkgs:\n cmd.append('\"{0}\"'.format('\" \"'.join(pkgs)))\n\n result = __salt__['cmd.run_all'](' '.join(cmd), runas=user, env=env)\n\n if result['retcode'] != 0:\n raise CommandExecutionError(result['stderr'])\n\n return result", "response": "Install a cabal package."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_(\n pkg=None,\n user=None,\n installed=False,\n env=None):\n '''\n List packages matching a search string.\n\n pkg\n Search string for matching package names\n user\n The user to run cabal list with\n installed\n If True, only return installed packages.\n env\n Environment variables to set when invoking cabal. Uses the\n same ``env`` format as the :py:func:`cmd.run\n <salt.modules.cmdmod.run>` execution function\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' cabal.list\n salt '*' cabal.list ShellCheck\n '''\n cmd = ['cabal list --simple-output']\n\n if installed:\n cmd.append('--installed')\n\n if pkg:\n cmd.append('\"{0}\"'.format(pkg))\n\n result = __salt__['cmd.run_all'](' '.join(cmd), runas=user, env=env)\n\n packages = {}\n for line in result['stdout'].splitlines():\n data = line.split()\n package_name = data[0]\n package_version = data[1]\n packages[package_name] = package_version\n\n return packages", "response": "List the packages matching a search string."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ntakes a string or file object and runs it through the YAML_EX parser and returns a Python data structure", "response": "def render(sls_data, saltenv='base', sls='', **kws):\n '''\n Accepts YAML_EX as a string or as a file object and runs it through the YAML_EX\n parser.\n\n :rtype: A Python data structure\n '''\n with warnings.catch_warnings(record=True) as warn_list:\n data = deserialize(sls_data) or {}\n\n for item in warn_list:\n log.warning(\n '%s found in %s saltenv=%s',\n item.message, salt.utils.url.create(sls), saltenv\n )\n\n log.debug('Results of SLS rendering: \\n%s', data)\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef returner(ret):\n '''\n Signal a Django server that a return is available\n '''\n signaled = dispatch.Signal(providing_args=['ret']).send(sender='returner', ret=ret)\n\n for signal in signaled:\n log.debug(\n 'Django returner function \\'returner\\' signaled %s '\n 'which responded with %s', signal[0], signal[1]\n )", "response": "Signal a Django server that a return is available on a resource."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef save_load(jid, load, minions=None):\n '''\n Save the load to the specified jid\n '''\n signaled = dispatch.Signal(\n providing_args=['jid', 'load']).send(\n sender='save_load', jid=jid, load=load)\n\n for signal in signaled:\n log.debug(\n 'Django returner function \\'save_load\\' signaled %s '\n 'which responded with %s', signal[0], signal[1]\n )", "response": "Save the load to the specified jid"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef prep_jid(nocache=False, passed_jid=None):\n '''\n Do any work necessary to prepare a JID, including sending a custom ID\n '''\n return passed_jid if passed_jid is not None else salt.utils.jid.gen_jid(__opts__)", "response": "Prepare a JID for sending to the master"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nloads and start all available netapi modules", "response": "def run(self):\n '''\n Load and start all available api modules\n '''\n if not len(self.netapi):\n log.error(\"Did not find any netapi configurations, nothing to start\")\n\n kwargs = {}\n if salt.utils.platform.is_windows():\n kwargs['log_queue'] = salt.log.setup.get_multiprocessing_logging_queue()\n kwargs['log_queue_level'] = salt.log.setup.get_multiprocessing_logging_level()\n\n for fun in self.netapi:\n if fun.endswith('.start'):\n log.info('Starting %s netapi module', fun)\n self.process_manager.add_process(\n RunNetapi,\n args=(self.opts, fun),\n kwargs=kwargs,\n name='RunNetapi'\n )\n\n # Install the SIGINT/SIGTERM handlers if not done so far\n if signal.getsignal(signal.SIGINT) is signal.SIG_DFL:\n # No custom signal handling was added, install our own\n signal.signal(signal.SIGINT, self._handle_signals)\n\n if signal.getsignal(signal.SIGTERM) is signal.SIG_DFL:\n # No custom signal handling was added, install our own\n signal.signal(signal.SIGTERM, self._handle_signals)\n\n self.process_manager.run()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef top(**kwargs):\n '''\n Query |varstack| for the top data (states of the minions).\n '''\n\n conf = __opts__['master_tops']['varstack']\n __grains__ = kwargs['grains']\n\n vs_ = varstack.Varstack(config_filename=conf)\n ret = vs_.evaluate(__grains__)\n return {'base': ret['states']}", "response": "Query |varstack| for the top data of the minions."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_value(file, element):\n '''\n Returns the value of the matched xpath element\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xml.get_value /tmp/test.xml \".//element\"\n '''\n try:\n root = ET.parse(file)\n element = root.find(element)\n return element.text\n except AttributeError:\n log.error(\"Unable to find element matching %s\", element)\n return False", "response": "Get the value of the matched xpath element\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nset the value of the matched xpath element", "response": "def set_value(file, element, value):\n '''\n Sets the value of the matched xpath element\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xml.set_value /tmp/test.xml \".//element\" \"new value\"\n '''\n try:\n root = ET.parse(file)\n relement = root.find(element)\n except AttributeError:\n log.error(\"Unable to find element matching %s\", element)\n return False\n relement.text = str(value)\n root.write(file)\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nget the attributes of the matched xpath element.", "response": "def get_attribute(file, element):\n '''\n Return the attributes of the matched xpath element.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xml.get_attribute /tmp/test.xml \".//element[@id='3']\"\n '''\n try:\n root = ET.parse(file)\n element = root.find(element)\n return element.attrib\n except AttributeError:\n log.error(\"Unable to find element matching %s\", element)\n return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the requested attribute key and value for matched xpath element.", "response": "def set_attribute(file, element, key, value):\n '''\n Set the requested attribute key and value for matched xpath element.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xml.set_attribute /tmp/test.xml \".//element[@id='3']\" editedby \"gal\"\n '''\n try:\n root = ET.parse(file)\n element = root.find(element)\n except AttributeError:\n log.error(\"Unable to find element matching %s\", element)\n return False\n element.set(key, str(value))\n root.write(file)\n return True"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_account(self):\n '''\n List Swift containers\n '''\n try:\n listing = self.conn.get_account()\n return listing\n except Exception as exc:\n log.error('There was an error::')\n if hasattr(exc, 'code') and hasattr(exc, 'msg'):\n log.error(' Code: %s: %s', exc.code, exc.msg)\n log.error(' Content: \\n%s', getattr(exc, 'read', lambda: six.text_type(exc))())\n return False", "response": "Get the current Swift containers from the server."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nretrieve a file from Swift", "response": "def get_object(self, cont, obj, local_file=None, return_bin=False):\n '''\n Retrieve a file from Swift\n '''\n try:\n if local_file is None and return_bin is False:\n return False\n\n headers, body = self.conn.get_object(cont, obj, resp_chunk_size=65536)\n\n if return_bin is True:\n fp = sys.stdout\n else:\n dirpath = dirname(local_file)\n if dirpath and not isdir(dirpath):\n mkdirs(dirpath)\n fp = salt.utils.files.fopen(local_file, 'wb') # pylint: disable=resource-leakage\n\n read_length = 0\n for chunk in body:\n read_length += len(chunk)\n fp.write(chunk)\n fp.close()\n return True\n\n # ClientException\n # file/dir exceptions\n except Exception as exc:\n log.error('There was an error::')\n if hasattr(exc, 'code') and hasattr(exc, 'msg'):\n log.error(' Code: %s: %s', exc.code, exc.msg)\n log.error(' Content: \\n%s', getattr(exc, 'read', lambda: six.text_type(exc))())\n return False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef put_object(self, cont, obj, local_file):\n '''\n Upload a file to Swift\n '''\n try:\n with salt.utils.files.fopen(local_file, 'rb') as fp_:\n self.conn.put_object(cont, obj, fp_)\n return True\n except Exception as exc:\n log.error('There was an error::')\n if hasattr(exc, 'code') and hasattr(exc, 'msg'):\n log.error(' Code: %s: %s', exc.code, exc.msg)\n log.error(' Content: \\n%s', getattr(exc, 'read', lambda: six.text_type(exc))())\n return False", "response": "Uploads a file to Swift\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete a file from Swift", "response": "def delete_object(self, cont, obj):\n '''\n Delete a file from Swift\n '''\n try:\n self.conn.delete_object(cont, obj)\n return True\n except Exception as exc:\n log.error('There was an error::')\n if hasattr(exc, 'code') and hasattr(exc, 'msg'):\n log.error(' Code: %s: %s', exc.code, exc.msg)\n log.error(' Content: \\n%s', getattr(exc, 'read', lambda: six.text_type(exc))())\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _xfs_info_get_kv(serialized):\n '''\n Parse one line of the XFS info output.\n '''\n # No need to know sub-elements here\n if serialized.startswith(\"=\"):\n serialized = serialized[1:].strip()\n\n serialized = serialized.replace(\" = \", \"=*** \").replace(\" =\", \"=\")\n\n # Keywords has no spaces, values do\n opt = []\n for tkn in serialized.split(\" \"):\n if not opt or \"=\" in tkn:\n opt.append(tkn)\n else:\n opt[len(opt) - 1] = opt[len(opt) - 1] + \" \" + tkn\n\n # Preserve ordering\n return [tuple(items.split(\"=\")) for items in opt]", "response": "Parse one line of the XFS info output."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nparse output from xfs_info or xfs_growfs - n.", "response": "def _parse_xfs_info(data):\n '''\n Parse output from \"xfs_info\" or \"xfs_growfs -n\".\n '''\n ret = {}\n spr = re.compile(r'\\s+')\n entry = None\n for line in [spr.sub(\" \", l).strip().replace(\", \", \" \") for l in data.split(\"\\n\")]:\n if not line:\n continue\n nfo = _xfs_info_get_kv(line)\n if not line.startswith(\"=\"):\n entry = nfo.pop(0)\n ret[entry[0]] = {'section': entry[(entry[1] != '***' and 1 or 0)]}\n ret[entry[0]].update(dict(nfo))\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef info(device):\n '''\n Get filesystem geometry information.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xfs.info /dev/sda1\n '''\n out = __salt__['cmd.run_all'](\"xfs_info {0}\".format(device))\n if out.get('stderr'):\n raise CommandExecutionError(out['stderr'].replace(\"xfs_info:\", \"\").strip())\n return _parse_xfs_info(out['stdout'])", "response": "Get filesystem geometry information."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _xfsdump_output(data):\n '''\n Parse CLI output of the xfsdump utility.\n '''\n out = {}\n summary = []\n summary_block = False\n\n for line in [l.strip() for l in data.split(\"\\n\") if l.strip()]:\n line = re.sub(\"^xfsdump: \", \"\", line)\n if line.startswith(\"session id:\"):\n out['Session ID'] = line.split(\" \")[-1]\n elif line.startswith(\"session label:\"):\n out['Session label'] = re.sub(\"^session label: \", \"\", line)\n elif line.startswith(\"media file size\"):\n out['Media size'] = re.sub(r\"^media file size\\s+\", \"\", line)\n elif line.startswith(\"dump complete:\"):\n out['Dump complete'] = re.sub(r\"^dump complete:\\s+\", \"\", line)\n elif line.startswith(\"Dump Status:\"):\n out['Status'] = re.sub(r\"^Dump Status:\\s+\", \"\", line)\n elif line.startswith(\"Dump Summary:\"):\n summary_block = True\n continue\n\n if line.startswith(\" \") and summary_block:\n summary.append(line.strip())\n elif not line.startswith(\" \") and summary_block:\n summary_block = False\n\n if summary:\n out['Summary'] = ' '.join(summary)\n\n return out", "response": "Parse the CLI output of the xfsdump utility."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndump filesystem device to the media file.", "response": "def dump(device, destination, level=0, label=None, noerase=None):\n '''\n Dump filesystem device to the media (file, tape etc).\n\n Required parameters:\n\n * **device**: XFS device, content of which to be dumped.\n * **destination**: Specifies a dump destination.\n\n Valid options are:\n\n * **label**: Label of the dump. Otherwise automatically generated label is used.\n * **level**: Specifies a dump level of 0 to 9.\n * **noerase**: Pre-erase media.\n\n Other options are not used in order to let ``xfsdump`` use its default\n values, as they are most optimal. See the ``xfsdump(8)`` manpage for\n a more complete description of these options.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xfs.dump /dev/sda1 /detination/on/the/client\n salt '*' xfs.dump /dev/sda1 /detination/on/the/client label='Company accountancy'\n salt '*' xfs.dump /dev/sda1 /detination/on/the/client noerase=True\n '''\n if not salt.utils.path.which(\"xfsdump\"):\n raise CommandExecutionError(\"Utility \\\"xfsdump\\\" has to be installed or missing.\")\n\n label = label and label or time.strftime(\"XFS dump for \\\"{0}\\\" of %Y.%m.%d, %H:%M\".format(device),\n time.localtime()).replace(\"'\", '\"')\n cmd = [\"xfsdump\"]\n cmd.append(\"-F\") # Force\n if not noerase:\n cmd.append(\"-E\") # pre-erase\n cmd.append(\"-L '{0}'\".format(label)) # Label\n cmd.append(\"-l {0}\".format(level)) # Dump level\n cmd.append(\"-f {0}\".format(destination)) # Media destination\n cmd.append(device) # Device\n\n cmd = ' '.join(cmd)\n out = __salt__['cmd.run_all'](cmd)\n _verify_run(out, cmd=cmd)\n\n return _xfsdump_output(out['stdout'])"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _xr_to_keyset(line):\n '''\n Parse xfsrestore output keyset elements.\n '''\n tkns = [elm for elm in line.strip().split(\":\", 1) if elm]\n if len(tkns) == 1:\n return \"'{0}': \".format(tkns[0])\n else:\n key, val = tkns\n return \"'{0}': '{1}',\".format(key.strip(), val.strip())", "response": "Parse xfsrestore output keyset elements."} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntransforming xfsrestore inventory data output to a Python dict source and evaluate it.", "response": "def _xfs_inventory_output(out):\n '''\n Transform xfsrestore inventory data output to a Python dict source and evaluate it.\n '''\n data = []\n out = [line for line in out.split(\"\\n\") if line.strip()]\n\n # No inventory yet\n if len(out) == 1 and 'restore status' in out[0].lower():\n return {'restore_status': out[0]}\n\n ident = 0\n data.append(\"{\")\n for line in out[:-1]:\n if len([elm for elm in line.strip().split(\":\") if elm]) == 1:\n n_ident = len(re.sub(\"[^\\t]\", \"\", line))\n if ident > n_ident:\n for step in range(ident):\n data.append(\"},\")\n ident = n_ident\n data.append(_xr_to_keyset(line))\n data.append(\"{\")\n else:\n data.append(_xr_to_keyset(line))\n for step in range(ident + 1):\n data.append(\"},\")\n data.append(\"},\")\n\n # We are evaling into a python dict, a json load\n # would be safer\n data = eval('\\n'.join(data))[0] # pylint: disable=W0123\n data['restore_status'] = out[-1]\n\n return data"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef prune_dump(sessionid):\n '''\n Prunes the dump session identified by the given session id.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xfs.prune_dump b74a3586-e52e-4a4a-8775-c3334fa8ea2c\n\n '''\n out = __salt__['cmd.run_all'](\"xfsinvutil -s {0} -F\".format(sessionid))\n _verify_run(out)\n\n data = _xfs_prune_output(out['stdout'], sessionid)\n if data:\n return data\n\n raise CommandExecutionError(\"Session UUID \\\"{0}\\\" was not found.\".format(sessionid))", "response": "Prune the dump session identified by the given session id."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _xfs_estimate_output(out):\n '''\n Parse xfs_estimate output.\n '''\n spc = re.compile(r\"\\s+\")\n data = {}\n for line in [l for l in out.split(\"\\n\") if l.strip()][1:]:\n directory, bsize, blocks, megabytes, logsize = spc.sub(\" \", line).split(\" \")\n data[directory] = {\n 'block _size': bsize,\n 'blocks': blocks,\n 'megabytes': megabytes,\n 'logsize': logsize,\n }\n\n return data", "response": "Parse xfs_estimate output.\n Parse xfs_estimate output.\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nestimating the space that an XFS filesystem would take.", "response": "def estimate(path):\n '''\n Estimate the space that an XFS filesystem will take.\n For each directory estimate the space that directory would take\n if it were copied to an XFS filesystem.\n Estimation does not cross mount points.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xfs.estimate /path/to/file\n salt '*' xfs.estimate /path/to/dir/*\n '''\n if not os.path.exists(path):\n raise CommandExecutionError(\"Path \\\"{0}\\\" was not found.\".format(path))\n\n out = __salt__['cmd.run_all'](\"xfs_estimate -v {0}\".format(path))\n _verify_run(out)\n\n return _xfs_estimate_output(out[\"stdout\"])"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a new file system on the specified device.", "response": "def mkfs(device, label=None, ssize=None, noforce=None,\n bso=None, gmo=None, ino=None, lso=None, rso=None, nmo=None, dso=None):\n '''\n Create a file system on the specified device. By default wipes out with force.\n\n General options:\n\n * **label**: Specify volume label.\n * **ssize**: Specify the fundamental sector size of the filesystem.\n * **noforce**: Do not force create filesystem, if disk is already formatted.\n\n Filesystem geometry options:\n\n * **bso**: Block size options.\n * **gmo**: Global metadata options.\n * **dso**: Data section options. These options specify the location, size,\n and other parameters of the data section of the filesystem.\n * **ino**: Inode options to specify the inode size of the filesystem, and other inode allocation parameters.\n * **lso**: Log section options.\n * **nmo**: Naming options.\n * **rso**: Realtime section options.\n\n See the ``mkfs.xfs(8)`` manpage for a more complete description of corresponding options description.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xfs.mkfs /dev/sda1\n salt '*' xfs.mkfs /dev/sda1 dso='su=32k,sw=6' noforce=True\n salt '*' xfs.mkfs /dev/sda1 dso='su=32k,sw=6' lso='logdev=/dev/sda2,size=10000b'\n '''\n\n getopts = lambda args: dict(((args and (\"=\" in args)\n and args or None)) and [kw.split(\"=\") for kw in args.split(\",\")] or [])\n cmd = [\"mkfs.xfs\"]\n if label:\n cmd.append(\"-L\")\n cmd.append(\"'{0}'\".format(label))\n\n if ssize:\n cmd.append(\"-s\")\n cmd.append(ssize)\n\n for switch, opts in [(\"-b\", bso), (\"-m\", gmo), (\"-n\", nmo), (\"-i\", ino),\n (\"-d\", dso), (\"-l\", lso), (\"-r\", rso)]:\n try:\n if getopts(opts):\n cmd.append(switch)\n cmd.append(opts)\n except Exception:\n raise CommandExecutionError(\"Wrong parameters \\\"{0}\\\" for option \\\"{1}\\\"\".format(opts, switch))\n\n if not noforce:\n cmd.append(\"-f\")\n cmd.append(device)\n\n cmd = ' '.join(cmd)\n out = __salt__['cmd.run_all'](cmd)\n _verify_run(out, cmd=cmd)\n\n return _parse_xfs_info(out['stdout'])"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef modify(device, label=None, lazy_counting=None, uuid=None):\n '''\n Modify parameters of an XFS filesystem.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xfs.modify /dev/sda1 label='My backup' lazy_counting=False\n salt '*' xfs.modify /dev/sda1 uuid=False\n salt '*' xfs.modify /dev/sda1 uuid=True\n '''\n if not label and lazy_counting is None and uuid is None:\n raise CommandExecutionError(\"Nothing specified for modification for \\\"{0}\\\" device\".format(device))\n\n cmd = ['xfs_admin']\n if label:\n cmd.append(\"-L\")\n cmd.append(\"'{0}'\".format(label))\n\n if lazy_counting is False:\n cmd.append(\"-c\")\n cmd.append(\"0\")\n elif lazy_counting:\n cmd.append(\"-c\")\n cmd.append(\"1\")\n\n if uuid is False:\n cmd.append(\"-U\")\n cmd.append(\"nil\")\n elif uuid:\n cmd.append(\"-U\")\n cmd.append(\"generate\")\n cmd.append(device)\n\n cmd = ' '.join(cmd)\n _verify_run(__salt__['cmd.run_all'](cmd), cmd=cmd)\n\n out = __salt__['cmd.run_all'](\"blkid -o export {0}\".format(device))\n _verify_run(out)\n\n return _blkid_output(out['stdout'])", "response": "Modify parameters of an XFS filesystem."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_mounts():\n '''\n List mounted filesystems.\n '''\n mounts = {}\n with salt.utils.files.fopen(\"/proc/mounts\") as fhr:\n for line in salt.utils.data.decode(fhr.readlines()):\n device, mntpnt, fstype, options, fs_freq, fs_passno = line.strip().split(\" \")\n if fstype != 'xfs':\n continue\n mounts[device] = {\n 'mount_point': mntpnt,\n 'options': options.split(\",\"),\n }\n\n return mounts", "response": "Return a list of mounted filesystems."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef defragment(device):\n '''\n Defragment mounted XFS filesystem.\n In order to mount a filesystem, device should be properly mounted and writable.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' xfs.defragment /dev/sda1\n '''\n if device == '/':\n raise CommandExecutionError(\"Root is not a device.\")\n\n if not _get_mounts().get(device):\n raise CommandExecutionError(\"Device \\\"{0}\\\" is not mounted\".format(device))\n\n out = __salt__['cmd.run_all'](\"xfs_fsr {0}\".format(device))\n _verify_run(out)\n\n return {\n 'log': out['stdout']\n }", "response": "Defragment mounted XFS filesystem."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nreturn a checksum digest for a string", "response": "def digest(instr, checksum='md5'):\n '''\n Return a checksum digest for a string\n\n instr\n A string\n checksum : ``md5``\n The hashing algorithm to use to generate checksums. Valid options: md5,\n sha256, sha512.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' hashutil.digest 'get salted'\n '''\n hashing_funcs = {\n 'md5': __salt__['hashutil.md5_digest'],\n 'sha256': __salt__['hashutil.sha256_digest'],\n 'sha512': __salt__['hashutil.sha512_digest'],\n }\n hash_func = hashing_funcs.get(checksum)\n\n if hash_func is None:\n raise salt.exceptions.CommandExecutionError(\n \"Hash func '{0}' is not supported.\".format(checksum))\n\n return hash_func(instr)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef digest_file(infile, checksum='md5'):\n '''\n Return a checksum digest for a file\n\n infile\n A file path\n checksum : ``md5``\n The hashing algorithm to use to generate checksums. Wraps the\n :py:func:`hashutil.digest <salt.modules.hashutil.digest>` execution\n function.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' hashutil.digest_file /path/to/file\n '''\n if not __salt__['file.file_exists'](infile):\n raise salt.exceptions.CommandExecutionError(\n \"File path '{0}' not found.\".format(infile))\n\n with salt.utils.files.fopen(infile, 'rb') as f:\n file_hash = __salt__['hashutil.digest'](f.read(), checksum)\n\n return file_hash", "response": "Returns a checksum digest for a file"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef base64_encodefile(fname):\n '''\n Read a file from the file system and return as a base64 encoded string\n\n .. versionadded:: 2016.3.0\n\n Pillar example:\n\n .. code-block:: yaml\n\n path:\n to:\n data: |\n {{ salt.hashutil.base64_encodefile('/path/to/binary_file') | indent(6) }}\n\n The :py:func:`file.decode <salt.states.file.decode>` state function can be\n used to decode this data and write it to disk.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' hashutil.base64_encodefile /path/to/binary_file\n '''\n encoded_f = BytesIO()\n\n with salt.utils.files.fopen(fname, 'rb') as f:\n base64.encode(f, encoded_f)\n\n encoded_f.seek(0)\n return salt.utils.stringutils.to_str(encoded_f.read())", "response": "Read a file from the file system and return as a base64 encoded string"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef hmac_signature(string, shared_secret, challenge_hmac):\n '''\n Verify a challenging hmac signature against a string / shared-secret\n\n .. versionadded:: 2014.7.0\n\n Returns a boolean if the verification succeeded or failed.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' hashutil.hmac_signature 'get salted' 'shared secret' 'eBWf9bstXg+NiP5AOwppB5HMvZiYMPzEM9W5YMm/AmQ='\n '''\n return salt.utils.hashutils.hmac_signature(string, shared_secret, challenge_hmac)", "response": "Verify a challenging hmac signature against a string"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef github_signature(string, shared_secret, challenge_hmac):\n '''\n Verify a challenging hmac signature against a string / shared-secret for\n github webhooks.\n\n .. versionadded:: 2017.7.0\n\n Returns a boolean if the verification succeeded or failed.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' hashutil.github_signature '{\"ref\":....} ' 'shared secret' 'sha1=bc6550fc290acf5b42283fa8deaf55cea0f8c206'\n '''\n msg = string\n key = shared_secret\n hashtype, challenge = challenge_hmac.split('=')\n if six.text_type:\n msg = salt.utils.stringutils.to_bytes(msg)\n key = salt.utils.stringutils.to_bytes(key)\n hmac_hash = hmac.new(key, msg, getattr(hashlib, hashtype))\n return hmac_hash.hexdigest() == challenge", "response": "Verify a challenging hmac signature against a string"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef cmd(\n name,\n fun=None,\n arg=(),\n **kwargs):\n '''\n Execute a runner asynchronous:\n\n USAGE:\n\n .. code-block:: yaml\n\n run_cloud:\n wheel.cmd:\n - fun: key.delete\n - match: minion_id\n '''\n ret = {'name': name,\n 'changes': {},\n 'comment': '',\n 'result': True}\n if fun is None:\n fun = name\n client = salt.wheel.WheelClient(__opts__)\n low = {'fun': fun,\n 'arg': arg,\n 'kwargs': kwargs}\n client.cmd_async(low)\n return ret", "response": "Execute a runner asynchronous for a specific key"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nget inspectlib module for the lazy loader.", "response": "def _(module):\n '''\n Get inspectlib module for the lazy loader.\n\n :param module:\n :return:\n '''\n\n mod = None\n # pylint: disable=E0598\n try:\n # importlib is in Python 2.7+ and 3+\n import importlib\n mod = importlib.import_module(\"salt.modules.inspectlib.{0}\".format(module))\n except ImportError:\n # No importlib around (2.6)\n mod = getattr(__import__(\"salt.modules.inspectlib\", globals(), locals(), fromlist=[six.text_type(module)]), module)\n # pylint: enable=E0598\n\n mod.__grains__ = __grains__\n mod.__pillar__ = __pillar__\n mod.__salt__ = __salt__\n\n return mod"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef inspect(mode='all', priority=19, **kwargs):\n '''\n Start node inspection and save the data to the database for further query.\n\n Parameters:\n\n * **mode**: Clarify inspection mode: configuration, payload, all (default)\n\n payload\n * **filter**: Comma-separated directories to track payload.\n\n * **priority**: (advanced) Set priority of the inspection. Default is low priority.\n\n\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' inspector.inspect\n salt '*' inspector.inspect configuration\n salt '*' inspector.inspect payload filter=/opt,/ext/oracle\n '''\n collector = _(\"collector\")\n try:\n return collector.Inspector(cachedir=__opts__['cachedir'],\n piddir=os.path.dirname(__opts__['pidfile']))\\\n .request_snapshot(mode, priority=priority, **kwargs)\n except InspectorSnapshotException as ex:\n raise CommandExecutionError(ex)\n except Exception as ex:\n log.error(_get_error_message(ex))\n raise Exception(ex)", "response": "Get the node information from the server and save it to the database."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nquerying the node for specific information.", "response": "def query(*args, **kwargs):\n '''\n Query the node for specific information.\n\n Parameters:\n\n * **scope**: Specify scope of the query.\n\n * **System**: Return system data.\n\n * **Software**: Return software information.\n\n * **Services**: Return known services.\n\n * **Identity**: Return user accounts information for this system.\n accounts\n Can be either 'local', 'remote' or 'all' (equal to \"local,remote\").\n Remote accounts cannot be resolved on all systems, but only\n those, which supports 'passwd -S -a'.\n\n disabled\n True (or False, default) to return only disabled accounts.\n\n * **payload**: Payload scope parameters:\n filter\n Include only results which path starts from the filter string.\n\n time\n Display time in Unix ticks or format according to the configured TZ (default)\n Values: ticks, tz (default)\n\n size\n Format size. Values: B, KB, MB, GB\n\n type\n Include payload type.\n Values (comma-separated): directory (or dir), link, file (default)\n Example (returns everything): type=directory,link,file\n\n owners\n Resolve UID/GID to an actual names or leave them numeric (default).\n Values: name (default), id\n\n brief\n Return just a list of payload elements, if True. Default: False.\n\n * **all**: Return all information (default).\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' inspector.query scope=system\n salt '*' inspector.query scope=payload type=file,link filter=/etc size=Kb brief=False\n '''\n query = _(\"query\")\n try:\n return query.Query(kwargs.get('scope'), cachedir=__opts__['cachedir'])(*args, **kwargs)\n except InspectorQueryException as ex:\n raise CommandExecutionError(ex)\n except Exception as ex:\n log.error(_get_error_message(ex))\n raise Exception(ex)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef build(format='qcow2', path='/tmp/'):\n '''\n Build an image from a current system description.\n The image is a system image can be output in bootable ISO or QCOW2 formats.\n\n Node uses the image building library Kiwi to perform the actual build.\n\n Parameters:\n\n * **format**: Specifies output format: \"qcow2\" or \"iso. Default: `qcow2`.\n * **path**: Specifies output path where to store built image. Default: `/tmp`.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion inspector.build\n salt myminion inspector.build format=iso path=/opt/builds/\n '''\n try:\n _(\"collector\").Inspector(cachedir=__opts__['cachedir'],\n piddir=os.path.dirname(__opts__['pidfile']),\n pidfilename='').reuse_snapshot().build(format=format, path=path)\n except InspectorKiwiProcessorException as ex:\n raise CommandExecutionError(ex)\n except Exception as ex:\n log.error(_get_error_message(ex))\n raise Exception(ex)", "response": "Build an image from a current system description."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nexports an image description for Kiwi.", "response": "def export(local=False, path=\"/tmp\", format='qcow2'):\n '''\n Export an image description for Kiwi.\n\n Parameters:\n\n * **local**: Specifies True or False if the export has to be in the local file. Default: False.\n * **path**: If `local=True`, then specifies the path where file with the Kiwi description is written.\n Default: `/tmp`.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion inspector.export\n salt myminion inspector.export format=iso path=/opt/builds/\n '''\n if getpass.getuser() != 'root':\n raise CommandExecutionError('In order to export system, the minion should run as \"root\".')\n try:\n description = _(\"query\").Query('all', cachedir=__opts__['cachedir'])()\n return _(\"collector\").Inspector().reuse_snapshot().export(description, local=local, path=path, format=format)\n except InspectorKiwiProcessorException as ex:\n raise CommandExecutionError(ex)\n except Exception as ex:\n log.error(_get_error_message(ex))\n raise Exception(ex)"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlist current description snapshots.", "response": "def snapshots():\n '''\n List current description snapshots.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt myminion inspector.snapshots\n '''\n try:\n return _(\"collector\").Inspector(cachedir=__opts__['cachedir'],\n piddir=os.path.dirname(__opts__['pidfile'])).db.list()\n except InspectorSnapshotException as err:\n raise CommandExecutionError(err)\n except Exception as err:\n log.error(_get_error_message(err))\n raise Exception(err)"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ndeletes description snapshots from the system.", "response": "def delete(all=False, *databases):\n '''\n Remove description snapshots from the system.\n\n ::parameter: all. Default: False. Remove all snapshots, if set to True.\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion inspector.delete <ID> <ID1> <ID2>..\n salt myminion inspector.delete all=True\n '''\n if not all and not databases:\n raise CommandExecutionError('At least one database ID required.')\n\n try:\n ret = dict()\n inspector = _(\"collector\").Inspector(cachedir=__opts__['cachedir'],\n piddir=os.path.dirname(__opts__['pidfile']))\n for dbid in all and inspector.db.list() or databases:\n ret[dbid] = inspector.db._db.purge(six.text_type(dbid))\n return ret\n except InspectorSnapshotException as err:\n raise CommandExecutionError(err)\n except Exception as err:\n log.error(_get_error_message(err))\n raise Exception(err)"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nstart a new instance of the NX - Log service.", "response": "def start(transport='zmq',\n address='0.0.0.0',\n port=49017,\n auth_address='0.0.0.0',\n auth_port=49018,\n disable_security=False,\n certificate=None,\n os_whitelist=None,\n os_blacklist=None,\n error_whitelist=None,\n error_blacklist=None,\n host_whitelist=None,\n host_blacklist=None):\n '''\n Listen to napalm-logs and publish events into the Salt event bus.\n\n transport: ``zmq``\n Choose the desired transport.\n\n .. note::\n Currently ``zmq`` is the only valid option.\n\n address: ``0.0.0.0``\n The address of the publisher, as configured on napalm-logs.\n\n port: ``49017``\n The port of the publisher, as configured on napalm-logs.\n\n auth_address: ``0.0.0.0``\n The address used for authentication\n when security is not disabled.\n\n auth_port: ``49018``\n Port used for authentication.\n\n disable_security: ``False``\n Trust unencrypted messages.\n Strongly discouraged in production.\n\n certificate: ``None``\n Absolute path to the SSL certificate.\n\n os_whitelist: ``None``\n List of operating systems allowed. By default everything is allowed.\n\n os_blacklist: ``None``\n List of operating system to be ignored. Nothing ignored by default.\n\n error_whitelist: ``None``\n List of errors allowed.\n\n error_blacklist: ``None``\n List of errors ignored.\n\n host_whitelist: ``None``\n List of hosts or IPs to be allowed.\n\n host_blacklist: ``None``\n List of hosts of IPs to be ignored.\n '''\n if not disable_security:\n if not certificate:\n log.critical('Please use a certificate, or disable the security.')\n return\n auth = napalm_logs.utils.ClientAuth(certificate,\n address=auth_address,\n port=auth_port)\n\n transport_recv_fun = _get_transport_recv(name=transport,\n address=address,\n port=port)\n if not transport_recv_fun:\n log.critical('Unable to start the engine', exc_info=True)\n return\n master = False\n if __opts__['__role'] == 'master':\n master = True\n while True:\n log.debug('Waiting for napalm-logs to send anything...')\n raw_object = transport_recv_fun()\n log.debug('Received from napalm-logs:')\n log.debug(raw_object)\n if not disable_security:\n dict_object = auth.decrypt(raw_object)\n else:\n dict_object = napalm_logs.utils.unserialize(raw_object)\n try:\n event_os = dict_object['os']\n if os_blacklist or os_whitelist:\n valid_os = salt.utils.stringutils.check_whitelist_blacklist(\n event_os,\n whitelist=os_whitelist,\n blacklist=os_blacklist)\n if not valid_os:\n log.info('Ignoring NOS %s as per whitelist/blacklist', event_os)\n continue\n event_error = dict_object['error']\n if error_blacklist or error_whitelist:\n valid_error = salt.utils.stringutils.check_whitelist_blacklist(\n event_error,\n whitelist=error_whitelist,\n blacklist=error_blacklist)\n if not valid_error:\n log.info('Ignoring error %s as per whitelist/blacklist', event_error)\n continue\n event_host = dict_object.get('host') or dict_object.get('ip')\n if host_blacklist or host_whitelist:\n valid_host = salt.utils.stringutils.check_whitelist_blacklist(\n event_host,\n whitelist=host_whitelist,\n blacklist=host_blacklist)\n if not valid_host:\n log.info('Ignoring messages from %s as per whitelist/blacklist', event_host)\n continue\n tag = 'napalm/syslog/{os}/{error}/{host}'.format(\n os=event_os,\n error=event_error,\n host=event_host\n )\n except KeyError as kerr:\n log.warning('Missing keys from the napalm-logs object:', exc_info=True)\n log.warning(dict_object)\n continue # jump to the next object in the queue\n log.debug('Sending event %s', tag)\n log.debug(raw_object)\n if master:\n event.get_master_event(__opts__,\n __opts__['sock_dir']\n ).fire_event(dict_object, tag)\n else:\n __salt__['event.send'](tag, dict_object)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef configure_proxy(name, proxyname='p8000', start=True):\n '''\n Create the salt proxy file and start the proxy process\n if required\n\n Parameters:\n name:\n The name of this state\n proxyname:\n Name to be used for this proxy (should match entries in pillar)\n start:\n Boolean indicating if the process should be started\n\n Example:\n\n ..code-block:: yaml\n\n salt-proxy-configure:\n salt_proxy.configure_proxy:\n - proxyname: p8000\n - start: True\n\n '''\n ret = __salt__['salt_proxy.configure_proxy'](proxyname,\n start=start)\n ret.update({\n 'name': name,\n 'comment': '{0} config messages'.format(name)\n })\n return ret", "response": "Configure the salt proxy process"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _hadoop_cmd(module, command, *args):\n '''\n Hadoop/hdfs command wrapper\n\n As Hadoop command has been deprecated this module will default\n to use hdfs command and fall back to hadoop if it is not found\n\n In order to prevent random execution the module name is checked\n\n Follows hadoop command template:\n hadoop module -command args\n E.g.: hadoop dfs -ls /\n '''\n tool = 'hadoop'\n if salt.utils.path.which('hdfs'):\n tool = 'hdfs'\n\n out = None\n if module and command:\n if module in __authorized_modules__:\n mappings = {'tool': tool, 'module': module, 'command': command, 'args': ' '.join(args)}\n cmd = '{tool} {module} -{command} {args}'.format(**mappings)\n out = __salt__['cmd.run'](cmd, python_shell=False)\n else:\n return 'Error: Unknown module'\n else:\n return 'Error: Module and command not defined'\n return out", "response": "Wrapper for hadoop command"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef dfs_present(path):\n '''\n Check if a file or directory is present on the distributed FS.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' hadoop.dfs_present /some_random_file\n\n Returns True if the file is present\n '''\n cmd_return = _hadoop_cmd('dfs', 'stat', path)\n match = 'No such file or directory'\n return False if match in cmd_return else True", "response": "Check if a file or directory is present on the distributed FS."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nchecks if a file or directory is absent on the distributed FS", "response": "def dfs_absent(path):\n '''\n Check if a file or directory is absent on the distributed FS.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' hadoop.dfs_absent /some_random_file\n\n Returns True if the file is absent\n '''\n cmd_return = _hadoop_cmd('dfs', 'stat', path)\n match = 'No such file or directory'\n return True if match in cmd_return else False"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nwaiting for subprocess to terminate and return subprocess", "response": "def run(self):\n '''\n wait for subprocess to terminate and return subprocess' return code.\n If timeout is reached, throw TimedProcTimeoutError\n '''\n def receive():\n if self.with_communicate:\n self.stdout, self.stderr = self.process.communicate(input=self.stdin)\n elif self.wait:\n self.process.wait()\n\n if not self.timeout:\n receive()\n else:\n rt = threading.Thread(target=receive)\n rt.start()\n rt.join(self.timeout)\n if rt.isAlive():\n # Subprocess cleanup (best effort)\n self.process.kill()\n\n def terminate():\n if rt.isAlive():\n self.process.terminate()\n threading.Timer(10, terminate).start()\n raise salt.exceptions.TimedProcTimeoutError(\n '{0} : Timed out after {1} seconds'.format(\n self.command,\n six.text_type(self.timeout),\n )\n )\n return self.process.returncode"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_full_stream(stream_name, region=None, key=None, keyid=None, profile=None):\n '''\n Get complete stream info from AWS, via describe_stream, including all shards.\n\n CLI example::\n\n salt myminion boto_kinesis._get_full_stream my_stream region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n r = {}\n stream = _get_basic_stream(stream_name, conn)['result']\n full_stream = stream\n\n # iterate through if there are > 100 shards (max that AWS will return from describe_stream)\n while stream[\"StreamDescription\"][\"HasMoreShards\"]:\n stream = _execute_with_retries(conn,\n \"describe_stream\",\n StreamName=stream_name,\n ExclusiveStartShardId=stream[\"StreamDescription\"][\"Shards\"][-1][\"ShardId\"])\n stream = stream['result']\n full_stream[\"StreamDescription\"][\"Shards\"] += stream[\"StreamDescription\"][\"Shards\"]\n\n r['result'] = full_stream\n return r", "response": "Get complete stream info from AWS via describe_stream"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ngetting the stream info from AWS returning only when the stream is in the ACTIVE state.", "response": "def get_stream_when_active(stream_name, region=None, key=None, keyid=None, profile=None):\n '''\n Get complete stream info from AWS, returning only when the stream is in the ACTIVE state.\n Continues to retry when stream is updating or creating.\n If the stream is deleted during retries, the loop will catch the error and break.\n\n CLI example::\n\n salt myminion boto_kinesis.get_stream_when_active my_stream region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n\n stream_status = None\n # only get basic stream until it's active,\n # so we don't pull the full list of shards repeatedly (in case of very large stream)\n attempt = 0\n max_retry_delay = 10\n while stream_status != \"ACTIVE\":\n time.sleep(_jittered_backoff(attempt, max_retry_delay))\n attempt += 1\n stream_response = _get_basic_stream(stream_name, conn)\n if 'error' in stream_response:\n return stream_response\n stream_status = stream_response['result'][\"StreamDescription\"][\"StreamStatus\"]\n\n # now it's active, get the full stream if necessary\n if stream_response['result'][\"StreamDescription\"][\"HasMoreShards\"]:\n stream_response = _get_full_stream(stream_name, region, key, keyid, profile)\n\n return stream_response"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef exists(stream_name, region=None, key=None, keyid=None, profile=None):\n '''\n Check if the stream exists. Returns False and the error if it does not.\n\n CLI example::\n\n salt myminion boto_kinesis.exists my_stream region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n r = {}\n\n stream = _get_basic_stream(stream_name, conn)\n if 'error' in stream:\n r['result'] = False\n r['error'] = stream['error']\n else:\n r['result'] = True\n\n return r", "response": "Check if the stream exists. Returns False and the error if it does not."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ncreate a stream with name stream_name and initial number of shards num_shards.", "response": "def create_stream(stream_name, num_shards, region=None, key=None, keyid=None, profile=None):\n '''\n Create a stream with name stream_name and initial number of shards num_shards.\n\n CLI example::\n\n salt myminion boto_kinesis.create_stream my_stream N region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n r = _execute_with_retries(conn,\n \"create_stream\",\n ShardCount=num_shards,\n StreamName=stream_name)\n if 'error' not in r:\n r['result'] = True\n return r"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndeleting the stream with name stream_name. This cannot be undone! All data will be lost!!", "response": "def delete_stream(stream_name, region=None, key=None, keyid=None, profile=None):\n '''\n Delete the stream with name stream_name. This cannot be undone! All data will be lost!!\n\n CLI example::\n\n salt myminion boto_kinesis.delete_stream my_stream region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n r = _execute_with_retries(conn,\n \"delete_stream\",\n StreamName=stream_name)\n if 'error' not in r:\n r['result'] = True\n return r"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nincrease stream retention period to retention_hours", "response": "def increase_stream_retention_period(stream_name, retention_hours,\n region=None, key=None, keyid=None, profile=None):\n '''\n Increase stream retention period to retention_hours\n\n CLI example::\n\n salt myminion boto_kinesis.increase_stream_retention_period my_stream N region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n r = _execute_with_retries(conn,\n \"increase_stream_retention_period\",\n StreamName=stream_name,\n RetentionPeriodHours=retention_hours)\n if 'error' not in r:\n r['result'] = True\n return r"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nenables enhanced monitoring for the specified shard - level metrics on the specified stream.", "response": "def enable_enhanced_monitoring(stream_name, metrics,\n region=None, key=None, keyid=None, profile=None):\n '''\n Enable enhanced monitoring for the specified shard-level metrics on stream stream_name\n\n CLI example::\n\n salt myminion boto_kinesis.enable_enhanced_monitoring my_stream [\"metrics\", \"to\", \"enable\"] region=us-east-1\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n r = _execute_with_retries(conn,\n \"enable_enhanced_monitoring\",\n StreamName=stream_name,\n ShardLevelMetrics=metrics)\n\n if 'error' not in r:\n r['result'] = True\n return r"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngetting some data for the current re - sharddard.", "response": "def get_info_for_reshard(stream_details):\n \"\"\"\n Collect some data: number of open shards, key range, etc.\n Modifies stream_details to add a sorted list of OpenShards.\n Returns (min_hash_key, max_hash_key, stream_details)\n\n CLI example::\n\n salt myminion boto_kinesis.get_info_for_reshard existing_stream_details\n \"\"\"\n min_hash_key = 0\n max_hash_key = 0\n stream_details[\"OpenShards\"] = []\n for shard in stream_details[\"Shards\"]:\n shard_id = shard[\"ShardId\"]\n if \"EndingSequenceNumber\" in shard[\"SequenceNumberRange\"]:\n # EndingSequenceNumber is null for open shards, so this shard must be closed\n log.debug(\"skipping closed shard %s\", shard_id)\n continue\n stream_details[\"OpenShards\"].append(shard)\n shard[\"HashKeyRange\"][\"StartingHashKey\"] = long_int(\n shard[\"HashKeyRange\"][\"StartingHashKey\"])\n shard[\"HashKeyRange\"][\"EndingHashKey\"] = long_int(\n shard[\"HashKeyRange\"][\"EndingHashKey\"])\n if shard[\"HashKeyRange\"][\"StartingHashKey\"] < min_hash_key:\n min_hash_key = shard[\"HashKeyRange\"][\"StartingHashKey\"]\n if shard[\"HashKeyRange\"][\"EndingHashKey\"] > max_hash_key:\n max_hash_key = shard[\"HashKeyRange\"][\"EndingHashKey\"]\n stream_details[\"OpenShards\"].sort(key=lambda shard: long_int(\n shard[\"HashKeyRange\"][\"StartingHashKey\"]))\n return min_hash_key, max_hash_key, stream_details"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list_streams(region=None, key=None, keyid=None, profile=None):\n '''\n Return a list of all streams visible to the current account\n\n CLI example:\n\n .. code-block:: bash\n\n salt myminion boto_kinesis.list_streams\n '''\n conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)\n streams = []\n exclusive_start_stream_name = ''\n while exclusive_start_stream_name is not None:\n args = {'ExclusiveStartStreamName': exclusive_start_stream_name} if exclusive_start_stream_name else {}\n ret = _execute_with_retries(conn, 'list_streams', **args)\n if 'error' in ret:\n return ret\n ret = ret['result'] if ret and ret.get('result') else {}\n streams += ret.get('StreamNames', [])\n exclusive_start_stream_name = streams[-1] if ret.get('HasMoreStreams', False) in (True, 'true') else None\n return {'result': streams}", "response": "Return a list of all streams visible to the current account\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_next_open_shard(stream_details, shard_id):\n '''\n Return the next open shard after shard_id\n\n CLI example::\n\n salt myminion boto_kinesis._get_next_open_shard existing_stream_details shard_id\n '''\n found = False\n for shard in stream_details[\"OpenShards\"]:\n current_shard_id = shard[\"ShardId\"]\n if current_shard_id == shard_id:\n found = True\n continue\n if found:\n return current_shard_id", "response": "Return the next open shard after shard_id CLI example ::\n\nAttributeNames salt myminion boto_kinesis _get_next_open_shard"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _execute_with_retries(conn, function, **kwargs):\n '''\n Retry if we're rate limited by AWS or blocked by another call.\n Give up and return error message if resource not found or argument is invalid.\n\n conn\n The connection established by the calling method via _get_conn()\n\n function\n The function to call on conn. i.e. create_stream\n\n **kwargs\n Any kwargs required by the above function, with their keywords\n i.e. StreamName=stream_name\n\n Returns:\n The result dict with the HTTP response and JSON data if applicable\n as 'result', or an error as 'error'\n\n CLI example::\n\n salt myminion boto_kinesis._execute_with_retries existing_conn function_name function_kwargs\n\n '''\n r = {}\n max_attempts = 18\n max_retry_delay = 10\n for attempt in range(max_attempts):\n log.info(\"attempt: %s function: %s\", attempt, function)\n try:\n fn = getattr(conn, function)\n r['result'] = fn(**kwargs)\n return r\n except botocore.exceptions.ClientError as e:\n error_code = e.response['Error']['Code']\n if \"LimitExceededException\" in error_code or \"ResourceInUseException\" in error_code:\n # could be rate limited by AWS or another command is blocking,\n # retry with exponential backoff\n log.debug(\"Retrying due to AWS exception\", exc_info=True)\n time.sleep(_jittered_backoff(attempt, max_retry_delay))\n else:\n # ResourceNotFoundException or InvalidArgumentException\n r['error'] = e.response['Error']\n log.error(r['error'])\n r['result'] = None\n return r\n\n r['error'] = \"Tried to execute function {0} {1} times, but was unable\".format(function, max_attempts)\n log.error(r['error'])\n return r", "response": "Execute a function on the specified resource and return the HTTP response and JSON data if successful."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nsets a single key to a single value", "response": "def apply(key, value):\n '''\n Set a single key\n\n .. note::\n\n This will strip comments from your config file\n '''\n path = __opts__['conf_file']\n if os.path.isdir(path):\n path = os.path.join(path, 'master')\n data = values()\n data[key] = value\n with salt.utils.files.fopen(path, 'w+') as fp_:\n salt.utils.yaml.safe_dump(data, default_flow_style=False)"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef update_config(file_name, yaml_contents):\n '''\n Update master config with\n ``yaml_contents``.\n\n Writes ``yaml_contents`` to a file named\n ``file_name.conf`` under the folder\n specified by ``default_include``.\n This folder is named ``master.d`` by\n default. Please look at\n :conf_master:`include-configuration`\n for more information.\n\n Example low data:\n\n .. code-block:: python\n\n data = {\n 'username': 'salt',\n 'password': 'salt',\n 'fun': 'config.update_config',\n 'file_name': 'gui',\n 'yaml_contents': {'id': 1},\n 'client': 'wheel',\n 'eauth': 'pam',\n }\n '''\n file_name = '{0}{1}'.format(file_name, '.conf')\n dir_path = os.path.join(__opts__['config_dir'],\n os.path.dirname(__opts__['default_include']))\n try:\n yaml_out = salt.utils.yaml.safe_dump(yaml_contents, default_flow_style=False)\n\n if not os.path.exists(dir_path):\n log.debug('Creating directory %s', dir_path)\n os.makedirs(dir_path, 0o755)\n\n file_path = os.path.join(dir_path, file_name)\n with salt.utils.files.fopen(file_path, 'w') as fp_:\n fp_.write(yaml_out)\n\n return 'Wrote {0}'.format(file_name)\n except (IOError, OSError, salt.utils.yaml.YAMLError, ValueError) as err:\n return six.text_type(err)", "response": "Update master config with yaml_contents."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nget PPA info from launchpad.", "response": "def _get_ppa_info_from_launchpad(owner_name, ppa_name):\n '''\n Idea from softwareproperties.ppa.\n Uses urllib2 which sacrifices server cert verification.\n\n This is used as fall-back code or for secure PPAs\n\n :param owner_name:\n :param ppa_name:\n :return:\n '''\n\n lp_url = 'https://launchpad.net/api/1.0/~{0}/+archive/{1}'.format(\n owner_name, ppa_name)\n request = _Request(lp_url, headers={'Accept': 'application/json'})\n lp_page = _urlopen(request)\n return salt.utils.json.load(lp_page)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _call_apt(args, scope=True, **kwargs):\n '''\n Call apt* utilities.\n '''\n cmd = []\n if scope and salt.utils.systemd.has_scope(__context__) and __salt__['config.get']('systemd.scope', True):\n cmd.extend(['systemd-run', '--scope'])\n cmd.extend(args)\n\n params = {'output_loglevel': 'trace',\n 'python_shell': False,\n 'env': salt.utils.environment.get_module_environment(globals())}\n params.update(kwargs)\n\n return __salt__['cmd.run_all'](cmd, **params)", "response": "Call apt * utilities. apt"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef refresh_db(cache_valid_time=0, failhard=False, **kwargs):\n '''\n Updates the APT database to latest packages based upon repositories\n\n Returns a dict, with the keys being package databases and the values being\n the result of the update attempt. Values can be one of the following:\n\n - ``True``: Database updated successfully\n - ``False``: Problem updating database\n - ``None``: Database already up-to-date\n\n cache_valid_time\n\n .. versionadded:: 2016.11.0\n\n Skip refreshing the package database if refresh has already occurred within\n <value> seconds\n\n failhard\n\n If False, return results of Err lines as ``False`` for the package database that\n encountered the error.\n If True, raise an error with a list of the package databases that encountered\n errors.\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 failhard = salt.utils.data.is_true(failhard)\n ret = {}\n error_repos = list()\n\n if cache_valid_time:\n try:\n latest_update = os.stat(APT_LISTS_PATH).st_mtime\n now = time.time()\n log.debug(\"now: %s, last update time: %s, expire after: %s seconds\", now, latest_update, cache_valid_time)\n if latest_update + cache_valid_time > now:\n return ret\n except TypeError as exp:\n log.warning(\"expected integer for cache_valid_time parameter, failed with: %s\", exp)\n except IOError as exp:\n log.warning(\"could not stat cache directory due to: %s\", exp)\n\n call = _call_apt(['apt-get', '-q', 'update'], scope=False)\n if call['retcode'] != 0:\n comment = ''\n if 'stderr' in call:\n comment += call['stderr']\n\n raise CommandExecutionError(comment)\n else:\n out = call['stdout']\n\n for line in out.splitlines():\n cols = line.split()\n if not cols:\n continue\n ident = ' '.join(cols[1:])\n if 'Get' in cols[0]:\n # Strip filesize from end of line\n ident = re.sub(r' \\[.+B\\]$', '', ident)\n ret[ident] = True\n elif 'Ign' in cols[0]:\n ret[ident] = False\n elif 'Hit' in cols[0]:\n ret[ident] = None\n elif 'Err' in cols[0]:\n ret[ident] = False\n error_repos.append(ident)\n\n if failhard and error_repos:\n raise CommandExecutionError('Error getting repos: {0}'.format(', '.join(error_repos)))\n\n return ret", "response": "Updates the APT database to latest packages based upon repositories."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef install(name=None,\n refresh=False,\n fromrepo=None,\n skip_verify=False,\n debconf=None,\n pkgs=None,\n sources=None,\n reinstall=False,\n ignore_epoch=False,\n **kwargs):\n '''\n .. versionchanged:: 2015.8.12,2016.3.3,2016.11.0\n On minions running systemd>=205, `systemd-run(1)`_ is now used to\n isolate commands which modify installed packages from the\n ``salt-minion`` daemon's control group. This is done to keep systemd\n from killing any apt-get/dpkg commands spawned by Salt when the\n ``salt-minion`` service is restarted. (see ``KillMode`` in the\n `systemd.kill(5)`_ manpage for more information). If desired, usage of\n `systemd-run(1)`_ can be suppressed by setting a :mod:`config option\n <salt.modules.config.get>` called ``systemd.scope``, with a value of\n ``False`` (no quotes).\n\n .. _`systemd-run(1)`: https://www.freedesktop.org/software/systemd/man/systemd-run.html\n .. _`systemd.kill(5)`: https://www.freedesktop.org/software/systemd/man/systemd.kill.html\n\n Install the passed package, add refresh=True to update the dpkg database.\n\n name\n The name of the package to be installed. Note that this parameter is\n ignored if either \"pkgs\" or \"sources\" is passed. Additionally, please\n note that this option can only be used to install packages from a\n software repository. To install a package file manually, use the\n \"sources\" option.\n\n 32-bit packages can be installed on 64-bit systems by appending the\n architecture designation (``:i386``, etc.) to the end of the package\n name.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install <package name>\n\n refresh\n Whether or not to refresh the package database before installing.\n\n cache_valid_time\n\n .. versionadded:: 2016.11.0\n\n Skip refreshing the package database if refresh has already occurred within\n <value> seconds\n\n fromrepo\n Specify a package repository to install from\n (e.g., ``apt-get -t unstable install somepackage``)\n\n skip_verify\n Skip the GPG verification check (e.g., ``--allow-unauthenticated``, or\n ``--force-bad-verify`` for install from package file).\n\n debconf\n Provide the path to a debconf answers file, processed before\n installation.\n\n version\n Install a specific version of the package, e.g. 1.2.3~0ubuntu0. Ignored\n if \"pkgs\" or \"sources\" is passed.\n\n .. versionchanged:: 2018.3.0\n version can now contain comparison operators (e.g. ``>1.2.3``,\n ``<=2.0``, etc.)\n\n reinstall : False\n Specifying reinstall=True will use ``apt-get install --reinstall``\n rather than simply ``apt-get install`` for requested packages that are\n already installed.\n\n If a version is specified with the requested package, then ``apt-get\n install --reinstall`` will only be used if the installed version\n matches the requested version.\n\n .. versionadded:: 2015.8.0\n\n ignore_epoch : False\n Only used when the version of a package is specified using a comparison\n operator (e.g. ``>4.1``). If set to ``True``, then the epoch will be\n ignored when comparing the currently-installed version to the desired\n version.\n\n .. versionadded:: 2018.3.0\n\n Multiple Package Installation Options:\n\n pkgs\n A list of packages to install from a software repository. Must be\n passed as a python list.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install pkgs='[\"foo\", \"bar\"]'\n salt '*' pkg.install pkgs='[\"foo\", {\"bar\": \"1.2.3-0ubuntu0\"}]'\n\n sources\n A list of DEB packages to install. Must be passed as a list of dicts,\n with the keys being package names, and the values being the source URI\n or local path to the package. Dependencies are automatically resolved\n and marked as auto-installed.\n\n 32-bit packages can be installed on 64-bit systems by appending the\n architecture designation (``:i386``, etc.) to the end of the package\n name.\n\n .. versionchanged:: 2014.7.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install sources='[{\"foo\": \"salt://foo.deb\"},{\"bar\": \"salt://bar.deb\"}]'\n\n force_yes\n Passes ``--force-yes`` to the apt-get command. Don't use this unless\n you know what you're doing.\n\n .. versionadded:: 0.17.4\n\n install_recommends\n Whether to install the packages marked as recommended. Default is True.\n\n .. versionadded:: 2015.5.0\n\n only_upgrade\n Only upgrade the packages, if they are already installed. Default is False.\n\n .. versionadded:: 2015.5.0\n\n force_conf_new\n Always install the new version of any configuration files.\n\n .. versionadded:: 2015.8.0\n\n Returns a dict containing the new package names and versions::\n\n {'<package>': {'old': '<old-version>',\n 'new': '<new-version>'}}\n '''\n _refresh_db = False\n if salt.utils.data.is_true(refresh):\n _refresh_db = True\n if 'version' in kwargs and kwargs['version']:\n _refresh_db = False\n _latest_version = latest_version(name,\n refresh=False,\n show_installed=True)\n _version = kwargs.get('version')\n # If the versions don't match, refresh is True, otherwise no need\n # to refresh\n if not _latest_version == _version:\n _refresh_db = True\n\n if pkgs:\n _refresh_db = False\n for pkg in pkgs:\n if isinstance(pkg, dict):\n _name = next(six.iterkeys(pkg))\n _latest_version = latest_version(_name,\n refresh=False,\n show_installed=True)\n _version = pkg[_name]\n # If the versions don't match, refresh is True, otherwise\n # no need to refresh\n if not _latest_version == _version:\n _refresh_db = True\n else:\n # No version specified, so refresh should be True\n _refresh_db = True\n\n if debconf:\n __salt__['debconf.set_file'](debconf)\n\n try:\n pkg_params, pkg_type = __salt__['pkg_resource.parse_targets'](\n name, pkgs, sources, **kwargs\n )\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n # Support old \"repo\" argument\n repo = kwargs.get('repo', '')\n if not fromrepo and repo:\n fromrepo = repo\n\n if not pkg_params:\n return {}\n\n cmd_prefix = []\n\n old = list_pkgs()\n targets = []\n downgrade = []\n to_reinstall = {}\n errors = []\n if pkg_type == 'repository':\n pkg_params_items = list(six.iteritems(pkg_params))\n has_comparison = [x for x, y in pkg_params_items\n if y is not None\n and (y.startswith('<') or y.startswith('>'))]\n _available = list_repo_pkgs(*has_comparison, byrepo=False, **kwargs) \\\n if has_comparison else {}\n # Build command prefix\n cmd_prefix.extend(['apt-get', '-q', '-y'])\n if kwargs.get('force_yes', False):\n cmd_prefix.append('--force-yes')\n if 'force_conf_new' in kwargs and kwargs['force_conf_new']:\n cmd_prefix.extend(['-o', 'DPkg::Options::=--force-confnew'])\n else:\n cmd_prefix.extend(['-o', 'DPkg::Options::=--force-confold'])\n cmd_prefix += ['-o', 'DPkg::Options::=--force-confdef']\n if 'install_recommends' in kwargs:\n if not kwargs['install_recommends']:\n cmd_prefix.append('--no-install-recommends')\n else:\n cmd_prefix.append('--install-recommends')\n if 'only_upgrade' in kwargs and kwargs['only_upgrade']:\n cmd_prefix.append('--only-upgrade')\n if skip_verify:\n cmd_prefix.append('--allow-unauthenticated')\n if fromrepo:\n cmd_prefix.extend(['-t', fromrepo])\n cmd_prefix.append('install')\n else:\n pkg_params_items = []\n for pkg_source in pkg_params:\n if 'lowpkg.bin_pkg_info' in __salt__:\n deb_info = __salt__['lowpkg.bin_pkg_info'](pkg_source)\n else:\n deb_info = None\n if deb_info is None:\n log.error(\n 'pkg.install: Unable to get deb information for %s. '\n 'Version comparisons will be unavailable.', pkg_source\n )\n pkg_params_items.append([pkg_source])\n else:\n pkg_params_items.append(\n [deb_info['name'], pkg_source, deb_info['version']]\n )\n # Build command prefix\n if 'force_conf_new' in kwargs and kwargs['force_conf_new']:\n cmd_prefix.extend(['dpkg', '-i', '--force-confnew'])\n else:\n cmd_prefix.extend(['dpkg', '-i', '--force-confold'])\n if skip_verify:\n cmd_prefix.append('--force-bad-verify')\n if HAS_APT:\n _resolve_deps(name, pkg_params, **kwargs)\n\n for pkg_item_list in pkg_params_items:\n if pkg_type == 'repository':\n pkgname, version_num = pkg_item_list\n if name \\\n and pkgs is None \\\n and kwargs.get('version') \\\n and len(pkg_params) == 1:\n # Only use the 'version' param if 'name' was not specified as a\n # comma-separated list\n version_num = kwargs['version']\n else:\n try:\n pkgname, pkgpath, version_num = pkg_item_list\n except ValueError:\n pkgname = None\n pkgpath = pkg_item_list[0]\n version_num = None\n\n if version_num is None:\n if pkg_type == 'repository':\n if reinstall and pkgname in old:\n to_reinstall[pkgname] = pkgname\n else:\n targets.append(pkgname)\n else:\n targets.append(pkgpath)\n else:\n # If we are installing a package file and not one from the repo,\n # and version_num is not None, then we can assume that pkgname is\n # not None, since the only way version_num is not None is if DEB\n # metadata parsing was successful.\n if pkg_type == 'repository':\n # Remove leading equals sign(s) to keep from building a pkgstr\n # with multiple equals (which would be invalid)\n version_num = version_num.lstrip('=')\n if pkgname in has_comparison:\n candidates = _available.get(pkgname, [])\n target = salt.utils.pkg.match_version(\n version_num,\n candidates,\n cmp_func=version_cmp,\n ignore_epoch=ignore_epoch,\n )\n if target is None:\n errors.append(\n 'No version matching \\'{0}{1}\\' could be found '\n '(available: {2})'.format(\n pkgname,\n version_num,\n ', '.join(candidates) if candidates else None\n )\n )\n continue\n else:\n version_num = target\n pkgstr = '{0}={1}'.format(pkgname, version_num)\n else:\n pkgstr = pkgpath\n\n cver = old.get(pkgname, '')\n if reinstall and cver \\\n and salt.utils.versions.compare(ver1=version_num,\n oper='==',\n ver2=cver,\n cmp_func=version_cmp):\n to_reinstall[pkgname] = pkgstr\n elif not cver or salt.utils.versions.compare(ver1=version_num,\n oper='>=',\n ver2=cver,\n cmp_func=version_cmp):\n targets.append(pkgstr)\n else:\n downgrade.append(pkgstr)\n\n if fromrepo and not sources:\n log.info('Targeting repo \\'%s\\'', fromrepo)\n\n cmds = []\n all_pkgs = []\n if targets:\n all_pkgs.extend(targets)\n cmd = copy.deepcopy(cmd_prefix)\n cmd.extend(targets)\n cmds.append(cmd)\n\n if downgrade:\n cmd = copy.deepcopy(cmd_prefix)\n if pkg_type == 'repository' and '--force-yes' not in cmd:\n # Downgrading requires --force-yes. Insert this before 'install'\n cmd.insert(-1, '--force-yes')\n cmd.extend(downgrade)\n cmds.append(cmd)\n\n if to_reinstall:\n all_pkgs.extend(to_reinstall)\n cmd = copy.deepcopy(cmd_prefix)\n if not sources:\n cmd.append('--reinstall')\n cmd.extend([x for x in six.itervalues(to_reinstall)])\n cmds.append(cmd)\n\n if not cmds:\n ret = {}\n else:\n cache_valid_time = kwargs.pop('cache_valid_time', 0)\n if _refresh_db:\n refresh_db(cache_valid_time)\n\n env = _parse_env(kwargs.get('env'))\n env.update(DPKG_ENV_VARS.copy())\n\n hold_pkgs = get_selections(state='hold').get('hold', [])\n # all_pkgs contains the argument to be passed to apt-get install, which\n # when a specific version is requested will be in the format\n # name=version. Strip off the '=' if present so we can compare the\n # held package names against the pacakges we are trying to install.\n targeted_names = [x.split('=')[0] for x in all_pkgs]\n to_unhold = [x for x in hold_pkgs if x in targeted_names]\n\n if to_unhold:\n unhold(pkgs=to_unhold)\n\n for cmd in cmds:\n out = _call_apt(cmd)\n if out['retcode'] != 0 and out['stderr']:\n errors.append(out['stderr'])\n\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n ret = salt.utils.data.compare_dicts(old, new)\n\n for pkgname in to_reinstall:\n if pkgname not in ret or pkgname in old:\n ret.update({pkgname: {'old': old.get(pkgname, ''),\n 'new': new.get(pkgname, '')}})\n\n if to_unhold:\n hold(pkgs=to_unhold)\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": "Installs a package from the specified software repository."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _uninstall(action='remove', name=None, pkgs=None, **kwargs):\n '''\n remove and purge do identical things but with different apt-get commands,\n this function performs the common logic.\n '''\n try:\n pkg_params = __salt__['pkg_resource.parse_targets'](name, pkgs)[0]\n except MinionError as exc:\n raise CommandExecutionError(exc)\n\n old = list_pkgs()\n old_removed = list_pkgs(removed=True)\n targets = [x for x in pkg_params if x in old]\n if action == 'purge':\n targets.extend([x for x in pkg_params if x in old_removed])\n if not targets:\n return {}\n cmd = ['apt-get', '-q', '-y', action]\n cmd.extend(targets)\n env = _parse_env(kwargs.get('env'))\n env.update(DPKG_ENV_VARS.copy())\n out = _call_apt(cmd, env=env)\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 new_removed = list_pkgs(removed=True)\n\n changes = salt.utils.data.compare_dicts(old, new)\n if action == 'purge':\n ret = {\n 'removed': salt.utils.data.compare_dicts(old_removed, new_removed),\n 'installed': changes\n }\n else:\n ret = changes\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": "Remove and purge apt - get command."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nreturn a list of packages that can be auto - removed by another package using apt - get command.", "response": "def autoremove(list_only=False, purge=False):\n '''\n .. versionadded:: 2015.5.0\n\n Remove packages not required by another package using ``apt-get\n autoremove``.\n\n list_only : False\n Only retrieve the list of packages to be auto-removed, do not actually\n perform the auto-removal.\n\n purge : False\n Also remove package config data when autoremoving packages.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.autoremove\n salt '*' pkg.autoremove list_only=True\n salt '*' pkg.autoremove purge=True\n '''\n cmd = []\n if list_only:\n ret = []\n cmd.extend(['apt-get', '--assume-no'])\n if purge:\n cmd.append('--purge')\n cmd.append('autoremove')\n out = _call_apt(cmd, ignore_retcode=True)['stdout']\n found = False\n for line in out.splitlines():\n if found is True:\n if line.startswith(' '):\n ret.extend(line.split())\n else:\n found = False\n elif 'The following packages will be REMOVED:' in line:\n found = True\n ret.sort()\n return ret\n else:\n old = list_pkgs()\n cmd.extend(['apt-get', '--assume-yes'])\n if purge:\n cmd.append('--purge')\n cmd.append('autoremove')\n _call_apt(cmd, ignore_retcode=True)\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n return salt.utils.data.compare_dicts(old, new)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef remove(name=None, pkgs=None, **kwargs):\n '''\n .. versionchanged:: 2015.8.12,2016.3.3,2016.11.0\n On minions running systemd>=205, `systemd-run(1)`_ is now used to\n isolate commands which modify installed packages from the\n ``salt-minion`` daemon's control group. This is done to keep systemd\n from killing any apt-get/dpkg commands spawned by Salt when the\n ``salt-minion`` service is restarted. (see ``KillMode`` in the\n `systemd.kill(5)`_ manpage for more information). If desired, usage of\n `systemd-run(1)`_ can be suppressed by setting a :mod:`config option\n <salt.modules.config.get>` called ``systemd.scope``, with a value of\n ``False`` (no quotes).\n\n .. _`systemd-run(1)`: https://www.freedesktop.org/software/systemd/man/systemd-run.html\n .. _`systemd.kill(5)`: https://www.freedesktop.org/software/systemd/man/systemd.kill.html\n\n Remove packages using ``apt-get remove``.\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 <package name>\n salt '*' pkg.remove <package1>,<package2>,<package3>\n salt '*' pkg.remove pkgs='[\"foo\", \"bar\"]'\n '''\n return _uninstall(action='remove', name=name, pkgs=pkgs, **kwargs)", "response": "Remove packages from the named package."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef purge(name=None, pkgs=None, **kwargs):\n '''\n .. versionchanged:: 2015.8.12,2016.3.3,2016.11.0\n On minions running systemd>=205, `systemd-run(1)`_ is now used to\n isolate commands which modify installed packages from the\n ``salt-minion`` daemon's control group. This is done to keep systemd\n from killing any apt-get/dpkg commands spawned by Salt when the\n ``salt-minion`` service is restarted. (see ``KillMode`` in the\n `systemd.kill(5)`_ manpage for more information). If desired, usage of\n `systemd-run(1)`_ can be suppressed by setting a :mod:`config option\n <salt.modules.config.get>` called ``systemd.scope``, with a value of\n ``False`` (no quotes).\n\n .. _`systemd-run(1)`: https://www.freedesktop.org/software/systemd/man/systemd-run.html\n .. _`systemd.kill(5)`: https://www.freedesktop.org/software/systemd/man/systemd.kill.html\n\n Remove packages via ``apt-get purge`` along with all configuration files.\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.purge <package name>\n salt '*' pkg.purge <package1>,<package2>,<package3>\n salt '*' pkg.purge pkgs='[\"foo\", \"bar\"]'\n '''\n return _uninstall(action='purge', name=name, pkgs=pkgs, **kwargs)", "response": "Remove packages from the system."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef upgrade(refresh=True, dist_upgrade=False, **kwargs):\n '''\n .. versionchanged:: 2015.8.12,2016.3.3,2016.11.0\n On minions running systemd>=205, `systemd-run(1)`_ is now used to\n isolate commands which modify installed packages from the\n ``salt-minion`` daemon's control group. This is done to keep systemd\n from killing any apt-get/dpkg commands spawned by Salt when the\n ``salt-minion`` service is restarted. (see ``KillMode`` in the\n `systemd.kill(5)`_ manpage for more information). If desired, usage of\n `systemd-run(1)`_ can be suppressed by setting a :mod:`config option\n <salt.modules.config.get>` called ``systemd.scope``, with a value of\n ``False`` (no quotes).\n\n .. _`systemd-run(1)`: https://www.freedesktop.org/software/systemd/man/systemd-run.html\n .. _`systemd.kill(5)`: https://www.freedesktop.org/software/systemd/man/systemd.kill.html\n\n Upgrades all packages via ``apt-get upgrade`` or ``apt-get dist-upgrade``\n if ``dist_upgrade`` is ``True``.\n\n Returns a dictionary containing the changes:\n\n .. code-block:: python\n\n {'<package>': {'old': '<old-version>',\n 'new': '<new-version>'}}\n\n dist_upgrade\n Whether to perform the upgrade using dist-upgrade vs upgrade. Default\n is to use upgrade.\n\n .. versionadded:: 2014.7.0\n\n cache_valid_time\n\n .. versionadded:: 2016.11.0\n\n Skip refreshing the package database if refresh has already occurred within\n <value> seconds\n\n download_only\n Only download the packages, don't unpack or install them\n\n .. versionadded:: 2018.3.0\n\n force_conf_new\n Always install the new version of any configuration files.\n\n .. versionadded:: 2015.8.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.upgrade\n '''\n cache_valid_time = kwargs.pop('cache_valid_time', 0)\n if salt.utils.data.is_true(refresh):\n refresh_db(cache_valid_time)\n\n old = list_pkgs()\n if 'force_conf_new' in kwargs and kwargs['force_conf_new']:\n force_conf = '--force-confnew'\n else:\n force_conf = '--force-confold'\n cmd = ['apt-get', '-q', '-y', '-o', 'DPkg::Options::={0}'.format(force_conf),\n '-o', 'DPkg::Options::=--force-confdef']\n\n if kwargs.get('force_yes', False):\n cmd.append('--force-yes')\n if kwargs.get('skip_verify', False):\n cmd.append('--allow-unauthenticated')\n if kwargs.get('download_only', False):\n cmd.append('--download-only')\n\n cmd.append('dist-upgrade' if dist_upgrade else 'upgrade')\n result = _call_apt(cmd, env=DPKG_ENV_VARS.copy())\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": "Return a dictionary of changes to the installed packages."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nholding a package in the hierarchy.", "response": "def hold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W0613\n '''\n .. versionadded:: 2014.7.0\n\n Set package in 'hold' state, meaning it will not be upgraded.\n\n name\n The name of the package, e.g., 'tmux'\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.hold <package name>\n\n pkgs\n A list of packages to hold. Must be passed as a python list.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.hold pkgs='[\"foo\", \"bar\"]'\n '''\n if not name and not pkgs and not sources:\n raise SaltInvocationError(\n 'One of name, pkgs, or sources must be specified.'\n )\n if pkgs and sources:\n raise SaltInvocationError(\n 'Only one of pkgs or sources can be specified.'\n )\n\n targets = []\n if pkgs:\n targets.extend(pkgs)\n elif sources:\n for source in sources:\n targets.append(next(iter(source)))\n else:\n targets.append(name)\n\n ret = {}\n for target in targets:\n if isinstance(target, dict):\n target = next(iter(target))\n\n ret[target] = {'name': target,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n state = get_selections(pattern=target, state='hold')\n if not state:\n ret[target]['comment'] = ('Package {0} not currently held.'\n .format(target))\n elif not salt.utils.data.is_true(state.get('hold', False)):\n if 'test' in __opts__ and __opts__['test']:\n ret[target].update(result=None)\n ret[target]['comment'] = ('Package {0} is set to be held.'\n .format(target))\n else:\n result = set_selections(selection={'hold': [target]})\n ret[target].update(changes=result[target], result=True)\n ret[target]['comment'] = ('Package {0} is now being held.'\n .format(target))\n else:\n ret[target].update(result=True)\n ret[target]['comment'] = ('Package {0} is already set to be held.'\n .format(target))\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_pkgs(versions_as_list=False,\n removed=False,\n purge_desired=False,\n **kwargs): # pylint: disable=W0613\n '''\n List the packages currently installed in a dict::\n\n {'<package_name>': '<version>'}\n\n removed\n If ``True``, then only packages which have been removed (but not\n purged) will be returned.\n\n purge_desired\n If ``True``, then only packages which have been marked to be purged,\n but can't be purged due to their status as dependencies for other\n installed packages, will be returned. Note that these packages will\n appear in installed\n\n .. versionchanged:: 2014.1.1\n\n Packages in this state now correctly show up in the output of this\n function.\n\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_pkgs\n salt '*' pkg.list_pkgs versions_as_list=True\n '''\n versions_as_list = salt.utils.data.is_true(versions_as_list)\n removed = salt.utils.data.is_true(removed)\n purge_desired = salt.utils.data.is_true(purge_desired)\n\n if 'pkg.list_pkgs' in __context__:\n if removed:\n ret = copy.deepcopy(__context__['pkg.list_pkgs']['removed'])\n else:\n ret = copy.deepcopy(__context__['pkg.list_pkgs']['purge_desired'])\n if not purge_desired:\n ret.update(__context__['pkg.list_pkgs']['installed'])\n if not versions_as_list:\n __salt__['pkg_resource.stringify'](ret)\n return ret\n\n ret = {'installed': {}, 'removed': {}, 'purge_desired': {}}\n cmd = ['dpkg-query', '--showformat',\n '${Status} ${Package} ${Version} ${Architecture}\\n', '-W']\n\n out = __salt__['cmd.run_stdout'](\n cmd,\n output_loglevel='trace',\n python_shell=False)\n # Typical lines of output:\n # install ok installed zsh 4.3.17-1ubuntu1 amd64\n # deinstall ok config-files mc 3:4.8.1-2ubuntu1 amd64\n for line in out.splitlines():\n cols = line.split()\n try:\n linetype, status, name, version_num, arch = \\\n [cols[x] for x in (0, 2, 3, 4, 5)]\n except (ValueError, IndexError):\n continue\n if __grains__.get('cpuarch', '') == 'x86_64':\n osarch = __grains__.get('osarch', '')\n if arch != 'all' and osarch == 'amd64' and osarch != arch:\n name += ':{0}'.format(arch)\n if cols:\n if ('install' in linetype or 'hold' in linetype) and \\\n 'installed' in status:\n __salt__['pkg_resource.add_pkg'](ret['installed'],\n name,\n version_num)\n elif 'deinstall' in linetype:\n __salt__['pkg_resource.add_pkg'](ret['removed'],\n name,\n version_num)\n elif 'purge' in linetype and status == 'installed':\n __salt__['pkg_resource.add_pkg'](ret['purge_desired'],\n name,\n version_num)\n\n for pkglist_type in ('installed', 'removed', 'purge_desired'):\n __salt__['pkg_resource.sort_pkglist'](ret[pkglist_type])\n\n __context__['pkg.list_pkgs'] = copy.deepcopy(ret)\n\n if removed:\n ret = ret['removed']\n else:\n ret = copy.deepcopy(__context__['pkg.list_pkgs']['purge_desired'])\n if not purge_desired:\n ret.update(__context__['pkg.list_pkgs']['installed'])\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": "Make a summary of the following Python 3 code\ndef _get_upgradable(dist_upgrade=True, **kwargs):\n '''\n Utility function to get upgradable packages\n\n Sample return data:\n { 'pkgname': '1.2.3-45', ... }\n '''\n\n cmd = ['apt-get', '--just-print']\n if dist_upgrade:\n cmd.append('dist-upgrade')\n else:\n cmd.append('upgrade')\n try:\n cmd.extend(['-o', 'APT::Default-Release={0}'.format(kwargs['fromrepo'])])\n except KeyError:\n pass\n\n call = _call_apt(cmd)\n if call['retcode'] != 0:\n msg = 'Failed to get upgrades'\n for key in ('stderr', 'stdout'):\n if call[key]:\n msg += ': ' + call[key]\n break\n raise CommandExecutionError(msg)\n else:\n out = call['stdout']\n\n # rexp parses lines that look like the following:\n # Conf libxfont1 (1:1.4.5-1 Debian:testing [i386])\n rexp = re.compile('(?m)^Conf '\n '([^ ]+) ' # Package name\n r'\\(([^ ]+)') # Version\n keys = ['name', 'version']\n _get = lambda l, k: l[keys.index(k)]\n\n upgrades = rexp.findall(out)\n\n ret = {}\n for line in upgrades:\n name = _get(line, 'name')\n version_num = _get(line, 'version')\n ret[name] = version_num\n\n return ret", "response": "Utility function to get upgradable packages"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nlisting all available upgrades.", "response": "def list_upgrades(refresh=True, dist_upgrade=True, **kwargs):\n '''\n List all available package upgrades.\n\n refresh\n Whether to refresh the package database before listing upgrades.\n Default: True.\n\n cache_valid_time\n\n .. versionadded:: 2016.11.0\n\n Skip refreshing the package database if refresh has already occurred within\n <value> seconds\n\n dist_upgrade\n Whether to list the upgrades using dist-upgrade vs upgrade. Default is\n to use dist-upgrade.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_upgrades\n '''\n cache_valid_time = kwargs.pop('cache_valid_time', 0)\n if salt.utils.data.is_true(refresh):\n refresh_db(cache_valid_time)\n return _get_upgradable(dist_upgrade, **kwargs)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef version_cmp(pkg1, pkg2, ignore_epoch=False, **kwargs):\n '''\n Do a cmp-style comparison on two packages. Return -1 if pkg1 < pkg2, 0 if\n pkg1 == pkg2, and 1 if pkg1 > pkg2. Return None if there was a problem\n making the comparison.\n\n ignore_epoch : False\n Set to ``True`` to ignore the epoch when comparing versions\n\n .. versionadded:: 2015.8.10,2016.3.2\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.version_cmp '0.2.4-0ubuntu1' '0.2.4.1-0ubuntu1'\n '''\n normalize = lambda x: six.text_type(x).split(':', 1)[-1] \\\n if ignore_epoch else six.text_type(x)\n # both apt_pkg.version_compare and _cmd_quote need string arguments.\n pkg1 = normalize(pkg1)\n pkg2 = normalize(pkg2)\n\n # if we have apt_pkg, this will be quickier this way\n # and also do not rely on shell.\n if HAS_APTPKG:\n try:\n # the apt_pkg module needs to be manually initialized\n apt_pkg.init_system()\n\n # if there is a difference in versions, apt_pkg.version_compare will\n # return an int representing the difference in minor versions, or\n # 1/-1 if the difference is smaller than minor versions. normalize\n # to -1, 0 or 1.\n try:\n ret = apt_pkg.version_compare(pkg1, pkg2)\n except TypeError:\n ret = apt_pkg.version_compare(six.text_type(pkg1), six.text_type(pkg2))\n return 1 if ret > 0 else -1 if ret < 0 else 0\n except Exception:\n # Try to use shell version in case of errors w/python bindings\n pass\n try:\n for oper, ret in (('lt', -1), ('eq', 0), ('gt', 1)):\n cmd = ['dpkg', '--compare-versions', pkg1, oper, pkg2]\n retcode = __salt__['cmd.retcode'](cmd,\n output_loglevel='trace',\n python_shell=False,\n ignore_retcode=True)\n if retcode == 0:\n return ret\n except Exception as exc:\n log.error(exc)\n return None", "response": "A version_cmp function that returns - 1 if pkg1 < pkg2 and 1 if pkg1 > pkg2 and 0 if pkg1 > pkg2 and 1 if pkg1 > pkg2 and - 1 if pkg1 < pkg2 and - 1 if pkg1 > pkg2 and - 1 if pkg1 > pkg2 and - 1 if pkg1 > pkg2 and - 1 if pkg1 > pkg2 and - 1 if pkg1 < pkg2 and - 1 if pkg2 > pkg1"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _split_repo_str(repo):\n '''\n Return APT source entry as a tuple.\n '''\n split = sourceslist.SourceEntry(repo)\n return split.type, split.architectures, split.uri, split.dist, split.comps", "response": "Split repo into APT source entry as a tuple."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_repo_pkgs(*args, **kwargs): # pylint: disable=unused-import\n '''\n .. versionadded:: 2017.7.0\n\n Returns all available packages. Optionally, package names (and name globs)\n can be passed and the results will be filtered to packages matching those\n names.\n\n This function can be helpful in discovering the version or repo to specify\n in a :mod:`pkg.installed <salt.states.pkg.installed>` state.\n\n The return data will be a dictionary mapping package names to a list of\n version numbers, ordered from newest to oldest. For example:\n\n .. code-block:: python\n\n {\n 'bash': ['4.3-14ubuntu1.1',\n '4.3-14ubuntu1'],\n 'nginx': ['1.10.0-0ubuntu0.16.04.4',\n '1.9.15-0ubuntu1']\n }\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.list_repo_pkgs\n salt '*' pkg.list_repo_pkgs foo bar baz\n '''\n if args:\n # Get only information about packages in args\n cmd = ['apt-cache', 'show'] + [arg for arg in args]\n else:\n # Get information about all available packages\n cmd = ['apt-cache', 'dump']\n\n out = _call_apt(cmd, scope=False, ignore_retcode=True)\n\n ret = {}\n pkg_name = None\n skip_pkg = False\n new_pkg = re.compile('^Package: (.+)')\n for line in salt.utils.itertools.split(out['stdout'], '\\n'):\n if not line.strip():\n continue\n try:\n cur_pkg = new_pkg.match(line).group(1)\n except AttributeError:\n pass\n else:\n if cur_pkg != pkg_name:\n pkg_name = cur_pkg\n continue\n comps = line.strip().split(None, 1)\n if comps[0] == 'Version:':\n ret.setdefault(pkg_name, []).append(comps[1])\n\n return ret", "response": "List available packages in a repository."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _skip_source(source):\n '''\n Decide to skip source or not.\n\n :param source:\n :return:\n '''\n if source.invalid:\n if source.uri and source.type and source.type in (\"deb\", \"deb-src\", \"rpm\", \"rpm-src\"):\n pieces = source.mysplit(source.line)\n if pieces[1].strip()[0] == \"[\":\n options = pieces.pop(1).strip(\"[]\").split()\n if options:\n log.debug(\"Source %s will be included although is marked invalid\", source.uri)\n return False\n return True\n else:\n return True\n return False", "response": "Decide to skip source or not."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nlist all repos in the sources. list and sources. lists. d", "response": "def list_repos(**kwargs):\n '''\n Lists all repos in the sources.list (and sources.lists.d) files\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_repos\n salt '*' pkg.list_repos disabled=True\n '''\n _check_apt()\n repos = {}\n sources = sourceslist.SourcesList()\n for source in sources.list:\n if _skip_source(source):\n continue\n repo = {}\n repo['file'] = source.file\n repo['comps'] = getattr(source, 'comps', [])\n repo['disabled'] = source.disabled\n repo['enabled'] = not repo['disabled'] # This is for compatibility with the other modules\n repo['dist'] = source.dist\n repo['type'] = source.type\n repo['uri'] = source.uri.rstrip('/')\n repo['line'] = salt.utils.pkg.deb.strip_uri(source.line.strip())\n repo['architectures'] = getattr(source, 'architectures', [])\n repos.setdefault(source.uri, []).append(repo)\n return repos"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_repo(repo, **kwargs):\n '''\n Display a repo from the sources.list / sources.list.d\n\n The repo passed in needs to be a complete repo entry.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.get_repo \"myrepo definition\"\n '''\n _check_apt()\n ppa_auth = kwargs.get('ppa_auth', None)\n # we have to be clever about this since the repo definition formats\n # are a bit more \"loose\" than in some other distributions\n if repo.startswith('ppa:') and __grains__['os'] in ('Ubuntu', 'Mint', 'neon'):\n # This is a PPA definition meaning special handling is needed\n # to derive the name.\n dist = __grains__['lsb_distrib_codename']\n owner_name, ppa_name = repo[4:].split('/')\n if ppa_auth:\n auth_info = '{0}@'.format(ppa_auth)\n repo = LP_PVT_SRC_FORMAT.format(auth_info, owner_name,\n ppa_name, dist)\n else:\n if HAS_SOFTWAREPROPERTIES:\n try:\n if hasattr(softwareproperties.ppa, 'PPAShortcutHandler'):\n repo = softwareproperties.ppa.PPAShortcutHandler(\n repo).expand(dist)[0]\n else:\n repo = softwareproperties.ppa.expand_ppa_line(\n repo,\n dist)[0]\n except NameError as name_error:\n raise CommandExecutionError(\n 'Could not find ppa {0}: {1}'.format(repo, name_error)\n )\n else:\n repo = LP_SRC_FORMAT.format(owner_name, ppa_name, dist)\n\n repos = list_repos()\n\n if repos:\n try:\n repo_type, repo_architectures, repo_uri, repo_dist, repo_comps = _split_repo_str(repo)\n if ppa_auth:\n uri_match = re.search('(http[s]?://)(.+)', repo_uri)\n if uri_match:\n if not uri_match.group(2).startswith(ppa_auth):\n repo_uri = '{0}{1}@{2}'.format(uri_match.group(1),\n ppa_auth,\n uri_match.group(2))\n except SyntaxError:\n raise CommandExecutionError(\n 'Error: repo \\'{0}\\' is not a well formatted definition'\n .format(repo)\n )\n\n for source in six.itervalues(repos):\n for sub in source:\n if (sub['type'] == repo_type and\n # strip trailing '/' from repo_uri, it's valid in definition\n # but not valid when compared to persisted source\n sub['uri'].rstrip('/') == repo_uri.rstrip('/') and\n sub['dist'] == repo_dist):\n if not repo_comps:\n return sub\n for comp in repo_comps:\n if comp in sub.get('comps', []):\n return sub\n return {}", "response": "Display a repo from the sources. list or sources. list. d\n "} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndelete a repository from the sources. list directory.", "response": "def del_repo(repo, **kwargs):\n '''\n Delete a repo from the sources.list / sources.list.d\n\n If the .list file is in the sources.list.d directory\n and the file that the repo exists in does not contain any other\n repo configuration, the file itself will be deleted.\n\n The repo passed in must be a fully formed repository definition\n string.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.del_repo \"myrepo definition\"\n '''\n _check_apt()\n is_ppa = False\n if repo.startswith('ppa:') and __grains__['os'] in ('Ubuntu', 'Mint', 'neon'):\n # This is a PPA definition meaning special handling is needed\n # to derive the name.\n is_ppa = True\n dist = __grains__['lsb_distrib_codename']\n if not HAS_SOFTWAREPROPERTIES:\n _warn_software_properties(repo)\n owner_name, ppa_name = repo[4:].split('/')\n if 'ppa_auth' in kwargs:\n auth_info = '{0}@'.format(kwargs['ppa_auth'])\n repo = LP_PVT_SRC_FORMAT.format(auth_info, dist, owner_name,\n ppa_name)\n else:\n repo = LP_SRC_FORMAT.format(owner_name, ppa_name, dist)\n else:\n if hasattr(softwareproperties.ppa, 'PPAShortcutHandler'):\n repo = softwareproperties.ppa.PPAShortcutHandler(repo).expand(dist)[0]\n else:\n repo = softwareproperties.ppa.expand_ppa_line(repo, dist)[0]\n\n sources = sourceslist.SourcesList()\n repos = [s for s in sources.list if not s.invalid]\n if repos:\n deleted_from = dict()\n try:\n repo_type, \\\n repo_architectures, \\\n repo_uri, \\\n repo_dist, \\\n repo_comps = _split_repo_str(repo)\n except SyntaxError:\n raise SaltInvocationError(\n 'Error: repo \\'{0}\\' not a well formatted definition'\n .format(repo)\n )\n\n for source in repos:\n if (source.type == repo_type\n and source.architectures == repo_architectures\n and source.uri == repo_uri\n and source.dist == repo_dist):\n\n s_comps = set(source.comps)\n r_comps = set(repo_comps)\n if s_comps.intersection(r_comps):\n deleted_from[source.file] = 0\n source.comps = list(s_comps.difference(r_comps))\n if not source.comps:\n try:\n sources.remove(source)\n except ValueError:\n pass\n # PPAs are special and can add deb-src where expand_ppa_line\n # doesn't always reflect this. Lets just cleanup here for good\n # measure\n if (is_ppa and repo_type == 'deb' and source.type == 'deb-src' and\n source.uri == repo_uri and source.dist == repo_dist):\n\n s_comps = set(source.comps)\n r_comps = set(repo_comps)\n if s_comps.intersection(r_comps):\n deleted_from[source.file] = 0\n source.comps = list(s_comps.difference(r_comps))\n if not source.comps:\n try:\n sources.remove(source)\n except ValueError:\n pass\n sources.save()\n if deleted_from:\n ret = ''\n for source in sources:\n if source.file in deleted_from:\n deleted_from[source.file] += 1\n for repo_file, count in six.iteritems(deleted_from):\n msg = 'Repo \\'{0}\\' has been removed from {1}.\\n'\n if count == 0 and 'sources.list.d/' in repo_file:\n if os.path.isfile(repo_file):\n msg = ('File {1} containing repo \\'{0}\\' has been '\n 'removed.')\n try:\n os.remove(repo_file)\n except OSError:\n pass\n ret += msg.format(repo, repo_file)\n # explicit refresh after a repo is deleted\n refresh_db()\n return ret\n\n raise CommandExecutionError(\n 'Repo {0} doesn\\'t exist in the sources.list(s)'.format(repo)\n )"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nadd a repo key to the current key list.", "response": "def add_repo_key(path=None, text=None, keyserver=None, keyid=None, saltenv='base'):\n '''\n .. versionadded:: 2017.7.0\n\n Add a repo key using ``apt-key add``.\n\n :param str path: The path of the key file to import.\n :param str text: The key data to import, in string form.\n :param str keyserver: The server to download the repo key specified by the keyid.\n :param str keyid: The key id of the repo key to add.\n :param str saltenv: The environment the key file resides in.\n\n :return: A boolean representing whether the repo key was added.\n :rtype: bool\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.add_repo_key 'salt://apt/sources/test.key'\n\n salt '*' pkg.add_repo_key text=\"'$KEY1'\"\n\n salt '*' pkg.add_repo_key keyserver='keyserver.example' keyid='0000AAAA'\n '''\n cmd = ['apt-key']\n kwargs = {}\n\n current_repo_keys = get_repo_keys()\n\n if path:\n cached_source_path = __salt__['cp.cache_file'](path, saltenv)\n\n if not cached_source_path:\n log.error('Unable to get cached copy of file: %s', path)\n return False\n\n cmd.extend(['add', cached_source_path])\n elif text:\n log.debug('Received value: %s', text)\n\n cmd.extend(['add', '-'])\n kwargs.update({'stdin': text})\n elif keyserver:\n if not keyid:\n error_msg = 'No keyid or keyid too short for keyserver: {0}'.format(keyserver)\n raise SaltInvocationError(error_msg)\n\n cmd.extend(['adv', '--batch', '--keyserver', keyserver, '--recv', keyid])\n elif keyid:\n error_msg = 'No keyserver specified for keyid: {0}'.format(keyid)\n raise SaltInvocationError(error_msg)\n else:\n raise TypeError('{0}() takes at least 1 argument (0 given)'.format(add_repo_key.__name__))\n\n # If the keyid is provided or determined, check it against the existing\n # repo key ids to determine whether it needs to be imported.\n if keyid:\n for current_keyid in current_repo_keys:\n if current_keyid[-(len(keyid)):] == keyid:\n log.debug(\"The keyid '%s' already present: %s\", keyid, current_keyid)\n return True\n\n cmd_ret = _call_apt(cmd, **kwargs)\n\n if cmd_ret['retcode'] == 0:\n return True\n log.error('Unable to add repo key: %s', cmd_ret['stderr'])\n return False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef del_repo_key(name=None, **kwargs):\n '''\n .. versionadded:: 2015.8.0\n\n Remove a repo key using ``apt-key del``\n\n name\n Repo from which to remove the key. Unnecessary if ``keyid`` is passed.\n\n keyid\n The KeyID of the GPG key to remove\n\n keyid_ppa : False\n If set to ``True``, the repo's GPG key ID will be looked up from\n ppa.launchpad.net and removed.\n\n .. note::\n\n Setting this option to ``True`` requires that the ``name`` param\n also be passed.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.del_repo_key keyid=0123ABCD\n salt '*' pkg.del_repo_key name='ppa:foo/bar' keyid_ppa=True\n '''\n if kwargs.get('keyid_ppa', False):\n if isinstance(name, six.string_types) and name.startswith('ppa:'):\n owner_name, ppa_name = name[4:].split('/')\n ppa_info = _get_ppa_info_from_launchpad(\n owner_name, ppa_name)\n keyid = ppa_info['signing_key_fingerprint'][-8:]\n else:\n raise SaltInvocationError(\n 'keyid_ppa requires that a PPA be passed'\n )\n else:\n if 'keyid' in kwargs:\n keyid = kwargs.get('keyid')\n else:\n raise SaltInvocationError(\n 'keyid or keyid_ppa and PPA name must be passed'\n )\n\n result = _call_apt(['apt-key', 'del', keyid], scope=False)\n if result['retcode'] != 0:\n msg = 'Failed to remove keyid {0}'\n if result['stderr']:\n msg += ': {0}'.format(result['stderr'])\n raise CommandExecutionError(msg)\n return keyid", "response": "Remove a GPG key from a set of GPG keys."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef mod_repo(repo, saltenv='base', **kwargs):\n '''\n Modify one or more values for a repo. If the repo does not exist, it will\n be created, so long as the definition is well formed. For Ubuntu the\n ``ppa:<project>/repo`` format is acceptable. ``ppa:`` format can only be\n used to create a new repository.\n\n The following options are available to modify a repo definition:\n\n architectures\n A comma-separated list of supported architectures, e.g. ``amd64`` If\n this option is not set, all architectures (configured in the system)\n will be used.\n\n comps\n A comma separated list of components for the repo, e.g. ``main``\n\n file\n A file name to be used\n\n keyserver\n Keyserver to get gpg key from\n\n keyid\n Key ID or a list of key IDs to load with the ``keyserver`` argument\n\n key_url\n URL to a GPG key to add to the APT GPG keyring\n\n key_text\n GPG key in string form to add to the APT GPG keyring\n\n .. versionadded:: 2018.3.0\n\n consolidate : False\n If ``True``, will attempt to de-duplicate and consolidate sources\n\n comments\n Sometimes you want to supply additional information, but not as\n enabled configuration. All comments provided here will be joined\n into a single string and appended to the repo configuration with a\n comment marker (#) before it.\n\n .. versionadded:: 2015.8.9\n\n .. note::\n Due to the way keys are stored for APT, there is a known issue where\n the key won't be updated unless another change is made at the same\n time. Keys should be properly added on initial configuration.\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.mod_repo 'myrepo definition' uri=http://new/uri\n salt '*' pkg.mod_repo 'myrepo definition' comps=main,universe\n '''\n if 'refresh_db' in kwargs:\n salt.utils.versions.warn_until(\n 'Neon',\n 'The \\'refresh_db\\' argument to \\'pkg.mod_repo\\' has been '\n 'renamed to \\'refresh\\'. Support for using \\'refresh_db\\' will be '\n 'removed in the Neon release of Salt.'\n )\n refresh = kwargs['refresh_db']\n else:\n refresh = kwargs.get('refresh', True)\n\n _check_apt()\n # to ensure no one sets some key values that _shouldn't_ be changed on the\n # object itself, this is just a white-list of \"ok\" to set properties\n if repo.startswith('ppa:'):\n if __grains__['os'] in ('Ubuntu', 'Mint', 'neon'):\n # secure PPAs cannot be supported as of the time of this code\n # implementation via apt-add-repository. The code path for\n # secure PPAs should be the same as urllib method\n if salt.utils.path.which('apt-add-repository') \\\n and 'ppa_auth' not in kwargs:\n repo_info = get_repo(repo)\n if repo_info:\n return {repo: repo_info}\n else:\n env = None\n http_proxy_url = _get_http_proxy_url()\n if http_proxy_url:\n env = {'http_proxy': http_proxy_url,\n 'https_proxy': http_proxy_url}\n if float(__grains__['osrelease']) < 12.04:\n cmd = ['apt-add-repository', repo]\n else:\n cmd = ['apt-add-repository', '-y', repo]\n out = _call_apt(cmd, env=env, scope=False, **kwargs)\n if out['retcode']:\n raise CommandExecutionError(\n 'Unable to add PPA \\'{0}\\'. \\'{1}\\' exited with '\n 'status {2!s}: \\'{3}\\' '.format(\n repo[4:],\n cmd,\n out['retcode'],\n out['stderr']\n )\n )\n # explicit refresh when a repo is modified.\n if refresh:\n refresh_db()\n return {repo: out}\n else:\n if not HAS_SOFTWAREPROPERTIES:\n _warn_software_properties(repo)\n else:\n log.info('Falling back to urllib method for private PPA')\n\n # fall back to urllib style\n try:\n owner_name, ppa_name = repo[4:].split('/', 1)\n except ValueError:\n raise CommandExecutionError(\n 'Unable to get PPA info from argument. '\n 'Expected format \"<PPA_OWNER>/<PPA_NAME>\" '\n '(e.g. saltstack/salt) not found. Received '\n '\\'{0}\\' instead.'.format(repo[4:])\n )\n dist = __grains__['lsb_distrib_codename']\n # ppa has a lot of implicit arguments. Make them explicit.\n # These will defer to any user-defined variants\n kwargs['dist'] = dist\n ppa_auth = ''\n if 'file' not in kwargs:\n filename = '/etc/apt/sources.list.d/{0}-{1}-{2}.list'\n kwargs['file'] = filename.format(owner_name, ppa_name,\n dist)\n try:\n launchpad_ppa_info = _get_ppa_info_from_launchpad(\n owner_name, ppa_name)\n if 'ppa_auth' not in kwargs:\n kwargs['keyid'] = launchpad_ppa_info[\n 'signing_key_fingerprint']\n else:\n if 'keyid' not in kwargs:\n error_str = 'Private PPAs require a ' \\\n 'keyid to be specified: {0}/{1}'\n raise CommandExecutionError(\n error_str.format(owner_name, ppa_name)\n )\n except HTTPError as exc:\n raise CommandExecutionError(\n 'Launchpad does not know about {0}/{1}: {2}'.format(\n owner_name, ppa_name, exc)\n )\n except IndexError as exc:\n raise CommandExecutionError(\n 'Launchpad knows about {0}/{1} but did not '\n 'return a fingerprint. Please set keyid '\n 'manually: {2}'.format(owner_name, ppa_name, exc)\n )\n\n if 'keyserver' not in kwargs:\n kwargs['keyserver'] = 'keyserver.ubuntu.com'\n if 'ppa_auth' in kwargs:\n if not launchpad_ppa_info['private']:\n raise CommandExecutionError(\n 'PPA is not private but auth credentials '\n 'passed: {0}'.format(repo)\n )\n # assign the new repo format to the \"repo\" variable\n # so we can fall through to the \"normal\" mechanism\n # here.\n if 'ppa_auth' in kwargs:\n ppa_auth = '{0}@'.format(kwargs['ppa_auth'])\n repo = LP_PVT_SRC_FORMAT.format(ppa_auth, owner_name,\n ppa_name, dist)\n else:\n repo = LP_SRC_FORMAT.format(owner_name, ppa_name, dist)\n else:\n raise CommandExecutionError(\n 'cannot parse \"ppa:\" style repo definitions: {0}'\n .format(repo)\n )\n\n sources = sourceslist.SourcesList()\n if kwargs.get('consolidate', False):\n # attempt to de-dup and consolidate all sources\n # down to entries in sources.list\n # this option makes it easier to keep the sources\n # list in a \"sane\" state.\n #\n # this should remove duplicates, consolidate comps\n # for a given source down to one line\n # and eliminate \"invalid\" and comment lines\n #\n # the second side effect is removal of files\n # that are not the main sources.list file\n sources = _consolidate_repo_sources(sources)\n\n repos = [s for s in sources if not s.invalid]\n mod_source = None\n try:\n repo_type, \\\n repo_architectures, \\\n repo_uri, \\\n repo_dist, \\\n repo_comps = _split_repo_str(repo)\n except SyntaxError:\n raise SyntaxError(\n 'Error: repo \\'{0}\\' not a well formatted definition'.format(repo)\n )\n\n full_comp_list = set(repo_comps)\n no_proxy = __salt__['config.option']('no_proxy')\n\n if 'keyid' in kwargs:\n keyid = kwargs.pop('keyid', None)\n keyserver = kwargs.pop('keyserver', None)\n if not keyid or not keyserver:\n error_str = 'both keyserver and keyid options required.'\n raise NameError(error_str)\n if not isinstance(keyid, list):\n keyid = [keyid]\n\n for key in keyid:\n if isinstance(key, int): # yaml can make this an int, we need the hex version\n key = hex(key)\n cmd = ['apt-key', 'export', key]\n output = __salt__['cmd.run_stdout'](cmd, python_shell=False, **kwargs)\n imported = output.startswith('-----BEGIN PGP')\n if keyserver:\n if not imported:\n http_proxy_url = _get_http_proxy_url()\n if http_proxy_url and keyserver not in no_proxy:\n cmd = ['apt-key', 'adv', '--batch', '--keyserver-options', 'http-proxy={0}'.format(http_proxy_url),\n '--keyserver', keyserver, '--logger-fd', '1', '--recv-keys', key]\n else:\n cmd = ['apt-key', 'adv', '--batch', '--keyserver', keyserver,\n '--logger-fd', '1', '--recv-keys', key]\n ret = _call_apt(cmd, scope=False, **kwargs)\n if ret['retcode'] != 0:\n raise CommandExecutionError(\n 'Error: key retrieval failed: {0}'.format(ret['stdout'])\n )\n\n elif 'key_url' in kwargs:\n key_url = kwargs['key_url']\n fn_ = __salt__['cp.cache_file'](key_url, saltenv)\n if not fn_:\n raise CommandExecutionError(\n 'Error: file not found: {0}'.format(key_url)\n )\n cmd = ['apt-key', 'add', fn_]\n out = __salt__['cmd.run_stdout'](cmd, python_shell=False, **kwargs)\n if not out.upper().startswith('OK'):\n raise CommandExecutionError(\n 'Error: failed to add key from {0}'.format(key_url)\n )\n\n elif 'key_text' in kwargs:\n key_text = kwargs['key_text']\n cmd = ['apt-key', 'add', '-']\n out = __salt__['cmd.run_stdout'](cmd, stdin=key_text,\n python_shell=False, **kwargs)\n if not out.upper().startswith('OK'):\n raise CommandExecutionError(\n 'Error: failed to add key:\\n{0}'.format(key_text)\n )\n\n if 'comps' in kwargs:\n kwargs['comps'] = kwargs['comps'].split(',')\n full_comp_list |= set(kwargs['comps'])\n else:\n kwargs['comps'] = list(full_comp_list)\n\n if 'architectures' in kwargs:\n kwargs['architectures'] = kwargs['architectures'].split(',')\n else:\n kwargs['architectures'] = repo_architectures\n\n if 'disabled' in kwargs:\n kwargs['disabled'] = salt.utils.data.is_true(kwargs['disabled'])\n\n kw_type = kwargs.get('type')\n kw_dist = kwargs.get('dist')\n\n for source in repos:\n # This series of checks will identify the starting source line\n # and the resulting source line. The idea here is to ensure\n # we are not returning bogus data because the source line\n # has already been modified on a previous run.\n repo_matches = source.type == repo_type and source.uri.rstrip('/') == repo_uri.rstrip('/') and source.dist == repo_dist\n kw_matches = source.dist == kw_dist and source.type == kw_type\n\n if repo_matches or kw_matches:\n for comp in full_comp_list:\n if comp in getattr(source, 'comps', []):\n mod_source = source\n if not source.comps:\n mod_source = source\n if kwargs['architectures'] != source.architectures:\n mod_source = source\n if mod_source:\n break\n\n if 'comments' in kwargs:\n kwargs['comments'] = \\\n salt.utils.pkg.deb.combine_comments(kwargs['comments'])\n\n if not mod_source:\n mod_source = sourceslist.SourceEntry(repo)\n if 'comments' in kwargs:\n mod_source.comment = kwargs['comments']\n sources.list.append(mod_source)\n elif 'comments' in kwargs:\n mod_source.comment = kwargs['comments']\n\n for key in kwargs:\n if key in _MODIFY_OK and hasattr(mod_source, key):\n setattr(mod_source, key, kwargs[key])\n sources.save()\n # on changes, explicitly refresh\n if refresh:\n refresh_db()\n return {\n repo: {\n 'architectures': getattr(mod_source, 'architectures', []),\n 'comps': mod_source.comps,\n 'disabled': mod_source.disabled,\n 'file': mod_source.file,\n 'type': mod_source.type,\n 'uri': mod_source.uri,\n 'line': mod_source.line\n }\n }", "response": "Modify one or more values for a repo."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ntake a repository definition and expand it to the full pkg repository dict", "response": "def expand_repo_def(**kwargs):\n '''\n Take a repository definition and expand it to the full pkg repository dict\n that can be used for comparison. This is a helper function to make\n the Debian/Ubuntu apt sources sane for comparison in the pkgrepo states.\n\n This is designed to be called from pkgrepo states and will have little use\n being called on the CLI.\n '''\n if 'repo' not in kwargs:\n raise SaltInvocationError('missing \\'repo\\' argument')\n\n _check_apt()\n\n sanitized = {}\n repo = salt.utils.pkg.deb.strip_uri(kwargs['repo'])\n if repo.startswith('ppa:') and __grains__['os'] in ('Ubuntu', 'Mint', 'neon'):\n dist = __grains__['lsb_distrib_codename']\n owner_name, ppa_name = repo[4:].split('/', 1)\n if 'ppa_auth' in kwargs:\n auth_info = '{0}@'.format(kwargs['ppa_auth'])\n repo = LP_PVT_SRC_FORMAT.format(auth_info, owner_name, ppa_name,\n dist)\n else:\n if HAS_SOFTWAREPROPERTIES:\n if hasattr(softwareproperties.ppa, 'PPAShortcutHandler'):\n repo = softwareproperties.ppa.PPAShortcutHandler(repo).expand(dist)[0]\n else:\n repo = softwareproperties.ppa.expand_ppa_line(repo, dist)[0]\n else:\n repo = LP_SRC_FORMAT.format(owner_name, ppa_name, dist)\n\n if 'file' not in kwargs:\n filename = '/etc/apt/sources.list.d/{0}-{1}-{2}.list'\n kwargs['file'] = filename.format(owner_name, ppa_name, dist)\n\n source_entry = sourceslist.SourceEntry(repo)\n for list_args in ('architectures', 'comps'):\n if list_args in kwargs:\n kwargs[list_args] = kwargs[list_args].split(',')\n for kwarg in _MODIFY_OK:\n if kwarg in kwargs:\n setattr(source_entry, kwarg, kwargs[kwarg])\n\n sanitized['file'] = source_entry.file\n sanitized['comps'] = getattr(source_entry, 'comps', [])\n sanitized['disabled'] = source_entry.disabled\n sanitized['dist'] = source_entry.dist\n sanitized['type'] = source_entry.type\n sanitized['uri'] = source_entry.uri.rstrip('/')\n sanitized['line'] = source_entry.line.strip()\n sanitized['architectures'] = getattr(source_entry, 'architectures', [])\n\n return sanitized"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing the format from dpkg -- get - selections and return a format that pkg. get_selections and pkg. set_selections work with.", "response": "def _parse_selections(dpkgselection):\n '''\n Parses the format from ``dpkg --get-selections`` and return a format that\n pkg.get_selections and pkg.set_selections work with.\n '''\n ret = {}\n if isinstance(dpkgselection, six.string_types):\n dpkgselection = dpkgselection.split('\\n')\n for line in dpkgselection:\n if line:\n _pkg, _state = line.split()\n if _state in ret:\n ret[_state].append(_pkg)\n else:\n ret[_state] = [_pkg]\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_selections(pattern=None, state=None):\n '''\n View package state from the dpkg database.\n\n Returns a dict of dicts containing the state, and package names:\n\n .. code-block:: python\n\n {'<host>':\n {'<state>': ['pkg1',\n ...\n ]\n },\n ...\n }\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.get_selections\n salt '*' pkg.get_selections 'python-*'\n salt '*' pkg.get_selections state=hold\n salt '*' pkg.get_selections 'openssh*' state=hold\n '''\n ret = {}\n cmd = ['dpkg', '--get-selections']\n cmd.append(pattern if pattern else '*')\n stdout = __salt__['cmd.run_stdout'](cmd,\n output_loglevel='trace',\n python_shell=False)\n ret = _parse_selections(stdout)\n if state:\n return {state: ret.get(state, [])}\n return ret", "response": "Get the state of the selected packages from the dpkg database."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nset the state of the available packages in the dpkg database.", "response": "def set_selections(path=None, selection=None, clear=False, saltenv='base'):\n '''\n Change package state in the dpkg database.\n\n The state can be any one of, documented in ``dpkg(1)``:\n\n - install\n - hold\n - deinstall\n - purge\n\n This command is commonly used to mark specific packages to be held from\n being upgraded, that is, to be kept at a certain version. When a state is\n changed to anything but being held, then it is typically followed by\n ``apt-get -u dselect-upgrade``.\n\n Note: Be careful with the ``clear`` argument, since it will start\n with setting all packages to deinstall state.\n\n Returns a dict of dicts containing the package names, and the new and old\n versions:\n\n .. code-block:: python\n\n {'<host>':\n {'<package>': {'new': '<new-state>',\n 'old': '<old-state>'}\n },\n ...\n }\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.set_selections selection='{\"install\": [\"netcat\"]}'\n salt '*' pkg.set_selections selection='{\"hold\": [\"openssh-server\", \"openssh-client\"]}'\n salt '*' pkg.set_selections salt://path/to/file\n salt '*' pkg.set_selections salt://path/to/file clear=True\n '''\n ret = {}\n if not path and not selection:\n return ret\n if path and selection:\n err = ('The \\'selection\\' and \\'path\\' arguments to '\n 'pkg.set_selections are mutually exclusive, and cannot be '\n 'specified together')\n raise SaltInvocationError(err)\n\n if isinstance(selection, six.string_types):\n try:\n selection = salt.utils.yaml.safe_load(selection)\n except (salt.utils.yaml.parser.ParserError, salt.utils.yaml.scanner.ScannerError) as exc:\n raise SaltInvocationError(\n 'Improperly-formatted selection: {0}'.format(exc)\n )\n\n if path:\n path = __salt__['cp.cache_file'](path, saltenv)\n with salt.utils.files.fopen(path, 'r') as ifile:\n content = [salt.utils.stringutils.to_unicode(x)\n for x in ifile.readlines()]\n selection = _parse_selections(content)\n\n if selection:\n valid_states = ('install', 'hold', 'deinstall', 'purge')\n bad_states = [x for x in selection if x not in valid_states]\n if bad_states:\n raise SaltInvocationError(\n 'Invalid state(s): {0}'.format(', '.join(bad_states))\n )\n\n if clear:\n cmd = ['dpkg', '--clear-selections']\n if not __opts__['test']:\n result = _call_apt(cmd, scope=False)\n if result['retcode'] != 0:\n err = ('Running dpkg --clear-selections failed: '\n '{0}'.format(result['stderr']))\n log.error(err)\n raise CommandExecutionError(err)\n\n sel_revmap = {}\n for _state, _pkgs in six.iteritems(get_selections()):\n sel_revmap.update(dict((_pkg, _state) for _pkg in _pkgs))\n\n for _state, _pkgs in six.iteritems(selection):\n for _pkg in _pkgs:\n if _state == sel_revmap.get(_pkg):\n continue\n cmd = ['dpkg', '--set-selections']\n cmd_in = '{0} {1}'.format(_pkg, _state)\n if not __opts__['test']:\n result = _call_apt(cmd, scope=False, stdin=cmd_in)\n if result['retcode'] != 0:\n log.error(\n 'failed to set state %s for package %s',\n _state, _pkg\n )\n else:\n ret[_pkg] = {'old': sel_revmap.get(_pkg),\n 'new': _state}\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a list of packages that depend on the given packages.", "response": "def _resolve_deps(name, pkgs, **kwargs):\n '''\n Installs missing dependencies and marks them as auto installed so they\n are removed when no more manually installed packages depend on them.\n\n .. versionadded:: 2014.7.0\n\n :depends: - python-apt module\n '''\n missing_deps = []\n for pkg_file in pkgs:\n deb = apt.debfile.DebPackage(filename=pkg_file, cache=apt.Cache())\n if deb.check():\n missing_deps.extend(deb.missing_deps)\n\n if missing_deps:\n cmd = ['apt-get', '-q', '-y']\n cmd = cmd + ['-o', 'DPkg::Options::=--force-confold']\n cmd = cmd + ['-o', 'DPkg::Options::=--force-confdef']\n cmd.append('install')\n cmd.extend(missing_deps)\n\n ret = __salt__['cmd.retcode'](\n cmd,\n env=kwargs.get('env'),\n python_shell=False\n )\n\n if ret != 0:\n raise CommandExecutionError(\n 'Error: unable to resolve dependencies for: {0}'.format(name)\n )\n else:\n try:\n cmd = ['apt-mark', 'auto'] + missing_deps\n __salt__['cmd.run'](\n cmd,\n env=kwargs.get('env'),\n python_shell=False\n )\n except MinionError as exc:\n raise CommandExecutionError(exc)\n return"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef show(*names, **kwargs):\n '''\n .. versionadded:: 2019.2.0\n\n Runs an ``apt-cache show`` on the passed package names, and returns the\n results in a nested dictionary. The top level of the return data will be\n the package name, with each package name mapping to a dictionary of version\n numbers to any additional information returned by ``apt-cache show``.\n\n filter\n An optional comma-separated list (or quoted Python list) of\n case-insensitive keys on which to filter. This allows one to restrict\n the information returned for each package to a smaller selection of\n pertinent items.\n\n refresh : False\n If ``True``, the apt cache will be refreshed first. By default, no\n refresh is performed.\n\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt myminion pkg.show gawk\n salt myminion pkg.show 'nginx-*'\n salt myminion pkg.show 'nginx-*' filter=description,provides\n '''\n kwargs = salt.utils.args.clean_kwargs(**kwargs)\n refresh = kwargs.pop('refresh', False)\n filter_ = salt.utils.args.split_input(\n kwargs.pop('filter', []),\n lambda x: six.text_type(x)\n if not isinstance(x, six.string_types)\n else x.lower()\n )\n if kwargs:\n salt.utils.args.invalid_kwargs(kwargs)\n\n if refresh:\n refresh_db()\n\n if not names:\n return {}\n\n result = _call_apt(['apt-cache', 'show'] + list(names), scope=False)\n\n def _add(ret, pkginfo):\n name = pkginfo.pop('Package', None)\n version = pkginfo.pop('Version', None)\n if name is not None and version is not None:\n ret.setdefault(name, {}).setdefault(version, {}).update(pkginfo)\n\n def _check_filter(key):\n key = key.lower()\n return True if key in ('package', 'version') or not filter_ \\\n else key in filter_\n\n ret = {}\n pkginfo = {}\n for line in salt.utils.itertools.split(result['stdout'], '\\n'):\n line = line.strip()\n if line:\n try:\n key, val = [x.strip() for x in line.split(':', 1)]\n except ValueError:\n pass\n else:\n if _check_filter(key):\n pkginfo[key] = val\n else:\n # We've reached a blank line, which separates packages\n _add(ret, pkginfo)\n # Clear out the pkginfo dict for the next package\n pkginfo = {}\n continue\n\n # Make sure to add whatever was in the pkginfo dict when we reached the end\n # of the output.\n _add(ret, pkginfo)\n\n return ret", "response": "Apt - cache show command."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef info_installed(*names, **kwargs):\n '''\n Return the information of the named package(s) installed on the system.\n\n .. versionadded:: 2015.8.1\n\n names\n The names of the packages for which to return information.\n\n failhard\n Whether to throw an exception if none of the packages are installed.\n Defaults to True.\n\n .. versionadded:: 2016.11.3\n\n attr\n Comma-separated package attributes. If no 'attr' is specified, all available attributes returned.\n\n Valid attributes are:\n version, vendor, release, build_date, build_date_time_t, install_date, install_date_time_t,\n build_host, group, source_rpm, arch, epoch, size, license, signature, packager, url, summary, description.\n\n .. versionadded:: Neon\n\n CLI example:\n\n .. code-block:: bash\n\n salt '*' pkg.info_installed <package1>\n salt '*' pkg.info_installed <package1> <package2> <package3> ...\n salt '*' pkg.info_installed <package1> failhard=false\n '''\n kwargs = salt.utils.args.clean_kwargs(**kwargs)\n failhard = kwargs.pop('failhard', True)\n kwargs.pop('errors', None) # Only for compatibility with RPM\n attr = kwargs.pop('attr', None) # Package attributes to return\n all_versions = kwargs.pop('all_versions', False) # This is for backward compatible structure only\n\n if kwargs:\n salt.utils.args.invalid_kwargs(kwargs)\n\n ret = dict()\n for pkg_name, pkg_nfo in __salt__['lowpkg.info'](*names, failhard=failhard, attr=attr).items():\n t_nfo = dict()\n if pkg_nfo.get('status', 'ii')[1] != 'i':\n continue # return only packages that are really installed\n # Translate dpkg-specific keys to a common structure\n for key, value in pkg_nfo.items():\n if key == 'package':\n t_nfo['name'] = value\n elif key == 'origin':\n t_nfo['vendor'] = value\n elif key == 'section':\n t_nfo['group'] = value\n elif key == 'maintainer':\n t_nfo['packager'] = value\n elif key == 'homepage':\n t_nfo['url'] = value\n elif key == 'status':\n continue # only installed pkgs are returned, no need for status\n else:\n t_nfo[key] = value\n\n if all_versions:\n ret.setdefault(pkg_name, []).append(t_nfo)\n else:\n ret[pkg_name] = t_nfo\n\n return ret", "response": "Return the information of the named packages installed on the system."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _get_http_proxy_url():\n '''\n Returns the http_proxy_url if proxy_username, proxy_password, proxy_host, and proxy_port\n config values are set.\n\n Returns a string.\n '''\n http_proxy_url = ''\n host = __salt__['config.option']('proxy_host')\n port = __salt__['config.option']('proxy_port')\n username = __salt__['config.option']('proxy_username')\n password = __salt__['config.option']('proxy_password')\n\n # Set http_proxy_url for use in various internet facing actions...eg apt-key adv\n if host and port:\n if username and password:\n http_proxy_url = 'http://{0}:{1}@{2}:{3}'.format(\n username,\n password,\n host,\n port\n )\n else:\n http_proxy_url = 'http://{0}:{1}'.format(\n host,\n port\n )\n\n return http_proxy_url", "response": "Returns the http_proxy_url if proxy_username proxy_password proxy_host and proxy_port config values are set. Otherwise returns an empty string."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _diff_and_merge_host_list(defined, existing):\n '''\n If Zabbix template is to be updated then list of assigned hosts must be provided in all or nothing manner to prevent\n some externally assigned hosts to be detached.\n\n :param defined: list of hosts defined in sls\n :param existing: list of hosts taken from live Zabbix\n :return: list to be updated (combinated or empty list)\n '''\n try:\n defined_host_ids = set([host['hostid'] for host in defined])\n existing_host_ids = set([host['hostid'] for host in existing])\n except KeyError:\n raise SaltException('List of hosts in template not defined correctly.')\n\n diff = defined_host_ids - existing_host_ids\n return [{'hostid': six.text_type(hostid)} for hostid in diff | existing_host_ids] if diff else []", "response": "Helper function to compare list of hosts in Zabbix template with list of hosts in live Zabbix template."} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\ngets a list of existing templates that are not inherited from other templates", "response": "def _get_existing_template_c_list(component, parent_id, **kwargs):\n '''\n Make a list of given component type not inherited from other templates because Zabbix API returns only list of all\n and list of inherited component items so we have to do a difference list.\n\n :param component: Template component (application, item, etc...)\n :param parent_id: ID of existing template the component is assigned to\n :return List of non-inherited (own) components\n '''\n c_def = TEMPLATE_COMPONENT_DEF[component]\n q_params = dict(c_def['output'])\n q_params.update({c_def['qselectpid']: parent_id})\n\n existing_clist_all = __salt__['zabbix.run_query'](c_def['qtype'] + '.get', q_params, **kwargs)\n\n # in some cases (e.g. templatescreens) the logic is reversed (even name of the flag is different!)\n if c_def['inherited'] == 'inherited':\n q_params.update({c_def['inherited']: 'true'})\n existing_clist_inherited = __salt__['zabbix.run_query'](c_def['qtype'] + '.get', q_params, **kwargs)\n else:\n existing_clist_inherited = []\n\n if existing_clist_inherited:\n return [c_all for c_all in existing_clist_all if c_all not in existing_clist_inherited]\n\n return existing_clist_all"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _adjust_object_lists(obj):\n '''\n For creation or update of object that have attribute which contains a list Zabbix awaits plain list of IDs while\n querying Zabbix for same object returns list of dicts\n\n :param obj: Zabbix object parameters\n '''\n for subcomp in TEMPLATE_COMPONENT_DEF:\n if subcomp in obj and TEMPLATE_COMPONENT_DEF[subcomp]['adjust']:\n obj[subcomp] = [item[TEMPLATE_COMPONENT_DEF[subcomp]['qidname']] for item in obj[subcomp]]", "response": "Adjusts the object lists of objects that have attribute which contains a list Zabbix awaits plain list of IDs while\n querying Zabbix for same object returns list of dicts"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _manage_component(component, parent_id, defined, existing, template_id=None, **kwargs):\n '''\n Takes particular component list, compares it with existing, call appropriate API methods - create, update, delete.\n\n :param component: component name\n :param parent_id: ID of parent entity under which component should be created\n :param defined: list of defined items of named component\n :param existing: list of existing items of named component\n :param template_id: In case that component need also template ID for creation (although parent_id is given?!?!?)\n '''\n zabbix_id_mapper = __salt__['zabbix.get_zabbix_id_mapper']()\n\n dry_run = __opts__['test']\n c_def = TEMPLATE_COMPONENT_DEF[component]\n compare_key = c_def['filter']\n\n defined_set = set([item[compare_key] for item in defined])\n existing_set = set([item[compare_key] for item in existing])\n\n create_set = defined_set - existing_set\n update_set = defined_set & existing_set\n delete_set = existing_set - defined_set\n\n create_list = [item for item in defined if item[compare_key] in create_set]\n for object_params in create_list:\n if parent_id:\n object_params.update({c_def['pid_ref_name']: parent_id})\n\n if 'pid_ref_name2' in c_def:\n object_params.update({c_def['pid_ref_name2']: template_id})\n\n _adjust_object_lists(object_params)\n\n if not dry_run:\n object_create = __salt__['zabbix.run_query'](c_def['qtype'] + '.create', object_params, **kwargs)\n if object_create:\n object_ids = object_create[c_def['res_id_name']]\n CHANGE_STACK.append({'component': component, 'action': 'create', 'params': object_params,\n c_def['filter']: object_params[c_def['filter']], 'object_id': object_ids})\n else:\n CHANGE_STACK.append({'component': component, 'action': 'create', 'params': object_params,\n 'object_id': 'CREATED '+TEMPLATE_COMPONENT_DEF[component]['qtype']+' ID'})\n\n delete_list = [item for item in existing if item[compare_key] in delete_set]\n for object_del in delete_list:\n object_id_name = zabbix_id_mapper[c_def['qtype']]\n CHANGE_STACK.append({'component': component, 'action': 'delete', 'params': [object_del[object_id_name]]})\n if not dry_run:\n __salt__['zabbix.run_query'](c_def['qtype'] + '.delete', [object_del[object_id_name]], **kwargs)\n\n for object_name in update_set:\n ditem = next((item for item in defined if item[compare_key] == object_name), None)\n eitem = next((item for item in existing if item[compare_key] == object_name), None)\n diff_params = __salt__['zabbix.compare_params'](ditem, eitem, True)\n\n if diff_params['new']:\n diff_params['new'][zabbix_id_mapper[c_def['qtype']]] = eitem[zabbix_id_mapper[c_def['qtype']]]\n diff_params['old'][zabbix_id_mapper[c_def['qtype']]] = eitem[zabbix_id_mapper[c_def['qtype']]]\n _adjust_object_lists(diff_params['new'])\n _adjust_object_lists(diff_params['old'])\n CHANGE_STACK.append({'component': component, 'action': 'update', 'params': diff_params['new']})\n\n if not dry_run:\n __salt__['zabbix.run_query'](c_def['qtype'] + '.update', diff_params['new'], **kwargs)", "response": "Manage a component list"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef is_present(name, **kwargs):\n '''\n Check if Zabbix Template already exists.\n\n :param name: Zabbix Template name\n :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)\n :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)\n :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)\n\n .. code-block:: yaml\n\n does_zabbix-template-exist:\n zabbix_template.is_present:\n - name: Template OS Linux\n '''\n ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}\n\n try:\n object_id = __salt__['zabbix.get_object_id_by_params']('template', {'filter': {'name': name}}, **kwargs)\n except SaltException:\n object_id = False\n\n if not object_id:\n ret['result'] = False\n ret['comment'] = 'Zabbix Template \"{0}\" does not exist.'.format(name)\n else:\n ret['result'] = True\n ret['comment'] = 'Zabbix Template \"{0}\" exists.'.format(name)\n\n return ret", "response": "Check if Zabbix Template exists."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef present(name, params, static_host_list=True, **kwargs):\n '''\n Creates Zabbix Template object or if differs update it according defined parameters. See Zabbix API documentation.\n\n Zabbix API version: >3.0\n\n :param name: Zabbix Template name\n :param params: Additional parameters according to Zabbix API documentation\n :param static_host_list: If hosts assigned to the template are controlled\n only by this state or can be also assigned externally\n :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)\n :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)\n :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)\n\n .. note::\n\n If there is a need to get a value from current zabbix online (e.g. ids of host groups you want the template\n to be associated with), put a dictionary with two keys \"query_object\" and \"query_name\" instead of the value.\n In this example we want to create template named \"Testing Template\", assign it to hostgroup Templates,\n link it to two ceph nodes and create a macro.\n\n .. note::\n\n IMPORTANT NOTE:\n Objects (except for template name) are identified by name (or by other key in some exceptional cases)\n so changing name of object means deleting old one and creating new one with new ID !!!\n\n .. note::\n\n NOT SUPPORTED FEATURES:\n - linked templates\n - trigger dependencies\n - groups and group prototypes for host prototypes\n\n SLS Example:\n\n .. code-block:: yaml\n\n zabbix-template-present:\n zabbix_template.present:\n - name: Testing Template\n # Do not touch existing assigned hosts\n # True will detach all other hosts than defined here\n - static_host_list: False\n - params:\n description: Template for Ceph nodes\n groups:\n # groups must already exist\n # template must be at least in one hostgroup\n - groupid:\n query_object: hostgroup\n query_name: Templates\n macros:\n - macro: \"{$CEPH_CLUSTER_NAME}\"\n value: ceph\n hosts:\n # hosts must already exist\n - hostid:\n query_object: host\n query_name: ceph-osd-01\n - hostid:\n query_object: host\n query_name: ceph-osd-02\n # templates:\n # Linked templates - not supported by state module but can be linked manually (will not be touched)\n\n applications:\n - name: Ceph OSD\n items:\n - name: Ceph OSD avg fill item\n key_: ceph.osd_avg_fill\n type: 2\n value_type: 0\n delay: 60\n units: '%'\n description: 'Average fill of OSD'\n applications:\n - applicationid:\n query_object: application\n query_name: Ceph OSD\n triggers:\n - description: \"Ceph OSD filled more that 90%\"\n expression: \"{{'{'}}Testing Template:ceph.osd_avg_fill.last(){{'}'}}>90\"\n priority: 4\n discoveries:\n - name: Mounted filesystem discovery\n key_: vfs.fs.discovery\n type: 0\n delay: 60\n itemprototypes:\n - name: Free disk space on {{'{#'}}FSNAME}\n key_: vfs.fs.size[{{'{#'}}FSNAME},free]\n type: 0\n value_type: 3\n delay: 60\n applications:\n - applicationid:\n query_object: application\n query_name: Ceph OSD\n triggerprototypes:\n - description: \"Free disk space is less than 20% on volume {{'{#'}}FSNAME{{'}'}}\"\n expression: \"{{'{'}}Testing Template:vfs.fs.size[{{'{#'}}FSNAME},free].last(){{'}'}}<20\"\n graphs:\n - name: Ceph OSD avg fill graph\n width: 900\n height: 200\n graphtype: 0\n gitems:\n - color: F63100\n itemid:\n query_object: item\n query_name: Ceph OSD avg fill item\n screens:\n - name: Ceph\n hsize: 1\n vsize: 1\n screenitems:\n - x: 0\n y: 0\n resourcetype: 0\n resourceid:\n query_object: graph\n query_name: Ceph OSD avg fill graph\n '''\n zabbix_id_mapper = __salt__['zabbix.get_zabbix_id_mapper']()\n\n dry_run = __opts__['test']\n ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}\n params['host'] = name\n\n del CHANGE_STACK[:]\n\n # Divide template yaml definition into parts\n # - template definition itself\n # - simple template components\n # - components that have other sub-components\n # (e.g. discoveries - where parent ID is needed in advance for sub-component manipulation)\n template_definition = {}\n template_components = {}\n discovery_components = []\n\n for attr in params:\n if attr in TEMPLATE_COMPONENT_ORDER and six.text_type(attr) != 'discoveries':\n template_components[attr] = params[attr]\n\n elif six.text_type(attr) == 'discoveries':\n d_rules = []\n for d_rule in params[attr]:\n d_rule_components = {'query_pid': {'component': attr,\n 'filter_val': d_rule[TEMPLATE_COMPONENT_DEF[attr]['filter']]}}\n for proto_name in DISCOVERYRULE_COMPONENT_ORDER:\n if proto_name in d_rule:\n d_rule_components[proto_name] = d_rule[proto_name]\n del d_rule[proto_name]\n\n discovery_components.append(d_rule_components)\n d_rules.append(d_rule)\n\n template_components[attr] = d_rules\n\n else:\n template_definition[attr] = params[attr]\n\n # if a component is not defined, it means to remove existing items during update (empty list)\n for attr in TEMPLATE_COMPONENT_ORDER:\n if attr not in template_components:\n template_components[attr] = []\n\n # if a component is not defined, it means to remove existing items during update (empty list)\n for attr in TEMPLATE_RELATIONS:\n template_definition[attr] = params[attr] if attr in params and params[attr] else []\n\n defined_obj = __salt__['zabbix.substitute_params'](template_definition, **kwargs)\n log.info('SUBSTITUTED template_definition: %s', six.text_type(json.dumps(defined_obj, indent=4)))\n\n tmpl_get = __salt__['zabbix.run_query']('template.get',\n {'output': 'extend', 'selectGroups': 'groupid', 'selectHosts': 'hostid',\n 'selectTemplates': 'templateid', 'selectMacros': 'extend',\n 'filter': {'host': name}},\n **kwargs)\n log.info('TEMPLATE get result: %s', six.text_type(json.dumps(tmpl_get, indent=4)))\n\n existing_obj = __salt__['zabbix.substitute_params'](tmpl_get[0], **kwargs) \\\n if tmpl_get and len(tmpl_get) == 1 else False\n\n if existing_obj:\n template_id = existing_obj[zabbix_id_mapper['template']]\n\n if not static_host_list:\n # Prepare objects for comparison\n defined_wo_hosts = defined_obj\n if 'hosts' in defined_obj:\n defined_hosts = defined_obj['hosts']\n del defined_wo_hosts['hosts']\n else:\n defined_hosts = []\n\n existing_wo_hosts = existing_obj\n if 'hosts' in existing_obj:\n existing_hosts = existing_obj['hosts']\n del existing_wo_hosts['hosts']\n else:\n existing_hosts = []\n\n # Compare host list separately from the rest of the object comparison since the merged list is needed for\n # update\n hosts_list = _diff_and_merge_host_list(defined_hosts, existing_hosts)\n\n # Compare objects without hosts\n diff_params = __salt__['zabbix.compare_params'](defined_wo_hosts, existing_wo_hosts, True)\n\n # Merge comparison results together\n if ('new' in diff_params and 'hosts' in diff_params['new']) or hosts_list:\n diff_params['new']['hosts'] = hosts_list\n\n else:\n diff_params = __salt__['zabbix.compare_params'](defined_obj, existing_obj, True)\n\n if diff_params['new']:\n diff_params['new'][zabbix_id_mapper['template']] = template_id\n diff_params['old'][zabbix_id_mapper['template']] = template_id\n log.info('TEMPLATE: update params: %s', six.text_type(json.dumps(diff_params, indent=4)))\n\n CHANGE_STACK.append({'component': 'template', 'action': 'update', 'params': diff_params['new']})\n if not dry_run:\n tmpl_update = __salt__['zabbix.run_query']('template.update', diff_params['new'], **kwargs)\n log.info('TEMPLATE update result: %s', six.text_type(tmpl_update))\n\n else:\n CHANGE_STACK.append({'component': 'template', 'action': 'create', 'params': defined_obj})\n if not dry_run:\n tmpl_create = __salt__['zabbix.run_query']('template.create', defined_obj, **kwargs)\n log.info('TEMPLATE create result: %s', tmpl_create)\n if tmpl_create:\n template_id = tmpl_create['templateids'][0]\n\n log.info('\\n\\ntemplate_components: %s', json.dumps(template_components, indent=4))\n log.info('\\n\\ndiscovery_components: %s', json.dumps(discovery_components, indent=4))\n log.info('\\n\\nCurrent CHANGE_STACK: %s', six.text_type(json.dumps(CHANGE_STACK, indent=4)))\n\n if existing_obj or not dry_run:\n for component in TEMPLATE_COMPONENT_ORDER:\n log.info('\\n\\n\\n\\n\\nCOMPONENT: %s\\n\\n', six.text_type(json.dumps(component)))\n # 1) query for components which belongs to the template\n existing_c_list = _get_existing_template_c_list(component, template_id, **kwargs)\n existing_c_list_subs = __salt__['zabbix.substitute_params'](existing_c_list, **kwargs) \\\n if existing_c_list else []\n\n if component in template_components:\n defined_c_list_subs = __salt__['zabbix.substitute_params'](\n template_components[component],\n extend_params={TEMPLATE_COMPONENT_DEF[component]['qselectpid']: template_id},\n filter_key=TEMPLATE_COMPONENT_DEF[component]['filter'],\n **kwargs)\n else:\n defined_c_list_subs = []\n # 2) take lists of particular component and compare -> do create, update and delete actions\n _manage_component(component, template_id, defined_c_list_subs, existing_c_list_subs, **kwargs)\n\n log.info('\\n\\nCurrent CHANGE_STACK: %s', six.text_type(json.dumps(CHANGE_STACK, indent=4)))\n\n for d_rule_component in discovery_components:\n # query for parent id -> \"query_pid\": {\"filter_val\": \"vfs.fs.discovery\", \"component\": \"discoveries\"}\n q_def = d_rule_component['query_pid']\n c_def = TEMPLATE_COMPONENT_DEF[q_def['component']]\n q_object = c_def['qtype']\n q_params = dict(c_def['output'])\n q_params.update({c_def['qselectpid']: template_id})\n q_params.update({'filter': {c_def['filter']: q_def['filter_val']}})\n\n parent_id = __salt__['zabbix.get_object_id_by_params'](q_object, q_params, **kwargs)\n\n for proto_name in DISCOVERYRULE_COMPONENT_ORDER:\n log.info('\\n\\n\\n\\n\\nPROTOTYPE_NAME: %s\\n\\n', six.text_type(json.dumps(proto_name)))\n existing_p_list = _get_existing_template_c_list(proto_name, parent_id, **kwargs)\n existing_p_list_subs = __salt__['zabbix.substitute_params'](existing_p_list, **kwargs)\\\n if existing_p_list else []\n\n if proto_name in d_rule_component:\n defined_p_list_subs = __salt__['zabbix.substitute_params'](\n d_rule_component[proto_name],\n extend_params={c_def['qselectpid']: template_id},\n **kwargs)\n else:\n defined_p_list_subs = []\n\n _manage_component(proto_name,\n parent_id,\n defined_p_list_subs,\n existing_p_list_subs,\n template_id=template_id,\n **kwargs)\n\n log.info('\\n\\nCurrent CHANGE_STACK: %s', six.text_type(json.dumps(CHANGE_STACK, indent=4)))\n\n if not CHANGE_STACK:\n ret['result'] = True\n ret['comment'] = 'Zabbix Template \"{0}\" already exists and corresponds to a definition.'.format(name)\n else:\n tmpl_action = next((item for item in CHANGE_STACK\n if item['component'] == 'template' and item['action'] == 'create'), None)\n if tmpl_action:\n ret['result'] = True\n if dry_run:\n ret['comment'] = 'Zabbix Template \"{0}\" would be created.'.format(name)\n ret['changes'] = {name: {'old': 'Zabbix Template \"{0}\" does not exist.'.format(name),\n 'new': 'Zabbix Template \"{0}\" would be created '\n 'according definition.'.format(name)}}\n else:\n ret['comment'] = 'Zabbix Template \"{0}\" created.'.format(name)\n ret['changes'] = {name: {'old': 'Zabbix Template \"{0}\" did not exist.'.format(name),\n 'new': 'Zabbix Template \"{0}\" created according definition.'.format(name)}}\n else:\n ret['result'] = True\n if dry_run:\n ret['comment'] = 'Zabbix Template \"{0}\" would be updated.'.format(name)\n ret['changes'] = {name: {'old': 'Zabbix Template \"{0}\" differs.'.format(name),\n 'new': 'Zabbix Template \"{0}\" would be updated '\n 'according definition.'.format(name)}}\n else:\n ret['comment'] = 'Zabbix Template \"{0}\" updated.'.format(name)\n ret['changes'] = {name: {'old': 'Zabbix Template \"{0}\" differed.'.format(name),\n 'new': 'Zabbix Template \"{0}\" updated according definition.'.format(name)}}\n\n return ret", "response": "Creates or updates a Zabbix Template object or updates it according to the given parameters."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef absent(name, **kwargs):\n '''\n Makes the Zabbix Template to be absent (either does not exist or delete it).\n\n :param name: Zabbix Template name\n :param _connection_user: Optional - zabbix user (can also be set in opts or pillar, see module's docstring)\n :param _connection_password: Optional - zabbix password (can also be set in opts or pillar, see module's docstring)\n :param _connection_url: Optional - url of zabbix frontend (can also be set in opts, pillar, see module's docstring)\n\n .. code-block:: yaml\n\n zabbix-template-absent:\n zabbix_template.absent:\n - name: Ceph OSD\n '''\n dry_run = __opts__['test']\n ret = {'name': name, 'result': False, 'comment': '', 'changes': {}}\n\n try:\n object_id = __salt__['zabbix.get_object_id_by_params']('template', {'filter': {'name': name}}, **kwargs)\n except SaltException:\n object_id = False\n\n if not object_id:\n ret['result'] = True\n ret['comment'] = 'Zabbix Template \"{0}\" does not exist.'.format(name)\n else:\n if dry_run:\n ret['result'] = True\n ret['comment'] = 'Zabbix Template \"{0}\" would be deleted.'.format(name)\n ret['changes'] = {name: {'old': 'Zabbix Template \"{0}\" exists.'.format(name),\n 'new': 'Zabbix Template \"{0}\" would be deleted.'.format(name)}}\n else:\n tmpl_delete = __salt__['zabbix.run_query']('template.delete', [object_id], **kwargs)\n if tmpl_delete:\n ret['result'] = True\n ret['comment'] = 'Zabbix Template \"{0}\" deleted.'.format(name)\n ret['changes'] = {name: {'old': 'Zabbix Template \"{0}\" existed.'.format(name),\n 'new': 'Zabbix Template \"{0}\" deleted.'.format(name)}}\n\n return ret", "response": "Ensures that the Zabbix Template with the given name is absent."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nchanging the gid for a named group CLI Example : bash salt '*' group. chgid foo 4376", "response": "def chgid(name, gid):\n '''\n Change the gid for a named group\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' group.chgid foo 4376\n '''\n pre_gid = __salt__['file.group_to_gid'](name)\n if gid == pre_gid:\n return True\n cmd = 'groupmod -g {0} {1}'.format(gid, name)\n __salt__['cmd.run'](cmd, python_shell=False)\n post_gid = __salt__['file.group_to_gid'](name)\n if post_gid != pre_gid:\n return post_gid == gid\n return False"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef gen_key(minion_id, dns_name=None, zone='default', password=None):\n '''\n Generate and return an private_key. If a ``dns_name`` is passed in, the\n private_key will be cached under that name. The type of key and the\n parameters used to generate the key are based on the default certificate\n use policy associated with the specified zone.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run venafi.gen_key <minion_id> [dns_name] [zone] [password]\n '''\n # Get the default certificate use policy associated with the zone\n # so we can generate keys that conform with policy\n\n # The /v1/zones/tag/{name} API call is a shortcut to get the zoneID\n # directly from the name\n\n qdata = __utils__['http.query'](\n '{0}/zones/tag/{1}'.format(_base_url(), zone),\n method='GET',\n decode=True,\n decode_type='json',\n header_dict={\n 'tppl-api-key': _api_key(),\n 'Content-Type': 'application/json',\n },\n )\n\n zone_id = qdata['dict']['id']\n\n # the /v1/certificatepolicies?zoneId API call returns the default\n # certificate use and certificate identity policies\n\n qdata = __utils__['http.query'](\n '{0}/certificatepolicies?zoneId={1}'.format(_base_url(), zone_id),\n method='GET',\n decode=True,\n decode_type='json',\n header_dict={\n 'tppl-api-key': _api_key(),\n 'Content-Type': 'application/json',\n },\n )\n\n policies = qdata['dict']['certificatePolicies']\n\n # Extract the key length and key type from the certificate use policy\n # and generate the private key accordingly\n\n for policy in policies:\n if policy['certificatePolicyType'] == \"CERTIFICATE_USE\":\n keyTypes = policy['keyTypes']\n # in case multiple keytypes and key lengths are supported\n # always use the first key type and key length\n keygen_type = keyTypes[0]['keyType']\n key_len = keyTypes[0]['keyLengths'][0]\n\n if int(key_len) < 2048:\n key_len = 2048\n\n if keygen_type == \"RSA\":\n if HAS_M2:\n gen = RSA.gen_key(key_len, 65537)\n private_key = gen.as_pem(cipher='des_ede3_cbc', callback=lambda x: six.b(password))\n else:\n gen = RSA.generate(bits=key_len)\n private_key = gen.exportKey('PEM', password)\n if dns_name is not None:\n bank = 'venafi/domains'\n cache = salt.cache.Cache(__opts__, syspaths.CACHE_DIR)\n try:\n data = cache.fetch(bank, dns_name)\n data['private_key'] = private_key\n data['minion_id'] = minion_id\n except TypeError:\n data = {'private_key': private_key,\n 'minion_id': minion_id}\n cache.store(bank, dns_name, data)\n return private_key", "response": "Generate and return a private key for a specific minion."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef gen_csr(\n minion_id,\n dns_name,\n zone='default',\n country=None,\n state=None,\n loc=None,\n org=None,\n org_unit=None,\n password=None,\n ):\n '''\n Generate a csr using the host's private_key.\n Analogous to:\n\n .. code-block:: bash\n\n VCert gencsr -cn [CN Value] -o \"Beta Organization\" -ou \"Beta Group\" \\\n -l \"Palo Alto\" -st \"California\" -c US\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run venafi.gen_csr <minion_id> <dns_name>\n '''\n tmpdir = tempfile.mkdtemp()\n os.chmod(tmpdir, 0o700)\n\n bank = 'venafi/domains'\n cache = salt.cache.Cache(__opts__, syspaths.CACHE_DIR)\n data = cache.fetch(bank, dns_name)\n if data is None:\n data = {}\n if 'private_key' not in data:\n data['private_key'] = gen_key(minion_id, dns_name, zone, password)\n\n tmppriv = '{0}/priv'.format(tmpdir)\n tmpcsr = '{0}/csr'.format(tmpdir)\n with salt.utils.files.fopen(tmppriv, 'w') as if_:\n if_.write(salt.utils.stringutils.to_str(data['private_key']))\n\n if country is None:\n country = __opts__.get('venafi', {}).get('country')\n\n if state is None:\n state = __opts__.get('venafi', {}).get('state')\n\n if loc is None:\n loc = __opts__.get('venafi', {}).get('loc')\n\n if org is None:\n org = __opts__.get('venafi', {}).get('org')\n\n if org_unit is None:\n org_unit = __opts__.get('venafi', {}).get('org_unit')\n\n subject = '/C={0}/ST={1}/L={2}/O={3}/OU={4}/CN={5}'.format(\n country,\n state,\n loc,\n org,\n org_unit,\n dns_name,\n )\n\n cmd = \"openssl req -new -sha256 -key {0} -out {1} -subj '{2}'\".format(\n tmppriv,\n tmpcsr,\n subject\n )\n if password is not None:\n cmd += ' -passin pass:{0}'.format(password)\n output = __salt__['salt.cmd']('cmd.run', cmd)\n\n if 'problems making Certificate Request' in output:\n raise CommandExecutionError(\n 'There was a problem generating the CSR. Please ensure that you '\n 'have the following variables set either on the command line, or '\n 'in the venafi section of your master configuration file: '\n 'country, state, loc, org, org_unit'\n )\n\n with salt.utils.files.fopen(tmpcsr, 'r') as of_:\n csr = salt.utils.stringutils.to_unicode(of_.read())\n\n data['minion_id'] = minion_id\n data['csr'] = csr\n cache.store(bank, dns_name, data)\n return csr", "response": "Generate a new private_key for a specific host."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef request(\n minion_id,\n dns_name=None,\n zone='default',\n request_id=None,\n country='US',\n state='California',\n loc='Palo Alto',\n org='Beta Organization',\n org_unit='Beta Group',\n password=None,\n zone_id=None,\n ):\n '''\n Request a new certificate\n\n Uses the following command:\n\n .. code-block:: bash\n\n VCert enroll -z <zone> -k <api key> -cn <domain name>\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run venafi.request <minion_id> <dns_name>\n '''\n if password is not None:\n if password.startswith('sdb://'):\n password = __salt__['sdb.get'](password)\n\n if zone_id is None:\n zone_id = __opts__.get('venafi', {}).get('zone_id')\n\n if zone_id is None and zone is not None:\n zone_id = get_zone_id(zone)\n\n if zone_id is None:\n raise CommandExecutionError(\n 'Either a zone or a zone_id must be passed in or '\n 'configured in the master file. This id can be retreived using '\n 'venafi.show_company <domain>'\n )\n\n private_key = gen_key(minion_id, dns_name, zone, password)\n\n csr = gen_csr(\n minion_id,\n dns_name,\n zone=zone,\n country=country,\n state=state,\n loc=loc,\n org=org,\n org_unit=org_unit,\n password=password,\n )\n\n pdata = salt.utils.json.dumps({\n 'zoneId': zone_id,\n 'certificateSigningRequest': csr,\n })\n\n qdata = __utils__['http.query'](\n '{0}/certificaterequests'.format(_base_url()),\n method='POST',\n data=pdata,\n decode=True,\n decode_type='json',\n header_dict={\n 'tppl-api-key': _api_key(),\n 'Content-Type': 'application/json',\n },\n )\n\n request_id = qdata['dict']['certificateRequests'][0]['id']\n ret = {\n 'request_id': request_id,\n 'private_key': private_key,\n 'csr': csr,\n 'zone': zone,\n }\n\n bank = 'venafi/domains'\n cache = salt.cache.Cache(__opts__, syspaths.CACHE_DIR)\n data = cache.fetch(bank, dns_name)\n if data is None:\n data = {}\n data.update({\n 'minion_id': minion_id,\n 'request_id': request_id,\n 'private_key': private_key,\n 'zone': zone,\n 'csr': csr,\n })\n cache.store(bank, dns_name, data)\n _id_map(minion_id, dns_name)\n\n return ret", "response": "Request a new certificate from a master file"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef register(email):\n '''\n Register a new user account\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run venafi.register email@example.com\n '''\n data = __utils__['http.query'](\n '{0}/useraccounts'.format(_base_url()),\n method='POST',\n data=salt.utils.json.dumps({\n 'username': email,\n 'userAccountType': 'API',\n }),\n status=True,\n decode=True,\n decode_type='json',\n header_dict={\n 'Content-Type': 'application/json',\n },\n )\n status = data['status']\n if six.text_type(status).startswith('4') or six.text_type(status).startswith('5'):\n raise CommandExecutionError(\n 'There was an API error: {0}'.format(data['error'])\n )\n return data.get('dict', {})", "response": "Register a new user account"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nshows company information especially the company id", "response": "def show_company(domain):\n '''\n Show company information, especially the company id\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run venafi.show_company example.com\n '''\n data = __utils__['http.query'](\n '{0}/companies/domain/{1}'.format(_base_url(), domain),\n status=True,\n decode=True,\n decode_type='json',\n header_dict={\n 'tppl-api-key': _api_key(),\n },\n )\n status = data['status']\n if six.text_type(status).startswith('4') or six.text_type(status).startswith('5'):\n raise CommandExecutionError(\n 'There was an API error: {0}'.format(data['error'])\n )\n return data.get('dict', {})"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef show_cert(id_):\n '''\n Show certificate requests for this API key\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run venafi.show_cert 01234567-89ab-cdef-0123-456789abcdef\n '''\n data = __utils__['http.query'](\n '{0}/certificaterequests/{1}/certificate'.format(_base_url(), id_),\n params={\n 'format': 'PEM',\n 'chainOrder': 'ROOT_FIRST'\n },\n status=True,\n text=True,\n header_dict={'tppl-api-key': _api_key()},\n )\n status = data['status']\n if six.text_type(status).startswith('4') or six.text_type(status).startswith('5'):\n raise CommandExecutionError(\n 'There was an API error: {0}'.format(data['error'])\n )\n data = data.get('body', '')\n csr_data = __utils__['http.query'](\n '{0}/certificaterequests/{1}'.format(_base_url(), id_),\n status=True,\n decode=True,\n decode_type='json',\n header_dict={'tppl-api-key': _api_key()},\n )\n status = csr_data['status']\n if six.text_type(status).startswith('4') or six.text_type(status).startswith('5'):\n raise CommandExecutionError(\n 'There was an API error: {0}'.format(csr_data['error'])\n )\n csr_data = csr_data.get('dict', {})\n certs = _parse_certs(data)\n dns_name = ''\n for item in csr_data['certificateName'].split(','):\n if item.startswith('cn='):\n dns_name = item.split('=')[1]\n #certs['CSR Data'] = csr_data\n\n cache = salt.cache.Cache(__opts__, syspaths.CACHE_DIR)\n domain_data = cache.fetch('venafi/domains', dns_name)\n if domain_data is None:\n domain_data = {}\n certs['private_key'] = domain_data.get('private_key')\n domain_data.update(certs)\n cache.store('venafi/domains', dns_name, domain_data)\n\n certs['request_id'] = id_\n return certs", "response": "Show certificate requests for this API key"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_domain_cache():\n '''\n List domains that have been cached\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run venafi.list_domain_cache\n '''\n cache = salt.cache.Cache(__opts__, syspaths.CACHE_DIR)\n return cache.list('venafi/domains')", "response": "List domains that have been cached by the master"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef del_cached_domain(domains):\n '''\n Delete cached domains from the master\n\n CLI Example:\n\n .. code-block:: bash\n\n salt-run venafi.del_cached_domain domain1.example.com,domain2.example.com\n '''\n cache = salt.cache.Cache(__opts__, syspaths.CACHE_DIR)\n if isinstance(domains, six.string_types):\n domains = domains.split(',')\n if not isinstance(domains, list):\n raise CommandExecutionError(\n 'You must pass in either a string containing one or more domains '\n 'separated by commas, or a list of single domain strings'\n )\n success = []\n failed = []\n for domain in domains:\n try:\n cache.flush('venafi/domains', domain)\n success.append(domain)\n except CommandExecutionError:\n failed.append(domain)\n return {'Succeeded': success, 'Failed': failed}", "response": "Delete cached domains from the master master"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef user_present(name, password=None, roles=None, encrypted=False, crypt_salt=None, algorithm='sha256'):\n '''\n Ensure a user is present with the specified groups\n\n name\n Name of user\n\n password\n Encrypted or Plain Text password for user\n\n roles\n List of roles the user should be assigned. Any roles not in this list will be removed\n\n encrypted\n Whether the password is encrypted already or not. Defaults to False\n\n crypt_salt\n Salt to use when encrypting the password. Default is None (salt is\n randomly generated for unhashed passwords)\n\n algorithm\n Algorithm to use for hashing password. Defaults to sha256.\n Accepts md5, blowfish, sha256, sha512\n\n .. note: sha512 may make the hash too long to save in NX OS which limits the has to 64 characters\n\n Examples:\n\n .. code-block:: yaml\n\n create:\n onyx.user_present:\n - name: daniel\n - roles:\n - vdc-admin\n\n set_password:\n onyx.user_present:\n - name: daniel\n - password: admin\n - roles:\n - network-admin\n\n update:\n onyx.user_present:\n - name: daniel\n - password: AiN9jaoP\n - roles:\n - network-admin\n - vdc-admin\n\n '''\n ret = {'name': name,\n 'result': False,\n 'changes': {},\n 'comment': ''}\n\n change_password = False\n if password is not None:\n change_password = not __salt__['onyx.cmd']('check_password', username=name,\n password=password, encrypted=encrypted)\n\n change_roles = False\n if roles is not None:\n cur_roles = __salt__['onyx.cmd']('get_roles', username=name)\n change_roles = set(roles) != set(cur_roles)\n\n old_user = __salt__['onyx.cmd']('get_user', username=name)\n\n if not any([change_password, change_roles, not old_user]):\n ret['result'] = True\n ret['comment'] = 'User already exists'\n return ret\n\n if change_roles is True:\n remove_roles = set(cur_roles) - set(roles)\n add_roles = set(roles) - set(cur_roles)\n\n if __opts__['test'] is True:\n ret['result'] = None\n if not old_user:\n ret['comment'] = 'User will be created'\n if password is not None:\n ret['changes']['password'] = True\n if roles is not None:\n ret['changes']['role'] = {'add': roles,\n 'remove': [], }\n return ret\n if change_password is True:\n ret['comment'] = 'User will be updated'\n ret['changes']['password'] = True\n if change_roles is True:\n ret['comment'] = 'User will be updated'\n ret['changes']['roles'] = {'add': list(add_roles), 'remove': list(remove_roles)}\n return ret\n\n if change_password is True:\n new_user = __salt__['onyx.cmd']('set_password', username=name, password=password,\n encrypted=encrypted, role=roles[0] if roles else None,\n crypt_salt=crypt_salt, algorithm=algorithm)\n ret['changes']['password'] = {\n 'new': new_user,\n 'old': old_user,\n }\n if change_roles is True:\n for role in add_roles:\n __salt__['onyx.cmd']('set_role', username=name, role=role)\n for role in remove_roles:\n __salt__['onyx.cmd']('unset_role', username=name, role=role)\n ret['changes']['roles'] = {\n 'new': __salt__['onyx.cmd']('get_roles', username=name),\n 'old': cur_roles,\n }\n\n correct_password = True\n if password is not None:\n correct_password = __salt__['onyx.cmd']('check_password', username=name, password=password,\n encrypted=encrypted)\n\n correct_roles = True\n if roles is not None:\n cur_roles = __salt__['onyx.cmd']('get_roles', username=name)\n correct_roles = set(roles) != set(cur_roles)\n\n if not correct_roles:\n ret['comment'] = 'Failed to set correct roles'\n elif not correct_password:\n ret['comment'] = 'Failed to set correct password'\n else:\n ret['comment'] = 'User set correctly'\n ret['result'] = True\n\n return ret", "response": "Ensure a user is present with the specified groups and optionally roles."} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nensure a user is absent", "response": "def user_absent(name):\n '''\n Ensure a user is not present\n\n name\n username to remove if it exists\n\n Examples:\n\n .. code-block:: yaml\n\n delete:\n onyx.user_absent:\n - name: daniel\n '''\n\n ret = {'name': name,\n 'result': False,\n 'changes': {},\n 'comment': ''}\n\n old_user = __salt__['onyx.cmd']('get_user', username=name)\n\n if not old_user:\n ret['result'] = True\n ret['comment'] = 'User does not exist'\n return ret\n\n if __opts__['test'] is True and old_user:\n ret['result'] = None\n ret['comment'] = 'User will be removed'\n ret['changes']['old'] = old_user\n ret['changes']['new'] = ''\n return ret\n\n __salt__['onyx.cmd']('remove_user', username=name)\n\n if __salt__['onyx.cmd']('get_user', username=name):\n ret['comment'] = 'Failed to remove user'\n else:\n ret['result'] = True\n ret['comment'] = 'User removed'\n ret['changes']['old'] = old_user\n ret['changes']['new'] = ''\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef config_present(name):\n '''\n Ensure a specific configuration line exists in the running config\n\n name\n config line to set\n\n Examples:\n\n .. code-block:: yaml\n\n add snmp group:\n onyx.config_present:\n - names:\n - snmp-server community randoSNMPstringHERE group network-operator\n - snmp-server community AnotherRandomSNMPSTring group network-admin\n\n add snmp acl:\n onyx.config_present:\n - names:\n - snmp-server community randoSNMPstringHERE use-acl snmp-acl-ro\n - snmp-server community AnotherRandomSNMPSTring use-acl snmp-acl-rw\n '''\n ret = {'name': name,\n 'result': False,\n 'changes': {},\n 'comment': ''}\n\n matches = __salt__['onyx.cmd']('find', name)\n\n if matches:\n ret['result'] = True\n ret['comment'] = 'Config is already set'\n\n elif __opts__['test'] is True:\n ret['result'] = None\n ret['comment'] = 'Config will be added'\n ret['changes']['new'] = name\n\n else:\n __salt__['onyx.cmd']('add_config', name)\n matches = __salt__['onyx.cmd']('find', name)\n if matches:\n ret['result'] = True\n ret['comment'] = 'Successfully added config'\n ret['changes']['new'] = name\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to add config'\n\n return ret", "response": "Ensure a specific configuration line exists in the running config"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef config_absent(name):\n '''\n Ensure a specific configuration line does not exist in the running config\n\n name\n config line to remove\n\n Examples:\n\n .. code-block:: yaml\n\n add snmp group:\n onyx.config_absent:\n - names:\n - snmp-server community randoSNMPstringHERE group network-operator\n - snmp-server community AnotherRandomSNMPSTring group network-admin\n\n .. note::\n For certain cases extra lines could be removed based on dependencies.\n In this example, included after the example for config_present, the\n ACLs would be removed because they depend on the existence of the\n group.\n\n '''\n ret = {'name': name,\n 'result': False,\n 'changes': {},\n 'comment': ''}\n\n matches = __salt__['onyx.cmd']('find', name)\n\n if not matches:\n ret['result'] = True\n ret['comment'] = 'Config is already absent'\n\n elif __opts__['test'] is True:\n ret['result'] = None\n ret['comment'] = 'Config will be removed'\n ret['changes']['new'] = name\n\n else:\n __salt__['onyx.cmd']('delete_config', name)\n matches = __salt__['onyx.cmd']('find', name)\n if not matches:\n ret['result'] = True\n ret['comment'] = 'Successfully deleted config'\n ret['changes']['new'] = name\n else:\n ret['result'] = False\n ret['comment'] = 'Failed to delete config'\n\n return ret", "response": "Ensure a specific configuration line does not exist in the running config"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreplace all instances of a string or full line in the running config with a replacement text.", "response": "def replace(name, repl, full_match=False):\n '''\n Replace all instances of a string or full line in the running config\n\n name\n String to replace\n\n repl\n The replacement text\n\n full_match\n Whether `name` will match the full line or only a subset of the line.\n Defaults to False. When False, .* is added around `name` for matching\n in the `show run` config.\n\n Examples:\n\n .. code-block:: yaml\n\n replace snmp string:\n onyx.replace:\n - name: randoSNMPstringHERE\n - repl: NEWrandoSNMPstringHERE\n\n replace full snmp string:\n onyx.replace:\n - name: ^snmp-server community randoSNMPstringHERE group network-operator$\n - repl: snmp-server community NEWrandoSNMPstringHERE group network-operator\n - full_match: True\n\n .. note::\n The first example will replace the SNMP string on both the group and\n the ACL, so you will not lose the ACL setting. Because the second is\n an exact match of the line, when the group is removed, the ACL is\n removed, but not readded, because it was not matched.\n\n '''\n ret = {'name': name,\n 'result': False,\n 'changes': {},\n 'comment': ''}\n\n if full_match is False:\n search = '^.*{0}.*$'.format(name)\n else:\n search = name\n\n matches = __salt__['onyx.cmd']('find', search)\n\n if not matches:\n ret['result'] = True\n ret['comment'] = 'Nothing found to replace'\n return ret\n\n if __opts__['test'] is True:\n ret['result'] = None\n ret['comment'] = 'Configs will be changed'\n ret['changes']['old'] = matches\n ret['changes']['new'] = [re.sub(name, repl, match) for match in matches]\n return ret\n\n ret['changes'] = __salt__['onyx.cmd']('replace', name, repl, full_match=full_match)\n\n matches = __salt__['onyx.cmd']('find', search)\n\n if matches:\n ret['result'] = False\n ret['comment'] = 'Failed to replace all instances of \"{0}\"'.format(name)\n else:\n ret['result'] = True\n ret['comment'] = 'Successfully replaced all instances of \"{0}\" with \"{1}\"'.format(name, repl)\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nensures that the named login is present in the specified server.", "response": "def present(name, password=None, domain=None, server_roles=None, options=None, **kwargs):\n '''\n Checks existance of the named login.\n If not present, creates the login with the specified roles and options.\n\n name\n The name of the login to manage\n password\n Creates a SQL Server authentication login\n Since hashed passwords are varbinary values, if the\n new_login_password is 'long', it will be considered\n to be HASHED.\n domain\n Creates a Windows authentication login.\n Needs to be NetBIOS domain or hostname\n server_roles\n Add this login to all the server roles in the list\n options\n Can be a list of strings, a dictionary, or a list of dictionaries\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}\n\n if bool(password) == bool(domain):\n ret['result'] = False\n ret['comment'] = 'One and only one of password and domain should be specifies'\n return ret\n if __salt__['mssql.login_exists'](name, domain=domain, **kwargs):\n ret['comment'] = 'Login {0} is already present (Not going to try to set its password)'.format(name)\n return ret\n if __opts__['test']:\n ret['result'] = None\n ret['comment'] = 'Login {0} is set to be added'.format(name)\n return ret\n\n login_created = __salt__['mssql.login_create'](name,\n new_login_password=password,\n new_login_domain=domain,\n new_login_roles=server_roles,\n new_login_options=_normalize_options(options),\n **kwargs)\n # Non-empty strings are also evaluated to True, so we cannot use if not login_created:\n if login_created is not True:\n ret['result'] = False\n ret['comment'] = 'Login {0} failed to be added: {1}'.format(name, login_created)\n return ret\n ret['comment'] = 'Login {0} has been added. '.format(name)\n ret['changes'][name] = 'Present'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nre-interprets the state returned by salt.state.run using our protocol.", "response": "def _reinterpreted_state(state):\n '''\n Re-interpret the state returned by salt.state.run using our protocol.\n '''\n ret = state['changes']\n state['changes'] = {}\n state['comment'] = ''\n\n out = ret.get('stdout')\n if not out:\n if ret.get('stderr'):\n state['comment'] = ret['stderr']\n return state\n\n is_json = False\n try:\n data = salt.utils.json.loads(out)\n if not isinstance(data, dict):\n return _failout(\n state,\n 'script JSON output must be a JSON object (e.g., {})!'\n )\n is_json = True\n except ValueError:\n idx = out.rstrip().rfind('\\n')\n if idx != -1:\n out = out[idx + 1:]\n data = {}\n try:\n for item in salt.utils.args.shlex_split(out):\n key, val = item.split('=')\n data[key] = val\n except ValueError:\n state = _failout(\n state,\n 'Failed parsing script output! '\n 'Stdout must be JSON or a line of name=value pairs.'\n )\n state['changes'].update(ret)\n return state\n\n changed = _is_true(data.get('changed', 'no'))\n\n if 'comment' in data:\n state['comment'] = data['comment']\n del data['comment']\n\n if changed:\n for key in ret:\n data.setdefault(key, ret[key])\n\n # if stdout is the state output in JSON, don't show it.\n # otherwise it contains the one line name=value pairs, strip it.\n data['stdout'] = '' if is_json else data.get('stdout', '')[:idx]\n state['changes'] = data\n\n #FIXME: if it's not changed but there's stdout and/or stderr then those\n # won't be shown as the function output. (though, they will be shown\n # inside INFO logs).\n return state"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nexecuting the onlyif and unless logic.", "response": "def mod_run_check(cmd_kwargs, onlyif, unless, creates):\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 # never use VT for onlyif/unless executions because this will lead\n # to quote problems\n cmd_kwargs = copy.deepcopy(cmd_kwargs)\n cmd_kwargs['use_vt'] = False\n cmd_kwargs['bg'] = False\n\n if onlyif is not None:\n if isinstance(onlyif, six.string_types):\n cmd = __salt__['cmd.retcode'](onlyif, ignore_retcode=True, python_shell=True, **cmd_kwargs)\n log.debug('Last command return code: %s', cmd)\n if cmd != 0:\n return {'comment': 'onlyif condition is false',\n 'skip_watch': True,\n 'result': True}\n elif isinstance(onlyif, list):\n for entry in onlyif:\n cmd = __salt__['cmd.retcode'](entry, ignore_retcode=True, python_shell=True, **cmd_kwargs)\n log.debug('Last command \\'%s\\' return code: %s', entry, cmd)\n if cmd != 0:\n return {'comment': 'onlyif condition is false: {0}'.format(entry),\n 'skip_watch': True,\n 'result': True}\n elif not isinstance(onlyif, six.string_types):\n if not onlyif:\n log.debug('Command not run: onlyif did not evaluate to string_type')\n return {'comment': 'onlyif condition is false',\n 'skip_watch': True,\n 'result': True}\n\n if unless is not None:\n if isinstance(unless, six.string_types):\n cmd = __salt__['cmd.retcode'](unless, ignore_retcode=True, python_shell=True, **cmd_kwargs)\n log.debug('Last command return code: %s', cmd)\n if cmd == 0:\n return {'comment': 'unless condition is true',\n 'skip_watch': True,\n 'result': True}\n elif isinstance(unless, list):\n cmd = []\n for entry in unless:\n cmd.append(__salt__['cmd.retcode'](entry, ignore_retcode=True, python_shell=True, **cmd_kwargs))\n log.debug('Last command return code: %s', cmd)\n if all([c == 0 for c in cmd]):\n return {'comment': 'unless condition is true',\n 'skip_watch': True,\n 'result': True}\n elif not isinstance(unless, six.string_types):\n if unless:\n log.debug('Command not run: unless did not evaluate to string_type')\n return {'comment': 'unless condition is true',\n 'skip_watch': True,\n 'result': True}\n\n if isinstance(creates, six.string_types) and os.path.exists(creates):\n return {'comment': '{0} exists'.format(creates),\n 'result': True}\n elif isinstance(creates, list) and all([\n os.path.exists(path) for path in creates\n ]):\n return {'comment': 'All files in creates exist',\n 'result': True}\n\n # No reason to stop, return True\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nexecutes a command in a chroot and wait for the command to complete.", "response": "def wait(name,\n onlyif=None,\n unless=None,\n creates=None,\n cwd=None,\n root=None,\n runas=None,\n shell=None,\n env=(),\n stateful=False,\n umask=None,\n output_loglevel='debug',\n hide_output=False,\n use_vt=False,\n success_retcodes=None,\n success_stdout=None,\n success_stderr=None,\n **kwargs):\n '''\n Run the given command only if the watch statement calls it.\n\n .. note::\n\n Use :mod:`cmd.run <salt.states.cmd.run>` together with :mod:`onchanges </ref/states/requisites#onchanges>`\n instead of :mod:`cmd.wait <salt.states.cmd.wait>`.\n\n name\n The command to execute, remember that the command will execute with the\n path and permissions of the salt-minion.\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 cwd\n The current working directory to execute the command in, defaults to\n /root\n\n root\n Path to the root of the jail to use. If this parameter is set, the command\n will run inside a chroot\n\n runas\n The user name to run the command as\n\n shell\n The shell to use for execution, defaults to /bin/sh\n\n env\n A list of environment variables to be set prior to execution.\n Example:\n\n .. code-block:: yaml\n\n script-foo:\n cmd.wait:\n - env:\n - BATCH: 'yes'\n\n .. warning::\n\n The above illustrates a common PyYAML pitfall, that **yes**,\n **no**, **on**, **off**, **true**, and **false** are all loaded as\n boolean ``True`` and ``False`` values, and must be enclosed in\n quotes to be used as strings. More info on this (and other) PyYAML\n idiosyncrasies can be found :ref:`here <yaml-idiosyncrasies>`.\n\n Variables as values are not evaluated. So $PATH in the following\n example is a literal '$PATH':\n\n .. code-block:: yaml\n\n script-bar:\n cmd.wait:\n - env: \"PATH=/some/path:$PATH\"\n\n One can still use the existing $PATH by using a bit of Jinja:\n\n .. code-block:: jinja\n\n {% set current_path = salt['environ.get']('PATH', '/bin:/usr/bin') %}\n\n mycommand:\n cmd.run:\n - name: ls -l /\n - env:\n - PATH: {{ [current_path, '/my/special/bin']|join(':') }}\n\n umask\n The umask (in octal) to use when running the command.\n\n stateful\n The command being executed is expected to return data about executing\n a state. For more information, see the :ref:`stateful-argument` section.\n\n creates\n Only run if the file specified by ``creates`` do not exist. If you\n specify a list of files then this state will only run if **any** of\n the files does not exist.\n\n .. versionadded:: 2014.7.0\n\n output_loglevel : debug\n Control the loglevel at which the output from the command is logged to\n the minion log.\n\n .. note::\n The command being run will still be logged at the ``debug``\n loglevel regardless, unless ``quiet`` is used for this value.\n\n hide_output : False\n Suppress stdout and stderr in the state's results.\n\n .. note::\n This is separate from ``output_loglevel``, which only handles how\n Salt logs to the minion log.\n\n .. versionadded:: 2018.3.0\n\n use_vt\n Use VT utils (saltstack) to stream the command output more\n interactively to the console and the logs.\n This is experimental.\n\n success_retcodes: This parameter will be allow a list of\n non-zero return codes that should be considered a success. If the\n return code returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: 2019.2.0\n\n success_stdout: This parameter will be allow a list of\n strings that when found in standard out should be considered a success.\n If stdout returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: Neon\n\n success_stderr: This parameter will be allow a list of\n strings that when found in standard error should be considered a success.\n If stderr returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: Neon\n '''\n # Ignoring our arguments is intentional.\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndownloading a script from a remote source and execute it if it calls it.", "response": "def wait_script(name,\n source=None,\n template=None,\n onlyif=None,\n unless=None,\n cwd=None,\n runas=None,\n shell=None,\n env=None,\n stateful=False,\n umask=None,\n use_vt=False,\n output_loglevel='debug',\n hide_output=False,\n success_retcodes=None,\n success_stdout=None,\n success_stderr=None,\n **kwargs):\n '''\n Download a script from a remote source and execute it only if a watch\n statement calls it.\n\n source\n The source script being downloaded to the minion, this source script is\n hosted on the salt master server. If the file is located on the master\n in the directory named spam, and is called eggs, the source string is\n salt://spam/eggs\n\n template\n If this setting is applied then the named templating engine will be\n used to render the downloaded file, currently jinja, mako, and wempy\n are supported\n\n name\n The command to execute, remember that the command will execute with the\n path and permissions of the salt-minion.\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 cwd\n The current working directory to execute the command in, defaults to\n /root\n\n runas\n The user name to run the command as\n\n shell\n The shell to use for execution, defaults to the shell grain\n\n env\n A list of environment variables to be set prior to execution.\n Example:\n\n .. code-block:: yaml\n\n salt://scripts/foo.sh:\n cmd.wait_script:\n - env:\n - BATCH: 'yes'\n\n .. warning::\n\n The above illustrates a common PyYAML pitfall, that **yes**,\n **no**, **on**, **off**, **true**, and **false** are all loaded as\n boolean ``True`` and ``False`` values, and must be enclosed in\n quotes to be used as strings. More info on this (and other) PyYAML\n idiosyncrasies can be found :ref:`here <yaml-idiosyncrasies>`.\n\n Variables as values are not evaluated. So $PATH in the following\n example is a literal '$PATH':\n\n .. code-block:: yaml\n\n salt://scripts/bar.sh:\n cmd.wait_script:\n - env: \"PATH=/some/path:$PATH\"\n\n One can still use the existing $PATH by using a bit of Jinja:\n\n .. code-block:: jinja\n\n {% set current_path = salt['environ.get']('PATH', '/bin:/usr/bin') %}\n\n mycommand:\n cmd.run:\n - name: ls -l /\n - env:\n - PATH: {{ [current_path, '/my/special/bin']|join(':') }}\n\n umask\n The umask (in octal) to use when running the command.\n\n stateful\n The command being executed is expected to return data about executing\n a state. For more information, see the :ref:`stateful-argument` section.\n\n use_vt\n Use VT utils (saltstack) to stream the command output more\n interactively to the console and the logs.\n This is experimental.\n\n output_loglevel : debug\n Control the loglevel at which the output from the command is logged to\n the minion log.\n\n .. note::\n The command being run will still be logged at the ``debug``\n loglevel regardless, unless ``quiet`` is used for this value.\n\n hide_output : False\n Suppress stdout and stderr in the state's results.\n\n .. note::\n This is separate from ``output_loglevel``, which only handles how\n Salt logs to the minion log.\n\n .. versionadded:: 2018.3.0\n\n success_retcodes: This parameter will be allow a list of\n non-zero return codes that should be considered a success. If the\n return code returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: 2019.2.0\n\n success_stdout: This parameter will be allow a list of\n strings that when found in standard out should be considered a success.\n If stdout returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: Neon\n\n success_stderr: This parameter will be allow a list of\n strings that when found in standard error should be considered a success.\n If stderr returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: Neon\n '''\n # Ignoring our arguments is intentional.\n return {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': ''}"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nexecuting a named command in a chroot and return a dictionary of the result.", "response": "def run(name,\n onlyif=None,\n unless=None,\n creates=None,\n cwd=None,\n root=None,\n runas=None,\n shell=None,\n env=None,\n prepend_path=None,\n stateful=False,\n umask=None,\n output_loglevel='debug',\n hide_output=False,\n timeout=None,\n ignore_timeout=False,\n use_vt=False,\n success_retcodes=None,\n success_stdout=None,\n success_stderr=None,\n **kwargs):\n '''\n Run a command if certain circumstances are met. Use ``cmd.wait`` if you\n want to use the ``watch`` requisite.\n\n name\n The command to execute, remember that the command will execute with the\n path and permissions of the salt-minion.\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 a zero exit status\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 a non-zero exit status\n\n cwd\n The current working directory to execute the command in, defaults to\n /root\n\n root\n Path to the root of the jail to use. If this parameter is set, the command\n will run inside a chroot\n\n runas\n The user name to run the command as\n\n shell\n The shell to use for execution, defaults to the shell grain\n\n env\n A list of environment variables to be set prior to execution.\n Example:\n\n .. code-block:: yaml\n\n script-foo:\n cmd.run:\n - env:\n - BATCH: 'yes'\n\n .. warning::\n\n The above illustrates a common PyYAML pitfall, that **yes**,\n **no**, **on**, **off**, **true**, and **false** are all loaded as\n boolean ``True`` and ``False`` values, and must be enclosed in\n quotes to be used as strings. More info on this (and other) PyYAML\n idiosyncrasies can be found :ref:`here <yaml-idiosyncrasies>`.\n\n Variables as values are not evaluated. So $PATH in the following\n example is a literal '$PATH':\n\n .. code-block:: yaml\n\n script-bar:\n cmd.run:\n - env: \"PATH=/some/path:$PATH\"\n\n One can still use the existing $PATH by using a bit of Jinja:\n\n .. code-block:: jinja\n\n {% set current_path = salt['environ.get']('PATH', '/bin:/usr/bin') %}\n\n mycommand:\n cmd.run:\n - name: ls -l /\n - env:\n - PATH: {{ [current_path, '/my/special/bin']|join(':') }}\n\n prepend_path\n $PATH segment to prepend (trailing ':' not necessary) to $PATH. This is\n an easier alternative to the Jinja workaround.\n\n .. versionadded:: 2018.3.0\n\n stateful\n The command being executed is expected to return data about executing\n a state. For more information, see the :ref:`stateful-argument` section.\n\n umask\n The umask (in octal) to use when running the command.\n\n output_loglevel : debug\n Control the loglevel at which the output from the command is logged to\n the minion log.\n\n .. note::\n The command being run will still be logged at the ``debug``\n loglevel regardless, unless ``quiet`` is used for this value.\n\n hide_output : False\n Suppress stdout and stderr in the state's results.\n\n .. note::\n This is separate from ``output_loglevel``, which only handles how\n Salt logs to the minion log.\n\n .. versionadded:: 2018.3.0\n\n quiet\n This option no longer has any functionality and will be removed, please\n set ``output_loglevel`` to ``quiet`` to suppress logging of the\n command.\n\n .. deprecated:: 2014.1.0\n\n timeout\n If the command has not terminated after timeout seconds, send the\n subprocess sigterm, and if sigterm is ignored, follow up with sigkill\n\n ignore_timeout\n Ignore the timeout of commands, which is useful for running nohup\n processes.\n\n .. versionadded:: 2015.8.0\n\n creates\n Only run if the file specified by ``creates`` do not exist. If you\n specify a list of files then this state will only run if **any** of\n the files does not exist.\n\n .. versionadded:: 2014.7.0\n\n use_vt : False\n Use VT utils (saltstack) to stream the command output more\n interactively to the console and the logs.\n This is experimental.\n\n bg : False\n If ``True``, run command in background and do not await or deliver its\n results.\n\n .. versionadded:: 2016.3.6\n\n success_retcodes: This parameter will be allow a list of\n non-zero return codes that should be considered a success. If the\n return code returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: 2019.2.0\n\n success_stdout: This parameter will be allow a list of\n strings that when found in standard out should be considered a success.\n If stdout returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: Neon\n\n success_stderr: This parameter will be allow a list of\n strings that when found in standard error should be considered a success.\n If stderr returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: Neon\n\n .. note::\n\n cmd.run supports the usage of ``reload_modules``. This functionality\n allows you to force Salt to reload all modules. You should only use\n ``reload_modules`` if your cmd.run does some sort of installation\n (such as ``pip``), if you do not reload the modules future items in\n your state which rely on the software being installed will fail.\n\n .. code-block:: yaml\n\n getpip:\n cmd.run:\n - name: /usr/bin/python /usr/local/sbin/get-pip.py\n - unless: which pip\n - require:\n - pkg: python\n - file: /usr/local/sbin/get-pip.py\n - reload_modules: True\n\n '''\n ### NOTE: The keyword arguments in **kwargs are passed directly to the\n ### ``cmd.run_all`` function and cannot be removed from the function\n ### definition, otherwise the use of unsupported arguments in a\n ### ``cmd.run`` state will result in a traceback.\n\n ret = {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n if 'quiet' in kwargs:\n quiet = kwargs.pop('quiet')\n msg = (\n 'The \\'quiet\\' argument for cmd.run has been deprecated since '\n '2014.1.0 and will be removed as of the Neon release. Please set '\n '\\'output_loglevel\\' to \\'quiet\\' instead.'\n )\n salt.utils.versions.warn_until('Neon', msg)\n ret.setdefault('warnings', []).append(msg)\n else:\n quiet = False\n\n test_name = None\n if not isinstance(stateful, list):\n stateful = stateful is True\n elif isinstance(stateful, list) and 'test_name' in stateful[0]:\n test_name = stateful[0]['test_name']\n if __opts__['test'] and test_name:\n name = test_name\n\n # Need the check for None here, if env is not provided then it falls back\n # to None and it is assumed that the environment is not being overridden.\n if env is not None and not isinstance(env, (list, dict)):\n ret['comment'] = ('Invalidly-formatted \\'env\\' parameter. See '\n 'documentation.')\n return ret\n\n cmd_kwargs = copy.deepcopy(kwargs)\n cmd_kwargs.update({'cwd': cwd,\n 'root': root,\n 'runas': runas,\n 'use_vt': use_vt,\n 'shell': shell or __grains__['shell'],\n 'env': env,\n 'prepend_path': prepend_path,\n 'umask': umask,\n 'output_loglevel': output_loglevel,\n 'hide_output': hide_output,\n 'quiet': quiet,\n 'success_retcodes': success_retcodes,\n 'success_stdout': success_stdout,\n 'success_stderr': success_stderr})\n\n cret = mod_run_check(cmd_kwargs, onlyif, unless, creates)\n if isinstance(cret, dict):\n ret.update(cret)\n return ret\n\n if __opts__['test'] and not test_name:\n ret['result'] = None\n ret['comment'] = 'Command \"{0}\" would have been executed'.format(name)\n return _reinterpreted_state(ret) if stateful else ret\n\n if cwd and not os.path.isdir(cwd):\n ret['comment'] = (\n 'Desired working directory \"{0}\" '\n 'is not available'\n ).format(cwd)\n return ret\n\n # Wow, we passed the test, run this sucker!\n try:\n run_cmd = 'cmd.run_all' if not root else 'cmd.run_chroot'\n cmd_all = __salt__[run_cmd](\n cmd=name, timeout=timeout, python_shell=True, **cmd_kwargs\n )\n except Exception as err:\n ret['comment'] = six.text_type(err)\n return ret\n\n ret['changes'] = cmd_all\n ret['result'] = not bool(cmd_all['retcode'])\n ret['comment'] = 'Command \"{0}\" run'.format(name)\n\n # Ignore timeout errors if asked (for nohups) and treat cmd as a success\n if ignore_timeout:\n trigger = 'Timed out after'\n if ret['changes'].get('retcode') == 1 and trigger in ret['changes'].get('stdout'):\n ret['changes']['retcode'] = 0\n ret['result'] = True\n\n if stateful:\n ret = _reinterpreted_state(ret)\n if __opts__['test'] and cmd_all['retcode'] == 0 and ret['changes']:\n ret['result'] = None\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef script(name,\n source=None,\n template=None,\n onlyif=None,\n unless=None,\n creates=None,\n cwd=None,\n runas=None,\n shell=None,\n env=None,\n stateful=False,\n umask=None,\n timeout=None,\n use_vt=False,\n output_loglevel='debug',\n hide_output=False,\n defaults=None,\n context=None,\n success_retcodes=None,\n success_stdout=None,\n success_stderr=None,\n **kwargs):\n '''\n Download a script and execute it with specified arguments.\n\n source\n The location of the script to download. If the file is located on the\n master in the directory named spam, and is called eggs, the source\n string is salt://spam/eggs\n\n template\n If this setting is applied then the named templating engine will be\n used to render the downloaded file. Currently jinja, mako, and wempy\n are supported\n\n name\n Either \"cmd arg1 arg2 arg3...\" (cmd is not used) or a source\n \"salt://...\".\n\n onlyif\n Run the named command only if the command passed to the ``onlyif``\n option returns true\n\n unless\n Run the named command only if the command passed to the ``unless``\n option returns false\n\n cwd\n The current working directory to execute the command in, defaults to\n /root\n\n runas\n The name of the user to run the command as\n\n shell\n The shell to use for execution. The default is set in grains['shell']\n\n env\n A list of environment variables to be set prior to execution.\n Example:\n\n .. code-block:: yaml\n\n salt://scripts/foo.sh:\n cmd.script:\n - env:\n - BATCH: 'yes'\n\n .. warning::\n\n The above illustrates a common PyYAML pitfall, that **yes**,\n **no**, **on**, **off**, **true**, and **false** are all loaded as\n boolean ``True`` and ``False`` values, and must be enclosed in\n quotes to be used as strings. More info on this (and other) PyYAML\n idiosyncrasies can be found :ref:`here <yaml-idiosyncrasies>`.\n\n Variables as values are not evaluated. So $PATH in the following\n example is a literal '$PATH':\n\n .. code-block:: yaml\n\n salt://scripts/bar.sh:\n cmd.script:\n - env: \"PATH=/some/path:$PATH\"\n\n One can still use the existing $PATH by using a bit of Jinja:\n\n .. code-block:: jinja\n\n {% set current_path = salt['environ.get']('PATH', '/bin:/usr/bin') %}\n\n mycommand:\n cmd.run:\n - name: ls -l /\n - env:\n - PATH: {{ [current_path, '/my/special/bin']|join(':') }}\n\n saltenv : ``base``\n The Salt environment to use\n\n umask\n The umask (in octal) to use when running the command.\n\n stateful\n The command being executed is expected to return data about executing\n a state. For more information, see the :ref:`stateful-argument` section.\n\n timeout\n If the command has not terminated after timeout seconds, send the\n subprocess sigterm, and if sigterm is ignored, follow up with sigkill\n\n args\n String of command line args to pass to the script. Only used if no\n args are specified as part of the `name` argument. To pass a string\n containing spaces in YAML, you will need to doubly-quote it: \"arg1\n 'arg two' arg3\"\n\n creates\n Only run if the file specified by ``creates`` do not exist. If you\n specify a list of files then this state will only run if **any** of\n the files does not exist.\n\n .. versionadded:: 2014.7.0\n\n use_vt\n Use VT utils (saltstack) to stream the command output more\n interactively to the console and the logs.\n This is experimental.\n\n context\n .. versionadded:: 2016.3.0\n\n Overrides default context variables passed to the template.\n\n defaults\n .. versionadded:: 2016.3.0\n\n Default context passed to the template.\n\n output_loglevel : debug\n Control the loglevel at which the output from the command is logged to\n the minion log.\n\n .. note::\n The command being run will still be logged at the ``debug``\n loglevel regardless, unless ``quiet`` is used for this value.\n\n hide_output : False\n Suppress stdout and stderr in the state's results.\n\n .. note::\n This is separate from ``output_loglevel``, which only handles how\n Salt logs to the minion log.\n\n .. versionadded:: 2018.3.0\n\n success_retcodes: This parameter will be allow a list of\n non-zero return codes that should be considered a success. If the\n return code returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: 2019.2.0\n\n success_stdout: This parameter will be allow a list of\n strings that when found in standard out should be considered a success.\n If stdout returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: Neon\n\n success_stderr: This parameter will be allow a list of\n strings that when found in standard error should be considered a success.\n If stderr returned from the run matches any in the provided list,\n the return code will be overridden with zero.\n\n .. versionadded:: Neon\n '''\n test_name = None\n if not isinstance(stateful, list):\n stateful = stateful is True\n elif isinstance(stateful, list) and 'test_name' in stateful[0]:\n test_name = stateful[0]['test_name']\n if __opts__['test'] and test_name:\n name = test_name\n\n ret = {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n # Need the check for None here, if env is not provided then it falls back\n # to None and it is assumed that the environment is not being overridden.\n if env is not None and not isinstance(env, (list, dict)):\n ret['comment'] = ('Invalidly-formatted \\'env\\' parameter. See '\n 'documentation.')\n return ret\n\n if context and not isinstance(context, dict):\n ret['comment'] = ('Invalidly-formatted \\'context\\' parameter. Must '\n 'be formed as a dict.')\n return ret\n if defaults and not isinstance(defaults, dict):\n ret['comment'] = ('Invalidly-formatted \\'defaults\\' parameter. Must '\n 'be formed as a dict.')\n return ret\n\n tmpctx = defaults if defaults else {}\n if context:\n tmpctx.update(context)\n\n cmd_kwargs = copy.deepcopy(kwargs)\n cmd_kwargs.update({'runas': runas,\n 'shell': shell or __grains__['shell'],\n 'env': env,\n 'onlyif': onlyif,\n 'unless': unless,\n 'cwd': cwd,\n 'template': template,\n 'umask': umask,\n 'timeout': timeout,\n 'output_loglevel': output_loglevel,\n 'hide_output': hide_output,\n 'use_vt': use_vt,\n 'context': tmpctx,\n 'saltenv': __env__,\n 'success_retcodes': success_retcodes,\n 'success_stdout': success_stdout,\n 'success_stderr': success_stderr})\n\n run_check_cmd_kwargs = {\n 'cwd': cwd,\n 'runas': runas,\n 'shell': shell or __grains__['shell']\n }\n\n # Change the source to be the name arg if it is not specified\n if source is None:\n source = name\n\n # If script args present split from name and define args\n if not cmd_kwargs.get('args', None) and len(name.split()) > 1:\n cmd_kwargs.update({'args': name.split(' ', 1)[1]})\n\n cret = mod_run_check(\n run_check_cmd_kwargs, onlyif, unless, creates\n )\n if isinstance(cret, dict):\n ret.update(cret)\n return ret\n\n if __opts__['test'] and not test_name:\n ret['result'] = None\n ret['comment'] = 'Command \\'{0}\\' would have been ' \\\n 'executed'.format(name)\n return _reinterpreted_state(ret) if stateful else ret\n\n if cwd and not os.path.isdir(cwd):\n ret['comment'] = (\n 'Desired working directory \"{0}\" '\n 'is not available'\n ).format(cwd)\n return ret\n\n # Wow, we passed the test, run this sucker!\n try:\n cmd_all = __salt__['cmd.script'](source, python_shell=True, **cmd_kwargs)\n except (CommandExecutionError, SaltRenderError, IOError) as err:\n ret['comment'] = six.text_type(err)\n return ret\n\n ret['changes'] = cmd_all\n if kwargs.get('retcode', False):\n ret['result'] = not bool(cmd_all)\n else:\n ret['result'] = not bool(cmd_all['retcode'])\n if ret.get('changes', {}).get('cache_error'):\n ret['comment'] = 'Unable to cache script {0} from saltenv ' \\\n '\\'{1}\\''.format(source, __env__)\n else:\n ret['comment'] = 'Command \\'{0}\\' run'.format(name)\n if stateful:\n ret = _reinterpreted_state(ret)\n if __opts__['test'] and cmd_all['retcode'] == 0 and ret['changes']:\n ret['result'] = None\n return ret", "response": "Download a script and execute it with specified arguments."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef call(name,\n func,\n args=(),\n kws=None,\n onlyif=None,\n unless=None,\n creates=None,\n output_loglevel='debug',\n hide_output=False,\n use_vt=False,\n **kwargs):\n '''\n Invoke a pre-defined Python function with arguments specified in the state\n declaration. This function is mainly used by the\n :mod:`salt.renderers.pydsl` renderer.\n\n The interpretation of ``onlyif`` and ``unless`` arguments are identical to\n those of :mod:`cmd.run <salt.states.cmd.run>`, and all other\n arguments(``cwd``, ``runas``, ...) allowed by :mod:`cmd.run\n <salt.states.cmd.run>` are allowed here, except that their effects apply\n only to the commands specified in `onlyif` and `unless` rather than to the\n function to be invoked.\n\n In addition, the ``stateful`` argument has no effects here.\n\n The return value of the invoked function will be interpreted as follows.\n\n If it's a dictionary then it will be passed through to the state system,\n which expects it to have the usual structure returned by any salt state\n function.\n\n Otherwise, the return value (denoted as ``result`` in the code below) is\n expected to be a JSON serializable object, and this dictionary is returned:\n\n .. code-block:: python\n\n {\n 'name': name\n 'changes': {'retval': result},\n 'result': True if result is None else bool(result),\n 'comment': result if isinstance(result, six.string_types) else ''\n }\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': False,\n 'comment': ''}\n\n cmd_kwargs = {'cwd': kwargs.get('cwd'),\n 'runas': kwargs.get('user'),\n 'shell': kwargs.get('shell') or __grains__['shell'],\n 'env': kwargs.get('env'),\n 'use_vt': use_vt,\n 'output_loglevel': output_loglevel,\n 'hide_output': hide_output,\n 'umask': kwargs.get('umask')}\n\n cret = mod_run_check(cmd_kwargs, onlyif, unless, creates)\n if isinstance(cret, dict):\n ret.update(cret)\n return ret\n\n if not kws:\n kws = {}\n result = func(*args, **kws)\n if isinstance(result, dict):\n ret.update(result)\n return ret\n else:\n # result must be JSON serializable else we get an error\n ret['changes'] = {'retval': result}\n ret['result'] = True if result is None else bool(result)\n if isinstance(result, six.string_types):\n ret['comment'] = result\n return ret", "response": "Calls a function in a state system."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nexecutes a cmd function based on a watch call", "response": "def mod_watch(name, **kwargs):\n '''\n Execute a cmd function based on a watch call\n\n .. note::\n This state exists to support special handling of the ``watch``\n :ref:`requisite <requisites>`. It should not be called directly.\n\n Parameters for this function should be set by the state being triggered.\n '''\n if kwargs['sfun'] in ('wait', 'run', 'watch'):\n if kwargs.get('stateful'):\n kwargs.pop('stateful')\n return _reinterpreted_state(run(name, **kwargs))\n return run(name, **kwargs)\n\n elif kwargs['sfun'] == 'wait_script' or kwargs['sfun'] == 'script':\n if kwargs.get('stateful'):\n kwargs.pop('stateful')\n return _reinterpreted_state(script(name, **kwargs))\n return script(name, **kwargs)\n\n elif kwargs['sfun'] == 'wait_call' or kwargs['sfun'] == 'call':\n if kwargs.get('func'):\n func = kwargs.pop('func')\n return call(name, func, **kwargs)\n else:\n return {'name': name,\n 'changes': {},\n 'comment': (\n 'cmd.{0[sfun]} needs a named parameter func'\n ).format(kwargs),\n 'result': False}\n\n return {'name': name,\n 'changes': {},\n 'comment': 'cmd.{0[sfun]} does not work with the watch requisite, '\n 'please use cmd.wait or cmd.wait_script'.format(kwargs),\n 'result': False}"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef check_nova():\n '''\n Check version of novaclient\n '''\n if HAS_NOVA:\n novaclient_ver = _LooseVersion(novaclient.__version__)\n min_ver = _LooseVersion(NOVACLIENT_MINVER)\n if min_ver <= novaclient_ver:\n return HAS_NOVA\n log.debug('Newer novaclient version required. Minimum: %s', NOVACLIENT_MINVER)\n return False", "response": "Check version of novaclient\n Check version of novaclient\n ArcGIS client and return True if version is less than or equal to NOVACLIENT_MINVER"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _get_version_from_url(self, url):\n '''\n Exctract API version from provided URL\n '''\n regex = re.compile(r\"^https?:\\/\\/.*\\/(v[0-9])(\\.[0-9])?(\\/)?$\")\n try:\n ver = regex.match(url)\n if ver.group(1):\n retver = ver.group(1)\n if ver.group(2):\n retver = retver + ver.group(2)\n return retver\n except AttributeError:\n return ''", "response": "Get the API version from a URL."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _discover_ks_version(self, url):\n '''\n Keystone API version discovery\n '''\n result = salt.utils.http.query(url, backend='requests', status=True, decode=True, decode_type='json')\n versions = json.loads(result['body'])\n try:\n links = [ver['links'] for ver in versions['versions']['values'] if ver['status'] == 'stable'][0] \\\n if result['status'] == 300 else versions['version']['links']\n resurl = [link['href'] for link in links if link['rel'] == 'self'][0]\n return self._get_version_from_url(resurl)\n except KeyError as exc:\n raise SaltCloudSystemExit('KeyError: key {0} not found in API response: {1}'.format(exc, versions))", "response": "Discover the version of a Keystone API version from the given URL."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef server_show_libcloud(self, uuid):\n '''\n Make output look like libcloud output for consistency\n '''\n server_info = self.server_show(uuid)\n server = next(six.itervalues(server_info))\n server_name = next(six.iterkeys(server_info))\n if not hasattr(self, 'password'):\n self.password = None\n ret = NovaServer(server_name, server, self.password)\n\n return ret", "response": "Make output look like libcloud output for consistency\n "} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef boot(self, name, flavor_id=0, image_id=0, timeout=300, **kwargs):\n '''\n Boot a cloud server.\n '''\n nt_ks = self.compute_conn\n kwargs['name'] = name\n kwargs['flavor'] = flavor_id\n kwargs['image'] = image_id or None\n ephemeral = kwargs.pop('ephemeral', [])\n block_device = kwargs.pop('block_device', [])\n boot_volume = kwargs.pop('boot_volume', None)\n snapshot = kwargs.pop('snapshot', None)\n swap = kwargs.pop('swap', None)\n kwargs['block_device_mapping_v2'] = _parse_block_device_mapping_v2(\n block_device=block_device, boot_volume=boot_volume, snapshot=snapshot,\n ephemeral=ephemeral, swap=swap\n )\n response = nt_ks.servers.create(**kwargs)\n self.uuid = response.id\n self.password = getattr(response, 'adminPass', None)\n\n start = time.time()\n trycount = 0\n while True:\n trycount += 1\n try:\n return self.server_show_libcloud(self.uuid)\n except Exception as exc:\n log.debug(\n 'Server information not yet available: %s', exc\n )\n time.sleep(1)\n if time.time() - start > timeout:\n log.error('Timed out after %s seconds '\n 'while waiting for data', timeout)\n return False\n\n log.debug(\n 'Retrying server_show() (try %s)', trycount\n )", "response": "Boot a cloud server."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef root_password(self, server_id, password):\n '''\n Change server(uuid's) root password\n '''\n nt_ks = self.compute_conn\n nt_ks.servers.change_password(server_id, password)", "response": "Change root password of a resource"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nfinding a server by its name", "response": "def server_by_name(self, name):\n '''\n Find a server by its name\n '''\n return self.server_show_libcloud(\n self.server_list().get(name, {}).get('id', '')\n )"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _volume_get(self, volume_id):\n '''\n Organize information about a volume from the volume_id\n '''\n if self.volume_conn is None:\n raise SaltCloudSystemExit('No cinder endpoint available')\n nt_ks = self.volume_conn\n volume = nt_ks.volumes.get(volume_id)\n response = {'name': volume.display_name,\n 'size': volume.size,\n 'id': volume.id,\n 'description': volume.display_description,\n 'attachments': volume.attachments,\n 'status': volume.status\n }\n return response", "response": "Get information about a volume"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef volume_list(self, search_opts=None):\n '''\n List all block volumes\n '''\n if self.volume_conn is None:\n raise SaltCloudSystemExit('No cinder endpoint available')\n nt_ks = self.volume_conn\n volumes = nt_ks.volumes.list(search_opts=search_opts)\n response = {}\n for volume in volumes:\n response[volume.display_name] = {\n 'name': volume.display_name,\n 'size': volume.size,\n 'id': volume.id,\n 'description': volume.display_description,\n 'attachments': volume.attachments,\n 'status': volume.status\n }\n return response", "response": "List all block volumes"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncreate a block device", "response": "def volume_create(self, name, size=100, snapshot=None, voltype=None,\n availability_zone=None):\n '''\n Create a block device\n '''\n if self.volume_conn is None:\n raise SaltCloudSystemExit('No cinder endpoint available')\n nt_ks = self.volume_conn\n response = nt_ks.volumes.create(\n size=size,\n display_name=name,\n volume_type=voltype,\n snapshot_id=snapshot,\n availability_zone=availability_zone\n )\n\n return self._volume_get(response.id)"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef volume_delete(self, name):\n '''\n Delete a block device\n '''\n if self.volume_conn is None:\n raise SaltCloudSystemExit('No cinder endpoint available')\n nt_ks = self.volume_conn\n try:\n volume = self.volume_show(name)\n except KeyError as exc:\n raise SaltCloudSystemExit('Unable to find {0} volume: {1}'.format(name, exc))\n if volume['status'] == 'deleted':\n return volume\n response = nt_ks.volumes.delete(volume['id'])\n return volume", "response": "Delete a block device"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef volume_detach(self,\n name,\n timeout=300):\n '''\n Detach a block device\n '''\n try:\n volume = self.volume_show(name)\n except KeyError as exc:\n raise SaltCloudSystemExit('Unable to find {0} volume: {1}'.format(name, exc))\n if not volume['attachments']:\n return True\n response = self.compute_conn.volumes.delete_server_volume(\n volume['attachments'][0]['server_id'],\n volume['attachments'][0]['id']\n )\n trycount = 0\n start = time.time()\n while True:\n trycount += 1\n try:\n response = self._volume_get(volume['id'])\n if response['status'] == 'available':\n return response\n except Exception as exc:\n log.debug('Volume is detaching: %s', name)\n time.sleep(1)\n if time.time() - start > timeout:\n log.error('Timed out after %d seconds '\n 'while waiting for data', timeout)\n return False\n\n log.debug(\n 'Retrying volume_show() (try %d)', trycount\n )", "response": "Detach a block device from the cloud provider"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nattach a block device to a server volume", "response": "def volume_attach(self,\n name,\n server_name,\n device='/dev/xvdb',\n timeout=300):\n '''\n Attach a block device\n '''\n try:\n volume = self.volume_show(name)\n except KeyError as exc:\n raise SaltCloudSystemExit('Unable to find {0} volume: {1}'.format(name, exc))\n server = self.server_by_name(server_name)\n response = self.compute_conn.volumes.create_server_volume(\n server.id,\n volume['id'],\n device=device\n )\n trycount = 0\n start = time.time()\n while True:\n trycount += 1\n try:\n response = self._volume_get(volume['id'])\n if response['status'] == 'in-use':\n return response\n except Exception as exc:\n log.debug('Volume is attaching: %s', name)\n time.sleep(1)\n if time.time() - start > timeout:\n log.error('Timed out after %s seconds '\n 'while waiting for data', timeout)\n return False\n\n log.debug(\n 'Retrying volume_show() (try %s)', trycount\n )"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nsuspending a server from running", "response": "def suspend(self, instance_id):\n '''\n Suspend a server\n '''\n nt_ks = self.compute_conn\n response = nt_ks.servers.suspend(instance_id)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\nresuming a server from running", "response": "def resume(self, instance_id):\n '''\n Resume a server\n '''\n nt_ks = self.compute_conn\n response = nt_ks.servers.resume(instance_id)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef delete(self, instance_id):\n '''\n Delete a server\n '''\n nt_ks = self.compute_conn\n response = nt_ks.servers.delete(instance_id)\n return True", "response": "Delete a server from cache"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a list of available flavors (ova flavor - list", "response": "def flavor_list(self, **kwargs):\n '''\n Return a list of available flavors (nova flavor-list)\n '''\n nt_ks = self.compute_conn\n ret = {}\n for flavor in nt_ks.flavors.list(**kwargs):\n links = {}\n for link in flavor.links:\n links[link['rel']] = link['href']\n ret[flavor.name] = {\n 'disk': flavor.disk,\n 'id': flavor.id,\n 'name': flavor.name,\n 'ram': flavor.ram,\n 'swap': flavor.swap,\n 'vcpus': flavor.vcpus,\n 'links': links,\n }\n if hasattr(flavor, 'rxtx_factor'):\n ret[flavor.name]['rxtx_factor'] = flavor.rxtx_factor\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef flavor_create(self,\n name, # pylint: disable=C0103\n flavor_id=0, # pylint: disable=C0103\n ram=0,\n disk=0,\n vcpus=1,\n is_public=True):\n '''\n Create a flavor\n '''\n nt_ks = self.compute_conn\n nt_ks.flavors.create(\n name=name, flavorid=flavor_id, ram=ram, disk=disk, vcpus=vcpus, is_public=is_public\n )\n return {'name': name,\n 'id': flavor_id,\n 'ram': ram,\n 'disk': disk,\n 'vcpus': vcpus,\n 'is_public': is_public}", "response": "Create a flavor from a user s name."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef flavor_access_list(self, **kwargs):\n '''\n Return a list of project IDs assigned to flavor ID\n '''\n flavor_id = kwargs.get('flavor_id')\n nt_ks = self.compute_conn\n ret = {flavor_id: []}\n flavor_accesses = nt_ks.flavor_access.list(flavor=flavor_id, **kwargs)\n for project in flavor_accesses:\n ret[flavor_id].append(project.tenant_id)\n return ret", "response": "Return a list of project IDs assigned to flavor ID\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nadd a project to the flavor access list", "response": "def flavor_access_add(self, flavor_id, project_id):\n '''\n Add a project to the flavor access list\n '''\n nt_ks = self.compute_conn\n ret = {flavor_id: []}\n flavor_accesses = nt_ks.flavor_access.add_tenant_access(flavor_id, project_id)\n for project in flavor_accesses:\n ret[flavor_id].append(project.tenant_id)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef flavor_access_remove(self, flavor_id, project_id):\n '''\n Remove a project from the flavor access list\n '''\n nt_ks = self.compute_conn\n ret = {flavor_id: []}\n flavor_accesses = nt_ks.flavor_access.remove_tenant_access(flavor_id, project_id)\n for project in flavor_accesses:\n ret[flavor_id].append(project.tenant_id)\n return ret", "response": "Remove a project from the flavor access list"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns a list of all keypairs", "response": "def keypair_list(self):\n '''\n List keypairs\n '''\n nt_ks = self.compute_conn\n ret = {}\n for keypair in nt_ks.keypairs.list():\n ret[keypair.name] = {\n 'name': keypair.name,\n 'fingerprint': keypair.fingerprint,\n 'public_key': keypair.public_key,\n }\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef keypair_add(self, name, pubfile=None, pubkey=None):\n '''\n Add a keypair\n '''\n nt_ks = self.compute_conn\n if pubfile:\n with salt.utils.files.fopen(pubfile, 'r') as fp_:\n pubkey = salt.utils.stringutils.to_unicode(fp_.read())\n if not pubkey:\n return False\n nt_ks.keypairs.create(name, public_key=pubkey)\n ret = {'name': name, 'pubkey': pubkey}\n return ret", "response": "Add a keypair to the keypairs"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nshows image details and metadata", "response": "def image_show(self, image_id):\n '''\n Show image details and metadata\n '''\n nt_ks = self.compute_conn\n image = nt_ks.images.get(image_id)\n links = {}\n for link in image.links:\n links[link['rel']] = link['href']\n ret = {\n 'name': image.name,\n 'id': image.id,\n 'status': image.status,\n 'progress': image.progress,\n 'created': image.created,\n 'updated': image.updated,\n 'metadata': image.metadata,\n 'links': links,\n }\n if hasattr(image, 'minDisk'):\n ret['minDisk'] = image.minDisk\n if hasattr(image, 'minRam'):\n ret['minRam'] = image.minRam\n\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef server_list(self):\n '''\n List servers\n '''\n nt_ks = self.compute_conn\n ret = {}\n for item in nt_ks.servers.list():\n try:\n ret[item.name] = {\n 'id': item.id,\n 'name': item.name,\n 'state': item.status,\n 'accessIPv4': item.accessIPv4,\n 'accessIPv6': item.accessIPv6,\n 'flavor': {'id': item.flavor['id'],\n 'links': item.flavor['links']},\n 'image': {'id': item.image['id'] if item.image else 'Boot From Volume',\n 'links': item.image['links'] if item.image else ''},\n }\n except TypeError:\n pass\n return ret", "response": "List servers in the cluster"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nlists minimal information about servers", "response": "def server_list_min(self):\n '''\n List minimal information about servers\n '''\n nt_ks = self.compute_conn\n ret = {}\n for item in nt_ks.servers.list(detailed=False):\n try:\n ret[item.name] = {\n 'id': item.id,\n 'state': 'Running'\n }\n except TypeError:\n pass\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef server_list_detailed(self):\n '''\n Detailed list of servers\n '''\n nt_ks = self.compute_conn\n ret = {}\n for item in nt_ks.servers.list():\n try:\n ret[item.name] = {\n 'OS-EXT-SRV-ATTR': {},\n 'OS-EXT-STS': {},\n 'accessIPv4': item.accessIPv4,\n 'accessIPv6': item.accessIPv6,\n 'addresses': item.addresses,\n 'created': item.created,\n 'flavor': {'id': item.flavor['id'],\n 'links': item.flavor['links']},\n 'hostId': item.hostId,\n 'id': item.id,\n 'image': {'id': item.image['id'] if item.image else 'Boot From Volume',\n 'links': item.image['links'] if item.image else ''},\n 'key_name': item.key_name,\n 'links': item.links,\n 'metadata': item.metadata,\n 'name': item.name,\n 'state': item.status,\n 'tenant_id': item.tenant_id,\n 'updated': item.updated,\n 'user_id': item.user_id,\n }\n except TypeError:\n continue\n\n ret[item.name]['progress'] = getattr(item, 'progress', '0')\n\n if hasattr(item.__dict__, 'OS-DCF:diskConfig'):\n ret[item.name]['OS-DCF'] = {\n 'diskConfig': item.__dict__['OS-DCF:diskConfig']\n }\n if hasattr(item.__dict__, 'OS-EXT-SRV-ATTR:host'):\n ret[item.name]['OS-EXT-SRV-ATTR']['host'] = \\\n item.__dict__['OS-EXT-SRV-ATTR:host']\n if hasattr(item.__dict__, 'OS-EXT-SRV-ATTR:hypervisor_hostname'):\n ret[item.name]['OS-EXT-SRV-ATTR']['hypervisor_hostname'] = \\\n item.__dict__['OS-EXT-SRV-ATTR:hypervisor_hostname']\n if hasattr(item.__dict__, 'OS-EXT-SRV-ATTR:instance_name'):\n ret[item.name]['OS-EXT-SRV-ATTR']['instance_name'] = \\\n item.__dict__['OS-EXT-SRV-ATTR:instance_name']\n if hasattr(item.__dict__, 'OS-EXT-STS:power_state'):\n ret[item.name]['OS-EXT-STS']['power_state'] = \\\n item.__dict__['OS-EXT-STS:power_state']\n if hasattr(item.__dict__, 'OS-EXT-STS:task_state'):\n ret[item.name]['OS-EXT-STS']['task_state'] = \\\n item.__dict__['OS-EXT-STS:task_state']\n if hasattr(item.__dict__, 'OS-EXT-STS:vm_state'):\n ret[item.name]['OS-EXT-STS']['vm_state'] = \\\n item.__dict__['OS-EXT-STS:vm_state']\n if hasattr(item.__dict__, 'security_groups'):\n ret[item.name]['security_groups'] = \\\n item.__dict__['security_groups']\n return ret", "response": "Return a list of servers in the cloud provider that are not in use"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef server_show(self, server_id):\n '''\n Show details of one server\n '''\n ret = {}\n try:\n servers = self.server_list_detailed()\n except AttributeError:\n raise SaltCloudSystemExit('Corrupt server in server_list_detailed. Remove corrupt servers.')\n for server_name, server in six.iteritems(servers):\n if six.text_type(server['id']) == server_id:\n ret[server_name] = server\n return ret", "response": "Show details of one server"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef secgroup_create(self, name, description):\n '''\n Create a security group\n '''\n nt_ks = self.compute_conn\n nt_ks.security_groups.create(name, description)\n ret = {'name': name, 'description': description}\n return ret", "response": "Create a security group"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef secgroup_delete(self, name):\n '''\n Delete a security group\n '''\n nt_ks = self.compute_conn\n for item in nt_ks.security_groups.list():\n if item.name == name:\n nt_ks.security_groups.delete(item.id)\n return {name: 'Deleted security group: {0}'.format(name)}\n return 'Security group not found: {0}'.format(name)", "response": "Delete a security group"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _item_list(self):\n '''\n List items\n '''\n nt_ks = self.compute_conn\n ret = []\n for item in nt_ks.items.list():\n ret.append(item.__dict__)\n return ret", "response": "Return a list of items"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _network_show(self, name, network_lst):\n '''\n Parse the returned network list\n '''\n for net in network_lst:\n if net.label == name:\n return net.__dict__\n return {}", "response": "Return the network s attributes"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef network_list(self):\n '''\n List extra private networks\n '''\n nt_ks = self.compute_conn\n return [network.__dict__ for network in nt_ks.networks.list()]", "response": "List extra private networks\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating extra private network", "response": "def network_create(self, name, **kwargs):\n '''\n Create extra private network\n '''\n nt_ks = self.compute_conn\n kwargs['label'] = name\n kwargs = self._sanatize_network_params(kwargs)\n net = nt_ks.networks.create(**kwargs)\n return net.__dict__"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ngetting virtual interfaces on slice", "response": "def virtual_interface_list(self, name):\n '''\n Get virtual interfaces on slice\n '''\n nt_ks = self.compute_conn\n nets = nt_ks.virtual_interfaces.list(self._server_uuid_from_name(name))\n return [network.__dict__ for network in nets]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nadding an interfaces to a slice", "response": "def virtual_interface_create(self, name, net_name):\n '''\n Add an interfaces to a slice\n '''\n nt_ks = self.compute_conn\n serverid = self._server_uuid_from_name(name)\n networkid = self.network_show(net_name).get('id', None)\n if networkid is None:\n return {net_name: False}\n nets = nt_ks.virtual_interfaces.create(networkid, serverid)\n return nets"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef floating_ip_pool_list(self):\n '''\n List all floating IP pools\n\n .. versionadded:: 2016.3.0\n '''\n nt_ks = self.compute_conn\n pools = nt_ks.floating_ip_pools.list()\n response = {}\n for pool in pools:\n response[pool.name] = {\n 'name': pool.name,\n }\n return response", "response": "List all floating IP pools"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef floating_ip_list(self):\n '''\n List floating IPs\n\n .. versionadded:: 2016.3.0\n '''\n nt_ks = self.compute_conn\n floating_ips = nt_ks.floating_ips.list()\n response = {}\n for floating_ip in floating_ips:\n response[floating_ip.ip] = {\n 'ip': floating_ip.ip,\n 'fixed_ip': floating_ip.fixed_ip,\n 'id': floating_ip.id,\n 'instance_id': floating_ip.instance_id,\n 'pool': floating_ip.pool\n }\n return response", "response": "List all the floating IPs in the resource store"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nallocate a floating IP and return its info", "response": "def floating_ip_create(self, pool=None):\n '''\n Allocate a floating IP\n\n .. versionadded:: 2016.3.0\n '''\n nt_ks = self.compute_conn\n floating_ip = nt_ks.floating_ips.create(pool)\n response = {\n 'ip': floating_ip.ip,\n 'fixed_ip': floating_ip.fixed_ip,\n 'id': floating_ip.id,\n 'instance_id': floating_ip.instance_id,\n 'pool': floating_ip.pool\n }\n return response"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\ndeletes a floating IP", "response": "def floating_ip_delete(self, floating_ip):\n '''\n De-allocate a floating IP\n\n .. versionadded:: 2016.3.0\n '''\n ip = self.floating_ip_show(floating_ip)\n nt_ks = self.compute_conn\n return nt_ks.floating_ips.delete(ip)"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef floating_ip_associate(self, server_name, floating_ip):\n '''\n Associate floating IP address to server\n\n .. versionadded:: 2016.3.0\n '''\n nt_ks = self.compute_conn\n server_ = self.server_by_name(server_name)\n server = nt_ks.servers.get(server_.__dict__['id'])\n server.add_floating_ip(floating_ip)\n return self.floating_ip_list()[floating_ip]", "response": "Associate a floating IP address to a server"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef floating_ip_disassociate(self, server_name, floating_ip):\n '''\n Disassociate a floating IP from server\n\n .. versionadded:: 2016.3.0\n '''\n nt_ks = self.compute_conn\n server_ = self.server_by_name(server_name)\n server = nt_ks.servers.get(server_.__dict__['id'])\n server.remove_floating_ip(floating_ip)\n return self.floating_ip_list()[floating_ip]", "response": "Disassociate a floating IP from a server"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef dependency_information(include_salt_cloud=False):\n '''\n Report versions of library dependencies.\n '''\n libs = [\n ('Python', None, sys.version.rsplit('\\n')[0].strip()),\n ('Jinja2', 'jinja2', '__version__'),\n ('M2Crypto', 'M2Crypto', 'version'),\n ('msgpack-python', 'msgpack', 'version'),\n ('msgpack-pure', 'msgpack_pure', 'version'),\n ('pycrypto', 'Crypto', '__version__'),\n ('pycryptodome', 'Cryptodome', 'version_info'),\n ('PyYAML', 'yaml', '__version__'),\n ('PyZMQ', 'zmq', '__version__'),\n ('ZMQ', 'zmq', 'zmq_version'),\n ('Mako', 'mako', '__version__'),\n ('Tornado', 'tornado', 'version'),\n ('timelib', 'timelib', 'version'),\n ('dateutil', 'dateutil', '__version__'),\n ('pygit2', 'pygit2', '__version__'),\n ('libgit2', 'pygit2', 'LIBGIT2_VERSION'),\n ('smmap', 'smmap', '__version__'),\n ('cffi', 'cffi', '__version__'),\n ('pycparser', 'pycparser', '__version__'),\n ('gitdb', 'gitdb', '__version__'),\n ('gitpython', 'git', '__version__'),\n ('python-gnupg', 'gnupg', '__version__'),\n ('mysql-python', 'MySQLdb', '__version__'),\n ('cherrypy', 'cherrypy', '__version__'),\n ('docker-py', 'docker', '__version__'),\n ]\n\n if include_salt_cloud:\n libs.append(\n ('Apache Libcloud', 'libcloud', '__version__'),\n )\n\n for name, imp, attr in libs:\n if imp is None:\n yield name, attr\n continue\n try:\n imp = __import__(imp)\n version = getattr(imp, attr)\n if callable(version):\n version = version()\n if isinstance(version, (tuple, list)):\n version = '.'.join(map(str, version))\n yield name, version\n except Exception:\n yield name, None", "response": "Return a generator that yields the version of all libraries and their dependencies."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef system_information():\n '''\n Report system versions.\n '''\n def system_version():\n '''\n Return host system version.\n '''\n lin_ver = linux_distribution()\n mac_ver = platform.mac_ver()\n win_ver = platform.win32_ver()\n\n if lin_ver[0]:\n return ' '.join(lin_ver)\n elif mac_ver[0]:\n if isinstance(mac_ver[1], (tuple, list)) and ''.join(mac_ver[1]):\n return ' '.join([mac_ver[0], '.'.join(mac_ver[1]), mac_ver[2]])\n else:\n return ' '.join([mac_ver[0], mac_ver[2]])\n elif win_ver[0]:\n return ' '.join(win_ver)\n else:\n return ''\n\n if platform.win32_ver()[0]:\n # Get the version and release info based on the Windows Operating\n # System Product Name. As long as Microsoft maintains a similar format\n # this should be future proof\n import win32api # pylint: disable=3rd-party-module-not-gated\n import win32con # pylint: disable=3rd-party-module-not-gated\n\n # Get the product name from the registry\n hkey = win32con.HKEY_LOCAL_MACHINE\n key = 'SOFTWARE\\\\Microsoft\\\\Windows NT\\\\CurrentVersion'\n value_name = 'ProductName'\n reg_handle = win32api.RegOpenKey(hkey, key)\n\n # Returns a tuple of (product_name, value_type)\n product_name, _ = win32api.RegQueryValueEx(reg_handle, value_name)\n\n version = 'Unknown'\n release = ''\n if 'Server' in product_name:\n for item in product_name.split(' '):\n # If it's all digits, then it's version\n if re.match(r'\\d+', item):\n version = item\n # If it starts with R and then numbers, it's the release\n # ie: R2\n if re.match(r'^R\\d+$', item):\n release = item\n release = '{0}Server{1}'.format(version, release)\n else:\n for item in product_name.split(' '):\n # If it's a number, decimal number, Thin or Vista, then it's the\n # version\n if re.match(r'^(\\d+(\\.\\d+)?)|Thin|Vista$', item):\n version = item\n release = version\n\n _, ver, sp, extra = platform.win32_ver()\n version = ' '.join([release, ver, sp, extra])\n else:\n version = system_version()\n release = platform.release()\n\n system = [\n ('system', platform.system()),\n ('dist', ' '.join(linux_distribution(full_distribution_name=False))),\n ('release', release),\n ('machine', platform.machine()),\n ('version', version),\n ('locale', __salt_system_encoding__),\n ]\n\n for name, attr in system:\n yield name, attr\n continue", "response": "Return a list of system versions and release info."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef versions_information(include_salt_cloud=False):\n '''\n Report the versions of dependent software.\n '''\n salt_info = list(salt_information())\n lib_info = list(dependency_information(include_salt_cloud))\n sys_info = list(system_information())\n\n return {'Salt Version': dict(salt_info),\n 'Dependency Versions': dict(lib_info),\n 'System Versions': dict(sys_info)}", "response": "Return a dictionary of version information for the salt and dependent software."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nyield each version properly formatted for console output.", "response": "def versions_report(include_salt_cloud=False):\n '''\n Yield each version properly formatted for console output.\n '''\n ver_info = versions_information(include_salt_cloud)\n\n lib_pad = max(len(name) for name in ver_info['Dependency Versions'])\n sys_pad = max(len(name) for name in ver_info['System Versions'])\n padding = max(lib_pad, sys_pad) + 1\n\n fmt = '{0:>{pad}}: {1}'\n info = []\n for ver_type in ('Salt Version', 'Dependency Versions', 'System Versions'):\n info.append('{0}:'.format(ver_type))\n # List dependencies in alphabetical, case insensitive order\n for name in sorted(ver_info[ver_type], key=lambda x: x.lower()):\n ver = fmt.format(name,\n ver_info[ver_type][name] or 'Not Installed',\n pad=padding)\n info.append(ver)\n info.append(' ')\n\n for line in info:\n yield line"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a string that can be used to uninstall and free internal version of a msi installer.", "response": "def msi_conformant_version():\n '''\n An msi installer uninstalls/replaces a lower \"internal version\" of itself.\n \"internal version\" is ivMAJOR.ivMINOR.ivBUILD with max values 255.255.65535.\n Using the build nr allows continuous integration of the installer.\n \"Display version\" is indipendent and free format: Year.Month.Bugfix as in Salt 2016.11.3.\n Calculation of the internal version fields:\n ivMAJOR = 'short year' (2 digits).\n ivMINOR = 20*(month-1) + Bugfix\n Combine Month and Bugfix to free ivBUILD for the build number\n This limits Bugfix < 20.\n The msi automatically replaces only 19 bugfixes of a month, one must uninstall manually.\n ivBUILD = git commit count (noc)\n noc for tags is 0, representing the final word, translates to the highest build number (65535).\n\n Examples:\n git checkout Display version Internal version Remark\n develop 2016.11.0-742 16.200.742 The develop branch has bugfix 0\n 2016.11 2016.11.2-78 16.202.78\n 2016.11 2016.11.9-88 16.209.88\n 2018.8 2018.3.2-1306 18.42.1306\n v2016.11.0 2016.11.0 16.200.65535 Tags have noc 0\n v2016.11.2 2016.11.2 16.202.65535\n\n '''\n short_year = int(six.text_type(__saltstack_version__.major)[2:])\n month = __saltstack_version__.minor\n bugfix = __saltstack_version__.bugfix\n if bugfix > 19:\n bugfix = 19\n noc = __saltstack_version__.noc\n if noc == 0:\n noc = 65535\n return '{}.{}.{}'.format(short_year, 20*(month-1)+bugfix, noc)"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ngenerating a new version of the winrepo. p file of the repository.", "response": "def genrepo(name, force=False, allow_empty=False):\n '''\n Refresh the winrepo.p file of the repository (salt-run winrepo.genrepo)\n\n If ``force`` is ``True`` no checks will be made and the repository will be\n generated if ``allow_empty`` is ``True`` then the state will not return an\n error if there are 0 packages,\n\n .. note::\n\n This state only loads on minions that have the ``roles: salt-master``\n grain set.\n\n Example:\n\n .. code-block:: yaml\n\n winrepo:\n winrepo.genrepo\n '''\n\n ret = {'name': name,\n 'result': True,\n 'changes': {},\n 'comment': ''}\n\n master_config = salt.config.master_config(\n os.path.join(salt.syspaths.CONFIG_DIR, 'master')\n )\n\n winrepo_dir = master_config['winrepo_dir']\n winrepo_cachefile = master_config['winrepo_cachefile']\n\n # We're actually looking for the full path to the cachefile here, so\n # prepend the winrepo_dir\n winrepo_cachefile = os.path.join(winrepo_dir, winrepo_cachefile)\n\n # Check if the winrepo directory exists\n # if not search for a file with a newer mtime than the winrepo_cachefile file\n execute = False\n if not force:\n if not os.path.exists(winrepo_dir):\n ret['result'] = False\n ret['comment'] = '{0} is missing'.format(winrepo_dir)\n return ret\n elif not os.path.exists(winrepo_cachefile):\n execute = True\n ret['comment'] = '{0} is missing'.format(winrepo_cachefile)\n else:\n winrepo_cachefile_mtime = os.stat(winrepo_cachefile)[stat.ST_MTIME]\n for root, dirs, files in salt.utils.path.os_walk(winrepo_dir):\n for name in itertools.chain(files, dirs):\n full_path = os.path.join(root, name)\n if os.stat(full_path)[stat.ST_MTIME] > winrepo_cachefile_mtime:\n ret['comment'] = 'mtime({0}) < mtime({1})'.format(winrepo_cachefile, full_path)\n execute = True\n break\n\n if __opts__['test']:\n ret['result'] = None\n return ret\n\n if not execute and not force:\n return ret\n\n runner = salt.runner.RunnerClient(master_config)\n runner_ret = runner.cmd('winrepo.genrepo', [])\n ret['changes'] = {'winrepo': runner_ret}\n if isinstance(runner_ret, dict) and runner_ret == {} and not allow_empty:\n os.remove(winrepo_cachefile)\n ret['result'] = False\n ret['comment'] = 'winrepo.genrepo returned empty'\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nprocesses the configured beacons and return a list of the names of the beacons that are processed", "response": "def process(self, config, grains):\n '''\n Process the configured beacons\n\n The config must be a list and looks like this in yaml\n\n .. code_block:: yaml\n beacons:\n inotify:\n - files:\n - /etc/fstab: {}\n - /var/cache/foo: {}\n '''\n ret = []\n b_config = copy.deepcopy(config)\n if 'enabled' in b_config and not b_config['enabled']:\n return\n for mod in config:\n if mod == 'enabled':\n continue\n\n # Convert beacons that are lists to a dict to make processing easier\n current_beacon_config = None\n if isinstance(config[mod], list):\n current_beacon_config = {}\n list(map(current_beacon_config.update, config[mod]))\n elif isinstance(config[mod], dict):\n current_beacon_config = config[mod]\n\n if 'enabled' in current_beacon_config:\n if not current_beacon_config['enabled']:\n log.trace('Beacon %s disabled', mod)\n continue\n else:\n # remove 'enabled' item before processing the beacon\n if isinstance(config[mod], dict):\n del config[mod]['enabled']\n else:\n self._remove_list_item(config[mod], 'enabled')\n\n log.trace('Beacon processing: %s', mod)\n beacon_name = None\n if self._determine_beacon_config(current_beacon_config, 'beacon_module'):\n beacon_name = current_beacon_config['beacon_module']\n else:\n beacon_name = mod\n fun_str = '{0}.beacon'.format(beacon_name)\n validate_str = '{0}.validate'.format(beacon_name)\n if fun_str in self.beacons:\n runonce = self._determine_beacon_config(current_beacon_config, 'run_once')\n interval = self._determine_beacon_config(current_beacon_config, 'interval')\n if interval:\n b_config = self._trim_config(b_config, mod, 'interval')\n if not self._process_interval(mod, interval):\n log.trace('Skipping beacon %s. Interval not reached.', mod)\n continue\n if self._determine_beacon_config(current_beacon_config, 'disable_during_state_run'):\n log.trace('Evaluting if beacon %s should be skipped due to a state run.', mod)\n b_config = self._trim_config(b_config, mod, 'disable_during_state_run')\n is_running = False\n running_jobs = salt.utils.minion.running(self.opts)\n for job in running_jobs:\n if re.match('state.*', job['fun']):\n is_running = True\n if is_running:\n close_str = '{0}.close'.format(beacon_name)\n if close_str in self.beacons:\n log.info('Closing beacon %s. State run in progress.', mod)\n self.beacons[close_str](b_config[mod])\n else:\n log.info('Skipping beacon %s. State run in progress.', mod)\n continue\n # Update __grains__ on the beacon\n self.beacons[fun_str].__globals__['__grains__'] = grains\n\n # Run the validate function if it's available,\n # otherwise there is a warning about it being missing\n if validate_str in self.beacons:\n valid, vcomment = self.beacons[validate_str](b_config[mod])\n\n if not valid:\n log.info('Beacon %s configuration invalid, '\n 'not running.\\n%s', mod, vcomment)\n continue\n\n # Fire the beacon!\n raw = self.beacons[fun_str](b_config[mod])\n for data in raw:\n tag = 'salt/beacon/{0}/{1}/'.format(self.opts['id'], mod)\n if 'tag' in data:\n tag += data.pop('tag')\n if 'id' not in data:\n data['id'] = self.opts['id']\n ret.append({'tag': tag,\n 'data': data,\n 'beacon_name': beacon_name})\n if runonce:\n self.disable_beacon(mod)\n else:\n log.warning('Unable to process beacon %s', mod)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ntaking a beacon configuration and strip out the interval bits", "response": "def _trim_config(self, b_config, mod, key):\n '''\n Take a beacon configuration and strip out the interval bits\n '''\n if isinstance(b_config[mod], list):\n self._remove_list_item(b_config[mod], key)\n elif isinstance(b_config[mod], dict):\n b_config[mod].pop(key)\n return b_config"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _determine_beacon_config(self, current_beacon_config, key):\n '''\n Process a beacon configuration to determine its interval\n '''\n\n interval = False\n if isinstance(current_beacon_config, dict):\n interval = current_beacon_config.get(key, False)\n\n return interval", "response": "Process a beacon configuration to determine its interval\n "} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef _process_interval(self, mod, interval):\n '''\n Process beacons with intervals\n Return True if a beacon should be run on this loop\n '''\n log.trace('Processing interval %s for beacon mod %s', interval, mod)\n loop_interval = self.opts['loop_interval']\n if mod in self.interval_map:\n log.trace('Processing interval in map')\n counter = self.interval_map[mod]\n log.trace('Interval counter: %s', counter)\n if counter * loop_interval >= interval:\n self.interval_map[mod] = 1\n return True\n else:\n self.interval_map[mod] += 1\n else:\n log.trace('Interval process inserting mod: %s', mod)\n self.interval_map[mod] = 1\n return False", "response": "Process beacons with intervals\n Return True if a beacon should be run on this loop otherwise Return False"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the index of a labeled config item in the beacon config", "response": "def _get_index(self, beacon_config, label):\n '''\n Return the index of a labeled config item in the beacon config, -1 if the index is not found\n '''\n\n indexes = [index for index, item in enumerate(beacon_config) if label in item]\n if not indexes:\n return -1\n else:\n return indexes[0]"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nremoves an item from a beacon config list", "response": "def _remove_list_item(self, beacon_config, label):\n '''\n Remove an item from a beacon config list\n '''\n\n index = self._get_index(beacon_config, label)\n del beacon_config[index]"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nupdating whether an individual beacon is enabled", "response": "def _update_enabled(self, name, enabled_value):\n '''\n Update whether an individual beacon is enabled\n '''\n\n if isinstance(self.opts['beacons'][name], dict):\n # Backwards compatibility\n self.opts['beacons'][name]['enabled'] = enabled_value\n else:\n enabled_index = self._get_index(self.opts['beacons'][name], 'enabled')\n if enabled_index >= 0:\n self.opts['beacons'][name][enabled_index]['enabled'] = enabled_value\n else:\n self.opts['beacons'][name].append({'enabled': enabled_value})"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_beacons(self,\n include_opts=True,\n include_pillar=True):\n '''\n Return the beacons data structure\n '''\n beacons = {}\n if include_pillar:\n pillar_beacons = self.opts.get('pillar', {}).get('beacons', {})\n if not isinstance(pillar_beacons, dict):\n raise ValueError('Beacons must be of type dict.')\n beacons.update(pillar_beacons)\n if include_opts:\n opts_beacons = self.opts.get('beacons', {})\n if not isinstance(opts_beacons, dict):\n raise ValueError('Beacons must be of type dict.')\n beacons.update(opts_beacons)\n return beacons", "response": "Return the beacons data structure"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_beacons(self,\n include_pillar=True,\n include_opts=True):\n '''\n List the beacon items\n\n include_pillar: Whether to include beacons that are\n configured in pillar, default is True.\n\n include_opts: Whether to include beacons that are\n configured in opts, default is True.\n '''\n beacons = self._get_beacons(include_pillar, include_opts)\n\n # Fire the complete event back along with the list of beacons\n evt = salt.utils.event.get_event('minion', opts=self.opts)\n evt.fire_event({'complete': True, 'beacons': beacons},\n tag='/salt/minion/minion_beacons_list_complete')\n\n return True", "response": "List the beacons in the minion"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_available_beacons(self):\n '''\n List the available beacons\n '''\n _beacons = ['{0}'.format(_beacon.replace('.beacon', ''))\n for _beacon in self.beacons if '.beacon' in _beacon]\n\n # Fire the complete event back along with the list of beacons\n evt = salt.utils.event.get_event('minion', opts=self.opts)\n evt.fire_event({'complete': True, 'beacons': _beacons},\n tag='/salt/minion/minion_beacons_list_available_complete')\n\n return True", "response": "List the available beacons"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn available beacon functions", "response": "def validate_beacon(self, name, beacon_data):\n '''\n Return available beacon functions\n '''\n validate_str = '{}.validate'.format(name)\n # Run the validate function if it's available,\n # otherwise there is a warning about it being missing\n if validate_str in self.beacons:\n if 'enabled' in beacon_data:\n del beacon_data['enabled']\n valid, vcomment = self.beacons[validate_str](beacon_data)\n else:\n vcomment = 'Beacon {0} does not have a validate' \\\n ' function, skipping validation.'.format(name)\n valid = True\n\n # Fire the complete event back along with the list of beacons\n evt = salt.utils.event.get_event('minion', opts=self.opts)\n evt.fire_event({'complete': True,\n 'vcomment': vcomment,\n 'valid': valid},\n tag='/salt/minion/minion_beacon_validation_complete')\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef add_beacon(self, name, beacon_data):\n '''\n Add a beacon item\n '''\n\n data = {}\n data[name] = beacon_data\n\n if name in self._get_beacons(include_opts=False):\n comment = 'Cannot update beacon item {0}, ' \\\n 'because it is configured in pillar.'.format(name)\n complete = False\n else:\n if name in self.opts['beacons']:\n comment = 'Updating settings for beacon ' \\\n 'item: {0}'.format(name)\n else:\n comment = 'Added new beacon item: {0}'.format(name)\n complete = True\n self.opts['beacons'].update(data)\n\n # Fire the complete event back along with updated list of beacons\n evt = salt.utils.event.get_event('minion', opts=self.opts)\n evt.fire_event({'complete': complete, 'comment': comment,\n 'beacons': self.opts['beacons']},\n tag='/salt/minion/minion_beacon_add_complete')\n\n return True", "response": "Add a beacon item to the beacon list"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\ndelete a beacon item", "response": "def delete_beacon(self, name):\n '''\n Delete a beacon item\n '''\n\n if name in self._get_beacons(include_opts=False):\n comment = 'Cannot delete beacon item {0}, ' \\\n 'it is configured in pillar.'.format(name)\n complete = False\n else:\n if name in self.opts['beacons']:\n del self.opts['beacons'][name]\n comment = 'Deleting beacon item: {0}'.format(name)\n else:\n comment = 'Beacon item {0} not found.'.format(name)\n complete = True\n\n # Fire the complete event back along with updated list of beacons\n evt = salt.utils.event.get_event('minion', opts=self.opts)\n evt.fire_event({'complete': complete, 'comment': comment,\n 'beacons': self.opts['beacons']},\n tag='/salt/minion/minion_beacon_delete_complete')\n\n return True"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef enable_beacons(self):\n '''\n Enable beacons\n '''\n\n self.opts['beacons']['enabled'] = True\n\n # Fire the complete event back along with updated list of beacons\n evt = salt.utils.event.get_event('minion', opts=self.opts)\n evt.fire_event({'complete': True, 'beacons': self.opts['beacons']},\n tag='/salt/minion/minion_beacons_enabled_complete')\n\n return True", "response": "Enable beacons on the master"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef disable_beacons(self):\n '''\n Enable beacons\n '''\n\n self.opts['beacons']['enabled'] = False\n\n # Fire the complete event back along with updated list of beacons\n evt = salt.utils.event.get_event('minion', opts=self.opts)\n evt.fire_event({'complete': True, 'beacons': self.opts['beacons']},\n tag='/salt/minion/minion_beacons_disabled_complete')\n\n return True", "response": "Disable the beacons on this minion"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef install(pkg, target='LocalSystem', store=False, allow_untrusted=False):\n '''\n Install a pkg file\n\n Args:\n pkg (str): The package to install\n target (str): The target in which to install the package to\n store (bool): Should the package be installed as if it was from the\n store?\n allow_untrusted (bool): Allow the installation of untrusted packages?\n\n Returns:\n dict: A dictionary containing the results of the installation\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' macpackage.install test.pkg\n '''\n if '*.' not in pkg:\n # If we use wildcards, we cannot use quotes\n pkg = _quote(pkg)\n\n target = _quote(target)\n\n cmd = 'installer -pkg {0} -target {1}'.format(pkg, target)\n if store:\n cmd += ' -store'\n if allow_untrusted:\n cmd += ' -allowUntrusted'\n\n # We can only use wildcards in python_shell which is\n # sent by the macpackage state\n python_shell = False\n if '*.' in cmd:\n python_shell = True\n\n return __salt__['cmd.run_all'](cmd, python_shell=python_shell)", "response": "Install a pkg file to the specified target"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\ninstall an app file by moving it into the specified Applications directory", "response": "def install_app(app, target='/Applications/'):\n '''\n Install an app file by moving it into the specified Applications directory\n\n Args:\n app (str): The location of the .app file\n target (str): The target in which to install the package to\n Default is ''/Applications/''\n\n Returns:\n str: The results of the rsync command\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' macpackage.install_app /tmp/tmp.app /Applications/\n '''\n\n if target[-4:] != '.app':\n if app[-1:] == '/':\n base_app = os.path.basename(app[:-1])\n else:\n base_app = os.path.basename(app)\n\n target = os.path.join(target, base_app)\n\n if not app[-1] == '/':\n app += '/'\n\n cmd = 'rsync -a --delete \"{0}\" \"{1}\"'.format(app, target)\n return __salt__['cmd.run'](cmd)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef mount(dmg):\n '''\n Attempt to mount a dmg file to a temporary location and return the\n location of the pkg file inside\n\n Args:\n dmg (str): The location of the dmg file to mount\n\n Returns:\n tuple: Tuple containing the results of the command along with the mount\n point\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' macpackage.mount /tmp/software.dmg\n '''\n\n temp_dir = __salt__['temp.dir'](prefix='dmg-')\n\n cmd = 'hdiutil attach -readonly -nobrowse -mountpoint {0} \"{1}\"'.format(temp_dir, dmg)\n\n return __salt__['cmd.run'](cmd), temp_dir", "response": "Mount a dmg file to a temporary location and return the location of the pkg file inside the\n "} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nget the package ID from a. pkg file", "response": "def get_pkg_id(pkg):\n '''\n Attempt to get the package ID from a .pkg file\n\n Args:\n pkg (str): The location of the pkg file\n\n Returns:\n list: List of all of the package IDs\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' macpackage.get_pkg_id /tmp/test.pkg\n '''\n pkg = _quote(pkg)\n package_ids = []\n\n # Create temp directory\n temp_dir = __salt__['temp.dir'](prefix='pkg-')\n\n try:\n # List all of the PackageInfo files\n cmd = 'xar -t -f {0} | grep PackageInfo'.format(pkg)\n out = __salt__['cmd.run'](cmd, python_shell=True, output_loglevel='quiet')\n files = out.split('\\n')\n\n if 'Error opening' not in out:\n # Extract the PackageInfo files\n cmd = 'xar -x -f {0} {1}'.format(pkg, ' '.join(files))\n __salt__['cmd.run'](cmd, cwd=temp_dir, output_loglevel='quiet')\n\n # Find our identifiers\n for f in files:\n i = _get_pkg_id_from_pkginfo(os.path.join(temp_dir, f))\n if i:\n package_ids.extend(i)\n else:\n package_ids = _get_pkg_id_dir(pkg)\n\n finally:\n # Clean up\n __salt__['file.remove'](temp_dir)\n\n return package_ids"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets the package IDs from a mounted. mpkg file", "response": "def get_mpkg_ids(mpkg):\n '''\n Attempt to get the package IDs from a mounted .mpkg file\n\n Args:\n mpkg (str): The location of the mounted mpkg file\n\n Returns:\n list: List of package IDs\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' macpackage.get_mpkg_ids /dev/disk2\n '''\n mpkg = _quote(mpkg)\n package_infos = []\n base_path = os.path.dirname(mpkg)\n\n # List all of the .pkg files\n cmd = 'find {0} -name *.pkg'.format(base_path)\n out = __salt__['cmd.run'](cmd, python_shell=True)\n\n pkg_files = out.split('\\n')\n for p in pkg_files:\n package_infos.extend(get_pkg_id(p))\n\n return package_infos"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef refresh_db(**kwargs):\n '''\n Updates the package list\n\n - ``True``: Database updated successfully\n - ``False``: Problem updating database\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.refresh_db\n '''\n ret = {}\n cmd = ['apk', 'update']\n call = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n if call['retcode'] == 0:\n errors = []\n ret = True\n else:\n errors = [call['stdout']]\n ret = False\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": "Updates the database with the current version of the package list"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef list_pkgs(versions_as_list=False, **kwargs):\n '''\n List the packages currently installed in a dict::\n\n {'<package_name>': '<version>'}\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.list_pkgs\n salt '*' pkg.list_pkgs versions_as_list=True\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 cmd = ['apk', 'info', '-v']\n ret = {}\n out = __salt__['cmd.run'](cmd, output_loglevel='trace', python_shell=False)\n for line in salt.utils.itertools.split(out, '\\n'):\n pkg_version = '-'.join(line.split('-')[-2:])\n pkg_name = '-'.join(line.split('-')[:-2])\n __salt__['pkg_resource.add_pkg'](ret, pkg_name, pkg_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", "response": "List the packages currently installed in a dict"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef install(name=None,\n refresh=False,\n pkgs=None,\n sources=None,\n **kwargs):\n '''\n Install the passed package, add refresh=True to update the apk database.\n\n name\n The name of the package to be installed. Note that this parameter is\n ignored if either \"pkgs\" or \"sources\" is passed. Additionally, please\n note that this option can only be used to install packages from a\n software repository. To install a package file manually, use the\n \"sources\" option.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install <package name>\n\n refresh\n Whether or not to refresh the package database before installing.\n\n\n Multiple Package Installation Options:\n\n pkgs\n A list of packages to install from a software repository. Must be\n passed as a python list.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install pkgs='[\"foo\", \"bar\"]'\n\n sources\n A list of IPK packages to install. Must be passed as a list of dicts,\n with the keys being package names, and the values being the source URI\n or local path to the package. Dependencies are automatically resolved\n and marked as auto-installed.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.install sources='[{\"foo\": \"salt://foo.deb\"},{\"bar\": \"salt://bar.deb\"}]'\n\n install_recommends\n Whether to install the packages marked as recommended. Default is True.\n\n Returns a dict containing the new package names and versions::\n\n {'<package>': {'old': '<old-version>',\n 'new': '<new-version>'}}\n '''\n refreshdb = salt.utils.data.is_true(refresh)\n pkg_to_install = []\n\n old = list_pkgs()\n\n if name and not (pkgs or sources):\n if ',' in name:\n pkg_to_install = name.split(',')\n else:\n pkg_to_install = [name]\n\n if pkgs:\n # We don't support installing specific version for now\n # so transform the dict in list ignoring version provided\n pkgs = [\n next(iter(p)) for p in pkgs\n if isinstance(p, dict)\n ]\n pkg_to_install.extend(pkgs)\n\n if not pkg_to_install:\n return {}\n\n if refreshdb:\n refresh_db()\n\n cmd = ['apk', 'add']\n\n # Switch in update mode if a package is already installed\n for _pkg in pkg_to_install:\n if old.get(_pkg):\n cmd.append('-u')\n break\n\n cmd.extend(pkg_to_install)\n\n out = __salt__['cmd.run_all'](\n cmd,\n output_loglevel='trace',\n python_shell=False\n )\n\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": "Installs the passed IPK package file from the software repository."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nremoves packages from the available packages.", "response": "def remove(name=None, pkgs=None, purge=False, **kwargs): # pylint: disable=unused-argument\n '''\n Remove packages using ``apk del``.\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 Returns a dict containing the changes.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' pkg.remove <package name>\n salt '*' pkg.remove <package1>,<package2>,<package3>\n salt '*' pkg.remove pkgs='[\"foo\", \"bar\"]'\n '''\n old = list_pkgs()\n pkg_to_remove = []\n\n if name:\n if ',' in name:\n pkg_to_remove = name.split(',')\n else:\n pkg_to_remove = [name]\n\n if pkgs:\n pkg_to_remove.extend(pkgs)\n\n if not pkg_to_remove:\n return {}\n\n if purge:\n cmd = ['apk', 'del', '--purge']\n else:\n cmd = ['apk', 'del']\n\n cmd.extend(pkg_to_remove)\n\n out = __salt__['cmd.run_all'](\n cmd,\n output_loglevel='trace',\n python_shell=False\n )\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"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef upgrade(name=None, pkgs=None, refresh=True, **kwargs):\n '''\n Upgrades all packages via ``apk upgrade`` or a specific package if name or\n pkgs is specified. Name is ignored if pkgs is specified\n\n Returns a dict containing the changes.\n\n {'<package>': {'old': '<old-version>',\n 'new': '<new-version>'}}\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 if salt.utils.data.is_true(refresh):\n refresh_db()\n\n old = list_pkgs()\n\n pkg_to_upgrade = []\n\n if name and not pkgs:\n if ',' in name:\n pkg_to_upgrade = name.split(',')\n else:\n pkg_to_upgrade = [name]\n\n if pkgs:\n pkg_to_upgrade.extend(pkgs)\n\n if pkg_to_upgrade:\n cmd = ['apk', 'add', '-u']\n cmd.extend(pkg_to_upgrade)\n else:\n cmd = ['apk', 'upgrade']\n\n call = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False,\n redirect_stderr=True)\n\n if call['retcode'] != 0:\n ret['result'] = False\n if call['stdout']:\n ret['comment'] = call['stdout']\n\n __context__.pop('pkg.list_pkgs', None)\n new = list_pkgs()\n ret['changes'] = salt.utils.data.compare_dicts(old, new)\n\n return ret", "response": "Upgrade all packages via apk upgrade or a specific package"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn a dict of all files in a set of packages grouped by package.", "response": "def file_dict(*packages, **kwargs):\n '''\n List the files that belong to a package, grouped by package. Not\n specifying any packages will return a list of _every_ file on the system's\n package database (not generally recommended).\n\n CLI Examples:\n\n .. code-block:: bash\n\n salt '*' pkg.file_list httpd\n salt '*' pkg.file_list httpd postfix\n salt '*' pkg.file_list\n '''\n errors = []\n ret = {}\n cmd_files = ['apk', 'info', '-L']\n\n if not packages:\n return 'Package name should be provided'\n\n for package in packages:\n files = []\n cmd = cmd_files[:]\n cmd.append(package)\n out = __salt__['cmd.run_all'](cmd,\n output_loglevel='trace',\n python_shell=False)\n for line in out['stdout'].splitlines():\n if line.endswith('contains:'):\n continue\n else:\n files.append(line)\n if files:\n ret[package] = files\n\n return {'errors': errors, 'packages': ret}"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef owner(*paths, **kwargs):\n '''\n Return the name of the package that owns the file. Multiple file paths can\n be passed. Like :mod:`pkg.version <salt.modules.apk.version`, if a single\n path is passed, a string will be returned, and if multiple paths are passed,\n a dictionary of file/package name pairs will be returned.\n\n If the file is not owned by a package, or is not present on the minion,\n then an empty string will be returned for that path.\n\n CLI Example:\n\n salt '*' pkg.owns /usr/bin/apachectl\n salt '*' pkg.owns /usr/bin/apachectl /usr/bin/basename\n '''\n if not paths:\n return 'You must provide a path'\n\n ret = {}\n cmd_search = ['apk', 'info', '-W']\n for path in paths:\n cmd = cmd_search[:]\n cmd.append(path)\n output = __salt__['cmd.run_stdout'](cmd,\n output_loglevel='trace',\n python_shell=False)\n if output:\n if 'ERROR:' in output:\n ret[path] = 'Could not find owner package'\n else:\n ret[path] = output.split('by ')[1].strip()\n else:\n ret[path] = 'Error running {0}'.format(cmd)\n\n return ret", "response": "Return the name of the package that owns the file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef options_present(name, sections=None, separator='=', strict=False):\n '''\n .. code-block:: yaml\n\n /home/saltminion/api-paste.ini:\n ini.options_present:\n - separator: '='\n - strict: True\n - sections:\n test:\n testkey: 'testval'\n secondoption: 'secondvalue'\n test1:\n testkey1: 'testval121'\n\n options present in file and not specified in sections\n dict will be untouched, unless `strict: True` flag is\n used\n\n changes dict will contain the list of changes made\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'No anomaly detected'\n }\n if __opts__['test']:\n ret['comment'] = ''\n # pylint: disable=too-many-nested-blocks\n try:\n changes = {}\n if sections:\n options = {}\n for sname, sbody in sections.items():\n if not isinstance(sbody, (dict, OrderedDict)):\n options.update({sname: sbody})\n cur_ini = __salt__['ini.get_ini'](name, separator)\n original_top_level_opts = {}\n original_sections = {}\n for key, val in cur_ini.items():\n if isinstance(val, (dict, OrderedDict)):\n original_sections.update({key: val})\n else:\n original_top_level_opts.update({key: val})\n if __opts__['test']:\n for option in options:\n if option in original_top_level_opts:\n if six.text_type(original_top_level_opts[option]) == six.text_type(options[option]):\n ret['comment'] += 'Unchanged key {0}.\\n'.format(option)\n else:\n ret['comment'] += 'Changed key {0}.\\n'.format(option)\n ret['result'] = None\n else:\n ret['comment'] += 'Changed key {0}.\\n'.format(option)\n ret['result'] = None\n else:\n options_updated = __salt__['ini.set_option'](name, options, separator)\n changes.update(options_updated)\n if strict:\n for opt_to_remove in set(original_top_level_opts).difference(options):\n if __opts__['test']:\n ret['comment'] += 'Removed key {0}.\\n'.format(opt_to_remove)\n ret['result'] = None\n else:\n __salt__['ini.remove_option'](name, None, opt_to_remove, separator)\n changes.update({opt_to_remove: {'before': original_top_level_opts[opt_to_remove],\n 'after': None}})\n for section_name, section_body in [(sname, sbody) for sname, sbody in sections.items()\n if isinstance(sbody, (dict, OrderedDict))]:\n section_descr = ' in section ' + section_name if section_name else ''\n changes[section_name] = {}\n if strict:\n original = cur_ini.get(section_name, {})\n for key_to_remove in set(original.keys()).difference(section_body.keys()):\n orig_value = original_sections.get(section_name, {}).get(key_to_remove, '#-#-')\n if __opts__['test']:\n ret['comment'] += 'Deleted key {0}{1}.\\n'.format(key_to_remove, section_descr)\n ret['result'] = None\n else:\n __salt__['ini.remove_option'](name, section_name, key_to_remove, separator)\n changes[section_name].update({key_to_remove: ''})\n changes[section_name].update({key_to_remove: {'before': orig_value,\n 'after': None}})\n if __opts__['test']:\n for option in section_body:\n if six.text_type(section_body[option]) == \\\n six.text_type(original_sections.get(section_name, {}).get(option, '#-#-')):\n ret['comment'] += 'Unchanged key {0}{1}.\\n'.format(option, section_descr)\n else:\n ret['comment'] += 'Changed key {0}{1}.\\n'.format(option, section_descr)\n ret['result'] = None\n else:\n options_updated = __salt__['ini.set_option'](name, {section_name: section_body}, separator)\n if options_updated:\n changes[section_name].update(options_updated[section_name])\n if not changes[section_name]:\n del changes[section_name]\n else:\n if not __opts__['test']:\n changes = __salt__['ini.set_option'](name, sections, separator)\n except (IOError, KeyError) as err:\n ret['comment'] = \"{0}\".format(err)\n ret['result'] = False\n return ret\n if 'error' in changes:\n ret['result'] = False\n ret['comment'] = 'Errors encountered. {0}'.format(changes['error'])\n ret['changes'] = {}\n else:\n for ciname, body in changes.items():\n if body:\n ret['comment'] = 'Changes take effect'\n ret['changes'].update({ciname: changes[ciname]})\n return ret", "response": "Ensures that the options present in file are present in sections and if strict is True the options are not present in file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef options_absent(name, sections=None, separator='='):\n '''\n .. code-block:: yaml\n\n /home/saltminion/api-paste.ini:\n ini.options_absent:\n - separator: '='\n - sections:\n test:\n - testkey\n - secondoption\n test1:\n - testkey1\n\n options present in file and not specified in sections\n dict will be untouched\n\n changes dict will contain the list of changes made\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'No anomaly detected'\n }\n if __opts__['test']:\n ret['result'] = True\n ret['comment'] = ''\n for section in sections or {}:\n section_name = ' in section ' + section if section else ''\n try:\n cur_section = __salt__['ini.get_section'](name, section, separator)\n except IOError as err:\n ret['comment'] = \"{0}\".format(err)\n ret['result'] = False\n return ret\n except AttributeError:\n cur_section = section\n if isinstance(sections[section], (dict, OrderedDict)):\n for key in sections[section]:\n cur_value = cur_section.get(key)\n if not cur_value:\n ret['comment'] += 'Key {0}{1} does not exist.\\n'.format(key, section_name)\n continue\n ret['comment'] += 'Deleted key {0}{1}.\\n'.format(key, section_name)\n ret['result'] = None\n else:\n option = section\n if not __salt__['ini.get_option'](name, None, option, separator):\n ret['comment'] += 'Key {0} does not exist.\\n'.format(option)\n continue\n ret['comment'] += 'Deleted key {0}.\\n'.format(option)\n ret['result'] = None\n\n if ret['comment'] == '':\n ret['comment'] = 'No changes detected.'\n return ret\n sections = sections or {}\n for section, keys in six.iteritems(sections):\n for key in keys:\n try:\n current_value = __salt__['ini.remove_option'](name, section, key, separator)\n except IOError as err:\n ret['comment'] = \"{0}\".format(err)\n ret['result'] = False\n return ret\n if not current_value:\n continue\n if section not in ret['changes']:\n ret['changes'].update({section: {}})\n ret['changes'][section].update({key: current_value})\n if not isinstance(sections[section], (dict, OrderedDict)):\n ret['changes'].update({section: current_value})\n # break\n ret['comment'] = 'Changes take effect'\n return ret", "response": "Ensures that the specified option is absent in the specified section."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef sections_present(name, sections=None, separator='='):\n '''\n .. code-block:: yaml\n\n /home/saltminion/api-paste.ini:\n ini.sections_present:\n - separator: '='\n - sections:\n - section_one\n - section_two\n\n This will only create empty sections. To also create options, use\n options_present state\n\n options present in file and not specified in sections will be deleted\n changes dict will contain the sections that changed\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'No anomaly detected'\n }\n if __opts__['test']:\n ret['result'] = True\n ret['comment'] = ''\n try:\n cur_ini = __salt__['ini.get_ini'](name, separator)\n except IOError as err:\n ret['result'] = False\n ret['comment'] = \"{0}\".format(err)\n return ret\n for section in sections or {}:\n if section in cur_ini:\n ret['comment'] += 'Section unchanged {0}.\\n'.format(section)\n continue\n else:\n ret['comment'] += 'Created new section {0}.\\n'.format(section)\n ret['result'] = None\n if ret['comment'] == '':\n ret['comment'] = 'No changes detected.'\n return ret\n section_to_update = {}\n for section_name in sections or []:\n section_to_update.update({section_name: {}})\n try:\n changes = __salt__['ini.set_option'](name, section_to_update, separator)\n except IOError as err:\n ret['result'] = False\n ret['comment'] = \"{0}\".format(err)\n return ret\n if 'error' in changes:\n ret['result'] = False\n ret['changes'] = 'Errors encountered {0}'.format(changes['error'])\n return ret\n ret['changes'] = changes\n ret['comment'] = 'Changes take effect'\n return ret", "response": "Ensure that the section is present in the ini file."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef sections_absent(name, sections=None, separator='='):\n '''\n .. code-block:: yaml\n\n /home/saltminion/api-paste.ini:\n ini.sections_absent:\n - separator: '='\n - sections:\n - test\n - test1\n\n options present in file and not specified in sections will be deleted\n changes dict will contain the sections that changed\n '''\n ret = {'name': name,\n 'changes': {},\n 'result': True,\n 'comment': 'No anomaly detected'\n }\n if __opts__['test']:\n ret['result'] = True\n ret['comment'] = ''\n try:\n cur_ini = __salt__['ini.get_ini'](name, separator)\n except IOError as err:\n ret['result'] = False\n ret['comment'] = \"{0}\".format(err)\n return ret\n for section in sections or []:\n if section not in cur_ini:\n ret['comment'] += 'Section {0} does not exist.\\n'.format(section)\n continue\n ret['comment'] += 'Deleted section {0}.\\n'.format(section)\n ret['result'] = None\n if ret['comment'] == '':\n ret['comment'] = 'No changes detected.'\n return ret\n for section in sections or []:\n try:\n cur_section = __salt__['ini.remove_section'](name, section, separator)\n except IOError as err:\n ret['result'] = False\n ret['comment'] = \"{0}\".format(err)\n return ret\n if not cur_section:\n continue\n ret['changes'][section] = cur_section\n ret['comment'] = 'Changes take effect'\n return ret", "response": "Ensures that the section in ini file is absent in the specified sections."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nparsing a standard pam config file and return a list of dictionaries", "response": "def _parse(contents=None, file_name=None):\n '''\n Parse a standard pam config file\n '''\n if contents:\n pass\n elif file_name and os.path.exists(file_name):\n with salt.utils.files.fopen(file_name, 'r') as ifile:\n contents = salt.utils.stringutils.to_unicode(ifile.read())\n else:\n log.error('File \"%s\" does not exist', file_name)\n return False\n\n rules = []\n for line in contents.splitlines():\n if not line:\n continue\n if line.startswith('#'):\n continue\n control_flag = ''\n module = ''\n arguments = []\n comps = line.split()\n interface = comps[0]\n position = 1\n if comps[1].startswith('['):\n control_flag = comps[1].replace('[', '')\n for part in comps[2:]:\n position += 1\n if part.endswith(']'):\n control_flag += ' {0}'.format(part.replace(']', ''))\n position += 1\n break\n else:\n control_flag += ' {0}'.format(part)\n else:\n control_flag = comps[1]\n position += 1\n module = comps[position]\n if len(comps) > position:\n position += 1\n arguments = comps[position:]\n rules.append({'interface': interface,\n 'control_flag': control_flag,\n 'module': module,\n 'arguments': arguments})\n return rules"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef __get_request_auth(username, password):\n '''\n Get libvirt.openAuth callback with username, password values overriding\n the configuration ones.\n '''\n\n # pylint: disable=unused-argument\n def __request_auth(credentials, user_data):\n '''Callback method passed to libvirt.openAuth().\n\n The credentials argument is a list of credentials that libvirt\n would like to request. An element of this list is a list containing\n 5 items (4 inputs, 1 output):\n - the credential type, e.g. libvirt.VIR_CRED_AUTHNAME\n - a prompt to be displayed to the user\n - a challenge\n - a default result for the request\n - a place to store the actual result for the request\n\n The user_data argument is currently not set in the openAuth call.\n '''\n for credential in credentials:\n if credential[0] == libvirt.VIR_CRED_AUTHNAME:\n credential[4] = username if username else \\\n __salt__['config.get']('virt:connection:auth:username', credential[3])\n elif credential[0] == libvirt.VIR_CRED_NOECHOPROMPT:\n credential[4] = password if password else \\\n __salt__['config.get']('virt:connection:auth:password', credential[3])\n else:\n log.info('Unhandled credential type: %s', credential[0])\n return 0", "response": "Return a callback that returns a function that will be called when the user requests the user."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef __get_conn(**kwargs):\n '''\n Detects what type of dom this node is and attempts to connect to the\n correct hypervisor via libvirt.\n\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n '''\n # This has only been tested on kvm and xen, it needs to be expanded to\n # support all vm layers supported by libvirt\n # Connection string works on bhyve, but auth is not tested.\n\n username = kwargs.get('username', None)\n password = kwargs.get('password', None)\n conn_str = kwargs.get('connection', None)\n if not conn_str:\n conn_str = __salt__['config.get']('virt.connect', None)\n if conn_str is not None:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'virt.connect\\' configuration property has been deprecated in favor '\n 'of \\'virt:connection:uri\\'. \\'virt.connect\\' will stop being used in '\n '{version}.'\n )\n else:\n conn_str = __salt__['config.get']('libvirt:connection', None)\n if conn_str is not None:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'libvirt.connection\\' configuration property has been deprecated in favor '\n 'of \\'virt:connection:uri\\'. \\'libvirt.connection\\' will stop being used in '\n '{version}.'\n )\n\n conn_str = __salt__['config.get']('virt:connection:uri', conn_str)\n\n hypervisor = __salt__['config.get']('libvirt:hypervisor', None)\n if hypervisor is not None:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'libvirt.hypervisor\\' configuration property has been deprecated. '\n 'Rather use the \\'virt:connection:uri\\' to properly define the libvirt '\n 'URI or alias of the host to connect to. \\'libvirt:hypervisor\\' will '\n 'stop being used in {version}.'\n )\n\n if hypervisor == 'esxi' and conn_str is None:\n salt.utils.versions.warn_until(\n 'Sodium',\n 'esxi hypervisor default with no default connection URI detected, '\n 'please set \\'virt:connection:uri\\' to \\'esx\\' for keep the legacy '\n 'behavior. Will default to libvirt guess once \\'libvirt:hypervisor\\' '\n 'configuration is removed in {version}.'\n )\n conn_str = 'esx'\n\n try:\n auth_types = [libvirt.VIR_CRED_AUTHNAME,\n libvirt.VIR_CRED_NOECHOPROMPT,\n libvirt.VIR_CRED_ECHOPROMPT,\n libvirt.VIR_CRED_PASSPHRASE,\n libvirt.VIR_CRED_EXTERNAL]\n conn = libvirt.openAuth(conn_str, [auth_types, __get_request_auth(username, password), None], 0)\n except Exception:\n raise CommandExecutionError(\n 'Sorry, {0} failed to open a connection to the hypervisor '\n 'software at {1}'.format(\n __grains__['fqdn'],\n conn_str\n )\n )\n return conn", "response": "Returns the libvirt connection string for the node"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _get_domain(conn, *vms, **kwargs):\n '''\n Return a domain object for the named VM or return domain object for all VMs.\n\n :params conn: libvirt connection object\n :param vms: list of domain names to look for\n :param iterable: True to return an array in all cases\n '''\n ret = list()\n lookup_vms = list()\n\n all_vms = []\n if kwargs.get('active', True):\n for id_ in conn.listDomainsID():\n all_vms.append(conn.lookupByID(id_).name())\n\n if kwargs.get('inactive', True):\n for id_ in conn.listDefinedDomains():\n all_vms.append(id_)\n\n if not all_vms:\n raise CommandExecutionError('No virtual machines found.')\n\n if vms:\n for name in vms:\n if name not in all_vms:\n raise CommandExecutionError('The VM \"{name}\" is not present'.format(name=name))\n else:\n lookup_vms.append(name)\n else:\n lookup_vms = list(all_vms)\n\n for name in lookup_vms:\n ret.append(conn.lookupByName(name))\n\n return len(ret) == 1 and not kwargs.get('iterable') and ret[0] or ret", "response": "Returns a domain object for the named VM or return all VMs."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nparse qemu - img info JSON output into a single dictionary", "response": "def _parse_qemu_img_info(info):\n '''\n Parse qemu-img info JSON output into disk infos dictionary\n '''\n raw_infos = salt.utils.json.loads(info)\n disks = []\n for disk_infos in raw_infos:\n disk = {\n 'file': disk_infos['filename'],\n 'file format': disk_infos['format'],\n 'disk size': disk_infos['actual-size'],\n 'virtual size': disk_infos['virtual-size'],\n 'cluster size': disk_infos['cluster-size'] if 'cluster-size' in disk_infos else None,\n }\n\n if 'full-backing-filename' in disk_infos.keys():\n disk['backing file'] = format(disk_infos['full-backing-filename'])\n\n if 'snapshots' in disk_infos.keys():\n disk['snapshots'] = [\n {\n 'id': snapshot['id'],\n 'tag': snapshot['name'],\n 'vmsize': snapshot['vm-state-size'],\n 'date': datetime.datetime.fromtimestamp(\n float('{}.{}'.format(snapshot['date-sec'], snapshot['date-nsec']))).isoformat(),\n 'vmclock': datetime.datetime.utcfromtimestamp(\n float('{}.{}'.format(snapshot['vm-clock-sec'],\n snapshot['vm-clock-nsec']))).time().isoformat()\n } for snapshot in disk_infos['snapshots']]\n disks.append(disk)\n\n for disk in disks:\n if 'backing file' in disk.keys():\n candidates = [info for info in disks if 'file' in info.keys() and info['file'] == disk['backing file']]\n if candidates:\n disk['backing file'] = candidates[0]\n\n return disks[0]"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _get_on_poweroff(dom):\n '''\n Return `on_poweroff` setting from the named vm\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_on_restart <domain>\n '''\n node = ElementTree.fromstring(get_xml(dom)).find('on_poweroff')\n return node.text if node is not None else ''", "response": "Get on_poweroff setting from the named vm\n CLI Example : bash\n\n salt '*' virt. get_on_restart <domain >"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn on_reboot setting from the named vm CLI Example : bash salt '*' virt. get_on_reboot <domain >", "response": "def _get_on_reboot(dom):\n '''\n Return `on_reboot` setting from the named vm\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_on_reboot <domain>\n '''\n node = ElementTree.fromstring(get_xml(dom)).find('on_reboot')\n return node.text if node is not None else ''"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _get_on_crash(dom):\n '''\n Return `on_crash` setting from the named vm\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_on_crash <domain>\n '''\n node = ElementTree.fromstring(get_xml(dom)).find('on_crash')\n return node.text if node is not None else ''", "response": "Get on_crash setting from the named vm\n\n CLI Example : bash\n\n salt '*' virt. get_on_crash <domain >"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ngets domain network interfaces from a libvirt domain object.", "response": "def _get_nics(dom):\n '''\n Get domain network interfaces from a libvirt domain object.\n '''\n nics = {}\n doc = ElementTree.fromstring(dom.XMLDesc(0))\n for iface_node in doc.findall('devices/interface'):\n nic = {}\n nic['type'] = iface_node.get('type')\n for v_node in iface_node:\n if v_node.tag == 'mac':\n nic['mac'] = v_node.get('address')\n if v_node.tag == 'model':\n nic['model'] = v_node.get('type')\n if v_node.tag == 'target':\n nic['target'] = v_node.get('dev')\n # driver, source, and match can all have optional attributes\n if re.match('(driver|source|address)', v_node.tag):\n temp = {}\n for key, value in six.iteritems(v_node.attrib):\n temp[key] = value\n nic[v_node.tag] = temp\n # virtualport needs to be handled separately, to pick up the\n # type attribute of the virtualport itself\n if v_node.tag == 'virtualport':\n temp = {}\n temp['type'] = v_node.get('type')\n for key, value in six.iteritems(v_node.attrib):\n temp[key] = value\n nic['virtualport'] = temp\n if 'mac' not in nic:\n continue\n nics[nic['mac']] = nic\n return nics"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nget domain graphics from a libvirt domain object.", "response": "def _get_graphics(dom):\n '''\n Get domain graphics from a libvirt domain object.\n '''\n out = {'autoport': 'None',\n 'keymap': 'None',\n 'listen': 'None',\n 'port': 'None',\n 'type': 'None'}\n doc = ElementTree.fromstring(dom.XMLDesc(0))\n for g_node in doc.findall('devices/graphics'):\n for key, value in six.iteritems(g_node.attrib):\n out[key] = value\n return out"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\ngets domain disks from libvirt domain object.", "response": "def _get_disks(dom):\n '''\n Get domain disks from a libvirt domain object.\n '''\n disks = {}\n doc = ElementTree.fromstring(dom.XMLDesc(0))\n for elem in doc.findall('devices/disk'):\n source = elem.find('source')\n if source is None:\n continue\n target = elem.find('target')\n if target is None:\n continue\n if 'dev' in target.attrib:\n qemu_target = source.get('file', '')\n if not qemu_target:\n qemu_target = source.get('dev', '')\n elif qemu_target.startswith('/dev/zvol/'):\n disks[target.get('dev')] = {\n 'file': qemu_target,\n 'zfs': True}\n continue\n if not qemu_target and 'protocol' in source.attrib and 'name' in source.attrib: # for rbd network\n qemu_target = '{0}:{1}'.format(\n source.get('protocol'),\n source.get('name'))\n if not qemu_target:\n continue\n\n disk = {'file': qemu_target, 'type': elem.get('device')}\n\n driver = elem.find('driver')\n if driver is not None and driver.get('type') == 'qcow2':\n try:\n stdout = subprocess.Popen(\n ['qemu-img', 'info', '--output', 'json', '--backing-chain', disk['file']],\n shell=False,\n stdout=subprocess.PIPE).communicate()[0]\n qemu_output = salt.utils.stringutils.to_str(stdout)\n output = _parse_qemu_img_info(qemu_output)\n disk.update(output)\n except TypeError:\n disk.update({'file': 'Does not exist'})\n\n disks[target.get('dev')] = disk\n return disks"} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nreturn the user and group that the disk images should be owned by Returns the user and group that the disk images should be owned by Returns the user and group that the disk images should be owned by Returns the user group", "response": "def _libvirt_creds():\n '''\n Returns the user and group that the disk images should be owned by\n '''\n g_cmd = 'grep ^\\\\s*group /etc/libvirt/qemu.conf'\n u_cmd = 'grep ^\\\\s*user /etc/libvirt/qemu.conf'\n try:\n stdout = subprocess.Popen(g_cmd,\n shell=True,\n stdout=subprocess.PIPE).communicate()[0]\n group = salt.utils.stringutils.to_str(stdout).split('\"')[1]\n except IndexError:\n group = 'root'\n try:\n stdout = subprocess.Popen(u_cmd,\n shell=True,\n stdout=subprocess.PIPE).communicate()[0]\n user = salt.utils.stringutils.to_str(stdout).split('\"')[1]\n except IndexError:\n user = 'root'\n return {'user': user, 'group': group}"} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns the command to be used to migrate the current version of the current migration types.", "response": "def _get_migrate_command():\n '''\n Returns the command shared by the different migration types\n '''\n tunnel = __salt__['config.option']('virt.tunnel')\n if tunnel:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'virt.tunnel\\' has been deprecated in favor of '\n '\\'virt:tunnel\\'. \\'virt.tunnel\\' will stop '\n 'being used in {version}.')\n else:\n tunnel = __salt__['config.get']('virt:tunnel')\n if tunnel:\n return ('virsh migrate --p2p --tunnelled --live --persistent '\n '--undefinesource ')\n return 'virsh migrate --live --persistent --undefinesource '"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _gen_xml(name,\n cpu,\n mem,\n diskp,\n nicp,\n hypervisor,\n os_type,\n arch,\n graphics=None,\n loader=None,\n **kwargs):\n '''\n Generate the XML string to define a libvirt VM\n '''\n mem = int(mem) * 1024 # MB\n context = {\n 'hypervisor': hypervisor,\n 'name': name,\n 'cpu': six.text_type(cpu),\n 'mem': six.text_type(mem),\n }\n if hypervisor in ['qemu', 'kvm']:\n context['controller_model'] = False\n elif hypervisor == 'vmware':\n # TODO: make bus and model parameterized, this works for 64-bit Linux\n context['controller_model'] = 'lsilogic'\n\n # By default, set the graphics to listen to all addresses\n if graphics:\n if 'listen' not in graphics:\n graphics['listen'] = {'type': 'address', 'address': '0.0.0.0'}\n elif 'address' not in graphics['listen'] and graphics['listen']['type'] == 'address':\n graphics['listen']['address'] = '0.0.0.0'\n\n # Graphics of type 'none' means no graphics device at all\n if graphics.get('type', 'none') == 'none':\n graphics = None\n context['graphics'] = graphics\n\n if loader and 'path' not in loader:\n log.info('`path` is a required property of `loader`, and cannot be found. Skipping loader configuration')\n loader = None\n elif loader:\n loader_attributes = []\n for key, val in loader.items():\n if key == 'path':\n continue\n loader_attributes.append(\"{key}='{val}'\".format(key=key, val=val))\n loader['_attributes'] = ' '.join(loader_attributes)\n\n if 'boot_dev' in kwargs:\n context['boot_dev'] = []\n for dev in kwargs['boot_dev'].split():\n context['boot_dev'].append(dev)\n else:\n context['boot_dev'] = ['hd']\n\n context['loader'] = loader\n\n if os_type == 'xen':\n # Compute the Xen PV boot method\n if __grains__['os_family'] == 'Suse':\n context['kernel'] = '/usr/lib/grub2/x86_64-xen/grub.xen'\n context['boot_dev'] = []\n\n if 'serial_type' in kwargs:\n context['serial_type'] = kwargs['serial_type']\n if 'serial_type' in context and context['serial_type'] == 'tcp':\n if 'telnet_port' in kwargs:\n context['telnet_port'] = kwargs['telnet_port']\n else:\n context['telnet_port'] = 23023 # FIXME: use random unused port\n if 'serial_type' in context:\n if 'console' in kwargs:\n context['console'] = kwargs['console']\n else:\n context['console'] = True\n\n context['disks'] = []\n disk_bus_map = {'virtio': 'vd', 'xen': 'xvd', 'fdc': 'fd', 'ide': 'hd'}\n for i, disk in enumerate(diskp):\n prefix = disk_bus_map.get(disk['model'], 'sd')\n disk_context = {\n 'device': disk.get('device', 'disk'),\n 'target_dev': '{0}{1}'.format(prefix, string.ascii_lowercase[i]),\n 'disk_bus': disk['model'],\n 'type': disk['format'],\n 'index': six.text_type(i),\n }\n if 'source_file' and disk['source_file']:\n disk_context['source_file'] = disk['source_file']\n\n if hypervisor in ['qemu', 'kvm', 'bhyve', 'xen']:\n disk_context['address'] = False\n disk_context['driver'] = True\n elif hypervisor in ['esxi', 'vmware']:\n disk_context['address'] = True\n disk_context['driver'] = False\n context['disks'].append(disk_context)\n context['nics'] = nicp\n\n context['os_type'] = os_type\n context['arch'] = arch\n\n fn_ = 'libvirt_domain.jinja'\n try:\n template = JINJA.get_template(fn_)\n except jinja2.exceptions.TemplateNotFound:\n log.error('Could not load template %s', fn_)\n return ''\n\n return template.render(**context)", "response": "Generate the XML string to define a libvirt VM."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _gen_vol_xml(vmname,\n diskname,\n disktype,\n size,\n pool):\n '''\n Generate the XML string to define a libvirt storage volume\n '''\n size = int(size) * 1024 # MB\n context = {\n 'name': vmname,\n 'filename': '{0}.{1}'.format(diskname, disktype),\n 'volname': diskname,\n 'disktype': disktype,\n 'size': six.text_type(size),\n 'pool': pool,\n }\n fn_ = 'libvirt_volume.jinja'\n try:\n template = JINJA.get_template(fn_)\n except jinja2.exceptions.TemplateNotFound:\n log.error('Could not load template %s', fn_)\n return ''\n return template.render(**context)", "response": "Generate the XML string to define a libvirt storage volume"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _gen_net_xml(name,\n bridge,\n forward,\n vport,\n tag=None):\n '''\n Generate the XML string to define a libvirt network\n '''\n context = {\n 'name': name,\n 'bridge': bridge,\n 'forward': forward,\n 'vport': vport,\n 'tag': tag,\n }\n fn_ = 'libvirt_network.jinja'\n try:\n template = JINJA.get_template(fn_)\n except jinja2.exceptions.TemplateNotFound:\n log.error('Could not load template %s', fn_)\n return ''\n return template.render(**context)", "response": "Generate the XML string to define a libvirt network\n "} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\ngenerate the XML string to define a libvirt storage pool", "response": "def _gen_pool_xml(name,\n ptype,\n target=None,\n permissions=None,\n source_devices=None,\n source_dir=None,\n source_adapter=None,\n source_hosts=None,\n source_auth=None,\n source_name=None,\n source_format=None):\n '''\n Generate the XML string to define a libvirt storage pool\n '''\n hosts = [host.split(':') for host in source_hosts or []]\n context = {\n 'name': name,\n 'ptype': ptype,\n 'target': {'path': target, 'permissions': permissions},\n 'source': {\n 'devices': source_devices or [],\n 'dir': source_dir,\n 'adapter': source_adapter,\n 'hosts': [{'name': host[0], 'port': host[1] if len(host) > 1 else None} for host in hosts],\n 'auth': source_auth,\n 'name': source_name,\n 'format': source_format\n }\n }\n fn_ = 'libvirt_pool.jinja'\n try:\n template = JINJA.get_template(fn_)\n except jinja2.exceptions.TemplateNotFound:\n log.error('Could not load template %s', fn_)\n return ''\n return template.render(**context)"} {"SOURCE": "codesearchnet", "instruction": "How would you code a function in Python 3 to\nextract the images dir from the configuration.", "response": "def _get_images_dir():\n '''\n Extract the images dir from the configuration. First attempts to\n find legacy virt.images, then tries virt:images.\n '''\n img_dir = __salt__['config.option']('virt.images')\n if img_dir:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'virt.images\\' has been deprecated in favor of '\n '\\'virt:images\\'. \\'virt.images\\' will stop '\n 'being used in {version}.')\n else:\n img_dir = __salt__['config.get']('virt:images')\n\n log.debug('Image directory from config option `virt:images`'\n ' is %s', img_dir)\n return img_dir"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncreating a new image on the ZFS filesystem.", "response": "def _zfs_image_create(vm_name,\n pool,\n disk_name,\n hostname_property_name,\n sparse_volume,\n disk_size,\n disk_image_name):\n '''\n Clones an existing image, or creates a new one.\n\n When cloning an image, disk_image_name refers to the source\n of the clone. If not specified, disk_size is used for creating\n a new zvol, and sparse_volume determines whether to create\n a thin provisioned volume.\n\n The cloned or new volume can have a ZFS property set containing\n the vm_name. Use hostname_property_name for specifying the key\n of this ZFS property.\n '''\n if not disk_image_name and not disk_size:\n raise CommandExecutionError(\n 'Unable to create new disk {0}, please specify'\n ' the disk image name or disk size argument'\n .format(disk_name)\n )\n\n if not pool:\n raise CommandExecutionError(\n 'Unable to create new disk {0}, please specify'\n ' the disk pool name'.format(disk_name))\n\n destination_fs = os.path.join(pool,\n '{0}.{1}'.format(vm_name, disk_name))\n log.debug('Image destination will be %s', destination_fs)\n\n existing_disk = __salt__['zfs.list'](name=pool)\n if 'error' in existing_disk:\n raise CommandExecutionError(\n 'Unable to create new disk {0}. {1}'\n .format(destination_fs, existing_disk['error'])\n )\n elif destination_fs in existing_disk:\n log.info('ZFS filesystem %s already exists. Skipping creation', destination_fs)\n blockdevice_path = os.path.join('/dev/zvol', pool, vm_name)\n return blockdevice_path\n\n properties = {}\n if hostname_property_name:\n properties[hostname_property_name] = vm_name\n\n if disk_image_name:\n __salt__['zfs.clone'](\n name_a=disk_image_name,\n name_b=destination_fs,\n properties=properties)\n\n elif disk_size:\n __salt__['zfs.create'](\n name=destination_fs,\n properties=properties,\n volume_size=disk_size,\n sparse=sparse_volume)\n\n blockdevice_path = os.path.join('/dev/zvol', pool, '{0}.{1}'\n .format(vm_name, disk_name))\n log.debug('Image path will be %s', blockdevice_path)\n return blockdevice_path"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _qemu_image_create(disk, create_overlay=False, saltenv='base'):\n '''\n Create the image file using specified disk_size or/and disk_image\n\n Return path to the created image file\n '''\n disk_size = disk.get('size', None)\n disk_image = disk.get('image', None)\n\n if not disk_size and not disk_image:\n raise CommandExecutionError(\n 'Unable to create new disk {0}, please specify'\n ' disk size and/or disk image argument'\n .format(disk['filename'])\n )\n\n img_dest = disk['source_file']\n log.debug('Image destination will be %s', img_dest)\n img_dir = os.path.dirname(img_dest)\n log.debug('Image destination directory is %s', img_dir)\n if not os.path.exists(img_dir):\n os.makedirs(img_dir)\n\n if disk_image:\n log.debug('Create disk from specified image %s', disk_image)\n sfn = __salt__['cp.cache_file'](disk_image, saltenv)\n\n qcow2 = False\n if salt.utils.path.which('qemu-img'):\n res = __salt__['cmd.run']('qemu-img info {}'.format(sfn))\n imageinfo = salt.utils.yaml.safe_load(res)\n qcow2 = imageinfo['file format'] == 'qcow2'\n try:\n if create_overlay and qcow2:\n log.info('Cloning qcow2 image %s using copy on write', sfn)\n __salt__['cmd.run'](\n 'qemu-img create -f qcow2 -o backing_file={0} {1}'\n .format(sfn, img_dest).split())\n else:\n log.debug('Copying %s to %s', sfn, img_dest)\n salt.utils.files.copyfile(sfn, img_dest)\n\n mask = salt.utils.files.get_umask()\n\n if disk_size and qcow2:\n log.debug('Resize qcow2 image to %sM', disk_size)\n __salt__['cmd.run'](\n 'qemu-img resize {0} {1}M'\n .format(img_dest, disk_size)\n )\n\n log.debug('Apply umask and remove exec bit')\n mode = (0o0777 ^ mask) & 0o0666\n os.chmod(img_dest, mode)\n\n except (IOError, OSError) as err:\n raise CommandExecutionError(\n 'Problem while copying image. {0} - {1}'\n .format(disk_image, err)\n )\n\n else:\n # Create empty disk\n try:\n mask = salt.utils.files.get_umask()\n\n if disk_size:\n log.debug('Create empty image with size %sM', disk_size)\n __salt__['cmd.run'](\n 'qemu-img create -f {0} {1} {2}M'\n .format(disk.get('format', 'qcow2'), img_dest, disk_size)\n )\n else:\n raise CommandExecutionError(\n 'Unable to create new disk {0},'\n ' please specify <size> argument'\n .format(img_dest)\n )\n\n log.debug('Apply umask and remove exec bit')\n mode = (0o0777 ^ mask) & 0o0666\n os.chmod(img_dest, mode)\n\n except (IOError, OSError) as err:\n raise CommandExecutionError(\n 'Problem while creating volume {0} - {1}'\n .format(img_dest, err)\n )\n\n return img_dest", "response": "Create the image file using the specified disk size or image and return the path to the created image file"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef _disk_profile(profile, hypervisor, disks=None, vm_name=None, image=None, pool=None, **kwargs):\n '''\n Gather the disk profile from the config or apply the default based\n on the active hypervisor\n\n This is the ``default`` profile for KVM/QEMU, which can be\n overridden in the configuration:\n\n .. code-block:: yaml\n\n virt:\n disk:\n default:\n - system:\n size: 8192\n format: qcow2\n model: virtio\n\n Example profile for KVM/QEMU with two disks, first is created\n from specified image, the second is empty:\n\n .. code-block:: yaml\n\n virt:\n disk:\n two_disks:\n - system:\n size: 8192\n format: qcow2\n model: virtio\n image: http://path/to/image.qcow2\n - lvm:\n size: 32768\n format: qcow2\n model: virtio\n\n The ``format`` and ``model`` parameters are optional, and will\n default to whatever is best suitable for the active hypervisor.\n '''\n default = [{'system':\n {'size': 8192}}]\n if hypervisor == 'vmware':\n overlay = {'format': 'vmdk',\n 'model': 'scsi',\n 'device': 'disk',\n 'pool': '[{0}] '.format(pool if pool else '0')}\n elif hypervisor in ['qemu', 'kvm']:\n overlay = {'format': 'qcow2',\n 'device': 'disk',\n 'model': 'virtio'}\n elif hypervisor in ['bhyve']:\n overlay = {'format': 'raw',\n 'device': 'disk',\n 'model': 'virtio',\n 'sparse_volume': False}\n elif hypervisor == 'xen':\n overlay = {'format': 'qcow2',\n 'device': 'disk',\n 'model': 'xen'}\n else:\n overlay = {}\n\n # Get the disks from the profile\n disklist = []\n if profile:\n disklist = copy.deepcopy(\n __salt__['config.get']('virt:disk', {}).get(profile, default))\n\n # Transform the list to remove one level of dictionnary and add the name as a property\n disklist = [dict(d, name=name) for disk in disklist for name, d in disk.items()]\n\n # Add the image to the first disk if there is one\n if image:\n # If image is specified in module arguments, then it will be used\n # for the first disk instead of the image from the disk profile\n log.debug('%s image from module arguments will be used for disk \"%s\"'\n ' instead of %s', image, disklist[0]['name'], disklist[0].get('image', \"\"))\n disklist[0]['image'] = image\n\n # Merge with the user-provided disks definitions\n if disks:\n for udisk in disks:\n if 'name' in udisk:\n found = [disk for disk in disklist if udisk['name'] == disk['name']]\n if found:\n found[0].update(udisk)\n else:\n disklist.append(udisk)\n\n for disk in disklist:\n # Add the missing properties that have defaults\n for key, val in six.iteritems(overlay):\n if key not in disk:\n disk[key] = val\n\n # We may have an already computed source_file (i.e. image not created by our module)\n if 'source_file' in disk and disk['source_file']:\n disk['filename'] = os.path.basename(disk['source_file'])\n elif 'source_file' not in disk:\n _fill_disk_filename(vm_name, disk, hypervisor, **kwargs)\n\n return disklist", "response": "Return the disk profile for the active hypervisor."} {"SOURCE": "codesearchnet", "instruction": "Implement a function in Python 3 to\nfill the disk file name and source file properties.", "response": "def _fill_disk_filename(vm_name, disk, hypervisor, **kwargs):\n '''\n Compute the disk file name and update it in the disk value.\n '''\n base_dir = disk.get('pool', None)\n if hypervisor in ['qemu', 'kvm', 'xen']:\n # Compute the base directory from the pool property. We may have either a path\n # or a libvirt pool name there.\n # If the pool is a known libvirt one with a target path, use it as target path\n if not base_dir:\n base_dir = _get_images_dir()\n else:\n if not base_dir.startswith('/'):\n # The pool seems not to be a path, lookup for pool infos\n infos = pool_info(base_dir, **kwargs)\n pool = infos[base_dir] if base_dir in infos else None\n if not pool or not pool['target_path'] or pool['target_path'].startswith('/dev'):\n raise CommandExecutionError(\n 'Unable to create new disk {0}, specified pool {1} does not exist '\n 'or is unsupported'.format(disk['name'], base_dir))\n base_dir = pool['target_path']\n if hypervisor == 'bhyve' and vm_name:\n disk['filename'] = '{0}.{1}'.format(vm_name, disk['name'])\n disk['source_file'] = os.path.join('/dev/zvol', base_dir or '', disk['filename'])\n elif vm_name:\n # Compute the filename and source file properties if possible\n disk['filename'] = '{0}_{1}.{2}'.format(vm_name, disk['name'], disk['format'])\n disk['source_file'] = os.path.join(base_dir, disk['filename'])"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ncomplete the NICs for the given network interfaces.", "response": "def _complete_nics(interfaces, hypervisor, dmac=None):\n '''\n Complete missing data for network interfaces.\n '''\n\n vmware_overlay = {'type': 'bridge', 'source': 'DEFAULT', 'model': 'e1000'}\n kvm_overlay = {'type': 'bridge', 'source': 'br0', 'model': 'virtio'}\n bhyve_overlay = {'type': 'bridge', 'source': 'bridge0', 'model': 'virtio'}\n xen_overlay = {'type': 'bridge', 'source': 'br0', 'model': None}\n overlays = {\n 'xen': xen_overlay,\n 'kvm': kvm_overlay,\n 'qemu': kvm_overlay,\n 'vmware': vmware_overlay,\n 'bhyve': bhyve_overlay,\n }\n\n def _normalize_net_types(attributes):\n '''\n Guess which style of definition:\n\n bridge: br0\n\n or\n\n network: net0\n\n or\n\n type: network\n source: net0\n '''\n for type_ in ['bridge', 'network']:\n if type_ in attributes:\n attributes['type'] = type_\n # we want to discard the original key\n attributes['source'] = attributes.pop(type_)\n\n attributes['type'] = attributes.get('type', None)\n attributes['source'] = attributes.get('source', None)\n\n def _apply_default_overlay(attributes):\n '''\n Apply the default overlay to attributes\n '''\n for key, value in six.iteritems(overlays[hypervisor]):\n if key not in attributes or not attributes[key]:\n attributes[key] = value\n\n def _assign_mac(attributes, hypervisor):\n '''\n Compute mac address for NIC depending on hypervisor\n '''\n if dmac is not None:\n log.debug('Default MAC address is %s', dmac)\n if salt.utils.validate.net.mac(dmac):\n attributes['mac'] = dmac\n else:\n msg = 'Malformed MAC address: {0}'.format(dmac)\n raise CommandExecutionError(msg)\n else:\n if hypervisor in ['qemu', 'kvm']:\n attributes['mac'] = salt.utils.network.gen_mac(\n prefix='52:54:00')\n else:\n attributes['mac'] = salt.utils.network.gen_mac()\n\n for interface in interfaces:\n _normalize_net_types(interface)\n if interface.get('mac', None) is None:\n _assign_mac(interface, hypervisor)\n if hypervisor in overlays:\n _apply_default_overlay(interface)\n\n return interfaces"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\ncomputing NIC data based on profile_name", "response": "def _nic_profile(profile_name, hypervisor, dmac=None):\n '''\n Compute NIC data based on profile\n '''\n\n default = [{'eth0': {}}]\n\n # support old location\n config_data = __salt__['config.option']('virt.nic', {}).get(\n profile_name, None\n )\n\n if config_data is not None:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'virt.nic\\' has been deprecated in favor of \\'virt:nic\\'. '\n '\\'virt.nic\\' will stop being used in {version}.'\n )\n else:\n config_data = __salt__['config.get']('virt:nic', {}).get(\n profile_name, default\n )\n\n interfaces = []\n\n # pylint: disable=invalid-name\n def append_dict_profile_to_interface_list(profile_dict):\n '''\n Append dictionary profile data to interfaces list\n '''\n for interface_name, attributes in six.iteritems(profile_dict):\n attributes['name'] = interface_name\n interfaces.append(attributes)\n\n # old style dicts (top-level dicts)\n #\n # virt:\n # nic:\n # eth0:\n # bridge: br0\n # eth1:\n # network: test_net\n if isinstance(config_data, dict):\n append_dict_profile_to_interface_list(config_data)\n\n # new style lists (may contain dicts)\n #\n # virt:\n # nic:\n # - eth0:\n # bridge: br0\n # - eth1:\n # network: test_net\n #\n # virt:\n # nic:\n # - name: eth0\n # bridge: br0\n # - name: eth1\n # network: test_net\n elif isinstance(config_data, list):\n for interface in config_data:\n if isinstance(interface, dict):\n if len(interface) == 1:\n append_dict_profile_to_interface_list(interface)\n else:\n interfaces.append(interface)\n\n # dmac can only be used from init()\n return _complete_nics(interfaces, hypervisor, dmac=dmac)"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _get_merged_nics(hypervisor, profile, interfaces=None, dmac=None):\n '''\n Get network devices from the profile and merge uer defined ones with them.\n '''\n nicp = _nic_profile(profile, hypervisor, dmac=dmac) if profile else []\n log.debug('NIC profile is %s', nicp)\n if interfaces:\n users_nics = _complete_nics(interfaces, hypervisor)\n for unic in users_nics:\n found = [nic for nic in nicp if nic['name'] == unic['name']]\n if found:\n found[0].update(unic)\n else:\n nicp.append(unic)\n log.debug('Merged NICs: %s', nicp)\n return nicp", "response": "Get network devices from the profile and merge uer defined ones with them."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef init(name,\n cpu,\n mem,\n image=None,\n nic='default',\n interfaces=None,\n hypervisor=None,\n start=True, # pylint: disable=redefined-outer-name\n disk='default',\n disks=None,\n saltenv='base',\n seed=True,\n install=True,\n pub_key=None,\n priv_key=None,\n seed_cmd='seed.apply',\n enable_vnc=False,\n enable_qcow=False,\n graphics=None,\n os_type=None,\n arch=None,\n loader=None,\n **kwargs):\n '''\n Initialize a new vm\n\n :param name: name of the virtual machine to create\n :param cpu: Number of virtual CPUs to assign to the virtual machine\n :param mem: Amount of memory to allocate to the virtual machine in MiB.\n :param image: Path to a disk image to use as the first disk (Default: ``None``).\n Deprecated in favor of the ``disks`` parameter. To set (or change) the\n image of a disk, add the following to the disks definitions:\n\n .. code-block:: python\n\n {\n 'name': 'name_of_disk_to_change',\n 'image': '/path/to/the/image'\n }\n\n :param nic: NIC profile to use (Default: ``'default'``).\n The profile interfaces can be customized / extended with the interfaces\n parameter. If set to ``None``, no profile will be used.\n\n :param interfaces:\n List of dictionaries providing details on the network interfaces to create.\n These data are merged with the ones from the nic profile. The structure of\n each dictionary is documented in init-nic-def_.\n\n .. versionadded:: 2019.2.0\n\n :param hypervisor: the virtual machine type. By default the value will be\n computed according to the virtual host capabilities.\n\n :param start: ``True`` to start the virtual machine after having defined it\n (Default: ``True``)\n\n :param disk: Disk profile to use (Default: ``'default'``). If set to\n ``None``, no profile will be used.\n\n :param disks: List of dictionaries providing details on the disk devices to\n create. These data are merged with the ones from the disk profile. The\n structure of each dictionary is documented in init-disk-def_.\n\n .. versionadded:: 2019.2.0\n\n :param saltenv: Fileserver environment (Default: ``'base'``)\n\n :param seed: ``True`` to seed the disk image. Only used when the ``image``\n parameter is provided. (Default: ``True``)\n\n :param install: install salt minion if absent (Default: ``True``)\n :param pub_key: public key to seed with (Default: ``None``)\n :param priv_key: public key to seed with (Default: ``None``)\n :param seed_cmd: Salt command to execute to seed the image. (Default:\n ``'seed.apply'``)\n\n :param enable_vnc:\n ``True`` to setup a vnc display for the VM (Default: ``False``)\n\n Deprecated in favor of the ``graphics`` parameter. Could be replaced with\n the following:\n\n .. code-block:: python\n\n graphics={'type': 'vnc'}\n\n .. deprecated:: 2019.2.0\n\n :param graphics:\n Dictionary providing details on the graphics device to create. (Default: ``None``)\n See init-graphics-def_ for more details on the possible values.\n\n .. versionadded:: 2019.2.0\n\n :param loader:\n Dictionary providing details on the BIOS firmware loader. (Default: ``None``)\n See init-loader-def_ for more details on the possible values.\n\n .. versionadded:: 2019.2.0\n :param os_type:\n type of virtualization as found in the ``//os/type`` element of the libvirt definition.\n The default value is taken from the host capabilities, with a preference for ``hvm``.\n\n .. versionadded:: 2019.2.0\n\n :param arch:\n architecture of the virtual machine. The default value is taken from the host capabilities,\n but ``x86_64`` is prefed over ``i686``.\n\n .. versionadded:: 2019.2.0\n\n :param enable_qcow:\n ``True`` to create a QCOW2 overlay image, rather than copying the image\n (Default: ``False``).\n\n Deprecated in favor of ``disks`` parameter. Add the following to the disks\n definitions to create an overlay image of a template disk image with an\n image set:\n\n .. code-block:: python\n\n {\n 'name': 'name_of_disk_to_change',\n 'overlay_image': True\n }\n\n .. deprecated:: 2019.2.0\n\n :param pool:\n Path of the folder where the image files are located for vmware/esx hypervisors.\n\n Deprecated in favor of ``disks`` parameter. Add the following to the disks\n definitions to set the vmware datastore of a disk image:\n\n .. code-block:: python\n\n {\n 'name': 'name_of_disk_to_change',\n 'pool': 'mydatastore'\n }\n\n .. deprecated:: Flurorine\n\n :param dmac:\n Default MAC address to use for the network interfaces. By default MAC addresses are\n automatically generated.\n\n Deprecated in favor of ``interfaces`` parameter. Add the following to the interfaces\n definitions to force the mac address of a NIC:\n\n .. code-block:: python\n\n {\n 'name': 'name_of_nic_to_change',\n 'mac': 'MY:MA:CC:ADD:RE:SS'\n }\n\n .. deprecated:: 2019.2.0\n\n :param config: minion configuration to use when seeding. See :mod:`seed\n module <salt.modules.seed>` for more details\n\n :param boot_dev: String of space-separated devices to boot from (Default: ``'hd'``)\n :param serial_type: Serial device type. One of ``'pty'``, ``'tcp'``\n (Default: ``None``)\n :param telnet_port: Telnet port to use for serial device of type ``tcp``.\n :param console: ``True`` to add a console device along with serial one\n (Default: ``True``)\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n .. _init-nic-def:\n\n **Network Interface Definitions**\n\n Network interfaces dictionaries can contain the following properties:\n\n name\n Name of the network interface. This is only used as a key to merge with the profile data\n\n type\n Network type. One of ``'bridge'``, ``'network'``\n\n source\n The network source, typically the bridge or network name\n\n mac\n The desired mac address, computed if ``None`` (Default: ``None``).\n\n model\n The network card model (Default: depends on the hypervisor)\n\n .. _init-disk-def:\n\n **Disk Definitions**\n\n Disk dictionaries can contain the following properties:\n\n name\n Name of the disk. This is mostly used in the name of the disk image and as a key to merge\n with the profile data.\n\n format\n Format of the disk image, like ``'qcow2'``, ``'raw'``, ``'vmdk'``.\n (Default: depends on the hypervisor)\n\n size\n Disk size in MiB\n\n pool\n Path to the folder or name of the pool where disks should be created.\n (Default: depends on hypervisor)\n\n model\n One of the disk busses allowed by libvirt (Default: depends on hypervisor)\n\n See the libvirt `disk element`_ documentation for the allowed bus types.\n\n image\n Path to the image to use for the disk. If no image is provided, an empty disk will be created\n (Default: ``None``)\n\n overlay_image\n ``True`` to create a QCOW2 disk image with ``image`` as backing file. If ``False``\n the file pointed to by the ``image`` property will simply be copied. (Default: ``False``)\n\n hostname_property\n When using ZFS volumes, setting this value to a ZFS property ID will make Salt store the name of the\n virtual machine inside this property. (Default: ``None``)\n\n sparse_volume\n Boolean to specify whether to use a thin provisioned ZFS volume.\n\n Example profile for a bhyve VM with two ZFS disks. The first is\n cloned from the specified image. The second disk is a thin\n provisioned volume.\n\n .. code-block:: yaml\n\n virt:\n disk:\n two_zvols:\n - system:\n image: zroot/bhyve/CentOS-7-x86_64-v1@v1.0.5\n hostname_property: virt:hostname\n pool: zroot/bhyve/guests\n - data:\n pool: tank/disks\n size: 20G\n hostname_property: virt:hostname\n sparse_volume: True\n\n source_file\n Absolute path to the disk image to use. Not to be confused with ``image`` parameter. This\n parameter is useful to use disk images that are created outside of this module. Can also\n be ``None`` for devices that have no associated image like cdroms.\n\n device\n Type of device of the disk. Can be one of 'disk', 'cdrom', 'floppy' or 'lun'.\n (Default: ``'disk'``)\n\n .. _init-graphics-def:\n\n **Graphics Definition**\n\n The graphics dictionnary can have the following properties:\n\n type\n Graphics type. The possible values are ``none``, ``'spice'``, ``'vnc'`` and other values\n allowed as a libvirt graphics type (Default: ``None``)\n\n See the libvirt `graphics element`_ documentation for more details on the possible types.\n\n port\n Port to export the graphics on for ``vnc``, ``spice`` and ``rdp`` types.\n\n tls_port\n Port to export the graphics over a secured connection for ``spice`` type.\n\n listen\n Dictionary defining on what address to listen on for ``vnc``, ``spice`` and ``rdp``.\n It has a ``type`` property with ``address`` and ``None`` as possible values, and an\n ``address`` property holding the IP or hostname to listen on.\n\n By default, not setting the ``listen`` part of the dictionary will default to\n listen on all addresses.\n\n .. _init-loader-def:\n\n **Loader Definition**\n\n The loader dictionary must have the following property:\n\n path\n Path to the UEFI firmware binary\n\n Optionally, you can provide arbitrary attributes such as ``readonly`` or ``type``. See\n the libvirt documentation for all supported loader parameters.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt 'hypervisor' virt.init vm_name 4 512 salt://path/to/image.raw\n salt 'hypervisor' virt.init vm_name 4 512 /var/lib/libvirt/images/img.raw\n salt 'hypervisor' virt.init vm_name 4 512 nic=profile disk=profile\n\n The disk images will be created in an image folder within the directory\n defined by the ``virt:images`` option. Its default value is\n ``/srv/salt-images/`` but this can changed with such a configuration:\n\n .. code-block:: yaml\n\n virt:\n images: /data/my/vm/images/\n\n .. _disk element: https://libvirt.org/formatdomain.html#elementsDisks\n .. _graphics element: https://libvirt.org/formatdomain.html#elementsGraphics\n '''\n caps = capabilities(**kwargs)\n os_types = sorted({guest['os_type'] for guest in caps['guests']})\n arches = sorted({guest['arch']['name'] for guest in caps['guests']})\n if not hypervisor:\n hypervisor = __salt__['config.get']('libvirt:hypervisor', hypervisor)\n if hypervisor is not None:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'libvirt:hypervisor\\' configuration property has been deprecated. '\n 'Rather use the \\'virt:connection:uri\\' to properly define the libvirt '\n 'URI or alias of the host to connect to. \\'libvirt:hypervisor\\' will '\n 'stop being used in {version}.'\n )\n else:\n # Use the machine types as possible values\n # Prefer 'kvm' over the others if available\n hypervisors = sorted({x for y in [guest['arch']['domains'].keys() for guest in caps['guests']] for x in y})\n hypervisor = 'kvm' if 'kvm' in hypervisors else hypervisors[0]\n\n # esxi used to be a possible value for the hypervisor: map it to vmware since it's the same\n hypervisor = 'vmware' if hypervisor == 'esxi' else hypervisor\n\n log.debug('Using hypervisor %s', hypervisor)\n\n # the NICs are computed as follows:\n # 1 - get the default NICs from the profile\n # 2 - Complete the users NICS\n # 3 - Update the default NICS list to the users one, matching key is the name\n dmac = kwargs.get('dmac', None)\n if dmac:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'dmac\\' parameter has been deprecated. Rather use the \\'interfaces\\' parameter '\n 'to properly define the desired MAC address. \\'dmac\\' will be removed in {version}.'\n )\n nicp = _get_merged_nics(hypervisor, nic, interfaces, dmac=dmac)\n\n # the disks are computed as follows:\n # 1 - get the disks defined in the profile\n # 2 - set the image on the first disk (will be removed later)\n # 3 - update the disks from the profile with the ones from the user. The matching key is the name.\n pool = kwargs.get('pool', None)\n if pool:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'pool\\' parameter has been deprecated. Rather use the \\'disks\\' parameter '\n 'to properly define the vmware datastore of disks. \\'pool\\' will be removed in {version}.'\n )\n\n if image:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'image\\' parameter has been deprecated. Rather use the \\'disks\\' parameter '\n 'to override or define the image. \\'image\\' will be removed in {version}.'\n )\n\n diskp = _disk_profile(disk, hypervisor, disks, name, image=image, pool=pool, **kwargs)\n\n # Create multiple disks, empty or from specified images.\n for _disk in diskp:\n log.debug(\"Creating disk for VM [ %s ]: %s\", name, _disk)\n\n if hypervisor == 'vmware':\n if 'image' in _disk:\n # TODO: we should be copying the image file onto the ESX host\n raise SaltInvocationError(\n 'virt.init does not support image '\n 'template in conjunction with esxi hypervisor'\n )\n else:\n # assume libvirt manages disks for us\n log.debug('Generating libvirt XML for %s', _disk)\n vol_xml = _gen_vol_xml(\n name,\n _disk['name'],\n _disk['format'],\n _disk['size'],\n _disk['pool']\n )\n define_vol_xml_str(vol_xml)\n\n elif hypervisor in ['qemu', 'kvm', 'xen']:\n\n create_overlay = enable_qcow\n if create_overlay:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'enable_qcow\\' parameter has been deprecated. Rather use the \\'disks\\' '\n 'parameter to override or define the image. \\'enable_qcow\\' will be removed '\n 'in {version}.'\n )\n else:\n create_overlay = _disk.get('overlay_image', False)\n\n if _disk['source_file']:\n if os.path.exists(_disk['source_file']):\n img_dest = _disk['source_file']\n else:\n img_dest = _qemu_image_create(_disk, create_overlay, saltenv)\n else:\n img_dest = None\n\n # Seed only if there is an image specified\n if seed and img_dest and _disk.get('image', None):\n log.debug('Seed command is %s', seed_cmd)\n __salt__[seed_cmd](\n img_dest,\n id_=name,\n config=kwargs.get('config'),\n install=install,\n pub_key=pub_key,\n priv_key=priv_key,\n )\n\n elif hypervisor in ['bhyve']:\n img_dest = _zfs_image_create(\n vm_name=name,\n pool=_disk.get('pool'),\n disk_name=_disk.get('name'),\n disk_size=_disk.get('size'),\n disk_image_name=_disk.get('image'),\n hostname_property_name=_disk.get('hostname_property'),\n sparse_volume=_disk.get('sparse_volume'),\n )\n\n else:\n # Unknown hypervisor\n raise SaltInvocationError(\n 'Unsupported hypervisor when handling disk image: {0}'\n .format(hypervisor)\n )\n\n log.debug('Generating VM XML')\n\n if enable_vnc:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'enable_vnc\\' parameter has been deprecated in favor of '\n '\\'graphics\\'. Use graphics={\\'type\\': \\'vnc\\'} for the same behavior. '\n '\\'enable_vnc\\' will be removed in {version}. ')\n graphics = {'type': 'vnc'}\n\n if os_type is None:\n os_type = 'hvm' if 'hvm' in os_types else os_types[0]\n if arch is None:\n arch = 'x86_64' if 'x86_64' in arches else arches[0]\n\n vm_xml = _gen_xml(name, cpu, mem, diskp, nicp, hypervisor, os_type, arch, graphics, loader, **kwargs)\n conn = __get_conn(**kwargs)\n try:\n conn.defineXML(vm_xml)\n except libvirtError as err:\n # check if failure is due to this domain already existing\n if \"domain '{}' already exists\".format(name) in six.text_type(err):\n # continue on to seeding\n log.warning(err)\n else:\n conn.close()\n raise err # a real error we should report upwards\n\n if start:\n log.debug('Starting VM %s', name)\n _get_domain(conn, name).create()\n conn.close()\n\n return True", "response": "Initialize a virtual machine with the given parameters."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\ntest if two disks are considered like the same device", "response": "def _disks_equal(disk1, disk2):\n '''\n Test if two disk elements should be considered like the same device\n '''\n target1 = disk1.find('target')\n target2 = disk2.find('target')\n source1 = ElementTree.tostring(disk1.find('source')) if disk1.find('source') is not None else None\n source2 = ElementTree.tostring(disk2.find('source')) if disk2.find('source') is not None else None\n\n return source1 == source2 and \\\n target1 is not None and target2 is not None and \\\n target1.get('bus') == target2.get('bus') and \\\n disk1.get('device', 'disk') == disk2.get('device', 'disk') and \\\n target1.get('dev') == target2.get('dev')"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _nics_equal(nic1, nic2):\n '''\n Test if two interface elements should be considered like the same device\n '''\n\n def _filter_nic(nic):\n '''\n Filter out elements to ignore when comparing nics\n '''\n return {\n 'type': nic.attrib['type'],\n 'source': nic.find('source').attrib[nic.attrib['type']] if nic.find('source') is not None else None,\n 'mac': nic.find('mac').attrib['address'].lower() if nic.find('mac') is not None else None,\n 'model': nic.find('model').attrib['type'] if nic.find('model') is not None else None,\n }\n return _filter_nic(nic1) == _filter_nic(nic2)", "response": "Test if two NICs are considered like the same device"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _graphics_equal(gfx1, gfx2):\n '''\n Test if two graphics devices should be considered the same device\n '''\n def _filter_graphics(gfx):\n '''\n When the domain is running, the graphics element may contain additional properties\n with the default values. This function will strip down the default values.\n '''\n gfx_copy = copy.deepcopy(gfx)\n\n defaults = [{'node': '.', 'attrib': 'port', 'values': ['5900', '-1']},\n {'node': '.', 'attrib': 'address', 'values': ['127.0.0.1']},\n {'node': 'listen', 'attrib': 'address', 'values': ['127.0.0.1']}]\n\n for default in defaults:\n node = gfx_copy.find(default['node'])\n attrib = default['attrib']\n if node is not None and (attrib not in node.attrib or node.attrib[attrib] in default['values']):\n node.set(attrib, default['values'][0])\n return gfx_copy\n\n return ElementTree.tostring(_filter_graphics(gfx1)) == ElementTree.tostring(_filter_graphics(gfx2))", "response": "Test if two graphics devices should be considered the same device"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ncompare lists to extract the changes from the old list and new list.", "response": "def _diff_lists(old, new, comparator):\n '''\n Compare lists to extract the changes\n\n :param old: old list\n :param new: new list\n :return: a dictionary with ``unchanged``, ``new``, ``deleted`` and ``sorted`` keys\n\n The sorted list is the union of unchanged and new lists, but keeping the original\n order from the new list.\n '''\n def _remove_indent(node):\n '''\n Remove the XML indentation to compare XML trees more easily\n '''\n node_copy = copy.deepcopy(node)\n node_copy.text = None\n for item in node_copy.iter():\n item.tail = None\n return node_copy\n\n diff = {'unchanged': [], 'new': [], 'deleted': [], 'sorted': []}\n # We don't want to alter old since it may be used later by caller\n old_devices = copy.deepcopy(old)\n for new_item in new:\n found = [item for item in old_devices if comparator(_remove_indent(item), _remove_indent(new_item))]\n if found:\n old_devices.remove(found[0])\n diff['unchanged'].append(found[0])\n diff['sorted'].append(found[0])\n else:\n diff['new'].append(new_item)\n diff['sorted'].append(new_item)\n diff['deleted'] = old_devices\n return diff"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncomparing disk definitions to extract the changes and fix target devices", "response": "def _diff_disk_lists(old, new):\n '''\n Compare disk definitions to extract the changes and fix target devices\n\n :param old: list of ElementTree nodes representing the old disks\n :param new: list of ElementTree nodes representing the new disks\n '''\n # Change the target device to avoid duplicates before diffing: this may lead\n # to additional changes. Think of unchanged disk 'hda' and another disk listed\n # before it becoming 'hda' too... the unchanged need to turn into 'hdb'.\n targets = []\n prefixes = ['fd', 'hd', 'vd', 'sd', 'xvd', 'ubd']\n for disk in new:\n target_node = disk.find('target')\n target = target_node.get('dev')\n prefix = [item for item in prefixes if target.startswith(item)][0]\n new_target = ['{0}{1}'.format(prefix, string.ascii_lowercase[i]) for i in range(len(new))\n if '{0}{1}'.format(prefix, string.ascii_lowercase[i]) not in targets][0]\n target_node.set('dev', new_target)\n targets.append(new_target)\n\n return _diff_lists(old, new, _disks_equal)"} {"SOURCE": "codesearchnet", "instruction": "Implement a Python 3 function for\ncomparing network interface definitions to extract the changes", "response": "def _diff_interface_lists(old, new):\n '''\n Compare network interface definitions to extract the changes\n\n :param old: list of ElementTree nodes representing the old interfaces\n :param new: list of ElementTree nodes representing the new interfaces\n '''\n diff = _diff_lists(old, new, _nics_equal)\n\n # Remove duplicated addresses mac addresses and let libvirt generate them for us\n macs = [nic.find('mac').get('address') for nic in diff['unchanged']]\n for nic in diff['new']:\n mac = nic.find('mac')\n if mac.get('address') in macs:\n nic.remove(mac)\n\n return diff"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nupdates the virtual machine definition of an existing domain.", "response": "def update(name,\n cpu=0,\n mem=0,\n disk_profile=None,\n disks=None,\n nic_profile=None,\n interfaces=None,\n graphics=None,\n live=True,\n **kwargs):\n '''\n Update the definition of an existing domain.\n\n :param name: Name of the domain to update\n :param cpu: Number of virtual CPUs to assign to the virtual machine\n :param mem: Amount of memory to allocate to the virtual machine in MiB.\n :param disk_profile: disk profile to use\n :param disks:\n Disk definitions as documented in the :func:`init` function.\n If neither the profile nor this parameter are defined, the disk devices\n will not be changed. However to clear disks set this parameter to empty list.\n\n :param nic_profile: network interfaces profile to use\n :param interfaces:\n Network interface definitions as documented in the :func:`init` function.\n If neither the profile nor this parameter are defined, the interface devices\n will not be changed. However to clear network interfaces set this parameter\n to empty list.\n\n :param graphics:\n The new graphics definition as defined in init-graphics-def_. If not set,\n the graphics will not be changed. To remove a graphics device, set this parameter\n to ``{'type': 'none'}``.\n\n :param live:\n ``False`` to avoid trying to live update the definition. In such a case, the\n new definition is applied at the next start of the virtual machine. If ``True``,\n not all aspects of the definition can be live updated, but as much as possible\n will be attempted. (Default: ``True``)\n\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n :return:\n\n Returns a dictionary indicating the status of what has been done. It is structured in\n the following way:\n\n .. code-block:: python\n\n {\n 'definition': True,\n 'cpu': True,\n 'mem': True,\n 'disks': {'attached': [list of actually attached disks],\n 'detached': [list of actually detached disks]},\n 'nics': {'attached': [list of actually attached nics],\n 'detached': [list of actually detached nics]},\n 'errors': ['error messages for failures']\n }\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.update domain cpu=2 mem=1024\n\n '''\n status = {\n 'definition': False,\n 'disk': {'attached': [], 'detached': []},\n 'interface': {'attached': [], 'detached': []}\n }\n conn = __get_conn(**kwargs)\n domain = _get_domain(conn, name)\n desc = ElementTree.fromstring(domain.XMLDesc(0))\n need_update = False\n\n # Compute the XML to get the disks, interfaces and graphics\n hypervisor = desc.get('type')\n all_disks = _disk_profile(disk_profile, hypervisor, disks, name, **kwargs)\n new_desc = ElementTree.fromstring(_gen_xml(name,\n cpu,\n mem,\n all_disks,\n _get_merged_nics(hypervisor, nic_profile, interfaces),\n hypervisor,\n domain.OSType(),\n desc.find('.//os/type').get('arch'),\n graphics,\n **kwargs))\n\n # Update the cpu\n cpu_node = desc.find('vcpu')\n if cpu and int(cpu_node.text) != cpu:\n cpu_node.text = six.text_type(cpu)\n cpu_node.set('current', six.text_type(cpu))\n need_update = True\n\n # Update the memory, note that libvirt outputs all memory sizes in KiB\n for mem_node_name in ['memory', 'currentMemory']:\n mem_node = desc.find(mem_node_name)\n if mem and int(mem_node.text) != mem * 1024:\n mem_node.text = six.text_type(mem)\n mem_node.set('unit', 'MiB')\n need_update = True\n\n # Update the XML definition with the new disks and diff changes\n devices_node = desc.find('devices')\n parameters = {'disk': ['disks', 'disk_profile'],\n 'interface': ['interfaces', 'nic_profile'],\n 'graphics': ['graphics']}\n changes = {}\n for dev_type in parameters:\n changes[dev_type] = {}\n func_locals = locals()\n if [param for param in parameters[dev_type] if func_locals.get(param, None) is not None]:\n old = devices_node.findall(dev_type)\n new = new_desc.findall('devices/{0}'.format(dev_type))\n changes[dev_type] = globals()['_diff_{0}_lists'.format(dev_type)](old, new)\n if changes[dev_type]['deleted'] or changes[dev_type]['new']:\n for item in old:\n devices_node.remove(item)\n devices_node.extend(changes[dev_type]['sorted'])\n need_update = True\n\n # Set the new definition\n if need_update:\n # Create missing disks if needed\n if changes['disk']:\n for idx, item in enumerate(changes['disk']['sorted']):\n source_file = all_disks[idx]['source_file']\n if item in changes['disk']['new'] and source_file and not os.path.isfile(source_file):\n _qemu_image_create(all_disks[idx])\n\n try:\n conn.defineXML(salt.utils.stringutils.to_str(ElementTree.tostring(desc)))\n status['definition'] = True\n except libvirt.libvirtError as err:\n conn.close()\n raise err\n\n # Do the live changes now that we know the definition has been properly set\n # From that point on, failures are not blocking to try to live update as much\n # as possible.\n commands = []\n if domain.isActive() and live:\n if cpu:\n commands.append({'device': 'cpu',\n 'cmd': 'setVcpusFlags',\n 'args': [cpu, libvirt.VIR_DOMAIN_AFFECT_LIVE]})\n if mem:\n commands.append({'device': 'mem',\n 'cmd': 'setMemoryFlags',\n 'args': [mem * 1024, libvirt.VIR_DOMAIN_AFFECT_LIVE]})\n\n for dev_type in ['disk', 'interface']:\n for added in changes[dev_type].get('new', []):\n commands.append({'device': dev_type,\n 'cmd': 'attachDevice',\n 'args': [salt.utils.stringutils.to_str(ElementTree.tostring(added))]})\n\n for removed in changes[dev_type].get('deleted', []):\n commands.append({'device': dev_type,\n 'cmd': 'detachDevice',\n 'args': [salt.utils.stringutils.to_str(ElementTree.tostring(removed))]})\n\n for cmd in commands:\n try:\n ret = getattr(domain, cmd['cmd'])(*cmd['args'])\n device_type = cmd['device']\n if device_type in ['cpu', 'mem']:\n status[device_type] = not bool(ret)\n else:\n actions = {'attachDevice': 'attached', 'detachDevice': 'detached'}\n status[device_type][actions[cmd['cmd']]].append(cmd['args'][0])\n\n except libvirt.libvirtError as err:\n if 'errors' not in status:\n status['errors'] = []\n status['errors'].append(six.text_type(err))\n\n conn.close()\n return status"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_domains(**kwargs):\n '''\n Return a list of available domains.\n\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.list_domains\n '''\n vms = []\n conn = __get_conn(**kwargs)\n for dom in _get_domain(conn, iterable=True):\n vms.append(dom.name())\n conn.close()\n return vms", "response": "Return a list of available domains."} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef list_active_vms(**kwargs):\n '''\n Return a list of names for active virtual machine on the minion\n\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.list_active_vms\n '''\n vms = []\n conn = __get_conn(**kwargs)\n for dom in _get_domain(conn, iterable=True, inactive=False):\n vms.append(dom.name())\n conn.close()\n return vms", "response": "Return a list of names for active virtual machine on the minion"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef list_inactive_vms(**kwargs):\n '''\n Return a list of names for inactive virtual machine on the minion\n\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.list_inactive_vms\n '''\n vms = []\n conn = __get_conn(**kwargs)\n for dom in _get_domain(conn, iterable=True, active=False):\n vms.append(dom.name())\n conn.close()\n return vms", "response": "Return a list of names for inactive virtual machine on the minion"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef vm_info(vm_=None, **kwargs):\n '''\n Return detailed information about the vms on this hyper in a\n list of dicts:\n\n :param vm_: name of the domain\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n .. code-block:: python\n\n [\n 'your-vm': {\n 'cpu': <int>,\n 'maxMem': <int>,\n 'mem': <int>,\n 'state': '<state>',\n 'cputime' <int>\n },\n ...\n ]\n\n If you pass a VM name in as an argument then it will return info\n for just the named VM, otherwise it will return all VMs.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.vm_info\n '''\n def _info(dom):\n '''\n Compute the infos of a domain\n '''\n raw = dom.info()\n return {'cpu': raw[3],\n 'cputime': int(raw[4]),\n 'disks': _get_disks(dom),\n 'graphics': _get_graphics(dom),\n 'nics': _get_nics(dom),\n 'uuid': _get_uuid(dom),\n 'loader': _get_loader(dom),\n 'on_crash': _get_on_crash(dom),\n 'on_reboot': _get_on_reboot(dom),\n 'on_poweroff': _get_on_poweroff(dom),\n 'maxMem': int(raw[1]),\n 'mem': int(raw[2]),\n 'state': VIRT_STATE_NAME_MAP.get(raw[0], 'unknown')}\n info = {}\n conn = __get_conn(**kwargs)\n if vm_:\n info[vm_] = _info(_get_domain(conn, vm_))\n else:\n for domain in _get_domain(conn, iterable=True):\n info[domain.name()] = _info(domain)\n conn.close()\n return info", "response": "Return detailed information about the vms on this hyper in a a\n list of dicts."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef vm_state(vm_=None, **kwargs):\n '''\n Return list of all the vms and their state.\n\n If you pass a VM name in as an argument then it will return info\n for just the named VM, otherwise it will return all VMs.\n\n :param vm_: name of the domain\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.vm_state <domain>\n '''\n def _info(dom):\n '''\n Compute domain state\n '''\n state = ''\n raw = dom.info()\n state = VIRT_STATE_NAME_MAP.get(raw[0], 'unknown')\n return state\n info = {}\n conn = __get_conn(**kwargs)\n if vm_:\n info[vm_] = _info(_get_domain(conn, vm_))\n else:\n for domain in _get_domain(conn, iterable=True):\n info[domain.name()] = _info(domain)\n conn.close()\n return info", "response": "Return list of all the VMs and their state."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _node_info(conn):\n '''\n Internal variant of node_info taking a libvirt connection as parameter\n '''\n raw = conn.getInfo()\n info = {'cpucores': raw[6],\n 'cpumhz': raw[3],\n 'cpumodel': six.text_type(raw[0]),\n 'cpus': raw[2],\n 'cputhreads': raw[7],\n 'numanodes': raw[4],\n 'phymemory': raw[1],\n 'sockets': raw[5]}\n return info", "response": "Internal variant of node_info taking a libvirt connection as parameter\n "} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nreturns a dict with information about this node", "response": "def node_info(**kwargs):\n '''\n Return a dict with information about this node\n\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.node_info\n '''\n conn = __get_conn(**kwargs)\n info = _node_info(conn)\n conn.close()\n return info"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef get_nics(vm_, **kwargs):\n '''\n Return info about the network interfaces of a named vm\n\n :param vm_: name of the domain\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_nics <domain>\n '''\n conn = __get_conn(**kwargs)\n nics = _get_nics(_get_domain(conn, vm_))\n conn.close()\n return nics", "response": "Return info about the network interfaces of a named vm"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef get_macs(vm_, **kwargs):\n '''\n Return a list off MAC addresses from the named vm\n\n :param vm_: name of the domain\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_macs <domain>\n '''\n doc = ElementTree.fromstring(get_xml(vm_, **kwargs))\n return [node.get('address') for node in doc.findall('devices/interface/mac')]", "response": "Return a list off MAC addresses from the named vm\n "} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the graphics information on a given domain", "response": "def get_graphics(vm_, **kwargs):\n '''\n Returns the information on vnc for a given vm\n\n :param vm_: name of the domain\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_graphics <domain>\n '''\n conn = __get_conn(**kwargs)\n graphics = _get_graphics(_get_domain(conn, vm_))\n conn.close()\n return graphics"} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef get_loader(vm_, **kwargs):\n '''\n Returns the information on the loader for a given vm\n\n :param vm_: name of the domain\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_loader <domain>\n\n .. versionadded:: 2019.2.0\n '''\n conn = __get_conn(**kwargs)\n loader = _get_loader(_get_domain(conn, vm_))\n conn.close()\n return loader", "response": "Returns the information on the loader for a given domain"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nreturns the disks of a named vm", "response": "def get_disks(vm_, **kwargs):\n '''\n Return the disks of a named vm\n\n :param vm_: name of the domain\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_disks <domain>\n '''\n conn = __get_conn(**kwargs)\n disks = _get_disks(_get_domain(conn, vm_))\n conn.close()\n return disks"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nsetting the amount of memory allocated to VM.", "response": "def setmem(vm_, memory, config=False, **kwargs):\n '''\n Changes the amount of memory allocated to VM. The VM must be shutdown\n for this to work.\n\n :param vm_: name of the domain\n :param memory: memory amount to set in MB\n :param config: if True then libvirt will be asked to modify the config as well\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.setmem <domain> <size>\n salt '*' virt.setmem my_domain 768\n '''\n conn = __get_conn(**kwargs)\n dom = _get_domain(conn, vm_)\n\n if VIRT_STATE_NAME_MAP.get(dom.info()[0], 'unknown') != 'shutdown':\n return False\n\n # libvirt has a funny bitwise system for the flags in that the flag\n # to affect the \"current\" setting is 0, which means that to set the\n # current setting we have to call it a second time with just 0 set\n flags = libvirt.VIR_DOMAIN_MEM_MAXIMUM\n if config:\n flags = flags | libvirt.VIR_DOMAIN_AFFECT_CONFIG\n\n ret1 = dom.setMemoryFlags(memory * 1024, flags)\n ret2 = dom.setMemoryFlags(memory * 1024, libvirt.VIR_DOMAIN_AFFECT_CURRENT)\n\n conn.close()\n\n # return True if both calls succeeded\n return ret1 == ret2 == 0"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef setvcpus(vm_, vcpus, config=False, **kwargs):\n '''\n Changes the amount of vcpus allocated to VM. The VM must be shutdown\n for this to work.\n\n If config is True then we ask libvirt to modify the config as well\n\n :param vm_: name of the domain\n :param vcpus: integer representing the number of CPUs to be assigned\n :param config: if True then libvirt will be asked to modify the config as well\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.setvcpus <domain> <amount>\n salt '*' virt.setvcpus my_domain 4\n '''\n conn = __get_conn(**kwargs)\n dom = _get_domain(conn, vm_)\n\n if VIRT_STATE_NAME_MAP.get(dom.info()[0], 'unknown') != 'shutdown':\n return False\n\n # see notes in setmem\n flags = libvirt.VIR_DOMAIN_VCPU_MAXIMUM\n if config:\n flags = flags | libvirt.VIR_DOMAIN_AFFECT_CONFIG\n\n ret1 = dom.setVcpusFlags(vcpus, flags)\n ret2 = dom.setVcpusFlags(vcpus, libvirt.VIR_DOMAIN_AFFECT_CURRENT)\n\n conn.close()\n\n return ret1 == ret2 == 0", "response": "Sets the amount of vcpus allocated to a VM."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef _freemem(conn):\n '''\n Internal variant of freemem taking a libvirt connection as parameter\n '''\n mem = conn.getInfo()[1]\n # Take off just enough to sustain the hypervisor\n mem -= 256\n for dom in _get_domain(conn, iterable=True):\n if dom.ID() > 0:\n mem -= dom.info()[2] / 1024\n return mem", "response": "Internal variant of freemem taking a libvirt connection as parameter\n "} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef freemem(**kwargs):\n '''\n Return an int representing the amount of memory (in MB) that has not\n been given to virtual machines on this node\n\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.freemem\n '''\n conn = __get_conn(**kwargs)\n mem = _freemem(conn)\n conn.close()\n return mem", "response": "Return the amount of memory that has not been given to virtual machines on this node."} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _freecpu(conn):\n '''\n Internal variant of freecpu taking a libvirt connection as parameter\n '''\n cpus = conn.getInfo()[2]\n for dom in _get_domain(conn, iterable=True):\n if dom.ID() > 0:\n cpus -= dom.info()[3]\n return cpus", "response": "Internal variant of freecpu taking a libvirt connection as parameter\n "} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef freecpu(**kwargs):\n '''\n Return an int representing the number of unallocated cpus on this\n hypervisor\n\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.freecpu\n '''\n conn = __get_conn(**kwargs)\n cpus = _freecpu(conn)\n conn.close()\n return cpus", "response": "Return an integer representing the number of unallocated cpus on this hypervisor."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nreturns the node_info vm_info and freemem_info.", "response": "def full_info(**kwargs):\n '''\n Return the node_info, vm_info and freemem\n\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.full_info\n '''\n conn = __get_conn(**kwargs)\n info = {'freecpu': _freecpu(conn),\n 'freemem': _freemem(conn),\n 'node_info': _node_info(conn),\n 'vm_info': vm_info()}\n conn.close()\n return info"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_xml(vm_, **kwargs):\n '''\n Returns the XML for a given vm\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_xml <domain>\n '''\n conn = __get_conn(**kwargs)\n xml_desc = _get_domain(conn, vm_).XMLDesc(0)\n conn.close()\n return xml_desc", "response": "Returns the XML for a given domain name"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the virt profiles for the given hypervisor.", "response": "def get_profiles(hypervisor=None, **kwargs):\n '''\n Return the virt profiles for hypervisor.\n\n Currently there are profiles for:\n\n - nic\n - disk\n\n :param hypervisor: override the default machine type.\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_profiles\n salt '*' virt.get_profiles hypervisor=esxi\n '''\n ret = {}\n\n caps = capabilities(**kwargs)\n hypervisors = sorted({x for y in [guest['arch']['domains'].keys() for guest in caps['guests']] for x in y})\n default_hypervisor = 'kvm' if 'kvm' in hypervisors else hypervisors[0]\n\n if not hypervisor:\n hypervisor = __salt__['config.get']('libvirt:hypervisor')\n if hypervisor is not None:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'libvirt:hypervisor\\' configuration property has been deprecated. '\n 'Rather use the \\'virt:connection:uri\\' to properly define the libvirt '\n 'URI or alias of the host to connect to. \\'libvirt:hypervisor\\' will '\n 'stop being used in {version}.'\n )\n else:\n # Use the machine types as possible values\n # Prefer 'kvm' over the others if available\n hypervisor = default_hypervisor\n virtconf = __salt__['config.get']('virt', {})\n for typ in ['disk', 'nic']:\n _func = getattr(sys.modules[__name__], '_{0}_profile'.format(typ))\n ret[typ] = {'default': _func('default', hypervisor)}\n if typ in virtconf:\n ret.setdefault(typ, {})\n for prf in virtconf[typ]:\n ret[typ][prf] = _func(prf, hypervisor)\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\npausing the named vm", "response": "def pause(vm_, **kwargs):\n '''\n Pause the named vm\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.pause <domain>\n '''\n conn = __get_conn(**kwargs)\n dom = _get_domain(conn, vm_)\n ret = dom.suspend() == 0\n conn.close()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef start(name, **kwargs):\n '''\n Start a defined domain\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.start <domain>\n '''\n conn = __get_conn(**kwargs)\n ret = _get_domain(conn, name).create() == 0\n conn.close()\n return ret", "response": "Start a libvirt domain"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nstops a virtual machine.", "response": "def stop(name, **kwargs):\n '''\n Hard power down the virtual machine, this is equivalent to pulling the power.\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.stop <domain>\n '''\n conn = __get_conn(**kwargs)\n ret = _get_domain(conn, name).destroy() == 0\n conn.close()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef reboot(name, **kwargs):\n '''\n Reboot a domain via ACPI request\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.reboot <domain>\n '''\n conn = __get_conn(**kwargs)\n ret = _get_domain(conn, name).reboot(libvirt.VIR_DOMAIN_REBOOT_DEFAULT) == 0\n conn.close()\n return ret", "response": "Reboot a domain via ACPI request"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef reset(vm_, **kwargs):\n '''\n Reset a VM by emulating the reset button on a physical machine\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.reset <domain>\n '''\n conn = __get_conn(**kwargs)\n dom = _get_domain(conn, vm_)\n\n # reset takes a flag, like reboot, but it is not yet used\n # so we just pass in 0\n # see: http://libvirt.org/html/libvirt-libvirt.html#virDomainReset\n ret = dom.reset(0) == 0\n conn.close()\n return ret", "response": "Reset a virtual machine on a physical machine"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef ctrl_alt_del(vm_, **kwargs):\n '''\n Sends CTRL+ALT+DEL to a VM\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.ctrl_alt_del <domain>\n '''\n conn = __get_conn(**kwargs)\n dom = _get_domain(conn, vm_)\n ret = dom.sendKey(0, 0, [29, 56, 111], 3, 0) == 0\n conn.close()\n return ret", "response": "Send a CTRL + ALT + DEL to a VM"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef create_xml_str(xml, **kwargs): # pylint: disable=redefined-outer-name\n '''\n Start a transient domain based on the XML passed to the function\n\n :param xml: libvirt XML definition of the domain\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.create_xml_str <XML in string format>\n '''\n conn = __get_conn(**kwargs)\n ret = conn.createXML(xml, 0) is not None\n conn.close()\n return ret", "response": "Create a transient domain based on the XML passed to the function\n "} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ncreating a transient domain based on the XML - file at the given path.", "response": "def create_xml_path(path, **kwargs):\n '''\n Start a transient domain based on the XML-file path passed to the function\n\n :param path: path to a file containing the libvirt XML definition of the domain\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.create_xml_path <path to XML file on the node>\n '''\n try:\n with salt.utils.files.fopen(path, 'r') as fp_:\n return create_xml_str(\n salt.utils.stringutils.to_unicode(fp_.read()),\n **kwargs\n )\n except (OSError, IOError):\n return False"} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\ndefines a persistent domain based on the XML passed to the function", "response": "def define_xml_str(xml, **kwargs): # pylint: disable=redefined-outer-name\n '''\n Define a persistent domain based on the XML passed to the function\n\n :param xml: libvirt XML definition of the domain\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.define_xml_str <XML in string format>\n '''\n conn = __get_conn(**kwargs)\n ret = conn.defineXML(xml) is not None\n conn.close()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef define_xml_path(path, **kwargs):\n '''\n Define a persistent domain based on the XML-file path passed to the function\n\n :param path: path to a file containing the libvirt XML definition of the domain\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.define_xml_path <path to XML file on the node>\n\n '''\n try:\n with salt.utils.files.fopen(path, 'r') as fp_:\n return define_xml_str(\n salt.utils.stringutils.to_unicode(fp_.read()),\n **kwargs\n )\n except (OSError, IOError):\n return False", "response": "This function will define a persistent domain based on the XML - file path passed to the function\n ArcGIS"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\ndefining a libvirt storage volume based on the XML passed to the function", "response": "def define_vol_xml_str(xml, **kwargs): # pylint: disable=redefined-outer-name\n '''\n Define a volume based on the XML passed to the function\n\n :param xml: libvirt XML definition of the storage volume\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.define_vol_xml_str <XML in string format>\n\n The storage pool where the disk image will be defined is ``default``\n unless changed with a configuration like this:\n\n .. code-block:: yaml\n\n virt:\n storagepool: mine\n '''\n poolname = __salt__['config.get']('libvirt:storagepool', None)\n if poolname is not None:\n salt.utils.versions.warn_until(\n 'Sodium',\n '\\'libvirt:storagepool\\' has been deprecated in favor of '\n '\\'virt:storagepool\\'. \\'libvirt:storagepool\\' will stop '\n 'being used in {version}.'\n )\n else:\n poolname = __salt__['config.get']('virt:storagepool', 'default')\n\n conn = __get_conn(**kwargs)\n pool = conn.storagePoolLookupByName(six.text_type(poolname))\n ret = pool.createXML(xml, 0) is not None\n conn.close()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef define_vol_xml_path(path, **kwargs):\n '''\n Define a volume based on the XML-file path passed to the function\n\n :param path: path to a file containing the libvirt XML definition of the volume\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.define_vol_xml_path <path to XML file on the node>\n\n '''\n try:\n with salt.utils.files.fopen(path, 'r') as fp_:\n return define_vol_xml_str(\n salt.utils.stringutils.to_unicode(fp_.read()),\n **kwargs\n )\n except (OSError, IOError):\n return False", "response": "This function will define a libvirt XML file on the node with the given path."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef migrate_non_shared(vm_, target, ssh=False):\n '''\n Attempt to execute non-shared storage \"all\" migration\n\n :param vm_: domain name\n :param target: target libvirt host name\n :param ssh: True to connect over ssh\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.migrate_non_shared <vm name> <target hypervisor>\n\n A tunnel data migration can be performed by setting this in the\n configuration:\n\n .. code-block:: yaml\n\n virt:\n tunnel: True\n\n For more details on tunnelled data migrations, report to\n https://libvirt.org/migration.html#transporttunnel\n '''\n cmd = _get_migrate_command() + ' --copy-storage-all ' + vm_\\\n + _get_target(target, ssh)\n\n stdout = subprocess.Popen(cmd,\n shell=True,\n stdout=subprocess.PIPE).communicate()[0]\n return salt.utils.stringutils.to_str(stdout)", "response": "Execute a migration on the non - shared storage."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nseed non shared migration on the specified disks.", "response": "def seed_non_shared_migrate(disks, force=False):\n '''\n Non shared migration requires that the disks be present on the migration\n destination, pass the disks information via this function, to the\n migration destination before executing the migration.\n\n :param disks: the list of disk data as provided by virt.get_disks\n :param force: skip checking the compatibility of source and target disk\n images if True. (default: False)\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.seed_non_shared_migrate <disks>\n '''\n for _, data in six.iteritems(disks):\n fn_ = data['file']\n form = data['file format']\n size = data['virtual size'].split()[1][1:]\n if os.path.isfile(fn_) and not force:\n # the target exists, check to see if it is compatible\n pre = salt.utils.yaml.safe_load(subprocess.Popen('qemu-img info arch',\n shell=True,\n stdout=subprocess.PIPE).communicate()[0])\n if pre['file format'] != data['file format']\\\n and pre['virtual size'] != data['virtual size']:\n return False\n if not os.path.isdir(os.path.dirname(fn_)):\n os.makedirs(os.path.dirname(fn_))\n if os.path.isfile(fn_):\n os.remove(fn_)\n cmd = 'qemu-img create -f ' + form + ' ' + fn_ + ' ' + size\n subprocess.call(cmd, shell=True)\n creds = _libvirt_creds()\n cmd = 'chown ' + creds['user'] + ':' + creds['group'] + ' ' + fn_\n subprocess.call(cmd, shell=True)\n return True"} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nset the autostart flag on a VM.", "response": "def set_autostart(vm_, state='on', **kwargs):\n '''\n Set the autostart flag on a VM so that the VM will start with the host\n system on reboot.\n\n :param vm_: domain name\n :param state: 'on' to auto start the pool, anything else to mark the\n pool not to be started when the host boots\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt \"*\" virt.set_autostart <domain> <on | off>\n '''\n conn = __get_conn(**kwargs)\n dom = _get_domain(conn, vm_)\n\n # return False if state is set to something other then on or off\n ret = False\n\n if state == 'on':\n ret = dom.setAutostart(1) == 0\n\n elif state == 'off':\n ret = dom.setAutostart(0) == 0\n\n conn.close()\n return ret"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef undefine(vm_, **kwargs):\n '''\n Remove a defined vm, this does not purge the virtual machine image, and\n this only works if the vm is powered down\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.undefine <domain>\n '''\n conn = __get_conn(**kwargs)\n dom = _get_domain(conn, vm_)\n if getattr(libvirt, 'VIR_DOMAIN_UNDEFINE_NVRAM', False):\n # This one is only in 1.2.8+\n ret = dom.undefineFlags(libvirt.VIR_DOMAIN_UNDEFINE_NVRAM) == 0\n else:\n ret = dom.undefine() == 0\n conn.close()\n return ret", "response": "Remove a defined vm from the virtual machine image and return True if the vm is powered down."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nreturning a bool whether or not this node is a KVM hypervisor", "response": "def _is_kvm_hyper():\n '''\n Returns a bool whether or not this node is a KVM hypervisor\n '''\n try:\n with salt.utils.files.fopen('/proc/modules') as fp_:\n if 'kvm_' not in salt.utils.stringutils.to_unicode(fp_.read()):\n return False\n except IOError:\n # No /proc/modules? Are we on Windows? Or Solaris?\n return False\n return 'libvirtd' in __salt__['cmd.run'](__grains__['ps'])"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef get_hypervisor():\n '''\n Returns the name of the hypervisor running on this node or ``None``.\n\n Detected hypervisors:\n\n - kvm\n - xen\n - bhyve\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.get_hypervisor\n\n .. versionadded:: 2019.2.0\n the function and the ``kvm``, ``xen`` and ``bhyve`` hypervisors support\n '''\n # To add a new 'foo' hypervisor, add the _is_foo_hyper function,\n # add 'foo' to the list below and add it to the docstring with a .. versionadded::\n hypervisors = ['kvm', 'xen', 'bhyve']\n result = [hyper for hyper in hypervisors if getattr(sys.modules[__name__], '_is_{}_hyper'.format(hyper))()]\n return result[0] if result else None", "response": "Returns the name of the hypervisor running on this node or None if no hypervisor is running on this node."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _is_bhyve_hyper():\n '''\n Returns a bool whether or not this node is a bhyve hypervisor\n '''\n sysctl_cmd = 'sysctl hw.vmm.create'\n vmm_enabled = False\n try:\n stdout = subprocess.Popen(sysctl_cmd,\n shell=True,\n stdout=subprocess.PIPE).communicate()[0]\n vmm_enabled = len(salt.utils.stringutils.to_str(stdout).split('\"')[1]) != 0\n except IndexError:\n pass\n return vmm_enabled", "response": "Returns a bool whether or not this node is a bhyve hypervisor"} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef vm_cputime(vm_=None, **kwargs):\n '''\n Return cputime used by the vms on this hyper in a\n list of dicts:\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n .. code-block:: python\n\n [\n 'your-vm': {\n 'cputime' <int>\n 'cputime_percent' <int>\n },\n ...\n ]\n\n If you pass a VM name in as an argument then it will return info\n for just the named VM, otherwise it will return all VMs.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.vm_cputime\n '''\n conn = __get_conn(**kwargs)\n host_cpus = conn.getInfo()[2]\n\n def _info(dom):\n '''\n Compute cputime info of a domain\n '''\n raw = dom.info()\n vcpus = int(raw[3])\n cputime = int(raw[4])\n cputime_percent = 0\n if cputime:\n # Divide by vcpus to always return a number between 0 and 100\n cputime_percent = (1.0e-7 * cputime / host_cpus) / vcpus\n return {\n 'cputime': int(raw[4]),\n 'cputime_percent': int('{0:.0f}'.format(cputime_percent))\n }\n info = {}\n if vm_:\n info[vm_] = _info(_get_domain(conn, vm_))\n else:\n for domain in _get_domain(conn, iterable=True):\n info[domain.name()] = _info(domain)\n conn.close()\n return info", "response": "Return a list of dicts that represents the cputime used by the vms on this hyper."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef vm_netstats(vm_=None, **kwargs):\n '''\n Return combined network counters used by the vms on this hyper in a\n list of dicts:\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n .. code-block:: python\n\n [\n 'your-vm': {\n 'rx_bytes' : 0,\n 'rx_packets' : 0,\n 'rx_errs' : 0,\n 'rx_drop' : 0,\n 'tx_bytes' : 0,\n 'tx_packets' : 0,\n 'tx_errs' : 0,\n 'tx_drop' : 0\n },\n ...\n ]\n\n If you pass a VM name in as an argument then it will return info\n for just the named VM, otherwise it will return all VMs.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.vm_netstats\n '''\n def _info(dom):\n '''\n Compute network stats of a domain\n '''\n nics = _get_nics(dom)\n ret = {\n 'rx_bytes': 0,\n 'rx_packets': 0,\n 'rx_errs': 0,\n 'rx_drop': 0,\n 'tx_bytes': 0,\n 'tx_packets': 0,\n 'tx_errs': 0,\n 'tx_drop': 0\n }\n for attrs in six.itervalues(nics):\n if 'target' in attrs:\n dev = attrs['target']\n stats = dom.interfaceStats(dev)\n ret['rx_bytes'] += stats[0]\n ret['rx_packets'] += stats[1]\n ret['rx_errs'] += stats[2]\n ret['rx_drop'] += stats[3]\n ret['tx_bytes'] += stats[4]\n ret['tx_packets'] += stats[5]\n ret['tx_errs'] += stats[6]\n ret['tx_drop'] += stats[7]\n\n return ret\n info = {}\n conn = __get_conn(**kwargs)\n if vm_:\n info[vm_] = _info(_get_domain(conn, vm_))\n else:\n for domain in _get_domain(conn, iterable=True):\n info[domain.name()] = _info(domain)\n conn.close()\n return info", "response": "Return combined network counters used by the vms on this hyper."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef vm_diskstats(vm_=None, **kwargs):\n '''\n Return disk usage counters used by the vms on this hyper in a\n list of dicts:\n\n :param vm_: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n .. code-block:: python\n\n [\n 'your-vm': {\n 'rd_req' : 0,\n 'rd_bytes' : 0,\n 'wr_req' : 0,\n 'wr_bytes' : 0,\n 'errs' : 0\n },\n ...\n ]\n\n If you pass a VM name in as an argument then it will return info\n for just the named VM, otherwise it will return all VMs.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.vm_blockstats\n '''\n def get_disk_devs(dom):\n '''\n Extract the disk devices names from the domain XML definition\n '''\n doc = ElementTree.fromstring(get_xml(dom, **kwargs))\n return [target.get('dev') for target in doc.findall('devices/disk/target')]\n\n def _info(dom):\n '''\n Compute the disk stats of a domain\n '''\n # Do not use get_disks, since it uses qemu-img and is very slow\n # and unsuitable for any sort of real time statistics\n disks = get_disk_devs(dom)\n ret = {'rd_req': 0,\n 'rd_bytes': 0,\n 'wr_req': 0,\n 'wr_bytes': 0,\n 'errs': 0\n }\n for disk in disks:\n stats = dom.blockStats(disk)\n ret['rd_req'] += stats[0]\n ret['rd_bytes'] += stats[1]\n ret['wr_req'] += stats[2]\n ret['wr_bytes'] += stats[3]\n ret['errs'] += stats[4]\n\n return ret\n info = {}\n conn = __get_conn(**kwargs)\n if vm_:\n info[vm_] = _info(_get_domain(conn, vm_))\n else:\n # Can not run function blockStats on inactive VMs\n for domain in _get_domain(conn, iterable=True, inactive=False):\n info[domain.name()] = _info(domain)\n conn.close()\n return info", "response": "Return the disk usage counters for the vms on this hyper."} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _parse_snapshot_description(vm_snapshot, unix_time=False):\n '''\n Parse XML doc and return a dict with the status values.\n\n :param xmldoc:\n :return:\n '''\n ret = dict()\n tree = ElementTree.fromstring(vm_snapshot.getXMLDesc())\n for node in tree:\n if node.tag == 'name':\n ret['name'] = node.text\n elif node.tag == 'creationTime':\n ret['created'] = datetime.datetime.fromtimestamp(float(node.text)).isoformat(' ') \\\n if not unix_time else float(node.text)\n elif node.tag == 'state':\n ret['running'] = node.text == 'running'\n\n ret['current'] = vm_snapshot.isCurrent() == 1\n\n return ret", "response": "Parse the snapshot description and return a dict with the status values."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef list_snapshots(domain=None, **kwargs):\n '''\n List available snapshots for certain vm or for all.\n\n :param domain: domain name\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.list_snapshots\n salt '*' virt.list_snapshots <domain>\n '''\n ret = dict()\n conn = __get_conn(**kwargs)\n for vm_domain in _get_domain(conn, *(domain and [domain] or list()), iterable=True):\n ret[vm_domain.name()] = [_parse_snapshot_description(snap) for snap in vm_domain.listAllSnapshots()] or 'N/A'\n\n conn.close()\n return ret", "response": "List available snapshots for a certain vm or for all."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\ncreating a snapshot of a virtual machine.", "response": "def snapshot(domain, name=None, suffix=None, **kwargs):\n '''\n Create a snapshot of a VM.\n\n :param domain: domain name\n :param name: Name of the snapshot. If the name is omitted, then will be used original domain\n name with ISO 8601 time as a suffix.\n\n :param suffix: Add suffix for the new name. Useful in states, where such snapshots\n can be distinguished from manually created.\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.snapshot <domain>\n '''\n if name and name.lower() == domain.lower():\n raise CommandExecutionError('Virtual Machine {name} is already defined. '\n 'Please choose another name for the snapshot'.format(name=name))\n if not name:\n name = \"{domain}-{tsnap}\".format(domain=domain, tsnap=time.strftime('%Y%m%d-%H%M%S', time.localtime()))\n\n if suffix:\n name = \"{name}-{suffix}\".format(name=name, suffix=suffix)\n\n doc = ElementTree.Element('domainsnapshot')\n n_name = ElementTree.SubElement(doc, 'name')\n n_name.text = name\n\n conn = __get_conn(**kwargs)\n _get_domain(conn, domain).snapshotCreateXML(\n salt.utils.stringutils.to_str(ElementTree.tostring(doc))\n )\n conn.close()\n\n return {'name': name}"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef delete_snapshots(name, *names, **kwargs):\n '''\n Delete one or more snapshots of the given VM.\n\n :param name: domain name\n :param names: names of the snapshots to remove\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.delete_snapshots <domain> all=True\n salt '*' virt.delete_snapshots <domain> <snapshot>\n salt '*' virt.delete_snapshots <domain> <snapshot1> <snapshot2> ...\n '''\n deleted = dict()\n conn = __get_conn(**kwargs)\n domain = _get_domain(conn, name)\n for snap in domain.listAllSnapshots():\n if snap.getName() in names or not names:\n deleted[snap.getName()] = _parse_snapshot_description(snap)\n snap.delete()\n conn.close()\n\n available = {name: [_parse_snapshot_description(snap) for snap in domain.listAllSnapshots()] or 'N/A'}\n\n return {'available': available, 'deleted': deleted}", "response": "Delete one or more snapshots of the given VM."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef revert_snapshot(name, vm_snapshot=None, cleanup=False, **kwargs):\n '''\n Revert snapshot to the previous from current (if available) or to the specific.\n\n :param name: domain name\n :param vm_snapshot: name of the snapshot to revert\n :param cleanup: Remove all newer than reverted snapshots. Values: True or False (default False).\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n .. versionadded:: 2016.3.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.revert <domain>\n salt '*' virt.revert <domain> <snapshot>\n '''\n ret = dict()\n conn = __get_conn(**kwargs)\n domain = _get_domain(conn, name)\n snapshots = domain.listAllSnapshots()\n\n _snapshots = list()\n for snap_obj in snapshots:\n _snapshots.append({'idx': _parse_snapshot_description(snap_obj, unix_time=True)['created'], 'ptr': snap_obj})\n snapshots = [w_ptr['ptr'] for w_ptr in sorted(_snapshots, key=lambda item: item['idx'], reverse=True)]\n del _snapshots\n\n if not snapshots:\n conn.close()\n raise CommandExecutionError('No snapshots found')\n elif len(snapshots) == 1:\n conn.close()\n raise CommandExecutionError('Cannot revert to itself: only one snapshot is available.')\n\n snap = None\n for p_snap in snapshots:\n if not vm_snapshot:\n if p_snap.isCurrent() and snapshots[snapshots.index(p_snap) + 1:]:\n snap = snapshots[snapshots.index(p_snap) + 1:][0]\n break\n elif p_snap.getName() == vm_snapshot:\n snap = p_snap\n break\n\n if not snap:\n conn.close()\n raise CommandExecutionError(\n snapshot and 'Snapshot \"{0}\" not found'.format(vm_snapshot) or 'No more previous snapshots available')\n elif snap.isCurrent():\n conn.close()\n raise CommandExecutionError('Cannot revert to the currently running snapshot.')\n\n domain.revertToSnapshot(snap)\n ret['reverted'] = snap.getName()\n\n if cleanup:\n delete = list()\n for p_snap in snapshots:\n if p_snap.getName() != snap.getName():\n delete.append(p_snap.getName())\n p_snap.delete()\n else:\n break\n ret['deleted'] = delete\n else:\n ret['deleted'] = 'N/A'\n\n conn.close()\n\n return ret", "response": "Revert a snapshot to the previous from current or to specific."} {"SOURCE": "codesearchnet", "instruction": "How would you implement a function in Python 3 that\nparses the host capabilities element and add it to the machines list.", "response": "def _caps_add_machine(machines, node):\n '''\n Parse the <machine> element of the host capabilities and add it\n to the machines list.\n '''\n maxcpus = node.get('maxCpus')\n canonical = node.get('canonical')\n name = node.text\n\n alternate_name = \"\"\n if canonical:\n alternate_name = name\n name = canonical\n\n machine = machines.get(name)\n if not machine:\n machine = {'alternate_names': []}\n if maxcpus:\n machine['maxcpus'] = int(maxcpus)\n machines[name] = machine\n if alternate_name:\n machine['alternate_names'].append(alternate_name)"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef _parse_caps_guest(guest):\n '''\n Parse the <guest> element of the connection capabilities XML\n '''\n arch_node = guest.find('arch')\n result = {\n 'os_type': guest.find('os_type').text,\n 'arch': {\n 'name': arch_node.get('name'),\n 'machines': {},\n 'domains': {}\n },\n }\n\n for child in arch_node:\n if child.tag == 'wordsize':\n result['arch']['wordsize'] = int(child.text)\n elif child.tag == 'emulator':\n result['arch']['emulator'] = child.text\n elif child.tag == 'machine':\n _caps_add_machine(result['arch']['machines'], child)\n elif child.tag == 'domain':\n domain_type = child.get('type')\n domain = {\n 'emulator': None,\n 'machines': {}\n }\n emulator_node = child.find('emulator')\n if emulator_node is not None:\n domain['emulator'] = emulator_node.text\n for machine in child.findall('machine'):\n _caps_add_machine(domain['machines'], machine)\n result['arch']['domains'][domain_type] = domain\n\n # Note that some features have no default and toggle attributes.\n # This may not be a perfect match, but represent them as enabled by default\n # without possibility to toggle them.\n # Some guests may also have no feature at all (xen pv for instance)\n features_nodes = guest.find('features')\n if features_nodes is not None:\n result['features'] = {child.tag: {'toggle': True if child.get('toggle') == 'yes' else False,\n 'default': True if child.get('default') == 'no' else True}\n for child in features_nodes}\n\n return result", "response": "Parses the guest element of the connection capabilities XML\n "} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef _parse_caps_cell(cell):\n '''\n Parse the <cell> nodes of the connection capabilities XML output.\n '''\n result = {\n 'id': int(cell.get('id'))\n }\n\n mem_node = cell.find('memory')\n if mem_node is not None:\n unit = mem_node.get('unit', 'KiB')\n memory = mem_node.text\n result['memory'] = \"{} {}\".format(memory, unit)\n\n pages = [{'size': \"{} {}\".format(page.get('size'), page.get('unit', 'KiB')),\n 'available': int(page.text)}\n for page in cell.findall('pages')]\n if pages:\n result['pages'] = pages\n\n distances = {int(distance.get('id')): int(distance.get('value'))\n for distance in cell.findall('distances/sibling')}\n if distances:\n result['distances'] = distances\n\n cpus = []\n for cpu_node in cell.findall('cpus/cpu'):\n cpu = {\n 'id': int(cpu_node.get('id'))\n }\n socket_id = cpu_node.get('socket_id')\n if socket_id:\n cpu['socket_id'] = int(socket_id)\n\n core_id = cpu_node.get('core_id')\n if core_id:\n cpu['core_id'] = int(core_id)\n siblings = cpu_node.get('siblings')\n if siblings:\n cpu['siblings'] = siblings\n cpus.append(cpu)\n if cpus:\n result['cpus'] = cpus\n\n return result", "response": "Parse the nodes of the connection capabilities XML output."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function for\nparsing the bank element of the connection capabilities XML.", "response": "def _parse_caps_bank(bank):\n '''\n Parse the <bank> element of the connection capabilities XML.\n '''\n result = {\n 'id': int(bank.get('id')),\n 'level': int(bank.get('level')),\n 'type': bank.get('type'),\n 'size': \"{} {}\".format(bank.get('size'), bank.get('unit')),\n 'cpus': bank.get('cpus')\n }\n\n controls = []\n for control in bank.findall('control'):\n unit = control.get('unit')\n result_control = {\n 'granularity': \"{} {}\".format(control.get('granularity'), unit),\n 'type': control.get('type'),\n 'maxAllocs': int(control.get('maxAllocs'))\n }\n\n minimum = control.get('min')\n if minimum:\n result_control['min'] = \"{} {}\".format(minimum, unit)\n controls.append(result_control)\n if controls:\n result['controls'] = controls\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nparse the XML of the connection capabilities XML.", "response": "def _parse_caps_host(host):\n '''\n Parse the <host> element of the connection capabilities XML.\n '''\n result = {}\n for child in host:\n\n if child.tag == 'uuid':\n result['uuid'] = child.text\n\n elif child.tag == 'cpu':\n cpu = {\n 'arch': child.find('arch').text if child.find('arch') is not None else None,\n 'model': child.find('model').text if child.find('model') is not None else None,\n 'vendor': child.find('vendor').text if child.find('vendor') is not None else None,\n 'features': [feature.get('name') for feature in child.findall('feature')],\n 'pages': [{'size': '{} {}'.format(page.get('size'), page.get('unit', 'KiB'))}\n for page in child.findall('pages')]\n }\n # Parse the cpu tag\n microcode = child.find('microcode')\n if microcode is not None:\n cpu['microcode'] = microcode.get('version')\n\n topology = child.find('topology')\n if topology is not None:\n cpu['sockets'] = int(topology.get('sockets'))\n cpu['cores'] = int(topology.get('cores'))\n cpu['threads'] = int(topology.get('threads'))\n result['cpu'] = cpu\n\n elif child.tag == \"power_management\":\n result['power_management'] = [node.tag for node in child]\n\n elif child.tag == \"migration_features\":\n result['migration'] = {\n 'live': child.find('live') is not None,\n 'transports': [node.text for node in child.findall('uri_transports/uri_transport')]\n }\n\n elif child.tag == \"topology\":\n result['topology'] = {\n 'cells': [_parse_caps_cell(cell) for cell in child.findall('cells/cell')]\n }\n\n elif child.tag == 'cache':\n result['cache'] = {\n 'banks': [_parse_caps_bank(bank) for bank in child.findall('bank')]\n }\n\n result['security'] = [{\n 'model': secmodel.find('model').text if secmodel.find('model') is not None else None,\n 'doi': secmodel.find('doi').text if secmodel.find('doi') is not None else None,\n 'baselabels': [{'type': label.get('type'), 'label': label.text}\n for label in secmodel.findall('baselabel')]\n }\n for secmodel in host.findall('secmodel')]\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef capabilities(**kwargs):\n '''\n Return the hypervisor connection capabilities.\n\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.capabilities\n '''\n conn = __get_conn(**kwargs)\n caps = ElementTree.fromstring(conn.getCapabilities())\n conn.close()\n\n return {\n 'host': _parse_caps_host(caps.find('host')),\n 'guests': [_parse_caps_guest(guest) for guest in caps.findall('guest')]\n }", "response": "Return the hypervisor connection capabilities."} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef _parse_caps_cpu(node):\n '''\n Parse the <cpu> element of the domain capabilities\n '''\n result = {}\n for mode in node.findall('mode'):\n if not mode.get('supported') == 'yes':\n continue\n\n name = mode.get('name')\n if name == 'host-passthrough':\n result[name] = True\n\n elif name == 'host-model':\n host_model = {}\n model_node = mode.find('model')\n if model_node is not None:\n model = {\n 'name': model_node.text\n }\n\n vendor_id = model_node.get('vendor_id')\n if vendor_id:\n model['vendor_id'] = vendor_id\n\n fallback = model_node.get('fallback')\n if fallback:\n model['fallback'] = fallback\n host_model['model'] = model\n\n vendor = mode.find('vendor').text if mode.find('vendor') is not None else None\n if vendor:\n host_model['vendor'] = vendor\n\n features = {feature.get('name'): feature.get('policy') for feature in mode.findall('feature')}\n if features:\n host_model['features'] = features\n\n result[name] = host_model\n\n elif name == 'custom':\n custom_model = {}\n models = {model.text: model.get('usable') for model in mode.findall('model')}\n if models:\n custom_model['models'] = models\n result[name] = custom_model\n\n return result", "response": "Parse the <cpu > element of the domain capabilities\n "} {"SOURCE": "codesearchnet", "instruction": "Can you implement a function in Python 3 that\nparses the devices or features list of the domain capatilities", "response": "def _parse_caps_devices_features(node):\n '''\n Parse the devices or features list of the domain capatilities\n '''\n result = {}\n for child in node:\n if child.get('supported') == 'yes':\n enums = [_parse_caps_enum(node) for node in child.findall('enum')]\n result[child.tag] = {item[0]: item[1] for item in enums if item[0]}\n return result"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef _parse_caps_loader(node):\n '''\n Parse the <loader> element of the domain capabilities.\n '''\n enums = [_parse_caps_enum(enum) for enum in node.findall('enum')]\n result = {item[0]: item[1] for item in enums if item[0]}\n\n values = [child.text for child in node.findall('value')]\n\n if values:\n result['values'] = values\n\n return result", "response": "Parse the loader element of the domain capabilities."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script for\nparsing the XML document of domain capabilities into a structure.", "response": "def _parse_domain_caps(caps):\n '''\n Parse the XML document of domain capabilities into a structure.\n Return the domain capabilities given an emulator, architecture, machine or virtualization type.\n\n .. versionadded:: 2019.2.0\n\n :param emulator: return the capabilities for the given emulator binary\n :param arch: return the capabilities for the given CPU architecture\n :param machine: return the capabilities for the given emulated machine type\n :param domain: return the capabilities for the given virtualization type.\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n The list of the possible emulator, arch, machine and domain can be found in\n the host capabilities output.\n\n If none of the parameters is provided the libvirt default domain capabilities\n will be returned.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.domain_capabilities arch='x86_64' domain='kvm'\n '''\n result = {\n 'emulator': caps.find('path').text if caps.find('path') is not None else None,\n 'domain': caps.find('domain').text if caps.find('domain') is not None else None,\n 'machine': caps.find('machine').text if caps.find('machine') is not None else None,\n 'arch': caps.find('arch').text if caps.find('arch') is not None else None\n }\n\n for child in caps:\n if child.tag == 'vcpu' and child.get('max'):\n result['max_vcpus'] = int(child.get('max'))\n\n elif child.tag == 'iothreads':\n result['iothreads'] = child.get('supported') == 'yes'\n\n elif child.tag == 'os':\n result['os'] = {}\n loader_node = child.find('loader')\n if loader_node is not None and loader_node.get('supported') == 'yes':\n loader = _parse_caps_loader(loader_node)\n result['os']['loader'] = loader\n\n elif child.tag == 'cpu':\n cpu = _parse_caps_cpu(child)\n if cpu:\n result['cpu'] = cpu\n\n elif child.tag == 'devices':\n devices = _parse_caps_devices_features(child)\n if devices:\n result['devices'] = devices\n\n elif child.tag == 'features':\n features = _parse_caps_devices_features(child)\n if features:\n result['features'] = features\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function for\nreturning the domain capabilities given an emulator architecture machine or virtualization type.", "response": "def domain_capabilities(emulator=None, arch=None, machine=None, domain=None, **kwargs):\n '''\n Return the domain capabilities given an emulator, architecture, machine or virtualization type.\n\n .. versionadded:: 2019.2.0\n\n :param emulator: return the capabilities for the given emulator binary\n :param arch: return the capabilities for the given CPU architecture\n :param machine: return the capabilities for the given emulated machine type\n :param domain: return the capabilities for the given virtualization type.\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n The list of the possible emulator, arch, machine and domain can be found in\n the host capabilities output.\n\n If none of the parameters is provided, the libvirt default one is returned.\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.domain_capabilities arch='x86_64' domain='kvm'\n\n '''\n conn = __get_conn(**kwargs)\n result = []\n try:\n caps = ElementTree.fromstring(conn.getDomainCapabilities(emulator, arch, machine, domain, 0))\n result = _parse_domain_caps(caps)\n finally:\n conn.close()\n\n return result"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef all_capabilities(**kwargs):\n '''\n Return the host and domain capabilities in a single call.\n\n .. versionadded:: Neon\n\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.all_capabilities\n\n '''\n conn = __get_conn(**kwargs)\n result = {}\n try:\n host_caps = ElementTree.fromstring(conn.getCapabilities())\n domains = [[(guest.get('arch', {}).get('name', None), key)\n for key in guest.get('arch', {}).get('domains', {}).keys()]\n for guest in [_parse_caps_guest(guest) for guest in host_caps.findall('guest')]]\n flattened = [pair for item in (x for x in domains) for pair in item]\n result = {\n 'host': {\n 'host': _parse_caps_host(host_caps.find('host')),\n 'guests': [_parse_caps_guest(guest) for guest in host_caps.findall('guest')]\n },\n 'domains': [_parse_domain_caps(ElementTree.fromstring(\n conn.getDomainCapabilities(None, arch, None, domain)))\n for (arch, domain) in flattened]}\n finally:\n conn.close()\n\n return result", "response": "Return the host and domain capabilities in a single call."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 function that can\nreturn the optimal custom CPU baseline config for VM s on this minion", "response": "def cpu_baseline(full=False, migratable=False, out='libvirt', **kwargs):\n '''\n Return the optimal 'custom' CPU baseline config for VM's on this minion\n\n .. versionadded:: 2016.3.0\n\n :param full: Return all CPU features rather than the ones on top of the closest CPU model\n :param migratable: Exclude CPU features that are unmigratable (libvirt 2.13+)\n :param out: 'libvirt' (default) for usable libvirt XML definition, 'salt' for nice dict\n :param connection: libvirt connection URI, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param username: username to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.cpu_baseline\n\n '''\n conn = __get_conn(**kwargs)\n caps = ElementTree.fromstring(conn.getCapabilities())\n cpu = caps.find('host/cpu')\n log.debug('Host CPU model definition: %s', salt.utils.stringutils.to_str(ElementTree.tostring(cpu)))\n\n flags = 0\n if migratable:\n # This one is only in 1.2.14+\n if getattr(libvirt, 'VIR_CONNECT_BASELINE_CPU_MIGRATABLE', False):\n flags += libvirt.VIR_CONNECT_BASELINE_CPU_MIGRATABLE\n else:\n conn.close()\n raise ValueError\n\n if full and getattr(libvirt, 'VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES', False):\n # This one is only in 1.1.3+\n flags += libvirt.VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES\n\n cpu = ElementTree.fromstring(conn.baselineCPU([salt.utils.stringutils.to_str(ElementTree.tostring(cpu))], flags))\n conn.close()\n\n if full and not getattr(libvirt, 'VIR_CONNECT_BASELINE_CPU_EXPAND_FEATURES', False):\n # Try do it by ourselves\n # Find the models in cpu_map.xml and iterate over them for as long as entries have submodels\n with salt.utils.files.fopen('/usr/share/libvirt/cpu_map.xml', 'r') as cpu_map:\n cpu_map = ElementTree.parse(cpu_map)\n\n cpu_model = cpu.find('model').text\n while cpu_model:\n cpu_map_models = cpu_map.findall('arch/model')\n cpu_specs = [el for el in cpu_map_models if el.get('name') == cpu_model and bool(len(el))]\n\n if not cpu_specs:\n raise ValueError('Model {0} not found in CPU map'.format(cpu_model))\n elif len(cpu_specs) > 1:\n raise ValueError('Multiple models {0} found in CPU map'.format(cpu_model))\n\n cpu_specs = cpu_specs[0]\n\n # libvirt's cpu map used to nest model elements, to point the parent model.\n # keep this code for compatibility with old libvirt versions\n model_node = cpu_specs.find('model')\n if model_node is None:\n cpu_model = None\n else:\n cpu_model = model_node.get('name')\n\n cpu.extend([feature for feature in cpu_specs.findall('feature')])\n\n if out == 'salt':\n return {\n 'model': cpu.find('model').text,\n 'vendor': cpu.find('vendor').text,\n 'features': [feature.get('name') for feature in cpu.findall('feature')]\n }\n return cpu.toxml()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef network_define(name, bridge, forward, **kwargs):\n '''\n Create libvirt network.\n\n :param name: Network name\n :param bridge: Bridge name\n :param forward: Forward mode(bridge, router, nat)\n :param vport: Virtualport type\n :param tag: Vlan tag\n :param autostart: Network autostart (default True)\n :param start: Network start (default True)\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.network_define network main bridge openvswitch\n\n .. versionadded:: 2019.2.0\n '''\n conn = __get_conn(**kwargs)\n vport = kwargs.get('vport', None)\n tag = kwargs.get('tag', None)\n autostart = kwargs.get('autostart', True)\n starting = kwargs.get('start', True)\n net_xml = _gen_net_xml(\n name,\n bridge,\n forward,\n vport,\n tag,\n )\n try:\n conn.networkDefineXML(net_xml)\n except libvirtError as err:\n log.warning(err)\n conn.close()\n raise err # a real error we should report upwards\n\n try:\n network = conn.networkLookupByName(name)\n except libvirtError as err:\n log.warning(err)\n conn.close()\n raise err # a real error we should report upwards\n\n if network is None:\n conn.close()\n return False\n\n if (starting is True or autostart is True) and network.isActive() != 1:\n network.create()\n\n if autostart is True and network.autostart() != 1:\n network.setAutostart(int(autostart))\n elif autostart is False and network.autostart() == 1:\n network.setAutostart(int(autostart))\n\n conn.close()\n\n return True", "response": "Function to create libvirt network."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist all virtual networks.", "response": "def list_networks(**kwargs):\n '''\n List all virtual networks.\n\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.list_networks\n '''\n conn = __get_conn(**kwargs)\n try:\n return [net.name() for net in conn.listAllNetworks()]\n finally:\n conn.close()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef network_info(name=None, **kwargs):\n '''\n Return informations on a virtual network provided its name.\n\n :param name: virtual network name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n If no name is provided, return the infos for all defined virtual networks.\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.network_info default\n '''\n result = {}\n conn = __get_conn(**kwargs)\n\n def _net_get_leases(net):\n '''\n Get all DHCP leases for a network\n '''\n leases = net.DHCPLeases()\n for lease in leases:\n if lease['type'] == libvirt.VIR_IP_ADDR_TYPE_IPV4:\n lease['type'] = 'ipv4'\n elif lease['type'] == libvirt.VIR_IP_ADDR_TYPE_IPV6:\n lease['type'] = 'ipv6'\n else:\n lease['type'] = 'unknown'\n return leases\n\n try:\n nets = [net for net in conn.listAllNetworks() if name is None or net.name() == name]\n result = {net.name(): {\n 'uuid': net.UUIDString(),\n 'bridge': net.bridgeName(),\n 'autostart': net.autostart(),\n 'active': net.isActive(),\n 'persistent': net.isPersistent(),\n 'leases': _net_get_leases(net)} for net in nets}\n except libvirt.libvirtError as err:\n log.debug('Silenced libvirt error: %s', str(err))\n finally:\n conn.close()\n return result", "response": "Return informations on a virtual network provided its name."} {"SOURCE": "codesearchnet", "instruction": "Write a Python 3 script to\nstart a defined virtual network.", "response": "def network_start(name, **kwargs):\n '''\n Start a defined virtual network.\n\n :param name: virtual network name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.network_start default\n '''\n conn = __get_conn(**kwargs)\n try:\n net = conn.networkLookupByName(name)\n return not bool(net.create())\n finally:\n conn.close()"} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nstops a defined virtual network.", "response": "def network_stop(name, **kwargs):\n '''\n Stop a defined virtual network.\n\n :param name: virtual network name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.network_stop default\n '''\n conn = __get_conn(**kwargs)\n try:\n net = conn.networkLookupByName(name)\n return not bool(net.destroy())\n finally:\n conn.close()"} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nremoves a defined virtual network. This does not stop the virtual network.", "response": "def network_undefine(name, **kwargs):\n '''\n Remove a defined virtual network. This does not stop the virtual network.\n\n :param name: virtual network name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.network_undefine default\n '''\n conn = __get_conn(**kwargs)\n try:\n net = conn.networkLookupByName(name)\n return not bool(net.undefine())\n finally:\n conn.close()"} {"SOURCE": "codesearchnet", "instruction": "Explain what the following Python 3 code does\ndef network_set_autostart(name, state='on', **kwargs):\n '''\n Set the autostart flag on a virtual network so that the network\n will start with the host system on reboot.\n\n :param name: virtual network name\n :param state: 'on' to auto start the network, anything else to mark the\n virtual network not to be started when the host boots\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt \"*\" virt.network_set_autostart <pool> <on | off>\n '''\n conn = __get_conn(**kwargs)\n try:\n net = conn.networkLookupByName(name)\n return not bool(net.setAutostart(1 if state == 'on' else 0))\n finally:\n conn.close()", "response": "Sets the autostart flag on a virtual network"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef pool_define(name,\n ptype,\n target=None,\n permissions=None,\n source_devices=None,\n source_dir=None,\n source_adapter=None,\n source_hosts=None,\n source_auth=None,\n source_name=None,\n source_format=None,\n transient=False,\n start=True, # pylint: disable=redefined-outer-name\n **kwargs):\n '''\n Create libvirt pool.\n\n :param name: Pool name\n :param ptype: Pool type. See `libvirt documentation\n <https://libvirt.org/storage.html>`_ for the possible values.\n :param target: Pool full path target\n :param permissions:\n Permissions to set on the target folder. This is mostly used for\n filesystem-based pool types. See pool-define-permissions_ for more\n details on this structure.\n\n :param source_devices:\n List of source devices for pools backed by physical devices. (Default:\n ``None``)\n\n Each item in the list is a dictionary with ``path`` and optionally\n ``part_separator`` keys. The path is the qualified name for iSCSI\n devices.\n\n Report to `this libvirt page\n <https://libvirt.org/formatstorage.html#StoragePool>`_ for more\n informations on the use of ``part_separator``\n\n :param source_dir: Path to the source directory for pools of type ``dir``,\n ``netfs`` or ``gluster``. (Default: ``None``)\n\n :param source_adapter:\n SCSI source definition. The value is a dictionary with ``type``,\n ``name``, ``parent``, ``managed``, ``parent_wwnn``, ``parent_wwpn``,\n ``parent_fabric_wwn``, ``wwnn``, ``wwpn`` and ``parent_address`` keys.\n\n The ``parent_address`` value is a dictionary with ``unique_id`` and\n ``address`` keys. The address represents a PCI address and is itself a\n dictionary with ``domain``, ``bus``, ``slot`` and ``function``\n properties. Report to `this libvirt page\n <https://libvirt.org/formatstorage.html#StoragePool>`_ for the meaning\n and possible values of these properties.\n\n :param source_hosts: List of source for pools backed by storage from remote\n servers. Each item is the hostname optionally followed by the port\n separated by a colon. (Default: ``None``)\n\n :param source_auth:\n Source authentication details. (Default: ``None``)\n\n The value is a dictionary with ``type``, ``username`` and ``secret``\n keys. The type can be one of ``ceph`` for Ceph RBD or ``chap`` for\n iSCSI sources.\n\n The ``secret`` value links to a libvirt secret object. It is a\n dictionary with ``type`` and ``value`` keys. The type value can be\n either ``uuid`` or ``usage``.\n\n Examples:\n\n .. code-block:: python\n\n source_auth={\n 'type': 'ceph',\n 'username': 'admin',\n 'secret': {\n 'type': 'uuid',\n 'uuid': '2ec115d7-3a88-3ceb-bc12-0ac909a6fd87'\n }\n }\n\n .. code-block:: python\n\n source_auth={\n 'type': 'chap',\n 'username': 'myname',\n 'secret': {\n 'type': 'usage',\n 'uuid': 'mycluster_myname'\n }\n }\n\n :param source_name:\n Identifier of name-based sources.\n :param source_format:\n String representing the source format. The possible values are depending on the\n source type. See `libvirt documentation <https://libvirt.org/storage.html>`_ for\n the possible values.\n :param start: Pool start (default True)\n :param transient:\n When ``True``, the pool will be automatically undefined after being stopped.\n Note that a transient pool will force ``start`` to ``True``. (Default: ``False``)\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. _pool-define-permissions:\n\n **Permissions definition**\n\n The permissions are described by a dictionary containing the following keys:\n\n mode\n The octal representation of the permissions. (Default: `0711`)\n\n owner\n the numeric user ID of the owner. (Default: from the parent folder)\n\n group\n the numeric ID of the group. (Default: from the parent folder)\n\n label\n the SELinux label. (Default: `None`)\n\n\n CLI Example:\n\n Local folder pool:\n\n .. code-block:: bash\n\n salt '*' virt.pool_define somepool dir target=/srv/mypool \\\n permissions=\"{'mode': '0744' 'ower': 107, 'group': 107 }\"\n\n CIFS backed pool:\n\n .. code-block:: bash\n\n salt '*' virt.pool_define myshare netfs source_format=cifs \\\n source_dir=samba_share source_hosts=\"['example.com']\" target=/mnt/cifs\n\n .. versionadded:: 2019.2.0\n '''\n conn = __get_conn(**kwargs)\n pool_xml = _gen_pool_xml(\n name,\n ptype,\n target,\n permissions=permissions,\n source_devices=source_devices,\n source_dir=source_dir,\n source_adapter=source_adapter,\n source_hosts=source_hosts,\n source_auth=source_auth,\n source_name=source_name,\n source_format=source_format\n )\n try:\n if transient:\n pool = conn.storagePoolCreateXML(pool_xml)\n else:\n pool = conn.storagePoolDefineXML(pool_xml)\n if start:\n pool.create()\n except libvirtError as err:\n raise err # a real error we should report upwards\n finally:\n conn.close()\n\n # libvirt function will raise a libvirtError in case of failure\n return True", "response": "Create libvirt pool.\n\n :param name: Pool name\n :param ptype: Pool type. See `libvirt documentation\n <https://libvirt.org/storage.html>`_ for the possible values.\n :param target: Pool full path target\n :param permissions:\n Permissions to set on the target folder. This is mostly used for\n filesystem-based pool types. See pool-define-permissions_ for more\n details on this structure.\n\n :param source_devices:\n List of source devices for pools backed by physical devices. (Default:\n ``None``)\n\n Each item in the list is a dictionary with ``path`` and optionally\n ``part_separator`` keys. The path is the qualified name for iSCSI\n devices.\n\n Report to `this libvirt page\n <https://libvirt.org/formatstorage.html#StoragePool>`_ for more\n informations on the use of ``part_separator``\n\n :param source_dir: Path to the source directory for pools of type ``dir``,\n ``netfs`` or ``gluster``. (Default: ``None``)\n\n :param source_adapter:\n SCSI source definition. The value is a dictionary with ``type``,\n ``name``, ``parent``, ``managed``, ``parent_wwnn``, ``parent_wwpn``,\n ``parent_fabric_wwn``, ``wwnn``, ``wwpn`` and ``parent_address`` keys.\n\n The ``parent_address`` value is a dictionary with ``unique_id`` and\n ``address`` keys. The address represents a PCI address and is itself a\n dictionary with ``domain``, ``bus``, ``slot`` and ``function``\n properties. Report to `this libvirt page\n <https://libvirt.org/formatstorage.html#StoragePool>`_ for the meaning\n and possible values of these properties.\n\n :param source_hosts: List of source for pools backed by storage from remote\n servers. Each item is the hostname optionally followed by the port\n separated by a colon. (Default: ``None``)\n\n :param source_auth:\n Source authentication details. (Default: ``None``)\n\n The value is a dictionary with ``type``, ``username`` and ``secret``\n keys. The type can be one of ``ceph`` for Ceph RBD or ``chap`` for\n iSCSI sources.\n\n The ``secret`` value links to a libvirt secret object. It is a\n dictionary with ``type`` and ``value`` keys. The type value can be\n either ``uuid`` or ``usage``.\n\n Examples:\n\n .. code-block:: python\n\n source_auth={\n 'type': 'ceph',\n 'username': 'admin',\n 'secret': {\n 'type': 'uuid',\n 'uuid': '2ec115d7-3a88-3ceb-bc12-0ac909a6fd87'\n }\n }\n\n .. code-block:: python\n\n source_auth={\n 'type': 'chap',\n 'username': 'myname',\n 'secret': {\n 'type': 'usage',\n 'uuid': 'mycluster_myname'\n }\n }\n\n :param source_name:\n Identifier of name-based sources.\n :param source_format:\n String representing the source format. The possible values are depending on the\n source type. See `libvirt documentation <https://libvirt.org/storage.html>`_ for\n the possible values.\n :param start: Pool start (default True)\n :param transient:\n When ``True``, the pool will be automatically undefined after being stopped.\n Note that a transient pool will force ``start`` to ``True``. (Default: ``False``)\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. _pool-define-permissions:\n\n **Permissions definition**\n\n The permissions are described by a dictionary containing the following keys:\n\n mode\n The octal representation of the permissions. (Default: `0711`)\n\n owner\n the numeric user ID of the owner. (Default: from the parent folder)\n\n group\n the numeric ID of the group. (Default: from the parent folder)\n\n label\n the SELinux label. (Default: `None`)\n\n\n CLI Example:\n\n Local folder pool:\n\n .. code-block:: bash\n\n salt '*' virt.pool_define somepool dir target=/srv/mypool \\\n permissions=\"{'mode': '0744' 'ower': 107, 'group': 107 }\"\n\n CIFS backed pool:\n\n .. code-block:: bash\n\n salt '*' virt.pool_define myshare netfs source_format=cifs \\\n source_dir=samba_share source_hosts=\"['example.com']\" target=/mnt/cifs\n\n .. versionadded:: 2019.2.0"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nlist all libvirt storage pools.", "response": "def list_pools(**kwargs):\n '''\n List all storage pools.\n\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.list_pools\n '''\n conn = __get_conn(**kwargs)\n try:\n return [pool.name() for pool in conn.listAllStoragePools()]\n finally:\n conn.close()"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef pool_info(name=None, **kwargs):\n '''\n Return informations on a storage pool provided its name.\n\n :param name: libvirt storage pool name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n If no name is provided, return the infos for all defined storage pools.\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.pool_info default\n '''\n result = {}\n conn = __get_conn(**kwargs)\n\n def _pool_extract_infos(pool):\n '''\n Format the pool info dictionary\n\n :param pool: the libvirt pool object\n '''\n states = ['inactive', 'building', 'running', 'degraded', 'inaccessible']\n infos = pool.info()\n state = states[infos[0]] if infos[0] < len(states) else 'unknown'\n desc = ElementTree.fromstring(pool.XMLDesc())\n path_node = desc.find('target/path')\n return {\n 'uuid': pool.UUIDString(),\n 'state': state,\n 'capacity': infos[1],\n 'allocation': infos[2],\n 'free': infos[3],\n 'autostart': pool.autostart(),\n 'persistent': pool.isPersistent(),\n 'target_path': path_node.text if path_node is not None else None,\n 'type': desc.get('type')\n }\n\n try:\n pools = [pool for pool in conn.listAllStoragePools() if name is None or pool.name() == name]\n result = {pool.name(): _pool_extract_infos(pool) for pool in pools}\n except libvirt.libvirtError as err:\n log.debug('Silenced libvirt error: %s', str(err))\n finally:\n conn.close()\n return result", "response": "Return informations on a libvirt storage pool provided its name."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef pool_start(name, **kwargs):\n '''\n Start a defined libvirt storage pool.\n\n :param name: libvirt storage pool name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.pool_start default\n '''\n conn = __get_conn(**kwargs)\n try:\n pool = conn.storagePoolLookupByName(name)\n return not bool(pool.create())\n finally:\n conn.close()", "response": "Start a libvirt storage pool."} {"SOURCE": "codesearchnet", "instruction": "Can you create a Python 3 function that\nbuilds a libvirt storage pool.", "response": "def pool_build(name, **kwargs):\n '''\n Build a defined libvirt storage pool.\n\n :param name: libvirt storage pool name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.pool_build default\n '''\n conn = __get_conn(**kwargs)\n try:\n pool = conn.storagePoolLookupByName(name)\n return not bool(pool.build())\n finally:\n conn.close()"} {"SOURCE": "codesearchnet", "instruction": "Given the following Python 3 function, write the documentation\ndef pool_stop(name, **kwargs):\n '''\n Stop a defined libvirt storage pool.\n\n :param name: libvirt storage pool name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.pool_stop default\n '''\n conn = __get_conn(**kwargs)\n try:\n pool = conn.storagePoolLookupByName(name)\n return not bool(pool.destroy())\n finally:\n conn.close()", "response": "Stop a defined libvirt storage pool."} {"SOURCE": "codesearchnet", "instruction": "Can you write a function in Python 3 where it\nremoves a libvirt storage pool.", "response": "def pool_undefine(name, **kwargs):\n '''\n Remove a defined libvirt storage pool. The pool needs to be stopped before calling.\n\n :param name: libvirt storage pool name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.pool_undefine default\n '''\n conn = __get_conn(**kwargs)\n try:\n pool = conn.storagePoolLookupByName(name)\n return not bool(pool.undefine())\n finally:\n conn.close()"} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\ndelete the resources of a libvirt storage pool.", "response": "def pool_delete(name, fast=True, **kwargs):\n '''\n Delete the resources of a defined libvirt storage pool.\n\n :param name: libvirt storage pool name\n :param fast: if set to False, zeroes out all the data.\n Default value is True.\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.pool_delete default\n '''\n conn = __get_conn(**kwargs)\n try:\n pool = conn.storagePoolLookupByName(name)\n flags = libvirt.VIR_STORAGE_POOL_DELETE_NORMAL\n if fast:\n flags = libvirt.VIR_STORAGE_POOL_DELETE_ZEROED\n return not bool(pool.delete(flags))\n finally:\n conn.close()"} {"SOURCE": "codesearchnet", "instruction": "Here you have a function in Python 3, explain what it does\ndef pool_refresh(name, **kwargs):\n '''\n Refresh a defined libvirt storage pool.\n\n :param name: libvirt storage pool name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' virt.pool_refresh default\n '''\n conn = __get_conn(**kwargs)\n try:\n pool = conn.storagePoolLookupByName(name)\n return not bool(pool.refresh())\n finally:\n conn.close()", "response": "Refresh a defined libvirt storage pool."} {"SOURCE": "codesearchnet", "instruction": "Create a Python 3 function to\nset the autostart flag on a libvirt storage pool", "response": "def pool_set_autostart(name, state='on', **kwargs):\n '''\n Set the autostart flag on a libvirt storage pool so that the storage pool\n will start with the host system on reboot.\n\n :param name: libvirt storage pool name\n :param state: 'on' to auto start the pool, anything else to mark the\n pool not to be started when the host boots\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt \"*\" virt.pool_set_autostart <pool> <on | off>\n '''\n conn = __get_conn(**kwargs)\n try:\n pool = conn.storagePoolLookupByName(name)\n return not bool(pool.setAutostart(1 if state == 'on' else 0))\n finally:\n conn.close()"} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef pool_list_volumes(name, **kwargs):\n '''\n List the volumes contained in a defined libvirt storage pool.\n\n :param name: libvirt storage pool name\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: 2019.2.0\n\n CLI Example:\n\n .. code-block:: bash\n\n salt \"*\" virt.pool_list_volumes <pool>\n '''\n conn = __get_conn(**kwargs)\n try:\n pool = conn.storagePoolLookupByName(name)\n return pool.listVolumes()\n finally:\n conn.close()", "response": "List the volumes contained in a libvirt storage pool."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef _get_all_volumes_paths(conn):\n '''\n Extract the path and backing stores path of all volumes.\n\n :param conn: libvirt connection to use\n '''\n volumes = [vol for l in [obj.listAllVolumes() for obj in conn.listAllStoragePools()] for vol in l]\n return {vol.path(): [path.text for path in ElementTree.fromstring(vol.XMLDesc()).findall('.//backingStore/path')]\n for vol in volumes if _is_valid_volume(vol)}", "response": "Extract the path and backing stores path of all volumes."} {"SOURCE": "codesearchnet", "instruction": "Can you tell what is the following Python 3 function doing\ndef volume_infos(pool=None, volume=None, **kwargs):\n '''\n Provide details on a storage volume. If no volume name is provided, the infos\n all the volumes contained in the pool are provided. If no pool is provided,\n the infos of the volumes of all pools are output.\n\n :param pool: libvirt storage pool name (default: ``None``)\n :param volume: name of the volume to get infos from (default: ``None``)\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: Neon\n\n CLI Example:\n\n .. code-block:: bash\n\n salt \"*\" virt.volume_infos <pool> <volume>\n '''\n result = {}\n conn = __get_conn(**kwargs)\n try:\n backing_stores = _get_all_volumes_paths(conn)\n domains = _get_domain(conn)\n domains_list = domains if isinstance(domains, list) else [domains]\n disks = {domain.name():\n {node.get('file') for node\n in ElementTree.fromstring(domain.XMLDesc(0)).findall('.//disk/source/[@file]')}\n for domain in domains_list}\n\n def _volume_extract_infos(vol):\n '''\n Format the volume info dictionary\n\n :param vol: the libvirt storage volume object.\n '''\n types = ['file', 'block', 'dir', 'network', 'netdir', 'ploop']\n infos = vol.info()\n\n # If we have a path, check its use.\n used_by = []\n if vol.path():\n as_backing_store = {path for (path, all_paths) in backing_stores.items() if vol.path() in all_paths}\n used_by = [vm_name for (vm_name, vm_disks) in disks.items()\n if vm_disks & as_backing_store or vol.path() in vm_disks]\n\n return {\n 'type': types[infos[0]] if infos[0] < len(types) else 'unknown',\n 'key': vol.key(),\n 'path': vol.path(),\n 'capacity': infos[1],\n 'allocation': infos[2],\n 'used_by': used_by,\n }\n\n pools = [obj for obj in conn.listAllStoragePools() if pool is None or obj.name() == pool]\n vols = {pool_obj.name(): {vol.name(): _volume_extract_infos(vol)\n for vol in pool_obj.listAllVolumes()\n if (volume is None or vol.name() == volume) and _is_valid_volume(vol)}\n for pool_obj in pools}\n return {pool_name: volumes for (pool_name, volumes) in vols.items() if volumes}\n except libvirt.libvirtError as err:\n log.debug('Silenced libvirt error: %s', str(err))\n finally:\n conn.close()\n return result", "response": "Return a dictionary of storage volume infos for a given libvirt storage pool and volume."} {"SOURCE": "codesearchnet", "instruction": "Can you generate the documentation for the following Python 3 function\ndef volume_delete(pool, volume, **kwargs):\n '''\n Delete a libvirt managed volume.\n\n :param pool: libvirt storage pool name\n :param volume: name of the volume to delete\n :param connection: libvirt connection URI, overriding defaults\n :param username: username to connect with, overriding defaults\n :param password: password to connect with, overriding defaults\n\n .. versionadded:: Neon\n\n CLI Example:\n\n .. code-block:: bash\n\n salt \"*\" virt.volume_delete <pool> <volume>\n '''\n conn = __get_conn(**kwargs)\n try:\n vol = _get_storage_vol(conn, pool, volume)\n return not bool(vol.delete())\n finally:\n conn.close()", "response": "Delete a libvirt managed volume."} {"SOURCE": "codesearchnet", "instruction": "How would you explain what the following Python 3 function does\ndef list_():\n '''\n List available metadata\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mdata.list\n '''\n mdata = _check_mdata_list()\n if mdata:\n cmd = '{0}'.format(mdata)\n return __salt__['cmd.run'](cmd, ignore_retcode=True).splitlines()\n return {}", "response": "List available metadata available on the minion"} {"SOURCE": "codesearchnet", "instruction": "Make a summary of the following Python 3 code\ndef get_(*keyname):\n '''\n Get metadata\n\n keyname : string\n name of key\n\n .. note::\n\n If no keynames are specified, we get all (public) properties\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_get()\n ret = {}\n\n if not keyname:\n keyname = list_()\n\n for k in keyname:\n if mdata:\n cmd = '{0} {1}'.format(mdata, k)\n res = __salt__['cmd.run_all'](cmd, ignore_retcode=True)\n ret[k] = res['stdout'] if res['retcode'] == 0 else ''\n else:\n ret[k] = ''\n\n return ret", "response": "Get metadata of a key."} {"SOURCE": "codesearchnet", "instruction": "Can you generate a brief explanation for the following Python 3 code\ndef put_(keyname, val):\n '''\n Put metadata\n\n prop : string\n name of property\n val : string\n value to set\n\n CLI Example:\n\n .. code-block:: bash\n\n salt '*' mdata.list\n '''\n mdata = _check_mdata_put()\n ret = {}\n\n if mdata:\n cmd = 'echo {2} | {0} {1}'.format(mdata, keyname, val)\n ret = __salt__['cmd.run_all'](cmd, python_shell=True, ignore_retcode=True)\n\n return ret['retcode'] == 0", "response": "Set the value of keyname in the\n property"}