def __init__(self, name, ptm): super(ZookeeperHost, self).__init__(name, ptm) self.zookeeper_ips = [] self.num_id = '1' self.ip = IP('', '') self.pid = 0 self.configurator = ZookeeperFileConfiguration()
def __init__(self, name, ptm): super(CassandraHost, self).__init__(name, ptm) self.cassandra_ips = [] self.num_id = '1' self.init_token = '' self.ip = IP() self.configurator = CassandraFileConfiguration()
def create_vm(self, ip, preferred_hv_host=None, preferred_name=None): """ Creates a guest VM on the Physical Topology and returns the Guest object representing the VM as part of the virtual topology. :param ip: str IP Address to use for the VM (required) :param preferred_hv_host: str: Hypervisor to use, otherwise the least-loaded HV host is chosen. :param preferred_name: str: Name to use for the VM. Otherwise one is generated. :return: Guest """ self.physical_topology_manager.LOG.debug("Creating VM on IP: " + str(ip)) if preferred_hv_host is None: # Pick the HV with the fewest running VMs least_busy_hv = None for hv in self.physical_topology_manager.hypervisors.itervalues(): if least_busy_hv is None or least_busy_hv > hv.get_vm_count(): least_busy_hv = hv if least_busy_hv is None: raise ObjectNotFoundException('No suitable hypervisor found to launch VM') start_hv = least_busy_hv else: if preferred_hv_host not in self.physical_topology_manager.hypervisors: raise ObjectNotFoundException('Requested host to start VM: ' + preferred_hv_host + ' not found') start_hv = self.physical_topology_manager.hypervisors[preferred_hv_host] if preferred_name is not None: vm_name = preferred_name else: vm_name = 'vm_' + str(VirtualTopologyManager.global_vm_id) VirtualTopologyManager.global_vm_id += 1 self.physical_topology_manager.LOG.debug("Starting VM with name: " + vm_name + " and IP: " + str(ip) + " on hypervisor: " + start_hv.name) new_vm = start_hv.create_vm(vm_name) new_vm.create_interface('eth0', ip_list=[IP.make_ip(ip)]) return Guest(new_vm)
def do_extra_config_host_for_process_control(self, cfg_map): self.num_id = cfg_map['num_id'] self.ip = IP.from_map(cfg_map['ip'])
class ZookeeperHost(NetNSHost): def __init__(self, name, ptm): super(ZookeeperHost, self).__init__(name, ptm) self.zookeeper_ips = [] self.num_id = '1' self.ip = IP('', '') self.pid = 0 self.configurator = ZookeeperFileConfiguration() def do_extra_config_from_ptc_def(self, cfg, impl_cfg): """ Configure this host type from a PTC HostDef config and the implementation-specific configuration :type cfg: HostDef :type impl_cfg: ImplementationDef :return: """ if len(cfg.interfaces.values()) > 0 and len(cfg.interfaces.values()[0].ip_addresses) > 0: self.ip = cfg.interfaces.values()[0].ip_addresses[0] if 'zookeeper_ips' in impl_cfg.kwargs: for i in impl_cfg.kwargs['zookeeper_ips']: self.zookeeper_ips.append(IP(i)) if 'id' in impl_cfg.kwargs: self.num_id = impl_cfg.kwargs['id'] def do_extra_create_host_cfg_map_for_process_control(self): return {'num_id': self.num_id, 'ip': self.ip.to_map()} def do_extra_config_host_for_process_control(self, cfg_map): self.num_id = cfg_map['num_id'] self.ip = IP.from_map(cfg_map['ip']) def prepare_config(self): self.configurator.configure(self.num_id, self.zookeeper_ips, self.LOG) log_dir = '/var/log/zookeeper.' + self.num_id self.ptm.log_manager.add_external_log_file(FileLocation(log_dir + '/zookeeper.log'), self.num_id, '%Y-%m-%d %H:%M:%S,%f') def print_config(self, indent=0): super(ZookeeperHost, self).print_config(indent) print (' ' * (indent + 1)) + 'Num-id: ' + self.num_id print (' ' * (indent + 1)) + 'Self-IP: ' + str(self.ip) print (' ' * (indent + 1)) + 'Zookeeper-IPs: ' + ', '.join(str(ip) for ip in self.zookeeper_ips) # TODO: Add a wait_for_process_stop here (and to all hosts) def wait_for_process_start(self): # Checking Zookeeper status retries = 0 max_retries = 45 connected = False while not connected: ping_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.LOG.info('Trying to contact ZK server @' + self.ip.ip) try: ping_socket.settimeout(1) ping_socket.connect((self.ip.ip, 2181)) ping_socket.send('ruok') if ping_socket.recv(16) == 'imok': connected = True except Exception: pass finally: if not connected: retries += 1 if retries > max_retries: raise SubprocessFailedException('Zookeeper host ' + self.num_id + ' timed out while starting') time.sleep(1) def prepare_environment(self): self.configurator.mount_config(self.num_id) def cleanup_environment(self): self.configurator.unmount_config() def control_start(self): self.cli.rm_files('/var/log/zookeeper') process = self.cli.cmd(('/usr/bin/java' ' -cp /etc/zookeeper/conf:' '/usr/share/java/jline.jar:' '/usr/share/java/log4j-1.2.jar:' '/usr/share/java/xercesImpl.jar:' '/usr/share/java/xmlParserAPIs.jar:' '/usr/share/java/netty.jar:' '/usr/share/java/slf4j-api.jar:' '/usr/share/java/slf4j-log4j12.jar:' '/usr/share/java/zookeeper.jar' ' -Dcom.sun.management.jmxremote' ' -Dcom.sun.management.jmxremote.local.only=false' ' -Dzookeeper.log.dir=/var/log/zookeeper' ' -Dzookeeper.root.logger=INFO,ROLLINGFILE' ' org.apache.zookeeper.server.quorum.QuorumPeerMain' ' /etc/zookeeper/conf/zoo.cfg'), blocking=False, shell=True) if process.pid == -1: raise SubprocessFailedException('java-zookeeper') real_pid = self.cli.get_parent_pids(process.pid)[-1] self.cli.write_to_file('/run/zookeeper/pid', str(real_pid)) def control_stop(self): if self.cli.exists('/run/zookeeper/pid'): pid = self.cli.read_from_file('/run/zookeeper/pid') self.cli.cmd('kill ' + str(pid)) self.cli.rm('/run/zookeeper/pid')
def do_extra_config_host_for_process_control(self, cfg_map): self.num_id = cfg_map['num_id'] self.my_ip = cfg_map['my_ip'] self.unique_id = uuid.UUID('urn:uuid:' + cfg_map['uuid']) self.zookeeper_ips = [IP.make_ip(s) for s in cfg_map['zookeeper_ips'].split(',')]
class CassandraHost(NetNSHost): def __init__(self, name, ptm): super(CassandraHost, self).__init__(name, ptm) self.cassandra_ips = [] self.num_id = '1' self.init_token = '' self.ip = IP() self.configurator = CassandraFileConfiguration() def do_extra_config_from_ptc_def(self, cfg, impl_cfg): """ Configure this host type from a PTC HostDef config and the implementation-specific configuration :type cfg: HostDef :type impl_cfg: ImplementationDef :return: """ if len(cfg.interfaces.values()) > 0 and len(cfg.interfaces.values()[0].ip_addresses) > 0: self.ip = cfg.interfaces.values()[0].ip_addresses[0] if 'init_token' in impl_cfg.kwargs: self.init_token = impl_cfg.kwargs['init_token'] if 'cassandra_ips' in impl_cfg.kwargs: for i in impl_cfg.kwargs['cassandra_ips']: self.cassandra_ips.append(IP(i)) if 'id' in impl_cfg.kwargs: self.num_id = impl_cfg.kwargs['id'] def prepare_config(self): self.configurator.configure(self.num_id, self.cassandra_ips, self.init_token, self.ip) log_dir = '/var/log/cassandra.' + self.num_id self.ptm.log_manager.add_external_log_file(FileLocation(log_dir + '/system.log'), self.num_id, '%Y-%m-%d %H:%M:%S,%f', 2) def print_config(self, indent=0): super(CassandraHost, self).print_config(indent) print (' ' * (indent + 1)) + 'Num-id: ' + self.num_id print (' ' * (indent + 1)) + 'Init-token: ' + self.init_token print (' ' * (indent + 1)) + 'Self-IP: ' + str(self.ip) print (' ' * (indent + 1)) + 'Cassandra-IPs: ' + ', '.join(str(ip) for ip in self.cassandra_ips) def do_extra_create_host_cfg_map_for_process_control(self): return {'num_id': self.num_id, 'ip': self.ip.to_map()} def do_extra_config_host_for_process_control(self, cfg_map): self.num_id = cfg_map['num_id'] self.ip = IP.from_map(cfg_map['ip']) def wait_for_process_start(self): # Wait a couple seconds for the process to start before polling nodetool time.sleep(2) # Checking Cassandra status retries = 0 max_retries = 10 connected = False while not connected: if self.cli.cmd('nodetool -h 127.0.0.1 status', return_status=True) == 0: connected = True else: retries += 1 if retries > max_retries: raise SocketException('Cassandra host ' + self.num_id + ' timed out while starting') time.sleep(2) def prepare_environment(self): self.configurator.mount_config(self.num_id) def cleanup_environment(self): self.configurator.unmount_config() def control_start(self): self.cli.cmd('/bin/bash -c "MAX_HEAP_SIZE=128M HEAP_NEWSIZE=64M /etc/init.d/cassandra start"') def control_stop(self): self.cli.cmd("/etc/init.d/cassandra stop")