SyntaxHighlighter

2012年9月28日金曜日

Rubyでデザイン確認用の簡易サーバを立てるよ

/でアクセスされた時が微妙だけど、動くからいいか。
例によってSinatra使ってます。

Rubyで簡易SSLサーバを立てるよ

鍵をOpenSSLとかで別途作るのがめんどかったので。Sinatra使っているのでそのへんは適当に。
HTTPとHTTPS混在しているサイトでのjavascriptの動作検証等に使ってました。



てか、今更ながらだけど…Gistいいね。

2012年9月11日火曜日

OSXで80番portでrackupでPermission deniedで。

$ bundle exec rackup -p 80
[2012-09-11 23:51:44] INFO  WEBrick 1.3.1
[2012-09-11 23:51:44] INFO  ruby 1.9.2 (2011-07-09) [x86_64-darwin11.4.2]
[2012-09-11 23:51:44] WARN  TCPServer Error: Permission denied - bind(2)
/Users/xxxxxxx/toybox/design/vendor/bundle/ruby/1.9.1/gems/webrick-1.3.1/lib/webrick/utils.rb:85:in `initialize': Permission denied - bind(2) (Errno::EACCES)
そんなときはsudoで。あぁ、そうですよね。
当たり前すぎて調べても見つからなかったのでメモ(泣)
$ sudo bundle exec rackup -p 80

2012年3月25日日曜日

ローカルのPostgresの内容を、Herokuの共有DB(Postgres)にPushする

この前の逆。すぐに忘れるのでメモ
heroku db:push postgres://postgres_id:postgres_password@localhost/database_name

2012年3月22日木曜日

Herokuの共有DB(Postgres)の内容をローカルのPostgresにPushする

tapsを使うことで、可能となるのだ!
ちなみに今回はOSXの雪豹でやりました。WindowsはTapsのインストールで断念した(笑)

まずはtapsをインストール
gem install taps

次にHerokuからDBをPullします。その際sqlite形式で落としました。
アプリ名を聞かれるのでそのままアプリ名を入力してください。
heroku db:pull sqlite://20120312.sqlite

PULLが完了したので、ローカルのPostgresにPushするのですが、
sqlite形式で落としたためtapsを中継サーバとして利用します。
Push先のPostgresサーバを指定して、tapsサーバを起動。
最後のパラメータのユーザ/パスワートは今立ち上げたtapsサーバのものですので
適当なものでオッケーです。
taps server postgres://postgres_id:postgres_password@localhost/database_name taps_user taps_password

あとは、sqlite形式のデータをtaps経由でいれるだけです。
上記コマンドはフォアグラウンドでサーバ起動していますので別のコンソールを開いて作業した方が簡単でしょう。
というわけで新しいコンソールで以下を実行
taps push sqlite://20120312.sqlite http://taps_user:taps_password@localhost:5000

エラーがでなければ完了です♪

参考

herokuで使用しているDBのデータをエクスポートしてローカルのDBにインポート
ほとんどまんまですね。感謝!

2012年3月18日日曜日

Herokuアカウントを複数使いたい!

今までマシン毎にHerokuアカウントを登録してたんだけど、いい加減めんどうになったので
MacMiniに統一したくて。

Heroku Accountsなるプラグインを導入します。

参考

herokuコマンドで複数アカウントを使いわける

おおむね、参考サイトのどおりです。
自分の場合~/.ssh/configがなかったので新規作成しました。

一点注意点だけ。
Herokuからcloneする場合はサーバ指定が~/.ssh/configに書いた内容で指定する


# git clone git@heroku.xxx:myapp.git

OSX 10.6.8(Snow Leopard) でRVM環境+Ruby1.9.2のインストール

RVMいれたいなー。

てなわけで、下記を参考にインストール開始

参照
Mac OS XでRVM環境のセッティング(依存ライブラリ周り)


$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
$ rvm pkg install ree_dependencies
結果、下記のエラーが出ていたけど、ncursesなので、無視して続行します。
Installing ncurses to /Users/xxx/.rvm/usr
Error running 'make install', please read /Users/xxx/.rvm/log/ncurses/make.install.log
んで下記を実行
$ rvm install 1.9.2
...
Installing yaml to /Users/xxxxx/.rvm/usr
The provided compiler '/usr/bin/gcc' is LLVM based, it is not yet fully supported by ruby and gems, please read `rvm requirements`.
あら、トラブルさん、こんにちは。今日はお早いお付きですね。 ってなわけでググると、Xcode4.2入れたらおかしくなるよと。Xcodeの環境も壊したくなかったので下記を参考にGCC入れ替えましょう!

参照 Mac OS X LionにXcode 4.2を入れてrvmによりRuby 1.8.xをinstallするとgem installで落ちる問題をXcode 4.2を残したまま解決

・・・って、homebrewなんて入ってない。MacPortsは入ってるけどなんか新しいみたいなので そっちも入れてみます。公式を参考に下記で。
$ /usr/bin/ruby -e "$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
はいったよ。
ln: dir: File exists
Error: The linking step did not complete successfully
The formula built, but is not symlinked into /usr/local
You can try again using `brew link gcc'
==> Summary
/usr/local/Cellar/gcc/4.6.2: 239 files, 28M, built in 41.1 minutes
エラーがでたけど、指示通り
# brew link gcc
でオッケーかな? 再挑戦!
$ rvm install 1.9.2
...
Install of ruby-1.9.2-p318 - #complete
おっけー☆フフッ

