Exemplo n.º 1
0
    def handle_api_time_sync(self, http_context):
        if subprocess.call(['which', 'ntpdate']) != 0:
            raise EndpointError(_('ntpdate utility is not installed'))

        try:
            subprocess.check_call(['ntpdate', '-u', '0.pool.ntp.org'])
        except Exception as e:
            raise EndpointError(e)
        return int(time.time() - time.timezone)
Exemplo n.º 2
0
 def handle_api_repo_list(self, http_context):
     if os.path.exists('/root/.cache/pip'):
         shutil.rmtree('/root/.cache/pip')
     try:
         return requests.get('http://webui.org/plugins/list').json()
     except Exception as e:
         raise EndpointError(e)
Exemplo n.º 3
0
 def handle_api_operate(self, http_context, manager_id=None, operation=None, service_id=None):
     if operation not in ['start', 'stop', 'restart']:
         return
     try:
         getattr(self.managers[manager_id], operation)(service_id)
     except ServiceOperationError as e:
         raise EndpointError(e)
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
 def handle_api_core_check_upgrade(self, http_context):
     url = 'https://pypi.python.org/pypi/%s/json' % 'webui-panel'
     try:
         data = requests.get(url).json()
     except Exception as e:
         raise EndpointError(e)
     version = data['info']['version']
     if version != wu.version:
         return version
     return None
Exemplo n.º 6
0
 def handle_script(self, http_context):
     data = http_context.json_body()
     try:
         p = subprocess.Popen(
             ['bash', '-c', data['script']],
             stdout=subprocess.PIPE,
             stderr=subprocess.PIPE,
             close_fds=True
         )
         o, e = p.communicate(data.get('input', None))
     except subprocess.CalledProcessError as e:
         raise EndpointError(e)
     except OSError as e:
         raise EndpointError(e)
     return {
         'code': p.returncode,
         'output': o,
         'stderr': e,
     }
Exemplo n.º 7
0
 def handle_api_fs_write(self, http_context, path=None):
     try:
         content = http_context.body
         if http_context.query:
             encoding = http_context.query.get('encoding', None)
             if encoding:
                 content = content.decode('utf-8').encode(encoding)
         with open(path, 'w') as f:
             f.write(content)
     except OSError as e:
         raise EndpointError(e)
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
 def handle_api_pypi_uninstall(self, http_context, name=None):
     try:
         subprocess.check_output(['pip', 'uninstall', '-y', 'webui.plugin.%s' % name])
     except subprocess.CalledProcessError as e:
         raise EndpointError(e.output)
Exemplo n.º 10
0
 def handle_api_pypi_install(self, http_context, name=None, version=None):
     # TODO replaced with a task
     try:
         subprocess.call(['pip', 'install', 'webui.plugin.%s==%s' % (name, version)])
     except subprocess.CalledProcessError as e:
         raise EndpointError(e.output)
Exemplo n.º 11
0
 def handle_api_fs_create_directory(self, http_context, path=None):
     try:
         os.makedirs(path)
     except OSError as e:
         raise EndpointError(e)
Exemplo n.º 12
0
 def handle_api_fs_create_file(self, http_context, path=None):
     try:
         os.mknod(path, int('644', 8))
     except OSError as e:
         raise EndpointError(e)