]> git.openstreetmap.org Git - chef.git/blob - cookbooks/openvpn/recipes/default.rb
Yet more rubucop cleanups
[chef.git] / cookbooks / openvpn / recipes / default.rb
1 #
2 # Cookbook Name:: 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 #     http://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   if peer = search(:node, "fqdn:#{details[:peer][:host]}").first
30     if peer[:openvpn] && !details[:peer][:address]
31       node.default[:openvpn][:tunnels][name][:peer][:address] = peer[:openvpn][:address]
32     end
33
34     node.default[:openvpn][:tunnels][name][:peer][:networks] = peer.interfaces(:role => :internal).collect do |interface|
35       { :address => interface[:network], :netmask => interface[:netmask] }
36     end
37   else
38     node.default[:openvpn][:tunnels][name][:peer][:networks] = []
39   end
40
41   if details[:mode] == "client"
42     execute "openvpn-genkey-#{name}" do
43       command "openvpn --genkey --secret /etc/openvpn/#{name}.key"
44       user "root"
45       group "root"
46       creates "/etc/openvpn/#{name}.key"
47     end
48
49     if File.exist?("/etc/openvpn/#{name}.key")
50       node.set[:openvpn][:keys][name] = IO.read("/etc/openvpn/#{name}.key")
51     end
52   elsif peer && peer[:openvpn]
53     file "/etc/openvpn/#{name}.key" do
54       owner "root"
55       group "root"
56       mode 0600
57       content peer[:openvpn][:keys][name]
58     end
59   end
60
61   if node[:openvpn][:tunnels][name][:peer][:address]
62     template "/etc/openvpn/#{name}.conf" do
63       source "tunnel.conf.erb"
64       owner "root"
65       group "root"
66       mode 0644
67       variables :name => name,
68                 :address => node[:openvpn][:address],
69                 :port => node[:openvpn][:tunnels][name][:port],
70                 :mode => node[:openvpn][:tunnels][name][:mode],
71                 :peer => node[:openvpn][:tunnels][name][:peer]
72       notifies :restart, "service[openvpn]"
73     end
74   else
75     file "/etc/openvpn/#{name}.conf" do
76       action :delete
77     end
78   end
79 end