b025f672da21663391b04006d4c60eb2059b6d19
[mirror/dsa-puppet.git] / modules / onion / manifests / service.pp
1 define onion::service (
2         $port,
3         $target_address,
4         $target_port,
5         $ensure = present,
6         $direct = false
7 ) {
8         if ($ensure == "ifstatic") {
9                 $my_ensure = has_static_component($name) ? {
10                         true => "present",
11                         false => "absent"
12                 }
13         } else {
14                 $my_ensure = $ensure
15         }
16
17         if ($my_ensure == "present") {
18                 include onion
19
20                 concat::fragment { "onion::torrc_onionservice::${name}":
21                         target  => "/etc/tor/torrc",
22                         order   => 50,
23                         content => "HiddenServiceDir /var/lib/tor/onion/${name}\nHiddenServicePort ${port} ${target_address}:${target_port}\n\n",
24                 }
25
26                 $onion_hn = onion_tor_service_hostname($name)
27                 if $onion_hn {
28                         $hostname_without_onion = regsubst($onion_hn, '\.onion$', '')
29
30                         if ($direct) {
31                                 @@concat::fragment { "onion::balance::onionbalance-services.yaml::${name}":
32                                         target  => "/srv/puppet.torproject.org/puppet-facts/onionbalance-services.yaml",
33                                         content => "{\"${name}\": \"${onion_hn}\"}\n",
34                                         tag     => "onionbalance-services.yaml",
35                                 }
36                         } else {
37                                 @@concat::fragment { "onion::balance::instance::dsa-snippet::$name::$fqdn":
38                                         target  => "/etc/onionbalance/config-dsa-snippet.yaml",
39                                         content => "- service: ${name}\n  address: ${hostname_without_onion}\n  name: ${hostname}-${name}\n",
40                                         tag     => "onion::balance::dsa-snippet",
41                                 }
42                         }
43                 }
44         }
45 }