2012年2月29日水曜日

GitでCommitを取り消す+Pushを取り消す

取り消すよ。

ローカルのcommitを取り消し

git reset --soft HEAD^

Pushを取り消し(originとmaster)

git push -f origin HEAD^:master

2012年2月2日木曜日

HerokuでDBIだ!

とりあえずHeroku上にてHelloWorldの儀式はすんでいる状況からスタート!
サンプルは下記の状態でとします。
#ls
Gemfile
Gemfile.lock
config.ru
hello.rb

#cat Gemfile
source :rubygems
gem 'sinatra'

#cat config.ru
require './hello'
run Sinatra::Application

#cat hello.rb
require 'rubygems'
require 'sinatra'

get '/' do
  "Hello World!"
end


まずはローカルのPostgresqlで動くように修正
#cat Gemfile
source :rubygems
gem 'sinatra'
gem 'dbi'
gem 'dbd-pg'

#cat hello.rb
require 'rubygems'
require 'sinatra'
require 'dbi'

get '/' do
  dbh = DBI.connect('dbi:Pg:dozaemon:localhost', 'dozaemon', '')
  version = dbh.select_one('select version()')
  "Postgresql version is #{version}"
end

Gemfileを編集したのでGemfile.lockを作り直す
bundle install

ローカルで動かしてみる。
#rackup config.ru 

きちんと動くのを確認!

次はHeroku側の接続情報を取得。
ここを参照。

#heroku config
BUNDLE_WITHOUT      => development:test
DATABASE_URL        => postgres://username:password@hostname/database
LANG                => en_US.UTF-8
RACK_ENV            => production
SHARED_DATABASE_URL => postgres://username:password@hostname/database

SHARED_DATABASE_URLを参考にRubyソースを編集
#cat hello.rb
require 'rubygems'
require 'sinatra'
require 'dbi'

get '/' do
  dbh = DBI.connect('dbi:Pg:database:hostname', 'username', 'password')
  version = dbh.select_one('select version()')
  "Postgresql version is #{version}"
end

サーバで動かしてみる。
#git add .
#git commit -m 'db test'
#git push heroku master
#heroku open

うごいた!だめなら heroku logs

2012年1月31日火曜日

さくらVPS(CentOS5.7)でRubyだDBIだdbd-pgだ

