はじめに
よく利用するコマンドについてAIのお力を借りて調査してみた備忘録です。
hostsshコマンド
CVMからクラスターを構成する全てのAHVに対して同じ操作をするためのコマンドです。パスは以下の通りです。
nutanix@NTNX-HOME-A-CVM:192.168.3.112:~$ which hostssh /usr/local/nutanix/cluster/bin/hostssh
catで確認
Pythonスクリプトでした。
#!/usr/bin/env python # # Copyright (c) 2016 Nutanix Inc. All rights reserved. # # Author: aman.nijhawan@nutanix.com # # # Setup Python 3 environmental variables. import os GENESIS_VIRTUALENV_PATH = os.path.abspath(os.path.join(os.path.dirname( __file__), "../.venv/bin/bin/python3.9")) if os.path.exists(GENESIS_VIRTUALENV_PATH): if os.environ.get("PYTHON_TARGET_VERSION") is None: os.environ["PYTHON_TARGET_VERSION"] = "3.9" if os.environ.get("PYTHON_TARGET_PATH") is None: os.environ["PYTHON_TARGET_PATH"] = GENESIS_VIRTUALENV_PATH # Must be first line import env import gflags import pipes import sys import cluster.client.consts from cluster.client.genesis_utils \ import (get_cached_zeus_configuration, get_ahv_host_user) from util.base import log from util.hypervisor import hypervisor from util.ndb.net.remote_shell import BasicRemoteShell from util.net.ssh_client import SSHClient from util.zeus.utils import get_external_host_ip_from_node_info, \ get_host_backplane_ip_from_ext_ip from zeus.configuration import ConfigurationProto from itertools import chain from google.protobuf.message import DecodeError FLAGS = gflags.FLAGS gflags.DEFINE_string("hypervisor_type", None, "Type of hypervisor on which to execute the command") def get_host_ip_types(): """ Returns a list of hypervisors, type by querying zookeeper cache. """ proto = get_cached_zeus_configuration() if not proto: log.FATAL("Error: Zeus configuration cache does not exist") hvs = [] for node in chain(proto.node_list, proto.compute_node_list): hvs.append(get_external_host_ip_from_node_info(node)) hvs = set(hvs) ret = [] for x in proto.management_server_list: if x.management_server_name in hvs: backplane_ip = get_host_backplane_ip_from_ext_ip( x.management_server_name, proto) ret.append((backplane_ip, x.management_server_type)) return ret def log_error_from_remote_cmd(ret, out, err, cmd, host_ip): log.ERROR("Got Non Zero Return code %d while executing cmd %s on host %s" % (ret, cmd, host_ip)) log.ERROR("stderr: %s" % err) log.ERROR("stdout: %s" % out) def run_remote_cmd_remote_shell(host_ip, cmd): with BasicRemoteShell(host_ip) as shell: ret, out, err = shell.execute(cmd, timeout_secs=30) if ret != 0: log_error_from_remote_cmd(ret, out, err, cmd, host_ip) else: sys.stdout.write(out) def run_remote_cmd_ssh_client(host_ip, cmd, user=None): user = user or FLAGS.hypervisor_username client = SSHClient(host_ip, user) ret, out, err = client.execute(cmd) if ret != 0: log_error_from_remote_cmd(ret, out, err, cmd, host_ip) else: sys.stdout.write(out.decode("utf-8", "ignore")) def run_remote_cmd(hypervisor_type, host_ip, cmd): log.INFO("Executing: \%s on host: %s with type: %s" % (cmd, host_ip, hypervisor.Hypervisor.get_hypervisor_name(hypervisor_type))) sys.stdout.write("============= %s ============\n" % host_ip) if hypervisor_type == ConfigurationProto.ManagementServer.kHyperv: run_remote_cmd_remote_shell(host_ip, cmd) elif hypervisor_type == ConfigurationProto.ManagementServer.kKvm: user = get_ahv_host_user() run_remote_cmd_ssh_client(host_ip, cmd, user) else: run_remote_cmd_ssh_client(host_ip, cmd) def is_mixed_hv_cluster(hypervisor_types): if len(set(hypervisor_types)) > 1: return True else: return False def get_cmd_from_args(args, hypervisor_type): if hypervisor_type == ConfigurationProto.ManagementServer.kHyperv: return ' '.join(args) return pipes.quote(' '.join(args)) def main(args): host_ip_types = get_host_ip_types() hypervisor_types = [x[1] for x in host_ip_types] if is_mixed_hv_cluster(hypervisor_types): if FLAGS.hypervisor_type is None: log.INFO("Detected multi hypervisor environment, " "Please specify --hypervisor_type") hosts_of_interest = host_ip_types else: hosts_of_interest = [x for x in host_ip_types if x[1] == hypervisor.Hypervisor.get_hypervisor_type_from_name( FLAGS.hypervisor_type)] else: hosts_of_interest = host_ip_types if not hosts_of_interest: log.FATAL("No valid hosts found") for host in hosts_of_interest: cmd = get_cmd_from_args(args, host[1]) if cmd.strip(): log.INFO("Executing cmd %s on host %s" % (cmd, host)) run_remote_cmd(host[1], host[0], cmd) else: log.WARNING("No command specified, not executing") if __name__ == "__main__": if ((("-h" in sys.argv or "--help" in sys.argv) and len(sys.argv) == 2) or len(sys.argv) == 1): sys.stdout.write( "Usage: hostssh [--hypervisor_type <kvm|esx|hyperv>] <cmd>\n") sys.exit(0) args = FLAGS(sys.argv) FLAGS.logtostderr = True FLAGS.use_sys_exit_on_fatal = True main(args[1:])
Copilotに聞いてみた
あくまでも参考程度でお願いします。
コメント