プログラマでありたい

おっさんになっても、プログラマでありつづけたい

今更ながらGitHubを使ってみる

 遥か昔にGitHubのアカウントを作ってたのですが、そのまま放置しっぱなしでした。サンプルプロジェクトの登録・公開にちょうど良いかなと思うので、使ってみることにしました。ただ使用方法とかすっかり忘れてたので、軽くまとめてみました。結論から言えば、Gitが使えれば簡単に使えます。

GitHubの登録&レポジトリの作成

何はともあれ、まずはGitHubでアカウントを登録しましょう。プランは無料ので良いと思います。

Gitの設定

Gitをインストールしていない場合は、入れておいてください。
後はconfigで名前等を登録してください

$ git config --global user.name "your name"
$ git config --global user.email foo@example.com

SSH公開鍵をgithubに登録

ssh-keygen

デフォルトのままで作ると、$HOME/.ssh/の下にid_rsaとid_rsa.pubが作られます。id_rsaが秘密鍵で、id_rsa.pubが公開鍵です。下記の要領で内容を表示して、コピーします。そしてGitHubのアカウント設定のSSH KeysでAdd New SSH Keyで適当なタイトルをつけて、Keyの部分に貼りつけてください。

cat ~/.ssh/id_rsa.pub

プロジェクトの登録

git init
git add .
git commit -m "initial commit"
git remote add origin git@github.com:username/repositoryname.git
git push origin master 


enjoy!!

RVMを使ってMacで、Ruby 1.8.7と1.9.3を共存

 Mac OSX(Snow Leopard) でのRuby 1.8.7と1.9.3の共存の設定です。開発環境であれば、手軽なRVMを使うと良いと思います。

RVMのインストール

$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

設定の読み込み

$ source ~/.bash_profile

必要なモジュールの確認
|

$ rvm requirements

Notes for Mac OS X 10.6.8, Xcode 4.2.

For MacRuby: Install LLVM first.

For JRuby: Install the JDK. See http://developer.apple.com/java/download/ # Current Java version "1.6.0_26"
For IronRuby: Install Mono >= 2.6
For Ruby 1.9.3: Install libksba # If using Homebrew, 'brew install libksba'

You can use & download osx-gcc-installer: https://github.com/kennethreitz/osx-gcc-installer

NOTE: Currently, Node.js is having issues building with osx-gcc-installer. The only fix is to install Xcode over osx-gcc-installer.

To use an RVM installed Ruby as default, instead of the system ruby:

rvm install 1.8.7 # installs patch 357: closest supported version
rvm system ; rvm gemset export system.gems ; rvm 1.8.7 ; rvm gemset import system.gems # migrate your gems
rvm alias create default 1.8.7

And reopen your terminal windows.

Xcode 4.2:
* is only supported by ruby 1.9.3+ using command line flag: --with-gcc=clang
* it breaks gems with native extensions, especially DB drivers.

Required Xcode Version 3.2.1 (1613) or later, such as 3.2.6 or Xcode 4.1.

You should download the Xcode tools from developer.apple.com, since the Snow Leopard dvd install contained bugs.

Xcode 4.2 users - please be warned -
in case of any compilation issues
* downgrade to Xcode 4.1
* or install osx-gcc-installer
and reinstall your rubies.

足りないモジュールがあったら、インストールしておきます。

RVM上にrubyのインストール

$ rvm install 1.9.3


MacPortでOpenSSLを入れている場合は、http.rbでSegmentation faultを起こす場合があります。その際は、下記の手順でどうぞ。

rvm pkg install iconv
rvm pkg install openssl
rvm install ruby-1.9.3 --with-openssl-dir=~$rvm_path/usr --with-iconv-dir=$rvm_path/usr 

インストール済みのrubyの一覧

$ rvm list

rvm rubies

=> ruby-1.9.3-p0 [ x86_64 ]

# Default ruby not set. Try 'rvm alias create default <ruby>'.

# => - current
# =* - current && default
#  * - default

切り替え色々

バージョン指定で使用

$ rvm use 1.9.3

デフォルトの設定

$ rvm --default use 1.9.3

デフォルトのversionを使用する

$ rvm default

システム設定のrubyに戻す

$ rvm system

gemのインストール

rootではなく、rvm使用中のユーザ権限で実行してください。

$gem install rails


非常に簡単です。 enjoy!!

RVM: Ruby Version Manager - Installing RVM

今更聞けないCapistranoでリリースの自動化

 ここ数年で開発の現場でAgile開発の文化や手法が、ずいぶんと取り入れられるようになってきているようです。アジャイル開発はその根底に文化が大事ですが、それを支えるツールというのも重要になってきます。ソース管理やビルド管理、テストの自動化と色々ありますが、今回はリリースの自動化のお話です。その中で主にRails使われることが多いCapistranoの設定と使い方です。

環境の説明

