Beispiel #1
0
    def _create_repository(self):
        if not self._create_repo:
            return True

        logging.debug('Creating repository operation...')

        if self._file:
            _packageset = self._file
        else:
            _packageset = self._server_directory

        _ret = self._url_request.run(
            'create_repositories_of_packageset',
            data={
                'project': self.packager_project,
                'version': self.packager_project,  # backwards compatibility
                'packageset': _packageset
            }
        )

        logging.debug('Creating repository response: %s', _ret)
        if self._debug:
            print('Response: %s' % _ret)

        if _ret['errmfs']['code'] != server_errors.ALL_OK:
            _error_info = server_errors.error_info(_ret['errmfs']['code'])
            print(_error_info)
            logging.error('Creating repository error: %s', _error_info)
            sys.exit(errno.EINPROGRESS)

        return True
Beispiel #2
0
    def _upload_file(self):
        logging.debug('Upload file operation...')
        if not os.path.isfile(self._file):
            print(_('File not found'))
            logging.error('File not found %s', self._file)
            sys.exit(errno.ENOENT)

        self._check_sign_keys()

        logging.debug('Uploading file: %s', self._file)
        _ret = self._url_request.run(
            'upload_server_package',
            data={
                'project': self.packager_project,
                'version': self.packager_project,  # backwards compatibility
                'store': self.packager_store,
                'source': self._is_regular_file
            },
            upload_file=os.path.abspath(self._file)
        )

        logging.debug('Uploading response: %s', _ret)
        if self._debug:
            print('Response: %s' % _ret)

        if _ret['errmfs']['code'] != server_errors.ALL_OK:
            _error_info = server_errors.error_info(_ret['errmfs']['code'])
            print(_error_info)
            logging.error('Uploading file error: %s', _error_info)
            sys.exit(errno.EINPROGRESS)

        return self._create_repository()
Beispiel #3
0
    def _create_repository(self):
        if not self._create_repo:
            return True

        logging.debug('Creating repository operation...')

        if self._file:
            _packageset = self._file
        else:
            _packageset = self._server_directory

        _ret = self._url_request.run(
            'create_repositories_of_packageset',
            data={
                'project': self.packager_project,
                'version': self.packager_project,  # backwards compatibility
                'packageset': _packageset
            }
        )

        logging.debug('Creating repository response: %s', _ret)
        if self._debug:
            print('Response: %s' % _ret)

        if _ret['errmfs']['code'] != server_errors.ALL_OK:
            _error_info = server_errors.error_info(_ret['errmfs']['code'])
            print(_error_info)
            logging.error('Creating repository error: %s', _error_info)
            sys.exit(errno.EINPROGRESS)

        return True
Beispiel #4
0
    def _upload_file(self):
        logging.debug('Upload file operation...')
        if not os.path.isfile(self._file):
            print(_('File not found'))
            logging.error('File not found %s', self._file)
            sys.exit(errno.ENOENT)

        self._check_sign_keys()

        logging.debug('Uploading file: %s', self._file)
        _ret = self._url_request.run(
            'upload_server_package',
            data={
                'project': self.packager_project,
                'version': self.packager_project,  # backwards compatibility
                'store': self.packager_store,
                'source': self._is_regular_file
            },
            upload_file=os.path.abspath(self._file)
        )

        logging.debug('Uploading response: %s', _ret)
        if self._debug:
            print('Response: %s' % _ret)

        if _ret['errmfs']['code'] != server_errors.ALL_OK:
            _error_info = server_errors.error_info(_ret['errmfs']['code'])
            print(_error_info)
            logging.error('Uploading file error: %s', _error_info)
            sys.exit(errno.EINPROGRESS)

        return self._create_repository()
