| #!/usr/bin/env python |
| """ |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| |
| Ambari Agent |
| |
| """ |
| |
| __all__ = ["System"] |
| |
| import os |
| import sys |
| from resource_management.core import shell |
| from functools import wraps |
| |
| def lazy_property(undecorated): |
| name = '_' + undecorated.__name__ |
| |
| @property |
| @wraps(undecorated) |
| def decorated(self): |
| try: |
| return getattr(self, name) |
| except AttributeError: |
| v = undecorated(self) |
| setattr(self, name, v) |
| return v |
| |
| return decorated |
| |
| class System(object): |
| @lazy_property |
| def os(self): |
| platform = sys.platform |
| if platform.startswith('linux'): |
| return "linux" |
| elif platform == "darwin": |
| return "darwin" |
| else: |
| return "unknown" |
| |
| def unquote(self, val): |
| if val[0] == '"': |
| val = val[1:-1] |
| return val |
| |
| @lazy_property |
| def arch(self): |
| machine = self.machine |
| if machine in ("i386", "i486", "i686"): |
| return "x86_32" |
| return machine |
| |
| @lazy_property |
| def machine(self): |
| code, out = shell.call(["/bin/uname", "-m"]) |
| return out.strip() |
| |
| @lazy_property |
| def lsb(self): |
| if os.path.exists("/usr/bin/lsb_release"): |
| code, out = shell.call(["/usr/bin/lsb_release", "-a"]) |
| lsb = {} |
| for l in out.split('\n'): |
| v = l.split(':', 1) |
| if len(v) != 2: |
| continue |
| lsb[v[0].strip().lower()] = self.unquote(v[1].strip().lower()) |
| |
| # failsafe |
| if not 'distributor id' in lsb: |
| return None |
| |
| lsb['id'] = lsb.pop('distributor id') |
| return lsb |
| |
| return None |
| |
| @lazy_property |
| def platform(self): |
| operatingsystem = self.os |
| if operatingsystem == "linux": |
| lsb = self.lsb |
| if not lsb: |
| if os.path.exists("/etc/redhat-release"): |
| return "redhat" |
| if os.path.exists("/etc/fedora-release"): |
| return "fedora" |
| if os.path.exists("/etc/centos-release"): |
| return "centos" |
| if os.path.exists("/etc/SuSE-release"): |
| return "suse" |
| if os.path.exists("/etc/system-release"): |
| with open("/etc/system-release", "rb") as fp: |
| release = fp.read() |
| if "Amazon Linux" in release: |
| return "amazon" |
| return "unknown" |
| |
| lsb_id = lsb['id'].lower() |
| if lsb_id =="suse linux": |
| return "suse" |
| return lsb_id |
| return "unknown" |
| |
| @lazy_property |
| def locales(self): |
| code, out = shell.call("locale -a") |
| return out.strip().split("\n") |
| |
| @lazy_property |
| def ec2(self): |
| if not os.path.exists("/proc/xen"): |
| return False |
| if os.path.exists("/etc/ec2_version"): |
| return True |
| return False |
| |
| @lazy_property |
| def vm(self): |
| if os.path.exists("/usr/bin/VBoxControl"): |
| return "vbox" |
| elif os.path.exists("/usr/bin/vmware-toolbox-cmd") or os.path.exists( |
| "/usr/sbin/vmware-toolbox-cmd"): |
| return "vmware" |
| elif os.path.exists("/proc/xen"): |
| return "xen" |
| return None |
| |
| @classmethod |
| def get_instance(cls): |
| try: |
| return cls._instance |
| except AttributeError: |
| cls._instance = cls() |
| return cls._instance |