PageRenderTime 38ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/controllers/whmcs/c_virtual_machines.php

https://github.com/Spencerx/OnApp-PHP-Frontend
PHP | 2659 lines | 1677 code | 523 blank | 459 comment | 151 complexity | c8dccc4786c55188be2094489f6dd67b MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. require_once( ONAPP_PATH . ONAPP_DS . 'controllers' . ONAPP_DS . ONAPP_CONTROLLERS . ONAPP_DS . 'controller.php');
  3. class Virtual_Machines extends Controller {
  4. /**
  5. * Main controller function
  6. *
  7. * @return void
  8. */
  9. public function view() {
  10. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  11. $id = onapp_get_arg('id');
  12. $action = onapp_get_arg('action');
  13. onapp_debug('id => ' . $id . ' action => ' . $action);
  14. switch ($action) {
  15. case 'details':
  16. $this->show_template_details($id);
  17. break;
  18. case 'create_page':
  19. $this->show_template_create();
  20. break;
  21. case 'startup':
  22. $this->startup($id);
  23. break;
  24. case 'reboot':
  25. $this->reboot($id);
  26. break;
  27. case 'shutdown':
  28. $this->shutdown($id);
  29. break;
  30. case 'suspend':
  31. $this->suspend($id);
  32. break;
  33. case 'reset_password':
  34. $this->reset_password($id);
  35. break;
  36. case 'create':
  37. $this->create();
  38. break;
  39. case 'cpu_usage':
  40. $this->show_template_cpu_usage($id);
  41. break;
  42. case 'delete':
  43. $this->delete($id);
  44. break;
  45. case 'rebuild':
  46. case 'build':
  47. $this->build($id);
  48. break;
  49. case 'backup':
  50. $this->show_template_backup($id);
  51. break;
  52. case 'backup_restore':
  53. $this->backup_restore($id);
  54. break;
  55. case 'backup_convert':
  56. $this->backup_convert($id);
  57. break;
  58. case 'backup_delete':
  59. $this->backup_delete($id);
  60. break;
  61. case 'ip_addresses':
  62. $this->show_template_ip_addresses($id);
  63. break;
  64. case 'rebuild_network':
  65. $this->rebuild_network($id);
  66. break;
  67. case 'ip_address_join_new':
  68. $this->ip_address_join_new($id);
  69. break;
  70. case 'ip_address_delete':
  71. $this->ip_address_delete($id);
  72. break;
  73. case 'network_interfaces':
  74. $this->show_template_network_interface($id);
  75. break;
  76. case 'network_interface_edit':
  77. $this->network_interface_edit($id);
  78. break;
  79. case 'network_interface_delete':
  80. $this->network_interface_delete($id);
  81. break;
  82. case 'network_interface_create':
  83. $this->network_interface_create($id);
  84. break;
  85. case 'disks':
  86. $this->show_template_disks($id);
  87. break;
  88. case 'change_owner':
  89. $this->change_owner($id);
  90. break;
  91. case 'edit':
  92. $this->edit($id);
  93. break;
  94. case 'autobackup_disable':
  95. $this->autobackup_disable($id);
  96. break;
  97. case 'autobackup_enable':
  98. $this->autobackup_enable($id);
  99. break;
  100. case 'disk_edit':
  101. $this->disk_edit($id);
  102. break;
  103. case 'disk_create':
  104. $this->disk_create($id);
  105. break;
  106. case 'disk_delete':
  107. $this->disk_delete($id);
  108. break;
  109. case 'disk_backups':
  110. $this->show_template_disk_backup($id);
  111. break;
  112. case 'disk_backups_schedule':
  113. $this->show_template_disk_backups_schedule($id);
  114. break;
  115. case 'disk_backups_schedule_edit':
  116. $this->disk_backups_schedule_edit($id);
  117. break;
  118. case 'disk_backups_schedule_delete':
  119. $this->disk_backups_schedule_delete($id);
  120. break;
  121. case 'disk_backups_schedule_create':
  122. $this->disk_backups_schedule_create($id);
  123. break;
  124. case 'firewall':
  125. $this->show_template_firewall($id);
  126. break;
  127. case 'firewall_rule_create':
  128. $this->firewall_rule_create($id);
  129. break;
  130. case 'firewall_rule_move':
  131. $this->firewall_rule_move($id);
  132. break;
  133. case 'firewall_rule_delete':
  134. $this->firewall_rule_delete($id);
  135. break;
  136. case 'firewall_rule_edit':
  137. $this->firewall_rule_edit($id);
  138. break;
  139. case 'firewall_rule_update_defaults':
  140. $this->firewall_rule_update_defaults($id);
  141. break;
  142. case 'firewall_rules_apply':
  143. $this->firewall_rules_apply($id);
  144. break;
  145. case 'backup_take':
  146. $this->backup_take($id);
  147. break;
  148. case 'migrate':
  149. $this->migrate($id);
  150. break;
  151. case 'edit_admin_note':
  152. $this->edit_admin_note($id);
  153. break;
  154. case 'disk_backups_schedule_details':
  155. $this->show_template_disk_backups_schedule_details($id);
  156. break;
  157. case 'console':
  158. $this->console($id);
  159. break;
  160. default:
  161. $this->show_template_view();
  162. break;
  163. }
  164. }
  165. /**
  166. * Displays default page with virtual machine list
  167. *
  168. * @param string error message
  169. * @param other message
  170. * @return void
  171. */
  172. private function show_template_view($error = NULL) {
  173. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  174. onapp_debug('error => ' . $error);
  175. $onapp = $this->get_factory();
  176. $hypervisor_id = onapp_get_arg('hypervisor_id');
  177. $hypervisor_label = onapp_get_arg('hypervisor_label');
  178. $user_id = onapp_get_arg('user_id');
  179. onapp_debug('hypervisor_id => ' . $hypervisor_id . ' hypervisor_label => ' . $hypervisor_label . ' user_id => ' . $user_id);
  180. onapp_permission(array('virtual_machines', 'virtual_machines.read.own', 'virtual_machines.read'));
  181. $virtual_machine = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  182. $virtual_machines_array = $virtual_machine->getList();
  183. $virtual_machines = $virtual_machines_array;
  184. /*** W H M C S ****/
  185. // $postfields = array(
  186. // 'pid' => 2,
  187. // );
  188. // print('<pre>'); print_r($_SESSION['profile_obj']); die();
  189. // if () {
  190. //
  191. // }
  192. //print('<pre>'); print_r($virtual_machines); die();
  193. if( ! $client_id = $this->is_whmcs_client( ) ){
  194. require_once 'c_profile.php';
  195. $profile = new Profile();
  196. $profile->show_template_view('NOT_WHMCS_CLIENT_ERROR');
  197. exit;
  198. }
  199. if ( ! is_null ( $virtual_machines ) ) {
  200. foreach ( $virtual_machines as $virtual_machine ) {
  201. if ( $product = $this->is_whmcs_maped( $virtual_machine, $client_id ) ) {
  202. $virtual_machine->_whmcs_maped = $product['id'];
  203. $virtual_machine->_whmcs_status = $product['status'];
  204. }
  205. }
  206. }
  207. //print('<pre>'); print_r($virtual_machines); die();
  208. //$this->is_whmcs_maped
  209. // print('<pre>'); print_r($response); die();
  210. /*** W H M C S ****/
  211. // $vm_backup = $onapp->factory('VirtualMachine_Backup', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  212. //
  213. // foreach ($virtual_machines as $virtual_machine) {
  214. // $vm_backup_obj = $vm_backup->getList($virtual_machine->_id);
  215. // $size_and_quantity = $this->calculateBackups($vm_backup_obj);
  216. // $vm_backups [] = $size_and_quantity;
  217. // }
  218. //TODO refactor when bug is fixed Ticket #2508
  219. if (!is_null($hypervisor_id)) {
  220. foreach ($virtual_machines as $virtual_machine)
  221. if ($virtual_machine->_hypervisor_id == $hypervisor_id)
  222. $hypervisor_vms[] = $virtual_machine;
  223. $virtual_machines = $hypervisor_vms;
  224. }
  225. if (!is_null($user_id)) {
  226. foreach ($virtual_machines as $virtual_machine)
  227. if ($virtual_machine->_user_id == $user_id)
  228. $user_vms[] = $virtual_machine;
  229. $virtual_machines = $user_vms;
  230. }
  231. $params = array(
  232. 'user_id' => $user_id,
  233. 'hypervisor_id' => $hypervisor_id,
  234. //'vm_backups' => $vm_backups,
  235. 'virtual_machines' => $virtual_machines,
  236. 'hypervisor_label' => $hypervisor_label,
  237. 'title' => onapp_string('VIRTUAL_MACHINES'),
  238. 'info_title' => onapp_string('VIRTUAL_MACHINES'),
  239. 'info_body' => onapp_string('VIRTUAL_MACHINE_INFO'),
  240. 'error' => $error,
  241. );
  242. onapp_show_template('vm_view', $params);
  243. }
  244. private function is_whmcs_maped( $virtual_machine, $client_id ) {
  245. $postfields = array( 'client_id' => $client_id );
  246. $response = onapp_send_whmcs_api_request( ONAPP_WHMCS_LOGIN, ONAPP_WHMCS_PASSWORD, ONAPP_WHMCS_API_FILE_URL, 'getclientsproducts', $postfields );
  247. $whmcs_products = $response['products']['product'];
  248. // print('<pre>');print_r($whmcs_products); die();
  249. foreach ( $whmcs_products as $product ) {
  250. if ( $product['domain'] == $virtual_machine->_hostname ) {
  251. onapp_debug( $product['domain'] , ' ==> ', $virtual_machine->_hostname , '<br /><br />' );
  252. return array( 'id' => $product['id'], 'status' => $product['status'] );
  253. }
  254. }
  255. //print('<pre>'); print_r($whmcs_products); die();
  256. return false;
  257. }
  258. /**
  259. * Checks whether user is whmcs client
  260. *
  261. * @return integer client_id whether whmcs client and boolean false if not
  262. */
  263. private function is_whmcs_client( ) {
  264. $response = onapp_send_whmcs_api_request( ONAPP_WHMCS_LOGIN, ONAPP_WHMCS_PASSWORD, ONAPP_WHMCS_API_FILE_URL, 'getclients', $postfields );
  265. $whmcs_clients = $response['clients']['client'];
  266. foreach ( $whmcs_clients as $client ) {
  267. onapp_debug( $client['firstname'], ' ==> ', $_SESSION['profile_obj']->_first_name, '<br />' );
  268. onapp_debug( $client['lastname'], ' ==> ', $_SESSION['profile_obj']->_last_name, '<br />' );
  269. onapp_debug( $client['email'], ' ==> ', $_SESSION['profile_obj']->_email, '<br />', '<br />' );
  270. if ( $client['firstname'] == $_SESSION['profile_obj']->_first_name
  271. && $client['lastname'] == $_SESSION['profile_obj']->_last_name
  272. && $client['email'] == $_SESSION['profile_obj']->_email
  273. ) {
  274. return $client['id'];
  275. }
  276. }
  277. return false;
  278. }
  279. /**
  280. * Displays particular virtual machine details page
  281. *
  282. * @param integer virtual Machine id
  283. * @param string error message
  284. * @param string other message
  285. * @return void
  286. */
  287. public function show_template_details($id, $error = NULL) {
  288. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  289. onapp_debug('id => ' . $id . ' error => ' . $error);
  290. $vm_obj = $this->load('VirtualMachine', array($id));
  291. $vm_backup_obj = $this->getList('VirtualMachine_Backup', array($id));
  292. $size_and_quantity = $this->calculateBackups($vm_backup_obj);
  293. $params = array(
  294. 'virtual_machine_id' => $id,
  295. 'user_obj' => $this->load('User', array($vm_obj->_user_id)),
  296. 'backups_quantity' => $size_and_quantity['quantity'],
  297. 'backups_total_size' => $size_and_quantity['size'],
  298. 'profile_obj' => $_SESSION['profile_obj'],
  299. 'hypervisor_obj' => $this->load('Hypervisor', array($vm_obj->_hypervisor_id) ),
  300. 'vm_obj' => $vm_obj,
  301. 'title' => onapp_string('VIRTUAL_MACHINE_DETAILS'),
  302. 'info_title' => onapp_string('VIRTUAL_MACHINE_DETAILS'),
  303. 'info_body' => onapp_string('VIRTUAL_MACHINE_DETAILS_INFO'),
  304. 'error' => $error,
  305. );
  306. onapp_show_template('vm_details', $params);
  307. }
  308. /**
  309. * Shows creating a new virtual machine page
  310. *
  311. * @param error message
  312. * @param other message
  313. * @return void
  314. */
  315. private function show_template_create($error = NULL) {
  316. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  317. onapp_debug('error => ' . $error);
  318. $onapp = $this -> get_factory();
  319. onapp_permission(array('virtual_machines', 'virtual_machines.create'));
  320. $template = $onapp->factory('Template', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  321. $params = array(
  322. 'network_zone_obj' => $this->getList('NetworkZone'),
  323. 'data_store_zone_obj' => $this->getList('DataStoreZone'),
  324. 'hypervisor_zones_obj' => $this->getList('HypervisorZone'),
  325. 'user_templates_obj' => $template->getUserTemplates(),
  326. 'templates_obj' => $this->getList('Template'),
  327. 'hypervisor_obj' => $this->getList('Hypervisor'),
  328. 'title' => onapp_string('CREATE_VIRTUAL_MACHINE'),
  329. 'info_title' => onapp_string('CREATE_VIRTUAL_MACHINE'),
  330. 'info_body' => onapp_string('CREATE_VIRTUAL_MACHINE_INFO'),
  331. 'error' => $error,
  332. );
  333. onapp_show_template('vm_create', $params);
  334. }
  335. /**
  336. * Shows virtual machine cpu usage
  337. *
  338. * @param integer virtual machine id
  339. * @return void
  340. */
  341. private function show_template_cpu_usage($id) {
  342. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  343. onapp_debug('id => ' . $id);
  344. $onapp = $this -> get_factory();
  345. onapp_permission(array('virtual_machines', 'virtual_machines.power', 'virtual_machines.power.own'));
  346. $cpuusage = $onapp->factory('VirtualMachine_CpuUsage', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  347. $cpuusage->_virtual_machine_id = $id; // print('<pre>');print_r($cpuusage); die();
  348. $list = $cpuusage->getList();
  349. $xaxis = '';
  350. $yaxis = '';
  351. $date = array();
  352. for ($i = 0; $i < count($list); $i++) {
  353. if (isset($date[$list[$i]->_created_at]))
  354. $date[$list[$i]->_created_at]++;
  355. else
  356. $date[$list[$i]->_created_at] = 1;
  357. }
  358. for ($i = 0; $i < count($list); $i++) {
  359. $created_at = str_replace(array('T', 'Z'), ' ', $list[$i]->_stat_time);
  360. $xaxis .= "<value xid='$i'>" . $created_at . "</value>";
  361. $xaxis .= "<value xid='$i'>" . $created_at . "</value>";
  362. if ($date[$list[$i]->_created_at] * 100 != 0) {
  363. $usage = $list[$i]->_cpu_time / ($date[$list[$i]->_created_at] * 10) / 100;
  364. }
  365. else
  366. $usage = 0;
  367. $yaxis .= "<value xid='$i'>" . number_format($usage, 2) . "</value>";
  368. }
  369. $params = array(
  370. 'virtual_machine_id' => $id,
  371. 'xaxis' => $xaxis,
  372. 'yaxis' => $yaxis,
  373. 'title' => onapp_string('CPU_USAGE'),
  374. 'info_title' => onapp_string('CPU_USAGE_FOR_THIS_VM'),
  375. 'info_body' => onapp_string('CPU_USAGE_FOR_THIS_VM_INFO'),
  376. );
  377. onapp_show_template('vm_cpuUsage', $params);
  378. }
  379. /**
  380. * Shows virtual machine backups
  381. *
  382. * @param integer virtual machine id
  383. * @return void
  384. */
  385. private function show_template_backup($id, $error = NULL) {
  386. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  387. onapp_debug('id => ' . $id . ' error => ' . $error);
  388. onapp_permission(array('backups.read.own', 'backups.read', 'backups'));
  389. $params = array(
  390. 'virtual_machine_id' => $id,
  391. 'backup_obj' => $this->getList('VirtualMachine_Backup', array($id)),
  392. 'title' => onapp_string('BACKUPS_FOR_THIS_VM'),
  393. 'info_title' => onapp_string('BACKUPS_FOR_THIS_VM'),
  394. 'info_body' => onapp_string('BACKUPS_FOR_THIS_VM_INFO'),
  395. 'error' => $error,
  396. );
  397. onapp_show_template('vm_backup', $params);
  398. }
  399. /**
  400. * Shows disk backups schedule details
  401. *
  402. * @param integer schedule id
  403. * @return void
  404. */
  405. private function show_template_disk_backups_schedule_details($id) {
  406. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  407. onapp_debug('id => ' . $id);
  408. onapp_permission(array('schedules', 'schedules.read', 'schedules.read.own'));
  409. $schedule_obj = $this->load('Disk_Schedule', array($id));
  410. $user_obj = $this->load('User', array($schedule_obj->_user_id));
  411. $params = array(
  412. 'user_first_name' => $user_obj->_first_name,
  413. 'user_last_name' => $user_obj->_last_name,
  414. 'schedule_obj' => $schedule_obj,
  415. 'title' => onapp_string('SCHEDULE_DETAILS'),
  416. 'info_title' => onapp_string('SCHEDULE_DETAILS'),
  417. 'info_body' => onapp_string('SCHEDULE_DETAILS_INFO'),
  418. );
  419. onapp_show_template('vm_diskBackupsScheduleDetails', $params);
  420. }
  421. /**
  422. * Shows virtual machine firewall
  423. *
  424. * @param integer virtual machine id
  425. * @return void
  426. */
  427. private function show_template_firewall($id, $error = NULL) {
  428. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  429. onapp_debug('id => ' . $id);
  430. onapp_permission(array('firewall_rules.read.own', 'firewall_rules.read', 'firewall_rules'));
  431. $firewall_obj = $this->getList('VirtualMachine_FirewallRule', array( $id ));
  432. $network_interface_obj = $this->getList('VirtualMachine_NetworkInterface', array($id));
  433. foreach ($network_interface_obj as $network_interface)
  434. $network_interface_object[$network_interface->_id] = $network_interface;
  435. if ( ! is_null( $firewall_obj )) {
  436. foreach ($firewall_obj as $firewall)
  437. $firewall_by_network[$firewall->_network_interface_id][] = $firewall;
  438. }
  439. else
  440. $firewall_by_network = NULL;
  441. $params = array(
  442. 'firewall_by_network' => $firewall_by_network,
  443. 'commands' => array('ACCEPT', 'DROP'),
  444. 'virtual_machine_id' => $id,
  445. 'network_interface_obj' => $network_interface_object,
  446. 'firewall_obj' => $firewall_obj,
  447. 'title' => onapp_string('FIREWALL_RULES'),
  448. 'info_title' => onapp_string('FIREWALL_RULES'),
  449. 'info_body' => onapp_string('FIREWALL_RULES_INFO'),
  450. 'error' => $error,
  451. );
  452. onapp_show_template('vm_firewall', $params);
  453. }
  454. /**
  455. * Shows virtual machine disk backups schedules
  456. *
  457. * @param integer virtual machine disk id
  458. * @return void
  459. */
  460. private function show_template_disk_backups_schedule($id, $error = NULL) {
  461. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  462. onapp_debug('id => ' . $id . ' error => ' . $error);
  463. onapp_permission(array('schedules', 'schedules.read', 'schedules.read.own'));
  464. $onapp = $this->get_factory();
  465. $schedule = $onapp->factory('Disk_Schedule', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  466. $schedule_obj = $schedule->getListByDiskId($id);
  467. $params = array(
  468. 'schedule_obj' => $schedule_obj,
  469. 'disk_id' => $id,
  470. 'title' => onapp_string('SCHEDULES_'),
  471. 'info_title' => onapp_string('SCHEDULES_'),
  472. 'info_body' => onapp_string('SCHEDULES_INFO'),
  473. 'error' => $error,
  474. );
  475. onapp_show_template('vm_diskBackupsSchedule', $params);
  476. }
  477. /**
  478. * Shows disk backups list
  479. *
  480. * @param integer virtual machine disk id
  481. * @return void
  482. */
  483. private function show_template_disk_backup($id, $error = NULL) {
  484. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  485. onapp_debug('id => ' . $id . ' error => ' . $error);
  486. $onapp = $this->get_factory();
  487. onapp_permission(array('backups.read.own', 'backups.read', 'backups'));
  488. $backup = $onapp->factory('VirtualMachine_Backup', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  489. $backup_obj = $backup->diskBackups($id);
  490. $params = array(
  491. 'disk_id' => $id,
  492. 'virtual_machine_id' => onapp_get_arg('virtual_machine_id'),
  493. 'backup_obj' => $backup_obj,
  494. 'title' => onapp_string('BACKUPS_FOR_THIS_DISK'),
  495. 'info_title' => onapp_string('BACKUPS_FOR_THIS_DISK'),
  496. 'info_body' => onapp_string('BACKUPS_FOR_THIS_DISK_INFO'),
  497. 'error' => $error,
  498. );
  499. onapp_show_template('vm_diskBackup', $params);
  500. }
  501. /**
  502. * Shows change virtual machine page
  503. *
  504. * @param integer virtual machine id
  505. * @return void
  506. */
  507. private function show_template_change_owner($id) {
  508. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  509. onapp_debug('id => ' . $id);
  510. onapp_permission(array('virtual_machines', 'virtual_machines.change_owner'));
  511. $params = array(
  512. 'vm_obj' => $this->load('VirtualMachine', array($id)),
  513. 'user_obj' => $this->getList('User'),
  514. 'title' => onapp_string('CHANGE_THIS_VIRTUAL_MACHINE_OWNER'),
  515. 'info_title' => onapp_string('CHANGE_THIS_VIRTUAL_MACHINE_OWNER'),
  516. 'info_body' => onapp_string('CHANGE_THIS_VIRTUAL_MACHINE_OWNER_INFO'),
  517. );
  518. onapp_show_template('vm_changeOwner', $params);
  519. }
  520. /**
  521. * Shows virtual machine disks list
  522. *
  523. * @param integer virtual machine id
  524. * @return void
  525. */
  526. private function show_template_disks($id, $error = NULL) {
  527. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  528. onapp_debug('id => ' . $id . ' error => ' . $error);
  529. $onapp = $this->get_factory();
  530. onapp_permission(array('disks', 'disks.read', 'disks.read.own'));
  531. $disk_obj = $this->getList('Disk', array($id));
  532. $data_store_obj = $this->getList('DataStore');
  533. foreach ($data_store_obj as $data_store)
  534. $data_store_object[$data_store->_id] = $data_store;
  535. $vm_obj = $this->getList('VirtualMachine');
  536. foreach ($vm_obj as $vm)
  537. $vm_object[$vm->_id] = $vm;
  538. $backup = $onapp->factory('VirtualMachine_Backup', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  539. foreach ($disk_obj as $disk) {
  540. $backup_obj = $backup->diskBackups( $disk->_id );
  541. if ( ! is_null($backup_obj) )
  542. $backup_object[$disk->_id] = count($backup_obj);
  543. else
  544. $backup_object[$disk->_id] = 0;
  545. }
  546. $params = array(
  547. 'backup_quantity' => $backup_object,
  548. 'vm_obj' => $vm_object,
  549. 'data_store_obj' => $data_store_object,
  550. 'disk_obj' => $disk_obj,
  551. 'title' => onapp_string('DISK_SETTINGS'),
  552. 'info_title' => onapp_string('DISK_SETTINGS'),
  553. 'info_body' => onapp_string('DISK_SETTINGS_INFO'),
  554. 'error' => $error,
  555. );
  556. onapp_show_template('vm_disk', $params);
  557. }
  558. /**
  559. * Shows virtual machine ip addresses list
  560. *
  561. * TODO doesn't work in XML ( Different structure of VirtualMachine_IpAddressJoin response! ) ticket #118
  562. * @param integer virtual machine id
  563. * @return void
  564. */
  565. private function show_template_ip_addresses($id, $error = NULL) {
  566. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  567. onapp_debug('id => ' . $id . ' error => ' . $error);
  568. onapp_permission(array('ip_address_joins', 'ip_address_joins.read', 'ip_address_joins.read.own'));
  569. $vm_obj = $this->load('VirtualMachine', array($id));
  570. foreach ($this->getList('VirtualMachine_NetworkInterface', array($id)) as $network_interface)
  571. $network_interface_array[$network_interface->_id] = $network_interface;
  572. foreach ($this->getList('Network') as $network)
  573. $network_array[$network->_id] = $network;
  574. $params = array(
  575. 'virtual_machine_id' => $id,
  576. 'hypervisor_label' => $this->load('Hypervisor', array($vm_obj->_hypervisor_id))->_label,
  577. 'network_obj' => $network_array,
  578. 'ip_address_obj' => $this->getList('VirtualMachine_IpAddressJoin', array($id)),
  579. 'network_interface_obj' => $network_interface_array,
  580. 'title' => onapp_string('IP_ADDRESSES_FOR_THIS_VIRTUAL_MACHINE'),
  581. 'info_title' => onapp_string('IP_ADDRESSES_FOR_THIS_VIRTUAL_MACHINE'),
  582. 'info_body' => onapp_string('IP_ADDRESSES_FOR_THIS_VIRTUAL_MACHINE_INFO'),
  583. 'error' => $error,
  584. );
  585. onapp_show_template('vm_ipAddress', $params);
  586. }
  587. /**
  588. * Shows virtual machine backups
  589. *
  590. * @param integer virtual machine id
  591. * @return void
  592. */
  593. private function show_template_backup_convert($id) {
  594. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  595. onapp_debug('id => ' . $id);
  596. onapp_permission(array('backups.convert.own', 'backups.convert', 'backups'));
  597. $params = array(
  598. 'id' => $id,
  599. 'title' => onapp_string('CONVERT_THIS_BACKUP_TO_TEMPLATE'),
  600. 'info_title' => onapp_string('CONVERT_THIS_BACKUP_TO_TEMPLATE'),
  601. 'info_body' => onapp_string('CONVERT_THIS_BACKUP_TO_TEMPLATE_INFO'),
  602. );
  603. onapp_show_template('vm_backupConvert', $params);
  604. }
  605. /**
  606. * Shows administrator's note edit page
  607. *
  608. * @param integer virtual machine id
  609. * @return void
  610. */
  611. private function show_template_edit_admin_note($id, $error = NULL) {
  612. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  613. onapp_debug('id => ' . $id . ' error => ' . $error);
  614. onapp_permission('virtual_machines', 'sysadmin_tools');
  615. $params = array(
  616. 'current_admin_note' => $this->load('VirtualMachine', array($id))->_admin_note,
  617. 'id' => $id,
  618. 'title' => onapp_string('EDIT_ADMIN_NOTE'),
  619. 'info_title' => onapp_string('EDIT_ADMIN_NOTE'),
  620. 'info_body' => onapp_string('EDIT_ADMIN_NOTE_INFO'),
  621. );
  622. onapp_show_template('vm_editAdminNote', $params);
  623. }
  624. /**
  625. * Shows virtual machine disk edit page
  626. *
  627. * @param integer virtual machine disk id
  628. * @return void
  629. */
  630. private function show_template_disk_edit($id) {
  631. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  632. onapp_debug('id => ' . $id);
  633. onapp_permission(array('disks', 'disks.update', 'disks.update.own'));
  634. $params = array(
  635. 'disk_obj' => $this->load('Disk', array($id)),
  636. 'title' => onapp_string('EDIT_DISK'),
  637. 'info_title' => onapp_string('EDIT_DISK'),
  638. 'info_body' => onapp_string('EDIT_DISK_INFO'),
  639. );
  640. onapp_show_template('vm_diskEdit', $params);
  641. }
  642. /**
  643. * Shows virtual machine disk edit page
  644. *
  645. * @param integer virtual machine disk id
  646. * @return void
  647. */
  648. private function show_template_migrate($id) {
  649. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  650. onapp_debug('id => ' . $id);
  651. onapp_permission(array('virtual_machines', 'virtual_machines.migrate.own', 'virtual_machines.migrate'));
  652. $vm_obj = $this->load('VirtualMachine', array($id));
  653. $params = array(
  654. 'virtual_machine_id' => $id,
  655. 'hypervisor_obj' => $this->load('Hypervisor', array($vm_obj->_hypervisor_id)),
  656. 'hypervisors_obj' => $this->getList('Hypervisor'),
  657. 'title' => onapp_string('MIGRATE_VIRTUAL_MACHINE'),
  658. 'info_title' => onapp_string('MIGRATE_VIRTUAL_MACHINE'),
  659. 'info_body' => onapp_string('MIGRATE_VIRTUAL_MACHINE_INFO'),
  660. );
  661. onapp_show_template('vm_migrate', $params);
  662. }
  663. /**
  664. * Shows disk backups schedule edit page
  665. *
  666. * @param integer disk backup schedule id
  667. * @return void
  668. */
  669. private function show_template_disk_backups_schedule_edit($id) {
  670. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  671. onapp_debug('id => ' . $id);
  672. onapp_permission(array('schedules', 'schedules.update'));
  673. $periods = array(
  674. 'days' => 'Days',
  675. 'weeks' => 'Weeks',
  676. 'months' => 'Months',
  677. 'years' => 'Years'
  678. );
  679. $params = array(
  680. 'periods' => $periods,
  681. 'schedule_obj' => $this->load('Disk_Schedule', array($id)),
  682. 'title' => onapp_string('EDIT_SCHEDULE'),
  683. 'info_title' => onapp_string('EDIT_SCHEDULE'),
  684. 'info_body' => onapp_string('EDIT_SCHEDULE_INFO'),
  685. );
  686. onapp_show_template('vm_diskBackupsScheduleEdit', $params);
  687. }
  688. /**
  689. * Shows firewall rule edit page
  690. *
  691. * @param integer firewall id
  692. * @return void
  693. */
  694. private function show_template_firewall_rule_edit($id) {
  695. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  696. onapp_debug('id => ' . $id);
  697. onapp_permission(array('firewall_rules', 'firewall_rules.update'));
  698. $virtual_machine_id = onapp_get_arg('virtual_machine_id');
  699. $params = array(
  700. 'virtual_machine_id' => $virtual_machine_id,
  701. 'network_interface_obj' => $this->getList('VirtualMachine_NetworkInterface', array($virtual_machine_id)),
  702. 'protocols' => array('TCP', 'UDP'),
  703. 'commands' => array('ACCEPT', 'DROP'),
  704. 'firewall_obj' => $this->load('VirtualMachine_FirewallRule', array($id, $virtual_machine_id)),
  705. 'title' => onapp_string('UPDATE_FIREWALL_RULE'),
  706. 'info_title' => onapp_string('UPDATE_FIREWALL_RULE'),
  707. 'info_body' => onapp_string('UPDATE_FIREWALL_RULE_INFO'),
  708. );
  709. onapp_show_template('vm_firewallRuleEdit', $params);
  710. }
  711. /**
  712. * Shows disk backups schedule create page
  713. *
  714. * @param integer disk backup schedule id
  715. * @return void
  716. */
  717. private function show_template_disk_backups_schedule_create($id) {
  718. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  719. onapp_debug('id => ' . $id);
  720. onapp_permission(array('schedules', 'schedules.create'));
  721. $params = array(
  722. 'disk_id' => $id,
  723. 'title' => onapp_string('ADD_A_SCHEDULE_FOR_THIS_DISK'),
  724. 'info_title' => onapp_string('ADD_A_SCHEDULE_FOR_THIS_DISK'),
  725. 'info_body' => onapp_string('ADD_A_SCHEDULE_FOR_THIS_DISK_INFO'),
  726. );
  727. onapp_show_template('vm_diskBackupsScheduleCreate', $params);
  728. }
  729. /**
  730. * Shows virtual machine disk create page
  731. *
  732. * @param integer virtual machine id
  733. * @return void
  734. */
  735. private function show_template_disk_create($id) {
  736. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  737. onapp_debug('id => ' . $id);
  738. onapp_permission(array('disks', 'disks.create'));
  739. $params = array(
  740. 'data_store_obj' => $this->getList('DataStore'),
  741. 'virtual_machine_id' => $id,
  742. 'title' => onapp_string('ADD_NEW_DISK'),
  743. 'info_title' => onapp_string('ADD_NEW_DISK'),
  744. 'info_body' => onapp_string('ADD_NEW_DISK_INFO'),
  745. );
  746. onapp_show_template('vm_diskCreate', $params);
  747. }
  748. /**
  749. * Shows 'Allocate New IP Address from Global Pool' page
  750. *
  751. * @param integer virtual machine id
  752. * @return void
  753. */
  754. private function show_template_ip_address_join_new($id) {
  755. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  756. onapp_debug('id => ' . $id);
  757. onapp_permission(array('ip_address_joins', 'ip_address_joins.create', 'ip_address_joins.create.own'));
  758. $onapp = $this->get_factory();
  759. $vm_obj = $this->load('VirtualMachine', array($id));
  760. $hypervisor_obj = $this->load('Hypervisor', array($vm_obj->_hypervisor_id));
  761. $network_join_obj = $this->getList('Hypervisor_NetworkJoin', array($vm_obj->_hypervisor_id));
  762. $networkzone_join_obj = $this->getList('HypervisorZone_NetworkJoin', array($hypervisor_obj->_hypervisor_group_id));
  763. $network = $onapp->factory('Network', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  764. if (is_array($network_join_obj)) {
  765. foreach ($network_join_obj as $network_join) {
  766. $network_obj = $network->load($network_join->_network_id);
  767. $network_obj_array[$network_join->_id] = $network_obj;
  768. }
  769. }
  770. if (is_array($networkzone_join_obj)) {
  771. foreach ($networkzone_join_obj as $network_join) {
  772. $network_obj = $network->load($network_join->_network_id);
  773. $network_obj_array[$network_join->_id] = $network_obj;
  774. }
  775. }
  776. $network_interface = $onapp->factory('VirtualMachine_NetworkInterface', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  777. $network_interface_obj = $network_interface->getList($id);
  778. foreach ($network_interface_obj as $network_interface_item) {
  779. $network_interface_object[$network_interface_item->_network_join_id] = $network_interface_item;
  780. }
  781. $ip_addresses = $onapp->factory('IpAddress', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  782. foreach ($network_obj_array as $interface_id => $network) {
  783. $ip_addresses_object = $ip_addresses->getList($network->_id);
  784. if ( ! is_null( $ip_addresses_object) ) {
  785. foreach ($ip_addresses_object as $object) {
  786. if ($object->_free == 1)
  787. $ip_addresses_array[$network_interface_object[$interface_id]->_id][$object->_id] = $object->_address . '/' . $object->_netmask . '/' . $object->_gateway;
  788. }
  789. }
  790. }
  791. $params = array(
  792. 'ip_addresses_array' => $ip_addresses_array,
  793. 'ip_addresses' => json_encode($ip_addresses_array),
  794. 'network_interface_obj' => $network_interface_obj,
  795. 'id' => $id,
  796. 'title' => onapp_string('ALLOCATE_NEW_IP_ADDRESS_ASSIGNMENT'),
  797. 'info_title' => onapp_string('ALLOCATE_NEW_IP_ADDRESS_ASSIGNMENT'),
  798. 'info_body' => onapp_string('ALLOCATE_NEW_IP_ADDRESS_ASSIGNMENT_INFO'),
  799. );
  800. onapp_show_template('vm_IpAddressJoinNew', $params);
  801. }
  802. /**
  803. * Shows network interface list for virtual machine
  804. *
  805. * @param integer virtual machine id
  806. * @return void
  807. */
  808. private function show_template_network_interface($id, $error = NULL) {
  809. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  810. onapp_debug('id => ' . $id . ' error => ' . $error);
  811. onapp_permission(array('networks', 'virtual_machines.update.own', 'networks.read', 'virtual_machines'));
  812. $onapp = $this->get_factory();
  813. $vm_obj = $this->load('VirtualMachine', array($id) );
  814. $hypervisor_obj = $this->load('Hypervisor', array($vm_obj->_hypervisor_id) );
  815. $hypervisor_zone_obj = $this->load('HypervisorZone', array($hypervisor_obj->_hypervisor_group_id) );
  816. $ip_address_obj = $this->getList('VirtualMachine_IpAddressJoin', array($id));
  817. $network_interface_obj = $this->getList('VirtualMachine_NetworkInterface', array($id));
  818. if ( ! is_null( $network_interface_obj ) ) {
  819. foreach ($network_interface_obj as $network_interface)
  820. $network_interface_array[$network_interface->_network_join_id] = $network_interface;
  821. }
  822. else
  823. $network_interface_array = NULL;
  824. $hypervisor_network_join_obj = $this->getList('Hypervisor_NetworkJoin', array($vm_obj->_hypervisor_id));
  825. $hypervisor_zone_network_join_obj = $this->getList('HypervisorZone_NetworkJoin', array($hypervisor_obj->_hypervisor_group_id));
  826. $network = $onapp->factory('Network', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  827. if (is_array($hypervisor_network_join_obj)) {
  828. foreach ($hypervisor_network_join_obj as $network_join) {
  829. if ( $network_join->_networ_id ) {
  830. $network_obj = $network->load( $network_join->_network_id );
  831. }
  832. $hypervisor_network_interfaces[$network_join->_id] = $network_obj;
  833. $network_labels[$network_join->_id] = $network_obj;
  834. $target_labels[$network_join->_id] = $hypervisor_obj->_label;
  835. }
  836. }
  837. if (is_array($hypervisor_zone_network_join_obj)) {
  838. foreach ($hypervisor_zone_network_join_obj as $network_join) {
  839. $network_obj = $network->load($network_join->_network_id);
  840. $hypervisor_zone_network_interfaces[$network_join->_id] = $network_obj;
  841. $network_labels[$network_join->_id] = $network_obj;
  842. $target_labels[$network_join->_id] = $hypervisor_zone_obj->_label;
  843. }
  844. }
  845. $params = array(
  846. 'virtual_machine_id' => $vm_obj->_id,
  847. 'hypervisor_label' => $hypervisor_obj->_label,
  848. 'network_labels' => $network_labels,
  849. 'target_labels' => $target_labels,
  850. 'network_interface_obj' => $network_interface_obj,
  851. 'id' => $id,
  852. 'title' => onapp_string('NETWORK_INTERFACE_FOR_THIS_VIRTUAL_MACHINE'),
  853. 'info_title' => onapp_string('NETWORK_INTERFACE_FOR_THIS_VIRTUAL_MACHINE'),
  854. 'info_body' => onapp_string('NETWORK_INTERFACE_FOR_THIS_VIRTUAL_MACHINE_INFO'),
  855. 'error' => $error,
  856. );
  857. onapp_show_template('vm_networkInterface', $params);
  858. }
  859. /**
  860. * Shows 'Edit network interface' page
  861. *
  862. * @param integer network interface id
  863. * @return void
  864. */
  865. private function show_template_network_interface_edit($id) {
  866. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  867. onapp_debug('id => ' . $id);
  868. onapp_permission(array('networks.update', 'networks'));
  869. $virtual_machine_id = onapp_get_arg('virtual_machine_id');
  870. $onapp = $this->get_factory();
  871. $network_interface_obj = $this->load('VirtualMachine_NetworkInterface', array($id, $virtual_machine_id));
  872. $vm_obj = $this->load('VirtualMachine', array($virtual_machine_id));
  873. $hypervisor_obj = $this->load('Hypervisor', array($vm_obj->_hypervisor_id));
  874. $hypervisor_zone_obj = $this->load('HypervisorZone', array($hypervisor_obj->_hypervisor_group_id));
  875. $network_join_obj = $this->getList('Hypervisor_NetworkJoin', array($vm_obj->_hypervisor_id));
  876. $networkzone_join_obj = $this->getList('HypervisorZone_NetworkJoin', array($hypervisor_obj->_hypervisor_group_id));
  877. $network = $onapp->factory('Network', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  878. if (is_array($network_join_obj)) {
  879. foreach ($network_join_obj as $network_join) {
  880. $network_obj = $network->load($network_join->_network_id);
  881. $network_obj_array[$network_join->_id] = $network_obj;
  882. $network_labels[$network_join->_id] = $network_obj;
  883. $target_labels[$network_join->_id] = $hypervisor_obj->_label;
  884. }
  885. }
  886. if (is_array($networkzone_join_obj)) {
  887. foreach ($networkzone_join_obj as $network_join) {
  888. $network_obj = $network->load($network_join->_network_id);
  889. $network_obj_array[$network_join->_id] = $network_obj;
  890. $network_labels[$network_join->_id] = $network_obj;
  891. $target_labels[$network_join->_id] = $hypervisor_zone_obj->_label;
  892. }
  893. }
  894. $params = array(
  895. 'target_labels' => $target_labels,
  896. 'hypervisor_label' => $hypervisor_obj->_label,
  897. 'network_interface_obj' => $network_interface_obj,
  898. 'network_obj' => $network_obj_array,
  899. 'id' => $id,
  900. 'title' => onapp_string('EDIT_NETWORK_INTERFACE'),
  901. 'info_title' => onapp_string('EDIT_NETWORK_INTERFACE'),
  902. 'info_body' => onapp_string('EDIT_NETWORK_INTERFACE_INFO'),
  903. );
  904. onapp_show_template('vm_networkInterfaceEdit', $params);
  905. }
  906. /**
  907. * Shows 'Create network interface' page
  908. *
  909. * @param integer virtual machine id
  910. * @return void
  911. */
  912. private function show_template_network_interface_create($id) {
  913. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  914. onapp_debug('id => ' . $id);
  915. $onapp = $this->get_factory();
  916. $vm_obj = $this->load('VirtualMachine', array($id));
  917. $hypervisor_obj = $this->load('Hypervisor', array($vm_obj->_hypervisor_id));
  918. $hypervisor_zone_obj = $this->load('HypervisorZone', array($hypervisor_obj->_hypervisor_group_id));
  919. $network_join_obj = $this->getList('Hypervisor_NetworkJoin', array($vm_obj->_hypervisor_id));
  920. $networkzone_join_obj = $this->getList('HypervisorZone_NetworkJoin', array($hypervisor_obj->_hypervisor_group_id));
  921. $network = $onapp->factory('Network', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  922. if (is_array($network_join_obj)) {
  923. foreach ($network_join_obj as $network_join) {
  924. $network_obj = $network->load($network_join->_network_id);
  925. $network_obj_array[$network_join->_id] = $network_obj;
  926. $network_labels[$network_join->_id] = $network_obj;
  927. $target_labels[$network_join->_id] = $hypervisor_obj->_label;
  928. }
  929. }
  930. if (is_array($networkzone_join_obj)) {
  931. foreach ($networkzone_join_obj as $network_join) {
  932. $network_obj = $network->load($network_join->_network_id);
  933. $network_obj_array[$network_join->_id] = $network_obj;
  934. $network_labels[$network_join->_id] = $network_obj;
  935. $target_labels[$network_join->_id] = $hypervisor_zone_obj->_label;
  936. }
  937. }
  938. $params = array(
  939. 'target_labels' => $target_labels,
  940. 'hypervisor_label' => $hypervisor_obj->_label,
  941. 'network_obj' => $network_obj_array,
  942. 'virtual_machine_id' => $id,
  943. 'title' => onapp_string('ADD_NEW_NETWORK_INTERFACE'),
  944. 'info_title' => onapp_string('ADD_NEW_NETWORK_INTERFACE'),
  945. 'info_body' => onapp_string('ADD_NEW_NETWORK_INTERFACE_INFO'),
  946. );
  947. onapp_show_template('vm_networkInterfaceCreate', $params);
  948. }
  949. /**
  950. * Shows virtual machine resource allocation page
  951. *
  952. * @param integer virtual machine id
  953. * @return void
  954. */
  955. private function show_template_edit($id) {
  956. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  957. onapp_debug('id => ' . $id);
  958. onapp_permission(array('virtual_machines', 'virtual_machines.update.own', 'virtual_machines.update'));
  959. $params = array(
  960. 'vm_obj' => $this->load('VirtualMachine', array($id)),
  961. 'title' => onapp_string('ADJUST_RESOURCE_ALLOCATIONS'),
  962. 'info_title' => onapp_string('ADJUST_RESOURCE_ALLOCATIONS'),
  963. 'info_body' => onapp_string('ADJUST_RESOURCE_ALLOCATIONS_INFO'),
  964. );
  965. onapp_show_template('vm_edit', $params);
  966. }
  967. /**
  968. * Creates a new virtual machine
  969. *
  970. * @global array $_ALIASES menu page aliases
  971. * @param object $onapp OnApp object
  972. * @return void
  973. */
  974. private function create() {
  975. global $_ALIASES;
  976. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  977. onapp_permission(array('virtual_machines', 'virtual_machines.create'));
  978. $onapp = $this->get_factory();
  979. $virtual_machine = onapp_get_arg('virtual_machine');
  980. $vm_obj = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  981. foreach ($virtual_machine as $key => $value)
  982. if ($virtual_machine[$key] != '' && !is_null($virtual_machine[$key]))
  983. $vm_obj->$key = $value;
  984. $vm_obj->save();
  985. if (is_null($vm_obj->error)) {
  986. $_SESSION['message'] = 'VIRTUAL_MACHINE_HAS_BEEN_CREATED_SUCCESSFULLY';
  987. onapp_event_exec( 'vm_create', array( $vm_obj->_obj, $this->load( 'User', array($vm_obj->_obj->_user_id ) ) ) );
  988. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $vm_obj->_id);
  989. }
  990. else {
  991. onapp_event_exec( 'vm_create_failed', array( $vm_obj->_obj, $this->load( 'User', array($vm_obj->_obj->_user_id ) ) ) );
  992. trigger_error ( print_r( $vm_obj->error, true ) );
  993. $this->show_template_view($vm_obj->error);
  994. }
  995. }
  996. /**
  997. * Startups virtual machine
  998. *
  999. * @global array $_ALIASES menu page aliases
  1000. * @param integer virtual machine Id
  1001. * @return void
  1002. */
  1003. private function startup($id) {
  1004. global $_ALIASES;
  1005. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1006. onapp_debug('id => ' . $id);
  1007. onapp_permission(array('virtual_machines', 'virtual_machines.power.own', 'virtual_machines.power'));
  1008. $onapp = $this->get_factory();
  1009. $virtual_machine = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1010. $mode = onapp_get_arg('mode');
  1011. switch ($mode) {
  1012. case 'recovery':
  1013. $virtual_machine->_id = $id;
  1014. $virtual_machine->startup(true);
  1015. break;
  1016. default:
  1017. $virtual_machine->_id = $id;
  1018. $virtual_machine->startup();
  1019. break;
  1020. }
  1021. if (is_null($virtual_machine->error)) {
  1022. onapp_event_exec( 'vm_startup', array( $virtual_machine->_obj, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1023. $_SESSION['message'] = 'VIRTUAL_MACHINE_STARTUP_HAS_BEEN_QUEUED';
  1024. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1025. }
  1026. else {
  1027. onapp_event_exec( 'vm_startup_failed', array( $virtual_machine, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1028. trigger_error ( print_r( $virtual_machine->error, true ) );
  1029. $this->show_template_view($virtual_machine->error);
  1030. }
  1031. }
  1032. /**
  1033. * Resets Virtual Machine Root Password
  1034. *
  1035. * @global array $_ALIASES menu page aliases
  1036. * @param integer virtual machine id
  1037. * @return void
  1038. */
  1039. private function reset_password($id) {
  1040. global $_ALIASES;
  1041. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1042. onapp_debug('id => ' . $id);
  1043. onapp_permission(array(
  1044. 'virtual_machines',
  1045. 'virtual_machines.reset_root_password',
  1046. 'virtual_machines.reset_root_password.own'
  1047. ));
  1048. $onapp = $this->get_factory();
  1049. $virtual_machine = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1050. $virtual_machine->_id = $id;
  1051. $virtual_machine->reset_password();
  1052. if (is_null($virtual_machine->error)) {
  1053. onapp_event_exec( 'vm_reset_password', array( $virtual_machine->_obj, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1054. $_SESSION['message'] = 'VIRTUAL_MACHINE_PASSWORD_WIL_BE_CHANGED_SHORTLY';
  1055. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1056. }
  1057. else {
  1058. onapp_event_exec( 'vm_reset_password_failed', array( $virtual_machine, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1059. trigger_error ( print_r( $virtual_machine->error, true ) );
  1060. $this->show_template_view($virtual_machine->error);
  1061. }
  1062. }
  1063. /**
  1064. * Reboots Virtual Machine
  1065. *
  1066. * @global array $_ALIASES menu page aliases
  1067. * @param integer virtual machine id
  1068. * @return void
  1069. */
  1070. private function reboot($id) {
  1071. global $_ALIASES;
  1072. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1073. onapp_debug('id => ' . $id);
  1074. onapp_permission(array('virtual_machines', 'virtual_machines.power.own', 'virtual_machines.power'));
  1075. $onapp = $this->get_factory();
  1076. $virtual_machine = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1077. $mode = onapp_get_arg('mode');
  1078. switch ($mode) {
  1079. case 'recovery':
  1080. $virtual_machine->_id = $id;
  1081. $virtual_machine->reboot(true);
  1082. break;
  1083. default:
  1084. $virtual_machine->_id = $id;
  1085. $virtual_machine->reboot();
  1086. break;
  1087. }
  1088. if (is_null($virtual_machine->error)) {
  1089. onapp_event_exec( 'vm_reboot', array( $virtual_machine->_obj, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1090. $_SESSION['message'] = 'VIRTUAL_MACHINE_WILL_BE_REBOOTED_SHORTLY';
  1091. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1092. }
  1093. else {
  1094. onapp_event_exec( 'vm_reboot_failed',

Large files files are truncated, but you can click here to view the full file