def launch(self, inf, radl, requested_radl, num_vm, auth_data): system = radl.systems[0] public_net = None for net in radl.networks: if net.isPublic(): public_net = net outports = None if public_net: outports = public_net.getOutPorts() apiVersion = self.get_api_version(auth_data) res = [] namespace = inf.id headers = {'Content-Type': 'application/json'} uri = "/api/" + apiVersion + "/namespaces" with inf._lock: resp = self.create_request('GET', uri + "/" + namespace, auth_data, headers) if resp.status_code != 200: namespace_data = { 'apiVersion': apiVersion, 'kind': 'Namespace', 'metadata': { 'name': namespace } } body = json.dumps(namespace_data) resp = self.create_request('POST', uri, auth_data, headers, body) if resp.status_code != 201: for _ in range(num_vm): res.append( (False, "Error creating the Namespace: " + resp.text)) return res i = 0 while i < num_vm: try: i += 1 vm = VirtualMachine(inf, None, self.cloud, radl, requested_radl, self) (nodename, _) = vm.getRequestedName( default_hostname=Config.DEFAULT_VM_NAME, default_domain=Config.DEFAULT_DOMAIN) pod_name = nodename # Do not use the Persistent volumes yet volumes = self._create_volumes(apiVersion, namespace, system, pod_name, auth_data) ssh_port = (KubernetesCloudConnector._port_base_num + KubernetesCloudConnector._port_counter) % 65535 KubernetesCloudConnector._port_counter += 1 pod_data = self._generate_pod_data(apiVersion, namespace, pod_name, outports, system, ssh_port, volumes) body = json.dumps(pod_data) uri = "/api/" + apiVersion + "/namespaces/" + namespace + "/pods" resp = self.create_request('POST', uri, auth_data, headers, body) if resp.status_code != 201: res.append( (False, "Error creating the Container: " + resp.text)) else: output = json.loads(resp.text) vm.id = output["metadata"]["name"] # Set SSH port in the RADL info of the VM vm.setSSHPort(ssh_port) # Set the default user and password to access the container vm.info.systems[0].setValue( 'disk.0.os.credentials.username', 'root') vm.info.systems[0].setValue( 'disk.0.os.credentials.password', self._root_password) vm.info.systems[0].setValue('instance_id', str(vm.id)) vm.info.systems[0].setValue('instance_name', str(vm.id)) res.append((True, vm)) except Exception as ex: self.log_exception( "Error connecting with Kubernetes API server") res.append((False, "ERROR: " + str(ex))) return res
def launch(self, inf, radl, requested_radl, num_vm, auth_data): system = radl.systems[0] public_net = None for net in radl.networks: if net.isPublic(): public_net = net outports = None if public_net: outports = public_net.getOutPorts() conn = self.get_http_connection(auth_data) res = [] i = 0 while i < num_vm: try: i += 1 ssh_port = 22 if public_net: ssh_port = (DockerCloudConnector._port_base_num + DockerCloudConnector._port_counter) % 65535 DockerCloudConnector._port_counter += 1 # Create the VM to get the nodename vm = VirtualMachine(inf, None, self.cloud, radl, requested_radl, self) # Create the container conn.putrequest('POST', "/containers/create") conn.putheader('Content-Type', 'application/json') cont_data = self._generate_create_request_data( outports, system, vm, ssh_port) body = json.dumps(cont_data) conn.putheader('Content-Length', len(body)) conn.endheaders(body) resp = conn.getresponse() output = resp.read() if resp.status != 201: res.append( (False, "Error creating the Container: " + output)) continue output = json.loads(output) # Set the cloud id to the VM vm.id = output["Id"] vm.info.systems[0].setValue('instance_id', str(vm.id)) # Now start it success, _ = self.start(vm, auth_data) if not success: res.append( (False, "Error starting the Container: " + str(output))) # Delete the container conn.request('DELETE', "/containers/" + vm.id) resp = conn.getresponse() resp.read() continue # Set the default user and password to access the container vm.info.systems[0].setValue( 'disk.0.os.credentials.username', 'root') vm.info.systems[0].setValue( 'disk.0.os.credentials.password', self._root_password) # Set ssh port in the RADL info of the VM vm.setSSHPort(ssh_port) res.append((True, vm)) except Exception, ex: self.logger.exception("Error connecting with Docker server") res.append((False, "ERROR: " + str(ex)))
def launch(self, inf, radl, requested_radl, num_vm, auth_data): system = radl.systems[0] public_net = None for net in radl.networks: if net.isPublic(): public_net = net outports = None if public_net: outports = public_net.getOutPorts() auth_header = self.get_auth_header(auth_data) conn = self.get_http_connection() apiVersion = self.get_api_version(auth_data) res = [] i = 0 while i < num_vm: try: i += 1 namespace = "im%d" % int(time.time() * 100) vm = VirtualMachine(inf, None, self.cloud, radl, requested_radl, self) (nodename, _) = vm.getRequestedName( default_hostname=Config.DEFAULT_VM_NAME, default_domain=Config.DEFAULT_DOMAIN) pod_name = nodename # Do not use the Persistent volumes yet volumes = self._create_volumes( apiVersion, namespace, system, pod_name, auth_data) # Create the pod conn.putrequest('POST', "/api/" + apiVersion + "/namespaces/" + namespace + "/pods") conn.putheader('Content-Type', 'application/json') if auth_header: conn.putheader(auth_header.keys()[ 0], auth_header.values()[0]) ssh_port = (KubernetesCloudConnector._port_base_num + KubernetesCloudConnector._port_counter) % 65535 KubernetesCloudConnector._port_counter += 1 pod_data = self._generate_pod_data( apiVersion, namespace, pod_name, outports, system, ssh_port, volumes) body = json.dumps(pod_data) conn.putheader('Content-Length', len(body)) conn.endheaders(body) resp = conn.getresponse() output = resp.read() if resp.status != 201: res.append( (False, "Error creating the Container: " + output)) else: output = json.loads(output) vm.id = output["metadata"]["namespace"] + \ "/" + output["metadata"]["name"] # Set SSH port in the RADL info of the VM vm.setSSHPort(ssh_port) # Set the default user and password to access the container vm.info.systems[0].setValue( 'disk.0.os.credentials.username', 'root') vm.info.systems[0].setValue( 'disk.0.os.credentials.password', self._root_password) vm.info.systems[0].setValue('instance_id', str(vm.id)) vm.info.systems[0].setValue('instance_name', str(vm.id)) res.append((True, vm)) except Exception, ex: self.logger.exception( "Error connecting with Kubernetes API server") res.append((False, "ERROR: " + str(ex)))
def launch(self, inf, radl, requested_radl, num_vm, auth_data): system = radl.systems[0] public_net = None for net in radl.networks: if net.isPublic(): public_net = net outports = None if public_net: outports = public_net.getOutPorts() conn = self.get_http_connection(auth_data) res = [] i = 0 while i < num_vm: try: i += 1 ssh_port = 22 if public_net: ssh_port = (DockerCloudConnector._port_base_num + DockerCloudConnector._port_counter) % 65535 DockerCloudConnector._port_counter += 1 # Create the VM to get the nodename vm = VirtualMachine(inf, None, self.cloud, radl, requested_radl, self) # Create the container conn.putrequest('POST', "/containers/create") conn.putheader('Content-Type', 'application/json') cont_data = self._generate_create_request_data( outports, system, vm, ssh_port) body = json.dumps(cont_data) conn.putheader('Content-Length', len(body)) conn.endheaders(body) resp = conn.getresponse() output = resp.read() if resp.status != 201: res.append( (False, "Error creating the Container: " + output)) continue output = json.loads(output) # Set the cloud id to the VM vm.id = output["Id"] vm.info.systems[0].setValue('instance_id', str(vm.id)) # Now start it success, _ = self.start(vm, auth_data) if not success: res.append( (False, "Error starting the Container: " + str(output))) # Delete the container conn.request('DELETE', "/containers/" + vm.id) resp = conn.getresponse() resp.read() continue # Set the default user and password to access the container vm.info.systems[0].setValue('disk.0.os.credentials.username', 'root') vm.info.systems[0].setValue('disk.0.os.credentials.password', self._root_password) # Set ssh port in the RADL info of the VM vm.setSSHPort(ssh_port) res.append((True, vm)) except Exception, ex: self.logger.exception("Error connecting with Docker server") res.append((False, "ERROR: " + str(ex)))
def launch(self, inf, radl, requested_radl, num_vm, auth_data): system = radl.systems[0] # Get the public network connected with this VM public_net = None for net in radl.networks: if net.isPublic() and system.getNumNetworkWithConnection( net.id) is not None: public_net = net outports = None if public_net: outports = public_net.getOutPorts() self._create_networks(inf, radl, auth_data) with inf._lock: self._create_volumes(system, auth_data) headers = {'Content-Type': 'application/json'} res = [] i = 0 while i < num_vm: try: i += 1 # Create the VM to get the nodename vm = VirtualMachine(inf, None, self.cloud, radl, requested_radl, self) ssh_port = 22 if vm.hasPublicNet(): ssh_port = (DockerCloudConnector._port_base_num + DockerCloudConnector._port_counter) % 65535 DockerCloudConnector._port_counter += 1 # The URI has this format: docker://image_name full_image_name = system.getValue("disk.0.image.url")[9:] # Create the container if self._is_swarm(auth_data): cont_data = self._generate_create_svc_request_data( full_image_name, outports, vm, ssh_port, auth_data) resp = self.create_request('POST', "/services/create", auth_data, headers, cont_data) else: # First we have to pull the image image_parts = full_image_name.split(":") image_name = image_parts[0] if len(image_parts) < 2: tag = "latest" else: tag = image_parts[1] resp = self.create_request( 'POST', "/images/create?fromImage=%s&tag=%s" % (image_name, tag), auth_data, headers) if resp.status_code not in [201, 200]: res.append( (False, "Error pulling the image: " + resp.text)) continue cont_data = self._generate_create_cont_request_data( full_image_name, outports, vm, ssh_port, auth_data) resp = self.create_request('POST', "/containers/create", auth_data, headers, cont_data) if resp.status_code != 201: res.append( (False, "Error creating the Container: " + resp.text)) continue output = json.loads(resp.text) # Set the cloud id to the VM if "Id" in output: vm.id = output["Id"] elif "ID" in output: vm.id = output["ID"] else: res.append((False, "Error: response format not expected.")) vm.info.systems[0].setValue('instance_id', str(vm.id)) if not self._is_swarm(auth_data): # In creation a container can only be attached to one one network # so now we must attach to the rest of networks (if any) success = self._attach_cont_to_networks(vm, auth_data) if not success: res.append( (False, "Error attaching to networks the Container")) # Delete the container resp = self.create_request('DELETE', "/containers/" + vm.id, auth_data) continue # Now start it success, msg = self.start(vm, auth_data) if not success: res.append( (False, "Error starting the Container: " + str(msg))) # Delete the container resp = self.create_request('DELETE', "/containers/" + vm.id, auth_data) continue # Set the default user and password to access the container vm.info.systems[0].setValue('disk.0.os.credentials.username', 'root') vm.info.systems[0].setValue('disk.0.os.credentials.password', self._root_password) # Set ssh port in the RADL info of the VM vm.setSSHPort(ssh_port) res.append((True, vm)) except Exception, ex: self.logger.exception("Error connecting with Docker server") res.append((False, "ERROR: " + str(ex)))
def launch(self, inf, radl, requested_radl, num_vm, auth_data): system = radl.systems[0] public_net = None for net in radl.networks: if net.isPublic(): public_net = net outports = None if public_net: outports = public_net.getOutPorts() auth_header = self.get_auth_header(auth_data) conn = self.get_http_connection() apiVersion = self.get_api_version(auth_data) res = [] i = 0 while i < num_vm: try: i += 1 namespace = "im%d" % int(time.time() * 100) vm = VirtualMachine(inf, None, self.cloud, radl, requested_radl, self) (nodename, _) = vm.getRequestedName( default_hostname=Config.DEFAULT_VM_NAME, default_domain=Config.DEFAULT_DOMAIN) pod_name = nodename # Do not use the Persistent volumes yet volumes = self._create_volumes(apiVersion, namespace, system, pod_name, auth_data) # Create the pod conn.putrequest( 'POST', "/api/" + apiVersion + "/namespaces/" + namespace + "/pods") conn.putheader('Content-Type', 'application/json') if auth_header: conn.putheader(auth_header.keys()[0], auth_header.values()[0]) ssh_port = (KubernetesCloudConnector._port_base_num + KubernetesCloudConnector._port_counter) % 65535 KubernetesCloudConnector._port_counter += 1 pod_data = self._generate_pod_data(apiVersion, namespace, pod_name, outports, system, ssh_port, volumes) body = json.dumps(pod_data) conn.putheader('Content-Length', len(body)) conn.endheaders(body) resp = conn.getresponse() output = resp.read() if resp.status != 201: res.append( (False, "Error creating the Container: " + output)) else: output = json.loads(output) vm.id = output["metadata"]["namespace"] + \ "/" + output["metadata"]["name"] # Set SSH port in the RADL info of the VM vm.setSSHPort(ssh_port) # Set the default user and password to access the container vm.info.systems[0].setValue( 'disk.0.os.credentials.username', 'root') vm.info.systems[0].setValue( 'disk.0.os.credentials.password', self._root_password) vm.info.systems[0].setValue('instance_id', str(vm.id)) vm.info.systems[0].setValue('instance_name', str(vm.id)) res.append((True, vm)) except Exception, ex: self.logger.exception( "Error connecting with Kubernetes API server") res.append((False, "ERROR: " + str(ex)))