1#!/usr/bin/env python
2##########################################################################
3# run/ec2-setup/terminate.py
4#
5# Part of Project Thrill - http://project-thrill.org
6#
7# Copyright (C) 2015 Matthias Stumpp <mstumpp@gmail.com>
8#
9# All rights reserved. Published under the BSD-2 license in the LICENSE file.
10##########################################################################
11
12import boto3
13import json
14import sys
15
16from subprocess import call
17
18with open('config.json') as data_file:
19    data = json.load(data_file)
20
21ec2 = boto3.resource('ec2')
22
23if len(sys.argv) != 2:
24    print "Usage: terminate.py 123"
25    sys.exit();
26
27job_id = str(sys.argv[1])
28
29instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']},
30                                          {'Name': 'key-name', 'Values': [data["EC2_KEY_HANDLE"]]},
31                                          {'Name': 'tag:JobId', 'Values':[job_id]}])
32
33ids = [instance.id for instance in instances]
34print("Terminating:", ids)
35
36ec2.instances.filter(InstanceIds=ids).terminate()
37
38##########################################################################
39