Defined Type: cfwebapp::redmine

Defined in:
manifests/redmine.pp

Overview

Parameters:

  • app_dbaccess (CfWeb::DBAccess)
  • smtp (CfWeb::SMTP) (defaults to: {})
  • imap (Optional[CfWeb::IMAP]) (defaults to: undef)
  • server_name (String[1]) (defaults to: $title)
  • auto_cert (Hash[String[1],Any]) (defaults to: {})
  • shared_cert (CfWeb::SharedCert) (defaults to: [])
  • is_backend (Boolean) (defaults to: false)
  • robots_noindex (Boolean) (defaults to: true)
  • memory_weight (Integer[1]) (defaults to: 100)
  • memory_min (Integer[256]) (defaults to: 404)
  • memory_max (Optional[Integer[404]]) (defaults to: undef)
  • deploy_type (String[1]) (defaults to: 'vcstag')
  • deploy_tool (String[1]) (defaults to: 'svn')
  • deploy_url (String[1]) (defaults to: 'https://svn.redmine.org/redmine')
  • deploy_match (String[1]) (defaults to: '4.*')
  • ruby_ver (String[1]) (defaults to: '2.6')
  • rake_secret (Optional[String[1]]) (defaults to: undef)
  • fw_ports (Hash[String[1], Hash]) (defaults to: {})
  • plugins (Hash[String[1], Hash]) (defaults to: { #'redmine_bots' => { # 'impl' => 'cfwebapp::redmine::redmine_bots', #}, #'redmine_2fa' => { # 'impl' => 'cfwebapp::redmine::redmine_2fa', #}, #'redmine_issue_checklist' => { # 'impl' => 'cfwebapp::redmine::redmine_issue_checklist', #}, })
  • themes (Hash[String[1], Hash]) (defaults to: {})
  • site_params (Hash[String[1], Any]) (defaults to: {})


6
7
8
9
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'manifests/redmine.pp', line 6

define cfwebapp::redmine (
    CfWeb::DBAccess $app_dbaccess,
    CfWeb::SMTP $smtp = {},
    Optional[CfWeb::IMAP] $imap = undef,

    String[1] $server_name = $title,

    Hash[String[1],Any] $auto_cert = {},
    CfWeb::SharedCert $shared_cert = [],

    Boolean $is_backend = false,
    Boolean $robots_noindex = true,

    Integer[1] $memory_weight = 100,
    Integer[256] $memory_min = 404,
    Optional[Integer[404]] $memory_max = undef,

    String[1] $deploy_type = 'vcstag',
    String[1] $deploy_tool = 'svn',
    String[1] $deploy_url = 'https://svn.redmine.org/redmine',
    String[1] $deploy_match = '4.*',
    String[1] $ruby_ver = '2.6',
    Optional[String[1]] $rake_secret = undef,

    Hash[String[1], Hash] $fw_ports = {},

    Hash[String[1], Hash] $plugins = {
        #'redmine_bots' => {
        #    'impl' => 'cfwebapp::redmine::redmine_bots',
        #},
        #'redmine_2fa' => {
        #    'impl' => 'cfwebapp::redmine::redmine_2fa',
        #},
        #'redmine_issue_checklist' => {
        #    'impl' => 'cfwebapp::redmine::redmine_issue_checklist',
        #},
    },

    Hash[String[1], Hash] $themes = {},

    Hash[String[1], Any] $site_params = {},
) {
    require cfwebapp::redmine::gandi
    include cfweb::nginx

    $user = "app_${title}"
    $site_dir = "${cfweb::nginx::web_dir}/${user}"

    # Where to put plugins for installation
    # ---
    $plugins_zip_dir  = "${site_dir}/.redmine_plugins"
    $themes_zip_dir  = "${site_dir}/.redmine_themes"

    file { [ $plugins_zip_dir, $themes_zip_dir ]:
        ensure  => directory,
        purge   => true,
        force   => true,
        recurse => true,
        owner   => $user,
        group   => $user,
        mode    => '0700',
    }
    -> file { "${site_dir}/.unpack-plugins.sh":
        owner   => $user,
        group   => $user,
        mode    => '0700',
        content => @(EOT)
        #!/bin/bash

        for t in plugins public/themes; do        
        for p in $(find ../.redmine_$(basename $t)/ -type f); do
            case $p in
                *.zip)
                    /usr/bin/unzip -q -d ./$t $p
                    ;;
                *.tgz|*.tar.gz)
                    /usr/bin/tar xzf -C ./$t $p
                    ;;
                *.tar|*.tar)
                    /usr/bin/tar xf -C ./$t $p
                    ;;
                *)
                    echo "Unsupported $p"
                    exit 1
                    ;;
            esac
            
            p=$(basename $p)
            p=${p%.*}
            pn=$(echo $p | cut -d- -f1)
            test -d ./$t/$pn || mv ./$t/$p ./$t/$pn
        done
        done
        |EOT
    }

    # Secret
    # ---
    # TODO: shared secret in cluster

    if !$rake_secret and $cfweb::is_secondary {
        fail('There must be shared rake_secret set in cluster')
    }

    $secret = cfsystem::gen_pass("rake:${title}", 32, $rake_secret)

    # SMTP
    # ---
    $smtp_host = pick_default($smtp['host'], 'localhost')
    $smtp_port = pick_default($smtp['port'], 25)
    ensure_resource('cfnetwork::describe_service', "smtp_${smtp_port}", {
        server => "tcp/${smtp_port}",
    })
    cfnetwork::client_port { "any:smtp_${smtp_port}:${user}":
        user => $user,
    }

    # IMAP
    # ---
    if $imap and !$cfweb::is_secondary {
        if $imap['ssl'] {
            $imap_port = pick($imap['port'], 993)
            $imap_ssl_arg = '--ssl'
        } else {
            $imap_port = pick($imap['port'], 143)
            $imap_ssl_arg = ''
        }

        package { 'fetchmail': }
        -> file { "${site_dir}/.fetchmail.sh":
            owner   => $user,
            group   => $user,
            mode    => '0700',
            content => @("EOT"/$)
            #!/bin/dash
            
            cat >\${HOME}/.netrc <<EOC
            machine ${imap['host']}
            login ${imap['user']}
            password ${imap['password']}
            EOC
            
            exec /usr/bin/fetchmail \
                --timeout 15 \
                --silent \
                --all \
                --nokeep \
                -p IMAP --idle \
                --service ${imap_port} \
                --folder INBOX \
                ${imap_ssl_arg} \
                --mda 'cid tool exec bundler -- exec rake redmine:email:read RAILS_ENV="production"' \
                --limit 5242880 \
                --user '${imap['user']}' \
                '${imap['host']}'
            |EOT
        }
        -> Cfweb::App::Futoin[$title]

        $imap_ep_tune = [
            '"internal":1',
            '"minMemory":"128M"',
            '"maxMemory":"128M"',
            '"maxInstances":1',
        ].join(',')

        $imap_deploy_set = [
            "entrypoint fetchmail exe ../.fetchmail.sh '{${imap_ep_tune}}'",
        ]

        ensure_resource('cfnetwork::describe_service', "imap_${imap_port}", {
            server => "tcp/${imap_port}",
        })
        cfnetwork::client_port { "any:imap_${imap_port}:${user}":
            user => $user,
        }
    } else {
        $imap_deploy_set = []
    }

    # RMagick
    # ---
    ensure_resource('package', 'libmagickwand-dev')

    Package['libmagickwand-dev']
    -> Cfweb::Site[$title]

    # ---
    ensure_resource('cfweb::site', $title, $site_params + {
        server_name        => $server_name,
        auto_cert          => $auto_cert,
        shared_cert        => $shared_cert,
        is_backend         => $is_backend,
        robots_noindex     => $robots_noindex,
        dbaccess           => {
            app => $app_dbaccess,
        },
        apps               => {
            futoin => {
                memory_weight => $memory_weight,
                memory_min    => $memory_min,
                memory_max    => $memory_max,
                fw_ports      => $fw_ports,
            },
        },
        deploy             => {
            type          => $deploy_type,
            tool          => $deploy_tool,
            url           => $deploy_url,
            match         => $deploy_match,
            custom_script => @("EOT"/$)
                #!/bin/bash
                set -e
                source .env
                umask 027
                
                # DB
                #----
                case \${DB_APP_TYPE} in
                    mysql)
                        adapter=mysql2
                        ;;
                    *)
                        adapter=\${DB_APP_TYPE}
                        ;;
                esac
                
                CONF_DIR=.redmine_conf
                mkdir -p \$CONF_DIR

                cat >\$CONF_DIR/database.yml.tmp <<EOF
                ---
                production:
                    adapter: \$adapter
                    database: \${DB_APP_DB}
                    host: \${DB_APP_HOST}
                    port: \${DB_APP_PORT}
                    username: \${DB_APP_USER}
                    password: \${DB_APP_PASS}
                    encoding: utf8
                    socket: \${DB_APP_SOCKET}
                    connect_timeout: 3
                EOF
                mv -f \$CONF_DIR/database.yml.tmp \$CONF_DIR/database.yml
                
                # Secret
                #----
                cat >\$CONF_DIR/secrets.yml.tmp <<EOF
                ---
                production:
                    secret_key_base: ${secret}
                    secret_token: ${secret}
                EOF
                mv -f \$CONF_DIR/secrets.yml.tmp \$CONF_DIR/secrets.yml
                
                # Main config
                #----
                c_from=${pick_default($smtp.dig('from'), '')}
                if [ -n "\$c_from" ]; then l_from="from: \$c_from"; fi
                    
                c_reply_to=${pick_default($smtp.dig('reply_to'), '')}
                if [ -n "\$c_reply_to" ]; then l_reply_to="reply_to: \$c_reply_to"; fi

                c_user=${pick_default($smtp.dig('user'), '')}
                if [ -n "\$c_user" ]; then
                    l_user="user_name: \$c_user"
                    l_pass="password: ${pick_default($smtp.dig('password'), '')}"
                    l_auth="authentication: ${pick_default($smtp.dig('auth_mode'), 'plain')}"
                fi
                
                which_ignore() {
                    which ${1} 2>/dev/null || true
                }

                #---
                cat >\$CONF_DIR/configuration.yml.tmp <<EOF
                ---
                production:
                    email_delivery:
                        delivery_method: :smtp
                        raise_delivery_errors: false
                        default_options:
                            \$l_from
                            \$l_reply_to
                        smtp_settings:
                            address: ${smtp_host}
                            port: ${smtp_port}
                            enable_starttls_auto: ${pick_default($smtp.dig('start_tls'), false)}
                            \$l_auth
                            \$l_user
                            \$l_pass
                
                default:
                    attachments_storage_path:
                    autologin_cookie_name:
                    autologin_cookie_path:
                    autologin_cookie_secure:
                    
                    scm_subversion_command: \$(which_ignore svn)
                    scm_mercurial_command: \$(which_ignore hg)
                    scm_git_command: \$(which_ignore git)
                    scm_cvs_command:
                    scm_bazaar_command:
                    scm_darcs_command:
                    
                    scm_stderr_log_file:
                    
                    database_cipher_key:
                    
                    rmagick_font_path:
                EOF
                mv -f \$CONF_DIR/configuration.yml.tmp \$CONF_DIR/configuration.yml
                
                #---
                cat >\$CONF_DIR/additional_environment.rb.tmp <<EOF
                require 'syslog/logger'
                
                config.logger = Syslog::Logger.new '${user}'
                config.log_level = :fatal
                EOF
                mv -f \$CONF_DIR/additional_environment.rb.tmp \$CONF_DIR/additional_environment.rb

                # Redmine #32109 until fixed in upstream
                #---
                cat >\$CONF_DIR/issue_32109.patch <<EOP
                Index: app/models/issue.rb
                ===================================================================
                --- app/models/issue.rb (revision 18330)
                +++ app/models/issue.rb (working copy)
                @@ -1509,7 +1509,18 @@
                   # Returns a scope of projects that user can assign the issue to
                   def allowed_target_projects(user=User.current, context=nil)
                     if new_record? && context.is_a?(Project) && !copy?
                -      current_project = context.self_and_descendants
                +      case Setting.cross_project_subtasks
                +      when 'system'
                +        current_project = nil
                +      when 'tree'
                +        current_project = context.root.self_and_descendants
                +      when 'hierarchy'
                +        current_project = context.hierarchy
                +      when 'descendants'
                +        current_project = context.self_and_descendants
                +      else
                +        current_project = nil
                +      end
                     elsif new_record?
                       current_project = nil
                     else
                EOP
                
                # Trigger re-deploy on change
                #---
                cid deploy set env redminePlugins "$(ls .redmine_plugins)"
                cid deploy set env redmineThemes "$(ls .redmine_themes)"
                | EOT
                ,
            deploy_set    => [
                "env rubyVer ${ruby_ver}",
                'action prepare app-config database-config unpack-plugins app-install',
                [
                    'action app-config',
                    "'ln -sfn ../../.redmine_conf/configuration.yml config/'",
                    "'ln -sfn ../../.redmine_conf/additional_environment.rb config/'",
                    "'rm -f config/initializers/secret_token.rb'",
                    "'ln -sfn ../../.redmine_conf/secrets.yml config/'",
                    "'rm -rf tmp && ln -s ../.tmp tmp'",
                    "'patch -p0 -l -i ../.redmine_conf/issue_32109.patch || true'",
                    "'@cid tool exec bundler -- remove puma'",
                ].join(' '),
                [
                    'action database-config',
                    "'ln -sfn ../../.redmine_conf/database.yml config/'",
                ].join(' '),
                [
                    'action app-install',
                    "'@cid build-dep ruby mysql-client imagemagick tzdata libxml2'",
                    "'@cid tool exec bundler -- install --without \"development test\"'",
                    "'@cid tool exec gem -- install puma'",
                ].join(' '),
                [
                    'action unpack-plugins',
                    '../.unpack-plugins.sh',
                ].join(' '),
                [
                    'action migrate',
                    "'@cid tool exec bundler -- exec rake db:migrate RAILS_ENV=production'",
                    "'@cid tool exec bundler -- exec rake redmine:load_default_data RAILS_ENV=production REDMINE_LANG=en'",
                    "'@cid tool exec bundler -- exec rake redmine:plugins:migrate RAILS_ENV=production'",
                ].join(' '),
                'persistent files log public/plugin_assets',
                'entrypoint web nginx public socketType=unix',
                'entrypoint app puma config.ru internal=1 connMemory=32M minMemory=256M',
                'webcfg root public',
                'webcfg main app',
                "webmount / '{\"static\":true}'",
            ] + $imap_deploy_set,
        }
    })

    # ---
    $plugins.each |$name, $params| {
        $impl = pick($params['impl'], 'cfwebapp::redmine::generic')
        $rsc_name = "${title}:${name}"

        create_resources(
            $impl,
            {
                $rsc_name => merge($params - 'impl', {
                    target_dir  => $plugins_zip_dir,
                    plugin_name => $name,
                })
            }
        )

        File[$plugins_zip_dir]
        -> Cfwebapp::Redmine::Generic[$rsc_name]
        -> Cfweb::Deploy[$title]
    }

    # ---
    $themes.each |$name, $params| {
        $impl = pick($params['impl'], 'cfwebapp::redmine::generic')
        $rsc_name = "${title}:${name}"

        create_resources(
            $impl,
            {
                $rsc_name => merge($params - ['impl', 'theme_version'], {
                    target_dir     => $themes_zip_dir,
                    plugin_name    => $name,
                    plugin_version => $params['theme_version'],
                })
            }
        )

        File[$themes_zip_dir]
        -> Cfwebapp::Redmine::Generic[$rsc_name]
        -> Cfweb::Deploy[$title]
    }
}