示例#1
0
文件: cli.py 项目: cakebread/jindo
 def __init__(self, api_key=None):
     #PyPI project name with proper case
     self.options = None
     self.logger = logging.getLogger("jindo")
     self.ansi = True
     if api_key:
         self.api_key = api_key
     else:
         #Obviously not going to work if we add more options.
         self.api_key = get_rc_file().split("=")[1].strip()
     self.jindo = JindoProxy()
示例#2
0
文件: cli.py 项目: cakebread/jindo
class JindoCLI(object):

    """
    Main class for jindo
    """

    def __init__(self, api_key=None):
        #PyPI project name with proper case
        self.options = None
        self.logger = logging.getLogger("jindo")
        self.ansi = True
        if api_key:
            self.api_key = api_key
        else:
            #Obviously not going to work if we add more options.
            self.api_key = get_rc_file().split("=")[1].strip()
        self.jindo = JindoProxy()


    def set_log_level(self):
        """
        Set log level according to command-line options

        @returns: logger object
        """

        if self.options.debug:
            self.logger.setLevel(logging.DEBUG)
        elif self.options.quiet:
            self.logger.setLevel(logging.ERROR)
        else:
            self.logger.setLevel(logging.INFO)
        self.logger.addHandler(logging.StreamHandler())
        return self.logger

    def get_service_details(self, service):
        '''Return details for a specific service'''
        data, code, status = self.jindo.fetch_service_details(service, self.api_key)
        #TODO: Raise exceptions based on code/status?
        return data

    def get_services(self):
        '''Return list of all service ids'''
        data, code, status = self.jindo.fetch_services(self.api_key)
        #TODO: Raise exceptions based on code/status?
        return data

    def reboot_server(self, service):
        '''Reboots server'''
        data, code, status = self.jindo.reboot_server(service, self.api_key)
        #TODO: Raise exceptions based on code/status?
        return data

    def add_diskspace(self, service):
        '''Adds temp disk space'''
        data, code, status = self.jindo.add_temp_diskspace(service, self.api_key)
        return data

    def details_all_services(self, ansi=True):
        services = self.get_services()
        for service in services:
            print "Service: %s\n" % service
            json_data = self.get_service_details(service)
            self.jindo.print_service_details(json_data, self.options.format, ansi)
            print "\n"

    def run(self):
        """
        Perform actions based on CLI options

        @returns: status code
        """
        opt_parser = setup_opt_parser()
        (self.options, remaining_args) = opt_parser.parse_args()
        logger = self.set_log_level()

        if self.options.quiet:
            self.ansi = False

        if self.options.jindo_version:
            self.jindo_version()
            return

        if (len(sys.argv) == 1 or len(remaining_args) > 2):
            opt_parser.print_help()
            return 2
        if self.options.service:
            json_data = self.get_service_details(self.options.service)
            self.jindo.print_service_details(json_data, self.options.format, self.ansi)
            return 0
        if self.options.details_all_services:
            self.details_all_services(self.ansi)
            return 0
        elif self.options.service_ids:
            json_data = self.get_services()
            #TODO: Could be formatted better. Ideas? Go for it.
            if self.options.format == 'text':
                if self.options.quiet:
                    print str(json_data).strip(']').lstrip('[')
                else:
                    print "Service IDs: %s" % json_data
            else:
                print json_data
            return 0
        elif self.options.reboot:
            json_data = self.reboot_server(self.options.reboot)
            self.jindo.print_response_details(json_data, self.options.format)
            return 0
        elif self.options.diskspace:
            json_data = self.add_diskspace(self.options.diskspace)
            self.jindo.print_response_details(json_data, self.options.format)
            return 0
        else:
            opt_parser.print_help()
            return 2

    def jindo_version(self):
        """
        Show jindo's version

        @returns: 0
        """
        self.logger.info("Jindo version %s" % __version__)
        return 0