Beispiel #5
0
    def _upload_set(self):
        logging.debug('Upload set operation...')
        if not os.path.isdir(self._directory):
            print(gettext.gettext('Directory not found'))
            logging.error('Directory not found %s', self._directory)
            sys.exit(errno.ENOENT)

        self._check_sign_keys()

        for _root, _, _files in os.walk(self._directory):
            for _file in _files:
                _filename = os.path.join(_root, _file)

                if os.path.isfile(_filename):
                    logging.debug('Uploading server set: %s', _filename)
                    if self._debug:
                        print('Uploading file: %s' % os.path.abspath(_filename))

                    _ret = self._url_request.run(
                        'upload_server_set',
                        data={
                            'project': self.packager_project,
                            'version': self.packager_project,  # backwards compatibility
                            'store': self.packager_store,
                            'packageset': self._server_directory,
                            'path': os.path.dirname(
                                os.path.join(
                                    _root,
                                    _file
                                )[len(self._directory) + 1:]
                            )
                        },
                        upload_file=os.path.abspath(_filename)
                    )

                    logging.debug('Uploading set response: %s', _ret)
                    if self._debug:
                        print('Response: %s' % _ret)

                    if _ret['errmfs']['code'] != server_errors.ALL_OK:
                        _error_info = server_errors.error_info(
                            _ret['errmfs']['code']
                        )
                        print(_error_info)
                        logging.error('Uploading set error: %s', _error_info)
                        sys.exit(errno.EINPROGRESS)

        return self._create_repository()
Beispiel #6
0
    def _upload_set(self):
        logging.debug('Upload set operation...')
        if not os.path.isdir(self._directory):
            print(gettext.gettext('Directory not found'))
            logging.error('Directory not found %s', self._directory)
            sys.exit(errno.ENOENT)

        self._check_sign_keys()

        for _root, _, _files in os.walk(self._directory):
            for _file in _files:
                _filename = os.path.join(_root, _file)

                if os.path.isfile(_filename):
                    logging.debug('Uploading server set: %s', _filename)
                    if self._debug:
                        print('Uploading file: %s' % os.path.abspath(_filename))

                    _ret = self._url_request.run(
                        'upload_server_set',
                        data={
                            'project': self.packager_project,
                            'version': self.packager_project,  # backwards compatibility
                            'store': self.packager_store,
                            'packageset': self._server_directory,
                            'path': os.path.dirname(
                                os.path.join(
                                    _root,
                                    _file
                                )[len(self._directory) + 1:]
                            )
                        },
                        upload_file=os.path.abspath(_filename)
                    )

                    logging.debug('Uploading set response: %s', _ret)
                    if self._debug:
                        print('Response: %s' % _ret)

                    if _ret['errmfs']['code'] != server_errors.ALL_OK:
                        _error_info = server_errors.error_info(
                            _ret['errmfs']['code']
                        )
                        print(_error_info)
                        logging.error('Uploading set error: %s', _error_info)
                        sys.exit(errno.EINPROGRESS)

        return self._create_repository()
Beispiel #7
0
    def _send_message(self, msg='', icon=None, mandatory=False):
        if msg:
            print('')
            printcolor.info(str(' ' + msg + ' ').center(76, '*'))

            if mandatory or self.migas_gui_verbose:
                if not icon:
                    icon = os.path.join(settings.ICON_PATH, self.ICON)

                if self._notify:
                    icon = 'file://%s' % os.path.join(settings.ICON_PATH, icon)

                    try:
                        self._notify.update(self.APP_NAME, msg, icon)
                        #self._notify.set_timeout(pynotify.EXPIRES_DEFAULT)
                        self._notify.show()
                    except:
                        pass

        _ret = self._url_request.run(
            'upload_computer_message',
            data=msg,
            exit_on_error=False
        )
        logging.debug('Message response: %s', _ret)
        if self._debug:
            print(('Message response: %s' % _ret))

        if 'errmfs' in _ret \
        and _ret['errmfs']['code'] == server_errors.COMPUTER_NOT_FOUND:
            return self._auto_register()

        if _ret['errmfs']['code'] != server_errors.ALL_OK:
            _msg = 'Error: %s\nInfo: %s' % (
                server_errors.error_info(_ret['errmfs']['code']),
                _ret['errmfs']['info']
            )
            _operation_failed(_msg)
            self._write_error(_msg, append=True)

        return (_ret['errmfs']['code'] == server_errors.ALL_OK)
