forked from giuliocalzolari/s3_async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_async.py
executable file
·349 lines (245 loc) · 11.3 KB
/
s3_async.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python
# If not standard should be in requirements.txt
import yaml
import time
import os
import fnmatch
from cli.log import LoggingApp
from stat import S_ISDIR
import StringIO
import subprocess, threading
import Queue
import commands
import schedule
from pprint import pprint
import boto
import boto.utils
import boto.ec2
from boto.s3.key import Key
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
__author__ = ['Giulio.Calzolari']
class ChangeHandler(FileSystemEventHandler):
def __init__(self, config, ec2, log):
self.config = config
self.log = log
self.ec2_auto = ec2
self.refresh = False
self.copying = False
self.last_access = time.time()
self.queue_to_nofity = {}
self.queue_thread_nofity = []
def replicate_action(self):
# replication with S3
for repl in self.config["replicator"]:
if self.config["replicator"][repl]["status"] != "enable":
continue
if len(self.config["replicator"][repl]["url"]) > 0:
self.current_repl = self.config["replicator"][repl]
if self.execute_sync():
self.copying = False
def execute_sync(self):
key_file = os.path.dirname(os.path.realpath(__file__))+"/.aws_"+self.current_repl["bucket"]+".key"
if not os.path.isfile(key_file):
f = open(key_file,'w+')
f.write("[default]"+"\n")
f.write("aws_access_key_id="+ self.current_repl["acces_key"]+"\n")
f.write("aws_secret_access_key="+ self.current_repl["secret_key"]+"\n")
f.write("region="+ self.current_repl["region"]+"\n")
f.close()
os.environ["AWS_CONFIG_FILE"] = key_file
cmd = "aws s3 sync s3://"+self.current_repl["bucket"]+" "+self.config["basedir"] +" --quiet --region "+self.current_repl["region"]
command_run = subprocess.call([cmd], shell=True)
if command_run != 0:
self.log.error("error on : " + cmd )
return False
else:
self.log.debug("success on : " + cmd )
return True
def notify(self):
t = threading.Thread(target=self.notify_exec, args=(self.current_notif,))
t.start()
self.queue_thread_nofity.append(t)
def notify_exec(self,cfg):
cmd = "echo 'refresh' | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o ConnectTimeout=5 -o ConnectionAttempts=1 "+cfg["username"]+"@"+cfg["url"]+" -p "+str(cfg["port"])+" -i "+cfg["private_key"]+" 'cat > /tmp/.notify'"
command_run = subprocess.call([cmd ], shell=True)
if command_run != 0:
self.log.error("error to notify : " + cfg["url"] )
# increment attempt
if 'attempt' in cfg:
cfg["attempt"] = cfg["attempt"] + 1
if cfg["attempt"] > self.config["max_retry"]:
del self.queue_to_nofity[cfg["url"]]
self.log.error("Impossible to notify : " + cfg["url"] )
return
else:
cfg["attempt"] = 1
self.queue_to_nofity[cfg["url"]] = cfg
else:
self.log.debug("success notify : " + cfg["url"] )
if cfg["url"] in self.queue_to_nofity:
del self.queue_to_nofity[cfg["url"]]
def wait_to_notify(self):
if self.refresh == False:
# self.log.debug("Nothing to do")
return
t1 = time.time()
if (t1 - self.last_access) > self.config["grace_period"]:
self.log.debug("Pass "+str(self.config["grace_period"])+" second from last change")
self.copying = True
self.replicate_action()
# fetch notificator
for notif in self.config["notificator"]:
if self.config["notificator"][notif]["status"] != "enable":
continue
self.current_notif = self.config["notificator"][notif]
# notify static instances
if not self.config["notificator"][notif]["url"].startswith('ec2://'):
self.notify()
# notify autodiscoverd instances
if self.config["notificator"][notif]["url"].startswith('ec2://'):
for ec2_id, ec2_ip in self.ec2_auto.iteritems():
self.current_notif["url"] = ec2_ip
self.notify()
self.refresh = False
# wait for all threads to finish
if self.queue_thread_nofity:
for t in self.queue_thread_nofity:
t.join()
self.re_nofity()
schedule.every(self.config["retry_notify"]).seconds.do(self.re_nofity)
else:
self.log.debug("I'm waiting "+str(self.config["grace_period"] - int(t1 - self.last_access))+" to notify")
def re_nofity(self):
# list to reexecute
if (len(self.queue_to_nofity) > 0 ):
self.log.info("List to re-execute:"+str(len(self.queue_to_nofity)))
for notif, cfg in self.queue_to_nofity.iteritems():
self.log.debug("re_nofity:"+cfg["url"])
self.current_notif = cfg
self.notify()
# wait for all threads to finish
if self.queue_thread_nofity:
for t in self.queue_thread_nofity:
t.join()
else:
self.log.debug("List to re-execute empty")
schedule.cancel_job(self.re_nofity)
def on_any_event(self, event):
"If any file or folder is changed"
if event.src_path == '/tmp/.notify':
self.log.debug("Update request ")
self.copying = True
self.replicate_action()
return
# to avoid update loop on tmp
if event.src_path.startswith('/tmp/'):
return
# to avoid update loop
if self.copying:
return
self.refresh = True
self.last_access = time.time()
if event.is_directory:
self.log.debug(
"DIR : " + event.src_path + " " + event.event_type + " ")
else:
self.log.debug(
"FILE: " + event.src_path + " " + event.event_type + " ")
class S3aSync(LoggingApp):
def get_config(self):
try:
self.last_load = os.path.getmtime(self.params.config)
self.config = yaml.load(open(self.params.config))
except IOError as e:
self.log.debug(e)
quit("No Configuration file found at " + self.params.config)
def execute_cmd(self, cmd):
command_run = subprocess.call([cmd], shell=True)
if command_run != 0:
self.log.error("error on execute_cmd: " + cmd)
else:
self.log.debug("success on execute_cmd: " + cmd)
def ec2_update_discovery(self, repl):
self.log.info("get all instances match: " + repl["match_ec2"])
idx = boto.utils.get_instance_metadata()['instance-id']
if idx != "":
self.log.debug("I am a ec2 instaces with id : " + idx)
self.ec2_auto = {}
ec2_conn = boto.ec2.connect_to_region(repl["region"],aws_access_key_id=repl["acces_key"],aws_secret_access_key=repl["secret_key"])
reservations = ec2_conn.get_all_instances()
instances = [i for r in reservations for i in r.instances]
for i in instances:
# pprint(i.__dict__)
if idx == i.id:
self.log.debug("skip myslef on ec2_update_discovery : " + idx)
continue
if fnmatch.fnmatch(i.tags["Name"], repl["match_ec2"]):
if not str(i._state).startswith("running"):
self.log.debug("skip not running instances : "+ i.tags["Name"] +" - "+ i.id)
continue
self.log.info("found new instances: " + i.tags["Name"])
if repl["mapping"] == "public_ip_address":
self.ec2_auto[i.id] = i.ip_address or ""
if repl["mapping"] == "private_ip_address":
self.ec2_auto[i.id] = i.private_ip_address or ""
if repl["mapping"] == "public_dns_name":
self.ec2_auto[i.id] = i.public_dns_name or ""
if repl["mapping"] == "public_dns_name":
self.ec2_auto[i.id] = i.private_dns_name or ""
f = open(os.path.dirname(os.path.abspath(self.params.config))+"/autodiscovery.txt",'w+')
for ec2_id, ec2_ip in self.ec2_auto.iteritems():
f.write(ec2_id+":"+ ec2_ip+"\n")
f.close()
def ec2_autodiscovery(self):
for notif in self.config["notificator"]:
if self.config["notificator"][notif]["status"] != "enable":
continue
c_url = self.config["notificator"][notif]["url"]
if c_url.startswith('ec2://'):
# autodiscovery polling
self.ec2_update_discovery(self.config["notificator"][notif])
interval = self.config["notificator"][notif]["refresh"]
schedule.every(interval).seconds.do(self.ec2_update_discovery, self.config["notificator"][notif])
# ===========================MAIN===========================#
def main(self):
self.log.info("Starting")
self.get_config()
self.log.debug("directory Selected: " + self.config["basedir"])
self.ec2_auto = {}
# self.config_scheduled_cmd()
self.ec2_autodiscovery()
while 1:
event_handler = ChangeHandler(self.config, self.ec2_auto, self.log)
observer = Observer()
_track = Observer()
observer.schedule(event_handler, self.config["basedir"], recursive=True)
_track.schedule(event_handler, "/tmp/", recursive=True)
observer.start()
_track.start()
try:
while True:
if os.path.getmtime(self.params.config) != self.last_load:
self.log.info("Config change reloading data")
self.get_config()
self.ec2_autodiscovery()
schedule.clear()
# self.config_scheduled_cmd()
time.sleep(1)
schedule.run_pending()
event_handler.wait_to_notify()
except KeyboardInterrupt:
quit("Exit")
observer.stop()
_track.stop()
observer.join()
_track.join()
self.log.debug("Finished")
# ===========================MAGIC==============================#
if __name__ == "__main__":
app = S3aSync()
# app.add_param("-f", "--farm", help="Which vSphere you want to connect to in your config", default=None, required=True, action="store")
# app.add_param("-m", "--mode", help="What to make foreman do. Excepted Values: ['index','create','command','script','snapshot','powercycle','filedrop',todo']", default=None, required=True, action="store")
app.add_param("-c", "--config", help="Change the Configuration file to use",
default="./config.yaml", required=False, action="store")
app.run()