2011年6月10日金曜日

herokuの設定

herokuを使ってみたい

まずはサイトを確認。
http://www.heroku.com/

Rubyとnode.jsの開発が出きるのか。
DBの価格設定の分け方が、FuguとかIkaとかおもしろい。
初めて知ったけどPoka-yoke(ポカヨケ)という単語がある。どうやらfail safeのことっぽい。興味深い。

さっそく触ってみる。
まずはSign upしよう。
メールアドレスを入力すると、メールが届く。
そのメールから本登録を行う。パスワードを入れるだけ。



My Appsのページに移動する。
Quickstart Guideというリンクがあるのでクリックしてサイトを見てみよう。

git pushでdeployできる。
Ruby1.9.2を推奨している。
SSH keyが必要。
説明はgithubのページにリンクされている。
http://help.github.com/linux-set-up-git/
herokuもgithubもSSH keyが必要なのか。

一通り目を通したので、試してみる。

と思ったけれど、Getting Started with HerokuのページにはHerokuのインストールが書いていない。どこか違うページに書いてあるのかな?

と思ったら、Prerequisitesの3に書いてあった。

1から進めていく。
素のRailsアプリを作る。rubyとgemは前回作ったfwifferを使う。
$ cd ~/Desktop
$ rvm use 1.9.2@fwiffer
$ rails new heroku_test

2のGitは既にインストールされている

3 herokuのインストール
globalかfwifferのどちらにいれるか迷うけど、fwifferにインストール
$ gem install heroku
$ heroku keys:add
/home/k10i/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:54:in `require': no such file to load -- readline (LoadError)
...エラー。readlineがインストールされていないらしい。インストールしたはずなんだけれどなぁ。
blogを読み返すと、redmineをインストールした1.8.7にはreadlineをインストールしたけど、1.9.2にはしていなかった。irbで文字化けするの分かっているんだから最初からインストールすれば良かったよな。

1.9.2を一度削除してインストール(gemは削除されない)
削除しないで良い方法は無いものかな?
$ rvm remove 1.9.2
$ rvm install 1.9.2 --with-zlib-dir=$rvm_path/usr --with-openssl-dir=$rvm_path/usr --with-libxml2-dir=$rvm_path/usr --with-readline-dir=$rvm_path/usr

再開
$ heroku keys:add
/home/k10i/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:54:in `require': no such file to load -- readline (LoadError)

あれ?まただ。rvmにインストールしたのでは駄目なのかな?
調べる。
http://blog.project-sierra.de/archives/1534

おぉ。rubyを再インストールしなくても、ext/readlineからextconf.rbが呼べてインストール出きるのか。
$ cd /home/k10i/.rvm/src/ruby-1.9.2-p180/ext/readline/
$ ruby extconf.rb
$ make
make: `all' に対して行うべき事はありません.
...これはエラーなのかな?
installする。
$ make install
make: `install' に対して行うべき事はありません.
うーん。makeが失敗している。
http://d.hatena.ne.jp/modka/20110115/1295070296
これはrvmのインストールの箇所に書いてあるけど、libreadline-devというのが必要なのかな?

Synapticでlibreadline-devをインストール。

再開。
$ ruby extconf.rb
...あきらかにさっきより長い。さっきとは違う事が分かった。
もうここまでくるとmakeとかしないで、herokuコマンドが入力できるんじゃないかな?
$ heroku keys:add
/home/k10i/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:54:in `require': no such file to load -- readline (LoadError)
やっぱり駄目だ。readlineのインストールを続ける。
$ make
$ make install
うまくインストールされたっぽい。

完了後、herokuの続き。
$ heroku keys:add
No ssh public key found in /home/k10i/.ssh/id_[rd]sa.pub.  You may want to specify the full path to the keyfile.
公開鍵が見つからないと。
これは後で作成するSSH keyがないと駄目なのかな。
ひとまづ置いておいて先に進む。
$ cd ~/Desktop/heroku_test
$ git init
$ git add .
$ git commit -m "new app"
SSH Keysの設定
$ cd .~/.ssh
bash: cd: /home/k10i/.ssh: そのようなファイルやディレクトリはありません
うーん。
$ which ssh
/usr/bin/ssh
はインストールされている。

あっ、githubの説明をよく見ると、そのエラーが出る場合は、step3へ進むって書いてあった。。。
よく読まないとな。
$ ssh -keygen -t rsa -C "xxxx.yyyy@gmail.com"
Bad escape character 'ygen'.
sshと-keygenはつながっているのか。
再度実行
$ ssh-keygen -t rsa -C "xxxx.yyyy@gmail.com"
/home/k10i/.ssh/id_rsa.pub
が作成された。githubのページではこれをgithubに登録する方法が書いてあるけどherokuは必要なさそうなので戻って進む。
$ heroku create
Emailとパスワードを入力すると、先ほどのid_rsa.pubが使われて、herokuに追加された。

URLが書いてある。アクセスしてみる。
Heroku | Welcome to your new app!
と書いてある。
$ heroku open
でも同じURLにアクセスできた。

URLの横に書いてあるのが、gitリポジトリみたい。
$ git remote show heroku
で確認できた。

ここで、
git remote show heroku
Warning: Permanently added the RSA host key for IP address 'xxx.xxx.xxx.xxx' to the list of known hosts.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
とエラーが出る場合は、rsaが読み込まれていないのかな。
違うターミナルで作成したら出た。
ターミナルを一度落とすか、rsaを作ったターミナルで実行するとうまく動作した

進む
$ git push heroku master
おぉ。ここでGemfileで設定してあるgemがインストールされる。
出力を見るかぎりdevelopmentとtestに書いてあるのは除外されるみたい。
$ heroku rake db:migrate
rake aborted!
uninitialized constant Rake::DSL
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2482:in `const_missing'
エラー。
ローカルでdbが無いのが問題かも。まだ一度も起動していないし。
$ rake db:migrate
rake aborted!
undefined method `task' for #
あれ?コマンドは違うんだっけ?
$ rake -T
rake aborted!
undefined method `task' for #
これrakeのバージョンがおかしいのかな。
rakeのバージョンは0.9が入っている。
前にblogに書いた。
#Rakefile
require File.expand_path('../config/application', __FILE__)
require 'rake'
module ::HerokuTest
  class Application
   include Rake::DSL
 end
