Example #1
0
function msg_die($msg)
{
    if (DBG_LOG) {
        dbg_log(' ', '!die-' . clean_filename($msg));
    }
    $output = bencode(array('min interval' => (int) 1800, 'failure reason' => (string) $msg, 'warning message' => (string) $msg));
    die($output);
}
Example #2
0
function db4_replace($key, $val)
{
    dbg_log("db4_replace({$key}, {$val})<br />");
    $ret = dba_replace(bin4($key), $val, $GLOBALS['db']);
    if ($ret === false) {
        die('failed to replace');
    }
    return $ret;
}
function mesos_get_state()
{
    $ch = curl_init();
    $url = get_config()->mesos_uri . "/state.json";
    dbg_log("Calling " . $url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    dbg_log($result);
    curl_close($ch);
    $decoded_object = json_decode($result, true);
    return $decoded_object;
}
function marathon_get_apps()
{
    $ch = curl_init();
    $url = get_config()->marathon_uri . "/v2/apps/";
    dbg_log("Calling " . $url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($httpCode != 200) {
        return NULL;
    }
    dbg_log($result);
    curl_close($ch);
    $decoded_object = json_decode($result, true);
    return $decoded_object["apps"];
}
Example #5
0
 function connect()
 {
     $connect_type = $this->cfg['pconnect'] ? 'pconnect' : 'connect';
     $this->cur_query = $connect_type . ' ' . $this->cfg['host'] . ':' . $this->cfg['port'];
     $this->debug('start');
     if (@$this->memcache->{$connect_type}($this->cfg['host'], $this->cfg['port'])) {
         $this->connected = true;
     }
     if (DBG_LOG) {
         dbg_log(' ', 'CACHE-connect' . ($this->connected ? '' : '-FAIL'));
     }
     if (!$this->connected && $this->cfg['con_required']) {
         die('Could not connect to memcached server');
     }
     $this->debug('stop');
     $this->cur_query = null;
 }
function docker_get_container_details_for_all($host)
{
    $containers = docker_get_containers($host);
    $container_details = array();
    foreach ($containers as $container) {
        #	var_dump($container);
        $containerID = $container["Id"];
        $containerDetails = docker_get_container_details($host, $containerID);
        foreach ($containerDetails["Config"]["Env"] as $env) {
            if (1 == preg_match('/^MESOS_TASK_ID\\=(.*)/', $env, $match)) {
                dbg_log("===========Found match {$match[1]}" . print_r($match, true) . " in " . $env);
                $container_details[$match[1]] = $containerDetails;
            }
        }
    }
    return $container_details;
}
function handle_status_update_event($event)
{
    dbg_log("Received event " . print_r($event, true));
    $prefix = substr($event["appId"], 1, strlen(get_config()->app_prefix));
    dbg_log("prefix is {$prefix}");
    if ($prefix == get_config()->app_prefix && $event["taskStatus"] == "TASK_RUNNING") {
        dbg_log("Handling event");
        $taskId = $event["taskId"];
        //then we need to add the pipework
        //to do that we need to identify the container
        //we have the marathon id but we need the docker id
        $host = $event["host"];
        $containers_details = docker_get_container_details_for_all($host);
        $container = $containers_details[$taskId];
        $containerID = $container["Id"];
        $ip = ip_pool_allocate(array("taskID" => "{$taskId}", "containerID" => "{$containerID}", "host" => $host));
        if ($ip == NULL) {
            throw new Exception("Could not allocate an ip address!");
        }
        dbg_log("Allocating ip {$ip} to docker id {$containerID} and mesos id {$taskId}");
        docker_add_container_public_ip($host, $containerID, $ip);
    }
    if ($prefix == get_config()->app_prefix && ($event["taskStatus"] == "TASK_KILLED" || $event["taskStatus"] == "TASK_LOST" || $event["taskStatus"] == "TASK_FAILED")) {
        dbg_log("Handling event");
        $taskID = $event["taskId"];
        $state = ip_pool_get_state();
        #var_dump($state);
        $publicIP = "none";
        $containerID = "none";
        foreach ($state["meta"] as $ip => $metadata) {
            if ($metadata["taskID"] == $taskID) {
                $containerID = $metadata["containerID"];
                $publicIP = $ip;
                break;
            }
        }
        if ($publicIP != "none") {
            dbg_log("Deallocating ip {$publicIP} from {$taskID}");
            ip_pool_deallocate($publicIP);
        } else {
            dbg_log("Could not find allocated ip for container {$taskID}");
        }
    }
}
Example #8
0
function get_square_id()
{
    if (isset($_REQUEST['square'])) {
        $square = format_int($_REQUEST['square']);
        dbg_log("{$looking} up #{$square}<br />\n");
        if (db4_exists($square)) {
            if (isset($_REQUEST['zoom'])) {
                list($parent, $position, $tog0, $tog1, $tog2, $tog3, $id0, $id1, $id2, $id3) = db_get_square($square);
                switch ($_REQUEST['zoom']) {
                    case '0':
                        $new = $id0;
                        break;
                    case '1':
                        $new = $id1;
                        break;
                    case '2':
                        $new = $id2;
                        break;
                    case '3':
                        $new = $id3;
                        break;
                    case 'out':
                        $new = $parent;
                        break;
                    default:
                        $new = false;
                }
                if ($new) {
                    # zoom zoom
                    $square = $new;
                }
            }
        } else {
            print 'NOT FOUND';
            $square = 1;
        }
    } else {
        $square = 1;
    }
    return $square;
}
function ssh_exec($conn, $cmd)
{
    dbg_log($cmd);
    return $ssh->exec($cmd);
}
Example #10
0
 /**
  * Close sql connection
  */
 function close()
 {
     if (is_resource($this->link)) {
         mysql_close($this->link);
     }
     $this->link = $this->selected_db = null;
     if (DBG_LOG) {
         dbg_log(str_repeat(' ', $this->num_queries), 'DB-num_queries-' . php_sapi_name());
     }
 }
<?php

include_once dirname(__FILE__) . "/../lib/docker_api.php";
include_once dirname(__FILE__) . "/../lib/utilities.php";
$ssh = ssh_connect("instance-11842.bigstep.io");
dbg_log($ssh->exec("ls /"));
Example #12
0
 function connect()
 {
     $this->cur_query = $this->dbg_enabled ? 'connect to: ' . $this->cfg['db_file_path'] : 'connect';
     $this->debug('start');
     if (@($this->dbh = new SQLite3($this->cfg['db_file_path']))) {
         $this->connected = true;
     }
     if (DBG_LOG) {
         dbg_log(' ', $this->cfg['log_name'] . '-connect' . ($this->connected ? '' : '-FAIL'));
     }
     if (!$this->connected && $this->cfg['con_required']) {
         trigger_error('SQLite not connected', E_USER_ERROR);
     }
     $this->debug('stop');
     $this->cur_query = null;
 }
<?php

require_once dirname(__FILE__) . "/../lib/utilities.php";
require_once dirname(__FILE__) . "/../lib/marathon_api.php";
var_dump($_POST);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (array_key_exists("appid", $_POST)) {
        $appid = strtolower($_POST["appid"]);
        $appid = str_replace(' ', '', $appid);
        if (strlen($appid > 100)) {
            $appid = substr($appid, 0, 100);
        }
        dbg_log("destroying app {$appid}");
        marathon_destroy($appid);
    }
}
<?php

require_once dirname(__FILE__) . "/../lib/utilities.php";
require_once dirname(__FILE__) . "/../lib/marathon_api.php";
var_dump($_POST);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (array_key_exists("appid", $_POST)) {
        $appid = strtolower($_POST["appid"]);
        $appid = str_replace(' ', '', $appid);
        if (strlen($appid > 100)) {
            $appid = substr($appid, 0, 100);
        }
        $dockerimage = strtolower($_POST["dockerimage"]);
        $instances = intval($_POST["instances"]);
        $cpus = floatval($_POST["cpus"]);
        $mem = intval($_POST["mem"]);
        dbg_log("creating app {$appid}");
        marathon_create($appid, $dockerimage, $cpus, $mem, $instances);
        header("Location: http://" . $_SERVER["SERVER_NAME"] . "/app_details.php?appid={$appid}");
    }
}
function ip_pool_save_state($state)
{
    $state_file = get_config()->ip_pool_state_file;
    dbg_log("Writing ippool state " . print_r($state, true) . " to {$state_file}");
    file_put_contents($state_file, json_encode($state));
}