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(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 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 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 get_network_config(self, azure_key_id, update): """ Return None if image type is vm and not update Public endpoint should be assigned in real time :param service: :return: """ azure_service = RequiredFeature("azure_service") if self.is_vm_image() and not update: return None cs = self.virtual_environment[self.T_CLOUD_SERVICE] nc = self.virtual_environment[self.T_NETWORK_CONFIG] network_config = ConfigurationSet() network_config.configuration_set_type = nc[self.T_CONFIGURATION_SET_TYPE] input_endpoints = nc[self.T_INPUT_ENDPOINTS] # avoid duplicate endpoint under same cloud service assigned_endpoints = azure_service.get_assigned_endpoints(azure_key_id, cs[self.SERVICE_NAME]) endpoints = map(lambda i: i[self.T_LOCAL_PORT], input_endpoints) unassigned_endpoints = map(str, find_unassigned_endpoints(endpoints, assigned_endpoints)) map(lambda (i, u): i.update({self.PORT: u}), zip(input_endpoints, unassigned_endpoints)) for input_endpoint in input_endpoints: network_config.input_endpoints.input_endpoints.append( ConfigurationSetInputEndpoint( input_endpoint[self.NAME], input_endpoint[self.PROTOCOL], input_endpoint[self.PORT], input_endpoint[self.T_LOCAL_PORT] ) ) 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 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 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 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 __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)
def create_instance(self, image_id=None, key_name=None, security_groups=None, instancetype_id="ExtraSmall", name=None, zone=None, subnet_id=None, private_ips=None, user_data=None, storage_name=None, username=None, password=None): ''' service_account = None ''' storage_account = None service_name = name result = self.__SMS.check_hosted_service_name_availability(service_name) if result is False: msg = "Hosted service with name:{} exists. please use different name".format(name) raise Exception(msg) azure_storages = self.__SMS.list_storage_accounts() if storage_name is None: # storage name not provided,pick any storage account as stroage # account is an expensive operation for azure_storage in azure_storages: storage_account = azure_storage storage_name = storage_account.service_name break else: for azure_storage in azure_storages: if azure_storage.service_name == storage_name: storage_account = azure_storage break if storage_account is None: if storage_name is None: storage_name = service_name response = self.__SMS.create_storage_account(storage_name, service_name, service_name, location=self._credentials['region_name']) import time for i in range(60): status = self.__SMS.get_operation_status(response.request_id) time.sleep(2) if not status == "InProgress": break keys = self.__SMS.get_storage_account_keys(storage_name) blob_service = BlobService(account_name=storage_name, account_key=keys.storage_service_keys.primary) blob_service.create_container(service_name) target_blob_name = name + ".vhd" os_image_url = "http://{}.blob.core.windows.net/{}/{}".format(storage_name, service_name, target_blob_name) azure_images = self.__SMS.list_os_images() image = None for azure_image in azure_images: if azure_image.name == image_id: image = azure_image break os_config = None endpoint_config = ConfigurationSet() endpoint_config.configuration_set_type = 'NetworkConfiguration' if image.os == 'Windows': os_config = WindowsConfigurationSet(computer_name=name, admin_username=username, admin_password=password) os_config.domain_join = None endpoint1 = ConfigurationSetInputEndpoint(name='rdp', protocol='tcp', port='3389', local_port='3389', load_balanced_endpoint_set_name=None, enable_direct_server_return=False) endpoint2 = ConfigurationSetInputEndpoint(name='web', protocol='tcp', port='80', local_port='80', 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) from azure.servicemanagement import WinRM from azure.servicemanagement import Listener endpoint_config.win_rm = WinRM() listner = Listener(protocol='http') os_config.win_rm.listeners.listeners.append(listner) if image.os == 'Linux': os_config = LinuxConfigurationSet(host_name=name, user_name=username, user_password=password, disable_ssh_password_authentication=False) os_config.domain_join = None endpoint1 = ConfigurationSetInputEndpoint(name='ssh', protocol='tcp', port='22', local_port='22', load_balanced_endpoint_set_name=None, enable_direct_server_return=False) endpoint_config.input_endpoints.input_endpoints.append(endpoint1) os_hd = OSVirtualHardDisk(image_id, os_image_url, disk_label=target_blob_name) self.__SMS.create_hosted_service(service_name, service_name, location=self._credentials['region_name']) # service_account = self.__SMS.get_hosted_service_properties(service_name) self.__SMS.create_virtual_machine_deployment(service_name=service_name, deployment_name=service_name, deployment_slot='production', label=service_name, role_name=service_name, system_config=os_config, os_virtual_hard_disk=os_hd, role_size=instancetype_id, network_config=endpoint_config) new_service = self.__SMS.get_hosted_service_properties(service_name) azure_instance = AzureInstancecls(new_service, credentials=self._credentials) return azure_instance
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,
else: self.sms.create_storage_account( service_name=storage, description=storage + " made from libcloud", label=storage, affinity_group=affinity_group, account_type='Standard_LRS') # check configuration from image extra["os"] if (image.extra["os"] == u'Linux'): vm_conf = LinuxConfigurationSet( name, vm_user, vm_password, True) network = ConfigurationSet() network.configuration_set_type = 'NetworkConfiguration' network.input_endpoints.input_endpoints.append( ConfigurationSetInputEndpoint('ssh', 'tcp', '22', '22')) else: vm_conf = WindowsConfigurationSet( computer_name=name, admin_username=vm_user, admin_password=vm_password) vm_conf.domain_join = None vm_conf.win_rm = None network = ConfigurationSet() network.configuration_set_type = 'NetworkConfiguration' network.input_endpoints.input_endpoints.append( ConfigurationSetInputEndpoint('ssh', 'tcp', '3389', '3389'))
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, enable_direct_server_return=False) endpoint_config.input_endpoints.input_endpoints.append(ssh_endpoint) endpoint_config.input_endpoints.input_endpoints.append(http_endpoint) result = sms.create_virtual_machine_deployment(service_name=hosted_service_name, deployment_name=hosted_service_name,
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)