Beispiel #8
0
    def _send_message(self, msg='', icon=None, mandatory=False):
        if msg:
            print('')
            printcolor.info(str(' ' + msg + ' ').center(76, '*'))

            if mandatory or self.migas_gui_verbose:
                if not icon:
                    icon = os.path.join(settings.ICON_PATH, self.ICON)

                if self._notify:
                    icon = 'file://%s' % os.path.join(settings.ICON_PATH, icon)

                    try:
                        self._notify.update(self.APP_NAME, msg, icon)
                        #self._notify.set_timeout(pynotify.EXPIRES_DEFAULT)
                        self._notify.show()
                    except:
                        pass

        _ret = self._url_request.run('upload_computer_message',
                                     data=msg,
                                     exit_on_error=False)
        logging.debug('Message response: %s', _ret)
        if self._debug:
            print(('Message response: %s' % _ret))

        if 'errmfs' in _ret \
        and _ret['errmfs']['code'] == server_errors.COMPUTER_NOT_FOUND:
            return self._auto_register()

        if _ret['errmfs']['code'] != server_errors.ALL_OK:
            _msg = 'Error: %s\nInfo: %s' % (server_errors.error_info(
                _ret['errmfs']['code']), _ret['errmfs']['info'])
            _operation_failed(_msg)
            self._write_error(_msg, append=True)

        return (_ret['errmfs']['code'] == server_errors.ALL_OK)
