Some simple cleanups + tiny features.
This commit is contained in:
parent
6516c05810
commit
010506d2b3
@ -199,9 +199,9 @@ class NovaUninstaller(comp.PythonUninstallComponent):
|
|||||||
sh.execute(*cmd, run_as_root=True)
|
sh.execute(*cmd, run_as_root=True)
|
||||||
|
|
||||||
def _clear_libvirt_domains(self):
|
def _clear_libvirt_domains(self):
|
||||||
inst_prefix = self.cfg.get('nova', 'instance_name_prefix')
|
|
||||||
virt_driver = self.cfg.get('nova', 'virt_driver')
|
virt_driver = self.cfg.get('nova', 'virt_driver')
|
||||||
if virt_driver == virsh.VIRT_TYPE:
|
if virt_driver == virsh.VIRT_TYPE:
|
||||||
|
inst_prefix = self.cfg.get('nova', 'instance_name_prefix')
|
||||||
libvirt_type = virsh.default(self.cfg.get('nova', 'libvirt_type'))
|
libvirt_type = virsh.default(self.cfg.get('nova', 'libvirt_type'))
|
||||||
virsh.clear_libvirt_domains(libvirt_type, inst_prefix)
|
virsh.clear_libvirt_domains(libvirt_type, inst_prefix)
|
||||||
|
|
||||||
|
@ -18,16 +18,9 @@ import os
|
|||||||
|
|
||||||
from devstack import log as logging
|
from devstack import log as logging
|
||||||
|
|
||||||
TRUE_VALUES = ['yes', 'true', 't', '1', 'on']
|
|
||||||
LOG = logging.getLogger("devstack.environment")
|
LOG = logging.getLogger("devstack.environment")
|
||||||
|
|
||||||
|
|
||||||
def _str2bool(value_str):
|
|
||||||
if value_str.lower().strip() in TRUE_VALUES:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def get():
|
def get():
|
||||||
return dict(os.environ)
|
return dict(os.environ)
|
||||||
|
|
||||||
@ -36,6 +29,7 @@ def set(k, v):
|
|||||||
#this is really screwy, python is really odd in this area
|
#this is really screwy, python is really odd in this area
|
||||||
#from http://docs.python.org/library/os.html
|
#from http://docs.python.org/library/os.html
|
||||||
if k is not None:
|
if k is not None:
|
||||||
|
LOG.debug("Setting environment key [%s] to value [%s]" % (k, v))
|
||||||
os.environ[str(k)] = str(v)
|
os.environ[str(k)] = str(v)
|
||||||
|
|
||||||
|
|
||||||
@ -49,9 +43,3 @@ def get_key(key, default_value=None):
|
|||||||
LOG.debug("Found \"%s\" in environment variable \"%s\"" % (value, key))
|
LOG.debug("Found \"%s\" in environment variable \"%s\"" % (value, key))
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def get_bool(key, default_value=False):
|
|
||||||
value = get_key(key, None)
|
|
||||||
if value is None:
|
|
||||||
return default_value
|
|
||||||
return _str2bool(value)
|
|
||||||
|
@ -32,21 +32,18 @@ VIRT_TYPE = 'libvirt'
|
|||||||
VIRT_LIB = VIRT_TYPE
|
VIRT_LIB = VIRT_TYPE
|
||||||
DEFAULT_VIRT = 'qemu'
|
DEFAULT_VIRT = 'qemu'
|
||||||
|
|
||||||
#how libvirt is restarted
|
#distros name the libvirt service differently :-(
|
||||||
LIBVIRT_RESTART_CMD = {
|
SV_NAME_MAP = {
|
||||||
settings.RHEL6: ['service', 'libvirtd', 'restart'],
|
settings.RHEL6: 'libvirtd',
|
||||||
settings.FEDORA16: ['service', 'libvirtd', 'restart'],
|
settings.FEDORA16: 'libvirtd',
|
||||||
#whyyyy??
|
settings.UBUNTU11: 'libvirt-bin',
|
||||||
settings.UBUNTU11: ['service', 'libvirt-bin', 'restart'],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#how libvirt is restarted
|
||||||
|
LIBVIRT_RESTART_CMD = ['service', '%SERVICE%', 'restart']
|
||||||
|
|
||||||
#how we check its status
|
#how we check its status
|
||||||
LIBVIRT_STATUS_CMD = {
|
LIBVIRT_STATUS_CMD = ['service', '%SERVICE%', 'status']
|
||||||
settings.RHEL6: ['service', 'libvirtd', 'status'],
|
|
||||||
settings.FEDORA16: ['service', 'libvirtd', 'status'],
|
|
||||||
#whyyyy??
|
|
||||||
settings.UBUNTU11: ['service', 'libvirt-bin', 'status'],
|
|
||||||
}
|
|
||||||
|
|
||||||
#status is either dead or alive!
|
#status is either dead or alive!
|
||||||
_DEAD = 'DEAD'
|
_DEAD = 'DEAD'
|
||||||
@ -60,8 +57,16 @@ def _get_virt_lib():
|
|||||||
|
|
||||||
|
|
||||||
def _status(distro):
|
def _status(distro):
|
||||||
cmd = LIBVIRT_STATUS_CMD[distro]
|
cmds = list()
|
||||||
(sysout, _) = sh.execute(*cmd, run_as_root=False, check_exit_code=False)
|
cmds.append({
|
||||||
|
'cmd': LIBVIRT_STATUS_CMD,
|
||||||
|
})
|
||||||
|
mp = dict()
|
||||||
|
mp['SERVICE'] = SV_NAME_MAP[distro]
|
||||||
|
result = utils.execute_template(*cmds,
|
||||||
|
check_exit_code=False,
|
||||||
|
params=mp)
|
||||||
|
sysout = result[0][0]
|
||||||
if sysout.find("running") != -1:
|
if sysout.find("running") != -1:
|
||||||
return _ALIVE
|
return _ALIVE
|
||||||
else:
|
else:
|
||||||
@ -83,8 +88,15 @@ def _destroy_domain(conn, dom_name):
|
|||||||
|
|
||||||
def restart(distro):
|
def restart(distro):
|
||||||
if _status(distro) != _ALIVE:
|
if _status(distro) != _ALIVE:
|
||||||
cmd = LIBVIRT_RESTART_CMD[distro]
|
cmds = list()
|
||||||
sh.execute(*cmd, run_as_root=True)
|
cmds.append({
|
||||||
|
'cmd': LIBVIRT_RESTART_CMD,
|
||||||
|
'run_as_root': True,
|
||||||
|
})
|
||||||
|
mp = dict()
|
||||||
|
mp['SERVICE'] = SV_NAME_MAP[distro]
|
||||||
|
utils.execute_template(*cmds,
|
||||||
|
params=mp)
|
||||||
|
|
||||||
|
|
||||||
def default(virt_type):
|
def default(virt_type):
|
||||||
@ -111,10 +123,10 @@ def virt_ok(virt_type, distro):
|
|||||||
|
|
||||||
def clear_libvirt_domains(virt_type, inst_prefix):
|
def clear_libvirt_domains(virt_type, inst_prefix):
|
||||||
libvirt = _get_virt_lib()
|
libvirt = _get_virt_lib()
|
||||||
virt_protocol = LIBVIRT_PROTOCOL_MAP.get(virt_type)
|
|
||||||
if not libvirt:
|
if not libvirt:
|
||||||
LOG.warn("Could not clear out libvirt domains, libvirt not installed for python.")
|
LOG.warn("Could not clear out libvirt domains, libvirt not available for python.")
|
||||||
return
|
return
|
||||||
|
virt_protocol = LIBVIRT_PROTOCOL_MAP.get(virt_type)
|
||||||
if not virt_protocol:
|
if not virt_protocol:
|
||||||
LOG.warn("Could not clear out libvirt domains, no valid protocol for virt type %s." % (virt_type))
|
LOG.warn("Could not clear out libvirt domains, no valid protocol for virt type %s." % (virt_type))
|
||||||
return
|
return
|
||||||
|
@ -27,8 +27,6 @@ from devstack import utils
|
|||||||
from devstack.packaging import apt
|
from devstack.packaging import apt
|
||||||
from devstack.packaging import yum
|
from devstack.packaging import yum
|
||||||
|
|
||||||
from devstack.components import keystone
|
|
||||||
|
|
||||||
from devstack.progs import common
|
from devstack.progs import common
|
||||||
|
|
||||||
LOG = logging.getLogger("devstack.progs.actions")
|
LOG = logging.getLogger("devstack.progs.actions")
|
||||||
|
@ -30,6 +30,9 @@ MKPW_CMD = ["openssl", 'rand', '-hex']
|
|||||||
PASS_ASK_ENV = 'PASS_ASK'
|
PASS_ASK_ENV = 'PASS_ASK'
|
||||||
LOG = logging.getLogger("devstack.shell")
|
LOG = logging.getLogger("devstack.shell")
|
||||||
ROOT_USER = "root"
|
ROOT_USER = "root"
|
||||||
|
ROOT_USER_UID = 0
|
||||||
|
SUDO_UID = 'SUDO_UID'
|
||||||
|
SUDO_GID = 'SUDO_GID'
|
||||||
|
|
||||||
|
|
||||||
#root context guard
|
#root context guard
|
||||||
@ -174,10 +177,10 @@ def joinpths(*paths):
|
|||||||
|
|
||||||
|
|
||||||
def _get_suids():
|
def _get_suids():
|
||||||
uid = os.environ.get('SUDO_UID')
|
uid = env.get_key(SUDO_UID)
|
||||||
if uid is not None:
|
if uid is not None:
|
||||||
uid = int(uid)
|
uid = int(uid)
|
||||||
gid = os.environ.get('SUDO_GID')
|
gid = env.get_key(SUDO_GID)
|
||||||
if gid is not None:
|
if gid is not None:
|
||||||
gid = int(gid)
|
gid = int(gid)
|
||||||
return (uid, gid)
|
return (uid, gid)
|
||||||
@ -216,15 +219,17 @@ def chown_r(path, uid, gid, run_as_root=True):
|
|||||||
LOG.debug("Changing ownership of %s to %s:%s" % (joinpths(root, f), uid, gid))
|
LOG.debug("Changing ownership of %s to %s:%s" % (joinpths(root, f), uid, gid))
|
||||||
|
|
||||||
|
|
||||||
def password(prompt_=None, pw_len=8):
|
def password(pw_prompt=None, pw_len=8):
|
||||||
rd = ""
|
pw = ""
|
||||||
ask_for_pw = env.get_bool(PASS_ASK_ENV, True)
|
ask_for_pw = env.get_key(PASS_ASK_ENV)
|
||||||
if ask_for_pw:
|
if ask_for_pw is not None:
|
||||||
rd = prompt_password(prompt_)
|
ask_for_pw = ask_for_pw.lower().strip()
|
||||||
if not rd:
|
if ask_for_pw not in ['f', 'false', '0', 'off']:
|
||||||
|
pw = prompt_password(pw_prompt)
|
||||||
|
if not pw:
|
||||||
return _gen_password(pw_len)
|
return _gen_password(pw_len)
|
||||||
else:
|
else:
|
||||||
return rd
|
return pw
|
||||||
|
|
||||||
|
|
||||||
def mkdirslist(path):
|
def mkdirslist(path):
|
||||||
@ -474,24 +479,31 @@ def copy_replace_file(fsrc, fdst, map_):
|
|||||||
|
|
||||||
|
|
||||||
def got_root():
|
def got_root():
|
||||||
return os.geteuid() == 0
|
return os.geteuid() == ROOT_USER_UID
|
||||||
|
|
||||||
|
|
||||||
def root_mode():
|
def root_mode(quiet=True):
|
||||||
root_uid = getuid(ROOT_USER)
|
root_uid = getuid(ROOT_USER)
|
||||||
root_gid = getgid(ROOT_USER)
|
root_gid = getgid(ROOT_USER)
|
||||||
if root_uid is None or root_gid is None:
|
if root_uid is None or root_gid is None:
|
||||||
LOG.warn("Cannot escalate permissions to (user=%s) - does that user exist??" % (ROOT_USER))
|
msg = "Cannot escalate permissions to (user=%s) - does that user exist??" % (ROOT_USER)
|
||||||
|
if quiet:
|
||||||
|
LOG.warn(msg)
|
||||||
|
else:
|
||||||
|
raise excp.StackException(msg)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
LOG.debug("Escalating permissions to (user=%s, group=%s)" % (root_uid, root_gid))
|
LOG.debug("Escalating permissions to (user=%s, group=%s)" % (root_uid, root_gid))
|
||||||
os.setreuid(0, root_uid)
|
os.setreuid(0, root_uid)
|
||||||
os.setregid(0, root_gid)
|
os.setregid(0, root_gid)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
if quiet:
|
||||||
LOG.warn("Cannot escalate permissions to (user=%s, group=%s)" % (root_uid, root_gid))
|
LOG.warn("Cannot escalate permissions to (user=%s, group=%s)" % (root_uid, root_gid))
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
def user_mode():
|
def user_mode(quiet=True):
|
||||||
(sudo_uid, sudo_gid) = _get_suids()
|
(sudo_uid, sudo_gid) = _get_suids()
|
||||||
if sudo_uid is not None and sudo_gid is not None:
|
if sudo_uid is not None and sudo_gid is not None:
|
||||||
try:
|
try:
|
||||||
@ -499,9 +511,16 @@ def user_mode():
|
|||||||
os.setregid(0, sudo_gid)
|
os.setregid(0, sudo_gid)
|
||||||
os.setreuid(0, sudo_uid)
|
os.setreuid(0, sudo_uid)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
if quiet:
|
||||||
LOG.warn("Cannot drop permissions to (user=%s, group=%s)" % (sudo_uid, sudo_gid))
|
LOG.warn("Cannot drop permissions to (user=%s, group=%s)" % (sudo_uid, sudo_gid))
|
||||||
else:
|
else:
|
||||||
LOG.warn("Can not switch to user mode, no suid user id or group id")
|
raise
|
||||||
|
else:
|
||||||
|
msg = "Can not switch to user mode, no suid user id or group id"
|
||||||
|
if quiet:
|
||||||
|
LOG.warn(msg)
|
||||||
|
else:
|
||||||
|
raise excp.StackException(msg)
|
||||||
|
|
||||||
|
|
||||||
def geteuid():
|
def geteuid():
|
||||||
|
2
stack
2
stack
@ -53,7 +53,7 @@ def main():
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
#drop to usermode
|
#drop to usermode
|
||||||
sh.user_mode()
|
sh.user_mode(False)
|
||||||
try:
|
try:
|
||||||
# now let's go
|
# now let's go
|
||||||
started_ok = actions.run(args)
|
started_ok = actions.run(args)
|
||||||
|
@ -5,6 +5,7 @@ import sys
|
|||||||
#useful for running like the following
|
#useful for running like the following
|
||||||
#find conf/ | grep ".json\$" | xargs python utils/list-pkgs.py "rhel-6"
|
#find conf/ | grep ".json\$" | xargs python utils/list-pkgs.py "rhel-6"
|
||||||
|
|
||||||
|
|
||||||
def clean_file(name):
|
def clean_file(name):
|
||||||
with open(name, "r") as f:
|
with open(name, "r") as f:
|
||||||
contents = f.read()
|
contents = f.read()
|
||||||
@ -101,4 +102,3 @@ if __name__ == "__main__":
|
|||||||
else:
|
else:
|
||||||
version = str(version)
|
version = str(version)
|
||||||
print("[%s] with version [%s]" % (name, version))
|
print("[%s] with version [%s]" % (name, version))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user