import argparse
from mr.awsome import aws, aws_ssh
from os import path


# register our own library and roles paths into ansible
ploy_path = path.abspath(path.join(path.dirname(__file__), '../'))

ansible_paths = dict(
    roles=[path.join(ploy_path, 'roles')],
    library=[path.join(ploy_path, 'library')]
)


class PloyBootstrapHostCmd(object):

    def __init__(self, aws):
        self.aws = aws

    def __call__(self, argv, help):
        """Bootstrap a jailhost that's been booted into MFSBsd."""
        parser = argparse.ArgumentParser(
            prog="ploy bootstrap-jailhost",
            description=help,
            add_help=False,
        )
        masters = dict((master.id, master) for master in self.aws.get_masters('ezjail_admin'))
        if len(masters) > 1:
            parser.add_argument(
                "master",
                nargs=1,
                metavar="master",
                help="Name of the jailhost from the config.",
                choices=masters)
        args = parser.parse_args(argv[:1])
        if len(masters) > 1:
            master = args.master[0]
        else:
            master = masters.keys()[0]
        instance = self.aws.instances[master]
        instance.do('bootstrap')


class PloyConfigureHostCmd(object):

    def __init__(self, aws):
        self.aws = aws

    def __call__(self, argv, help):
        """Configure a jailhost (installs ezjail etc.) after it has been bootstrapped."""
        parser = argparse.ArgumentParser(
            prog="ploy configure-jailhost",
            description=help,
            add_help=False,
        )
        masters = dict((master.id, master) for master in self.aws.get_masters('ezjail_admin'))
        if len(masters) > 1:
            parser.add_argument(
                "master",
                nargs=1,
                metavar="master",
                help="Name of the jailhost from the config.",
                choices=masters)
        args = parser.parse_args(argv[:1])
        if len(masters) > 1:
            master = args.master[0]
        else:
            master = masters.keys()[0]
        instance = self.aws.instances[master]
        instance.apply_playbook(path.join(ploy_path, 'roles', 'jailhost.yml'))


def get_commands(aws):
    return [
        ('bootstrap-jailhost', PloyBootstrapHostCmd(aws)),
        ('configure-jailhost', PloyConfigureHostCmd(aws))]


def get_massagers():
    from mr.awsome.config import HooksMassager
    return [HooksMassager('ez-instance', 'hooks')]

plugin = dict(
    get_massagers=get_massagers,
    get_commands=get_commands)


class AWSomeHooks(object):

    def before_start(self, server):
        """make sure we have a startup script for jails that installs python
        into it (so we can control it via ansible)
        """
        if 'startup_script' not in server.config:
            server.config['startup_script'] = path.join(ploy_path, 'startup-ansible-jail')


def ploy(configname=None, **kw):  # pragma: no cover
    if configname is None:
        configname = 'ploy.conf'
    return aws(configname=configname, **kw)


def ploy_ssh(configname=None, **kw):  # pragma: no cover
    if configname is None:
        configname = 'ploy.conf'
    return aws_ssh(configname=configname, **kw)

@@ -2506,88 +2506,8 @@
 )%5D%0A%0A
-plugin = dict(%0A    get_massagers=get_massagers,%0A    get_commands=get_commands)%0A%0A
 %0Acla
@@ -2703,24 +2703,108 @@
         %22%22%22%0A
+        if not server.master.sectiongroupname.startswith('ez-'):%0A            return%0A
         if '
@@ -2930,24 +2930,176 @@
 le-jail')%0A%0A%0A
+def get_hooks():%0A    return %5BAWSomeHooks()%5D%0A%0A%0Aplugin = dict(%0A    get_hooks=get_hooks,%0A    get_massagers=get_massagers,%0A    get_commands=get_commands)%0A%0A%0A
 def ploy(con

Use new get_hooks plugin feature of mr.awsome.