おすすめbotを自分のURLで動かしたくてRuby 2.0.0 + PostgreSQL環境整えたり今更Herokuデビューしたりしてた

LingrVim部屋でいつも頑張っておすすめしてくれているのでちょっといじったりしてみたくなった。
あとHerokuもいい加減触りたかったのですでに動いているHeroku appを別のURLで動かしてみた。

環境

OS: Arch Linux

herokuコマンド入れた

最初は https://aur.archlinux.org/packages/heroku-client/

を入れてたけど、バージョンを上げないとgit push時のデプロイでエラーが出るとの事だったので AUR (en) - heroku-toolbelt をインストールした。

いろいろ試した

$ git clone https://github.com/akechi/osusume-lingrbot
$ cd osusume-lingrbot/
$ heroku create tyru-osusume
Enter your Heroku credentials.
Email: tyru.exe@gmail.com
Password (typing will be hidden): 
Logged in as tyru.exe@gmail.com
Creating tyru-osusume... done, stack is cedar-14
https://tyru-osusume.herokuapp.com/ | https://git.heroku.com/tyru-osusume.git
$ git push heroku master
Counting objects: 1093, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (491/491), done.
Writing objects: 100% (1093/1093), 130.74 KiB | 0 bytes/s, done.
Total 1093 (delta 593), reused 1093 (delta 593)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rack
remote: -----> Using Ruby version: ruby-2.0.0
remote: -----> Installing dependencies using bundler 1.9.7
remote:        Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote:        Fetching gem metadata from https://rubygems.org/...........
remote:        Fetching version metadata from https://rubygems.org/...
remote:        Fetching dependency metadata from https://rubygems.org/..
remote:        Rubygems 2.0.14.1 is not threadsafe, so your gems must be installed one at a time. Upgrade to Rubygems 2.1.0 or higher to enable parallel gem installation.
remote:        Installing rake 10.0.4
remote:        Installing addressable 2.3.5
remote:        Installing bcrypt-ruby 3.1.2
remote:        Installing coffee-script-source 1.6.3
remote:        Installing execjs 2.0.1
remote:        Installing coffee-script 2.2.0
remote:        Installing dm-core 1.2.1
remote:        Installing dm-aggregates 1.2.0
remote:        Installing dm-constraints 1.2.0
remote:        Installing dm-migrations 1.2.0
remote:        Installing fastercsv 1.5.5
remote:        Installing json 1.8.0
remote:        Installing json_pure 1.8.0
remote:        Installing multi_json 1.7.9
remote:        Installing dm-serializer 1.2.2
remote:        Installing dm-timestamps 1.2.0
remote:        Installing dm-transactions 1.2.0
remote:        Installing stringex 1.5.1
remote:        Installing uuidtools 2.1.4
remote:        Installing dm-types 1.2.2
remote:        Installing dm-validations 1.2.0
remote:        Installing data_mapper 1.2.0
remote:        Installing data_objects 0.10.13
remote:        Installing dm-do-adapter 1.2.0
remote:        Installing do_postgres 0.10.13
remote:        Installing dm-postgres-adapter 1.2.0
remote:        Installing libv8 3.16.14.3
remote:        Installing mini_portile 0.5.1
remote:        Installing nokogiri 1.6.0
remote:        Installing rack 1.5.2
remote:        Installing rack-protection 1.5.0
remote:        Installing ref 1.0.5
remote:        Installing sass 3.2.10
remote:        Installing tilt 1.4.1
remote:        Installing sinatra 1.4.3
remote:        Installing temple 0.6.6
remote:        Installing slim 2.0.1
remote:        Installing therubyracer 0.12.1
remote:        Using bundler 1.9.7
remote:        Bundle complete! 13 Gemfile dependencies, 39 gems now installed.
remote:        Gems in the groups development and test were not installed.
remote:        Bundled gems are installed into ./vendor/bundle.
remote:        Bundle completed (42.36s)
remote:        Cleaning up the bundler cache.
remote:        Could not detect rake tasks
remote:        ensure you can run `$ bundle exec rake -P` against your app with no environment variables present
remote:        and using the production group of your Gemfile.
remote:        rake aborted!
remote:        cannot load such file -- rspec/core/rake_task
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/Rakefile:7:in `require'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/Rakefile:7:in `<top (required)>'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/vendor/bundle/ruby/2.0.0/gems/rake-10.0.4/lib/rake/rake_module.rb:25:in `load'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/vendor/bundle/ruby/2.0.0/gems/rake-10.0.4/lib/rake/rake_module.rb:25:in `load_rakefile'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/vendor/bundle/ruby/2.0.0/gems/rake-10.0.4/lib/rake/application.rb:589:in `raw_load_rakefile'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/vendor/bundle/ruby/2.0.0/gems/rake-10.0.4/lib/rake/application.rb:89:in `block in load_rakefile'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/vendor/bundle/ruby/2.0.0/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/vendor/bundle/ruby/2.0.0/gems/rake-10.0.4/lib/rake/application.rb:88:in `load_rakefile'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/vendor/bundle/ruby/2.0.0/gems/rake-10.0.4/lib/rake/application.rb:72:in `block in run'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/vendor/bundle/ruby/2.0.0/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/vendor/bundle/ruby/2.0.0/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
remote:        /tmp/build_3fb3cf6337c8dbf851b9464d24fbf880/vendor/bundle/ruby/2.0.0/gems/rake-10.0.4/bin/rake:33:in `<top (required)>'
remote:        vendor/bundle/bin/rake:16:in `load'
remote:        vendor/bundle/bin/rake:16:in `<main>'
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types     -> web
remote:        Default types for buildpack -> console, rake
remote: 
remote: -----> Compressing...
remote:        Done: 41M
remote: -----> Launching...
remote:        Released v4
remote:        https://tyru-osusume.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/tyru-osusume.git
 * [new branch]      master -> master
$ heroku labs:enable user-env-compile
 !    No such feature: user-env-compile

heroku forkした

heroku forkというコマンドがあることを知った。
が、これだけで全てうまく行くほどおすすめさんは甘くなかった。

$ heroku fork --from osusume --to tyru-osusume
 ▸    You do not have access to the app osusume.

ちなみにここを見ておすすめさんのURLは知っていた。

Endpoint: http://osusume.herokuapp.com/lingr

rbenv入れた

rbenv を利用した Ruby 環境の構築 | Developers.IO

$ rbenv install 2.0.0-p648
$ rbenv local 2.0.0-p648
$ rbenv versions
  system
* 2.0.0-p648 (set by /home/tyru/git/osusume-lingrbot/.ruby-version)

bundle exec ruby web.rb したけど2.0.0用のbundlerを入れてなかった

$ bundle exec ruby web.rb
Could not find rake-10.0.4 in any of the sources
Run `bundle install` to install missing gems.

ここでbundle exec gem install bundlerとかやったせいで、bundle installしたらお前のRubyのバージョンが違うと言われた
単純にgem install bundlerでよかった。

$ bundle install
Your Ruby version is 2.3.0, but your Gemfile specified 2.0.0

2.0.0用のbundlerを入れなおした

入れなおしたらPosgreSQLをインストールし忘れててエラーが出た。*1

$ gem install bundler
$ hash -r
$ bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Rubygems 2.0.14.1 is not threadsafe, so your gems will be installed one at a time. Upgrade to Rubygems 2.1.0 or higher to enable parallel gem installation.
Installing rake 10.0.4
Installing addressable 2.3.5
Installing bcrypt-ruby 3.1.2 with native extensions
Installing coffee-script-source 1.6.3
Installing execjs 2.0.1
Installing fastercsv 1.5.5
Installing json 1.8.0 with native extensions
Installing json_pure 1.8.0
Installing multi_json 1.7.9
Installing stringex 1.5.1
Installing uuidtools 2.1.4
Installing diff-lcs 1.2.4
Installing libv8 3.16.14.3
Installing mini_portile 0.5.1
Installing rack 1.5.2
Installing ref 1.0.5
Installing rspec-core 2.14.5
Installing rspec-mocks 2.14.3
Installing sass 3.2.10
Installing tilt 1.4.1
Installing temple 0.6.6
Using bundler 1.11.2
Installing dm-core 1.2.1
Installing data_objects 0.10.13
Installing coffee-script 2.2.0
Installing rspec-expectations 2.14.2
Installing nokogiri 1.6.0 with native extensions
Installing rack-protection 1.5.0
Installing rack-test 0.6.2
Installing therubyracer 0.12.1 with native extensions
Installing slim 2.0.1
Installing dm-aggregates 1.2.0
Installing dm-constraints 1.2.0
Installing dm-migrations 1.2.0
Installing dm-serializer 1.2.2
Installing dm-timestamps 1.2.0
Installing dm-transactions 1.2.0
Installing dm-types 1.2.2
Installing dm-validations 1.2.0
Installing dm-yaml-adapter 1.2.0
Installing dm-do-adapter 1.2.0
Installing do_postgres 0.10.13 with native extensions

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /home/tyru/.rbenv/versions/2.0.0-p648/bin/ruby extconf.rb
checking for main() in -lpq... no
checking for main() in -llibpq... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/home/tyru/.rbenv/versions/2.0.0-p648/bin/ruby
        --with-pgsql-server-dir
        --without-pgsql-server-dir
        --with-pgsql-server-include
        --without-pgsql-server-include=${pgsql-server-dir}/include
        --with-pgsql-server-lib
        --without-pgsql-server-lib=${pgsql-server-dir}/
        --with-pgsql-client-dir
        --without-pgsql-client-dir
        --with-pgsql-client-include
        --without-pgsql-client-include=${pgsql-client-dir}/include
        --with-pgsql-client-lib
        --without-pgsql-client-lib=${pgsql-client-dir}/
        --with-pqlib
        --without-pqlib
        --with-libpqlib
        --without-libpqlib
Could not find PostgreSQL build environment (libraries & headers): Makefile not created


Gem files will remain installed in /home/tyru/.rbenv/versions/2.0.0-p648/lib/ruby/gems/2.0.0/gems/do_postgres-0.10.13 for inspection.
Results logged to /home/tyru/.rbenv/versions/2.0.0-p648/lib/ruby/gems/2.0.0/gems/do_postgres-0.10.13/ext/do_postgres/gem_make.out
Installing rspec 2.14.1
Installing sinatra 1.4.3
Installing data_mapper 1.2.0
An error occurred while installing do_postgres (0.10.13), and Bundler cannot continue.
Make sure that `gem install do_postgres -v '0.10.13'` succeeds before bundling.

sudo pacman -S postgresql したら解決。

$ sudo pacman -S postgresql 
(snip)
$ bundle install
(snip)
Using dm-yaml-adapter 1.2.0
Using dm-do-adapter 1.2.0
Installing do_postgres 0.10.13 with native extensions
Using rspec 2.14.1
Using sinatra 1.4.3
Using data_mapper 1.2.0
Installing dm-postgres-adapter 1.2.0
Bundle complete! 13 Gemfile dependencies, 46 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

appが動かなかったけど環境変数が定義されてなかった

$ ag ENV
spec/web_spec.rb
1:ENV['RACK_ENV'] = 'test'

web.rb
45:$web_uri = ENV['OSUSUME_WEB_URI'] || "http://osusume.herokuapp.com/"
66:  dsn = ENV["HEROKU_POSTGRESQL_TEAL_URL"]
82:BOT_VERIFIER = Digest::SHA1.hexdigest("osusume#{ENV["OSUSUME_BOT_SECRET"]}")

OSUSUME_WEB_URI

自分の場合は元のURLから変えたので以下を設定。

$ heroku config:add 'OSUSUME_WEB_URI=http://tyru-osusume.herokuapp.com/'

HEROKU_POSTGRESQL_TEAL_URL

mattnさんから教えてもらったので、Web画面から「Create Database」してURLを取得して設定した。

OSUSUME_BOT_SECRET

Lingr の bot を作ってみた | matoken's meme

bot_verifier は載っていませんが,bot ID とSecret から求められます. KagolugML_bot と xxxxxxxxxxxxxxxxxxxxxxxxxxx を結合した文字列をsha1 にかけます.

$ echo -n 'KagolugML_botxxxxxxxxxxxxxxxxxxxxxxxxxxx'|sha1sum b3c03556dca8dfffb8c509cb709817044c98ad87 -

この場合 b3c03556dca8dfffb8c509cb709817044c98ad87 が bot_verifier です.

heroku restartした

環境変数を適用させるためにプロセスをheroku restartで再起動させた。

heroku logs --tailするとリクエストは来てるっぽいけど発言に反応してくれない

いまここ

http://lingr.com/room/tyru/archives/2016/01/29#message-22996522

参考記事

*1:Rubygems 2.0.14.1 is not threadsafe」も気になるけどまぁこのバージョンのRuby指定されてる事だし無視