Esempio n. 1
0
def launch_node(node_id, creds, params, init, ssh_username, ssh_private_key):
    # "pip install azure"
    sms = ServiceManagementService(
        subscription_id='69581868-8a08-4d98-a5b0-1d111c616fc3',
        cert_file='/Users/dgriffin/certs/iOSWAToolkit.pem')
    for i in sms.list_os_images():
        print 'I is ', i.name, ' -- ', i.label, ' -- ', i.location, ' -- ', i.media_link
    media_link = \
        'http://opdemandstorage.blob.core.windows.net/communityimages/' + \
        'b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu_DAILY_BUILD-' + \
        'precise-12_04_2-LTS-amd64-server-20130702-en-us-30GB.vhd'
    config = LinuxConfigurationSet(user_name="ubuntu",
                                   user_password="******")
    hard_disk = OSVirtualHardDisk(
        'b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu_DAILY_BUILD-' +
        'precise-12_04_2-LTS-amd64-server-20130702-en-us-30GB',
        media_link,
        disk_label='opdemandservice')
    ret = sms.create_virtual_machine_deployment('opdemandservice', 'deploy1',
                                                'production',
                                                'opdemandservice2',
                                                'opdemandservice3', config,
                                                hard_disk)
    # service_name, deployment_name, deployment_slot, label, role_name
    # system_config, os_virtual_hard_disk
    print 'Ret ', ret
    return sms
Esempio n. 2
0
    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 get_system_config(self):
        if self.is_vm_image():
            return None

        sc = self.virtual_environment[AZURE_UNIT.SYSTEM_CONFIG]
        # check whether virtual machine is Windows or Linux
        if sc[AZURE_UNIT.SYSTEM_CONFIG_OS_FAMILY] == AZURE_UNIT.WINDOWS:
            system_config = WindowsConfigurationSet(
                computer_name=sc[AZURE_UNIT.SYSTEM_CONFIG_HOST_NAME],
                admin_password=sc[AZURE_UNIT.SYSTEM_CONFIG_USER_PASSWORD],
                admin_username=sc[AZURE_UNIT.SYSTEM_CONFIG_USER_NAME])
            system_config.domain_join = None
            system_config.win_rm = None
        else:
            system_config = LinuxConfigurationSet(
                host_name=sc[AZURE_UNIT.SYSTEM_CONFIG_HOST_NAME],
                user_name=sc[AZURE_UNIT.SYSTEM_CONFIG_USER_NAME],
                user_password=sc[AZURE_UNIT.SYSTEM_CONFIG_USER_PASSWORD],
                disable_ssh_password_authentication=False)
        return system_config
Esempio n. 4
0
 def create_linux_configuration(self,
                                username='******',
                                instance_name=None,
                                disable_ssh_password_authentication=True,
                                password=None,
                                custom_data=None,
                                fingerprint=''):
     """
         create a linux configuration
     """
     self.__validate_custom_data_length(custom_data)
     # The given instance name is used as the host name in linux
     linux_config = LinuxConfigurationSet(
         instance_name, username, password,
         disable_ssh_password_authentication, custom_data)
     if fingerprint:
         ssh_key_file = '/home/' + username + '/.ssh/authorized_keys'
         ssh_pub_key = PublicKey(fingerprint, ssh_key_file)
         linux_config.ssh.public_keys = [ssh_pub_key]
     return linux_config
Esempio n. 5
0
 def get_system_config(self):
     """
     Return None if image type is vm
     :return:
     """
     if self.is_vm_image():
         return None
     sc = self.virtual_environment[self.T_SC]
     # check whether virtual machine is Windows or Linux
     if sc[self.T_SC_OF] == self.WINDOWS:
         system_config = WindowsConfigurationSet(
             computer_name=sc[self.T_SC_HN],
             admin_password=sc[self.T_SC_UP],
             admin_username=sc[self.T_SC_UN])
         system_config.domain_join = None
         system_config.win_rm = None
     else:
         system_config = LinuxConfigurationSet(
             host_name=sc[self.T_SC_HN],
             user_name=sc[self.T_SC_UN],
             user_password=sc[self.T_SC_UP],
             disable_ssh_password_authentication=False)
     return system_config
