1 package org.kuali.common.devops.jenkins.upgrade;
2
3 import static com.google.common.base.Preconditions.checkState;
4 import static java.lang.Integer.parseInt;
5 import static java.lang.String.format;
6 import static java.lang.System.currentTimeMillis;
7 import static org.apache.commons.lang3.StringUtils.isNotBlank;
8 import static org.kuali.common.aws.model.AWS.decryptedCopy;
9 import static org.kuali.common.devops.aws.EncryptedAWSCredentials.ENCRYPTED_AWS_CREDENTIALS_FOUNDATION;
10 import static org.kuali.common.devops.jenkins.upgrade.Jenkins.archiveBuildsToS3;
11 import static org.kuali.common.devops.jenkins.upgrade.Jenkins.buildJenkinsSummary;
12 import static org.kuali.common.devops.jenkins.upgrade.Jenkins.persistAndThenRead;
13 import static org.kuali.common.devops.jenkins.upgrade.Jenkins.restore;
14 import static org.kuali.common.devops.jenkins.upgrade.JenkinsCommand.REPORTING;
15 import static org.kuali.common.devops.jenkins.upgrade.JenkinsCommand.RESTORE;
16 import static org.kuali.common.devops.jenkins.upgrade.JenkinsCommand.UPLOAD;
17 import static org.kuali.common.devops.jenkins.upgrade.JenkinsReporting.doReporting;
18 import static org.kuali.common.util.FormatUtils.dateAsString;
19 import static org.kuali.common.util.FormatUtils.getMillis;
20 import static org.kuali.common.util.FormatUtils.getTime;
21 import static org.kuali.common.util.base.Exceptions.illegalArgument;
22 import static org.kuali.common.util.encrypt.Encryption.getDefaultEncryptor;
23 import static org.kuali.common.util.log.Loggers.newLogger;
24
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27
28 import org.junit.Test;
29 import org.kuali.common.aws.s3.DefaultS3Service;
30 import org.kuali.common.aws.s3.S3Service;
31 import org.kuali.common.core.cli.api.CommandLineService;
32 import org.kuali.common.core.cli.plexus.PlexusCLIService;
33 import org.kuali.common.core.json.api.JsonService;
34 import org.kuali.common.core.json.jackson.JacksonJsonService;
35 import org.kuali.common.core.system.VirtualSystem;
36 import org.kuali.common.devops.jenkins.upgrade.model.JenkinsSummary;
37 import org.slf4j.Logger;
38
39 import com.amazonaws.auth.AWSCredentials;
40 import com.google.common.base.Splitter;
41 import com.google.common.base.Stopwatch;
42
43 public class JenkinsScanner {
44
45 private static final Logger logger = newLogger();
46
47 @Test
48 public void test() {
49 try {
50 VirtualSystem vs = VirtualSystem.build();
51 JsonService service = new JacksonJsonService();
52 AWSCredentials creds = decryptedCopy(getDefaultEncryptor(), ENCRYPTED_AWS_CREDENTIALS_FOUNDATION);
53 S3Service s3 = DefaultS3Service.build(creds);
54 JenkinsArchiveContext ctx = getContext(vs);
55 CommandLineService cli = new PlexusCLIService();
56 info(service, ctx);
57 if (ctx.getCommand().equals(RESTORE)) {
58 restore(ctx, s3, service, cli);
59 } else {
60 long now = currentTimeMillis();
61 long startedBefore = now - getMillis(ctx.getAge());
62 info("now -> %s", dateAsString(now, ctx.getTimezone()));
63 info("cutoff -> %s", dateAsString(startedBefore, ctx.getTimezone()));
64 Path home = Paths.get(ctx.getHome());
65 JenkinsSummary jenkins = buildJenkinsSummary(home, ctx.getTimezone(), startedBefore);
66 if (ctx.getCommand().equals(REPORTING)) {
67 doReporting(jenkins);
68 } else if (ctx.getCommand().equals(UPLOAD)) {
69 persistAndThenRead(ctx, jenkins, service, s3, now);
70 archiveBuildsToS3(ctx, jenkins, home, startedBefore, s3);
71 } else {
72 throw illegalArgument("[%s] is an unknown command", ctx.getCommand());
73 }
74 }
75 } catch (Throwable e) {
76 e.printStackTrace();
77 }
78 }
79
80 private static JenkinsArchiveContext getContext(VirtualSystem vs) {
81 String bucket = vs.getProperty("bucket", "archive.kuali.org");
82 String hostname = vs.getEnv("JENKINS_MASTER", "unit.test.kuali.org");
83 String home = vs.getProperty("home", vs.getUser().getHome().getPath() + "/.jenkins");
84 String stack = getStack(vs);
85 String timezone = vs.getProperty("timezone", "US/Eastern");
86 int threads = parseInt(vs.getProperty("threads", "5"));
87 String age = vs.getProperty("age", "12h");
88 AWSCredentials creds = decryptedCopy(getDefaultEncryptor(), ENCRYPTED_AWS_CREDENTIALS_FOUNDATION);
89 JenkinsCommand command = JenkinsCommand.valueOf(vs.getProperty("command", "reporting").toUpperCase());
90 return JenkinsArchiveContext.builder().withStack(stack).withCommand(command).withThreads(threads).withAge(age).withTimezone(timezone).withBucket(bucket)
91 .withCredentials(creds).withHome(home).withHostname(hostname).build();
92 }
93
94 private static String getStack(VirtualSystem vs) {
95 String stack = vs.getEnv("EC2_STACK");
96 if (isNotBlank(stack)) {
97 return stack;
98 } else {
99 return vs.getProperty("stack", "dev");
100 }
101 }
102
103
104
105
106
107
108
109
110 protected static <T> T checkPerfectReadWrite(JsonService json, T reference, Class<T> type) {
111 String expected = json.writeString(reference);
112 String actual = json.writeString(json.readString(expected, type));
113 checkState(expected.equals(actual));
114 return reference;
115 }
116
117 protected static void elapsed(Stopwatch sw) {
118 info("elapsed -> %s", getTime(sw));
119 }
120
121 protected static <T> void info(JsonService json, T reference) {
122 String text = json.writeString(reference);
123 Iterable<String> lines = Splitter.on('\n').split(text);
124 for (String line : lines) {
125 info("%s", line);
126 }
127 }
128
129 protected static void debug(String msg, Object... args) {
130 logger.debug((args == null || args.length == 0) ? msg : format(msg, args));
131 }
132
133 protected static void info(String msg, Object... args) {
134 logger.info((args == null || args.length == 0) ? msg : format(msg, args));
135 }
136
137 }