Using the SCInterface PHP API
From SCInterface Wiki
The following is an overview and introduction to the SCInterface PHP API. This page includes several API examples to assist with learning how to use the SCInterface API.
Contents |
Prerequisites
The following are the prerequisites for using the SCIntrface PHP API. Make sure all of these following steps are completed before continuing.
- SCInterface is fully installed and functional. Refer to the SCInterface Quick Start Guide.
- If the SCInterface web interface is fully functional, the following should be true:
- The PHP API is installed on your web server under sci_phpapi.
- api_config.php is configured to communicate with the SCM.
Using the PHP API in a script
These are some quick pointers for ensuring that you are using the SCInterface PHP API correctly:
- Copy the entire sci_phpapi directory from your working SCInterface installation to the location where you wish to make use of the SCInterface API. This can be on any PHP enabled web server.
- api_config.php should already be configured to connect into your SCM.
- Review the SCInterface PHP API. You will need this documentation to understand the syntax and format of calling SCInterface PHP functions from the API.
- Create a new PHP file that has the following information in it. At a minimum, you should configure the $adminUsername and $adminPassword with a user that has access to the system. We would recommend not using sysadmin as this is usually not a good practice.
//Include the SCInterface PHP API require_once(dirname(__FILE__)."/sci_phpapi/apifunctions.php"); //These parameters must be set based on the user that will be retrieving this information //Make sure that this user has permissions to actually retrieve this information $adminUsername = 'FILLMEIN'; $adminPassword = 'STRONGPASSWORD'; //Instantiate the API variable SCIapi from the SCInterface PHP API $SCIapi = new API($adminUsername, $adminPassword);
This assumes that the new PHP file that you have created is in the same folder as your sci_phpapi directory. The PHP file should not be within the sci_phpapi directory.
Example Script #1 - Platform List
The following script gets a list of available platforms and returns them in a list along with the SCInterface id number for that platform.
Example Script #1 - Code
To start:
- Read all of the code below and make sure that you understand it.
- Make sure that the PHP API is installed on a web server that supports PHP.
- Copy and paste the following code into a PHP file and save to the web server.
- Read all of the code again... there are some things you will need to change.
<?php
/* SCInterface API Example: Fetch a list of platforms available for this SCM
* Author: Chris Machut
* Date: 1/29/2007
*
* This is an example SCInterface application that makes use of the SCInterface
API.
* This application must be used in conjunction with an SCInterface installation
* that is configured correctly. For more information, please visit
* http://www.scinterface.com
*
*/
//Include the SCInterface PHP API
require_once(dirname(__FILE__)."/sci_phpapi/apifunctions.php");
//These parameters must be set based on the user that will be retrieving this in
formation
//Make sure that this user has permissions to actually retrieve this information
$adminUsername = 'FILLMEIN';
$adminPassword = 'STRONGPASSWORD';
//Instantiate the API variable SCIapi from the SCInterface PHP API
$SCIapi = new API($adminUsername, $adminPassword);
//Get a list of only those platforms that SCInterface supports
$search_for_app_type = 'SC';
$SCIapi->getSupportedAppTypes($search_for_app_type);
$SCIresults = $SCIapi->exec();
//Check for errors. Otherwise, continue
if(isset($SCIresults[0]->error)){
print("<br/>\nError returned by SCInterface: " . $results[0]->error . "<
br/>
\n<br/>\nOn Line: " . __LINE__ . "<br/>\n<br/>\nSCInterface Script Termi
nated.");
exit;
}
// fetching results for user and sconnector info queries
$appTypes = $SCIresults[0]->desc_array;
/*
* fun stuff: (for 0 <= $i < count($appTypes))
* $appTypes[$i]->id // appTypeID
* $appTypes[$i]->parent // this apptype's parent's ID
* $appTypes[$i]->name // this SConnector's name
*/
//Debug: print_r($appTypes);
$result = "The following application types are available from SCInterface:\n\n";
foreach ($appTypes as $appType) {
$result .= "SCInterface " . $search_for_app_type . ": " . $appType->name
.
" (SCInterface ID: " . $appType->id . ")\n";
}
print(nl2br($result));
print('<br/><br/><hr>This is an example of using the <a href="http://www.scinter
face.com"
title="SCInterface PHP API" target="_blank">SCInterface PHP API</a>.');
exit;
?>
Example Script #1 - Results
The following is an example of what would be returned by this script:
The following application types are available from SCInterface: SCInterface SC: BSD SC 32-bit (SCInterface ID: 86) SCInterface SC: Linux SC 32-bit (SCInterface ID: 85) SCInterface SC: Linux SC 64-bit (SCInterface ID: 459) SCInterface SC: Posix Scontroller (SCInterface ID: 460) SCInterface SC: Windows SC (SCInterface ID: 3576) -------------------------------------------------------------------------------- This is an example of using the SCInterface PHP API.
Example Script #2 - Add a user and SC
This is a much more complicated and detailed PHP script that does a number of things at once. The following script will see if the user exists. If the user does not exist, it will add the user. Otherwise, display an error message. After adding the user, an SC will be created. The SC will only be able to be created if:
- The admin that creates the user (defined as $adminUsername with the script) has enough SC Credits to assign to the new user.
- If the new user has enough SC Credits as well.
The user and the SC will have been successfully added if the words SUCCESS! appear.
Example Script #2 - Code
To start:
- Read all of the code below and make sure that you understand it.
- Make sure that the PHP API is installed on a web server that supports PHP.
- Copy and paste the following code into a PHP file and save to the web server.
- Read all of the code again... there are some things you will need to change.
<?php
/* SCInterface API Example: Create a new user and SC for the user
* Author: Chris Machut
* Date: 1/29/2007
*
* This is an example SCInterface application that makes use of the SCInterface API.
* This application must be used in conjunction with an SCInterface installation
* that is configured correctly. For more information, please visit
* http://www.scinterface.com
*
*/
//Include the SCInterface PHP API
require_once(dirname(__FILE__)."/sci_phpapi/apifunctions.php");
///////////////////
// FUNCTIONS
///////////////////
//Function to display an error message and exit the script gracefully.
function display_and_exit($text, $error_description, $line_number){
global $message;
print('<b>An Error Occurred:</b><br/><br/>' . $text . '<br/><br/>');
if(isset($error_description) && $error_description != ""){
print('Error returned by SCInterface: ' . $error_description . '<br/><br/>');
}
print('Error on Line: ' . $line_number . '<br/><br/>');
if(isset($message) && $message != ""){
print('<b>The following commands did complete successfully.</b> You may have to manually ' .
'remove them from within SCInterface before refreshing or re-adding these users.<br/><br/>' .
$message . '<br/><br/>');
}
print('SCInterface Script Terminated. Click \'Back\' in your web browser to try again.<br/><br/>');
print('<hr>This is an example of using the <a href="http://www.scinterface.com"
title="SCInterface PHP API" target="_blank">SCInterface PHP API</a>.</body></html>');
exit;
}
///////////////////
// MAIN
///////////////////
//These parameters must be set based on the user that will be retrieving this information
//Make sure that this user has permissions to actually retrieve this information
$adminUsername = 'FILLMEIN';
$adminPassword = 'SETMYPASSWORD';
//Instantiate the API variable SCIapi from the SCInterface PHP API
$SCIapi = new API($adminUsername, $adminPassword);
print('<html><head><title>SCInterface PHP API Example #2</title></head><body>');
if($adminUsername == "FILLMEIN"){
display_and_exit("Script Stopped! Do not run this script until you actually read all of the code and ' .
'understand what it is supposed to do.","",__LINE__);
}
///////////////////
// Display form or create user and SCs
///////////////////
if(isset($_POST['sciusername'])){
///////////////////
// Create user and SCs
///////////////////
//Do checks on all of the variables
if($_POST['sciusername'] == ""){
display_and_exit('Username must be defined before continuing.', '', __LINE__);
}
if($_POST['sciusername'] == ""){
display_and_exit('\'sciusername\' must be defined before continuing.', '', __LINE__);
}
if($_POST['sciuserpassword'] == ""){
display_and_exit('\'sciuserpassword\' must be defined before continuing.', '', __LINE__);
}
if($_POST['sciuserfirstname'] == ""){
display_and_exit('\'sciuserfirstname\' must be defined before continuing.', '', __LINE__);
}
if($_POST['sciuserlastname'] == ""){
display_and_exit('\'sciuserlastname\' must be defined before continuing.', '', __LINE__);
}
if($_POST['sciuseremail'] == ""){
display_and_exit('\'sciuseremail\' must be defined before continuing.', '', __LINE__);
}
if($_POST['sciusersccredits'] == ""){
display_and_exit('\'sciusersccredits\' must be defined before continuing.', '', __LINE__);
}
if($_POST['appTypeID'] == ""){
display_and_exit('\'appTypeID\' must be defined before continuing.', '', __LINE__);
}
if($_POST['sciscdescription'] == ""){
display_and_exit('\'sciscdescription\' must be defined before continuing.', '', __LINE__);
}
if($_POST['ip'] == ""){
display_and_exit('\'ip\' must be defined before continuing.', '', __LINE__);
}
//Check to see if this user exists yet
$SCIapi->getUserInfo($_POST['sciusername']);
$SCIresults = $SCIapi->exec();
//Only attempt to add the user if the user doesn't already exist
if(eregi("no user", $SCIresults[0]->error)){
//User doesn't already exist.
//First get the AdminID we are adding this user under.
$SCIapi->getUserInfo($adminUsername);
$SCIAdminresults = $SCIapi->exec();
if(eregi("no user", $SCIAdminresults[0]->error)){
display_and_exit('There was an error adding your user \'' . $_POST['sciusername'] .
'\' because the admin user \'' . $adminUsername . '\' does not exist.',
$SCIresults[0]->error, __LINE__);
}
$adminUserID = $SCIAdminresults[0]->desc_array[0]->userID;
//Add them to SCI
$SCIapi->addUser($_POST['sciusername'], $_POST['sciuserfirstname'], $_POST['sciuserlastname'] .
" (Created by SCInterface Example 2 API Script)", $_POST['sciuserpassword'],
$_POST['sciuseremail'], '', '','', '', $adminUserID);
$SCIresults = $SCIapi->exec();
if(isset($SCIresults[0]->error)){
display_and_exit('There was an error adding your user \'' .
$_POST['sciusername'] . '\'.', $SCIresults[0]->error, __LINE__);
}
//Get the SCInterface user ID number to user later.
$SCUserID = $SCIresults[0]->desc;
$message .= "<LI>Your user '" . $_POST['sciusername'] . "' (" . $SCUserID .
") has been added into SCInterface.</LI><br/>";
$SCIapi->modifyUserSCcredit($SCUserID, $_POST['sciusersccredits']);
$SCIresults = $SCIapi->exec();
if(isset($SCIresults[0]->error)){
//This is the error message that *should* be displayed here
display_and_exit('There was an error adding SC Credits to user \'' . $_POST['sciusername'] .
'\'.', $SCIresults[0]->error, __LINE__);
}
} else {
//User exists. Get their ID number
$SCUserID = $SCIresults[0]->desc_array[0]->userID;
//Make sure they have at least one credit
$SCIapi->getUserSCCredits($SCUserID);
$SCIresults = $SCIapi->exec();
if(isset($SCIresults[0]->error)){
display_and_exit('There was an error detecting SC Credits to user \'' .
$_POST['sciusername'] . '\'.', $SCIresults[0]->error, __LINE__);
}
$message .= '<LI>User \'' . $_POST['sciusername'] . '\' already exists. The user will not be added
again.</LI><br/>';
}
//Add the SC for this user.
$SCIapi->addSC($_POST['ip'], 0, 51025, $_POST['sciscdescription'] .
" (Created by SCInterface Example 2 API Script)", 10, "kasnd23jnsa" . rand(),
0, $SCUserID, 0, $_POST['appTypeID'], 300);
$SCIresults = $SCIapi->exec();
if(isset($SCIresults[0]->error)){
display_and_exit('There was an error when trying to add your SC into SCInterface with
the IP address or hostname of ' . $_POST['ip'] . ' to user \'' .
$_POST['sciusername'] . '\'.', $SCIresults[0]->error, __LINE__);
} elseif($SCIresults[0]->retcode != 0){
display_and_exit('There was an unknown return code when trying to add your SC into SCInterface ' .
'with the IP address or hostname of ' .
$_POST['ip'] . ' to user \'' .
$_POST['sciusername'] . '\'.', $SCIresults[0]->retcode, __LINE__);
} else {
$message .= '<LI>Your SC has been activated for use with the IP address or hostname \'' .
$_POST['ip'] . '\' and assigned to user \'' . $_POST['sciusername'] . '\'.</LI>';
}
print('<b>SUCCESS!</b> Do not click refresh or another SC will be created with the same credentials. ' .
'You should put in checks for these kind of things within your own program. Here is what was completed ' .
'successfully:<br/><br/>');
print($message);
} else {
///////////////////
// Display create new user form and SCs
///////////////////
print('<b>SCInterface New SC and User Info</b><br/>');
print('Note: Great care should be taken before invoking this SCInterfacescript. This script should never
be used within a production environment. This script will:<br/>
<LI>Create a User only if it doesn\'t already exist</LI>
<LI>Assign <a href="http://wiki.scinterface.com/index.php/SC_Credit" target="_blank">SC Credits</a> to
the user (if available from the administrator \'$adminUsername\' (defined within this script).</LI>
<LI>Create an <a href="http://wiki.scinterface.com/index.php/SC" target="_blank">SC</a> and assign it
to this user</LI><br/><br/>
Use at your own risk.<br/><br/>');
print('<hr>');
print('Fill in all of the following fields with the information requested.<br/><br/>');
print('<b>User Info</b><br/>');
print('<form id="newuserform" name="newuserform" method="post" action="' . $_SERVER['PHP_SELF'] . '">');
print('Username: <input name="sciusername" value="">');
print('<br/>');
print('Password: <input name="sciuserpassword" value="">');
print('<br/>');
print('First Name: <input name="sciuserfirstname" value="">');
print('<br/>');
print('Last Name: <input name="sciuserlastname" value="">');
print('<br/>');
print('Email: <input name="sciuseremail" value="">');
print('<br/>');
print('Number of <a href="http://wiki.scinterface.com/index.php/SC_Credit" target="_blank">SC Credits</a>
for this user: <select name="sciusersccredits">');
print('\t<option value="">Unknown</option>');
for($i = 1; $i < 100; $i++){
print('<option value="' . $i . '">' . $i . '</option>');
}
print('</select>');
print('<br/><br/>');
print('<b>SC Info</b><br/>');
//Get a list of only those platforms that SCInterface supports
$search_for_app_type = 'SC';
$SCIapi->getSupportedAppTypes($search_for_app_type);
$SCIresults = $SCIapi->exec();
//Check for errors. Otherwise, continue
if(isset($SCIresults[0]->error)){
display_and_exit('Unable to get a list of application types.', $SCIresults[0]->error, __LINE__);
}
//Fetching results for user and sconnector info queries
$appTypes = $SCIresults[0]->desc_array;
/*
* fun stuff: (for 0 <= $i < count($appTypes))
* $appTypes[$i]->id // appTypeID
* $appTypes[$i]->parent // this apptype's parent's ID
* $appTypes[$i]->name // this SConnector's name
*/
//Debug: print_r($appTypes);
print('Choose an SC Type: <select name="appTypeID">');
print('\t<option value="">Unknown</option>');
foreach ($appTypes as $appType) {
print('\t<option value="' . $appType->id . '">' . $appType->name . '</option>');
}
print('</select>');
print('<br/>');
print('<a href="http://wiki.scinterface.com/index.php/SC" target="_blank">SC</a> Description Name:
<input name="sciscdescription" value="">');
print('<br/>');
print('<a href="http://wiki.scinterface.com/index.php/SC" target="_blank">SC</a> IP Address:
<input name="ip" value=""> Format: 123.123.123.123');
print('<br/><br/>');
print('<input type="submit" name="Create User and SC" value="Create User and SC">');
print('</form>');
}
//Default footer
print('<br/><br/><hr>This is an example of using the <a href="http://www.scinterface.com"
title="SCInterface PHP API" target="_blank">SCInterface PHP API</a>.</body></html>');
exit;
?>
Example Script #2 - Results
The following is an example of the PHP script running successfully with the fields filled in with sample data.
If successful, the following text will appear:
SUCCESS! Do not click refresh or another SC will be created with the same credentials. You should put in checks for these kind of things within your own program. Here is what was completed successfully: Your user 'test1' (74152) has been added into SCInterface. Your SC has been activated for use with the IP address or hostname '23.123.123.123' and assigned to user 'test1'. -------------------------------------------------------------------------------- This is an example of using the SCInterface PHP API.
Example Script #3 - Deploying Multiple Applications
The purpose of this script is to provide an example script to install and configure multiple servers on an SCM automatically with a single click of a button. This script is designed to be the 'start' script for an SConnector - lets call it Deploy My Server - that would deploy a particular application - called My Server - across multiples SCs.
Example Script #3 - Code
To start:
- The applications are already defined within the SCM on the SCs for a given SConnector. They do not have to be installed.
- This script is packaged up with the SCInterface PHP API.
- The variables below are configured.
- Read all of the code again... there are some things you will need to change.
#!/usr/bin/php
<?php
//Title: Deploy servers
//Author: Chris Machut, Netarus, LLC
//Date: January 23, 2008
//
// The purpose of this script is to provide an example script to install and configure
// multiple servers on an SCM automatically with a single click of a button.
// This script is designed to be the 'start' script for an SConnector - lets call it
// Deploy My Server - that would deploy a particular application - called My Server -
// across multiples SCs.
//
// Prerequisites:
// 1) The applications are already defined within the SCM on the SCs. They do not
// have to be installed
// 2) This script is packaged up with the SCInterface PHP API.
// 3) The variables below are configured.
//
// The content of this script is the property of Netarus, LLC
//////////////////////////////
//VARIABLES
//////////////////////////////
//command line:
//These parameters must be set based on the user that will be retrieving this information
//Make sure that this user has permissions to actually retrieve this information
//It is highly recommended NOT to use the sysadmin username and password if you don't need to.
$adminUsername = 'sysadmin';
$adminPassword = 'password';
//Configurable variables
$version = "20080402"; //Version of this script
$search_for_app_type = "SConnector name"; //The SConnector for each of the applications that we'll be deploying
$debugenabled = false; //Enable/disable additional debug information for this script
//These variables will need to be defined/edited within the SConnector.
//Some PHP code *will* have to be changed to accomodate these values for your given SConnector.
$servername = "My Server Name"; //SConnector value #1 - The base servername for each application
$default_startup_map = "Custom Config - Default Map"; //SConnector value #2
$datacenter = "Custom Config - IP Address"; //SConnector value #3
$mapcycle = "Custom Config - 2"; //SConnector value #4
$numSlots = "32"; //SConnector value #5
//These probably don't need to change; These are the scripts that will be called to install, start, stop
//and get the status of the applications during installation.
$installer_macro = "Install";
$start_macro = "start";
$stop_macro = "stop";
$status_macro = "status";
//Do not change these:
$installer_id = 0;
$stop_id = 0;
$start_id = 0;
$status_id = 0;
$appTypeID = 0;
$display_servers = array();
//Include the SCInterface PHP API
require_once(dirname(__FILE__)."/sci_phpapi/apifunctions.php");
require_once(dirname(__FILE__)."/logic/util.php");
//////////////////////////////
//FUNCTIONS
//////////////////////////////
function exitapp($message, $line=""){
global $debugenabled, $debug_messages;
for($i = 0; $i < 1; $i++){
print("(" . $i . ") Script failed due to: " . $message . " (Line: " . $line . ").\n");
print("Press the stop button within SCInterface to try again!\n\n");
sleep(3);
}
if($debugenabled){
print("API DEBUG Messages = " . $debug_messages . "\n\n");
}
exit(1);
}
function wait($message="", $line=""){
global $debugenabled, $debug_messages;
for($i = 0; $i < 10; $i++){
print("waiting(" . $i . ")...\n");
sleep(1);
}
if($debugenabled){
print("API DEBUG Messages = " . $debug_messages . "\n\n");
}
exit(0);
}
function printdebug($message, $line=""){
global $debugenabled;
if($debugenabled){
print("\n------------------\nStart DEBUG => ");
if($line != ""){
print("Line (" . $line . ") ");
}
print("\n\n");
print_r($message);
print("\n\nEnd DEBUG");
if($line != ""){
print(" Line (" . $line . ") ");
}
print("\n------------------\n");
}
}
function printtext($message, $line=""){
print("(" . $line . ") - ");
print_r($message);
print("\n");
}
function sleep_loop($seconds){
global $api, $version;
$api->disconnect();
$sleep_for = $seconds;
print("\n\n(Version " . $version . ") Sleeping for " . $sleep_for . " seconds...");
for($i=1; $i <= $sleep_for; $i++){
print($i . "...");
sleep(1);
}
print("done.\n\n");
}
//////////////////////////////
//MAIN
//////////////////////////////
//Instantiate the API variable SCIapi from the SCInterface PHP API
printtext("Initializing (Version " . $version . ")...", __LINE__);
global $mySCM;
$api = new API($adminUsername, $adminPassword);
//Get a list of only those platforms that SCInterface supports
printtext("Getting application type ID...", __LINE__);
$api->getSupportedAppTypes($search_for_app_type);
$SCIresults = $api->exec();
//Check for errors. Otherwise, continue
if(isset($SCIresults[0]->error)){
exitapp("Error returned by SCInterface: " . $results[0]->error, __LINE__);
}
// Get the SConnector ID for this application type.
$appTypes = $SCIresults[0]->desc_array;
/*
* fun stuff: (for 0 <= $i < count($appTypes))
* $appTypes[$i]->id // appTypeID
* $appTypes[$i]->parent // this apptype's parent's ID
* $appTypes[$i]->name // this SConnector's name
*/
$appTypeID = "";
foreach ($appTypes as $appType) {
if($search_for_app_type == $appType->name){
$appTypeID = $appType->id;
}
}
if($appTypeID == ""){
exitapp("appTypeID could not be retrieved from the server", __LINE__);
}
printtext("The " . $search_for_app_type . " appTypeID = [" . $appTypeID . "]....", __LINE__);
//Get the application IDs and get a list of the servers for this apptype
printtext("Getting a list of available " . $search_for_app_type ." servers ....", __LINE__);
$api->getServers(0, 0, 0, 1);
$SCIresults = $api->exec();
$servers = $SCIresults[0];
//Check for errors. Otherwise, continue
if(isset($SCIresults[0]->error)){
exitapp("Error returned by SCInterface: " . $results[0]->error, __LINE__);
}
printdebug($servers, __LINE__);
foreach ($servers->desc_array as $key=>$value){
if (strcmp($servers->desc_array[$key]->appTypeID, $appTypeID)==0){
$display_servers[] = $value;
}
}
if(sizeof($display_servers) < 1){
exitapp("The SCM did not return any servers of the apptype " . $search_for_app_type . " (" . $appTypeID. ").", __LINE__);
}
printdebug($display_servers, __LINE__);
//Get the install macro ID for one of these application types
printtext("Getting the installer ID for " . $search_for_app_type ." servers ....", __LINE__);
$api->getSupportedMacrosByAppType($appTypeID);
$SCIresults = $api->exec();
$supported_macros = $SCIresults[0];
//printdebug($supported_macros, __LINE__);
//Check for errors. Otherwise, continue
if(isset($SCIresults[0]->error)){
exitapp("Error returned by SCInterface: " . $results[0]->error, __LINE__);
}
foreach($supported_macros->desc_array as $key=>$value){
//printdebug("\$value->macroName = " . $value->macroName, __LINE__);
if (strcmp($value->macroName, $installer_macro)==0){
$installer_id = $value->macroID;
}
}
if($installer_id <= 0){
exitapp("The installer ID (value returned by the SCM = " . $installer_id . ") could not be retrieved.", __LINE__);
}
printdebug("The installer ID for " . $search_for_app_type . " [" . $appTypeID . "] = [" . $installer_id . "].\n", __LINE__);
//Get the start macro ID for one of these application types
printtext("Getting the start ID for " . $search_for_app_type ." servers ....", __LINE__);
foreach($supported_macros->desc_array as $key=>$value){
//printdebug("\$value->macroName = " . $value->macroName, __LINE__);
if (strcmp($value->macroName, $start_macro)==0){
$start_id = $value->macroID;
}
}
if($start_id <= 0){
exitapp("The start ID (value returned by the SCM = " . $start_id . ") could not be retrieved.", __LINE__);
}
printdebug("The start ID for " . $search_for_app_type . " [" . $appTypeID . "] = [" . $start_id . "].\n", __LINE__);
//Get the stop macro ID for one of these application types
printtext("Getting the stop ID for " . $search_for_app_type ." servers ....", __LINE__);
foreach($supported_macros->desc_array as $key=>$value){
//printdebug("\$value->macroName = " . $value->macroName, __LINE__);
if (strcmp($value->macroName, $stop_macro)==0){
$stop_id = $value->macroID;
}
}
if($stop_id <= 0){
exitapp("The stop ID (value returned by the SCM = " . $stop_id . ") could not be retrieved.", __LINE__);
}
printdebug("The stop ID for " . $search_for_app_type . " [" . $appTypeID . "] = [" . $stop_id . "].\n", __LINE__);
//Get the status macro ID for one of these application types
printtext("Getting the status ID for " . $search_for_app_type ." servers ....", __LINE__);
foreach($supported_macros->desc_array as $key=>$value){
//printdebug("\$value->macroName = " . $value->macroName, __LINE__);
if (strcmp($value->macroName, $status_macro)==0){
$status_id = $value->macroID;
}
}
if($status_id <= 0){
exitapp("The status ID (value returned by the SCM = " . $status_id . ") could not be retrieved.", __LINE__);
}
printdebug("The status ID for " . $search_for_app_type . " [" . $appTypeID . "] = [" . $status_id . "].\n", __LINE__);
///////////////////////
//Stopping
//Loop through each of the applications
//Stop the applications
printtext("Stopping the " . sizeof($display_servers) . " " . $search_for_app_type ." servers ....", __LINE__);
$paramList = array();
foreach($display_servers as $server){
$api->runMacro($stop_id, $server->appID, $paramList);
}
$SCIresults = $api->exec();
if(isset($SCIresults->error)){
printtext("A " . $search_for_app_type ." server could not be stopped: " . $SCIresults->error, __LINE__);
}
//Check for errors. Otherwise, continue
foreach($SCIresults as $serverresults){
printdebug($serverresults->desc_array[0]->line_0);
}
///////////////////////
//Status
$api->clear();
printtext("Getting the status for the " . sizeof($display_servers) . " " . $search_for_app_type ." servers ....", __LINE__);
sleep_loop(10);
$stop = false;
while(!$stop){
$paramList = array();
foreach($display_servers as $server){
$api->serverStatus($server->appID);
//$api->runMacro($status_id, $server->appID, $paramList);
}
printdebug($api, __LINE__);
$SCIresults = $api->exec();
printdebug($SCIresults, __LINE__);
if(isset($SCIresults[0]->error)){
printtext("WARNING: The status of all of the " . $search_for_app_type ." server could not be retrieved: " . $SCIresults[0]->error, __LINE__);
sleep_loop(30);
exitapp("Exiting...", __LINE__);
}
$server_iterator = 0;
//Only remove the stop == true if all servers have stopped installing.
$stop = true;
foreach($SCIresults as $serverresults){
printdebug($serverresults, __LINE__);
print($server_iterator . ") " . $search_for_app_type . " " . $display_servers[$server_iterator]->serverName . " ID(" . $display_servers[$server_iterator]->appID. ")...\n ");
printdebug($serverresults->desc_array[0]->output, __LINE__);
if($serverresults->desc_array[0]->output == 0){
print("Server Started!");
}
if($serverresults->desc_array[0]->output == 1){
print("Server Stopped!");
}
if($serverresults->desc_array[0]->output == 3){
print("Server Executable could not be located!");
}
if($serverresults->desc_array[0]->output == 4){
print("Installing!");
$stop = false;
}
print("\n");
if(isset($serverresults->error)){
printtext("A " . $search_for_app_type ." server's status could not be retrieved: " . $serverresults->error, __LINE__);
}
$server_iterator++;
}
if(!$stop){
printtext("The Installer is currently running on other servers...please wait while checking again.\n\n", __LINE__);
sleep_loop(240);
} else {
printtext("No installers seem to be running....preparing to start the installation process...\n\n", __LINE__);
sleep_loop(10);
}
}
///////////////////////
//Installing - If we get here, there are no installers running on the pod.
$api->clear();
printtext("Spawning the installer for the " . sizeof($display_servers) . " " . $search_for_app_type ." servers ....", __LINE__);
$paramList = array();
$paramList['force'] = "yes";
$paramList['viewed_license'] = "yes";
$paramList['installLockFile'] = "install_lock";
foreach($display_servers as $value){
printdebug("installer -> \$value->appID = [". $value->appID. "]", __LINE__);
$api->runMacro($installer_id, $value->appID, $paramList);
}
$SCIresults = $api->exec();
if(isset($SCIresults[0]->error)){
printtext("The " . $search_for_app_type ." server could not installed: " . $SCIresults[0]->error, __LINE__);
}
//Check for errors. Otherwise, continue
foreach($SCIresults as $serverresults){
printdebug($serverresults->desc_array[0]);
}
///////////////////////
//Set Startup Parameters
$api->clear();
printtext("Setting the startup parameters for the " . sizeof($display_servers) . " " . $search_for_app_type ." servers ....", __LINE__);
$paramList = array();
$paramList['Set Server Mode'] = "server";
$paramList['Startup Map'] = $default_startup_map;
$paramList['Seek Free'] = "-SeekFreePackageMap";
$paramList['Xbox'] = "-xlsp";
$paramList['Console'] = "-ConsoleControl";
$paramList['Unattended'] = "-Unattended";
printtext($paramList);
foreach($display_servers as $value){
printdebug("installer -> \$value->appID = [". $value->appID. "]", __LINE__);
$api->setParameters($value->appID, $paramList);
}
$SCIresults = $api->exec();
if(isset($SCIresults[0]->error)){
printtext("A " . $search_for_app_type ." server's startup parameters could not be configured: " . $SCIresults[0]->error, __LINE__);
}
printdebug($SCIresults);
//Check for errors. Otherwise, continue
foreach($SCIresults as $serverresults){
}
///////////////////////
//Set Configuration Files
$api->clear();
printtext("Setting the configuration file parameters for the " . sizeof($display_servers) . " " . $search_for_app_type ." servers ....", __LINE__);
$paramList = array();
$paramList['Data Center']['datacenter'] = $datacenter;
$paramList['Map Cycle']['mapcycle'] = $mapcycle;
$paramList['Number of Slots']['numSlots'] = $numSlots;
$paramList['Server Name']['servername'] = "not set yet";
$servername_iterator=2;
foreach($display_servers as $value1){
$servername_iterator++;
$paramList['Server Name']['servername'] = $servername . $servername_iterator;
printtext($paramList['Server Name']['servername'], __LINE__);
foreach ($paramList as $ac_name => $params) {
$api->runAlterConfig ($value1->appID, $ac_name, $params, 0);
}
}
$SCIresults = $api->exec();
if(isset($SCIresults[0]->error)){
printtext("A " . $search_for_app_type ." server's configuration file parameters could not be configured: " . $SCIresults[0]->error, __LINE__);
}
printdebug($SCIresults);
//Check for errors. Otherwise, continue
foreach($SCIresults as $serverresults){
}
///////////////////////
//Sleep for 2 minutes
printtext("Preparing to check the status of the installation on the servers...", __LINE__);
sleep_loop(120);
///////////////////////
//Get the Status once more
$api->clear();
printtext("Getting the status for the " . sizeof($display_servers) . " " . $search_for_app_type ." servers ....", __LINE__);
$stop = false;
while(!$stop){
$paramList = array();
foreach($display_servers as $server){
$api->serverStatus($server->appID);
//$api->runMacro($status_id, $server->appID, $paramList);
}
$SCIresults = $api->exec();
if(isset($SCIresults[0]->error)){
printtext("WARNING: The status of all of the " . $search_for_app_type ." server could not be retrieved: " . $SCIresults[0]->error, __LINE__);
}
$server_iterator = 0;
//Only remove the stop == true if all servers have stopped installing.
$stop = true;
foreach($SCIresults as $serverresults){
//printdebug($serverresults);
print($server_iterator . ") " . $search_for_app_type . " " . $display_servers[$server_iterator]->serverName . " ID(" . $display_servers[$server_iterator]->appID. ")...\n ");
printdebug($serverresults->desc_array[0]->output);
if($serverresults->desc_array[0]->output == 0){
print("Server Started!");
}
if($serverresults->desc_array[0]->output == 1){
print("Server Stopped! Trying to start " . $display_servers[$server_iterator]->serverName . " (" . $display_servers[$server_iterator]->appID . ")...");
$paramList = array();
$api->runMacro($start_id, $display_servers[$server_iterator]->appID, $paramList);
$SCIresults = $api->exec();
if(isset($SCIresults[0]->error)){
printtext("WARNING: The " . $search_for_app_type ." server " . $display_servers[$server_iterator]->serverName . " ID(" . $display_servers[$server_iterator]->appID. ") could not be started: " . $SCIresults[0]->error, __LINE__);
}
//Check for errors. Otherwise, continue
printdebug($SCIresults);
foreach($SCIresults as $serverresults_started){
printdebug($serverresults_started->desc_array[0]->line_0);
}
}
if($serverresults->desc_array[0]->output == 3){
print("Server Executable could not be located!");
}
if($serverresults->desc_array[0]->output == 4){
print("Installing!");
$stop = false;
}
print("\n");
if(isset($serverresults->error)){
printtext("A " . $search_for_app_type ." server's status could not be retrieved: " . $serverresults->error, __LINE__);
}
$server_iterator++;
}
if($stop){
printtext("Notice: All installers have finished!", __LINE__);
}
sleep_loop(240);
}
wait();
?>


