Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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