You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2012-2013 Calculate Ltd. http://www.calculate-linux.org
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. from os import path
  17. from itertools import ifilter
  18. from calculate.core.datavars import DataVarsCore
  19. from calculate.core.server.gen_pid import search_worked_process
  20. from calculate.lib.cl_template import SystemIni
  21. from calculate.lib.utils.content import getCfgFiles
  22. from calculate.lib.utils.files import getRunCommands, readFile
  23. import time
  24. class UpdateInfo(object):
  25. """
  26. Информационный объект о процессе обновления
  27. """
  28. section = "update"
  29. varname = "updates"
  30. def __init__(self, dv=None):
  31. if dv is None:
  32. self.dv = DataVarsCore()
  33. self.dv.importCore()
  34. else:
  35. self.dv = dv
  36. def need_update(self):
  37. return self.update_ready() or self.check_for_dispatch()
  38. @classmethod
  39. def update_ready(cls):
  40. """
  41. Проверить есть ли обновления по ini.env
  42. """
  43. return SystemIni().getVar(cls.section, cls.varname) == u'on'
  44. @classmethod
  45. def set_update_ready(cls, value):
  46. """
  47. Установить статус обновления
  48. """
  49. SystemIni().setVar(cls.section,
  50. {cls.varname:"on" if value else "off"})
  51. def check_for_dispatch(self):
  52. """
  53. Есть ли в системе не примененные файлы для dispatch-conf
  54. """
  55. return bool(getCfgFiles(self.dv.Get('cl_config_protect'),
  56. prefix=self.dv.Get('cl_chroot_path')))
  57. def is_console_gui_run(self):
  58. """
  59. Проверить есть ли уже запущенная копия console-gui
  60. """
  61. return any(ifilter(lambda x: "cl-console-gui" in x, getRunCommands()))
  62. def update_already_run(self):
  63. """
  64. В системе уже есть работающий процесс обновления
  65. """
  66. return search_worked_process("update", self.dv)
  67. @property
  68. def outdated_kernel(self):
  69. flag_path = self.dv.Get('update.cl_update_outdated_kernel_path')
  70. if path.exists(flag_path):
  71. try:
  72. flag_kernel = readFile(flag_path).strip()
  73. current_kernel = self.dv.Get('update.cl_update_kernel_version')
  74. if flag_kernel != current_kernel:
  75. return True
  76. except ValueError:
  77. pass
  78. return False
  79. @outdated_kernel.setter
  80. def outdated_kernel(self, value):
  81. flag_path = self.dv.Get('update.cl_update_outdated_kernel_path')
  82. if value:
  83. with open(flag_path, 'w') as f:
  84. f.write(self.dv.Get('update.cl_update_kernel_version'))
  85. else:
  86. if path.exists(flag_path):
  87. os.unlink(flag_path)