Esempio n. 6
0
    def create_machine(self, name, region='West US',
                       image=None, role_size='Small',
                       min_count=1, max_count=1,
                       media='storage_url_blob_cloudrunner',
                       username='', password='', ssh_pub_key='',
                       server=CR_SERVER,
                       cleanup=None, **kwargs):
        self.log.info("Registering Azure machine [%s::%s] for [%s]" %
                      (name, image, CR_SERVER))
        try:
            sms = ServiceManagementService(self.profile.username,
                                           self._cert_path)
            server_config = LinuxConfigurationSet('myhostname', 'myuser',
                                                  'mypassword', True)
            media_link = "%s__%s" % (media, name)
            os_hd = OSVirtualHardDisk(image, media_link)

            res = sms.create_virtual_machine_deployment(
                service_name=name,
                deployment_name=name,
                deployment_slot='production',
                label=name,
                role_name=name,
                system_config=server_config,
                os_virtual_hard_disk=os_hd,
                role_size='Small')

            instance_ids = []
            meta = {}
            if not res:
                return self.FAIL, [], {}
            meta['deployment_name'] = name
            meta['cleanup_service'] = cleanup in ['1', 'True', 'true']
            return self.OK, instance_ids, meta
        except Exception, ex:
            self.log.exception(ex)
            return self.FAIL, [], {}
    def create_virtual_machine(self, service_name, vm_name, image_name,
                               role_size):
        media_link = self._get_media_link(vm_name)
        # Linux VM configuration
        hostname = '-'.join((vm_name, 'host'))
        linux_config = LinuxConfigurationSet(hostname, self.linux_user,
                                             self.linux_pass, True)

        # Hard disk for the OS
        os_hd = OSVirtualHardDisk(image_name, media_link)

        # Create vm
        result = self.sms.create_virtual_machine_deployment(
            service_name=service_name,
            deployment_name=vm_name,
            deployment_slot='production',
            label=vm_name,
            role_name=vm_name,
            system_config=linux_config,
            os_virtual_hard_disk=os_hd,
            role_size=role_size)
        request_id = result.request_id

        return {'request_id': request_id, 'media_link': media_link}
Esempio n. 8
0
    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)
Esempio n. 9
0
def createInstances(sms, name, region, imageID, instanceSize, pkfile, count):
    hostname = name
    username = "******"
    password = None
    affGrp = affinity_group = createAffinityGrp(sms, name, name, name, region)
    storage_name = StorageCreate(sms, name, name, name, region, affGrp)
    media_link = "http://" + storage_name + ".blob.core.windows.net/disks/" + name + ".vhd"
    log.info("Configuring Media Link %s", media_link)
    Service_url = newCloudService(sms, name, name, name, region, affGrp)
    Service_name = name

    # Configuring EndPoint "Firwall Configuration":
    endpoint_config = ConfigurationSet()
    endpoint_config.configuration_set_type = 'NetworkConfiguration'
    endpoint1 = ConfigurationSetInputEndpoint(
        name='HTTP',
        protocol='tcp',
        port='80',
        local_port='80',
        load_balanced_endpoint_set_name=None,
        enable_direct_server_return=False)
    endpoint2 = 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)
    endpoint_config.input_endpoints.input_endpoints.append(endpoint2)
    log.info("Configuring EndPoints 'Firwall Configuration' port 22 and 80")

    # Linux VM Configuration:
    linux_config = LinuxConfigurationSet(hostname, username, password, True)
    publickey = PublicKey(thumbprint, pkfile)
    linux_config.ssh.public_keys.public_keys.append(publickey)
    log.info("Linux VM Configuration")

    # Configuring Image ID:
    #----------------------
    os_hd = OSVirtualHardDisk(imageID, media_link)
    log.info("Configuring The Virtual Hard Disk using Image ID: %s", imageID)

    # Start Deployment of VM on Azure:
    log.info("Start Deployment of Elastic hpc on Azure")

    # Configuring Certificates:
    cert_format = 'pfx'
    cert_password = '******'

    try:
        for num in range(count):
            log.info("Deploying Node number: %d", num)
            name = name + "-" + str(num)
            result_cert = sms.add_service_certificate(
                service_name=Service_name,
                data=cert_data,
                certificate_format=cert_format,
                password=cert_password)
            log.info("Start Deploying VM with Name: " + name)
            try:
                log.info(vars(result_cert))
            except:
                log.info(result_cert)
            result = sms.create_virtual_machine_deployment(
                service_name=Service_name,
                deployment_name=name,
                deployment_slot='production',
                label=name,
                role_name=name,
                system_config=linux_config,
                os_virtual_hard_disk=os_hd,
                network_config=endpoint_config,
                role_size=instanceSize)
            operation_result = sms.get_operation_status(result.request_id)
            log.info("Start Deployment .............")
            for i in range(100):
                time.sleep(2)
                sys.stdout.write("Deployment Status Progress \r%d .%%" % i)
                sys.stdout.flush()
            print "\n"
            log.info("ssh -i " + certificate_path + " " + username + "@" +
                     Service_name + ".cloudapp.net")
    except WindowsAzureConflictError:
        log.info("Error: Can not Create VM")
        sys.exit(1)
Esempio n. 10
0
    def createInstances(self, sms, name, region, imageID, instanceSize, pkfile,
                        count, thumbprint, cert_data, num_instances,
                        certPasswd, storagename, master_size):
        hostname = name
        mainPath = os.path.dirname(os.path.abspath(__file__))
        username = "******"
        password = None
        affGrp = self.createAffinityGrp(sms, storagename, storagename,
                                        storagename, region)
        storage_name = self.StorageCreate(sms, storagename, storagename,
                                          storagename, region, affGrp)
        account_key = self.getStorageAccountKey(sms, storage_name)
        permission = None
        container_name = self.get_vm_name()
        account_name = storage_name
        container_name = self.create_containers(sms, storage_name, account_key,
                                                container_name.lower(), None)
        time.sleep(5)
        #print "Container Name:"+disks
        medialink = "http://" + storage_name + ".blob.core.windows.net/" + container_name + "/"
        # --------------------------------------------------------
        blobService = BlobService(account_name=storage_name,
                                  account_key=account_key)
        blobName = container_name + "blob.vhd"
        try:
            image = sms.get_os_image(imageID)
        except:
            if (self.copy_image_vmDepot(blobService, container_name, blobName,
                                        imageID, sms)):
                print "INFO -- The Disk Blob has been copied"
            media_link = "http://" + storage_name + ".blob.core.windows.net/" + container_name + "/" + blobName
            if (self.make_os_image(sms, media_link, imageID)):
                print "INFO -- The image '" + imageID + "' is ready now!!"

            else:
                print "Error: Can not complete creating The image"
                exit(0)

        #-----------------------------------------------------------
        medialink = "http://" + storage_name + ".blob.core.windows.net/" + container_name + "/"
        media_link = ""
        # Configuring EndPoint "Firwall Configuration":
        endpoint_config = ConfigurationSet()
        endpoint_config.configuration_set_type = 'NetworkConfiguration'
        endpoint1 = ConfigurationSetInputEndpoint(
            name='XML',
            protocol='tcp',
            port='5000',
            local_port='5000',
            load_balanced_endpoint_set_name=None,
            enable_direct_server_return=False)
        endpoint_config.input_endpoints.input_endpoints.append(endpoint1)
        self.logger.info(
            "Configuring EndPoints 'Firwall Configuration' SHH, PBS Torque and OpenMPI ports"
        )
        # Linux VM Configuration:
        linux_config = LinuxConfigurationSet(hostname, username, password,
                                             True)
        publickey = PublicKey(thumbprint, pkfile)
        linux_config.ssh.public_keys.public_keys.append(publickey)
        self.logger.info("Linux VM Configuration")
        # Configuring Image ID:
        #----------------------
        os_hd = OSVirtualHardDisk(imageID, media_link)
        self.logger.info(
            "Configuring The Virtual Hard Disk using Image ID: %s", imageID)

        # Start Deployment of VM on Azure:
        self.logger.info("Start Deployment of Elastic hpc on Azure")

        # Configuring Certificates:
        cert_format = 'pfx'
        cert_password = certPasswd
        VMname = hostname
        vmname = hostname
        instances = []

        try:
            for num in range(count):
                name = vmname + str(num)
                vname = vmname
                Service_name = vname
                Service_url = self.newCloudService(sms, Service_name,
                                                   Service_name, Service_name,
                                                   region, affGrp)
                endpoint3 = ConfigurationSetInputEndpoint(
                    name='SSH' + str(num),
                    protocol='tcp',
                    port='220' + str(num),
                    local_port='22',
                    load_balanced_endpoint_set_name=None,
                    enable_direct_server_return=False)
                endpoint_config.input_endpoints.input_endpoints.append(
                    endpoint3)
                #endpoint4 = ConfigurationSetInputEndpoint(name="FTP", protocol='tcp', port='21', local_port='21', load_balanced_endpoint_set_name=None, enable_direct_server_return=False)
                #endpoint_config.input_endpoints.input_endpoints.append(endpoint4)
                #endpoint5 = ConfigurationSetInputEndpoint(name="FTP1", protocol='tcp', port='20', local_port='20', load_balanced_endpoint_set_name=None, enable_direct_server_return=False)
                #endpoint_config.input_endpoints.input_endpoints.append(endpoint5)
                #endpoint6 = ConfigurationSetInputEndpoint(name="FTPudp", protocol='udp', port='21', local_port='21', load_balanced_endpoint_set_name=None, enable_direct_server_return=False)
                #endpoint_config.input_endpoints.input_endpoints.append(endpoint6)
                #for i in range(6):
                #	endpointpasv = ConfigurationSetInputEndpoint(name="FTPpasv"+str(i), protocol='tcp', port='4000'+str(i), local_port='4000'+str(i), load_balanced_endpoint_set_name=None, enable_direct_server_return=False)
                #	endpoint_config.input_endpoints.input_endpoints.append(endpointpasv)
                pbs_endpoints = self.get_pbs_endpoints(0)
                for endpoint in pbs_endpoints:
                    endpoint_config.input_endpoints.input_endpoints.append(
                        endpoint)
                media_link = medialink + name[:-1] + ".vhd"
                self.logger.info("Configuring Media Link %s", media_link)
                # Configuring Image ID:
                #----------------------
                os_hd = OSVirtualHardDisk(imageID, media_link)
                self.logger.info(
                    "Configuring The Virtual Hard Disk using Image ID: %s",
                    imageID)
                self.logger.info("Deploying Node number: %d", num)
                result_cert = sms.add_service_certificate(
                    service_name=Service_name,
                    data=cert_data,
                    certificate_format=cert_format,
                    password=cert_password)
                self.logger.info("Start Deploying VM with Name: " + vname)
                try:
                    self.logger.info(vars(result_cert))
                except:
                    self.logger.info(result_cert)
                time.sleep(5)
                result = sms.create_virtual_machine_deployment(
                    service_name=Service_name,
                    deployment_name=vname,
                    deployment_slot='production',
                    label=Service_name,
                    role_name=vname,
                    system_config=linux_config,
                    os_virtual_hard_disk=os_hd,
                    network_config=endpoint_config,
                    role_size=master_size)
                #role_size="Large")
                self.logger.info("Start Deployment")
                self.wait_for_async(sms, result.request_id)
                self.wait_for_deployment_status(sms, Service_name, vname,
                                                'Running')
                self.wait_for_role_instance_status(sms, Service_name, vname,
                                                   vname, 'ReadyRole')
                instances.append(Service_name + ".cloudapp.net")
                instances.append(container_name)
        except WindowsAzureConflictError:
            self.logger.info("Error: Can not Create VM")
            sys.exit(1)
        return instances
