Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoos committed Jun 23, 2016
1 parent 0a27961 commit dcfebf7
Show file tree
Hide file tree
Showing 153 changed files with 12,826 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
@@ -0,0 +1,3 @@
{
"directory" : "vendor/bower"
}
34 changes: 34 additions & 0 deletions .gitignore
@@ -0,0 +1,34 @@
# phpstorm project files
.idea

# netbeans project files
nbproject

# zend studio for eclipse project files
.buildpath
.project
.settings

# windows thumbnail cache
Thumbs.db

# composer vendor dir
/vendor

# composer itself is not needed
composer.phar

# Mac DS_Store Files
.DS_Store

# phpunit itself is not needed
phpunit.phar
# local phpunit config
/phpunit.xml

/.ssh
/backups
/uploads
/runtime
/pwsafe.sublime-workspace
/web/assets
32 changes: 32 additions & 0 deletions LICENSE.md
@@ -0,0 +1,32 @@
The Yii framework is free software. It is released under the terms of
the following BSD License.

Copyright © 2008 by Yii Software LLC (http://www.yiisoft.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Yii Software LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
29 changes: 29 additions & 0 deletions assets/AppAsset.php
@@ -0,0 +1,29 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace app\assets;

use yii\web\AssetBundle;

/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
18 changes: 18 additions & 0 deletions assets/EventAsset.php
@@ -0,0 +1,18 @@
<?php

namespace app\assets;

use yii\web\AssetBundle;

class EventAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
];
public $js = [
'js/events.js',
];
public $depends = [
];
}
212 changes: 212 additions & 0 deletions commands/BackupController.php
@@ -0,0 +1,212 @@
<?php

namespace app\commands;

use yii;
use yii\db\Expression;
use app\commands\DaemonController;
use app\models\Ticket;
use app\models\Daemon;
use app\models\Activity;

/**
* Backup Daemon (pull)
* This is the Daemon which calls rdiff-backup to pull the data from the clients one by one.
*/
class BackupController extends DaemonController
{

public $ticket;
private $_cmd;
public $remoteUser = 'root';
public $remotePath = '/home/user';
//public $sshUserKnownHostsFile = ".ssh/known_hosts";

/**
* @inheritdoc
*/
public function start()
{
parent::start();
}

/**
* @inheritdoc
*/
public function doJob($id = '')
{

while (true) {
pcntl_signal_dispatch();
$this->cleanup();

if ($id != '') {
if (($this->ticket = Ticket::findOne(['id' => $id, 'backup_lock' => 0, 'restore_lock' => 0])) == null){
$this->log('Error: ticket with id ' . $id . ' not found or it is already in processing.');
return;
}
$this->ticket->backup_lock = 1;
$this->ticket->running_daemon_id = $this->daemon->id;
$this->ticket->save(false);
}

if ($this->ticket == null) {
$this->log('idle', true);
while (($this->ticket = $this->getNextTicket()) === null) {
sleep(5);
}
}

$this->log('Processing ticket: ' .
( empty($this->ticket->test_taker) ? $this->ticket->token : $this->ticket->test_taker) .
' (' . $this->ticket->ip . ')', true);
$this->ticket->backup_state = 'connecting to client...';
$this->ticket->save(false);

if ($this->checkPort(22, 3) === false) {
$this->ticket->backup_state = 'network error.';
$this->ticket->backup_last_try = new Expression('NOW()');
$this->ticket->backup_lock = 0;
$this->ticket->save(false);
}else{
$this->ticket->backup_state = 'backup in progress...';
$this->ticket->save(false);

$this->_cmd = "rdiff-backup --remote-schema 'ssh -i " . \Yii::$app->basePath . "/.ssh/rsa "
. "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -C %s rdiff-backup --server' "
. "-v5 --print-statistics "
. escapeshellarg($this->remoteUser . "@" . $this->ticket->ip . "::" . $this->remotePath) . " "
. escapeshellarg(\Yii::$app->basePath . "/backups/" . $this->ticket->token . "/") . " "
. "2>&1";

$this->log('Executing rdiff-backup: ' . $this->_cmd);

$output = array();
$lastLine = exec($this->_cmd, $output, $retval);
$output = implode(PHP_EOL, $output);

if($retval != 0){
$this->ticket->backup_state = 'rdiff-backup failed (retval: ' . $retval . '), output: '
. PHP_EOL . $output;
$this->log($this->ticket->backup_state);
}else{
$this->log($output);
$this->ticket->backup_last = new Expression('NOW()');
$this->ticket->backup_state = 'backup successful.';
}

$this->ticket->backup_last_try = new Expression('NOW()');
$this->ticket->backup_lock = 0;
$this->ticket->save(false);

}

$this->ticket = null;

if ($id != '') {
return;
}

}

}

/**
* @inheritdoc
*/
public function stop()
{

if ($this->ticket != null) {
$this->ticket->backup_state = 'backup aborted.';
$this->ticket->save(false, ['backup_state']);
}

parent::stop();
}

private function checkPort($port, $times = 1)
{
for($c=1;$c<=$times;$c++){
$fp = @fsockopen($this->ticket->ip, $port, $errno, $errstr, 10);
if (!$fp) {
$this->log('Port ' . $port . ' is closed or blocked. (try ' . $c . '/' . $times . ')');
sleep(5);
} else {
// port is open and available
fclose($fp);
return true;
}
}
return false;
}

// clean up abandoned tickets
private function cleanup()
{

$query = Ticket::find()
->where(['backup_lock' => 1])
->orWhere(['restore_lock' => 1]);

$tickets = $query->all();
foreach ($tickets as $ticket) {
if (($daemon = Daemon::findOne($ticket->running_daemon_id)) !== null) {
if ($daemon->running != true) {
$ticket->backup_lock = $ticket->restore_lock = 0;
$ticket->save(false);
$daemon->delete();
}
}else{
$ticket->backup_lock = $ticket->restore_lock = 0;
$ticket->save(false);
}
}
}

private function getNextTicket()
{

$this->cleanup();

// first those tickets with force flag on
$query = Ticket::find()
->where(['backup_force' => 1])
->andWhere(['backup_lock' => 0])
->orderBy(['backup_force' => SORT_DESC, 'backup_last_try' => SORT_ASC]);

if (($ticket = $query->one()) !== null) {
$ticket->backup_lock = 1;
$ticket->running_daemon_id = $this->daemon->id;
$ticket->backup_force = 0;
$ticket->save(false);
return $ticket;
}

// now those which weren't tried in the last 5 minutes
$query = Ticket::find()
->where(['not', ['start' => null]])
->andWhere(['end' => null])
->andWhere(['not', ['ip' => null]])
->andWhere([
'or',
['<', 'backup_last_try', new Expression('NOW() - INTERVAL 5 MINUTE')],
['backup_last_try' => null]
])
->andWhere(['backup_lock' => 0])
->andWhere(['restore_lock' => 0])
->orderBy(['backup_last_try' => SORT_ASC]);


if (($ticket = $query->one()) !== null) {
$ticket->backup_lock = 1;
$ticket->running_daemon_id = $this->daemon->id;
$ticket->save(false);
return $ticket;
}

return null;

}

}

0 comments on commit dcfebf7

Please sign in to comment.