~0x44/nova/bug838466

« back to all changes in this revision

Viewing changes to tools/euca-get-ajax-console

  • Committer: Brian Waldon
  • Date: 2011-07-29 16:54:55 UTC
  • mto: This revision was merged to the branch mainline in revision 1364.
  • Revision ID: brian.waldon@rackspace.com-20110729165455-4ebqwv8s5pkscmmg
one last change

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# pylint: disable=C0103
 
3
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
4
 
 
5
# Copyright 2010 United States Government as represented by the
 
6
# Administrator of the National Aeronautics and Space Administration.
 
7
# All Rights Reserved.
 
8
#
 
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
 
12
#
 
13
#        http://www.apache.org/licenses/LICENSE-2.0
 
14
#
 
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.
 
20
 
 
21
"""Euca add-on to use ajax console"""
 
22
 
 
23
import getopt
 
24
import os
 
25
import sys
 
26
 
 
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]),
 
30
                                   os.pardir,
 
31
                                   os.pardir))
 
32
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
 
33
    sys.path.insert(0, possible_topdir)
 
34
 
 
35
import boto
 
36
import nova
 
37
from boto.ec2.connection import EC2Connection
 
38
import euca2ools
 
39
from euca2ools import Euca2ool, InstanceValidationError, Util
 
40
 
 
41
usage_string = """
 
42
Retrieves a url to an ajax console terminal
 
43
 
 
44
euca-get-ajax-console [-h, --help] [--version] [--debug] instance_id
 
45
 
 
46
REQUIRED PARAMETERS
 
47
 
 
48
instance_id: unique identifier for the instance show the console output for.
 
49
 
 
50
OPTIONAL PARAMETERS
 
51
 
 
52
"""
 
53
 
 
54
 
 
55
# This class extends boto to add AjaxConsole functionality
 
56
class NovaEC2Connection(EC2Connection):
 
57
 
 
58
    def get_ajax_console(self, instance_id):
 
59
        """
 
60
        Retrieves a console connection for the specified instance.
 
61
 
 
62
        :type instance_id: string
 
63
        :param instance_id: The instance ID of a running instance on the cloud.
 
64
 
 
65
        :rtype: :class:`AjaxConsole`
 
66
        """
 
67
 
 
68
        class AjaxConsole:
 
69
            def __init__(self, parent=None):
 
70
                self.parent = parent
 
71
                self.instance_id = None
 
72
                self.url = None
 
73
 
 
74
            def startElement(self, name, attrs, connection):
 
75
                return None
 
76
 
 
77
            def endElement(self, name, value, connection):
 
78
                if name == 'instanceId':
 
79
                    self.instance_id = value
 
80
                elif name == 'url':
 
81
                    self.url = value
 
82
                else:
 
83
                    setattr(self, name, value)
 
84
 
 
85
        params = {}
 
86
        self.build_list_params(params, [instance_id], 'InstanceId')
 
87
        return self.get_object('GetAjaxConsole', params, AjaxConsole)
 
88
    pass
 
89
 
 
90
 
 
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)
 
95
 
 
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
 
99
 
 
100
# Override Euca2ools' EC2Connection class (which it gets from boto)
 
101
# (This is for Euca2ools 1.3)
 
102
euca2ools.EC2Connection = NovaEC2Connection
 
103
 
 
104
 
 
105
def usage(status=1):
 
106
    print usage_string
 
107
    Util().usage()
 
108
    sys.exit(status)
 
109
 
 
110
 
 
111
def version():
 
112
    print Util().version()
 
113
    sys.exit()
 
114
 
 
115
 
 
116
def display_console_output(console_output):
 
117
    print console_output.instance_id
 
118
    print console_output.timestamp
 
119
    print console_output.output
 
120
 
 
121
 
 
122
def display_ajax_console_output(console_output):
 
123
    print console_output.url
 
124
 
 
125
 
 
126
def main():
 
127
    try:
 
128
        euca = Euca2ool()
 
129
    except Exception, e:
 
130
        print e
 
131
        usage()
 
132
 
 
133
    instance_id = None
 
134
 
 
135
    for name, value in euca.opts:
 
136
        if name in ('-h', '--help'):
 
137
            usage(0)
 
138
        elif name == '--version':
 
139
            version()
 
140
        elif name == '--debug':
 
141
            debug = True
 
142
 
 
143
    for arg in euca.args:
 
144
        instance_id = arg
 
145
        break
 
146
 
 
147
    if instance_id:
 
148
        try:
 
149
            euca.validate_instance_id(instance_id)
 
150
        except InstanceValidationError:
 
151
            print 'Invalid instance id'
 
152
            sys.exit(1)
 
153
 
 
154
        try:
 
155
            euca_conn = euca.make_connection()
 
156
        except Exception, e:
 
157
            print e.message
 
158
            sys.exit(1)
 
159
        try:
 
160
            console_output = euca_conn.get_ajax_console(instance_id)
 
161
        except Exception, ex:
 
162
            euca.display_error_and_exit('%s' % ex)
 
163
 
 
164
        display_ajax_console_output(console_output)
 
165
    else:
 
166
        print 'instance_id must be specified'
 
167
        usage()
 
168
 
 
169
if __name__ == "__main__":
 
170
    main()