Esempio n. 11
0
            result = azure.create_hosted_service(service_name=name, label=name, location=location)
            _wait_for_completion(azure, result, wait_timeout, "create_hosted_service")
            changed = True
        except AzureException, e:
            module.fail_json(msg="failed to create the new service, error was: %s" % str(e))

    try:
        # check to see if a vm with this name exists; if so, do nothing
        azure.get_role(name, name, name)
    except AzureMissingException:
        # vm does not exist; create it
        
        if os_type == 'linux':
            # Create linux configuration
            disable_ssh_password_authentication = not password
            vm_config = LinuxConfigurationSet(hostname, user, password, disable_ssh_password_authentication)
        else:
            #Create Windows Config
            vm_config = WindowsConfigurationSet(hostname, password, module.params.get('reset_pass_atlogon'),\
                                                 module.params.get('auto_updates'), None, user)
            vm_config.domain_join = None
            if module.params.get('enable_winrm'):
                listener = Listener('Http')
                vm_config.win_rm.listeners.listeners.append(listener)
            else:
                vm_config.win_rm = None

        # Add ssh certificates if specified
        if ssh_cert_path:
            fingerprint, pkcs12_base64 = get_ssh_certificate_tokens(module, ssh_cert_path)
            # Add certificate to cloud service
Esempio n. 12
0
            _wait_for_completion(azure, result, wait_timeout,
                                 "create_hosted_service")
            changed = True
        except WindowsAzureError, e:
            module.fail_json(
                msg="failed to create the new service, error was: %s" % str(e))

    try:
        # check to see if a vm with this name exists; if so, do nothing
        azure.get_role(name, name, name)
    except WindowsAzureMissingResourceError:
        # vm does not exist; create it

        # Create linux configuration
        disable_ssh_password_authentication = not password
        linux_config = LinuxConfigurationSet(
            hostname, user, password, disable_ssh_password_authentication)

        # Add ssh certificates if specified
        if ssh_cert_path:
            fingerprint, pkcs12_base64 = get_ssh_certificate_tokens(
                module, ssh_cert_path)
            # Add certificate to cloud service
            result = azure.add_service_certificate(name, pkcs12_base64, 'pfx',
                                                   '')
            _wait_for_completion(azure, result, wait_timeout,
                                 "add_service_certificate")

            # Create ssh config
            ssh_config = SSH()
            ssh_config.public_keys = PublicKeys()
            authorized_keys_path = u'/home/%s/.ssh/authorized_keys' % user
