herokuでsmtpを使うためにsendgrid addonを入れる。(smtpにgmailを使う場合は要らない)

$ heroku addons:add sendgrid:starter
# config/environment.rb:

(snip)

ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',                                                                                            
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],                                                                         
  :password       => ENV['SENDGRID_PASSWORD'],                                                                         
  :domain         => 'heroku.com',
  :enable_starttls_auto => true
}
# config/initializers/exception_notification.rb:
AppName::Application.config.middleware.use ExceptionNotification::Rack,
  :email => {
    :email_prefix => "[AppName] ",
    :sender_address => %{"notifier" <notifier@example.com>},
    :exception_recipients => %w{komagata@example.com}
  }
# Gemfile:
gem 'exception_notification'
# config/environments/production.rb:

(snip)

  # config.action_mailer.raise_delivery_errors = false
  config.action_mailer.raise_delivery_errors = true

(snip)

  config.action_mailer.perform_deliveries = true
end

modelのscope書く時、現在時間を使う時はテストしやすいようにデフォルト値ありの引数を与えることにしてるんだけど、

「あれ、引数だけ先に評価されちゃわなかったっけな?」

と心配になったので試してみた。

foo = ->(now = Time.now) {
  puts now, Time.now
}

sleep 10

foo.call
% ruby arg.rb
2013-07-11 16:07:16 +0900
2013-07-11 16:07:16 +0900

杞憂に終わった。

Sunzi: Server provisioning utility for minimalists

"The supreme art of war is to subdue the enemy without fighting." - Sunzi

いわゆるchefのようなサーバープロビジョニングツール。

特徴

  • レシピはshell script
  • 冪等性?犬にくれてやれ!
  • root前提
  • デフォルト設定との差分だけを設定する
  • サーバー側に入れなきゃいけないソフトは無し(shell scriptだから)

ミニマリスト向けツールに見えますが意外と高機能で、基本的にknife-soloのようにクライアントからサーバーに対して使うの前提だし、berkshelfのようにリモートのrecipeを取り込む機能があるし、erbのテンプレートやweb, dbみたいなrole機能もあります。

そもそも俺はdebian(今だったらwheezy)しか使わないし、冪等性とか要らないし、VPSをサクッと設定したいだけなのでちょうどハマりました。

recipes/ruby.sh:

apt-get install -y ruby1.9.3

例えばrubyのrecipeはこんな感じ(少しだけ賢いsunzi.installの方がおすすめですが)。shell scriptなので他人のrecipeがいきなり読めるのがおっさんエンジニアには嬉しい。ちょっとした設定書き換えをsedでやってるの見るとホッとします。

これだったらちょこっと設定してcapistranoでrailsアプリデプロイするとこまで道筋がすごく見えやすいです。

vagrantでsandbox rollbackしながらrecipeを書いてくのが快適です。

疑問

仕事に使おうとしてるんですがいくつか疑問点が。

  • 鍵認証のrootユーザー、sshはデフォルトの22番で運用するのが普通?
  • capistranoでdeploy(=appサーバーの実行者)もrootユーザーでやるの?
  • ソフトのバージョンアップとかはどういう感じでやるの?

$ gem install icalendar
requre 'icalendar'

calendar = Icalendar::Calendar.new                                                                                 
event = Icalendar::Event.new                                                                                       
event.start = Date.new(2013, 7, 1)
event.end = Date.new(2013, 7, 8)
event.summary = "Foo Festival"
event.description = "Greatest event in the world."
event.location = "Hatsudai Tokyo Japan"
calendar.add_event(event)

puts calendar.to_ical

railsでリンクをクリックしたらiCalファイルダウンロード → iCal起動+登録ってやりたい場合。

class FooController < ApplicationController
  def calendar
    calendar = Icalendar::Calendar.new

    # implementation...

    headers['Content-Type'] = 'text/calendar; charset=UTF-8'
    render :inline, layout: false, text: calendar.to_ical
  end
end
$ heroku labs:enable user-env-compile

これすると行ける。理由はわからん・・・。

怖話(こわばな)でラジオにCMを出しました。

怖話が山口敏太郎のラジオ番組に進出!ラジオCMになりました! - 怖話からのお知らせ

世界最大の怖い話サイト「怖話」がラジオに進出!

オカルト評論家 山口敏太郎のラジオ番組内でラジオCMが放送されます!

