aboutsummaryrefslogtreecommitdiffstats
path: root/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/verification/Main.java
blob: a33dd07dff27683d211acb5e7ef9cccf347338d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.verification;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.log.LogLevel;
import com.yahoo.log.LogSetup;
import com.yahoo.vespa.hosted.node.verification.commons.CommandExecutor;
import com.yahoo.vespa.hosted.node.verification.commons.report.HardwareDivergenceReport;
import com.yahoo.vespa.hosted.node.verification.hardware.HardwareBenchmarker;
import com.yahoo.vespa.hosted.node.verification.spec.SpecVerifier;
import io.airlift.command.Cli;
import io.airlift.command.Help;
import io.airlift.command.Option;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author freva
 */
public class Main {
    private static final String EXECUTABLE_NAME = "node-verifier";
    private static final Logger logger = Logger.getLogger(Main.class.getName());
    private static final ObjectMapper om = new ObjectMapper();

    public static void main(String[] args) {
        LogSetup.initVespaLogging(EXECUTABLE_NAME);

        try {
            System.out.println(execute(args, new CommandExecutor()));
        } catch (Exception e) {
            logger.log(LogLevel.ERROR, "Something went wrong", e);
            System.exit(1);
        }
    }

    @SuppressWarnings("unchecked")
    public static String execute(String[] args, CommandExecutor commandExecutor) throws IOException {
        Cli.CliBuilder<Object> builder = Cli.builder(EXECUTABLE_NAME)
                .withDescription("Verifies that node meets the expected specification and benchmarks")
                .withDefaultCommand(Help.class)
                .withCommands(Help.class, SpecVerifier.class, HardwareBenchmarker.class);

        Object command = builder.build().parse(args);
        if (command instanceof VerifierCommand) {
            HardwareDivergenceReport report = ((VerifierCommand) command).getPreviousHardwareDivergence();
            ((VerifierCommand) command).run(report, commandExecutor);
            return hardwareDivergenceReportToString(report);
        } else if (command instanceof Runnable) {
            ((Runnable) command).run();
            return "";
        }

        throw new RuntimeException("Unknown command class " + command.getClass().getName());
    }

    public static abstract class VerifierCommand {
        @Option(name = {"-h", "--divergence"}, description = "JSON of the previous hardware divergence report")
        private String hardwareDivergence;

        private HardwareDivergenceReport getPreviousHardwareDivergence() {
            if (hardwareDivergence == null) {
                return new HardwareDivergenceReport();
            }
            try {
                return om.readValue(hardwareDivergence, HardwareDivergenceReport.class);
            } catch (IOException e) {
                logger.log(Level.WARNING, "Failed to parse hardware divergence:\n" + hardwareDivergence, e.getMessage());
                return new HardwareDivergenceReport();
            }
        }

        protected abstract void run(HardwareDivergenceReport hardwareDivergenceReport, CommandExecutor commandExecutor);
    }

    private static String hardwareDivergenceReportToString(HardwareDivergenceReport hardwareDivergenceReport) throws IOException {
        if (hardwareDivergenceReport.isHardwareDivergenceReportEmpty()) {
            return "null";
        } else {
            return om.writeValueAsString(hardwareDivergenceReport);
        }
    }
}