Puppet Class: nextcloud

Defined in:
manifests/init.pp

Summary

Install and configure Nextcloud

Overview

Install and configure Nextcloud

Examples:

class { 'nextcloud':
  version           => '18.0.2',
  database_host     => '10.0.0.10',
  database_password => 'secret',
  additional_apps   => ['unsplash'],
}

Parameters:

  • archive_url (Stdlib::HTTPUrl) (defaults to: 'https://download.nextcloud.com/server/releases/')

    URL from which the Nextcloud archive will be downloaded

  • manage_phplibs (Boolean) (defaults to: true)

    false: you have to manage somewhere else the required php libs. true: the module installs php packages (which are listed in phplibs)

  • phplibs (Array) (defaults to: ['php7.2','php7.2-zip','php7.2-xml','php7.2-mbstring','php7.2-curl','php7.2-gd','php7.2-json','php-imagick','php7.2-intl','php-redis'])

    If manage_phplibs is true, list of php packages to be installed.

  • manage_redis (Boolean) (defaults to: true)

    false: the module does not install redis on the node. true: a redis service is installed on the node and bound to 127.0.0.1

  • system_user (String) (defaults to: 'www-data')

    System account existong on node that own Nextcloud files.

  • version (String) (defaults to: '18.0.3')

    Version of installed Nexcloud.

  • additional_apps (Array[String]) (defaults to: [])

    Nextcloud can be extended by applications. You can give a list a additional applications here.

  • database_driver (Enum['mysql']) (defaults to: 'mysql')

    The database engine used as backend.

  • database_host (Stdlib::Host) (defaults to: '127.0.0.1')

    Host address of the database used as backend.

  • database_name (String) (defaults to: 'nextcloud')

    Name of the database used as backend.

  • database_user (String) (defaults to: 'nextcloud')

    User accound used to connect the database.

  • database_password (String[1])

    Password used to connect the database

  • config (Nextcloud::Config) (defaults to: {})

    Content of config.php, details of handled settings described in custom data type Nextcloud::Config and Nextcloud::Config::Redis.



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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'manifests/init.pp', line 31

