def run_script(self, script, dry=False, return_output=False, verbose=False): if return_output: raise ValueError("Cannot return output for GCP scripts.") # Upload script to GCS cmd_split = shlex.split(script) script_fname = cmd_split[0] if len(cmd_split) > 1: script_args = ' '.join(cmd_split[1:]) else: script_args = '' remote_script = gcp_util.upload_file_to_gcp_storage(self.gcp_bucket, script_fname, dry=dry) exp_name = "{}-{}".format(self.gcp_label, gcp_util.make_timekey()) exp_prefix = self.gcp_label with open(gcp_util.GCP_STARTUP_SCRIPT_PATH) as f: start_script = f.read() with open(gcp_util.GCP_SHUTDOWN_SCRIPT_PATH) as f: stop_script = f.read() metadata = { 'shell_interpreter': self.shell_interpreter, 'gcp_bucket_path': self.gcp_log_path, 'remote_script_path': remote_script, 'bucket_name': self.gcp_bucket, 'terminate': json.dumps(self.terminate_on_end), 'use_gpu': self.use_gpu, 'script_args': script_args, 'startup-script': start_script, 'shutdown-script': stop_script, 'data_sync_interval': self.data_sync_interval } # instance name must match regex '(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)'"> unique_name = "doodad" + str(uuid.uuid4()).replace("-", "") instance_info = self.create_instance(metadata, unique_name, exp_name, exp_prefix, dry=dry) if verbose: print('Launched instance %s' % unique_name) print(instance_info) return metadata
def run_script(self, script, dry=False, return_output=False, verbose=True, wait=False): if return_output: raise NotImplementedError() print("Creating network") t1 = time.time() nic_id = self.create_network() print("Created network", time.time() - t1) # Upload script to GCS cmd_split = shlex.split(script) script_fname = cmd_split[0] if len(cmd_split) > 1: script_args = ' '.join(cmd_split[1:]) else: script_args = '' # remote_script = azure_util.upload_file_to_azure_storage( # credentials=self.credentials, # #subscription_id=self.subscription_id, # group_name=self.azure_group_name, # #storage_name=self.azure_storage, # location=self.location, # file_name=script_fname, # dry=dry) remote_script_path = gcp_util.upload_file_to_gcp_storage( self.gcp_bucket_name, script_fname, dry=dry) exp_name = "{}-{}".format(self.azure_label, azure_util.make_timekey()) exp_prefix = self.azure_label with open(azure_util.AZURE_STARTUP_SCRIPT_PATH) as f: start_script = self.process_script( f.read(), script_args, remote_script_path=remote_script_path) with open(azure_util.AZURE_SHUTDOWN_SCRIPT_PATH) as f: stop_script = self.process_script(f.read()) print("Creating msi") t1 = time.time() msi_identity, msi_accounts = self.create_msi() print("Created msi", time.time() - t1) vm_parameters = { 'location': self.location, 'os_profile': { 'computer_name': self.unique_name, 'admin_username': self.username, 'admin_password': self.password, 'custom_data': start_script, }, 'hardware_profile': { #'vm_size': 'Standard_DS1' 'vm_size': self.instance_type }, 'storage_profile': { 'image_reference': { 'publisher': self.vm_reference['linux']['publisher'], 'offer': self.vm_reference['linux']['offer'], 'sku': self.vm_reference['linux']['sku'], 'version': self.vm_reference['linux']['version'] } }, 'network_profile': { 'network_interfaces': [{ 'id': nic_id, }] }, 'identity': msi_identity } if self.use_spot: from azure.mgmt.compute.models import VirtualMachinePriorityTypes, VirtualMachineEvictionPolicyTypes, BillingProfile vm_parameters[ 'priority'] = VirtualMachinePriorityTypes.spot, # use Azure spot intance vm_parameters[ 'eviction_policy'] = VirtualMachineEvictionPolicyTypes.deallocate, #For Azure Spot virtual machines, the only supported value is 'Deallocate' vm_parameters['billing_profile'] = BillingProfile( max_price=float(2)) print("Creating vm", self.unique_name) t1 = time.time() vm_result = self.create_instance(vm_parameters, self.unique_name, exp_name, exp_prefix, dry=dry, wait=wait) print("Created vm", time.time() - t1) print('Launched instance %s' % self.unique_name) if azure_util.SYSTEM_ASSIGNED_IDENTITY: msi_accounts.append(vm_result.identity.principal_id) self.assign_msi(msi_accounts) # if self.auto_shutdown: # import datetime # from datetime import timezone # from azure.cli.core import get_default_cli # dt = datetime.datetime.now() # utc_time = dt.replace(tzinfo = timezone.utc) # utc_time += datetime.timedelta(minutes=self.auto_shutdown) # time_close = utc_time.strftime('%H%M') # cmd = f"az vm auto-shutdown -g {self.azure_group_name} -n {self.unique_name} --time {time_close}" # print(f"Auto shutdown: {cmd}") # get_default_cli()(cmd.split()) return vm_result