forked from numan/ReDiS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadministration.py
More file actions
194 lines (147 loc) · 5.63 KB
/
administration.py
File metadata and controls
194 lines (147 loc) · 5.63 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Copyright (C) 2011, 2012 9apps B.V.
#
# This file is part of Redis for AWS.
#
# Redis for AWS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Redis for AWS 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 Redis for AWS. If not, see <http://www.gnu.org/licenses/>.
import os, sys
import json, urllib2
import hashlib
from time import gmtime,strftime
from boto.sdb.connection import SDBConnection
from boto.sdb.regioninfo import RegionInfo
try:
url = "http://169.254.169.254/latest/"
public_hostname = urllib2.urlopen(url + "meta-data/public-hostname").read()
availability_zone = urllib2.urlopen(url + "meta-data/placement/availability-zone").read()
region = availability_zone[:-1]
except:
exit("We should be getting user-data here...")
region_info = RegionInfo(name=region,endpoint="sdb.{0}.amazonaws.com".format(region))
def set_cluster_metadata(key, access, cluster):
sdb = SDBConnection(key, access, region=region_info)
domain = sdb.create_domain(cluster)
# set the basic values in the 'master' record
metadata = domain.new_item('metadata')
#master = "{0}.{1}".format(
# os.environ['REDIS_NAME'].strip(),
# os.environ['HOSTED_ZONE_NAME'].rstrip('.'))
#metadata.add_value('master', master)
try:
if "" != os.environ['REDIS_SIZE'].strip():
metadata.add_value('size', os.environ['REDIS_SIZE'])
except:
pass
try:
if "" != os.environ['REDIS_PERSISTENCE'].strip():
metadata.add_value('persistence', os.environ['REDIS_PERSISTENCE'])
else:
metadata.add_value('persistence', 'no')
except:
metadata.add_value('persistence', 'no')
try:
if "" != os.environ['REDIS_SNAPSHOT'].strip():
metadata.add_value('snapshot', os.environ['REDIS_SNAPSHOT'])
except:
pass
try:
if "" != os.environ['REDIS_RDB'].strip():
metadata.add_value('rdb', os.environ['REDIS_RDB'])
except:
pass
try:
if "" != os.environ['REDIS_AOF'].strip():
metadata.add_value('aof', os.environ['REDIS_AOF'])
except:
pass
metadata.save()
def get_cluster_metadata(key, access, cluster):
sdb = SDBConnection(key, access, region=region_info)
domain = sdb.create_domain(cluster)
return domain.get_item('metadata', True)
def set_RDB(key, access, cluster, location):
sdb = SDBConnection(key, access, region=region_info)
domain = sdb.create_domain(cluster)
# add the latest rdb (for automatic restores)
latest = domain.new_item('rdb')
latest.add_value('rdb', location)
# get the expiration date from the object name (for comparison)
latest.add_value('created', strftime("%Y-%m-%d %H:%M:%S", gmtime()))
latest.save()
def get_latest_RDB(key, access, cluster):
sdb = SDBConnection(key, access, region=region_info)
domain = sdb.create_domain(cluster)
try:
return domain.get_item('rdb', True)['rdb']
except:
return None
def add_snapshot(key, access, cluster, snapshot):
sdb = SDBConnection(key, access, region=region_info)
domain = sdb.create_domain(cluster)
now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
# add the latest rdb (for automatic restores)
latest = domain.new_item('snapshot')
latest.add_value('snapshot', snapshot[0])
# get the expiration date from the object name (for comparison)
latest.add_value('created', now)
latest.save()
# add the snapshot for expiration
backup = domain.new_item(snapshot[0])
backup.add_value('snapshot', snapshot[0])
backup.add_value('created', now)
backup.add_value('expires', snapshot[1])
backup.save()
# add the latest (for automatic restores)
latest = domain.new_item('snapshot')
latest.add_value('snapshot', snapshot[0])
latest.save()
def get_latest_snapshot(key, access, cluster):
sdb = SDBConnection(key, access, region=region_info)
domain = sdb.create_domain(cluster)
try:
return domain.get_item('snapshot', True)['snapshot']
except:
return None
def delete_snapshot(key, access, cluster, snapshot_id):
sdb = SDBConnection(key, access, region=region_info)
domain = sdb.create_domain(cluster)
return domain.delete_item(domain.get_item(snapshot_id))
def get_expired_snapshots(key, access, cluster):
sdb = SDBConnection(key, access, region=region_info)
domain = sdb.create_domain(cluster)
now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
select = "select * from `{0}` where itemName() > 'snap-' and itemName() != 'snapshot' and expires < '{1}'".format(cluster, now)
snapshots = domain.select(select, consistent_read=True)
return snapshots
def get_all_snapshots(key, access, cluster):
sdb = SDBConnection(key, access, region=region_info)
domain = sdb.create_domain(cluster)
now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
select = "select * from `{0}` where itemName() > 'snap-' and itemName() != 'snapshot'".format(cluster)
snapshots = domain.select(select, consistent_read=True)
return snapshots
def get_identity(key, access, cluster):
sdb = SDBConnection(key, access, region=region_info)
domain = sdb.create_domain(cluster)
slave_id = hashlib.md5(public_hostname).hexdigest()[:8]
slave_fqdn = "{0}.{1}".format(slave_id, cluster)
slave = domain.new_item(slave_fqdn)
slave.add_value('id', slave_id)
slave.add_value('endpoint', public_hostname)
slave.save()
return slave_fqdn
if __name__ == '__main__':
import sys
#set_cluster_metadata(sys.argv[1],sys.argv[2])
metadata = get_cluster_metadata(sys.argv[1],sys.argv[2])
print metadata['master']