weizhouapache commented on code in PR #14320:
URL: https://github.com/apache/cloudstack/pull/14320#discussion_r4272369273


##########
scripts/vm/hypervisor/kvm/kvmheartbeat_rbd.py:
##########
@@ -0,0 +1,131 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Ceph RBD flavor of kvmheartbeat.sh/kvmsmpheartbeat.sh.
+#
+# There is no shared POSIX mount point to write a heartbeat file to when the
+# primary storage pool is Ceph RBD, so the heartbeat timestamp is instead
+# stored as a small RADOS object (one object per host) in the same RBD pool.
+# Any host with a working path to the Ceph cluster can write/read this object,
+# which gives the same semantics as the NFS/SharedMountPoint heartbeat file.
+#
+# The object is written and read with the Python bindings of librados
+# (python3-rados), see kvmha_rbd.py. The cephx key is not an argument, as it 
would be visible in the process list: it is
+# read from the environment variable CEPH_KEY.
+
+import argparse
+import os
+import re
+import subprocess
+import sys
+import time
+
+from kvmha_rbd import BindingsError, Ceph, CephError, KEY_ENV, 
silence_standard_error
+
+
+class Parser(argparse.ArgumentParser):
+    def error(self, message):
+        self.print_usage(sys.stderr)
+        sys.exit(1)
+
+
+def main():
+    parser = Parser(description="Writes or checks the heartbeat of a KVM host 
on Ceph RBD storage", add_help=False)
+    parser.add_argument("-s", dest="mon_hosts", default="", help="ceph monitor 
host(s), comma separated")
+    parser.add_argument("-o", dest="pool", default="", help="ceph/rbd pool 
name")
+    parser.add_argument("-n", dest="user", default="", help="cephx auth user 
(optional, the key is then in " + KEY_ENV + ")")
+    parser.add_argument("-h", dest="host", default="", help="host")
+    parser.add_argument("-r", dest="check", action="store_true", help="read 
the heartbeat, instead of writing it")
+    parser.add_argument("-c", dest="cleanup", action="store_true", 
help="cleanup: reboot the host")
+    parser.add_argument("-t", dest="interval", default="", help="maximum age 
of the heartbeat in seconds, when it is read")
+    parser.add_argument("--help", action="help")
+    args = parser.parse_args()
+
+    key = os.environ.pop(KEY_ENV, "")
+    if not args.mon_hosts or not args.pool:
+        sys.exit(1)
+    # the host IP names the heartbeat object, so it is required except for the 
self-fencing (-c)
+    if not args.cleanup and not args.host:
+        sys.exit(1)
+    if args.user and not key:
+        sys.exit(1)
+    if args.check and not re.fullmatch(r"[0-9]+", args.interval):
+        sys.exit(1)
+
+    if args.cleanup:
+        reboot()
+        return
+
+    heartbeat_object = "KVMHA-hb-" + args.host
+    silence_standard_error()
+    try:
+        ceph = Ceph(args.mon_hosts, args.pool, args.user, key)
+    except BindingsError as error:
+        if args.check:
+            # not a proof that the host is dead, so DEAD is not reported
+            print("=====> Unable to check the heartbeat, %s <======" % error)
+            sys.exit(2)
+        sys.exit(1)
+    try:
+        if args.check:
+            check(ceph, heartbeat_object, args.pool, int(args.interval))
+            return
+        try:
+            ceph.write(heartbeat_object, "%d\n" % time.time())
+        except CephError:
+            sys.exit(1)
+    finally:
+        ceph.close()
+
+
+def check(ceph, heartbeat_object, pool, interval):
+    now = int(time.time())
+    try:
+        heartbeat = ceph.read(heartbeat_object).strip()
+    except CephError:

Review Comment:
   updated.
   now exit 2 if fail to connect to ceph



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to