feat: add incus module #230

Merged
unkinben merged 1 commits from neoloc/incus_module into develop 2025-03-30 01:12:53 +11:00
4 changed files with 130 additions and 0 deletions
Showing only changes of commit 0a978e651d - Show all commits

View File

@ -0,0 +1,18 @@
# frozen_string_literal: true
require 'yaml'
Facter.add(:incus) do
setcode do
# Check if the 'incus' executable exists
incus_path = Facter::Util::Resolution.which('incus')
next {} unless incus_path # Return an empty fact if incus isn't found
# Run the `incus info` command using the found path
incus_output = Facter::Core::Execution.execute("#{incus_path} info")
next {} if incus_output.empty? # Return an empty fact if there's no output
# Parse the output as YAML and return it
YAML.safe_load(incus_output)
end
end

View File

@ -0,0 +1,57 @@
# manage incus clusters
class incus::cluster (
Boolean $members_lookup = false,
String $members_role = undef,
String $master = undef,
Array $servers = [],
Stdlib::Fqdn $server_fqdn = $facts['networking']['fqdn'],
Stdlib::Port $server_port = 8443,
){
# check that the master is named
unless !($master == undef) {
fail("master must be provided for ${title}")
}
# if lookup is enabled
if $members_lookup {
# check that the role is also set
unless !($members_role == undef) {
fail("members_role must be provided for ${title} when members_lookup is True")
}
# if it is, find hosts, sort them so they dont cause changes every run
$servers_array = sort(query_nodes("enc_role='${members_role}' and region='${facts['region']}'", 'networking.fqdn'))
# else use provided array from params
}else{
$servers_array = $servers
}
# if its not an empty array. Give puppetdb a chance to be populated with data.
if length($servers_array) >= 3 {
# check if this is the master_node
if $master == $trusted['certname'] {
$master_bool = true
}else{
$master_bool = false
}
# find bootstrap status for servers
$bootstrap_array = puppetdb_query("inventory[certname, facts] { facts.enc_role = '${members_role}' }").map |$node| {
{
'fqdn' => $node['certname'],
'ip' => $node['facts']['networking']['ip'],
'clustered' => $node['facts']['incus']['environment']['server_clustered'],
'certificate' => $node['facts']['incus']['environment']['certificate'],
}
}
# determine if the cluster is bootstrapped
$cluster_bootstrapped = $bootstrap_array.any |$server| {
$server['fqdn'] == $master and $server['clustered'] == true
}
}
}

View File

@ -0,0 +1,35 @@
class incus (
Array[String] $packages = [
'incus',
'incus-tools',
'incus-client'
],
) {
package { $packages:
ensure => installed,
}
service { 'incus':
ensure => running,
enable => true,
hasstatus => true,
hasrestart => true,
}
file_line { 'subuid_root':
ensure => present,
path => '/etc/subuid',
line => 'root:1000000:1000000000',
match => '^root:',
notify => Service['incus'],
}
file_line { 'subgid_root':
ensure => present,
path => '/etc/subgid',
line => 'root:1000000:1000000000',
match => '^root:',
notify => Service['incus'],
}
}

View File

@ -0,0 +1,20 @@
config: {}
networks: []
storage_pools: []
profiles: []
projects: []
cluster:
server_name: <%= @server_fqdn %>
enabled: true
member_config:
- entity: storage-pool
name: local
key: source
value: ""
description: '"source" property for storage pool "local"'
cluster_address: <%= @cluster_address %>:<%= @server_port %>
cluster_certificate: |
<%= @certificate %>
server_address: <%= @server_fqdn %>:<%= @server_port %>
cluster_token: <%= @cluster_token %>
cluster_certificate_path: ""