初めてのHeroku
と、いう事で開発環境も整ったので
Ruby on Rails向けPaaS「Heroku」って、こんな感じです(スクリーンキャスト):Rails Hub情報局:エンジニアライフをトレースしてみる。
Herokuへのユーザ登録は記憶にないけど、いつのまにか終ってます。
gemsetをつくる
Heroku専用のgemsetがあった方がいいよね。たぶん。
satsuki:myapp hana-da$ rvm list rvm rubies => ruby-1.8.7-p352 [ x86_64 ] ruby-1.9.2-p290 [ x86_64 ] satsuki:apps hana-da$ rvm gemset use heroku
もう慣れたぜ。へへへ。
satsuki:apps hana-da$ gem list *** LOCAL GEMS *** bundler (1.0.18) rake (0.9.2)
@globalにbundlerを入れてるので いい感じ
で、Powがイカスらしいので試してみる - 日々此妄想の時に作ったmyappにGemfileがあるのでそれでbundle install
satsuki:~ hana-da$ cd Documents/Development/Rails/myapp/ satsuki:myapp hana-da$ bundle install Fetching source index for http://rubygems.org/ Using rake (0.9.2) Installing abstract (1.0.0) WARNING: abstract-1.0.0 has an invalid nil value for @cert_chain Installing activesupport (3.0.10) Installing builder (2.1.2) WARNING: builder-2.1.2 has an invalid nil value for @cert_chain Installing i18n (0.5.0) : Installing rails (3.0.10) Installing sqlite3 (1.3.4) with native extensions Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
あーなんか、すっげー今風。すげーすげー。
で、Pow用のおまじない
satsuki:myapp hana-da$ echo "rvm 1.8.7@heroku" > .rvmrc
これで下準備完了
herokuコマンドの導入
gem install heroku
satsuki:myapp hana-da$ gem install heroku Fetching: term-ansicolor-1.0.6.gem (100%) Fetching: rest-client-1.6.3.gem (100%) Fetching: addressable-2.2.6.gem (100%) Fetching: launchy-2.0.5.gem (100%) Fetching: heroku-2.4.2.gem (100%) Successfully installed term-ansicolor-1.0.6 Successfully installed rest-client-1.6.3 Successfully installed addressable-2.2.6 Successfully installed launchy-2.0.5 Successfully installed heroku-2.4.2 5 gems installed
さくさく行くね。
ちゃんと入ったかな
satsuki:myapp hana-da$ heroku version heroku-gem/2.4.2
桶々
gitリポジトリの作成
git init
satsuki:myapp hana-da$ git init Initialized empty Git repository in /Users/hana-da/Documents/Development/Rails/myapp/.git/
git add
satsuki:myapp hana-da$ git add .
そういや .gitignore が標準装備だっけ?
satsuki:myapp hana-da$ cat .gitignore .bundle db/*.sqlite3 log/*.log tmp/
ほほう。
で、git commit
satsuki:myapp hana-da$ git commit -am 'first commit' [master (root-commit) 5bb485c] first commit 55 files changed, 10387 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 .rvmrc create mode 100644 Gemfile create mode 100644 Gemfile.lock : create mode 100644 vendor/plugins/.gitkeep
おっしゃ。あとは heroku にデプロイするんだな。
heroku create
heroku側にリポジトリ(?)を作る
satsuki:myapp hana-da$ heroku create myapp Enter your Heroku credentials. Email: heroku@example.com Password: ! Name is already taken
お決りの Name is already taken いただきました。シナリオ通りです。
てか、鍵の登録聞かれなかったな。。。記憶がない時にやったんだっけ。やった気がするな。。。どうやってやったんだっけ??? 記憶にない。。。まぁいいか。
名前を変えて再登録
satsuki:myapp hana-da$ heroku create myapp368934 Creating myapp368934... done, stack is bamboo-mri-1.9.2 http://myapp368934.heroku.com/ | git@heroku.com:myapp368934.git
わーい。
open http://myapp368934.heroku.com してみる
おおおお。
https://api.heroku.com/myappsでも確認した。
git remoteは
satsuki:myapp hana-da$ git remote -v heroku git@heroku.com:myapp368934.git (fetch) heroku git@heroku.com:myapp368934.git (push)
登録済。ステキ
いよいよ git push
いきまーす。
satsuki:myapp hana-da$ git push heroku master Counting objects: 86, done. Delta compression using up to 4 threads. Compressing objects: 100% (68/68), done. Writing objects: 100% (86/86), 88.35 KiB, done. Total 86 (delta 2), reused 0 (delta 0) -----> Heroku receiving push -----> Ruby/Rails app detected -----> Detected Rails is not set to serve static_assets Installing rails3_serve_static_assets... done -----> Configure Rails 3 to disable x-sendfile Installing rails3_disable_x_sendfile... done -----> Configure Rails to log to stdout Installing rails_log_stdout... done -----> Gemfile detected, running Bundler version 1.0.7 Unresolved dependencies detected; Installing... Using --without development:test Fetching source index for http://rubygems.org/ Installing rake (0.9.2) Installing abstract (1.0.0) Installing activesupport (3.0.10) Installing builder (2.1.2) Installing i18n (0.5.0) Installing activemodel (3.0.10) Installing erubis (2.6.6) Installing rack (1.2.3) Installing rack-mount (0.6.14) Installing rack-test (0.5.7) Installing tzinfo (0.3.29) Installing actionpack (3.0.10) Installing mime-types (1.16) Installing polyglot (0.3.2) Installing treetop (1.4.10) Installing mail (2.2.19) Installing actionmailer (3.0.10) Installing arel (2.0.10) Installing activerecord (3.0.10) Installing activeresource (3.0.10) Using bundler (1.0.7) Installing rdoc (3.9.2) Installing thor (0.14.6) Installing railties (3.0.10) Installing rails (3.0.10) Installing sqlite3 (1.3.4) with native extensions Your bundle is complete! It was installed into ./.bundle/gems/ -----> Compiled slug size is 4.1MB -----> Launching... done, v4 http://myapp368934.heroku.com deployed to Heroku To git@heroku.com:myapp368934.git * [new branch] master -> master
いった。
確認するするー heroku open てやればブラウザが立ち上がるはず
satsuki:myapp hana-da$ heroku open Opening http://myapp368934.heroku.com/
heroku で db:migrate
db:migrate忘れてたよ。てへっ。との事なので heroku rake db:migrate する
satsuki:myapp hana-da$ heroku rake db:migrate (in /app) rake aborted! uninitialized constant Rake::DSL /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2482:in `const_missing' /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:8:in `<class:TaskLib>' /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:6:in `<module:Rake>' /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:3:in `<top (required)>' /app/.bundle/gems/ruby/1.9.1/gems/rdoc-3.9.2/lib/rdoc/task.rb:37:in `require' /app/.bundle/gems/ruby/1.9.1/gems/rdoc-3.9.2/lib/rdoc/task.rb:37:in `<top (required)>' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks/documentation.rake:2:in `require' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks/documentation.rake:2:in `<top (required)>' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks.rb:15:in `load' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks.rb:15:in `block in <top (required)>' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks.rb:6:in `each' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks.rb:6:in `<top (required)>' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/application.rb:215:in `require' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/application.rb:215:in `initialize_tasks' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/application.rb:139:in `load_tasks' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/application.rb:77:in `method_missing' /app/Rakefile:7:in `<top (required)>' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `load' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:1991:in `run' /usr/ruby1.9.2/bin/rake:31:in `<main>'
へ???
あ、、、、 もしかして Heroku って Ruby 1.9.2 なの?
がちょぴーん。
せっかく作った @heroku を 1.9.2 にも作る
Heroku側のRubyのバージョンを変更する方法がどっかに書いてあった気がするけど、ここはRVM環境の真価を発揮し。。。。どうやるんだ?
地道にやるか。。。
まず 1.9.2 に移る
satsuki:myapp hana-da$ rvm use 1.9.2 Using /Users/hana-da/.rvm/gems/ruby-1.9.2-p290
@herokuを作ってuse
satsuki:myapp hana-da$ rvm gemset create heroku 'heroku' gemset created (/Users/hana-da/.rvm/gems/ruby-1.9.2-p290@heroku). satsuki:myapp hana-da$ rvm use 1.9.2@heroku Using /Users/hana-da/.rvm/gems/ruby-1.9.2-p290 with gem set heroku
ふむ。
gem listは
satsuki:myapp hana-da$ gem list *** LOCAL GEMS *** rake (0.9.2)
あれ? bundler がない。。。。
そか 1.8.7@globalにしか入れてないか。。。
1.9.2@globalにbunderを入れる
satsuki:myapp hana-da$ rvm use 1.9.2@global Using /Users/hana-da/.rvm/gems/ruby-1.9.2-p290 with gemset global satsuki:myapp hana-da$ gem install bundler Fetching: bundler-1.0.18.gem (100%) Successfully installed bundler-1.0.18 1 gem installed Installing ri documentation for bundler-1.0.18... Installing RDoc documentation for bundler-1.0.18...
1.9.2@herokuに戻ってherokuを入れる
satsuki:myapp hana-da$ rvm use 1.9.2@heroku Using /Users/hana-da/.rvm/gems/ruby-1.9.2-p290 with gemset heroku satsuki:myapp hana-da$ gem list *** LOCAL GEMS *** bundler (1.0.18) rake (0.9.2) satsuki:myapp hana-da$ gem install heroku Fetching: term-ansicolor-1.0.6.gem (100%) Fetching: mime-types-1.16.gem (100%) Fetching: rest-client-1.6.3.gem (100%) Fetching: addressable-2.2.6.gem (100%) Fetching: launchy-2.0.5.gem (100%) Fetching: heroku-2.4.2.gem (100%) Successfully installed term-ansicolor-1.0.6 Successfully installed mime-types-1.16 Successfully installed rest-client-1.6.3 Successfully installed addressable-2.2.6 Successfully installed launchy-2.0.5 Successfully installed heroku-2.4.2 6 gems installed
ふむ。
で、後なんだ?
あ、Powね。
satsuki:myapp hana-da$ echo "rvm 1.9.2@heroku" > .rvmrc
でーbundle installかな。
satsuki:myapp hana-da$ bundle install Fetching source index for http://rubygems.org/ Using rake (0.9.2) Installing abstract (1.0.0) WARNING: abstract-1.0.0 has an invalid nil value for @cert_chain Installing activesupport (3.0.10) Installing builder (2.1.2) WARNING: builder-2.1.2 has an invalid nil value for @cert_chain Installing i18n (0.5.0) Installing activemodel (3.0.10) Installing erubis (2.6.6) Installing rack (1.2.3) Installing rack-mount (0.6.14) Installing rack-test (0.5.7) Installing tzinfo (0.3.29) Installing actionpack (3.0.10) Using mime-types (1.16) Installing polyglot (0.3.2) Installing treetop (1.4.10) Installing mail (2.2.19) Installing actionmailer (3.0.10) Installing arel (2.0.10) Installing activerecord (3.0.10) Installing activeresource (3.0.10) Using bundler (1.0.18) Installing rdoc (3.9.2) Installing thor (0.14.6) Installing railties (3.0.10) Installing rails (3.0.10) Installing sqlite3 (1.3.4) with native extensions Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
以上、地道な作業でした。
気をとりなおして、もう一度 db:migrate
するよー
satsuki:myapp hana-da$ heroku rake db:migrate (in /app) rake aborted! uninitialized constant Rake::DSL /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2482:in `const_missing' /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:8:in `<class:TaskLib>' /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:6:in `<module:Rake>' /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:3:in `<top (required)>' /app/.bundle/gems/ruby/1.9.1/gems/rdoc-3.9.2/lib/rdoc/task.rb:37:in `require' /app/.bundle/gems/ruby/1.9.1/gems/rdoc-3.9.2/lib/rdoc/task.rb:37:in `<top (required)>' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks/documentation.rake:2:in `require' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks/documentation.rake:2:in `<top (required)>' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks.rb:15:in `load' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks.rb:15:in `block in <top (required)>' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks.rb:6:in `each' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/tasks.rb:6:in `<top (required)>' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/application.rb:215:in `require' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/application.rb:215:in `initialize_tasks' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/application.rb:139:in `load_tasks' /app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.10/lib/rails/application.rb:77:in `method_missing' /app/Rakefile:7:in `<top (required)>' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `load' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile' /usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:1991:in `run' /usr/ruby1.9.2/bin/rake:31:in `<main>'
ワロタ
んーもう1回 git push した方がいいのかな?
satsuki:myapp hana-da$ git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: .rvmrc # no changes added to commit (use "git add" and/or "git commit -a")
やー。こんな状態だから意味ないか。
たぶんもう1回bunderをheroku側で走らせてあげた方がいいような気が。。。気のせいかなぁ。。。
とりあえずカスじゃないので
uninitialized constant Rake::DSL
をググります。
ruby on rails - How to fix the uninitialized constant Rake::DSL problem on Heroku? - Stack Overflow こんなん出ました。
素直にしたがい Rakefile の requre 'rake' の上に
require 'rake/dsl_definition'
を追加。
ほんでもってgit commit and push
satsuki:myapp hana-da$ git add Rakefile satsuki:myapp hana-da$ git commit -m 'solve: uninitialized constant Rake::DSL' [master 70df50a] solve: uninitialized constant Rake::DSL 1 files changed, 1 insertions(+), 0 deletions(-) satsuki:myapp hana-da$ git push heroku master Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 338 bytes, done. Total 3 (delta 2), reused 0 (delta 0) -----> Heroku receiving push -----> Ruby/Rails app detected -----> Detected Rails is not set to serve static_assets Installing rails3_serve_static_assets... done -----> Configure Rails 3 to disable x-sendfile Installing rails3_disable_x_sendfile... done -----> Configure Rails to log to stdout Installing rails_log_stdout... done -----> Gemfile detected, running Bundler version 1.0.7 All dependencies are satisfied -----> Compiled slug size is 4.1MB -----> Launching... done, v5 http://myapp368934.heroku.com deployed to Heroku To git@heroku.com:myapp368934.git 5bb485c..70df50a master -> master
よし。
今度こそ
satsuki:myapp hana-da$ heroku rake db:migrate (in /app) /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/file_utils.rb:10: warning: already initialized constant RUBY /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2/lib/rake/file_utils.rb:84: warning: already initialized constant LN_SUPPORTED Migrating to CreateBlogs (20110819084614) == CreateBlogs: migrating ==================================================== -- create_table(:blogs) -> 0.0164s == CreateBlogs: migrated (0.0165s) ===========================================
おっしゃー!!!!
ほんでもって heroku open やー
おおしゃーおおしゃーん
そっかHerokuが1.9.2だから手元が1.8.7だとrakeできないってのは発想がおかしいか。
Heroku側でもbundlerが動いてrakeとかはHeroku側用のが入るわけだし。
とかいう妄想あってるかな?
うわさのdb:pushを試す
手元のsqliteのデータをHeroku側にpushしてくれるんだって。すげー
satsuki:myapp hana-da$ heroku db:push Taps Load Error: no such file to load -- taps/operation You may need to install or update the taps gem to use db commands. On most systems this will be: sudo gem install taps
うごかねぇ。
tapsがいるって事かな?
satsuki:myapp hana-da$ gem install taps Fetching: sequel-3.20.0.gem (100%) Fetching: sinatra-1.0.gem (100%) Fetching: sqlite3-ruby-1.3.3.gem (100%) :
気にせずガンガン入れれるっていいよね。
って sinatra まで入ったぞ。
ん?てかこうやって入れるんじゃなくてGemfileに書いてbundle installするのが正解なのかな?
まぁいいや。
では再度
satsuki:myapp hana-da$ heroku db:push Loaded Taps v0.3.23 Auto-detected local database: sqlite://db/development.sqlite3 Warning: Data in the app 'myapp368934' will be overwritten and will not be recoverable. ! WARNING: Potentially Destructive Action ! This command will affect the app: myapp368934 ! To proceed, type "myapp368934" or re-run this command with --confirm myapp368934 > myapp368934 Sending schema Schema: 100% |==========================================| Time: 00:00:02 Sending indexes schema_migrat: 100% |==========================================| Time: 00:00:01 Sending data 2 tables, 3 records schema_migrat: 100% |==========================================| Time: 00:00:00 blogs: 100% |==========================================| Time: 00:00:00 Resetting sequences satsuki:myapp hana-da$
キター
ステキスグル!キンニクスルグ!!!
変更を加える
とりあえずviewを何か適当にいじる
satsuki:myapp hana-da$ vi app/views/blogs/index.html.erb
んで commit and push
satsuki:myapp hana-da$ git commit -am 'change title' [master b05267c] change title 3 files changed, 3 insertions(+), 2 deletions(-) satsuki:myapp hana-da$ git push heroku master Counting objects: 15, done. Delta compression using up to 4 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (8/8), 650 bytes, done. Total 8 (delta 4), reused 0 (delta 0) -----> Heroku receiving push -----> Ruby/Rails app detected -----> Detected Rails is not set to serve static_assets Installing rails3_serve_static_assets... done -----> Configure Rails 3 to disable x-sendfile Installing rails3_disable_x_sendfile... done -----> Configure Rails to log to stdout Installing rails_log_stdout... done -----> Gemfile detected, running Bundler version 1.0.7 All dependencies are satisfied -----> Compiled slug size is 4.1MB -----> Launching... done, v6 http://myapp368934.heroku.com deployed to Heroku To git@heroku.com:myapp368934.git 70df50a..b05267c master -> master
さくっといくね♪
という事でscreen castもここでブチ切れてるので、おしまいっと。