以前発表いたしました「怖い話グランプリ」の期待の高まるなか、タッグを組んでいる山口敏太郎のラジオ番組「日本大好き」に怖話のラジオCMが流れることになりました!

音源は@machidaさんが知り合いに声も録音して作ってくれました。

@machidaさんはWebデザインよりDTM歴の方が圧倒的に長いのでメインで使われてるBGMも自作です。PS版弟切草のCMのイメージで作ったと言ってました。

ハマりポイント。

VMを壊す例として$ sudo rm -rf / --no-preserve-rootすると/vagrantにマウントされてるMac上のディレクトリまでVagrantfileごと消えるのでおすすめできない。

vagrantのplugin、saharaを使う

saharaを入れる。

% vagrant plugin install sahara

sandboxモードに入る。

% vagrant sandbox on

壊す。

% vagrant ssh
$ sudo rm -rf /usr
$ ls /usr
ls: cannot access /usr: No such file or directory
$ exit

戻す。

% vagrant sandbox rollback
% vagrant ssh
$ ls /usr/
bin  games  include  lib  local  sbin  share  src

ワオ、戻ってる!

昨日、隣の仙川でねじり鉢巻した建設関係っぽい軽トラのおっさんが、前にいるチャリンコのおっさんに対して、

「道の真ん中でフラフラと走ってんじゃねーよ!」

と非常に大きな声でスムーズに文句言ってた。

「ったく、道の真ん中でフラフラしてんじゃねーよ!」

あ、二回言った。

ものすごく怒ってるという感じではなくて、単に思ってることを一番気持ちいい声量で発声してる感じ。

あんな風に思ってることを微塵の遠慮もなく表明できたらストレス溜まらないだろうなあ。

現状渡しされてパスワードがわからない時。

mysql> update wp_users set user_pass = '$P$BQ6n8cNLFJBJyxYYoPK3bDWymBXILO.' where user_login = 'admin';

これでadminユーザーのパスワードがfooになります。

置いておきます。

https://dl.dropboxusercontent.com/u/188423/wheezy.box

VirtualBoxでVM作成

OS
Linux
バージョン
Debian
メモリ
512MB
オーディオ
無効
USB
無効
ストレージ
VMDK 32GB

Debian wheezyをインストール

rootパスワード: vagrant

vagrantパスワード: vagrant

VirtualBox Guest Additionのインストール

# apt-get install build-essential module-assistant sudo ssh ca-certificates zerofree
# m-a prepare
# reboot

Virtualboxのメニュー > Device > Install Guest Additions

# mount /media/cdrom
# sh /media/cdrom/VBoxLinuxAdditions.run

その他色々

# visudo
%sudo ALL=(ALL) NOPASSWD:ALL
# gpasswd -a vagrant sudo
# mkdir /home/vagrant/.ssh
# cd /home/vagrant/.ssh
# wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub
# mv vagrant.pub authorized_keys
# chmod 700 /home/vagrant/.ssh
# chown -R vagrant:vagrant /home/vagrant/.ssh
# echo 'UseDNS no' >> /etc/ssh/sshd_config

イメージサイズ縮小

# apt-get clean
# rm -rf /usr/src/vboxguest*
# rm -rf /usr/share/doc
# find /var/cache -type f -exec rm -rf {} \;
# rm -rf /usr/share/locale/{af,am,ar,as,ast,az,bal,be,bg,bn,bn_IN,br,bs,byn,ca,cr,cs,csb,cy,da,de,de_AT,dz,el,en_AU,en_CA,eo,es,et,et_EE,eu,fa,fi,fo,fr,fur,ga,gez,gl,gu,haw,he,hi,hr,hu,hy,id,is,it,ja,ka,kk,km,kn,ko,kok,ku,ky,lg,lt,lv,mg,mi,mk,ml,mn,mr,ms,mt,nb,ne,nl,nn,no,nso,oc,or,pa,pl,ps,pt,pt_BR,qu,ro,ru,rw,si,sk,sl,so,sq,sr,sr*latin,sv,sw,ta,te,th,ti,tig,tk,tl,tr,tt,ur,urd,ve,vi,wa,wal,wo,xh,zh,zh_HK,zh_CN,zh_TW,zu}
# init 1
# mount -o remount,ro /dev/sda1
# zerofree /dev/sda1
# halt

box作成

Macで。

% vagrant package --base wheezy