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



59
60
61
62
63
# File 'lib/bard.rb', line 59

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

#ciObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/bard.rb', line 136

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



50
51
52
53
54
55
# File 'lib/bard.rb', line 50

def create(project_name)
  auto_update!
  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



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

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)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bard.rb', line 110

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(project_name) ⇒ Object



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

def install(project_name)
  auto_update!
  command = <<-BASH
  git clone [email protected]:#{project_name}.git

  cd #{project_name}
  git checkout integration
  rvm . do bundle
  rvm . do bundle exec rake bootstrap

  sudo -s <#{"<EOF"}
    echo "<VirtualHost *:80>
      ServerName #{project_name}.local
      DocumentRoot `pwd`/public
</VirtualHost>" > /etc/apache2/sites-available/#{project_name}
    a2ensite #{project_name}
    apache2ctl restart

    if ! grep "#{project_name}.local" /etc/hosts; then
      echo 127.0.0.1 #{project_name}.local >> /etc/hosts
    fi
EOF
  BASH
  exec command
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



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

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)


88
89
90
91
92
93
94
95
96
# File 'lib/bard.rb', line 88

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



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

def stage
  invoke :push

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

  puts green("Stage Succeeded")
end