2
# pylint: disable-msg=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
38
from euca2ools import Euca2ool, InstanceValidationError, Util, ConnectionFailed
41
Retrieves a url to an ajax console terminal
43
euca-get-ajax-console [-h, --help] [--version] [--debug] instance_id
47
instance_id: unique identifier for the instance show the console output for.
54
# This class extends boto to add AjaxConsole functionality
55
class NovaEC2Connection(EC2Connection):
57
def get_ajax_console(self, instance_id):
59
Retrieves a console connection for the specified instance.
61
:type instance_id: string
62
:param instance_id: The instance ID of a running instance on the cloud.
64
:rtype: :class:`AjaxConsole`
68
def __init__(self, parent=None):
70
self.instance_id = None
73
def startElement(self, name, attrs, connection):
76
def endElement(self, name, value, connection):
77
if name == 'instanceId':
78
self.instance_id = value
82
setattr(self, name, value)
85
self.build_list_params(params, [instance_id], 'InstanceId')
86
return self.get_object('GetAjaxConsole', params, AjaxConsole)
90
def override_connect_ec2(aws_access_key_id=None,
91
aws_secret_access_key=None, **kwargs):
92
return NovaEC2Connection(aws_access_key_id,
93
aws_secret_access_key, **kwargs)
95
# override boto's connect_ec2 method, so that we can use NovaEC2Connection
96
boto.connect_ec2 = override_connect_ec2
106
print Util().version()
110
def display_console_output(console_output):
111
print console_output.instance_id
112
print console_output.timestamp
113
print console_output.output
116
def display_ajax_console_output(console_output):
117
print console_output.url
129
for name, value in euca.opts:
130
if name in ('-h', '--help'):
132
elif name == '--version':
134
elif name == '--debug':
137
for arg in euca.args:
143
euca.validate_instance_id(instance_id)
144
except InstanceValidationError:
145
print 'Invalid instance id'
149
euca_conn = euca.make_connection()
150
except ConnectionFailed, e:
154
console_output = euca_conn.get_ajax_console(instance_id)
155
except Exception, ex:
156
euca.display_error_and_exit('%s' % ex)
158
display_ajax_console_output(console_output)
160
print 'instance_id must be specified'
163
if __name__ == "__main__":