Esempio n. 1
0
function switchserver_main()
{
    global $argc, $argv;
    global $gbl, $sgbl, $login, $ghtml;
    //sleep(60);
    initProgram("admin");
    if ($argc === 1) {
        print "Usage: {$argv['0']} --class= --name= --v-syncserver= \n";
        exit;
    }
    try {
        $opt = parse_opt($argv);
        $param = get_variable($opt);
        dprintr($param);
        $class = $opt['class'];
        $name = $opt['name'];
        if (lx_core_lock("{$class}-{$name}.switchserver")) {
            exit;
        }
        $object = new $class(null, 'localhost', $name);
        $object->get();
        if ($object->dbaction === 'add') {
            throw new lxException("no_object", '', '');
            exit;
        }
        if (!$object->syncserver) {
            print "No_synserver...\n";
            throw new lxException("no_syncserver", '', '');
            exit;
        }
        if ($param['syncserver'] === $object->syncserver) {
            print "No Change...\n";
            throw new lxException("no_change", '', '');
            exit;
        }
        $driverapp_old = $gbl->getSyncClass('localhost', $object->syncserver, $object->get__table());
        $driverapp_new = $gbl->getSyncClass('localhost', $param['syncserver'], $object->get__table());
        if ($driverapp_new !== $driverapp_old) {
            //throw new lxException ("the_drivers_are_different_in_two_servers", '', '');
        }
        $object->doupdateSwitchserver($param);
    } catch (exception $e) {
        print $e->getMessage();
        /// hcak ahck... Chnage only the olddelete variable which is the mutex used for locking in the process of switch. The problem is we want to totally bail out if the switchserver fails. The corect way would be save after reverting the syncserve to the old value, but that's a bit risky. So we just use a hack to change only the olddeleteflag; Not a real hack.. This is the better way.
        $message = "{$e->getMessage()}";
        write_to_object($object, $message, $param['syncserver']);
        $fullmesage = "Switch of {$object->get__table()}:{$object->nname} to {$object->syncserver} failed due to {$e->getMessage()}";
        log_switch($fullmesage);
        mail($login->contactemail, "Switch Failed:", "{$fullmesage}\n");
        print "\n";
        exit;
    }
    mail($login->contactemail, "Switch Succeeded", "Switch Succeeded {$object->get__table()}:{$object->nname} to {$param['syncserver']}\n");
}
Esempio n. 2
0
function backup_main()
{
    global $argc, $argv;
    global $gbl, $login, $ghtml;
    $gbl->__restore_flag = true;
    if ($argc === 1) {
        print "Usage: {$argv['0']} --restore/--list --accounts='domain-<domain1.com>,client-<client1>,domain-<domain2.com>' <backup-file> [--switchserverlist='oldserver1:newserver1,oldserver2:newserver2']\n Use --accounts=all to restore everything.\n";
        exit;
    }
    initProgram("admin");
    $object = $login;
    $opt = parse_opt($argv);
    if (isset($opt['class']) && isset($opt['name'])) {
        $object = new $opt['class'](null, null, $opt['name']);
        $object->get();
        if ($object->dbaction === 'add') {
            log_error("{$opt['class']} doesnt exist");
            print "{$opt['class']} doesnt exist\n";
            exit;
        }
    }
    $class = $opt['class'];
    $name = $opt['name'];
    if (lx_core_lock("{$class}-{$name}.restore")) {
        print "Another Restore for the same class is happening..\n";
        exit;
    }
    $backup = $object->getObject('lxbackup');
    if (isset($opt['switchserverlist'])) {
        $sq = new Sqlite(null, "pserver");
        $serverlist = $sq->getTable();
        $serverlist = get_namelist_from_arraylist($serverlist);
        $server = $opt['switchserverlist'];
        $list = explode(",", $server);
        foreach ($list as $l) {
            if (!$l) {
                continue;
            }
            $q = explode(":", $l);
            $rlist[$q[0]] = $q[1];
            if (!array_search_bool($q[1], $serverlist)) {
                print "The server {$q[1]} doesn't exist in the server system here\n";
                exit;
            }
        }
        $param['switchserverlist'] = $rlist;
        dprint("\n");
    } else {
        $param['switchserverlist'] = null;
    }
    /*
    	if (!testAllServersWithMessage()) {
    		$backup->restorestage = "Failed due to: could not connect to slave servers";
    		clearLxbackup($backup);
    		exit;
    	}
    */
    $file = $opt['final'];
    //$param = get_variable($opt);
    if (isset($opt['list'])) {
        $gbl->__var_list_flag = true;
        $param['_accountselect'] = null;
    } else {
        if (isset($opt['restore'])) {
            $gbl->__var_list_flag = false;
            if (!isset($opt['accounts'])) {
                print "Restore option needs accounts that are to be restored. --accounts='domain-domain.com,client:clientname'... Use --list to find out all the domain/clients in the backup archive.\n";
                clearLxbackup($backup);
                exit;
            }
            $account = $opt['accounts'];
            //$account = str_replace(":", "_s_vv_p_", $account);
            $account = str_replace(":", "-", $account);
            $accountlist = explode(",", $account);
            $param['_accountselect'] = $accountlist;
        } else {
            print "Usage: {$argv['0']} <--list/--restore --accounts=> <filename>\n";
            clearLxbackup($backup);
            exit;
        }
    }
    if (isset($opt['priority']) && $opt['priority'] === 'low') {
        sleep(20);
    }
    dprintr($param);
    //dprint($file);
    try {
        $backup->doUpdateRestore($file, $param);
        $backup->restorestage = 'done';
    } catch (exception $e) {
        log_error("Restore Failed. Reason: {$e->__full_message} \n");
        print "Restore Failed. Reason: {$e->__full_message} \n";
        $mess = $e->__full_message;
        mail($object->contactemail, "Restore Failed..", "Restore Failed for {$object->nname} with the Message {$mess}");
        $backup->restorestage = "Restore failed due to {$mess}";
    }
    clearLxbackup($backup);
}
Esempio n. 3
0
function exit_if_another_instance_running()
{
    if (lx_core_lock()) {
        print "Another Copy of the same program is currently Running on pid\n";
        exit;
    }
}
Esempio n. 4
0
 public function doRealCreate()
 {
     global $gbl, $sgbl, $login, $ghtml;
     $nname = $this->main->nname;
     lx_core_lock("{$nname}.create");
     $this->createRootPath();
     lxfile_mkdir($this->main->configrootdir);
     $this->setDhCP();
     if ($this->main->isWindows()) {
         $templatefile = "__path_program_home/xen/template/{$this->main->ostemplate}";
     } else {
         $templatefile = "__path_program_home/xen/template/{$this->main->ostemplate}.tar.gz";
     }
     $this->main->getOsTemplateFromMaster($templatefile);
     if (!lxfile_real($templatefile)) {
         log_error("could not create vm. Could not download {$templatefile}");
         lfile_put_contents("__path_program_root/tmp/{$nname}.createfailed", "Could not download {$templatefile}");
         exit;
     }
     $size = 0;
     if ($this->main->isWindows()) {
         $size = lxfile_size($templatefile);
         $size = $size / (1024 * 1024);
     }
     try {
         $this->createConfig();
         $this->setMemoryUsage();
         $this->setCpuUsage();
         $this->setSwapUsage();
         $this->createSwap();
         $this->createDisk($size);
     } catch (Exception $e) {
         log_error("could not create vm. Error was {$e->getMessage()}");
         lfile_put_contents("__path_program_root/tmp/{$nname}.createfailed", "{$e->getMessage()}");
         exit;
     }
     if (!$this->main->isWindows()) {
         $mountpoint = $this->mount_this_guy();
         lxshell_return("tar", "-C", $mountpoint, "--numeric-owner", "-xpzf", $templatefile);
         $this->setInternalParam($mountpoint);
         $this->copyKernelModules();
         lxfile_cp("../file/sysfile/xen/fstab", "{$mountpoint}/etc/fstab");
         lunlink("{$mountpoint}/etc/mtab");
     } else {
         if (!$this->main->isBlankWindows()) {
             $templatefile = expand_real_root($templatefile);
             lxshell_return("parted", "-s", $this->main->maindisk, "mklabel", "msdos");
             $this->runParted();
             $partition = $this->getPartition();
             lxshell_return("ntfsfix", $partition);
             //lxshell_return("dd", "if=$templatefile", "of={$this->main->maindisk}");
             lxshell_return("ntfsclone", "--restore-image", "--force", "-O", $partition, $templatefile);
             $this->kpart_remove();
             $this->expandPartitionToImage();
             //lxshell_return("ntfsclone", "-O", $loop, $templatefile);
             //lo_remove($loop);
         }
     }
     $this->main->status = 'On';
     try {
         $this->toggleStatus();
     } catch (Exception $e) {
     }
     $this->postCreate();
 }
