Created
          March 3, 2017 23:47 
        
      - 
      
 - 
        
Save billyshambrook/a195fca51cc25265d1c965ac438f5d30 to your computer and use it in GitHub Desktop.  
    Catch essential container result from sidecar container in ECS
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import logging | |
| import signal | |
| import socket | |
| import sys | |
| import docker | |
| import boto3 | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| hostname = socket.gethostname() | |
| ecs = boto3.client('ecs') | |
| d = docker.from_env() | |
| container = d.containers.get(hostname) | |
| labels = container.attrs['Config']['Labels'] | |
| def handle_signal(signum, frame): | |
| """ """ | |
| logger.info('caught signal') | |
| task = ecs.describe_tasks( | |
| cluster=labels['com.amazonaws.ecs.cluster'], | |
| tasks=[labels['com.amazonaws.ecs.task-arn']])['tasks'][0] | |
| other_container = next(c for c in task['containers'] if c['name'] == 'job') | |
| if not other_container: | |
| logger.info('no other container found') | |
| logger.info(task) | |
| logger.info(other_container) | |
| logger.info(other_container.get('exitCode', 1000)) | |
| if other_container.get('exitCode', 1000) > 0: | |
| logger.error('other container failed') | |
| else: | |
| logger.info('other container passed') | |
| sys.exit(0) | |
| signal.signal(signal.SIGTERM, handle_signal) | |
| while True: | |
| pass | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment