Permalink
Browse files

Adding support for hooks and various other improvements.

  • Loading branch information...
1 parent a7f7447 commit f993896d0813ef8d8c4a8c28a99671c3f6d153cc @animecyc animecyc committed Aug 13, 2012
Showing with 1,169 additions and 119 deletions.
  1. +50 −41 deploy.php
  2. +49 −55 index.php
  3. +1,046 −0 libs/spyc.php
  4. +3 −3 models/Project.php
  5. +4 −0 models/Setting.php
  6. +17 −20 slimjim.sql
View
@@ -1,43 +1,52 @@
#!/usr/bin/php
<?php
-
-chdir('/srv/www/slimjim.ruddl.com/public_html/');
-
-$argv = $_SERVER['argv'];
-
-if (file_exists("./requests/".$argv[1])) {
-
- //Get first line
- $request = fopen("./requests/".$argv[1], "r");
- $arguments = fgets($request);
- fclose($request);
-
- //Split string
- $params = explode("|", $arguments);
-
- //Change the working dir
- chdir($params[0]);
-
- //Run commands on repo
- $commands = array(
- 'unset GIT_DIR',
- 'git reset --hard',
- 'git checkout ' . $params[1],
- 'git fetch origin',
- 'git rebase origin/'. $params[1] . ' ' . $params[1],
- 'git status',
- );
-
- $output = '';
- foreach($commands AS $command) {
- $tmp = shell_exec($command . " 2>&1"); //> /dev/null 2>&1 &
- $output .= "{$command}\n";
- $output .= htmlentities(trim($tmp)) . "\n";
- }
-
- echo $output;
-
- //TODO: Delete request file
-}
-
-?>
+ chdir('/home/domains/ve.vq6t2pyw.vesrv.com/public/');
+
+ require "libs/spyc.php";
+
+ @ list(, $request_file) = $_SERVER['argv'];
+
+ if ($request_file && file_exists('requests/' . $request_file)) {
+ if($request_data = file_get_contents('requests/' . $request_file)) {
+ unlink('requests/' . $request_file);
+
+ $request_data = unserialize($request_data);
+
+ chdir($request_data['path']);
+
+ foreach(array(
+ 'git clone ' . $request_data['clone_url'] . ' ./',
+ 'git reset --hard',
+ 'git checkout ' . $request_data['branch'],
+ 'git fetch origin',
+ 'git rebase origin/'. $request_data['branch'] . ' ' . $request_data['branch'],
+ 'git status',
+ ) AS $command)
+ {
+ syslog(LOG_INFO, $command);
+ syslog(LOG_INFO, "===== " . shell_exec($command . " 2>&1"));
+ }
+
+ sleep(1);
+
+ $hooks = @ Spyc::YAMLLoad($request_data['hook_path']) ?: array();
+
+ syslog(LOG_INFO, serialize($hooks));
+
+ if(isset($hooks['writable'])) {
+ foreach($hooks['writable'] AS $make_writeable) {
+ $cmd = "chmod 777 -R " . $make_writeable . " 2>&1";
+ syslog(LOG_INFO, $cmd);
+ syslog(LOG_INFO, "===== " . shell_exec($cmd));
+ }
+ }
+
+ if(isset($hooks['after_deploy'])) {
+ foreach($hooks['after_deploy'] AS $after_deploy) {
+ $cmd = $after_deploy . " 2>&1";
+ syslog(LOG_INFO, $cmd);
+ syslog(LOG_INFO, "===== " . shell_exec($cmd));
+ }
+ }
+ }
+ }
View
104 index.php
@@ -1,64 +1,58 @@
-<?
+<?php
+ require 'libs/Slim/Slim.php';
+ require 'libs/Paris/idiorm.php';
+ require 'libs/Paris/paris.php';
+ require 'models/Project.php';
+ require 'models/Setting.php';
-//Slim
-require 'libs/Slim/Slim.php';
+ ORM::configure('mysql:host=localhost;dbname=slimjim');
+ ORM::configure('username', 'animecyc');
+ ORM::configure('password', '84542737');
-// Paris and Idiorm
-require 'libs/Paris/idiorm.php';
-require 'libs/Paris/paris.php';
+ $settings = array();
-// Models
-require 'models/Project.php';
-
-//Init DB
-ORM::configure('mysql:host=localhost;dbname=slimjim');
-ORM::configure('username', 'slimjim');
-ORM::configure('password', 'password');
-
-//App with custom settings
-$app = new Slim(array(
- 'log.enable' => true,
- 'log.path' => './logs',
- 'log.level' => 4
-));
+ foreach((Model::factory('Setting')->find_many() ?: array()) AS $obj) {
+ $settings[$obj->key] = $obj->value;
+ }
-//GET route
-$app->get('/', function () {
- echo "Hello you've reached SlimJim!";
-});
+ $app = new Slim(array(
+ 'settings' => $settings
+ ));
-//POST route
-$app->post('/deploy', function () use ($app) {
-
- //TODO: Check for github's IP
- $ip = $app->request()->getIp();
+ $app->post('/deploy', function () use ($app) {
+ $deploy_settings = $app->config('settings');
- $payload = $app->request()->params('payload');
+ if(! in_array($app->request()->getIp(), explode(',', $deploy_settings['allowed_from']))) {
+ $app->halt(401);
+ }
- if(empty($payload)) {
- $app->halt(403);
- }
-
- $payload = json_decode($payload);
+ if(! ($payload = $app->request()->params('payload'))) {
+ $app->halt(403);
+ }
- if(isset($payload->repository) && isset($payload->ref)) {
- $payload_branch = explode("/", $payload->ref);
- $payload_branch = $payload_branch[2];
+ $payload = json_decode($payload);
- //Check to see if repo is in the db
- $project = Model::factory('Project')
- ->where_equal('name', $payload->repository->name)
- ->where_equal('branch', $payload_branch)
- ->find_one();
-
- $file = 'requests/'.$payload->after.'.txt';
- $content = $project->path.'|'.$project->branch;
-
- file_put_contents($file, $content, LOCK_EX);
-
- } else {
- $app->halt(400);
- }
-});
-
-$app->run();
+ if(isset($payload->repository) && isset($payload->ref)) {
+ $payload_branch = explode("/", $payload->ref);
+ $payload_branch = $payload_branch[2];
+
+ $project = Model::factory('Project')
+ ->where_equal('name', $payload->repository->name)
+ ->where_equal('branch', $payload_branch)
+ ->find_one();
+
+ if($project) {
+ file_put_contents('requests/' . $payload->after . '.txt', serialize(array(
+ 'name' => $project->name,
+ 'clone_url' => $project->clone_url,
+ 'path' => $project->path,
+ 'branch' => $project->branch,
+ 'hook_path' => $project->path . $deploy_settings['hook_file']
+ )), LOCK_EX);
+ }
+ } else {
+ $app->halt(400);
+ }
+ });
+
+ $app->run();
Oops, something went wrong.

0 comments on commit f993896

Please sign in to comment.