Esempio n. 5
0
function switchserver_main()
{
    global $argc, $argv;
    global $gbl, $sgbl, $login, $ghtml;
    //sleep(60);
    initProgram("admin");
    if ($argc === 1) {
        print "Usage: {$argv['0']} --class= --name= --v-syncserver= \n";
        exit;
    }
    try {
        $opt = parse_opt($argv);
        $param = get_variable($opt);
        dprintr($param);
        $class = $opt['class'];
        $name = $opt['name'];
        if (lx_core_lock("{$class}-{$name}.livemigrate")) {
            exit;
        }
        $object = new $class(null, 'localhost', $name);
        $object->get();
        if ($object->dbaction === 'add') {
            throw new lxException("no_object", '', '');
            exit;
        }
        if (!$object->syncserver) {
            print "No_synserver...\n";
            throw new lxException("no_syncserver", '', '');
            exit;
        }
        if ($param['syncserver'] === $object->syncserver) {
            print "No Change...\n";
            throw new lxException("no_change", '', '');
            exit;
        }
        $driverapp_old = $gbl->getSyncClass('localhost', $object->syncserver, $object->getClass());
        $driverapp_new = $gbl->getSyncClass('localhost', $param['syncserver'], $object->getClass());
        $oldserver = $object->syncserver;
        $newserver = $param['syncserver'];
        if ($driverapp_new !== $driverapp_old) {
            throw new lxException("the_drivers_are_different_in_two_servers", '', '');
        }
        $actualserver = getFQDNforServer($newserver);
        setup_ssh_channel($oldserver, $newserver, $actualserver);
        $ssh_port = db_get_value("sshconfig", $newserver, "ssh_port");
        if (!$ssh_port) {
            $ssh_port = "22";
        }
        $res = rl_exec_get(null, $oldserver, "exec_vzmigrate", array($object->vpsid, $actualserver, $ssh_port));
        list($ret, $error) = $res;
        if ($ret !== 0) {
            throw new lxException("vzmigrate_failed_due_to:{$error}", '', '');
        }
        $object->olddeleteflag = 'done';
        $object->syncserver = $newserver;
        $object->username = null;
        $object->makeSureTheUserExists();
        $object->setUpdateSubaction();
        $object->write();
    } catch (exception $e) {
        print $e->getMessage();
        /// hcak ahck... Chnage only the olddelete variable which is the mutex used for locking in the process of switch. The problem is we want to totally bail out if the switchserver fails. The corect way would be save after reverting the syncserve to the old value, but that's a bit risky. So we just use a hack to change only the olddeleteflag; Not a real hack.. This is the better way.
        $message = "{$e->getMessage()}";
        $message = str_replace("'", "", $message);
        write_to_object($object, $message, $param['syncserver']);
        $fullmesage = "Switch of {$object->getClass()}:{$object->nname} to {$param['syncserver']} failed due to {$e->getMessage()}";
        log_switch($fullmesage);
        mail($login->contactemail, "Switch Failed:", "{$fullmesage}\n");
        print "\n";
        exit;
    }
    mail($login->contactemail, "Switch Succeeded", "Switch Succeeded {$object->getClass()}:{$object->nname} to {$param['syncserver']}\n");
}
Esempio n. 6
0
 /**
 * Check the existance of a VPS lock running.
 * 
 * Throws a exception if someone else is using a VPS.
 *
 * @author Anonymous <*****@*****.**>
 * @author Ángel Guzmán Maeso <*****@*****.**>
 *
 * @throws lxException
 * @return void
 */
 function checkVPSLock($vps_id = NULL)
 {
     $file = 'vpslock_' . $vpsid . '.pid';
     // @todo this seems a harmful way to check a lock file with sleep. Research this
     $i = 0;
     while (TRUE) {
         if (lx_core_lock($file)) {
             sleep(3);
             $i++;
             if ($i >= 8) {
                 throw new lxexception('vps_is_locked_by_another_user', '', $vpsid);
             }
         } else {
             break;
         }
     }
 }
