Added utility to allow remote git updates.
authorManu Sporny <msporny@digitalbazaar.com>
Tue, 12 Jul 2011 23:18:21 -0400
changeset 63 37dacb0921d1
parent 61 589c75920830
child 64 b03c4e4a4b0f
Added utility to allow remote git updates.
utils/README
utils/git.php
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils/README	Tue Jul 12 23:18:21 2011 -0400
@@ -0,0 +1,13 @@
+Usage
+-----
+
+In order to use the git.php file, you must create a file called 
+remote-update-token.txt and place a value in there. You must then call
+the git.php file with a URL parameter named 'token' set to the value in
+the file. For example:
+
+http://json-ld.org/utils/git.php?token=7384724849
+
+While this is not a fool-proof security solution, it'll be good enough
+for now. Updates are throttled, even in the event of a DDoS, the update
+rate is once every 5 seconds.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils/git.php	Tue Jul 12 23:18:21 2011 -0400
@@ -0,0 +1,45 @@
+<?php
+
+// open the token file
+$tfile = fopen('remote-update-token.txt', 'r');
+
+// Acquire the lock in a non-blocking manner
+if($tfile == FALSE)
+{
+   echo 'ERROR: You must create a file called remote-update-token.txt and ' .
+      'place a secret token in that file. See the README for more information.';
+}
+else if(flock($tfile, LOCK_EX | LOCK_NB))
+{
+   $token = trim(fgets($tfile));
+   
+   // check to make sure that the token value is correct
+   if(array_key_exists('token', $_GET) and $token === $_GET['token'])
+   {
+      // perform a git pull
+      chdir('..');
+      $gitdir = getcwd() . "/.git";
+      system("git --git-dir $gitdir pull");
+      
+      echo 'git update successful';
+      
+      // Sleep for 5 seconds to throttle the update rate to 12 per minute
+      sleep(5);
+   }
+   else
+   {
+      echo 'ERROR: Invalid secret token provided. ' .
+         'See the README for more information.';
+   }
+   
+   // Release the lock file
+   flock($tfile, LOCK_UN); // release the lock
+}
+else 
+{
+   echo 'ERROR: An update is currently being performed, ' .
+      'this request has been rejected.';
+}
+
+fclose($tfile);
+?>