public
Fork of keithpitty/bivouac
Description: Railscamp deployment tool
Homepage:
Clone URL: git://github.com/martinstannard/bivouac.git
bivouac / helpers.rb
100644 80 lines (65 sloc) 1.84 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require 'fileutils'
 
helpers do
 
  include Sinatra::Authorization
 
  def create_site(site)
    cat_key(site)
    init_repo(site)
    add_post_commit(site)
  end
 
  def init_repo(site)
    FileUtils.mkdir_p File.join(site.directory, 'tmp')
    `touch #{File.join(site.directory, 'tmp')}/never-deployed`
    FileUtils.mkdir_p File.join(site.directory, 'public')
    current_dir = Dir.getwd
    Dir.chdir site.directory
    `git init`
    Dir.chdir current_dir
    `ln -s #{site.directory} #{File.join(SITE_ROOT, site.name)}`
  end
 
  def add_post_commit(site)
    post_commit = File.join(site.directory, '.git/hooks/post-receive')
    File.open(post_commit, 'w') do |f|
      f.write <<-HERE
#!/bin/sh
cd #{site.directory};
git reset --hard;
# install any gems required
if [ -f geminstaller.yml ] || [ -f config/geminstaller.yml ]
geminstaller
fi
# run initial_deploy_hook here unless /tmp/deployed exists
# initial_deploy should be created in site.directory/deploy-hooks
 
if [ -x deploy-hooks/initial-deploy ] && [ -f tmp/never-deployed ]
then
deploy-hooks/initial-deploy;
rm #{site.directory}/tmp/never-deployed;
fi
 
# post_deploy_hook should be created in site.directory/deploy
 
if [ -x deploy-hooks/post-receive ]
then
deploy-hooks/post-receive;
fi
 
cd #{site.directory} && git --git-dir=`pwd`/.git reset --hard;
touch #{site.directory}/tmp/restart.txt;
HERE
      f.chmod(0775)
    end
  end
 
  def cat_key(site)
    File.open("#{ENV['HOME']}/.ssh/authorized_keys2", 'a') do |f|
      f << site.ssh_public_key << "\n"
    end
  end
 
  def authorization_realm
    'bivou.ac'
  end
 
  def authorize(login, password)
    login == "bivvy" && password == "whackers"
  end
 
  # this uses the gemtools gem to install any gems
  def install_gems
    Dir.chdir site.directory
    `geminstaller`
    Dir.chdir current_dir
  end
 
end