US Tray Menu: Move Servers

From The Uniform Server Wiki
Revision as of 12:18, 31 May 2010 by Ric (talk | contribs) (New page: {{Nav US Tray Menu}} '''''US Tray Menu Move servers''''' == Introduction == At this stage our new menu still has move-servers disabled. To enable this function requires modifications to t...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

 

UniServer 5-Nano
US Tray Menu.

US Tray Menu Move servers

Introduction

At this stage our new menu still has move-servers disabled.

To enable this function requires modifications to the move-server script and control functions associated with it.

Earlier I stated no modification allowed to the control architecture hence script and functions require duplication in our new menu folder and modified accordingly.

This page covers modifications required to enable move-servers.

Preparation

Folder UniServer\unicon\move_servers was copied to tray_menu_2 and renamed move_servers2

Files were renamed as follows:

  • Run.bat to Run2.bat
  • move_servers.php to move_servers_2.php

Edit Run2.bat

Edit Run2.bat to run script move_servers_2.php

..\php.exe -n  move_servers_2.php

Top

Edit move_servers_2.php

Script move_servers_2.php requires several modification:

It needs to pick up functions and constants from main control. Also requires access to new functions and constants hence the includes section changes as follows:

include_once "../../main/includes/config.inc.php";
include_once "../../main/includes/functions.php";
include_once "../winbinder/winbinder.php";
include_once "../includes/config2.inc.php";
include_once "../includes/functions2.php";

Replace all occurrences of get_unitray_exe()with get_unitray2_exe()

Replace the "UPDATE UniTray" block with the following new code:

//=============== UPDATE UniTray ==========================

if (unitray2_running()){       // Is Unitray running
  stop_unitray2();             // yes: kill it
  $unitray_was_running =true;  // set tacker
}
else{                          // no: 
  $unitray_was_running =false; // reset tacker
}

 // Rename UniTray exe
 copy(US_UNICON_TRAY_MENU2."/".$UniTray_name_old, US_UNICON_TRAY_MENU2."/".$UniTray_name); // New file
 unlink(US_UNICON_TRAY_MENU2."/".$UniTray_name_old);                                    // Delete old

 //=== Update ini file
 $ini_file = US_UNICON_TRAY_MENU2."/UniTray.ini"; // Ini file path

 // SET new tray icon
 $s_str = "tray_image_".$old_id.".ico";       // Unitray ID
 $s_str = preg_quote($s_str,'/');             // Convert to regex format
 $s_str = '/'.$s_str.'/';                     // Create regex pattern
 $r_str = "tray_image_".$new_id.".ico";
 file_search_replace($ini_file,$s_str,$r_str); // Update file

if ($unitray_was_running){  // It was running
    start_unitray2();       // Restart with new values
}

That completes all modifications required. However running the script highlighted a number of problems. Top

Problems

Top

System error

Unable to find path, this was resolved by adding change chdir to function start_unitray2()

function start_unitray2(){

  $UniTray_exe = get_unitray2_exe();    // get program name  
  $cmd1 = "start $UniTray_exe";         // start program
  $cmd2 = ' -n ';                       // pass onfiguration file 
  $cmd3 = 'UniTray.phpw';               // pass script
  $cmd  = $cmd1 . $cmd2 . $cmd3  ;      // Build command

  $original_dir = getcwd();
  chdir(US_UNICON_TRAY_MENU2);
   pclose(popen($cmd,'r'));             // run and detatch
  chdir($original_dir);
}

Top

Access denied

With above mod script ran however when it came to unlink the old UniTray*.exe access was denied.

Problem, it takes time to kill a script. unlink occurred while the script was running hence the error message. Resolved by modifying function stop_unitray2()

function stop_unitray2(){

  $UniTray_exe = get_unitray2_exe();            // get program name  
  start_unitray2();                             // Forces min to max before killing
  sleep(2);                                     // Give it a chance to pop-up

  $cmd = USF_PSKILL_EXE." $UniTray_exe" .'  c';  // Create command c=kill command 
  exec($cmd,$dummy,$return);                     // run command to kill app 
  sleep(1);                                      // Give it a chance to pop-up
}

Top

Could not run application

At this point move-servers appears to work fine until it was run from UniTray.

It was enabled in the configuration file; lines un-commented and correct path added:

item[]      = "Move Servers multi-server operation"        
action[]    = "run"                                  
file[]      = "move_servers2/run2.bat"
parameter[] = ""   
icon[]      = "13" 

When run produced the following:

  • Warning: wbExec: Could not run application move_servers2/run2.bat

Strange since it runs other batch files for example "../key_cert_gen/Run.bat"!

The WinBinder function wb_exec() does not handle relative paths. The following path move_servers2/run2.bat is relative (relative to current folder) explaining why the batch file fails to run.

Reason why "../key_cert_gen/Run.bat" works is because all ../ are converted to absolute paths using function relative_to_absolute_paths($str).

Relative paths

  • ..// means move up one folder level
  • ./ means start or look in the current folder

Modifying this line as shown in the configuration file

  • file[] = "./move_servers2/run2.bat"

Allows it to be picked up as relative in our relative_to_absolute_paths($str) function.

Top

Modified relative to absolute paths function

New code as shown below:

//=== CONVERT RELATIVE TO ABSOLUTE PATHS ====================================== 
// Convers any relative paths found in a string to absolute paths
// Following are converted ../ move up one folder ./ current folder
// Assumes paths are relative to this file


function relative_to_absolute_paths($str){
$path_array  = explode("\\",dirname(__FILE__));           // Blow path appart at "\"
                                                          // Path is this file

$str = trim($str);                                        // Remove L&R spaces
$str = preg_replace('/\s+/', ' ', $str);                  // Remove double spaces
$str_array   = explode(" ",$str);                         // Blow string appart at " " 

//== Deal with ../ paths

for($i = 0; $i < count($str_array); $i++) {                // scan str array line by line
  if( strchr($str_array[$i],"../")){                       // Does string contain  ../
   $str_count = substr_count($str_array[$i],"../");        // Yes: How many
   $str_array[$i] = substr_replace($str_array[$i],"",0,$str_count*3); // Remove

   $new_path ="";                                             // reset new path        
   for($i2 = 0; $i2 < count($path_array)-$str_count; $i2++) { // scan original path 
     $new_path = $new_path.$path_array[$i2]."/";              // build new path
   }
   $str_array[$i] =  $new_path.$str_array[$i];          // Replace relative path
  }                                                     // with absolute path
}

$new_str="";
for($i = 0; $i < count($str_array); $i++) {             // scan str array line by line
 $new_str = $new_str. $str_array[$i]." ";               // Rebuild new string
}

//== New path has these removed ../ however it may contain ./
//Deal with ./

$current_path    = dirname(__FILE__);                         // Current path
$current_path_f  = preg_replace('/\\\/','/', $current_path);  // Replace \ with /

$str_array_2   = explode(" ",$new_str);                       // Blow string appart at " " 

for($i = 0; $i < count($str_array_2); $i++) {                   // scan str array line by line
  if( strchr($str_array[$i],"./")){                             // Does string contain  ./
    $str_array_2[$i] = substr_replace($str_array_2[$i],"",0,2); // Remove
    $str_array_2[$i] = $current_path_f.'/'.$str_array_2[$i];    // Add absolute path
 }
}

$new_str="";
for($i = 0; $i < count($str_array_2); $i++) {           // scan str array line by line
 $new_str = $new_str. $str_array_2[$i]." ";             // Rebuild new string
}

  return $new_str;                                      // Return modified string
}
//================================== END CONVERT RELATIVE TO ABSOLUTE PATHS ===

Top

Final Test

With all modification in place you will find move-servers runs correctly.

You may be unaware that running server-status uses server’s PHP core and its curl extension. Real point is architecture flexibility, server’s core PHP can be changed without the need to update WinBinder in UniTray.

An interesting comparison is size

  • Original tray menu 1.22 MB
  • New tray menu 1.32 MB

I was surprised! Difference is only 100K, a price worth paying for UTF-8 support alone.

However it packs a real punch, WinBinder combined with PHP core (908KB), offering real potential for further features.

Note: Full menu integration would offer (908KB - 56.0KB) saving.


Top

Summary

That concludes this tutorial. In terms of functionally our new menu reflects that of the original UniTray menu.

Our new menu has three real advantages

  • UTF-8 support
  • Full access, allows it to be modified with ease.
  • Windows interface capability.

Last two items are of real importance; they provide the ability for crating a user-friendlier interface. To be covered in a follow-up tutorial

Top