]> www.vanbest.org Git - motioneye-debian/commitdiff
added an update handler
authorCalin Crisan <ccrisan@gmail.com>
Sun, 27 Oct 2013 08:22:41 +0000 (10:22 +0200)
committerCalin Crisan <ccrisan@gmail.com>
Sun, 27 Oct 2013 08:50:16 +0000 (10:50 +0200)
src/handlers.py
src/server.py
src/update.py

index 89bb9bda1b8468bd8f3d4321fcd1bacd41dffaeb..81829aad6a263e7b4ecfdd115c1f474ad9b151be 100644 (file)
@@ -10,6 +10,7 @@ import mjpgclient
 import motionctl
 import remote
 import template
+import update
 import v4l2ctl
 
 
@@ -582,3 +583,25 @@ class MovieHandler(BaseHandler):
         # TODO implement me
         
         self.finish_json()
+
+
+class UpdateHandler(BaseHandler):
+    @BaseHandler.auth(admin=True)
+    def get(self):
+        logging.debug('listing versions')
+        
+        stable = self.get_argument('stable', default='false') == 'true'
+        
+        versions = update.get_all_versions(stable=stable)
+        
+        self.finish_json(versions)
+
+    @BaseHandler.auth(admin=True)
+    def post(self):
+        version = self.get_argument('version')
+        
+        logging.debug('performing update to version %(version)s' % {'version': version})
+        
+        result = update.perform_update(version)
+
+        return self.finish_json(result)
index c47aa2b5440ae5636607d7992113630060b7d24c..8818a8a09652636ab0c0afae8e3cee98089a07f3 100644 (file)
@@ -32,6 +32,7 @@ application = Application(
         (r'^/snapshot/(?P<camera_id>\d+)/(?P<op>download)/(?P<filename>.+)/?$', handlers.SnapshotHandler),
         (r'^/movie/(?P<camera_id>\d+)/(?P<op>list)/?$', handlers.MovieHandler),
         (r'^/movie/(?P<camera_id>\d+)/(?P<op>download)/(?P<filename>.+)/?$', handlers.MovieHandler),
+        (r'^/update/?$', handlers.UpdateHandler),
     ],
     debug=False, # also disables autoreloading
     log_function=log_request,
index 524149b0a5e84b623b45ead57d8433941ef8a995..961a000e920ba578a2d3d49cbb7e0e47d9adbc09 100644 (file)
@@ -40,6 +40,9 @@ def get_all_versions(stable=False):
         if stable:
             versions = [v for v in versions if v.count('.') == 1]
 
+        versions.sort()
+        
+        return ['master'] # TODO
         return versions
 
     except Exception as e:
@@ -85,6 +88,8 @@ def download(version):
             'version': version}
     
     try:
+        logging.debug('downloading %(url)s...' % {'url': url})
+        
         response = urllib2.urlopen(url, timeout=settings.REMOTE_REQUEST_TIMEOUT)
         data = response.read()
 
@@ -96,6 +101,8 @@ def download(version):
     path = tempfile.mkdtemp()
     path = os.path.join(path, version + '.tar.gz')
     
+    logging.debug('writing archive to %(path)s...' % {'path': path})
+    
     try:
         with open(path, 'w') as f:
             f.write(data)
@@ -110,7 +117,7 @@ def download(version):
 
 def cleanup(path):
     try:
-        os.remove(path)
+        shutil.rmtree(path)
     
     except Exception as e:
         logging.error('could cleanup update directory: %(msg)s' % {'msg': unicode(e)})
@@ -126,32 +133,43 @@ def is_updatable():
 
 
 def perform_update(version):
+    logging.info('updating to version %(version)s...' % {'version': version})
+    
     try:
         # download the archive
         archive = download(version)
         temp_path = os.path.dirname(archive)
         
         # extract the archive
-        if os.system('tar zxf %(archive)s -C %(path)s' % {
-                'archive': archive, 'path': temp_path}):
+        logging.debug('extracting archive %(archive)s...' % {'archive': archive})
+        os.system('tar zxf %(archive)s -C %(path)s' % {
+                'archive': archive, 'path': temp_path})
             
-            raise Exception('archive extraction failed')
-        
         # determine the root path of the extracted archive
-        root_path = [f for f in os.listdir(temp_path) if os.path.isdir(f)][0]
+        root_name = [f for f in os.listdir(temp_path) if os.path.isdir(os.path.join(temp_path, f))][0]
+        root_path = os.path.join(temp_path, root_name)
         
         for p in _UPDATE_PATHS:
             src = os.path.join(root_path, p)
             dst = os.path.join(settings.PROJECT_PATH, p)
             
+            logging.debug('copying %(src)s over %(dst)s...' % {'src': src, 'dst':  dst})
+            
             if os.path.isdir(dst):
-                os.remove(dst)
+                shutil.rmtree(dst)
             
-            shutil.copytree(src, dst)
+            if os.path.isdir(src):
+                shutil.copytree(src, dst)
+            
+            else:
+                shutil.copy(src, dst)
 
         # remove the temporary update directory
+        logging.debug('removing %(path)s...' % {'path': temp_path})
         cleanup(temp_path)
         
+        logging.info('updating done')
+        
         return True
     
     except Exception as e: