-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from animecyc/feature/hooks
Adding support for hooks and various other improvements.
- Loading branch information
Showing
6 changed files
with
1,169 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('PATH/TO/YOUR/ENVIROMENTS'); | ||
|
||
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)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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=dbname'); | ||
ORM::configure('username', 'username'); | ||
ORM::configure('password', 'password'); | ||
|
||
// 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.