Whenever の schedule.rb 内の設定を環境ごとに変える

$ bundle exec whenever --update-crontab appname --set environment=production

このように処理を実行した時、schedule.rb の中では、ENV['RAILS_ENV'] で取得できない。

$ config/schedule.rb

require File.expand_path(File.dirname(__FILE__) + "/environment")
require "yaml"

RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + "/..") unless defined?(RAILS_ROOT)
CONFIG = YAML.load_file(RAILS_ROOT + "/config/schedule.yml")[@environment]

set :output, 'log/cron.log'

every 1.minute do
  command "curl #{CONFIG['value']} --tlsv1.0 -k"
end

$ config/schedule.yml

development:
    request: "http://batch/push"

test:
    value: "http://batch/push/hoge"

staging:
    value: "http://batch/push"

production:
    value: "http://aa/batch/push"

Capistrano3 でRailsをデプロイするとき、current_path, shared_path などを正しく取得する

$ config/deploy.rb

# config valid only for current version of Capistrano
lock '3.4.0'

set :application, 'hoge'
set :repo_url, 'git@bitbucket.org:hoge/hoge.git'

# Default value for :scm is :git
set :scm, :git

#
set :rbenv_ruby, '2.2.1'

# Default value for :pty is false
set :pty, true

# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

# Default value for default_env is {}
set :default_env, { rbenv_root: "$HOME/.rbenv/bin/rbenv", path: "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH" }

# Default value for keep_releases is 5
set :keep_releases, 4

# ここ!lambda を使う!
set :unicorn_config_path, -> {"#{current_path}/config/unicorn.rb"}

# ここ!lambda を使う!
set :unicorn_pid, -> {"#{shared_path}/tmp/pids/unicorn.pid"}

$ config/deploy/production.rb

set :stage,            :production
set :rails_env,        :production
set :unicorn_rack_env, :production

# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/var/www/html/hoge'

server 'localhost', user: 'app', roles: %w{web app db

$ Capfile

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

#
require 'capistrano/rbenv'
require 'capistrano/bundler'
#require 'capistrano/rails/assets' # asset_sync を使うため外している
require 'capistrano/rails/migrations'
require 'capistrano3/unicorn'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

heroku の Postgres のデータを、独自サーバーの mysql へ転送する

アップロード先 mysqlのサーバー起動

$ gem install taps
$ gem install mysql
$ taps server mysql://root@localhost/releasenote_stg?encoding=utf8 <basic認証名前> <basic認証パスワード>

5000 番ポートでサーバーが立ち上がります。
サーバーIP アドレスを確認しておく。

コピー元のheroku を操作する

$ gem install taps
$ gem install pg
$ gem install sqlite3
$ heroku config

HEROKU_POSTGRESQL_BRONZE_URL ここの値をコピーしておく。
仮に、postgres://dyzpnfomdzrvtv:HxftJDwDIEdih_VydYcL5CI-pT@ec2-54-204-43-139.compute-1.amazonaws.com:5432/diedpheq6q045 とする。

転送

$ taps push postgres://dyzpnfomdzrvtv:HxftJDwDIEdih_VydYcL5CI-pT@ec2-54-204-43-139.compute-1.amazonaws.com:5432/diedpheq6q045   http://<basic認証名前>:<basic認証パスワード>@<サーバーIPアドレス>:5000/

Mac の VirtualBox で Linux を動かし、どうしても編集に ホストOSのエディタを使いたい場合

自分では使っていないが、やりたい人もいるのでメモ。

VirtualBox 起動、接続まで

mac$ brew install caskroom/cask/brew-cask
mac$ brew cask install virtualbox
mac$ brew cask install vagrant

mac$ vagrant box add centos7 https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box
mac$ vagrant init centos7
mac$ vagrant plugin install vagrant-vbguest
mac$ vagrant up
mac$ vagrant ssh
# ちなみに停止したいときは vagrant halt

LinuxMac の フォルダを共有

mac$ vi Vagrantfile
config.vm.synced_folder "/Users/<UserName>/html", "/var/www/html",
mount_options: ["dmode=777,fmode=755"]
mac$ vagrant reload

Linux の設定をする (省略してある)

vagrant$ yum -y update
vagrant$ yum -y install nginx mysql-server php-fpm php-mysql

vagrant$ systemctl start mysqld
vagrant$ systemctl start nginx
vagrant$ systemctl start php-fpm
vagrant$ systemctl stop firewalld

vagrant$ systemctl enable mysqld
vagrant$ systemctl enable nginx
vagrant$ systemctl enable php-fpm
vagrant$ systemctl disable firewalld

vagrant$ vi /etc/selinux/config
# SELINUX=disabled にする

あとは Mac上でファイルを編集

Carrierwave で 独自のversion ファイルを保存する

例えば、wav ファイルを 保存したときに、別バージョンとして mp3 も保存するような場合。

version :mp3 do

  process :convert_mp3

  # この名前が、最終的なファイル名になる。
  # デフォルトで、 mp3_****.wav になっているので、リネーム
  def full_filename for_file
    super.gsub(/^mp3_/, '').gsub(/wav$/, 'mp3')
  end

end

def convert_mp3

  # current_path に、オリジナルのファイルがあります。
  # この関数を抜けた後に、current_path にあるファイルがコピーされて、最終的な位置に保存されます。

  # 一時的な名前
  out_path = current_path.gsub(/wav$/, 'mp3')

  system("lame -V4 #{current_path} #{out_path}")

  # 元ファイルは削除
  File.unlink current_path
  # 元ファイルの位置に配置
  File.rename out_path, current_path

end

実際にはこれだけの記述であると、tmp ファイルが残ってしまったり、コピーが複数発生したりするので、その設定も追加する。