Exemplo n.º 1
0
def stx_ansible(node, localhost_config):
    LOG.Info("# Config Controller with config file %s" % localhost_config)
    CK_RET(node.copy_to_node(localhost_config, "~/"))

    # Using ansible
    cmd = []
    cmd.append("export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin")
    cmd.append("export HOSTNAME=localhost")
    cmd.append(
        "BOOTSTRAP_YML=`find /usr/share/ansible/stx-ansible/playbooks -name bootstrap.yml`"
    )
    cmd.append("ansible-playbook "
               "$BOOTSTRAP_YML "
               "-e \"ansible_become_pass=%s\"" % node.get_password())

    retval, retlog = node.ssh(
        ";".join(cmd), logfile=getlogfile("controller-0.00_ansible.log"))
    LOG.print_log(retlog)

    if retval == 0:
        for l in retlog:
            if l.find("failed=") >= 0:
                import re
                failed = re.compile(".*failed=(\d*)").match(l).group(1)
                if failed != "0":
                    LOG.Error(
                        "##### ERROR: ansible doesn't return good result.")
                    return -1
    else:
        LOG.Error("##### ERROR: ansible script exited abnormally.")
        return -1

    return 0
Exemplo n.º 2
0
def stx_unlock(node0, node, logfile=None):
    hostname = node
    if isinstance(node, NODE.Node):
        hostname = node.get_hostname()
    cmd = "set -ex; system host-unlock %s" % hostname
    retval, retlog = node0.stx_cmd(cmd, logfile=logfile)
    LOG.print_log(retlog)
    # sleep awhile until the node rebooted
    time.sleep(60 * 5)
    return retval
Exemplo n.º 3
0
def stx_get_mgmt_ip(node0, node):
    retval, retlog = node0.stx_cmd("system host-show %s" % node.get_hostname())
    LOG.print_log(retlog)

    if retval == 0:
        for l in retlog:
            if l.find("mgmt_ip") >= 0:
                mgmt_ip = l.split()[3]
                node.set_mgmt_ip(mgmt_ip)
                return True

    LOG.Error("MGMT ip not found for %s." % node0.get_hostname())
    return False
Exemplo n.º 4
0
def get_ctn_related_status(node, logfile=None, withceph=False):
    cmds = []
    cmds.append("system host-list")
    if withceph == True:
        cmds.append("ceph -s")
    cmds.append("export KUBECONFIG=\"/etc/kubernetes/admin.conf\"")
    cmds.append("kubectl -n kube-system get po")
    cmds.append("sudo docker ps")

    retval, retlog = node.stx_cmd(";".join(cmds), logfile=getlogfile(logfile))
    if retval == 0:
        LOG.print_log(retlog)
    else:
        LOG.print_error(retlog)
Exemplo n.º 5
0
 def remove_from_knownhosts(self):
     oam_ip = self.get_oam_ip()
     floating_ip = self.get_floating_ip()
     cmd = []
     if oam_ip:
         cmd.append(
             'ssh-keygen -f "$HOME/.ssh/known_hosts" -R %s >/dev/null 2>&1'
             % oam_ip)
     if floating_ip:
         cmd.append(
             'ssh-keygen -f "$HOME/.ssh/known_hosts" -R %s >/dev/null 2>&1'
             % floating_ip)
     retval, retlog = CMD.shell(';'.join(cmd), silent=True)
     if retlog:
         LOG.print_log(retlog)
Exemplo n.º 6
0
    def sudo_nopasswd(self):
        ## TODO
        sudo_script = "~/sudo_nopwd.sh"
        cmds = []
        cmds.append("echo \"cat << EOF | sudo tee /etc/sudoers.d/%s\" > %s" %
                    (self.User, sudo_script))
        cmds.append("echo \"%s ALL = (root) NOPASSWD:ALL\" >> %s" %
                    (self.User, sudo_script))
        cmds.append("echo \"EOF\" >> %s" % (sudo_script))
        cmds.append("chmod +x %s" % (sudo_script))
        retval, retlog = self.ssh(";".join(cmds))

        retval, retlog = self.ssh(sudo_script, sudo=True)
        LOG.print_log(retlog)
        if retval == 0:
            self.SudoNoPwd = True