男は黙ってDBI。
# gem install dbi
Fetching: deprecated-2.0.1.gem (100%)
Fetching: dbi-0.4.5.gem (100%)
Successfully installed deprecated-2.0.1
Successfully installed dbi-0.4.5
2 gems installed
Installing ri documentation for deprecated-2.0.1...
Installing ri documentation for dbi-0.4.5...
Installing RDoc documentation for deprecated-2.0.1...
Installing RDoc documentation for dbi-0.4.5...
楽勝。次、dbd-pgだ。
# gem install dbd-pg
Fetching: pg-0.12.2.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing dbd-pg:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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=/usr/bin/ruby
        --with-pg
        --without-pg
        --with-pg-dir
        --without-pg-dir
        --with-pg-include
        --without-pg-include=${pg-dir}/include
        --with-pg-lib
        --without-pg-lib=${pg-dir}/lib
        --with-pg-config
        --without-pg-config
        --with-pg_config
        --without-pg_config


Gem files will remain installed in /usr/lib/ruby/gems/1.9.1/gems/pg-0.12.2 for inspection.
Results logged to /usr/lib/ruby/gems/1.9.1/gems/pg-0.12.2/ext/gem_make.out
ぐぬぬ。libpq-fe.hがないって?あーん?develいれてなかったから入れてみる。
#yum install postgresql91-devel.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * epel: ftp.kddilabs.jp
 * extras: ftp.nara.wide.ad.jp
 * updates: ftp.nara.wide.ad.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package postgresql91-devel.x86_64 0:9.1.2-1PGDG.rhel5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================
 Package                    Arch           Version                   Repository      Size
==========================================================================================
Installing:
 postgresql91-devel         x86_64         9.1.2-1PGDG.rhel5         pgdg91         1.6 M

Transaction Summary
==========================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 1.6 M
Is this ok [y/N]: y
Downloading Packages:
postgresql91-devel-9.1.2-1PGDG.rhel5.x86_64.rpm                    | 1.6 MB     00:01     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : postgresql91-devel                                                 1/1 

Installed:
  postgresql91-devel.x86_64 0:9.1.2-1PGDG.rhel5                                           

Complete!
うっし、もう一回!
# gem install dbd-pg
Building native extensions.  This could take a while...
ERROR:  Error installing dbd-pg:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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=/usr/bin/ruby
        --with-pg
        --without-pg
        --with-pg-dir
        --without-pg-dir
        --with-pg-include
        --without-pg-include=${pg-dir}/include
        --with-pg-lib
        --without-pg-lib=${pg-dir}/lib
        --with-pg-config
        --without-pg-config
        --with-pg_config
        --without-pg_config


Gem files will remain installed in /usr/lib/ruby/gems/1.9.1/gems/pg-0.12.2 for inspection.
Results logged to /usr/lib/ruby/gems/1.9.1/gems/pg-0.12.2/ext/gem_make.out
ぐぎぎぎぎ。pg_configがないって?見落としてた。言われるままに再チャレンジ!
#gem install dbd-pg -- --with-pg_config=/usr/pgsql-9.1/bin/pg_config
Building native extensions.  This could take a while...
Fetching: dbd-pg-0.3.9.gem (100%)
Successfully installed pg-0.12.2
Successfully installed dbd-pg-0.3.9
2 gems installed
Installing ri documentation for pg-0.12.2...
Installing ri documentation for dbd-pg-0.3.9...
Installing RDoc documentation for pg-0.12.2...
Installing RDoc documentation for dbd-pg-0.3.9...
お、うまくいったっぽい?
試してみる。
# irb 
irb(main):001:0> require 'dbi'
=> true
irb(main):002:0> dbh = DBI.connect('dbi:Pg:dozaemon:localhost','dozaemon','')
=> #<DBI::DatabaseHandle:0x00・・・以下略>
irb(main):003:0> dbh.select_one('select version()')
=> ["PostgreSQL 9.1.2 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-51), 64-bit"]
irb(main):004:0> exit
でけた!
#昨日のセットアップのままだとパスワードはいらないっす。

