Last active
December 22, 2015 13:49
-
-
Save sawanoboly/6481665 to your computer and use it in GitHub Desktop.
実践LWRP、HTTP認証用ファイル(htpasswd,htdigest)をChefのリソースとして管理する part.1 of 3 ref: http://qiita.com/sawanoboly/items/9bdaebcfc98d73f84843
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hoge:iLWIQVkBPUswQ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
default_action :create | |
attribute :user, :kind_of => String, :required => true | |
attribute :password, :kind_of => String | |
attribute :path, :kind_of => String, :required => true | |
続いてproviderも変更、`:name`部分を`:path`に変えるだけですね。 | |
### `providers/auth_basic.rb` | |
```ruby:cookbooks/httpsv/providers/auth_basic.rb-add_path | |
require 'webrick' | |
def whyrun_supported? | |
true | |
end | |
action :create do | |
converge_by("====== Create http_auth user #{@new_resource.user}") do | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.path) | |
htpasswd.set_passwd nil, @new_resource.user, @new_resource.password | |
htpasswd.flush | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webrick' | |
action :create do | |
Chef::Log.warn "====== Create http_auth user #{@new_resource.user}" | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.name) | |
htpasswd.set_passwd nil, @new_resource.user, @new_resource.password | |
htpasswd.flush | |
@new_resource.updated_by_last_action(true) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
action :create do | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
action :create do | |
@new_resource.updated_by_last_action(true) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webrick' | |
def whyrun_supported? | |
true | |
end | |
action :create do | |
converge_by("====== Create http_auth user #{@new_resource.user}") do | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.name) | |
htpasswd.set_passwd nil, @new_resource.user, @new_resource.password | |
htpasswd.flush | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webrick' | |
def whyrun_supported? | |
true | |
end | |
action :create do | |
converge_by("====== Create http_auth user #{@new_resource.user}") do | |
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(@new_resource.name) | |
htpasswd.set_passwd nil, @new_resource.user, @new_resource.password | |
htpasswd.flush | |
file @new_resource.name do | |
owner 'guest' | |
mode 0644 | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cookbooks/httpsv/ | |
├── CHANGELOG.md | |
├── README.md | |
├── attributes | |
├── metadata.rb | |
├── providers | |
├── recipes | |
│ └── default.rb | |
└── resources |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cookbooks/httpsv/ | |
├── CHANGELOG.md | |
├── README.md | |
├── attributes | |
├── metadata.rb | |
├── providers | |
├── recipes | |
│ └── default.rb | |
└── resources | |
└── auth_basic.rb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
httpsv_auth_basic '/var/www/site1' do | |
action :create | |
user 'hoge' | |
password 'password' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
httpsv_auth_digest '/var/www/site1' do | |
action :delete | |
user 'hoge' | |
realm 'realm1' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ chef-solo -c solo.rb -o 'httpsv::sample03' | |
Starting Chef Client, version 11.6.0 | |
[2013-09-07T16:05:37+09:00] WARN: Run List override has been provided. | |
[2013-09-07T16:05:37+09:00] WARN: Original Run List: [] | |
[2013-09-07T16:05:37+09:00] WARN: Overridden Run List: [recipe[httpsv::sample03]] | |
Compiling Cookbooks... | |
Converging 2 resources | |
Recipe: httpsv::sample03 | |
* httpsv_auth_basic[/Users/sawanoboriyu/github/opsrockin/lwrp_http_userdb/var/www/site1/.htpasswd:hoge1] action create | |
- ====== Create http_auth user hoge1 | |
* httpsv_auth_basic[/Users/sawanoboriyu/github/opsrockin/lwrp_http_userdb/var/www/site1/.htpasswd:hoge2] action create | |
- ====== Create http_auth user hoge2 | |
Chef Client finished, 2 resources updated | |
$ cat var/www/site1/.htpasswd | |
hoge1:LfhaERgNIfVDo | |
hoge2:UsNzKe0MKEkaU |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
base_dir = ENV['PWD'] | |
file_path = File.join(base_dir, 'var/www/site1/.htpasswd') | |
def mode_to_log(file_path) | |
Chef::Log.warn "========= new resource mode is " + resources("file[#{file_path}]").mode | |
end | |
## >> レシピ部分 | |
file file_path do ; mode '0660' ; end ; mode_to_log(file_path) | |
file file_path do ; mode '0666' ; end ; mode_to_log(file_path) | |
file file_path do ; mode '0644' ; end ; mode_to_log(file_path) | |
file file_path do ; mode '0640' ; end ; mode_to_log(file_path) | |
## << レシピ部分 | |
require 'chef/handler' | |
class Chef::Handler::LogReport < ::Chef::Handler | |
def report | |
Chef::Log.warn '======= Update Resources are following...' | |
data[:updated_resources].each.with_index do |r,idx| | |
Chef::Log.warn [idx, r.to_s].join(':') | |
end | |
end | |
end | |
Chef::Config[:report_handlers] << Chef::Handler::LogReport.new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base_dir = ENV['PWD'] | |
httpsv_auth_basic File.join(base_dir, 'var/www/site1/.htpasswd') do | |
user 'hoge' | |
password 'password' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base_dir = ENV['PWD'] | |
httpsv_auth_basic File.join(base_dir, 'var/www/site1/.htpasswd') do | |
user 'hoge1' | |
password 'password1' | |
end | |
httpsv_auth_basic File.join(base_dir, 'var/www/site1/.htpasswd') do | |
user 'hoge2' | |
password 'password2' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base_dir = ENV['PWD'] | |
httpsv_auth_basic File.join(base_dir, 'var/www/site1/.htpasswd') do | |
user 'hoge1' | |
path self.name.to_s | |
name [self.path, self.user].join(':') | |
password 'password1' | |
end | |
httpsv_auth_basic File.join(base_dir, 'var/www/site1/.htpasswd') do | |
user 'hoge2' | |
path self.name.to_s | |
name [self.path, self.user].join(':') | |
password 'password2' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment