function openqrm_linuxcoe_appliance($cmd, $appliance_fields)
{
    global $event;
    global $OPENQRM_SERVER_BASE_DIR;
    global $OPENQRM_SERVER_IP_ADDRESS;
    global $OPENQRM_EXEC_PORT;
    $openqrm_server = new openqrm_server();
    $appliance_id = $appliance_fields["appliance_id"];
    $appliance_name = $appliance_fields["appliance_name"];
    $resource = new resource();
    $resource->get_instance_by_id($appliance_fields["appliance_resources"]);
    $resource_mac = $resource->mac;
    $resource_ip = $resource->ip;
    $appliance = new appliance();
    $appliance->get_instance_by_id($appliance_id);
    $linuxcoe_install_timeout = 240;
    // check appliance values, maybe we are in update and they are incomplete
    if ($appliance->imageid == 1) {
        return;
    }
    if ($resource->id == "-1" || $resource->id == "") {
        return;
    }
    // check if image is type linuxcoe-deployment
    $image = new image();
    $image->get_instance_by_id($appliance->imageid);
    // linuxcoe configured in image deployment parameters ?
    $linuxcoe_auto_install_enabled = false;
    $linuxcoe_deployment_parameters = trim($image->get_deployment_parameter("INSTALL_CONFIG"));
    if (strlen($linuxcoe_deployment_parameters)) {
        $linuxcoe_deployment_parameter_arr = explode(":", $linuxcoe_deployment_parameters);
        $local_deployment_persistent = $linuxcoe_deployment_parameter_arr[0];
        $local_deployment_type = $linuxcoe_deployment_parameter_arr[1];
        if (strcmp($local_deployment_type, "linuxcoe-deployment")) {
            $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Appliance " . $appliance_id . "/" . $appliance_name . " image is not from type linuxcoe-deployment", "", "", 0, 0, $resource->id);
            return;
        }
        $linuxcoe_server_storage_id = $linuxcoe_deployment_parameter_arr[2];
        $linuxcoe_install_profile = $linuxcoe_deployment_parameter_arr[3];
        $linuxcoe_auto_install_enabled = true;
    }
    $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Handling {$cmd} event {$appliance_id}/{$appliance_name}/{$resource_ip}/{$resource_mac}", "", "", 0, 0, $resource->id);
    switch ($cmd) {
        case "start":
            $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "START event {$appliance_id}/{$appliance_name}/{$resource_ip}/{$resource_mac}", "", "", 0, 0, $resource->id);
            if ($linuxcoe_auto_install_enabled) {
                // prepare automatic-installation / transfer client to linuxcoe server
                $linuxcoe_command = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/linuxcoe/bin/openqrm-linuxcoe-manager transfer_to_linuxcoe -x " . $resource->id . " -i " . $resource_ip . " -m " . $resource_mac . " -n " . $linuxcoe_install_profile . " --openqrm-cmd-mode background";
                $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "transfer_to_linuxcoe {$resource_ip} / {$linuxcoe_install_profile}", "", "", 0, 0, $resource->id);
                $openqrm_server->send_command($linuxcoe_command, NULL, true);
                // Remove image-deployment paramters, if auto-install is a single-shot actions
                if (!strcmp($local_deployment_persistent, "0")) {
                    $image->set_deployment_parameters("INSTALL_CONFIG", "");
                }
                // create linuxcoestate-state object to allow to run a late setboot to local command on the vm host
                $linuxcoe_state = new linuxcoestate();
                $linuxcoe_state->remove_by_resource_id($resource->id);
                $linuxcoe_state_fields = array();
                $linuxcoe_state_fields["linuxcoe_id"] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
                $linuxcoe_state_fields["linuxcoe_resource_id"] = $resource->id;
                $linuxcoe_state_fields["linuxcoe_install_start"] = $_SERVER['REQUEST_TIME'];
                $linuxcoe_state_fields["linuxcoe_timeout"] = $linuxcoe_install_timeout;
                $linuxcoe_state->add($linuxcoe_state_fields);
            } else {
                if (strcmp($image->type, "linuxcoe-deployment")) {
                    $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Appliance {$appliance_id}/{$appliance_name} image is not from type linuxcoe-deployment", "", "", 0, 0, $resource->id);
                } else {
                    // we have auto-installed already, if it is VM the linuxcoeresource object will care to set the boot-sequence on the VM Host to local boot
                    $linuxcoeresource = new linuxcoeresource();
                    $linuxcoeresource->set_boot($resource->id, 1);
                    // set pxe config to local-boot
                    $event->log("openqrm_linuxcoe_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Setting resource {$resource_ip} to local-boot", "", "", 0, 0, $resource->id);
                    $linuxcoe_command = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/linuxcoe/bin/openqrm-linuxcoe-manager set_linuxcoe_client_to_local_boot -m " . $resource_mac . " --openqrm-cmd-mode background";
                    $openqrm_server->send_command($linuxcoe_command, NULL, true);
                }
            }
            break;
        case "stop":
            if (strcmp($image->type, "linuxcoe-deployment")) {
                $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Appliance {$appliance_id}/{$appliance_name} image is not from type linuxcoe-deployment", "", "", 0, 0, $resource->id);
            } else {
                $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "openqrm-linuxcoe-appliance-hook.php", "Stop event for appliance " . $appliance_id . "/" . $appliance->name . ".", "", "", 0, 0, $resource->id);
                // remove linuxcoestate-state object if existing
                $local_storage_state = new linuxcoestate();
                $local_storage_state->remove_by_resource_id($resource->id);
                // if it is VM the linuxcoeresource object will care to set the boot-sequence on the VM Host to network boot
                $linuxcoeresource = new linuxcoeresource();
                $linuxcoeresource->set_boot($resource->id, 0);
            }
            break;
    }
}
function storage_auth_deployment_stop($image_id)
{
    global $event;
    global $OPENQRM_SERVER_BASE_DIR;
    global $OPENQRM_SERVER_IP_ADDRESS;
    global $OPENQRM_EXEC_PORT;
    $image = new image();
    $image->get_instance_by_id($image_id);
    $image_name = $image->name;
    $image_rootdevice = $image->rootdevice;
    $storage = new storage();
    $storage->get_instance_by_id($image->storageid);
    $storage_resource = new resource();
    $storage_resource->get_instance_by_id($storage->resource_id);
    $storage_ip = $storage_resource->ip;
    $deployment = new deployment();
    $deployment->get_instance_by_type($image->type);
    $deployment_type = $deployment->type;
    $deployment_plugin_name = $deployment->storagetype;
    // just for sending the commands
    $resource = new resource();
    // get install deployment params
    $install_from_nfs_param = trim($image->get_deployment_parameter("IMAGE_INSTALL_FROM_NFS"));
    if (strlen($install_from_nfs_param)) {
        // storage -> resource -> auth
        $ip_storage_id = $deployment->parse_deployment_parameter("id", $install_from_nfs_param);
        $ip_storage_ip = $deployment->parse_deployment_parameter("ip", $install_from_nfs_param);
        $ip_image_rootdevice = $deployment->parse_deployment_parameter("path", $install_from_nfs_param);
        $ip_storage = new storage();
        $ip_storage->get_instance_by_id($ip_storage_id);
        $ip_storage_resource = new resource();
        $ip_storage_resource->get_instance_by_id($ip_storage->resource_id);
        $op_storage_ip = $ip_storage_resource->ip;
        $ip_deployment = new deployment();
        $ip_deployment->get_instance_by_id($ip_storage->type);
        $ip_deployment_type = $ip_deployment->type;
        $ip_deployment_plugin_name = $ip_deployment->storagetype;
        $event->log("storage_auth_function", $_SERVER['REQUEST_TIME'], 5, "openqrm-lvm-nfs-deployment-auth-hook.php", "Install-from-NFS: Authenticating {$resource_ip} on storage id {$ip_storage_id}:{$ip_storage_ip}:{$ip_image_rootdevice}", "", "", 0, 0, $resource_id);
        $auth_install_from_nfs_start_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/" . $ip_deployment_plugin_name . "/bin/openqrm-" . $ip_deployment_plugin_name . " auth -r " . $ip_image_rootdevice . " -i " . $OPENQRM_SERVER_IP_ADDRESS . " -t " . $ip_deployment_type . " --openqrm-cmd-mode background";
        $resource->send_command($ip_storage_ip, $auth_install_from_nfs_start_cmd);
    }
    // get transfer deployment params
    $transfer_from_nfs_param = trim($image->get_deployment_parameter("IMAGE_TRANSFER_TO_NFS"));
    if (strlen($transfer_from_nfs_param)) {
        // storage -> resource -> auth
        $tp_storage_id = $deployment->parse_deployment_parameter("id", $transfer_from_nfs_param);
        $tp_storage_ip = $deployment->parse_deployment_parameter("ip", $transfer_from_nfs_param);
        $tp_image_rootdevice = $deployment->parse_deployment_parameter("path", $transfer_from_nfs_param);
        $tp_storage = new storage();
        $tp_storage->get_instance_by_id($tp_storage_id);
        $tp_storage_resource = new resource();
        $tp_storage_resource->get_instance_by_id($tp_storage->resource_id);
        $op_storage_ip = $tp_storage_resource->ip;
        $tp_deployment = new deployment();
        $tp_deployment->get_instance_by_id($tp_storage->type);
        $tp_deployment_type = $tp_deployment->type;
        $tp_deployment_plugin_name = $tp_deployment->storagetype;
        $event->log("storage_auth_function", $_SERVER['REQUEST_TIME'], 5, "openqrm-lvm-nfs-deployment-auth-hook.php", "Install-from-NFS: Authenticating {$resource_ip} on storage id {$tp_storage_id}:{$tp_storage_ip}:{$tp_image_rootdevice}", "", "", 0, 0, $resource_id);
        $auth_install_from_nfs_start_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/" . $tp_deployment_plugin_name . "/bin/openqrm-" . $tp_deployment_plugin_name . " auth -r " . $tp_image_rootdevice . " -i " . $OPENQRM_SERVER_IP_ADDRESS . " -t " . $tp_deployment_type . " --openqrm-cmd-mode background";
        $resource->send_command($tp_storage_ip, $auth_install_from_nfs_start_cmd);
    }
}
Esempio n. 3
0
 function send_command($resource_ip, $resource_command, $command_timeout = NULL)
 {
     global $OPENQRM_EXEC_PORT;
     global $OPENQRM_SERVER_BASE_DIR;
     global $OPENQRM_EXECUTION_LAYER;
     global $event;
     global $RootDir;
     // here we assume that we are the resource
     // plugin hook in case a resource gets rebooted or halted
     switch ($resource_command) {
         case 'reboot':
             // start the hook
             $plugin = new plugin();
             $enabled_plugins = $plugin->enabled();
             foreach ($enabled_plugins as $index => $plugin_name) {
                 $plugin_start_resource_hook = "{$RootDir}/plugins/{$plugin_name}/openqrm-{$plugin_name}-resource-hook.php";
                 if (file_exists($plugin_start_resource_hook)) {
                     $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found plugin {$plugin_name} handling start-resource event.", "", "", 0, 0, $this->id);
                     // prepare resource_fields array
                     $resource_fields = array();
                     $resource_fields = $this->get_fields($this->id);
                     // include the plugin function file and run it
                     require_once "{$plugin_start_resource_hook}";
                     $resource_function = "openqrm_" . "{$plugin_name}" . "_resource";
                     $resource_function = str_replace("-", "_", $resource_function);
                     $resource_function("start", $resource_fields);
                 }
             }
             // here we check if the resource is virtual or a host and if
             // the virtualization plugin wants to reboot it via the host
             $virtualization = new virtualization();
             $virtualization->get_instance_by_id($this->vtype);
             $virtualization_plugin_name = $virtualization->get_plugin_name();
             $plugin_resource_virtual_command_hook_vm_type = "{$RootDir}/plugins/{$virtualization_plugin_name}/openqrm-{$virtualization_plugin_name}-resource-virtual-command-hook.php";
             // we also give the deployment type a chance to implement virtual commands
             $deployment_plugin_name = '';
             if ($this->imageid != 1) {
                 $image = new image();
                 $image->get_instance_by_id($this->imageid);
                 $storage = new storage();
                 $storage->get_instance_by_id($image->storageid);
                 $deployment = new deployment();
                 $deployment->get_instance_by_id($storage->type);
                 $deployment_plugin_name = $deployment->storagetype;
             }
             $plugin_resource_virtual_command_hook = '';
             $plugin_resource_virtual_command_hook_image_type = "{$RootDir}/plugins/{$deployment_plugin_name}/openqrm-{$deployment_plugin_name}-resource-virtual-command-hook.php";
             //            $plugin_resource_virtual_command_hook_image_type = "$RootDir/plugins/sanboot-storage/openqrm-sanboot-storage-resource-virtual-command-hook.php";
             if (file_exists($plugin_resource_virtual_command_hook_vm_type)) {
                 $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found virtualization {$virtualization_plugin_name} managing virtual command.", "", "", 0, 0, $this->id);
                 $plugin_resource_virtual_command_hook = $plugin_resource_virtual_command_hook_vm_type;
             } else {
                 if (file_exists($plugin_resource_virtual_command_hook_image_type)) {
                     // check if IMAGE_VIRTUAL_RESOURCE_COMMAND=true
                     $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found deploymetn {$deployment_plugin_name} managing virtual command.", "", "", 0, 0, $this->id);
                     $image = new image();
                     $image->get_instance_by_id($this->imageid);
                     $virtual_command_enabled = $image->get_deployment_parameter("IMAGE_VIRTUAL_RESOURCE_COMMAND");
                     if (!strcmp($virtual_command_enabled, "true")) {
                         $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found IMAGE_VIRTUAL_RESOURCE_COMMAND enabled, using virtual command.", "", "", 0, 0, $this->id);
                         $plugin_resource_virtual_command_hook = $plugin_resource_virtual_command_hook_image_type;
                         $virtualization_plugin_name = $deployment_plugin_name;
                     } else {
                         $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found IMAGE_VIRTUAL_RESOURCE_COMMAND disabled, using regular command.", "", "", 0, 0, $this->id);
                     }
                 }
             }
             if (strlen($plugin_resource_virtual_command_hook)) {
                 $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found plugin {$virtualization_plugin_name} virtually handling the reboot command.", "", "", 0, 0, $this->id);
                 // prepare resource_fields array
                 $resource_fields = array();
                 $resource_fields = $this->get_fields($this->id);
                 // include the plugin function file and run it
                 require_once "{$plugin_resource_virtual_command_hook}";
                 $resource_function = "openqrm_" . "{$virtualization_plugin_name}" . "_resource_virtual_command";
                 $resource_function = str_replace("-", "_", $resource_function);
                 $resource_function("reboot", $resource_fields);
                 // the virtual reboot function can only be
                 // implemented by a single plugin depending on the
                 // resource type. So we return after that and
                 // do not try to reboot the resource via its ip
                 return;
             }
             break;
         case 'halt':
             // stop hook
             $plugin = new plugin();
             $enabled_plugins = $plugin->enabled();
             foreach ($enabled_plugins as $index => $plugin_name) {
                 $plugin_start_resource_hook = "{$RootDir}/plugins/{$plugin_name}/openqrm-{$plugin_name}-resource-hook.php";
                 if (file_exists($plugin_start_resource_hook)) {
                     $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found plugin {$plugin_name} handling start-resource event.", "", "", 0, 0, $this->id);
                     // prepare resource_fields array
                     $resource_fields = array();
                     $resource_fields = $this->get_fields($this->id);
                     // include the plugin function file and run it
                     require_once "{$plugin_start_resource_hook}";
                     $resource_function = "openqrm_" . "{$plugin_name}" . "_resource";
                     $resource_function = str_replace("-", "_", $resource_function);
                     $resource_function("stop", $resource_fields);
                 }
             }
             // here we check if the resource is virtual or a host and if
             // the virtualization plugin wants to reboot it via the host
             $virtualization = new virtualization();
             $virtualization->get_instance_by_id($this->vtype);
             $virtualization_plugin_name = $virtualization->get_plugin_name();
             $plugin_resource_virtual_command_hook_vm_type = "{$RootDir}/plugins/{$virtualization_plugin_name}/openqrm-{$virtualization_plugin_name}-resource-virtual-command-hook.php";
             // we also give the deployment type a chance to implement virtual commands
             $deployment_plugin_name = '';
             if ($this->imageid != 1) {
                 $image = new image();
                 $image->get_instance_by_id($this->imageid);
                 $storage = new storage();
                 $storage->get_instance_by_id($image->storageid);
                 $deployment = new deployment();
                 $deployment->get_instance_by_id($storage->type);
                 $deployment_plugin_name = $deployment->storagetype;
             }
             $plugin_resource_virtual_command_hook = '';
             $plugin_resource_virtual_command_hook_image_type = "{$RootDir}/plugins/{$deployment_plugin_name}/openqrm-{$deployment_plugin_name}-resource-virtual-command-hook.php";
             if (file_exists($plugin_resource_virtual_command_hook_vm_type)) {
                 $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found virtualization {$virtualization_plugin_name} managing virtual command.", "", "", 0, 0, $this->id);
                 $plugin_resource_virtual_command_hook = $plugin_resource_virtual_command_hook_vm_type;
             } else {
                 if (file_exists($plugin_resource_virtual_command_hook_image_type)) {
                     // check if IMAGE_VIRTUAL_RESOURCE_COMMAND=true
                     $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found deploymetn {$deployment_plugin_name} managing virtual command.", "", "", 0, 0, $this->id);
                     $image = new image();
                     $image->get_instance_by_id($this->imageid);
                     $virtual_command_enabled = $image->get_deployment_parameter("IMAGE_VIRTUAL_RESOURCE_COMMAND");
                     if (!strcmp($virtual_command_enabled, "true")) {
                         $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found IMAGE_VIRTUAL_RESOURCE_COMMAND enabled, using virtual command.", "", "", 0, 0, $this->id);
                         $plugin_resource_virtual_command_hook = $plugin_resource_virtual_command_hook_image_type;
                         $virtualization_plugin_name = $deployment_plugin_name;
                     } else {
                         $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found IMAGE_VIRTUAL_RESOURCE_COMMAND disabled, using regular command.", "", "", 0, 0, $this->id);
                     }
                 }
             }
             if (strlen($plugin_resource_virtual_command_hook)) {
                 $event->log("stop", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Found plugin {$virtualization_plugin_name} virtually handling the reboot command.", "", "", 0, 0, $this->id);
                 // prepare resource_fields array
                 $resource_fields = array();
                 $resource_fields = $this->get_fields($this->id);
                 // include the plugin function file and run it
                 require_once "{$plugin_resource_virtual_command_hook}";
                 $resource_function = "openqrm_" . "{$virtualization_plugin_name}" . "_resource_virtual_command";
                 $resource_function = str_replace("-", "_", $resource_function);
                 $resource_function("halt", $resource_fields);
                 // the virtual halt function can only be
                 // implemented by a single plugin depending on the
                 // resource type. So we return after that and
                 // do not try to halt the resource via its ip
                 return;
             }
             break;
     }
     // check which execution layer to use
     switch ($OPENQRM_EXECUTION_LAYER) {
         case 'dropbear':
             // generate a random token for the cmd
             $cmd_token = md5(uniqid(rand(), true));
             // custom timeout ?
             if (!is_null($command_timeout)) {
                 $cmd_token .= "." . $command_timeout;
             }
             $final_resource_command = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/sbin/openqrm-exec -i {$resource_ip} -t {$cmd_token} -c \"{$resource_command}\"";
             //$event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Running : $final_resource_command", "", "", 0, 0, 0);
             shell_exec($final_resource_command);
             break;
         case 'openqrm-execd':
             $fp = fsockopen($resource_ip, $OPENQRM_EXEC_PORT, $errno, $errstr, 30);
             if (!$fp) {
                 $event->log("send_command", $_SERVER['REQUEST_TIME'], 2, "resource.class.php", "Could not send the command to resource {$resource_ip}", "", "", 0, 0, 0);
                 $event->log("send_command", $_SERVER['REQUEST_TIME'], 2, "resource.class.php", "{$errstr} ({$errno})", "", "", 0, 0, 0);
             } else {
                 // $event->log("start", $_SERVER['REQUEST_TIME'], 5, "resource.class.php", "Running : $resource_command", "", "", 0, 0, $resource->id);
                 fputs($fp, "{$resource_command}");
                 fclose($fp);
             }
             break;
     }
 }