PageRenderTime 74ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/controllers/default/c_virtual_machines.php

https://github.com/Spencerx/OnApp-PHP-Frontend
PHP | 2571 lines | 1630 code | 504 blank | 437 comment | 135 complexity | 4469e4a87f06d6011395b7c20836f0ee MD5 | raw file

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

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

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