Esempio n. 1
0
 def do_ensure_test_images(cname):
     # TODO: Do not duplicate images
     self = facility.get_component(cname)
     self.have_content()
     tempest_git_dir = gitutils.component_git_dir(self)
     image_file = tempest_git_dir + '/etc/cirros.img'
     admin_snippet = util.userrc_script('admin')
     image_uuid = localsh.ret(
         admin_snippet +
         "openstack image create cirros --public --file {image_file} --disk-format qcow2 | awk '/\\| id/{{print $4}}'"
         .format(image_file=image_file))
     image_alt_uuid = localsh.ret(
         admin_snippet +
         "openstack image create cirros_alt --public --file {image_file} --disk-format qcow2 | awk '/\\| id/{{print $4}}'"
         .format(image_file=image_file))
     return (image_uuid.strip(), image_alt_uuid.strip())
Esempio n. 2
0
def queury_non_root():
    non_root = os.environ.get('SUDO_USER', 'root').strip()
    if non_root and non_root != 'root':
        return non_root
    try:
        non_root = localsh.ret('logname').strip()
        if non_root and non_root != 'root':
            return non_root
    except Exception:
        pass
    try:
        localsh.ret("who am i | awk '{print $1}'").strip()
        if non_root and non_root != 'root':
            return non_root
    except Exception:
        pass
Esempio n. 3
0
 def do_mariadb_query_state(cname):
     wsrep_cluster_size = None
     wsrep_cluster_conf_id = None
     r = localsh.ret("mysql <<<\"select * from INFORMATION_SCHEMA.GLOBAL_STATUS where VARIABLE_NAME = 'wsrep_cluster_size' or VARIABLE_NAME = 'wsrep_cluster_conf_id';\"")
     r.split('\n')
     for l in r:
         if l.startswith('WSREP_CLUSTER_SIZE'):
             wsrep_cluster_size = SECOND_THING.match(l)[0]
         if l.startswith('WSREP_CLUSTER_CONF_ID'):
             wsrep_cluster_conf_id = SECOND_THING.match(l)[0]
     return {'wsrep_cluster_size': wsrep_cluster_size, 'wsrep_cluster_conf_id': wsrep_cluster_conf_id}
Esempio n. 4
0
def detect_pkg_mgr():
    global PKG_MGR_STR
    if PKG_MGR_STR is not None:
        return PKG_MGR_STR

    pkg_mgr = localsh.ret("""
if which zypper &>/dev/null; then
   echo zypper
elif [ -e /etc/redhat-release ]; then
   echo dnf
else
   echo apt-get
fi
""")
    return pkg_mgr.strip()
Esempio n. 5
0
 def do_fetch_fernet_as_tar(cname):
     return localsh.ret('tar -c /etc/keystone/fernet-keys', binary=True)
Esempio n. 6
0
 def do_rabbitmq_test_in(cname, candidates):
     r = localsh.ret("rabbitmqctl cluster_status")
     # TODO: parse erlang data
     return [c for c in candidates if ('rabbit@' + c) in r]
Esempio n. 7
0
def _file_md5_sum(for_sum):
    # compare its speed with some built in function
    return localsh.ret("md5sum '{}'".format(for_sum)).split(" ")[0]
Esempio n. 8
0
 def do_network_id(cname):
     net_uuid = localsh.ret(
         util.userrc_script('admin') +
         "openstack network list --external --name public | awk '/ public / {print $2}'"
     )
     return net_uuid.strip()
Esempio n. 9
0
def discover_default_route_src_addr():
    return localsh.ret(r""" PATH=$PATH:/usr/sbin
         ip -4 -o address show $(ip route | awk '/default/ {print $5}' | head -n 1)|
         sed  -r 's|.*inet ([[:digit:]]+[.][[:digit:]]+[.][[:digit:]]+[.][[:digit:]]+)/.*|\1|g' | head -n 1"""
                       ).strip()
Esempio n. 10
0
def user(name, primary_group,
         secondary_groups=[], uid=None, home=None,
         shell='/sbin/nologin', system=True, passwd=None):
    u = None
    try:
        u = pwd.getpwnam(name)
        if uid and u[2] != uid:
            LOG.warning("User '{name}' already exists"
                        " with uid:{real_uid}, not with {wanted_uid}", name=name,
                        real_uid=u[2], wanted_uid=uid)
            uid = u[2]
    except KeyError:
        pass

    if not u and uid:
        try:
            u = pwd.getpwuid(uid)
            if u[2] != uid:
                LOG.warning("User '{name}' already exists"
                            " with {real_uid} uid", name=name,
                            real_uid=u[2])
        except KeyError:
            pass

    if not u:  # new user
        opts = []
        if system:
            opts.append('-r')
        if primary_group:  # not optional
            opts.append("-g '{primary_group}'".format(
                        primary_group=primary_group))
        if secondary_groups:
            opts.append("-G '" + ','.join(secondary_groups) + "'")
        if home:
            opts.append(''.join(("-d '", home, "'")))
        if shell:
            opts.append(''.join(("-s '", shell, "'")))
        if passwd:
            opts.append(''.join(("-p '", passwd_to_hash(passwd), "'")))
        localsh.run("useradd {opts} {name}".format(opts=' '.join(opts),
                                                   name=name))
        return 1
    # needs mod ?
    # TODO: consider non shadow use cases
    opts = []
    if passwd:
        s = spwd.getspnam(name)
        if not check_hash(passwd, s[1]):
            opts.append(''.join(("-p '", passwd_to_hash(passwd), "'")))

    if primary_group and u[3] != _grp_int(primary_group):
        opts.append("-g '{primary_group}'".format(
                    primary_group=primary_group))
    # TODO: cache groups, maintane state(in case we made change chace changes)
    groups = localsh.ret("groups '" + name + "'")
    g_list = groups.split(':')[1].lstrip().split(' ')
    g_wanted = sorted(set(map(_grp_str, secondary_groups + [u[3]])))
    g_current = sorted(set(map(_grp_str, g_list + [u[3]])))
    if g_wanted != g_current:
        opts.append("-G '" + ','.join(secondary_groups) + "'")
    if opts:
        localsh.run("usermod {opts} {name}".format(opts=' '.join(opts),
                                                   name=name))
        return 1
    return 0
Esempio n. 11
0
def image_info(img):
    jdata = localsh.ret("qemu-img info --output=json " + cmd_quote(img))
    return json.loads(jdata)
Esempio n. 12
0
def _file_sha256_sum(for_sum):
    # compare this with some built in function
    return localsh.ret("sha256sum '{}'".format(
                       for_sum)).split(" ")[0]