def connection(self, host, port, user, password, ssl=False, **kwargs): # must set X-Requested-By in newer versions of Ambari self.x_requested_by = user if user == 'admin': self.x_requested_by = os.getenv('USER', user) #log.info("contacting Ambari as '%s'" % self.user) if not isHost(host) or not isPort(port) or not isUser(user) or not password: raise InvalidOptionException('invalid options passed to AmbariBlueprint()') proto = 'http' # pylint: disable=unused-variable if ssl: proto = 'https' self.host = host self.port = port self.user = user self.password = password # if kwargs.has_key('strip_config') and kwargs['strip_config']: if 'strip_config' in kwargs and kwargs['strip_config']: self.strip_config = True self.url_base = '%(proto)s://%(host)s:%(port)s/api/v1' % locals() if 'dir' in kwargs and kwargs['dir']: self.blueprint_dir = kwargs['dir'] if not isDirname(self.blueprint_dir): qquit('UNKNOWN', 'invalid dir arg passed to AmbariBlueprintTool') try: if not self.blueprint_dir or not os.path.exists(self.blueprint_dir): log.info("creating blueprint data dir '%s'" % self.blueprint_dir) os.mkdir(self.blueprint_dir) if not os.path.isdir(self.blueprint_dir): raise IOError("blueprint dir '%s'already taken and is not a directory" % self.blueprint_dir) except IOError as _: die("'failed to create dir '%s': %s" % (self.blueprint_dir, _))
def construct_msg(self): # user = os.getenv('USER', '').strip() user = getpass.getuser() if not isUser(user): # print("invalid user '%s' determined from environment variable $USER, failed regex validation" % user) print( "invalid user '%s' returned by getpass.getuser(), failed regex validation" % user) sys.exit(ERRORS['CRITICAL']) user = self.case_user(user) msg = 'Welcome %s - ' % user last = '' if which("last"): _ = os.popen('last -100') _.readline() re_skip = re.compile(r'^(?:reboot|wtmp)|^\s*$') last = '' for line in _: last = line.rstrip('\n') if re_skip.match(last): last = '' continue break _.close() else: printerr( "WARNING: 'last' command not found, will not be able to get last login information" ) if last: msg += 'last login was ' last_user = re.sub(r'\s+.*$', '', last) if last_user == 'root': last_user = '******' # strip up to "Day Mon NN" ie "%a %b %e ..." (last, num_replacements) = re.subn(r'.*(\w{3}\s+\w{3}\s+\d+)', r'\g<1>', last) if not num_replacements: print('failed to find the date format in the last log') sys.exit(ERRORS['CRITICAL']) last = re.sub(' *$', '', last) if last_user == 'ROOT': msg += 'ROOT' elif last_user.lower() == user.lower(): msg += 'by you' else: msg += 'by %s' % last_user msg += ' => %s' % last else: msg += 'no last login information available!' return msg
def construct_msg(self): # pylint: disable=no-self-use # user = os.getenv('USER', '').strip() user = getpass.getuser() if not isUser(user): # print("invalid user '%s' determined from environment variable $USER, failed regex validation" % user) print("invalid user '%s' returned by getpass.getuser(), failed regex validation" % user) sys.exit(ERRORS['CRITICAL']) user = self.case_user(user) msg = 'Welcome %s - ' % user last = '' if which("last"): _ = os.popen('last -100') _.readline() re_skip = re.compile(r'^(?:reboot|wtmp)|^\s*$') last = '' for line in _: last = line.rstrip('\n') if re_skip.match(last): last = '' continue break _.close() else: printerr("WARNING: 'last' command not found, will not be able to get last login information") if last: msg += 'last login was ' last_user = re.sub(r'\s+.*$', '', last) if last_user == 'root': last_user = '******' # strip up to "Day Mon NN" ie "%a %b %e ..." (last, num_replacements) = re.subn(r'.*(\w{3}\s+\w{3}\s+\d+)', r'\g<1>', last) if not num_replacements: print('failed to find the date format in the last log') sys.exit(ERRORS['CRITICAL']) last = re.sub(' *$', '', last) if last_user == 'ROOT': msg += 'ROOT' elif last_user.lower() == user.lower(): msg += 'by you' else: msg += 'by %s' % last_user msg += ' => %s' % last else: msg += 'no last login information available!' return msg