]> git.openstreetmap.org Git - chef.git/blob - cookbooks/openvpn/recipes/default.rb
c027466f1eef1edb9745785b0fdf51817040e056
[chef.git] / cookbooks / openvpn / recipes / default.rb
1 #
2 # Cookbook:: openvpn
3 # Recipe:: default
4 #
5 # Copyright:: 2012, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 package "openvpn"
21
22 service "openvpn" do
23   action [:enable, :start]
24   supports :status => true, :restart => true, :reload => true
25   ignore_failure true
26 end
27
28 node[:openvpn][:tunnels].each do |name, details|
29   peer = search(:node, "fqdn:#{details[:peer][:host]}").first
30
31   if peer
32     if peer[:openvpn] && !details[:peer][:address]
33       node.default[:openvpn][:tunnels][name][:peer][:address] = peer[:openvpn][:address]
34     end
35
36     node.default[:openvpn][:tunnels][name][:peer][:networks] = peer.interfaces(:role => :internal).collect do |interface|
37       { :address => interface[:network], :netmask => interface[:netmask] }
38     end
39   else
40     node.default[:openvpn][:tunnels][name][:peer][:networks] = []
41   end
42
43   if details[:mode] == "client"
44     execute "openvpn-genkey-#{name}" do
45       command "openvpn --genkey --secret /etc/openvpn/#{name}.key"
46       user "root"
47       group "root"
48       creates "/etc/openvpn/#{name}.key"
49     end
50
51     if File.exist?("/etc/openvpn/#{name}.key")
52       node.default[:openvpn][:keys][name] = IO.read("/etc/openvpn/#{name}.key")
53     end
54   elsif peer && peer[:openvpn]
55     file "/etc/openvpn/#{name}.key" do
56       owner "root"
57       group "root"
58       mode "600"
59       content peer[:openvpn][:keys][name]
60     end
61   end
62
63   if node[:openvpn][:tunnels][name][:peer][:address]
64     template "/etc/openvpn/#{name}.conf" do
65       source "tunnel.conf.erb"
66       owner "root"
67       group "root"
68       mode "644"
69       variables :name => name,
70                 :address => node[:openvpn][:address],
71                 :port => node[:openvpn][:tunnels][name][:port],
72                 :mode => node[:openvpn][:tunnels][name][:mode],
73                 :peer => node[:openvpn][:tunnels][name][:peer]
74       notifies :restart, "service[openvpn]"
75     end
76   else
77     file "/etc/openvpn/#{name}.conf" do
78       action :delete
79     end
80   end
81 end