首页 > 代码库 > 服务配置文件gb2312编码,使用SaltStack管理报错
服务配置文件gb2312编码,使用SaltStack管理报错
Salt发布2014.7.0后,及时进行了更新,测试下来,在上一版本正常运行的操作,这一版本却出现报错。公司的好多服务由于历史原因,配置文件延续下来,使用的是gb2312编码,其中还有中文注释。在使用salt进行管理的时候,在Master端会报UnicodeDecodeError错误。
执行配置文件更新,并重启服务操作:
salt ‘zhaogb-202‘ state.sls update_conf.manager
manager.sls是更新Manager服务配置文件的模板:
ma_watch: service: - name: ManagerServerd - running - reload: True - watch: - file: /home/ManagerServer/config.xml /home/ManagerServer/config.xml: file.managed: - source: salt://dzh_store/conf_file/ManagerServer/config.xml - user: root - group: root - mode: 644 - backup: minion
执行该更新操作后,会报如下错误:
[ERROR ] An un-handled exception was caught by salt‘s global exception handler: UnicodeDecodeError: ‘utf8‘ codec can‘t decode byte 0xca in position 18: invalid continuation byte Traceback (most recent call last): File "/usr/bin/salt", line 10, in <module> salt_main() File "/usr/lib/python2.6/site-packages/salt/scripts.py", line 240, in salt_main client.run() File "/usr/lib/python2.6/site-packages/salt/cli/__init__.py", line 189, in run self._output_ret(ret_, out) File "/usr/lib/python2.6/site-packages/salt/cli/__init__.py", line 243, in _output_ret salt.output.display_output(ret, out, self.config) File "/usr/lib/python2.6/site-packages/salt/output/__init__.py", line 44, in display_output display_data = get_printout(out, opts)(data).rstrip() File "/usr/lib/python2.6/site-packages/salt/output/highstate.py", line 74, in output return _format_host(host, hostdata)[0] File "/usr/lib/python2.6/site-packages/salt/output/highstate.py", line 118, in _format_host schanged, ctext = _format_changes(ret[‘changes‘]) File "/usr/lib/python2.6/site-packages/salt/output/highstate.py", line 340, in _format_changes __opts__) File "/usr/lib/python2.6/site-packages/salt/output/__init__.py", line 121, in out_format return get_printout(out, opts)(data).rstrip() File "/usr/lib/python2.6/site-packages/salt/output/nested.py", line 124, in output return nest.display(ret, __opts__.get(‘nested_indent‘, 0), ‘‘, ‘‘) File "/usr/lib/python2.6/site-packages/salt/output/nested.py", line 115, in display out = self.display(val, indent + 4, ‘‘, out) File "/usr/lib/python2.6/site-packages/salt/output/nested.py", line 90, in display prefix=prefix) File "/usr/lib/python2.6/site-packages/salt/output/nested.py", line 59, in ustring indent, color, prefix, msg.decode(encoding), endc, suffix) File "/usr/lib64/python2.6/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: ‘utf8‘ codec can‘t decode byte 0xca in position 18: invalid continuation byte Traceback (most recent call last): File "/usr/bin/salt", line 10, in <module> salt_main() File "/usr/lib/python2.6/site-packages/salt/scripts.py", line 240, in salt_main client.run() File "/usr/lib/python2.6/site-packages/salt/cli/__init__.py", line 189, in run self._output_ret(ret_, out) File "/usr/lib/python2.6/site-packages/salt/cli/__init__.py", line 243, in _output_ret salt.output.display_output(ret, out, self.config) File "/usr/lib/python2.6/site-packages/salt/output/__init__.py", line 44, in display_output display_data = get_printout(out, opts)(data).rstrip() File "/usr/lib/python2.6/site-packages/salt/output/highstate.py", line 74, in output return _format_host(host, hostdata)[0] File "/usr/lib/python2.6/site-packages/salt/output/highstate.py", line 118, in _format_host schanged, ctext = _format_changes(ret[‘changes‘]) File "/usr/lib/python2.6/site-packages/salt/output/highstate.py", line 340, in _format_changes __opts__) File "/usr/lib/python2.6/site-packages/salt/output/__init__.py", line 121, in out_format return get_printout(out, opts)(data).rstrip() File "/usr/lib/python2.6/site-packages/salt/output/nested.py", line 124, in output return nest.display(ret, __opts__.get(‘nested_indent‘, 0), ‘‘, ‘‘) File "/usr/lib/python2.6/site-packages/salt/output/nested.py", line 115, in display out = self.display(val, indent + 4, ‘‘, out) File "/usr/lib/python2.6/site-packages/salt/output/nested.py", line 90, in display prefix=prefix) File "/usr/lib/python2.6/site-packages/salt/output/nested.py", line 59, in ustring indent, color, prefix, msg.decode(encoding), endc, suffix) File "/usr/lib64/python2.6/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: ‘utf8‘ codec can‘t decode byte 0xca in position 18: invalid continuation byte
执行返回报错,但是Minion端配置文件已经更新,并且服务重启载入新的配置文件。也就是说,这个报错产生在Master端,对Minion没有影响。所有操作都在上一个版本(2014.1.0)验证过,没有任何问题。排查下来,找到
/usr/lib/python2.6/site-packages/salt/output/nested.py
这个文件,与上一个版本相比,增加了一个ustring方法,其中定义了一个带默认值encoding=‘utf-8‘参数,问题就出现在这里。
2014.7.0 nested.py
由于我的配置文件采用gb2312编码,所以在对msg进行decode操作时使用utf-8会报错,再增加一个try,except调试来解决这个问题,修改后的代码如下:
也就是修改了except,再做一次判断。
至此,这个问题暂时解决了,无论是utf-8配置文件,还是gb2312配置文件,都通过,不确定是否会影响Salt本身的功能。
本文出自 “Guibin的博客” 博客,请务必保留此出处http://guibin.blog.51cto.com/8909901/1596296
服务配置文件gb2312编码,使用SaltStack管理报错