Esempio n. 13
0
result = blob_service.create_container(container_name)

container_url_template = "http://{}.blob.core.windows.net/{}"

container_url = container_url_template.format(storage_acc_name, container_name)

image_name = "b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140414-en-us-30GB"

blob_url = container_url + "/ubuntu.vhd"

os_hd = OSVirtualHardDisk(image_name, blob_url)

vm_name = name_generator()

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)
Esempio n. 14
0
result = blob_service.create_container(container_name)

container_url_template = "http://{}.blob.core.windows.net/{}"

container_url = container_url_template.format(storage_acc_name, container_name)

image_name = "b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140414-en-us-30GB"

blob_url = container_url + "/ubuntu.vhd"

os_hd = OSVirtualHardDisk(image_name, blob_url)

vm_name = name_generator()

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()
Esempio n. 15
0
    def create_docker_host_vm(self, hackathon_id):
        """
        create docker host VM for hackathon whose id is hackathon_id

        :param hackathon_id: the id of hackathon in DB table:hackathon
        :type hackathon_id: Integer

        :return: True if send an Azure creating VM request via API successfully after validating storage, container and
         service
         Otherwise, False
        :rtype: bool
        """
        sms = self.__get_sms_object(hackathon_id)
        if sms is None:
            self.log.error('No Azure account found for Hackathon:%d' % hackathon_id)
            return False
        # get storage and container
        res, storage_account_name, container_name = self.__get_available_storage_account_and_container(hackathon_id)
        if not res:
            return False
        # get service
        res, service_name = self.__get_available_cloud_service(hackathon_id)
        if not res:
            return False
        # set host_name to ensure its uniqueness
        host_name = str(uuid1())[0:9] + strftime("%Y%m%d%H%M%S")
        # set vm os image and hard disk storage
        image_name_default = 'b549f4301d0b4295b8e76ceb65df47d4__Ubuntu-14_04-LTS-amd64-server-20140606.1-en-us-30GB'
        image_name = self.util.safe_get_config('dockerhostserver.vm.image_name', image_name_default)
        media_link = 'https://%s.blob.core.chinacloudapi.cn/%s/%s.vhd' % (storage_account_name, container_name,
                                                                          host_name)
        os_hd = OSVirtualHardDisk(image_name, media_link)
        # set linux_config and endpoint_config
        customdata = self.__get_customdata_from_local_file()
        linux_config = LinuxConfigurationSet(host_name,
                                             self.util.safe_get_config('dockerhostserver.vm.username', 'opentech'),
                                             self.util.safe_get_config('dockerhostserver.vm.password', 'Password01!'),
                                             False, custom_data=customdata)
        endpoint_config = self.__set_endpoint_config(service_name, hackathon_id)
        deployment_exist = True
        deployment_slot = ServiceDeploymentSlot.PRODUCTION
        try:
            deployment_exist = self.__deployment_exists(service_name, deployment_slot, hackathon_id)
        except Exception as e:
            self.log.error(e)
            return False
        if not deployment_exist:
            try:
                result = sms.create_virtual_machine_deployment(service_name=service_name,
                                                               network_config=endpoint_config,
                                                               deployment_name=service_name,
                                                               deployment_slot=deployment_slot,
                                                               label=service_name,
                                                               role_name=host_name,
                                                               system_config=linux_config,
                                                               os_virtual_hard_disk=os_hd,
                                                               role_size=AzureVMSize.MEDIUM_SIZE)
                self.log.debug('To create VM:%s in service:%s.(deployment)' % (host_name, service_name))
            except Exception as e:
                self.log.error(e)
                return False
        else:
            try:
                result = sms.add_role(service_name=service_name,
                                      deployment_name=service_name,
                                      role_name=host_name,
                                      system_config=linux_config,
                                      os_virtual_hard_disk=os_hd,
                                      network_config=endpoint_config,
                                      role_size=AzureVMSize.MEDIUM_SIZE)
                self.log.debug('To create VM:%s in service:%s.(add role)' % (host_name, service_name))
            except Exception as e:
                self.log.error(e)
                return False
        # storage parameters in context
        context = Context(hackathon_id=hackathon_id, request_id=result.request_id,
                          service_name=service_name, role_name=host_name,
                          deployment_name=service_name, deployment_slot=deployment_slot,
                          host_name=host_name)
        # start schedule
        self.sche.add_once('docker_host_manager', 'check_vm_status', context=context, minutes=5)
        return True