2012年1月30日月曜日

さくらVPS(CentOS5.7)へYUMでPostgreSQL9.1.2をインストール

既存のyumではver8しかないため、yumのリポジトリをダウンロード+インストール
# wget http://yum.postgresql.org/9.1/redhat/rhel-5-i386/pgdg-centos91-9.1-4.noarch.rpm
# rpm -ivh pgdg-centos91-9.1-4.noarch.rpm 
念のためyumのリストにあるか確認
# yum list | grep postgresql
んで、インストール
# yum install postgresql91-server.x86_64
今のうちにシステム起動時に自動起動するように設定。
確認、設定、確認。OK。
# chkconfig --list | grep postgres
# chkconfig postgresql-9.1 on
# chkconfig --list | grep postgres
postgresql-9.1  0:off   1:off   2:on    3:on    4:on    5:on    6:off
データベースを初期化。ここでデフォルトのエンコーディングを指定した方が無難。
あとlocaleもこの段階では指定しない方がいいと思われ。
service postgresql-9.1 initdb --no-locale -E UTF8
サービスを起動。ここまでで一応インストール完了。
# service postgresql-9.1.2 start
DB作成ユーザを追加します。必須ではないのですが作った方が管理はしやすいです。
#  useradd dozaemon
postgresの管理者権限にsuして、postgres側にユーザを追加します。
# su - postres
$ createuser dozaemon
今作成したdozaemonで初期データベースを作成します。
$ exit
# su - dozaemon
$ createdb dozaemondb
作成したデータベースを確認するよ。
$ psql -l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 dozaemon  | dozaemon | UTF8     | C       | C     | 
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(4 rows)

でけた!
もしinitdbからやり直したい場合は、データベースデータを丸ごと消してしまえばいいっす。
自分の環境では /var/lib/pgsql/9.1/data/のdataフォルダ毎消しちゃいました。
#自己責任でお願いします。

2012年1月29日日曜日

さくらVPS(CentOS5.7)でRuby1.9.2を入れてしばらくしたらセグメンテーション違反

何もしてないのにRuby1.9.2インストールの数時間後に
$ irb
<internal:lib/rubygems/custom_require>:29:in `require': closed stream (IOError)
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from /usr/lib/ruby/1.9.1/irb.rb:13:in `<top (required)>'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from /usr/bin/irb:9:in `<main>'
とか
$ heroku logs
<internal:lib/rubygems/custom_require>:29: [BUG] Segmentation fault
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

-- control frame ----------
c:0005 p:---- s:0016 b:0016 l:000015 d:000015 CFUNC  :require
c:0004 p:0013 s:0012 b:0012 l:000011 d:000011 METHOD <internal:lib/rubygems/custom_require>:29
c:0003 p:0011 s:0007 b:0007 l:001498 d:000440 EVAL   /usr/bin/heroku:9
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:001498 d:001498 TOP   
---------------------------
-- Ruby level backtrace information ----------------------------------------
/usr/bin/heroku:9:in `<main>'
<internal:lib/rubygems/custom_require>:29:in `require'
<internal:lib/rubygems/custom_require>:29:in `require'

セグメンテーション違反です


とか。

コンソール開きっぱなしにしておいただけでこうなるってどういうこと?
って調べてみると以下が。

http://www.ruby-forum.com/topic/133765

どうやらprelinkがいかんらしい。
なので下記を参考に修正してみた
http://selfkleptomaniac.org/archives/1341
どうだろ?
# vi /etc/prelink.conf.d/ruby.conf
# cat /etc/prelink.conf.d/ruby.conf  
-b /usr/bin/ruby

#2012/2/28追加
まったく問題起こってません♪
Bloggerでソースコードを表示

最低限の使い方ではここが詳しい
http://recoboo.blog28.fc2.com/blog-entry-91.html

テーマの種類はここで
http://memogawari497.blog112.fc2.com/blog-entry-11.html