|
| 1 | +package org.zstack.utils.zsha2; |
| 2 | + |
| 3 | +import org.zstack.utils.ShellResult; |
| 4 | +import org.zstack.utils.ShellUtils; |
| 5 | +import org.zstack.utils.Utils; |
| 6 | +import org.zstack.utils.gson.JSONObjectUtil; |
| 7 | +import org.zstack.utils.logging.CLogger; |
| 8 | +import org.zstack.utils.path.PathUtil; |
| 9 | + |
| 10 | + |
| 11 | +/** |
| 12 | + * Created by mingjian.deng on 2020/4/2. |
| 13 | + */ |
| 14 | +public class ZSha2Helper { |
| 15 | + private static final CLogger logger = Utils.getLogger(ZSha2Helper.class); |
| 16 | + |
| 17 | + public static boolean isMNHaEnvironment() { |
| 18 | + return PathUtil.exists("/usr/local/bin/zsha2"); |
| 19 | + } |
| 20 | + |
| 21 | + public static ZSha2Info getInfo() { |
| 22 | + ShellResult result = ShellUtils.runAndReturn("sudo -i /usr/local/bin/zsha2 status", false); |
| 23 | + if (!result.isReturnCode(0)) { |
| 24 | + throw new RuntimeException(String.format("cannot get zsha2 status, because %s", result.getStderr())); |
| 25 | + } |
| 26 | + |
| 27 | + result = ShellUtils.runAndReturn("/usr/local/bin/zsha2 show-config"); |
| 28 | + if (!result.isReturnCode(0)) { |
| 29 | + throw new RuntimeException(String.format("cannot get zsha2 config, because %s, maybe you need upgrade zsha2", result.getStderr())); |
| 30 | + } |
| 31 | + |
| 32 | + ZSha2Info info = JSONObjectUtil.toObject(result.getStdout(), ZSha2Info.class); |
| 33 | + |
| 34 | + info.setMaster(ShellUtils.runAndReturn(String.format( |
| 35 | + "ip addr show %s | grep -q '[^0-9]%s[^0-9]'", info.getNic(), info.getDbvip())).isReturnCode(0)); |
| 36 | + return info; |
| 37 | + } |
| 38 | + |
| 39 | +} |
0 commit comments