[buildout]
extends =
  ../fluentd/software-py2.cfg
  test-common.cfg

[template-erp5]
recipe =
target = ${template-erp5-patched:location}

[template-erp5-base]
<= template-erp5
recipe = slapos.recipe.build:download

[template-erp5-patched]
<= patch-template
base = ${template-erp5-base:target}

[template-fluentd]
<= patch-template
method = unpack
extra =
  {%- set tags = {} %}
  ${:tags}
  {%- if tags %}
  [feeder]
  recipe = slapos.cookbook:wrapper
  wrapper-path = $${directory:services}/$${:_buildout_section_name_}
  command-line = ${feeder:bin-directory}/feeder $${fluentd-agent-conf:output}
    $${:_buildout_section_name_} .4
  {%- for tag in tags %}
  [{{ section(tag) }}]
  <= feeder
  {%- endfor %}

  [{{ section('fluentd') }}]
  recipe = slapos.cookbook:wrapper
  wrapper-path = $${directory:services}/$${:_buildout_section_name_}
  command-line = ${fluentd:location}/bin/fluentd
    -v -c $${fluentd-agent-conf:output}
  environment =
    GEM_PATH=${fluentd:location}/lib/ruby/gems/1.8

  [fluentd-agent-conf]
  recipe = slapos.recipe.template:jinja2
  output = $${directory:etc}/fluentd-agent.conf
  inline =
    {% raw -%}
    <match debug.**>
      @type stdout
      @id stdout_output
    </match>
    <source>
# in_exec is unusable because it logs dropped events in case of buffer overflow
      @type unix
      path $${directory:run}/fluentd.sock
    </source>
    {%- for tag, netloc in tags.iteritems() %}
    <match {{ tag }}>
      @type          wendelin
      @id            {{ tag }}
      streamtool_uri http://{{ netloc }}/erp5/portal_ingestion_policies/scalability_test_${:method}
      user           {{ user }}
      password       {{ password }}
      buffer_type    memory
      buffer_queue_limit 4
    </match>
    {%- endfor %}
    {%- endraw %}
  context =
    key tags :tags
    raw user {{ slapparameter_dict['inituser-login'] }}
    raw password {{ slapparameter_dict['inituser-password'] }}
  tags = {{ dumps(tags) }}
  {%- endif %}

[feeder]
recipe = zc.recipe.egg
eggs = fluent-logger
entry-points = ${:_buildout_section_name_}=__main__:main
initialization =
  import re, time, urlparse
  from urllib import FancyURLopener
  from fluent.sender import FluentSender
  def main():
    conf, tag, interval = sys.argv[1:]
    with open(conf) as f:
      for k, v in re.compile(r"<(.+?)>\s*(.*?)\s*</.+?>",
                             re.S).findall(f.read()):
        v = dict(v.split() for v in v.splitlines())
        if k == 'source':
          sock = v['path']
        elif v['@type'] == 'wendelin':
          if k.split(None, 1)[1] == tag:
            conf = v
    emit = FluentSender(tag, host='unix://'+sock, timeout=None,
      msgpack_kwargs={'use_bin_type': True}).emit
    url = urlparse.urlsplit(conf['streamtool_uri'])
    url = urlparse.urlunsplit(url._replace(
      netloc='%s:%s@%s' % (conf['user'], conf['password'], url.netloc),
      path='/erp5/isPortalBeingCreated'))
    urlopen = FancyURLopener().open
    while True:
      try:
        f = urlopen(url)
      except IOError:
        pass
      else:
        try:
          if not f.read():
            break
        finally:
          f.close()
      time.sleep(5)
    interval = float(interval)
    ${:feeder}

[versions]
fluent-logger = 0.5.3
