LoginSignup
8
8

More than 5 years have passed since last update.

Itamaeでシェルの再起動が必要になった場合の対応

Posted at

Itamaeでanyenvを使ってrbenvやndenvを入れようと思った時に、公式通りにやろうとして次のように書いてもうまく動作しません。理由については参考URL1,2のリンク先に詳しく書いてありますが、要するにanyenvのインストールを反映させるにはシェルの再起動が必要だがItamae実行中にシェルを再起動することはできないということです。

cookbooks/anyenv/default.rb
HOME_DIR = "/home/vagrant"

# anyenvのインストール
execute 'git clone https://github.com/riywo/anyenv ~/.anyenv' do
  not_if "test -d #{HOME_DIR}/.anyenv"
end

remote_file "#{HOME_DIR}/.bashrc" do
  source './files/.bashrc'
end

# シェルの再機動
# ここでItamaeが止まってしまう。
execute 'exec $SHELL -l'

# rbenvのインストール
execute 'anyenv install rbenv' do
  not_if "test -d #{HOME_DIR}/.anyenv/envs/rbenv"
end

シェルの再機動をしないとanyenvのインストールとbashの設定が反映されないし、でも再起動できないしで困ったと思ったんですが、下記のようにやればうまくいきました。
-lオプションを使ってログインシェルとして/bin/bashを起動することで、anyenvのインストールが反映されます。.bashrc等の設定の変更も反映されるので、コマンドが無いと怒られることもありません。

cookbooks/anyenv/default.rb
HOME_DIR = "/home/vagrant"

# anyenvのインストール
execute 'git clone https://github.com/riywo/anyenv ~/.anyenv' do
  not_if "test -d #{HOME_DIR}/.anyenv"
end

remote_file "#{HOME_DIR}/.bashrc" do
  source './files/.bashrc'
end

# rbenvのインストール
execute '/bin/bash -lc "anyenv install rbenv"' do
  not_if "test -d #{HOME_DIR}/.anyenv/envs/rbenv"
end

ただ、事前にシェルの再起動が必要なコマンドには毎回同じことを書かなきゃいけないので、もっと良い方法があれば教えて下さい!

参考URL

  1. http://qiita.com/yusabana/items/c4de582c6f85a42817d8
  2. http://stackoverflow.com/questions/33048024/exec-shell-executes-from-ssh-wont-execute-in-playbook
  3. http://qiita.com/FGtatsuro/items/2366c93131c47aef8dfe
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8