コピペでCentOS7上にRails Api環境を構築する。っていう記事を少し前に書いたけど。
実際の開発の場で「コピペでどーぞ」なんてクソ恥ずかしい笑
しかも、そんなものシェルスクリプトにでも書くのかいって感じだよね。
なので、chefのレシピにしておくことにしましょう。
chefにしておく利点は、まずAWSでも扱いやすい点が大きい。
rubyDSLなので、Webエンジニアでもインフラ部分の記述・設定が楽。
あと幾つかあるけど、自分で調べてくださいな。(PythonユーザーはAnsibleも良いよね!そのうちAnsibleでも記事書くね。)
使用するCookBook
rubyをインストールするためrbenvのCookBookが必要になります。
Berkshelfを使用してください。
下記のように書いてbundle exec berks vendor cookbooksしてくれれば大丈夫です。
| 
					 1  | 
						cookbook 'rbenv', '~> 1.7.1'  | 
					
あと記入するcookbookのmetadata.rbに下記を追加。
| 
					 1  | 
						depends 'rbenv'  | 
					
やること
- ChefClientがsudoを使用できるようにrequirettyをコメントアウトとかする
 - あんまり良くないけど、簡易的にSelinuxを無効化する。
 - ユーザー作成と権限、認証設定
 - vimやgitとかの基本的なパッケージをインストール
 - rbenvのインストール
 - ruby-buildとrubyをインストール
 
意外とこれだけ笑
レシピ全文
やることを全部レシピにしたのがこちら笑
他にも必要なものがあれば適宜編集してね。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105  | 
						$home = "/home/centos" $user = "centos" # ChefClientがEC2内でsudoを使えるようする。 if $user == "centos" ||  $user == "ec2-user"   file "/etc/sudoers" do     _file = Chef::Util::FileEdit.new(path)     _file.search_file_replace(/^Defaults *requiretty/, "#Defaults requiretty\n")     _file.search_file_replace(/^Defaults *!visiblepw/, "Defaults visiblepw\n")     content _file.send(:editor).lines.join   end end #selinuxの無効化 reboot 'now' do   action :nothing   reason 'Cannot continue Chef run without a reboot.' end file "/etc/selinux/config" do   _file = Chef::Util::FileEdit.new(path)   _file.search_file_replace(/SELINUX=enforcing/, "#SELINUX=enforcing\nSELINUX=Disabled")   content _file.send(:editor).lines.join   not_if "grep '#SELINUX=enforcing' /etc/selinux/config"   #notifies :request_reboot, 'reboot[now]', :immediately end bash "change selinux to permissive mode" do   code "sudo setenforce 0"   not_if "sudo getenforce | grep -e 'Permissive' -e 'Disabled'" end # ec2-userの作成 user "ec2-user" do   home "/home/ec2-user"   username "ec2-user" end bash "ssh conf new" do   code <<-EOH     sudo mkdir /home/ec2-user     sudo mkdir /home/ec2-user/.ssh     sudo cp #{$home}/.ssh/* /home/ec2-user/.ssh/     sudo cp #{$home}/.bashrc /home/ec2-user/.bashrc     sudo cp #{$home}/.bash_profile /home/ec2-user/.bash_profile     sudo chown -R ec2-user:ec2-user /home/ec2-user     EOH   not_if { File.exists?("/home/ec2-user/.ssh")} end group "wheel" do   action [:modify]   members ["ec2-user"]   append true end execute "/etc/sudoers edit" do   command "echo '%wheel ALL=(ALL) NOPASSWD:  ALL\n' | sudo tee -a /etc/sudoers"   not_if "sudo grep '%wheel ALL=(ALL) NOPASSWD:  ALL' /etc/sudoers" end #sshd の認証設定変更 bash "edit sshd" do   code <<-EOH     echo -e '\nPermitRootLogin no\nPubkeyAuthentication yes\nPasswordAuthentication no\n' | sudo tee -a /etc/ssh/sshd_config     sudo systemctl restart sshd     EOH   not_if "grep '^PermitRootLogin no' /etc/ssh/sshd_config"   #notifies :reboot_now, 'reboot[now]', :immediately end # 基本的なパッケージのインストール。ほかに欲しいものがあったらここに追加してね %w{ git screen htop vim }.each do |p|   package p do     action :install   end end # install rbenv %w{ bzip2 }.each do |p|   package p do     action :install   end end include_recipe "rbenv::default" #rubyのインストール ruby_v = "2.2.3" # <= インストールしたいバージョンがあればここを変更してね include_recipe "rbenv::ruby_build" rbenv_ruby ruby_v do   global true end rbenv_gem "bundler" do   ruby_version ruby_v end bash "settings ruby -v ruby_v" do   code <<-EOH       echo 'export PATH="/opt/rbenv/bin:$PATH"' | sudo tee -a  #{$home}/.bash_profile       echo 'eval "$(rbenv init -)"' | sudo tee -a #{$home}/.bash_profile       source #{$home}/.bash_profile       rbenv rehash       rbenv global #{ruby_v}      EOH  not_if "grep 'rbenv init' #{$home}/.bash_profile || [ -e '/etc/profile.d/rbenv.sh' ]" end  | 
					
いじょう。
一回書いとけば、何度でも、どこでも同じ結果。
簡単だね。
実際には、複数のレシピに分けたほうが見通しもいいしお勧めです。
じゃあね〜〜〜。
