log-uploader 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/python2.7

import sys
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler(FileSystemEventHandler):
  def on_modified(self, event):
    print vars(event)
    
  def on_created(self, event):
    if ".completed" in event.file:
      print "partition completed!"
      f = open("tmp/tmpupload", 'w')
      f.write(fmt_date() + ": partition completed") 
      f.close()
      upload_file(event.file, open("tmp/tmpupload", 'rb')
19
    else:
20 21 22 23 24 25
      return

def fmt_date():
  return time.strftime("[ %Y%m%d %H:%M:%S ]")

class uploader():
26 27
  system_name = "first-machine" # possibly generate later?
  url = 'https://[2001:67c:1254:e:a9::bafb]:???/post'
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

  # takes a file handler and a name (where it should be saved on server)
  def upload_file(file_name, contents):
    files = {'file': f}
    values = {'file_path': system_name+file_name}
    r = requests.post(url, files=files, data=values)


if __name__ == "__main__":
  event_handler = MyHandler()
  observer = Observer()
  observer.schedule(event_handler, path='/opt/slapos/log', recursive=False)
  observer.start()


  # check if .completed exists or not, and write it to the server log
  observer.schedule(event_handler, path='/opt/slapgrid/', recursive=True, patterns=".completed")
  observer.start()

  try:
    while True:
      time.sleep(1)
  except KeyboardInterrupt:
    observer.stop()
  observer.join()