Defined Type: ohmyzsh::install

Defined in:
manifests/install.pp

Overview

Define: ohmyzsh::install

This is the ohmyzsh module. It installs oh-my-zsh for a user and changes their shell to zsh. It has been tested under Ubuntu.

This module is called ohmyzsh as Puppet does not support hyphens in module names.

oh-my-zsh is a community-driven framework for managing your zsh configuration.

Parameters

set_sh: (boolean) controls whether to change the user shell to zsh update_zshrc: (enum) controls the update of .zshrc from the upstream template. Default value is ‘disabled` auto_update_mode: (enum) controls the update check for oh-my-zsh. Default value is `disabled`. auto_update_frequency: (integer) controls the update check frequency. Default value is `14`.

Authors

Leon Brocard <acme@astray.com> Zan Loy <zan.loy@gmail.com> Rehan Mahmood

Copyright 2022

Parameters:

  • ensure (Enum[present, latest]) (defaults to: latest)
  • set_sh (Boolean) (defaults to: false)
  • update_zshrc (Enum[always, disabled, sync]) (defaults to: disabled)
  • backup_zshrc (Boolean) (defaults to: true)
  • auto_update_mode (Enum[auto, disabled, reminder]) (defaults to: disabled)
  • auto_update_frequency (Integer[0]) (defaults to: 14)


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'manifests/install.pp', line 28

define ohmyzsh::install (
  Enum[present, latest] $ensure     = latest,
  Boolean $set_sh                   = false,
  Enum[always, disabled, sync]
  $update_zshrc                     = disabled,
  Boolean $backup_zshrc             = true,
  Enum[auto, disabled, reminder]
  $auto_update_mode                 = disabled,
  Integer[0] $auto_update_frequency = 14,
) {
  include ohmyzsh
  $date_command = '$(date +\'%Y-%m-%dT%H:%M:%S%:z\')'

  if !defined(Package['git']) {
    package { 'git':
      ensure => present,
    }
  }

  if !defined(Package['zsh']) {
    package { 'zsh':
      ensure => present,
    }
  }

  if $name == 'root' {
    $home = '/root'
    $group = fact('os.family') ? {
      /(Free|Open)BSD/ => 'wheel',
      default          => 'root',
    }
  } else {
    $home = "${ohmyzsh::home}/${name}"
    $group = $name
  }

  if $set_sh {
    if !defined(User[$name]) {
      user { "ohmyzsh::user ${name}":
        ensure     => present,
        name       => $name,
        managehome => true,
        shell      => lookup('ohmyzsh::zsh_shell_path'),
        require    => Package['zsh'],
      }
    } else {
      User <| title == $name |> {
        shell => lookup('ohmyzsh::zsh_shell_path')
      }

      Package['zsh'] -> User <| title == $name |>
    }
  }

  vcsrepo { "${home}/.oh-my-zsh":
    ensure   => $ensure,
    provider => git,
    source   => $ohmyzsh::source,
    revision => 'master',
    user     => $name,
    require  => Package['git'],
  }
  -> if $update_zshrc == sync {
    if $backup_zshrc {
      exec { "backup .zshrc ${name}":
        command     => "cp ${home}/.zshrc ${home}/.zshrc.bak.${date_command}",
        path        => ['/bin', '/usr/bin'],
        onlyif      => "test -f ${home}/.zshrc",
        user        => $name,
        before      => [
          File_Line["ohmyzsh::auto_update_frequency - ${name}"],
        ],
        subscribe   => Vcsrepo["${home}/.oh-my-zsh"],
        refreshonly => true,
      }
    }
    -> exec { "ohmyzsh::cp .zshrc ${name}":
      command     => "cp ${home}/.oh-my-zsh/templates/zshrc.zsh-template ${home}/.zshrc",
      path        => ['/bin', '/usr/bin'],
      user        => $name,
      before      => [
        File_Line["ohmyzsh::auto_update_frequency - ${name}"],
      ],
      subscribe   => Vcsrepo["${home}/.oh-my-zsh"],
      refreshonly => true,
    }
  } elsif $update_zshrc == disabled {
    exec { "ohmyzsh::cp .zshrc ${name}":
      creates => "${home}/.zshrc",
      command => "cp ${home}/.oh-my-zsh/templates/zshrc.zsh-template ${home}/.zshrc",
      path    => ['/bin', '/usr/bin'],
      onlyif  => "getent passwd ${name} | cut -d : -f 6 | xargs test -e",
      user    => $name,
    }
  } elsif $update_zshrc == always {
    if $backup_zshrc {
      exec { "backup .zshrc ${name}":
        command => "cp ${home}/.zshrc ${home}/.zshrc.bak.${date_command}",
        path    => ['/bin', '/usr/bin'],
        onlyif  => "test -f ${home}/.zshrc",
        user    => $name,
      }
    }
    -> exec { "ohmyzsh::cp .zshrc ${name}":
      command => "cp ${home}/.oh-my-zsh/templates/zshrc.zsh-template ${home}/.zshrc",
      path    => ['/bin', '/usr/bin'],
      user    => $name,
    }
  }
  -> file_line { "ohmyzsh::auto_update_frequency - ${name}":
    path  => "${home}/.zshrc",
    line  => "zstyle ':omz:update' frequency ${auto_update_frequency}",
    match => '.*zstyle\ \':omz:update\'\ frequency .*',
  }
  -> if $auto_update_mode == disabled {
    file_line { "enable ohmyzsh::auto_update_mode disabled - ${name}":
      path  => "${home}/.zshrc",
      line  => "zstyle ':omz:update' mode disabled",
      match => '.*zstyle\ \':omz:update\'\ mode\ disabled.*',
    }
    file_line { "disable ohmyzsh::auto_update_mode auto - ${name}":
      path  => "${home}/.zshrc",
      line  => "# zstyle ':omz:update' mode auto",
      match => '.*zstyle\ \':omz:update\'\ mode\ auto.*',
    }
    file_line { "disable ohmyzsh::auto_update_mode reminder - ${name}":
      path  => "${home}/.zshrc",
      line  => "# zstyle ':omz:update' mode reminder",
      match => '.*zstyle\ \':omz:update\'\ mode\ reminder.*',
    }
  } elsif $auto_update_mode == auto {
    file_line { "enable ohmyzsh::auto_update_mode auto - ${name}":
      path  => "${home}/.zshrc",
      line  => "zstyle ':omz:update' mode auto",
      match => '.*zstyle\ \':omz:update\'\ mode\ auto.*',
    }
    file_line { "disable ohmyzsh::auto_update_mode disabled - ${name}":
      path  => "${home}/.zshrc",
      line  => "# zstyle ':omz:update' mode disabled",
      match => '.*zstyle\ \':omz:update\'\ mode\ disabled.*',
    }
    file_line { "disable ohmyzsh::auto_update_mode reminder - ${name}":
      path  => "${home}/.zshrc",
      line  => "# zstyle ':omz:update' mode reminder",
      match => '.*zstyle\ \':omz:update\'\ mode\ reminder.*',
    }
  } elsif $auto_update_mode == reminder {
    file_line { "enable ohmyzsh::auto_update_mode reminder - ${name}":
      path  => "${home}/.zshrc",
      line  => "zstyle ':omz:update' mode reminder",
      match => '.*zstyle\ \':omz:update\'\ mode\ reminder.*',
    }
    file_line { "disable ohmyzsh::auto_update_mode auto - ${name}":
      path  => "${home}/.zshrc",
      line  => "# zstyle ':omz:update' mode auto",
      match => '.*zstyle\ \':omz:update\'\ mode\ auto.*',
    }
    file_line { "disable ohmyzsh::auto_update_mode disabled - ${name}":
      path  => "${home}/.zshrc",
      line  => "# zstyle ':omz:update' mode disabled",
      match => '.*zstyle\ \':omz:update\'\ mode\ disabled.*',
    }
  }
  # Fix permissions on '~/.oh-my-zsh/cache/completions'
  -> file { "${home}/.oh-my-zsh/cache/completions":
    ensure  => directory,
    replace => 'no',
    owner   => $name,
    group   => $group,
    mode    => '0755',
    require => Vcsrepo["${home}/.oh-my-zsh"],
  }
}