mbailey / deprec

master respository for deprec - deployment recipes for capistrano

This URL has Read+Write access

deprec / lib / deprec / recipes / git.rb
100644 98 lines (80 sloc) 3.238 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright 2006-2008 by Mike Bailey. All rights reserved.
Capistrano::Configuration.instance(:must_exist).load do
  namespace :deprec do
    namespace :git do
 
      set :git_user, 'git'
      set :git_group, 'git'
      set :git_port, '9418'
      set :git_keys_file, '/home/git/.ssh/authorized_keys'
      set :git_root, '/var/git'
 
      SRC_PACKAGES[:git] = {
        :url => "http://kernel.org/pub/software/scm/git/git-1.6.3.2.tar.gz",
        :md5sum => "c9819ba1ad3edbcf3b5de3116048d59f git-1.6.3.2.tar.gz"
      }
 
      desc "Install git"
      task :install do
        install_deps
        deprec2.download_src(SRC_PACKAGES[:git], src_dir)
        deprec2.install_from_src(SRC_PACKAGES[:git], src_dir)
        # create_git_root
        # create_git_user
      end
 
      # install dependencies for nginx
      task :install_deps do
        apt.install( {:base => %w(zlib1g-dev gettext libcurl4-gnutls-dev)}, :stable )
      end
      
      # "Start git server in local directory"
      task :serve do
        cmd = "git-daemon --verbose --port=#{git_port} --base-path=#{Dir.pwd} --base-path-relaxed"
        puts cmd
        `#{cmd}`
      end
      
      desc "Create git repos for current dir"
      task :init do
        `git init`
        create_gitignore
        create_files_in_empty_dirs
        `git add . && git commit -m 'initial import'`
      end
      
      task :create_gitignore do
        system("echo '.DS_Store' >> .gitignore") # files sometimes created by OSX
        system("echo 'log/*' >> .gitignore") if File.directory?('log')
        system("echo 'tmp/**/*' >> .gitignore") if File.directory?('tmp')
      end
      
      task :create_files_in_empty_dirs do
        %w(log tmp).each { |dir|
          system("touch #{dir}/.gitignore") if File.directory?(dir)
        }
      end
      
      desc "Create remote origin for current dir"
      task :create_remote_origin do
        File.directory?('.git') || init
         
        # Push to remote git repo
        hostname = capture "echo $CAPISTRANO:HOST$"
        system "git remote add origin git@#{hostname.chomp}:#{application}"
        system "git push origin master:refs/heads/master"
        
        puts
        puts "New remote Git repo: #{git_user}@#{hostname.chomp}:#{application}"
        puts
        
        # File.open('.git/config', 'w') do |c|
        # c.write 'Add the following to .git/config'
        # c.write '[branch "master"]'
        # c.write ' remote = origin'
        # c.write ' merge = refs/heads/master'
        # end
          
      end
 
      # Create root dir for git repositories
      task :create_git_root do
        deprec2.mkdir(git_root, :mode => 02775, :owner => git_user, :group => git_group, :via => :sudo)
        sudo "chmod -R g+w #{git_root}"
      end
      
      # regenerate git authorized keys file from users file in same dir
      task :regenerate_authorized_keys do
        sudo "echo '' > #{git_keys_file}"
        sudo "for file in `ls #{git_keys_file}-*`; do cat $file >> #{git_keys_file}; echo \"\n\" >> #{git_keys_file} ; done"
        sudo "chown #{git_user}.#{git_group} #{git_keys_file}"
        sudo "chmod 0600 #{git_keys_file}"
      end
 
 
    end
  end
end