from cubric import DeploymentBase
from cubric import Git, File, Template, NGINX
from path import Path
import plumbum
import datetime


class EmberLocalBuild(DeploymentBase):
    requires = (Git, File)

    def sync_repo(self, env):
        self.git.cloneup(self.config.repo, self.config.project)

    def run(self, env):
        self.file.removed(self.config.project)
        self.sync_repo(env)
        with env.chdir(self.config.project):
            print("Using node:")
            env.command("node", "-v")
            env.command("npm", "install")
            env.command("bower", "install")
            # env.command("ember", "build", "--env=production")
            env.command("ember", "build", "--env=demo")


class EmberDeploy(DeploymentBase):
    requires = (Git, File, Template, NGINX)

    def run(self, env):
        """
            - Create remote instance folder
            - Create folder based on commit hash
            - Recursive copy dist/
            - Symlink
            - Setup / update nginx
        """

        env.mkdir(self.config.instancepath, chdir=True)
        # Not usable, works remotely not local!
        # print("Hash", self.git.shorthash(self.config.project))

        tsfolder = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        for d in ("logs", tsfolder):
            env.mkdir(self.config.instancepath / d)
        plumbum.path.utils.copy(Path(self.config.project) / 'dist',
                                env.host.path(self.config.instancepath
                                              / tsfolder))
        self.file.present(self.config.instancepath / tsfolder,
                          target=self.config.instancepath / 'www',
                          type=File.LINK)
        self.template.create(src="templates/emberapp-nginx.conf",
                             dst=self.config.instancepath / 'nginx.conf',
                             args=self.config)
        self.nginx.install(self.config.instancepath / 'nginx.conf',
                           self.config.projectid)

@@ -195,16 +195,201 @@
  File)%0A%0A
+    _ember_env = %22demo%22%0A%0A    @property%0A    def ember_env(self):%0A        try:%0A            return self.config.ember_env%0A        except AttributeError:%0A            return self._ember_env%0A%0A
     def 
@@ -402,32 +402,32 @@
 epo(self, env):%0A
-
         self.git
@@ -787,18 +787,16 @@
         
- #
  env.com
@@ -829,75 +829,34 @@
 env=
-production%22)%0A            env.command(%22ember%22, %22build%22, %22--env=demo%22
+%7B%7D%22.format(self.ember_env)
 )%0A%0A%0A
@@ -1169,16 +1169,45 @@
    %22%22%22%0A%0A
+        with env.sudo():%0A    
         
@@ -1252,16 +1252,62 @@
 dir=True
+,%0A                      owner=self.config.user
 )%0A      

make env configurable; make sure folder is owned properly