Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Commit

Permalink
Added an example of a Libral provider using Puppet directly
Browse files Browse the repository at this point in the history
  • Loading branch information
garethr committed Aug 22, 2017
1 parent 82619d5 commit dd6f0fb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
1 change: 1 addition & 0 deletions contrib/puppet/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ RUN apt-get update && \
ENV PATH=/usr/src/ral/bin:$PATH

COPY apt_libral /etc/puppetlabs/code/modules/apt_libral
COPY puppet.prov /usr/src/ral/data/providers

CMD ["resource", "apt_libral"]
39 changes: 36 additions & 3 deletions contrib/puppet/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Integrating Libral with Puppet

This folder contains a simple example of integrating Puppet with Libral, specifically exposing Libral providers to Puppet. This is not intended to be a complete solution, more at early working prototype.
This folder contains a simple example of integrating Puppet with Libral, specifically exposing Libral providers to Puppet and using Puppet as a Libral provider. This is not intended to be a complete solution, more an early working prototype.

You can see this working with the accompanying Dockerfile.The image contains both Puppet and Libral, along with Ralsh.

Expand Down Expand Up @@ -40,10 +40,43 @@ apt_libral { 'base-files':
ensure => '9.4ubuntu4.5',
```

This example also contains an example Libral provider which uses Puppet under the hood. This is incredibly blunt, in that it shells out and then naively parses the returned YAML (MRuby isn't built with the YAML gem yet), but it demonstrates this works. For instance lets run our new provider via Ralsh.

```
$ docker run --entrypoint ralsh puppet/puppet-libral -l none group::puppet | head
group::puppet { 'adm':
ensure => 'present',
gid => '4',
}
group::puppet { 'audio':
ensure => 'present',
gid => '29',
}
group::puppet { 'backup':
ensure => 'present',
```

Compare that with the groupadd provider which already ships with libral:

```
$ docker run --entrypoint ralsh puppet/puppet-libral -l none group::puppet root
group::puppet { 'root':
ensure => 'present',
gid => '0',
}
$ docker run --entrypoint ralsh puppet/puppet-libral -l none group::groupadd root
group::groupadd { 'root':
ensure => 'present',
gid => '0',
}
```

## Notes

* This only looks at `list` operations so far
* The types could be autogenerated from the libral providers, or loaded dynamically at runtime
* The Puppet types could be autogenerated from the libral providers, or loaded dynamically at runtime
* The libral types, and provider code, could be autogenerated from the Puppet types. Some prior-art around extraction in https://github.com/puppetlabs/pcore-to-jsonschema
* The providers are nearly 100% boilerplate, which should mean we can have a simple base class which does all the work for any libral provider
* The exception is type-specific fields, which again can be autogenerated or parsed from the provider/ralsh at runtime
* I've implemented both a simple provider and a ralsh provider. A JSON provider would be simple enough, and once https://github.com/puppetlabs/libral/pull/25 is working it should be possible to use the native Ruby interface. I'm doing this for completeness, in reality we'd probably pick one
* I've implemented both a simple provider and a ralsh provider. A JSON provider would be simple enough to add, and once https://github.com/puppetlabs/libral/pull/25 is working it should be possible to use the native Ruby interface. I'm doing this for completeness, in reality we'd probably pick one
53 changes: 53 additions & 0 deletions contrib/puppet/puppet.prov
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#! /usr/bin/env mruby
# -*- ruby -*-


class Group
def describe(ctx)
puts <<EOS
---
provider:
type: group
desc: |
Puppet group
invoke: json
actions: [get]
suitable: true
attributes:
name:
desc: "The name of the group"
ensure:
type: enum[absent, present]
gid:
desc: "The group ID"
EOS
end

def get(ctx, names)
output = `puppet resource group --to_yaml`
lines = output.split("\n")
resources = {}
lines.each do |line|
unless line == "group:"
key, value = line.split(":", 2)
if (value.nil? || value.empty?) and !key.nil?
$name = key.strip
resources[$name] = {}
else
unless $name.nil? or key.nil?
resources[$name][key.strip] = value.strip.gsub("'", '')
end
end
end
end
resources.collect do |name, resource|
{
"name" => name,
"ensure" => resource["ensure"],
"gid" => resource["gid"],
}
end
end
end

Ral::CLI::run(Group)

0 comments on commit dd6f0fb

Please sign in to comment.