Class: Bard

Inherits:
Thor
  • Object
show all
Includes:
BardGit, BardIO
Defined in:
lib/bard.rb,
lib/bard/check.rb,
lib/bard/error.rb,
lib/bard/ssh_delegation.rb

Constant Summary collapse

VERSION =
File.read(File.expand_path(File.dirname(__FILE__) + "../../VERSION")).chomp

Instance Method Summary collapse

Instance Method Details

#check(project_path = nil) ⇒ Object



33
34
35
36
37
# File 'lib/bard.rb', line 33

def check(project_path = nil)
  project_path = "." if project_path.nil? and File.directory? ".git" and File.exist? "config/environment.rb"
  check_dependencies
  check_project project_path if project_path
end

#create(project_name) ⇒ Object



26
27
28
29
30
# File 'lib/bard.rb', line 26

def create(project_name)
  template_path = File.expand_path(File.dirname(__FILE__) + "/bard/template.rb")
  command = "rails --template=#{template_path} #{project_name}"
  exec command
end

#dataObject



40
41
42
# File 'lib/bard.rb', line 40

def data
  exec "cap data:pull"
end

#delegate(key) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bard/ssh_delegation.rb', line 14

def delegate(key)
  command = ENV['SSH_ORIGINAL_COMMAND']

  case command
  when /^scp -f (\w+)\.sql\.gz$/
    project = $1
    `#{command_for("staging", "cd #{project} && rake db:dump RAILS_ENV=staging && gzip -9f db/data.sql")}`
    command = "scp -f ~/#{project}/db/data.sql.gz"
  end

  exec command_for("staging", command)
end

#deployObject

Raises:

  • (MasterNonFastForwardError)


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
# File 'lib/bard.rb', line 69

def deploy
  invoke :push

  run_crucial "git fetch origin"
  run_crucial "git checkout master"
  run_crucial "git pull --rebase origin master"
  raise MasterNonFastForwardError if not fast_forward_merge? "master", "integration"

  run_crucial "git merge integration"
  run_crucial "git push origin master"
  run_crucial "git checkout integration"

  if `curl -s -I http://integrity.botandrose.com/#{project_name}` !~ /\b404\b/
    puts "Integrity: verifying build..."
    system "curl -sX POST http://integrity.botandrose.com/#{project_name}/builds"
    while true
      response = `curl -s http://integrity.botandrose.com/#{project_name}`
      break unless response =~ /div class='(building|pending)' id='last_build'/
      sleep(2)
    end
    case response
      when /div class='failed' id='last_build'/ then raise TestsFailedError
      when /div class='success' id='last_build'/ then puts "Integrity: success! deploying to production"
      else raise "Unknown response from CI server:\n#{response}"
    end
  end

  run_crucial_via_bard "cap deploy"
end

#install_authorized_keys(source_user, dest_user) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/bard/ssh_delegation.rb', line 3

def install_authorized_keys(source_user, dest_user)
  source = "/home/#{source_user}/.ssh/authorized_keys"
  dest = "/home/#{dest_user}/.ssh/authorized_keys"

  file = File.read(source)
  file.gsub! /gitosis-serve/, "bard delegate"

  File.open(dest, "w") { |f| f.write(file) }
end

#pullObject



45
46
47
48
49
50
51
52
53
# File 'lib/bard.rb', line 45

def pull
  ensure_sanity!

  warn NonFastForwardError unless fast_forward_merge?

  run_crucial "git pull --rebase origin integration"

  prepare_environment changed_files(@common_ancestor, "origin/integration")
end

#pushObject

Raises:

  • (SubmoduleDirtyError)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bard.rb', line 56

def push
  ensure_sanity!

  raise SubmoduleDirtyError if submodule_dirty?
  raise SubmoduleUnpushedError if submodule_unpushed?
  raise NonFastForwardError unless fast_forward_merge?

  run_crucial "git push origin integration", true
  
  run_crucial_via_bard "bard stage"
end

#stageObject



100
101
102
103
104
105
106
107
# File 'lib/bard.rb', line 100

def stage
  check_dependencies

  ENV['RAILS_ENV'] = "staging"
  run_crucial "git fetch"
  run_crucial "git reset --hard origin/integration"
  prepare_environment
end