-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.pl
129 lines (90 loc) · 2.87 KB
/
aws.pl
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
117
118
119
120
121
122
123
124
125
126
:- use_module(library(sgml)).
:- use_module(library(apply)).
shutdown_all :-
findall(I, instance(I, instanceId, _), Is),
maplist(ec2_terminate_inst, Is).
% --------------------------------------------------------------------
% Build knowledge base
load_aws :-
load_regions(_),
load_instance(_),
load_snapshot(_),
load_volume(_).
% assert region information
load_regions(Rs) :-
findall(R, (aws_region(R), assertz(region(R))), Rs).
% assert instance information
load_instance(Ids) :-
findall(Id, (ec2_instance(I),
xpath(I, //('instanceId'(text)), Id),
I = element(_, _, Cs),
load_instance_attr(Id, Cs)),
Ids).
load_instance_attr(_, []).
% load_instance_attr(Id, [element(tagSet, _, Items) | Cs]) :-
% load_tags(Id, Items).
load_instance_attr(Id, [C | Cs]) :-
C = element(A, _, V),
assertz(instance(Id, A, V)),
load_instance_attr(Id, Cs).
% assert snapshot info
load_snapshot(Ids) :-
findall(Id, (ec2_snapshot(I),
xpath(I, //('snapshotId'(text)), Id),
I = element(_, _, Cs),
load_snapshot_attr(Id, Cs)),
Ids).
load_snapshot_attr(_, []).
load_snapshot_attr(Id, [C | Cs]) :-
C = element(A, _, V),
assertz(snapshot(Id, A, V)),
load_snapshot_attr(Id, Cs).
% assert volume info
load_volume(Ids) :-
findall(Id, (ec2_volume(I),
xpath(I, //('volumeId'(text)), Id),
I = element(_, _, Cs),
load_volume_attr(Id, Cs)),
Ids).
load_volume_attr(_, []).
load_volume_attr(Id, [C | Cs]) :-
C = element(A, _, V),
assertz(volume(Id, A, V)),
load_volume_attr(Id, Cs).
% Transform an element list of attributes into a list of [Attribute, Value]
xml_to_list([], _).
xml_to_list([X | Xml], [[A,V] | Ls] ) :-
X = element(A, _, V),
xml_to_list(Xml, Ls).
% --------------------------------------------------------------------
% EC2 related calls
ec2_terminate_inst(Id) :-
aws_fake_run(['terminate-instances', Id], _).
% Return an XML chunk for each instance
ec2_instance(Instance) :-
aws_run(['describe-instances'], Xml),
xpath(Xml, //('instancesSet')/('item'), Instance).
% Return XML for each snapshot
ec2_snapshot(Snapshot) :-
aws_run(['describe-snapshots'], Xml),
xpath(Xml, //('snapshotSet')/('item'), Snapshot).
% Return XML for each volume
ec2_volume(Volume) :-
aws_run(['describe-volumes'], Xml),
xpath(Xml, //('volumeSet')/('item'), Volume).
% Return each region
aws_region(Region) :-
aws_run(['describe-regions'], Xml),
xpath(Xml, //('regionName'(text)), Region).
% --------------------------------------------------------------------
% Low level calls to processes
% Calls aws perl script available at timkay.com/aws
aws_run(Options, Xml) :-
process_create(path(aws), ['--xml' | Options], [stdout(pipe(P))]),
load_structure(P,Xml,[dialect(xml),space(remove)]).
% Fake aws_run for testing
aws_fake_run(Options, []) :-
write('EXEC: aws --xml'),
maplist(my_write, Options),
nl.
my_write(T) :- write(' '), write(T).