2
# pylint: disable=C0103
3
# vim: tabstop=4 shiftwidth=4 softtabstop=4
5
# Copyright 2010 United States Government as represented by the
6
# Administrator of the National Aeronautics and Space Administration.
9
# Licensed under the Apache License, Version 2.0 (the "License");
10
# you may not use this file except in compliance with the License.
11
# You may obtain a copy of the License at
13
# http://www.apache.org/licenses/LICENSE-2.0
15
# Unless required by applicable law or agreed to in writing, software
16
# distributed under the License is distributed on an "AS IS" BASIS,
17
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
# See the License for the specific language governing permissions and
19
# limitations under the License.
21
"""Euca add-on to use ajax console"""
27
# If ../nova/__init__.py exists, add ../ to Python search path, so that
28
# it will override what happens to be installed in /usr/(local/)lib/python...
29
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
32
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
33
sys.path.insert(0, possible_topdir)
37
from boto.ec2.connection import EC2Connection
39
from euca2ools import Euca2ool, InstanceValidationError, Util
42
Retrieves a url to an ajax console terminal
44
euca-get-ajax-console [-h, --help] [--version] [--debug] instance_id
48
instance_id: unique identifier for the instance show the console output for.
55
# This class extends boto to add AjaxConsole functionality
56
class NovaEC2Connection(EC2Connection):
58
def get_ajax_console(self, instance_id):
60
Retrieves a console connection for the specified instance.
62
:type instance_id: string
63
:param instance_id: The instance ID of a running instance on the cloud.
65
:rtype: :class:`AjaxConsole`
69
def __init__(self, parent=None):
71
self.instance_id = None
74
def startElement(self, name, attrs, connection):
77
def endElement(self, name, value, connection):
78
if name == 'instanceId':
79
self.instance_id = value
83
setattr(self, name, value)
86
self.build_list_params(params, [instance_id], 'InstanceId')
87
return self.get_object('GetAjaxConsole', params, AjaxConsole)
91
def override_connect_ec2(aws_access_key_id=None,
92
aws_secret_access_key=None, **kwargs):
93
return NovaEC2Connection(aws_access_key_id,
94
aws_secret_access_key, **kwargs)
96
# override boto's connect_ec2 method, so that we can use NovaEC2Connection
97
# (This is for Euca2ools 1.2)
98
boto.connect_ec2 = override_connect_ec2
100
# Override Euca2ools' EC2Connection class (which it gets from boto)
101
# (This is for Euca2ools 1.3)
102
euca2ools.EC2Connection = NovaEC2Connection
112
print Util().version()
116
def display_console_output(console_output):
117
print console_output.instance_id
118
print console_output.timestamp
119
print console_output.output
122
def display_ajax_console_output(console_output):
123
print console_output.url
135
for name, value in euca.opts:
136
if name in ('-h', '--help'):
138
elif name == '--version':
140
elif name == '--debug':
143
for arg in euca.args:
149
euca.validate_instance_id(instance_id)
150
except InstanceValidationError:
151
print 'Invalid instance id'
155
euca_conn = euca.make_connection()
160
console_output = euca_conn.get_ajax_console(instance_id)
161
except Exception, ex:
162
euca.display_error_and_exit('%s' % ex)
164
display_ajax_console_output(console_output)
166
print 'instance_id must be specified'
169
if __name__ == "__main__":