Skip to content

Instantly share code, notes, and snippets.

@kapilt
Created July 25, 2018 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kapilt/1cdc40bd15eb10a85105ebc55bde1cdb to your computer and use it in GitHub Desktop.
Save kapilt/1cdc40bd15eb10a85105ebc55bde1cdb to your computer and use it in GitHub Desktop.
# Copyright 2018 Capital One Services, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
from c7n.config import Config
from c7n.policy import load as policy_load
from c7n import mu, resources
def load_policies(options):
policies = []
for f in options.config_files:
collection = policy_load(options, f)
policies.extend(collection.filter(options.policy_filter))
return policies
def setup_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
'-c', '--config', dest="config_file", required=True,
help="Policy configuration files")
parser.add_argument("-p", "--policies", default=None, dest='policy_filter',
help="Only use named/matched policies")
parser.add_argument("-o", "--output-dir", default=None, required=True)
return parser
def main():
parser = setup_parser()
options = parser.parse_args()
config = Config.empty()
resources.load_resources()
collection = policy_load(
config, options.config_file).filter(options.policy_filter)
for p in collection:
exec_mode_type = p.data.get('mode', {'type': 'pull'}).get('type')
if exec_mode_type == 'pull':
continue
archive = mu.PolicyLambda(p).get_archive()
with open(os.path.join(options.output_dir, "%s.zip" % p.name), 'wb') as fh:
fh.write(archive.get_bytes())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment