help needed with reduce function

21 views
Skip to first unread message

Arnau

unread,
Apr 15, 2024, 11:17:59 AMApr 15
to puppet...@googlegroups.com
Dear all,

I'm working on a custom puppet (7.28) module to deploy python applciations like django, streamlint, etc... A part from the python part, the module also deploys an apache vhost (using puppet apache module from the forge).

I have some default options for apache vhost but I like to extend those defaults with custom options based on some data that I get from hiera (mainly, some excpetions to the proxy and adding some directories so apache serves the static files). 
As an example, one of my apps look like this in hiera:

python_apps:
  biostars:
    type: uwsgi
    apache:
      enable: true
      servername: forum
      app_protocol: https
      extra_proxy_pass:
       static: 'export/static'
       images: 'export/images'

the relevant part here is the extra_proxy_pass. What I'd like to achieve is create something like

proxy_pass => { 'path' => 'static', 'url' => '!' }
directories => [ 'path' => "${path}/${name}/static", 'Allow ' = >  => 'from all', 'Options' => 'Indexes'}]

proxy_pass => { 'path' => 'images', 'url' => '!' }
directories => [ 'path' => "${path}/${name}/images", 'Allow ' = >  => 'from all', 'Options' => 'Indexes'}]

To later on pass it to my vhost definition.

So, as for my understaning, I could use reduce to achieve that.But it's not clear to me how to use memo to append thos extar proxy_pass or directories to it in a reusable format.

My code, so far, looks like this:

 $base_proxy_pass =
  { 'path' => '/',
    'url' => "${apache[app_protocol]}://127.0.0.1:${port}/"
  }
if $apache.has_key(extra_proxy_pass) {
  $options = $apache[extra_proxy_pass]
  $extra_proxy_pass = $options.reduce([{proxy_pass => $base_proxy_pass}])| $memo, $value| {
  $memo <<  "proxy_pass => { 'path' => ${value[0]}, 'url' => '!' }"
  #       directories => {
  #               'path'    => "${path}/${name}/${value},
  #                           'Allow'   => 'from all',
  #                           'Options' => 'Indexes',
  #       }

  }
}
  notify {"EXTRA_MEMORY_PATH: $extra_proxy_pass":;}

This produces proxy_pass as I need it:

  Notice: EXTRA_MEMORY_PATH: [{proxy_pass => {path => /, url => https://127.0.0.1:8000/}}, proxy_pass => { 'path' => static, 'url' => '!' }, proxy_pass => { 'path' => images, 'url' => '!' }]

But now I am not sure if I can use memo for building the directories array of hashes in the same piece of code. Reading reduce docs seems like memo can be used to store differnt values:

$data = {a => 1, b => 2, c => 3}
$combine = $data.reduce |$memo, $value| {
  $string = "${memo[0]}${value[0]}"
  $number = $memo[1] + $value[1]
  [$string, $number]
}
# $combine contains [abc, 6]

but I've tried several options and none did work.

Also, is ther a way to reuse memo for the directories? or should I split my code in two calls to reduce, one for building the extra_proxy array and another call for the directories?

Maybe I using a wrong approach and there is a simplest way to achieve what I'm trying to do. I'm open to suggestions.


Thanks in advance,

Arnau

unread,
Apr 15, 2024, 4:45:47 PMApr 15
to Puppet Users
With a bit more of debuging I figured out that my code was wrong.
what I really need is:

  $extra_proxy_pass = $options.reduce([$base_proxy_pass])|$memo, $value| {
    $memo  << { 'path' => "${value[0]}", 'url' => '!' }
    }


in my previous code I was passing Strings instead of Hashes.

Now I'm calling redcue twice and got it working .

Best
Reply all
Reply to author
Forward
0 new messages