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

Defined Under Namespace

Classes: TestsAbortedError, TestsFailedError

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



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

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

#ciObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bard.rb', line 112

def ci
  return unless has_ci?

  puts "Continuous integration: starting build..."
  last_build_number = get_last_build_number
  last_time_elapsed = get_last_time_elapsed
  start_ci
  sleep(2) while last_build_number == get_last_build_number

  start_time = Time.new.to_i
  while (response = `curl -s #{ci_host}/lastBuild/api/xml?token=botandrose`).include? "<building>true</building>"
    elapsed_time = Time.new.to_i - start_time
    percentage = (elapsed_time.to_f / last_time_elapsed.to_f * 100).to_i
    output = "  Estimated completion: #{percentage}%"
    print "\x08" * output.length
    print output
    $stdout.flush
    sleep(2)
  end
  puts

  case response
    when /<result>FAILURE<\/result>/ then 
      puts
      puts `curl -s #{ci_host}/lastBuild/console?token=botandrose`.match(/<pre>(.+)<\/pre>/m)[1]
      puts
      raise TestsFailedError, "#{ci_host}/#{get_last_build_number}/console"

    when /<result>ABORTED<\/result>/ then 
      raise TestsAbortedError, "#{ci_host}/#{get_last_build_number}/console"

    when /<result>SUCCESS<\/result>/ then
      puts "Continuous integration: success! deploying to production"

    else raise "Unknown response from CI server:\n#{response}"
  end
end

#create(project_name) ⇒ Object



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

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

#data(role = "production") ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/bard.rb', line 42

def data(role = "production")
  ensure_sanity!(true)
  if role == "production" and heroku?
    exec "heroku db:pull --confirm #{project_name}"
  else
    exec "cap data:pull ROLES=#{role}"
  end
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)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bard.rb', line 86

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"

  invoke :ci

  if heroku?
    run_crucial "git push production", options.verbose?
    run_crucial "heroku run rake bootstrap:production:post", options.verbose?
  else
    run_crucial "cap deploy", options.verbose?
  end

  puts green("Deploy Succeeded")
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



53
54
55
56
57
58
59
60
# File 'lib/bard.rb', line 53

def pull
  ensure_sanity!

  warn NonFastForwardError unless fast_forward_merge?("origin/#{current_branch}")

  run_crucial "git pull --rebase origin #{current_branch}", options.verbose?
  run_crucial "bundle && bundle exec rake bootstrap", options.verbose?
end

#pushObject

Raises:

  • (SubmoduleDirtyError)


64
65
66
67
68
69
70
71
72
# File 'lib/bard.rb', line 64

def push
  ensure_sanity!

  raise SubmoduleDirtyError if submodule_dirty?
  raise SubmoduleUnpushedError if submodule_unpushed?
  raise NonFastForwardError unless fast_forward_merge?("origin/#{current_branch}")

  run_crucial "git push origin #{current_branch}", true
end

#stageObject



76
77
78
79
80
81
82
# File 'lib/bard.rb', line 76

def stage
  invoke :push

  run_crucial "cap stage BRANCH=#{current_branch}", options.verbose?

  puts green("Stage Succeeded")
end