Exemplo n.º 7
0
def create_secure_path(server, user, password):
    cmd = "ssh -t -oStrictHostKeyChecking=no -oCheckHostIP=no "  \
          "%s@%s mkdir -p ~/.ssh" % (user, server)
    retval, retlog = run_expect_cmd_with_password(cmd, password)
    if retval != 0:
        return retval, retlog

    if not os.path.exists(TEST_PUBKEY):
        LOG.Info("pubkey %s not existing, generate new key" % TEST_PUBKEY)
        ret, log = CMD.shell("ssh-keygen -o -f %s -P \"\"" % TEST_KEY)
        LOG.print_log(log)
    add_sshkey_config(server, user)

    retval, retlog = scp_to_server(TEST_PUBKEY, "~/.ssh/authorized_keys",
                                   server, user, password)

    return retval, retlog
Exemplo n.º 8
0
def main():
    import argparse

    parser = argparse.ArgumentParser(description="Node Controller")
    parser.add_argument('vmname', help="VM name", type=str)
    parser.add_argument('node_config', help="Test Node Config.", type=str)

    args = parser.parse_args()
    node = Node_KVM(args.node_config, args.vmname, HOSTNAME_CONTROLLER0, 1,
                    "10.10.10.3")
    ret = node.create_secure_path()
    LOG.Info("== Create Secure Path: ret %s" % ret)
    ret, log = node.ssh("ls")
    LOG.Info("== SSH CMD: ret %s" % ret)
    LOG.print_log(log)
    print(node.get_oam_ip())
    print(node.get_nic(name="eth1001"))
    print(node.get_nic(bridge="virbr3"))
    print(node.is_power_on())
Exemplo n.º 9
0
def __stx_app_op(node0, op, helm_charts=None, appname=None, silent=False):
    retval = 0
    if helm_charts or appname:
        cmd = "set -ex; system %s %s" % (op, helm_charts
                                         if helm_charts else appname)
        retval, retlog = node0.stx_cmd(cmd, silent=silent)
    elif op == APP_OP_LIST:
        retval, retlog = node0.stx_cmd("system %s" % op, silent=silent)
    else:
        LOG.Error(
            "No valid helm charts or app name provided, or unsupported operation."
        )
        return -1, None

    if retval:
        LOG.print_error(retlog)
    else:
        LOG.print_log(retlog)
    return retval, retlog
Exemplo n.º 10
0
def exec_provision_on_host(node,
                           script,
                           logname=None,
                           needsudo=False,
                           args=[]):

    script = getfile(STX_PROVISION_DIR, script)
    cmd = os.path.join("~", script)
    if needsudo:
        cmd = "sudo " + cmd
    if args:
        for a in args:
            # TODO: if space in arguments, need to add "".
            cmd = cmd + " " + str(a)
    if not logname:
        logname = os.path.splitext(os.path.basename(script))[0] + ".log"

    retval, retlog = node.ssh(cmd, logfile=getlogfile(logname), sudo=needsudo)
    LOG.print_log(retlog)
    CK_RET(retval)
Exemplo n.º 11
0
    for k in overwrite:
        if k in json_config and isinstance(json_config[k], dict):
            json_config[k].update(overwrite[k])
        else:
            json_config[k] = overwrite[k]


## DEBUGGING ENTRY ##
if __name__ == '__main__':
    print("### Run cmd on server with password.")
    retval, retlog = secure_ssh("ls",
                                "10.10.10.3",
                                "sysadmin",
                                password="******")
    print("Result Value: %d" % retval)
    LOG.print_log(retlog)

    print("### Create secure path.")
    create_secure_path("10.10.10.3", "sysadmin", password="******")

    print("### Run cmd on server without password.")
    retval, retlog = secure_ssh("ls", "10.10.10.3", "sysadmin")
    print("Result Value: %d" % retval)
    LOG.print_log(retlog)

    print("### Run cmd for unreachable server.")
    retval, retlog = secure_ssh("ls",
                                "10.10.10.5",
                                "sysadmin",
                                password="******",
                                silent=True)