def bsk_put_file(session, datastore, local_path, bsk_path): """ Put the file data pointed to by {local_path} into {bsk_path} Return True on success Raise on error """ # put the file with `POST http://localhost:{port}/v1/stores/{store ID}/files?path={bsk_path}` # get back {'status': True} # get data from disk with open(local_path, 'r') as f: data = f.read() rpc = bsk_rpc.local_api_connect(api_session=session) assert rpc res = bsk_data.datastore_putfile(rpc, datastore, bsk_path, data, APPLICATION_KEY) if 'error' in res: raise Exception('failed to put file: {}'.format(res)) return True
def bsk_listdir(session, datastore, bsk_path, file=sys.stdout): """ List a directory at {bsk_path} in the given data store. Write the listing to stdout. Return True on success Raise on error """ # list the directory with `GET http://localhost:{port}/v1/stores/{store ID}/directories?path={bsk_path}` # get back the structured inode of the directory. # iterate through the inode's children and print their names and types (i.e. append '/' to directories) rpc = bsk_rpc.local_api_connect(api_session=session) assert rpc res = bsk_data.datastore_listdir(rpc, datastore, bsk_path) if 'error' in res: raise Exception("Failed to list directory: {}".format(res)) dir_listing = res['dir']['idata'] # extract names and types names = [] for name in dir_listing.keys(): dirent = dir_listing[name] if dirent['type'] == 2: # this is a file name += '/' names.append(name) names.sort() print >> file, '\n'.join(names) return True
def issue_zonefile(domain_fqa, user_data_txt): from ysi_client.rpc import local_api_connect rpc = local_api_connect() assert rpc try: resp = rpc.backend_update(domain_fqa, user_data_txt, None, None) except Exception as e: log.exception(e) return {'error': 'Exception submitting zonefile for update'} return resp
def bsk_make_datastore(session, drivers): """ Make our data store. The data store will be specific to this application's name (APPLICATION_NAME). This method is idempotent. If the data store already exists, then no action will be taken (except to query the ID) Return the datastore ID on success (even if it already exists). Raise on error """ # create a datastore with `POST http://localhost:{port}/v1/stores?drivers={drivers}` # get back {'status': True, 'datastore_id': datastore ID} global APPLICATION_KEY datastore_pk = APPLICATION_KEY datastore_pubkey = bsk.get_pubkey_hex(datastore_pk) datastore_id = bsk_data.datastore_get_id(datastore_pubkey) rpc = bsk_rpc.local_api_connect(api_session=session) assert rpc # get back datastore? ds_res = rpc.backend_datastore_get(datastore_id) if 'error' not in ds_res: return { 'status': True, 'datastore': ds_res['datastore'], 'datastore_id': datastore_id } # make one! ds_info = bsk_data.make_datastore_info('datastore', datastore_pubkey, driver_names=drivers) if 'error' in ds_info: raise Exception("make_datastore_info: {}".format(ds_info)) res = bsk_data.put_datastore(rpc, ds_info, datastore_pk) if 'error' in res: raise Exception('put_datastore_info: {}'.format(res)) # get back datastore ds_res = rpc.backend_datastore_get(datastore_id) if 'error' in ds_res: raise Exception('get_datastore: {}'.format(ds_res)) return { 'status': True, 'datastore': ds_res['datastore'], 'datastore_id': datastore_id }
def bsk_stat(session, datastore, bsk_path, port=6270): """ Stat a file or directory Return the inode structure on success Raise on error """ # send `GET http://localhost:{port}/v1/stores/{store ID}/inodes?path={bsk_path` rpc = bsk_rpc.local_api_connect(api_session=session) assert rpc res = bsk_data.datastore_stat(rpc, datastore, bsk_path) if 'error' in res: raise Exception("Failed to stat: {}".format(res)) return res['inode_info']['inode']
def bsk_rmdir(session, datastore, bsk_path): """ Remove a directory at {bsk_path} in the given data store. Return True on success Raise on error """ # delete the directory with `DELETE http://localhost:{port}/v1/stores/{store ID}/directories?path={bsk_path}` rpc = bsk_rpc.local_api_connect(api_session=session) assert rpc res = bsk_data.datastore_rmdir(rpc, datastore, bsk_path, APPLICATION_KEY) if 'error' in res: raise Exception("Failed to remove directory: {}".format(res)) return True
def bsk_mkdir(session, datastore, bsk_path): """ Make a directory at {bsk_path} in the given data store Return True on success Raise on error """ # make the directory with `POST http://localhost:{port}/v1/stores/{store ID}/directories?path={bsk_path}` # get back {'status': True} (http 200) rpc = bsk_rpc.local_api_connect(api_session=session) assert rpc res = bsk_data.datastore_mkdir(rpc, datastore, bsk_path, APPLICATION_KEY) if 'error' in res: raise Exception("failed to make directory: {}".format(res)) return True
def bsk_delete_datastore(session): """ Delete our data store. Return True on success Raise on error """ # delete datastore with `DELETE http://localhost:{port}/v1/stores` rpc = bsk_rpc.local_api_connect(api_session=session) assert rpc # clear datastore res = bsk_data.delete_datastore(rpc, datastore, APPLICATION_KEY) if 'error' in res: raise Exception('failed to delete datastore: {}'.format(res)) return True
def bsk_delete_file(session, datastore, bsk_path, port=6270): """ Delete the file data pointed to by {bsk_path} from the data store. Return True on success Raise on error """ # delete with `DELETE http://localhost:{port}/v1/stores/{store ID}/files?path={bsk_path)` # get back {'status': True} rpc = bsk_rpc.local_api_connect(api_session=session) assert rpc res = bsk_data.datastore_deletefile(rpc, datastore, bsk_path, APPLICATION_KEY) if 'error' in res: raise Exception("failed to delete file: {}".format(res)) return True
def bsk_get_file(session, datastore, bsk_path, file=sys.stdout): """ Get the file data pointed to by {bsk_path} from the data store. Write it to stdout by default (override with file=) Return True on success Raise on error """ # get the file with `GET http://localhost:{port}/v1/stores/{store ID}/files?path={bsk_path}` # get back raw data (application/octet-stream) rpc = bsk_rpc.local_api_connect(api_session=session) assert rpc res = bsk_data.datastore_getfile(rpc, datastore, bsk_path) if 'error' in res: raise Exception('failed to get file: {}'.format(res)) print >> file, res['file']['idata'] return True