public
Description: Continuous Integration. In a Box!
Homepage: http://github.com/thewoolleyman/cinabox/tree/master/README.txt
Clone URL: git://github.com/thewoolleyman/cinabox.git
cinabox / setup_ci.rb
100755 97 lines (82 sloc) 3.74 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
#!/usr/bin/env ruby
 
class Cinabox
  def self.setup
    require 'fileutils'
    require 'socket'
 
    # Settings
    cruise_user = ENV['CRUISE_USER'] || ENV['USER']
    cruise_home = ENV['CRUISE_HOME'] || "#{ENV['HOME']}/ccrb"
    cruise_branch = ENV['CRUISE_BRANCH'] || "git://github.com/thoughtworks/cruisecontrol.rb.git"
    # cruise_tag = ENV['CRUISE_TAG']
    cruise_tag = ENV['CRUISE_TAG']
    cinabox_dir = File.expand_path(File.dirname(__FILE__))
    cruise_daemon_template = ENV['CRUISE_DAEMON_TEMPLATE'] || "#{cruise_home}/daemon/cruise"
    hostname = ENV['HOSTNAME']
    
    # set hostname
    run "sudo echo '#{hostname}' > /etc/hostname" if hostname
    
    # Build/download dir
    build_dir = ENV['BUILD_DIR'] || "#{ENV['HOME']}/build"
    FileUtils.mkdir_p(build_dir)
 
    # warning - the '--force' option will blow away any existing settings
    force = ARGV[0] == '--force' ? true : false
 
    # Install important packages
    run "sudo aptitude install -y subversion" if !((run "dpkg -l subversion", false) =~ /ii subversion/) || force
    run "sudo aptitude install -y git-core" if !((run "dpkg -l git-core", false) =~ /ii git-core/) || force
    run "sudo aptitude install -y git-svn" if !((run "dpkg -l git-svn", false) =~ /ii git-svn/) || force
 
    # Install cruisecontrol.rb via git and dependencies
    if !File.exist?(cruise_home) || force
      run "rm -rf #{cruise_home}"
      run "git clone #{cruise_branch} #{cruise_home}"
      run "cd #{cruise_home} && git checkout -b #{cruise_tag} refs/tags/#{cruise_tag}" if cruise_tag
      run "sudo gem install --bindir /usr/bin rake rack thin"
      unless RUBY_VERSION =~ /^1.9/
        run "sudo gem install --bindir /usr/bin mongrel mongrel_cluster"
      end
    end
 
    # Always update cruisecontrol.rb
    # TODO: disabled for now, it will break if we are on a tag
    # run "cd #{cruise_home} && git pull"
    
    # Write out init script daemon based on template
    if !File.exist?('/etc/init.d/cruise') || force
      run "sudo rm -f /etc/init.d/cruise"
      run "sudo touch /etc/init.d/cruise"
      run "sudo chown #{cruise_user} /etc/init.d/cruise"
      run "chmod a+x /etc/init.d/cruise"
      File.open(cruise_daemon_template, "r") do |input|
        File.open("/etc/init.d/cruise", "w") do |output|
          input.each_line do |line|
            line = "CRUISE_USER = '#{cruise_user}'\n" if line =~ /CRUISE_USER =/
            line = "CRUISE_HOME = '#{cruise_home}'\n" if line =~ /CRUISE_HOME =/
            output.print(line)
          end
        end
      end
    end
    
    # Enable on system reboot
    if !File.exist?('/etc/rc3.d/S20cruise') || force
      run "sudo update-rc.d -f cruise defaults"
    end
    
    # Install and configure postfix
    if !((run "dpkg -l postfix", false) =~ /ii postfix/) || force
      run "sudo aptitude install debconf-utils -y"
      run "echo 'postfix\tpostfix/mailname\tstring\t#{Socket.gethostbyname(Socket.gethostname)[0]}' > #{build_dir}/postfix-selections"
      run "echo 'postfix\tpostfix/main_mailer_type\tselect\tInternet Site' >> #{build_dir}/postfix-selections"
      run "sudo debconf-set-selections #{build_dir}/postfix-selections"
      run "sudo aptitude install postfix -y"
    end
    
    # TODO: when run via 'su -l ci' from another user, this doesn't always drop a pid file, even though it starts
    run "/etc/init.d/cruise start" unless ENV['NO_DAEMON_START']
 
    print "\n\nSetup script completed.\n"
  end
  
  def self.run(cmd, fail_on_error = true)
    puts "Running command: #{cmd}"
    output = `#{cmd}`
    puts output
    if !$?.success? and fail_on_error
      print "\n\nCommand failed: #{cmd}\n"
      exit $?.to_i
    end
    output
  end
end
 
Cinabox.setup