Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added function to returns a file system path as escaped by systemd #95

Closed
wants to merge 1 commit into from

Conversation

KoenDierckx
Copy link

While creating a systemd mount and automount unit file, I needed a way to convert a mount point into a systemd unit name, as this requires some very specific conventions
https://www.freedesktop.org/software/systemd/man/systemd.unit.html#String%20Escaping%20for%20Inclusion%20in%20Unit%20Names

Instead of recreating the logic in puppet, I've created a custom function that calls the native systemd-escape binary.

This can be used like this:
$mount_name = systemd_escape($mountpoint)
systemd::unit_file { "${mount_name}.mount": }

@bastelfreak
Copy link
Member

bastelfreak commented Dec 15, 2018

Hi @KoenDierckx, thanks for providing this PR. Can you please convert his to the modern functions API? Some docs are available at https://puppet.com/docs/puppet/4.9/functions_basics.html. Pretty helpful is also: https://github.com/puppetlabs/puppet-specifications/blob/master/language/func-api.md

@alexjfisher
Copy link
Member

Use of the modern API is needed for correct ‘environment isolation’. With the new API, you can also namespace functions. This is recommended for all functions that aren’t built into puppet.
https://github.com/voxpupuli/puppet-extlib has lots of examples, including name spacing, testing and use of puppet-strings.

Copy link
Member

@dhollinger dhollinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the modern Ruby function API mentioned by @bastelfreak and @alexjfisher

) do |args|
raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 1)") if args.length != 1
path = args[0]
cmd = "/bin/systemd-escape --path #{path}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function will be run on the master, not the agents. Could this be a problem? (Perhaps the server isn't a systemd based system and doesn't have this command available).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, Linux OSes without systemd are getting more and more rare these days…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably ok then (so long as they all put that binary under /bin)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ which systemd-escape
/usr/bin/systemd-escape
$ facter  os
{
  architecture => "x86_64",
  distro => {
    codename => "n/a",
    description => "Arch Linux",
    id => "Arch",
    release => {
      full => "rolling",
      major => "rolling"
    },
    specification => "1.4"
  },
  family => "Archlinux",
  hardware => "x86_64",
  name => "Archlinux",
  release => {
    full => "4.19.10-arch1-1-ARCH",
    major => "4",
    minor => "19"
  },
  selinux => {
    enabled => false
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Systemd: the unique layer to standardize everything on Linux OSes 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need to provide an absolute path? I thought puppet is able to figure this out by itself?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use /usr/bin/env?

https://www.freedesktop.org/software/systemd/man/systemd.unit.html#String%20Escaping%20for%20Inclusion%20in%20Unit%20Names
EOS
) do |args|
raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 1)") if args.length != 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this was copied from the validate_cmd function code. It should be adapted to reflect the name of the function.

@bastelfreak bastelfreak added the needs-work not ready to merge just yet label Dec 23, 2018
escaped = Puppet::Util::Execution.execute(cmd)
escaped.strip
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the missing newline here.

@towo
Copy link
Member

towo commented Aug 20, 2019

@KoenDierckx Hey there, could you finish up this PR?

@stale
Copy link

stale bot commented Apr 19, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Apr 19, 2021
@stale stale bot closed this Apr 27, 2021
@traylenator
Copy link
Contributor

New attempt #220

op-ct pushed a commit to op-ct/puppet-systemd that referenced this pull request Jun 17, 2022
op-ct pushed a commit to op-ct/puppet-systemd that referenced this pull request Jun 17, 2022
Fixes voxpupuli#95 Add EPEL GPG Key and logic to handle yum::gpgkeys
kajinamit pushed a commit to kajinamit/puppet-systemd that referenced this pull request Mar 25, 2023
.rubocop.yml : update TrailingCommainLiteral
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-work not ready to merge just yet wontfix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants