Sinatraで作ってHerokuにデプロイ
Railsより規模が小さくて、さくっと作りたいもの用にSinatraを触ってみる。
動的なものはHerokuにデプロイしておくのがイマドキな感じがするので、
初めてのHerokuもやってみた。
Sinatraインストール
$ gem install sinatra
sinatra (1.4.4)がインストールされた。
Hello Worldアプリケーション
Getting Started with Ruby on Heroku
フォルダ構成はこんなかんじになる。
.
├── Gemfile
├── Gemfile.lock
├── Procfile
└── web.rb
Gemfile.lockはbundleで自動で作られるようなので、
作成するのは実質3ファイル
Gemfile
source "https://rubygems.org" ruby "2.1.1" gem 'sinatra', '1.4.4'
Procfile
web: bundle exec ruby web.rb -p $PORT
Procfile
require 'sinatra' get '/' do 'Hello world!' end
ローカルで実行するには…
$ foreman start
localhostの5000ポートで起動を確認!!!
Herokuのアカウント作る
https://www.heroku.com/からサインアップ
HerokuToolbeltのダウンロード→インストール
Herokuコマンドが使えるようになるので
$ heroku login
e-mailアドレスとパスワードを聞かれるので、入力すると、
sshの公開鍵がアップロードされる。
Herokuにデプロイ
いよいよデプロイ!
- プロジェクトをGitで管理
$ git init $ git add -A $ git commit -m "initial commit"
- Herokouアプリケーションの作成
heroku create
新規でアプリケーションが作成される。
- Herokuにpush(=デプロイ)
ここでちょっとはまる…
$ git push heroku master Warning: Permanently added the RSA host key for IP address '*.*.*.*' to the list of known hosts. Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
sshの公開鍵認証がうまくいかないらしい。
鍵を作りなおしてみる。 id_rsa_heroku.pubを作る。
$ ssh-keygen -t rsa -C [e-mailアドレス] Generating public/private rsa key pair. Enter file in which to save the key (/Users/shimpeiws/.ssh/id_rsa): id_rsa_heroku Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in id_rsa_heroku. Your public key has been saved in id_rsa_heroku.pub. The key fingerprint is: 33:f3:63:19:7d:d2:1e:aa:5b:bd:aa:1e:e2:54:2c:4a shimpeiws@gmail.com The key's randomart image is: [finger pring]
~/.ssh/config に以下を追加
Host heroku.com Hostname heroku.com port 22 IdentityFile ~/.ssh/id_rsa_heroku TCPKeepAlive yes IdentitiesOnly yes
作成した公開鍵をHerokuに追加
heroku keys:add ~/.ssh/id_rsa_heroku.pub
再度pushしてみる。
$ git push heroku master Identity added: /Users/*****/.ssh/id_rsa_heroku (/Users/*****/.ssh/id_rsa_heroku) Initializing repository, done. Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 631 bytes | 0 bytes/s, done. Total 6 (delta 0), reused 0 (delta 0) -----> Ruby app detected -----> Compiling Ruby/Rack -----> Using Ruby version: ruby-2.1.1 -----> Installing dependencies using 1.5.2 Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment Fetching gem metadata from https://rubygems.org/.......... Fetching additional metadata from https://rubygems.org/.. Using bundler (1.5.2) Installing tilt (1.4.1) Installing rack (1.5.2) Installing rack-protection (1.5.2) Installing sinatra (1.4.4) Your bundle is complete! Gems in the groups development and test were not installed. It was installed into ./vendor/bundle Bundle completed (6.85s) Cleaning up the bundler cache. -----> Discovering process types Procfile declares types -> web Default types for Ruby -> console, rake -----> Compressing... done, 13.0MB -----> Launching... done, v4 http://afternoon-shore-2353.herokuapp.com/ deployed to Heroku To git@heroku.com:afternoon-shore-2353.git * [new branch] master -> master
デプロイ成功!!!
このままだと自動で振られた
http://afternoon-shore-2353.herokuapp.com/
が接続先になるけれど、
Herokuにログインすると、app名(=URL)が変更できる。
http://shimpeiws-first.herokuapp.com/
まとめ
Heroku色々自動で、すごくモダン!
Sinatraは次はパラメータの受け取り & DB接続あたりをやってみよう。
参考
HerokuのSSH設定
[Sinatra]herokuセットアップ、sinatraを動かす
homebrewでパーミッションにちょっとはまる