Sublime Textの教科書とチーム開発実践入門
Web制作者のためのSublime Textの教科書 今すぐ最高のエディタを使いこなすプロのノウハウ
- 作者: 上野正大,杉本淳,前川昌幸,森田壮,こもりまさあき
- 出版社/メーカー: インプレスジャパン
- 発売日: 2014/03/20
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (1件) を見る
チーム開発実践入門 ~共同作業を円滑に行うツール・メソッド (WEB+DB PRESS plus)
- 作者: 池田尚史,藤倉和明,井上史彰
- 出版社/メーカー: 技術評論社
- 発売日: 2014/04/16
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (2件) を見る
このところSublimeTextを使っていて、各所で絶賛される意味が分かってきたのでもうちょっと便利な使い方はないかなと思い買ってみました。主にMarkdownとRubyを書くときに使っていますが、まだちゃんと使いこなせていないので。
チーム開発実践入門の方は 著者の池田さんがブログで仰ってた内容に惹かれて買ってみました。以下当該部分を引用します。
むしろのこの『チーム開発実践入門』という本は、『GitHub実践入門』、『Jenkins実践入門』、『JUnit実践入門』といった書籍の間をつなぐミッシングリンクのような本です。
最近現場で段々とチームメンバーが増えており、プロジェクトを上手に推進させていくにはどうしたら良いかと考えることが多くなってきたので、ここらでチーム開発とはなんたるかをもう一度しっかりと考えてみたいと思います。
チケット駆動開発とGit逆引き入門
- 作者: 小川明彦,阪井誠
- 出版社/メーカー: 翔泳社
- 発売日: 2012/08/24
- メディア: 大型本
- クリック: 31回
- この商品を含むブログ (7件) を見る
- 作者: 松下雅和,船ヶ山慶,平木聡,土橋林太郎,三上丈晴
- 出版社/メーカー: シーアンドアール研究所
- 発売日: 2014/04/09
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (4件) を見る
チケット駆動開発はだいぶ前に買っていましたが載せてなかったので。最近サービスの保守・運用でうまく情報共有できていないことが発覚したため、開発チームだけではなく関係者全員(企画や他のプロジェクトも含めて)でチケットベースでやりとりが出来ないかな、と考えています。今は情報が散見していて、一目でいまこのサービスに何が起こっているのか、過去にどんなことが起こっていたのかが分からず、また不具合があっても担当者間でのメールベースのやりとりで終わってしまっているので、これをもうちょっとどうにかしたいと思ってます。開発チーム内ではチケット駆動が上手くいってきているので、うまく流用というかパターンに組み込めると良いなと思ってるんですが、はてさてどうなるか。
Git逆引き入門はまだイマイチやりたいことと実際のコマンドが結びつかないことがあるので、あったら便利そうだと思い買ってみました。で、本に書かれているSourceTreeというソフトが便利そうだったので早速入れてみようと思います。個人的にはCUIの方が好きなんですが、チームに導入しようとするとGUIの方が受け入れられやすいんですよね。SourceTreeには専用ターミナルも付属しているとのことなので、CUI/GUIのどちらでも使えるというのはいいなと思います。
ntpの導入をTDDライクにやってみる
serverspecでテストを実行できるようになったので、ntpの導入をTDDライクに進めながらやってみようと思います。まずはテストケースから。
describe package('ntp') do it { should be_installed } end describe file('/etc/ntp.conf') do it { should be_file } its(:content) { should match /server ntp.nict.jp/} end describe service('ntpd') do it { should be_enabled } it { should be_running } end
実行してみると当然失敗します。
Failed examples: rspec ./spec/default/base_spec.rb:63 # Package "ntp" should be installed rspec ./spec/default/base_spec.rb:67 # File "/etc/ntp.conf" should be file rspec ./spec/default/base_spec.rb:68 # File "/etc/ntp.conf" content should match /server ntp.nict.jp/ rspec ./spec/default/base_spec.rb:72 # Service "ntpd" should be enabled rspec ./spec/default/base_spec.rb:73 # Service "ntpd" should be running C:/Ruby193/bin/ruby.exe -S rspec spec/default/base_spec.rb failed
次にレシピですが、きっとopscodeにあると思って検索してみたらやっぱりありました。なのでこちらを使ってみます。まずはBerksfileに追加。
# chef-repo/Berksfile site :opscode (中略) cookbook 'ntp'
で、インストール。
berks install --path cookbooks
これでcookbooksにntpが追加されたので、個別のパラメータを追加しています。設定はattributes/default.rbに行います。serversを追加するのと同期を行うような設定を追加します。serversは公開NTPサーバを2つ追加しておきます。
# default attributes for all platforms #default['ntp']['servers'] = [] # The default recipe sets a list of common NTP servers (COOK-1170) default['ntp']['servers'] = ['ntp.nict.jp', 'ntp.jst.mfeed.ad.jp'] # The default recipe sets a list of common NTP servers (COOK-1170) default['ntp']['peers'] = [] default['ntp']['restrictions'] = [] # internal attributes (中略) #default['ntp']['sync_clock'] = false default['ntp']['sync_clock'] = true
ここまで来て気付いたんですが、タイムゾーンの設定を行わなきゃダメそうです。CentOSの場合、タイムゾーンの設定は/etc/sysconfig/clockを書き換えてやれば良さそうなので、まずは今の状態を確認しておきます。
[vagrant@localhost ~]$ cat /etc/sysconfig/clock ZONE="UTC"
で、これを下記のように書き換えるようにして、レシピのtemplates/default/clock.erbとして保存しておきます。
# clock.erb ZONE="Asia/Tokyo" UTC="false"
次にrecipes/default.rbをこのファイルを使うように編集します。
file "/etc/localtime" do content IO.read("/usr/share/zoneinfo/Asia/Tokyo") action :nothing end template "/etc/sysconfig/clock" do source 'clock.erb' owner 'root' group 'root' mode '0644' notifies :create, resources(:file => "/etc/localtime"), :immediately end log "done copy to /etc/sysconfig/clock."
clock.erbを書き換えた後に/usr/share/zoneinfo/Asia/Tokyoを/etc/localtimeにコピーするようにしてみました。executeリソース側でacitonが:nothingとなっているのはtemplateリソースから呼ばれたときだけ実行されるようにです。
最後にVagrantfileにntpのレシピを使うように定義します。
config.vm.provision "chef_solo" do |chef| chef.cookbooks_path = ["./chef-repo/cookbooks", "./chef-repo/site-cookbooks"] chef.add_recipe "yum" chef.add_recipe "rbenv" chef.add_recipe "ruby_env" chef.add_recipe "nodejs" chef.add_recipe "ntp" end
これでvagrant provision
を実行します。
vagrant provision INFO: template[/etc/sysconfig/clock] backed up to /var/chef/backup/etc/sysconfig/clock.chef-20140402081837.835605 INFO: template[/etc/sysconfig/clock] updated file contents /etc/sysconfig/clock INFO: template[/etc/sysconfig/clock] sending createaction to file[/etc/localtime] (immediate) INFO: file[/etc/localtime] backed up to /var/chef/backup/etc/localtime.chef-20140402081838.206681 INFO: file[/etc/localtime] updated file contents /etc/localtime INFO: done copy to /etc/sysconfig/clock. INFO: package[ntp] installing ntp-4.2.6p5-1.el6.centos from base repository INFO: cookbook_file[/etc/ntp.leapseconds] created file /etc/ntp.leapseconds INFO: cookbook_file[/etc/ntp.leapseconds] updated file contents /etc/ntp.leapseconds INFO: cookbook_file[/etc/ntp.leapseconds] owner changed to 0 INFO: cookbook_file[/etc/ntp.leapseconds] group changed to 0 INFO: cookbook_file[/etc/ntp.leapseconds] mode changed to 644 INFO: template[/etc/ntp.conf] backed up to /var/chef/backup/etc/ntp.conf.chef-20140402171846.501042 INFO: template[/etc/ntp.conf] updated file contents/etc/ntp.conf INFO: execute[Stop ntpd in preparation for ntpdate]ran successfully INFO: execute[Stop ntpd in preparation for ntpdate]sending stop action to service[ntpd] (immediate) INFO: execute[Force sync system clock with ntp server] ran successfully INFO: service[ntpd] enabled INFO: service[ntpd] started INFO: template[/etc/ntp.conf] sending restart action to service[ntpd] (delayed) INFO: service[ntpd] restarted INFO: execute[Force sync system clock with ntp server] sending start action to service[ntpd] (delayed) INFO: Chef Run complete in 60.733069975 seconds INFO: Running report handlers INFO: Report handlers complete
templateとファイルコピーが正常に行われ、その後にntpのインストールが動作しているのが分かります。続いてテストの実行ですが、タイムゾーンの設定を追加したのでテストケースも追加しておきます。また公開NTPサーバも2つにしたのでそれのテストも増やします。
describe file('/etc/sysconfig/clock') do it { should be_file } its(:content) { should match /ZONE="Asia\/Tokyo"/ } its(:content) { should match /UTC="false"/ } end describe file('/etc/ntp.conf') do it { should be_file } its(:content) { should match /server ntp.nict.jp/} its(:content) { should match /server ntp.jst.mfeed.ad.jp/} end
うーん、2行になってしまっていますが、この書き方で良いんでしょうか。もうちょっとスマートな方法がありそうですがここはこのまま実行してみます。
rake spec C:/Ruby193/bin/ruby.exe -S rspec spec/default/base_spec.rb ......................... Finished in 33.55 seconds 25 examples, 0 failures
無事テストが通ってntpのインストールとタイムゾーンの設定がうまくいったようです。最後に実際に時間が変更されているか確認します。
[vagrant@localhost ~]$ date Wed Apr 2 22:54:12 JST 2014
おぉ、タイムゾーンも変更されて時間もちゃんと設定されました:-) ただあんまりTDDライクに進められた気がしないので、次はもうちょっとちゃんと進められたらいいなぁ。
参考にさせて頂いたページ
digをインストールする
チュートリアル環境にnslookup
もdig
も入っていなかったのでインストールしておきます。
[vagrant@localhost ~]$ dig -bash: dig: command not found [vagrant@localhost ~]$ nslookup -bash: nslookup: command not found [vagrant@localhost ~]$
[CentOS] digコマンドをインストールするの巻 - TrippyBoyの愉快な日々 を参考にすると、bind-utilsを入れればいいことが分かったのでレシピとテストケースに追加します。
# chef-repo/site-cookbooks/ruby_env/recipes/default.rb package "bind-utils" do action :install end
# spec/default/base_spec.rb describe package('bind-utils') do it { should be_installed } end
あとはvagrant provision
してテストケースを実行するだけ。
cd rails3_tutorial vagrant provision rake spec C:/Ruby193/bin/ruby.exe -S rspec spec/default/base_spec.rb ................ Finished in 33.19 seconds 16 examples, 0 failures
テストが通ったので実際に確認。
[vagrant@localhost ~]$ which dig /usr/bin/dig [vagrant@localhost ~]$ which nslookup /usr/bin/nslookup
無事にインストールできました:-)
あとどうでも良いことですが、記事の書き方をはてな記法からmarkdown記法に変更してみました。GitHubでも使えるし覚えておく必要があるかなと。この書き方はかなり楽ですね。改行が半角スペース2つっていうのが微妙に面倒ですが。
serverspecのテストケースを育ててみた
前回の記事で作成したserverspecのテストケースを育ててみました。Ruby on Railsチュートリアルの写経を始めました - m-namikiの日記のときに作成したレシピを対象にします。レシピはこんなことをやっていました。
- iptablesの停止、無効化
- epel、remiをyumのリポジトリに追加
- sqlite-develのインストール
- rbenvでruby1.9.3-p545のインストール
- gemでrbenv-rehash、rails3.2.14のインストール
で、追加したテストケースはこちら。
# package for rails3-tutorial describe package('sqlite-devel') do it { should be_installed } end # ruby versions describe command('ruby -v') do it { should return_stdout /ruby 1\.9\.3p545.+/ } end # for gem packages describe package('rbenv-rehash') do it { should be_installed.by('gem') } end describe package('rails') do it { should be_installed.by('gem').with_version('3.2.14') } end
epel、remiに関しては最初に書いたときに入れていたので割愛します。で、さっそく実行してみると
C:\Users\m-namiki\Documents\20_vagrant\rails3_tutorial>rake spec C:/Ruby193/bin/ruby.exe -S rspec spec/default/base_spec.rb ..........FFF Failures: 1) Command "ruby -v" should return stdout /ruby 1\.9\.3p545.+/ Failure/Error: it { should return_stdout /ruby 1\.9\.3p545.+/ } sudo ruby -v ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux] expected Command "ruby -v" to return stdout /ruby 1\.9\.3p545.+/ # ./spec/default/base_spec.rb:36:in `block (2 levels) in <top (required)>' 2) Package "rbenv-rehash" should be installed Failure/Error: it { should be_installed.by('gem') } sudo gem list --local | grep -w -- \^rbenv-rehash expected Package "rbenv-rehash" to be installed # ./spec/default/base_spec.rb:41:in `block (2 levels) in <top (required)>' 3) Package "rails" should be installed Failure/Error: it { should be_installed.by('gem').with_version('3.2.14') } sudo gem list --local | grep -w -- \^rails | grep -w -- 3.2.14 expected Package "rails" to be installed # ./spec/default/base_spec.rb:45:in `block (2 levels) in <top (required)>' Finished in 27.6 seconds 13 examples, 3 failures Failed examples: rspec ./spec/default/base_spec.rb:36 # Command "ruby -v" should return stdout /r uby 1\.9\.3p545.+/ rspec ./spec/default/base_spec.rb:41 # Package "rbenv-rehash" should be installe d rspec ./spec/default/base_spec.rb:45 # Package "rails" should be installed C:/Ruby193/bin/ruby.exe -S rspec spec/default/base_spec.rb failed
残念、失敗!エラーメッセージを見るとsudo
しているので、rbenvでインストールしたrubyのバージョンが違ったり、gemで認識されていなかったりするようです。なのでまずはvagrantユーザで実行しているruby
とgem
はどこを見ているのか確認してみます。
[vagrant@localhost ~]$ which ruby /opt/rbenv/shims/ruby [vagrant@localhost shims]$ which gem /opt/rbenv/shims/gem
なるほど、ならばリソースタイプがcommandの場合はこのパスを追加してあげれば良さそうですが、packageの場合はどうしよう。答えは公式ドキュメントに書いてありました。ページ下段の方にあるBlock scoped PATH environment variableを読んでみるとブロック内でPATHを指定することができるようです。なので以下のように書き換えてみました。
# ruby versions describe command('ruby -v') do let(:path) { '/opt/rbenv/shims' } it { should return_stdout /ruby 1\.9\.3p545.+/ } end # for gem packages describe package('rbenv-rehash') do let(:path) { '/opt/rbenv/shims' } it { should be_installed.by('gem') } end describe package('rails') do let(:path) { '/opt/rbenv/shims' } it { should be_installed.by('gem').with_version('3.2.14') } end
同じ設定を3回書いてるのがちょっとアレですが、これで実行してみます。
C:\Users\m2-namiki\Documents\20_vagrant\rails3_tutorial>rake spec C:/Ruby193/bin/ruby.exe -S rspec spec/default/base_spec.rb ............... Finished in 29.69 seconds 15 examples, 0 failures
今度は無事に成功しました。公式ドキュメントは隅々まで読まなきゃダメですね。。。あとはChefでrbenvを使ってRubyをインストールするCookbookを書いた | ひげろぐを参考にさせていただき、最終的なテストケースはこんな感じになりました。
serverspecをvagrantと連携させるためのプラグイン(vagrant-serverspec)もあるそうなので、今度はこちらも試してみようと思います。
serverspecを書いてみた
ここら辺を調べてるときにチラチラと見かけたserverspecが気になっていたので調べてみました。serverspec - Homeはサーバの状態・設定をテストするためのプロダクト、のようです。なので、Railsチュートリアル環境に対してのテストを書いてみたいと思います。
serverspecのインストール
serverspecはgemで提供されているので、まずはホームディレクトリにGemfileを作成します。
// ~/Gemfile source 'https://rubygems.org' gem "serverspec", "1.0.0"
続いてインストール。
C:\Users\m-namiki>bundle install Using diff-lcs 1.2.5 Using highline 1.6.21 Using net-ssh 2.8.0 Using rspec-core 2.14.8 Using rspec-expectations 2.14.5 Using rspec-mocks 2.14.6 Using rspec 2.14.1 Using specinfra 1.0.1 Using serverspec 1.0.0 Using bundler 1.6.0 Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
無事インストールされたので、Railsチュートリアル環境のディレクトリで初期設定を行います。
C:\Users\m-namiki>cd vagrant\rails3-tutorial C:\Users\m-namiki\vagrant\rails3-tutorial>serverspec-init Select OS type: 1) UN*X 2) Windows Select number: 1 Select a backend type: 1) SSH 2) Exec (local) Select number: 1 Vagrant instance y/n: y Auto-configure Vagrant from Vagrantfile? y/n: y + spec/ + spec/default/ + spec/default/httpd_spec.rb + spec/spec_helper.rb + Rakefile
テストコードを書く
Rakefileを見るとspec/以下にある*_spec.rbを実行対象とするようです。なので、デフォルトで作られるhttpd_spec.rbは削除して、新たにbase_spec.rbを書きます。
require 'spec_helper' describe yumrepo('epel') do it { should exist } it { should be_enabled } end describe yumrepo('remi') do it { should exist } it { should be_enabled } end
yumリポジトリにepelとremiが存在するかどうかのテストケースを作成してみました。上記でyumrepoと書いてある部分をResourceTypeといい、他にpackageとかserviceとか色々あるようです。詳細はserverspec - Resource Typesに記載されているので、後々確認してみます。
テスト実行
テストケースが完成したのでさっそく実行。
C:\Users\m-namiki\vagrant\rails3-tutorial>rake spec C:/Ruby193/bin/ruby.exe -S rspec spec/default/base_spec.rb Unable to find ~/.rspec because the HOME environment variable is not set FFFF Failures: 1) Yumrepo "epel" Failure/Error: Unable to find matching line from backtrace ArgumentError: non-absolute home non-absolute home (中略) Finished in 0.002 seconds 4 examples, 4 failures Failed examples: rspec ./spec/default/base_spec.rb:4 # Yumrepo "epel" rspec ./spec/default/base_spec.rb:5 # Yumrepo "epel" rspec ./spec/default/base_spec.rb:9 # Yumrepo "remi" rspec ./spec/default/base_spec.rb:10 # Yumrepo "remi" C:/Ruby193/bin/ruby.exe -S rspec spec/default/base_spec.rb failed
見事に失敗。メッセージを読むと環境変数HOMEがないから.rspecが見つからないよと言われてるようです。なので確認してみます。
C:\Users\m-namiki\vagrant\rails3-tutorial>set HOME HOME=%USERPROFILE% HOMEDRIVE=C: HOMEPATH=\Users\m-namiki C:\Users\m-namiki\vagrant\rails3-tutorial>set USERPROFILE USERPROFILE=C:\Users\m-namiki
環境変数HOMEは設定されていますが、Windows的な何かじゃないのかもしれない。。。なのでRakefileに定義してみました。
require 'rake' require 'rspec/core/rake_task' # ここにHOMEを追記 ENV['HOME'] = 'C:\Users\m-namiki' RSpec::Core::RakeTask.new(:spec) do |t| t.pattern = 'spec/*/*_spec.rb' end task :default => :spec
で、改めて実行してみると
C:\Users\m-namiki\vagrant\rails3-tutorial>rake spec C:/Ruby193/bin/ruby.exe -S rspec spec/default/base_spec.rb .... Finished in 25.05 seconds 4 examples, 0 failures
今度は無事に成功しました。まだ簡単なテストしか書いていないので次はもうちょっと色々なケースを追加してみようと思います。
参考にさせて頂いたページ
heroku toolbeltのインストールでハマった話
Ruby on Rails チュートリアル:実例を使って Rails を学ぼうの1.4節でHerokuにデプロイするというのがありますが、色々ハマったのでメモしておきます。
CentOS6.5でHeroku Toolbeltからtoolbetをダウンロードする場合、Standaloneを選択して下に表示されたwget -qO- https://toolbelt.heroku.com/install.sh | sh
を実行すれば良いはず。なので、まずはその通りにやってみました。
[vagrant@localhost ~]$ wget -qO- https://toolbelt.heroku.com/install.sh | sh [vagrant@localhost ~]$ heroku login -bash: heroku: command not found
が、インストールされていない。。。ので、「CentOSにheroku toolbeltをインストール」が失敗する場合の対処を参考に、証明書を無視する形で実行してみました。
[vagrant@localhost ~]$ wget --no-check-certificate https://toolbelt.heroku.com/install.sh --2014-03-27 03:06:26-- https://toolbelt.heroku.com/install.sh Resolving toolbelt.heroku.com... 23.21.54.126, 23.23.227.87, 50.19.233.212 Connecting to toolbelt.heroku.com|23.21.54.126|:443... connected. WARNING: certificate common name www.heroku.comtoolbelt.heroku.cominstall.shinstall.sh [vagrant@localhost ~]$ sudo sh ./install.sh This script requires superuser access to install software. You will be prompted for your password by sudo. Add the Heroku CLI to your PATH using: $ echo 'PATH="/usr/local/heroku/bin:$PATH"' >> ~/.profile Installation complete [vagrant@localhost ~]$ echo 'PATH="/usr/local/heroku/bin:$PATH"' >> .bashrc [vagrant@localhost ~]$ cat .bashrc # .bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # User specific aliases and functions PATH="/usr/local/heroku/bin:$PATH" [vagrant@localhost ~]$ source .bashrc
で、実行してみると
[vagrant@localhost ~]$ heroku version /opt/rbenv/versions/1.9.3-p545/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- heroku-api (LoadError) from /opt/rbenv/versions/1.9.3-p545/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from /usr/local/heroku/lib/heroku/client/organizations.rb:1:in `<top (required)>' from /opt/rbenv/versions/1.9.3-p545/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from /opt/rbenv/versions/1.9.3-p545/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from /usr/local/heroku/lib/heroku/command/base.rb:4:in `<top (required)>' from /opt/rbenv/versions/1.9.3-p545/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from /opt/rbenv/versions/1.9.3-p545/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from /usr/local/heroku/lib/heroku/command/auth.rb:1:in `<top (required)>' from /opt/rbenv/versions/1.9.3-p545/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from /opt/rbenv/versions/1.9.3-p545/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from /usr/local/heroku/lib/heroku/command.rb:14:in `block in load' from /usr/local/heroku/lib/heroku/command.rb:13:in `each' from /usr/local/heroku/lib/heroku/command.rb:13:in `load' from /usr/local/heroku/lib/heroku/cli.rb:27:in `start' from /usr/local/heroku/bin/heroku:24:in `<main>'
heroku-apiというのが足りないらしい。これまた調べてみるとgemで提供されているようなのでGemfileを作ってbundlerでインストールしておきます。
[vagrant@localhost ~]$ cat Gemfile source 'https://rubygems.org' gem "heroku-api", "0.3.18" [vagrant@localhost ~]$ bundle install Fetching gem metadata from https://rubygems.org/... Resolving dependencies... Installing excon (0.32.1) Using multi_json (1.9.2) Installing heroku-api (0.3.18) Using bundler (1.5.3) Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
で、確認。
[vagrant@localhost ~]$ heroku version heroku-toolbelt/3.6.0 (x86_64-linux) ruby/1.9.3
今度は無事に実行できるようになりました。