~ubuntu-core-dev/eucalyptus/euca2ools

75 by Neil
refactored directory structure some more.
1
#!/usr/bin/env python
2
3
# Software License Agreement (BSD License)
4
#
88 by Neil
changed copyright.
5
# Copyright (c) 2009, Eucalyptus Systems, Inc.
75 by Neil
refactored directory structure some more.
6
# All rights reserved.
7
#
8
# Redistribution and use of this software in source and binary forms, with or
9
# without modification, are permitted provided that the following conditions
10
# are met:
11
#
12
#   Redistributions of source code must retain the above
13
#   copyright notice, this list of conditions and the
14
#   following disclaimer.
15
#
16
#   Redistributions in binary form must reproduce the above
17
#   copyright notice, this list of conditions and the
18
#   following disclaimer in the documentation and/or other
19
#   materials provided with the distribution.
20
#
21
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
# POSSIBILITY OF SUCH DAMAGE.
32
#
88 by Neil
changed copyright.
33
# Author: Neil Soman neil@eucalyptus.com
75 by Neil
refactored directory structure some more.
34
35
import getopt, sys, os
266 by Dustin Kirkland
* Cherry pick fixes from upstream, up to bzr r265, fixes:
36
from euca2ools import Euca2ool, AddressValidationError, Util, ConnectionFailed
75 by Neil
refactored directory structure some more.
37
38
usage_string = """
153 by Neil
cleaning up help format to make it manpage friendly and consistent.
39
Disassociate a public IP address from an instance.
40
41
euca-disassociate-address [-h, --help] [--version] [--debug] ip
42
43
REQUIRED PARAMETERS
44
45
ip				IP address to disassociate.
46
47
OPTIONAL PARAMETERS
48
	
75 by Neil
refactored directory structure some more.
49
"""
50
51
268 by Dustin Kirkland
- LP: #531076 - fix euca-describe-instances against one instance
52
def usage(status=1):
75 by Neil
refactored directory structure some more.
53
    print usage_string
91 by Neil
bunch of refactoring and input validation.
54
    Util().usage()
268 by Dustin Kirkland
- LP: #531076 - fix euca-describe-instances against one instance
55
    sys.exit(status)
75 by Neil
refactored directory structure some more.
56
57
def version():
266 by Dustin Kirkland
* Cherry pick fixes from upstream, up to bzr r265, fixes:
58
    print Util().version()
75 by Neil
refactored directory structure some more.
59
    sys.exit()
60
61
def main():
62
    euca = None
63
    try:
95 by Neil
changing name.
64
	euca = Euca2ool()
150 by Neil
better reporting of input validation.
65
    except Exception, e:
66
	print e
75 by Neil
refactored directory structure some more.
67
        usage()
68
 
69
    ip = None
70
71
    for name, value in euca.opts:
72
        if name in ('-h', '--help'):
268 by Dustin Kirkland
- LP: #531076 - fix euca-describe-instances against one instance
73
            usage(0)
100 by Neil
number of fixes for command line processing
74
	elif name == '--version':
75 by Neil
refactored directory structure some more.
75
	    version()
76
77
    for arg in euca.args:
78
        ip = arg
79
	break
80
81
82
    if ip:
91 by Neil
bunch of refactoring and input validation.
83
        try:
84
    	    euca.validate_address(ip)
111 by Neil
converted to older exception types
85
        except AddressValidationError:
86
            print 'Invalid address', ip 
91 by Neil
bunch of refactoring and input validation.
87
            sys.exit(1)
88
266 by Dustin Kirkland
* Cherry pick fixes from upstream, up to bzr r265, fixes:
89
        try:
90
            euca_conn = euca.make_connection()
91
        except ConnectionFailed, e:
92
            print e.message
93
            sys.exit(1)
94
95
        try:
190 by Neil
nicer error reporting. fixes #401172
96
            return_code = euca_conn.disassociate_address(public_ip = ip)
97
        except Exception, ex:
98
            euca.display_error_and_exit('%s' % ex)
99
91 by Neil
bunch of refactoring and input validation.
100
        if return_code:
138 by Neil
fixed disassociate address.
101
            print 'ADDRESS\t%s' % ip
75 by Neil
refactored directory structure some more.
102
    else:
103
	print 'ip address must be specified'
104
	usage()
105
106
if __name__ == "__main__":
107
    main()
108