Commit 8e14aa0f authored by Seblu's avatar Seblu
Browse files

Add profile support

parent c530ed29
Loading
Loading
Loading
Loading
+43 −1
Original line number Diff line number Diff line
======
Config
======
cc-cli support FreeDesktop BaseDirectory specifications (http://standards.freedesktop.org/basedir-spec/0.6/)
Your main config is stored in ~/.config/cc-cli/cli (ini file format with section cli)
You can configure all parameter in this file
You will find an example in examples subdirectory in git.

You can set default behaviour with environment variables. The following are recognised:
CC_SERVER, CC_PORT, CC_LOGIN, CC_PASS, CC_DEBUG, CC_PROFILE

To quickly connect to mulptiple servers, you can define profiles.
A profile is an ini config file in ~/.config/cc-cli/ directory with suffix .profile and section profile.
Profiles file are loaded exactly like the main config file.
You can load toto profile by calling cc-cli -P toto
You have to play with the following loading order to do worderful things.

You configuration is loaded in this order:
1 load default config file settings
2 load default config file profile
2 load environment profile
3 load environment settings
4 load argline profile
5 load argline settings

=====
Alias
=====
CLI support aliasing. This is a very useful command substitution
You can use the alias command to set alias.

Setting an alias w, which list online client
alias w "list a&con!=offline&role=client"

Setting an alias vm, which list running vm (and show cpu)
alias vm "list vm&status=running$cpu"

===
TQL
===
You can look in TQL file to see the power of tql.

===========
New release
===========
+55 −50
Original line number Diff line number Diff line
@@ -19,25 +19,41 @@ from cccli.cli import Cli
from cccli.printer import Printer
from cccli.exception import *

canonical_name="cc-cli"
settings = {
    "port": "1984",
    "timeout": "5",
    "timeout": "30",
    "hsize": "100",
    "config": "%s/cli"%BaseDirectory.save_config_path(canonical_name),
    "alias": "%s/alias"%BaseDirectory.save_config_path(canonical_name),
    "history": "%s/history"%BaseDirectory.save_data_path(canonical_name),
    "config": "%s/cli"%BaseDirectory.save_config_path(cccli.canonical_name),
    "alias": "%s/alias"%BaseDirectory.save_config_path(cccli.canonical_name),
    "history": "%s/history"%BaseDirectory.save_data_path(cccli.canonical_name),
    }

printer = Printer(False)
def load_config_file(config, filename, section):
    '''Update a dict with param from an ini file '''
    if os.access(filename, os.R_OK):
        fparser = ConfigParser.SafeConfigParser()
        fparser.read(filename)
        if fparser.has_section(section):
            config.update(fparser.items(section))
    else:
        printer.warn("Unable to load file %s"%filename)

def get_profile_path(profile):
    '''Force a profile path from a profile name'''
    return "%s/%s/%s.profile"%(BaseDirectory.xdg_config_home, cccli.canonical_name, profile)

try:
    # load a printer
    printer = Printer(False)

    # parse config file
    if os.access(settings["config"], os.R_OK):
        fparser = ConfigParser.SafeConfigParser()
        fparser.read(settings["config"])
        if fparser.has_section("cli"):
            settings.update(fparser.items("cli"))
        load_config_file(settings, settings["config"], "cli")

    # parse config file profile
    if "profile" in settings:
        load_config_file(settings, get_profile_path(settings["profile"]), "profile")
        del settings["profile"]

    # parse env
    if "CC_SERVER" in os.environ:
@@ -51,65 +67,51 @@ try:
    if "CC_DEBUG" in os.environ:
        settings["debug"] = "True"

    # parse env profile
    if "CC_PROFILE" in os.environ:
        load_config_file(settings, get_profile_path(os.environ["CC_PROFILE"]), "profile")

    # Parse line argument
    oparser = optparse.OptionParser(usage="usage: %prog [options] [commands]",
                                    version=cccli.version)
    oparser.add_option("-d", "--debug",
                       action="store_true",
                       dest="debug",
                       default="",
                       help="debug mode")
    oparser.add_option("-l", "--login",
                       action="store",
                       dest="login",
                       default="",
                       help="server login")
    oparser.add_option("-s", "--server",
                       action="store",
                       dest="server",
                       default="",
                       help="server address")
    oparser.add_option("-p", "--port",
                       action="store",
                       dest="port",
                       default="1984",
                       help="server port")
    oparser.add_option("-t", "--timeout",
                       action="store",
                       dest="timeout",
                       default="10",
                       help="connection timeout")
    oparser.add_option("--history-file",
                       action="store",
                       dest="history",
                       default="",
    oparser.add_option("-d", "--debug", action="store_true",dest="debug",
                       help="Debug mode")
    oparser.add_option("-l", "--login",action="store",dest="login",
                       help="Server login")
    oparser.add_option("-H", "--hostname",action="store",dest="server",
                       help="Server hostname")
    oparser.add_option("-P", "--port",action="store",dest="port",
                       help="Server port")
    oparser.add_option("-t", "--timeout",action="store",dest="timeout",
                       help="Connection timeout")
    oparser.add_option("-p", "--profile",action="store",dest="profile",
                       help="Profile name")
    oparser.add_option("--history-file",action="store",dest="history",
                       help="History file")
    oparser.add_option("--history-size",
                       action="store",
                       dest="hsize",
                       default="",
    oparser.add_option("--history-size",action="store",dest="hsize",
                       help="History max entry count")
    oparser.add_option("--force-yes",
                       action="store_true",
                       dest="forceyes",
                       default="",
                       help="Answer yes to yes/no question")
    oparser.add_option("--force-yes",action="store_true",dest="forceyes",
                       help="Answer Yes to all questions (Dangerous)")
    (options, args) = oparser.parse_args()

    # options handling
    for i in ("debug", "login", "server", "port", "timeout", "history", "hsize", "forceyes"):
    for i in [ x.dest for x in oparser.option_list if x.dest ]:
        if hasattr(options, i):
            o = getattr(options, i)
            if o:
                settings[i] = o

    # parse argline file profile
    if "profile" in settings:
        load_config_file(settings, get_profile_path(settings["profile"]), "profile")
        del settings["profile"]

    # debug stuff
    if "debug" in settings:
        cccli.debug = bool(settings["debug"])
    else:
        warnings.filterwarnings("ignore")
    printer.debug("Debugging on")
    printer.debug("Settings: %s"%settings)

    # checking server name
    if "server" not in settings:
@@ -131,6 +133,9 @@ try:
        printer.setinteractive()
        settings["pass"] = printer.getpass("Password: ")

    # print settings
    printer.debug("Settings: %s"%settings)

    # start cli
    cli = Cli(settings)
    cli.start(" ".join(args))
+1 −0
Original line number Diff line number Diff line
@@ -5,5 +5,6 @@
CloudControl CLI
'''

canonical_name="cc-cli"
version = "1~dev"
debug = False

examples/alias

0 → 100644
+6 −0
Original line number Diff line number Diff line
[alias]
lt = list -t
ls = list
w = list a&con!=offline&role=client
vm = list vm&status=running$cpu

examples/cli

0 → 100644
+4 −0
Original line number Diff line number Diff line
[cli]
hsize = 1000
profile = lab
Loading