|
@@ -13,6 +13,8 @@
|
|
|
# At the end of your shift:
|
|
|
# xdrtest stop
|
|
|
#
|
|
|
+# You can also start/stop individual instances by ID (provided the tag _also_ matches):
|
|
|
+# xdrtest start openvpn
|
|
|
import argparse
|
|
|
import boto3
|
|
|
import json
|
|
@@ -114,7 +116,8 @@ if __name__ == "__main__":
|
|
|
parser.add_argument('--config', help='AWS Config File', default='~/.aws/config')
|
|
|
parser.add_argument('--profile', help='Profile to Use', default=None)
|
|
|
parser.add_argument('--tagname', help='Tag Name to Filter On', default='Schedule')
|
|
|
- parser.add_argument('tagvalue', help='Tag Value to Filter On', nargs='?', default='MSOC')
|
|
|
+ parser.add_argument('--tagvalue', help='Tag Value to Filter On', default='MSOC')
|
|
|
+ parser.add_argument('instancename', help='Instance Name or ID to action on', nargs='?', default=None)
|
|
|
parser.add_argument('--commercial', help='Include Commercial Accounts', action='store_true')
|
|
|
parser.add_argument('--dry-run', help='Dry Run', action='store_true')
|
|
|
parser.add_argument('--debug', help='Output Debug Information', action='store_true')
|
|
@@ -138,7 +141,7 @@ if __name__ == "__main__":
|
|
|
ec2 = dict()
|
|
|
instances = dict()
|
|
|
instance_count = 0
|
|
|
- print(f'Instances to Start:')
|
|
|
+ print(f'Instances to { args.action }:')
|
|
|
for p in profiles:
|
|
|
logger.debug(f'Gathering from profile {p}...')
|
|
|
try:
|
|
@@ -155,9 +158,10 @@ if __name__ == "__main__":
|
|
|
|
|
|
instances[p] = list()
|
|
|
for instance in gather_instances(ec2[p], args.tagname, args.tagvalue, exclude_state):
|
|
|
- print(f'{p:<30}\t{ instance["instance_id"] }\t{instance["state"]:<10}\t{instance["name"]}')
|
|
|
- instances[p].append(instance['instance_id'])
|
|
|
- instance_count += 1
|
|
|
+ if args.instancename is None or args.instancename == instance['name'] or args.instancename == instance['instance_id']:
|
|
|
+ print(f'{p:<30}\t{ instance["instance_id"] }\t{instance["state"]:<10}\t{instance["name"]}')
|
|
|
+ instances[p].append(instance['instance_id'])
|
|
|
+ instance_count += 1
|
|
|
|
|
|
if instance_count == 0:
|
|
|
print('')
|
|
@@ -178,11 +182,13 @@ if __name__ == "__main__":
|
|
|
if len(instances[p]) > 0:
|
|
|
for i in ec2[p].instances.filter(InstanceIds=instances[p]):
|
|
|
if args.action == 'start':
|
|
|
+ print(f'Starting instances in profile { p }: { instances[p] }')
|
|
|
try:
|
|
|
i.start(DryRun=args.dry_run)
|
|
|
except Exception as e:
|
|
|
print(f'An error occured while starting instance {i.id}. Error: {e}. Skipping.')
|
|
|
elif args.action == 'stop':
|
|
|
+ print(f'Stopping instances in profile { p }: { instances[p] }')
|
|
|
try:
|
|
|
i.stop(DryRun=args.dry_run)
|
|
|
except Exception as e:
|