Defined Type: psick::tools::create_dir

Defined in:
manifests/tools/create_dir.pp

Summary

Create a directory and its eventual parents

Overview

Examples:

Create the the directory /data/utils/bin

psick::tools::create_dir { '/data/utils/bin': }

Parameters:

  • owner (Optional[String]) (defaults to: undef)

    The owner of the created directory

  • group (Optional[String]) (defaults to: undef)

    The group of the created directory

  • mode (Optional[Stdlib::Filemode]) (defaults to: undef)

    The mode of the created directory

  • path (Stdlib::AbsolutePath) (defaults to: $title)

    The path of the dir to create. Default $title

  • command_provider (Optional[String]) (defaults to: undef)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'manifests/tools/create_dir.pp', line 10

define psick::tools::create_dir (
  Optional[String] $owner    = undef,
  Optional[String] $group    = undef,
  Optional[Stdlib::Filemode] $mode     = undef,
  Stdlib::AbsolutePath $path = $title,
  Optional[String] $command_provider = undef,
) {
  $mkdir_command = $facts['os']['family'] ? {
    'windows' => "New-Item -ItemType Directory -Force -Path '${path}'",
    default   => "mkdir -p '${path}'",
  }
  exec { "Create directory ${title}":
    command  => $mkdir_command,
    path     => $facts['path'],
    creates  => $path,
    provider => $command_provider,
  }

  if $facts['os']['family'] != 'windows' {
    if $owner {
      exec { "chown ${owner} ${title}":
        command => "chown '${owner}' '${path}'",
        path    => $facts['path'],
        onlyif  => "[ \$(stat -c '%U' '${path}') != '${owner}' ]",
      }
    }
    if $group {
      exec { "chgrp ${group} ${title}":
        command => "chgrp '${group}' '${path}'",
        path    => $facts['path'],
        onlyif  => "[ \$(stat -c '%G' '${path}') != '${group}' ]",
      }
    }
    if $mode {
      $short_mode = regsubst($mode, '^0', '')
      exec { "chmod ${mode} ${title}":
        command => "chmod '${mode}' '${path}'",
        path    => '/bin:/sbin:/usr/sbin:/usr/bin',
        onlyif  => "[ \$(stat -c '%a' '${path}') != '${short_mode}' ]",
      }
    }
  }
}