Exemplo n.º 1
0
 def __init__(self, options, pwd):
     self.in_data = ParseJson(USER_PROMPT_FILE).load()
     self.pwd = pwd
Exemplo n.º 2
0
 def __init__(self, options, pwd):
     self.in_data = ParseJson(USER_PROMPT_FILE).load()
     self.pwd = pwd
Exemplo n.º 3
0
class UserInput(object):
    def __init__(self, options, pwd):
        self.in_data = ParseJson(USER_PROMPT_FILE).load()
        self.pwd = pwd

    def _basic_check(self, name, answer):
        isYN = self.in_data[name].has_key('isYN')
        isdigit = self.in_data[name].has_key('isdigit')
        isexist = self.in_data[name].has_key('isexist')
        isfile = self.in_data[name].has_key('isfile')
        isremote_exist = self.in_data[name].has_key('isremote_exist')
        isIP = self.in_data[name].has_key('isIP')
        isuser = self.in_data[name].has_key('isuser')

        # check answer value basicly
        answer = answer.strip()
        if answer:
            if isYN:
                answer = answer.upper()
                if answer != 'Y' and answer != 'N':
                    log_err('Invalid parameter for %s, should be \'Y|y|N|n\'' %
                            name)
            elif isdigit:
                if not answer.isdigit():
                    log_err('Invalid parameter for %s, should be a number' %
                            name)
            elif isexist:
                if not os.path.exists(answer):
                    log_err('%s path \'%s\' doesn\'t exist' % (name, answer))
            elif isfile:
                if not os.path.isfile(answer):
                    log_err('%s file \'%s\' doesn\'t exist' % (name, answer))
            elif isremote_exist:
                hosts = cfgs['node_list'].split(',')
                remotes = [Remote(host, pwd=self.pwd) for host in hosts]

                nodes = ''
                for remote in remotes:
                    # check if directory exists on remote host
                    remote.execute('ls %s 2>&1 >/dev/null' % answer,
                                   chkerr=False)
                    if remote.rc != 0:
                        nodes += ' ' + remote.host
                if nodes:
                    log_err('%s path \'%s\' doesn\'t exist on node(s) \'%s\'' %
                            (name, answer, nodes))
            elif isIP:
                try:
                    socket.inet_pton(socket.AF_INET, answer)
                except:
                    log_err('Invalid IP address \'%s\'' % answer)
            elif isuser:
                if re.match(r'\w+', answer).group() != answer:
                    log_err('Invalid user name \'%s\'' % answer)

        else:
            log_err('Empty value for \'%s\'' % name)

    def _handle_prompt(self, name, user_defined):
        prompt = self.in_data[name]['prompt']
        default = user_defined

        if (not default) and self.in_data[name].has_key('default'):
            default = self.in_data[name]['default']

        ispasswd = self.in_data[name].has_key('ispasswd')
        isYN = self.in_data[name].has_key('isYN')

        # no default value for password
        if ispasswd: default = ''

        if isYN:
            prompt = prompt + ' (Y/N) '

        if default:
            prompt = prompt + ' [' + default + ']: '
        else:
            prompt = prompt + ': '

        # no default value for password
        if ispasswd:
            orig = getpass.getpass(prompt)
            confirm = getpass.getpass('Confirm ' + prompt)
            if orig == confirm:
                answer = confirm
            else:
                log_err('Password mismatch')
        else:
            try:
                answer = raw_input(prompt)
            except UnicodeEncodeError:
                log_err('Character Encode error, check user input')
            if not answer and default: answer = default

        return answer

    def get_input(self, name, user_defined='', prompt_mode=True):
        if self.in_data.has_key(name):
            if prompt_mode:
                # save configs to global dict
                cfgs[name] = self._handle_prompt(name, user_defined)

            # check basic values from global configs
            self._basic_check(name, cfgs[name])
        else:
            # should not go to here, just in case
            log_err('Invalid prompt')

    def get_confirm(self):
        answer = raw_input('Confirm result (Y/N) [N]: ')
        if not answer: answer = 'N'

        answer = answer.upper()
        if answer != 'Y' and answer != 'N':
            log_err('Invalid parameter, should be \'Y|y|N|n\'')
        return answer

    def notify_user(self):
        """ show the final configs to user """
        format_output('Final Configs')
        title = ['config type', 'value']
        pt = PrettyTable(title)
        for item in title:
            pt.align[item] = 'l'

        for key, value in sorted(cfgs.items()):
            # only notify user input value
            if self.in_data.has_key(key) and value:
                if self.in_data[key].has_key('ispasswd'): continue
                pt.add_row([key, value])
        print pt
        confirm = self.get_confirm()
        if confirm != 'Y':
            if os.path.exists(DBCFG_FILE): os.remove(DBCFG_FILE)
            run_cmd('rm -rf %s/*.status' % INSTALLER_LOC)
            log_err('User quit')
