85
80
'recent one with an approved owner.'))
83
def _convert_instance_type(arg):
84
"""Ensure that `arg` is acceptable as an instance type."""
85
if arg not in AVAILABLE_INSTANCE_TYPES:
86
raise BzrCommandError('Unknown instance type %r' % arg)
88
90
instance_type_option = Option(
89
'instance', short_name='i',
91
'instance', short_name='i', type=_convert_instance_type,
91
92
param_name='instance_type',
92
93
help=('The AWS instance type on which to base this run. '
93
94
'Available options are %r. Defaults to `%s`.' %
131
region_option = Option(
134
help=("Name of the AWS region in which to run the instance. "
135
"Must be the same as the region holding the image file. "
136
"For example, 'us-west-1'."))
139
132
def filename_type(filename):
140
133
"""An option validator for filenames.
293
285
noemail=False, submit_pqm_message=None, pqm_public_location=None,
294
286
pqm_submit_location=None, pqm_email=None, postmortem=False,
295
287
attached=False, debug=False, open_browser=False,
297
288
include_download_cache_changes=False):
298
289
set_trace_if(debug)
299
290
if branch is None:
324
315
session_name = EC2SessionName.make(EC2TestRunner.name)
325
instance = EC2Instance.make(session_name, instance_type, machine,
316
instance = EC2Instance.make(session_name, instance_type, machine)
328
318
runner = EC2TestRunner(
329
319
test_branch, email=email, file=file,
397
384
instance_type=DEFAULT_INSTANCE_TYPE, postmortem=False,
398
385
debug=False, commit_text=None, dry_run=False, testfix=False,
399
386
no_qa=False, incremental=False, rollback=None, print_commit=False,
400
force=False, attached=False,
401
region=DEFAULT_REGION,
387
force=False, attached=False):
404
389
from devscripts.autoland import (
405
390
LaunchpadBranchLander, MissingReviewError, MissingBugsError,
489
474
session_name = EC2SessionName.make(EC2TestRunner.name)
490
475
instance = EC2Instance.make(
491
session_name, instance_type, machine, region=region)
476
session_name, instance_type, machine)
493
478
runner = EC2TestRunner(
494
479
mp.source_branch, email=emails,
603
586
def run(self, ami_name, machine=None, instance_type='m1.large',
604
587
debug=False, postmortem=False, extra_update_image_command=None,
607
589
set_trace_if(debug)
616
598
for variable in ['LANG', 'LC_ALL', 'LC_TIME']:
617
599
os.environ.pop(variable, None)
601
credentials = EC2Credentials.load_from_file()
619
603
session_name = EC2SessionName.make(EC2TestRunner.name)
620
604
instance = EC2Instance.make(
621
605
session_name, instance_type, machine,
623
instance.check_bundling_prerequisites(ami_name)
606
credentials=credentials)
607
instance.check_bundling_prerequisites(
608
ami_name, credentials)
624
609
instance.set_up_and_run(
625
610
postmortem, True, self.update_image, instance,
626
extra_update_image_command, ami_name, instance._credentials,
611
extra_update_image_command, ami_name, credentials, public)
629
613
def update_image(self, instance, extra_update_image_command, ami_name,
630
614
credentials, public):
684
668
The first in the list is the default image.
691
def run(self, region=None):
672
credentials = EC2Credentials.load_from_file()
692
673
session_name = EC2SessionName.make(EC2TestRunner.name)
693
credentials = EC2Credentials.load_from_file(region_name=region)
694
674
account = credentials.connect(session_name)
695
format = "%5s %-12s %-12s %-12s %s\n"
697
format % ("Rev", "AMI", "Owner ID", "Owner", "Description"))
675
format = "%5s %-12s %-12s %s\n"
676
self.outf.write(format % ("Rev", "AMI", "Owner ID", "Owner"))
698
677
for revision, images in account.find_images():
699
678
for image in images:
700
679
self.outf.write(format % (
701
revision, image.id, image.ownerId,
702
VALID_AMI_OWNERS.get(image.ownerId, "unknown"),
703
image.description or ''))
706
class cmd_kill(EC2Command):
707
"""Kill one or more running EC2 instances.
709
You can get the instance id from 'ec2 list'.
715
takes_args = ['instance_id*']
717
def run(self, instance_id_list, region=None):
718
credentials = EC2Credentials.load_from_file(region_name=region)
719
account = credentials.connect('ec2 kill')
720
self.outf.write("killing %d instances: " % len(instance_id_list,))
721
account.conn.terminate_instances(instance_id_list)
722
self.outf.write("done\n")
680
revision, image.id, image.ownerId,
681
VALID_AMI_OWNERS.get(image.ownerId, "unknown")))
725
684
class cmd_list(EC2Command):
753
711
"""How long has 'instance' been running?"""
754
712
expected_format = '%Y-%m-%dT%H:%M:%S.000Z'
755
713
launch_time = datetime.strptime(instance.launch_time, expected_format)
757
715
datetime.utcnow().replace(tzinfo=UTC)
758
716
- launch_time.replace(tzinfo=UTC))
759
return timedelta(delta.days, delta.seconds) # Round it.
761
718
def get_http_url(self, instance):
762
719
hostname = instance.public_dns_name
783
740
:param data: Launchpad-specific data.
784
741
:param verbose: Whether we want verbose output.
786
description = instance.id
787
743
uptime = self.get_uptime(instance)
788
if instance.state != 'running':
789
current_status = instance.state
745
description = instance.id
746
current_status = 'unknown '
792
current_status = 'unknown '
748
description = data['description']
749
if data['failed-yet']:
750
current_status = '[FAILED]'
794
description = data['description']
795
if data['failed-yet']:
796
current_status = '[FAILED]'
798
current_status = '[OK] '
800
'%-40s %-10s (up for %s) %10s'
801
% (description, current_status, uptime, instance.id))
752
current_status = '[OK] '
753
output = '%s %s (up for %s)' % (description, current_status, uptime)
803
755
url = self.get_http_url(instance)
805
757
url = "No web service"
806
758
output += '\n %s' % (url,)
807
if instance.state_reason:
809
'\n transition reason: %s'
810
% instance.state_reason.get('message', ''))
813
761
def format_summary(self, by_state):
815
763
': '.join((state, str(num)))
816
764
for (state, num) in sorted(list(by_state.items())))
818
def run(self, show_urls=False, all=False, region=None):
766
def run(self, show_urls=False, all=False):
767
credentials = EC2Credentials.load_from_file()
819
768
session_name = EC2SessionName.make(EC2TestRunner.name)
820
credentials = EC2Credentials.load_from_file(region_name=region)
821
769
account = credentials.connect(session_name)
822
770
instances = list(self.iter_instances(account))
823
771
if len(instances) == 0:
830
778
data = self.get_ec2test_info(instance)
831
779
if data is None and not all:
833
print self.format_instance(
834
instance, data, verbose=(show_urls or is_verbose()))
781
print self.format_instance(instance, data, show_urls)
835
782
print 'Summary: %s' % (self.format_summary(by_state),)