7.09.2013

AWS: More Listings of EC2 Information

AWS: EC2 情報の簡易一覧表示

こちらの改良版。
mog project: AWS: Brief Listing of EC2 Instances 

インスタンスの他に、ボリューム、スナップショットの一覧も表示。

aws_list.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
import subprocess
try:
    import json
except ImportError:
    print('You need python 2.6 or later to run this script.')
    sys.exit(1)
 
 
def list_instances():
    print('Instances:')
    command_args = ['describe-instances']
    headers = [
        ('Name', 16),
        ('Instance', 16),
        ('Type', 10),
        ('Launch Time', 26),
        ('State', 12),
    ]
 
    def generator(output):
        for i in output['Reservations'][0]['Instances']:
            yield [
                get_name(i),
                i['InstanceId'],
                i['InstanceType'],
                i['LaunchTime'],
                i['State']['Name'],
            ]
    print_list(command_args, headers, generator)
 
 
def list_volumes():
    print('Volumes:')
    command_args = ['describe-volumes']
    headers = [
        ('Name', 16),
        ('Volume ID', 16),
        ('Cap', 8),
        ('Type', 11),
        ('Zone', 17),
        ('State', 12),
    ]
 
    def generator(output):
        for i in output['Volumes']:
            yield [
                get_name(i),
                i['VolumeId'],
                '%d GB' % i['Size'],
                i['VolumeType'],
                i['AvailabilityZone'],
                i['State'],
            ]
    print_list(command_args, headers, generator)
 
 
def list_snapshots():
    print('Snapshots:')
    command_args = ['describe-snapshots', '--owner-ids', 'self']
    headers = [
        ('Name', 16),
        ('Snapshot ID', 16),
        ('Cap', 8),
        ('Start Time', 28),
        ('State', 12),
    ]
 
    def generator(output):
        for i in output['Snapshots']:
            yield [
                get_name(i),
                i['SnapshotId'],
                '%d GB' % i['VolumeSize'],
                i['StartTime'],
                i['State'],
            ]
    print_list(command_args, headers, generator)
 
 
def get_name(obj):
    return ''.join(
        [x['Value'] for x in obj.get('Tags', []) if x['Key'] == 'Name'])
 
 
def print_list(command_args, headers, generator):
    # Print header.
    print(''.join([x[0].ljust(x[1]) for x in headers]))
    print('-' * sum([x[1] for x in headers]))
 
    # Run command.
    stdout = subprocess.check_output(['aws', 'ec2'] + command_args)
    output = json.loads(stdout)
 
    # Print result.
    for result in generator(output):
        print(''.join(
            [result[i][:x[1] - 1].ljust(x[1]) for i, x in enumerate(headers)]))
 
    # Print footer.
    print('')
 
 
if __name__ == '__main__':
    list_instances()
    list_volumes()
    list_snapshots()

0 件のコメント:

コメントを投稿