class nextcloud (
  String[1] $database_password,
  Stdlib::HTTPUrl $archive_url = 'https://download.nextcloud.com/server/releases/',
  Boolean $manage_phplibs = true,
  Boolean $manage_redis = true,
  Array $phplibs = ['php7.2','php7.2-zip','php7.2-xml','php7.2-mbstring','php7.2-curl','php7.2-gd','php7.2-json','php-imagick','php7.2-intl','php-redis'],
  Enum['mysql'] $database_driver = 'mysql',
  String $database_name = 'nextcloud',
  String $database_user = 'nextcloud',
  Stdlib::Host $database_host = '127.0.0.1',
  String $system_user = 'www-data',
  String $version = '18.0.3',
  Array[String] $additional_apps = [],
  Nextcloud::Config $config = {},
) {

  if $manage_phplibs {
    $_driver_package = $database_driver ? { 'mysql' => 'php7.2-mysql', 'pgsql' => 'php7.2-pgsql' }
    $_redis_package = 'php-redis'

    package { $_driver_package :
      ensure => present,
    }
    package { $phplibs :
      ensure => present,
    }

    ensure_packages( $_redis_package, { ensure => present } )
  }

  if $manage_redis {
    include redis
  }

  package { 'unzip':
    ensure => present,
  }

  $_nextcloud_appdirectory = '/var/nextcloud-app'

  $_default_config = {
    'dbhost'               => $database_host,
    'dbname'               => $database_name,
    'dbuser'               => $database_user,
    'dbpassword'           => $database_password,
    'datadirectory'        => '/var/nextcloud-data',
    'trusted_domains'      => ['localhost', $facts['networking']['fqdn']],
    'memcache.local'       => '\OC\Memcache\Redis',
    'memcache.distributed' => '\OC\Memcache\Redis',
    'memcache.locking'     => '\OC\Memcache\Redis',
    'filelocking.enabled'  => true,
    'redis'                => {
      'host'    => '127.0.0.1',
      'port'    => 6379,
      'timeout' => '1.5',
    },
  }

  $_real_config = $_default_config + $config

  $_nextcloud_installcmd = "php occ maintenance:install --database '${database_driver}' \
 --database-host ${database_host} \
 --database-name ${database_name} \
 --database-user ${database_user} \
 --database-pass '${database_password}' \
 --admin-user admin \
 --admin-pass changeme \
 --data-dir ${_real_config['datadirectory']}"

  if $manage_phplibs {
    Package[$phplibs] -> Exec['occ maintenance:install']
  }

  if $manage_redis and $manage_phplibs {
    Class['redis'] -> Package[$_redis_package] -> Exec['occ maintenance:install']
  } elsif $manage_redis {
    Class['redis'] -> Exec['occ maintenance:install']
  }

  file { [ $_nextcloud_appdirectory, $_real_config['datadirectory'] ]:
    ensure => directory,
    owner  => $system_user,
    group  => $system_user,
    mode   => '0700',
  }

  file { "${_real_config['datadirectory']}/.ocdata":
    ensure =>  file,
    owner  => $system_user,
    group  => $system_user,
    mode   => '0644',
  }

  archive { "nextcloud-${version}":
    path         => "/tmp/nextcloud-${version}.zip",
    source       => "${archive_url}/nextcloud-${version}.zip",
    extract      => true,
    extract_path => $_nextcloud_appdirectory,
    creates      => "${_nextcloud_appdirectory}/nextcloud/index.php",
    cleanup      => true,
    user         => $system_user,
    group        => $system_user,
    require      => [
      Package['unzip'],
      File[$_nextcloud_appdirectory],
      File[$_real_config['datadirectory']],
    ],
  }

  exec { 'occ maintenance:install':
    path        => '/usr/sbin:/usr/bin:/sbin:/bin',
    command     => $_nextcloud_installcmd,
    cwd         => "${_nextcloud_appdirectory}/nextcloud",
    user        => $system_user,
    refreshonly => true,
    subscribe   => Archive["nextcloud-${version}"],
  }

  exec { 'occ db:add-missing-indices':
    path        => '/usr/sbin:/usr/bin:/sbin:/bin',
    command     => 'php occ db:add-missing-indices --no-interaction',
    cwd         => "${_nextcloud_appdirectory}/nextcloud",
    user        => $system_user,
    refreshonly => true,
    subscribe   => Exec['occ maintenance:install'],
  }

  exec { 'occ db:convert-filecache-bigint':
    path        => '/usr/sbin:/usr/bin:/sbin:/bin',
    command     => 'php occ db:convert-filecache-bigint --no-interaction',
    cwd         => "${_nextcloud_appdirectory}/nextcloud",
    user        => $system_user,
    refreshonly => true,
    subscribe   => Exec['occ maintenance:install'],
  }

  cron { 'background_jobs':
    command   => "php -f ${_nextcloud_appdirectory}/nextcloud/cron.php",
    user      => $system_user,
    minute    => '*/5',
    subscribe => Exec['occ maintenance:install'],
  }

  $_real_config.each | $_config_key, $_config_value | {
    case $_config_value {
      String, Boolean, Integer: {
        exec { "occ config:system:set ${_config_key}":
          path      => '/usr/sbin:/usr/bin:/sbin:/bin',
          command   => "php occ config:system:set ${_config_key} --value='${_config_value}'",
          cwd       => "${_nextcloud_appdirectory}/nextcloud",
          user      => $system_user,
          unless    => "php occ config:system:get ${_config_key} | grep -qF '${_config_value}'",
          subscribe => Exec['occ maintenance:install'],
        }
      }
      Array: {
        $_config_value.each | $_index, $_config_array_value | {
          exec { "occ config:system:set ${_config_key} index ${_index}":
            path      => '/usr/sbin:/usr/bin:/sbin:/bin',
            command   => "php occ config:system:set ${_config_key} ${_index} --value='${_config_array_value}'",
            cwd       => "${_nextcloud_appdirectory}/nextcloud",
            user      => $system_user,
            unless    => "php occ config:system:get ${_config_key} | grep -qF '${_config_array_value}'",
            subscribe => Exec['occ maintenance:install'],
          }
        }
      }
      Hash: {
        $_config_value.each | $_config_hash_key, $_config_hash_value | {
          exec { "occ config:system:set ${_config_key} key ${_config_hash_key}":
            path      => '/usr/sbin:/usr/bin:/sbin:/bin',
            command   => "php occ config:system:set ${_config_key} '${_config_hash_key}' --value='${_config_hash_value}'",
            cwd       => "${_nextcloud_appdirectory}/nextcloud",
            user      => $system_user,
            unless    => "php occ config:system:get ${_config_key} ${_config_hash_key} | grep -qF '${_config_hash_value}'",
            subscribe => Exec['occ maintenance:install'],
          }
        }
      }
      default: {
        fail('unexpected data type in config')
      }
    }
  }

  $additional_apps.each | String $_app_name | {
    exec { "occ-app-install-${_app_name}":
      path      => '/usr/sbin:/usr/bin:/sbin:/bin',
      command   => "php occ app:install ${_app_name}",
      cwd       => "${_nextcloud_appdirectory}/nextcloud",
      user      => $system_user,
      unless    => "php occ app:list | grep -qP ' ${_app_name}: '",
      subscribe => Exec['occ maintenance:install'],
    }
  }

}