Skip to content
This repository was archived by the owner on Jan 16, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions manifests/address.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Resource: syncthing::address
#
# This resource adds address entry to specified device in config.xml
define syncthing::address
(
$home_path,
$device_id,
$address,

$ensure = 'present',
)
{
if ! defined(Class['syncthing']) {
fail('You must include the syncthing base class before using any syncthing defined resources')
}

syncthing::element{ "set device ${device_id} address ${address} in instance ${home_path}":
ensure => $ensure,
home_path => $home_path,
element => 'address',
value => $address,
parent_element => 'device',
parent_id => $device_id,
}
}
74 changes: 66 additions & 8 deletions manifests/device.pp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@
fail('You must include the syncthing base class before using any syncthing defined resources')
}

$instance_config_xml_path = "${home_path}/config.xml"
validate_re($compression, '^(metadata|always|never)$')
validate_bool($introducer)

if $ensure == 'present' {
$changes = parseyaml( template('syncthing/config_device-changes.yaml.erb') )
} else {
$changes = "rm device[#attribute/id='${id}']"
unless empty($options) {
warning('DEPRECATION: $options parameter support will be removed in future release. Please use syncthing::address class instead.')
}

augeas { "configure instance ${home_path} device ${id}":
$instance_config_xml_path = "${home_path}/config.xml"

Augeas {
incl => $instance_config_xml_path,
lens => 'Xml.lns',
context => "/files${instance_config_xml_path}/configuration",
changes => $changes,

notify => [
Service['syncthing'],
],
Expand All @@ -39,4 +38,63 @@
Exec["create syncthing instance ${home_path}"],
],
}

if $ensure == 'present' {

augeas { "update device ${id} in instance ${home_path}":
changes => [
"set device[#attribute/id='${id}']/#attribute/name ${device_name}",
"set device[#attribute/id='${id}']/#attribute/compression ${compression}",
"set device[#attribute/id='${id}']/#attribute/introducer ${introducer}",
],
onlyif => "match device[#attribute/id='${id}'] size > 0",
}

augeas { "create device ${id} in instance ${home_path}":
changes => [
"ins #text after device[last()]",
"set device[last()]/following-sibling::#text[1] ' '",
"ins device after device[last()]/following-sibling::#text[1]",
"set device[last()]/#attribute/id ${id}",
"set device[#attribute/id='${id}']/#attribute/name ${device_name}",
"set device[#attribute/id='${id}']/#attribute/compression ${compression}",
"set device[#attribute/id='${id}']/#attribute/introducer ${introducer}",
],
onlyif => "match device[#attribute/id='${id}'] size == 0",
}

each($options) | $option, $value | {
::syncthing::element { "set device ${id} option ${option} in instance ${home_path}":
home_path => $home_path,
parent_element => 'device',
parent_id => $id,
element => $option,
value => $value,
require => [
Augeas["update device ${id} in instance ${home_path}"],
Augeas["create device ${id} in instance ${home_path}"],
],
}
}
any2array($address).each | $addr | {
::syncthing::address{ "set device ${id} address ${addr} in instance ${home_path}":
home_path => $home_path,
device_id => $id,
address => $addr,
ensure => $ensure,
require => Augeas["create device ${id} in instance ${home_path}"],
}
}
} else {

augeas { "remove device ${id} in instance ${home_path}":
changes => [
"rm device[#attribute/id='${id}']/preceding-sibling::#text[1]",
"rm device[#attribute/id='${id}']",
],
onlyif => "match device[#attribute/id='${id}'] size > 0",
}

}

}
101 changes: 101 additions & 0 deletions manifests/element.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Resource: syncthing::element
#
# This is private class, which is used to add elements to XML file
define syncthing::element
(
$home_path,
$element,
$parent_element,
$parent_id,
$value,

$ensure = 'present',
)
{

# Private class checking
assert_private("Use of private class ${name} by ${caller_module_name}")

# Hacky indent definition
$indent_1 = ' '
$indent_2 = ' '

$instance_config_xml_path = "${home_path}/config.xml"

Augeas {
incl => $instance_config_xml_path,
lens => 'Xml.lns',
context => "/files${instance_config_xml_path}/configuration",
# notify => [
# Service['syncthing'],
#],

require => [
Exec["create syncthing instance ${home_path}"],
],
}

if $ensure == 'present' {

# Add first element with top padding
augeas { "create ${element} ${value} for ${parent_element} ${parent_id} in instance ${home_path}":
changes => [
"set ${parent_element}[#attribute/id='${parent_id}']/#text[1] '\n${indent_2}'",
"ins ${element} after ${parent_element}[#attribute/id='${parent_id}']/#text[last()]",
"set ${parent_element}[#attribute/id='${parent_id}']/${element}[last()]/#text '${value}'",
"ins #text after ${parent_element}[#attribute/id='${parent_id}']/${element}[last()]",
"set ${parent_element}[#attribute/id='${parent_id}']/${element}[last()]/following-sibling::#text[1] '\n${indent_2}'",
],
onlyif => "match ${parent_element}[#attribute/id='${parent_id}']/*[label() != '#attribute'] size == 0",
}

# Add additional element
augeas { "create additional ${element} ${value} for ${parent_element} ${parent_id} in instance ${home_path}":
changes => [
"ins ${element} after ${parent_element}[#attribute/id='${parent_id}']/#text[last()]/preceding-sibling::*[1]",
"set ${parent_element}[#attribute/id='${parent_id}']/${element}[last()]/#text '${value}'",
"ins #text before ${parent_element}[#attribute/id='${parent_id}']/${element}[last()]",
"set ${parent_element}[#attribute/id='${parent_id}']/${element}[last()]/preceding-sibling::#text[1] '${indent_2}'",
],
onlyif => "match ${parent_element}[#attribute/id='${parent_id}']/${element}[#text='${value}'] size == 0",
require => Augeas["create ${element} ${value} for ${parent_element} ${parent_id} in instance ${home_path}"],
}

# Set up proper bottom padding
augeas { "create bottom padding for ${element} ${value} for ${parent_element} ${parent_id} in instance ${home_path}":
changes => [
"set ${parent_element}[#attribute/id='${parent_id}']/#text[last()] '${indent_1}'",
],
onlyif => "match ${parent_element}[#attribute/id='${parent_id}']/*[label() != '#attribute'] size > 0",
require => Augeas["create additional ${element} ${value} for ${parent_element} ${parent_id} in instance ${home_path}"],
}

} else {

# Remove element
augeas { "remove ${element} ${value} for ${parent_element} ${parent_id} in instance ${home_path}":
changes => [
"rm ${parent_element}[#attribute/id='${parent_id}']/${element}[#text='${value}']/following-sibling::#text[1]",
"rm ${parent_element}[#attribute/id='${parent_id}']/${element}[#text='${value}']",
],
}

# Remove all paddings if there is no more elements
augeas { "remove padding for ${parent_element} ${parent_id} in instance ${home_path}":
changes => "rm ${parent_element}[#attribute/id='${parent_id}']/#text",
onlyif => "match ${parent_element}[#attribute/id='${parent_id}']/*[label() != '#attribute'] size == 1",
require => Augeas["create bottom padding for ${element} ${value} for ${parent_element} ${parent_id} in instance ${home_path}"],
}

# Set up prosper bottom padding after removing element
augeas { "create bottom padding for ${element} ${value} for ${parent_element} ${parent_id} in instance ${home_path}":
changes => [
"set ${parent_element}[#attribute/id='${parent_id}']/#text[last()] '${indent_1}'",
],
onlyif => "match ${parent_element}[#attribute/id='${parent_id}']/*[label() != '#attribute'] size > 0",
require => Augeas["remove ${element} ${value} for ${parent_element} ${parent_id} in instance ${home_path}"],
}

}

}
2 changes: 1 addition & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

$instance_options = {}

$device_compression = false
$device_compression = 'metadata'
$device_introducer = false
$device_options = {}
}
13 changes: 0 additions & 13 deletions templates/config_device-changes.yaml.erb

This file was deleted.