Revision 26f15862

b/lib/backend.py
691 691
    logging.error("os create command '%s' returned error: %s, logfile: %s,"
692 692
                  " output: %s", result.cmd, result.fail_reason, logfile,
693 693
                  result.output)
694
    lines = [val.encode("string_escape")
694
    lines = [utils.SafeEncode(val)
695 695
             for val in utils.TailFile(logfile, lines=20)]
696 696
    return (False, "OS create script failed (%s), last lines in the"
697 697
            " log file:\n%s" % (result.fail_reason, "\n".join(lines)))
......
725 725
  if result.failed:
726 726
    logging.error("os create command '%s' returned error: %s output: %s",
727 727
                  result.cmd, result.fail_reason, result.output)
728
    lines = [val.encode("string_escape")
728
    lines = [utils.SafeEncode(val)
729 729
             for val in utils.TailFile(logfile, lines=20)]
730 730
    return (False, "OS rename script failed (%s), last lines in the"
731 731
            " log file:\n%s" % (result.fail_reason, "\n".join(lines)))
......
2434 2434
            #logging.exception("Error while closing fd %s", fd)
2435 2435
            pass
2436 2436

  
2437
    return result == 0, output
2437
    return result == 0, utils.SafeEncode(output.strip())
2438 2438

  
2439 2439
  def RunHooks(self, hpath, phase, env):
2440 2440
    """Run the scripts in the hooks directory.
b/lib/cli.py
554 554
        if callable(feedback_fn):
555 555
          feedback_fn(log_entry[1:])
556 556
        else:
557
          print "%s %s" % (time.ctime(utils.MergeTime(timestamp)), message)
557
          encoded = utils.SafeEncode(message)
558
          print "%s %s" % (time.ctime(utils.MergeTime(timestamp)), encoded)
558 559
        prev_logmsg_serial = max(prev_logmsg_serial, serial)
559 560

  
560 561
    # TODO: Handle canceled and archived jobs
b/lib/cmdlib.py
976 976
      lvdata = nresult.get(constants.NV_LVLIST, "Missing LV data")
977 977
      if isinstance(lvdata, basestring):
978 978
        feedback_fn("  - ERROR: LVM problem on node %s: %s" %
979
                    (node, lvdata.encode('string_escape')))
979
                    (node, utils.SafeEncode(lvdata)))
980 980
        bad = True
981 981
        node_volume[node] = {}
982 982
      elif not isinstance(lvdata, dict):
b/lib/mcpu.py
346 346
          continue
347 347
        for script, hkr, output in res.data:
348 348
          if hkr == constants.HKR_FAIL:
349
            output = output.strip().encode("string_escape")
350 349
            errs.append((node_name, script, output))
351 350
      if errs:
352 351
        raise errors.HooksAbort(errs)
b/lib/utils.py
576 576
          try:
577 577
            val = int(val)
578 578
          except ValueError, err:
579
            raise errors.OpPrereqError("Invalid %s size: %s" % (item, str(err)))
579
            raise errors.OpPrereqError("Invalid %s size: %s" % (item, err))
580 580
          beparams[item] = val
581 581
      if item in (constants.BE_AUTO_BALANCE):
582 582
        val = beparams[item]
......
1743 1743
  return rows[-lines:]
1744 1744

  
1745 1745

  
1746
def SafeEncode(text):
1747
  """Return a 'safe' version of a source string.
1748

  
1749
  This function mangles the input string and returns a version that
1750
  should be safe to disply/encode as ASCII. To this end, we first
1751
  convert it to ASCII using the 'backslashreplace' encoding which
1752
  should get rid of any non-ASCII chars, and then we again encode it
1753
  via 'string_escape' which converts '\n' into '\\n' so that log
1754
  messages remain one-line.
1755

  
1756
  @type text: str or unicode
1757
  @param text: input data
1758
  @rtype: str
1759
  @return: a safe version of text
1760

  
1761
  """
1762
  text = text.encode('ascii', 'backslashreplace')
1763
  text = text.encode('string_escape')
1764
  return text
1765

  
1766

  
1746 1767
def LockedMethod(fn):
1747 1768
  """Synchronized object access decorator.
1748 1769

  
b/scripts/gnt-cluster
381 381
  if nlvm:
382 382
    for node, text in nlvm.iteritems():
383 383
      ToStdout("Error on node %s: LVM error: %s",
384
               node, text[-400:].encode('string_escape'))
384
               node, utils.SafeEncode(text[-400:]))
385 385
      retcode |= 1
386 386
      ToStdout("You need to fix these nodes first before fixing instances")
387 387

  
b/scripts/gnt-job
28 28
from ganeti.cli import *
29 29
from ganeti import constants
30 30
from ganeti import errors
31
from ganeti import utils
31 32

  
32 33

  
33 34
#: default list of fields for L{ListJobs}
......
307 308
      format(3, "Execution log:")
308 309
      for serial, log_ts, log_type, log_msg in log:
309 310
        time_txt = FormatTimestamp(log_ts)
310
        encoded = str(log_msg).encode('string_escape')
311
        encoded = utils.SafeEncode(log_msg)
311 312
        format(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded))
312 313
  return 0
313 314

  

Also available in: Unified diff