Class: Vulcan::CLI

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

Instance Method Summary collapse

Instance Method Details

#buildObject



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

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

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

  Dir.mktmpdir do |dir|
    puts ">> Packaging local directory"
    %x{ cd #{source} && tar czvf #{dir}/input.tgz . 2>&1 }

    puts ">> Uploading code for build"
    File.open("#{dir}/input.tgz", "r") do |input|
      request = Net::HTTP::Post::Multipart.new "/make",
        "code" => UploadIO.new(input, "application/octet-stream", "input.tgz"),
        "command" => command,
        "prefix" => prefix

      puts ">> Building with: #{command}"
      response = Net::HTTP.start(server.host, server.port) do |http|
        http.request(request) do |response|
          response.read_body do |chunk|
            print chunk if options[:verbose]
          end
        end
      end

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

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

      File.open(output, "w") do |output|
        begin
          output.print RestClient.get("#{server}/output/#{response["X-Make-Id"]}")
        rescue Exception => ex
          puts ex.inspect
        end
      end
    end
  end
rescue Errno::EPIPE
  error "Could not connect to build server: #{server}"
end

#create(name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vulcan/cli.rb', line 77

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 :ap=> name, :host => "#{name}.herokuapp.com", :secret => secret
  update
end

#updateObject



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

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

  FileUtils.mkdir_p File.expand_path("~/.heroku/plugins/heroku-credentials")

  File.open(File.expand_path("~/.heroku/plugins/heroku-credentials/init.rb"), "w") do |file|
    file.puts <<-CONTENTS
      class Heroku::Auth
        def self.api_key
          Heroku::Client.auth(user, password, host)["api_key"]
        end
      end
      class Heroku::Command::Credentials < Heroku::Command::Base
        def index
          puts Heroku::Auth.api_key
        end
      end
    CONTENTS
  end

  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      api_key = %x{ env BUNDLE_GEMFILE= heroku credentials }

      system "git init"
      system "git remote add heroku [email protected]:#{config[:app]}.git"
      FileUtils.cp_r "#{server_path}/.", "."
      File.open(".gitignore", "w") do |file|
        file.puts ".env"
        file.puts "node_modules"
      end
      system "git add . >/dev/null"
      system "git commit -m commit >/dev/null"
      system "git push heroku -f master"

      %x{ env BUNDLE_GEMFILE= heroku config:add SECRET=#{config[:secret]} SPAWN_ENV=heroku HEROKU_APP=#{config[:app]} HEROKU_API_KEY=#{api_key} 2>&1 }
      %x{ env BUNDLE_GEMFILE= heroku addons:add cloudant:oxygen }
    end
  end
end