# ex: set expandtab softtabstop=4 shiftwidth=4: -*- cpy-indent-level: 4; indent-tabs-mode: nil -*-
# Copyright (C) 2008 Morgan Stanley
#
# This module is part of Aquilon
"""Contains the logic for `aq deploy`."""


import os

from aquilon.server.broker import BrokerCommand
from aquilon.server.dbwrappers.domain import verify_domain
from aquilon.server.processes import run_command


class CommandDeploy(BrokerCommand):

    required_parameters = ["domain"]

    def render(self, session, domain, to, **arguments):
        """ This currently ignores the 'to' parameter."""

        # Verify that it exists before trying to deploy it.
        dbdomain = verify_domain(session, domain,
                self.config.get("broker", "servername"))
        domaindir = os.path.join(self.config.get("broker", "templatesdir"),
                dbdomain.name)
        git_env={"PATH":"%s:%s" % (self.config.get("broker", "git_path"),
                os.environ.get("PATH", ""))}
        run_command(["git", "pull", domaindir], env=git_env,
                path=self.config.get("broker", "kingdir"))
        return



@@ -266,16 +266,80 @@
 Command%0A
+from aquilon.exceptions_ import ProcessException, ArgumentError%0A
 from aqu
@@ -1023,16 +1023,33 @@
 , %22%22))%7D%0A
+        try:%0A    
         
@@ -1117,16 +1117,20 @@
         
+    
 path=sel
@@ -1160,24 +1160,245 @@
 %22kingdir%22))%0A
+        except ProcessException, e:%0A            run_command(%5B%22git%22, %22reset%22, %22--hard%22%5D, env=git_env,%0A                    path=self.config.get(%22broker%22, %22kingdir%22))%0A            raise ArgumentError(%22%5Cn%25s%25s%22 %25(e.out,e.err))%0A
         retu

reset template-king in the event of a merge conflict