# Copyright Hybrid Logic Ltd.  See LICENSE file for details.
# -*- test-case-name: flocker.node.test.test_script -*-

"""
The command-line ``flocker-changestate`` tool.
"""

from twisted.python.usage import Options, UsageError
from twisted.internet.defer import succeed

from yaml import safe_load
from yaml.error import YAMLError

from zope.interface import implementer

from ..common.script import (
    flocker_standard_options, FlockerScriptRunner, ICommandLineScript)

__all__ = [
    "ChangeStateOptions",
    "ChangeStateScript",
    "flocker_changestate_main",
]


@flocker_standard_options
class ChangeStateOptions(Options):
    """
    Command line options for ``flocker-changestate`` management tool.
    """

    longdesc = """\
    flocker-changestate is called by flocker-deploy to set the configuration of
    a node.

        DEPLOYMENT_CONFIGURATION: The YAML string describing the desired
            deployment configuration.

        APPLICATION_CONFIGURATION: The YAML string describing the desired
            application configuration.
    """
    synopsis = ("Usage: flocker-changestate [OPTIONS] "
                "DEPLOYMENT_CONFIGURATION APPLICATION_CONFIGURATION")

    def parseArgs(self, deployment_config, app_config, hostname):
        """
        Parse `deployment_config` and `app_config` strings as YAML and assign
        the resulting dictionaries to this `Options` dictionary.

        :param bytes deployment_config: The YAML string describing the desired
            deployment configuration.
        :param bytes app_config: The YAML string describing the desired
            application configuration.
        :param bytes hostname: The ascii encoded hostname of this node.
        """
        try:
            self['deployment_config'] = safe_load(deployment_config)
        except YAMLError as e:
            raise UsageError(
                "Deployment config could not be parsed as YAML:\n\n" + str(e)
            )
        try:
            self['app_config'] = safe_load(app_config)
        except YAMLError as e:
            raise UsageError(
                "Application config could not be parsed as YAML:\n\n" + str(e)
            )
        try:
            self['hostname'] = hostname.decode('ascii')
        except UnicodeDecodeError:
            raise UsageError(
                "Non-ascii hostname: {hostname}".format(hostname=hostname)
            )




@implementer(ICommandLineScript)
class ChangeStateScript(object):
    """
    A command to get a node into a desired state by pushing volumes, starting
    and stopping applications, opening up application ports and setting up
    routes to other nodes.
    """
    def main(self, reactor, options):
        """
        See :py:meth:`ICommandLineScript.main` for parameter documentation.
        """
        return succeed(None)


def flocker_changestate_main():
    return FlockerScriptRunner(
        script=ChangeStateScript(),
        options=ChangeStateOptions()
    ).main()

@@ -835,36 +835,34 @@
     
-    DEPLOYMENT_CONFIGURATION
+* deployment_configuration
 : Th
@@ -898,36 +898,32 @@
 desired%0A        
-    
 deployment confi
@@ -941,37 +941,35 @@
     
-    APPLICATION_CONFIGURATION
+* application_configuration
 : Th
@@ -1005,36 +1005,32 @@
 desired%0A        
-    
 application conf
@@ -1036,24 +1036,67 @@
 figuration.%0A
+    * hostname: The hostname of this node.%0A
     %22%22%22%0A    
@@ -1168,58 +1168,72 @@
    %22
-DEPLOYMENT_CONFIGURATION APPLICATION_CONFIGURATION
+%3Cdeployment_configuration %3Capplication_configuration%3E %3Chostname%3E
 %22)%0A%0A
@@ -2448,18 +2448,16 @@
     )%0A%0A%0A
-%0A%0A
 @impleme

Update the synopsis and description