function stop_users_vms()
{
    logActivity("Starting to stop vms for users.");
    //Find all users whos credit is low
    $table = "tblclients";
    $fields = "*";
    $result = select_query($table, $fields);
    if ($result) {
        while ($data = mysql_fetch_array($result)) {
            $userid = $data['id'];
            $balanceLimit = get_balance_limit($userid);
            if (!$balanceLimit || !is_numeric($balanceLimit)) {
                $balanceLimit = 0;
            }
            logActivity("Balance limit for user " . $userid . ": " . $balanceLimit);
            if (getCreditForUserId($userid) + $balanceLimit < 0) {
                logActivity("Stopping vms for user: "******"Stopped vms for user: "******". Result:" . $res);
                } else {
                    logActivity("Stoping vms failed for user: "******"Stopping vms for users ended.");
}
function modify_oms_passwd($vars)
{
    $userid = $vars['userid'];
    $password = $vars['password'];
    $command = '/bin/passwd?arg=-u&arg=' . $userid . '&arg=' . $password;
    $result = oms_command($command);
    logActivity('Modified password of the OMS user ' . $username . ', result: ' . $result);
}
Esempio n. 3
0
/**
Authenticate user against OMS server.
*/
function oms_auth($username, $password)
{
    $command = 'auth';
    $data = array();
    $data['username'] = $username;
    $data['password'] = $password;
    $result = oms_command($command, json_encode($data));
}
function create_new_vm_with_invoice($vars)
{
    global $oms_usage_db, $oms_templates_mapping, $vm_default_nameservers;
    $invoiceId = $vars['invoiceid'];
    logActivity("Starting processing paid invoice:" . $invoiceId);
    $invoice = getInvoiceById($invoiceId);
    $userId = $invoice['userid'];
    $username = $userId;
    // XXX cleanup once username is removed
    // On invoice payment it is needed that credit stays on account. It is automattically removed, so need to add it manually
    // XXX yes, it's an ugly hack
    $amountPaid = $invoice['total'];
    $items = $invoice['items'];
    foreach ($items['item'] as $item) {
        $boughtVmProduct = false;
        // to track if the item is a VM or a credit package
        $vmData = array();
        $itemId = $item['relid'];
        $clientproduct = getClientsProduct($userId, $itemId);
        //get template
        if ($clientproduct['configoptions']) {
            foreach ($clientproduct['configoptions'] as $configoption) {
                $confopt = $configoption[0];
                if ($confopt['option'] == "Template") {
                    $boughtVmProduct = true;
                    //Use value(OMS template name) or find it from mapping variableF
                    if (is_array($oms_templates_mapping) && count($oms_templates_mapping) > 0) {
                        $template = $oms_templates_mapping[$confopt['value']];
                        if (!$template) {
                            logActivity("Error: No OMS template found in oms_templates_mapping for value(using it instead):" . $confopt['value']);
                            $vmData['template'] = $confopt['value'];
                        } else {
                            $vmData['template'] = $template;
                            logActivity("Using template name from mapping variable:" . $template);
                        }
                    } else {
                        $vmData['template'] = $confopt['value'];
                    }
                }
            }
        }
        if ($boughtVmProduct) {
            if (!$vmData['template']) {
                logActivity("Error. No template found for product id:" . $itemId);
                continue;
            }
        } else {
            logActivity("Processing a non-VM product");
        }
        //$vmData[swap_size]=0.5;
        if ($clientproduct && $boughtVmProduct) {
            //get data from client product
            $vmData['hostname'] = $clientproduct['domain'];
            $vmData['root_password'] = $clientproduct['password'];
            $vmData['root_password_repeat'] = $clientproduct['password'];
            $vmData['nameservers'] = $vm_default_nameservers;
            //get item
            $products = getBundlesProductsByOtherProductId($clientproduct['pid']);
            if ($products) {
                foreach ($products as $product) {
                    // If name contains number. eg 10GB
                    preg_match('/^\\d*/', $product['name'], $matches);
                    $amount = $matches[0] ? $matches[0] : 1;
                    if (stristr($product['name'], "core")) {
                        $vmData['num_cores'] = $product['count'] * $amount;
                    }
                    if (stristr($product['name'], "ram")) {
                        $vmData['memory'] = $product['count'] * $amount;
                    }
                    if (stristr($product['name'], "storage")) {
                        $vmData['diskspace'] = $product['count'] * $amount * 1024;
                    }
                    // OMS expects values in MB
                }
                logActivity("VM settings. cores: " . $vmData['num_cores'] . ". memory:" . $vmData['memory'] . ". disk: " . $vmData['diskspace']);
                if ($vmData['num_cores'] > 0 && $vmData['memory'] > 0 && $vmData['diskspace'] > 0) {
                    logActivity("Running oms command to create VM.");
                    $command = '/machines/hangar/vms-openvz';
                    $result = oms_command($command, json_encode($vmData));
                    logActivity($result);
                    $data = json_decode($result);
                    $id = $data->result->id;
                    if ($id) {
                        $urlHangar = $command . "/" . $id;
                        $urlChowning = $urlHangar;
                        logActivity("Running command Chown for username:"******" and url:" . $urlChowning);
                        oms_command('/bin/chown?arg=' . $username . '&arg=' . $urlChowning);
                        $urlHangarAllocate = $urlHangar . "/actions/allocate";
                        logActivity("Attempting to allocate " . $urlHangar);
                        $result = oms_command($urlHangarAllocate);
                        logActivity("Allocation of " . $urlHangar . " result: " . $result);
                    } else {
                        logActivity("Error running command Chown for username:"******". No computeId");
                    }
                } else {
                    logActivity("Error: VM settings not set.");
                }
            }
        }
    }
    // TODO this breaks Add Funds functionality as it shall add funds x2
    $desc = "Adding credit for invoice:" . $invoiceId;
    addCreditForUserId($userId, $amountPaid, $desc);
    updateClientCreditBalance($userId);
    /*logActivity("Removing orders for userId:" . $userId);
      $orders = getUsersOrders($userId);
      if ($orders) {
      foreach ($orders as $order) {
      removeUsersOrder($order['id']);
      }
      }*/
    logActivity("Processing user purchase is done.");
}
/**
 * Update templates from OMS or from oms_templates array
 */
function updateTemplates($productConfOptionId)
{
    global $oms_templates;
    if (is_array($oms_templates) && count($oms_templates) > 0) {
        logActivity("Building templates from array oms_templates.");
        $i = 0;
        foreach ($oms_templates as $template) {
            $values[configid] = $productConfOptionId;
            $values[optionname] = $template;
            $values[sortorder] = $i;
            $values[hidden] = 0;
            createOrUpdateProductCongfigOptionsSub($values);
            $i++;
        }
    } else {
        logActivity("Quering templates from oms.");
        $command = '/templates?depth=1&attrs=name&exclude=actions';
        $result = oms_command($command, null, "GET");
        $i = 0;
        $data = json_decode($result);
        foreach ($data->children as $template) {
            $values[configid] = $productConfOptionId;
            $values[optionname] = $template->name;
            $values[sortorder] = $i;
            $values[hidden] = 0;
            createOrUpdateProductCongfigOptionsSub($values);
            $i++;
        }
    }
}