-
Notifications
You must be signed in to change notification settings - Fork 479
/
ci.rake
110 lines (100 loc) · 3.85 KB
/
ci.rake
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
99
100
101
102
103
104
105
106
107
108
109
110
require_relative '../../deployment'
require 'cdo/chat_client'
require 'cdo/rake_utils'
require 'cdo/git_utils'
require 'tempfile'
namespace :ci do
# Synchronize the Chef cookbooks to the Chef repo for this environment using Berkshelf.
task :chef_update do
if CDO.chef_local_mode
# Update local cookbooks from repository in local mode.
ChatClient.log 'Updating local <b>chef</b> cookbooks...'
RakeUtils.with_bundle_dir(cookbooks_dir) do
Tempfile.open(['berks', '.tgz']) do |file|
RakeUtils.bundle_exec "berks package #{file.path}"
RakeUtils.sudo "tar xzf #{file.path} -C /var/chef"
end
end
elsif CDO.daemon && CDO.chef_managed
ChatClient.log('Updating Chef cookbooks...')
RakeUtils.with_bundle_dir(cookbooks_dir) do
# Automatically update Chef cookbook versions in staging environment.
RakeUtils.bundle_exec './update_cookbook_versions' if rack_env?(:staging)
RakeUtils.bundle_exec 'berks', 'install'
if rack_env?(:staging) && GitUtils.file_changed_from_git?(cookbooks_dir)
RakeUtils.system 'git', 'add', '.'
RakeUtils.system 'git', 'commit', '-m', '"Updated cookbook versions"'
RakeUtils.git_push
end
RakeUtils.bundle_exec 'berks', 'upload', (rack_env?(:production) ? '' : '--no-freeze')
RakeUtils.bundle_exec 'berks', 'apply', rack_env
end
ChatClient.log 'Applying <b>chef</b> profile...'
RakeUtils.sudo '/opt/chef/bin/chef-client'
end
end
# Perform a normal local build by calling the top-level Rakefile.
# Additionally run the lint task if specified for the environment.
task build: [:chef_update] do
Dir.chdir(deploy_dir) do
ChatClient.wrap('rake lint') {Rake::Task['lint'].invoke} if CDO.lint
ChatClient.wrap('rake build') {Rake::Task['build'].invoke}
end
end
multitask deploy_multi: [:deploy_console, :deploy_stack]
task :deploy_stack do
ChatClient.wrap('CloudFormation stack update') do
RakeUtils.system_stream_output 'QUIET=1 bundle exec rake stack:start' do |io|
io.each do |line|
line = "[stack update] #{line.chomp}"
ChatClient.log line
CDO.log.info(line) if CDO.hip_chat_logging
end
end
end
end
task :deploy_console do
if rack_env?(:production) && (console = CDO.app_servers['console'])
upgrade_frontend 'console', console
end
end
desc 'Publish a new tag and release to GitHub'
task :publish_github_release do
begin
RakeUtils.system "bin/create-release --force"
ChatClient.log '<a href="https://github.com/code-dot-org/code-dot-org/releases/latest">New release created</a>'
rescue RuntimeError => e
ChatClient.log 'Failed to create a new release.', color: 'red'
ChatClient.log "/quote #{e.message}\n#{CDO.backtrace e}", message_format: 'text', color: 'red'
end
end
all_tasks = []
all_tasks << 'firebase:ci'
all_tasks << :build
all_tasks << :deploy_multi
all_tasks << :publish_github_release if rack_env?(:production)
task all: all_tasks
task test: [
:all,
'test:ci'
]
end
desc 'Update the server as part of continuous integration.'
task :ci do
ChatClient.wrap('CI build', backtrace: true) {Rake::Task[rack_env?(:test) ? 'ci:test' : 'ci:all'].invoke}
end
# Returns true if upgrade succeeded, false if failed.
def upgrade_frontend(name, hostname)
ChatClient.log "Upgrading <b>#{name}</b> (#{hostname})..."
command = 'sudo /opt/chef/bin/chef-client'
log_path = aws_dir "deploy-#{name}.log"
begin
RakeUtils.system "ssh -i ~/.ssh/deploy-id_rsa #{hostname} '#{command} 2>&1' >> #{log_path}"
ChatClient.log "Upgraded <b>#{name}</b> (#{hostname})."
true
rescue RuntimeError
ChatClient.log "<b>#{name}</b> (#{hostname}) failed to upgrade.", color: 'red'
ChatClient.log "/quote #{File.read(log_path)}"
false
end
end