5 # Copyright:: 2012, OpenStreetMap Foundation
 
   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
 
  11 #     https://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  23   action [:enable, :start]
 
  24   supports :status => true, :restart => true, :reload => true
 
  28 node[:openvpn][:tunnels].each do |name, details|
 
  29   peer = search(:node, "fqdn:#{details[:peer][:host]}").first
 
  32     if peer[:openvpn] && !details[:peer][:address]
 
  33       node.default[:openvpn][:tunnels][name][:peer][:address] = peer[:openvpn][:address]
 
  36     node.default[:openvpn][:tunnels][name][:peer][:networks] = peer.interfaces(:role => :internal).collect do |interface|
 
  37       { :address => interface[:network], :netmask => interface[:netmask] }
 
  40     node.default[:openvpn][:tunnels][name][:peer][:networks] = []
 
  43   if details[:mode] == "client"
 
  44     execute "openvpn-genkey-#{name}" do
 
  45       command "openvpn --genkey --secret /etc/openvpn/#{name}.key"
 
  48       creates "/etc/openvpn/#{name}.key"
 
  51     if File.exist?("/etc/openvpn/#{name}.key")
 
  52       node.default[:openvpn][:keys][name] = IO.read("/etc/openvpn/#{name}.key")
 
  54   elsif peer && peer[:openvpn]
 
  55     file "/etc/openvpn/#{name}.key" do
 
  59       content peer[:openvpn][:keys][name]
 
  63   if node[:openvpn][:tunnels][name][:peer][:address]
 
  64     template "/etc/openvpn/#{name}.conf" do
 
  65       source "tunnel.conf.erb"
 
  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]"
 
  77     file "/etc/openvpn/#{name}.conf" do