PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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