~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-01-21 11:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110121114806-v8fvnnl6az4m4ohv
Tags: upstream-2011.1~bzr597
ImportĀ upstreamĀ versionĀ 2011.1~bzr597

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# pylint: disable-msg=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
from euca2ools import Euca2ool, InstanceValidationError, Util, ConnectionFailed
 
39
 
 
40
usage_string = """
 
41
Retrieves a url to an ajax console terminal
 
42
 
 
43
euca-get-ajax-console [-h, --help] [--version] [--debug] instance_id
 
44
 
 
45
REQUIRED PARAMETERS
 
46
 
 
47
instance_id: unique identifier for the instance show the console output for.
 
48
 
 
49
OPTIONAL PARAMETERS
 
50
 
 
51
"""
 
52
 
 
53
 
 
54
# This class extends boto to add AjaxConsole functionality
 
55
class NovaEC2Connection(EC2Connection):
 
56
 
 
57
    def get_ajax_console(self, instance_id):
 
58
        """
 
59
        Retrieves a console connection for the specified instance.
 
60
 
 
61
        :type instance_id: string
 
62
        :param instance_id: The instance ID of a running instance on the cloud.
 
63
 
 
64
        :rtype: :class:`AjaxConsole`
 
65
        """
 
66
 
 
67
        class AjaxConsole:
 
68
            def __init__(self, parent=None):
 
69
                self.parent = parent
 
70
                self.instance_id = None
 
71
                self.url = None
 
72
 
 
73
            def startElement(self, name, attrs, connection):
 
74
                return None
 
75
 
 
76
            def endElement(self, name, value, connection):
 
77
                if name == 'instanceId':
 
78
                    self.instance_id = value
 
79
                elif name == 'url':
 
80
                    self.url = value
 
81
                else:
 
82
                    setattr(self, name, value)
 
83
 
 
84
        params = {}
 
85
        self.build_list_params(params, [instance_id], 'InstanceId')
 
86
        return self.get_object('GetAjaxConsole', params, AjaxConsole)
 
87
    pass
 
88
 
 
89
 
 
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)
 
94
 
 
95
# override boto's connect_ec2 method, so that we can use NovaEC2Connection
 
96
boto.connect_ec2 = override_connect_ec2
 
97
 
 
98
 
 
99
def usage(status=1):
 
100
    print usage_string
 
101
    Util().usage()
 
102
    sys.exit(status)
 
103
 
 
104
 
 
105
def version():
 
106
    print Util().version()
 
107
    sys.exit()
 
108
 
 
109
 
 
110
def display_console_output(console_output):
 
111
    print console_output.instance_id
 
112
    print console_output.timestamp
 
113
    print console_output.output
 
114
 
 
115
 
 
116
def display_ajax_console_output(console_output):
 
117
    print console_output.url
 
118
 
 
119
 
 
120
def main():
 
121
    try:
 
122
        euca = Euca2ool()
 
123
    except Exception, e:
 
124
        print e
 
125
        usage()
 
126
 
 
127
    instance_id = None
 
128
 
 
129
    for name, value in euca.opts:
 
130
        if name in ('-h', '--help'):
 
131
            usage(0)
 
132
        elif name == '--version':
 
133
            version()
 
134
        elif name == '--debug':
 
135
            debug = True
 
136
 
 
137
    for arg in euca.args:
 
138
        instance_id = arg
 
139
        break
 
140
 
 
141
    if instance_id:
 
142
        try:
 
143
            euca.validate_instance_id(instance_id)
 
144
        except InstanceValidationError:
 
145
            print 'Invalid instance id'
 
146
            sys.exit(1)
 
147
 
 
148
        try:
 
149
            euca_conn = euca.make_connection()
 
150
        except ConnectionFailed, e:
 
151
            print e.message
 
152
            sys.exit(1)
 
153
        try:
 
154
            console_output = euca_conn.get_ajax_console(instance_id)
 
155
        except Exception, ex:
 
156
            euca.display_error_and_exit('%s' % ex)
 
157
 
 
158
        display_ajax_console_output(console_output)
 
159
    else:
 
160
        print 'instance_id must be specified'
 
161
        usage()
 
162
 
 
163
if __name__ == "__main__":
 
164
    main()