PageRenderTime 53ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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', array( $virtual_machine->_obj, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1095. trigger_error ( print_r( $virtual_machine->error, true ) );
  1096. $this->show_template_view($virtual_machine->error);
  1097. }
  1098. }
  1099. /**
  1100. * Deletes Virtual Machine
  1101. *
  1102. * @global array $_ALIASES menu page aliases
  1103. * @param integer virtual machine id
  1104. * @return void
  1105. */
  1106. private function delete($id) {
  1107. global $_ALIASES;
  1108. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1109. onapp_debug('id => ' . $id);
  1110. onapp_permission(array('virtual_machines', 'virtual_machines.delete', 'virtual_machines.delete.own'));
  1111. $onapp = $this->get_factory();
  1112. $virtual_machine = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1113. $virtual_machine->_id = $id;
  1114. $virtual_machine->delete();
  1115. if (is_null($virtual_machine->error)) {
  1116. onapp_event_exec( 'vm_delete' );
  1117. $_SESSION['message'] = 'VIRTUAL_MACHINE_HAVE_BEEN_SQUEDULED_FOR_DESTRUCTION';
  1118. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1119. }
  1120. else {
  1121. onapp_event_exec( 'vm_delete_failed' );
  1122. trigger_error ( print_r( $virtual_machine->error, true ) );
  1123. $this->show_template_view($virtual_machine->error);
  1124. }
  1125. }
  1126. /**
  1127. * Disables virtual machine disk autobackups
  1128. *
  1129. * @global array $_ALIASES menu page aliases
  1130. * @param integer virtual machine disk id
  1131. * @return void
  1132. */
  1133. private function autobackup_disable($id) {
  1134. global $_ALIASES;
  1135. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1136. onapp_debug('id => ' . $id);
  1137. onapp_permission(array('disks', 'disks.autobackup', 'disks.autobackup.own'));
  1138. $onapp = $this->get_factory();
  1139. $disk = $onapp->factory('Disk', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1140. $disk->disableAutobackup($id);
  1141. if (is_null($disk->error)) {
  1142. onapp_event_exec( 'disk_autobackup_disable', array( $disk->_obj ) );
  1143. $_SESSION['message'] = 'AUTOBACKUP_HAS_BEEN_DISABLED_FOR_THIS_DISK';
  1144. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=disks&id=' . onapp_get_arg('virtual_machine_id'));
  1145. }
  1146. else {
  1147. onapp_event_exec( 'disk_autobackup_disable_failed', array( $disk->_obj ) );
  1148. trigger_error ( print_r( $disk->error, true ) );
  1149. $this->show_template_disks(onapp_get_arg('virtual_machine_id'), $disk->error);
  1150. }
  1151. }
  1152. /**
  1153. * Enables virtual machine disk autobackups
  1154. *
  1155. * @global array $_ALIASES menu page aliases
  1156. * @param integer virtual machine disk id
  1157. * @return void
  1158. */
  1159. private function autobackup_enable($id) {
  1160. global $_ALIASES;
  1161. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1162. onapp_debug('id => ' . $id);
  1163. onapp_permission(array('disks', 'disks.autobackup', 'disks.autobackup.own'));
  1164. $onapp = $this->get_factory();
  1165. $disk = $onapp->factory('Disk', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1166. $disk->enableAutobackup($id);
  1167. if (is_null($disk->error)) {
  1168. onapp_event_exec( 'disk_autobackup_enable', array( $disk->_obj ) );
  1169. $_SESSION['message'] = 'AUTOBACKUP_HAS_BEEN_ENABLED_FOR_THIS_DISK';
  1170. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=disks&id=' . onapp_get_arg('virtual_machine_id'));
  1171. }
  1172. else {
  1173. onapp_event_exec( 'disk_autobackup_enable_failed', array( $disk->_obj ) );
  1174. trigger_error ( print_r( $disk->error, true ) );
  1175. $this->show_template_disks(onapp_get_arg('virtual_machine_id'), $disk->error);
  1176. }
  1177. }
  1178. /**
  1179. * Shutdowns Virtual Machine
  1180. *
  1181. * @global array $_ALIASES menu page aliases
  1182. * @param integer virtual machine id
  1183. * @return void
  1184. */
  1185. private function shutdown($id) {
  1186. global $_ALIASES;
  1187. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1188. onapp_debug('id => ' . $id);
  1189. onapp_permission(array('virtual_machines', 'virtual_machines.power', 'virtual_machines.power.own'));
  1190. $onapp = $this->get_factory();
  1191. $virtual_machine = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1192. $virtual_machine->_id = $id;
  1193. $virtual_machine->shutdown();
  1194. onapp_debug('virtual_machine => ' . print_r($virtual_machine, true));
  1195. if (is_null($virtual_machine->error)) {
  1196. onapp_event_exec( 'vm_shutdown', array( $virtual_machine->_obj, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1197. $_SESSION['message'] = 'VIRTUAL_MACHINE_WILL_BE_STOPED_SHORTLY';
  1198. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1199. }
  1200. else {
  1201. onapp_event_exec( 'vm_shutdown_failed', array( $virtual_machine->_obj, $this->load( 'User', array( $virtual_machine_id->_obj->_user_id ) ) ) );
  1202. trigger_error ( print_r( $virtual_machine->error, true ) );
  1203. $this->show_template_view($virtual_machine->error);
  1204. }
  1205. }
  1206. /**
  1207. * Suspends virtual machine
  1208. *
  1209. * @global array $_ALIASES menu page aliases
  1210. * @param integer virtual machine id
  1211. * @return void
  1212. */
  1213. private function suspend($id) {
  1214. global $_ALIASES;
  1215. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1216. onapp_debug('id => ' . $id);
  1217. onapp_permission(array('virtual_machines', 'virtual_machines.suspend'));
  1218. $onapp = $this->get_factory();
  1219. $virtual_machine = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1220. $virtual_machine->_id = $id;
  1221. $event = ( $virtual_machine->_suspended ) ? 'vm_unsuspend' : 'vm_suspend' ;
  1222. $virtual_machine->suspend();
  1223. if (is_null($virtual_machine->error)) {
  1224. onapp_event_exec( $event, array( $virtual_machine->_obj, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1225. $_SESSION['message'] = 'VIRTUAL_MACHINE_WILL_BE_STOPED_SHORTLY';
  1226. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1227. }
  1228. else {
  1229. onapp_event_exec( $event.'_failed', array( $virtual_machine->_obj, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1230. trigger_error ( print_r( $virtual_machine->error, true ) );
  1231. $this->show_template_view($virtual_machine->error);
  1232. }
  1233. }
  1234. /**
  1235. * Builds virtual machine
  1236. *
  1237. * @global array $_ALIASES menu page aliases
  1238. * @param integer virtual machine id
  1239. * @return void
  1240. */
  1241. private function build($id) {
  1242. global $_ALIASES;
  1243. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1244. onapp_debug('id => ' . $id);
  1245. onapp_permission(array('virtual_machines', 'virtual_machines.create'));
  1246. $onapp = $this->get_factory();
  1247. $virtual_machine = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1248. $virtual_machine->_id = $id;
  1249. $virtual_machine->build(); //print('<pre>'); print_r($virtual_machine); die();
  1250. onapp_debug('virtual_machine => ' . print_r($virtual_machine, true));
  1251. if (is_null($virtual_machine->error)) {
  1252. onapp_event_exec( 'vm_build', array( $virtual_machine->_obj, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1253. $_SESSION['message'] = 'VIRTUAL_MACHINE_BUILD_HAS_BEEN_QUEUED';
  1254. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1255. }
  1256. else {
  1257. onapp_event_exec( 'vm_build_failed', array( $virtual_machine->_obj, $this->load( 'User', array( $virtual_machine->_obj->_user_id ) ) ) );
  1258. trigger_error ( print_r( $virtual_machine->error, true ) );
  1259. $this->show_template_view($virtual_machine->error);
  1260. }
  1261. }
  1262. /**
  1263. * Deletes virtual machine backup
  1264. *
  1265. * @global array $_ALIASES menu page aliases
  1266. * @param integer backup id
  1267. * @return void
  1268. */
  1269. private function backup_delete($id) {
  1270. global $_ALIASES;
  1271. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1272. onapp_debug('id => ' . $id);
  1273. onapp_permission(array('backups.delete', 'backups', 'backups.delete.own'));
  1274. $onapp = $this->get_factory();
  1275. $backup = $onapp->factory('VirtualMachine_Backup', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1276. $backup->_id = $id;
  1277. $backup_obj = $backup->delete($id);
  1278. onapp_debug('backup_obj => ' . print_r($backup_obj, true));
  1279. if (is_null($backup->error)) {
  1280. onapp_event_exec( 'backup_delete' );
  1281. $_SESSION['message'] = 'BACKUP_HAS_BEEN_SQUEDULED_FOR_REMOVAL';
  1282. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines']);
  1283. }
  1284. else {
  1285. onapp_event_exec( 'backup_delete_failed' );
  1286. trigger_error ( print_r( $backup->error, true ) );
  1287. $this->show_template_view($backup->error);
  1288. }
  1289. }
  1290. /**
  1291. * Takes disk backup
  1292. *
  1293. * @global array $_ALIASES menu page aliases
  1294. * @param integer disk id
  1295. * @return void
  1296. */
  1297. private function backup_take($id) {
  1298. global $_ALIASES;
  1299. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1300. onapp_debug('id => ' . $id);
  1301. onapp_permission(array('backups.create', 'backups', 'backups.create.own'));
  1302. $onapp = $this->get_factory();
  1303. $disk = $onapp->factory('Disk', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1304. $disk->takeBackup($id);
  1305. onapp_debug('disk => ' . print_r($disk, true));
  1306. if (is_null($disk->error)) {
  1307. onapp_event_exec( 'backup_take', array( $disk->_obj ) );
  1308. $_SESSION['message'] = 'BACKUP_HAS_BEEN_CREATED_AND_WILL_BE_TAKEN_SHORTLY';
  1309. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=disk_backups&id=' . $id . '&virtual_machine_id=' . onapp_get_arg('virtual_machine_id'));
  1310. }
  1311. else {
  1312. onapp_event_exec( 'backup_take_failed', array( $disk->_obj ) );
  1313. trigger_error ( print_r( $disk->error, true ) );
  1314. $this->show_template_disk_backup(onapp_get_arg('virtual_machine_id'), $disk->error);
  1315. }
  1316. }
  1317. /**
  1318. * Deletes virtual machine firewall rule
  1319. *
  1320. * @global array $_ALIASES menu page aliases
  1321. * @param integer firewall rule id
  1322. * @return void
  1323. */
  1324. private function firewall_rule_delete($id) {
  1325. global $_ALIASES;
  1326. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1327. onapp_debug('id => ' . $id);
  1328. onapp_permission(array('firewall_rules.delete', 'firewall_rules', 'firewall_rules.delete.own'));
  1329. $onapp = $this->get_factory();
  1330. $virtual_machine_id = onapp_get_arg('virtual_machine_id');
  1331. $firewall = $onapp->factory('VirtualMachine_FirewallRule', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1332. $firewall->_id = $id;
  1333. $firewall->_virtual_machine_id = $virtual_machine_id;
  1334. $firewall_obj = $firewall->delete($id);
  1335. onapp_debug('firewall_obj => ' . print_r($firewall_obj, true));
  1336. if (is_null($firewall->error)) {
  1337. onapp_event_exec( 'firewall_rule_delete', array( $firewall->_obj ) );
  1338. $_SESSION['message'] = 'RULE_HAS_BEEN_DESTROYED';
  1339. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=firewall&id=' . $virtual_machine_id);
  1340. }
  1341. else {
  1342. onapp_event_exec( 'firewall_rule_delete_failed', array( $firewall->_obj ) );
  1343. trigger_error ( print_r( $firewall->error, true ) );
  1344. $this->show_template_firewall($virtual_machine_id, $firewall->error);
  1345. }
  1346. }
  1347. /**
  1348. * Restores virtual machine backup
  1349. *
  1350. * @global array $_ALIASES menu page aliases
  1351. * @param integer backup id
  1352. * @return void
  1353. */
  1354. private function backup_restore($id) {
  1355. global $_ALIASES;
  1356. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1357. onapp_debug('id => ' . $id);
  1358. onapp_permission(array('backups.convert', 'backups.convert.own', 'backups'));
  1359. $onapp = $this->get_factory();
  1360. $backup = $onapp->factory('VirtualMachine_Backup', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1361. $backup->_id = $id;
  1362. $backup_obj = $backup->restore();
  1363. onapp_debug( '$backup_obj => ' . print_r( $backup_obj, true) );
  1364. if (is_null($backup->error)) {
  1365. onapp_event_exec( 'backup_restore', array( $backup->_obj ) );
  1366. $_SESSION['message'] = 'BACKUP_HAS_BEEN_SQUEDULED_FOR_RESTORE';
  1367. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines']);
  1368. }
  1369. else {
  1370. onapp_event_exec( 'backup_restore_failed', array( $backup->_obj ) );
  1371. trigger_error ( print_r( $backup->error, true ) );
  1372. $this->show_template_view($backup->error);
  1373. }
  1374. }
  1375. /**
  1376. * Converts virtual machine backup to template
  1377. *
  1378. * @global array $_ALIASES menu page aliases
  1379. * @param integer backup id
  1380. * @return void
  1381. */
  1382. private function backup_convert( $id ) {
  1383. global $_ALIASES;
  1384. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1385. onapp_debug('id => ' . $id);
  1386. onapp_permission(array('backups.convert', 'backups.convert.own', 'backups'));
  1387. $label = onapp_get_arg('label');
  1388. if (is_null($label))
  1389. $this->show_template_backup_convert($id);
  1390. else {
  1391. $onapp = $this->get_factory();
  1392. $backup = $onapp->factory('VirtualMachine_Backup', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1393. $backup->_id = $id;
  1394. $backup_obj = $backup->convert($label);
  1395. onapp_debug( '$backup_obj => ' . print_r( $backup_obj, true) );
  1396. if (is_null($backup->error)) {
  1397. onapp_event_exec( 'backup_convert', array( $backup->_obj ) );
  1398. $_SESSION['message'] = 'BACKUP_HAS_BEEN_SQUEDULED_FOR_CONVERTION';
  1399. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines']);
  1400. }
  1401. else {
  1402. onapp_event_exec( 'backup_convert_failed', array( $backup->_obj ) );
  1403. trigger_error ( print_r( $backup->error, true ) );
  1404. $this->show_template_view($backup->error);
  1405. }
  1406. }
  1407. }
  1408. /**
  1409. * Edits Administrator's note
  1410. *
  1411. * @global array $_ALIASES menu page aliases
  1412. * @param integer virtual machine id
  1413. * @return void
  1414. */
  1415. private function edit_admin_note($id) {
  1416. global $_ALIASES;
  1417. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1418. onapp_debug('id => ' . $id);
  1419. onapp_permission('virtual_machines', 'sysadmin_tools');
  1420. $note = onapp_get_arg('note');
  1421. if (is_null($note))
  1422. $this->show_template_edit_admin_note($id);
  1423. else {
  1424. $onapp = $this->get_factory();
  1425. $vm = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1426. $vm->editAdminNote($id, $note);
  1427. onapp_debug( 'vm => ' . print_r( $vm, true ) );
  1428. if (is_null($vm->error)) {
  1429. onapp_event_exec( 'admin_note_edit', array( $vm->_obj ) );
  1430. $_SESSION['message'] = 'RESOURCES_UPDATED_SUCCESSFULLY';
  1431. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1432. }
  1433. else {
  1434. onapp_event_exec( 'admin_note_edit_failed', array( $vm->_obj ) );
  1435. trigger_error ( print_r( $vm->error, true ) );
  1436. $this->show_template_view($id, $vm->error);
  1437. }
  1438. }
  1439. }
  1440. /**
  1441. * Edits virtual machine disk
  1442. *
  1443. * @global array $_ALIASES menu page aliases
  1444. * @param integer virtual machine disk id
  1445. * @return void
  1446. */
  1447. private function disk_edit($id) {
  1448. global $_ALIASES;
  1449. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1450. onapp_debug('id => ' . $id);
  1451. onapp_permission(array('disks', 'disks.update', 'disks.update.own'));
  1452. $disk = onapp_get_arg('disk');
  1453. if (is_null($disk))
  1454. $this->show_template_disk_edit($id);
  1455. else {
  1456. $onapp = $this->get_factory();
  1457. $disk_obj = $onapp->factory('Disk', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1458. foreach ($disk as $key => $value)
  1459. $disk_obj->$key = $value;
  1460. $disk_obj->save();
  1461. if (is_null($disk_obj->error)) {
  1462. onapp_event_exec( 'disk_edit', array( $disk_obj->_obj ) );
  1463. $_SESSION['message'] = 'DISK_RESIZE_HAS_BEEN_REQUESTED';
  1464. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=disks&id=' . onapp_get_arg('virtual_machine_id'));
  1465. }
  1466. else {
  1467. onapp_event_exec( 'disk_edit_failed', array( $disk_obj->_obj ) );
  1468. trigger_error ( print_r( $disk_obj->error, true ) );
  1469. $this->show_template_view($disk_obj->error);
  1470. }
  1471. }
  1472. }
  1473. /**
  1474. * Edits disk backups schedule
  1475. *
  1476. * @global array $_ALIASES menu page aliases
  1477. * @param integer virtual machine disk id
  1478. * @return void
  1479. */
  1480. private function disk_backups_schedule_edit($id) {
  1481. global $_ALIASES;
  1482. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1483. onapp_debug('id => ' . $id);
  1484. onapp_permission(array('schedules', 'schedules.update'));
  1485. $schedule = onapp_get_arg('schedule');
  1486. if (is_null($schedule))
  1487. $this->show_template_disk_backups_schedule_edit($id);
  1488. else {
  1489. $onapp = $this->get_factory();
  1490. $schedule_obj = $onapp->factory('Disk_Schedule', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1491. foreach ($schedule as $key => $value)
  1492. $schedule_obj->$key = $value;
  1493. $schedule_obj->save();
  1494. if (is_null($schedule_obj->error)) {
  1495. onapp_event_exec( 'schedule_edit', array( $schedule_obj->_obj ) );
  1496. $_SESSION['message'] = 'SCHEDULE_WAS_SUCCESSFULLY_UPDATED';
  1497. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=disk_backups_schedule&id=' . $id);
  1498. }
  1499. else {
  1500. onapp_event_exec( 'schedule_edit_failed', array( $schedule_obj->_obj ) );
  1501. trigger_error ( print_r( $schedule_obj->error, true ) );
  1502. $this->show_template_disk_backups_schedule($id, $schedule_obj->error);
  1503. }
  1504. }
  1505. }
  1506. /**
  1507. * Edits virtual machine firewall rule
  1508. *
  1509. * @global array $_ALIASES menu page aliases
  1510. * @param integer virtual machine firewall rule id
  1511. * @return void
  1512. */
  1513. private function firewall_rule_edit($id) {
  1514. global $_ALIASES;
  1515. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1516. onapp_debug('id => ' . $id);
  1517. onapp_permission(array('firewall_rules', 'firewall_rules.update', 'firewall_rules.update.own'));
  1518. $virtual_machine_id = onapp_get_arg('virtual_machine_id');
  1519. $firewall = onapp_get_arg('firewall');
  1520. if (is_null($firewall))
  1521. $this->show_template_firewall_rule_edit($id);
  1522. else {
  1523. $onapp = $this->get_factory();
  1524. $firewall_obj = $onapp->factory('VirtualMachine_FirewallRule', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1525. foreach ($firewall as $key => $value)
  1526. $firewall_obj->$key = $value;
  1527. $firewall_obj->save();
  1528. if (is_null($firewall->error)) {
  1529. onapp_event_exec( 'firewall_rule_edit', array( $firewall_obj->_obj ) );
  1530. $_SESSION['message'] = 'RULE_HAS_BEEN_UPDATED';
  1531. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=firewall&id=' . $virtual_machine_id);
  1532. }
  1533. else {
  1534. onapp_event_exec( 'firewall_rule_edit_failed', array( $firewall_obj->_obj ) );
  1535. trigger_error ( print_r( $firewall->error, true ) );
  1536. $this->show_template_disk_backups_schedule($virtual_machine_id, $firewall->error);
  1537. }
  1538. }
  1539. }
  1540. /**
  1541. * Creates new disk backups schedule
  1542. *
  1543. * @global array $_ALIASES menu page aliases
  1544. * @param integer virtual machine disk id
  1545. * @return void
  1546. */
  1547. private function disk_backups_schedule_create($id) {
  1548. global $_ALIASES;
  1549. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1550. onapp_debug('id => ' . $id);
  1551. onapp_permission(array('schedules', 'schedules.create'));
  1552. $schedule = onapp_get_arg('schedule');
  1553. if (is_null($schedule))
  1554. $this->show_template_disk_backups_schedule_create($id);
  1555. else {
  1556. $onapp = $this->get_factory();
  1557. $schedule_obj = $onapp->factory('Disk_Schedule', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1558. foreach ($schedule as $key => $value)
  1559. $schedule_obj->$key = $value;
  1560. $schedule_obj->save();
  1561. if (is_null($schedule_obj->error)) {
  1562. onapp_event_exec( 'schedule_create', array( $schedule_obj->_obj ) );
  1563. $_SESSION['message'] = 'SCHEDULE_HAS_BEEN_CREATED';
  1564. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=disk_backups_schedule&id=' . $id);
  1565. }
  1566. else {
  1567. onapp_event_exec( 'schedule_create_failed', array( $schedule_obj->_obj ) );
  1568. trigger_error ( print_r( $schedule_obj->error, true ) );
  1569. $this->show_template_disk_backups_schedule($id, $schedule_obj->error);
  1570. }
  1571. }
  1572. }
  1573. /**
  1574. * Creates firewall rule for virtual machine
  1575. *
  1576. * @global array $_ALIASES menu page aliases
  1577. * @param integer virtual machine id
  1578. * @return void
  1579. */
  1580. private function firewall_rule_create($id) {
  1581. global $_ALIASES;
  1582. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1583. onapp_debug('id => ' . $id);
  1584. onapp_permission(array('firewall_rules', 'firewall_rules.create'));
  1585. $firewall = onapp_get_arg('firewall');
  1586. $onapp = $this->get_factory();
  1587. $firewall_obj = $onapp->factory('VirtualMachine_FirewallRule', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1588. foreach ($firewall as $key => $value)
  1589. $firewall_obj->$key = $value;
  1590. $firewall_obj->save();
  1591. if (is_null($firewall_obj->error)) {
  1592. onapp_event_exec( 'firewall_rule_create', array( $firewall_obj->_obj ) );
  1593. $_SESSION['message'] = 'RULE_HAS_BEEN_ADDED';
  1594. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=firewall&id=' . $id);
  1595. }
  1596. else {
  1597. onapp_event_exec( 'firewall_rule_create_failed', array( $firewall_obj->_obj ) );
  1598. trigger_error ( print_r( $firewall_obj->error, true ) );
  1599. $this->show_template_firewall($id, $firewall_obj->error);
  1600. }
  1601. }
  1602. /**
  1603. * Updates default firewall rule for network interface
  1604. *
  1605. * @global array $_ALIASES menu page aliases
  1606. * @param integer virtual machine id
  1607. * @return void
  1608. */
  1609. private function firewall_rule_update_defaults($id) {
  1610. global $_ALIASES;
  1611. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1612. onapp_debug('id => ' . $id);
  1613. onapp_permission(array('networks', 'networks.update'));
  1614. $firewall = onapp_get_arg('firewall');
  1615. $onapp = $this->get_factory();
  1616. $firewall_obj = $onapp->factory('VirtualMachine_FirewallRule', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1617. $firewall_obj->updateDefaults($id, $firewall);
  1618. if (is_null($firewall_obj->error)) {
  1619. onapp_event_exec( 'firewall_rule_update', array( $firewall_obj->_obj ) );
  1620. $_SESSION['message'] = 'DEFAULT_RULES_HAVE_BEEN_UPDATED';
  1621. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=firewall&id=' . $id);
  1622. }
  1623. else {
  1624. onapp_event_exec( 'firewall_rule_update_failed', array( $firewall_obj->_obj ) );
  1625. trigger_error ( print_r( $firewall->error, true ) );
  1626. $this->show_template_firewall($id, $firewall->error);
  1627. }
  1628. }
  1629. /**
  1630. * Creates a new disk on virtual machine
  1631. *
  1632. * @global array $_ALIASES menu page aliases
  1633. * @param integer virtual machine id
  1634. * @return void
  1635. */
  1636. private function disk_create($id) {
  1637. global $_ALIASES;
  1638. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1639. onapp_debug('id => ' . $id);
  1640. onapp_permission(array('disks', 'disks.create'));
  1641. $disk = onapp_get_arg('disk');
  1642. if (is_null($disk))
  1643. $this->show_template_disk_create($id);
  1644. else {
  1645. $onapp = $this->get_factory();
  1646. $disk_obj = $onapp->factory('Disk', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1647. foreach ($disk as $key => $value)
  1648. $disk_obj->$key = $value;
  1649. $disk_obj->save();
  1650. onapp_debug('disk_obj => ' . print_r($disk_obj, true));
  1651. if (is_null($disk_obj->error)) {
  1652. onapp_event_exec( 'disk_create', array( $disk_obj->_obj ) );
  1653. $_SESSION['message'] = 'DISK_HAS_BEEN_ADDED_SUCCESSFULLY';
  1654. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=disks&id=' . $id);
  1655. }
  1656. else {
  1657. onapp_event_exec( 'disk_create_failed', array( $disk_obj->_obj ) );
  1658. trigger_error ( print_r( $disk_obj->error, true ) );
  1659. $this->show_template_view($disk_obj->error);
  1660. }
  1661. }
  1662. }
  1663. /**
  1664. * Changes virtual machine owner
  1665. *
  1666. * @global array $_ALIASES menu page aliases
  1667. * @param integer virtual machine id
  1668. * @return void
  1669. */
  1670. private function change_owner($id) {
  1671. global $_ALIASES;
  1672. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1673. onapp_debug('id => ' . $id);
  1674. onapp_permission(array('virtual_machines', 'virtual_machines.change_owner'));
  1675. $onapp = $this->get_factory();
  1676. $user_id = onapp_get_arg('user_id');
  1677. if (is_null($user_id))
  1678. $this->show_template_change_owner($id);
  1679. else {
  1680. $vm = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1681. $vm->_id = $id;
  1682. $vm->_user_id = $user_id;
  1683. $vm->change_owner($user_id);
  1684. onapp_debug('vm => ' . print_r($vm, true));
  1685. if (is_null($vm->error)) {
  1686. onapp_event_exec( 'change_owner', array( $vm->_obj ) );
  1687. $_SESSION['message'] = 'VIRTUAL_MACHINE_OWNER_HAS_BEEN_CHANGED_SUCCESSFULLY';
  1688. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1689. }
  1690. else {
  1691. onapp_event_exec( 'change_owner_failed', array( $vm->_obj ) );
  1692. trigger_error ( print_r( $vm->error, true ) );
  1693. $this->show_template_details($id, $vm->error);
  1694. }
  1695. }
  1696. }
  1697. /**
  1698. * Moves to the upper or lower position the virtual machine firewall rule
  1699. *
  1700. * @global array $_ALIASES menu page aliases
  1701. * @param integer virtual machine firewall rule id
  1702. * @return void
  1703. */
  1704. private function firewall_rule_move($id) {
  1705. global $_ALIASES;
  1706. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1707. onapp_debug('id => ' . $id);
  1708. onapp_permission(array('firewall_rules.update', 'firewall_rules.update.own', 'firewall_rules'));
  1709. $onapp = $this->get_factory();
  1710. $virtual_machine_id = onapp_get_arg('virtual_machine_id');
  1711. $position = onapp_get_arg('position');
  1712. $firewall = $onapp->factory('VirtualMachine_FirewallRule', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1713. $firewall->_virtual_machine_id = $virtual_machine_id;
  1714. $firewall->_id = $id;
  1715. $firewall->move($position);
  1716. onapp_debug('firewall => ' . print_r($firewall, true));
  1717. if (is_null($firewall->errors)) {
  1718. onapp_event_exec( 'firewall_rule_move', array( $firewall->_obj ) );
  1719. onapp_event_exec( 'vm_create', array( $firewall ) );
  1720. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=firewall&id=' . $virtual_machine_id);
  1721. }
  1722. else {
  1723. onapp_event_exec( 'firewall_rule_move_failed', array( $firewall->_obj ) );
  1724. trigger_error ( print_r( $firewall->error, true ) );
  1725. $this->show_template_firewall($virtual_machine_id, $firewall->error);
  1726. }
  1727. }
  1728. /**
  1729. * Edits network interface for virtual machine
  1730. *
  1731. * @global array $_ALIASES menu page aliases
  1732. * @param integer network_interface id
  1733. * @return void
  1734. */
  1735. private function network_interface_edit($id) {
  1736. global $_ALIASES;
  1737. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1738. onapp_debug('id => ' . $id);
  1739. onapp_permission(array('networks', 'networks.update'));
  1740. $network_interface = onapp_get_arg('network_interface');
  1741. if (is_null($network_interface))
  1742. $this->show_template_network_interface_edit($id);
  1743. else {
  1744. $onapp = $this->get_factory();
  1745. $network_interface_obj = $onapp->factory('VirtualMachine_NetworkInterface', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1746. foreach ($network_interface as $key => $value)
  1747. $network_interface_obj->$key = $value;
  1748. $network_interface_obj->save();
  1749. onapp_debug('network_interface_obj => ' . print_r($network_interface_obj, true));
  1750. if (is_null($network_interface_obj->error)) {
  1751. onapp_event_exec( 'network_interface_edit', array( $network_interface_obj->_obj ) );
  1752. $_SESSION['message'] = 'NETWORK_INTERFACE_HAVE_BEEN_UPDATED_SUCCESSFULLY';
  1753. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=network_interfaces&id=' . $network_interface['_virtual_machine_id']);
  1754. }
  1755. else {
  1756. onapp_event_exec( 'network_interface_edit_failed', array( $network_interface_obj->_obj ) );
  1757. trigger_error ( print_r( $network_interface_obj->error, true ) );
  1758. $this->show_template_network_interface($network_interface['_virtual_machine_id'], $network_interface_obj->error);
  1759. }
  1760. }
  1761. }
  1762. /**
  1763. * Adjust virtual machine resource allocation
  1764. *
  1765. * @global array $_ALIASES menu page aliases
  1766. * @param integer virtual machine id
  1767. * @return void
  1768. */
  1769. private function edit($id) {
  1770. global $_ALIASES;
  1771. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1772. onapp_debug('id => ' . $id);
  1773. onapp_permission(array('virtual_machines', 'virtual_machines.update', 'virtual_machines.update.own'));
  1774. $virtual_machine = onapp_get_arg('virtual_machine');
  1775. if (is_null($virtual_machine))
  1776. $this->show_template_edit($id);
  1777. else {
  1778. $onapp = $this->get_factory();
  1779. $vm = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1780. foreach ($virtual_machine as $key => $value)
  1781. $vm->$key = $value;
  1782. $vm->save();
  1783. onapp_debug('vm => ' . print_r($vm, true));
  1784. if (is_null($vm->error)) {
  1785. onapp_event_exec( 'vm_edit', array( $vm->_obj ) );
  1786. $_SESSION['message'] = 'RESOURCES_UPDATED_SUCCESSFULLY';
  1787. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1788. }
  1789. else {
  1790. onapp_event_exec( 'vm_edit_failed', array( $vm->_obj ) );
  1791. trigger_error ( print_r( $vm->error, true ) );
  1792. $this->show_template_details($id, $vm->error);
  1793. }
  1794. }
  1795. }
  1796. /**
  1797. * Creates network interface for virtual machine
  1798. *
  1799. * @global array $_ALIASES menu page aliases
  1800. * @param integer virtual machine id
  1801. * @return void
  1802. */
  1803. private function network_interface_create($id) {
  1804. global $_ALIASES;
  1805. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1806. onapp_debug('id => ' . $id);
  1807. onapp_permission(array('networks', 'networks.create'));
  1808. $network_interface = onapp_get_arg('network_interface');
  1809. if (is_null($network_interface))
  1810. $this->show_template_network_interface_create($id);
  1811. else {
  1812. $onapp = $this->get_factory();
  1813. $network_interface_obj = $onapp->factory('VirtualMachine_NetworkInterface', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1814. foreach ($network_interface as $key => $value)
  1815. $network_interface_obj->$key = $value;
  1816. $network_interface_obj->save();
  1817. onapp_debug('network_interface_obj => ' . print_r($network_interface_obj, true));
  1818. if (is_null($network_interface_obj->error)) {
  1819. onapp_event_exec( 'network_interface_create', array( $network_interface_obj->_obj ) );
  1820. $_SESSION['message'] = 'NETWORK_INTERFACE_HAS_BEEN_CREATED_SUCCESSFULLY';
  1821. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=network_interfaces&id=' . $id);
  1822. }
  1823. else {
  1824. onapp_event_exec( 'network_interface_create_failed', array( $network_interface_obj->_obj ) );
  1825. trigger_error ( print_r( $network_interface_obj->error, true ) );
  1826. $this->show_template_network_interface($id, $network_interface_obj->error);
  1827. }
  1828. }
  1829. }
  1830. /**
  1831. * Deletes virtual machine ip_address assignment
  1832. *
  1833. * @global array $_ALIASES menu page aliases
  1834. * @param integer ip_address id
  1835. * @return void
  1836. */
  1837. private function ip_address_delete($id) {
  1838. global $_ALIASES;
  1839. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1840. onapp_debug('id => ' . $id);
  1841. onapp_permission(array('ip_address_joins.delete', 'ip_address_joins', 'ip_address_joins.delete.own'));
  1842. $onapp = $this->get_factory();
  1843. $virtual_machine_id = onapp_get_arg('virtual_machine_id');
  1844. $ip_address = $onapp->factory('VirtualMachine_IpAddressJoin', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1845. $ip_address->_virtual_machine_id = $virtual_machine_id;
  1846. $ip_address->_id = $id;
  1847. $ip_address_obj = $ip_address->delete($id);
  1848. onapp_debug('ip_address_obj => ' . print_r($ip_address_obj, true));
  1849. if (is_null($ip_address->error)) {
  1850. onapp_event_exec( 'ip_address_delete', array( $ip_address_obj->_obj ) );
  1851. $_SESSION['message'] = 'IP_ADDRESS_ASSEGNMENT_HAS_BEEN_REMOVED_SUCCESSFULLY';
  1852. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=ip_addresses&id=' . $virtual_machine_id);
  1853. }
  1854. else {
  1855. onapp_event_exec( 'ip_address_delete_failed', array( $ip_address_obj->_obj ) );
  1856. trigger_error ( print_r( $ip_address->error, true ) );
  1857. $this->show_template_ip_addresses($virtual_machine_id, $ip_address->error);
  1858. }
  1859. }
  1860. /**
  1861. * Deletes network interface for virtual machine
  1862. *
  1863. * @global array $_ALIASES menu page aliases
  1864. * @param integer network interface id
  1865. * @return void
  1866. */
  1867. private function network_interface_delete($id) {
  1868. global $_ALIASES;
  1869. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1870. onapp_debug('id => ' . $id);
  1871. onapp_permission(array('networks.delete', 'networks'));
  1872. $virtual_machine_id = onapp_get_arg('virtual_machine_id');
  1873. $onapp = $this->get_factory();
  1874. $network_interface = $onapp->factory('VirtualMachine_NetworkInterface', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1875. $network_interface->_id = $id;
  1876. $network_interface->_virtual_machine_id = $virtual_machine_id;
  1877. $network_interface_obj = $network_interface->delete($id);
  1878. onapp_debug('network_interface => ' . print_r($network_interface, true));
  1879. if (is_null($network_interface->error)) {
  1880. onapp_event_exec( 'network_interface_delete', array( $network_interface->_obj ) );
  1881. $_SESSION['message'] = 'NETWORK_INTERFACE_WILL_BE_REMOVED_IN_A_MOMENT';
  1882. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=network_interfaces&id=' . $virtual_machine_id);
  1883. }
  1884. else {
  1885. onapp_event_exec( 'network_interface_delete_failed', array( $network_interface->_obj ) );
  1886. trigger_error ( print_r( $network_interface_obj->error, true ) );
  1887. $this->show_template_network_interface($virtual_machine_id, $network_interface_obj->error);
  1888. }
  1889. }
  1890. /**
  1891. * Deletes virtual machine disks
  1892. *
  1893. * @global array $_ALIASES menu page aliases
  1894. * @param integer virtual machine disk id
  1895. * @return void
  1896. */
  1897. private function disk_delete($id) {
  1898. global $_ALIASES;
  1899. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1900. onapp_debug('id => ' . $id);
  1901. onapp_permission(array('disks', 'disks.delete.own', 'disks.delete'));
  1902. $virtual_machine_id = onapp_get_arg('virtual_machine_id');
  1903. $onapp = $this->get_factory();
  1904. $disk = $onapp->factory('Disk', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1905. $disk->_id = $id;
  1906. $disk->_virtual_machine_id = $virtual_machine_id;
  1907. $disk->delete($id);
  1908. onapp_debug('disk => ' . print_r($disk, true));
  1909. if (is_null($disk->error)) {
  1910. onapp_event_exec( 'disk_delete', array( $disk->_obj ) );
  1911. $_SESSION['message'] = 'DISK_HAS_BEEN_SCHEDULED_FOR_DESTRUCTION';
  1912. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=disks&id=' . $virtual_machine_id);
  1913. }
  1914. else {
  1915. onapp_event_exec( 'disk_delete_failed', array( $disk->_obj ) );
  1916. trigger_error ( print_r( $network_interface_obj->error, true ) );
  1917. $this->show_template_network_interface($virtual_machine_id, $network_interface_obj->error);
  1918. }
  1919. }
  1920. /**
  1921. * Deletes disk backup schedule
  1922. *
  1923. * @global array $_ALIASES menu page aliases
  1924. * @param integer disk backup schedule id
  1925. * @return void
  1926. */
  1927. private function disk_backups_schedule_delete($id) {
  1928. global $_ALIASES;
  1929. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1930. onapp_debug('id => ' . $id);
  1931. onapp_permission(array('schedules', 'schedules.delete'));
  1932. $disk_id = onapp_get_arg('disk_id');
  1933. $onapp = $this->get_factory();
  1934. $schedule = $onapp->factory('Disk_Schedule', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1935. $schedule->_id = $id;
  1936. $schedule->delete($id);
  1937. onapp_debug('schedule => ' . print_r($schedule, true));
  1938. if (is_null($schedule->error)) {
  1939. onapp_event_exec( 'schedule_delete', array( $schedule->_obj ) );
  1940. $_SESSION['message'] = 'SCHEDULE_HAS_BEEN_DESTRUCTED';
  1941. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=disk_backups_schedule&id=' . $disk_id);
  1942. }
  1943. else {
  1944. onapp_event_exec( 'schedule_delete_failed', array( $schedule->_obj ) );
  1945. trigger_error ( print_r( $schedule->error, true ) );
  1946. $this->show_template_disk_backups_schedule($disk_id, $schedule->error);
  1947. }
  1948. }
  1949. /**
  1950. * Applies / Updates virtual machine firewall rules
  1951. *
  1952. * @global array $_ALIASES menu page aliases
  1953. * @param integer virtual machine id
  1954. * @return void
  1955. */
  1956. private function firewall_rules_apply($id) {
  1957. global $_ALIASES;
  1958. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1959. onapp_debug('id => ' . $id);
  1960. onapp_permission(array('firewall_rules', 'firewall_rules.update'));
  1961. $onapp = $this->get_factory();
  1962. $firewall = $onapp->factory('VirtualMachine_FirewallRule', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1963. $firewall->update($id);
  1964. onapp_debug('firewall => ' . print_r($firewall, true));
  1965. if (is_null($firewall->error)) {
  1966. onapp_event_exec( 'firewall_rules_apply', array( $firewall->_obj ) );
  1967. $_SESSION['message'] = 'AN_UPDATE_OF_FIREWALL_RULES_HAS_BEEN_QUEUED';
  1968. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  1969. }
  1970. else {
  1971. onapp_event_exec( 'firewall_rules_apply_failed', array( $firewall->_obj ) );
  1972. trigger_error ( print_r( $firewall->error, true ) );
  1973. $this->show_template_details($id, $firewall->error);
  1974. }
  1975. }
  1976. /**
  1977. * Rebuilds network for virtual machine
  1978. *
  1979. * @global array $_ALIASES menu page aliases
  1980. * @param integer virtual machine id
  1981. * @return void
  1982. */
  1983. private function rebuild_network($id) {
  1984. global $_ALIASES;
  1985. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  1986. onapp_debug('id => ' . $id);
  1987. onapp_permission(
  1988. array(
  1989. 'virtual_machines.rebuild_network',
  1990. 'virtual_machines.rebuild_network.own',
  1991. 'virtual_machines'
  1992. )
  1993. );
  1994. $onapp = $this->get_factory();
  1995. $virtual_machine = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  1996. $virtual_machine->_id = $id;
  1997. $virtual_machine->rebuild_network($id);
  1998. onapp_debug('virtual_machine => ' . print_r($virtual_machine, true));
  1999. if (is_null($virtual_machine->error)) {
  2000. onapp_event_exec( 'rebuild_netword', array( $virtual_machine->_obj ) );
  2001. $_SESSION['message'] = 'NETWORK_INTERFACE_WILL_BE_REBUILD_FOR_THIS_VIRTUAL_MACHINE';
  2002. onapp_redirect(
  2003. ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines']
  2004. . '?action=ip_addresses&id=' . $id
  2005. );
  2006. }
  2007. else {
  2008. onapp_event_exec( 'rebuild_netword_failed', array( $virtual_machine->_obj ) );
  2009. trigger_error ( print_r( $ip_address->error, true ) );
  2010. $this->show_template_ip_addresses($id, $ip_address->error);
  2011. }
  2012. }
  2013. /**
  2014. * Assigns new IP Address to virtual machine
  2015. *
  2016. * @global array $_ALIASES menu page aliases
  2017. * @param interger virtual machine id
  2018. * @return void
  2019. */
  2020. private function ip_address_join_new($id) {
  2021. global $_ALIASES;
  2022. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  2023. onapp_debug('id => ' . $id);
  2024. onapp_permission(array('ip_address_joins.create', 'ip_address_joins', 'ip_address_joins.create.own'));
  2025. $ip_address = onapp_get_arg('ip_address');
  2026. if (!$ip_address) {
  2027. $this->show_template_ip_address_join_new($id);
  2028. } else {
  2029. $onapp = $this->get_factory();
  2030. $ip_address_obj = $onapp->factory('VirtualMachine_IpAddress', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  2031. foreach ($ip_address as $key => $value)
  2032. $ip_address_obj->$key = $value;
  2033. $ip_address_obj->join();
  2034. onapp_debug('ip_address_obj => ' . print_r($ip_address_obj, true));
  2035. if (is_null($ip_address_obj->error)) {
  2036. onapp_event_exec( 'ip_address_join', array( $ip_address_obj->_obj ) );
  2037. $_SESSION['message'] = 'IP_ADDRESS_HAS_BEEN_ASSEGNED_SUCCESSFULLY';
  2038. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=ip_addresses&id=' . $id);
  2039. }
  2040. else {
  2041. onapp_event_exec( 'ip_address_join_failed', array( $ip_address_obj->_obj ) );
  2042. trigger_error ( print_r( $ip_address_obj->error, true ) );
  2043. $this->show_template_ip_addresses($id, $ip_address_obj->error);
  2044. }
  2045. }
  2046. }
  2047. /**
  2048. * Migrate VM to other Hypervisor
  2049. *
  2050. * @global array $_ALIASES menu page aliases
  2051. * @param interger virtual machine id
  2052. * @return void
  2053. */
  2054. private function migrate($id) {
  2055. global $_ALIASES;
  2056. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  2057. onapp_debug('id => ' . $id);
  2058. onapp_permission(array('virtual_machines', 'virtual_machines.migrate', 'virtual_machines.migrate.own'));
  2059. $virtual_machine = onapp_get_arg('virtual_machine');
  2060. if (!$virtual_machine) {
  2061. $this->show_template_migrate($id);
  2062. } else {
  2063. $onapp = $this->get_factory();
  2064. $vm = $onapp->factory('VirtualMachine', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  2065. $vm->migrate($id, $virtual_machine['_destination_id']); // print('<pre>');print_r($vm);die();
  2066. onapp_debug('vm => ' . print_r($vm, true));
  2067. if (is_null($vm->error)) {
  2068. onapp_event_exec( 'vm_migrate', array( $vm->_obj ) );
  2069. $_SESSION['message'] = 'VIRTUAL_MACHINE_MIGRATE_HAS_BEEN_QUEUED';
  2070. onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['virtual_machines'] . '?action=details&id=' . $id);
  2071. }
  2072. else {
  2073. onapp_event_exec( 'vm_migrate_failed', array( $vm->_obj ) );
  2074. trigger_error ( print_r( $vm->error, true ) );
  2075. $this->show_template_details($id, $vm->error);
  2076. }
  2077. }
  2078. }
  2079. /**
  2080. * Opens virtual machine console id
  2081. *
  2082. * @global array $_ALIASES menu page aliases
  2083. * @param interger virtual machine id
  2084. * @return void
  2085. */
  2086. private function console($id) {
  2087. global $_ALIASES;
  2088. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  2089. onapp_debug('id => ' . $id);
  2090. onapp_permission(array('virtual_machines', 'virtual_machines.console', 'virtual_machines.console.own'));
  2091. $onapp = $this->get_factory();
  2092. $console = $onapp->factory('Console', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
  2093. $console_obj = $console->load($id);
  2094. onapp_debug('console_obj => ' . print_r($console_obj, true));
  2095. $url = $_SESSION['host'] . ONAPP_DS . 'console_remote' . ONAPP_DS . $console->_obj->_remote_key;
  2096. if (is_null($console->error)) {
  2097. onapp_redirect($url);
  2098. } else {
  2099. trigger_error ( print_r( $vm->error, true ) );
  2100. $this->show_template_details($id, $vm->error);
  2101. }
  2102. }
  2103. /**
  2104. * Calculates backups' quantity and sizes
  2105. *
  2106. * @param object $vm_backup_obj Virtual Machine Backup object
  2107. * @return array Virtual Machine backups' quantity and sizes
  2108. */
  2109. private function calculateBackups($vm_backup_obj) {
  2110. onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
  2111. $backups_quantity = 0;
  2112. $backups_total_size = 0;
  2113. if ($vm_backup_obj && is_array($vm_backup_obj)) {
  2114. foreach ($vm_backup_obj as $value) {
  2115. $backups_quantity++;
  2116. $backups_total_size += $value->_backup_size;
  2117. }
  2118. $backups_total_size = round($backups_total_size / 1024);
  2119. } else if (count($vm_backup_obj) == 1 && !is_array($vm_backup_obj) && $vm_backup_obj->_id) {
  2120. $backups_quantity = 1;
  2121. $backups_total_size = round($vm_backup_obj->_backup_size / 1024);
  2122. }
  2123. return $size_and_quantity = array(
  2124. 'size' => $backups_total_size,
  2125. 'quantity' => $backups_quantity
  2126. );
  2127. }
  2128. /**
  2129. * Checks necessary access to this class
  2130. *
  2131. * @return boolean [true|false]
  2132. */
  2133. static function access() {
  2134. return onapp_has_permission(array('virtual_machines', 'virtual_machines.read.own'));
  2135. }
  2136. }