Class: Vulcan::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/vulcan/cli.rb

Instance Method Summary collapse

Instance Method Details

#buildObject



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
# File 'lib/vulcan/cli.rb', line 31

def build
  app = read_config[:app] || "need a server first, use vulcan create"

  source  = options[:source]  || Dir.pwd
  name    = options[:name]    || File.basename(source, File.extname(source))
  output  = options[:output]  || "/tmp/#{name}.tgz"
  prefix  = options[:prefix]  || "/app/vendor/#{name}"
  command = options[:command] || "./configure --prefix #{prefix} && make install"
  deps    = options[:deps]    || []
  server  = URI.parse(ENV["VULCAN_HOST"] || "http://#{app}.herokuapp.com")

  source_is_url = URI.parse(source).scheme

  Dir.mktmpdir do |dir|
    unless source_is_url
      input_tgz = "#{dir}/input.tgz"
      if File.directory?(source)
        action "Packaging local directory" do
          %x{ cd #{source} && tar czvf #{input_tgz} . 2>&1 }
        end
      else
        input_tgz = source
      end
      input = File.open(input_tgz, "r")
    end

    make_options = {
      "command" => command,
      "prefix"  => prefix,
      "secret"  => config[:secret],
      "deps"    => deps
    }

    if source_is_url
      make_options["code_url"] = source
    else
      make_options["code"] = UploadIO.new(input, "application/octet-stream", "input.tgz")
    end

    request = Net::HTTP::Post::Multipart.new "/make", make_options

    if source_is_url
      print "Initializing build... "
    else
      print "Uploading source package... "
    end

    response = Net::HTTP.start(server.host, server.port) do |http|
      http.request(request) do |response|
        response.read_body do |chunk|
          unless chunk == 0.chr + "\n"
            print chunk if options[:verbose]
          end
        end
      end
    end

    error "Unknown error, no build output given" unless response["X-Make-Id"]

    puts ">> Downloading build artifacts to: #{output}"

    output_url = "#{server}/output/#{response["X-Make-Id"]}"
    puts "   (available at #{output_url})"

    File.open(output, "w") do |output|
      begin
        output.print RestClient.get(output_url)
      rescue Exception => ex
        puts ex.inspect
      end
    end
  end
rescue Interrupt
  error "Aborted by user"
rescue Errno::EPIPE
  error "Could not connect to build server: #{server}"
end

#create(name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/vulcan/cli.rb', line 114

def create(name)
  secret = Digest::SHA1.hexdigest("--#{rand(10000)}--#{Time.now}--")

  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      system "env BUNDLE_GEMFILE= heroku create #{name} -s cedar"
    end
  end
  write_config :app => name, :host => "#{name}.herokuapp.com", :secret => secret
  update
end

#updateObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/vulcan/cli.rb', line 132

def update
  error "no app yet, create first" unless config[:app]

  # clean up old plugin, can use auth:token now
  FileUtils.rm_rf(File.expand_path("~/.heroku/plugins/heroku-credentials"))

  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      Heroku::Plugin.load!
      api_key = Heroku::Auth.api_key
      error "invalid api key detected, try running `heroku auth:token`" if api_key =~ / /

      system "git init"
      system "git remote add heroku git@#{heroku_git_domain}:#{config[:app]}.git"
      FileUtils.cp_r "#{server_path}/.", "."
      File.open(".gitignore", "w") do |file|
        file.puts ".env"
      end

      system "git add . >/dev/null"
      system "git commit -m commit >/dev/null"
      system "git push heroku -f master"

      heroku "config:add SECRET=#{config[:secret]} SPAWN_ENV=heroku HEROKU_APP=#{config[:app]} HEROKU_API_KEY=#{api_key} NODE_PATH=lib NODE_ENV=production"
      heroku "addons:add cloudant:oxygen"
    end
  end
end