os_check.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python2.6
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import os
  18. import platform
  19. class OSCheck:
  20. @staticmethod
  21. def get_os_type():
  22. """
  23. Return values:
  24. redhat, fedora, centos, oraclelinux, ascendos,
  25. amazon, xenserver, oel, ovs, cloudlinux, slc, scientific, psbm,
  26. ubuntu, debian, sles, sled, opensuse, suse ... and others
  27. In case cannot detect - exit.
  28. """
  29. # Read content from /etc/*-release file
  30. # Full release name
  31. dist = platform.linux_distribution()
  32. operatingSystem = dist[0].lower()
  33. # special cases
  34. if os.path.exists('/etc/oracle-release'):
  35. return 'oraclelinux'
  36. elif operatingSystem.startswith('suse linux enterprise server'):
  37. return 'sles'
  38. elif operatingSystem.startswith('red hat enterprise linux server'):
  39. return 'redhat'
  40. if operatingSystem != '':
  41. return operatingSystem
  42. else:
  43. raise Exception("Cannot detect os type. Exiting...")
  44. @staticmethod
  45. def get_os_family():
  46. """
  47. Return values:
  48. redhat, debian, suse ... and others
  49. In case cannot detect raises exception( from self.get_operating_system_type() ).
  50. """
  51. os_family = OSCheck.get_os_type()
  52. if os_family in ['redhat', 'fedora', 'centos', 'oraclelinux', 'ascendos',
  53. 'amazon', 'xenserver', 'oel', 'ovs', 'cloudlinux',
  54. 'slc', 'scientific', 'psbm', 'centos linux']:
  55. os_family = 'RedHat'
  56. elif os_family in ['ubuntu', 'debian']:
  57. os_family = 'Debian'
  58. elif os_family in ['sles', 'sled', 'opensuse', 'suse']:
  59. os_family = 'Suse'
  60. #else: os_family = OSCheck.get_os_type()
  61. return os_family.lower()
  62. @staticmethod
  63. def get_os_version():
  64. """
  65. Returns the OS version
  66. In case cannot detect raises exception.
  67. """
  68. # Read content from /etc/*-release file
  69. # Full release name
  70. dist = platform.linux_distribution()
  71. dist = dist[1]
  72. if dist:
  73. return dist
  74. else:
  75. raise Exception("Cannot detect os version. Exiting...")
  76. @staticmethod
  77. def get_os_major_version():
  78. """
  79. Returns the main OS version like
  80. Centos 6.5 --> 6
  81. RedHat 1.2.3 --> 1
  82. """
  83. return OSCheck.get_os_version().split('.')[0]
  84. @staticmethod
  85. def get_os_release_name():
  86. """
  87. Returns the OS release name
  88. In case cannot detect raises exception.
  89. """
  90. dist = platform.linux_distribution()
  91. dist = dist[2].lower()
  92. if dist:
  93. return dist
  94. else:
  95. raise Exception("Cannot detect os release name. Exiting...")