def handle_api_set(self, http_context, id=None): """ Save the data to config file using augeas endpoint. Method POST. :param http_context: HttpContext :type http_context: HttpContext :param id: Augeas endpoint id, e.g. hosts :type id: string """ data = json.loads(http_context.body.decode()) ep = self.__get_augeas_endpoint(id) if not ep: raise EndpointReturn(404) aug = ep.get_augeas() aug.load() def __apply_tree(e): aug.set(e['path'], e['value']) for sp in aug.match(e['path'] + '/*'): aug.remove(sp) for child in e['children']: __apply_tree(child) __apply_tree(data) aug.save()
def handle_api_get(self, http_context, id=None): """ Get file content and informations through augeas endpoint. :param http_context: HttpContext :type http_context: HttpContext :param id: Id of augeas endpoint, e.g. hosts :type id: string :return: Data and file informations :rtype: dict """ ep = self.__get_augeas_endpoint(id) if not ep: raise EndpointReturn(404) aug = ep.get_augeas() aug.load() root_path = ep.get_root_path() data = {} def __wrap_tree(path): r = { 'name': path.split('/')[-1].split('[')[0], 'path': path, 'value': aug.get(path), 'children': [] } for sp in aug.match(path + '/*'): r['children'].append(__wrap_tree(sp)) return r data = __wrap_tree(root_path) return data
def handle_api_fs_read(self, http_context, path=None): if not os.path.exists(path): raise EndpointReturn(404) try: return open(path).read() except OSError as e: raise EndpointError(e)
def handle_api_users(self, http_context): """ Load (method get) and save (method post) the ajenti users config file. Method GET. Method POST. :param http_context: HttpContext :type http_context: HttpContext :return: Content of the ajenti users config file :rtype: dict """ if os.getuid() != 0: raise EndpointReturn(403) if http_context.method == 'GET': with authorize('core:config:read'): self.context.worker.reload_master_config() return aj.users.data if http_context.method == 'POST': with authorize('core:config:write'): data = json.loads(http_context.body.decode()) aj.users.data.update(data) aj.users.save() self.context.worker.reload_master_config() return aj.users.data
def handle_api_fs_chmod(self, http_context, path=None): if not os.path.exists(path): raise EndpointReturn(404) data = json.loads(http_context.body) try: os.chmod(path, data['mode']) except OSError as e: raise EndpointError(e)
def handle_api_config(self, http_context): if self.context.identity != 'root': raise EndpointReturn(403) if http_context.method == 'GET': self.context.worker.reload_master_config() return aj.config.data if http_context.method == 'POST': data = json.loads(http_context.body) aj.config.data.update(data) aj.config.save() return aj.config.data
def handle_api_fs_list(self, http_context, path=None): """ Return a list of objects (files, directories, ...) in a specific directory, and their informations. :param http_context: HttpContext :type http_context: HttpContext :param path: Directory path :type path: string :return: All items with informations :rtype: dict """ if not os.path.exists(path): raise EndpointReturn(404) try: items = [] for name in os.listdir(path): item_path = os.path.join(path, name) data = { 'name': name, 'path': item_path, 'isDir': os.path.isdir(item_path), 'isFile': os.path.isfile(item_path), 'isLink': os.path.islink(item_path), } try: stat = os.stat(item_path) data.update({ 'mode': stat.st_mode, 'mtime': stat.st_mtime, 'uid': stat.st_uid, 'gid': stat.st_gid, 'size': stat.st_size, }) except OSError as e: data['accessError'] = str(e) if e.errno == errno.ENOENT and os.path.islink(item_path): data['brokenLink'] = True items.append(data) return { 'parent': os.path.dirname(os.path.normpath(path)) if path != '/' else None, 'items': items, } except OSError as e: raise EndpointError(e)
def handle_api_config(self, http_context): if os.getuid() != 0: raise EndpointReturn(403) if http_context.method == 'GET': with authorize('core:config:read'): self.context.worker.reload_master_config() return aj.config.data if http_context.method == 'POST': with authorize('core:config:write'): data = json.loads(http_context.body.decode()) aj.config.data.update(data) aj.config.save() self.context.worker.reload_master_config() return aj.config.data
def handle_api_fs_stat(self, http_context, path=None): """ Get all informations from a specific path. :param http_context: HttpContext :type http_context: HttpContext :param path: Path of file/directory :type path: string :return: POSIX permissions, size, type, ... :rtype: dict """ if not os.path.exists(path): raise EndpointReturn(404) data = { 'name': os.path.split(path)[1], 'path': path, 'isDir': os.path.isdir(path), 'isFile': os.path.isfile(path), 'isLink': os.path.islink(path), 'readAccess': os.access(path, os.R_OK), 'writeAccess': os.access(path, os.W_OK), 'executeAccess': os.access(path, os.X_OK), } try: stat = os.stat(path) data.update({ 'mode': stat.st_mode, 'mtime': stat.st_mtime, 'uid': stat.st_uid, 'gid': stat.st_gid, 'size': stat.st_size, }) try: data['user'] = pwd.getpwuid(stat.st_uid).pw_name except KeyError: pass try: data['group'] = grp.getgrgid(stat.st_gid).gr_name except KeyError: pass except OSError as e: data['accessError'] = str(e) if e.errno == errno.ENOENT and os.path.islink(path): data['brokenLink'] = True return data
def handle_api_get_smtp_config(self, http_context): """ Load the smtp config file without password. :param http_context: HttpContext :type http_context: HttpContext :return: Content of the ajenti config file without password :rtype: dict """ if os.getuid() != 0: raise EndpointReturn(403) with authorize('core:config:read'): return aj.smtp_config.data
def handle_api_post_smtp_config(self, http_context): """ Save the smtp config file without password. :param http_context: HttpContext :type http_context: HttpContext :return: Content of the ajenti config file without password :rtype: dict """ if os.getuid() != 0: raise EndpointReturn(403) with authorize('core:config:write'): data = json.loads(http_context.body.decode()) aj.smtp_config.save(data)
def handle_api_get_config(self, http_context): """ Load the ajenti config file. :param http_context: HttpContext :type http_context: HttpContext :return: Content of the ajenti config file :rtype: dict """ if os.getuid() != 0: raise EndpointReturn(403) with authorize('core:config:read'): self.context.worker.reload_master_config() return aj.config.data
def handle_api_fs_chmod(self, http_context, path=None): """ Change mode for a specific file. :param http_context: HttpContext :type http_context: HttpContext :param path: Path of file :type path: string """ if not os.path.exists(path): raise EndpointReturn(404) data = json.loads(http_context.body.decode()) try: os.chmod(path, data['mode']) except OSError as e: raise EndpointError(e)
def handle_api_calculate(self, http_context, operation=None, a=None, b=None): start_time = time.time() try: if operation == 'add': result = int(a) + int(b) elif operation == 'divide': result = int(a) / int(b) else: raise EndpointReturn(404) except ZeroDivisionError: raise EndpointError('Division by zero') return {'value': result, 'time': time.time() - start_time}
def handle_api_post_config(self, http_context): """ Save the ajenti config file. :param http_context: HttpContext :type http_context: HttpContext :return: Content of the ajenti config file :rtype: dict """ if os.getuid() != 0: raise EndpointReturn(403) with authorize('core:config:write'): data = json.loads(http_context.body.decode()) aj.config.data.update(data) aj.config.save() self.context.worker.reload_master_config() return aj.config.data
def handle_api_set(self, http_context, id=None): data = json.loads(http_context.body.decode()) ep = self.__get_augeas_endpoint(id) if not ep: raise EndpointReturn(404) aug = ep.get_augeas() aug.load() def __apply_tree(e): aug.set(e['path'], e['value']) for sp in aug.match(e['path'] + '/*'): aug.remove(sp) for child in e['children']: __apply_tree(child) __apply_tree(data) aug.save()
def handle_api_fs_list(self, http_context, path=None): if not os.path.exists(path): raise EndpointReturn(404) try: items = [] for name in os.listdir(path): item_path = os.path.join(path, name) data = { 'name': name, 'path': item_path, 'isDir': os.path.isdir(item_path), 'isFile': os.path.isfile(item_path), 'isLink': os.path.islink(item_path), } try: stat = os.stat(item_path) data.update({ 'mode': stat.st_mode, 'mtime': stat.st_mtime, 'uid': stat.st_uid, 'gid': stat.st_gid, 'size': stat.st_size, }) except OSError as e: data['accessError'] = str(e) if e.errno == errno.ENOENT and os.path.islink(item_path): data['brokenLink'] = True items.append(data) return { 'parent': os.path.dirname(os.path.normpath(path)) if path != '/' else None, 'items': items, } except OSError as e: raise EndpointError(e)
def handle_api_fs_stat(self, http_context, path=None): if not os.path.exists(path): raise EndpointReturn(404) data = { 'name': os.path.split(path)[1], 'path': path, 'isDir': os.path.isdir(path), 'isFile': os.path.isfile(path), 'isLink': os.path.islink(path), 'readAccess': os.access(path, os.R_OK), 'writeAccess': os.access(path, os.W_OK), 'executeAccess': os.access(path, os.X_OK), } try: stat = os.stat(path) data.update({ 'mode': stat.st_mode, 'mtime': stat.st_mtime, 'uid': stat.st_uid, 'gid': stat.st_gid, 'size': stat.st_size, }) try: data['user'] = pwd.getpwuid(stat.st_uid).pw_name except KeyError: pass try: data['group'] = grp.getgrgid(stat.st_gid).gr_name except KeyError: pass except OSError as e: data['accessError'] = str(e) if e.errno == errno.ENOENT and os.path.islink(path): data['brokenLink'] = True return data
def handle_api_get(self, http_context, id=None): ep = self.__get_augeas_endpoint(id) if not ep: raise EndpointReturn(404) aug = ep.get_augeas() aug.load() root_path = ep.get_root_path() data = {} def __wrap_tree(path): r = { 'name': path.split('/')[-1].split('[')[0], 'path': path, 'value': aug.get(path), 'children': [] } for sp in aug.match(path + '/*'): r['children'].append(__wrap_tree(sp)) return r data = __wrap_tree(root_path) return data