def upload(self, image, name=None, max_chunk_size=None, max_attempts=5): if not os.path.exists(image): raise AzureStorageFileNotFound('File %s not found' % image) blob_service = PageBlobService( self.account_name, self.account_key, endpoint_suffix=self.blob_service_host_base) image_type = FileType(image) blob_name = (name or image_type.basename()) if not name: log.info('blob-name: %s', blob_name) image_size = self.__upload_byte_size(image, image_type) try: stream = self.__open_upload_stream(image, image_type) except Exception as e: raise AzureStorageStreamError('%s: %s' % (type(e).__name__, format(e))) try: page_blob = PageBlob(blob_service, blob_name, self.container, image_size) self.__upload_status(0, image_size) while True: bytes_transfered = page_blob.next(stream, max_chunk_size, max_attempts) self.__upload_status(bytes_transfered, image_size) except StopIteration: stream.close() self.__upload_status(image_size, image_size) except Exception as e: stream.close() raise AzureStorageUploadError('%s: %s' % (type(e).__name__, format(e)))
def __update(self): log.info( 'Updating image metadata for: %s', self.command_args['--name'] ) update_elements = { 'description': self.command_args['--description'], 'eula': self.command_args['--eula'], 'image_family': self.command_args['--image-family'], 'icon_uri': self.command_args['--icon-uri'], 'label': self.command_args['--label'], 'language': self.command_args['--language'], 'privacy_uri': self.command_args['--privacy-uri'], 'published_date': self.command_args['--published-date'], 'small_icon_uri': self.command_args['--small-icon-uri'], 'recommended_vm_size': self.command_args['--recommended-vm-size'], 'show_in_gui': self.command_args['--show-in-gui'], } for name, value in update_elements.items(): if value is not None: log.info('--> %s = %s', name, value) self.image.update( self.command_args['--name'], update_elements )
def __replicate(self): image_name = self.command_args['--name'] request_id = self.image.replicate( image_name, self.command_args['--regions'].split(','), self.command_args['--offer'], self.command_args['--sku'], self.command_args['--image-version'] ) self.result.add( 'replicate:' + self.command_args['--name'] + ':' + self.command_args['--regions'], request_id ) if not self.command_args['--quiet']: self.out.display() if self.command_args['--wait']: if not self.command_args['--quiet']: progress = BackgroundScheduler(timezone=utc) progress.add_job( lambda: self.image.print_replication_status(image_name), 'interval', minutes=5 ) progress.start() try: self.image.wait_for_replication_completion(image_name) self.image.print_replication_status(image_name) except KeyboardInterrupt: raise SystemExit('azurectl aborted by keyboard interrupt') finally: progress.shutdown() if not self.command_args['--quiet']: print() log.info('Replicated %s', image_name)
def set_default_region(self, section_name): section_name = 'region:' + section_name if section_name not in self.config.sections(): log.info('Section %s does not exist', section_name) return False self.config.defaults()['default_region'] = section_name return True
def __set_region_default(self): if self.setup.set_default_region(self.command_args['--region']): self.setup.write() log.info( 'Region %s is now set as default', self.command_args['--region'], )
def __region_add(self): self.setup.add_region( self.command_args['--region'], self.command_args['--storage-account-name'], self.command_args['--container-name'] ) self.setup.write() log.info('Added region %s', self.command_args['--region'])
def __default(self): Config.set_default_config_file( account_name=self.command_args['--name'] ) log.info( 'Account %s has been set as default configuration', self.command_args['--name'] )
def __upload_with_progress(self): image = self.command_args['--source'] progress = BackgroundScheduler(timezone=utc) progress.add_job(self.storage.print_upload_status, 'interval', seconds=3) progress.start() try: self.__process_upload() self.storage.print_upload_status() progress.shutdown() except (KeyboardInterrupt): progress.shutdown() raise SystemExit('azurectl aborted by keyboard interrupt') print() log.info('Uploaded %s', image)
def remove_region(self, name): """ remove specified region section """ name = 'region:' + name if name == self.__get_default_region(): log.info('Section %s is the default region section', name) log.info('Please setup a new default prior to removing') return False if not self.config.remove_section(name): log.info('Section %s does not exist', name) return False return True
def __remove(self): self.setup.remove() log.info('Removed account config file %s', self.setup.filename)
def __share_delete(self): share_name = self.command_args['--name'] self.fileshare.delete(share_name) log.info('Deleted Files Share %s', share_name)
def __container_delete(self): container_name = self.command_args['--name'] log.info('Request to delete container %s', container_name) self.container.delete(container_name) log.info('Deleted %s container', container_name)
def __container_create(self): container_name = self.command_args['--name'] log.info('Request to create container %s', container_name) self.container.create(container_name) log.info('Created %s container', container_name)
def __delete(self): self.data_disk.delete(self.command_args['--disk-name']) log.info('Deleted data disk %s', self.command_args['--disk-name'])
def __create(self): self.data_disk.create(self.command_args['--disk-basename'], self.command_args['--size'], self.command_args['--label']) log.info('Created new data disk %s', self.data_disk.data_disk_name.replace('.vhd', ''))
def __delete(self): image = self.command_args['--blob-name'] self.storage.delete(image) log.info('Deleted %s', image)
def __share_create(self): share_name = self.command_args['--name'] self.fileshare.create(share_name) log.info('Created Files Share %s', share_name)
def __configure_account(self): self.__check_account_existing_in_default_config() self.setup.configure_account( self.command_args['--name'], self.command_args['--publish-settings-file'], self.command_args['--region'], self.command_args['--storage-account-name'], self.command_args['--container-name'], self.command_args['--subscription-id'], self.command_args['--management-pem-file'], self.command_args['--management-url'] ) self.setup.write() log.info( 'Added account %s', self.command_args['--name'] ) if self.command_args['--create']: self.global_args['--account'] = self.command_args['--name'] self.load_config() self.account = AzureAccount(self.config) self.__load_account_setup( self.command_args['--name'] ) try: storage_account_name = \ self.command_args['--storage-account-name'] storage_account = StorageAccount(self.account) if not storage_account.exists(storage_account_name): storage_account_request_id = storage_account.create( name=storage_account_name, description=self.command_args['--name'], label=self.command_args['--storage-account-name'], account_type=Defaults.account_type_for_docopts( self.command_args ) ) self.request_wait(storage_account_request_id) log.info( 'Created %s storage account', storage_account_name ) else: log.info( 'Storage account %s already exists', storage_account_name ) container_name = self.command_args['--container-name'] container = Container(self.account) if not container.exists(container_name): container.create(container_name) log.info( 'Created %s container', container_name ) else: log.info( 'Container %s already exists', container_name ) except Exception as e: self.__remove() raise AzureAccountConfigurationError( '%s: %s' % (type(e).__name__, format(e)) )