repo stringlengths 7 55 | path stringlengths 4 223 | func_name stringlengths 1 134 | original_string stringlengths 75 104k | language stringclasses 1 value | code stringlengths 75 104k | code_tokens listlengths 19 28.4k | docstring stringlengths 1 46.9k | docstring_tokens listlengths 1 1.97k | sha stringlengths 40 40 | url stringlengths 87 315 | partition stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|
saltstack/salt | salt/modules/pyenv.py | versions | def versions(runas=None):
'''
List the installed versions of python.
CLI Example:
.. code-block:: bash
salt '*' pyenv.versions
'''
ret = _pyenv_exec('versions', '--bare', runas=runas)
return [] if ret is False else ret.splitlines() | python | def versions(runas=None):
'''
List the installed versions of python.
CLI Example:
.. code-block:: bash
salt '*' pyenv.versions
'''
ret = _pyenv_exec('versions', '--bare', runas=runas)
return [] if ret is False else ret.splitlines() | [
"def",
"versions",
"(",
"runas",
"=",
"None",
")",
":",
"ret",
"=",
"_pyenv_exec",
"(",
"'versions'",
",",
"'--bare'",
",",
"runas",
"=",
"runas",
")",
"return",
"[",
"]",
"if",
"ret",
"is",
"False",
"else",
"ret",
".",
"splitlines",
"(",
")"
] | List the installed versions of python.
CLI Example:
.. code-block:: bash
salt '*' pyenv.versions | [
"List",
"the",
"installed",
"versions",
"of",
"python",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/pyenv.py#L209-L220 | train |
saltstack/salt | salt/modules/pyenv.py | default | def default(python=None, runas=None):
'''
Returns or sets the currently defined default python.
python=None
The version to set as the default. Should match one of the versions
listed by :mod:`pyenv.versions <salt.modules.pyenv.versions>`. Leave
blank to return the current default.
CLI Example:
.. code-block:: bash
salt '*' pyenv.default
salt '*' pyenv.default 2.0.0-p0
'''
if python:
_pyenv_exec('global', python, runas=runas)
return True
else:
ret = _pyenv_exec('global', runas=runas)
return '' if ret is False else ret.strip() | python | def default(python=None, runas=None):
'''
Returns or sets the currently defined default python.
python=None
The version to set as the default. Should match one of the versions
listed by :mod:`pyenv.versions <salt.modules.pyenv.versions>`. Leave
blank to return the current default.
CLI Example:
.. code-block:: bash
salt '*' pyenv.default
salt '*' pyenv.default 2.0.0-p0
'''
if python:
_pyenv_exec('global', python, runas=runas)
return True
else:
ret = _pyenv_exec('global', runas=runas)
return '' if ret is False else ret.strip() | [
"def",
"default",
"(",
"python",
"=",
"None",
",",
"runas",
"=",
"None",
")",
":",
"if",
"python",
":",
"_pyenv_exec",
"(",
"'global'",
",",
"python",
",",
"runas",
"=",
"runas",
")",
"return",
"True",
"else",
":",
"ret",
"=",
"_pyenv_exec",
"(",
"'g... | Returns or sets the currently defined default python.
python=None
The version to set as the default. Should match one of the versions
listed by :mod:`pyenv.versions <salt.modules.pyenv.versions>`. Leave
blank to return the current default.
CLI Example:
.. code-block:: bash
salt '*' pyenv.default
salt '*' pyenv.default 2.0.0-p0 | [
"Returns",
"or",
"sets",
"the",
"currently",
"defined",
"default",
"python",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/pyenv.py#L223-L244 | train |
saltstack/salt | salt/modules/pyenv.py | do | def do(cmdline=None, runas=None):
'''
Execute a python command with pyenv's shims from the user or the system.
CLI Example:
.. code-block:: bash
salt '*' pyenv.do 'gem list bundler'
salt '*' pyenv.do 'gem list bundler' deploy
'''
path = _pyenv_path(runas)
cmd_split = cmdline.split()
quoted_line = ''
for cmd in cmd_split:
quoted_line = quoted_line + ' ' + _cmd_quote(cmd)
result = __salt__['cmd.run_all'](
'env PATH={0}/shims:$PATH {1}'.format(_cmd_quote(path), quoted_line),
runas=runas,
python_shell=True
)
if result['retcode'] == 0:
rehash(runas=runas)
return result['stdout']
else:
return False | python | def do(cmdline=None, runas=None):
'''
Execute a python command with pyenv's shims from the user or the system.
CLI Example:
.. code-block:: bash
salt '*' pyenv.do 'gem list bundler'
salt '*' pyenv.do 'gem list bundler' deploy
'''
path = _pyenv_path(runas)
cmd_split = cmdline.split()
quoted_line = ''
for cmd in cmd_split:
quoted_line = quoted_line + ' ' + _cmd_quote(cmd)
result = __salt__['cmd.run_all'](
'env PATH={0}/shims:$PATH {1}'.format(_cmd_quote(path), quoted_line),
runas=runas,
python_shell=True
)
if result['retcode'] == 0:
rehash(runas=runas)
return result['stdout']
else:
return False | [
"def",
"do",
"(",
"cmdline",
"=",
"None",
",",
"runas",
"=",
"None",
")",
":",
"path",
"=",
"_pyenv_path",
"(",
"runas",
")",
"cmd_split",
"=",
"cmdline",
".",
"split",
"(",
")",
"quoted_line",
"=",
"''",
"for",
"cmd",
"in",
"cmd_split",
":",
"quoted... | Execute a python command with pyenv's shims from the user or the system.
CLI Example:
.. code-block:: bash
salt '*' pyenv.do 'gem list bundler'
salt '*' pyenv.do 'gem list bundler' deploy | [
"Execute",
"a",
"python",
"command",
"with",
"pyenv",
"s",
"shims",
"from",
"the",
"user",
"or",
"the",
"system",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/pyenv.py#L281-L307 | train |
saltstack/salt | salt/modules/pyenv.py | do_with_python | def do_with_python(python, cmdline, runas=None):
'''
Execute a python command with pyenv's shims using a specific python version.
CLI Example:
.. code-block:: bash
salt '*' pyenv.do_with_python 2.0.0-p0 'gem list bundler'
salt '*' pyenv.do_with_python 2.0.0-p0 'gem list bundler' deploy
'''
if python:
cmd = 'PYENV_VERSION={0} {1}'.format(python, cmdline)
else:
cmd = cmdline
return do(cmd, runas=runas) | python | def do_with_python(python, cmdline, runas=None):
'''
Execute a python command with pyenv's shims using a specific python version.
CLI Example:
.. code-block:: bash
salt '*' pyenv.do_with_python 2.0.0-p0 'gem list bundler'
salt '*' pyenv.do_with_python 2.0.0-p0 'gem list bundler' deploy
'''
if python:
cmd = 'PYENV_VERSION={0} {1}'.format(python, cmdline)
else:
cmd = cmdline
return do(cmd, runas=runas) | [
"def",
"do_with_python",
"(",
"python",
",",
"cmdline",
",",
"runas",
"=",
"None",
")",
":",
"if",
"python",
":",
"cmd",
"=",
"'PYENV_VERSION={0} {1}'",
".",
"format",
"(",
"python",
",",
"cmdline",
")",
"else",
":",
"cmd",
"=",
"cmdline",
"return",
"do"... | Execute a python command with pyenv's shims using a specific python version.
CLI Example:
.. code-block:: bash
salt '*' pyenv.do_with_python 2.0.0-p0 'gem list bundler'
salt '*' pyenv.do_with_python 2.0.0-p0 'gem list bundler' deploy | [
"Execute",
"a",
"python",
"command",
"with",
"pyenv",
"s",
"shims",
"using",
"a",
"specific",
"python",
"version",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/pyenv.py#L310-L326 | train |
saltstack/salt | salt/states/boto_rds.py | present | def present(name,
allocated_storage,
db_instance_class,
engine,
master_username,
master_user_password,
db_name=None,
storage_type=None,
db_security_groups=None,
vpc_security_group_ids=None,
vpc_security_groups=None,
availability_zone=None,
db_subnet_group_name=None,
preferred_maintenance_window=None,
db_parameter_group_name=None,
db_cluster_identifier=None,
tde_credential_arn=None,
tde_credential_password=None,
storage_encrypted=None,
kms_keyid=None,
backup_retention_period=None,
preferred_backup_window=None,
port=None,
multi_az=None,
engine_version=None,
auto_minor_version_upgrade=None,
license_model=None,
iops=None,
option_group_name=None,
character_set_name=None,
publicly_accessible=None,
wait_status=None,
tags=None,
copy_tags_to_snapshot=None,
region=None,
domain=None,
key=None,
keyid=None,
monitoring_interval=None,
monitoring_role_arn=None,
domain_iam_role_name=None,
promotion_tier=None,
profile=None):
'''
Ensure RDS instance exists.
name
Name of the RDS state definition.
allocated_storage
The amount of storage (in gigabytes) to be initially allocated for the
database instance.
db_instance_class
The compute and memory capacity of the Amazon RDS DB instance.
engine
The name of the database engine to be used for this instance. Supported
engine types are: MySQL, mariadb, oracle-se1, oracle-se, oracle-ee, sqlserver-ee,
sqlserver-se, sqlserver-ex, sqlserver-web, postgres and aurora. For more
information, please see the ``engine`` argument in the Boto3 RDS
`create_db_instance`_ documentation.
master_username
The name of master user for the client DB instance.
master_user_password
The password for the master database user. Can be any printable ASCII
character except "/", '"', or "@".
db_name
The meaning of this parameter differs according to the database engine you use.
See the Boto3 RDS documentation to determine the appropriate value for your configuration.
https://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.create_db_instance
storage_type
Specifies the storage type to be associated with the DB instance.
Options are standard, gp2 and io1. If you specify io1, you must also include
a value for the Iops parameter.
db_security_groups
A list of DB security groups to associate with this DB instance.
vpc_security_group_ids
A list of EC2 VPC security group IDs to associate with this DB instance.
vpc_security_groups
A list of EC2 VPC security groups (IDs or Name tags) to associate with this DB instance.
availability_zone
The EC2 Availability Zone that the database instance will be created
in.
db_subnet_group_name
A DB subnet group to associate with this DB instance.
preferred_maintenance_window
The weekly time range (in UTC) during which system maintenance can
occur.
db_parameter_group_name
A DB parameter group to associate with this DB instance.
db_cluster_identifier
If the DB instance is a member of a DB cluster, contains the name of
the DB cluster that the DB instance is a member of.
tde_credential_arn
The ARN from the Key Store with which the instance is associated for
TDE encryption.
tde_credential_password
The password to use for TDE encryption if an encryption key is not used.
storage_encrypted
Specifies whether the DB instance is encrypted.
kms_keyid
If storage_encrypted is true, the KMS key identifier for the encrypted
DB instance.
backup_retention_period
The number of days for which automated backups are retained.
preferred_backup_window
The daily time range during which automated backups are created if
automated backups are enabled.
port
The port number on which the database accepts connections.
multi_az
Specifies if the DB instance is a Multi-AZ deployment. You cannot set
the AvailabilityZone parameter if the MultiAZ parameter is set to true.
engine_version
The version number of the database engine to use.
auto_minor_version_upgrade
Indicates that minor engine upgrades will be applied automatically to
the DB instance during the maintenance window.
license_model
License model information for this DB instance.
iops
The amount of Provisioned IOPS (input/output operations per second) to
be initially allocated for the DB instance.
option_group_name
Indicates that the DB instance should be associated with the specified
option group.
character_set_name
For supported engines, indicates that the DB instance should be
associated with the specified CharacterSet.
publicly_accessible
Specifies the accessibility options for the DB instance. A value of
true specifies an Internet-facing instance with a publicly resolvable
DNS name, which resolves to a public IP address. A value of false
specifies an internal instance with a DNS name that resolves to a
private IP address.
wait_status
Wait for the RDS instance to reach a desired status before finishing
the state. Available states: available, modifying, backing-up
tags
A dict of tags.
copy_tags_to_snapshot
Specifies whether tags are copied from the DB instance to snapshots of
the DB instance.
region
Region to connect to.
domain
The identifier of the Active Directory Domain.
key
AWS secret key to be used.
keyid
AWS access key to be used.
monitoring_interval
The interval, in seconds, between points when Enhanced Monitoring
metrics are collected for the DB instance.
monitoring_role_arn
The ARN for the IAM role that permits RDS to send Enhanced Monitoring
metrics to CloudWatch Logs.
domain_iam_role_name
Specify the name of the IAM role to be used when making API calls to
the Directory Service.
promotion_tier
A value that specifies the order in which an Aurora Replica is
promoted to the primary instance after a failure of the existing
primary instance. For more information, see Fault Tolerance for an
Aurora DB Cluster .
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
.. _create_db_instance: https://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.create_db_instance
'''
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
r = __salt__['boto_rds.exists'](name, tags, region, key, keyid, profile)
if not r.get('exists'):
if __opts__['test']:
ret['comment'] = 'RDS instance {0} would be created.'.format(name)
ret['result'] = None
return ret
r = __salt__['boto_rds.create'](name, allocated_storage,
db_instance_class, engine,
master_username,
master_user_password,
db_name, db_security_groups,
vpc_security_group_ids,
vpc_security_groups,
availability_zone,
db_subnet_group_name,
preferred_maintenance_window,
db_parameter_group_name,
backup_retention_period,
preferred_backup_window,
port, multi_az, engine_version,
auto_minor_version_upgrade,
license_model, iops,
option_group_name,
character_set_name,
publicly_accessible, wait_status,
tags, db_cluster_identifier,
storage_type, tde_credential_arn,
tde_credential_password,
storage_encrypted, kms_keyid,
domain, copy_tags_to_snapshot,
monitoring_interval,
monitoring_role_arn,
domain_iam_role_name, region,
promotion_tier, key, keyid, profile)
if not r.get('created'):
ret['result'] = False
ret['comment'] = 'Failed to create RDS instance {0}.'.format(r['error']['message'])
return ret
ret['changes']['old'] = {'instance': None}
ret['changes']['new'] = {
'instance': __salt__['boto_rds.describe_db_instances'](
name=name, jmespath='DBInstances[0]', region=region, key=key,
keyid=keyid, profile=profile)}
ret['comment'] = 'RDS instance {0} created.'.format(name)
else:
ret['comment'] = 'RDS instance {0} exists.'.format(name)
return ret | python | def present(name,
allocated_storage,
db_instance_class,
engine,
master_username,
master_user_password,
db_name=None,
storage_type=None,
db_security_groups=None,
vpc_security_group_ids=None,
vpc_security_groups=None,
availability_zone=None,
db_subnet_group_name=None,
preferred_maintenance_window=None,
db_parameter_group_name=None,
db_cluster_identifier=None,
tde_credential_arn=None,
tde_credential_password=None,
storage_encrypted=None,
kms_keyid=None,
backup_retention_period=None,
preferred_backup_window=None,
port=None,
multi_az=None,
engine_version=None,
auto_minor_version_upgrade=None,
license_model=None,
iops=None,
option_group_name=None,
character_set_name=None,
publicly_accessible=None,
wait_status=None,
tags=None,
copy_tags_to_snapshot=None,
region=None,
domain=None,
key=None,
keyid=None,
monitoring_interval=None,
monitoring_role_arn=None,
domain_iam_role_name=None,
promotion_tier=None,
profile=None):
'''
Ensure RDS instance exists.
name
Name of the RDS state definition.
allocated_storage
The amount of storage (in gigabytes) to be initially allocated for the
database instance.
db_instance_class
The compute and memory capacity of the Amazon RDS DB instance.
engine
The name of the database engine to be used for this instance. Supported
engine types are: MySQL, mariadb, oracle-se1, oracle-se, oracle-ee, sqlserver-ee,
sqlserver-se, sqlserver-ex, sqlserver-web, postgres and aurora. For more
information, please see the ``engine`` argument in the Boto3 RDS
`create_db_instance`_ documentation.
master_username
The name of master user for the client DB instance.
master_user_password
The password for the master database user. Can be any printable ASCII
character except "/", '"', or "@".
db_name
The meaning of this parameter differs according to the database engine you use.
See the Boto3 RDS documentation to determine the appropriate value for your configuration.
https://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.create_db_instance
storage_type
Specifies the storage type to be associated with the DB instance.
Options are standard, gp2 and io1. If you specify io1, you must also include
a value for the Iops parameter.
db_security_groups
A list of DB security groups to associate with this DB instance.
vpc_security_group_ids
A list of EC2 VPC security group IDs to associate with this DB instance.
vpc_security_groups
A list of EC2 VPC security groups (IDs or Name tags) to associate with this DB instance.
availability_zone
The EC2 Availability Zone that the database instance will be created
in.
db_subnet_group_name
A DB subnet group to associate with this DB instance.
preferred_maintenance_window
The weekly time range (in UTC) during which system maintenance can
occur.
db_parameter_group_name
A DB parameter group to associate with this DB instance.
db_cluster_identifier
If the DB instance is a member of a DB cluster, contains the name of
the DB cluster that the DB instance is a member of.
tde_credential_arn
The ARN from the Key Store with which the instance is associated for
TDE encryption.
tde_credential_password
The password to use for TDE encryption if an encryption key is not used.
storage_encrypted
Specifies whether the DB instance is encrypted.
kms_keyid
If storage_encrypted is true, the KMS key identifier for the encrypted
DB instance.
backup_retention_period
The number of days for which automated backups are retained.
preferred_backup_window
The daily time range during which automated backups are created if
automated backups are enabled.
port
The port number on which the database accepts connections.
multi_az
Specifies if the DB instance is a Multi-AZ deployment. You cannot set
the AvailabilityZone parameter if the MultiAZ parameter is set to true.
engine_version
The version number of the database engine to use.
auto_minor_version_upgrade
Indicates that minor engine upgrades will be applied automatically to
the DB instance during the maintenance window.
license_model
License model information for this DB instance.
iops
The amount of Provisioned IOPS (input/output operations per second) to
be initially allocated for the DB instance.
option_group_name
Indicates that the DB instance should be associated with the specified
option group.
character_set_name
For supported engines, indicates that the DB instance should be
associated with the specified CharacterSet.
publicly_accessible
Specifies the accessibility options for the DB instance. A value of
true specifies an Internet-facing instance with a publicly resolvable
DNS name, which resolves to a public IP address. A value of false
specifies an internal instance with a DNS name that resolves to a
private IP address.
wait_status
Wait for the RDS instance to reach a desired status before finishing
the state. Available states: available, modifying, backing-up
tags
A dict of tags.
copy_tags_to_snapshot
Specifies whether tags are copied from the DB instance to snapshots of
the DB instance.
region
Region to connect to.
domain
The identifier of the Active Directory Domain.
key
AWS secret key to be used.
keyid
AWS access key to be used.
monitoring_interval
The interval, in seconds, between points when Enhanced Monitoring
metrics are collected for the DB instance.
monitoring_role_arn
The ARN for the IAM role that permits RDS to send Enhanced Monitoring
metrics to CloudWatch Logs.
domain_iam_role_name
Specify the name of the IAM role to be used when making API calls to
the Directory Service.
promotion_tier
A value that specifies the order in which an Aurora Replica is
promoted to the primary instance after a failure of the existing
primary instance. For more information, see Fault Tolerance for an
Aurora DB Cluster .
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
.. _create_db_instance: https://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.create_db_instance
'''
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
r = __salt__['boto_rds.exists'](name, tags, region, key, keyid, profile)
if not r.get('exists'):
if __opts__['test']:
ret['comment'] = 'RDS instance {0} would be created.'.format(name)
ret['result'] = None
return ret
r = __salt__['boto_rds.create'](name, allocated_storage,
db_instance_class, engine,
master_username,
master_user_password,
db_name, db_security_groups,
vpc_security_group_ids,
vpc_security_groups,
availability_zone,
db_subnet_group_name,
preferred_maintenance_window,
db_parameter_group_name,
backup_retention_period,
preferred_backup_window,
port, multi_az, engine_version,
auto_minor_version_upgrade,
license_model, iops,
option_group_name,
character_set_name,
publicly_accessible, wait_status,
tags, db_cluster_identifier,
storage_type, tde_credential_arn,
tde_credential_password,
storage_encrypted, kms_keyid,
domain, copy_tags_to_snapshot,
monitoring_interval,
monitoring_role_arn,
domain_iam_role_name, region,
promotion_tier, key, keyid, profile)
if not r.get('created'):
ret['result'] = False
ret['comment'] = 'Failed to create RDS instance {0}.'.format(r['error']['message'])
return ret
ret['changes']['old'] = {'instance': None}
ret['changes']['new'] = {
'instance': __salt__['boto_rds.describe_db_instances'](
name=name, jmespath='DBInstances[0]', region=region, key=key,
keyid=keyid, profile=profile)}
ret['comment'] = 'RDS instance {0} created.'.format(name)
else:
ret['comment'] = 'RDS instance {0} exists.'.format(name)
return ret | [
"def",
"present",
"(",
"name",
",",
"allocated_storage",
",",
"db_instance_class",
",",
"engine",
",",
"master_username",
",",
"master_user_password",
",",
"db_name",
"=",
"None",
",",
"storage_type",
"=",
"None",
",",
"db_security_groups",
"=",
"None",
",",
"vp... | Ensure RDS instance exists.
name
Name of the RDS state definition.
allocated_storage
The amount of storage (in gigabytes) to be initially allocated for the
database instance.
db_instance_class
The compute and memory capacity of the Amazon RDS DB instance.
engine
The name of the database engine to be used for this instance. Supported
engine types are: MySQL, mariadb, oracle-se1, oracle-se, oracle-ee, sqlserver-ee,
sqlserver-se, sqlserver-ex, sqlserver-web, postgres and aurora. For more
information, please see the ``engine`` argument in the Boto3 RDS
`create_db_instance`_ documentation.
master_username
The name of master user for the client DB instance.
master_user_password
The password for the master database user. Can be any printable ASCII
character except "/", '"', or "@".
db_name
The meaning of this parameter differs according to the database engine you use.
See the Boto3 RDS documentation to determine the appropriate value for your configuration.
https://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.create_db_instance
storage_type
Specifies the storage type to be associated with the DB instance.
Options are standard, gp2 and io1. If you specify io1, you must also include
a value for the Iops parameter.
db_security_groups
A list of DB security groups to associate with this DB instance.
vpc_security_group_ids
A list of EC2 VPC security group IDs to associate with this DB instance.
vpc_security_groups
A list of EC2 VPC security groups (IDs or Name tags) to associate with this DB instance.
availability_zone
The EC2 Availability Zone that the database instance will be created
in.
db_subnet_group_name
A DB subnet group to associate with this DB instance.
preferred_maintenance_window
The weekly time range (in UTC) during which system maintenance can
occur.
db_parameter_group_name
A DB parameter group to associate with this DB instance.
db_cluster_identifier
If the DB instance is a member of a DB cluster, contains the name of
the DB cluster that the DB instance is a member of.
tde_credential_arn
The ARN from the Key Store with which the instance is associated for
TDE encryption.
tde_credential_password
The password to use for TDE encryption if an encryption key is not used.
storage_encrypted
Specifies whether the DB instance is encrypted.
kms_keyid
If storage_encrypted is true, the KMS key identifier for the encrypted
DB instance.
backup_retention_period
The number of days for which automated backups are retained.
preferred_backup_window
The daily time range during which automated backups are created if
automated backups are enabled.
port
The port number on which the database accepts connections.
multi_az
Specifies if the DB instance is a Multi-AZ deployment. You cannot set
the AvailabilityZone parameter if the MultiAZ parameter is set to true.
engine_version
The version number of the database engine to use.
auto_minor_version_upgrade
Indicates that minor engine upgrades will be applied automatically to
the DB instance during the maintenance window.
license_model
License model information for this DB instance.
iops
The amount of Provisioned IOPS (input/output operations per second) to
be initially allocated for the DB instance.
option_group_name
Indicates that the DB instance should be associated with the specified
option group.
character_set_name
For supported engines, indicates that the DB instance should be
associated with the specified CharacterSet.
publicly_accessible
Specifies the accessibility options for the DB instance. A value of
true specifies an Internet-facing instance with a publicly resolvable
DNS name, which resolves to a public IP address. A value of false
specifies an internal instance with a DNS name that resolves to a
private IP address.
wait_status
Wait for the RDS instance to reach a desired status before finishing
the state. Available states: available, modifying, backing-up
tags
A dict of tags.
copy_tags_to_snapshot
Specifies whether tags are copied from the DB instance to snapshots of
the DB instance.
region
Region to connect to.
domain
The identifier of the Active Directory Domain.
key
AWS secret key to be used.
keyid
AWS access key to be used.
monitoring_interval
The interval, in seconds, between points when Enhanced Monitoring
metrics are collected for the DB instance.
monitoring_role_arn
The ARN for the IAM role that permits RDS to send Enhanced Monitoring
metrics to CloudWatch Logs.
domain_iam_role_name
Specify the name of the IAM role to be used when making API calls to
the Directory Service.
promotion_tier
A value that specifies the order in which an Aurora Replica is
promoted to the primary instance after a failure of the existing
primary instance. For more information, see Fault Tolerance for an
Aurora DB Cluster .
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
.. _create_db_instance: https://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.create_db_instance | [
"Ensure",
"RDS",
"instance",
"exists",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/boto_rds.py#L94-L361 | train |
saltstack/salt | salt/states/boto_rds.py | replica_present | def replica_present(name, source, db_instance_class=None,
availability_zone=None, port=None,
auto_minor_version_upgrade=None, iops=None,
option_group_name=None, publicly_accessible=None,
tags=None, region=None, key=None, keyid=None,
profile=None, db_parameter_group_name=None):
'''
Ensure RDS replica exists.
.. code-block:: yaml
Ensure myrds replica RDS exists:
boto_rds.create_replica:
- name: myreplica
- source: mydb
'''
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
replica_exists = __salt__['boto_rds.exists'](name, tags, region, key,
keyid, profile)
if not replica_exists.get('exists'):
if __opts__['test']:
ret['comment'] = 'RDS read replica {0} is set to be created '.format(name)
ret['result'] = None
return ret
created = __salt__['boto_rds.create_read_replica'](name, source,
db_instance_class,
availability_zone, port,
auto_minor_version_upgrade,
iops, option_group_name,
publicly_accessible,
tags, region, key,
keyid, profile)
if created:
ret['comment'] = 'RDS replica {0} created.'.format(name)
ret['changes']['old'] = {'instance': None}
ret['changes']['new'] = {
'instance': __salt__['boto_rds.describe_db_instances'](
name=name, jmespath='DBInstances[0]', region=region,
key=key, keyid=keyid, profile=profile)}
else:
ret['result'] = False
ret['comment'] = 'Failed to create RDS replica {0}.'.format(name)
else:
jmespath = 'DBInstances[0].DBParameterGroups[0].DBParameterGroupName'
pmg_name = __salt__['boto_rds.describe_db_instances'](name=name,
jmespath=jmespath, region=region, key=key, keyid=keyid,
profile=profile)
pmg_name = pmg_name[0] if pmg_name else None
if pmg_name != db_parameter_group_name:
modified = __salt__['boto_rds.modify_db_instance'](
name=name, db_parameter_group_name=db_parameter_group_name,
region=region, key=key, keyid=keyid, profile=profile)
if not modified:
ret['result'] = False
ret['comment'] = ('Failed to update parameter group of {0} RDS '
'instance.'.format(name))
ret['changes']['old'] = pmg_name
ret['changes']['new'] = db_parameter_group_name
ret['result'] = True
ret['comment'] = 'RDS replica {0} exists.'.format(name)
return ret | python | def replica_present(name, source, db_instance_class=None,
availability_zone=None, port=None,
auto_minor_version_upgrade=None, iops=None,
option_group_name=None, publicly_accessible=None,
tags=None, region=None, key=None, keyid=None,
profile=None, db_parameter_group_name=None):
'''
Ensure RDS replica exists.
.. code-block:: yaml
Ensure myrds replica RDS exists:
boto_rds.create_replica:
- name: myreplica
- source: mydb
'''
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
replica_exists = __salt__['boto_rds.exists'](name, tags, region, key,
keyid, profile)
if not replica_exists.get('exists'):
if __opts__['test']:
ret['comment'] = 'RDS read replica {0} is set to be created '.format(name)
ret['result'] = None
return ret
created = __salt__['boto_rds.create_read_replica'](name, source,
db_instance_class,
availability_zone, port,
auto_minor_version_upgrade,
iops, option_group_name,
publicly_accessible,
tags, region, key,
keyid, profile)
if created:
ret['comment'] = 'RDS replica {0} created.'.format(name)
ret['changes']['old'] = {'instance': None}
ret['changes']['new'] = {
'instance': __salt__['boto_rds.describe_db_instances'](
name=name, jmespath='DBInstances[0]', region=region,
key=key, keyid=keyid, profile=profile)}
else:
ret['result'] = False
ret['comment'] = 'Failed to create RDS replica {0}.'.format(name)
else:
jmespath = 'DBInstances[0].DBParameterGroups[0].DBParameterGroupName'
pmg_name = __salt__['boto_rds.describe_db_instances'](name=name,
jmespath=jmespath, region=region, key=key, keyid=keyid,
profile=profile)
pmg_name = pmg_name[0] if pmg_name else None
if pmg_name != db_parameter_group_name:
modified = __salt__['boto_rds.modify_db_instance'](
name=name, db_parameter_group_name=db_parameter_group_name,
region=region, key=key, keyid=keyid, profile=profile)
if not modified:
ret['result'] = False
ret['comment'] = ('Failed to update parameter group of {0} RDS '
'instance.'.format(name))
ret['changes']['old'] = pmg_name
ret['changes']['new'] = db_parameter_group_name
ret['result'] = True
ret['comment'] = 'RDS replica {0} exists.'.format(name)
return ret | [
"def",
"replica_present",
"(",
"name",
",",
"source",
",",
"db_instance_class",
"=",
"None",
",",
"availability_zone",
"=",
"None",
",",
"port",
"=",
"None",
",",
"auto_minor_version_upgrade",
"=",
"None",
",",
"iops",
"=",
"None",
",",
"option_group_name",
"=... | Ensure RDS replica exists.
.. code-block:: yaml
Ensure myrds replica RDS exists:
boto_rds.create_replica:
- name: myreplica
- source: mydb | [
"Ensure",
"RDS",
"replica",
"exists",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/boto_rds.py#L364-L428 | train |
saltstack/salt | salt/states/boto_rds.py | subnet_group_present | def subnet_group_present(name, description, subnet_ids=None, subnet_names=None,
tags=None, region=None, key=None, keyid=None,
profile=None):
'''
Ensure DB subnet group exists.
name
The name for the DB subnet group. This value is stored as a lowercase string.
subnet_ids
A list of the EC2 Subnet IDs for the DB subnet group.
Either subnet_ids or subnet_names must be provided.
subnet_names
A list of The EC2 Subnet names for the DB subnet group.
Either subnet_ids or subnet_names must be provided.
description
Subnet group description.
tags
A dict of tags.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
'''
if not salt.utils.data.exactly_one((subnet_ids, subnet_names)):
raise SaltInvocationError('One (but not both) of subnet_ids or '
'subnet_names must be provided.')
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
if not subnet_ids:
subnet_ids = []
if subnet_names:
for i in subnet_names:
r = __salt__['boto_vpc.get_resource_id']('subnet', name=i,
region=region,
key=key, keyid=keyid,
profile=profile)
if 'error' in r:
ret['comment'] = 'Error looking up subnet ids: {0}'.format(
r['error']['message'])
ret['result'] = False
return ret
if r['id'] is None:
ret['comment'] = 'Subnet {0} does not exist.'.format(i)
ret['result'] = False
return ret
subnet_ids.append(r['id'])
exists = __salt__['boto_rds.subnet_group_exists'](name=name, tags=tags, region=region, key=key,
keyid=keyid, profile=profile)
if not exists.get('exists'):
if __opts__['test']:
ret['comment'] = 'Subnet group {0} is set to be created.'.format(name)
ret['result'] = None
return ret
created = __salt__['boto_rds.create_subnet_group'](name=name,
description=description,
subnet_ids=subnet_ids,
tags=tags, region=region,
key=key, keyid=keyid,
profile=profile)
if not created:
ret['result'] = False
ret['comment'] = 'Failed to create {0} subnet group.'.format(name)
return ret
ret['changes']['old'] = None
ret['changes']['new'] = name
ret['comment'] = 'Subnet {0} created.'.format(name)
return ret
else:
ret['comment'] = 'Subnet {0} present.'.format(name)
return ret | python | def subnet_group_present(name, description, subnet_ids=None, subnet_names=None,
tags=None, region=None, key=None, keyid=None,
profile=None):
'''
Ensure DB subnet group exists.
name
The name for the DB subnet group. This value is stored as a lowercase string.
subnet_ids
A list of the EC2 Subnet IDs for the DB subnet group.
Either subnet_ids or subnet_names must be provided.
subnet_names
A list of The EC2 Subnet names for the DB subnet group.
Either subnet_ids or subnet_names must be provided.
description
Subnet group description.
tags
A dict of tags.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
'''
if not salt.utils.data.exactly_one((subnet_ids, subnet_names)):
raise SaltInvocationError('One (but not both) of subnet_ids or '
'subnet_names must be provided.')
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
if not subnet_ids:
subnet_ids = []
if subnet_names:
for i in subnet_names:
r = __salt__['boto_vpc.get_resource_id']('subnet', name=i,
region=region,
key=key, keyid=keyid,
profile=profile)
if 'error' in r:
ret['comment'] = 'Error looking up subnet ids: {0}'.format(
r['error']['message'])
ret['result'] = False
return ret
if r['id'] is None:
ret['comment'] = 'Subnet {0} does not exist.'.format(i)
ret['result'] = False
return ret
subnet_ids.append(r['id'])
exists = __salt__['boto_rds.subnet_group_exists'](name=name, tags=tags, region=region, key=key,
keyid=keyid, profile=profile)
if not exists.get('exists'):
if __opts__['test']:
ret['comment'] = 'Subnet group {0} is set to be created.'.format(name)
ret['result'] = None
return ret
created = __salt__['boto_rds.create_subnet_group'](name=name,
description=description,
subnet_ids=subnet_ids,
tags=tags, region=region,
key=key, keyid=keyid,
profile=profile)
if not created:
ret['result'] = False
ret['comment'] = 'Failed to create {0} subnet group.'.format(name)
return ret
ret['changes']['old'] = None
ret['changes']['new'] = name
ret['comment'] = 'Subnet {0} created.'.format(name)
return ret
else:
ret['comment'] = 'Subnet {0} present.'.format(name)
return ret | [
"def",
"subnet_group_present",
"(",
"name",
",",
"description",
",",
"subnet_ids",
"=",
"None",
",",
"subnet_names",
"=",
"None",
",",
"tags",
"=",
"None",
",",
"region",
"=",
"None",
",",
"key",
"=",
"None",
",",
"keyid",
"=",
"None",
",",
"profile",
... | Ensure DB subnet group exists.
name
The name for the DB subnet group. This value is stored as a lowercase string.
subnet_ids
A list of the EC2 Subnet IDs for the DB subnet group.
Either subnet_ids or subnet_names must be provided.
subnet_names
A list of The EC2 Subnet names for the DB subnet group.
Either subnet_ids or subnet_names must be provided.
description
Subnet group description.
tags
A dict of tags.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid. | [
"Ensure",
"DB",
"subnet",
"group",
"exists",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/boto_rds.py#L431-L523 | train |
saltstack/salt | salt/states/boto_rds.py | absent | def absent(name, skip_final_snapshot=None, final_db_snapshot_identifier=None,
tags=None, wait_for_deletion=True, timeout=180,
region=None, key=None, keyid=None, profile=None):
'''
Ensure RDS instance is absent.
name
Name of the RDS instance.
skip_final_snapshot
Whether a final db snapshot is created before the instance is deleted.
If True, no snapshot is created.
If False, a snapshot is created before deleting the instance.
final_db_snapshot_identifier
If a final snapshot is requested, this is the identifier used for that
snapshot.
tags
A dict of tags.
wait_for_deletion (bool)
Wait for the RDS instance to be deleted completely before finishing
the state.
timeout (in seconds)
The amount of time that can pass before raising an Exception.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
'''
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
current = __salt__['boto_rds.describe_db_instances'](
name=name, region=region, key=key, keyid=keyid, profile=profile)
if not current:
ret['result'] = True
ret['comment'] = '{0} RDS already absent.'.format(name)
return ret
if __opts__['test']:
ret['comment'] = 'RDS {0} would be removed.'.format(name)
ret['result'] = None
return ret
deleted = __salt__['boto_rds.delete'](name, skip_final_snapshot,
final_db_snapshot_identifier,
region, key, keyid, profile, tags,
wait_for_deletion, timeout)
if not deleted:
ret['result'] = False
ret['comment'] = 'Failed to delete {0} RDS.'.format(name)
return ret
ret['changes']['old'] = {'instance': current[0]}
ret['changes']['new'] = {'instance': None}
ret['comment'] = 'RDS {0} deleted.'.format(name)
return ret | python | def absent(name, skip_final_snapshot=None, final_db_snapshot_identifier=None,
tags=None, wait_for_deletion=True, timeout=180,
region=None, key=None, keyid=None, profile=None):
'''
Ensure RDS instance is absent.
name
Name of the RDS instance.
skip_final_snapshot
Whether a final db snapshot is created before the instance is deleted.
If True, no snapshot is created.
If False, a snapshot is created before deleting the instance.
final_db_snapshot_identifier
If a final snapshot is requested, this is the identifier used for that
snapshot.
tags
A dict of tags.
wait_for_deletion (bool)
Wait for the RDS instance to be deleted completely before finishing
the state.
timeout (in seconds)
The amount of time that can pass before raising an Exception.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
'''
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
current = __salt__['boto_rds.describe_db_instances'](
name=name, region=region, key=key, keyid=keyid, profile=profile)
if not current:
ret['result'] = True
ret['comment'] = '{0} RDS already absent.'.format(name)
return ret
if __opts__['test']:
ret['comment'] = 'RDS {0} would be removed.'.format(name)
ret['result'] = None
return ret
deleted = __salt__['boto_rds.delete'](name, skip_final_snapshot,
final_db_snapshot_identifier,
region, key, keyid, profile, tags,
wait_for_deletion, timeout)
if not deleted:
ret['result'] = False
ret['comment'] = 'Failed to delete {0} RDS.'.format(name)
return ret
ret['changes']['old'] = {'instance': current[0]}
ret['changes']['new'] = {'instance': None}
ret['comment'] = 'RDS {0} deleted.'.format(name)
return ret | [
"def",
"absent",
"(",
"name",
",",
"skip_final_snapshot",
"=",
"None",
",",
"final_db_snapshot_identifier",
"=",
"None",
",",
"tags",
"=",
"None",
",",
"wait_for_deletion",
"=",
"True",
",",
"timeout",
"=",
"180",
",",
"region",
"=",
"None",
",",
"key",
"=... | Ensure RDS instance is absent.
name
Name of the RDS instance.
skip_final_snapshot
Whether a final db snapshot is created before the instance is deleted.
If True, no snapshot is created.
If False, a snapshot is created before deleting the instance.
final_db_snapshot_identifier
If a final snapshot is requested, this is the identifier used for that
snapshot.
tags
A dict of tags.
wait_for_deletion (bool)
Wait for the RDS instance to be deleted completely before finishing
the state.
timeout (in seconds)
The amount of time that can pass before raising an Exception.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid. | [
"Ensure",
"RDS",
"instance",
"is",
"absent",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/boto_rds.py#L526-L595 | train |
saltstack/salt | salt/states/boto_rds.py | parameter_present | def parameter_present(name, db_parameter_group_family, description, parameters=None,
apply_method="pending-reboot", tags=None, region=None, key=None, keyid=None, profile=None):
'''
Ensure DB parameter group exists and update parameters.
name
The name for the parameter group.
db_parameter_group_family
The DB parameter group family name. A
DB parameter group can be associated with one and only one DB
parameter group family, and can be applied only to a DB instance
running a database engine and engine version compatible with that
DB parameter group family.
description
Parameter group description.
parameters
The DB parameters that need to be changed of type dictionary.
apply_method
The `apply-immediate` method can be used only for dynamic
parameters; the `pending-reboot` method can be used with MySQL
and Oracle DB instances for either dynamic or static
parameters. For Microsoft SQL Server DB instances, the
`pending-reboot` method can be used only for static
parameters.
tags
A dict of tags.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
'''
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
res = __salt__['boto_rds.parameter_group_exists'](name=name, tags=tags, region=region, key=key,
keyid=keyid, profile=profile)
if not res.get('exists'):
if __opts__['test']:
ret['comment'] = 'Parameter group {0} is set to be created.'.format(name)
ret['result'] = None
return ret
created = __salt__['boto_rds.create_parameter_group'](name=name, db_parameter_group_family=db_parameter_group_family,
description=description, tags=tags, region=region,
key=key, keyid=keyid, profile=profile)
if not created:
ret['result'] = False
ret['comment'] = 'Failed to create {0} parameter group.'.format(name)
return ret
ret['changes']['New Parameter Group'] = name
ret['comment'] = 'Parameter group {0} created.'.format(name)
else:
ret['comment'] = 'Parameter group {0} present.'.format(name)
if parameters is not None:
params = {}
changed = {}
for items in parameters:
for k, value in items.items():
if type(value) is bool:
params[k] = 'on' if value else 'off'
else:
params[k] = six.text_type(value)
log.debug('Parameters from user are : %s.', params)
options = __salt__['boto_rds.describe_parameters'](name=name, region=region, key=key, keyid=keyid, profile=profile)
if not options.get('result'):
ret['result'] = False
ret['comment'] = os.linesep.join([ret['comment'], 'Faled to get parameters for group {0}.'.format(name)])
return ret
for parameter in options['parameters'].values():
if parameter['ParameterName'] in params and params.get(parameter['ParameterName']) != six.text_type(parameter['ParameterValue']):
log.debug(
'Values that are being compared for %s are %s:%s.',
parameter['ParameterName'],
params.get(parameter['ParameterName']),
parameter['ParameterValue']
)
changed[parameter['ParameterName']] = params.get(parameter['ParameterName'])
if changed:
if __opts__['test']:
ret['comment'] = os.linesep.join([ret['comment'], 'Parameters {0} for group {1} are set to be changed.'.format(changed, name)])
ret['result'] = None
return ret
update = __salt__['boto_rds.update_parameter_group'](name, parameters=changed, apply_method=apply_method, tags=tags, region=region,
key=key, keyid=keyid, profile=profile)
if 'error' in update:
ret['result'] = False
ret['comment'] = os.linesep.join([ret['comment'], 'Failed to change parameters {0} for group {1}:'.format(changed, name), update['error']['message']])
return ret
ret['changes']['Parameters'] = changed
ret['comment'] = os.linesep.join([ret['comment'], 'Parameters {0} for group {1} are changed.'.format(changed, name)])
else:
ret['comment'] = os.linesep.join([ret['comment'], 'Parameters {0} for group {1} are present.'.format(params, name)])
return ret | python | def parameter_present(name, db_parameter_group_family, description, parameters=None,
apply_method="pending-reboot", tags=None, region=None, key=None, keyid=None, profile=None):
'''
Ensure DB parameter group exists and update parameters.
name
The name for the parameter group.
db_parameter_group_family
The DB parameter group family name. A
DB parameter group can be associated with one and only one DB
parameter group family, and can be applied only to a DB instance
running a database engine and engine version compatible with that
DB parameter group family.
description
Parameter group description.
parameters
The DB parameters that need to be changed of type dictionary.
apply_method
The `apply-immediate` method can be used only for dynamic
parameters; the `pending-reboot` method can be used with MySQL
and Oracle DB instances for either dynamic or static
parameters. For Microsoft SQL Server DB instances, the
`pending-reboot` method can be used only for static
parameters.
tags
A dict of tags.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
'''
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}
}
res = __salt__['boto_rds.parameter_group_exists'](name=name, tags=tags, region=region, key=key,
keyid=keyid, profile=profile)
if not res.get('exists'):
if __opts__['test']:
ret['comment'] = 'Parameter group {0} is set to be created.'.format(name)
ret['result'] = None
return ret
created = __salt__['boto_rds.create_parameter_group'](name=name, db_parameter_group_family=db_parameter_group_family,
description=description, tags=tags, region=region,
key=key, keyid=keyid, profile=profile)
if not created:
ret['result'] = False
ret['comment'] = 'Failed to create {0} parameter group.'.format(name)
return ret
ret['changes']['New Parameter Group'] = name
ret['comment'] = 'Parameter group {0} created.'.format(name)
else:
ret['comment'] = 'Parameter group {0} present.'.format(name)
if parameters is not None:
params = {}
changed = {}
for items in parameters:
for k, value in items.items():
if type(value) is bool:
params[k] = 'on' if value else 'off'
else:
params[k] = six.text_type(value)
log.debug('Parameters from user are : %s.', params)
options = __salt__['boto_rds.describe_parameters'](name=name, region=region, key=key, keyid=keyid, profile=profile)
if not options.get('result'):
ret['result'] = False
ret['comment'] = os.linesep.join([ret['comment'], 'Faled to get parameters for group {0}.'.format(name)])
return ret
for parameter in options['parameters'].values():
if parameter['ParameterName'] in params and params.get(parameter['ParameterName']) != six.text_type(parameter['ParameterValue']):
log.debug(
'Values that are being compared for %s are %s:%s.',
parameter['ParameterName'],
params.get(parameter['ParameterName']),
parameter['ParameterValue']
)
changed[parameter['ParameterName']] = params.get(parameter['ParameterName'])
if changed:
if __opts__['test']:
ret['comment'] = os.linesep.join([ret['comment'], 'Parameters {0} for group {1} are set to be changed.'.format(changed, name)])
ret['result'] = None
return ret
update = __salt__['boto_rds.update_parameter_group'](name, parameters=changed, apply_method=apply_method, tags=tags, region=region,
key=key, keyid=keyid, profile=profile)
if 'error' in update:
ret['result'] = False
ret['comment'] = os.linesep.join([ret['comment'], 'Failed to change parameters {0} for group {1}:'.format(changed, name), update['error']['message']])
return ret
ret['changes']['Parameters'] = changed
ret['comment'] = os.linesep.join([ret['comment'], 'Parameters {0} for group {1} are changed.'.format(changed, name)])
else:
ret['comment'] = os.linesep.join([ret['comment'], 'Parameters {0} for group {1} are present.'.format(params, name)])
return ret | [
"def",
"parameter_present",
"(",
"name",
",",
"db_parameter_group_family",
",",
"description",
",",
"parameters",
"=",
"None",
",",
"apply_method",
"=",
"\"pending-reboot\"",
",",
"tags",
"=",
"None",
",",
"region",
"=",
"None",
",",
"key",
"=",
"None",
",",
... | Ensure DB parameter group exists and update parameters.
name
The name for the parameter group.
db_parameter_group_family
The DB parameter group family name. A
DB parameter group can be associated with one and only one DB
parameter group family, and can be applied only to a DB instance
running a database engine and engine version compatible with that
DB parameter group family.
description
Parameter group description.
parameters
The DB parameters that need to be changed of type dictionary.
apply_method
The `apply-immediate` method can be used only for dynamic
parameters; the `pending-reboot` method can be used with MySQL
and Oracle DB instances for either dynamic or static
parameters. For Microsoft SQL Server DB instances, the
`pending-reboot` method can be used only for static
parameters.
tags
A dict of tags.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid. | [
"Ensure",
"DB",
"parameter",
"group",
"exists",
"and",
"update",
"parameters",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/boto_rds.py#L627-L734 | train |
saltstack/salt | salt/utils/find.py | _parse_interval | def _parse_interval(value):
'''
Convert an interval string like 1w3d6h into the number of seconds, time
resolution (1 unit of the smallest specified time unit) and the modifier(
'+', '-', or '').
w = week
d = day
h = hour
m = minute
s = second
'''
match = _INTERVAL_REGEX.match(six.text_type(value))
if match is None:
raise ValueError('invalid time interval: \'{0}\''.format(value))
result = 0
resolution = None
for name, multiplier in [('second', 1),
('minute', 60),
('hour', 60 * 60),
('day', 60 * 60 * 24),
('week', 60 * 60 * 24 * 7)]:
if match.group(name) is not None:
result += float(match.group(name)) * multiplier
if resolution is None:
resolution = multiplier
return result, resolution, match.group('modifier') | python | def _parse_interval(value):
'''
Convert an interval string like 1w3d6h into the number of seconds, time
resolution (1 unit of the smallest specified time unit) and the modifier(
'+', '-', or '').
w = week
d = day
h = hour
m = minute
s = second
'''
match = _INTERVAL_REGEX.match(six.text_type(value))
if match is None:
raise ValueError('invalid time interval: \'{0}\''.format(value))
result = 0
resolution = None
for name, multiplier in [('second', 1),
('minute', 60),
('hour', 60 * 60),
('day', 60 * 60 * 24),
('week', 60 * 60 * 24 * 7)]:
if match.group(name) is not None:
result += float(match.group(name)) * multiplier
if resolution is None:
resolution = multiplier
return result, resolution, match.group('modifier') | [
"def",
"_parse_interval",
"(",
"value",
")",
":",
"match",
"=",
"_INTERVAL_REGEX",
".",
"match",
"(",
"six",
".",
"text_type",
"(",
"value",
")",
")",
"if",
"match",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"'invalid time interval: \\'{0}\\''",
".",
"f... | Convert an interval string like 1w3d6h into the number of seconds, time
resolution (1 unit of the smallest specified time unit) and the modifier(
'+', '-', or '').
w = week
d = day
h = hour
m = minute
s = second | [
"Convert",
"an",
"interval",
"string",
"like",
"1w3d6h",
"into",
"the",
"number",
"of",
"seconds",
"time",
"resolution",
"(",
"1",
"unit",
"of",
"the",
"smallest",
"specified",
"time",
"unit",
")",
"and",
"the",
"modifier",
"(",
"+",
"-",
"or",
")",
".",... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/find.py#L152-L179 | train |
saltstack/salt | salt/utils/find.py | find | def find(path, options):
'''
WRITEME
'''
finder = Finder(options)
for path in finder.find(path):
yield path | python | def find(path, options):
'''
WRITEME
'''
finder = Finder(options)
for path in finder.find(path):
yield path | [
"def",
"find",
"(",
"path",
",",
"options",
")",
":",
"finder",
"=",
"Finder",
"(",
"options",
")",
"for",
"path",
"in",
"finder",
".",
"find",
"(",
"path",
")",
":",
"yield",
"path"
] | WRITEME | [
"WRITEME"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/find.py#L697-L703 | train |
saltstack/salt | salt/utils/find.py | Finder.find | def find(self, path):
'''
Generate filenames in path that satisfy criteria specified in
the constructor.
This method is a generator and should be repeatedly called
until there are no more results.
'''
if self.mindepth < 1:
dirpath, name = os.path.split(path)
match, fstat = self._check_criteria(dirpath, name, path)
if match:
for result in self._perform_actions(path, fstat=fstat):
yield result
for dirpath, dirs, files in salt.utils.path.os_walk(path):
relpath = os.path.relpath(dirpath, path)
depth = path_depth(relpath) + 1
if depth >= self.mindepth and (self.maxdepth is None or self.maxdepth >= depth):
for name in dirs + files:
fullpath = os.path.join(dirpath, name)
match, fstat = self._check_criteria(dirpath, name, fullpath)
if match:
for result in self._perform_actions(fullpath, fstat=fstat):
yield result
if self.maxdepth is not None and depth > self.maxdepth:
dirs[:] = [] | python | def find(self, path):
'''
Generate filenames in path that satisfy criteria specified in
the constructor.
This method is a generator and should be repeatedly called
until there are no more results.
'''
if self.mindepth < 1:
dirpath, name = os.path.split(path)
match, fstat = self._check_criteria(dirpath, name, path)
if match:
for result in self._perform_actions(path, fstat=fstat):
yield result
for dirpath, dirs, files in salt.utils.path.os_walk(path):
relpath = os.path.relpath(dirpath, path)
depth = path_depth(relpath) + 1
if depth >= self.mindepth and (self.maxdepth is None or self.maxdepth >= depth):
for name in dirs + files:
fullpath = os.path.join(dirpath, name)
match, fstat = self._check_criteria(dirpath, name, fullpath)
if match:
for result in self._perform_actions(fullpath, fstat=fstat):
yield result
if self.maxdepth is not None and depth > self.maxdepth:
dirs[:] = [] | [
"def",
"find",
"(",
"self",
",",
"path",
")",
":",
"if",
"self",
".",
"mindepth",
"<",
"1",
":",
"dirpath",
",",
"name",
"=",
"os",
".",
"path",
".",
"split",
"(",
"path",
")",
"match",
",",
"fstat",
"=",
"self",
".",
"_check_criteria",
"(",
"dir... | Generate filenames in path that satisfy criteria specified in
the constructor.
This method is a generator and should be repeatedly called
until there are no more results. | [
"Generate",
"filenames",
"in",
"path",
"that",
"satisfy",
"criteria",
"specified",
"in",
"the",
"constructor",
".",
"This",
"method",
"is",
"a",
"generator",
"and",
"should",
"be",
"repeatedly",
"called",
"until",
"there",
"are",
"no",
"more",
"results",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/find.py#L632-L658 | train |
saltstack/salt | salt/modules/pagerduty.py | list_services | def list_services(profile=None, api_key=None):
'''
List services belonging to this account
CLI Example:
salt myminion pagerduty.list_services my-pagerduty-account
'''
return salt.utils.pagerduty.list_items(
'services',
'name',
__salt__['config.option'](profile),
api_key,
opts=__opts__
) | python | def list_services(profile=None, api_key=None):
'''
List services belonging to this account
CLI Example:
salt myminion pagerduty.list_services my-pagerduty-account
'''
return salt.utils.pagerduty.list_items(
'services',
'name',
__salt__['config.option'](profile),
api_key,
opts=__opts__
) | [
"def",
"list_services",
"(",
"profile",
"=",
"None",
",",
"api_key",
"=",
"None",
")",
":",
"return",
"salt",
".",
"utils",
".",
"pagerduty",
".",
"list_items",
"(",
"'services'",
",",
"'name'",
",",
"__salt__",
"[",
"'config.option'",
"]",
"(",
"profile",... | List services belonging to this account
CLI Example:
salt myminion pagerduty.list_services my-pagerduty-account | [
"List",
"services",
"belonging",
"to",
"this",
"account"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/pagerduty.py#L37-L51 | train |
saltstack/salt | salt/modules/pagerduty.py | create_event | def create_event(service_key=None, description=None, details=None,
incident_key=None, profile=None):
'''
Create an event in PagerDuty. Designed for use in states.
CLI Example:
.. code-block:: yaml
salt myminion pagerduty.create_event <service_key> <description> <details> \
profile=my-pagerduty-account
The following parameters are required:
service_key
This key can be found by using pagerduty.list_services.
description
This is a short description of the event.
details
This can be a more detailed description of the event.
profile
This refers to the configuration profile to use to connect to the
PagerDuty service.
'''
trigger_url = 'https://events.pagerduty.com/generic/2010-04-15/create_event.json'
if isinstance(details, six.string_types):
details = salt.utils.yaml.safe_load(details)
if isinstance(details, six.string_types):
details = {'details': details}
ret = salt.utils.json.loads(salt.utils.pagerduty.query(
method='POST',
profile_dict=__salt__['config.option'](profile),
api_key=service_key,
data={
'service_key': service_key,
'incident_key': incident_key,
'event_type': 'trigger',
'description': description,
'details': details,
},
url=trigger_url,
opts=__opts__
))
return ret | python | def create_event(service_key=None, description=None, details=None,
incident_key=None, profile=None):
'''
Create an event in PagerDuty. Designed for use in states.
CLI Example:
.. code-block:: yaml
salt myminion pagerduty.create_event <service_key> <description> <details> \
profile=my-pagerduty-account
The following parameters are required:
service_key
This key can be found by using pagerduty.list_services.
description
This is a short description of the event.
details
This can be a more detailed description of the event.
profile
This refers to the configuration profile to use to connect to the
PagerDuty service.
'''
trigger_url = 'https://events.pagerduty.com/generic/2010-04-15/create_event.json'
if isinstance(details, six.string_types):
details = salt.utils.yaml.safe_load(details)
if isinstance(details, six.string_types):
details = {'details': details}
ret = salt.utils.json.loads(salt.utils.pagerduty.query(
method='POST',
profile_dict=__salt__['config.option'](profile),
api_key=service_key,
data={
'service_key': service_key,
'incident_key': incident_key,
'event_type': 'trigger',
'description': description,
'details': details,
},
url=trigger_url,
opts=__opts__
))
return ret | [
"def",
"create_event",
"(",
"service_key",
"=",
"None",
",",
"description",
"=",
"None",
",",
"details",
"=",
"None",
",",
"incident_key",
"=",
"None",
",",
"profile",
"=",
"None",
")",
":",
"trigger_url",
"=",
"'https://events.pagerduty.com/generic/2010-04-15/cre... | Create an event in PagerDuty. Designed for use in states.
CLI Example:
.. code-block:: yaml
salt myminion pagerduty.create_event <service_key> <description> <details> \
profile=my-pagerduty-account
The following parameters are required:
service_key
This key can be found by using pagerduty.list_services.
description
This is a short description of the event.
details
This can be a more detailed description of the event.
profile
This refers to the configuration profile to use to connect to the
PagerDuty service. | [
"Create",
"an",
"event",
"in",
"PagerDuty",
".",
"Designed",
"for",
"use",
"in",
"states",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/pagerduty.py#L149-L197 | train |
saltstack/salt | salt/states/win_certutil.py | add_store | def add_store(name, store, saltenv='base'):
'''
Store a certificate to the given store
name
The certificate to store, this can use local paths
or salt:// paths
store
The store to add the certificate to
saltenv
The salt environment to use, this is ignored if a local
path is specified
'''
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}}
cert_file = __salt__['cp.cache_file'](name, saltenv)
if cert_file is False:
ret['result'] = False
ret['comment'] += 'Certificate file not found.'
else:
cert_serial = __salt__['certutil.get_cert_serial'](cert_file)
serials = __salt__['certutil.get_stored_cert_serials'](store)
if cert_serial not in serials:
out = __salt__['certutil.add_store'](name, store)
if "successfully" in out:
ret['changes']['added'] = name
else:
ret['result'] = False
ret['comment'] += "Failed to store certificate {0}".format(name)
else:
ret['comment'] += "{0} already stored.".format(name)
return ret | python | def add_store(name, store, saltenv='base'):
'''
Store a certificate to the given store
name
The certificate to store, this can use local paths
or salt:// paths
store
The store to add the certificate to
saltenv
The salt environment to use, this is ignored if a local
path is specified
'''
ret = {'name': name,
'result': True,
'comment': '',
'changes': {}}
cert_file = __salt__['cp.cache_file'](name, saltenv)
if cert_file is False:
ret['result'] = False
ret['comment'] += 'Certificate file not found.'
else:
cert_serial = __salt__['certutil.get_cert_serial'](cert_file)
serials = __salt__['certutil.get_stored_cert_serials'](store)
if cert_serial not in serials:
out = __salt__['certutil.add_store'](name, store)
if "successfully" in out:
ret['changes']['added'] = name
else:
ret['result'] = False
ret['comment'] += "Failed to store certificate {0}".format(name)
else:
ret['comment'] += "{0} already stored.".format(name)
return ret | [
"def",
"add_store",
"(",
"name",
",",
"store",
",",
"saltenv",
"=",
"'base'",
")",
":",
"ret",
"=",
"{",
"'name'",
":",
"name",
",",
"'result'",
":",
"True",
",",
"'comment'",
":",
"''",
",",
"'changes'",
":",
"{",
"}",
"}",
"cert_file",
"=",
"__sa... | Store a certificate to the given store
name
The certificate to store, this can use local paths
or salt:// paths
store
The store to add the certificate to
saltenv
The salt environment to use, this is ignored if a local
path is specified | [
"Store",
"a",
"certificate",
"to",
"the",
"given",
"store"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/win_certutil.py#L34-L73 | train |
saltstack/salt | salt/client/mixins.py | SyncClientMixin.master_call | def master_call(self, **kwargs):
'''
Execute a function through the master network interface.
'''
load = kwargs
load['cmd'] = self.client
channel = salt.transport.client.ReqChannel.factory(self.opts,
crypt='clear',
usage='master_call')
try:
ret = channel.send(load)
finally:
channel.close()
if isinstance(ret, collections.Mapping):
if 'error' in ret:
salt.utils.error.raise_error(**ret['error'])
return ret | python | def master_call(self, **kwargs):
'''
Execute a function through the master network interface.
'''
load = kwargs
load['cmd'] = self.client
channel = salt.transport.client.ReqChannel.factory(self.opts,
crypt='clear',
usage='master_call')
try:
ret = channel.send(load)
finally:
channel.close()
if isinstance(ret, collections.Mapping):
if 'error' in ret:
salt.utils.error.raise_error(**ret['error'])
return ret | [
"def",
"master_call",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"load",
"=",
"kwargs",
"load",
"[",
"'cmd'",
"]",
"=",
"self",
".",
"client",
"channel",
"=",
"salt",
".",
"transport",
".",
"client",
".",
"ReqChannel",
".",
"factory",
"(",
"self... | Execute a function through the master network interface. | [
"Execute",
"a",
"function",
"through",
"the",
"master",
"network",
"interface",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/client/mixins.py#L139-L155 | train |
saltstack/salt | salt/client/mixins.py | SyncClientMixin.cmd_sync | def cmd_sync(self, low, timeout=None, full_return=False):
'''
Execute a runner function synchronously; eauth is respected
This function requires that :conf_master:`external_auth` is configured
and the user is authorized to execute runner functions: (``@runner``).
.. code-block:: python
runner.eauth_sync({
'fun': 'jobs.list_jobs',
'username': 'saltdev',
'password': 'saltdev',
'eauth': 'pam',
})
'''
event = salt.utils.event.get_master_event(self.opts, self.opts['sock_dir'], listen=True)
job = self.master_call(**low)
ret_tag = salt.utils.event.tagify('ret', base=job['tag'])
if timeout is None:
timeout = self.opts.get('rest_timeout', 300)
ret = event.get_event(tag=ret_tag, full=True, wait=timeout, auto_reconnect=True)
if ret is None:
raise salt.exceptions.SaltClientTimeout(
"RunnerClient job '{0}' timed out".format(job['jid']),
jid=job['jid'])
return ret if full_return else ret['data']['return'] | python | def cmd_sync(self, low, timeout=None, full_return=False):
'''
Execute a runner function synchronously; eauth is respected
This function requires that :conf_master:`external_auth` is configured
and the user is authorized to execute runner functions: (``@runner``).
.. code-block:: python
runner.eauth_sync({
'fun': 'jobs.list_jobs',
'username': 'saltdev',
'password': 'saltdev',
'eauth': 'pam',
})
'''
event = salt.utils.event.get_master_event(self.opts, self.opts['sock_dir'], listen=True)
job = self.master_call(**low)
ret_tag = salt.utils.event.tagify('ret', base=job['tag'])
if timeout is None:
timeout = self.opts.get('rest_timeout', 300)
ret = event.get_event(tag=ret_tag, full=True, wait=timeout, auto_reconnect=True)
if ret is None:
raise salt.exceptions.SaltClientTimeout(
"RunnerClient job '{0}' timed out".format(job['jid']),
jid=job['jid'])
return ret if full_return else ret['data']['return'] | [
"def",
"cmd_sync",
"(",
"self",
",",
"low",
",",
"timeout",
"=",
"None",
",",
"full_return",
"=",
"False",
")",
":",
"event",
"=",
"salt",
".",
"utils",
".",
"event",
".",
"get_master_event",
"(",
"self",
".",
"opts",
",",
"self",
".",
"opts",
"[",
... | Execute a runner function synchronously; eauth is respected
This function requires that :conf_master:`external_auth` is configured
and the user is authorized to execute runner functions: (``@runner``).
.. code-block:: python
runner.eauth_sync({
'fun': 'jobs.list_jobs',
'username': 'saltdev',
'password': 'saltdev',
'eauth': 'pam',
}) | [
"Execute",
"a",
"runner",
"function",
"synchronously",
";",
"eauth",
"is",
"respected"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/client/mixins.py#L157-L185 | train |
saltstack/salt | salt/client/mixins.py | SyncClientMixin.cmd | def cmd(self, fun, arg=None, pub_data=None, kwarg=None, print_event=True, full_return=False):
'''
Execute a function
.. code-block:: python
>>> opts = salt.config.master_config('/etc/salt/master')
>>> runner = salt.runner.RunnerClient(opts)
>>> runner.cmd('jobs.list_jobs', [])
{
'20131219215650131543': {
'Arguments': [300],
'Function': 'test.sleep',
'StartTime': '2013, Dec 19 21:56:50.131543',
'Target': '*',
'Target-type': 'glob',
'User': 'saltdev'
},
'20131219215921857715': {
'Arguments': [300],
'Function': 'test.sleep',
'StartTime': '2013, Dec 19 21:59:21.857715',
'Target': '*',
'Target-type': 'glob',
'User': 'saltdev'
},
}
'''
if arg is None:
arg = tuple()
if not isinstance(arg, list) and not isinstance(arg, tuple):
raise salt.exceptions.SaltInvocationError(
'arg must be formatted as a list/tuple'
)
if pub_data is None:
pub_data = {}
if not isinstance(pub_data, dict):
raise salt.exceptions.SaltInvocationError(
'pub_data must be formatted as a dictionary'
)
if kwarg is None:
kwarg = {}
if not isinstance(kwarg, dict):
raise salt.exceptions.SaltInvocationError(
'kwarg must be formatted as a dictionary'
)
arglist = salt.utils.args.parse_input(
arg,
no_parse=self.opts.get('no_parse', []))
# if you were passed kwarg, add it to arglist
if kwarg:
kwarg['__kwarg__'] = True
arglist.append(kwarg)
args, kwargs = salt.minion.load_args_and_kwargs(
self.functions[fun], arglist, pub_data
)
low = {'fun': fun,
'arg': args,
'kwarg': kwargs}
return self.low(fun, low, print_event=print_event, full_return=full_return) | python | def cmd(self, fun, arg=None, pub_data=None, kwarg=None, print_event=True, full_return=False):
'''
Execute a function
.. code-block:: python
>>> opts = salt.config.master_config('/etc/salt/master')
>>> runner = salt.runner.RunnerClient(opts)
>>> runner.cmd('jobs.list_jobs', [])
{
'20131219215650131543': {
'Arguments': [300],
'Function': 'test.sleep',
'StartTime': '2013, Dec 19 21:56:50.131543',
'Target': '*',
'Target-type': 'glob',
'User': 'saltdev'
},
'20131219215921857715': {
'Arguments': [300],
'Function': 'test.sleep',
'StartTime': '2013, Dec 19 21:59:21.857715',
'Target': '*',
'Target-type': 'glob',
'User': 'saltdev'
},
}
'''
if arg is None:
arg = tuple()
if not isinstance(arg, list) and not isinstance(arg, tuple):
raise salt.exceptions.SaltInvocationError(
'arg must be formatted as a list/tuple'
)
if pub_data is None:
pub_data = {}
if not isinstance(pub_data, dict):
raise salt.exceptions.SaltInvocationError(
'pub_data must be formatted as a dictionary'
)
if kwarg is None:
kwarg = {}
if not isinstance(kwarg, dict):
raise salt.exceptions.SaltInvocationError(
'kwarg must be formatted as a dictionary'
)
arglist = salt.utils.args.parse_input(
arg,
no_parse=self.opts.get('no_parse', []))
# if you were passed kwarg, add it to arglist
if kwarg:
kwarg['__kwarg__'] = True
arglist.append(kwarg)
args, kwargs = salt.minion.load_args_and_kwargs(
self.functions[fun], arglist, pub_data
)
low = {'fun': fun,
'arg': args,
'kwarg': kwargs}
return self.low(fun, low, print_event=print_event, full_return=full_return) | [
"def",
"cmd",
"(",
"self",
",",
"fun",
",",
"arg",
"=",
"None",
",",
"pub_data",
"=",
"None",
",",
"kwarg",
"=",
"None",
",",
"print_event",
"=",
"True",
",",
"full_return",
"=",
"False",
")",
":",
"if",
"arg",
"is",
"None",
":",
"arg",
"=",
"tup... | Execute a function
.. code-block:: python
>>> opts = salt.config.master_config('/etc/salt/master')
>>> runner = salt.runner.RunnerClient(opts)
>>> runner.cmd('jobs.list_jobs', [])
{
'20131219215650131543': {
'Arguments': [300],
'Function': 'test.sleep',
'StartTime': '2013, Dec 19 21:56:50.131543',
'Target': '*',
'Target-type': 'glob',
'User': 'saltdev'
},
'20131219215921857715': {
'Arguments': [300],
'Function': 'test.sleep',
'StartTime': '2013, Dec 19 21:59:21.857715',
'Target': '*',
'Target-type': 'glob',
'User': 'saltdev'
},
} | [
"Execute",
"a",
"function"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/client/mixins.py#L187-L249 | train |
saltstack/salt | salt/client/mixins.py | SyncClientMixin.store_job | def store_job(self):
'''
Helper that allows us to turn off storing jobs for different classes
that may incorporate this mixin.
'''
try:
class_name = self.__class__.__name__.lower()
except AttributeError:
log.warning(
'Unable to determine class name',
exc_info_on_loglevel=logging.DEBUG
)
return True
try:
return self.opts['{0}_returns'.format(class_name)]
except KeyError:
# No such option, assume this isn't one we care about gating and
# just return True.
return True | python | def store_job(self):
'''
Helper that allows us to turn off storing jobs for different classes
that may incorporate this mixin.
'''
try:
class_name = self.__class__.__name__.lower()
except AttributeError:
log.warning(
'Unable to determine class name',
exc_info_on_loglevel=logging.DEBUG
)
return True
try:
return self.opts['{0}_returns'.format(class_name)]
except KeyError:
# No such option, assume this isn't one we care about gating and
# just return True.
return True | [
"def",
"store_job",
"(",
"self",
")",
":",
"try",
":",
"class_name",
"=",
"self",
".",
"__class__",
".",
"__name__",
".",
"lower",
"(",
")",
"except",
"AttributeError",
":",
"log",
".",
"warning",
"(",
"'Unable to determine class name'",
",",
"exc_info_on_logl... | Helper that allows us to turn off storing jobs for different classes
that may incorporate this mixin. | [
"Helper",
"that",
"allows",
"us",
"to",
"turn",
"off",
"storing",
"jobs",
"for",
"different",
"classes",
"that",
"may",
"incorporate",
"this",
"mixin",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/client/mixins.py#L258-L277 | train |
saltstack/salt | salt/client/mixins.py | SyncClientMixin.low | def low(self, fun, low, print_event=True, full_return=False):
'''
Execute a function from low data
Low data includes:
required:
- fun: the name of the function to run
optional:
- arg: a list of args to pass to fun
- kwarg: kwargs for fun
- __user__: user who is running the command
- __jid__: jid to run under
- __tag__: tag to run under
'''
# fire the mminion loading (if not already done) here
# this is not to clutter the output with the module loading
# if we have a high debug level.
self.mminion # pylint: disable=W0104
jid = low.get('__jid__', salt.utils.jid.gen_jid(self.opts))
tag = low.get('__tag__', salt.utils.event.tagify(jid, prefix=self.tag_prefix))
data = {'fun': '{0}.{1}'.format(self.client, fun),
'jid': jid,
'user': low.get('__user__', 'UNKNOWN'),
}
event = salt.utils.event.get_event(
'master',
self.opts['sock_dir'],
self.opts['transport'],
opts=self.opts,
listen=False)
if print_event:
print_func = self.print_async_event \
if hasattr(self, 'print_async_event') \
else None
else:
# Suppress printing of return event (this keeps us from printing
# runner/wheel output during orchestration).
print_func = None
namespaced_event = salt.utils.event.NamespacedEvent(
event,
tag,
print_func=print_func
)
# TODO: test that they exist
# TODO: Other things to inject??
func_globals = {'__jid__': jid,
'__user__': data['user'],
'__tag__': tag,
# weak ref to avoid the Exception in interpreter
# teardown of event
'__jid_event__': weakref.proxy(namespaced_event),
}
try:
self_functions = pycopy.copy(self.functions)
salt.utils.lazy.verify_fun(self_functions, fun)
# Inject some useful globals to *all* the function's global
# namespace only once per module-- not per func
completed_funcs = []
for mod_name in six.iterkeys(self_functions):
if '.' not in mod_name:
continue
mod, _ = mod_name.split('.', 1)
if mod in completed_funcs:
continue
completed_funcs.append(mod)
for global_key, value in six.iteritems(func_globals):
self.functions[mod_name].__globals__[global_key] = value
# There are some discrepancies of what a "low" structure is in the
# publisher world it is a dict including stuff such as jid, fun,
# arg (a list of args, with kwargs packed in). Historically this
# particular one has had no "arg" and just has had all the kwargs
# packed into the top level object. The plan is to move away from
# that since the caller knows what is an arg vs a kwarg, but while
# we make the transition we will load "kwargs" using format_call if
# there are no kwargs in the low object passed in.
if 'arg' in low and 'kwarg' in low:
args = low['arg']
kwargs = low['kwarg']
else:
f_call = salt.utils.args.format_call(
self.functions[fun],
low,
expected_extra_kws=CLIENT_INTERNAL_KEYWORDS
)
args = f_call.get('args', ())
kwargs = f_call.get('kwargs', {})
# Update the event data with loaded args and kwargs
data['fun_args'] = list(args) + ([kwargs] if kwargs else [])
func_globals['__jid_event__'].fire_event(data, 'new')
# Track the job locally so we know what is running on the master
serial = salt.payload.Serial(self.opts)
jid_proc_file = os.path.join(*[self.opts['cachedir'], 'proc', jid])
data['pid'] = os.getpid()
with salt.utils.files.fopen(jid_proc_file, 'w+b') as fp_:
fp_.write(serial.dumps(data))
del data['pid']
# Initialize a context for executing the method.
with tornado.stack_context.StackContext(self.functions.context_dict.clone):
func = self.functions[fun]
try:
data['return'] = func(*args, **kwargs)
except TypeError as exc:
data['return'] = salt.utils.text.cli_info('Error: {exc}\nUsage:\n{doc}'.format(
exc=exc, doc=func.__doc__), 'Passed invalid arguments')
except Exception as exc:
data['return'] = salt.utils.text.cli_info(six.text_type(exc), 'General error occurred')
try:
data['success'] = self.context.get('retcode', 0) == 0
except AttributeError:
# Assume a True result if no context attribute
data['success'] = True
if isinstance(data['return'], dict) and 'data' in data['return']:
# some functions can return boolean values
data['success'] = salt.utils.state.check_result(data['return']['data'])
except (Exception, SystemExit) as ex:
if isinstance(ex, salt.exceptions.NotImplemented):
data['return'] = six.text_type(ex)
else:
data['return'] = 'Exception occurred in {client} {fun}: {tb}'.format(
client=self.client, fun=fun, tb=traceback.format_exc())
data['success'] = False
finally:
# Job has finished or issue found, so let's clean up after ourselves
try:
os.remove(jid_proc_file)
except OSError as err:
log.error("Error attempting to remove master job tracker: %s", err)
if self.store_job:
try:
salt.utils.job.store_job(
self.opts,
{
'id': self.opts['id'],
'tgt': self.opts['id'],
'jid': data['jid'],
'return': data,
},
event=None,
mminion=self.mminion,
)
except salt.exceptions.SaltCacheError:
log.error('Could not store job cache info. '
'Job details for this run may be unavailable.')
# Outputters _can_ mutate data so write to the job cache first!
namespaced_event.fire_event(data, 'ret')
# if we fired an event, make sure to delete the event object.
# This will ensure that we call destroy, which will do the 0MQ linger
log.info('Runner completed: %s', data['jid'])
del event
del namespaced_event
return data if full_return else data['return'] | python | def low(self, fun, low, print_event=True, full_return=False):
'''
Execute a function from low data
Low data includes:
required:
- fun: the name of the function to run
optional:
- arg: a list of args to pass to fun
- kwarg: kwargs for fun
- __user__: user who is running the command
- __jid__: jid to run under
- __tag__: tag to run under
'''
# fire the mminion loading (if not already done) here
# this is not to clutter the output with the module loading
# if we have a high debug level.
self.mminion # pylint: disable=W0104
jid = low.get('__jid__', salt.utils.jid.gen_jid(self.opts))
tag = low.get('__tag__', salt.utils.event.tagify(jid, prefix=self.tag_prefix))
data = {'fun': '{0}.{1}'.format(self.client, fun),
'jid': jid,
'user': low.get('__user__', 'UNKNOWN'),
}
event = salt.utils.event.get_event(
'master',
self.opts['sock_dir'],
self.opts['transport'],
opts=self.opts,
listen=False)
if print_event:
print_func = self.print_async_event \
if hasattr(self, 'print_async_event') \
else None
else:
# Suppress printing of return event (this keeps us from printing
# runner/wheel output during orchestration).
print_func = None
namespaced_event = salt.utils.event.NamespacedEvent(
event,
tag,
print_func=print_func
)
# TODO: test that they exist
# TODO: Other things to inject??
func_globals = {'__jid__': jid,
'__user__': data['user'],
'__tag__': tag,
# weak ref to avoid the Exception in interpreter
# teardown of event
'__jid_event__': weakref.proxy(namespaced_event),
}
try:
self_functions = pycopy.copy(self.functions)
salt.utils.lazy.verify_fun(self_functions, fun)
# Inject some useful globals to *all* the function's global
# namespace only once per module-- not per func
completed_funcs = []
for mod_name in six.iterkeys(self_functions):
if '.' not in mod_name:
continue
mod, _ = mod_name.split('.', 1)
if mod in completed_funcs:
continue
completed_funcs.append(mod)
for global_key, value in six.iteritems(func_globals):
self.functions[mod_name].__globals__[global_key] = value
# There are some discrepancies of what a "low" structure is in the
# publisher world it is a dict including stuff such as jid, fun,
# arg (a list of args, with kwargs packed in). Historically this
# particular one has had no "arg" and just has had all the kwargs
# packed into the top level object. The plan is to move away from
# that since the caller knows what is an arg vs a kwarg, but while
# we make the transition we will load "kwargs" using format_call if
# there are no kwargs in the low object passed in.
if 'arg' in low and 'kwarg' in low:
args = low['arg']
kwargs = low['kwarg']
else:
f_call = salt.utils.args.format_call(
self.functions[fun],
low,
expected_extra_kws=CLIENT_INTERNAL_KEYWORDS
)
args = f_call.get('args', ())
kwargs = f_call.get('kwargs', {})
# Update the event data with loaded args and kwargs
data['fun_args'] = list(args) + ([kwargs] if kwargs else [])
func_globals['__jid_event__'].fire_event(data, 'new')
# Track the job locally so we know what is running on the master
serial = salt.payload.Serial(self.opts)
jid_proc_file = os.path.join(*[self.opts['cachedir'], 'proc', jid])
data['pid'] = os.getpid()
with salt.utils.files.fopen(jid_proc_file, 'w+b') as fp_:
fp_.write(serial.dumps(data))
del data['pid']
# Initialize a context for executing the method.
with tornado.stack_context.StackContext(self.functions.context_dict.clone):
func = self.functions[fun]
try:
data['return'] = func(*args, **kwargs)
except TypeError as exc:
data['return'] = salt.utils.text.cli_info('Error: {exc}\nUsage:\n{doc}'.format(
exc=exc, doc=func.__doc__), 'Passed invalid arguments')
except Exception as exc:
data['return'] = salt.utils.text.cli_info(six.text_type(exc), 'General error occurred')
try:
data['success'] = self.context.get('retcode', 0) == 0
except AttributeError:
# Assume a True result if no context attribute
data['success'] = True
if isinstance(data['return'], dict) and 'data' in data['return']:
# some functions can return boolean values
data['success'] = salt.utils.state.check_result(data['return']['data'])
except (Exception, SystemExit) as ex:
if isinstance(ex, salt.exceptions.NotImplemented):
data['return'] = six.text_type(ex)
else:
data['return'] = 'Exception occurred in {client} {fun}: {tb}'.format(
client=self.client, fun=fun, tb=traceback.format_exc())
data['success'] = False
finally:
# Job has finished or issue found, so let's clean up after ourselves
try:
os.remove(jid_proc_file)
except OSError as err:
log.error("Error attempting to remove master job tracker: %s", err)
if self.store_job:
try:
salt.utils.job.store_job(
self.opts,
{
'id': self.opts['id'],
'tgt': self.opts['id'],
'jid': data['jid'],
'return': data,
},
event=None,
mminion=self.mminion,
)
except salt.exceptions.SaltCacheError:
log.error('Could not store job cache info. '
'Job details for this run may be unavailable.')
# Outputters _can_ mutate data so write to the job cache first!
namespaced_event.fire_event(data, 'ret')
# if we fired an event, make sure to delete the event object.
# This will ensure that we call destroy, which will do the 0MQ linger
log.info('Runner completed: %s', data['jid'])
del event
del namespaced_event
return data if full_return else data['return'] | [
"def",
"low",
"(",
"self",
",",
"fun",
",",
"low",
",",
"print_event",
"=",
"True",
",",
"full_return",
"=",
"False",
")",
":",
"# fire the mminion loading (if not already done) here",
"# this is not to clutter the output with the module loading",
"# if we have a high debug l... | Execute a function from low data
Low data includes:
required:
- fun: the name of the function to run
optional:
- arg: a list of args to pass to fun
- kwarg: kwargs for fun
- __user__: user who is running the command
- __jid__: jid to run under
- __tag__: tag to run under | [
"Execute",
"a",
"function",
"from",
"low",
"data",
"Low",
"data",
"includes",
":",
"required",
":",
"-",
"fun",
":",
"the",
"name",
"of",
"the",
"function",
"to",
"run",
"optional",
":",
"-",
"arg",
":",
"a",
"list",
"of",
"args",
"to",
"pass",
"to",... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/client/mixins.py#L279-L444 | train |
saltstack/salt | salt/client/mixins.py | SyncClientMixin.get_docs | def get_docs(self, arg=None):
'''
Return a dictionary of functions and the inline documentation for each
'''
if arg:
if '*' in arg:
target_mod = arg
_use_fnmatch = True
else:
target_mod = arg + '.' if not arg.endswith('.') else arg
_use_fnmatch = False
if _use_fnmatch:
docs = [(fun, self.functions[fun].__doc__)
for fun in fnmatch.filter(self.functions, target_mod)]
else:
docs = [(fun, self.functions[fun].__doc__)
for fun in sorted(self.functions)
if fun == arg or fun.startswith(target_mod)]
else:
docs = [(fun, self.functions[fun].__doc__)
for fun in sorted(self.functions)]
docs = dict(docs)
return salt.utils.doc.strip_rst(docs) | python | def get_docs(self, arg=None):
'''
Return a dictionary of functions and the inline documentation for each
'''
if arg:
if '*' in arg:
target_mod = arg
_use_fnmatch = True
else:
target_mod = arg + '.' if not arg.endswith('.') else arg
_use_fnmatch = False
if _use_fnmatch:
docs = [(fun, self.functions[fun].__doc__)
for fun in fnmatch.filter(self.functions, target_mod)]
else:
docs = [(fun, self.functions[fun].__doc__)
for fun in sorted(self.functions)
if fun == arg or fun.startswith(target_mod)]
else:
docs = [(fun, self.functions[fun].__doc__)
for fun in sorted(self.functions)]
docs = dict(docs)
return salt.utils.doc.strip_rst(docs) | [
"def",
"get_docs",
"(",
"self",
",",
"arg",
"=",
"None",
")",
":",
"if",
"arg",
":",
"if",
"'*'",
"in",
"arg",
":",
"target_mod",
"=",
"arg",
"_use_fnmatch",
"=",
"True",
"else",
":",
"target_mod",
"=",
"arg",
"+",
"'.'",
"if",
"not",
"arg",
".",
... | Return a dictionary of functions and the inline documentation for each | [
"Return",
"a",
"dictionary",
"of",
"functions",
"and",
"the",
"inline",
"documentation",
"for",
"each"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/client/mixins.py#L446-L468 | train |
saltstack/salt | salt/client/mixins.py | AsyncClientMixin.asynchronous | def asynchronous(self, fun, low, user='UNKNOWN', pub=None):
'''
Execute the function in a multiprocess and return the event tag to use
to watch for the return
'''
async_pub = pub if pub is not None else self._gen_async_pub()
proc = salt.utils.process.SignalHandlingMultiprocessingProcess(
target=self._proc_function,
args=(fun, low, user, async_pub['tag'], async_pub['jid']))
with salt.utils.process.default_signals(signal.SIGINT, signal.SIGTERM):
# Reset current signals before starting the process in
# order not to inherit the current signal handlers
proc.start()
proc.join() # MUST join, otherwise we leave zombies all over
return async_pub | python | def asynchronous(self, fun, low, user='UNKNOWN', pub=None):
'''
Execute the function in a multiprocess and return the event tag to use
to watch for the return
'''
async_pub = pub if pub is not None else self._gen_async_pub()
proc = salt.utils.process.SignalHandlingMultiprocessingProcess(
target=self._proc_function,
args=(fun, low, user, async_pub['tag'], async_pub['jid']))
with salt.utils.process.default_signals(signal.SIGINT, signal.SIGTERM):
# Reset current signals before starting the process in
# order not to inherit the current signal handlers
proc.start()
proc.join() # MUST join, otherwise we leave zombies all over
return async_pub | [
"def",
"asynchronous",
"(",
"self",
",",
"fun",
",",
"low",
",",
"user",
"=",
"'UNKNOWN'",
",",
"pub",
"=",
"None",
")",
":",
"async_pub",
"=",
"pub",
"if",
"pub",
"is",
"not",
"None",
"else",
"self",
".",
"_gen_async_pub",
"(",
")",
"proc",
"=",
"... | Execute the function in a multiprocess and return the event tag to use
to watch for the return | [
"Execute",
"the",
"function",
"in",
"a",
"multiprocess",
"and",
"return",
"the",
"event",
"tag",
"to",
"use",
"to",
"watch",
"for",
"the",
"return"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/client/mixins.py#L525-L540 | train |
saltstack/salt | salt/client/mixins.py | AsyncClientMixin.print_async_event | def print_async_event(self, suffix, event):
'''
Print all of the events with the prefix 'tag'
'''
if not isinstance(event, dict):
return
# if we are "quiet", don't print
if self.opts.get('quiet', False):
return
# some suffixes we don't want to print
if suffix in ('new',):
return
try:
outputter = self.opts.get('output', event.get('outputter', None) or event.get('return').get('outputter'))
except AttributeError:
outputter = None
# if this is a ret, we have our own set of rules
if suffix == 'ret':
# Check if outputter was passed in the return data. If this is the case,
# then the return data will be a dict two keys: 'data' and 'outputter'
if isinstance(event.get('return'), dict) \
and set(event['return']) == set(('data', 'outputter')):
event_data = event['return']['data']
outputter = event['return']['outputter']
else:
event_data = event['return']
else:
event_data = {'suffix': suffix, 'event': event}
salt.output.display_output(event_data, outputter, self.opts) | python | def print_async_event(self, suffix, event):
'''
Print all of the events with the prefix 'tag'
'''
if not isinstance(event, dict):
return
# if we are "quiet", don't print
if self.opts.get('quiet', False):
return
# some suffixes we don't want to print
if suffix in ('new',):
return
try:
outputter = self.opts.get('output', event.get('outputter', None) or event.get('return').get('outputter'))
except AttributeError:
outputter = None
# if this is a ret, we have our own set of rules
if suffix == 'ret':
# Check if outputter was passed in the return data. If this is the case,
# then the return data will be a dict two keys: 'data' and 'outputter'
if isinstance(event.get('return'), dict) \
and set(event['return']) == set(('data', 'outputter')):
event_data = event['return']['data']
outputter = event['return']['outputter']
else:
event_data = event['return']
else:
event_data = {'suffix': suffix, 'event': event}
salt.output.display_output(event_data, outputter, self.opts) | [
"def",
"print_async_event",
"(",
"self",
",",
"suffix",
",",
"event",
")",
":",
"if",
"not",
"isinstance",
"(",
"event",
",",
"dict",
")",
":",
"return",
"# if we are \"quiet\", don't print",
"if",
"self",
".",
"opts",
".",
"get",
"(",
"'quiet'",
",",
"Fal... | Print all of the events with the prefix 'tag' | [
"Print",
"all",
"of",
"the",
"events",
"with",
"the",
"prefix",
"tag"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/client/mixins.py#L542-L575 | train |
saltstack/salt | doc/_ext/shorturls.py | write_urls_index | def write_urls_index(app, exc):
'''
Generate a JSON file to serve as an index for short-URL lookups
'''
inventory = os.path.join(app.builder.outdir, 'objects.inv')
objects = sphinx.ext.intersphinx.fetch_inventory(app, DOCS_URL, inventory)
with open(os.path.join(app.builder.outdir, 'shorturls.json'), 'w') as f:
json.dump(objects, f) | python | def write_urls_index(app, exc):
'''
Generate a JSON file to serve as an index for short-URL lookups
'''
inventory = os.path.join(app.builder.outdir, 'objects.inv')
objects = sphinx.ext.intersphinx.fetch_inventory(app, DOCS_URL, inventory)
with open(os.path.join(app.builder.outdir, 'shorturls.json'), 'w') as f:
json.dump(objects, f) | [
"def",
"write_urls_index",
"(",
"app",
",",
"exc",
")",
":",
"inventory",
"=",
"os",
".",
"path",
".",
"join",
"(",
"app",
".",
"builder",
".",
"outdir",
",",
"'objects.inv'",
")",
"objects",
"=",
"sphinx",
".",
"ext",
".",
"intersphinx",
".",
"fetch_i... | Generate a JSON file to serve as an index for short-URL lookups | [
"Generate",
"a",
"JSON",
"file",
"to",
"serve",
"as",
"an",
"index",
"for",
"short",
"-",
"URL",
"lookups"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/doc/_ext/shorturls.py#L11-L19 | train |
saltstack/salt | salt/utils/mac_utils.py | execute_return_success | def execute_return_success(cmd):
'''
Executes the passed command. Returns True if successful
:param str cmd: The command to run
:return: True if successful, otherwise False
:rtype: bool
:raises: Error if command fails or is not supported
'''
ret = _run_all(cmd)
if ret['retcode'] != 0 or 'not supported' in ret['stdout'].lower():
msg = 'Command Failed: {0}\n'.format(cmd)
msg += 'Return Code: {0}\n'.format(ret['retcode'])
msg += 'Output: {0}\n'.format(ret['stdout'])
msg += 'Error: {0}\n'.format(ret['stderr'])
raise CommandExecutionError(msg)
return True | python | def execute_return_success(cmd):
'''
Executes the passed command. Returns True if successful
:param str cmd: The command to run
:return: True if successful, otherwise False
:rtype: bool
:raises: Error if command fails or is not supported
'''
ret = _run_all(cmd)
if ret['retcode'] != 0 or 'not supported' in ret['stdout'].lower():
msg = 'Command Failed: {0}\n'.format(cmd)
msg += 'Return Code: {0}\n'.format(ret['retcode'])
msg += 'Output: {0}\n'.format(ret['stdout'])
msg += 'Error: {0}\n'.format(ret['stderr'])
raise CommandExecutionError(msg)
return True | [
"def",
"execute_return_success",
"(",
"cmd",
")",
":",
"ret",
"=",
"_run_all",
"(",
"cmd",
")",
"if",
"ret",
"[",
"'retcode'",
"]",
"!=",
"0",
"or",
"'not supported'",
"in",
"ret",
"[",
"'stdout'",
"]",
".",
"lower",
"(",
")",
":",
"msg",
"=",
"'Comm... | Executes the passed command. Returns True if successful
:param str cmd: The command to run
:return: True if successful, otherwise False
:rtype: bool
:raises: Error if command fails or is not supported | [
"Executes",
"the",
"passed",
"command",
".",
"Returns",
"True",
"if",
"successful"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/mac_utils.py#L144-L165 | train |
saltstack/salt | salt/utils/mac_utils.py | execute_return_result | def execute_return_result(cmd):
'''
Executes the passed command. Returns the standard out if successful
:param str cmd: The command to run
:return: The standard out of the command if successful, otherwise returns
an error
:rtype: str
:raises: Error if command fails or is not supported
'''
ret = _run_all(cmd)
if ret['retcode'] != 0 or 'not supported' in ret['stdout'].lower():
msg = 'Command Failed: {0}\n'.format(cmd)
msg += 'Return Code: {0}\n'.format(ret['retcode'])
msg += 'Output: {0}\n'.format(ret['stdout'])
msg += 'Error: {0}\n'.format(ret['stderr'])
raise CommandExecutionError(msg)
return ret['stdout'] | python | def execute_return_result(cmd):
'''
Executes the passed command. Returns the standard out if successful
:param str cmd: The command to run
:return: The standard out of the command if successful, otherwise returns
an error
:rtype: str
:raises: Error if command fails or is not supported
'''
ret = _run_all(cmd)
if ret['retcode'] != 0 or 'not supported' in ret['stdout'].lower():
msg = 'Command Failed: {0}\n'.format(cmd)
msg += 'Return Code: {0}\n'.format(ret['retcode'])
msg += 'Output: {0}\n'.format(ret['stdout'])
msg += 'Error: {0}\n'.format(ret['stderr'])
raise CommandExecutionError(msg)
return ret['stdout'] | [
"def",
"execute_return_result",
"(",
"cmd",
")",
":",
"ret",
"=",
"_run_all",
"(",
"cmd",
")",
"if",
"ret",
"[",
"'retcode'",
"]",
"!=",
"0",
"or",
"'not supported'",
"in",
"ret",
"[",
"'stdout'",
"]",
".",
"lower",
"(",
")",
":",
"msg",
"=",
"'Comma... | Executes the passed command. Returns the standard out if successful
:param str cmd: The command to run
:return: The standard out of the command if successful, otherwise returns
an error
:rtype: str
:raises: Error if command fails or is not supported | [
"Executes",
"the",
"passed",
"command",
".",
"Returns",
"the",
"standard",
"out",
"if",
"successful"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/mac_utils.py#L168-L189 | train |
saltstack/salt | salt/utils/mac_utils.py | validate_enabled | def validate_enabled(enabled):
'''
Helper function to validate the enabled parameter. Boolean values are
converted to "on" and "off". String values are checked to make sure they are
either "on" or "off"/"yes" or "no". Integer ``0`` will return "off". All
other integers will return "on"
:param enabled: Enabled can be boolean True or False, Integers, or string
values "on" and "off"/"yes" and "no".
:type: str, int, bool
:return: "on" or "off" or errors
:rtype: str
'''
if isinstance(enabled, six.string_types):
if enabled.lower() not in ['on', 'off', 'yes', 'no']:
msg = '\nMac Power: Invalid String Value for Enabled.\n' \
'String values must be \'on\' or \'off\'/\'yes\' or \'no\'.\n' \
'Passed: {0}'.format(enabled)
raise SaltInvocationError(msg)
return 'on' if enabled.lower() in ['on', 'yes'] else 'off'
return 'on' if bool(enabled) else 'off' | python | def validate_enabled(enabled):
'''
Helper function to validate the enabled parameter. Boolean values are
converted to "on" and "off". String values are checked to make sure they are
either "on" or "off"/"yes" or "no". Integer ``0`` will return "off". All
other integers will return "on"
:param enabled: Enabled can be boolean True or False, Integers, or string
values "on" and "off"/"yes" and "no".
:type: str, int, bool
:return: "on" or "off" or errors
:rtype: str
'''
if isinstance(enabled, six.string_types):
if enabled.lower() not in ['on', 'off', 'yes', 'no']:
msg = '\nMac Power: Invalid String Value for Enabled.\n' \
'String values must be \'on\' or \'off\'/\'yes\' or \'no\'.\n' \
'Passed: {0}'.format(enabled)
raise SaltInvocationError(msg)
return 'on' if enabled.lower() in ['on', 'yes'] else 'off'
return 'on' if bool(enabled) else 'off' | [
"def",
"validate_enabled",
"(",
"enabled",
")",
":",
"if",
"isinstance",
"(",
"enabled",
",",
"six",
".",
"string_types",
")",
":",
"if",
"enabled",
".",
"lower",
"(",
")",
"not",
"in",
"[",
"'on'",
",",
"'off'",
",",
"'yes'",
",",
"'no'",
"]",
":",
... | Helper function to validate the enabled parameter. Boolean values are
converted to "on" and "off". String values are checked to make sure they are
either "on" or "off"/"yes" or "no". Integer ``0`` will return "off". All
other integers will return "on"
:param enabled: Enabled can be boolean True or False, Integers, or string
values "on" and "off"/"yes" and "no".
:type: str, int, bool
:return: "on" or "off" or errors
:rtype: str | [
"Helper",
"function",
"to",
"validate",
"the",
"enabled",
"parameter",
".",
"Boolean",
"values",
"are",
"converted",
"to",
"on",
"and",
"off",
".",
"String",
"values",
"are",
"checked",
"to",
"make",
"sure",
"they",
"are",
"either",
"on",
"or",
"off",
"/",... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/mac_utils.py#L213-L236 | train |
saltstack/salt | salt/utils/mac_utils.py | confirm_updated | def confirm_updated(value, check_fun, normalize_ret=False, wait=5):
'''
Wait up to ``wait`` seconds for a system parameter to be changed before
deciding it hasn't changed.
:param str value: The value indicating a successful change
:param function check_fun: The function whose return is compared with
``value``
:param bool normalize_ret: Whether to normalize the return from
``check_fun`` with ``validate_enabled``
:param int wait: The maximum amount of seconds to wait for a system
parameter to change
'''
for i in range(wait):
state = validate_enabled(check_fun()) if normalize_ret else check_fun()
if value in state:
return True
time.sleep(1)
return False | python | def confirm_updated(value, check_fun, normalize_ret=False, wait=5):
'''
Wait up to ``wait`` seconds for a system parameter to be changed before
deciding it hasn't changed.
:param str value: The value indicating a successful change
:param function check_fun: The function whose return is compared with
``value``
:param bool normalize_ret: Whether to normalize the return from
``check_fun`` with ``validate_enabled``
:param int wait: The maximum amount of seconds to wait for a system
parameter to change
'''
for i in range(wait):
state = validate_enabled(check_fun()) if normalize_ret else check_fun()
if value in state:
return True
time.sleep(1)
return False | [
"def",
"confirm_updated",
"(",
"value",
",",
"check_fun",
",",
"normalize_ret",
"=",
"False",
",",
"wait",
"=",
"5",
")",
":",
"for",
"i",
"in",
"range",
"(",
"wait",
")",
":",
"state",
"=",
"validate_enabled",
"(",
"check_fun",
"(",
")",
")",
"if",
... | Wait up to ``wait`` seconds for a system parameter to be changed before
deciding it hasn't changed.
:param str value: The value indicating a successful change
:param function check_fun: The function whose return is compared with
``value``
:param bool normalize_ret: Whether to normalize the return from
``check_fun`` with ``validate_enabled``
:param int wait: The maximum amount of seconds to wait for a system
parameter to change | [
"Wait",
"up",
"to",
"wait",
"seconds",
"for",
"a",
"system",
"parameter",
"to",
"be",
"changed",
"before",
"deciding",
"it",
"hasn",
"t",
"changed",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/mac_utils.py#L239-L260 | train |
saltstack/salt | salt/utils/mac_utils.py | launchctl | def launchctl(sub_cmd, *args, **kwargs):
'''
Run a launchctl command and raise an error if it fails
Args: additional args are passed to launchctl
sub_cmd (str): Sub command supplied to launchctl
Kwargs: passed to ``cmd.run_all``
return_stdout (bool): A keyword argument. If true return the stdout of
the launchctl command
Returns:
bool: ``True`` if successful
str: The stdout of the launchctl command if requested
Raises:
CommandExecutionError: If command fails
CLI Example:
.. code-block:: bash
import salt.utils.mac_service
salt.utils.mac_service.launchctl('debug', 'org.cups.cupsd')
'''
# Get return type
return_stdout = kwargs.pop('return_stdout', False)
# Construct command
cmd = ['launchctl', sub_cmd]
cmd.extend(args)
# Run command
kwargs['python_shell'] = False
kwargs = salt.utils.args.clean_kwargs(**kwargs)
ret = __salt__['cmd.run_all'](cmd, **kwargs)
error = _check_launchctl_stderr(ret)
# Raise an error or return successful result
if ret['retcode'] or error:
out = 'Failed to {0} service:\n'.format(sub_cmd)
out += 'stdout: {0}\n'.format(ret['stdout'])
out += 'stderr: {0}\n'.format(ret['stderr'])
out += 'retcode: {0}'.format(ret['retcode'])
raise CommandExecutionError(out)
else:
return ret['stdout'] if return_stdout else True | python | def launchctl(sub_cmd, *args, **kwargs):
'''
Run a launchctl command and raise an error if it fails
Args: additional args are passed to launchctl
sub_cmd (str): Sub command supplied to launchctl
Kwargs: passed to ``cmd.run_all``
return_stdout (bool): A keyword argument. If true return the stdout of
the launchctl command
Returns:
bool: ``True`` if successful
str: The stdout of the launchctl command if requested
Raises:
CommandExecutionError: If command fails
CLI Example:
.. code-block:: bash
import salt.utils.mac_service
salt.utils.mac_service.launchctl('debug', 'org.cups.cupsd')
'''
# Get return type
return_stdout = kwargs.pop('return_stdout', False)
# Construct command
cmd = ['launchctl', sub_cmd]
cmd.extend(args)
# Run command
kwargs['python_shell'] = False
kwargs = salt.utils.args.clean_kwargs(**kwargs)
ret = __salt__['cmd.run_all'](cmd, **kwargs)
error = _check_launchctl_stderr(ret)
# Raise an error or return successful result
if ret['retcode'] or error:
out = 'Failed to {0} service:\n'.format(sub_cmd)
out += 'stdout: {0}\n'.format(ret['stdout'])
out += 'stderr: {0}\n'.format(ret['stderr'])
out += 'retcode: {0}'.format(ret['retcode'])
raise CommandExecutionError(out)
else:
return ret['stdout'] if return_stdout else True | [
"def",
"launchctl",
"(",
"sub_cmd",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"# Get return type",
"return_stdout",
"=",
"kwargs",
".",
"pop",
"(",
"'return_stdout'",
",",
"False",
")",
"# Construct command",
"cmd",
"=",
"[",
"'launchctl'",
",",
... | Run a launchctl command and raise an error if it fails
Args: additional args are passed to launchctl
sub_cmd (str): Sub command supplied to launchctl
Kwargs: passed to ``cmd.run_all``
return_stdout (bool): A keyword argument. If true return the stdout of
the launchctl command
Returns:
bool: ``True`` if successful
str: The stdout of the launchctl command if requested
Raises:
CommandExecutionError: If command fails
CLI Example:
.. code-block:: bash
import salt.utils.mac_service
salt.utils.mac_service.launchctl('debug', 'org.cups.cupsd') | [
"Run",
"a",
"launchctl",
"command",
"and",
"raise",
"an",
"error",
"if",
"it",
"fails"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/mac_utils.py#L263-L309 | train |
saltstack/salt | salt/utils/mac_utils.py | _available_services | def _available_services(refresh=False):
'''
This is a helper function for getting the available macOS services.
The strategy is to look through the known system locations for
launchd plist files, parse them, and use their information for
populating the list of services. Services can run without a plist
file present, but normally services which have an automated startup
will have a plist file, so this is a minor compromise.
'''
try:
if __context__['available_services'] and not refresh:
log.debug('Found context for available services.')
__context__['using_cached_services'] = True
return __context__['available_services']
except KeyError:
pass
launchd_paths = [
'/Library/LaunchAgents',
'/Library/LaunchDaemons',
'/System/Library/LaunchAgents',
'/System/Library/LaunchDaemons',
]
try:
for user in os.listdir('/Users/'):
agent_path = '/Users/{}/Library/LaunchAgents'.format(user)
if os.path.isdir(agent_path):
launchd_paths.append(agent_path)
except OSError:
pass
_available_services = dict()
for launch_dir in launchd_paths:
for root, dirs, files in salt.utils.path.os_walk(launch_dir):
for file_name in files:
# Must be a plist file
if not file_name.endswith('.plist'):
continue
# Follow symbolic links of files in _launchd_paths
file_path = os.path.join(root, file_name)
true_path = os.path.realpath(file_path)
log.trace('Gathering service info for %s', true_path)
# ignore broken symlinks
if not os.path.exists(true_path):
continue
try:
if six.PY2:
# py2 plistlib can't read binary plists, and
# uses a different API than py3.
plist = plistlib.readPlist(true_path)
else:
with salt.utils.files.fopen(true_path, 'rb') as handle:
plist = plistlib.load(handle)
except plistlib.InvalidFileException:
# Raised in python3 if the file is not XML.
# There's nothing we can do; move on to the next one.
msg = 'Unable to parse "%s" as it is invalid XML: InvalidFileException.'
logging.warning(msg, true_path)
continue
except xml.parsers.expat.ExpatError:
# Raised by py2 for all errors.
# Raised by py3 if the file is XML, but with errors.
if six.PY3:
# There's an error in the XML, so move on.
msg = 'Unable to parse "%s" as it is invalid XML: xml.parsers.expat.ExpatError.'
logging.warning(msg, true_path)
continue
# Use the system provided plutil program to attempt
# conversion from binary.
cmd = '/usr/bin/plutil -convert xml1 -o - -- "{0}"'.format(
true_path)
try:
plist_xml = __salt__['cmd.run'](cmd)
plist = plistlib.readPlistFromString(plist_xml)
except xml.parsers.expat.ExpatError:
# There's still an error in the XML, so move on.
msg = 'Unable to parse "%s" as it is invalid XML: xml.parsers.expat.ExpatError.'
logging.warning(msg, true_path)
continue
try:
# not all launchd plists contain a Label key
_available_services[plist['Label'].lower()] = {
'file_name': file_name,
'file_path': true_path,
'plist': plist}
except KeyError:
log.debug('Service %s does not contain a'
' Label key. Skipping.', true_path)
continue
# put this in __context__ as this is a time consuming function.
# a fix for this issue. https://github.com/saltstack/salt/issues/48414
__context__['available_services'] = _available_services
# this is a fresh gathering of services, set cached to false
__context__['using_cached_services'] = False
return __context__['available_services'] | python | def _available_services(refresh=False):
'''
This is a helper function for getting the available macOS services.
The strategy is to look through the known system locations for
launchd plist files, parse them, and use their information for
populating the list of services. Services can run without a plist
file present, but normally services which have an automated startup
will have a plist file, so this is a minor compromise.
'''
try:
if __context__['available_services'] and not refresh:
log.debug('Found context for available services.')
__context__['using_cached_services'] = True
return __context__['available_services']
except KeyError:
pass
launchd_paths = [
'/Library/LaunchAgents',
'/Library/LaunchDaemons',
'/System/Library/LaunchAgents',
'/System/Library/LaunchDaemons',
]
try:
for user in os.listdir('/Users/'):
agent_path = '/Users/{}/Library/LaunchAgents'.format(user)
if os.path.isdir(agent_path):
launchd_paths.append(agent_path)
except OSError:
pass
_available_services = dict()
for launch_dir in launchd_paths:
for root, dirs, files in salt.utils.path.os_walk(launch_dir):
for file_name in files:
# Must be a plist file
if not file_name.endswith('.plist'):
continue
# Follow symbolic links of files in _launchd_paths
file_path = os.path.join(root, file_name)
true_path = os.path.realpath(file_path)
log.trace('Gathering service info for %s', true_path)
# ignore broken symlinks
if not os.path.exists(true_path):
continue
try:
if six.PY2:
# py2 plistlib can't read binary plists, and
# uses a different API than py3.
plist = plistlib.readPlist(true_path)
else:
with salt.utils.files.fopen(true_path, 'rb') as handle:
plist = plistlib.load(handle)
except plistlib.InvalidFileException:
# Raised in python3 if the file is not XML.
# There's nothing we can do; move on to the next one.
msg = 'Unable to parse "%s" as it is invalid XML: InvalidFileException.'
logging.warning(msg, true_path)
continue
except xml.parsers.expat.ExpatError:
# Raised by py2 for all errors.
# Raised by py3 if the file is XML, but with errors.
if six.PY3:
# There's an error in the XML, so move on.
msg = 'Unable to parse "%s" as it is invalid XML: xml.parsers.expat.ExpatError.'
logging.warning(msg, true_path)
continue
# Use the system provided plutil program to attempt
# conversion from binary.
cmd = '/usr/bin/plutil -convert xml1 -o - -- "{0}"'.format(
true_path)
try:
plist_xml = __salt__['cmd.run'](cmd)
plist = plistlib.readPlistFromString(plist_xml)
except xml.parsers.expat.ExpatError:
# There's still an error in the XML, so move on.
msg = 'Unable to parse "%s" as it is invalid XML: xml.parsers.expat.ExpatError.'
logging.warning(msg, true_path)
continue
try:
# not all launchd plists contain a Label key
_available_services[plist['Label'].lower()] = {
'file_name': file_name,
'file_path': true_path,
'plist': plist}
except KeyError:
log.debug('Service %s does not contain a'
' Label key. Skipping.', true_path)
continue
# put this in __context__ as this is a time consuming function.
# a fix for this issue. https://github.com/saltstack/salt/issues/48414
__context__['available_services'] = _available_services
# this is a fresh gathering of services, set cached to false
__context__['using_cached_services'] = False
return __context__['available_services'] | [
"def",
"_available_services",
"(",
"refresh",
"=",
"False",
")",
":",
"try",
":",
"if",
"__context__",
"[",
"'available_services'",
"]",
"and",
"not",
"refresh",
":",
"log",
".",
"debug",
"(",
"'Found context for available services.'",
")",
"__context__",
"[",
"... | This is a helper function for getting the available macOS services.
The strategy is to look through the known system locations for
launchd plist files, parse them, and use their information for
populating the list of services. Services can run without a plist
file present, but normally services which have an automated startup
will have a plist file, so this is a minor compromise. | [
"This",
"is",
"a",
"helper",
"function",
"for",
"getting",
"the",
"available",
"macOS",
"services",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/mac_utils.py#L312-L417 | train |
saltstack/salt | salt/utils/mac_utils.py | console_user | def console_user(username=False):
'''
Gets the UID or Username of the current console user.
:return: The uid or username of the console user.
:param bool username: Whether to return the username of the console
user instead of the UID. Defaults to False
:rtype: Interger of the UID, or a string of the username.
Raises:
CommandExecutionError: If we fail to get the UID.
CLI Example:
.. code-block:: bash
import salt.utils.mac_service
salt.utils.mac_service.console_user()
'''
try:
# returns the 'st_uid' stat from the /dev/console file.
uid = os.stat('/dev/console')[4]
except (OSError, IndexError):
# we should never get here but raise an error if so
raise CommandExecutionError('Failed to get a UID for the console user.')
if username:
return pwd.getpwuid(uid)[0]
return uid | python | def console_user(username=False):
'''
Gets the UID or Username of the current console user.
:return: The uid or username of the console user.
:param bool username: Whether to return the username of the console
user instead of the UID. Defaults to False
:rtype: Interger of the UID, or a string of the username.
Raises:
CommandExecutionError: If we fail to get the UID.
CLI Example:
.. code-block:: bash
import salt.utils.mac_service
salt.utils.mac_service.console_user()
'''
try:
# returns the 'st_uid' stat from the /dev/console file.
uid = os.stat('/dev/console')[4]
except (OSError, IndexError):
# we should never get here but raise an error if so
raise CommandExecutionError('Failed to get a UID for the console user.')
if username:
return pwd.getpwuid(uid)[0]
return uid | [
"def",
"console_user",
"(",
"username",
"=",
"False",
")",
":",
"try",
":",
"# returns the 'st_uid' stat from the /dev/console file.",
"uid",
"=",
"os",
".",
"stat",
"(",
"'/dev/console'",
")",
"[",
"4",
"]",
"except",
"(",
"OSError",
",",
"IndexError",
")",
"... | Gets the UID or Username of the current console user.
:return: The uid or username of the console user.
:param bool username: Whether to return the username of the console
user instead of the UID. Defaults to False
:rtype: Interger of the UID, or a string of the username.
Raises:
CommandExecutionError: If we fail to get the UID.
CLI Example:
.. code-block:: bash
import salt.utils.mac_service
salt.utils.mac_service.console_user() | [
"Gets",
"the",
"UID",
"or",
"Username",
"of",
"the",
"current",
"console",
"user",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/mac_utils.py#L441-L472 | train |
saltstack/salt | salt/states/pkgrepo.py | managed | def managed(name, ppa=None, **kwargs):
'''
This state manages software package repositories. Currently, :mod:`yum
<salt.modules.yumpkg>`, :mod:`apt <salt.modules.aptpkg>`, and :mod:`zypper
<salt.modules.zypper>` repositories are supported.
**YUM/DNF/ZYPPER-BASED SYSTEMS**
.. note::
One of ``baseurl`` or ``mirrorlist`` below is required. Additionally,
note that this state is not presently capable of managing more than one
repo in a single repo file, so each instance of this state will manage
a single repo file containing the configuration for a single repo.
name
This value will be used in two ways: Firstly, it will be the repo ID,
as seen in the entry in square brackets (e.g. ``[foo]``) for a given
repo. Secondly, it will be the name of the file as stored in
/etc/yum.repos.d (e.g. ``/etc/yum.repos.d/foo.conf``).
enabled : True
Whether or not the repo is enabled. Can be specified as True/False or
1/0.
disabled : False
Included to reduce confusion due to APT's use of the ``disabled``
argument. If this is passed for a YUM/DNF/Zypper-based distro, then the
reverse will be passed as ``enabled``. For example passing
``disabled=True`` will assume ``enabled=False``.
humanname
This is used as the "name" value in the repo file in
``/etc/yum.repos.d/`` (or ``/etc/zypp/repos.d`` for SUSE distros).
baseurl
The URL to a yum repository
mirrorlist
A URL which points to a file containing a collection of baseurls
comments
Sometimes you want to supply additional information, but not as
enabled configuration. Anything supplied for this list will be saved
in the repo configuration with a comment marker (#) in front.
gpgautoimport
Only valid for Zypper package manager. If set to True, automatically
trust and import public GPG key for the repository. The key should be
specified with ``gpgkey`` parameter. See details below.
Additional configuration values seen in YUM/DNF/Zypper repo files, such as
``gpgkey`` or ``gpgcheck``, will be used directly as key-value pairs.
For example:
.. code-block:: yaml
foo:
pkgrepo.managed:
- humanname: Personal repo for foo
- baseurl: https://mydomain.tld/repo/foo/$releasever/$basearch
- gpgkey: file:///etc/pki/rpm-gpg/foo-signing-key
- gpgcheck: 1
**APT-BASED SYSTEMS**
ppa
On Ubuntu, you can take advantage of Personal Package Archives on
Launchpad simply by specifying the user and archive name. The keyid
will be queried from launchpad and everything else is set
automatically. You can override any of the below settings by simply
setting them as you would normally. For example:
.. code-block:: yaml
logstash-ppa:
pkgrepo.managed:
- ppa: wolfnet/logstash
ppa_auth
For Ubuntu PPAs there can be private PPAs that require authentication
to access. For these PPAs the username/password can be passed as an
HTTP Basic style username/password combination.
.. code-block:: yaml
logstash-ppa:
pkgrepo.managed:
- ppa: wolfnet/logstash
- ppa_auth: username:password
name
On apt-based systems this must be the complete entry as it would be
seen in the sources.list file. This can have a limited subset of
components (i.e. 'main') which can be added/modified with the
``comps`` option.
.. code-block:: yaml
precise-repo:
pkgrepo.managed:
- name: deb http://us.archive.ubuntu.com/ubuntu precise main
.. note::
The above example is intended as a more readable way of configuring
the SLS, it is equivalent to the following:
.. code-block:: yaml
'deb http://us.archive.ubuntu.com/ubuntu precise main':
pkgrepo.managed
disabled : False
Toggles whether or not the repo is used for resolving dependencies
and/or installing packages.
enabled : True
Included to reduce confusion due to YUM/DNF/Zypper's use of the
``enabled`` argument. If this is passed for an APT-based distro, then
the reverse will be passed as ``disabled``. For example, passing
``enabled=False`` will assume ``disabled=False``.
architectures
On apt-based systems, architectures can restrict the available
architectures that the repository provides (e.g. only amd64).
architectures should be a comma-separated list.
comps
On apt-based systems, comps dictate the types of packages to be
installed from the repository (e.g. main, nonfree, ...). For
purposes of this, comps should be a comma-separated list.
file
The filename for the .list that the repository is configured in.
It is important to include the full-path AND make sure it is in
a directory that APT will look in when handling packages
dist
This dictates the release of the distro the packages should be built
for. (e.g. unstable). This option is rarely needed.
keyid
The KeyID or a list of KeyIDs of the GPG key to install.
This option also requires the ``keyserver`` option to be set.
keyserver
This is the name of the keyserver to retrieve gpg keys from. The
``keyid`` option must also be set for this option to work.
key_url
URL to retrieve a GPG key from. Allows the usage of ``http://``,
``https://`` as well as ``salt://``.
.. note::
Use either ``keyid``/``keyserver`` or ``key_url``, but not both.
key_text
The string representation of the GPG key to install.
.. versionadded:: 2018.3.0
.. note::
Use either ``keyid``/``keyserver``, ``key_url``, or ``key_text`` but
not more than one method.
consolidate : False
If set to ``True``, this will consolidate all sources definitions to the
sources.list file, cleanup the now unused files, consolidate components
(e.g. main) for the same URI, type, and architecture to a single line,
and finally remove comments from the sources.list file. The consolidate
will run every time the state is processed. The option only needs to be
set on one repo managed by salt to take effect.
clean_file : False
If set to ``True``, empty the file before config repo
.. note::
Use with care. This can be dangerous if multiple sources are
configured in the same file.
.. versionadded:: 2015.8.0
refresh : True
If set to ``False`` this will skip refreshing the apt package database
on debian based systems.
refresh_db : True
.. deprecated:: 2018.3.0
Use ``refresh`` instead.
require_in
Set this to a list of pkg.installed or pkg.latest to trigger the
running of apt-get update prior to attempting to install these
packages. Setting a require in the pkg state will not work for this.
'''
if 'refresh_db' in kwargs:
salt.utils.versions.warn_until(
'Neon',
'The \'refresh_db\' argument to \'pkg.mod_repo\' has been '
'renamed to \'refresh\'. Support for using \'refresh_db\' will be '
'removed in the Neon release of Salt.'
)
kwargs['refresh'] = kwargs.pop('refresh_db')
ret = {'name': name,
'changes': {},
'result': None,
'comment': ''}
if 'pkg.get_repo' not in __salt__:
ret['result'] = False
ret['comment'] = 'Repo management not implemented on this platform'
return ret
if 'key_url' in kwargs and ('keyid' in kwargs or 'keyserver' in kwargs):
ret['result'] = False
ret['comment'] = 'You may not use both "keyid"/"keyserver" and ' \
'"key_url" argument.'
if 'key_text' in kwargs and ('keyid' in kwargs or 'keyserver' in kwargs):
ret['result'] = False
ret['comment'] = 'You may not use both "keyid"/"keyserver" and ' \
'"key_text" argument.'
if 'key_text' in kwargs and ('key_url' in kwargs):
ret['result'] = False
ret['comment'] = 'You may not use both "key_url" and ' \
'"key_text" argument.'
if 'repo' in kwargs:
ret['result'] = False
ret['comment'] = ('\'repo\' is not a supported argument for this '
'state. The \'name\' argument is probably what was '
'intended.')
return ret
enabled = kwargs.pop('enabled', None)
disabled = kwargs.pop('disabled', None)
if enabled is not None and disabled is not None:
ret['result'] = False
ret['comment'] = 'Only one of enabled/disabled is allowed'
return ret
elif enabled is None and disabled is None:
# If neither argument was passed we assume the repo will be enabled
enabled = True
repo = name
if __grains__['os'] in ('Ubuntu', 'Mint'):
if ppa is not None:
# overload the name/repo value for PPAs cleanly
# this allows us to have one code-path for PPAs
try:
repo = ':'.join(('ppa', ppa))
except TypeError:
repo = ':'.join(('ppa', six.text_type(ppa)))
kwargs['disabled'] = not salt.utils.data.is_true(enabled) \
if enabled is not None \
else salt.utils.data.is_true(disabled)
elif __grains__['os_family'] in ('RedHat', 'Suse'):
if 'humanname' in kwargs:
kwargs['name'] = kwargs.pop('humanname')
if 'name' not in kwargs:
# Fall back to the repo name if humanname not provided
kwargs['name'] = repo
kwargs['enabled'] = not salt.utils.data.is_true(disabled) \
if disabled is not None \
else salt.utils.data.is_true(enabled)
elif __grains__['os_family'] in ('NILinuxRT', 'Poky'):
# opkg is the pkg virtual
kwargs['enabled'] = not salt.utils.data.is_true(disabled) \
if disabled is not None \
else salt.utils.data.is_true(enabled)
for kwarg in _STATE_INTERNAL_KEYWORDS:
kwargs.pop(kwarg, None)
try:
pre = __salt__['pkg.get_repo'](repo=repo, **kwargs)
except CommandExecutionError as exc:
ret['result'] = False
ret['comment'] = \
'Failed to examine repo \'{0}\': {1}'.format(name, exc)
return ret
# This is because of how apt-sources works. This pushes distro logic
# out of the state itself and into a module that it makes more sense
# to use. Most package providers will simply return the data provided
# it doesn't require any "specialized" data massaging.
if 'pkg.expand_repo_def' in __salt__:
sanitizedkwargs = __salt__['pkg.expand_repo_def'](repo=repo, **kwargs)
else:
sanitizedkwargs = kwargs
if __grains__['os_family'] == 'Debian':
repo = salt.utils.pkg.deb.strip_uri(repo)
if pre:
#22412: Remove file attribute in case same repo is set up multiple times but with different files
pre.pop('file', None)
sanitizedkwargs.pop('file', None)
for kwarg in sanitizedkwargs:
if kwarg not in pre:
if kwarg == 'enabled':
# On a RedHat-based OS, 'enabled' is assumed to be true if
# not explicitly set, so we don't need to update the repo
# if it's desired to be enabled and the 'enabled' key is
# missing from the repo definition
if __grains__['os_family'] == 'RedHat':
if not salt.utils.data.is_true(sanitizedkwargs[kwarg]):
break
else:
break
else:
break
elif kwarg == 'comps':
if sorted(sanitizedkwargs[kwarg]) != sorted(pre[kwarg]):
break
elif kwarg == 'line' and __grains__['os_family'] == 'Debian':
# split the line and sort everything after the URL
sanitizedsplit = sanitizedkwargs[kwarg].split()
sanitizedsplit[3:] = sorted(sanitizedsplit[3:])
reposplit, _, pre_comments = \
[x.strip() for x in pre[kwarg].partition('#')]
reposplit = reposplit.split()
reposplit[3:] = sorted(reposplit[3:])
if sanitizedsplit != reposplit:
break
if 'comments' in kwargs:
post_comments = \
salt.utils.pkg.deb.combine_comments(kwargs['comments'])
if pre_comments != post_comments:
break
elif kwarg == 'comments' and __grains__['os_family'] == 'RedHat':
precomments = salt.utils.pkg.rpm.combine_comments(pre[kwarg])
kwargcomments = salt.utils.pkg.rpm.combine_comments(
sanitizedkwargs[kwarg])
if precomments != kwargcomments:
break
elif kwarg == 'architectures' and sanitizedkwargs[kwarg]:
if set(sanitizedkwargs[kwarg]) != set(pre[kwarg]):
break
else:
if __grains__['os_family'] in ('RedHat', 'Suse') \
and any(isinstance(x, bool) for x in
(sanitizedkwargs[kwarg], pre[kwarg])):
# This check disambiguates 1/0 from True/False
if salt.utils.data.is_true(sanitizedkwargs[kwarg]) != \
salt.utils.data.is_true(pre[kwarg]):
break
else:
if six.text_type(sanitizedkwargs[kwarg]) != six.text_type(pre[kwarg]):
break
else:
ret['result'] = True
ret['comment'] = ('Package repo \'{0}\' already configured'
.format(name))
return ret
if __opts__['test']:
ret['comment'] = (
'Package repo \'{0}\' would be configured. This may cause pkg '
'states to behave differently than stated if this action is '
'repeated without test=True, due to the differences in the '
'configured repositories.'.format(name)
)
if pre:
for kwarg in sanitizedkwargs:
if sanitizedkwargs.get(kwarg) != pre.get(kwarg):
ret['changes'][kwarg] = {'new': sanitizedkwargs.get(kwarg),
'old': pre.get(kwarg)}
else:
ret['changes']['repo'] = name
return ret
# empty file before configure
if kwargs.get('clean_file', False):
with salt.utils.files.fopen(kwargs['file'], 'w'):
pass
try:
if __grains__['os_family'] == 'Debian':
__salt__['pkg.mod_repo'](repo, saltenv=__env__, **kwargs)
else:
__salt__['pkg.mod_repo'](repo, **kwargs)
except Exception as exc:
# This is another way to pass information back from the mod_repo
# function.
ret['result'] = False
ret['comment'] = \
'Failed to configure repo \'{0}\': {1}'.format(name, exc)
return ret
try:
post = __salt__['pkg.get_repo'](repo=repo, **kwargs)
if pre:
for kwarg in sanitizedkwargs:
if post.get(kwarg) != pre.get(kwarg):
ret['changes'][kwarg] = {'new': post.get(kwarg),
'old': pre.get(kwarg)}
else:
ret['changes'] = {'repo': repo}
ret['result'] = True
ret['comment'] = 'Configured package repo \'{0}\''.format(name)
except Exception as exc:
ret['result'] = False
ret['comment'] = \
'Failed to confirm config of repo \'{0}\': {1}'.format(name, exc)
# Clear cache of available packages, if present, since changes to the
# repositories may change the packages that are available.
if ret['changes']:
sys.modules[
__salt__['test.ping'].__module__
].__context__.pop('pkg._avail', None)
return ret | python | def managed(name, ppa=None, **kwargs):
'''
This state manages software package repositories. Currently, :mod:`yum
<salt.modules.yumpkg>`, :mod:`apt <salt.modules.aptpkg>`, and :mod:`zypper
<salt.modules.zypper>` repositories are supported.
**YUM/DNF/ZYPPER-BASED SYSTEMS**
.. note::
One of ``baseurl`` or ``mirrorlist`` below is required. Additionally,
note that this state is not presently capable of managing more than one
repo in a single repo file, so each instance of this state will manage
a single repo file containing the configuration for a single repo.
name
This value will be used in two ways: Firstly, it will be the repo ID,
as seen in the entry in square brackets (e.g. ``[foo]``) for a given
repo. Secondly, it will be the name of the file as stored in
/etc/yum.repos.d (e.g. ``/etc/yum.repos.d/foo.conf``).
enabled : True
Whether or not the repo is enabled. Can be specified as True/False or
1/0.
disabled : False
Included to reduce confusion due to APT's use of the ``disabled``
argument. If this is passed for a YUM/DNF/Zypper-based distro, then the
reverse will be passed as ``enabled``. For example passing
``disabled=True`` will assume ``enabled=False``.
humanname
This is used as the "name" value in the repo file in
``/etc/yum.repos.d/`` (or ``/etc/zypp/repos.d`` for SUSE distros).
baseurl
The URL to a yum repository
mirrorlist
A URL which points to a file containing a collection of baseurls
comments
Sometimes you want to supply additional information, but not as
enabled configuration. Anything supplied for this list will be saved
in the repo configuration with a comment marker (#) in front.
gpgautoimport
Only valid for Zypper package manager. If set to True, automatically
trust and import public GPG key for the repository. The key should be
specified with ``gpgkey`` parameter. See details below.
Additional configuration values seen in YUM/DNF/Zypper repo files, such as
``gpgkey`` or ``gpgcheck``, will be used directly as key-value pairs.
For example:
.. code-block:: yaml
foo:
pkgrepo.managed:
- humanname: Personal repo for foo
- baseurl: https://mydomain.tld/repo/foo/$releasever/$basearch
- gpgkey: file:///etc/pki/rpm-gpg/foo-signing-key
- gpgcheck: 1
**APT-BASED SYSTEMS**
ppa
On Ubuntu, you can take advantage of Personal Package Archives on
Launchpad simply by specifying the user and archive name. The keyid
will be queried from launchpad and everything else is set
automatically. You can override any of the below settings by simply
setting them as you would normally. For example:
.. code-block:: yaml
logstash-ppa:
pkgrepo.managed:
- ppa: wolfnet/logstash
ppa_auth
For Ubuntu PPAs there can be private PPAs that require authentication
to access. For these PPAs the username/password can be passed as an
HTTP Basic style username/password combination.
.. code-block:: yaml
logstash-ppa:
pkgrepo.managed:
- ppa: wolfnet/logstash
- ppa_auth: username:password
name
On apt-based systems this must be the complete entry as it would be
seen in the sources.list file. This can have a limited subset of
components (i.e. 'main') which can be added/modified with the
``comps`` option.
.. code-block:: yaml
precise-repo:
pkgrepo.managed:
- name: deb http://us.archive.ubuntu.com/ubuntu precise main
.. note::
The above example is intended as a more readable way of configuring
the SLS, it is equivalent to the following:
.. code-block:: yaml
'deb http://us.archive.ubuntu.com/ubuntu precise main':
pkgrepo.managed
disabled : False
Toggles whether or not the repo is used for resolving dependencies
and/or installing packages.
enabled : True
Included to reduce confusion due to YUM/DNF/Zypper's use of the
``enabled`` argument. If this is passed for an APT-based distro, then
the reverse will be passed as ``disabled``. For example, passing
``enabled=False`` will assume ``disabled=False``.
architectures
On apt-based systems, architectures can restrict the available
architectures that the repository provides (e.g. only amd64).
architectures should be a comma-separated list.
comps
On apt-based systems, comps dictate the types of packages to be
installed from the repository (e.g. main, nonfree, ...). For
purposes of this, comps should be a comma-separated list.
file
The filename for the .list that the repository is configured in.
It is important to include the full-path AND make sure it is in
a directory that APT will look in when handling packages
dist
This dictates the release of the distro the packages should be built
for. (e.g. unstable). This option is rarely needed.
keyid
The KeyID or a list of KeyIDs of the GPG key to install.
This option also requires the ``keyserver`` option to be set.
keyserver
This is the name of the keyserver to retrieve gpg keys from. The
``keyid`` option must also be set for this option to work.
key_url
URL to retrieve a GPG key from. Allows the usage of ``http://``,
``https://`` as well as ``salt://``.
.. note::
Use either ``keyid``/``keyserver`` or ``key_url``, but not both.
key_text
The string representation of the GPG key to install.
.. versionadded:: 2018.3.0
.. note::
Use either ``keyid``/``keyserver``, ``key_url``, or ``key_text`` but
not more than one method.
consolidate : False
If set to ``True``, this will consolidate all sources definitions to the
sources.list file, cleanup the now unused files, consolidate components
(e.g. main) for the same URI, type, and architecture to a single line,
and finally remove comments from the sources.list file. The consolidate
will run every time the state is processed. The option only needs to be
set on one repo managed by salt to take effect.
clean_file : False
If set to ``True``, empty the file before config repo
.. note::
Use with care. This can be dangerous if multiple sources are
configured in the same file.
.. versionadded:: 2015.8.0
refresh : True
If set to ``False`` this will skip refreshing the apt package database
on debian based systems.
refresh_db : True
.. deprecated:: 2018.3.0
Use ``refresh`` instead.
require_in
Set this to a list of pkg.installed or pkg.latest to trigger the
running of apt-get update prior to attempting to install these
packages. Setting a require in the pkg state will not work for this.
'''
if 'refresh_db' in kwargs:
salt.utils.versions.warn_until(
'Neon',
'The \'refresh_db\' argument to \'pkg.mod_repo\' has been '
'renamed to \'refresh\'. Support for using \'refresh_db\' will be '
'removed in the Neon release of Salt.'
)
kwargs['refresh'] = kwargs.pop('refresh_db')
ret = {'name': name,
'changes': {},
'result': None,
'comment': ''}
if 'pkg.get_repo' not in __salt__:
ret['result'] = False
ret['comment'] = 'Repo management not implemented on this platform'
return ret
if 'key_url' in kwargs and ('keyid' in kwargs or 'keyserver' in kwargs):
ret['result'] = False
ret['comment'] = 'You may not use both "keyid"/"keyserver" and ' \
'"key_url" argument.'
if 'key_text' in kwargs and ('keyid' in kwargs or 'keyserver' in kwargs):
ret['result'] = False
ret['comment'] = 'You may not use both "keyid"/"keyserver" and ' \
'"key_text" argument.'
if 'key_text' in kwargs and ('key_url' in kwargs):
ret['result'] = False
ret['comment'] = 'You may not use both "key_url" and ' \
'"key_text" argument.'
if 'repo' in kwargs:
ret['result'] = False
ret['comment'] = ('\'repo\' is not a supported argument for this '
'state. The \'name\' argument is probably what was '
'intended.')
return ret
enabled = kwargs.pop('enabled', None)
disabled = kwargs.pop('disabled', None)
if enabled is not None and disabled is not None:
ret['result'] = False
ret['comment'] = 'Only one of enabled/disabled is allowed'
return ret
elif enabled is None and disabled is None:
# If neither argument was passed we assume the repo will be enabled
enabled = True
repo = name
if __grains__['os'] in ('Ubuntu', 'Mint'):
if ppa is not None:
# overload the name/repo value for PPAs cleanly
# this allows us to have one code-path for PPAs
try:
repo = ':'.join(('ppa', ppa))
except TypeError:
repo = ':'.join(('ppa', six.text_type(ppa)))
kwargs['disabled'] = not salt.utils.data.is_true(enabled) \
if enabled is not None \
else salt.utils.data.is_true(disabled)
elif __grains__['os_family'] in ('RedHat', 'Suse'):
if 'humanname' in kwargs:
kwargs['name'] = kwargs.pop('humanname')
if 'name' not in kwargs:
# Fall back to the repo name if humanname not provided
kwargs['name'] = repo
kwargs['enabled'] = not salt.utils.data.is_true(disabled) \
if disabled is not None \
else salt.utils.data.is_true(enabled)
elif __grains__['os_family'] in ('NILinuxRT', 'Poky'):
# opkg is the pkg virtual
kwargs['enabled'] = not salt.utils.data.is_true(disabled) \
if disabled is not None \
else salt.utils.data.is_true(enabled)
for kwarg in _STATE_INTERNAL_KEYWORDS:
kwargs.pop(kwarg, None)
try:
pre = __salt__['pkg.get_repo'](repo=repo, **kwargs)
except CommandExecutionError as exc:
ret['result'] = False
ret['comment'] = \
'Failed to examine repo \'{0}\': {1}'.format(name, exc)
return ret
# This is because of how apt-sources works. This pushes distro logic
# out of the state itself and into a module that it makes more sense
# to use. Most package providers will simply return the data provided
# it doesn't require any "specialized" data massaging.
if 'pkg.expand_repo_def' in __salt__:
sanitizedkwargs = __salt__['pkg.expand_repo_def'](repo=repo, **kwargs)
else:
sanitizedkwargs = kwargs
if __grains__['os_family'] == 'Debian':
repo = salt.utils.pkg.deb.strip_uri(repo)
if pre:
#22412: Remove file attribute in case same repo is set up multiple times but with different files
pre.pop('file', None)
sanitizedkwargs.pop('file', None)
for kwarg in sanitizedkwargs:
if kwarg not in pre:
if kwarg == 'enabled':
# On a RedHat-based OS, 'enabled' is assumed to be true if
# not explicitly set, so we don't need to update the repo
# if it's desired to be enabled and the 'enabled' key is
# missing from the repo definition
if __grains__['os_family'] == 'RedHat':
if not salt.utils.data.is_true(sanitizedkwargs[kwarg]):
break
else:
break
else:
break
elif kwarg == 'comps':
if sorted(sanitizedkwargs[kwarg]) != sorted(pre[kwarg]):
break
elif kwarg == 'line' and __grains__['os_family'] == 'Debian':
# split the line and sort everything after the URL
sanitizedsplit = sanitizedkwargs[kwarg].split()
sanitizedsplit[3:] = sorted(sanitizedsplit[3:])
reposplit, _, pre_comments = \
[x.strip() for x in pre[kwarg].partition('#')]
reposplit = reposplit.split()
reposplit[3:] = sorted(reposplit[3:])
if sanitizedsplit != reposplit:
break
if 'comments' in kwargs:
post_comments = \
salt.utils.pkg.deb.combine_comments(kwargs['comments'])
if pre_comments != post_comments:
break
elif kwarg == 'comments' and __grains__['os_family'] == 'RedHat':
precomments = salt.utils.pkg.rpm.combine_comments(pre[kwarg])
kwargcomments = salt.utils.pkg.rpm.combine_comments(
sanitizedkwargs[kwarg])
if precomments != kwargcomments:
break
elif kwarg == 'architectures' and sanitizedkwargs[kwarg]:
if set(sanitizedkwargs[kwarg]) != set(pre[kwarg]):
break
else:
if __grains__['os_family'] in ('RedHat', 'Suse') \
and any(isinstance(x, bool) for x in
(sanitizedkwargs[kwarg], pre[kwarg])):
# This check disambiguates 1/0 from True/False
if salt.utils.data.is_true(sanitizedkwargs[kwarg]) != \
salt.utils.data.is_true(pre[kwarg]):
break
else:
if six.text_type(sanitizedkwargs[kwarg]) != six.text_type(pre[kwarg]):
break
else:
ret['result'] = True
ret['comment'] = ('Package repo \'{0}\' already configured'
.format(name))
return ret
if __opts__['test']:
ret['comment'] = (
'Package repo \'{0}\' would be configured. This may cause pkg '
'states to behave differently than stated if this action is '
'repeated without test=True, due to the differences in the '
'configured repositories.'.format(name)
)
if pre:
for kwarg in sanitizedkwargs:
if sanitizedkwargs.get(kwarg) != pre.get(kwarg):
ret['changes'][kwarg] = {'new': sanitizedkwargs.get(kwarg),
'old': pre.get(kwarg)}
else:
ret['changes']['repo'] = name
return ret
# empty file before configure
if kwargs.get('clean_file', False):
with salt.utils.files.fopen(kwargs['file'], 'w'):
pass
try:
if __grains__['os_family'] == 'Debian':
__salt__['pkg.mod_repo'](repo, saltenv=__env__, **kwargs)
else:
__salt__['pkg.mod_repo'](repo, **kwargs)
except Exception as exc:
# This is another way to pass information back from the mod_repo
# function.
ret['result'] = False
ret['comment'] = \
'Failed to configure repo \'{0}\': {1}'.format(name, exc)
return ret
try:
post = __salt__['pkg.get_repo'](repo=repo, **kwargs)
if pre:
for kwarg in sanitizedkwargs:
if post.get(kwarg) != pre.get(kwarg):
ret['changes'][kwarg] = {'new': post.get(kwarg),
'old': pre.get(kwarg)}
else:
ret['changes'] = {'repo': repo}
ret['result'] = True
ret['comment'] = 'Configured package repo \'{0}\''.format(name)
except Exception as exc:
ret['result'] = False
ret['comment'] = \
'Failed to confirm config of repo \'{0}\': {1}'.format(name, exc)
# Clear cache of available packages, if present, since changes to the
# repositories may change the packages that are available.
if ret['changes']:
sys.modules[
__salt__['test.ping'].__module__
].__context__.pop('pkg._avail', None)
return ret | [
"def",
"managed",
"(",
"name",
",",
"ppa",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"'refresh_db'",
"in",
"kwargs",
":",
"salt",
".",
"utils",
".",
"versions",
".",
"warn_until",
"(",
"'Neon'",
",",
"'The \\'refresh_db\\' argument to \\'pkg.mod_... | This state manages software package repositories. Currently, :mod:`yum
<salt.modules.yumpkg>`, :mod:`apt <salt.modules.aptpkg>`, and :mod:`zypper
<salt.modules.zypper>` repositories are supported.
**YUM/DNF/ZYPPER-BASED SYSTEMS**
.. note::
One of ``baseurl`` or ``mirrorlist`` below is required. Additionally,
note that this state is not presently capable of managing more than one
repo in a single repo file, so each instance of this state will manage
a single repo file containing the configuration for a single repo.
name
This value will be used in two ways: Firstly, it will be the repo ID,
as seen in the entry in square brackets (e.g. ``[foo]``) for a given
repo. Secondly, it will be the name of the file as stored in
/etc/yum.repos.d (e.g. ``/etc/yum.repos.d/foo.conf``).
enabled : True
Whether or not the repo is enabled. Can be specified as True/False or
1/0.
disabled : False
Included to reduce confusion due to APT's use of the ``disabled``
argument. If this is passed for a YUM/DNF/Zypper-based distro, then the
reverse will be passed as ``enabled``. For example passing
``disabled=True`` will assume ``enabled=False``.
humanname
This is used as the "name" value in the repo file in
``/etc/yum.repos.d/`` (or ``/etc/zypp/repos.d`` for SUSE distros).
baseurl
The URL to a yum repository
mirrorlist
A URL which points to a file containing a collection of baseurls
comments
Sometimes you want to supply additional information, but not as
enabled configuration. Anything supplied for this list will be saved
in the repo configuration with a comment marker (#) in front.
gpgautoimport
Only valid for Zypper package manager. If set to True, automatically
trust and import public GPG key for the repository. The key should be
specified with ``gpgkey`` parameter. See details below.
Additional configuration values seen in YUM/DNF/Zypper repo files, such as
``gpgkey`` or ``gpgcheck``, will be used directly as key-value pairs.
For example:
.. code-block:: yaml
foo:
pkgrepo.managed:
- humanname: Personal repo for foo
- baseurl: https://mydomain.tld/repo/foo/$releasever/$basearch
- gpgkey: file:///etc/pki/rpm-gpg/foo-signing-key
- gpgcheck: 1
**APT-BASED SYSTEMS**
ppa
On Ubuntu, you can take advantage of Personal Package Archives on
Launchpad simply by specifying the user and archive name. The keyid
will be queried from launchpad and everything else is set
automatically. You can override any of the below settings by simply
setting them as you would normally. For example:
.. code-block:: yaml
logstash-ppa:
pkgrepo.managed:
- ppa: wolfnet/logstash
ppa_auth
For Ubuntu PPAs there can be private PPAs that require authentication
to access. For these PPAs the username/password can be passed as an
HTTP Basic style username/password combination.
.. code-block:: yaml
logstash-ppa:
pkgrepo.managed:
- ppa: wolfnet/logstash
- ppa_auth: username:password
name
On apt-based systems this must be the complete entry as it would be
seen in the sources.list file. This can have a limited subset of
components (i.e. 'main') which can be added/modified with the
``comps`` option.
.. code-block:: yaml
precise-repo:
pkgrepo.managed:
- name: deb http://us.archive.ubuntu.com/ubuntu precise main
.. note::
The above example is intended as a more readable way of configuring
the SLS, it is equivalent to the following:
.. code-block:: yaml
'deb http://us.archive.ubuntu.com/ubuntu precise main':
pkgrepo.managed
disabled : False
Toggles whether or not the repo is used for resolving dependencies
and/or installing packages.
enabled : True
Included to reduce confusion due to YUM/DNF/Zypper's use of the
``enabled`` argument. If this is passed for an APT-based distro, then
the reverse will be passed as ``disabled``. For example, passing
``enabled=False`` will assume ``disabled=False``.
architectures
On apt-based systems, architectures can restrict the available
architectures that the repository provides (e.g. only amd64).
architectures should be a comma-separated list.
comps
On apt-based systems, comps dictate the types of packages to be
installed from the repository (e.g. main, nonfree, ...). For
purposes of this, comps should be a comma-separated list.
file
The filename for the .list that the repository is configured in.
It is important to include the full-path AND make sure it is in
a directory that APT will look in when handling packages
dist
This dictates the release of the distro the packages should be built
for. (e.g. unstable). This option is rarely needed.
keyid
The KeyID or a list of KeyIDs of the GPG key to install.
This option also requires the ``keyserver`` option to be set.
keyserver
This is the name of the keyserver to retrieve gpg keys from. The
``keyid`` option must also be set for this option to work.
key_url
URL to retrieve a GPG key from. Allows the usage of ``http://``,
``https://`` as well as ``salt://``.
.. note::
Use either ``keyid``/``keyserver`` or ``key_url``, but not both.
key_text
The string representation of the GPG key to install.
.. versionadded:: 2018.3.0
.. note::
Use either ``keyid``/``keyserver``, ``key_url``, or ``key_text`` but
not more than one method.
consolidate : False
If set to ``True``, this will consolidate all sources definitions to the
sources.list file, cleanup the now unused files, consolidate components
(e.g. main) for the same URI, type, and architecture to a single line,
and finally remove comments from the sources.list file. The consolidate
will run every time the state is processed. The option only needs to be
set on one repo managed by salt to take effect.
clean_file : False
If set to ``True``, empty the file before config repo
.. note::
Use with care. This can be dangerous if multiple sources are
configured in the same file.
.. versionadded:: 2015.8.0
refresh : True
If set to ``False`` this will skip refreshing the apt package database
on debian based systems.
refresh_db : True
.. deprecated:: 2018.3.0
Use ``refresh`` instead.
require_in
Set this to a list of pkg.installed or pkg.latest to trigger the
running of apt-get update prior to attempting to install these
packages. Setting a require in the pkg state will not work for this. | [
"This",
"state",
"manages",
"software",
"package",
"repositories",
".",
"Currently",
":",
"mod",
":",
"yum",
"<salt",
".",
"modules",
".",
"yumpkg",
">",
":",
"mod",
":",
"apt",
"<salt",
".",
"modules",
".",
"aptpkg",
">",
"and",
":",
"mod",
":",
"zypp... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/pkgrepo.py#L112-L535 | train |
saltstack/salt | salt/states/pkgrepo.py | absent | def absent(name, **kwargs):
'''
This function deletes the specified repo on the system, if it exists. It
is essentially a wrapper around pkg.del_repo.
name
The name of the package repo, as it would be referred to when running
the regular package manager commands.
**UBUNTU-SPECIFIC OPTIONS**
ppa
On Ubuntu, you can take advantage of Personal Package Archives on
Launchpad simply by specifying the user and archive name.
.. code-block:: yaml
logstash-ppa:
pkgrepo.absent:
- ppa: wolfnet/logstash
ppa_auth
For Ubuntu PPAs there can be private PPAs that require authentication
to access. For these PPAs the username/password can be specified. This
is required for matching if the name format uses the ``ppa:`` specifier
and is private (requires username/password to access, which is encoded
in the URI).
.. code-block:: yaml
logstash-ppa:
pkgrepo.absent:
- ppa: wolfnet/logstash
- ppa_auth: username:password
keyid
If passed, then the GPG key corresponding to the passed KeyID will also
be removed.
keyid_ppa : False
If set to ``True``, the GPG key's ID will be looked up from
ppa.launchpad.net and removed, and the ``keyid`` argument will be
ignored.
.. note::
This option will be disregarded unless the ``ppa`` argument is
present.
'''
ret = {'name': name,
'changes': {},
'result': None,
'comment': ''}
if 'ppa' in kwargs and __grains__['os'] in ('Ubuntu', 'Mint'):
name = kwargs.pop('ppa')
if not name.startswith('ppa:'):
name = 'ppa:' + name
remove_key = any(kwargs.get(x) is not None
for x in ('keyid', 'keyid_ppa'))
if remove_key and 'pkg.del_repo_key' not in __salt__:
ret['result'] = False
ret['comment'] = \
'Repo key management is not implemented for this platform'
return ret
try:
repo = __salt__['pkg.get_repo'](name, **kwargs)
except CommandExecutionError as exc:
ret['result'] = False
ret['comment'] = \
'Failed to configure repo \'{0}\': {1}'.format(name, exc)
return ret
if not repo:
ret['comment'] = 'Package repo {0} is absent'.format(name)
ret['result'] = True
return ret
if __opts__['test']:
ret['comment'] = ('Package repo \'{0}\' will be removed. This may '
'cause pkg states to behave differently than stated '
'if this action is repeated without test=True, due '
'to the differences in the configured repositories.'
.format(name))
return ret
try:
__salt__['pkg.del_repo'](repo=name, **kwargs)
except (CommandExecutionError, SaltInvocationError) as exc:
ret['result'] = False
ret['comment'] = exc.strerror
return ret
repos = __salt__['pkg.list_repos']()
if name not in repos:
ret['changes']['repo'] = name
ret['comment'] = 'Removed repo {0}'.format(name)
if not remove_key:
ret['result'] = True
else:
try:
removed_keyid = __salt__['pkg.del_repo_key'](name, **kwargs)
except (CommandExecutionError, SaltInvocationError) as exc:
ret['result'] = False
ret['comment'] += ', but failed to remove key: {0}'.format(exc)
else:
ret['result'] = True
ret['changes']['keyid'] = removed_keyid
ret['comment'] += ', and keyid {0}'.format(removed_keyid)
else:
ret['result'] = False
ret['comment'] = 'Failed to remove repo {0}'.format(name)
return ret | python | def absent(name, **kwargs):
'''
This function deletes the specified repo on the system, if it exists. It
is essentially a wrapper around pkg.del_repo.
name
The name of the package repo, as it would be referred to when running
the regular package manager commands.
**UBUNTU-SPECIFIC OPTIONS**
ppa
On Ubuntu, you can take advantage of Personal Package Archives on
Launchpad simply by specifying the user and archive name.
.. code-block:: yaml
logstash-ppa:
pkgrepo.absent:
- ppa: wolfnet/logstash
ppa_auth
For Ubuntu PPAs there can be private PPAs that require authentication
to access. For these PPAs the username/password can be specified. This
is required for matching if the name format uses the ``ppa:`` specifier
and is private (requires username/password to access, which is encoded
in the URI).
.. code-block:: yaml
logstash-ppa:
pkgrepo.absent:
- ppa: wolfnet/logstash
- ppa_auth: username:password
keyid
If passed, then the GPG key corresponding to the passed KeyID will also
be removed.
keyid_ppa : False
If set to ``True``, the GPG key's ID will be looked up from
ppa.launchpad.net and removed, and the ``keyid`` argument will be
ignored.
.. note::
This option will be disregarded unless the ``ppa`` argument is
present.
'''
ret = {'name': name,
'changes': {},
'result': None,
'comment': ''}
if 'ppa' in kwargs and __grains__['os'] in ('Ubuntu', 'Mint'):
name = kwargs.pop('ppa')
if not name.startswith('ppa:'):
name = 'ppa:' + name
remove_key = any(kwargs.get(x) is not None
for x in ('keyid', 'keyid_ppa'))
if remove_key and 'pkg.del_repo_key' not in __salt__:
ret['result'] = False
ret['comment'] = \
'Repo key management is not implemented for this platform'
return ret
try:
repo = __salt__['pkg.get_repo'](name, **kwargs)
except CommandExecutionError as exc:
ret['result'] = False
ret['comment'] = \
'Failed to configure repo \'{0}\': {1}'.format(name, exc)
return ret
if not repo:
ret['comment'] = 'Package repo {0} is absent'.format(name)
ret['result'] = True
return ret
if __opts__['test']:
ret['comment'] = ('Package repo \'{0}\' will be removed. This may '
'cause pkg states to behave differently than stated '
'if this action is repeated without test=True, due '
'to the differences in the configured repositories.'
.format(name))
return ret
try:
__salt__['pkg.del_repo'](repo=name, **kwargs)
except (CommandExecutionError, SaltInvocationError) as exc:
ret['result'] = False
ret['comment'] = exc.strerror
return ret
repos = __salt__['pkg.list_repos']()
if name not in repos:
ret['changes']['repo'] = name
ret['comment'] = 'Removed repo {0}'.format(name)
if not remove_key:
ret['result'] = True
else:
try:
removed_keyid = __salt__['pkg.del_repo_key'](name, **kwargs)
except (CommandExecutionError, SaltInvocationError) as exc:
ret['result'] = False
ret['comment'] += ', but failed to remove key: {0}'.format(exc)
else:
ret['result'] = True
ret['changes']['keyid'] = removed_keyid
ret['comment'] += ', and keyid {0}'.format(removed_keyid)
else:
ret['result'] = False
ret['comment'] = 'Failed to remove repo {0}'.format(name)
return ret | [
"def",
"absent",
"(",
"name",
",",
"*",
"*",
"kwargs",
")",
":",
"ret",
"=",
"{",
"'name'",
":",
"name",
",",
"'changes'",
":",
"{",
"}",
",",
"'result'",
":",
"None",
",",
"'comment'",
":",
"''",
"}",
"if",
"'ppa'",
"in",
"kwargs",
"and",
"__gra... | This function deletes the specified repo on the system, if it exists. It
is essentially a wrapper around pkg.del_repo.
name
The name of the package repo, as it would be referred to when running
the regular package manager commands.
**UBUNTU-SPECIFIC OPTIONS**
ppa
On Ubuntu, you can take advantage of Personal Package Archives on
Launchpad simply by specifying the user and archive name.
.. code-block:: yaml
logstash-ppa:
pkgrepo.absent:
- ppa: wolfnet/logstash
ppa_auth
For Ubuntu PPAs there can be private PPAs that require authentication
to access. For these PPAs the username/password can be specified. This
is required for matching if the name format uses the ``ppa:`` specifier
and is private (requires username/password to access, which is encoded
in the URI).
.. code-block:: yaml
logstash-ppa:
pkgrepo.absent:
- ppa: wolfnet/logstash
- ppa_auth: username:password
keyid
If passed, then the GPG key corresponding to the passed KeyID will also
be removed.
keyid_ppa : False
If set to ``True``, the GPG key's ID will be looked up from
ppa.launchpad.net and removed, and the ``keyid`` argument will be
ignored.
.. note::
This option will be disregarded unless the ``ppa`` argument is
present. | [
"This",
"function",
"deletes",
"the",
"specified",
"repo",
"on",
"the",
"system",
"if",
"it",
"exists",
".",
"It",
"is",
"essentially",
"a",
"wrapper",
"around",
"pkg",
".",
"del_repo",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/pkgrepo.py#L538-L653 | train |
saltstack/salt | salt/pillar/foreman.py | ext_pillar | def ext_pillar(minion_id,
pillar, # pylint: disable=W0613
key=None,
only=()):
'''
Read pillar data from Foreman via its API.
'''
url = __opts__['foreman.url']
user = __opts__['foreman.user']
password = __opts__['foreman.password']
api = __opts__['foreman.api']
verify = __opts__['foreman.verifyssl']
certfile = __opts__['foreman.certfile']
keyfile = __opts__['foreman.keyfile']
cafile = __opts__['foreman.cafile']
lookup_parameters = __opts__['foreman.lookup_parameters']
log.info("Querying Foreman at %r for information for %r", url, minion_id)
try:
# Foreman API version 1 is currently not supported
if api != 2:
log.error('Foreman API v2 is supported only, please specify'
'version 2 in your Salt master config')
raise Exception
headers = {'accept': 'version=' + six.text_type(api) + ',application/json'}
if verify and cafile is not None:
verify = cafile
resp = requests.get(
url + '/hosts/' + minion_id,
auth=(user, password),
headers=headers,
verify=verify,
cert=(certfile, keyfile)
)
result = resp.json()
log.debug('Raw response of the Foreman request is %r', result)
if lookup_parameters:
parameters = dict()
for param in result['all_parameters']:
parameters.update({param['name']: param['value']})
result['parameters'] = parameters
if only:
result = dict((k, result[k]) for k in only if k in result)
except Exception:
log.exception(
'Could not fetch host data via Foreman API:'
)
return {}
if key:
result = {key: result}
return result | python | def ext_pillar(minion_id,
pillar, # pylint: disable=W0613
key=None,
only=()):
'''
Read pillar data from Foreman via its API.
'''
url = __opts__['foreman.url']
user = __opts__['foreman.user']
password = __opts__['foreman.password']
api = __opts__['foreman.api']
verify = __opts__['foreman.verifyssl']
certfile = __opts__['foreman.certfile']
keyfile = __opts__['foreman.keyfile']
cafile = __opts__['foreman.cafile']
lookup_parameters = __opts__['foreman.lookup_parameters']
log.info("Querying Foreman at %r for information for %r", url, minion_id)
try:
# Foreman API version 1 is currently not supported
if api != 2:
log.error('Foreman API v2 is supported only, please specify'
'version 2 in your Salt master config')
raise Exception
headers = {'accept': 'version=' + six.text_type(api) + ',application/json'}
if verify and cafile is not None:
verify = cafile
resp = requests.get(
url + '/hosts/' + minion_id,
auth=(user, password),
headers=headers,
verify=verify,
cert=(certfile, keyfile)
)
result = resp.json()
log.debug('Raw response of the Foreman request is %r', result)
if lookup_parameters:
parameters = dict()
for param in result['all_parameters']:
parameters.update({param['name']: param['value']})
result['parameters'] = parameters
if only:
result = dict((k, result[k]) for k in only if k in result)
except Exception:
log.exception(
'Could not fetch host data via Foreman API:'
)
return {}
if key:
result = {key: result}
return result | [
"def",
"ext_pillar",
"(",
"minion_id",
",",
"pillar",
",",
"# pylint: disable=W0613",
"key",
"=",
"None",
",",
"only",
"=",
"(",
")",
")",
":",
"url",
"=",
"__opts__",
"[",
"'foreman.url'",
"]",
"user",
"=",
"__opts__",
"[",
"'foreman.user'",
"]",
"passwor... | Read pillar data from Foreman via its API. | [
"Read",
"pillar",
"data",
"from",
"Foreman",
"via",
"its",
"API",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/pillar/foreman.py#L81-L141 | train |
saltstack/salt | salt/modules/boto_dynamodb.py | create_table | def create_table(table_name, region=None, key=None, keyid=None, profile=None,
read_capacity_units=None, write_capacity_units=None,
hash_key=None, hash_key_data_type=None, range_key=None,
range_key_data_type=None, local_indexes=None,
global_indexes=None):
'''
Creates a DynamoDB table.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.create_table table_name /
region=us-east-1 /
hash_key=id /
hash_key_data_type=N /
range_key=created_at /
range_key_data_type=N /
read_capacity_units=1 /
write_capacity_units=1
'''
schema = []
primary_index_fields = []
primary_index_name = ''
if hash_key:
hash_key_obj = HashKey(hash_key, data_type=hash_key_data_type)
schema.append(hash_key_obj)
primary_index_fields.append(hash_key_obj)
primary_index_name += hash_key
if range_key:
range_key_obj = RangeKey(range_key, data_type=range_key_data_type)
schema.append(range_key_obj)
primary_index_fields.append(range_key_obj)
primary_index_name += '_'
primary_index_name += range_key
primary_index_name += '_index'
throughput = {
'read': read_capacity_units,
'write': write_capacity_units
}
local_table_indexes = []
if local_indexes:
for index in local_indexes:
local_table_indexes.append(extract_index(index))
global_table_indexes = []
if global_indexes:
for index in global_indexes:
global_table_indexes.append(
extract_index(index, global_index=True)
)
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
Table.create(
table_name,
schema=schema,
throughput=throughput,
indexes=local_table_indexes,
global_indexes=global_table_indexes,
connection=conn
)
# Table creation can take several seconds to propagate.
# We will check MAX_ATTEMPTS times.
MAX_ATTEMPTS = 30
for i in range(MAX_ATTEMPTS):
if exists(
table_name,
region,
key,
keyid,
profile
):
return True
else:
time.sleep(1) # sleep for one second and try again
return False | python | def create_table(table_name, region=None, key=None, keyid=None, profile=None,
read_capacity_units=None, write_capacity_units=None,
hash_key=None, hash_key_data_type=None, range_key=None,
range_key_data_type=None, local_indexes=None,
global_indexes=None):
'''
Creates a DynamoDB table.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.create_table table_name /
region=us-east-1 /
hash_key=id /
hash_key_data_type=N /
range_key=created_at /
range_key_data_type=N /
read_capacity_units=1 /
write_capacity_units=1
'''
schema = []
primary_index_fields = []
primary_index_name = ''
if hash_key:
hash_key_obj = HashKey(hash_key, data_type=hash_key_data_type)
schema.append(hash_key_obj)
primary_index_fields.append(hash_key_obj)
primary_index_name += hash_key
if range_key:
range_key_obj = RangeKey(range_key, data_type=range_key_data_type)
schema.append(range_key_obj)
primary_index_fields.append(range_key_obj)
primary_index_name += '_'
primary_index_name += range_key
primary_index_name += '_index'
throughput = {
'read': read_capacity_units,
'write': write_capacity_units
}
local_table_indexes = []
if local_indexes:
for index in local_indexes:
local_table_indexes.append(extract_index(index))
global_table_indexes = []
if global_indexes:
for index in global_indexes:
global_table_indexes.append(
extract_index(index, global_index=True)
)
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
Table.create(
table_name,
schema=schema,
throughput=throughput,
indexes=local_table_indexes,
global_indexes=global_table_indexes,
connection=conn
)
# Table creation can take several seconds to propagate.
# We will check MAX_ATTEMPTS times.
MAX_ATTEMPTS = 30
for i in range(MAX_ATTEMPTS):
if exists(
table_name,
region,
key,
keyid,
profile
):
return True
else:
time.sleep(1) # sleep for one second and try again
return False | [
"def",
"create_table",
"(",
"table_name",
",",
"region",
"=",
"None",
",",
"key",
"=",
"None",
",",
"keyid",
"=",
"None",
",",
"profile",
"=",
"None",
",",
"read_capacity_units",
"=",
"None",
",",
"write_capacity_units",
"=",
"None",
",",
"hash_key",
"=",
... | Creates a DynamoDB table.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.create_table table_name /
region=us-east-1 /
hash_key=id /
hash_key_data_type=N /
range_key=created_at /
range_key_data_type=N /
read_capacity_units=1 /
write_capacity_units=1 | [
"Creates",
"a",
"DynamoDB",
"table",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/boto_dynamodb.py#L85-L161 | train |
saltstack/salt | salt/modules/boto_dynamodb.py | exists | def exists(table_name, region=None, key=None, keyid=None, profile=None):
'''
Check to see if a table exists.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.exists table_name region=us-east-1
'''
try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
conn.describe_table(table_name)
except JSONResponseError as e:
if e.error_code == 'ResourceNotFoundException':
return False
raise
return True | python | def exists(table_name, region=None, key=None, keyid=None, profile=None):
'''
Check to see if a table exists.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.exists table_name region=us-east-1
'''
try:
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
conn.describe_table(table_name)
except JSONResponseError as e:
if e.error_code == 'ResourceNotFoundException':
return False
raise
return True | [
"def",
"exists",
"(",
"table_name",
",",
"region",
"=",
"None",
",",
"key",
"=",
"None",
",",
"keyid",
"=",
"None",
",",
"profile",
"=",
"None",
")",
":",
"try",
":",
"conn",
"=",
"_get_conn",
"(",
"region",
"=",
"region",
",",
"key",
"=",
"key",
... | Check to see if a table exists.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.exists table_name region=us-east-1 | [
"Check",
"to",
"see",
"if",
"a",
"table",
"exists",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/boto_dynamodb.py#L164-L182 | train |
saltstack/salt | salt/modules/boto_dynamodb.py | delete | def delete(table_name, region=None, key=None, keyid=None, profile=None):
'''
Delete a DynamoDB table.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.delete table_name region=us-east-1
'''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
table = Table(table_name, connection=conn)
table.delete()
# Table deletion can take several seconds to propagate.
# We will retry MAX_ATTEMPTS times.
MAX_ATTEMPTS = 30
for i in range(MAX_ATTEMPTS):
if not exists(table_name, region, key, keyid, profile):
return True
else:
time.sleep(1) # sleep for one second and try again
return False | python | def delete(table_name, region=None, key=None, keyid=None, profile=None):
'''
Delete a DynamoDB table.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.delete table_name region=us-east-1
'''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
table = Table(table_name, connection=conn)
table.delete()
# Table deletion can take several seconds to propagate.
# We will retry MAX_ATTEMPTS times.
MAX_ATTEMPTS = 30
for i in range(MAX_ATTEMPTS):
if not exists(table_name, region, key, keyid, profile):
return True
else:
time.sleep(1) # sleep for one second and try again
return False | [
"def",
"delete",
"(",
"table_name",
",",
"region",
"=",
"None",
",",
"key",
"=",
"None",
",",
"keyid",
"=",
"None",
",",
"profile",
"=",
"None",
")",
":",
"conn",
"=",
"_get_conn",
"(",
"region",
"=",
"region",
",",
"key",
"=",
"key",
",",
"keyid",... | Delete a DynamoDB table.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.delete table_name region=us-east-1 | [
"Delete",
"a",
"DynamoDB",
"table",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/boto_dynamodb.py#L185-L208 | train |
saltstack/salt | salt/modules/boto_dynamodb.py | update | def update(table_name, throughput=None, global_indexes=None,
region=None, key=None, keyid=None, profile=None):
'''
Update a DynamoDB table.
CLI example::
salt myminion boto_dynamodb.update table_name region=us-east-1
'''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
table = Table(table_name, connection=conn)
return table.update(throughput=throughput, global_indexes=global_indexes) | python | def update(table_name, throughput=None, global_indexes=None,
region=None, key=None, keyid=None, profile=None):
'''
Update a DynamoDB table.
CLI example::
salt myminion boto_dynamodb.update table_name region=us-east-1
'''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
table = Table(table_name, connection=conn)
return table.update(throughput=throughput, global_indexes=global_indexes) | [
"def",
"update",
"(",
"table_name",
",",
"throughput",
"=",
"None",
",",
"global_indexes",
"=",
"None",
",",
"region",
"=",
"None",
",",
"key",
"=",
"None",
",",
"keyid",
"=",
"None",
",",
"profile",
"=",
"None",
")",
":",
"conn",
"=",
"_get_conn",
"... | Update a DynamoDB table.
CLI example::
salt myminion boto_dynamodb.update table_name region=us-east-1 | [
"Update",
"a",
"DynamoDB",
"table",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/boto_dynamodb.py#L211-L222 | train |
saltstack/salt | salt/modules/boto_dynamodb.py | create_global_secondary_index | def create_global_secondary_index(table_name, global_index, region=None,
key=None, keyid=None, profile=None):
'''
Creates a single global secondary index on a DynamoDB table.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.create_global_secondary_index table_name /
index_name
'''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
table = Table(table_name, connection=conn)
return table.create_global_secondary_index(global_index) | python | def create_global_secondary_index(table_name, global_index, region=None,
key=None, keyid=None, profile=None):
'''
Creates a single global secondary index on a DynamoDB table.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.create_global_secondary_index table_name /
index_name
'''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
table = Table(table_name, connection=conn)
return table.create_global_secondary_index(global_index) | [
"def",
"create_global_secondary_index",
"(",
"table_name",
",",
"global_index",
",",
"region",
"=",
"None",
",",
"key",
"=",
"None",
",",
"keyid",
"=",
"None",
",",
"profile",
"=",
"None",
")",
":",
"conn",
"=",
"_get_conn",
"(",
"region",
"=",
"region",
... | Creates a single global secondary index on a DynamoDB table.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.create_global_secondary_index table_name /
index_name | [
"Creates",
"a",
"single",
"global",
"secondary",
"index",
"on",
"a",
"DynamoDB",
"table",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/boto_dynamodb.py#L225-L238 | train |
saltstack/salt | salt/modules/boto_dynamodb.py | update_global_secondary_index | def update_global_secondary_index(table_name, global_indexes, region=None,
key=None, keyid=None, profile=None):
'''
Updates the throughput of the given global secondary indexes.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.update_global_secondary_index table_name /
indexes
'''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
table = Table(table_name, connection=conn)
return table.update_global_secondary_index(global_indexes) | python | def update_global_secondary_index(table_name, global_indexes, region=None,
key=None, keyid=None, profile=None):
'''
Updates the throughput of the given global secondary indexes.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.update_global_secondary_index table_name /
indexes
'''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
table = Table(table_name, connection=conn)
return table.update_global_secondary_index(global_indexes) | [
"def",
"update_global_secondary_index",
"(",
"table_name",
",",
"global_indexes",
",",
"region",
"=",
"None",
",",
"key",
"=",
"None",
",",
"keyid",
"=",
"None",
",",
"profile",
"=",
"None",
")",
":",
"conn",
"=",
"_get_conn",
"(",
"region",
"=",
"region",... | Updates the throughput of the given global secondary indexes.
CLI Example:
.. code-block:: bash
salt myminion boto_dynamodb.update_global_secondary_index table_name /
indexes | [
"Updates",
"the",
"throughput",
"of",
"the",
"given",
"global",
"secondary",
"indexes",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/boto_dynamodb.py#L241-L254 | train |
saltstack/salt | salt/modules/boto_dynamodb.py | describe | def describe(table_name, region=None, key=None, keyid=None, profile=None):
'''
Describe a DynamoDB table.
CLI example::
salt myminion boto_dynamodb.describe table_name region=us-east-1
'''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
table = Table(table_name, connection=conn)
return table.describe() | python | def describe(table_name, region=None, key=None, keyid=None, profile=None):
'''
Describe a DynamoDB table.
CLI example::
salt myminion boto_dynamodb.describe table_name region=us-east-1
'''
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
table = Table(table_name, connection=conn)
return table.describe() | [
"def",
"describe",
"(",
"table_name",
",",
"region",
"=",
"None",
",",
"key",
"=",
"None",
",",
"keyid",
"=",
"None",
",",
"profile",
"=",
"None",
")",
":",
"conn",
"=",
"_get_conn",
"(",
"region",
"=",
"region",
",",
"key",
"=",
"key",
",",
"keyid... | Describe a DynamoDB table.
CLI example::
salt myminion boto_dynamodb.describe table_name region=us-east-1 | [
"Describe",
"a",
"DynamoDB",
"table",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/boto_dynamodb.py#L257-L267 | train |
saltstack/salt | salt/modules/boto_dynamodb.py | extract_index | def extract_index(index_data, global_index=False):
'''
Instantiates and returns an AllIndex object given a valid index
configuration
CLI Example:
salt myminion boto_dynamodb.extract_index index
'''
parsed_data = {}
keys = []
for key, value in six.iteritems(index_data):
for item in value:
for field, data in six.iteritems(item):
if field == 'hash_key':
parsed_data['hash_key'] = data
elif field == 'hash_key_data_type':
parsed_data['hash_key_data_type'] = data
elif field == 'range_key':
parsed_data['range_key'] = data
elif field == 'range_key_data_type':
parsed_data['range_key_data_type'] = data
elif field == 'name':
parsed_data['name'] = data
elif field == 'read_capacity_units':
parsed_data['read_capacity_units'] = data
elif field == 'write_capacity_units':
parsed_data['write_capacity_units'] = data
elif field == 'includes':
parsed_data['includes'] = data
elif field == 'keys_only':
parsed_data['keys_only'] = True
if parsed_data['hash_key']:
keys.append(
HashKey(
parsed_data['hash_key'],
data_type=parsed_data['hash_key_data_type']
)
)
if parsed_data.get('range_key'):
keys.append(
RangeKey(
parsed_data['range_key'],
data_type=parsed_data['range_key_data_type']
)
)
if (
global_index and
parsed_data['read_capacity_units'] and
parsed_data['write_capacity_units']):
parsed_data['throughput'] = {
'read': parsed_data['read_capacity_units'],
'write': parsed_data['write_capacity_units']
}
if parsed_data['name'] and keys:
if global_index:
if parsed_data.get('keys_only') and parsed_data.get('includes'):
raise SaltInvocationError('Only one type of GSI projection can be used.')
if parsed_data.get('includes'):
return GlobalIncludeIndex(
parsed_data['name'],
parts=keys,
throughput=parsed_data['throughput'],
includes=parsed_data['includes']
)
elif parsed_data.get('keys_only'):
return GlobalKeysOnlyIndex(
parsed_data['name'],
parts=keys,
throughput=parsed_data['throughput'],
)
else:
return GlobalAllIndex(
parsed_data['name'],
parts=keys,
throughput=parsed_data['throughput']
)
else:
return AllIndex(
parsed_data['name'],
parts=keys
) | python | def extract_index(index_data, global_index=False):
'''
Instantiates and returns an AllIndex object given a valid index
configuration
CLI Example:
salt myminion boto_dynamodb.extract_index index
'''
parsed_data = {}
keys = []
for key, value in six.iteritems(index_data):
for item in value:
for field, data in six.iteritems(item):
if field == 'hash_key':
parsed_data['hash_key'] = data
elif field == 'hash_key_data_type':
parsed_data['hash_key_data_type'] = data
elif field == 'range_key':
parsed_data['range_key'] = data
elif field == 'range_key_data_type':
parsed_data['range_key_data_type'] = data
elif field == 'name':
parsed_data['name'] = data
elif field == 'read_capacity_units':
parsed_data['read_capacity_units'] = data
elif field == 'write_capacity_units':
parsed_data['write_capacity_units'] = data
elif field == 'includes':
parsed_data['includes'] = data
elif field == 'keys_only':
parsed_data['keys_only'] = True
if parsed_data['hash_key']:
keys.append(
HashKey(
parsed_data['hash_key'],
data_type=parsed_data['hash_key_data_type']
)
)
if parsed_data.get('range_key'):
keys.append(
RangeKey(
parsed_data['range_key'],
data_type=parsed_data['range_key_data_type']
)
)
if (
global_index and
parsed_data['read_capacity_units'] and
parsed_data['write_capacity_units']):
parsed_data['throughput'] = {
'read': parsed_data['read_capacity_units'],
'write': parsed_data['write_capacity_units']
}
if parsed_data['name'] and keys:
if global_index:
if parsed_data.get('keys_only') and parsed_data.get('includes'):
raise SaltInvocationError('Only one type of GSI projection can be used.')
if parsed_data.get('includes'):
return GlobalIncludeIndex(
parsed_data['name'],
parts=keys,
throughput=parsed_data['throughput'],
includes=parsed_data['includes']
)
elif parsed_data.get('keys_only'):
return GlobalKeysOnlyIndex(
parsed_data['name'],
parts=keys,
throughput=parsed_data['throughput'],
)
else:
return GlobalAllIndex(
parsed_data['name'],
parts=keys,
throughput=parsed_data['throughput']
)
else:
return AllIndex(
parsed_data['name'],
parts=keys
) | [
"def",
"extract_index",
"(",
"index_data",
",",
"global_index",
"=",
"False",
")",
":",
"parsed_data",
"=",
"{",
"}",
"keys",
"=",
"[",
"]",
"for",
"key",
",",
"value",
"in",
"six",
".",
"iteritems",
"(",
"index_data",
")",
":",
"for",
"item",
"in",
... | Instantiates and returns an AllIndex object given a valid index
configuration
CLI Example:
salt myminion boto_dynamodb.extract_index index | [
"Instantiates",
"and",
"returns",
"an",
"AllIndex",
"object",
"given",
"a",
"valid",
"index",
"configuration"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/boto_dynamodb.py#L270-L353 | train |
saltstack/salt | salt/modules/cpan.py | install | def install(module):
'''
Install a Perl module from CPAN
CLI Example:
.. code-block:: bash
salt '*' cpan.install Template::Alloy
'''
ret = {
'old': None,
'new': None,
}
old_info = show(module)
cmd = 'cpan -i {0}'.format(module)
out = __salt__['cmd.run'](cmd)
if "don't know what it is" in out:
ret['error'] = 'CPAN cannot identify this package'
return ret
new_info = show(module)
ret['old'] = old_info.get('installed version', None)
ret['new'] = new_info['installed version']
return ret | python | def install(module):
'''
Install a Perl module from CPAN
CLI Example:
.. code-block:: bash
salt '*' cpan.install Template::Alloy
'''
ret = {
'old': None,
'new': None,
}
old_info = show(module)
cmd = 'cpan -i {0}'.format(module)
out = __salt__['cmd.run'](cmd)
if "don't know what it is" in out:
ret['error'] = 'CPAN cannot identify this package'
return ret
new_info = show(module)
ret['old'] = old_info.get('installed version', None)
ret['new'] = new_info['installed version']
return ret | [
"def",
"install",
"(",
"module",
")",
":",
"ret",
"=",
"{",
"'old'",
":",
"None",
",",
"'new'",
":",
"None",
",",
"}",
"old_info",
"=",
"show",
"(",
"module",
")",
"cmd",
"=",
"'cpan -i {0}'",
".",
"format",
"(",
"module",
")",
"out",
"=",
"__salt_... | Install a Perl module from CPAN
CLI Example:
.. code-block:: bash
salt '*' cpan.install Template::Alloy | [
"Install",
"a",
"Perl",
"module",
"from",
"CPAN"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/cpan.py#L35-L63 | train |
saltstack/salt | salt/modules/cpan.py | remove | def remove(module, details=False):
'''
Attempt to remove a Perl module that was installed from CPAN. Because the
``cpan`` command doesn't actually support "uninstall"-like functionality,
this function will attempt to do what it can, with what it has from CPAN.
Until this function is declared stable, USE AT YOUR OWN RISK!
CLI Example:
.. code-block:: bash
salt '*' cpan.remove Old::Package
'''
ret = {
'old': None,
'new': None,
}
info = show(module)
if 'error' in info:
return {
'error': info['error']
}
version = info.get('installed version', None)
if version is None:
return ret
ret['old'] = version
if 'cpan build dirs' not in info:
return {
'error': 'No CPAN data available to use for uninstalling'
}
mod_pathfile = module.replace('::', '/') + '.pm'
ins_path = info['installed file'].replace(mod_pathfile, '')
files = []
for build_dir in info['cpan build dirs']:
contents = os.listdir(build_dir)
if 'MANIFEST' not in contents:
continue
mfile = os.path.join(build_dir, 'MANIFEST')
with salt.utils.files.fopen(mfile, 'r') as fh_:
for line in fh_.readlines():
line = salt.utils.stringutils.to_unicode(line)
if line.startswith('lib/'):
files.append(line.replace('lib/', ins_path).strip())
rm_details = {}
for file_ in files:
if file_ in rm_details:
continue
log.trace('Removing %s', file_)
if __salt__['file.remove'](file_):
rm_details[file_] = 'removed'
else:
rm_details[file_] = 'unable to remove'
if details:
ret['details'] = rm_details
return ret | python | def remove(module, details=False):
'''
Attempt to remove a Perl module that was installed from CPAN. Because the
``cpan`` command doesn't actually support "uninstall"-like functionality,
this function will attempt to do what it can, with what it has from CPAN.
Until this function is declared stable, USE AT YOUR OWN RISK!
CLI Example:
.. code-block:: bash
salt '*' cpan.remove Old::Package
'''
ret = {
'old': None,
'new': None,
}
info = show(module)
if 'error' in info:
return {
'error': info['error']
}
version = info.get('installed version', None)
if version is None:
return ret
ret['old'] = version
if 'cpan build dirs' not in info:
return {
'error': 'No CPAN data available to use for uninstalling'
}
mod_pathfile = module.replace('::', '/') + '.pm'
ins_path = info['installed file'].replace(mod_pathfile, '')
files = []
for build_dir in info['cpan build dirs']:
contents = os.listdir(build_dir)
if 'MANIFEST' not in contents:
continue
mfile = os.path.join(build_dir, 'MANIFEST')
with salt.utils.files.fopen(mfile, 'r') as fh_:
for line in fh_.readlines():
line = salt.utils.stringutils.to_unicode(line)
if line.startswith('lib/'):
files.append(line.replace('lib/', ins_path).strip())
rm_details = {}
for file_ in files:
if file_ in rm_details:
continue
log.trace('Removing %s', file_)
if __salt__['file.remove'](file_):
rm_details[file_] = 'removed'
else:
rm_details[file_] = 'unable to remove'
if details:
ret['details'] = rm_details
return ret | [
"def",
"remove",
"(",
"module",
",",
"details",
"=",
"False",
")",
":",
"ret",
"=",
"{",
"'old'",
":",
"None",
",",
"'new'",
":",
"None",
",",
"}",
"info",
"=",
"show",
"(",
"module",
")",
"if",
"'error'",
"in",
"info",
":",
"return",
"{",
"'erro... | Attempt to remove a Perl module that was installed from CPAN. Because the
``cpan`` command doesn't actually support "uninstall"-like functionality,
this function will attempt to do what it can, with what it has from CPAN.
Until this function is declared stable, USE AT YOUR OWN RISK!
CLI Example:
.. code-block:: bash
salt '*' cpan.remove Old::Package | [
"Attempt",
"to",
"remove",
"a",
"Perl",
"module",
"that",
"was",
"installed",
"from",
"CPAN",
".",
"Because",
"the",
"cpan",
"command",
"doesn",
"t",
"actually",
"support",
"uninstall",
"-",
"like",
"functionality",
"this",
"function",
"will",
"attempt",
"to",... | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/cpan.py#L66-L130 | train |
saltstack/salt | salt/modules/cpan.py | list_ | def list_():
'''
List installed Perl modules, and the version installed
CLI Example:
.. code-block:: bash
salt '*' cpan.list
'''
ret = {}
cmd = 'cpan -l'
out = __salt__['cmd.run'](cmd).splitlines()
for line in out:
comps = line.split()
ret[comps[0]] = comps[1]
return ret | python | def list_():
'''
List installed Perl modules, and the version installed
CLI Example:
.. code-block:: bash
salt '*' cpan.list
'''
ret = {}
cmd = 'cpan -l'
out = __salt__['cmd.run'](cmd).splitlines()
for line in out:
comps = line.split()
ret[comps[0]] = comps[1]
return ret | [
"def",
"list_",
"(",
")",
":",
"ret",
"=",
"{",
"}",
"cmd",
"=",
"'cpan -l'",
"out",
"=",
"__salt__",
"[",
"'cmd.run'",
"]",
"(",
"cmd",
")",
".",
"splitlines",
"(",
")",
"for",
"line",
"in",
"out",
":",
"comps",
"=",
"line",
".",
"split",
"(",
... | List installed Perl modules, and the version installed
CLI Example:
.. code-block:: bash
salt '*' cpan.list | [
"List",
"installed",
"Perl",
"modules",
"and",
"the",
"version",
"installed"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/cpan.py#L133-L149 | train |
saltstack/salt | salt/modules/cpan.py | show | def show(module):
'''
Show information about a specific Perl module
CLI Example:
.. code-block:: bash
salt '*' cpan.show Template::Alloy
'''
ret = {}
ret['name'] = module
# This section parses out details from CPAN, if possible
cmd = 'cpan -D {0}'.format(module)
out = __salt__['cmd.run'](cmd).splitlines()
mode = 'skip'
info = []
for line in out:
if line.startswith('-------------'):
mode = 'parse'
continue
if mode == 'skip':
continue
info.append(line)
if len(info) == 6:
# If the module is not installed, we'll be short a line
info.insert(2, '')
if len(info) < 6:
# This must not be a real package
ret['error'] = 'This package does not seem to exist'
return ret
ret['description'] = info[0].strip()
ret['cpan file'] = info[1].strip()
if info[2].strip():
ret['installed file'] = info[2].strip()
else:
ret['installed file'] = None
comps = info[3].split(':')
if len(comps) > 1:
ret['installed version'] = comps[1].strip()
if 'installed version' not in ret or not ret['installed version']:
ret['installed version'] = None
comps = info[4].split(':')
comps = comps[1].split()
ret['cpan version'] = comps[0].strip()
ret['author name'] = info[5].strip()
ret['author email'] = info[6].strip()
# Check and see if there are cpan build directories
config = show_config()
build_dir = config.get('build_dir', None)
if build_dir is not None:
ret['cpan build dirs'] = []
builds = os.listdir(build_dir)
pfile = module.replace('::', '-')
for file_ in builds:
if file_.startswith(pfile):
ret['cpan build dirs'].append(os.path.join(build_dir, file_))
return ret | python | def show(module):
'''
Show information about a specific Perl module
CLI Example:
.. code-block:: bash
salt '*' cpan.show Template::Alloy
'''
ret = {}
ret['name'] = module
# This section parses out details from CPAN, if possible
cmd = 'cpan -D {0}'.format(module)
out = __salt__['cmd.run'](cmd).splitlines()
mode = 'skip'
info = []
for line in out:
if line.startswith('-------------'):
mode = 'parse'
continue
if mode == 'skip':
continue
info.append(line)
if len(info) == 6:
# If the module is not installed, we'll be short a line
info.insert(2, '')
if len(info) < 6:
# This must not be a real package
ret['error'] = 'This package does not seem to exist'
return ret
ret['description'] = info[0].strip()
ret['cpan file'] = info[1].strip()
if info[2].strip():
ret['installed file'] = info[2].strip()
else:
ret['installed file'] = None
comps = info[3].split(':')
if len(comps) > 1:
ret['installed version'] = comps[1].strip()
if 'installed version' not in ret or not ret['installed version']:
ret['installed version'] = None
comps = info[4].split(':')
comps = comps[1].split()
ret['cpan version'] = comps[0].strip()
ret['author name'] = info[5].strip()
ret['author email'] = info[6].strip()
# Check and see if there are cpan build directories
config = show_config()
build_dir = config.get('build_dir', None)
if build_dir is not None:
ret['cpan build dirs'] = []
builds = os.listdir(build_dir)
pfile = module.replace('::', '-')
for file_ in builds:
if file_.startswith(pfile):
ret['cpan build dirs'].append(os.path.join(build_dir, file_))
return ret | [
"def",
"show",
"(",
"module",
")",
":",
"ret",
"=",
"{",
"}",
"ret",
"[",
"'name'",
"]",
"=",
"module",
"# This section parses out details from CPAN, if possible",
"cmd",
"=",
"'cpan -D {0}'",
".",
"format",
"(",
"module",
")",
"out",
"=",
"__salt__",
"[",
"... | Show information about a specific Perl module
CLI Example:
.. code-block:: bash
salt '*' cpan.show Template::Alloy | [
"Show",
"information",
"about",
"a",
"specific",
"Perl",
"module"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/cpan.py#L152-L214 | train |
saltstack/salt | salt/modules/cpan.py | show_config | def show_config():
'''
Return a dict of CPAN configuration values
CLI Example:
.. code-block:: bash
salt '*' cpan.show_config
'''
ret = {}
cmd = 'cpan -J'
out = __salt__['cmd.run'](cmd).splitlines()
for line in out:
if '=>' not in line:
# TODO: Some options take up multiple lines, so this doesn't always work
continue
comps = line.split('=>')
key = comps[0].replace("'", '').strip()
val = comps[1].replace("',", '').replace("'", '').strip()
ret[key] = val
return ret | python | def show_config():
'''
Return a dict of CPAN configuration values
CLI Example:
.. code-block:: bash
salt '*' cpan.show_config
'''
ret = {}
cmd = 'cpan -J'
out = __salt__['cmd.run'](cmd).splitlines()
for line in out:
if '=>' not in line:
# TODO: Some options take up multiple lines, so this doesn't always work
continue
comps = line.split('=>')
key = comps[0].replace("'", '').strip()
val = comps[1].replace("',", '').replace("'", '').strip()
ret[key] = val
return ret | [
"def",
"show_config",
"(",
")",
":",
"ret",
"=",
"{",
"}",
"cmd",
"=",
"'cpan -J'",
"out",
"=",
"__salt__",
"[",
"'cmd.run'",
"]",
"(",
"cmd",
")",
".",
"splitlines",
"(",
")",
"for",
"line",
"in",
"out",
":",
"if",
"'=>'",
"not",
"in",
"line",
"... | Return a dict of CPAN configuration values
CLI Example:
.. code-block:: bash
salt '*' cpan.show_config | [
"Return",
"a",
"dict",
"of",
"CPAN",
"configuration",
"values"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/cpan.py#L217-L238 | train |
saltstack/salt | salt/cache/mysql_cache.py | run_query | def run_query(conn, query, retries=3):
'''
Get a cursor and run a query. Reconnect up to `retries` times if
needed.
Returns: cursor, affected rows counter
Raises: SaltCacheError, AttributeError, OperationalError
'''
try:
cur = conn.cursor()
out = cur.execute(query)
return cur, out
except (AttributeError, OperationalError) as e:
if retries == 0:
raise
# reconnect creating new client
sleep(_RECONNECT_INTERVAL_SEC)
if conn is None:
log.debug("mysql_cache: creating db connection")
else:
log.info("mysql_cache: recreating db connection due to: %r", e)
global client
client = MySQLdb.connect(**_mysql_kwargs)
return run_query(client, query, retries - 1)
except Exception as e:
if len(query) > 150:
query = query[:150] + "<...>"
raise SaltCacheError("Error running {0}: {1}".format(query, e)) | python | def run_query(conn, query, retries=3):
'''
Get a cursor and run a query. Reconnect up to `retries` times if
needed.
Returns: cursor, affected rows counter
Raises: SaltCacheError, AttributeError, OperationalError
'''
try:
cur = conn.cursor()
out = cur.execute(query)
return cur, out
except (AttributeError, OperationalError) as e:
if retries == 0:
raise
# reconnect creating new client
sleep(_RECONNECT_INTERVAL_SEC)
if conn is None:
log.debug("mysql_cache: creating db connection")
else:
log.info("mysql_cache: recreating db connection due to: %r", e)
global client
client = MySQLdb.connect(**_mysql_kwargs)
return run_query(client, query, retries - 1)
except Exception as e:
if len(query) > 150:
query = query[:150] + "<...>"
raise SaltCacheError("Error running {0}: {1}".format(query, e)) | [
"def",
"run_query",
"(",
"conn",
",",
"query",
",",
"retries",
"=",
"3",
")",
":",
"try",
":",
"cur",
"=",
"conn",
".",
"cursor",
"(",
")",
"out",
"=",
"cur",
".",
"execute",
"(",
"query",
")",
"return",
"cur",
",",
"out",
"except",
"(",
"Attribu... | Get a cursor and run a query. Reconnect up to `retries` times if
needed.
Returns: cursor, affected rows counter
Raises: SaltCacheError, AttributeError, OperationalError | [
"Get",
"a",
"cursor",
"and",
"run",
"a",
"query",
".",
"Reconnect",
"up",
"to",
"retries",
"times",
"if",
"needed",
".",
"Returns",
":",
"cursor",
"affected",
"rows",
"counter",
"Raises",
":",
"SaltCacheError",
"AttributeError",
"OperationalError"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/cache/mysql_cache.py#L92-L118 | train |
saltstack/salt | salt/cache/mysql_cache.py | _create_table | def _create_table():
'''
Create table if needed
'''
# Explicitely check if the table already exists as the library logs a
# warning on CREATE TABLE
query = """SELECT COUNT(TABLE_NAME) FROM information_schema.tables
WHERE table_schema = '{0}' AND table_name = '{1}'""".format(
_mysql_kwargs['db'],
_table_name,
)
cur, _ = run_query(client, query)
r = cur.fetchone()
cur.close()
if r[0] == 1:
return
query = """CREATE TABLE IF NOT EXISTS {0} (
bank CHAR(255),
etcd_key CHAR(255),
data MEDIUMBLOB,
PRIMARY KEY(bank, etcd_key)
);""".format(_table_name)
log.info("mysql_cache: creating table %s", _table_name)
cur, _ = run_query(client, query)
cur.close() | python | def _create_table():
'''
Create table if needed
'''
# Explicitely check if the table already exists as the library logs a
# warning on CREATE TABLE
query = """SELECT COUNT(TABLE_NAME) FROM information_schema.tables
WHERE table_schema = '{0}' AND table_name = '{1}'""".format(
_mysql_kwargs['db'],
_table_name,
)
cur, _ = run_query(client, query)
r = cur.fetchone()
cur.close()
if r[0] == 1:
return
query = """CREATE TABLE IF NOT EXISTS {0} (
bank CHAR(255),
etcd_key CHAR(255),
data MEDIUMBLOB,
PRIMARY KEY(bank, etcd_key)
);""".format(_table_name)
log.info("mysql_cache: creating table %s", _table_name)
cur, _ = run_query(client, query)
cur.close() | [
"def",
"_create_table",
"(",
")",
":",
"# Explicitely check if the table already exists as the library logs a",
"# warning on CREATE TABLE",
"query",
"=",
"\"\"\"SELECT COUNT(TABLE_NAME) FROM information_schema.tables\n WHERE table_schema = '{0}' AND table_name = '{1}'\"\"\"",
".",
"for... | Create table if needed | [
"Create",
"table",
"if",
"needed"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/cache/mysql_cache.py#L121-L146 | train |
saltstack/salt | salt/cache/mysql_cache.py | _init_client | def _init_client():
"""Initialize connection and create table if needed
"""
if client is not None:
return
global _mysql_kwargs, _table_name
_mysql_kwargs = {
'host': __opts__.get('mysql.host', '127.0.0.1'),
'user': __opts__.get('mysql.user', None),
'passwd': __opts__.get('mysql.password', None),
'db': __opts__.get('mysql.database', _DEFAULT_DATABASE_NAME),
'port': __opts__.get('mysql.port', 3306),
'unix_socket': __opts__.get('mysql.unix_socket', None),
'connect_timeout': __opts__.get('mysql.connect_timeout', None),
'autocommit': True,
}
_table_name = __opts__.get('mysql.table_name', _table_name)
# TODO: handle SSL connection parameters
for k, v in _mysql_kwargs.items():
if v is None:
_mysql_kwargs.pop(k)
kwargs_copy = _mysql_kwargs.copy()
kwargs_copy['passwd'] = "<hidden>"
log.info("mysql_cache: Setting up client with params: %r", kwargs_copy)
# The MySQL client is created later on by run_query
_create_table() | python | def _init_client():
"""Initialize connection and create table if needed
"""
if client is not None:
return
global _mysql_kwargs, _table_name
_mysql_kwargs = {
'host': __opts__.get('mysql.host', '127.0.0.1'),
'user': __opts__.get('mysql.user', None),
'passwd': __opts__.get('mysql.password', None),
'db': __opts__.get('mysql.database', _DEFAULT_DATABASE_NAME),
'port': __opts__.get('mysql.port', 3306),
'unix_socket': __opts__.get('mysql.unix_socket', None),
'connect_timeout': __opts__.get('mysql.connect_timeout', None),
'autocommit': True,
}
_table_name = __opts__.get('mysql.table_name', _table_name)
# TODO: handle SSL connection parameters
for k, v in _mysql_kwargs.items():
if v is None:
_mysql_kwargs.pop(k)
kwargs_copy = _mysql_kwargs.copy()
kwargs_copy['passwd'] = "<hidden>"
log.info("mysql_cache: Setting up client with params: %r", kwargs_copy)
# The MySQL client is created later on by run_query
_create_table() | [
"def",
"_init_client",
"(",
")",
":",
"if",
"client",
"is",
"not",
"None",
":",
"return",
"global",
"_mysql_kwargs",
",",
"_table_name",
"_mysql_kwargs",
"=",
"{",
"'host'",
":",
"__opts__",
".",
"get",
"(",
"'mysql.host'",
",",
"'127.0.0.1'",
")",
",",
"'... | Initialize connection and create table if needed | [
"Initialize",
"connection",
"and",
"create",
"table",
"if",
"needed"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/cache/mysql_cache.py#L149-L176 | train |
saltstack/salt | salt/cache/mysql_cache.py | store | def store(bank, key, data):
'''
Store a key value.
'''
_init_client()
data = __context__['serial'].dumps(data)
query = b"REPLACE INTO {0} (bank, etcd_key, data) values('{1}', '{2}', " \
b"'{3}')".format(_table_name,
bank,
key,
data)
cur, cnt = run_query(client, query)
cur.close()
if cnt not in (1, 2):
raise SaltCacheError(
'Error storing {0} {1} returned {2}'.format(bank, key, cnt)
) | python | def store(bank, key, data):
'''
Store a key value.
'''
_init_client()
data = __context__['serial'].dumps(data)
query = b"REPLACE INTO {0} (bank, etcd_key, data) values('{1}', '{2}', " \
b"'{3}')".format(_table_name,
bank,
key,
data)
cur, cnt = run_query(client, query)
cur.close()
if cnt not in (1, 2):
raise SaltCacheError(
'Error storing {0} {1} returned {2}'.format(bank, key, cnt)
) | [
"def",
"store",
"(",
"bank",
",",
"key",
",",
"data",
")",
":",
"_init_client",
"(",
")",
"data",
"=",
"__context__",
"[",
"'serial'",
"]",
".",
"dumps",
"(",
"data",
")",
"query",
"=",
"b\"REPLACE INTO {0} (bank, etcd_key, data) values('{1}', '{2}', \"",
"b\"'{... | Store a key value. | [
"Store",
"a",
"key",
"value",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/cache/mysql_cache.py#L179-L196 | train |
saltstack/salt | salt/cache/mysql_cache.py | fetch | def fetch(bank, key):
'''
Fetch a key value.
'''
_init_client()
query = "SELECT data FROM {0} WHERE bank='{1}' AND etcd_key='{2}'".format(
_table_name, bank, key)
cur, _ = run_query(client, query)
r = cur.fetchone()
cur.close()
if r is None:
return {}
return __context__['serial'].loads(r[0]) | python | def fetch(bank, key):
'''
Fetch a key value.
'''
_init_client()
query = "SELECT data FROM {0} WHERE bank='{1}' AND etcd_key='{2}'".format(
_table_name, bank, key)
cur, _ = run_query(client, query)
r = cur.fetchone()
cur.close()
if r is None:
return {}
return __context__['serial'].loads(r[0]) | [
"def",
"fetch",
"(",
"bank",
",",
"key",
")",
":",
"_init_client",
"(",
")",
"query",
"=",
"\"SELECT data FROM {0} WHERE bank='{1}' AND etcd_key='{2}'\"",
".",
"format",
"(",
"_table_name",
",",
"bank",
",",
"key",
")",
"cur",
",",
"_",
"=",
"run_query",
"(",
... | Fetch a key value. | [
"Fetch",
"a",
"key",
"value",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/cache/mysql_cache.py#L199-L211 | train |
saltstack/salt | salt/cache/mysql_cache.py | flush | def flush(bank, key=None):
'''
Remove the key from the cache bank with all the key content.
'''
_init_client()
query = "DELETE FROM {0} WHERE bank='{1}'".format(_table_name, bank)
if key is not None:
query += " AND etcd_key='{0}'".format(key)
cur, _ = run_query(client, query)
cur.close() | python | def flush(bank, key=None):
'''
Remove the key from the cache bank with all the key content.
'''
_init_client()
query = "DELETE FROM {0} WHERE bank='{1}'".format(_table_name, bank)
if key is not None:
query += " AND etcd_key='{0}'".format(key)
cur, _ = run_query(client, query)
cur.close() | [
"def",
"flush",
"(",
"bank",
",",
"key",
"=",
"None",
")",
":",
"_init_client",
"(",
")",
"query",
"=",
"\"DELETE FROM {0} WHERE bank='{1}'\"",
".",
"format",
"(",
"_table_name",
",",
"bank",
")",
"if",
"key",
"is",
"not",
"None",
":",
"query",
"+=",
"\"... | Remove the key from the cache bank with all the key content. | [
"Remove",
"the",
"key",
"from",
"the",
"cache",
"bank",
"with",
"all",
"the",
"key",
"content",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/cache/mysql_cache.py#L214-L224 | train |
saltstack/salt | salt/cache/mysql_cache.py | ls | def ls(bank):
'''
Return an iterable object containing all entries stored in the specified
bank.
'''
_init_client()
query = "SELECT etcd_key FROM {0} WHERE bank='{1}'".format(
_table_name, bank)
cur, _ = run_query(client, query)
out = [row[0] for row in cur.fetchall()]
cur.close()
return out | python | def ls(bank):
'''
Return an iterable object containing all entries stored in the specified
bank.
'''
_init_client()
query = "SELECT etcd_key FROM {0} WHERE bank='{1}'".format(
_table_name, bank)
cur, _ = run_query(client, query)
out = [row[0] for row in cur.fetchall()]
cur.close()
return out | [
"def",
"ls",
"(",
"bank",
")",
":",
"_init_client",
"(",
")",
"query",
"=",
"\"SELECT etcd_key FROM {0} WHERE bank='{1}'\"",
".",
"format",
"(",
"_table_name",
",",
"bank",
")",
"cur",
",",
"_",
"=",
"run_query",
"(",
"client",
",",
"query",
")",
"out",
"=... | Return an iterable object containing all entries stored in the specified
bank. | [
"Return",
"an",
"iterable",
"object",
"containing",
"all",
"entries",
"stored",
"in",
"the",
"specified",
"bank",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/cache/mysql_cache.py#L227-L238 | train |
saltstack/salt | salt/cache/mysql_cache.py | contains | def contains(bank, key):
'''
Checks if the specified bank contains the specified key.
'''
_init_client()
query = "SELECT COUNT(data) FROM {0} WHERE bank='{1}' " \
"AND etcd_key='{2}'".format(_table_name, bank, key)
cur, _ = run_query(client, query)
r = cur.fetchone()
cur.close()
return r[0] == 1 | python | def contains(bank, key):
'''
Checks if the specified bank contains the specified key.
'''
_init_client()
query = "SELECT COUNT(data) FROM {0} WHERE bank='{1}' " \
"AND etcd_key='{2}'".format(_table_name, bank, key)
cur, _ = run_query(client, query)
r = cur.fetchone()
cur.close()
return r[0] == 1 | [
"def",
"contains",
"(",
"bank",
",",
"key",
")",
":",
"_init_client",
"(",
")",
"query",
"=",
"\"SELECT COUNT(data) FROM {0} WHERE bank='{1}' \"",
"\"AND etcd_key='{2}'\"",
".",
"format",
"(",
"_table_name",
",",
"bank",
",",
"key",
")",
"cur",
",",
"_",
"=",
... | Checks if the specified bank contains the specified key. | [
"Checks",
"if",
"the",
"specified",
"bank",
"contains",
"the",
"specified",
"key",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/cache/mysql_cache.py#L241-L251 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.new | def new(self):
'''
Create a new database and opens it.
:return:
'''
dbname = self._label()
self.db_path = os.path.join(self.path, dbname)
if not os.path.exists(self.db_path):
os.makedirs(self.db_path)
self._opened = True
self.list_tables()
return dbname | python | def new(self):
'''
Create a new database and opens it.
:return:
'''
dbname = self._label()
self.db_path = os.path.join(self.path, dbname)
if not os.path.exists(self.db_path):
os.makedirs(self.db_path)
self._opened = True
self.list_tables()
return dbname | [
"def",
"new",
"(",
"self",
")",
":",
"dbname",
"=",
"self",
".",
"_label",
"(",
")",
"self",
".",
"db_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"path",
",",
"dbname",
")",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
... | Create a new database and opens it.
:return: | [
"Create",
"a",
"new",
"database",
"and",
"opens",
"it",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L76-L89 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.purge | def purge(self, dbid):
'''
Purge the database.
:param dbid:
:return:
'''
db_path = os.path.join(self.path, dbid)
if os.path.exists(db_path):
shutil.rmtree(db_path, ignore_errors=True)
return True
return False | python | def purge(self, dbid):
'''
Purge the database.
:param dbid:
:return:
'''
db_path = os.path.join(self.path, dbid)
if os.path.exists(db_path):
shutil.rmtree(db_path, ignore_errors=True)
return True
return False | [
"def",
"purge",
"(",
"self",
",",
"dbid",
")",
":",
"db_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"path",
",",
"dbid",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"db_path",
")",
":",
"shutil",
".",
"rmtree",
"(",
"db_... | Purge the database.
:param dbid:
:return: | [
"Purge",
"the",
"database",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L91-L102 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.flush | def flush(self, table):
'''
Flush table.
:param table:
:return:
'''
table_path = os.path.join(self.db_path, table)
if os.path.exists(table_path):
os.unlink(table_path) | python | def flush(self, table):
'''
Flush table.
:param table:
:return:
'''
table_path = os.path.join(self.db_path, table)
if os.path.exists(table_path):
os.unlink(table_path) | [
"def",
"flush",
"(",
"self",
",",
"table",
")",
":",
"table_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"db_path",
",",
"table",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"table_path",
")",
":",
"os",
".",
"unlink",
"(",... | Flush table.
:param table:
:return: | [
"Flush",
"table",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L104-L113 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.list | def list(self):
'''
List all the databases on the given path.
:return:
'''
databases = []
for dbname in os.listdir(self.path):
databases.append(dbname)
return list(reversed(sorted(databases))) | python | def list(self):
'''
List all the databases on the given path.
:return:
'''
databases = []
for dbname in os.listdir(self.path):
databases.append(dbname)
return list(reversed(sorted(databases))) | [
"def",
"list",
"(",
"self",
")",
":",
"databases",
"=",
"[",
"]",
"for",
"dbname",
"in",
"os",
".",
"listdir",
"(",
"self",
".",
"path",
")",
":",
"databases",
".",
"append",
"(",
"dbname",
")",
"return",
"list",
"(",
"reversed",
"(",
"sorted",
"("... | List all the databases on the given path.
:return: | [
"List",
"all",
"the",
"databases",
"on",
"the",
"given",
"path",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L115-L124 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.list_tables | def list_tables(self):
'''
Load existing tables and their descriptions.
:return:
'''
if not self._tables:
for table_name in os.listdir(self.db_path):
self._tables[table_name] = self._load_table(table_name)
return self._tables.keys() | python | def list_tables(self):
'''
Load existing tables and their descriptions.
:return:
'''
if not self._tables:
for table_name in os.listdir(self.db_path):
self._tables[table_name] = self._load_table(table_name)
return self._tables.keys() | [
"def",
"list_tables",
"(",
"self",
")",
":",
"if",
"not",
"self",
".",
"_tables",
":",
"for",
"table_name",
"in",
"os",
".",
"listdir",
"(",
"self",
".",
"db_path",
")",
":",
"self",
".",
"_tables",
"[",
"table_name",
"]",
"=",
"self",
".",
"_load_ta... | Load existing tables and their descriptions.
:return: | [
"Load",
"existing",
"tables",
"and",
"their",
"descriptions",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L126-L136 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.open | def open(self, dbname=None):
'''
Open database from the path with the name or latest.
If there are no yet databases, create a new implicitly.
:return:
'''
databases = self.list()
if self.is_closed():
self.db_path = os.path.join(self.path, dbname or (databases and databases[0] or self.new()))
if not self._opened:
self.list_tables()
self._opened = True | python | def open(self, dbname=None):
'''
Open database from the path with the name or latest.
If there are no yet databases, create a new implicitly.
:return:
'''
databases = self.list()
if self.is_closed():
self.db_path = os.path.join(self.path, dbname or (databases and databases[0] or self.new()))
if not self._opened:
self.list_tables()
self._opened = True | [
"def",
"open",
"(",
"self",
",",
"dbname",
"=",
"None",
")",
":",
"databases",
"=",
"self",
".",
"list",
"(",
")",
"if",
"self",
".",
"is_closed",
"(",
")",
":",
"self",
".",
"db_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"pa... | Open database from the path with the name or latest.
If there are no yet databases, create a new implicitly.
:return: | [
"Open",
"database",
"from",
"the",
"path",
"with",
"the",
"name",
"or",
"latest",
".",
"If",
"there",
"are",
"no",
"yet",
"databases",
"create",
"a",
"new",
"implicitly",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L142-L154 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.create_table_from_object | def create_table_from_object(self, obj):
'''
Create a table from the object.
NOTE: This method doesn't stores anything.
:param obj:
:return:
'''
get_type = lambda item: str(type(item)).split("'")[1]
if not os.path.exists(os.path.join(self.db_path, obj._TABLE)):
with gzip.open(os.path.join(self.db_path, obj._TABLE), 'wb') as table_file:
csv.writer(table_file).writerow(['{col}:{type}'.format(col=elm[0], type=get_type(elm[1]))
for elm in tuple(obj.__dict__.items())])
self._tables[obj._TABLE] = self._load_table(obj._TABLE) | python | def create_table_from_object(self, obj):
'''
Create a table from the object.
NOTE: This method doesn't stores anything.
:param obj:
:return:
'''
get_type = lambda item: str(type(item)).split("'")[1]
if not os.path.exists(os.path.join(self.db_path, obj._TABLE)):
with gzip.open(os.path.join(self.db_path, obj._TABLE), 'wb') as table_file:
csv.writer(table_file).writerow(['{col}:{type}'.format(col=elm[0], type=get_type(elm[1]))
for elm in tuple(obj.__dict__.items())])
self._tables[obj._TABLE] = self._load_table(obj._TABLE) | [
"def",
"create_table_from_object",
"(",
"self",
",",
"obj",
")",
":",
"get_type",
"=",
"lambda",
"item",
":",
"str",
"(",
"type",
"(",
"item",
")",
")",
".",
"split",
"(",
"\"'\"",
")",
"[",
"1",
"]",
"if",
"not",
"os",
".",
"path",
".",
"exists",
... | Create a table from the object.
NOTE: This method doesn't stores anything.
:param obj:
:return: | [
"Create",
"a",
"table",
"from",
"the",
"object",
".",
"NOTE",
":",
"This",
"method",
"doesn",
"t",
"stores",
"anything",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L172-L185 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.store | def store(self, obj, distinct=False):
'''
Store an object in the table.
:param obj: An object to store
:param distinct: Store object only if there is none identical of such.
If at least one field is different, store it.
:return:
'''
if distinct:
fields = dict(zip(self._tables[obj._TABLE].keys(),
obj._serialize(self._tables[obj._TABLE])))
db_obj = self.get(obj.__class__, eq=fields)
if db_obj and distinct:
raise Exception("Object already in the database.")
with gzip.open(os.path.join(self.db_path, obj._TABLE), 'a') as table:
csv.writer(table).writerow(self._validate_object(obj)) | python | def store(self, obj, distinct=False):
'''
Store an object in the table.
:param obj: An object to store
:param distinct: Store object only if there is none identical of such.
If at least one field is different, store it.
:return:
'''
if distinct:
fields = dict(zip(self._tables[obj._TABLE].keys(),
obj._serialize(self._tables[obj._TABLE])))
db_obj = self.get(obj.__class__, eq=fields)
if db_obj and distinct:
raise Exception("Object already in the database.")
with gzip.open(os.path.join(self.db_path, obj._TABLE), 'a') as table:
csv.writer(table).writerow(self._validate_object(obj)) | [
"def",
"store",
"(",
"self",
",",
"obj",
",",
"distinct",
"=",
"False",
")",
":",
"if",
"distinct",
":",
"fields",
"=",
"dict",
"(",
"zip",
"(",
"self",
".",
"_tables",
"[",
"obj",
".",
"_TABLE",
"]",
".",
"keys",
"(",
")",
",",
"obj",
".",
"_s... | Store an object in the table.
:param obj: An object to store
:param distinct: Store object only if there is none identical of such.
If at least one field is different, store it.
:return: | [
"Store",
"an",
"object",
"in",
"the",
"table",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L187-L203 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.update | def update(self, obj, matches=None, mt=None, lt=None, eq=None):
'''
Update object(s) in the database.
:param obj:
:param matches:
:param mt:
:param lt:
:param eq:
:return:
'''
updated = False
objects = list()
for _obj in self.get(obj.__class__):
if self.__criteria(_obj, matches=matches, mt=mt, lt=lt, eq=eq):
objects.append(obj)
updated = True
else:
objects.append(_obj)
self.flush(obj._TABLE)
self.create_table_from_object(obj)
for obj in objects:
self.store(obj)
return updated | python | def update(self, obj, matches=None, mt=None, lt=None, eq=None):
'''
Update object(s) in the database.
:param obj:
:param matches:
:param mt:
:param lt:
:param eq:
:return:
'''
updated = False
objects = list()
for _obj in self.get(obj.__class__):
if self.__criteria(_obj, matches=matches, mt=mt, lt=lt, eq=eq):
objects.append(obj)
updated = True
else:
objects.append(_obj)
self.flush(obj._TABLE)
self.create_table_from_object(obj)
for obj in objects:
self.store(obj)
return updated | [
"def",
"update",
"(",
"self",
",",
"obj",
",",
"matches",
"=",
"None",
",",
"mt",
"=",
"None",
",",
"lt",
"=",
"None",
",",
"eq",
"=",
"None",
")",
":",
"updated",
"=",
"False",
"objects",
"=",
"list",
"(",
")",
"for",
"_obj",
"in",
"self",
"."... | Update object(s) in the database.
:param obj:
:param matches:
:param mt:
:param lt:
:param eq:
:return: | [
"Update",
"object",
"(",
"s",
")",
"in",
"the",
"database",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L205-L229 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.delete | def delete(self, obj, matches=None, mt=None, lt=None, eq=None):
'''
Delete object from the database.
:param obj:
:param matches:
:param mt:
:param lt:
:param eq:
:return:
'''
deleted = False
objects = list()
for _obj in self.get(obj):
if not self.__criteria(_obj, matches=matches, mt=mt, lt=lt, eq=eq):
objects.append(_obj)
else:
deleted = True
self.flush(obj._TABLE)
self.create_table_from_object(obj())
for _obj in objects:
self.store(_obj)
return deleted | python | def delete(self, obj, matches=None, mt=None, lt=None, eq=None):
'''
Delete object from the database.
:param obj:
:param matches:
:param mt:
:param lt:
:param eq:
:return:
'''
deleted = False
objects = list()
for _obj in self.get(obj):
if not self.__criteria(_obj, matches=matches, mt=mt, lt=lt, eq=eq):
objects.append(_obj)
else:
deleted = True
self.flush(obj._TABLE)
self.create_table_from_object(obj())
for _obj in objects:
self.store(_obj)
return deleted | [
"def",
"delete",
"(",
"self",
",",
"obj",
",",
"matches",
"=",
"None",
",",
"mt",
"=",
"None",
",",
"lt",
"=",
"None",
",",
"eq",
"=",
"None",
")",
":",
"deleted",
"=",
"False",
"objects",
"=",
"list",
"(",
")",
"for",
"_obj",
"in",
"self",
"."... | Delete object from the database.
:param obj:
:param matches:
:param mt:
:param lt:
:param eq:
:return: | [
"Delete",
"object",
"from",
"the",
"database",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L231-L255 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.__criteria | def __criteria(self, obj, matches=None, mt=None, lt=None, eq=None):
'''
Returns True if object is aligned to the criteria.
:param obj:
:param matches:
:param mt:
:param lt:
:param eq:
:return: Boolean
'''
# Fail matcher if "less than"
for field, value in (mt or {}).items():
if getattr(obj, field) <= value:
return False
# Fail matcher if "more than"
for field, value in (lt or {}).items():
if getattr(obj, field) >= value:
return False
# Fail matcher if "not equal"
for field, value in (eq or {}).items():
if getattr(obj, field) != value:
return False
# Fail matcher if "doesn't match"
for field, value in (matches or {}).items():
if not re.search(value, str(getattr(obj, field))):
return False
return True | python | def __criteria(self, obj, matches=None, mt=None, lt=None, eq=None):
'''
Returns True if object is aligned to the criteria.
:param obj:
:param matches:
:param mt:
:param lt:
:param eq:
:return: Boolean
'''
# Fail matcher if "less than"
for field, value in (mt or {}).items():
if getattr(obj, field) <= value:
return False
# Fail matcher if "more than"
for field, value in (lt or {}).items():
if getattr(obj, field) >= value:
return False
# Fail matcher if "not equal"
for field, value in (eq or {}).items():
if getattr(obj, field) != value:
return False
# Fail matcher if "doesn't match"
for field, value in (matches or {}).items():
if not re.search(value, str(getattr(obj, field))):
return False
return True | [
"def",
"__criteria",
"(",
"self",
",",
"obj",
",",
"matches",
"=",
"None",
",",
"mt",
"=",
"None",
",",
"lt",
"=",
"None",
",",
"eq",
"=",
"None",
")",
":",
"# Fail matcher if \"less than\"",
"for",
"field",
",",
"value",
"in",
"(",
"mt",
"or",
"{",
... | Returns True if object is aligned to the criteria.
:param obj:
:param matches:
:param mt:
:param lt:
:param eq:
:return: Boolean | [
"Returns",
"True",
"if",
"object",
"is",
"aligned",
"to",
"the",
"criteria",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L263-L294 | train |
saltstack/salt | salt/modules/inspectlib/fsdb.py | CsvDB.get | def get(self, obj, matches=None, mt=None, lt=None, eq=None):
'''
Get objects from the table.
:param table_name:
:param matches: Regexp.
:param mt: More than.
:param lt: Less than.
:param eq: Equals.
:return:
'''
objects = []
with gzip.open(os.path.join(self.db_path, obj._TABLE), 'rb') as table:
header = None
for data in csv.reader(table):
if not header:
header = data
continue
_obj = obj()
for t_attr, t_data in zip(header, data):
t_attr, t_type = t_attr.split(':')
setattr(_obj, t_attr, self._to_type(t_data, t_type))
if self.__criteria(_obj, matches=matches, mt=mt, lt=lt, eq=eq):
objects.append(_obj)
return objects | python | def get(self, obj, matches=None, mt=None, lt=None, eq=None):
'''
Get objects from the table.
:param table_name:
:param matches: Regexp.
:param mt: More than.
:param lt: Less than.
:param eq: Equals.
:return:
'''
objects = []
with gzip.open(os.path.join(self.db_path, obj._TABLE), 'rb') as table:
header = None
for data in csv.reader(table):
if not header:
header = data
continue
_obj = obj()
for t_attr, t_data in zip(header, data):
t_attr, t_type = t_attr.split(':')
setattr(_obj, t_attr, self._to_type(t_data, t_type))
if self.__criteria(_obj, matches=matches, mt=mt, lt=lt, eq=eq):
objects.append(_obj)
return objects | [
"def",
"get",
"(",
"self",
",",
"obj",
",",
"matches",
"=",
"None",
",",
"mt",
"=",
"None",
",",
"lt",
"=",
"None",
",",
"eq",
"=",
"None",
")",
":",
"objects",
"=",
"[",
"]",
"with",
"gzip",
".",
"open",
"(",
"os",
".",
"path",
".",
"join",
... | Get objects from the table.
:param table_name:
:param matches: Regexp.
:param mt: More than.
:param lt: Less than.
:param eq: Equals.
:return: | [
"Get",
"objects",
"from",
"the",
"table",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/inspectlib/fsdb.py#L296-L320 | train |
saltstack/salt | salt/runners/event.py | send | def send(tag, data=None):
'''
Send an event with the given tag and data.
This is useful for sending events directly to the master from the shell
with salt-run. It is also quite useful for sending events in orchestration
states where the ``fire_event`` requisite isn't sufficient because it does
not support sending custom data with the event.
Note that event tags will *not* be namespaced like events sent with the
``fire_event`` requisite! Whereas events produced from ``fire_event`` are
prefixed with ``salt/state_result/<jid>/<minion_id>/<name>``, events sent
using this runner module will have no such prefix. Make sure your reactors
don't expect a prefix!
:param tag: the tag to send with the event
:param data: an optional dictionary of data to send with the event
CLI Example:
.. code-block:: bash
salt-run event.send my/custom/event '{"foo": "bar"}'
Orchestration Example:
.. code-block:: yaml
# orch/command.sls
run_a_command:
salt.function:
- name: cmd.run
- tgt: my_minion
- arg:
- exit {{ pillar['exit_code'] }}
send_success_event:
salt.runner:
- name: event.send
- tag: my_event/success
- data:
foo: bar
- require:
- salt: run_a_command
send_failure_event:
salt.runner:
- name: event.send
- tag: my_event/failure
- data:
baz: qux
- onfail:
- salt: run_a_command
.. code-block:: bash
salt-run state.orchestrate orch.command pillar='{"exit_code": 0}'
salt-run state.orchestrate orch.command pillar='{"exit_code": 1}'
'''
data = data or {}
event = salt.utils.event.get_master_event(__opts__, __opts__['sock_dir'],
listen=False)
return event.fire_event(data, tag) | python | def send(tag, data=None):
'''
Send an event with the given tag and data.
This is useful for sending events directly to the master from the shell
with salt-run. It is also quite useful for sending events in orchestration
states where the ``fire_event`` requisite isn't sufficient because it does
not support sending custom data with the event.
Note that event tags will *not* be namespaced like events sent with the
``fire_event`` requisite! Whereas events produced from ``fire_event`` are
prefixed with ``salt/state_result/<jid>/<minion_id>/<name>``, events sent
using this runner module will have no such prefix. Make sure your reactors
don't expect a prefix!
:param tag: the tag to send with the event
:param data: an optional dictionary of data to send with the event
CLI Example:
.. code-block:: bash
salt-run event.send my/custom/event '{"foo": "bar"}'
Orchestration Example:
.. code-block:: yaml
# orch/command.sls
run_a_command:
salt.function:
- name: cmd.run
- tgt: my_minion
- arg:
- exit {{ pillar['exit_code'] }}
send_success_event:
salt.runner:
- name: event.send
- tag: my_event/success
- data:
foo: bar
- require:
- salt: run_a_command
send_failure_event:
salt.runner:
- name: event.send
- tag: my_event/failure
- data:
baz: qux
- onfail:
- salt: run_a_command
.. code-block:: bash
salt-run state.orchestrate orch.command pillar='{"exit_code": 0}'
salt-run state.orchestrate orch.command pillar='{"exit_code": 1}'
'''
data = data or {}
event = salt.utils.event.get_master_event(__opts__, __opts__['sock_dir'],
listen=False)
return event.fire_event(data, tag) | [
"def",
"send",
"(",
"tag",
",",
"data",
"=",
"None",
")",
":",
"data",
"=",
"data",
"or",
"{",
"}",
"event",
"=",
"salt",
".",
"utils",
".",
"event",
".",
"get_master_event",
"(",
"__opts__",
",",
"__opts__",
"[",
"'sock_dir'",
"]",
",",
"listen",
... | Send an event with the given tag and data.
This is useful for sending events directly to the master from the shell
with salt-run. It is also quite useful for sending events in orchestration
states where the ``fire_event`` requisite isn't sufficient because it does
not support sending custom data with the event.
Note that event tags will *not* be namespaced like events sent with the
``fire_event`` requisite! Whereas events produced from ``fire_event`` are
prefixed with ``salt/state_result/<jid>/<minion_id>/<name>``, events sent
using this runner module will have no such prefix. Make sure your reactors
don't expect a prefix!
:param tag: the tag to send with the event
:param data: an optional dictionary of data to send with the event
CLI Example:
.. code-block:: bash
salt-run event.send my/custom/event '{"foo": "bar"}'
Orchestration Example:
.. code-block:: yaml
# orch/command.sls
run_a_command:
salt.function:
- name: cmd.run
- tgt: my_minion
- arg:
- exit {{ pillar['exit_code'] }}
send_success_event:
salt.runner:
- name: event.send
- tag: my_event/success
- data:
foo: bar
- require:
- salt: run_a_command
send_failure_event:
salt.runner:
- name: event.send
- tag: my_event/failure
- data:
baz: qux
- onfail:
- salt: run_a_command
.. code-block:: bash
salt-run state.orchestrate orch.command pillar='{"exit_code": 0}'
salt-run state.orchestrate orch.command pillar='{"exit_code": 1}' | [
"Send",
"an",
"event",
"with",
"the",
"given",
"tag",
"and",
"data",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/event.py#L17-L80 | train |
saltstack/salt | salt/runners/cache.py | grains | def grains(tgt=None, tgt_type='glob', **kwargs):
'''
.. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Return cached grains of the targeted minions.
tgt
Target to match minion ids.
.. versionchanged:: 2017.7.5,2018.3.0
The ``tgt`` argument is now required to display cached grains. If
not used, the function will not return grains. This optional
argument will become mandatory in the Salt ``Sodium`` release.
tgt_type
The type of targeting to use for matching, such as ``glob``, ``list``,
etc.
CLI Example:
.. code-block:: bash
salt-run cache.grains '*'
'''
if tgt is None:
# Change ``tgt=None`` to ``tgt`` (mandatory kwarg) in Salt Sodium.
# This behavior was changed in PR #45588 to fix Issue #45489.
salt.utils.versions.warn_until(
'Sodium',
'Detected missing \'tgt\' option. Cached grains will not be returned '
'without a specified \'tgt\'. This option will be required starting in '
'Salt Sodium and this warning will be removed.'
)
pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type,
use_cached_grains=True,
grains_fallback=False,
opts=__opts__)
cached_grains = pillar_util.get_minion_grains()
return cached_grains | python | def grains(tgt=None, tgt_type='glob', **kwargs):
'''
.. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Return cached grains of the targeted minions.
tgt
Target to match minion ids.
.. versionchanged:: 2017.7.5,2018.3.0
The ``tgt`` argument is now required to display cached grains. If
not used, the function will not return grains. This optional
argument will become mandatory in the Salt ``Sodium`` release.
tgt_type
The type of targeting to use for matching, such as ``glob``, ``list``,
etc.
CLI Example:
.. code-block:: bash
salt-run cache.grains '*'
'''
if tgt is None:
# Change ``tgt=None`` to ``tgt`` (mandatory kwarg) in Salt Sodium.
# This behavior was changed in PR #45588 to fix Issue #45489.
salt.utils.versions.warn_until(
'Sodium',
'Detected missing \'tgt\' option. Cached grains will not be returned '
'without a specified \'tgt\'. This option will be required starting in '
'Salt Sodium and this warning will be removed.'
)
pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type,
use_cached_grains=True,
grains_fallback=False,
opts=__opts__)
cached_grains = pillar_util.get_minion_grains()
return cached_grains | [
"def",
"grains",
"(",
"tgt",
"=",
"None",
",",
"tgt_type",
"=",
"'glob'",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"tgt",
"is",
"None",
":",
"# Change ``tgt=None`` to ``tgt`` (mandatory kwarg) in Salt Sodium.",
"# This behavior was changed in PR #45588 to fix Issue #45489... | .. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Return cached grains of the targeted minions.
tgt
Target to match minion ids.
.. versionchanged:: 2017.7.5,2018.3.0
The ``tgt`` argument is now required to display cached grains. If
not used, the function will not return grains. This optional
argument will become mandatory in the Salt ``Sodium`` release.
tgt_type
The type of targeting to use for matching, such as ``glob``, ``list``,
etc.
CLI Example:
.. code-block:: bash
salt-run cache.grains '*' | [
"..",
"versionchanged",
"::",
"2017",
".",
"7",
".",
"0",
"The",
"expr_form",
"argument",
"has",
"been",
"renamed",
"to",
"tgt_type",
"earlier",
"releases",
"must",
"use",
"expr_form",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/cache.py#L33-L74 | train |
saltstack/salt | salt/runners/cache.py | pillar | def pillar(tgt=None, tgt_type='glob', **kwargs):
'''
.. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Return cached pillars of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.pillar
'''
pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type,
use_cached_grains=True,
grains_fallback=False,
use_cached_pillar=True,
pillar_fallback=False,
opts=__opts__)
cached_pillar = pillar_util.get_minion_pillar()
return cached_pillar | python | def pillar(tgt=None, tgt_type='glob', **kwargs):
'''
.. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Return cached pillars of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.pillar
'''
pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type,
use_cached_grains=True,
grains_fallback=False,
use_cached_pillar=True,
pillar_fallback=False,
opts=__opts__)
cached_pillar = pillar_util.get_minion_pillar()
return cached_pillar | [
"def",
"pillar",
"(",
"tgt",
"=",
"None",
",",
"tgt_type",
"=",
"'glob'",
",",
"*",
"*",
"kwargs",
")",
":",
"pillar_util",
"=",
"salt",
".",
"utils",
".",
"master",
".",
"MasterPillarUtil",
"(",
"tgt",
",",
"tgt_type",
",",
"use_cached_grains",
"=",
"... | .. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Return cached pillars of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.pillar | [
"..",
"versionchanged",
"::",
"2017",
".",
"7",
".",
"0",
"The",
"expr_form",
"argument",
"has",
"been",
"renamed",
"to",
"tgt_type",
"earlier",
"releases",
"must",
"use",
"expr_form",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/cache.py#L77-L98 | train |
saltstack/salt | salt/runners/cache.py | mine | def mine(tgt=None, tgt_type='glob', **kwargs):
'''
.. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Return cached mine data of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.mine
'''
pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type,
use_cached_grains=False,
grains_fallback=False,
use_cached_pillar=False,
pillar_fallback=False,
opts=__opts__)
cached_mine = pillar_util.get_cached_mine_data()
return cached_mine | python | def mine(tgt=None, tgt_type='glob', **kwargs):
'''
.. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Return cached mine data of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.mine
'''
pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type,
use_cached_grains=False,
grains_fallback=False,
use_cached_pillar=False,
pillar_fallback=False,
opts=__opts__)
cached_mine = pillar_util.get_cached_mine_data()
return cached_mine | [
"def",
"mine",
"(",
"tgt",
"=",
"None",
",",
"tgt_type",
"=",
"'glob'",
",",
"*",
"*",
"kwargs",
")",
":",
"pillar_util",
"=",
"salt",
".",
"utils",
".",
"master",
".",
"MasterPillarUtil",
"(",
"tgt",
",",
"tgt_type",
",",
"use_cached_grains",
"=",
"Fa... | .. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Return cached mine data of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.mine | [
"..",
"versionchanged",
"::",
"2017",
".",
"7",
".",
"0",
"The",
"expr_form",
"argument",
"has",
"been",
"renamed",
"to",
"tgt_type",
"earlier",
"releases",
"must",
"use",
"expr_form",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/cache.py#L101-L122 | train |
saltstack/salt | salt/runners/cache.py | _clear_cache | def _clear_cache(tgt=None,
tgt_type='glob',
clear_pillar_flag=False,
clear_grains_flag=False,
clear_mine_flag=False,
clear_mine_func_flag=None):
'''
Clear the cached data/files for the targeted minions.
'''
if tgt is None:
return False
pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type,
use_cached_grains=True,
grains_fallback=False,
use_cached_pillar=True,
pillar_fallback=False,
opts=__opts__)
return pillar_util.clear_cached_minion_data(clear_pillar=clear_pillar_flag,
clear_grains=clear_grains_flag,
clear_mine=clear_mine_flag,
clear_mine_func=clear_mine_func_flag) | python | def _clear_cache(tgt=None,
tgt_type='glob',
clear_pillar_flag=False,
clear_grains_flag=False,
clear_mine_flag=False,
clear_mine_func_flag=None):
'''
Clear the cached data/files for the targeted minions.
'''
if tgt is None:
return False
pillar_util = salt.utils.master.MasterPillarUtil(tgt, tgt_type,
use_cached_grains=True,
grains_fallback=False,
use_cached_pillar=True,
pillar_fallback=False,
opts=__opts__)
return pillar_util.clear_cached_minion_data(clear_pillar=clear_pillar_flag,
clear_grains=clear_grains_flag,
clear_mine=clear_mine_flag,
clear_mine_func=clear_mine_func_flag) | [
"def",
"_clear_cache",
"(",
"tgt",
"=",
"None",
",",
"tgt_type",
"=",
"'glob'",
",",
"clear_pillar_flag",
"=",
"False",
",",
"clear_grains_flag",
"=",
"False",
",",
"clear_mine_flag",
"=",
"False",
",",
"clear_mine_func_flag",
"=",
"None",
")",
":",
"if",
"t... | Clear the cached data/files for the targeted minions. | [
"Clear",
"the",
"cached",
"data",
"/",
"files",
"for",
"the",
"targeted",
"minions",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/cache.py#L125-L145 | train |
saltstack/salt | salt/runners/cache.py | clear_all | def clear_all(tgt=None, tgt_type='glob'):
'''
.. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Clear the cached pillar, grains, and mine data of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.clear_all
'''
return _clear_cache(tgt,
tgt_type,
clear_pillar_flag=True,
clear_grains_flag=True,
clear_mine_flag=True) | python | def clear_all(tgt=None, tgt_type='glob'):
'''
.. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Clear the cached pillar, grains, and mine data of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.clear_all
'''
return _clear_cache(tgt,
tgt_type,
clear_pillar_flag=True,
clear_grains_flag=True,
clear_mine_flag=True) | [
"def",
"clear_all",
"(",
"tgt",
"=",
"None",
",",
"tgt_type",
"=",
"'glob'",
")",
":",
"return",
"_clear_cache",
"(",
"tgt",
",",
"tgt_type",
",",
"clear_pillar_flag",
"=",
"True",
",",
"clear_grains_flag",
"=",
"True",
",",
"clear_mine_flag",
"=",
"True",
... | .. versionchanged:: 2017.7.0
The ``expr_form`` argument has been renamed to ``tgt_type``, earlier
releases must use ``expr_form``.
Clear the cached pillar, grains, and mine data of the targeted minions
CLI Example:
.. code-block:: bash
salt-run cache.clear_all | [
"..",
"versionchanged",
"::",
"2017",
".",
"7",
".",
"0",
"The",
"expr_form",
"argument",
"has",
"been",
"renamed",
"to",
"tgt_type",
"earlier",
"releases",
"must",
"use",
"expr_form",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/cache.py#L218-L236 | train |
saltstack/salt | salt/runners/cache.py | clear_git_lock | def clear_git_lock(role, remote=None, **kwargs):
'''
.. versionadded:: 2015.8.2
Remove the update locks for Salt components (gitfs, git_pillar, winrepo)
which use gitfs backend code from salt.utils.gitfs.
.. note::
Running :py:func:`cache.clear_all <salt.runners.cache.clear_all>` will
not include this function as it does for pillar, grains, and mine.
Additionally, executing this function with a ``role`` of ``gitfs`` is
equivalent to running ``salt-run fileserver.clear_lock backend=git``.
role
Which type of lock to remove (``gitfs``, ``git_pillar``, or
``winrepo``)
remote
If specified, then any remotes which contain the passed string will
have their lock cleared. For example, a ``remote`` value of **github**
will remove the lock from all github.com remotes.
type : update,checkout,mountpoint
The types of lock to clear. Can be one or more of ``update``,
``checkout``, and ``mountpoint``, and can be passed either as a
comma-separated or Python list.
.. versionadded:: 2015.8.8
.. versionchanged:: 2018.3.0
``mountpoint`` lock type added
CLI Examples:
.. code-block:: bash
salt-run cache.clear_git_lock gitfs
salt-run cache.clear_git_lock git_pillar
salt-run cache.clear_git_lock git_pillar type=update
salt-run cache.clear_git_lock git_pillar type=update,checkout
salt-run cache.clear_git_lock git_pillar type='["update", "mountpoint"]'
'''
kwargs = salt.utils.args.clean_kwargs(**kwargs)
type_ = salt.utils.args.split_input(
kwargs.pop('type', ['update', 'checkout', 'mountpoint']))
if kwargs:
salt.utils.args.invalid_kwargs(kwargs)
if role == 'gitfs':
git_objects = [
salt.utils.gitfs.GitFS(
__opts__,
__opts__['gitfs_remotes'],
per_remote_overrides=salt.fileserver.gitfs.PER_REMOTE_OVERRIDES,
per_remote_only=salt.fileserver.gitfs.PER_REMOTE_ONLY
)
]
elif role == 'git_pillar':
git_objects = []
for ext_pillar in __opts__['ext_pillar']:
key = next(iter(ext_pillar))
if key == 'git':
if not isinstance(ext_pillar['git'], list):
continue
obj = salt.utils.gitfs.GitPillar(
__opts__,
ext_pillar['git'],
per_remote_overrides=salt.pillar.git_pillar.PER_REMOTE_OVERRIDES,
per_remote_only=salt.pillar.git_pillar.PER_REMOTE_ONLY,
global_only=salt.pillar.git_pillar.GLOBAL_ONLY)
git_objects.append(obj)
elif role == 'winrepo':
winrepo_dir = __opts__['winrepo_dir']
winrepo_remotes = __opts__['winrepo_remotes']
git_objects = []
for remotes, base_dir in (
(winrepo_remotes, winrepo_dir),
(__opts__['winrepo_remotes_ng'], __opts__['winrepo_dir_ng'])
):
obj = salt.utils.gitfs.WinRepo(
__opts__,
remotes,
per_remote_overrides=salt.runners.winrepo.PER_REMOTE_OVERRIDES,
per_remote_only=salt.runners.winrepo.PER_REMOTE_ONLY,
global_only=salt.runners.winrepo.GLOBAL_ONLY,
cache_root=base_dir)
git_objects.append(obj)
else:
raise SaltInvocationError('Invalid role \'{0}\''.format(role))
ret = {}
for obj in git_objects:
for lock_type in type_:
cleared, errors = _clear_lock(obj.clear_lock,
role,
remote=remote,
lock_type=lock_type)
if cleared:
ret.setdefault('cleared', []).extend(cleared)
if errors:
ret.setdefault('errors', []).extend(errors)
if not ret:
return 'No locks were removed'
return ret | python | def clear_git_lock(role, remote=None, **kwargs):
'''
.. versionadded:: 2015.8.2
Remove the update locks for Salt components (gitfs, git_pillar, winrepo)
which use gitfs backend code from salt.utils.gitfs.
.. note::
Running :py:func:`cache.clear_all <salt.runners.cache.clear_all>` will
not include this function as it does for pillar, grains, and mine.
Additionally, executing this function with a ``role`` of ``gitfs`` is
equivalent to running ``salt-run fileserver.clear_lock backend=git``.
role
Which type of lock to remove (``gitfs``, ``git_pillar``, or
``winrepo``)
remote
If specified, then any remotes which contain the passed string will
have their lock cleared. For example, a ``remote`` value of **github**
will remove the lock from all github.com remotes.
type : update,checkout,mountpoint
The types of lock to clear. Can be one or more of ``update``,
``checkout``, and ``mountpoint``, and can be passed either as a
comma-separated or Python list.
.. versionadded:: 2015.8.8
.. versionchanged:: 2018.3.0
``mountpoint`` lock type added
CLI Examples:
.. code-block:: bash
salt-run cache.clear_git_lock gitfs
salt-run cache.clear_git_lock git_pillar
salt-run cache.clear_git_lock git_pillar type=update
salt-run cache.clear_git_lock git_pillar type=update,checkout
salt-run cache.clear_git_lock git_pillar type='["update", "mountpoint"]'
'''
kwargs = salt.utils.args.clean_kwargs(**kwargs)
type_ = salt.utils.args.split_input(
kwargs.pop('type', ['update', 'checkout', 'mountpoint']))
if kwargs:
salt.utils.args.invalid_kwargs(kwargs)
if role == 'gitfs':
git_objects = [
salt.utils.gitfs.GitFS(
__opts__,
__opts__['gitfs_remotes'],
per_remote_overrides=salt.fileserver.gitfs.PER_REMOTE_OVERRIDES,
per_remote_only=salt.fileserver.gitfs.PER_REMOTE_ONLY
)
]
elif role == 'git_pillar':
git_objects = []
for ext_pillar in __opts__['ext_pillar']:
key = next(iter(ext_pillar))
if key == 'git':
if not isinstance(ext_pillar['git'], list):
continue
obj = salt.utils.gitfs.GitPillar(
__opts__,
ext_pillar['git'],
per_remote_overrides=salt.pillar.git_pillar.PER_REMOTE_OVERRIDES,
per_remote_only=salt.pillar.git_pillar.PER_REMOTE_ONLY,
global_only=salt.pillar.git_pillar.GLOBAL_ONLY)
git_objects.append(obj)
elif role == 'winrepo':
winrepo_dir = __opts__['winrepo_dir']
winrepo_remotes = __opts__['winrepo_remotes']
git_objects = []
for remotes, base_dir in (
(winrepo_remotes, winrepo_dir),
(__opts__['winrepo_remotes_ng'], __opts__['winrepo_dir_ng'])
):
obj = salt.utils.gitfs.WinRepo(
__opts__,
remotes,
per_remote_overrides=salt.runners.winrepo.PER_REMOTE_OVERRIDES,
per_remote_only=salt.runners.winrepo.PER_REMOTE_ONLY,
global_only=salt.runners.winrepo.GLOBAL_ONLY,
cache_root=base_dir)
git_objects.append(obj)
else:
raise SaltInvocationError('Invalid role \'{0}\''.format(role))
ret = {}
for obj in git_objects:
for lock_type in type_:
cleared, errors = _clear_lock(obj.clear_lock,
role,
remote=remote,
lock_type=lock_type)
if cleared:
ret.setdefault('cleared', []).extend(cleared)
if errors:
ret.setdefault('errors', []).extend(errors)
if not ret:
return 'No locks were removed'
return ret | [
"def",
"clear_git_lock",
"(",
"role",
",",
"remote",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"kwargs",
"=",
"salt",
".",
"utils",
".",
"args",
".",
"clean_kwargs",
"(",
"*",
"*",
"kwargs",
")",
"type_",
"=",
"salt",
".",
"utils",
".",
"args... | .. versionadded:: 2015.8.2
Remove the update locks for Salt components (gitfs, git_pillar, winrepo)
which use gitfs backend code from salt.utils.gitfs.
.. note::
Running :py:func:`cache.clear_all <salt.runners.cache.clear_all>` will
not include this function as it does for pillar, grains, and mine.
Additionally, executing this function with a ``role`` of ``gitfs`` is
equivalent to running ``salt-run fileserver.clear_lock backend=git``.
role
Which type of lock to remove (``gitfs``, ``git_pillar``, or
``winrepo``)
remote
If specified, then any remotes which contain the passed string will
have their lock cleared. For example, a ``remote`` value of **github**
will remove the lock from all github.com remotes.
type : update,checkout,mountpoint
The types of lock to clear. Can be one or more of ``update``,
``checkout``, and ``mountpoint``, and can be passed either as a
comma-separated or Python list.
.. versionadded:: 2015.8.8
.. versionchanged:: 2018.3.0
``mountpoint`` lock type added
CLI Examples:
.. code-block:: bash
salt-run cache.clear_git_lock gitfs
salt-run cache.clear_git_lock git_pillar
salt-run cache.clear_git_lock git_pillar type=update
salt-run cache.clear_git_lock git_pillar type=update,checkout
salt-run cache.clear_git_lock git_pillar type='["update", "mountpoint"]' | [
"..",
"versionadded",
"::",
"2015",
".",
"8",
".",
"2"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/cache.py#L239-L343 | train |
saltstack/salt | salt/runners/cache.py | cloud | def cloud(tgt, provider=None):
'''
Return cloud cache data for target.
.. note:: Only works with glob matching
tgt
Glob Target to match minion ids
provider
Cloud Provider
CLI Example:
.. code-block:: bash
salt-run cache.cloud 'salt*'
salt-run cache.cloud glance.example.org provider=openstack
'''
if not isinstance(tgt, six.string_types):
return {}
opts = salt.config.cloud_config(
os.path.join(os.path.dirname(__opts__['conf_file']), 'cloud')
)
if not opts.get('update_cachedir'):
return {}
cloud_cache = __utils__['cloud.list_cache_nodes_full'](opts=opts, provider=provider)
if cloud_cache is None:
return {}
ret = {}
for driver, providers in six.iteritems(cloud_cache):
for provider, servers in six.iteritems(providers):
for name, data in six.iteritems(servers):
if fnmatch.fnmatch(name, tgt):
ret[name] = data
ret[name]['provider'] = provider
return ret | python | def cloud(tgt, provider=None):
'''
Return cloud cache data for target.
.. note:: Only works with glob matching
tgt
Glob Target to match minion ids
provider
Cloud Provider
CLI Example:
.. code-block:: bash
salt-run cache.cloud 'salt*'
salt-run cache.cloud glance.example.org provider=openstack
'''
if not isinstance(tgt, six.string_types):
return {}
opts = salt.config.cloud_config(
os.path.join(os.path.dirname(__opts__['conf_file']), 'cloud')
)
if not opts.get('update_cachedir'):
return {}
cloud_cache = __utils__['cloud.list_cache_nodes_full'](opts=opts, provider=provider)
if cloud_cache is None:
return {}
ret = {}
for driver, providers in six.iteritems(cloud_cache):
for provider, servers in six.iteritems(providers):
for name, data in six.iteritems(servers):
if fnmatch.fnmatch(name, tgt):
ret[name] = data
ret[name]['provider'] = provider
return ret | [
"def",
"cloud",
"(",
"tgt",
",",
"provider",
"=",
"None",
")",
":",
"if",
"not",
"isinstance",
"(",
"tgt",
",",
"six",
".",
"string_types",
")",
":",
"return",
"{",
"}",
"opts",
"=",
"salt",
".",
"config",
".",
"cloud_config",
"(",
"os",
".",
"path... | Return cloud cache data for target.
.. note:: Only works with glob matching
tgt
Glob Target to match minion ids
provider
Cloud Provider
CLI Example:
.. code-block:: bash
salt-run cache.cloud 'salt*'
salt-run cache.cloud glance.example.org provider=openstack | [
"Return",
"cloud",
"cache",
"data",
"for",
"target",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/cache.py#L346-L385 | train |
saltstack/salt | salt/runners/cache.py | store | def store(bank, key, data, cachedir=None):
'''
Lists entries stored in the specified bank.
CLI Example:
.. code-block:: bash
salt-run cache.store mycache mykey 'The time has come the walrus said'
'''
if cachedir is None:
cachedir = __opts__['cachedir']
try:
cache = salt.cache.Cache(__opts__, cachedir=cachedir)
except TypeError:
cache = salt.cache.Cache(__opts__)
return cache.store(bank, key, data) | python | def store(bank, key, data, cachedir=None):
'''
Lists entries stored in the specified bank.
CLI Example:
.. code-block:: bash
salt-run cache.store mycache mykey 'The time has come the walrus said'
'''
if cachedir is None:
cachedir = __opts__['cachedir']
try:
cache = salt.cache.Cache(__opts__, cachedir=cachedir)
except TypeError:
cache = salt.cache.Cache(__opts__)
return cache.store(bank, key, data) | [
"def",
"store",
"(",
"bank",
",",
"key",
",",
"data",
",",
"cachedir",
"=",
"None",
")",
":",
"if",
"cachedir",
"is",
"None",
":",
"cachedir",
"=",
"__opts__",
"[",
"'cachedir'",
"]",
"try",
":",
"cache",
"=",
"salt",
".",
"cache",
".",
"Cache",
"(... | Lists entries stored in the specified bank.
CLI Example:
.. code-block:: bash
salt-run cache.store mycache mykey 'The time has come the walrus said' | [
"Lists",
"entries",
"stored",
"in",
"the",
"specified",
"bank",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/cache.py#L388-L405 | train |
saltstack/salt | salt/runners/cache.py | list_ | def list_(bank, cachedir=None):
'''
Lists entries stored in the specified bank.
CLI Example:
.. code-block:: bash
salt-run cache.list cloud/active/ec2/myec2 cachedir=/var/cache/salt/
'''
if cachedir is None:
cachedir = __opts__['cachedir']
try:
cache = salt.cache.Cache(__opts__, cachedir=cachedir)
except TypeError:
cache = salt.cache.Cache(__opts__)
return cache.list(bank) | python | def list_(bank, cachedir=None):
'''
Lists entries stored in the specified bank.
CLI Example:
.. code-block:: bash
salt-run cache.list cloud/active/ec2/myec2 cachedir=/var/cache/salt/
'''
if cachedir is None:
cachedir = __opts__['cachedir']
try:
cache = salt.cache.Cache(__opts__, cachedir=cachedir)
except TypeError:
cache = salt.cache.Cache(__opts__)
return cache.list(bank) | [
"def",
"list_",
"(",
"bank",
",",
"cachedir",
"=",
"None",
")",
":",
"if",
"cachedir",
"is",
"None",
":",
"cachedir",
"=",
"__opts__",
"[",
"'cachedir'",
"]",
"try",
":",
"cache",
"=",
"salt",
".",
"cache",
".",
"Cache",
"(",
"__opts__",
",",
"cached... | Lists entries stored in the specified bank.
CLI Example:
.. code-block:: bash
salt-run cache.list cloud/active/ec2/myec2 cachedir=/var/cache/salt/ | [
"Lists",
"entries",
"stored",
"in",
"the",
"specified",
"bank",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/cache.py#L408-L425 | train |
saltstack/salt | salt/runners/cache.py | fetch | def fetch(bank, key, cachedir=None):
'''
Fetch data from a salt.cache bank.
CLI Example:
.. code-block:: bash
salt-run cache.fetch cloud/active/ec2/myec2 myminion cachedir=/var/cache/salt/
'''
if cachedir is None:
cachedir = __opts__['cachedir']
try:
cache = salt.cache.Cache(__opts__, cachedir=cachedir)
except TypeError:
cache = salt.cache.Cache(__opts__)
return cache.fetch(bank, key) | python | def fetch(bank, key, cachedir=None):
'''
Fetch data from a salt.cache bank.
CLI Example:
.. code-block:: bash
salt-run cache.fetch cloud/active/ec2/myec2 myminion cachedir=/var/cache/salt/
'''
if cachedir is None:
cachedir = __opts__['cachedir']
try:
cache = salt.cache.Cache(__opts__, cachedir=cachedir)
except TypeError:
cache = salt.cache.Cache(__opts__)
return cache.fetch(bank, key) | [
"def",
"fetch",
"(",
"bank",
",",
"key",
",",
"cachedir",
"=",
"None",
")",
":",
"if",
"cachedir",
"is",
"None",
":",
"cachedir",
"=",
"__opts__",
"[",
"'cachedir'",
"]",
"try",
":",
"cache",
"=",
"salt",
".",
"cache",
".",
"Cache",
"(",
"__opts__",
... | Fetch data from a salt.cache bank.
CLI Example:
.. code-block:: bash
salt-run cache.fetch cloud/active/ec2/myec2 myminion cachedir=/var/cache/salt/ | [
"Fetch",
"data",
"from",
"a",
"salt",
".",
"cache",
"bank",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/runners/cache.py#L428-L445 | train |
saltstack/salt | salt/queues/pgjsonb_queue.py | _conn | def _conn(commit=False):
'''
Return an postgres cursor
'''
defaults = {'host': 'localhost',
'user': 'salt',
'password': 'salt',
'dbname': 'salt',
'port': 5432}
conn_kwargs = {}
for key, value in defaults.items():
conn_kwargs[key] = __opts__.get('queue.{0}.{1}'.format(__virtualname__, key), value)
try:
conn = psycopg2.connect(**conn_kwargs)
except psycopg2.OperationalError as exc:
raise SaltMasterError('pgjsonb returner could not connect to database: {exc}'.format(exc=exc))
cursor = conn.cursor()
try:
yield cursor
except psycopg2.DatabaseError as err:
error = err.args
sys.stderr.write(six.text_type(error))
cursor.execute("ROLLBACK")
raise err
else:
if commit:
cursor.execute("COMMIT")
else:
cursor.execute("ROLLBACK")
finally:
conn.close() | python | def _conn(commit=False):
'''
Return an postgres cursor
'''
defaults = {'host': 'localhost',
'user': 'salt',
'password': 'salt',
'dbname': 'salt',
'port': 5432}
conn_kwargs = {}
for key, value in defaults.items():
conn_kwargs[key] = __opts__.get('queue.{0}.{1}'.format(__virtualname__, key), value)
try:
conn = psycopg2.connect(**conn_kwargs)
except psycopg2.OperationalError as exc:
raise SaltMasterError('pgjsonb returner could not connect to database: {exc}'.format(exc=exc))
cursor = conn.cursor()
try:
yield cursor
except psycopg2.DatabaseError as err:
error = err.args
sys.stderr.write(six.text_type(error))
cursor.execute("ROLLBACK")
raise err
else:
if commit:
cursor.execute("COMMIT")
else:
cursor.execute("ROLLBACK")
finally:
conn.close() | [
"def",
"_conn",
"(",
"commit",
"=",
"False",
")",
":",
"defaults",
"=",
"{",
"'host'",
":",
"'localhost'",
",",
"'user'",
":",
"'salt'",
",",
"'password'",
":",
"'salt'",
",",
"'dbname'",
":",
"'salt'",
",",
"'port'",
":",
"5432",
"}",
"conn_kwargs",
"... | Return an postgres cursor | [
"Return",
"an",
"postgres",
"cursor"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/queues/pgjsonb_queue.py#L72-L105 | train |
saltstack/salt | salt/queues/pgjsonb_queue.py | insert | def insert(queue, items):
'''
Add an item or items to a queue
'''
handle_queue_creation(queue)
with _conn(commit=True) as cur:
if isinstance(items, dict):
items = salt.utils.json.dumps(items)
cmd = str('''INSERT INTO {0}(data) VALUES('{1}')''').format(queue, items) # future lint: disable=blacklisted-function
log.debug('SQL Query: %s', cmd)
try:
cur.execute(cmd)
except psycopg2.IntegrityError as esc:
return ('Item already exists in this queue. '
'postgres error: {0}'.format(esc))
if isinstance(items, list):
items = [(salt.utils.json.dumps(el),) for el in items]
cmd = str("INSERT INTO {0}(data) VALUES (%s)").format(queue) # future lint: disable=blacklisted-function
log.debug('SQL Query: %s', cmd)
try:
cur.executemany(cmd, items)
except psycopg2.IntegrityError as esc:
return ('One or more items already exists in this queue. '
'postgres error: {0}'.format(esc))
return True | python | def insert(queue, items):
'''
Add an item or items to a queue
'''
handle_queue_creation(queue)
with _conn(commit=True) as cur:
if isinstance(items, dict):
items = salt.utils.json.dumps(items)
cmd = str('''INSERT INTO {0}(data) VALUES('{1}')''').format(queue, items) # future lint: disable=blacklisted-function
log.debug('SQL Query: %s', cmd)
try:
cur.execute(cmd)
except psycopg2.IntegrityError as esc:
return ('Item already exists in this queue. '
'postgres error: {0}'.format(esc))
if isinstance(items, list):
items = [(salt.utils.json.dumps(el),) for el in items]
cmd = str("INSERT INTO {0}(data) VALUES (%s)").format(queue) # future lint: disable=blacklisted-function
log.debug('SQL Query: %s', cmd)
try:
cur.executemany(cmd, items)
except psycopg2.IntegrityError as esc:
return ('One or more items already exists in this queue. '
'postgres error: {0}'.format(esc))
return True | [
"def",
"insert",
"(",
"queue",
",",
"items",
")",
":",
"handle_queue_creation",
"(",
"queue",
")",
"with",
"_conn",
"(",
"commit",
"=",
"True",
")",
"as",
"cur",
":",
"if",
"isinstance",
"(",
"items",
",",
"dict",
")",
":",
"items",
"=",
"salt",
".",... | Add an item or items to a queue | [
"Add",
"an",
"item",
"or",
"items",
"to",
"a",
"queue"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/queues/pgjsonb_queue.py#L183-L208 | train |
saltstack/salt | salt/queues/pgjsonb_queue.py | delete | def delete(queue, items):
'''
Delete an item or items from a queue
'''
with _conn(commit=True) as cur:
if isinstance(items, dict):
cmd = str("""DELETE FROM {0} WHERE data = '{1}'""").format( # future lint: disable=blacklisted-function
queue,
salt.utils.json.dumps(items))
log.debug('SQL Query: %s', cmd)
cur.execute(cmd)
return True
if isinstance(items, list):
items = [(salt.utils.json.dumps(el),) for el in items]
cmd = 'DELETE FROM {0} WHERE data = %s'.format(queue)
log.debug('SQL Query: %s', cmd)
cur.executemany(cmd, items)
return True | python | def delete(queue, items):
'''
Delete an item or items from a queue
'''
with _conn(commit=True) as cur:
if isinstance(items, dict):
cmd = str("""DELETE FROM {0} WHERE data = '{1}'""").format( # future lint: disable=blacklisted-function
queue,
salt.utils.json.dumps(items))
log.debug('SQL Query: %s', cmd)
cur.execute(cmd)
return True
if isinstance(items, list):
items = [(salt.utils.json.dumps(el),) for el in items]
cmd = 'DELETE FROM {0} WHERE data = %s'.format(queue)
log.debug('SQL Query: %s', cmd)
cur.executemany(cmd, items)
return True | [
"def",
"delete",
"(",
"queue",
",",
"items",
")",
":",
"with",
"_conn",
"(",
"commit",
"=",
"True",
")",
"as",
"cur",
":",
"if",
"isinstance",
"(",
"items",
",",
"dict",
")",
":",
"cmd",
"=",
"str",
"(",
"\"\"\"DELETE FROM {0} WHERE data = '{1}'\"\"\"",
... | Delete an item or items from a queue | [
"Delete",
"an",
"item",
"or",
"items",
"from",
"a",
"queue"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/queues/pgjsonb_queue.py#L211-L228 | train |
saltstack/salt | salt/queues/pgjsonb_queue.py | pop | def pop(queue, quantity=1, is_runner=False):
'''
Pop one or more or all items from the queue return them.
'''
cmd = 'SELECT id, data FROM {0}'.format(queue)
if quantity != 'all':
try:
quantity = int(quantity)
except ValueError as exc:
error_txt = ('Quantity must be an integer or "all".\n'
'Error: "{0}".'.format(exc))
raise SaltInvocationError(error_txt)
cmd = ''.join([cmd, ' LIMIT {0};'.format(quantity)])
log.debug('SQL Query: %s', cmd)
items = []
with _conn(commit=True) as cur:
cur.execute(cmd)
result = cur.fetchall()
if result:
ids = [six.text_type(item[0]) for item in result]
items = [item[1] for item in result]
idlist = "','".join(ids)
del_cmd = '''DELETE FROM {0} WHERE id IN ('{1}');'''.format(
queue, idlist)
log.debug('SQL Query: %s', del_cmd)
cur.execute(del_cmd)
return items | python | def pop(queue, quantity=1, is_runner=False):
'''
Pop one or more or all items from the queue return them.
'''
cmd = 'SELECT id, data FROM {0}'.format(queue)
if quantity != 'all':
try:
quantity = int(quantity)
except ValueError as exc:
error_txt = ('Quantity must be an integer or "all".\n'
'Error: "{0}".'.format(exc))
raise SaltInvocationError(error_txt)
cmd = ''.join([cmd, ' LIMIT {0};'.format(quantity)])
log.debug('SQL Query: %s', cmd)
items = []
with _conn(commit=True) as cur:
cur.execute(cmd)
result = cur.fetchall()
if result:
ids = [six.text_type(item[0]) for item in result]
items = [item[1] for item in result]
idlist = "','".join(ids)
del_cmd = '''DELETE FROM {0} WHERE id IN ('{1}');'''.format(
queue, idlist)
log.debug('SQL Query: %s', del_cmd)
cur.execute(del_cmd)
return items | [
"def",
"pop",
"(",
"queue",
",",
"quantity",
"=",
"1",
",",
"is_runner",
"=",
"False",
")",
":",
"cmd",
"=",
"'SELECT id, data FROM {0}'",
".",
"format",
"(",
"queue",
")",
"if",
"quantity",
"!=",
"'all'",
":",
"try",
":",
"quantity",
"=",
"int",
"(",
... | Pop one or more or all items from the queue return them. | [
"Pop",
"one",
"or",
"more",
"or",
"all",
"items",
"from",
"the",
"queue",
"return",
"them",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/queues/pgjsonb_queue.py#L231-L259 | train |
saltstack/salt | salt/utils/process.py | daemonize | def daemonize(redirect_out=True):
'''
Daemonize a process
'''
# Avoid circular import
import salt.utils.crypt
try:
pid = os.fork()
if pid > 0:
# exit first parent
salt.utils.crypt.reinit_crypto()
os._exit(salt.defaults.exitcodes.EX_OK)
except OSError as exc:
log.error('fork #1 failed: %s (%s)', exc.errno, exc)
sys.exit(salt.defaults.exitcodes.EX_GENERIC)
# decouple from parent environment
os.chdir('/')
# noinspection PyArgumentList
os.setsid()
os.umask(0o022) # pylint: disable=blacklisted-function
# do second fork
try:
pid = os.fork()
if pid > 0:
salt.utils.crypt.reinit_crypto()
sys.exit(salt.defaults.exitcodes.EX_OK)
except OSError as exc:
log.error('fork #2 failed: %s (%s)', exc.errno, exc)
sys.exit(salt.defaults.exitcodes.EX_GENERIC)
salt.utils.crypt.reinit_crypto()
# A normal daemonization redirects the process output to /dev/null.
# Unfortunately when a python multiprocess is called the output is
# not cleanly redirected and the parent process dies when the
# multiprocessing process attempts to access stdout or err.
if redirect_out:
with salt.utils.files.fopen('/dev/null', 'r+') as dev_null:
# Redirect python stdin/out/err
# and the os stdin/out/err which can be different
os.dup2(dev_null.fileno(), sys.stdin.fileno())
os.dup2(dev_null.fileno(), sys.stdout.fileno())
os.dup2(dev_null.fileno(), sys.stderr.fileno())
os.dup2(dev_null.fileno(), 0)
os.dup2(dev_null.fileno(), 1)
os.dup2(dev_null.fileno(), 2) | python | def daemonize(redirect_out=True):
'''
Daemonize a process
'''
# Avoid circular import
import salt.utils.crypt
try:
pid = os.fork()
if pid > 0:
# exit first parent
salt.utils.crypt.reinit_crypto()
os._exit(salt.defaults.exitcodes.EX_OK)
except OSError as exc:
log.error('fork #1 failed: %s (%s)', exc.errno, exc)
sys.exit(salt.defaults.exitcodes.EX_GENERIC)
# decouple from parent environment
os.chdir('/')
# noinspection PyArgumentList
os.setsid()
os.umask(0o022) # pylint: disable=blacklisted-function
# do second fork
try:
pid = os.fork()
if pid > 0:
salt.utils.crypt.reinit_crypto()
sys.exit(salt.defaults.exitcodes.EX_OK)
except OSError as exc:
log.error('fork #2 failed: %s (%s)', exc.errno, exc)
sys.exit(salt.defaults.exitcodes.EX_GENERIC)
salt.utils.crypt.reinit_crypto()
# A normal daemonization redirects the process output to /dev/null.
# Unfortunately when a python multiprocess is called the output is
# not cleanly redirected and the parent process dies when the
# multiprocessing process attempts to access stdout or err.
if redirect_out:
with salt.utils.files.fopen('/dev/null', 'r+') as dev_null:
# Redirect python stdin/out/err
# and the os stdin/out/err which can be different
os.dup2(dev_null.fileno(), sys.stdin.fileno())
os.dup2(dev_null.fileno(), sys.stdout.fileno())
os.dup2(dev_null.fileno(), sys.stderr.fileno())
os.dup2(dev_null.fileno(), 0)
os.dup2(dev_null.fileno(), 1)
os.dup2(dev_null.fileno(), 2) | [
"def",
"daemonize",
"(",
"redirect_out",
"=",
"True",
")",
":",
"# Avoid circular import",
"import",
"salt",
".",
"utils",
".",
"crypt",
"try",
":",
"pid",
"=",
"os",
".",
"fork",
"(",
")",
"if",
"pid",
">",
"0",
":",
"# exit first parent",
"salt",
".",
... | Daemonize a process | [
"Daemonize",
"a",
"process"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L63-L110 | train |
saltstack/salt | salt/utils/process.py | daemonize_if | def daemonize_if(opts):
'''
Daemonize a module function process if multiprocessing is True and the
process is not being called by salt-call
'''
if 'salt-call' in sys.argv[0]:
return
if not opts.get('multiprocessing', True):
return
if sys.platform.startswith('win'):
return
daemonize(False) | python | def daemonize_if(opts):
'''
Daemonize a module function process if multiprocessing is True and the
process is not being called by salt-call
'''
if 'salt-call' in sys.argv[0]:
return
if not opts.get('multiprocessing', True):
return
if sys.platform.startswith('win'):
return
daemonize(False) | [
"def",
"daemonize_if",
"(",
"opts",
")",
":",
"if",
"'salt-call'",
"in",
"sys",
".",
"argv",
"[",
"0",
"]",
":",
"return",
"if",
"not",
"opts",
".",
"get",
"(",
"'multiprocessing'",
",",
"True",
")",
":",
"return",
"if",
"sys",
".",
"platform",
".",
... | Daemonize a module function process if multiprocessing is True and the
process is not being called by salt-call | [
"Daemonize",
"a",
"module",
"function",
"process",
"if",
"multiprocessing",
"is",
"True",
"and",
"the",
"process",
"is",
"not",
"being",
"called",
"by",
"salt",
"-",
"call"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L113-L124 | train |
saltstack/salt | salt/utils/process.py | notify_systemd | def notify_systemd():
'''
Notify systemd that this process has started
'''
try:
import systemd.daemon
except ImportError:
if salt.utils.path.which('systemd-notify') \
and systemd_notify_call('--booted'):
# Notify systemd synchronously
notify_socket = os.getenv('NOTIFY_SOCKET')
if notify_socket:
# Handle abstract namespace socket
if notify_socket.startswith('@'):
notify_socket = '\0{0}'.format(notify_socket[1:])
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock.connect(notify_socket)
sock.sendall('READY=1'.encode())
sock.close()
except socket.error:
return systemd_notify_call('--ready')
return True
return False
if systemd.daemon.booted():
try:
return systemd.daemon.notify('READY=1')
except SystemError:
# Daemon was not started by systemd
pass | python | def notify_systemd():
'''
Notify systemd that this process has started
'''
try:
import systemd.daemon
except ImportError:
if salt.utils.path.which('systemd-notify') \
and systemd_notify_call('--booted'):
# Notify systemd synchronously
notify_socket = os.getenv('NOTIFY_SOCKET')
if notify_socket:
# Handle abstract namespace socket
if notify_socket.startswith('@'):
notify_socket = '\0{0}'.format(notify_socket[1:])
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock.connect(notify_socket)
sock.sendall('READY=1'.encode())
sock.close()
except socket.error:
return systemd_notify_call('--ready')
return True
return False
if systemd.daemon.booted():
try:
return systemd.daemon.notify('READY=1')
except SystemError:
# Daemon was not started by systemd
pass | [
"def",
"notify_systemd",
"(",
")",
":",
"try",
":",
"import",
"systemd",
".",
"daemon",
"except",
"ImportError",
":",
"if",
"salt",
".",
"utils",
".",
"path",
".",
"which",
"(",
"'systemd-notify'",
")",
"and",
"systemd_notify_call",
"(",
"'--booted'",
")",
... | Notify systemd that this process has started | [
"Notify",
"systemd",
"that",
"this",
"process",
"has",
"started"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L134-L164 | train |
saltstack/salt | salt/utils/process.py | set_pidfile | def set_pidfile(pidfile, user):
'''
Save the pidfile
'''
pdir = os.path.dirname(pidfile)
if not os.path.isdir(pdir) and pdir:
os.makedirs(pdir)
try:
with salt.utils.files.fopen(pidfile, 'w+') as ofile:
ofile.write(str(os.getpid())) # future lint: disable=blacklisted-function
except IOError:
pass
log.debug('Created pidfile: %s', pidfile)
if salt.utils.platform.is_windows():
return True
import pwd # after confirming not running Windows
#import grp
try:
pwnam = pwd.getpwnam(user)
uid = pwnam[2]
gid = pwnam[3]
#groups = [g.gr_gid for g in grp.getgrall() if user in g.gr_mem]
except (KeyError, IndexError):
sys.stderr.write(
'Failed to set the pid to user: {0}. The user is not '
'available.\n'.format(
user
)
)
sys.exit(salt.defaults.exitcodes.EX_NOUSER)
if os.getuid() == uid:
# The current user already owns the pidfile. Return!
return
try:
os.chown(pidfile, uid, gid)
except OSError as err:
msg = (
'Failed to set the ownership of PID file {0} to user {1}.'.format(
pidfile, user
)
)
log.debug('%s Traceback follows:', msg, exc_info=True)
sys.stderr.write('{0}\n'.format(msg))
sys.exit(err.errno)
log.debug('Chowned pidfile: %s to user: %s', pidfile, user) | python | def set_pidfile(pidfile, user):
'''
Save the pidfile
'''
pdir = os.path.dirname(pidfile)
if not os.path.isdir(pdir) and pdir:
os.makedirs(pdir)
try:
with salt.utils.files.fopen(pidfile, 'w+') as ofile:
ofile.write(str(os.getpid())) # future lint: disable=blacklisted-function
except IOError:
pass
log.debug('Created pidfile: %s', pidfile)
if salt.utils.platform.is_windows():
return True
import pwd # after confirming not running Windows
#import grp
try:
pwnam = pwd.getpwnam(user)
uid = pwnam[2]
gid = pwnam[3]
#groups = [g.gr_gid for g in grp.getgrall() if user in g.gr_mem]
except (KeyError, IndexError):
sys.stderr.write(
'Failed to set the pid to user: {0}. The user is not '
'available.\n'.format(
user
)
)
sys.exit(salt.defaults.exitcodes.EX_NOUSER)
if os.getuid() == uid:
# The current user already owns the pidfile. Return!
return
try:
os.chown(pidfile, uid, gid)
except OSError as err:
msg = (
'Failed to set the ownership of PID file {0} to user {1}.'.format(
pidfile, user
)
)
log.debug('%s Traceback follows:', msg, exc_info=True)
sys.stderr.write('{0}\n'.format(msg))
sys.exit(err.errno)
log.debug('Chowned pidfile: %s to user: %s', pidfile, user) | [
"def",
"set_pidfile",
"(",
"pidfile",
",",
"user",
")",
":",
"pdir",
"=",
"os",
".",
"path",
".",
"dirname",
"(",
"pidfile",
")",
"if",
"not",
"os",
".",
"path",
".",
"isdir",
"(",
"pdir",
")",
"and",
"pdir",
":",
"os",
".",
"makedirs",
"(",
"pdi... | Save the pidfile | [
"Save",
"the",
"pidfile"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L167-L215 | train |
saltstack/salt | salt/utils/process.py | get_pidfile | def get_pidfile(pidfile):
'''
Return the pid from a pidfile as an integer
'''
try:
with salt.utils.files.fopen(pidfile) as pdf:
pid = pdf.read().strip()
return int(pid)
except (OSError, IOError, TypeError, ValueError):
return -1 | python | def get_pidfile(pidfile):
'''
Return the pid from a pidfile as an integer
'''
try:
with salt.utils.files.fopen(pidfile) as pdf:
pid = pdf.read().strip()
return int(pid)
except (OSError, IOError, TypeError, ValueError):
return -1 | [
"def",
"get_pidfile",
"(",
"pidfile",
")",
":",
"try",
":",
"with",
"salt",
".",
"utils",
".",
"files",
".",
"fopen",
"(",
"pidfile",
")",
"as",
"pdf",
":",
"pid",
"=",
"pdf",
".",
"read",
"(",
")",
".",
"strip",
"(",
")",
"return",
"int",
"(",
... | Return the pid from a pidfile as an integer | [
"Return",
"the",
"pid",
"from",
"a",
"pidfile",
"as",
"an",
"integer"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L225-L234 | train |
saltstack/salt | salt/utils/process.py | clean_proc | def clean_proc(proc, wait_for_kill=10):
'''
Generic method for cleaning up multiprocessing procs
'''
# NoneType and other fun stuff need not apply
if not proc:
return
try:
waited = 0
while proc.is_alive():
proc.terminate()
waited += 1
time.sleep(0.1)
if proc.is_alive() and (waited >= wait_for_kill):
log.error('Process did not die with terminate(): %s', proc.pid)
os.kill(proc.pid, signal.SIGKILL)
except (AssertionError, AttributeError):
# Catch AssertionError when the proc is evaluated inside the child
# Catch AttributeError when the process dies between proc.is_alive()
# and proc.terminate() and turns into a NoneType
pass | python | def clean_proc(proc, wait_for_kill=10):
'''
Generic method for cleaning up multiprocessing procs
'''
# NoneType and other fun stuff need not apply
if not proc:
return
try:
waited = 0
while proc.is_alive():
proc.terminate()
waited += 1
time.sleep(0.1)
if proc.is_alive() and (waited >= wait_for_kill):
log.error('Process did not die with terminate(): %s', proc.pid)
os.kill(proc.pid, signal.SIGKILL)
except (AssertionError, AttributeError):
# Catch AssertionError when the proc is evaluated inside the child
# Catch AttributeError when the process dies between proc.is_alive()
# and proc.terminate() and turns into a NoneType
pass | [
"def",
"clean_proc",
"(",
"proc",
",",
"wait_for_kill",
"=",
"10",
")",
":",
"# NoneType and other fun stuff need not apply",
"if",
"not",
"proc",
":",
"return",
"try",
":",
"waited",
"=",
"0",
"while",
"proc",
".",
"is_alive",
"(",
")",
":",
"proc",
".",
... | Generic method for cleaning up multiprocessing procs | [
"Generic",
"method",
"for",
"cleaning",
"up",
"multiprocessing",
"procs"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L237-L257 | train |
saltstack/salt | salt/utils/process.py | os_is_running | def os_is_running(pid):
'''
Use OS facilities to determine if a process is running
'''
if isinstance(pid, six.string_types):
pid = int(pid)
if HAS_PSUTIL:
return psutil.pid_exists(pid)
else:
try:
os.kill(pid, 0) # SIG 0 is the "are you alive?" signal
return True
except OSError:
return False | python | def os_is_running(pid):
'''
Use OS facilities to determine if a process is running
'''
if isinstance(pid, six.string_types):
pid = int(pid)
if HAS_PSUTIL:
return psutil.pid_exists(pid)
else:
try:
os.kill(pid, 0) # SIG 0 is the "are you alive?" signal
return True
except OSError:
return False | [
"def",
"os_is_running",
"(",
"pid",
")",
":",
"if",
"isinstance",
"(",
"pid",
",",
"six",
".",
"string_types",
")",
":",
"pid",
"=",
"int",
"(",
"pid",
")",
"if",
"HAS_PSUTIL",
":",
"return",
"psutil",
".",
"pid_exists",
"(",
"pid",
")",
"else",
":",... | Use OS facilities to determine if a process is running | [
"Use",
"OS",
"facilities",
"to",
"determine",
"if",
"a",
"process",
"is",
"running"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L260-L273 | train |
saltstack/salt | salt/utils/process.py | ProcessManager.add_process | def add_process(self, tgt, args=None, kwargs=None, name=None):
'''
Create a processes and args + kwargs
This will deterimine if it is a Process class, otherwise it assumes
it is a function
'''
if args is None:
args = []
if kwargs is None:
kwargs = {}
if salt.utils.platform.is_windows():
# Need to ensure that 'log_queue' and 'log_queue_level' is
# correctly transferred to processes that inherit from
# 'MultiprocessingProcess'.
if type(MultiprocessingProcess) is type(tgt) and (
issubclass(tgt, MultiprocessingProcess)):
need_log_queue = True
else:
need_log_queue = False
if need_log_queue:
if 'log_queue' not in kwargs:
if hasattr(self, 'log_queue'):
kwargs['log_queue'] = self.log_queue
else:
kwargs['log_queue'] = (
salt.log.setup.get_multiprocessing_logging_queue()
)
if 'log_queue_level' not in kwargs:
if hasattr(self, 'log_queue_level'):
kwargs['log_queue_level'] = self.log_queue_level
else:
kwargs['log_queue_level'] = (
salt.log.setup.get_multiprocessing_logging_level()
)
# create a nicer name for the debug log
if name is None:
if isinstance(tgt, types.FunctionType):
name = '{0}.{1}'.format(
tgt.__module__,
tgt.__name__,
)
else:
name = '{0}{1}.{2}'.format(
tgt.__module__,
'.{0}'.format(tgt.__class__) if six.text_type(tgt.__class__) != "<type 'type'>" else '',
tgt.__name__,
)
if type(multiprocessing.Process) is type(tgt) and issubclass(tgt, multiprocessing.Process):
process = tgt(*args, **kwargs)
else:
process = multiprocessing.Process(target=tgt, args=args, kwargs=kwargs, name=name)
if isinstance(process, SignalHandlingMultiprocessingProcess):
with default_signals(signal.SIGINT, signal.SIGTERM):
process.start()
else:
process.start()
log.debug("Started '%s' with pid %s", name, process.pid)
self._process_map[process.pid] = {'tgt': tgt,
'args': args,
'kwargs': kwargs,
'Process': process}
return process | python | def add_process(self, tgt, args=None, kwargs=None, name=None):
'''
Create a processes and args + kwargs
This will deterimine if it is a Process class, otherwise it assumes
it is a function
'''
if args is None:
args = []
if kwargs is None:
kwargs = {}
if salt.utils.platform.is_windows():
# Need to ensure that 'log_queue' and 'log_queue_level' is
# correctly transferred to processes that inherit from
# 'MultiprocessingProcess'.
if type(MultiprocessingProcess) is type(tgt) and (
issubclass(tgt, MultiprocessingProcess)):
need_log_queue = True
else:
need_log_queue = False
if need_log_queue:
if 'log_queue' not in kwargs:
if hasattr(self, 'log_queue'):
kwargs['log_queue'] = self.log_queue
else:
kwargs['log_queue'] = (
salt.log.setup.get_multiprocessing_logging_queue()
)
if 'log_queue_level' not in kwargs:
if hasattr(self, 'log_queue_level'):
kwargs['log_queue_level'] = self.log_queue_level
else:
kwargs['log_queue_level'] = (
salt.log.setup.get_multiprocessing_logging_level()
)
# create a nicer name for the debug log
if name is None:
if isinstance(tgt, types.FunctionType):
name = '{0}.{1}'.format(
tgt.__module__,
tgt.__name__,
)
else:
name = '{0}{1}.{2}'.format(
tgt.__module__,
'.{0}'.format(tgt.__class__) if six.text_type(tgt.__class__) != "<type 'type'>" else '',
tgt.__name__,
)
if type(multiprocessing.Process) is type(tgt) and issubclass(tgt, multiprocessing.Process):
process = tgt(*args, **kwargs)
else:
process = multiprocessing.Process(target=tgt, args=args, kwargs=kwargs, name=name)
if isinstance(process, SignalHandlingMultiprocessingProcess):
with default_signals(signal.SIGINT, signal.SIGTERM):
process.start()
else:
process.start()
log.debug("Started '%s' with pid %s", name, process.pid)
self._process_map[process.pid] = {'tgt': tgt,
'args': args,
'kwargs': kwargs,
'Process': process}
return process | [
"def",
"add_process",
"(",
"self",
",",
"tgt",
",",
"args",
"=",
"None",
",",
"kwargs",
"=",
"None",
",",
"name",
"=",
"None",
")",
":",
"if",
"args",
"is",
"None",
":",
"args",
"=",
"[",
"]",
"if",
"kwargs",
"is",
"None",
":",
"kwargs",
"=",
"... | Create a processes and args + kwargs
This will deterimine if it is a Process class, otherwise it assumes
it is a function | [
"Create",
"a",
"processes",
"and",
"args",
"+",
"kwargs",
"This",
"will",
"deterimine",
"if",
"it",
"is",
"a",
"Process",
"class",
"otherwise",
"it",
"assumes",
"it",
"is",
"a",
"function"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L366-L433 | train |
saltstack/salt | salt/utils/process.py | ProcessManager.restart_process | def restart_process(self, pid):
'''
Create new process (assuming this one is dead), then remove the old one
'''
if self._restart_processes is False:
return
log.info(
'Process %s (%s) died with exit status %s, restarting...',
self._process_map[pid]['tgt'],
pid,
self._process_map[pid]['Process'].exitcode
)
# don't block, the process is already dead
self._process_map[pid]['Process'].join(1)
self.add_process(self._process_map[pid]['tgt'],
self._process_map[pid]['args'],
self._process_map[pid]['kwargs'])
del self._process_map[pid] | python | def restart_process(self, pid):
'''
Create new process (assuming this one is dead), then remove the old one
'''
if self._restart_processes is False:
return
log.info(
'Process %s (%s) died with exit status %s, restarting...',
self._process_map[pid]['tgt'],
pid,
self._process_map[pid]['Process'].exitcode
)
# don't block, the process is already dead
self._process_map[pid]['Process'].join(1)
self.add_process(self._process_map[pid]['tgt'],
self._process_map[pid]['args'],
self._process_map[pid]['kwargs'])
del self._process_map[pid] | [
"def",
"restart_process",
"(",
"self",
",",
"pid",
")",
":",
"if",
"self",
".",
"_restart_processes",
"is",
"False",
":",
"return",
"log",
".",
"info",
"(",
"'Process %s (%s) died with exit status %s, restarting...'",
",",
"self",
".",
"_process_map",
"[",
"pid",
... | Create new process (assuming this one is dead), then remove the old one | [
"Create",
"new",
"process",
"(",
"assuming",
"this",
"one",
"is",
"dead",
")",
"then",
"remove",
"the",
"old",
"one"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L435-L454 | train |
saltstack/salt | salt/utils/process.py | ProcessManager.run | def run(self, asynchronous=False):
'''
Load and start all available api modules
'''
log.debug('Process Manager starting!')
appendproctitle(self.name)
# make sure to kill the subprocesses if the parent is killed
if signal.getsignal(signal.SIGTERM) is signal.SIG_DFL:
# There are no SIGTERM handlers installed, install ours
signal.signal(signal.SIGTERM, self.kill_children)
if signal.getsignal(signal.SIGINT) is signal.SIG_DFL:
# There are no SIGINT handlers installed, install ours
signal.signal(signal.SIGINT, self.kill_children)
while True:
log.trace('Process manager iteration')
try:
# in case someone died while we were waiting...
self.check_children()
# The event-based subprocesses management code was removed from here
# because os.wait() conflicts with the subprocesses management logic
# implemented in `multiprocessing` package. See #35480 for details.
if asynchronous:
yield gen.sleep(10)
else:
time.sleep(10)
if not self._process_map:
break
# OSError is raised if a signal handler is called (SIGTERM) during os.wait
except OSError:
break
except IOError as exc:
# IOError with errno of EINTR (4) may be raised
# when using time.sleep() on Windows.
if exc.errno != errno.EINTR:
raise
break | python | def run(self, asynchronous=False):
'''
Load and start all available api modules
'''
log.debug('Process Manager starting!')
appendproctitle(self.name)
# make sure to kill the subprocesses if the parent is killed
if signal.getsignal(signal.SIGTERM) is signal.SIG_DFL:
# There are no SIGTERM handlers installed, install ours
signal.signal(signal.SIGTERM, self.kill_children)
if signal.getsignal(signal.SIGINT) is signal.SIG_DFL:
# There are no SIGINT handlers installed, install ours
signal.signal(signal.SIGINT, self.kill_children)
while True:
log.trace('Process manager iteration')
try:
# in case someone died while we were waiting...
self.check_children()
# The event-based subprocesses management code was removed from here
# because os.wait() conflicts with the subprocesses management logic
# implemented in `multiprocessing` package. See #35480 for details.
if asynchronous:
yield gen.sleep(10)
else:
time.sleep(10)
if not self._process_map:
break
# OSError is raised if a signal handler is called (SIGTERM) during os.wait
except OSError:
break
except IOError as exc:
# IOError with errno of EINTR (4) may be raised
# when using time.sleep() on Windows.
if exc.errno != errno.EINTR:
raise
break | [
"def",
"run",
"(",
"self",
",",
"asynchronous",
"=",
"False",
")",
":",
"log",
".",
"debug",
"(",
"'Process Manager starting!'",
")",
"appendproctitle",
"(",
"self",
".",
"name",
")",
"# make sure to kill the subprocesses if the parent is killed",
"if",
"signal",
".... | Load and start all available api modules | [
"Load",
"and",
"start",
"all",
"available",
"api",
"modules"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L485-L522 | train |
saltstack/salt | salt/utils/process.py | ProcessManager.check_children | def check_children(self):
'''
Check the children once
'''
if self._restart_processes is True:
for pid, mapping in six.iteritems(self._process_map):
if not mapping['Process'].is_alive():
log.trace('Process restart of %s', pid)
self.restart_process(pid) | python | def check_children(self):
'''
Check the children once
'''
if self._restart_processes is True:
for pid, mapping in six.iteritems(self._process_map):
if not mapping['Process'].is_alive():
log.trace('Process restart of %s', pid)
self.restart_process(pid) | [
"def",
"check_children",
"(",
"self",
")",
":",
"if",
"self",
".",
"_restart_processes",
"is",
"True",
":",
"for",
"pid",
",",
"mapping",
"in",
"six",
".",
"iteritems",
"(",
"self",
".",
"_process_map",
")",
":",
"if",
"not",
"mapping",
"[",
"'Process'",... | Check the children once | [
"Check",
"the",
"children",
"once"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L524-L532 | train |
saltstack/salt | salt/utils/process.py | ProcessManager.kill_children | def kill_children(self, *args, **kwargs):
'''
Kill all of the children
'''
# first lets reset signal handlers to default one to prevent running this twice
signal.signal(signal.SIGTERM, signal.SIG_IGN)
signal.signal(signal.SIGINT, signal.SIG_IGN)
# check that this is the correct process, children inherit this
# handler, if we are in a child lets just run the original handler
if os.getpid() != self._pid:
if callable(self._sigterm_handler):
return self._sigterm_handler(*args)
elif self._sigterm_handler is not None:
return signal.default_int_handler(signal.SIGTERM)(*args)
else:
return
if salt.utils.platform.is_windows():
if multiprocessing.current_process().name != 'MainProcess':
# Since the main process will kill subprocesses by tree,
# no need to do anything in the subprocesses.
# Sometimes, when both a subprocess and the main process
# call 'taskkill', it will leave a 'taskkill' zombie process.
# We want to avoid this.
return
with salt.utils.files.fopen(os.devnull, 'wb') as devnull:
for pid, p_map in six.iteritems(self._process_map):
# On Windows, we need to explicitly terminate sub-processes
# because the processes don't have a sigterm handler.
subprocess.call(
['taskkill', '/F', '/T', '/PID', six.text_type(pid)],
stdout=devnull, stderr=devnull
)
p_map['Process'].terminate()
else:
for pid, p_map in six.iteritems(self._process_map.copy()):
log.trace('Terminating pid %s: %s', pid, p_map['Process'])
if args:
# escalate the signal to the process
try:
os.kill(pid, args[0])
except OSError:
pass
try:
p_map['Process'].terminate()
except OSError as exc:
if exc.errno not in (errno.ESRCH, errno.EACCES):
raise
if not p_map['Process'].is_alive():
try:
del self._process_map[pid]
except KeyError:
# Race condition
pass
end_time = time.time() + self.wait_for_kill # when to die
log.trace('Waiting to kill process manager children')
while self._process_map and time.time() < end_time:
for pid, p_map in six.iteritems(self._process_map.copy()):
log.trace('Joining pid %s: %s', pid, p_map['Process'])
p_map['Process'].join(0)
if not p_map['Process'].is_alive():
# The process is no longer alive, remove it from the process map dictionary
try:
del self._process_map[pid]
except KeyError:
# This is a race condition if a signal was passed to all children
pass
# if any managed processes still remain to be handled, let's kill them
kill_iterations = 2
while kill_iterations >= 0:
kill_iterations -= 1
for pid, p_map in six.iteritems(self._process_map.copy()):
if not p_map['Process'].is_alive():
# The process is no longer alive, remove it from the process map dictionary
try:
del self._process_map[pid]
except KeyError:
# This is a race condition if a signal was passed to all children
pass
continue
log.trace('Killing pid %s: %s', pid, p_map['Process'])
try:
os.kill(pid, signal.SIGKILL)
except OSError as exc:
log.exception(exc)
# in case the process has since decided to die, os.kill returns OSError
if not p_map['Process'].is_alive():
# The process is no longer alive, remove it from the process map dictionary
try:
del self._process_map[pid]
except KeyError:
# This is a race condition if a signal was passed to all children
pass
if self._process_map:
# Some processes disrespected the KILL signal!!!!
available_retries = kwargs.get('retry', 3)
if available_retries >= 0:
log.info(
'Some processes failed to respect the KILL signal: %s',
'; '.join(
'Process: {0} (Pid: {1})'.format(v['Process'], k) for # pylint: disable=str-format-in-logging
(k, v) in self._process_map.items()
)
)
log.info('kill_children retries left: %s', available_retries)
kwargs['retry'] = available_retries - 1
return self.kill_children(*args, **kwargs)
else:
log.warning(
'Failed to kill the following processes: %s',
'; '.join(
'Process: {0} (Pid: {1})'.format(v['Process'], k) for # pylint: disable=str-format-in-logging
(k, v) in self._process_map.items()
)
)
log.warning(
'Salt will either fail to terminate now or leave some '
'zombie processes behind'
) | python | def kill_children(self, *args, **kwargs):
'''
Kill all of the children
'''
# first lets reset signal handlers to default one to prevent running this twice
signal.signal(signal.SIGTERM, signal.SIG_IGN)
signal.signal(signal.SIGINT, signal.SIG_IGN)
# check that this is the correct process, children inherit this
# handler, if we are in a child lets just run the original handler
if os.getpid() != self._pid:
if callable(self._sigterm_handler):
return self._sigterm_handler(*args)
elif self._sigterm_handler is not None:
return signal.default_int_handler(signal.SIGTERM)(*args)
else:
return
if salt.utils.platform.is_windows():
if multiprocessing.current_process().name != 'MainProcess':
# Since the main process will kill subprocesses by tree,
# no need to do anything in the subprocesses.
# Sometimes, when both a subprocess and the main process
# call 'taskkill', it will leave a 'taskkill' zombie process.
# We want to avoid this.
return
with salt.utils.files.fopen(os.devnull, 'wb') as devnull:
for pid, p_map in six.iteritems(self._process_map):
# On Windows, we need to explicitly terminate sub-processes
# because the processes don't have a sigterm handler.
subprocess.call(
['taskkill', '/F', '/T', '/PID', six.text_type(pid)],
stdout=devnull, stderr=devnull
)
p_map['Process'].terminate()
else:
for pid, p_map in six.iteritems(self._process_map.copy()):
log.trace('Terminating pid %s: %s', pid, p_map['Process'])
if args:
# escalate the signal to the process
try:
os.kill(pid, args[0])
except OSError:
pass
try:
p_map['Process'].terminate()
except OSError as exc:
if exc.errno not in (errno.ESRCH, errno.EACCES):
raise
if not p_map['Process'].is_alive():
try:
del self._process_map[pid]
except KeyError:
# Race condition
pass
end_time = time.time() + self.wait_for_kill # when to die
log.trace('Waiting to kill process manager children')
while self._process_map and time.time() < end_time:
for pid, p_map in six.iteritems(self._process_map.copy()):
log.trace('Joining pid %s: %s', pid, p_map['Process'])
p_map['Process'].join(0)
if not p_map['Process'].is_alive():
# The process is no longer alive, remove it from the process map dictionary
try:
del self._process_map[pid]
except KeyError:
# This is a race condition if a signal was passed to all children
pass
# if any managed processes still remain to be handled, let's kill them
kill_iterations = 2
while kill_iterations >= 0:
kill_iterations -= 1
for pid, p_map in six.iteritems(self._process_map.copy()):
if not p_map['Process'].is_alive():
# The process is no longer alive, remove it from the process map dictionary
try:
del self._process_map[pid]
except KeyError:
# This is a race condition if a signal was passed to all children
pass
continue
log.trace('Killing pid %s: %s', pid, p_map['Process'])
try:
os.kill(pid, signal.SIGKILL)
except OSError as exc:
log.exception(exc)
# in case the process has since decided to die, os.kill returns OSError
if not p_map['Process'].is_alive():
# The process is no longer alive, remove it from the process map dictionary
try:
del self._process_map[pid]
except KeyError:
# This is a race condition if a signal was passed to all children
pass
if self._process_map:
# Some processes disrespected the KILL signal!!!!
available_retries = kwargs.get('retry', 3)
if available_retries >= 0:
log.info(
'Some processes failed to respect the KILL signal: %s',
'; '.join(
'Process: {0} (Pid: {1})'.format(v['Process'], k) for # pylint: disable=str-format-in-logging
(k, v) in self._process_map.items()
)
)
log.info('kill_children retries left: %s', available_retries)
kwargs['retry'] = available_retries - 1
return self.kill_children(*args, **kwargs)
else:
log.warning(
'Failed to kill the following processes: %s',
'; '.join(
'Process: {0} (Pid: {1})'.format(v['Process'], k) for # pylint: disable=str-format-in-logging
(k, v) in self._process_map.items()
)
)
log.warning(
'Salt will either fail to terminate now or leave some '
'zombie processes behind'
) | [
"def",
"kill_children",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"# first lets reset signal handlers to default one to prevent running this twice",
"signal",
".",
"signal",
"(",
"signal",
".",
"SIGTERM",
",",
"signal",
".",
"SIG_IGN",
")",
... | Kill all of the children | [
"Kill",
"all",
"of",
"the",
"children"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/utils/process.py#L534-L657 | train |
saltstack/salt | salt/modules/victorops.py | _query | def _query(action=None,
routing_key=None,
args=None,
method='GET',
header_dict=None,
data=None):
'''
Make a web call to VictorOps
'''
api_key = __salt__['config.get']('victorops.api_key') or \
__salt__['config.get']('victorops:api_key')
path = 'https://alert.victorops.com/integrations/generic/20131114/'
if action:
path += '{0}/'.format(action)
if api_key:
path += '{0}/'.format(api_key)
if routing_key:
path += routing_key
log.debug('VictorOps URL: %s', path)
if not isinstance(args, dict):
args = {}
if header_dict is None:
header_dict = {'Content-type': 'application/json'}
if method != 'POST':
header_dict['Accept'] = 'application/json'
decode = True
if method == 'DELETE':
decode = False
result = salt.utils.http.query(
path,
method,
params=args,
data=data,
header_dict=header_dict,
decode=decode,
decode_type='json',
text=True,
status=True,
cookies=True,
persist_session=True,
opts=__opts__,
)
if 'error' in result:
log.error(result['error'])
return [result['status'], result['error']]
return [result['status'], result.get('dict', {})] | python | def _query(action=None,
routing_key=None,
args=None,
method='GET',
header_dict=None,
data=None):
'''
Make a web call to VictorOps
'''
api_key = __salt__['config.get']('victorops.api_key') or \
__salt__['config.get']('victorops:api_key')
path = 'https://alert.victorops.com/integrations/generic/20131114/'
if action:
path += '{0}/'.format(action)
if api_key:
path += '{0}/'.format(api_key)
if routing_key:
path += routing_key
log.debug('VictorOps URL: %s', path)
if not isinstance(args, dict):
args = {}
if header_dict is None:
header_dict = {'Content-type': 'application/json'}
if method != 'POST':
header_dict['Accept'] = 'application/json'
decode = True
if method == 'DELETE':
decode = False
result = salt.utils.http.query(
path,
method,
params=args,
data=data,
header_dict=header_dict,
decode=decode,
decode_type='json',
text=True,
status=True,
cookies=True,
persist_session=True,
opts=__opts__,
)
if 'error' in result:
log.error(result['error'])
return [result['status'], result['error']]
return [result['status'], result.get('dict', {})] | [
"def",
"_query",
"(",
"action",
"=",
"None",
",",
"routing_key",
"=",
"None",
",",
"args",
"=",
"None",
",",
"method",
"=",
"'GET'",
",",
"header_dict",
"=",
"None",
",",
"data",
"=",
"None",
")",
":",
"api_key",
"=",
"__salt__",
"[",
"'config.get'",
... | Make a web call to VictorOps | [
"Make",
"a",
"web",
"call",
"to",
"VictorOps"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/victorops.py#L41-L97 | train |
saltstack/salt | salt/modules/victorops.py | create_event | def create_event(message_type=None, routing_key='everybody', **kwargs):
'''
Create an event in VictorOps. Designed for use in states.
The following parameters are required:
:param message_type: One of the following values: INFO, WARNING, ACKNOWLEDGEMENT, CRITICAL, RECOVERY.
The following parameters are optional:
:param routing_key: The key for where messages should be routed. By default, sent to
'everyone' route.
:param entity_id: The name of alerting entity. If not provided, a random name will be assigned.
:param timestamp: Timestamp of the alert in seconds since epoch. Defaults to the
time the alert is received at VictorOps.
:param timestamp_fmt The date format for the timestamp parameter.
:param state_start_time: The time this entity entered its current state
(seconds since epoch). Defaults to the time alert is received.
:param state_start_time_fmt: The date format for the timestamp parameter.
:param state_message: Any additional status information from the alert item.
:param entity_is_host: Used within VictorOps to select the appropriate
display format for the incident.
:param entity_display_name: Used within VictorOps to display a human-readable name for the entity.
:param ack_message: A user entered comment for the acknowledgment.
:param ack_author: The user that acknowledged the incident.
:return: A dictionary with result, entity_id, and message if result was failure.
CLI Example:
.. code-block:: yaml
salt myminion victorops.create_event message_type='CRITICAL' routing_key='everyone' \
entity_id='hostname/diskspace'
salt myminion victorops.create_event message_type='ACKNOWLEDGEMENT' routing_key='everyone' \
entity_id='hostname/diskspace' ack_message='Acknowledged' ack_author='username'
salt myminion victorops.create_event message_type='RECOVERY' routing_key='everyone' \
entity_id='hostname/diskspace'
The following parameters are required:
message_type
'''
keyword_args = {'entity_id': str,
'state_message': str,
'entity_is_host': bool,
'entity_display_name': str,
'ack_message': str,
'ack_author': str
}
data = {}
if not message_type:
raise SaltInvocationError('Required argument "message_type" is missing.')
if message_type.upper() not in ['INFO', 'WARNING', 'ACKNOWLEDGEMENT', 'CRITICAL', 'RECOVERY']:
raise SaltInvocationError('"message_type" must be INFO, WARNING, ACKNOWLEDGEMENT, CRITICAL, or RECOVERY.')
data['message_type'] = message_type
data['monitoring_tool'] = 'SaltStack'
if 'timestamp' in kwargs:
timestamp_fmt = kwargs.get('timestamp_fmt', '%Y-%m-%dT%H:%M:%S')
try:
timestamp = datetime.datetime.strptime(kwargs['timestamp'], timestamp_fmt)
data['timestamp'] = int(time.mktime(timestamp.timetuple()))
except (TypeError, ValueError):
raise SaltInvocationError('Date string could not be parsed: {0}, {1}'.format(
kwargs['timestamp'], timestamp_fmt)
)
if 'state_start_time' in kwargs:
state_start_time_fmt = kwargs.get('state_start_time_fmt', '%Y-%m-%dT%H:%M:%S')
try:
state_start_time = datetime.datetime.strptime(kwargs['state_start_time'], state_start_time_fmt)
data['state_start_time'] = int(time.mktime(state_start_time.timetuple()))
except (TypeError, ValueError):
raise SaltInvocationError('Date string could not be parsed: {0}, {1}'.format(
kwargs['state_start_time'], state_start_time_fmt)
)
for kwarg in keyword_args:
if kwarg in kwargs:
if isinstance(kwargs[kwarg], keyword_args[kwarg]):
data[kwarg] = kwargs[kwarg]
else:
# Should this faile on the wrong type.
log.error('Wrong type, skipping %s', kwarg)
status, result = _query(action='alert',
routing_key=routing_key,
data=salt.utils.json.dumps(data),
method='POST'
)
return result | python | def create_event(message_type=None, routing_key='everybody', **kwargs):
'''
Create an event in VictorOps. Designed for use in states.
The following parameters are required:
:param message_type: One of the following values: INFO, WARNING, ACKNOWLEDGEMENT, CRITICAL, RECOVERY.
The following parameters are optional:
:param routing_key: The key for where messages should be routed. By default, sent to
'everyone' route.
:param entity_id: The name of alerting entity. If not provided, a random name will be assigned.
:param timestamp: Timestamp of the alert in seconds since epoch. Defaults to the
time the alert is received at VictorOps.
:param timestamp_fmt The date format for the timestamp parameter.
:param state_start_time: The time this entity entered its current state
(seconds since epoch). Defaults to the time alert is received.
:param state_start_time_fmt: The date format for the timestamp parameter.
:param state_message: Any additional status information from the alert item.
:param entity_is_host: Used within VictorOps to select the appropriate
display format for the incident.
:param entity_display_name: Used within VictorOps to display a human-readable name for the entity.
:param ack_message: A user entered comment for the acknowledgment.
:param ack_author: The user that acknowledged the incident.
:return: A dictionary with result, entity_id, and message if result was failure.
CLI Example:
.. code-block:: yaml
salt myminion victorops.create_event message_type='CRITICAL' routing_key='everyone' \
entity_id='hostname/diskspace'
salt myminion victorops.create_event message_type='ACKNOWLEDGEMENT' routing_key='everyone' \
entity_id='hostname/diskspace' ack_message='Acknowledged' ack_author='username'
salt myminion victorops.create_event message_type='RECOVERY' routing_key='everyone' \
entity_id='hostname/diskspace'
The following parameters are required:
message_type
'''
keyword_args = {'entity_id': str,
'state_message': str,
'entity_is_host': bool,
'entity_display_name': str,
'ack_message': str,
'ack_author': str
}
data = {}
if not message_type:
raise SaltInvocationError('Required argument "message_type" is missing.')
if message_type.upper() not in ['INFO', 'WARNING', 'ACKNOWLEDGEMENT', 'CRITICAL', 'RECOVERY']:
raise SaltInvocationError('"message_type" must be INFO, WARNING, ACKNOWLEDGEMENT, CRITICAL, or RECOVERY.')
data['message_type'] = message_type
data['monitoring_tool'] = 'SaltStack'
if 'timestamp' in kwargs:
timestamp_fmt = kwargs.get('timestamp_fmt', '%Y-%m-%dT%H:%M:%S')
try:
timestamp = datetime.datetime.strptime(kwargs['timestamp'], timestamp_fmt)
data['timestamp'] = int(time.mktime(timestamp.timetuple()))
except (TypeError, ValueError):
raise SaltInvocationError('Date string could not be parsed: {0}, {1}'.format(
kwargs['timestamp'], timestamp_fmt)
)
if 'state_start_time' in kwargs:
state_start_time_fmt = kwargs.get('state_start_time_fmt', '%Y-%m-%dT%H:%M:%S')
try:
state_start_time = datetime.datetime.strptime(kwargs['state_start_time'], state_start_time_fmt)
data['state_start_time'] = int(time.mktime(state_start_time.timetuple()))
except (TypeError, ValueError):
raise SaltInvocationError('Date string could not be parsed: {0}, {1}'.format(
kwargs['state_start_time'], state_start_time_fmt)
)
for kwarg in keyword_args:
if kwarg in kwargs:
if isinstance(kwargs[kwarg], keyword_args[kwarg]):
data[kwarg] = kwargs[kwarg]
else:
# Should this faile on the wrong type.
log.error('Wrong type, skipping %s', kwarg)
status, result = _query(action='alert',
routing_key=routing_key,
data=salt.utils.json.dumps(data),
method='POST'
)
return result | [
"def",
"create_event",
"(",
"message_type",
"=",
"None",
",",
"routing_key",
"=",
"'everybody'",
",",
"*",
"*",
"kwargs",
")",
":",
"keyword_args",
"=",
"{",
"'entity_id'",
":",
"str",
",",
"'state_message'",
":",
"str",
",",
"'entity_is_host'",
":",
"bool",... | Create an event in VictorOps. Designed for use in states.
The following parameters are required:
:param message_type: One of the following values: INFO, WARNING, ACKNOWLEDGEMENT, CRITICAL, RECOVERY.
The following parameters are optional:
:param routing_key: The key for where messages should be routed. By default, sent to
'everyone' route.
:param entity_id: The name of alerting entity. If not provided, a random name will be assigned.
:param timestamp: Timestamp of the alert in seconds since epoch. Defaults to the
time the alert is received at VictorOps.
:param timestamp_fmt The date format for the timestamp parameter.
:param state_start_time: The time this entity entered its current state
(seconds since epoch). Defaults to the time alert is received.
:param state_start_time_fmt: The date format for the timestamp parameter.
:param state_message: Any additional status information from the alert item.
:param entity_is_host: Used within VictorOps to select the appropriate
display format for the incident.
:param entity_display_name: Used within VictorOps to display a human-readable name for the entity.
:param ack_message: A user entered comment for the acknowledgment.
:param ack_author: The user that acknowledged the incident.
:return: A dictionary with result, entity_id, and message if result was failure.
CLI Example:
.. code-block:: yaml
salt myminion victorops.create_event message_type='CRITICAL' routing_key='everyone' \
entity_id='hostname/diskspace'
salt myminion victorops.create_event message_type='ACKNOWLEDGEMENT' routing_key='everyone' \
entity_id='hostname/diskspace' ack_message='Acknowledged' ack_author='username'
salt myminion victorops.create_event message_type='RECOVERY' routing_key='everyone' \
entity_id='hostname/diskspace'
The following parameters are required:
message_type | [
"Create",
"an",
"event",
"in",
"VictorOps",
".",
"Designed",
"for",
"use",
"in",
"states",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/victorops.py#L100-L211 | train |
saltstack/salt | salt/states/telemetry_alert.py | present | def present(name, deployment_id, metric_name, alert_config, api_key=None, profile='telemetry'):
'''
Ensure the telemetry alert exists.
name
An optional description of the alarm (not currently supported by telemetry API)
deployment_id
Specifies the ID of the root deployment resource
(replica set cluster or sharded cluster) to which this alert definition is attached
metric_name
Specifies the unique ID of the metric to whose values these thresholds will be applied
alert_config: Is a list of dictionaries where each dict contains the following fields:
filter
By default the alert will apply to the deployment and all its constituent resources.
If the alert only applies to a subset of those resources, a filter may be specified to narrow this scope.
min
the smallest "ok" value the metric may take on; if missing or null, no minimum is enforced.
max
the largest "ok" value the metric may take on; if missing or null, no maximum is enforced.
notify_all
Used to indicate if you want to alert both onCallEngineer and apiNotifications
api_key
Telemetry api key for the user
profile
A dict of telemetry config information. If present, will be used instead of
api_key.
'''
ret = {'name': metric_name, 'result': True, 'comment': '', 'changes': {}}
saved_alert_config = __salt__['telemetry.get_alert_config'](
deployment_id, metric_name, api_key, profile)
post_body = {
"deployment": deployment_id,
"filter": alert_config.get('filter'),
"notificationChannel": __salt__['telemetry.get_notification_channel_id'](alert_config.get('escalate_to')).split(),
"condition": {
"metric": metric_name,
"max": alert_config.get('max'),
"min": alert_config.get('min')
}
}
# Diff the alert config with the passed-in attributes
difference = []
if saved_alert_config:
#del saved_alert_config["_id"]
for k, v in post_body.items():
if k not in saved_alert_config:
difference.append("{0}={1} (new)".format(k, v))
continue
v2 = saved_alert_config[k]
if v == v2:
continue
if isinstance(v, string_types) and six.text_type(v) == six.text_type(v2):
continue
if isinstance(v, float) and v == float(v2):
continue
if isinstance(v, int) and v == int(v2):
continue
difference.append("{0}='{1}' was: '{2}'".format(k, v, v2))
else:
difference.append("new alert config")
create_or_update_args = (
deployment_id,
metric_name,
alert_config,
api_key,
profile,
)
if saved_alert_config: # alert config is present. update, or do nothing
# check to see if attributes matches is_present. If so, do nothing.
if not difference:
ret['comment'] = "alert config {0} present and matching".format(metric_name)
return ret
if __opts__['test']:
msg = 'alert config {0} is to be updated.'.format(metric_name)
ret['comment'] = msg
ret['result'] = "\n".join(difference)
return ret
result, msg = __salt__['telemetry.update_alarm'](*create_or_update_args)
if result:
ret['changes']['diff'] = difference
ret['comment'] = "Alert updated."
else:
ret['result'] = False
ret['comment'] = 'Failed to update {0} alert config: {1}'.format(metric_name, msg)
else: # alert config is absent. create it.
if __opts__['test']:
msg = 'alert config {0} is to be created.'.format(metric_name)
ret['comment'] = msg
ret['result'] = None
return ret
result, msg = __salt__['telemetry.create_alarm'](*create_or_update_args)
if result:
ret['changes']['new'] = msg
else:
ret['result'] = False
ret['comment'] = 'Failed to create {0} alert config: {1}'.format(metric_name, msg)
return ret | python | def present(name, deployment_id, metric_name, alert_config, api_key=None, profile='telemetry'):
'''
Ensure the telemetry alert exists.
name
An optional description of the alarm (not currently supported by telemetry API)
deployment_id
Specifies the ID of the root deployment resource
(replica set cluster or sharded cluster) to which this alert definition is attached
metric_name
Specifies the unique ID of the metric to whose values these thresholds will be applied
alert_config: Is a list of dictionaries where each dict contains the following fields:
filter
By default the alert will apply to the deployment and all its constituent resources.
If the alert only applies to a subset of those resources, a filter may be specified to narrow this scope.
min
the smallest "ok" value the metric may take on; if missing or null, no minimum is enforced.
max
the largest "ok" value the metric may take on; if missing or null, no maximum is enforced.
notify_all
Used to indicate if you want to alert both onCallEngineer and apiNotifications
api_key
Telemetry api key for the user
profile
A dict of telemetry config information. If present, will be used instead of
api_key.
'''
ret = {'name': metric_name, 'result': True, 'comment': '', 'changes': {}}
saved_alert_config = __salt__['telemetry.get_alert_config'](
deployment_id, metric_name, api_key, profile)
post_body = {
"deployment": deployment_id,
"filter": alert_config.get('filter'),
"notificationChannel": __salt__['telemetry.get_notification_channel_id'](alert_config.get('escalate_to')).split(),
"condition": {
"metric": metric_name,
"max": alert_config.get('max'),
"min": alert_config.get('min')
}
}
# Diff the alert config with the passed-in attributes
difference = []
if saved_alert_config:
#del saved_alert_config["_id"]
for k, v in post_body.items():
if k not in saved_alert_config:
difference.append("{0}={1} (new)".format(k, v))
continue
v2 = saved_alert_config[k]
if v == v2:
continue
if isinstance(v, string_types) and six.text_type(v) == six.text_type(v2):
continue
if isinstance(v, float) and v == float(v2):
continue
if isinstance(v, int) and v == int(v2):
continue
difference.append("{0}='{1}' was: '{2}'".format(k, v, v2))
else:
difference.append("new alert config")
create_or_update_args = (
deployment_id,
metric_name,
alert_config,
api_key,
profile,
)
if saved_alert_config: # alert config is present. update, or do nothing
# check to see if attributes matches is_present. If so, do nothing.
if not difference:
ret['comment'] = "alert config {0} present and matching".format(metric_name)
return ret
if __opts__['test']:
msg = 'alert config {0} is to be updated.'.format(metric_name)
ret['comment'] = msg
ret['result'] = "\n".join(difference)
return ret
result, msg = __salt__['telemetry.update_alarm'](*create_or_update_args)
if result:
ret['changes']['diff'] = difference
ret['comment'] = "Alert updated."
else:
ret['result'] = False
ret['comment'] = 'Failed to update {0} alert config: {1}'.format(metric_name, msg)
else: # alert config is absent. create it.
if __opts__['test']:
msg = 'alert config {0} is to be created.'.format(metric_name)
ret['comment'] = msg
ret['result'] = None
return ret
result, msg = __salt__['telemetry.create_alarm'](*create_or_update_args)
if result:
ret['changes']['new'] = msg
else:
ret['result'] = False
ret['comment'] = 'Failed to create {0} alert config: {1}'.format(metric_name, msg)
return ret | [
"def",
"present",
"(",
"name",
",",
"deployment_id",
",",
"metric_name",
",",
"alert_config",
",",
"api_key",
"=",
"None",
",",
"profile",
"=",
"'telemetry'",
")",
":",
"ret",
"=",
"{",
"'name'",
":",
"metric_name",
",",
"'result'",
":",
"True",
",",
"'c... | Ensure the telemetry alert exists.
name
An optional description of the alarm (not currently supported by telemetry API)
deployment_id
Specifies the ID of the root deployment resource
(replica set cluster or sharded cluster) to which this alert definition is attached
metric_name
Specifies the unique ID of the metric to whose values these thresholds will be applied
alert_config: Is a list of dictionaries where each dict contains the following fields:
filter
By default the alert will apply to the deployment and all its constituent resources.
If the alert only applies to a subset of those resources, a filter may be specified to narrow this scope.
min
the smallest "ok" value the metric may take on; if missing or null, no minimum is enforced.
max
the largest "ok" value the metric may take on; if missing or null, no maximum is enforced.
notify_all
Used to indicate if you want to alert both onCallEngineer and apiNotifications
api_key
Telemetry api key for the user
profile
A dict of telemetry config information. If present, will be used instead of
api_key. | [
"Ensure",
"the",
"telemetry",
"alert",
"exists",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/telemetry_alert.py#L40-L155 | train |
saltstack/salt | salt/states/telemetry_alert.py | absent | def absent(name, deployment_id, metric_name, api_key=None, profile="telemetry"):
'''
Ensure the telemetry alert config is deleted
name
An optional description of the alarms (not currently supported by telemetry API)
deployment_id
Specifies the ID of the root deployment resource
(replica set cluster or sharded cluster) to which this alert definition is attached
metric_name
Specifies the unique ID of the metric to whose values these thresholds will be applied
api_key
Telemetry api key for the user
profile
A dict with telemetry config data. If present, will be used instead of
api_key.
'''
ret = {'name': metric_name, 'result': True, 'comment': '', 'changes': {}}
is_present = __salt__['telemetry.get_alert_config'](
deployment_id, metric_name, api_key, profile)
if is_present:
alert_id = is_present.get('_id')
if __opts__['test']:
ret['comment'] = 'alert {0} is set to be removed from deployment: {1}.'.format(metric_name, deployment_id)
ret['result'] = None
return ret
deleted, msg = __salt__['telemetry.delete_alarms'](
deployment_id, alert_id, is_present.get('condition', {}).get('metric'), api_key, profile)
if deleted:
ret['changes']['old'] = metric_name
ret['changes']['new'] = None
else:
ret['result'] = False
ret['comment'] = 'Failed to delete alert {0} from deployment: {1}'.format(metric_name, msg)
else:
ret['comment'] = 'alarm on {0} does not exist within {1}.'.format(metric_name, deployment_id)
return ret | python | def absent(name, deployment_id, metric_name, api_key=None, profile="telemetry"):
'''
Ensure the telemetry alert config is deleted
name
An optional description of the alarms (not currently supported by telemetry API)
deployment_id
Specifies the ID of the root deployment resource
(replica set cluster or sharded cluster) to which this alert definition is attached
metric_name
Specifies the unique ID of the metric to whose values these thresholds will be applied
api_key
Telemetry api key for the user
profile
A dict with telemetry config data. If present, will be used instead of
api_key.
'''
ret = {'name': metric_name, 'result': True, 'comment': '', 'changes': {}}
is_present = __salt__['telemetry.get_alert_config'](
deployment_id, metric_name, api_key, profile)
if is_present:
alert_id = is_present.get('_id')
if __opts__['test']:
ret['comment'] = 'alert {0} is set to be removed from deployment: {1}.'.format(metric_name, deployment_id)
ret['result'] = None
return ret
deleted, msg = __salt__['telemetry.delete_alarms'](
deployment_id, alert_id, is_present.get('condition', {}).get('metric'), api_key, profile)
if deleted:
ret['changes']['old'] = metric_name
ret['changes']['new'] = None
else:
ret['result'] = False
ret['comment'] = 'Failed to delete alert {0} from deployment: {1}'.format(metric_name, msg)
else:
ret['comment'] = 'alarm on {0} does not exist within {1}.'.format(metric_name, deployment_id)
return ret | [
"def",
"absent",
"(",
"name",
",",
"deployment_id",
",",
"metric_name",
",",
"api_key",
"=",
"None",
",",
"profile",
"=",
"\"telemetry\"",
")",
":",
"ret",
"=",
"{",
"'name'",
":",
"metric_name",
",",
"'result'",
":",
"True",
",",
"'comment'",
":",
"''",... | Ensure the telemetry alert config is deleted
name
An optional description of the alarms (not currently supported by telemetry API)
deployment_id
Specifies the ID of the root deployment resource
(replica set cluster or sharded cluster) to which this alert definition is attached
metric_name
Specifies the unique ID of the metric to whose values these thresholds will be applied
api_key
Telemetry api key for the user
profile
A dict with telemetry config data. If present, will be used instead of
api_key. | [
"Ensure",
"the",
"telemetry",
"alert",
"config",
"is",
"deleted"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/states/telemetry_alert.py#L158-L201 | train |
saltstack/salt | salt/modules/mac_pkgutil.py | list_ | def list_():
'''
List the installed packages.
:return: A list of installed packages
:rtype: list
CLI Example:
.. code-block:: bash
salt '*' pkgutil.list
'''
cmd = 'pkgutil --pkgs'
ret = salt.utils.mac_utils.execute_return_result(cmd)
return ret.splitlines() | python | def list_():
'''
List the installed packages.
:return: A list of installed packages
:rtype: list
CLI Example:
.. code-block:: bash
salt '*' pkgutil.list
'''
cmd = 'pkgutil --pkgs'
ret = salt.utils.mac_utils.execute_return_result(cmd)
return ret.splitlines() | [
"def",
"list_",
"(",
")",
":",
"cmd",
"=",
"'pkgutil --pkgs'",
"ret",
"=",
"salt",
".",
"utils",
".",
"mac_utils",
".",
"execute_return_result",
"(",
"cmd",
")",
"return",
"ret",
".",
"splitlines",
"(",
")"
] | List the installed packages.
:return: A list of installed packages
:rtype: list
CLI Example:
.. code-block:: bash
salt '*' pkgutil.list | [
"List",
"the",
"installed",
"packages",
"."
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/mac_pkgutil.py#L39-L54 | train |
saltstack/salt | salt/modules/mac_pkgutil.py | _install_from_path | def _install_from_path(path):
'''
Internal function to install a package from the given path
'''
if not os.path.exists(path):
msg = 'File not found: {0}'.format(path)
raise SaltInvocationError(msg)
cmd = 'installer -pkg "{0}" -target /'.format(path)
return salt.utils.mac_utils.execute_return_success(cmd) | python | def _install_from_path(path):
'''
Internal function to install a package from the given path
'''
if not os.path.exists(path):
msg = 'File not found: {0}'.format(path)
raise SaltInvocationError(msg)
cmd = 'installer -pkg "{0}" -target /'.format(path)
return salt.utils.mac_utils.execute_return_success(cmd) | [
"def",
"_install_from_path",
"(",
"path",
")",
":",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"path",
")",
":",
"msg",
"=",
"'File not found: {0}'",
".",
"format",
"(",
"path",
")",
"raise",
"SaltInvocationError",
"(",
"msg",
")",
"cmd",
"=",
... | Internal function to install a package from the given path | [
"Internal",
"function",
"to",
"install",
"a",
"package",
"from",
"the",
"given",
"path"
] | e8541fd6e744ab0df786c0f76102e41631f45d46 | https://github.com/saltstack/salt/blob/e8541fd6e744ab0df786c0f76102e41631f45d46/salt/modules/mac_pkgutil.py#L73-L82 | train |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.