#!/usr/bin/python

from sys import exit, argv
from getopt import getopt

from opennode.cli.actions.vm import openvz
from opennode.cli import config

def _help():
    print """
NAME
    vzcfgcreator: OpenVZ CT UBC configuration generator utility.

SYNOPSIS
    vzcfgcreator [OPTIONS] 

OPTIONS:
    -m, --memory=X
        Guaranteed memory (in GB) for container
        Default: 0.25GB
    -s, --swap=X
        Swap space (in GB) for container
        Default: 0.25GB
    -d, --disk=X
        Disk space allocation limit for container (in GB)
        Default: 10GB
    -c, --cpus=X
        Number of CPUs enabled for container
        Default: 1
    -l, --limitcpu=X
        CPU usage limit (in %)
        Default: 50%
    -h, --help
        Display usage help.

SEE ALSO:
    OpenNode web page:
    http://opennodecloud.com
    """
    exit()

if __name__ == '__main__':
    #sys.stdout.write(os.popen('clear').read())
    try:
        options, args = getopt(argv[1:], 'm:s:d:c:l:h', 
                               ['memory=', 'swap=', 'disk=', 'cpus=', 
                               'limitcpu=', 'help'])
    except getopt.GetoptError, err:
        # print help information and exit:
        _help()
        print str(err) # will print something like "option -a not recognized"
        exit(2)
    
    memory = swap = disk = cpus = cpulimit = ''
    
    for o, a in options:
        if o in ("-m", "--memory"): memory = a
        elif o in ("-s", "--swap"): swap = a
        elif o in ("-d", "--disk"): disk = a
        elif o in ("-c", "--cpus"): cpus = a
        elif o in ("-l", "--limitcpu"): cpulimit = a
        elif o in ("-h", "--help"): _help()
        else: assert False, "unhandled option"
    
    inputvars = {
        "memory": memory or config.c("ovf-defaults", "memory", "openvz"),
        "swap": swap or config.c("ovf-defaults", "swap", "openvz"),
        "disk": disk or config.c("ovf-defaults", "disk", "openvz"),
        "vcpu": cpus or config.c("ovf-defaults", "vcpu", "openvz"),
        "vcpulimit": cpulimit or config.c("ovf-defaults", "vcpulimit", "openvz")
    }
    vzconf = openvz.generate_ubc_config(inputvars)
    print vzconf
