summaryrefslogtreecommitdiff
path: root/ec2-get-info.py
blob: 7b73b365090cef323f63ef2c23bcfbe15a31ea98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
#
#    Fetch the information about an AMI instance.
#    Copyright (C) 2008-2009 Canonical Ltd.
#
#    Author: Chuck Short <chuck.short@canonical.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3, as
#    published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from  optparse import OptionParser
import urllib

ec2Data = ['ami-id',
           'ami-launch-index',
           'ami-manifest-path',
           'ancestor-ami-id',
           'block-device-mapping',
           'instance-id',
           'instance-type',
           'local-hostname',
           'local-ipv4',
           'kernel-id',
           'product-codes',
           'public-hostname',
           'public-ipv4',
           'ramdisk-id',
           'reservation-id',
           'security-groups']

def ec2Version():
    print file("/etc/ec2_version").read()

def getData(ec2data):
    api_ver = '2008-02-01'
    base_url = 'http://169.254.169.254/%s/meta-data' % api_ver
    print "%s: %s" %(ec2data,urllib.urlopen('%s/%s' %(base_url,ec2data)).read())

def getAllData(ec2Data):
    for x in ec2Data:
        getData(x)

def main():
    usage = "usage: %prog [options]"

    parser = OptionParser(prog='ec2-get-info', usage=usage)
    parser.add_option('--ami-id', dest='amiid', action='store_true', help='Display the ami-id.')
    parser.add_option('--launch-index', dest='launch', action='store_true', help='Display the AMI launch index.')
    parser.add_option('--manifest', dest='manifest', action='store_true', help='Display the AMI manifest path.')
    parser.add_option('--ancestor-id', dest='ancestor', action='store_true', help='Display the AMI ancestor id.')
    parser.add_option('--block-device', dest='block', action='store_true', help='Display the block device id.')
    parser.add_option('--instance-id', dest='id', action='store_true', help='Display the instance id.')
    parser.add_option('--instance-type', dest='type', action='store_true', help='Display the instance type.')
        parser.add_option('--local-hostname', dest='lhostname', action='store_true', help='Display the local hostname.')
    parser.add_option('--local-ipv4', dest='lipv4', action='store_true', help='Display the local ipv4 IP address.')
    parser.add_option('--kernel-id', dest='aki', action='store_true', help='List the AKI.')
    parser.add_option('--product-codes', dest='code', action='store_true', help='List the product codes associated with thsi AMI.')
    parser.add_option('--public-hostname', dest='phostname', action='store_true', help='Show the public hostname.')
    parser.add_option('--public_ipv4', dest='pipv4', action='store_true', help='Show the public IPV4 IP address.')
    parser.add_option('--ramdisk-id', dest='ari', action='store_true', help='Display the ARI.')
    parser.add_option('--reservation-id', dest='rid', action='store_true', help='Display the reservation id.')
    parser.add_option('--security-groups', dest='security', action='store_true', help='Display the security groups.')
    parser.add_option('--ec2-version', dest='ec2', action='store_true', help='Display the current Ubuntu EC2 version')
    parser.add_option('--all',dest='all', action='store_true', help='Display all informantion.')

    options, args = parser.parse_args()

    if options.amiid:
        getData(ec2Data[0])
    if options.launch:
        getData(ec2Data[1])
    if options.manifest:
        getData(ec2Data[2])
    if options.ancestor:
        getData(ec2Data[3])
    if options.block:
        getData(ec2Data[4])
    if options.id:
        getData(ec2Data[5])
    if options.type:
        getData(ec2Data[6])
    if options.lhostname:
        getData(ec2Data[7])
    if options.lipv4:
        getData(ec2Data[8])
    if options.aki:
        getData(ec2Data[9])
    if options.code:
        getData(ec2Data[10])
    if options.phostname:
        getData(ec2Data[11])
    if options.pipv4:
        getData(ec2Data[12])
    if options.ari:
        getData(ec2Data[13])
    if options.rid:
        getData(ec2Data[14])
    if options.security:
        getData(ec2Data[15])
    if options.ec2:
        ec2Version()
    if options.all:
        getAllData(ec2Data)

if __name__ == "__main__":
   main()