Esempio n. 16
0
def create_virtual_machine(module, azure):
    """
    Create new virtual machine

    module : AnsibleModule object
    azure: authenticated azure ServiceManagementService object

    Returns:
        True if a new virtual machine and/or cloud service was created, false otherwise
    """
    name = module.params.get('name')
    hostname = module.params.get('hostname') or name + ".cloudapp.net"
    endpoints = module.params.get('endpoints').split(',')
    ssh_cert_path = module.params.get('ssh_cert_path')
    user = module.params.get('user')
    password = module.params.get('password')
    location = module.params.get('location')
    role_size = module.params.get('role_size')
    storage_account = module.params.get('storage_account')
    image = module.params.get('image')
    virtual_network_name = module.params.get('virtual_network_name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))

    changed = False

    # Check if a deployment with the same name already exists
    cloud_service_name_available = azure.check_hosted_service_name_availability(name)
    if cloud_service_name_available.result:
        # cloud service does not exist; create it
        try:
            result = azure.create_hosted_service(service_name=name, label=name, location=location)
            _wait_for_completion(azure, result, wait_timeout, "create_hosted_service")
            changed = True
        except WindowsAzureError as e:
            module.fail_json(msg="failed to create the new service, error was: %s" % str(e))

    try:
        # check to see if a vm with this name exists; if so, do nothing
        azure.get_role(name, name, name)
    except WindowsAzureMissingResourceError:
        # vm does not exist; create it

        # Create linux configuration
        disable_ssh_password_authentication = not password
        linux_config = LinuxConfigurationSet(hostname, user, password, disable_ssh_password_authentication)

        # Add ssh certificates if specified
        if ssh_cert_path:
            fingerprint, pkcs12_base64 = get_ssh_certificate_tokens(module, ssh_cert_path)
            # Add certificate to cloud service
            result = azure.add_service_certificate(name, pkcs12_base64, 'pfx', '')
            _wait_for_completion(azure, result, wait_timeout, "add_service_certificate")

            # Create ssh config
            ssh_config = SSH()
            ssh_config.public_keys = PublicKeys()
            authorized_keys_path = u'/home/%s/.ssh/authorized_keys' % user
            ssh_config.public_keys.public_keys.append(PublicKey(path=authorized_keys_path, fingerprint=fingerprint))
            # Append ssh config to linux machine config
            linux_config.ssh = ssh_config

        # Create network configuration
        network_config = ConfigurationSetInputEndpoints()
        network_config.configuration_set_type = 'NetworkConfiguration'
        network_config.subnet_names = []
        network_config.public_ips = None
        for port in endpoints:
            network_config.input_endpoints.append(ConfigurationSetInputEndpoint(name='TCP-%s' % port,
                                                                                protocol='TCP',
                                                                                port=port,
                                                                                local_port=port))

        # First determine where to store disk
        today = datetime.date.today().strftime('%Y-%m-%d')
        disk_prefix = u'%s-%s' % (name, name)
        media_link = u'http://%s.blob.core.windows.net/vhds/%s-%s.vhd' % (storage_account, disk_prefix, today)
        # Create system hard disk
        os_hd = OSVirtualHardDisk(image, media_link)

        # Spin up virtual machine
        try:
            result = azure.create_virtual_machine_deployment(service_name=name,
                                                             deployment_name=name,
                                                             deployment_slot='production',
                                                             label=name,
                                                             role_name=name,
                                                             system_config=linux_config,
                                                             network_config=network_config,
                                                             os_virtual_hard_disk=os_hd,
                                                             role_size=role_size,
                                                             role_type='PersistentVMRole',
                                                             virtual_network_name=virtual_network_name)
            _wait_for_completion(azure, result, wait_timeout, "create_virtual_machine_deployment")
            changed = True
        except WindowsAzureError as e:
            module.fail_json(msg="failed to create the new virtual machine, error was: %s" % str(e))

    try:
        deployment = azure.get_deployment_by_name(service_name=name, deployment_name=name)
        return (changed, urlparse(deployment.url).hostname, deployment)
    except WindowsAzureError as e:
        module.fail_json(msg="failed to lookup the deployment information for %s, error was: %s" % (name, str(e)))
Esempio n. 17
0
def create_virtual_machine(module, azure):
    """
    Create new virtual machine

    module : AnsibleModule object
    azure: authenticated azure ServiceManagementService object

    Returns:
        True if a new virtual machine was created, false otherwise
    """
    name = module.params.get('name')
    hostname = module.params.get('hostname') or name + ".cloudapp.net"
    endpoints = module.params.get('endpoints').split(',')
    ssh_cert_path = module.params.get('ssh_cert_path')
    user = module.params.get('user')
    password = module.params.get('password')
    location = module.params.get('location')
    role_size = module.params.get('role_size')
    storage_account = module.params.get('storage_account')
    image = module.params.get('image')
    vm_image = module.params.get('vm_image')
    virtual_network_name = module.params.get('virtual_network_name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))

    use_vm_image = vm_image is not None and image is None

    # Check if a deployment with the same name already exists
    cloud_service_name_available = azure.check_hosted_service_name_availability(
        name)
    if not cloud_service_name_available.result:
        changed = False
    else:
        changed = True
        # Create cloud service if necessary
        try:
            result = azure.create_hosted_service(service_name=name,
                                                 label=name,
                                                 location=location)
            _wait_for_completion(azure, result, wait_timeout,
                                 "create_hosted_service")
        except WindowsAzureError as e:
            module.fail_json(
                msg=
                "failed to create the new service name, it already exists: %s"
                % str(e))

        # Create linux configuration
        if not use_vm_image:
            disable_ssh_password_authentication = not password
            linux_config = LinuxConfigurationSet(
                hostname, user, password, disable_ssh_password_authentication)

            # Add ssh certificates if specified
            if ssh_cert_path:
                fingerprint, pkcs12_base64 = get_ssh_certificate_tokens(
                    module, ssh_cert_path)
                # Add certificate to cloud service
                result = azure.add_service_certificate(name, pkcs12_base64,
                                                       'pfx', '')
                _wait_for_completion(azure, result, wait_timeout,
                                     "add_service_certificate")

                # Create ssh config
                ssh_config = SSH()
                ssh_config.public_keys = PublicKeys()
                authorized_keys_path = u'/home/%s/.ssh/authorized_keys' % user
                ssh_config.public_keys.public_keys.append(
                    PublicKey(path=authorized_keys_path,
                              fingerprint=fingerprint))
                # Append ssh config to linux machine config
                linux_config.ssh = ssh_config
        else:
            # When using vm_image, you can't specify an OS configuration set (burned into the image)
            linux_config = None

        # Create network configuration
        network_config = ConfigurationSetInputEndpoints()
        network_config.configuration_set_type = 'NetworkConfiguration'
        network_config.subnet_names = []
        network_config.public_ips = None
        for port in endpoints:
            network_config.input_endpoints.append(
                ConfigurationSetInputEndpoint(name='TCP-%s' % port,
                                              protocol='TCP',
                                              port=port,
                                              local_port=port))

        # First determine where to store disk
        today = datetime.date.today().strftime('%Y-%m-%d')
        disk_prefix = u'%s-%s' % (name, name)
        media_link = u'http://%s.blob.core.windows.net/vhds/%s-%s.vhd' % (
            storage_account, disk_prefix, today)
        # Create system hard disk
        if image:
            os_hd = OSVirtualHardDisk(image, media_link)
        else:
            # When using vm_image_name, you can't specify an OS Virtual Hard Disk,
            # since the image already contains the VHD configuration
            os_hd = None

        # Spin up virtual machine
        try:
            result = azure.create_virtual_machine_deployment(
                service_name=name,
                deployment_name=name,
                deployment_slot='production',
                label=name,
                role_name=name,
                system_config=linux_config,
                network_config=network_config,
                os_virtual_hard_disk=os_hd,
                vm_image_name=vm_image,
                role_size=role_size,
                role_type='PersistentVMRole',
                virtual_network_name=virtual_network_name)
            _wait_for_completion(azure, result, wait_timeout,
                                 "create_virtual_machine_deployment")
        except WindowsAzureError as e:
            module.fail_json(
                msg="failed to create the new virtual machine, error was: %s" %
                str(e))

    try:
        deployment = azure.get_deployment_by_name(service_name=name,
                                                  deployment_name=name)
        return (changed, urlparse(deployment.url).hostname, deployment)
    except WindowsAzureError as e:
        module.fail_json(
            msg=
            "failed to lookup the deployment information for %s, error was: %s"
            % (name, str(e)))
Esempio n. 18
0
def create_virtual_machine(module, azure):
    """
    Create new virtual machine

    module : AnsibleModule object
    azure: authenticated azure ServiceManagementService object

    Returns:
        True if a new virtual machine and/or cloud service was created, false otherwise
    """
    name = module.params.get('name')
    os_type = module.params.get('os_type')
    hostname = module.params.get('hostname') or name + ".cloudapp.net"
    endpoints = module.params.get('endpoints').split(',')
    ssh_cert_path = module.params.get('ssh_cert_path')
    user = module.params.get('user')
    password = module.params.get('password')
    location = module.params.get('location')
    role_size = module.params.get('role_size')
    storage_account = module.params.get('storage_account')
    image = module.params.get('image')
    virtual_network_name = module.params.get('virtual_network_name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))

    changed = False

    # Check if a deployment with the same name already exists
    cloud_service_name_available = azure.check_hosted_service_name_availability(
        name)
    if cloud_service_name_available.result:
        # cloud service does not exist; create it
        try:
            result = azure.create_hosted_service(service_name=name,
                                                 label=name,
                                                 location=location)
            _wait_for_completion(azure, result, wait_timeout,
                                 "create_hosted_service")
            changed = True
        except AzureException as e:
            module.fail_json(
                msg="failed to create the new service, error was: %s" % str(e))

    try:
        # check to see if a vm with this name exists; if so, do nothing
        azure.get_role(name, name, name)
    except AzureMissingException:
        # vm does not exist; create it

        if os_type == 'linux':
            # Create linux configuration
            disable_ssh_password_authentication = not password
            vm_config = LinuxConfigurationSet(
                hostname, user, password, disable_ssh_password_authentication)
        else:
            # Create Windows Config
            vm_config = WindowsConfigurationSet(
                hostname, password, None, module.params.get('auto_updates'),
                None, user)
            vm_config.domain_join = None
            if module.params.get('enable_winrm'):
                listener = Listener('Http')
                vm_config.win_rm.listeners.listeners.append(listener)
            else:
                vm_config.win_rm = None

        # Add ssh certificates if specified
        if ssh_cert_path:
            fingerprint, pkcs12_base64 = get_ssh_certificate_tokens(
                module, ssh_cert_path)
            # Add certificate to cloud service
            result = azure.add_service_certificate(name, pkcs12_base64, 'pfx',
                                                   '')
            _wait_for_completion(azure, result, wait_timeout,
                                 "add_service_certificate")

            # Create ssh config
            ssh_config = SSH()
            ssh_config.public_keys = PublicKeys()
            authorized_keys_path = u'/home/%s/.ssh/authorized_keys' % user
            ssh_config.public_keys.public_keys.append(
                PublicKey(path=authorized_keys_path, fingerprint=fingerprint))
            # Append ssh config to linux machine config
            vm_config.ssh = ssh_config

        # Create network configuration
        network_config = ConfigurationSetInputEndpoints()
        network_config.configuration_set_type = 'NetworkConfiguration'
        network_config.subnet_names = []
        network_config.public_ips = None
        for port in endpoints:
            network_config.input_endpoints.append(
                ConfigurationSetInputEndpoint(name='TCP-%s' % port,
                                              protocol='TCP',
                                              port=port,
                                              local_port=port))

        # First determine where to store disk
        today = datetime.date.today().strftime('%Y-%m-%d')
        disk_prefix = u'%s-%s' % (name, name)
        media_link = u'http://%s.blob.core.windows.net/vhds/%s-%s.vhd' % (
            storage_account, disk_prefix, today)
        # Create system hard disk
        os_hd = OSVirtualHardDisk(image, media_link)

        # Spin up virtual machine
        try:
            result = azure.create_virtual_machine_deployment(
                service_name=name,
                deployment_name=name,
                deployment_slot='production',
                label=name,
                role_name=name,
                system_config=vm_config,
                network_config=network_config,
                os_virtual_hard_disk=os_hd,
                role_size=role_size,
                role_type='PersistentVMRole',
                virtual_network_name=virtual_network_name)
            _wait_for_completion(azure, result, wait_timeout,
                                 "create_virtual_machine_deployment")
            changed = True
        except AzureException as e:
            module.fail_json(
                msg="failed to create the new virtual machine, error was: %s" %
                str(e))

    try:
        deployment = azure.get_deployment_by_name(service_name=name,
                                                  deployment_name=name)
        return (changed, urlparse(deployment.url).hostname, deployment)
    except AzureException as e:
        module.fail_json(
            msg=
            "failed to lookup the deployment information for %s, error was: %s"
            % (name, str(e)))
Esempio n. 19
0
                    label=storage,
                    location=location,
                    account_type='Standard_LRS')
            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'