Improve heat launcher user retrieval

Use getent argument to not list all users, and pwd/grp when on local
machine.

Change-Id: If98a88fa7e0b0b9cbd97fec662425650d67b49d0
This commit is contained in:
Thomas Herve
2018-04-12 15:36:21 +02:00
parent e32c3da3ff
commit be5082e09b

View File

@@ -15,9 +15,11 @@
from __future__ import print_function from __future__ import print_function
import datetime import datetime
import grp
import json import json
import logging import logging
import os import os
import pwd
import signal import signal
import subprocess import subprocess
import tempfile import tempfile
@@ -268,28 +270,26 @@ class HeatDockerLauncher(HeatBaseLauncher):
cmd = [ cmd = [
'docker', 'run', '--rm', 'docker', 'run', '--rm',
self.container_image, self.container_image,
'getent', 'passwd' 'getent', 'passwd', self.user
] ]
log.debug(' '.join(cmd)) log.debug(' '.join(cmd))
p = subprocess.Popen(cmd, stdout=subprocess.PIPE) p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
result = p.communicate()[0] result = p.communicate()[0]
for line in result.split("\n"): if result:
if line.startswith('%s:' % self.user): return result.split(':')[2]
return line.split(':')[2]
raise Exception('Could not find heat uid') raise Exception('Could not find heat uid')
def get_heat_gid(self): def get_heat_gid(self):
cmd = [ cmd = [
'docker', 'run', '--rm', 'docker', 'run', '--rm',
self.container_image, self.container_image,
'getent', 'group' 'getent', 'group', self.user
] ]
log.debug(' '.join(cmd)) log.debug(' '.join(cmd))
p = subprocess.Popen(cmd, stdout=subprocess.PIPE) p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
result = p.communicate()[0] result = p.communicate()[0]
for line in result.split("\n"): if result:
if line.startswith('%s:' % self.user): return result.split(':')[2]
return line.split(':')[2]
raise Exception('Could not find heat gid') raise Exception('Could not find heat gid')
def kill_heat(self, pid): def kill_heat(self, pid):
@@ -313,14 +313,10 @@ class HeatNativeLauncher(HeatBaseLauncher):
self.config_file, 'db_sync']) self.config_file, 'db_sync'])
def get_heat_uid(self): def get_heat_uid(self):
p = subprocess.Popen(["getent", "passwd", "|", "grep", "heat"], return pwd.getpwnam('heat').pw_uid
stdout=subprocess.PIPE)
return p.communicate()[0].rstrip().split(':')[2]
def get_heat_gid(self): def get_heat_gid(self):
p = subprocess.Popen(["getent", "group", "|", "grep", "heat"], return grp.getgrnam('heat').gr_gid
stdout=subprocess.PIPE)
return p.communicate()[0].rstrip().split(':')[2]
def kill_heat(self, pid): def kill_heat(self, pid):
os.kill(pid, signal.SIGKILL) os.kill(pid, signal.SIGKILL)