徒然なるままに

子育てとプログラミングが同居する不思議な空間

Chef でファイアウォールに穴開けるときの試行錯誤

最近は Chef + Knife Solo で JavaTomcat のインストールを自動化しようといろいろ試行錯誤している。今日は /etc/firewalld/services/tomcat.xml を置いて firewalld で穴を開けようとしたものの、どういうわけか tomcat.xml が見えていないらしくエラーになっていた。

Error: INVALID_SERVICE: tomcat

理由は簡単で、firewall-cmd --reload していないからだった。

まだ attribute とか何も使っていないけど、現時点でのレシピは以下のとおり。

#
# Cookbook Name:: tomcat
# Recipe:: default
#
# Copyright 2015, Monota
#
# All rights reserved - Do Not Redistribute
#
cookbook_file "#{Chef::Config[:file_cache_path]}/apache-tomcat-8.0.18.tar.gz" do
    mode 00644
end

user "tomcat" do
    comment  "tomcat user"
    home     "/home/tomcat"
    shell    "/bin/false"
    password nil
    supports :manage_home => true
    action   [:create, :manage]
end
 
group "tomcat" do
    members ['tomcat']
    action :create
end

bash "install_tomcat" do
    code <<-EOF
        cd #{Chef::Config[:file_cache_path]}
        tar zxf apache-tomcat-8.0.18.tar.gz -C /opt
    EOF
    not_if do
        File.exists?("/opt/tomcat")
    end
end

bash "chown_tomcat" do
    code <<-EOF
        chown -R tomcat:tomcat /opt/apache-tomcat-8.0.18
    EOF
    not_if do
        File.exists?("/opt/tomcat")
    end
end

cookbook_file "/etc/firewalld/services/tomcat.xml" do
    source "tomcat.xml"
    mode 00644
end

bash "reload_firewall" do
    code <<-EOF
        firewall-cmd --reload
    EOF
    not_if do
        File.exists?("/opt/tomcat")
    end
end

cookbook_file "/etc/sysconfig/tomcat" do
    source "tomcat"
    mode 00644
end

cookbook_file "/etc/systemd/system/tomcat.service" do
    source "tomcat.service"
    mode 00644
end

service "firewalld" do
    supports :status => true, :restart => true
    action [ :nothing ]
end

link "/opt/tomcat" do
  to "/opt/apache-tomcat-8.0.18"
  link_type :symbolic
end

service "tomcat" do
    supports :status => true, :restart => true
    action [ :enable, :start ]
end

firewalld_service 'tomcat' do
    action :add
    zone   'public'
    notifies :restart, 'service[firewalld]', :immediately
end

最後の firewalld_service は以下のものを利用している。

今のところ firewall-cmd --reload はしてくれないように思う。自分で修正してプルリク送ろうかな・・・ruby よくわかってないけど。