| 
									
										
										
										
											2017-05-09 16:33:56 +02:00
										 |  |  | import oca | 
					
						
							|  |  |  | import socket | 
					
						
							| 
									
										
										
										
											2017-05-12 12:13:18 -05:00
										 |  |  | import logging | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-09 16:33:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from django.conf import settings | 
					
						
							| 
									
										
										
										
											2017-05-11 12:45:09 +02:00
										 |  |  | from django.utils.functional import cached_property | 
					
						
							| 
									
										
										
										
											2017-05-09 16:33:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from oca.pool import WrongNameError | 
					
						
							| 
									
										
										
										
											2017-05-12 12:13:18 -05:00
										 |  |  | from oca.exceptions import OpenNebulaException | 
					
						
							|  |  |  | logger = logging.getLogger(__name__) | 
					
						
							| 
									
										
										
										
											2017-05-10 04:06:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-09 16:33:56 +02:00
										 |  |  | class OpenNebulaManager(): | 
					
						
							|  |  |  |     """This class represents an opennebula manager.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |     def __init__(self, email=None, password=None): | 
					
						
							| 
									
										
										
										
											2017-05-09 16:33:56 +02:00
										 |  |  |          | 
					
						
							|  |  |  |         # Get oneadmin client | 
					
						
							|  |  |  |         self.oneadmin_client = self._get_opennebula_client( | 
					
						
							|  |  |  |             settings.OPENNEBULA_USERNAME, | 
					
						
							|  |  |  |             settings.OPENNEBULA_PASSWORD | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-05-10 00:39:41 +02:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2017-05-09 16:33:56 +02:00
										 |  |  |         # Get or create oppenebula user using given credentials | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             self.opennebula_user = self._get_or_create_user( | 
					
						
							|  |  |  |                 email, | 
					
						
							|  |  |  |                 password | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             # If opennebula user was created/obtained, get his client | 
					
						
							| 
									
										
										
										
											2017-05-09 16:33:56 +02:00
										 |  |  |             self.client = self._get_opennebula_client( | 
					
						
							|  |  |  |                 email, | 
					
						
							|  |  |  |                 password | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |         except: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2017-05-09 16:33:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _get_opennebula_client(self, username, password): | 
					
						
							|  |  |  |         return oca.Client("{0}:{1}".format( | 
					
						
							|  |  |  |             username, | 
					
						
							|  |  |  |             password), | 
					
						
							|  |  |  |             "{protocol}://{domain}:{port}{endpoint}".format( | 
					
						
							|  |  |  |                 protocol=settings.OPENNEBULA_PROTOCOL, | 
					
						
							|  |  |  |                 domain=settings.OPENNEBULA_DOMAIN, | 
					
						
							|  |  |  |                 port=settings.OPENNEBULA_PORT, | 
					
						
							|  |  |  |                 endpoint=settings.OPENNEBULA_ENDPOINT | 
					
						
							|  |  |  |         )) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _get_or_create_user(self, email, password): | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2017-05-10 00:39:41 +02:00
										 |  |  |             user_pool = self._get_user_pool() | 
					
						
							| 
									
										
										
										
											2017-05-09 16:33:56 +02:00
										 |  |  |             opennebula_user = user_pool.get_by_name(email) | 
					
						
							|  |  |  |             return opennebula_user | 
					
						
							|  |  |  |         except WrongNameError as wrong_name_err: | 
					
						
							|  |  |  |             opennebula_user = self.oneadmin_client.call(oca.User.METHODS['allocate'], email, | 
					
						
							|  |  |  |                                                         password, 'core') | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |             logger.debug( | 
					
						
							|  |  |  |                  "User {0} does not exist. Created the user. User id = {1}", | 
					
						
							|  |  |  |                  email, | 
					
						
							|  |  |  |                  opennebula_user | 
					
						
							|  |  |  |              ) | 
					
						
							| 
									
										
										
										
											2017-05-10 02:49:03 +02:00
										 |  |  |             return opennebula_user | 
					
						
							| 
									
										
										
										
											2017-05-10 00:39:41 +02:00
										 |  |  |         except ConnectionRefusedError: | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |             logger.info('Could not connect to host: {host} via protocol {protocol}'.format( | 
					
						
							| 
									
										
										
										
											2017-05-10 00:39:41 +02:00
										 |  |  |                     host=settings.OPENNEBULA_DOMAIN, | 
					
						
							|  |  |  |                     protocol=settings.OPENNEBULA_PROTOCOL) | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  |             raise ConnectionRefusedError | 
					
						
							| 
									
										
										
										
											2017-05-09 17:15:12 +02:00
										 |  |  |     def _get_user_pool(self): | 
					
						
							| 
									
										
										
										
											2017-05-10 00:39:41 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             user_pool = oca.UserPool(self.oneadmin_client) | 
					
						
							|  |  |  |             user_pool.info() | 
					
						
							|  |  |  |         except ConnectionRefusedError: | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |             logger.info('Could not connect to host: {host} via protocol {protocol}'.format( | 
					
						
							| 
									
										
										
										
											2017-05-10 00:39:41 +02:00
										 |  |  |                     host=settings.OPENNEBULA_DOMAIN, | 
					
						
							|  |  |  |                     protocol=settings.OPENNEBULA_PROTOCOL) | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  |             raise ConnectionRefusedError | 
					
						
							| 
									
										
										
										
											2017-05-09 17:15:12 +02:00
										 |  |  |         return user_pool | 
					
						
							| 
									
										
										
										
											2017-05-10 01:31:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  |     def _get_vm_pool(self): | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2017-05-11 12:45:09 +02:00
										 |  |  |             vm_pool = oca.VirtualMachinePool(self.client) | 
					
						
							|  |  |  |             vm_pool.info() | 
					
						
							| 
									
										
										
										
											2017-05-21 22:01:26 -05:00
										 |  |  |             return vm_pool | 
					
						
							| 
									
										
										
										
											2017-05-11 12:45:09 +02:00
										 |  |  |         except AttributeError: | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |             logger.info('Could not connect via client, using oneadmin instead')  | 
					
						
							| 
									
										
										
										
											2017-05-14 12:44:36 +02:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 vm_pool = oca.VirtualMachinePool(self.oneadmin_client) | 
					
						
							|  |  |  |                 vm_pool.info(filter=-2) | 
					
						
							| 
									
										
										
										
											2017-05-14 12:45:22 +02:00
										 |  |  |                 return vm_pool | 
					
						
							| 
									
										
										
										
											2017-05-14 12:44:36 +02:00
										 |  |  |             except: | 
					
						
							|  |  |  |                 raise ConnectionRefusedError | 
					
						
							| 
									
										
										
										
											2017-05-11 12:45:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  |         except ConnectionRefusedError: | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |             logger.info('Could not connect to host: {host} via protocol {protocol}'.format( | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  |                     host=settings.OPENNEBULA_DOMAIN, | 
					
						
							|  |  |  |                     protocol=settings.OPENNEBULA_PROTOCOL) | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             raise ConnectionRefusedError | 
					
						
							| 
									
										
										
										
											2017-05-14 12:22:10 +02:00
										 |  |  |         # For now we'll just handle all other errors as connection errors | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             raise ConnectionRefusedError | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 12:07:05 +02:00
										 |  |  |     def get_vms(self): | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             return self._get_vm_pool() | 
					
						
							|  |  |  |         except ConnectionRefusedError: | 
					
						
							| 
									
										
										
										
											2017-05-14 12:22:10 +02:00
										 |  |  |             raise ConnectionRefusedError | 
					
						
							|  |  |  |              | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  |     | 
					
						
							| 
									
										
										
										
											2017-05-12 12:07:05 +02:00
										 |  |  |     def get_vm(self, vm_id): | 
					
						
							| 
									
										
										
										
											2017-05-13 06:59:57 +02:00
										 |  |  |         vm_id = int(vm_id) | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             vm_pool = self._get_vm_pool() | 
					
						
							| 
									
										
										
										
											2017-05-13 06:59:57 +02:00
										 |  |  |             return vm_pool.get_by_id(vm_id) | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |         except: | 
					
						
							| 
									
										
										
										
											2017-05-14 12:22:10 +02:00
										 |  |  |             raise ConnectionRefusedError | 
					
						
							| 
									
										
										
										
											2017-05-10 01:31:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 13:47:53 +02:00
										 |  |  |     def create_template(self, name, cores, memory, disk_size, core_price, memory_price, | 
					
						
							|  |  |  |                         disk_size_price, ssh='' ): | 
					
						
							|  |  |  |         """Create and add a new template to opennebula.
 | 
					
						
							|  |  |  |         :param name:      A string representation describing the template. | 
					
						
							|  |  |  |                           Used as label in view. | 
					
						
							|  |  |  |         :param cores:     Amount of virtual cpu cores for the VM. | 
					
						
							|  |  |  |         :param memory:  Amount of RAM for the VM (GB) | 
					
						
							|  |  |  |         :param disk_size:    Amount of disk space for VM (GB) | 
					
						
							|  |  |  |         :param core_price:     Price of virtual cpu for the VM per core. | 
					
						
							|  |  |  |         :param memory_price:  Price of RAM for the VM per GB | 
					
						
							|  |  |  |         :param disk_size_price:    Price of disk space for VM per GB | 
					
						
							|  |  |  |         :param ssh: User public ssh key | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         template_id = oca.VmTemplate.allocate( | 
					
						
							|  |  |  |             self.oneadmin_client, | 
					
						
							|  |  |  |             template_string_formatter.format( | 
					
						
							|  |  |  |                 name=name, | 
					
						
							|  |  |  |                 vcpu=cores, | 
					
						
							|  |  |  |                 cpu=0.1*cores, | 
					
						
							|  |  |  |                 size=1024 * disk_size, | 
					
						
							|  |  |  |                 memory=1024 * memory, | 
					
						
							|  |  |  |                 # * 10 because we set cpu to *0.1 | 
					
						
							|  |  |  |                 cpu_cost=10*core_price, | 
					
						
							|  |  |  |                 memory_cost=memory_price, | 
					
						
							|  |  |  |                 disk_cost=disk_size_price, | 
					
						
							|  |  |  |                 ssh=ssh | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-05-13 06:59:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 13:47:53 +02:00
										 |  |  |     def create_vm(self, template_id, specs, ssh_key=None): | 
					
						
							| 
									
										
										
										
											2017-05-13 06:59:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 13:47:53 +02:00
										 |  |  |         template = self.get_template(template_id) | 
					
						
							|  |  |  |         vm_specs_formatter = """<TEMPLATE>
 | 
					
						
							|  |  |  |                                  <MEMORY>{memory}</MEMORY> | 
					
						
							|  |  |  |                                  <VCPU>{vcpu}</VCPU> | 
					
						
							|  |  |  |                                  <CPU>{cpu}</CPU> | 
					
						
							|  |  |  |                                  <CONTEXT> | 
					
						
							| 
									
										
										
										
											2017-05-14 02:18:16 +02:00
										 |  |  |                                   <SSH_PUBLIC_KEY>{ssh}</SSH_PUBLIC_KEY> | 
					
						
							| 
									
										
										
										
											2017-05-13 13:47:53 +02:00
										 |  |  |                                  </CONTEXT> | 
					
						
							|  |  |  |                            """
 | 
					
						
							| 
									
										
										
										
											2017-05-14 02:18:16 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             disk = template.template.disks[0] | 
					
						
							|  |  |  |             image_id = disk.image_id | 
					
						
							|  |  |  |             vm_specs = vm_specs_formatter.format( | 
					
						
							| 
									
										
										
										
											2017-05-13 13:47:53 +02:00
										 |  |  |                                         vcpu=int(specs['cpu']), | 
					
						
							|  |  |  |                                         cpu=0.1* int(specs['cpu']), | 
					
						
							|  |  |  |                                         memory=1024 * int(specs['memory']), | 
					
						
							|  |  |  |                                         ssh=ssh_key | 
					
						
							| 
									
										
										
										
											2017-05-14 02:18:16 +02:00
										 |  |  |                      | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |             vm_specs += """<DISK>
 | 
					
						
							|  |  |  |                                   <TYPE>fs</TYPE> | 
					
						
							|  |  |  |                                   <SIZE>{size}</SIZE> | 
					
						
							|  |  |  |                                   <DEV_PREFIX>vd</DEV_PREFIX> | 
					
						
							|  |  |  |                                   <IMAGE_ID>{image_id}</IMAGE_ID> | 
					
						
							|  |  |  |                            </DISK> | 
					
						
							|  |  |  |                           </TEMPLATE> | 
					
						
							|  |  |  |                         """.format(size=1024 * int(specs['disk_size']),
 | 
					
						
							|  |  |  |                                    image_id=image_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             disk = template.template.disks[0] | 
					
						
							|  |  |  |             image = disk.image | 
					
						
							|  |  |  |             image_uname = disk.image_uname | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             vm_specs = vm_specs_formatter.format( | 
					
						
							|  |  |  |                                         vcpu=int(specs['cpu']), | 
					
						
							|  |  |  |                                         cpu=0.1* int(specs['cpu']), | 
					
						
							|  |  |  |                                         memory=1024 * int(specs['memory']), | 
					
						
							|  |  |  |                                         ssh=ssh_key | 
					
						
							|  |  |  |                      | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |             vm_specs += """<DISK>
 | 
					
						
							|  |  |  |                                   <TYPE>fs</TYPE> | 
					
						
							|  |  |  |                                   <SIZE>{size}</SIZE> | 
					
						
							|  |  |  |                                   <DEV_PREFIX>vd</DEV_PREFIX> | 
					
						
							|  |  |  |                                   <IMAGE>{image}</IMAGE> | 
					
						
							|  |  |  |                                   <IMAGE_UNAME>{image_uname}</IMAGE_UNAME> | 
					
						
							|  |  |  |                            </DISK> | 
					
						
							|  |  |  |                           </TEMPLATE> | 
					
						
							|  |  |  |                         """.format(size=1024 * int(specs['disk_size']),
 | 
					
						
							|  |  |  |                                    image=image, | 
					
						
							|  |  |  |                                    image_uname=image_uname) | 
					
						
							|  |  |  |         vm_id = template.instantiate(name ='', | 
					
						
							|  |  |  |                                     pending=False, | 
					
						
							|  |  |  |                                     extra_template=vm_specs,                                    ) | 
					
						
							| 
									
										
										
										
											2017-05-13 06:59:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-11 05:51:30 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             self.oneadmin_client.call( | 
					
						
							|  |  |  |                 oca.VirtualMachine.METHODS['chown'], | 
					
						
							|  |  |  |                 vm_id, | 
					
						
							|  |  |  |                 self.opennebula_user.id, | 
					
						
							|  |  |  |                 self.opennebula_user.group_ids[0] | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         except AttributeError: | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |             logger.info('Could not change owner for vm with id: {}.'.format(vm_id)) | 
					
						
							| 
									
										
										
										
											2017-05-10 01:31:27 +02:00
										 |  |  |         return vm_id | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  |     def delete_vm(self, vm_id): | 
					
						
							| 
									
										
										
										
											2017-05-12 12:13:18 -05:00
										 |  |  |         TERMINATE_ACTION = 'terminate' | 
					
						
							|  |  |  |         vm_terminated = False | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             self.oneadmin_client.call( | 
					
						
							|  |  |  |                 oca.VirtualMachine.METHODS['action'], | 
					
						
							|  |  |  |                 TERMINATE_ACTION, | 
					
						
							|  |  |  |                 int(vm_id), | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             vm_terminated = True | 
					
						
							|  |  |  |         except socket.timeout as socket_err: | 
					
						
							|  |  |  |             logger.info("Socket timeout error: {0}".format(socket_err)) | 
					
						
							|  |  |  |         except OpenNebulaException as opennebula_err: | 
					
						
							|  |  |  |             logger.info("OpenNebulaException error: {0}".format(opennebula_err)) | 
					
						
							|  |  |  |         except OSError as os_err: | 
					
						
							|  |  |  |             logger.info("OSError : {0}".format(os_err)) | 
					
						
							|  |  |  |         except ValueError as value_err: | 
					
						
							|  |  |  |             logger.info("ValueError : {0}".format(value_err)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return vm_terminated | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _get_template_pool(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |            template_pool = oca.VmTemplatePool(self.oneadmin_client) | 
					
						
							|  |  |  |            template_pool.info() | 
					
						
							| 
									
										
										
										
											2017-05-14 12:22:10 +02:00
										 |  |  |            return template_pool | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  |         except ConnectionRefusedError: | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |             logger.info('Could not connect to host: {host} via protocol {protocol}'.format( | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  |                     host=settings.OPENNEBULA_DOMAIN, | 
					
						
							|  |  |  |                     protocol=settings.OPENNEBULA_PROTOCOL) | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             raise ConnectionRefusedError | 
					
						
							| 
									
										
										
										
											2017-05-14 12:22:10 +02:00
										 |  |  |         except: | 
					
						
							|  |  |  |             raise ConnectionRefusedError | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 12:07:05 +02:00
										 |  |  |     def get_templates(self): | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             public_templates = [ | 
					
						
							|  |  |  |                     template  | 
					
						
							|  |  |  |                     for template in self._get_template_pool() | 
					
						
							|  |  |  |                     if 'public-' in template.name  | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |             return public_templates  | 
					
						
							|  |  |  |         except ConnectionRefusedError: | 
					
						
							| 
									
										
										
										
											2017-05-14 12:22:10 +02:00
										 |  |  |             raise ConnectionRefusedError | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             raise ConnectionRefusedError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def try_get_templates(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             return self.get_templates() | 
					
						
							|  |  |  |         except: | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |             return [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 12:07:05 +02:00
										 |  |  |     def get_template(self, template_id): | 
					
						
							| 
									
										
										
										
											2017-05-13 06:59:57 +02:00
										 |  |  |         template_id = int(template_id) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             template_pool = self._get_template_pool() | 
					
						
							|  |  |  |             return template_pool.get_by_id(template_id) | 
					
						
							|  |  |  |         except: | 
					
						
							| 
									
										
										
										
											2017-05-14 12:22:10 +02:00
										 |  |  |             raise ConnectionRefusedError | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2017-05-12 12:07:05 +02:00
										 |  |  |     def create_template(self, name, cores, memory, disk_size, core_price, memory_price, | 
					
						
							|  |  |  |                         disk_size_price, ssh='' ): | 
					
						
							| 
									
										
										
										
											2017-05-10 02:49:03 +02:00
										 |  |  |         """Create and add a new template to opennebula.
 | 
					
						
							|  |  |  |         :param name:      A string representation describing the template. | 
					
						
							|  |  |  |                           Used as label in view. | 
					
						
							|  |  |  |         :param cores:     Amount of virtual cpu cores for the VM. | 
					
						
							| 
									
										
										
										
											2017-05-12 12:07:05 +02:00
										 |  |  |         :param memory:  Amount of RAM for the VM (GB) | 
					
						
							|  |  |  |         :param disk_size:    Amount of disk space for VM (GB) | 
					
						
							|  |  |  |         :param core_price:     Price of virtual cpu for the VM per core. | 
					
						
							|  |  |  |         :param memory_price:  Price of RAM for the VM per GB | 
					
						
							|  |  |  |         :param disk_size_price:    Price of disk space for VM per GB | 
					
						
							|  |  |  |         :param ssh: User public ssh key | 
					
						
							| 
									
										
										
										
											2017-05-10 02:49:03 +02:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2017-05-10 01:11:49 +02:00
										 |  |  |         template_string_formatter = """<TEMPLATE>
 | 
					
						
							|  |  |  |                                         <NAME>{name}</NAME> | 
					
						
							|  |  |  |                                         <MEMORY>{memory}</MEMORY> | 
					
						
							|  |  |  |                                         <VCPU>{vcpu}</VCPU> | 
					
						
							|  |  |  |                                         <CPU>{cpu}</CPU> | 
					
						
							|  |  |  |                                         <DISK> | 
					
						
							|  |  |  |                                          <TYPE>fs</TYPE> | 
					
						
							|  |  |  |                                          <SIZE>{size}</SIZE> | 
					
						
							|  |  |  |                                          <DEV_PREFIX>vd</DEV_PREFIX> | 
					
						
							|  |  |  |                                         </DISK> | 
					
						
							| 
									
										
										
										
											2017-05-12 12:07:05 +02:00
										 |  |  |                                         <CPU_COST>{cpu_cost}</CPU_COST> | 
					
						
							|  |  |  |                                         <MEMORY_COST>{memory_cost}</MEMORY_COST> | 
					
						
							|  |  |  |                                         <DISK_COST>{disk_cost}</DISK_COST> | 
					
						
							|  |  |  |                                         <SSH_PUBLIC_KEY>{ssh}</SSH_PUBLIC_KEY> | 
					
						
							| 
									
										
										
										
											2017-05-10 01:11:49 +02:00
										 |  |  |                                        </TEMPLATE> | 
					
						
							|  |  |  |                                        """
 | 
					
						
							|  |  |  |         template_id = oca.VmTemplate.allocate( | 
					
						
							|  |  |  |             self.oneadmin_client, | 
					
						
							|  |  |  |             template_string_formatter.format( | 
					
						
							|  |  |  |                 name=name, | 
					
						
							|  |  |  |                 vcpu=cores, | 
					
						
							|  |  |  |                 cpu=0.1*cores, | 
					
						
							|  |  |  |                 size=1024 * disk_size, | 
					
						
							| 
									
										
										
										
											2017-05-12 12:07:05 +02:00
										 |  |  |                 memory=1024 * memory, | 
					
						
							|  |  |  |                 # * 10 because we set cpu to *0.1 | 
					
						
							|  |  |  |                 cpu_cost=10*core_price, | 
					
						
							|  |  |  |                 memory_cost=memory_price, | 
					
						
							|  |  |  |                 disk_cost=disk_size_price, | 
					
						
							|  |  |  |                 ssh=ssh | 
					
						
							| 
									
										
										
										
											2017-05-10 01:11:49 +02:00
										 |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return template_id | 
					
						
							| 
									
										
										
										
											2017-05-11 04:05:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def delete_template(self, template_id): | 
					
						
							|  |  |  |         self.oneadmin_client.call(oca.VmTemplate.METHODS['delete'], template_id, False) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 05:50:56 +02:00
										 |  |  |     def change_user_password(self, new_password): | 
					
						
							|  |  |  |         self.oneadmin_client.call( | 
					
						
							|  |  |  |             oca.User.METHODS['passwd'], | 
					
						
							|  |  |  |             self.opennebula_user.id, | 
					
						
							|  |  |  |             new_password | 
					
						
							|  |  |  |         )  |