def command_template_init(*args):
    '''
    admin template init prefix template_name package

    Instantiate this template into a set of services with a common
    prefix. For example, if the template has services 'space' and
    'oh', a prefix of 'myworld' will result in two services,
    'myworld-space' and 'myworld-oh'.
    '''
    if len(args) < 3:
        print "Must specify at least template name and service prefix"
        return 1

    prefix = args[0]
    template = args[1]
    package = args[2]

    if not validate_template_config(template):
        return 1

    # First sanity check that we don't have any services in the way
    top_servpath = service.service_path(prefix)
    if os.path.exists(top_servpath):
        print "Top-level service directory", top_servpath, "already exists."
        return 1
    svcs = templateconfig.services
    for s in svcs:
        servpath = service.service_path(prefix, s.name)
        if os.path.exists(servpath):
            print "Service directory", os.path.join(prefix, s.name), "already exists."
            return 1

    return command_template_reinit(*args, notre=True)
def command_template_reinit(*args, **kwargs):
    '''
    admin template reinit prefix template_name package

    Reinstantiate a template over an existing instantiation. It
    performs the same steps as template init but leaves existing data
    in place.
    '''
    if len(args) < 3:
        print "Must specify at least template name and service prefix"
        return 1

    prefix = args[0]
    template = args[1]
    package = args[2]

    if not validate_template_config(template):
        return 1

    # Then instantiate each of them
    svcs = templateconfig.services
    for s in svcs:
        servname = os.path.join(prefix, s.name)
        if 'notre' in kwargs and kwargs['notre']:
            service.command_service_init(*[servname, package, os.path.join(template, s.source)])
        else:
            service.command_service_reinit(*[servname, package, os.path.join(template, s.source)])

    # Copy the template configuration in so we can make sure we keep the same setup
    shutil.copy(template_path(template, 'template.py'), service.service_path(prefix))
def command_template_destroy(*args):
    '''
    admin template start template_name prefix

    Destroy this template instantiated with the given prefix
    '''
    if len(args) < 1:
        print "Must specify at least template instance prefix"
        return 1

    prefix = args[0]

    if not validate_instance_config(prefix):
        return 1

    # Then instantiate each of them
    svcs = templateconfig.services
    for s in svcs:
        servname = os.path.join(prefix, s.name)
        service.command_service_destroy(*[servname])
    # Make sure we get rid of the parent directory since each
    # individual service only deletes its own directory
    shutil.rmtree( service.service_path(prefix) )
def template_load_instance_config(prefix):
    """
    Load the configuration for an instance with the given prefix
    """
    return templateconfig.load_config(service.service_path(prefix, 'template.py'))