def do_run_instances(self, instance_ids, reservation_id, user_id, params_t, image_id, image_url, kernel_id, kernel_url, ramdisk_id, ramdisk_url, mac_addrs, target_node_id=None): self._logger.info('invoked') verify_run_instances_arguments = lambda: len(instance_ids) == len(mac_addrs) and self._has_node(target_node_id) == -1 # verify arguments if not verify_run_instances_arguments(): self._logger.wran('error arguments') return Result.new(0xFFFF, 'failed to run instances') # setup NetConfig net_configs = [NetConfig.new_instance(mac=mac) for mac in mac_addrs] param = VirtualMachine(params_t) self._startup_run_instances_thread(instance_ids, reservation_id, user_id, param, image_id, image_url, kernel_id, kernel_url, ramdisk_id, ramdisk_url, net_configs, target_node_id) self._logger.debug('done') return Result.new(0x0, {'msg': "run instances"})
def do_remove_node(self, nid, force=False): self._logger.info('invoked') with self._res_lock: if self._has_node(nid) == -1: self._logger.warn('node %s is not exists' % (nid,)) return Result.new(0xFFFF, 'failed to remove node %s' % (nid,)) if not force: if self._instance_exist_on_node(nid): self._logger.warn('failed to remove node %s, some vm running on node %s' % (nid, nid)) return Result.new(0xFFFF, 'failed to remove node %s' % (nid,)) else: # if force is true, find all instance which is running on node insts = self._get_instances_on_node(nid) inst_ids = [inst.instance_id for inst in insts] if inst_ids: if force: with self._nccall_sem: self._logger.info('terminate instances: %s' % inst_ids) ret = self._startup_terminate_instances(inst_ids) if ret.code != 0x0: self._logger.warn('failed to remove node %s, terminate vm failed' % (nid,)) return Result.new(0xFFFF, 'failed to remove node %s' % (nid,)) else: return Result.new(0xFFFF, 'failed to remove node %s' % (nid,)) with self._res_lock: self._logger.info('remove node %s' % nid) self._remove_node(nid) self._logger.debug('done') return Result.new(0x0, "remove node %s" % nid)
def do_describe_instances(self, inst_ids=None): import copy self._logger.info('invoked') if inst_ids and not isinstance(inst_ids, list): self._logger.warn('error arguments with: ' + inst_ids) return Result.new(0xFFFF, {'msg': 'error arguments'}) rs = [] with self._inst_lock: if inst_ids == None: [rs.append(inst) for inst in self._iter_instance()] else: for inst_id in inst_ids: inst = self._get_instance(inst_id) if inst: oi = inst inst = copy.deepcopy(inst) if (inst.state_code == InstanceState.RUNNING and inst.net.ip != '0.0.0.0') or inst.state_code == InstanceState.TEARDOWN: inst.state_code = 0 else: inst.state_code = 2 rs.append(inst) self._logger.debug('done') return Result.new(0x0, {'msg': "describe instances", 'instances': rs})
def do_reboot_instance(self, instance_id): with self._inst_lock: inst = self._get_instance(instance_id) if inst == None: self._logger.error("cannot find instance %s" % instance_id) return Result.new(0xFFFF, 'cannot find instance %s' % instance_id) self._startup_reboot_instance_thread(inst) return Result.new(0x0, 'reboot instance %s' % instance_id)
def do_terminate_instances(self, inst_ids): self._logger.info('invoked') if not isinstance(inst_ids, list): self._logger.warn('error arguments') return Result.new(0x0, {'msg': 'error arguments'}) [self._find_and_terminate_instance(inst_id) for inst_id in inst_ids] self._logger.info('done') return Result.new(0x0, 'terminate instances')
def do_reboot_instances(self, inst_ids): self._logger.info('invoked') if not isinstance(inst_ids, list): self._logger.warn('error arguments') return Result.new(0xFFFF, {'msg': 'error arguments'}) [self._find_and_reboot_instance(inst_id) for inst_id in inst_ids] self._logger.debug('done') return Result.new(0x0, "reboot instances")
def do_add_node(self, nid, ip, port): self._logger.info('invoked') with self._res_lock: # node is already in cluster? if self._has_node(nid) != -1: self._logger.warn('failed to add node %s, %s in node list' % (nid, nid)) return Result.new(0xFFFF, {'msg': 'failed to add node %s' % (nid,)}) self._startup_add_node_thread(nid, ip, port) self._logger.debug('done') return Result.new(0x0, {'msg': 'add node %s' % (nid,)})
def do_terminate_instance(self, instance_id): self._logger.info('invoked') with self._inst_lock: inst = self._find_and_terminate_instance(instance_id, 1) if inst == None: return Result.new(0xFFFF, "instance not found") if inst.state_code != InstanceState.TEARDOWN: self._change_instance_state(inst, InstanceState.SHUTOFF) self._logger.debug('done') return Result.new(0x0, 'terminate instance')
def do_run_instance(self, instance_id, reservation_id, params, # data.VirtualMachine image_id, image_url, kernel_id, kernel_url, # should be set none ramdisk_id, ramdisk_url, # should be set none net_config, # data.NetConfig user_id): self._logger.info('invoked') self._inst_lock.acquire() idx = self._has_instance(instance_id) self._inst_lock.release() if idx != -1: self._logger.error("instance %s is already running." % instance_id) return Result.new(0xFFFF, "failed to startup instance %s" % (instance_id,)) params_t = data.VirtualMachine(params) net_config_t = data.NetConfig(net_config) inst = data.Instance.new_instance(instance_id, reservation_id, params_t, image_id, image_url, kernel_id, kernel_url, ramdisk_id, ramdisk_url, InstanceState.PENDING, net_config_t, user_id) with self._res_lock: if self._allocate_resource(inst.params): self._logger.warn("failed to allocate resource") return Result.new(0xFFFF, "failed to allocate resource") self._inst_lock.acquire() self._add_instance(inst) self._inst_lock.release() self._startup_instance_thread(inst) self._logger.debug('done') return Result.new(0x0, "run instance")
def do_describe_instances(self, inst_ids=None): self._logger.info('invoked') if inst_ids and not isinstance(inst_ids, list): self._logger.warn("error arguments with: " + inst_ids) return Result.new(0xFFFF, {'msg': "error arguments"}) rs = [] with self._inst_lock: if inst_ids == None: [rs.append(inst) for inst in self._iter_global_instances()] else: for inst_id in inst_ids: inst = self._get_instance(inst_id) inst and rs.append(inst) self._logger.debug('done') return Result.new(0x0, {'msg': 'describe instances', 'instances': rs})
def do_describe_node(self, nids=None): self._logger.info('invoked') if not nids: nids = [node.id for node in self._iter_node()] ress = [] [ress.append(node) for node in [self._get_node(nid) for nid in nids] if node != None] self._logger.debug('done') return Result.new(0x0, {'msg': 'describe node', 'resources': ress})
def do_describe_resources(self): self._logger.info('invoked') with self._res_lock: (cores_available, mem_available, disk_available) = reduce(lambda x, acc: (x[0] + acc[0], x[1] + acc[1], x[2] + acc[2]), [(res.number_cores_available, res.mem_size_available, res.disk_size_available) for res in self._iter_running_node()], (0,0,0)) with self._inst_lock: (sum_cores, sum_mem, sum_disk) = reduce(lambda x, acc: (x[0] + acc[0], x[1] + acc[1], x[2] + acc[2]), [(inst.params.cores, inst.params.mem, inst.params.disk) for inst in self._iter_instance() if inst.state_code != InstanceState.TEARDOWN], (0,0,0)) free_cores = cores_available - sum_cores if free_cores < 0: self._logger.warn('Error cores free with %d, fix to 0' % free_cores) free_cores = 0 free_mem = mem_available - sum_mem if free_mem < 0: self._logger.warn('Error memory free with %d, fix to 0' % free_mem) free_mem = 0 free_disk = disk_available - sum_disk if free_disk < 0: self._logger.warn('Error disk free with %d, fix to 0' % free_disk) free_disk = 0 res = NodeResource.new_instance("ok", free_mem, mem_available, free_disk, disk_available, free_cores, cores_available) self._logger.debug('done') return Result.new(0x0, {'msg': "describe resources", 'resource': res})
def do_describe_resource(self): self._logger.info('invoked') sum_cores = sum_mem = sum_disk = 0 for inst in self._iter_global_instances(): if inst.state_code == InstanceState.TEARDOWN: continue sum_cores += inst.params.cores sum_mem += inst.params.mem sum_disk += inst.params.disk cores_free = self._nc_detail.config_max_cores - sum_cores if cores_free < 0: self._logger.warning("Error cores free with %d, fix to 0" % cores_free) cores_free = 0 mem_free = self._nc_detail.config_max_mem - sum_mem if mem_free < 0: self._logger.warning("Error memory free with %d, fix to 0" % mem_free) mem_free = 0 disk_free = self._nc_detail.config_max_disk - sum_disk if disk_free < 0: self._logger.warning("Error disk free with %d, fix to 0" % disk_free) disk_free = 0 res = data.NodeResource.new_instance("ok", mem_free, self._nc_detail.config_max_mem, disk_free, self._nc_detail.config_max_disk, cores_free, self._nc_detail.config_max_cores) self._logger.debug('done') return Result.new(0x0, {'msg': 'describe resource', 'resource': res})
def do_get_console_output(self): return Result.new(0xFFFF, 'get console output')
def do_start_network(self): return Result.new(0xFFFF, 'start network')
def do_get_console_output(self): return Result.new(0x0, "console output")
def do_detach_volume(self): return Result.new(0xFFFF, 'detach volume')
def do_attach_volume(self): return Result.new(0xFFFF, 'attache volume')
def do_attach_volume(self): return Result.new(0xFFFF, "attach volume")
def stop(self): self._logger.info("invoked") super(Node, self).stop() self._logger.debug("done") return Result.new(0x0, "stop service")
def do_power_down(self): return Result.new(0x0, 'power down')