Skip to content
Newer
Older
100644 215 lines (175 sloc) 6.12 KB
60d4eee @ddollar initial
ddollar authored
1 require "digest/sha1"
caf27cb @ddollar get api key from heroku gem
ddollar authored
2 require "heroku/auth"
3 require "heroku/command"
4 require "heroku/command/base"
5 require "heroku/command/help"
6 require "heroku/plugin"
60d4eee @ddollar initial
ddollar authored
7 require "net/http/post/multipart"
8 require "rest_client"
9 require "thor"
10 require "tmpdir"
11 require "uri"
12 require "vulcan"
4920f72 @ddollar initial recipe support
ddollar authored
13 require "vulcan/recipe"
60d4eee @ddollar initial
ddollar authored
14 require "yaml"
15
16 class Vulcan::CLI < Thor
17
548e28e @ddollar remove COMMAND from banner
ddollar authored
18 desc "build", <<-DESC
c272dfc @geemus typo fixes
geemus authored
19 build a piece of software for the heroku cloud using COMMAND as a build command
60d4eee @ddollar initial
ddollar authored
20 if no COMMAND is specified, a sensible default will be chosen for you
21
22 DESC
23
24 method_option :command, :aliases => "-c", :desc => "the command to run for compilation"
7cafe88 @lpetre Better dependency support (using arrays)
lpetre authored
25 method_option :name, :aliases => "-n", :desc => "the name of the library (defaults to the directory name)"
60d4eee @ddollar initial
ddollar authored
26 method_option :output, :aliases => "-o", :desc => "output build artifacts to this file"
165e543 @ddollar tweak --prefix option help
ddollar authored
27 method_option :prefix, :aliases => "-p", :desc => "vulcan will look in this path for the compiled artifacts"
60d4eee @ddollar initial
ddollar authored
28 method_option :source, :aliases => "-s", :desc => "the source directory to build from"
7cafe88 @lpetre Better dependency support (using arrays)
lpetre authored
29 method_option :deps, :aliases => "-d", :desc => "urls of vulcan compiled libraries to build with", :type=>:array
60d4eee @ddollar initial
ddollar authored
30 method_option :verbose, :aliases => "-v", :desc => "show the full build output", :type => :boolean
31
32 def build
33 app = read_config[:app] || "need a server first, use vulcan create"
34
00aced6 @ddollar tweak readme
ddollar authored
35 source = options[:source] || Dir.pwd
36 name = options[:name] || File.basename(source)
60d4eee @ddollar initial
ddollar authored
37 output = options[:output] || "/tmp/#{name}.tgz"
38 prefix = options[:prefix] || "/app/vendor/#{name}"
d1667bf @ddollar put command in the right place
ddollar authored
39 command = options[:command] || "./configure --prefix #{prefix} && make install"
7cafe88 @lpetre Better dependency support (using arrays)
lpetre authored
40 deps = options[:deps] || []
15b90c5 @ddollar rewrite in coffeescript, include keepalive
ddollar authored
41 server = URI.parse(ENV["VULCAN_HOST"] || "http://#{app}.herokuapp.com")
60d4eee @ddollar initial
ddollar authored
42
43 Dir.mktmpdir do |dir|
15b90c5 @ddollar rewrite in coffeescript, include keepalive
ddollar authored
44 action "Packaging local directory" do
45 %x{ cd #{source} && tar czvf #{dir}/input.tgz . 2>&1 }
46 end
60d4eee @ddollar initial
ddollar authored
47
48 File.open("#{dir}/input.tgz", "r") do |input|
49 request = Net::HTTP::Post::Multipart.new "/make",
50 "code" => UploadIO.new(input, "application/octet-stream", "input.tgz"),
51 "command" => command,
00aced6 @ddollar tweak readme
ddollar authored
52 "prefix" => prefix,
24bf5ba @lpetre adding support for vulcan build -d <dep1>,...,<depN>
lpetre authored
53 "secret" => config[:secret],
54 "deps" => deps
60d4eee @ddollar initial
ddollar authored
55
15b90c5 @ddollar rewrite in coffeescript, include keepalive
ddollar authored
56 print "Uploading source package... "
60d4eee @ddollar initial
ddollar authored
57 response = Net::HTTP.start(server.host, server.port) do |http|
58 http.request(request) do |response|
59 response.read_body do |chunk|
15b90c5 @ddollar rewrite in coffeescript, include keepalive
ddollar authored
60 unless chunk == 0.chr + "\n"
61 print chunk if options[:verbose]
62 end
60d4eee @ddollar initial
ddollar authored
63 end
64 end
65 end
66
67 error "Unknown error, no build output given" unless response["X-Make-Id"]
68
69 puts ">> Downloading build artifacts to: #{output}"
70
635f6c0 @halorgium Print the URL where the output is available
halorgium authored
71 output_url = "#{server}/output/#{response["X-Make-Id"]}"
72 puts " (available at #{output_url})"
73
60d4eee @ddollar initial
ddollar authored
74 File.open(output, "w") do |output|
75 begin
635f6c0 @halorgium Print the URL where the output is available
halorgium authored
76 output.print RestClient.get(output_url)
60d4eee @ddollar initial
ddollar authored
77 rescue Exception => ex
78 puts ex.inspect
79 end
80 end
81 end
82 end
9f8e55e @ddollar gracefully handle ctrl-c
ddollar authored
83 rescue Interrupt
84 error "Aborted by user"
60d4eee @ddollar initial
ddollar authored
85 rescue Errno::EPIPE
86 error "Could not connect to build server: #{server}"
87 end
88
89 desc "create APP_NAME", <<-DESC
90 create a build server on Heroku
91
92 DESC
93
94 def create(name)
95 secret = Digest::SHA1.hexdigest("--#{rand(10000)}--#{Time.now}--")
96
97 Dir.mktmpdir do |dir|
98 Dir.chdir(dir) do
99 system "env BUNDLE_GEMFILE= heroku create #{name} -s cedar"
100 end
101 end
d1d38e1 @hone it's :app not :ap
hone authored
102 write_config :app => name, :host => "#{name}.herokuapp.com", :secret => secret
60d4eee @ddollar initial
ddollar authored
103 update
104 end
105
106
107 desc "update", <<-DESC
108 update the build server
109
110 DESC
111
112 def update
c272dfc @geemus typo fixes
geemus authored
113 error "no app yet, create first" unless config[:app]
60d4eee @ddollar initial
ddollar authored
114
caf27cb @ddollar get api key from heroku gem
ddollar authored
115 # clean up old plugin, can use auth:token now
116 FileUtils.rm_rf(File.expand_path("~/.heroku/plugins/heroku-credentials"))
60d4eee @ddollar initial
ddollar authored
117
118 Dir.mktmpdir do |dir|
119 Dir.chdir(dir) do
caf27cb @ddollar get api key from heroku gem
ddollar authored
120 Heroku::Plugin.load!
121 api_key = Heroku::Auth.api_key
122 error "invalid api key detected, try running `heroku auth:token`" if api_key =~ / /
60d4eee @ddollar initial
ddollar authored
123
124 system "git init"
6410c21 @ricaurte abstracted out the heroku domain that is used for git authentications…
ricaurte authored
125 system "git remote add heroku git@#{heroku_git_domain}:#{config[:app]}.git"
60d4eee @ddollar initial
ddollar authored
126 FileUtils.cp_r "#{server_path}/.", "."
127 File.open(".gitignore", "w") do |file|
128 file.puts ".env"
129 end
5f558b6 @ddollar adjust buildpack location after git remotes are set up
ddollar authored
130
e89ac09 @ddollar silent git
ddollar authored
131 system "git add . >/dev/null"
132 system "git commit -m commit >/dev/null"
60d4eee @ddollar initial
ddollar authored
133 system "git push heroku -f master"
134
a502766 @ddollar fix NODE_ENV, remove unused sessions
ddollar authored
135 heroku "config:add SECRET=#{config[:secret]} SPAWN_ENV=heroku HEROKU_APP=#{config[:app]} HEROKU_API_KEY=#{api_key} NODE_PATH=lib NODE_ENV=production"
15b90c5 @ddollar rewrite in coffeescript, include keepalive
ddollar authored
136 heroku "addons:add cloudant:oxygen"
60d4eee @ddollar initial
ddollar authored
137 end
138 end
139 end
140
4920f72 @ddollar initial recipe support
ddollar authored
141 desc "recipe", "build a recipe"
142
143 def recipe(recipe, prefix)
144 load recipe
145 Vulcan::Recipe.recipes.each do |recipe|
146 if options[:version]
147 version = recipe.versions[options[:version]] || raise("no such version: #{options[:version]}")
148 puts "compiling: #{version}"
149 version.build_version prefix
150 else
151 recipe.each_version do |version|
152 puts "compiling: #{version}"
153 version.build_version prefix
154 end
155 end
156 end
157 rescue StandardError => ex
158 puts ex.message
159 end
160
60d4eee @ddollar initial
ddollar authored
161 private
162
15b90c5 @ddollar rewrite in coffeescript, include keepalive
ddollar authored
163 def action(message)
164 print "#{message}... "
165 yield
166 puts "done"
167 end
168
169 def heroku(command)
170 %x{ env BUNDLE_GEMFILE= heroku #{command} 2>&1 }
171 end
172
60d4eee @ddollar initial
ddollar authored
173 def config_file
174 File.expand_path("~/.vulcan")
175 end
176
177 def config
178 read_config
179 end
180
181 def read_config
182 return {} unless File.exists?(config_file)
183 config = YAML.load_file(config_file)
184 config.is_a?(Hash) ? config : {}
185 end
186
187 def write_config(config)
188 full_config = read_config.merge(config)
189 File.open(config_file, "w") do |file|
190 file.puts YAML.dump(full_config)
191 end
192 end
193
194 def error(message)
195 puts "!! #{message}"
196 exit 1
197 end
198
199 def server_path
200 File.expand_path("../../../server", __FILE__)
201 end
7d347ed @ddollar dont ignore node_modules
ddollar authored
202
6410c21 @ricaurte abstracted out the heroku domain that is used for git authentications…
ricaurte authored
203 #
204 # heroku_git_domain checks to see if the heroku-accounts plugin is present,
205 # and if so, it will set the domain to the one that matches the credentials
206 # for the currently set account
207 #
208 def heroku_git_domain
209 suffix = %x{ git config heroku.account }
210 suffix = "com" if suffix.nil? or suffix.strip == ""
211 "heroku.#{suffix.strip}"
212 end
60d4eee @ddollar initial
ddollar authored
213
214 end
Something went wrong with that request. Please try again.