end
module ::RakeFileUtils
 extend Rake::FileUtilsExt
end
HerokuTest::Application.load_tasks
と編集。
$ rake db:migrate
エラーは出なかった。
herokuの出力を見ると、rakeは0.9.2がインストールされている。
Rakefileをコミットして再度migrationをしてみよう。
$ git commit Rakefile -m "rake 0.9"
[master ba3ccc3] rake 0.9  Committer: k10i
 Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly:
     git config --global user.name "Your Name"
     git config --global user.email you@example.com
 If the identity used for this commit is wrong, you can fix it with:
     git commit --amend --author='Your Name '

1 files changed, 10 insertions(+), 0 deletions(-)

追加はされたけど、Gitの設定が全然していなかった。

まずは、herokuでmigrationができるかテスト。

と思ったけれど、更新を反映させるにはpushする必要があるのか。
$ git push heroku
$ heroku rake db:migrate rake aborted! uninitialized constant Rake::DSL
また同じようなエラーだ。0.9.2は書き方が違うのかな?
一つ前に出たエラーと比べると、少しは進んでいそう。

調べていると、rakeは0.8.7にした方が、今の時点では問題なさそう。
Rakefileを元に戻して、
Gemfileを編集
gem 'rake', '0.8.7'
を追加。
$ git commit . -m "rake 0.8.7"
$ git push heroku
表示を見る限りだと、herokuはpushする毎にbundlerでインストールしているのかな?
rake 0.8.7がインストールされたので、migrationを実行。
$ heroku rake db:migrate
エラーなし。
アクセスしてみる。
$ heroku open
うまくいった。Railsの初期画面が表示されている。

scaffoldを1つ作って、DBと連携するプログラムをコミットしてみる

$ script/rails scaffold user name:string password:string
You have requested:   rake = 0.8.7
 The bundle currently has rake locked at 0.9.0. Try running `bundle update rake`
rakeが0.9.0でlockされているのかな。
$ bundle update rake
続いてscaffold
$ script/rails scaffold user name:string password:string
Error: Command not recognized Usage: rails COMMAND [ARGS]
あっ、コマンドが違う。
$ script/rails generate scaffold user name:string password:string
$ rake db:migrate
rake aborted! 
You have already activated rake 0.9.0, but your Gemfile requires rake 0.8.7. Consider using bundle exec.
うーん。0.9.0がactivateになっているから0.8.7を使うようにしろって。

さっきのupdateじゃ駄目だったんだ。代わりにbundle execを使ってみたらって。
$ bundle exec rake db:migrate
うまくいった。
$ rails server
で確認。CRUDができる。

commitしてpushしよう。
その前に、gitのユーザ名とメールアドレスを登録しておこう。
$ git config --global user.name "k10i" 
$ git config --global user.email xxxx.yyyy@gmail.com
でコミット・プッシュ
$ git add . 
$ git commit -m "user scaffod"
$ git push heroku $ heroku rake db:migrate
migration成功後にサイトを開いて、/usersにアクセス。

うまくいった!
今回のgit push herokuの時は、gemはインストールされなかった。
Gemfileに変更があった場合にすべてインストールしなおすようになっているのかもしれない。

0 件のコメント:

コメントを投稿