Beispiel #9
0
    def run(self,
            cmd,
            data='',
            upload_file=None,
            sign=True,
            exit_on_error=True):
        logging.debug('URL base: %s', self._url_base)
        logging.debug('URL command: %s', cmd)
        logging.debug('URL data: %s', data)
        logging.debug('URL upload file: %s', upload_file)
        logging.debug('Sign request: %s', sign)
        logging.debug('Exit on error: %s', exit_on_error)

        if not os.path.exists(TMP_PATH):
            try:
                os.makedirs(TMP_PATH, 0777)
            except OSError:
                _msg = 'Error creating %s directory' % TMP_PATH
                logging.exception(_msg)
                return {
                    'errmfs': {
                        'info': _msg,
                        'code': server_errors.GENERIC
                    }
                }

        _filename = os.path.join(TMP_PATH,
                                 '%s.%s' % (self._filename_pattern, cmd))
        if self._debug:
            print(_filename)
        if sign:
            secure.wrap(_filename, {cmd: data},
                        key=os.path.join(self._path_keys, self._private_key))
        else:
            secure.wrap(_filename, {cmd: data})

        _post = [('message', (pycurl.FORM_FILE, _filename))]
        if upload_file:
            _post.append(('package', (pycurl.FORM_FILE, upload_file)))

        logging.debug('Post data: %s', _post)

        _curl = curl.Curl(
            self._url_base,
            _post,
            proxy=self._proxy,
            cert=self._cert,
        )
        _curl.run()
        if not self._debug:
            os.remove(_filename)

        if _curl.error:
            _msg = _('Curl error: %s') % _curl.error
            logging.error(_msg)
            print(_msg)

            return {'errmfs': {'info': _msg, 'code': _curl.errno}}

        if _curl.http_code >= 400:
            print(_('HTTP error code: %s') % _curl.http_code)
            if self._debug:
                _file = os.path.join(
                    TMP_PATH, 'response.%s.%s.html' % (_curl.http_code, cmd))
                utils.write_file(_file, str(_curl.body))
                print(_file)

            return {
                'errmfs': {
                    'info': str(_curl.body),
                    'code': server_errors.GENERIC
                }
            }

        # evaluate response
        _response = '%s.return' % _filename
        utils.write_file(_response, str(_curl.body))
        if sign:
            _ret = secure.unwrap(_response,
                                 key=os.path.join(self._path_keys,
                                                  self._public_key))
        else:
            _ret = secure.unwrap(_response)

        if not self._debug:
            os.remove(_response)
        else:
            print(_response)

        if not isinstance(_ret, dict) or not ('%s.return' % cmd) in _ret:
            if 'errmfs' in _ret:
                _msg = server_errors.error_info(_ret['errmfs']['code'])
                logging.error(_msg)
                print(_msg)

            _msg = 'url_request unexpected response: %s. Expected: %s'
            if self._debug:
                print(_msg % (_ret, '%s.return' % cmd))

            logging.critical(_msg, _ret, '%s.return' % cmd)
            sys.exit(errno.EACCES)

        _ret = _ret['%s.return' % cmd]  # unwrapping cmd response
        if isinstance(_ret, dict) and 'errmfs' in _ret:
            if _ret['errmfs']['code'] != server_errors.ALL_OK:
                _error = server_errors.error_info(_ret['errmfs']['code'])
                if self._debug:
                    print(_('Error: %s') % _error)
                    if _ret['errmfs']['info']:
                        print(_('Information: %s') % _ret['errmfs']['info'])

                logging.error('url_request server error response code: %s',
                              _error)
                logging.error('url_request server error response info: %s',
                              _ret['errmfs']['info'])

                if exit_on_error:
                    print(_('Error: %s') % _error)
                    sys.exit(errno.EACCES)

        return _ret
    def run(
        self,
        cmd,
        data='',
        upload_file=None,
        sign=True,
        exit_on_error=True
    ):
        logging.debug('URL base: %s', self._url_base)
        logging.debug('URL command: %s', cmd)
        logging.debug('URL data: %s', data)
        logging.debug('URL upload file: %s', upload_file)
        logging.debug('Sign request: %s', sign)
        logging.debug('Exit on error: %s', exit_on_error)

        if not os.path.exists(TMP_PATH):
            try:
                os.makedirs(TMP_PATH, 0777)
            except OSError:
                _msg = 'Error creating %s directory' % TMP_PATH
                logging.exception(_msg)
                return {
                    'errmfs': {
                        'info': _msg,
                        'code': server_errors.GENERIC
                    }
                }

        _filename = os.path.join(
            TMP_PATH,
            '%s.%s' % (
                self._filename_pattern,
                cmd
            )
        )
        if self._debug:
            print(_filename)
        if sign:
            secure.wrap(
                _filename,
                {cmd: data},
                key=os.path.join(self._path_keys, self._private_key)
            )
        else:
            secure.wrap(_filename, {cmd: data})

        _post = [
            ('message', (pycurl.FORM_FILE, _filename))
        ]
        if upload_file:
            _post.append(('package', (pycurl.FORM_FILE, upload_file)))

        logging.debug('Post data: %s', _post)

        _curl = curl.Curl(
            self._url_base,
            _post,
            proxy=self._proxy,
            cert=self._cert,
        )
        _curl.run()
        if not self._debug:
            os.remove(_filename)

        if _curl.error:
            _msg = _('Curl error: %s') % _curl.error
            logging.error(_msg)
            print(_msg)

            return {
                'errmfs': {
                    'info': _msg,
                    'code': _curl.errno
                }
            }

        if _curl.http_code >= 400:
            print(_('HTTP error code: %s') % _curl.http_code)
            if self._debug:
                _file = os.path.join(
                    TMP_PATH,
                    'response.%s.%s.html' % (
                        _curl.http_code,
                        cmd
                    )
                )
                utils.write_file(_file, str(_curl.body))
                print(_file)

            return {
                'errmfs': {
                    'info': str(_curl.body),
                    'code': server_errors.GENERIC
                }
            }

        # evaluate response
        _response = '%s.return' % _filename
        utils.write_file(_response, str(_curl.body))
        if sign:
            _ret = secure.unwrap(
                _response,
                key=os.path.join(self._path_keys, self._public_key)
            )
        else:
            _ret = secure.unwrap(_response)

        if not self._debug:
            os.remove(_response)
        else:
            print(_response)

        if not isinstance(_ret, dict) or not ('%s.return' % cmd) in _ret:
            if 'errmfs' in _ret:
                _msg = server_errors.error_info(_ret['errmfs']['code'])
                logging.error(_msg)
                print(_msg)

            _msg = 'url_request unexpected response: %s. Expected: %s'
            if self._debug:
                print(_msg % (_ret, '%s.return' % cmd))

            logging.critical(_msg, _ret, '%s.return' % cmd)
            sys.exit(errno.EACCES)

        _ret = _ret['%s.return' % cmd]  # unwrapping cmd response
        if isinstance(_ret, dict) and 'errmfs' in _ret:
            if _ret['errmfs']['code'] != server_errors.ALL_OK:
                _error = server_errors.error_info(_ret['errmfs']['code'])
                if self._debug:
                    print(_('Error: %s') % _error)
                    if _ret['errmfs']['info']:
                        print(_('Information: %s') % _ret['errmfs']['info'])

                logging.error(
                    'url_request server error response code: %s',
                    _error
                )
                logging.error(
                    'url_request server error response info: %s',
                    _ret['errmfs']['info']
                )

                if exit_on_error:
                    print(_('Error: %s') % _error)
                    sys.exit(errno.EACCES)

        return _ret