・Ruby 1.9
・Rails 3.2
・Passenger
・Git
・SQLite3

目指す構成

 今回は単純化する為に、1サーバの中にGitのリポジトリもApache+PassengerもDBも入れておきます。また複数の環境(開発、ステージング、本番)にデプロイ出来るように、それぞれの構成を別けて記述するようにします。(capistrano-extを使用)

設定

必要モジュールのインストール

$gem install capistrano
$gem install capistrano-ext
$gem install capistrano_colors

また、Gemfileの中に追記しておくと良いと思います。

$ vi Gemfile
group :deployment do
  gem 'capistrano'
  gem 'capistrano-ext'
  gem 'capistrano_colors'
end
$ bundle install


サンプルのプロジェクトの作成

$ rails new capistrano-sample
$ cd capistrano-sample
$ rails g model item name:string price:integer
$ rails g controller items recent
$ git init
$ git add .
$ git commit -m "initial import"


capistranoのひな形ファイルの作成

$ capify .
[add] writing './Capfile'
[add] writing './config/deploy.rb'
[done] capified!


ひな形を元に編集

vi config/deploy.rb

基本方針として、共通設定をconfig/deploy.rbに記述します。
環境ごとの差分をconfig/deploy/環境名.rbに記述します。
config/deploy.rb
/deploy/development.rb
/staging.rb
/production.rb


変更箇所は、次のような感じです。
deploy.rb

require 'capistrano/ext/multistage'

set :application, "capistrano sample"

# リポジトリの設定
set :repository,  "git+ssh://localhost/path/to/develop/capistrano-sample"
set :scm, :git

# RVMを利用時の設定
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require 'rvm/capistrano'
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :user

# ユーザの設定
set :user, 'username'
set :use_sudo, false 
default_run_options[:pty] = true

def restart_file
  File.join(current_path, 'tmp', 'restart.txt')
end

namespace :deploy do
  task :restart, :roles => :app do
    run "touch #{restart_file}"
  end
end


deplpy/development.rb

set :rails_env, 'development'

role :web, "localhost"
role :app, "localhost"
role :db,  "localhost", :primary => true

set :deploy_to, '/var/www/capistrano_sample/development/' 

deploy/production.rb

set :rails_env, 'production'

role :web, "localhost"
role :app, "localhost"
role :db,  "localhost", :primary => true

set :deploy_to, '/var/www/capistrano_sample/production/' 

デプロイ

開発環境にデプロイ

$ cap development deploy:setup
$ cap development deploy:cold


本番環境にデプロイ

$ cap production deploy:setup
$ cap production deploy:cold


コマンド一覧

$ cap -T
cap bundle:install        # Install the current Bundler environment.
cap deploy                # Deploys your project.
cap deploy:check          # Test deployment dependencies.
cap deploy:cleanup        # Clean up old releases.
cap deploy:cold           # Deploys and starts a `cold' application.
cap deploy:create_symlink # Updates the symlink to the most recently deployed...
cap deploy:migrate        # Run the migrate rake task.
cap deploy:migrations     # Deploy and run pending migrations.
cap deploy:pending        # Displays the commits since your last deploy.
cap deploy:pending:diff   # Displays the `diff' since your last deploy.
cap deploy:rollback       # Rolls back to a previous version and restarts.
cap deploy:rollback:code  # Rolls back to the previously deployed version.
cap deploy:setup          # Prepares one or more servers for deployment.
cap deploy:start          # Blank task exists as a hook into which to install...
cap deploy:stop           # Blank task exists as a hook into which to install...
cap deploy:symlink        # Deprecated API.
cap deploy:update         # Copies your project and updates the symlink.
cap deploy:update_code    # Copies your project to the remote servers.
cap deploy:upload         # Copy files to the currently deployed version.
cap deploy:web:disable    # Present a maintenance page to visitors.
cap deploy:web:enable     # Makes the application web-accessible again.
cap development           # Set the target stage to `development'.
cap invoke                # Invoke a single command on the remote servers.
cap multistage:prepare    # Stub out the staging config files.
cap production            # Set the target stage to `production'.
cap shell                 # Begin an interactive Capistrano session.

Some tasks were not listed, either because they have no description,
or because they are only used internally by other tasks. To see all
tasks, type `cap -vT'.

Extended help may be available for these tasks.
Type `cap -e taskname' to view it.


 今回の構成では、同一サーバに開発・本番環境とパスを別けていました。実際のプロジェクトでは、role :web,:app,:db等で別のサーバを指定してください。DBの部分も、rakeのmigrateの設定をちゃんと記述していれば問題ないです。
 
See Also:
Capistranoのタスク一覧
ChefとCapistranoの素敵な関係。或いはレイヤーの違いの話


参考:
Capistrano 実践Tips集
さくらVPS Capistrano編 - プログラミングノート
Capistranoとcapistrano-ext - 祈れ、そして働け 〜 Ora et labora