Exemplo n.º 4
0
class UserInput(object):
    def __init__(self, options, pwd):
        self.in_data = ParseJson(USER_PROMPT_FILE).load()
        self.pwd = pwd

    def _basic_check(self, name, answer):
        isYN = self.in_data[name].has_key('isYN')
        isdigit = self.in_data[name].has_key('isdigit')
        isexist = self.in_data[name].has_key('isexist')
        isfile = self.in_data[name].has_key('isfile')
        isremote_exist = self.in_data[name].has_key('isremote_exist')
        isIP = self.in_data[name].has_key('isIP')
        isuser = self.in_data[name].has_key('isuser')

        # check answer value basicly
        answer = answer.strip()
        if answer:
            if isYN:
                answer = answer.upper()
                if answer != 'Y' and answer != 'N':
                    log_err('Invalid parameter for %s, should be \'Y|y|N|n\'' % name)
            elif isdigit:
                if not answer.isdigit():
                    log_err('Invalid parameter for %s, should be a number' % name)
            elif isexist:
                if not os.path.exists(answer):
                    log_err('%s path \'%s\' doesn\'t exist' % (name, answer))
            elif isfile:
                if not os.path.isfile(answer):
                    log_err('%s file \'%s\' doesn\'t exist' % (name, answer))
            elif isremote_exist:
                hosts = cfgs['node_list'].split(',')
                remotes = [Remote(host, pwd=self.pwd) for host in hosts]

                nodes = ''
                for remote in remotes:
                    # check if directory exists on remote host
                    remote.execute('ls %s 2>&1 >/dev/null' % answer, chkerr=False)
                    if remote.rc != 0:
                        nodes += ' ' + remote.host
                if nodes:
                    log_err('%s path \'%s\' doesn\'t exist on node(s) \'%s\'' % (name, answer, nodes))
            elif isIP:
                try:
                    socket.inet_pton(socket.AF_INET, answer)
                except:
                    log_err('Invalid IP address \'%s\'' % answer)
            elif isuser:
                if re.match(r'\w+', answer).group() != answer:
                    log_err('Invalid user name \'%s\'' % answer)

        else:
            log_err('Empty value for \'%s\'' % name)

    def _handle_prompt(self, name, user_defined):
        prompt = self.in_data[name]['prompt']
        default = user_defined

        if (not default) and self.in_data[name].has_key('default'):
            default = self.in_data[name]['default']

        ispasswd = self.in_data[name].has_key('ispasswd')
        isYN = self.in_data[name].has_key('isYN')

        # no default value for password
        if ispasswd: default = ''

        if isYN:
            prompt = prompt + ' (Y/N) '

        if default:
            prompt = prompt + ' [' + default + ']: '
        else:
            prompt = prompt + ': '

        # no default value for password
        if ispasswd:
            orig = getpass.getpass(prompt)
            confirm = getpass.getpass('Confirm ' + prompt)
            if orig == confirm:
                answer = confirm
            else:
                log_err('Password mismatch')
        else:
            try:
                answer = raw_input(prompt)
            except UnicodeEncodeError:
                log_err('Character Encode error, check user input')
            if not answer and default: answer = default

        return answer

    def get_input(self, name, user_defined='', prompt_mode=True):
        if self.in_data.has_key(name):
            if prompt_mode:
                # save configs to global dict
                cfgs[name] = self._handle_prompt(name, user_defined)

            # check basic values from global configs
            self._basic_check(name, cfgs[name])
        else:
            # should not go to here, just in case
            log_err('Invalid prompt')

    def get_confirm(self):
        answer = raw_input('Confirm result (Y/N) [N]: ')
        if not answer: answer = 'N'

        answer = answer.upper()
        if answer != 'Y' and answer != 'N':
            log_err('Invalid parameter, should be \'Y|y|N|n\'')
        return answer

    def notify_user(self):
        """ show the final configs to user """
        format_output('Final Configs')
        title = ['config type', 'value']
        pt = PrettyTable(title)
        for item in title:
            pt.align[item] = 'l'

        for key, value in sorted(cfgs.items()):
            # only notify user input value
            if self.in_data.has_key(key) and value:
                if self.in_data[key].has_key('ispasswd'): continue
                pt.add_row([key, value])
        print pt
        confirm = self.get_confirm()
        if confirm != 'Y':
            if os.path.exists(DBCFG_FILE): os.remove(DBCFG_FILE)
            run_cmd('rm -rf %s/*.status' % INSTALLER_LOC)
            log_err('User quit')