Esempio n. 7
0
 public function changeLocation()
 {
     if ($this->main->newlocation === $this->main->corerootdir) {
         throw new lxException("old_new_location_same");
     }
     $this->stop();
     $this->umountThis();
     $this->setMemoryUsage();
     $this->setCpuUsage();
     $this->setDiskUsage();
     $this->__oldlocation = $this->main->corerootdir;
     $this->main->corerootdir = $this->main->newlocation;
     $name = strtil($this->main->nname, ".vm");
     $this->main->maindiskname = "{$name}_rootimg";
     $this->main->swapdiskname = "{$name}_vmswap";
     $this->initXenVars();
     $this->createDisk();
     $this->setSwapUsage();
     $this->createSwap();
     //$this->mount_this_guy();
     if (char_search_beg($this->__oldlocation, "lvm:")) {
         $vgname = fix_vgname($this->__oldlocation);
         $oldimage = "/dev/{$vgname}/{$this->main->maindiskname}";
     } else {
         $oldimage = "{$this->__oldlocation}/{$this->main->nname}/root.img";
     }
     if (lx_core_lock("{$name}.changelocation")) {
         throw new lxException("could_not_clone");
     }
     $ret = lxshell_return("dd", "if={$oldimage}", "of={$this->main->maindisk}");
     if ($ret) {
         throw new lxException("could_not_clone");
     }
     // Don't do this at all. The saved space is not going to be very important for the short period.
     //lunlink("$this->__oldlocation/{$this->main->nname}/root.img");
     /*
     if (char_search_beg($this->__oldlocation, "lvm:")) {
     	$vg = fix_vgname($this->__oldlocation);
     	lvm_remove("/dev/$vg/{$this->main->swapdiskname}");
     } else {
     	lunlink("$this->__oldlocation/{$this->main->nname}/vm.swap");
     }
     */
     $this->start();
     $ret = array("__syncv_corerootdir" => $this->main->newlocation, "__syncv_maindiskname" => $this->main->maindiskname, "__syncv_swapdiskname" => $this->main->swapdiskname);
     return $ret;
 }
