Foreman 1.24.2 - Upload Salt Reports no longer working

Problem:
After upgrading to Foreman 1.24.2 and foreman-salt 13.2.0, salt execution report stopped working.

Expected outcome:
Report should be loaded from Salt to Foreman by the cron job.

Foreman and Proxy versions:
Foreman 1.24.2
foreman-salt 13.2.0
python 2.7.5

Distribution and version:
CentOS Linux release 7.7.1908 (Core)

Other relevant data:
After each run, the following error is printed in the log:
Traceback (most recent call last):
File “/usr/sbin/upload-salt-reports”, line 151, in
upload(jobs_to_upload())
File “/usr/sbin/upload-salt-reports”, line 129, in upload
write_last_uploaded(job_id)
File “/usr/sbin/upload-salt-reports”, line 69, in write_last_uploaded
f.write(last_uploaded)
TypeError: must be unicode, not str

A workarround is to patch line 69 of /usr/sbin/upload-salt-reports
– f.write(last_uploaded)
++ f.write(unicode(last_uploaded,“utf-8”))

After this patch, the upload work again. I think this is a regression from a Python 3 support of the code that is not compatible with python 2.

Hi,

thank you very much for your patch. I’m trying to find a solution which works for python 2 and python 3. Currently, the following small script works for python2.7 and python3.5, python3.6:

import io
import sys

if sys.version_info.major == 3:
    unicode = str

last_uploaded = "Hallo welt"
LAST_UPLOADED = "/tmp/test.txt"
with io.open(LAST_UPLOADED, 'w+') as f:
  f.write(unicode(last_uploaded))

The difference to your patch is, that it does not use "unicdoe(last_uploaded, ‘utf-8’) but instead just a unicode(last_uploaded). Can you please test, if this works, too?

Thank you very much.
Best regards,
Bernhard

I tested your variation and it was successful.

Great, than I will create a PR on smart_proxy_salt.

Thanks for you help. Very appreciated.

Btw, do you already know that you can use salt reactors to upload the salt report? Have a look at the manual how to configure it.

Nope. I haven’t seen anything in the Foreman Salt plugin Documentation.
Unless you are referring into building a reactor that would listen to any hightstate event and would then execute the script /usr/sbin/upload-salt-reports ?

See https://github.com/theforeman/smart_proxy_salt/tree/master/salt/report_upload

Unicode issue PR: https://github.com/theforeman/smart_proxy_salt/pull/46