Skip to content

Instantly share code, notes, and snippets.

@yutokyokutyo
Last active November 2, 2018 09:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutokyokutyo/8a8b2a287b0a485a4c5e to your computer and use it in GitHub Desktop.
Save yutokyokutyo/8a8b2a287b0a485a4c5e to your computer and use it in GitHub Desktop.

Ops研修(12月12日)

Chapter 7: Puppet を導入する

git clone してくるときに、もうそのディレクトリありますよと言われる

creates で指定しているのになんでこんなエラーが出てくるのだろう。。

==> default: Notice: /Stage[main]/Main/Exec[clone rbenv]/returns: fatal: destination path 'rbenv' already exists and is not an empty directory.
==> default: Error: git clone https://github.com/sstephenson/rbenv.git returned 128 instead of one of [0]
==> default: Error: /Stage[main]/Main/Exec[clone rbenv]/returns: change from notrun to 0 failed: git clone https://github.com/sstephenson/rbenv.git returned 128 instead of one of [0]
	cwd      => "/home/vagrant",
	path     => ['/usr/bin'],
	command  => "git clone https://github.com/sstephenson/rbenv.git",
	creates => "/home/vagrant/.rbenv",
  • 上の様な manifest を作成したが、この記述だと $HOME に rbenv というディレクトリができてしまっていた。
  • デフォルトでは git clone してくる際に、$HOME以下にrbenvというディレクトリを作成してしまう。
  • そのため commnad に Path を指定してあげなくてはならない。
  • command => "git clone https://github.com/sstephenson/rbenv.git .rbenv",
  • cwdとpathの区別がつけることができた

依存関係

実行したいリソースタイプ内で他のリソースが必要な場合は require で依存関係を作らないと実行できないと思ってました。例えば、git clone するには、Package の git について依存関係を作らないと、実行できないと思ってました。

==> default: Notice: /Stage[main]/Main/File[/home/vagrant/.bash_profile]: Dependency Exec[clone rbenv] has failures: true
==> default: Warning: /Stage[main]/Main/File[/home/vagrant/.bash_profile]: Skipping because of failed dependencies
==> default: Notice: Finished catalog run in 0.36 seconds
  • require は依存関係を作るだけatributes。(dependency relationship)。どこかのリソースが実行できなかった場合、そのリソースに依存しているものは全て実行できなくなる。依存関係だらけになりすぎるのも良くないので、できるだけ使わないようにするのが望ましい。

allow_virtual Warning

==> default: Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
==> default:    (at /usr/lib/ruby/site_ruby/1.8/puppet/type/package.rb:430:in `default')

Package { allow_virtual => true } をmanifest の文頭に付け加えることでWorningが消えました。 ちょっと難しいので保留。

テンプレートについて勘違い

  • テンプレートの作成まで、provision で行なうのかなと思っていました。
  • テンプレートは先に作っておき、gitで管理している場所に置いて適宜参照すること
  • sourse でリポジトリからポコポコ持ってこれる。
file { '/home/vagrant/.bash_profile':
source => '/vagrant/template_bash_profile',
}

bash_profile の再読み込み

notify を指定したものを再読み込みさせる atributes だと思っていたので、以下のように記述すればいいのかなと勘違いしました。

file { '/home/vagrant/.bash_profile':
source => '/vagrant/template_bash_profile',
notify => File['/home/vagrant/.bash_profile']
}

でも、notice はそんな機能ではなかった。 notice は、例えば file から service に対して依存関係を作ったときに、fileの内容を変更したときなんかに通知がいくようにしてくれる atributes 。(変更に対してリスタートしてね〜とか)(iptablesの設定のときとかに必要かな?)

==> default: Error: Could not apply complete catalog: Found 1 dependency cycle:
==> default: (File[/home/vagrant/.bash_profile] => File[/home/vagrant/.bash_profile])
==> default: Try the '--graph' option and opening the resulting '.dot' file in OmniGraffle or GraphViz

notice はいらなかったのであった。

.bash_profile を読み込む

exec リソースタイプでsourseを使って .bash_profile を読みこもうとするとエラーがでる。

==> default: Error: Validation of Exec[.bash_profile load] failed: 'source /home/vagrant/.bash_profile' is not qualified and no path was specified. Please qualify the command or specify a path. at /tmp/vagrant-puppet-3/manifests/init.pp:43
==> default: Wrapped exception:
==> default: 'source /home/vagrant/.bash_profile' is not qualified and no path was specified. Please qualify the command or specify a path.

manifestはこれ

exec { '.bash_profile load':
    command     => 'source /home/vagrant/.bash_profile',
    subscribe   => File['/home/vagrant/.bash_profile'],
    refreshonly => true,
}

解決

  • exec と puppet apply は独立しているので、もしも exec で読み込ませたとしても、puppet apply には関与しないのであった。
  • そのため解決策としては、フルパスで実行するか、.bash_profile の変更点が必要な場合のみ、逐一読み込ませるかをすること
  • 今回はrbenv で ruby をインストールする場合のみに必要になるので、逐一読み込ませるmanifestを書いていくことにしました。

ruby-build をcloneする

clone しようと思ったら、~/.rbenv/pluginsなんてディレクトリはないよっていわれた

==> default: Error: Working directory '/home/vagrant/.rbenv/plugins' does not exist
==> default: Error: /Stage[main]/Main/Exec[clone ruby-build]/returns: change from notrun to 0 failed: Working directory '/home/vagrant/.rbenv/plugins' does not exist
==> default: Notice: Finished catalog run in 0.20 seconds

ssh でみてみたら、確かにない。。もしかしてこのディレクトリを作る必要あるのかな。。 いや、そんなことしなくても、cwd と command の中の path の指定をしてあげることで実行された

==> default: Notice: /Stage[main]/Main/Exec[clone ruby-build]/returns: executed successfully
==> default: Notice: Finished catalog run in 8.20 seconds

Ruby をインストールする

source が見つからないよ!エラー

==> default: Error: Could not find command '/bin/bash/source'
==> default: Error: /Stage[main]/Main/Exec[ruby install]/returns: change from notrun to 0 failed: Could not find command '/bin/bash/source'
  • コマンドには組み込みコマンドがある(ビルトインコマンド)。これらは/usr/bin//bin/のような環境変数pathは関係なく、bash や zsh のようなシェルを指定してあげることで実行できる
  • 組み込みコマンドかどうかの判別は、man をして左上にBUILTIN(1)とでること
  • 普段使っているビルトインコマンドではないコマンドは、shell から子プロセスをはやして扱っている。
  • 具体的な解決策はbash -c'hoge'で良いっぽい!来週書いてみます!

PR

https://gist.github.com/yutokyokutyo/a3d34d9d941dcdbdf1ce

備考

  • チェックサム:データを送受信する際の誤り検出方法の一つ

http://e-words.jp/w/E38381E382A7E38383E382AFE382B5E383A0.html

  • directoryは必ず ensure => directory を明示する

http://pepabo.github.io/docs/puppet/style-guide.html

  • recurse は再帰的な処理

英単語

  • with sum 加える
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment