instance.pp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # create a tinc vpn net
  2. define tinc::instance(
  3. $ensure = 'present',
  4. $connect_on_boot = true,
  5. $tinc_interface = 'eth0',
  6. $tinc_address = undef,
  7. $tinc_address_to_export = undef,
  8. $port = '655',
  9. $port_to_export = '655',
  10. $compression = '10',
  11. $mode = 'switch',
  12. $options = {},
  13. $tinc_up_content = undef,
  14. $tinc_down_content = undef,
  15. ){
  16. include ::tinc
  17. # needed in template tinc.conf.erb
  18. $fqdn_tinc = regsubst($::fqdn,'[._-]+','','G')
  19. $tinc_config = "/etc/tinc/${name}/tinc.conf"
  20. # register net for bootup?
  21. $boot_ensure = $ensure ? {
  22. 'present' => $connect_on_boot ? {
  23. true => 'present',
  24. default => 'absent'
  25. },
  26. default => 'absent'
  27. }
  28. # which service do we have to manage?
  29. if $tinc::uses_systemd {
  30. $service_name = "tincd@${name}"
  31. service{$service_name: }
  32. if $ensure == 'present' {
  33. # if we don't want to start
  34. # on boot, we don't need to
  35. # manage that part of the service
  36. if $boot_ensure == 'present' {
  37. Service[$service_name]{
  38. ensure => running,
  39. enable => true,
  40. }
  41. }
  42. } else {
  43. Service[$service_name]{
  44. ensure => stopped,
  45. enable => false,
  46. before => File["/etc/tinc/${name}"],
  47. }
  48. }
  49. } else {
  50. $service_name = 'tinc'
  51. # only relevant for non-systemd systems
  52. concat::fragment{"tinc_net_${name}":
  53. ensure => $boot_ensure,
  54. content => "${name}\n",
  55. target => '/etc/tinc/nets.boot',
  56. notify => Service[$service_name],
  57. }
  58. }
  59. file{"/etc/tinc/${name}":
  60. require => Package['tinc'],
  61. owner => root,
  62. group => 0,
  63. mode => '0600';
  64. }
  65. if $ensure == 'present' {
  66. File["/etc/tinc/${name}"]{
  67. ensure => directory,
  68. notify => Service[$service_name],
  69. }
  70. concat{$tinc_config:
  71. notify => Service[$service_name],
  72. owner => root,
  73. group => 0,
  74. mode => '0600';
  75. }
  76. file{"/etc/tinc/${name}/hosts":
  77. ensure => directory,
  78. recurse => true,
  79. purge => true,
  80. force => true,
  81. notify => Service[$service_name],
  82. owner => root,
  83. group => 0,
  84. mode => '0600';
  85. }
  86. if $tinc_address {
  87. $host_address = $tinc_address
  88. } else {
  89. $int_name_escaped = regsubst($tinc_interface,'\.','_','G')
  90. $host_address = getvar("::ipaddress_${int_name_escaped}")
  91. }
  92. if $tinc_address_to_export {
  93. $export_addr = $tinc_address_to_export
  94. } else {
  95. $export_addr = $host_address
  96. }
  97. # get the keys
  98. # [ priv, pub ]
  99. $tinc_keys = tinc_keygen($name,"${tinc::key_source_path}/${name}/${::fqdn}")
  100. file{
  101. "/etc/tinc/${name}/rsa_key.priv":
  102. content => $tinc_keys[0],
  103. notify => Service[$service_name],
  104. owner => root,
  105. group => 0,
  106. mode => '0600';
  107. "/etc/tinc/${name}/rsa_key.pub":
  108. content => $tinc_keys[1],
  109. notify => Service[$service_name],
  110. owner => root,
  111. group => 0,
  112. mode => '0600';
  113. }
  114. # export this host and collect all the other hosts
  115. @@tinc::host{"${fqdn_tinc}@${name}":
  116. port => $port_to_export,
  117. compression => $compression,
  118. address => $export_addr,
  119. public_key => $tinc_keys[1],
  120. tag => "tinc::host_for_${name}",
  121. }
  122. Tinc::Host<<| tag == "tinc::host_for_${name}" |>>
  123. concat::fragment{"tinc_conf_header_${name}":
  124. target => $tinc_config,
  125. content => template('tinc/tinc.conf-header.erb'),
  126. order => '100',
  127. }
  128. @@tinc::connect_to{"${name}_connect_to_${fqdn_tinc}":
  129. to => $fqdn_tinc,
  130. to_fqdn => $::fqdn,
  131. target => $tinc_config,
  132. tag => "tinc_${name}_auto",
  133. }
  134. Tinc::Connect_to<<| tag == "tinc_${name}_auto" |>>
  135. file { "/etc/tinc/${name}/tinc-up":
  136. content => $tinc_up_content,
  137. notify => Service[$service_name],
  138. owner => root,
  139. group => 0,
  140. mode => '0700';
  141. }
  142. file { "/etc/tinc/${name}/tinc-down":
  143. content => $tinc_down_content,
  144. notify => Service[$service_name],
  145. owner => root,
  146. group => 0,
  147. mode => '0700';
  148. }
  149. } else {
  150. File["/etc/tinc/${name}"]{
  151. ensure => absent,
  152. recurse => true,
  153. purge => true,
  154. force => true
  155. }
  156. }
  157. }