Esempio n. 8
0
function main_system_lock()
{
    global $gbl, $sgbl, $login, $ghtml;
    // Not needed for hyperVM. HyperVM has more intelligent per vps locking.
    if ($sgbl->isHyperVm()) {
        return;
    }
    return;
    $lname = null;
    $nlname = $login->getClName();
    if ($nlname !== $lname && isModifyAction() && lx_core_lock($nlname)) {
        $ghtml->print_redirect_back('system_is_locked_by_u', '');
        exit;
    }
}
Esempio n. 9
0
function backup_main()
{
    global $argc, $argv;
    global $gbl, $sgbl, $login, $ghtml;
    $progname = $sgbl->__var_program_name;
    if ($argc === 1) {
        print "Usage: {$argv['0']} --class= --name= --v-backup_file_name= \n";
        exit;
    }
    //sleep(60);
    $opt = parse_opt($argv);
    $class = $opt['class'];
    $name = $opt['name'];
    $param = get_variable($opt);
    initProgram("admin");
    $object = new $class(null, 'localhost', $name);
    $object->get();
    if ($object->dbaction === 'add') {
        print "No objectc\n";
        exit;
    }
    if (!$object->isLxclient()) {
        print "This is not a backupable object... This object alone cannot be backed up\n";
        //exit;
    }
    $backup = $object->getObject('lxbackup');
    if (lx_core_lock("{$class}-{$name}.backup")) {
        exit;
    }
    /*
    	if (!testAllServersWithMessage()) {
    		mail($object->contactemail, "Backup Failed..", "Could not connect to slave servers.");
    		$backup->backupstage = "Failed due to: could not connect to slave servers";
    		clearLxbackup($backup);
    		exit;
    	}
    */
    if ($object->dbaction === 'add') {
        print "object {$name} doesn exist\n";
        $backup->backupstage = "Failed due to: {$name} doesn't exist";
        clearLxbackup($backup);
        log_error("Backup.php Client {$name} doesnt exist");
        exit;
    }
    if (isset($opt['priority']) && $opt['priority'] === 'low') {
        //sleep(20);
    }
    if ($opt['v-backup_file_name']) {
        $param['backup_to_file_f'] = $opt['v-backup_file_name'];
    } else {
        $param['backup_to_file_f'] = "{$progname}-scheduled";
    }
    foreach ($opt as $k => $v) {
        if (csb($k, "--v-backupextra_")) {
            $kk = strfrom($k, "--v-");
            $param[$kk] = $v;
        }
    }
    try {
        $backup->doupdateBackup($param);
        $backup->backupstage = 'done';
        print "Backup has been saved in {$sgbl->__path_program_home}/{$backup->getParentO()->get__table()}/{$backup->getParentO()->nname}/__backup/{$param['backup_to_file_f']}\n";
    } catch (exception $e) {
        $mess = "{$e->__full_message}\n";
        $backup->backupstage = "Failed due to: {$mess}";
        print "Backup failed due to: {$mess}\n";
        mail($object->contactemail, "Backup Failed..", "Backup Failed for {$object->nname} with the Message {$mess}");
    }
    clearLxbackup($backup);
}