old_contents: from fabric.api import cd, run, sudo, env, execute, task, local
  from datetime import datetime
  
  env.hosts = ['andrewlorente.com']
  @task
  def deploy(app):
      if app not in apps.keys():
          raise Exception("Unknown deploy target '{0}'".format(app))
  
      release_id = datetime.now().strftime("%Y%m%d%H%M%S")
      release_dir = "/u/apps/{0}/releases/{1}".format(app, release_id)
  
      execute(checkout, app, release_dir, hosts=apps[app]['hosts'])
      execute(apps[app]['build'], app, release_dir, hosts=apps[app]['hosts'])
      if 'extra' in apps[app]:
          execute(apps[app]['extra'], app, release_dir, hosts=apps[app]['hosts'])
      execute(restart, app, hosts=['alorente@andrewlorente.com'])
  
  def checkout(app, release_dir):
      repo = "https://git.andrewlorente.com/AndrewLorente/{0}.git".format(app)
      run("git clone -q {0} {1}".format(repo, release_dir))
  
  def build_haskell(app, release_dir):
      with cd(release_dir):
          run("cabal update")
          run("cabal install --constraint 'template-haskell installed' "
              "--dependencies-only --force-reinstall")
          run("cabal configure")
          run("cabal build")
      run("ln -nfs {0} /u/apps/{1}/current".format(release_dir, app))
  
  def build_python(app, release_dir):
      with cd(release_dir):
          run("virtualenv Env")
          run("source Env/bin/activate")
          run("Env/bin/python setup.py develop")
      run("ln -nfs {0} /u/apps/{1}/current".format(release_dir, app))
  
  def link_session_key(app, release_dir):
      run("ln -nfs /u/apps/{0}/shared/session_key.txt "
          "{1}/session_key.txt".format(app, release_dir))
  
  def restart(app):
      sudo("initctl restart " + app)
  
  @task
  def puppet():
      local('bundle exec cap production puppet:apply')
  
  apps = {
      'bloge': {
          'build': build_haskell,
          'hosts': ['bloge@andrewlorente.com'],
      },
      'andrewlorente': {
          'build': build_haskell,
          'hosts': ['andrewlorente@andrewlorente.com'],
          'extra': link_session_key,
      },
      'catsnap': {
          'build': build_python,
          'hosts': ['catsnap@andrewlorente.com'],
      },
  }
  
  
new_contents: from fabric.api import cd, run, sudo, env, execute, task, local
  from datetime import datetime
  
  env.hosts = ['andrewlorente.com']
  @task
  def deploy(app):
      if app not in apps.keys():
          raise Exception("Unknown deploy target '{0}'".format(app))
  
      release_id = datetime.now().strftime("%Y%m%d%H%M%S")
      release_dir = "/u/apps/{0}/releases/{1}".format(app, release_id)
  
      execute(checkout, app, release_dir, hosts=apps[app]['hosts'])
      execute(apps[app]['build'], app, release_dir, hosts=apps[app]['hosts'])
      if 'extra' in apps[app]:
          execute(apps[app]['extra'], app, release_dir, hosts=apps[app]['hosts'])
      execute(restart, app, hosts=['alorente@andrewlorente.com'])
  
  def checkout(app, release_dir):
      repo = "https://git.andrewlorente.com/AndrewLorente/{0}.git".format(app)
      run("git clone -q {0} {1}".format(repo, release_dir))
  
  def build_haskell(app, release_dir):
      with cd(release_dir):
          run("cabal update")
          run("cabal install --constraint 'template-haskell installed' "
              "--dependencies-only --force-reinstall")
          run("cabal configure")
          run("cabal build")
      run("ln -nfs {0} /u/apps/{1}/current".format(release_dir, app))
  
  def build_python(app, release_dir):
      with cd(release_dir):
          run("virtualenv Env")
          run("source Env/bin/activate")
          run("Env/bin/python setup.py develop")
      run("ln -nfs {0} /u/apps/{1}/current".format(release_dir, app))
  
  def link_session_key(app, release_dir):
      run("ln -nfs /u/apps/{0}/shared/session_key.txt "
          "{1}/session_key.txt".format(app, release_dir))
  
  def dotenv(app, release_dir):
      run("ln -nfs /u/apps/{0}/shared/.env {1}/.env".format(app, release_dir))
  
  def restart(app):
      sudo("initctl restart " + app)
  
  @task
  def puppet():
      local('bundle exec cap production puppet:apply')
  
  apps = {
      'bloge': {
          'build': build_haskell,
          'hosts': ['bloge@andrewlorente.com'],
      },
      'andrewlorente': {
          'build': build_haskell,
          'hosts': ['andrewlorente@andrewlorente.com'],
          'extra': link_session_key,
      },
      'catsnap': {
          'build': build_python,
          'hosts': ['catsnap@andrewlorente.com'],
          'extra': dotenv,
      },
  }
  
  
subject: Include a .env file into catsnap's life