Guest User

Untitled

a guest
Jan 12th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import argparse
  4. import zipfile
  5. import boto3
  6. import tempfile
  7. import os
  8. import yaml
  9. import json
  10. import stat
  11. import logging
  12. import sys
  13.  
  14. BASE_DIR = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
  15.  
  16.  
  17. def fetch_file_path(dpath, root_dir):
  18. tf = []
  19. for root, dirs, files in os.walk(dpath):
  20. for fname in files:
  21. if not root.endswith('/__pycache__'):
  22. fpath = os.path.join(root, fname)
  23. tf.append((fpath, fpath[len(root_dir) + 1:]))
  24.  
  25. return tf
  26.  
  27.  
  28. def pack_zip_file(config):
  29. fd1, tpath = tempfile.mkstemp('.zip')
  30. os.close(fd1)
  31.  
  32. target_files = []
  33. pkg_dir = os.path.normpath(
  34. os.path.join(os.path.dirname(boto3.__path__[0]), '..')
  35. )
  36.  
  37. target_files = fetch_file_path(pkg_dir, pkg_dir) + \
  38. fetch_file_path(os.path.join(BASE_DIR, 'bin'), BASE_DIR) + \
  39. fetch_file_path(os.path.join(BASE_DIR, 'slam'), BASE_DIR)
  40.  
  41. with zipfile.ZipFile(tpath, 'w', zipfile.ZIP_DEFLATED) as z:
  42. for fpath, wpath in target_files:
  43. if wpath.startswith('boto3-') or wpath.startswith('botocore-'):
  44. continue
  45.  
  46. z.write(fpath, wpath)
  47.  
  48. fd2, jpath = tempfile.mkstemp('.json')
  49. os.write(fd2, json.dumps(config).encode('utf8'))
  50. os.close(fd2)
  51. os.chmod(jpath, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
  52. z.write(jpath, 'config.json')
  53.  
  54. return tpath
  55.  
  56.  
  57. def configure_lambda(client, raw_data, config):
  58. logger.info('configure {} -> {}'.format(config['script-name'],
  59. config['function-name']))
  60. code_res = client.update_function_code(
  61. FunctionName=config['function-name'],
  62. ZipFile=raw_data,
  63. )
  64. logger.debug(' Upload code: %s', json.dumps(code_res, indent=4))
  65.  
  66. conf_res = client.update_function_configuration(
  67. FunctionName=config['function-name'],
  68. Handler='bin.{}.lambda_handler'.format(config['script-name']),
  69. Timeout=config.get('timeout', 60),
  70. MemorySize=config.get('memory', 128),
  71. Runtime='python3.6'
  72. )
  73. logger.debug(' Update config: %s', json.dumps(conf_res, indent=4))
  74.  
  75.  
  76. def main():
  77. psr = argparse.ArgumentParser()
  78. psr.add_argument('-c', '--config',
  79. default=os.path.join(BASE_DIR, 'config.yml'), type=open)
  80. args = psr.parse_args()
  81.  
  82. config = yaml.load(args.config)
  83.  
  84. zip_fpath = pack_zip_file(config)
  85.  
  86. client = boto3.client('lambda')
  87.  
  88.  
  89. raw_data = open(zip_fpath, 'rb').read()
  90. for func in config['functions']:
  91. configure_lambda(client, raw_data, func)
  92.  
  93.  
  94.  
  95. if __name__ == '__main__':
  96. logger = logging.getLogger()
  97. logger.setLevel(logging.INFO)
  98. logger.addHandler(logging.StreamHandler(sys.stdout))
  99. main()
Add Comment
Please, Sign In to add comment