def get_network_config(network_config, assigned_endpoints): """A helper to generate network config from azure_template_unit's network config decouple from azure_template_unit.get_network_config Public endpoint should be assigned in real time """ nc = network_config network_config = ConfigurationSet() network_config.configuration_set_type = nc[ AZURE_UNIT.NETWORK_CONFIG_CONFIGURATION_SET_TYPE] input_endpoints = nc[AZURE_UNIT.NETWORK_CONFIG_INPUT_ENDPOINTS] # avoid duplicate endpoint under same cloud service endpoints = map( lambda i: i[AZURE_UNIT.NETWORK_CONFIG_INPUT_ENDPOINTS_LOCAL_PORT], input_endpoints) unassigned_endpoints = map( str, find_unassigned_endpoints(endpoints, assigned_endpoints)) map( lambda (i, u): i.update({AZURE_UNIT.NETWORK_CONFIG_INPUT_ENDPOINTS_PORT: u}), zip(input_endpoints, unassigned_endpoints)) for input_endpoint in input_endpoints: network_config.input_endpoints.input_endpoints.append( ConfigurationSetInputEndpoint( input_endpoint[AZURE_UNIT.NETWORK_CONFIG_INPUT_ENDPOINTS_NAME], input_endpoint[ AZURE_UNIT.NETWORK_CONFIG_INPUT_ENDPOINTS_PROTOCOL], input_endpoint[AZURE_UNIT.NETWORK_CONFIG_INPUT_ENDPOINTS_PORT], input_endpoint[ AZURE_UNIT.NETWORK_CONFIG_INPUT_ENDPOINTS_LOCAL_PORT])) return network_config
def add_endpoint_to_network_config(network_config, public_endpoints, private_endpoints): """ Return a new network config :param network_config: :param public_endpoints: a list of int or str :param private_endpoints: a list of int or str :return: """ endpoints = zip(map(str, public_endpoints), map(str, private_endpoints)) new_network_config = ConfigurationSet() new_network_config.configuration_set_type = network_config.configuration_set_type if network_config.input_endpoints is not None: for input_endpoint in network_config.input_endpoints.input_endpoints: new_network_config.input_endpoints.input_endpoints.append( ConfigurationSetInputEndpoint(input_endpoint.name, input_endpoint.protocol, input_endpoint.port, input_endpoint.local_port)) for endpoint in endpoints: new_network_config.input_endpoints.input_endpoints.append( ConfigurationSetInputEndpoint(ENDPOINT_PREFIX + endpoint[0], ENDPOINT_PROTOCOL, endpoint[0], endpoint[1])) return new_network_config
def get_network_config(self, service, update): """ Return None if image type is vm and not update Public endpoint should be assigned in real time :param service: :return: """ if self.is_vm_image() and not update: return None cs = self.virtual_environment[self.T_CS] nc = self.virtual_environment[self.T_NC] network_config = ConfigurationSet() network_config.configuration_set_type = nc[self.T_NC_CST] input_endpoints = nc[self.T_NC_IE] # avoid duplicate endpoint under same cloud service assigned_endpoints = service.get_assigned_endpoints(cs[self.T_CS_SN]) endpoints = map(lambda i: i[self.T_NC_IE_LP], input_endpoints) unassigned_endpoints = map( str, find_unassigned_endpoints(endpoints, assigned_endpoints)) map(lambda (i, u): i.update({self.T_NC_IE_PO: u}), zip(input_endpoints, unassigned_endpoints)) for input_endpoint in input_endpoints: network_config.input_endpoints.input_endpoints.append( ConfigurationSetInputEndpoint(input_endpoint[self.T_NC_IE_N], input_endpoint[self.T_NC_IE_PR], input_endpoint[self.T_NC_IE_PO], input_endpoint[self.T_NC_IE_LP])) return network_config
def create_network_configuration(self, network_endpoints): """ create a network configuration """ network_config = ConfigurationSet() for endpoint in network_endpoints: network_config.input_endpoints.input_endpoints.append(endpoint) network_config.configuration_set_type = 'NetworkConfiguration' return network_config
def _ensureBuildMachineExists(self): """ Creates the VM for the build server. """ service_name = self.config.getBuildServiceName() service_storage_name = self.config.getStorageAccountName() cert_thumbprint = self.config.getServiceCertificateThumbprint() vm_username = self.config.getVirtualMachineLogonUsername() vm_password = self.config.getVirtualMachineLogonPassword() vm_hostname = service_name role_instances = self._getRoleInstances(service_name) if vm_hostname in role_instances: logger.warn("Role instance %s already exists: skipping creation.", vm_hostname) else: logger.info("Role instance %s provisioning begins.", vm_hostname) self._assertOsImageExists(self.config.getBuildOSImageName()) vm_diskname = '{0}.vhd'.format(vm_hostname) vm_disk_media_link = 'http://{0}.blob.core.windows.net/vhds/{1}'.format(service_storage_name, vm_diskname) os_hd = OSVirtualHardDisk(self.config.getBuildOSImageName(), vm_disk_media_link, disk_name=vm_diskname, disk_label=vm_diskname) linux_config = LinuxConfigurationSet(vm_hostname, vm_username, vm_password, True) linux_config.ssh.public_keys.public_keys.append( PublicKey(cert_thumbprint, u'/home/{0}/.ssh/authorized_keys'.format(vm_username)) ) linux_config.ssh.key_pairs.key_pairs.append( KeyPair(cert_thumbprint, u'/home/{0}/.ssh/id_rsa'.format(vm_username)) ) network_config = ConfigurationSet() network_config.configuration_set_type = 'NetworkConfiguration' ssh_endpoint = ConfigurationSetInputEndpoint(name='SSH', protocol='TCP', port=u'22', local_port=u'22') network_config.input_endpoints.input_endpoints.append(ssh_endpoint) result = self.sms.create_virtual_machine_deployment(service_name=service_name, deployment_name=service_name, deployment_slot='Production', label=vm_hostname, role_name=vm_hostname, system_config=linux_config, os_virtual_hard_disk=os_hd, network_config=network_config, availability_set_name=None, data_virtual_hard_disks=None, role_size=self.config.getBuildInstanceRoleSize()) self._wait_for_operation_success(result.request_id, timeout=self.config.getAzureOperationTimeout()) self._wait_for_role_instance_status(vm_hostname, service_name, 'ReadyRole', self.config.getAzureOperationTimeout()) logger.info("Role instance %s has been created.", vm_hostname)
def test_get_virtual_machine_network_config(self): cs_name = 'grtit3' dm_name = 'fdjhfr' vm_name = 'grt65g' self.service.get_virtual_machine = Mock() self.service.get_virtual_machine.return_value = None self.assertIsNone(self.service.get_virtual_machine_network_config(cs_name, dm_name, vm_name)) c = ConfigurationSet() p = PersistentVMRole() p.configuration_sets.configuration_sets = [c] self.service.get_virtual_machine.return_value = p self.assertEqual(self.service.get_virtual_machine_network_config(cs_name, dm_name, vm_name), c)
def test_get_assigned_endpoints(self): cs_name = 'dsfsd' self.service.get_hosted_service_properties = Mock() i = ConfigurationSetInputEndpoint('http', 'tcp', '80', '80') c = ConfigurationSet() c.input_endpoints.input_endpoints = [i] r = Role() r.configuration_sets.configuration_sets = [c] d = Deployment() d.role_list.roles = [r] h = HostedService() h.deployments.deployments = [d] self.service.get_hosted_service_properties.return_value = h self.assertEqual(len(self.service.get_assigned_endpoints(cs_name)), 1)
def delete_endpoint_from_network_config(network_config, private_endpoints): """ Return a new network config :param network_config: :param private_endpoints: a list of int or str :return: """ private_endpoints = map(str, private_endpoints) new_network_config = ConfigurationSet() new_network_config.configuration_set_type = network_config.configuration_set_type if network_config.input_endpoints is not None: for input_endpoint in network_config.input_endpoints.input_endpoints: if input_endpoint.local_port not in private_endpoints: new_network_config.input_endpoints.input_endpoints.append( ConfigurationSetInputEndpoint(input_endpoint.name, input_endpoint.protocol, input_endpoint.port, input_endpoint.local_port)) return new_network_config
def __set_endpoint_config(self, service_name, hackathon_id): """ Set endpoint configuration which is need in creating Azure VM :param service_name: the name of cloud service :type service_name: str|unicode :param hackathon_id: the id of hackathon :type hackathon_id: integer :return: the endpoint configuration set :rtype: class 'azure.servicemanagement.ConfigurationSet' """ used_port_set = self.__get_used_public_port_set( service_name, hackathon_id) docker_port = 4243 ssh_port = 9000 while docker_port in used_port_set: docker_port += 1 while ssh_port in used_port_set: ssh_port += 1 assert docker_port < 40000 and ssh_port < 40000 endpoint_config = ConfigurationSet() endpoint_config.configuration_set_type = AzureVMEnpointConfigType.NETWORK endpoint1 = ConfigurationSetInputEndpoint( name=AzureVMEndpointName.DOCKER, protocol=TCPProtocol.TCP, port=str(docker_port), local_port=str(AzureVMEndpointDefaultPort.DOCKER), load_balanced_endpoint_set_name=None, enable_direct_server_return=False) endpoint2 = ConfigurationSetInputEndpoint( name=AzureVMEndpointName.SSH, protocol=TCPProtocol.TCP, port=str(ssh_port), local_port=str(AzureVMEndpointDefaultPort.SSH), load_balanced_endpoint_set_name=None, enable_direct_server_return=False) endpoint_config.input_endpoints.input_endpoints.append(endpoint1) endpoint_config.input_endpoints.input_endpoints.append(endpoint2) return endpoint_config
def _ensureVirtualMachinesExist(self): """ Creates the VMs for the web site. """ service_name = self.config.getServiceName() cert_thumbprint = self.config.getServiceCertificateThumbprint() vm_username = self.config.getVirtualMachineLogonUsername() vm_password = self.config.getVirtualMachineLogonPassword() vm_role_size = self.config.getServiceInstanceRoleSize() vm_numbers = self.config.getServiceInstanceCount() if vm_numbers < 1: raise Exception( "Detected an invalid number of instances: {0}.".format( vm_numbers)) self._assertOsImageExists(self.config.getServiceOSImageName()) role_instances = self._getRoleInstances(service_name) for vm_number in range(1, vm_numbers + 1): vm_hostname = '{0}-{1}'.format(service_name, vm_number) if vm_hostname in role_instances: logger.warn( "Role instance %s already exists: skipping creation.", vm_hostname) continue logger.info("Role instance %s provisioning begins.", vm_hostname) vm_diskname = '{0}.vhd'.format(vm_hostname) vm_disk_media_link = 'http://{0}.blob.core.windows.net/vhds/{1}'.format( self.config.getServiceStorageAccountName(), vm_diskname) ssh_port = str(self.config.getServiceInstanceSshPort() + vm_number) os_hd = OSVirtualHardDisk(self.config.getServiceOSImageName(), vm_disk_media_link, disk_name=vm_diskname, disk_label=vm_diskname) linux_config = LinuxConfigurationSet(vm_hostname, vm_username, vm_password, True) linux_config.ssh.public_keys.public_keys.append( PublicKey( cert_thumbprint, u'/home/{0}/.ssh/authorized_keys'.format(vm_username))) linux_config.ssh.key_pairs.key_pairs.append( KeyPair(cert_thumbprint, u'/home/{0}/.ssh/id_rsa'.format(vm_username))) network_config = ConfigurationSet() network_config.configuration_set_type = 'NetworkConfiguration' ssh_endpoint = ConfigurationSetInputEndpoint(name='SSH', protocol='TCP', port=ssh_port, local_port=u'22') network_config.input_endpoints.input_endpoints.append(ssh_endpoint) http_endpoint = ConfigurationSetInputEndpoint( name='HTTP', protocol='TCP', port=u'80', local_port=u'80', load_balanced_endpoint_set_name=service_name) http_endpoint.load_balancer_probe.port = '80' http_endpoint.load_balancer_probe.protocol = 'TCP' network_config.input_endpoints.input_endpoints.append( http_endpoint) if vm_number == 1: result = self.sms.create_virtual_machine_deployment( service_name=service_name, deployment_name=service_name, deployment_slot='Production', label=vm_hostname, role_name=vm_hostname, system_config=linux_config, os_virtual_hard_disk=os_hd, network_config=network_config, availability_set_name=service_name, data_virtual_hard_disks=None, role_size=vm_role_size) self._wait_for_operation_success( result.request_id, timeout=self.config.getAzureOperationTimeout()) self._wait_for_role_instance_status( vm_hostname, service_name, 'ReadyRole', self.config.getAzureOperationTimeout()) else: result = self.sms.add_role(service_name=service_name, deployment_name=service_name, role_name=vm_hostname, system_config=linux_config, os_virtual_hard_disk=os_hd, network_config=network_config, availability_set_name=service_name, role_size=vm_role_size) self._wait_for_operation_success( result.request_id, timeout=self.config.getAzureOperationTimeout()) self._wait_for_role_instance_status( vm_hostname, service_name, 'ReadyRole', self.config.getAzureOperationTimeout()) logger.info("Role instance %s has been created.", vm_hostname)
linux_config = LinuxConfigurationSet(vm_name, 'rohan', 'qwerty12#', True) SERVICE_CERT_THUMBPRINT = config_params["vm_cert_thumbprint"] vm_public_key_path = config_params["vm_pub_key_path"] pk = PublicKey(SERVICE_CERT_THUMBPRINT, vm_public_key_path) pair = KeyPair(SERVICE_CERT_THUMBPRINT, vm_public_key_path) linux_config.ssh = SSH() linux_config.ssh.key_pairs.key_pairs.append(pair) linux_config.ssh.public_keys.public_keys.append(pk) endpoint_config = ConfigurationSet() endpoint_config.configuration_set_type = 'NetworkConfiguration' ssh_endpoint = ConfigurationSetInputEndpoint( name='ssh', protocol='tcp', port='22', local_port='22', load_balanced_endpoint_set_name=None, enable_direct_server_return=False) http_endpoint = ConfigurationSetInputEndpoint( name='http', protocol='tcp', port='80', local_port='80', load_balanced_endpoint_set_name=None,