Skip to content

Commit

Permalink
Fix issue #53 - implement the no-action switch for termination
Browse files Browse the repository at this point in the history
  • Loading branch information
alexxa committed Aug 12, 2015
1 parent fb439b1 commit fca58ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
16 changes: 10 additions & 6 deletions dva/work/terminate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

ID_PARAMS = {
'cloud': None,
'enabled': True,
'enabled': False,
'hostname': None,
'cloudhwname': None,
'platform':None,
Expand All @@ -20,7 +20,7 @@
'ami':None
}

def main(conf, istream, ostream, cloud, verbose):
def main(conf, istream, ostream, cloud, no_action, verbose):

id_region_list = []
file_data = istream.readlines()
Expand All @@ -42,24 +42,26 @@ def main(conf, istream, ostream, cloud, verbose):
terminated_instances=0
not_terminated_instances=[]
not_found_instances=0
p = ID_PARAMS.copy()
p['enabled'] = not no_action
for id_region in unique_id_region_list:
instance_id_region = id_region[11:].split('_')
p = ID_PARAMS.copy()
p['id'] = instance_id_region[0]
p['region'] = instance_id_region[1]
p['cloud'] = cloud
record_cloud_config(p, conf)

try:
terminate_instance(p)
print 'Ami {0} in {1:>15} region was terminated.'.format(p['id'], p['region'])
print '{0} in {1:>15} region was terminated.'.format(p['id'], p['region'])
terminated_instances += 1
except KeyboardInterrupt:
sys.exit()
except EC2ResponseError:
print "The instance {0} in {1} region may not be terminated. Modify its 'disableApiTermination' instance attribute and try again.".format(p['id'], p['region'])
not_terminated_instances.append((p['id'], p['region']))
except PermanentCloudException:
print 'Ami {0} in {1:>15} region was NOT FOUND.'.format(p['id'], p['region'])
print '{0} in {1:>15} region was NOT FOUND.'.format(p['id'], p['region'])
not_found_instances += 1
except Exception as e:
print 'Unexpected error: {0}'.format(e)
Expand All @@ -72,6 +74,8 @@ def main(conf, istream, ostream, cloud, verbose):
print 'Not terminated: {:>23}'.format(len(not_terminated_instances))
if not_terminated_instances:
for instance in not_terminated_instances:
print 'Ami {0} in {1:>12} region was NOT TERMINATED.'.format(instance[0], instance[1])
print '{0} in {1:>12} region was NOT TERMINATED.'.format(instance[0], instance[1])
else:
print "\nAll found instances were terminated.\n"
if no_action:
print "NO ACTION was perfomed because of -n switch!\n"
5 changes: 3 additions & 2 deletions scripts/dva
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ def summary(loglevel, conf, istream,test_whitelist,compare,xunit):
@APP.cmd_arg('-i', '--istream', help='input filename', type=argparse.FileType('r'), default=sys.stdin)
@APP.cmd_arg('-o', '--ostream', help='output filename', type=argparse.FileType('w'), default=sys.stdout)
@APP.cmd_arg('--cloud', help='Default value is \'ec2\', and \'openstack\' is another option', default='ec2')
@APP.cmd_arg('-n', '--no-action', help='take no action', action='store_true')
@APP.cmd_arg('-V', '--verbose', help='print the progress', action='store_true')
def terminate(loglevel, conf, istream, ostream, cloud, verbose):
def terminate(loglevel, conf, istream, ostream, cloud, no_action, verbose):
logging.basicConfig(level=getattr(logging, loglevel))
from dva.work.terminate import main
return main(conf, istream, ostream, cloud, verbose)
return main(conf, istream, ostream, cloud, no_action, verbose)

@APP.cmd(help='jenkins upload tool')
@APP.cmd_arg('-i', '--istream', help='input filename', type=argparse.FileType('r'), default=sys.stdin)
Expand Down

0 comments on commit fca58ac

Please sign in to comment.