Commit a3633ae6 authored by Aaron Wells's avatar Aaron Wells
Browse files

more WIP on docker

parent 6e658d42
...@@ -13,8 +13,10 @@ ENV LOCALE en_US.UTF-8 ...@@ -13,8 +13,10 @@ ENV LOCALE en_US.UTF-8
# Install needed software # Install needed software
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y \
curl \
firefox \ firefox \
git \ git \
haveged \
libapache2-mod-php5 \ libapache2-mod-php5 \
postgresql \ postgresql \
php5-cli \ php5-cli \
...@@ -28,6 +30,7 @@ RUN apt-get update && apt-get install -y \ ...@@ -28,6 +30,7 @@ RUN apt-get update && apt-get install -y \
npm \ npm \
openjdk-7-jre \ openjdk-7-jre \
supervisor \ supervisor \
wget \
xvfb \ xvfb \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
......
<?php
echo "\n";
echo "########## Install composer\n";
echo "\n";
chdir('external');
passthru_or_die("curl -sS https://getcomposer.org/installer | php");
passthru_or_die(PHP_BINARY . ' composer.phar update');
chdir('..');
///////////////////////////////////// FUNCTIONS ///////////////////////////////////////
/**
* Call this function to do passthru(), but die if the command that was being
* invoked exited with a non-success return value.
*
* @param string $command The command to run
* @param string $diemsg If we die, then print this message explaining why we died.
*/
function passthru_or_die($command, $diemsg = null) {
echo "$command\n";
passthru($command, $return_var);
if ($return_var !== 0) {
if ($diemsg) {
gerrit_comment($diemsg);
}
else {
gerrit_comment(
"This patch failed attempting to run this command:\n{$command}\n\n"
."This is probably an error in Jenkins. Please retrigger this patch when the problem in Jenkins has been resolved."
,false
);
}
log_and_die($command, $return_var);
}
}
/**
* Call this function to do exec() but die if the command errored out.
* @param unknown $command
*/
function exec_or_die($command, &$output = null, &$return_var = null) {
echo "$command\n";
$returnstring = exec($command, $output, $return_var);
if ($return_var !== 0) {
gerrit_comment(
"This patch failed attempting to run this command:\n{$command}\n\n"
."This is probably an error in Jenkins. Please retrigger this patch when the problem in Jenkins has been resolved."
, false
);
log_and_die($command, $return_var);
}
return $returnstring;
}
/**
* This function emulates shellexec(), but dies if the command that was being
* invoked exited with a non-success exit value.
*
* Because it calls exec() on the backend, it also has the side effect of
* trimming whitespace from the return value (unlike shellexec(), which normally
* includes the ending "\n" on the output)
*
* @param unknown $command
*/
function shell_exec_or_die($command) {
echo "$command\n";
// shellexec() doesn't normally give you access to the command's exit code,
// so we instead will call exec()
exec_or_die($command, $output);
return implode("\n", $output);
}
/**
* Call this method to die after a bad command. It prints an error message
* about the command that failed, and then exits with status code 1.
* @param string $commandtolog The command to log a message about
* @param integer $itsreturnvar The return value of that command
*/
function log_and_die($commandtolog, $itsreturnvar) {
echo "\nEXITING WITH FAILURE\n";
echo "ERROR: Return value of '$itsreturnvar' on this command:\n";
echo "$commandtolog\n";
echo "\n";
debug_print_backtrace();
echo "\n";
exit(1);
}
\ No newline at end of file
...@@ -243,6 +243,26 @@ $BEHATTESTREGEX = "^test/behat/features/"; ...@@ -243,6 +243,26 @@ $BEHATTESTREGEX = "^test/behat/features/";
// $i++; // $i++;
// } // }
// echo "\n";
// echo "########## Verify that the patch contains a Behat test\n";
// echo "\n";
// if (trim(shell_exec("git diff-tree --no-commit-id --name-only -r HEAD | grep -c $BEHATTESTREGEX")) >= 1) {
// echo "Patch includes a Behat test.\n";
// }
// else {
// # Check whether the commit message has "behatnotneeded" in it.
// if (trim(shell_exec("git log -1 | grep -i -c $BEHATNOTNEEDED")) >= 1) {
// echo "This patch does not include a Behat test!\n... but the patch is marked with \"$BEHATNOTNEEDED\", so we will continue.\n";
// }
// else {
// gerrit_comment(
// "This patch does not include a Behat test (an automated test plan).\n\n"
// ."Please write a Behat test for it, or, if it cannot be tested in Behat or is covered by existing tests, put \"$BEHATNOTNEEDED\" in its commit message."
// );
// exit(1);
// }
// }
// Create and start docker instance // Create and start docker instance
$imagename = "mahara/jenkins:{$JOB_NAME}"; $imagename = "mahara/jenkins:{$JOB_NAME}";
$containername = "container-{$JOB_NAME}"; $containername = "container-{$JOB_NAME}";
...@@ -251,9 +271,7 @@ passthru_or_die("docker build -t {$imagename} docker"); ...@@ -251,9 +271,7 @@ passthru_or_die("docker build -t {$imagename} docker");
// Initialize the Mahara site // Initialize the Mahara site
passthru("docker rm -f $containername"); passthru("docker rm -f $containername");
$containerid = exec_or_die("docker run --name={$containername} -v {$HOME}:/var/lib/codesrc:rw {$imagename} git clone --no-hardlinks --depth 2 file:///var/lib/codesrc /var/www/mahara"); $containerid = exec_or_die("docker run --name={$containername} -v {$HOME}:/var/lib/codesrc:rw {$imagename} git clone --no-hardlinks --depth 2 file:///var/lib/codesrc /var/www/mahara");
passthru_or_die("docker commit --change='WORKDIR /var/www/mahara' --change='CMD /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf' $containername $imagename"); save_docker();
passthru("docker rm -f $containername");
passthru_or_die("docker run -d --name={$containername} {$imagename}");
echo "########## Run make minaccept\n"; echo "########## Run make minaccept\n";
echo "\n"; echo "\n";
...@@ -284,71 +302,54 @@ passthru_or_die( ...@@ -284,71 +302,54 @@ passthru_or_die(
. " {$containername}" . " {$containername}"
. " sudo -u www-data " . PHP_BINARY . " htdocs/admin/cli/install.php --adminpassword='password' --adminemail=never@example.com" . " sudo -u www-data " . PHP_BINARY . " htdocs/admin/cli/install.php --adminpassword='password' --adminemail=never@example.com"
); );
passthru_or_die("docker commit --change='WORKDIR /var/www/mahara' --change='CMD /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf' $containername $imagename"); //save_docker();
passthru("docker rm -f $containername");
passthru_or_die("docker run -d --name={$containername} {$imagename}");
// # Check if composer is not available // # Check if composer is not available
// if (!file_exists("external/composer.json")) { exec("docker exec $containername [ -f external/composer.json ]", $output, $return_var);
// exit(0); if ($return_var !== 0) {
// } echo "No composer.json. Exiting.";
exit(0);
}
// echo "\n"; echo "\n";
// echo "########## Install composer\n"; echo "########## Install composer\n";
// echo "\n"; echo "\n";
// chdir('external'); passthru_or_die(
// passthru_or_die("curl -sS https://getcomposer.org/installer | php"); "docker exec"
// passthru_or_die(PHP_BINARY . ' composer.phar update'); . " {$containername}"
// chdir('..'); . " /bin/sh -c \""
. " cd external;"
. " curl -sS https://getcomposer.org/installer | php; "
. PHP_BINARY . " composer.phar update;"
. "\""
);
//save_docker();
// echo "\n"; echo "\n";
// echo "########## Run unit tests\n"; echo "########## Run unit tests\n";
// echo "\n"; echo "\n";
// passthru_or_die( passthru_or_die(
// 'external/vendor/bin/phpunit htdocs/', "docker exec"
// "This patch caused one or more phpunit tests to fail.\n\n" . " {$containername}"
// ."Please see the console output on test.mahara.org for details, and fix any failing tests." . " external/vendor/bin/phpunit htdocs/",
// ); "This patch caused one or more phpunit tests to fail.\n\n"
."Please see the console output on test.mahara.org for details, and fix any failing tests."
);
// echo "\n";
// echo "########## Verify that the patch contains a Behat test\n";
// echo "\n";
// if (trim(shell_exec("git diff-tree --no-commit-id --name-only -r HEAD | grep -c $BEHATTESTREGEX")) >= 1) {
// echo "Patch includes a Behat test.\n";
// }
// else {
// # Check whether the commit message has "behatnotneeded" in it.
// if (trim(shell_exec("git log -1 | grep -i -c $BEHATNOTNEEDED")) >= 1) {
// echo "This patch does not include a Behat test!\n... but the patch is marked with \"$BEHATNOTNEEDED\", so we will continue.\n";
// }
// else {
// gerrit_comment(
// "This patch does not include a Behat test (an automated test plan).\n\n"
// ."Please write a Behat test for it, or, if it cannot be tested in Behat or is covered by existing tests, put \"$BEHATNOTNEEDED\" in its commit message."
// );
// exit(1);
// }
// }
// echo "\n"; echo "\n";
// echo "########## Build & Minify CSS\n"; echo "########## Run Behat\n";
// echo "\n"; echo "\n";
// passthru_or_die(
// 'make',
// "This patch encountered an error while attempting to build its CSS.\n\n"
// ."This may be an error in Jenkins"
// );
// echo "\n"; passthru_or_die(
// echo "########## Run Behat\n"; 'docker exec'
// echo "\n"; . " {$containername}"
. ' test/behat/mahara_behat.sh runheadless',
"This patch caused one or more Behat tests to fail.\n\n"
."Please see the console output on test.mahara.org for details, and fix any failing tests."
);
// passthru_or_die( passthru('docker rm -f {$containername}');
// 'test/behat/mahara_behat.sh runheadless',
// "This patch caused one or more Behat tests to fail.\n\n"
// ."Please see the console output on test.mahara.org for details, and fix any failing tests."
// );
exit(0); exit(0);
...@@ -531,4 +532,19 @@ function gerrit_post($relurl, $postobj, $authenticated = false) { ...@@ -531,4 +532,19 @@ function gerrit_post($relurl, $postobj, $authenticated = false) {
// We need to fetch the json line from the result // We need to fetch the json line from the result
$content = explode("\n", $content); $content = explode("\n", $content);
return json_decode($content[1]); return json_decode($content[1]);
}
function save_docker() {
global $containername, $imagename;
passthru_or_die("docker commit --change='WORKDIR /var/www/mahara' --change='CMD /usr/bin/supervisord -n -c /etc/supervisor/conf.d/supervisord.conf' $containername $imagename");
passthru("docker rm -f $containername");
passthru_or_die("docker run -d --name={$containername} {$imagename}");
while(true) {
exec("docker exec {$containername} sudo -u postgres psql -l", $output, $exitcode);
if ($exitcode === 0) {
break;
}
echo "waiting for postgres...\n";
sleep(1);
}
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment