This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
plugin.rb
132 lines (111 loc) · 3.6 KB
/
plugin.rb
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require 'bundler'
require 'vcap/staging/plugin/rails3/database_support'
require "uuidtools"
require_relative("buildpack_installer")
class BuildpackPlugin < StagingPlugin
include RailsDatabaseSupport
def stage_application
Dir.chdir(destination_directory) do
create_app_directories
copy_source_files
Bundler.with_clean_env do
build_pack.compile
end
stage_rails_console if rails_buildpack?
create_startup_script
end
end
def clone_buildpack(buildpack_url)
buildpack_path = "#{app_dir}/.buildpacks/#{File.basename(buildpack_url)}"
ok = system("git clone #{buildpack_url} #{buildpack_path}")
raise "Failed to git clone buildpack" unless ok
BuildpackInstaller.new(Pathname.new(buildpack_path), app_dir)
end
def build_pack
return @build_pack if @build_pack
custom_url = environment[:buildpack]
return @build_pack = clone_buildpack(custom_url) if custom_url
@build_pack = installers.detect(&:detect)
raise "Unable to detect a supported application type" unless @build_pack
@build_pack
end
def buildpacks_path
Pathname.new(__FILE__) + '../../../../../../vendor/buildpacks/'
end
def installers
buildpacks_path.children.map do |buildpack|
BuildpackInstaller.new(buildpack, app_dir)
end
end
def start_command
return environment[:meta][:command] if environment[:meta] && environment[:meta][:command]
procfile["web"] ||
release_info.fetch("default_process_types", {})["web"] ||
raise("Please specify a web start command in your manifest.yml or Procfile")
end
def procfile
@procfile ||= procfile_contents ? YAML.load(procfile_contents) : {}
raise "Invalid Procfile format. Please ensure it is a valid YAML hash" unless @procfile.kind_of?(Hash)
@procfile
end
def procfile_contents
procfile_path = "#{app_dir}/Procfile"
File.read(procfile_path) if File.exists?(procfile_path)
end
# TODO - remove this when we have the ability to ssh to a locally-running console
def rails_buildpack?
@build_pack.name == "Ruby/Rails"
end
def stage_rails_console
#Copy cf-rails-console to app
cf_rails_console_dir = app_dir + '/cf-rails-console'
FileUtils.mkdir_p(cf_rails_console_dir)
FileUtils.cp_r(File.expand_path('../resources/cf-rails-console', __FILE__), app_dir)
#Generate console access file for caldecott access
config_file = cf_rails_console_dir + '/.consoleaccess'
data = {'username' => UUIDTools::UUID.random_create.to_s,'password' => UUIDTools::UUID.random_create.to_s}
File.open(config_file, 'w') do |fh|
fh.write(YAML.dump(data))
end
end
def startup_script
generate_startup_script(environment_variables) do
script_content = <<-BASH
unset GEM_PATH
if [ -d app/.profile.d ]; then
for i in app/.profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
env > logs/env.log
BASH
if rails_buildpack?
script_content += <<-BASH
if [ -n "$VCAP_CONSOLE_PORT" ]; then
cd app
bundle exec ruby cf-rails-console/rails_console.rb >> ../logs/console.log 2>> ../logs/console.log &
CONSOLE_STARTED=$!
echo "$CONSOLE_STARTED" >> ../console.pid
cd ..
fi
BASH
end
script_content
end
end
def release_info
build_pack.release_info
end
def environment_variables
vars = release_info['config_vars'] || {}
vars.each { |k, v| vars[k] = "${#{k}:-#{v}}" }
vars["HOME"] = "$PWD/app"
vars["PORT"] = "$VCAP_APP_PORT"
vars["DATABASE_URL"] = database_uri if rails_buildpack? && bound_database
vars["MEMORY_LIMIT"] = "#{application_memory}m"
vars
end
end