本文整理汇总了Python中c7n.mu.PythonPackageArchive.add_contents方法的典型用法代码示例。如果您正苦于以下问题:Python PythonPackageArchive.add_contents方法的具体用法?Python PythonPackageArchive.add_contents怎么用?Python PythonPackageArchive.add_contents使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类c7n.mu.PythonPackageArchive
的用法示例。
在下文中一共展示了PythonPackageArchive.add_contents方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_function
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def get_function(session_factory, options, groups):
config = dict(
name='cloud-maid-error-notify',
handler='logsub.process_log_event',
runtime='python2.7',
memory_size=512,
timeout=15,
role=options.role,
description='Maid Error Notify',
events=[
CloudWatchLogSubscription(
session_factory, groups, options.pattern)])
archive = PythonPackageArchive(
# Directory to lambda file
os.path.join(
os.path.dirname(inspect.getabsfile(c7n)), 'logsub.py'),
# Don't include virtualenv deps
lib_filter=lambda x, y, z: ([], []))
archive.create()
archive.add_contents(
'config.json', json.dumps({
'topic': options.topic,
'subject': options.subject
}))
archive.close()
return LambdaFunction(config, archive)
示例2: get_function
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def get_function(session_factory, name, role, sns_topic, log_groups,
subject="Lambda Error", pattern="Traceback"):
"""Lambda function provisioning.
Self contained within the component, to allow for easier reuse.
"""
# Lazy import to avoid runtime dependency
from c7n.mu import (
LambdaFunction, PythonPackageArchive, CloudWatchLogSubscription)
config = dict(
name=name,
handler='logsub.process_log_event',
runtime='python2.7',
memory_size=512,
timeout=15,
role=role,
description='Custodian Ops Error Notify',
events=[
CloudWatchLogSubscription(
session_factory, log_groups, pattern)])
archive = PythonPackageArchive()
archive.add_py_file(__file__)
archive.add_contents(
'config.json', json.dumps({
'topic': sns_topic,
'subject': subject
}))
archive.close()
return LambdaFunction(config, archive)
示例3: create_function
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def create_function(self, client, name):
archive = PythonPackageArchive()
self.addCleanup(archive.remove)
archive.add_contents('index.py', SAMPLE_FUNC)
archive.close()
lfunc = client.create_function(
FunctionName=name,
Runtime="python2.7",
MemorySize=128,
Handler='index.handler',
Publish=True,
Role='arn:aws:iam::644160558196:role/lambda_basic_execution',
Code={'ZipFile': archive.get_bytes()})
self.addCleanup(client.delete_function, FunctionName=name)
return lfunc
示例4: test_lambda_cross_account
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def test_lambda_cross_account(self):
self.patch(CrossAccountAccessFilter, "executor_factory", MainThreadExecutor)
session_factory = self.replay_flight_data("test_cross_account_lambda")
client = session_factory().client("lambda")
name = "c7n-cross-check"
tmp_dir = tempfile.mkdtemp()
self.addCleanup(os.rmdir, tmp_dir)
archive = PythonPackageArchive()
archive.add_contents("handler.py", LAMBDA_SRC)
archive.close()
func = LambdaFunction(
{
"runtime": "python2.7",
"name": name,
"description": "",
"handler": "handler.handler",
"memory_size": 128,
"timeout": 5,
"role": self.role,
},
archive,
)
manager = LambdaManager(session_factory)
manager.publish(func)
self.addCleanup(manager.remove, func)
client.add_permission(
FunctionName=name,
StatementId="oops",
Principal="*",
Action="lambda:InvokeFunction",
)
p = self.load_policy(
{
"name": "lambda-cross",
"resource": "lambda",
"filters": ["cross-account"],
},
session_factory=session_factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["FunctionName"], name)
示例5: make_func
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def make_func(self, **kw):
func_data = dict(
name='test-foo-bar',
handler='index.handler',
memory_size=128,
timeout=3,
role=ROLE,
runtime='python2.7',
description='test')
func_data.update(kw)
archive = PythonPackageArchive()
archive.add_contents('index.py',
'''def handler(*a, **kw):\n print("Greetings, program!")''')
archive.close()
self.addCleanup(archive.remove)
return LambdaFunction(func_data, archive)
示例6: make_func
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def make_func(self, **kw):
func_data = dict(
name="test-foo-bar",
handler="index.handler",
memory_size=128,
timeout=3,
role=ROLE,
runtime="python2.7",
description="test",
)
func_data.update(kw)
archive = PythonPackageArchive()
archive.add_contents(
"index.py", """def handler(*a, **kw):\n print("Greetings, program!")"""
)
archive.close()
self.addCleanup(archive.remove)
return LambdaFunction(func_data, archive)
示例7: get_function
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def get_function(session_factory, name, role, sns_topic, log_groups,
subject="Lambda Error", pattern="Traceback"):
"""Lambda function provisioning.
Self contained within the component, to allow for easier reuse.
"""
# Lazy import to avoid runtime dependency
import inspect
import os
import c7n
from c7n.mu import (
LambdaFunction, PythonPackageArchive, CloudWatchLogSubscription)
config = dict(
name=name,
handler='logsub.process_log_event',
runtime='python2.7',
memory_size=512,
timeout=15,
role=role,
description='Custodian Ops Error Notify',
events=[
CloudWatchLogSubscription(
session_factory, log_groups, pattern)])
archive = PythonPackageArchive(
# Directory to lambda file
os.path.join(
os.path.dirname(inspect.getabsfile(c7n)), 'ufuncs', 'logsub.py'),
# Don't include virtualenv deps
lib_filter=lambda x, y, z: ([], []))
archive.create()
archive.add_contents(
'config.json', json.dumps({
'topic': sns_topic,
'subject': subject
}))
archive.close()
return LambdaFunction(config, archive)
示例8: test_lambda_cross_account
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def test_lambda_cross_account(self):
self.patch(
CrossAccountAccessFilter, 'executor_factory', MainThreadExecutor)
session_factory = self.replay_flight_data('test_cross_account_lambda')
client = session_factory().client('lambda')
name = 'c7n-cross-check'
tmp_dir = tempfile.mkdtemp()
self.addCleanup(os.rmdir, tmp_dir)
archive = PythonPackageArchive(tmp_dir, tmp_dir)
archive.create()
archive.add_contents('handler.py', LAMBDA_SRC)
archive.close()
func = LambdaFunction({
'runtime': 'python2.7',
'name': name, 'description': '',
'handler': 'handler.handler',
'memory_size': 128,
'timeout': 5,
'role': self.role}, archive)
manager = LambdaManager(session_factory)
info = manager.publish(func)
self.addCleanup(manager.remove, func)
client.add_permission(
FunctionName=name,
StatementId='oops',
Principal='*',
Action='lambda:InvokeFunction')
p = self.load_policy(
{'name': 'lambda-cross',
'resource': 'lambda',
'filters': ['cross-account']},
session_factory=session_factory)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]['FunctionName'], name)
示例9: get_function
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def get_function(session_factory, name, handler, runtime, role,
log_groups,
project, account_name, account_id,
sentry_dsn,
pattern="Traceback"):
"""Lambda function provisioning.
Self contained within the component, to allow for easier reuse.
"""
# Lazy import to avoid runtime dependency
from c7n.mu import (
LambdaFunction, PythonPackageArchive, CloudWatchLogSubscription)
config = dict(
name=name,
handler=handler,
runtime=runtime,
memory_size=512,
timeout=15,
role=role,
description='Custodian Sentry Relay',
events=[
CloudWatchLogSubscription(
session_factory, log_groups, pattern)])
archive = PythonPackageArchive('c7n_sentry')
archive.add_contents(
'config.json', json.dumps({
'project': project,
'account_name': account_name,
'account_id': account_id,
'sentry_dsn': sentry_dsn,
}))
archive.add_contents(
'handler.py',
'from c7n_sentry.c7nsentry import process_log_event'
)
archive.close()
return LambdaFunction(config, archive)
示例10: get_archive
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def get_archive(config):
archive = PythonPackageArchive(
'c7n_mailer', 'ldap3', 'pyasn1', 'jinja2', 'markupsafe', 'ruamel',
'redis', 'datadog', 'slackclient', 'requests')
template_dir = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', 'msg-templates'))
for t in os.listdir(template_dir):
with open(os.path.join(template_dir, t)) as fh:
archive.add_contents('msg-templates/%s' % t, fh.read())
archive.add_contents('config.json', json.dumps(config))
archive.add_contents('periodic.py', entry_source)
archive.close()
return archive
示例11: get_archive
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def get_archive(config):
required = ['ldap', 'jinja2', 'markupsafe']
remove = ['_yaml.so', 'c7n.egg-link']
def lib_filter(root, dirs, files):
for f in tuple(files):
if f.endswith('.pyo'):
files.remove(f)
for r in remove:
if r in files:
files.remove(r)
if os.path.basename(root) == 'site-packages':
for n in tuple(dirs):
if n not in required:
dirs.remove(n)
return dirs, files
archive = PythonPackageArchive(
os.path.dirname(__file__),
skip='*.pyc',
lib_filter=lib_filter)
archive.create()
template_dir = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', 'msg-templates'))
for t in os.listdir(template_dir):
with open(os.path.join(template_dir, t)) as fh:
archive.add_contents('msg-templates/%s' % t, fh.read())
archive.add_contents('config.json', json.dumps(config))
archive.add_contents('periodic.py', entry_source)
archive.close()
return archive
示例12: get_archive
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
def get_archive(config):
archive = PythonPackageArchive(
'c7n_mailer',
# core deps
'jinja2', 'markupsafe', 'ruamel', 'ldap3', 'pyasn1', 'redis',
# transport datadog - recursive deps
'datadog', 'simplejson', 'decorator',
# transport slack - recursive deps
'slackclient', 'websocket',
# requests (recursive deps), needed by datadog and slackclient
'requests', 'urllib3', 'idna', 'chardet', 'certifi')
template_dir = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', 'msg-templates'))
for t in os.listdir(template_dir):
with open(os.path.join(template_dir, t)) as fh:
archive.add_contents('msg-templates/%s' % t, fh.read())
archive.add_contents('config.json', json.dumps(config))
archive.add_contents('periodic.py', entry_source)
archive.close()
return archive
示例13: FunctionPackage
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
class FunctionPackage(object):
def __init__(self, name, function_path=None):
self.log = logging.getLogger('custodian.azure.function_package')
self.pkg = PythonPackageArchive()
self.name = name
self.function_path = function_path or os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'function.py')
self.enable_ssl_cert = not distutils.util.strtobool(
os.environ.get(ENV_CUSTODIAN_DISABLE_SSL_CERT_VERIFICATION, 'no'))
if not self.enable_ssl_cert:
self.log.warning('SSL Certificate Validation is disabled')
def _add_functions_required_files(self, policy, queue_name=None):
self.pkg.add_file(self.function_path,
dest=self.name + '/function.py')
self.pkg.add_contents(dest=self.name + '/__init__.py', contents='')
self._add_host_config()
if policy:
config_contents = self.get_function_config(policy, queue_name)
policy_contents = self._get_policy(policy)
self.pkg.add_contents(dest=self.name + '/function.json',
contents=config_contents)
self.pkg.add_contents(dest=self.name + '/config.json',
contents=policy_contents)
if policy['mode']['type'] == FUNCTION_EVENT_TRIGGER_MODE:
self._add_queue_binding_extensions()
def _add_host_config(self):
config = \
{
"version": "2.0",
"healthMonitor": {
"enabled": True,
"healthCheckInterval": "00:00:10",
"healthCheckWindow": "00:02:00",
"healthCheckThreshold": 6,
"counterThreshold": 0.80
},
"functionTimeout": "00:05:00",
"logging": {
"fileLoggingMode": "debugOnly"
},
"extensions": {
"http": {
"routePrefix": "api",
"maxConcurrentRequests": 5,
"maxOutstandingRequests": 30
}
}
}
self.pkg.add_contents(dest='host.json', contents=json.dumps(config))
def _add_queue_binding_extensions(self):
bindings_dir_path = os.path.abspath(
os.path.join(os.path.join(__file__, os.pardir), 'function_binding_resources'))
bin_path = os.path.join(bindings_dir_path, 'bin')
self.pkg.add_directory(bin_path)
self.pkg.add_file(os.path.join(bindings_dir_path, 'extensions.csproj'))
def get_function_config(self, policy, queue_name=None):
config = \
{
"scriptFile": "function.py",
"bindings": [{
"direction": "in"
}]
}
mode_type = policy['mode']['type']
binding = config['bindings'][0]
if mode_type == FUNCTION_TIME_TRIGGER_MODE:
binding['type'] = 'timerTrigger'
binding['name'] = 'input'
binding['schedule'] = policy['mode']['schedule']
elif mode_type == FUNCTION_EVENT_TRIGGER_MODE:
binding['type'] = 'queueTrigger'
binding['connection'] = 'AzureWebJobsStorage'
binding['name'] = 'input'
binding['queueName'] = queue_name
else:
self.log.error("Mode not yet supported for Azure functions (%s)"
% mode_type)
return json.dumps(config, indent=2)
def _get_policy(self, policy):
return json.dumps({'policies': [policy]}, indent=2)
def _add_cffi_module(self):
#.........这里部分代码省略.........
示例14: FunctionPackage
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
class FunctionPackage(object):
def __init__(self, name, function_path=None, target_subscription_ids=None):
self.log = logging.getLogger('custodian.azure.function_package')
self.pkg = PythonPackageArchive()
self.name = name
self.function_path = function_path or os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'function.py')
self.enable_ssl_cert = not distutils.util.strtobool(
os.environ.get(ENV_CUSTODIAN_DISABLE_SSL_CERT_VERIFICATION, 'no'))
if target_subscription_ids is not None:
self.target_subscription_ids = target_subscription_ids
else:
self.target_subscription_ids = [None]
if not self.enable_ssl_cert:
self.log.warning('SSL Certificate Validation is disabled')
def _add_functions_required_files(self, policy, queue_name=None):
s = local_session(Session)
for target_subscription_id in self.target_subscription_ids:
name = self.name + ("_" + target_subscription_id if target_subscription_id else "")
# generate and add auth
self.pkg.add_contents(dest=name + '/auth.json',
contents=s.get_functions_auth_string(target_subscription_id))
self.pkg.add_file(self.function_path,
dest=name + '/function.py')
self.pkg.add_contents(dest=name + '/__init__.py', contents='')
if policy:
config_contents = self.get_function_config(policy, queue_name)
policy_contents = self._get_policy(policy)
self.pkg.add_contents(dest=name + '/function.json',
contents=config_contents)
self.pkg.add_contents(dest=name + '/config.json',
contents=policy_contents)
if policy['mode']['type'] == FUNCTION_EVENT_TRIGGER_MODE:
self._add_queue_binding_extensions()
self._add_host_config()
def _add_host_config(self):
config = \
{
"version": "2.0",
"healthMonitor": {
"enabled": True,
"healthCheckInterval": "00:00:10",
"healthCheckWindow": "00:02:00",
"healthCheckThreshold": 6,
"counterThreshold": 0.80
},
"functionTimeout": "00:05:00",
"logging": {
"fileLoggingMode": "debugOnly"
},
"extensions": {
"http": {
"routePrefix": "api",
"maxConcurrentRequests": 5,
"maxOutstandingRequests": 30
}
}
}
self.pkg.add_contents(dest='host.json', contents=json.dumps(config))
def _add_queue_binding_extensions(self):
bindings_dir_path = os.path.abspath(
os.path.join(os.path.join(__file__, os.pardir), 'function_binding_resources'))
bin_path = os.path.join(bindings_dir_path, 'bin')
self.pkg.add_directory(bin_path)
self.pkg.add_file(os.path.join(bindings_dir_path, 'extensions.csproj'))
def get_function_config(self, policy, queue_name=None):
config = \
{
"scriptFile": "function.py",
"bindings": [{
"direction": "in"
}]
}
mode_type = policy['mode']['type']
binding = config['bindings'][0]
if mode_type == FUNCTION_TIME_TRIGGER_MODE:
binding['type'] = 'timerTrigger'
binding['name'] = 'input'
binding['schedule'] = policy['mode']['schedule']
elif mode_type == FUNCTION_EVENT_TRIGGER_MODE:
binding['type'] = 'queueTrigger'
binding['connection'] = 'AzureWebJobsStorage'
#.........这里部分代码省略.........
示例15: FunctionPackage
# 需要导入模块: from c7n.mu import PythonPackageArchive [as 别名]
# 或者: from c7n.mu.PythonPackageArchive import add_contents [as 别名]
class FunctionPackage(object):
def __init__(self, name, function_path=None):
self.log = logging.getLogger('custodian.azure.function_package')
self.pkg = PythonPackageArchive()
self.name = name
self.function_path = function_path or os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'function.py')
def _add_functions_required_files(self, policy):
self.pkg.add_file(self.function_path,
dest=self.name + '/function.py')
self.pkg.add_contents(dest=self.name + '/__init__.py', contents='')
self._add_host_config()
if policy:
config_contents = self.get_function_config(policy)
policy_contents = self._get_policy(policy)
self.pkg.add_contents(dest=self.name + '/function.json',
contents=config_contents)
self.pkg.add_contents(dest=self.name + '/config.json',
contents=policy_contents)
def _add_host_config(self):
config = \
{
"http": {
"routePrefix": "api",
"maxConcurrentRequests": 5,
"maxOutstandingRequests": 30
},
"logger": {
"defaultLevel": "Trace",
"categoryLevels": {
"Worker": "Trace"
}
},
"queues": {
"visibilityTimeout": "00:00:10"
},
"swagger": {
"enabled": True
},
"eventHub": {
"maxBatchSize": 1000,
"prefetchCount": 1000,
"batchCheckpointFrequency": 1
},
"healthMonitor": {
"enabled": True,
"healthCheckInterval": "00:00:10",
"healthCheckWindow": "00:02:00",
"healthCheckThreshold": 6,
"counterThreshold": 0.80
},
"functionTimeout": "00:05:00"
}
self.pkg.add_contents(dest='host.json', contents=json.dumps(config))
def get_function_config(self, policy):
config = \
{
"scriptFile": "function.py",
"bindings": [{
"direction": "in"
}]
}
mode_type = policy['mode']['type']
binding = config['bindings'][0]
if mode_type == 'azure-periodic':
binding['type'] = 'timerTrigger'
binding['name'] = 'input'
binding['schedule'] = policy['mode']['schedule']
elif mode_type == 'azure-stream':
binding['type'] = 'httpTrigger'
binding['authLevel'] = 'anonymous'
binding['name'] = 'input'
binding['methods'] = ['post']
config['bindings'].append({
"name": "$return",
"type": "http",
"direction": "out"})
else:
self.log.error("Mode not yet supported for Azure functions (%s)"
% mode_type)
return json.dumps(config, indent=2)
def _get_policy(self, policy):
return json.dumps({'policies': [policy]}, indent=2)
def _add_cffi_module(self):
"""CFFI native bits aren't discovered automatically
#.........这里部分代码省略.........