ch.sbb.releasetrain.webui.DirectorRunnerGui.java Source code

Java tutorial

Introduction

Here is the source code for ch.sbb.releasetrain.webui.DirectorRunnerGui.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements;
 * and to You under the Apache License, Version 2.0.
 */
package ch.sbb.releasetrain.webui;

import ch.sbb.releasetrain.action.jenkins.JenkinsJobThread;
import ch.sbb.releasetrain.config.model.releaseconfig.JenkinsActionConfig;
import ch.sbb.releasetrain.director.Director;
import ch.sbb.releasetrain.git.GITAccessor;
import ch.sbb.releasetrain.utils.http.HttpUtilImpl;
import ch.sbb.releasetrain.utils.mavenserach.LatetVersionFromMavenserach;
import ch.sbb.releasetrain.webui.backingbeans.DefaultPersistence;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Mighty Director - dictator
 *
 * @author u203244 (Daniel Marthaler)
 * @since 0.0.1, 2016
 */
@Component
@Slf4j
@Data
public class DirectorRunnerGui {

    private Boolean on = Boolean.FALSE;

    @Autowired
    private DefaultPersistence pers;

    @Autowired
    private HttpUtilImpl util;

    @Autowired
    private Director director;

    @Autowired
    private GITAccessor git;

    @Autowired
    private LatetVersionFromMavenserach version;

    public void init() {
        this.disableJob();
        JenkinsActionConfig conf = (JenkinsActionConfig) pers.getNewForName("jenkinsAction");
        templateJob = conf.getJenkinsJobname();
        newJob = conf.getJenkinsJobname() + ".new";
    }

    @PreDestroy
    private void startup() {
        this.enableJob();
    }

    private String templateJob = "";

    private String templateText;

    private String newJob = "";

    private String releasetrainVersion = version.getLatestVersionForGroupId("ch.sbb.releasetrain");
    private String spec = "H/5 * * * *";
    private String jdk = "Oracle JDK 1.8 64-Bit";

    private String concurrentBuild = "false";
    private String description = "<h1>generated by release train</h1>";

    private String mavenName = "Apache Maven 3.3";

    public String getButton() {

        if (!on) {
            return "Start local";
        } else {
            return "Stop local";
        }
    }

    public void go() {
        director.setShutdown(false);
        on = !on;
    }

    @Scheduled(fixedRate = 10 * 1000)
    public void run() {
        if (!on) {
            log.info("not waking up the director ...");
            return;
        }
        log.info("wake up the director...");
        director.direct();
        log.info("director is going to sleep a while ...");
    }

    public Boolean isTemplateAvailable() {
        JenkinsActionConfig conf = (JenkinsActionConfig) pers.getNewForName("jenkinsAction");
        util.setUser(conf.getJenkinsUser());
        util.setPassword(conf.getEncPassword());
        JenkinsJobThread th = new JenkinsJobThread(this.templateJob, "Cause", conf.getJenkinsUrl(), "build", util,
                null);
        return th.isJobPresent();
    }

    public Boolean isJobAvailable() {
        JenkinsActionConfig conf = (JenkinsActionConfig) pers.getNewForName("jenkinsAction");
        util.setUser(conf.getJenkinsUser());
        util.setPassword(conf.getEncPassword());
        JenkinsJobThread th = new JenkinsJobThread(this.newJob, "Cause", conf.getJenkinsUrl(), "build", util, null);
        return th.isJobPresent();
    }

    public void loadTemplate() {
        JenkinsActionConfig conf = (JenkinsActionConfig) pers.getNewForName("jenkinsAction");
        util.setUser(conf.getJenkinsUser());
        util.setPassword(conf.getEncPassword());
        JenkinsJobThread th = new JenkinsJobThread(this.templateJob, "Cause", conf.getJenkinsUrl(), "build", util,
                null);
        this.templateText = th.readConfig();
    }

    public void createJobOnJenkins() {

        String tempText = templateText;
        StringBuilder builder = new StringBuilder();
        builder.append("ch.sbb.releasetrain:mavenmojos:" + this.releasetrainVersion + ":releasetrain\n");
        builder.append("-Dconfig.url=" + git.getModel().getConfigUrl() + "\n");
        builder.append("-Dconfig.branch=" + git.getModel().getConfigBranch() + "\n");
        builder.append("-Dconfig.user=" + git.getModel().getConfigUser() + "\n");
        builder.append("-Dconfig.password=" + git.getModel().getConfigPassword() + "\n");
        builder.append("-Djava.io.tmpdir=\\${WORKSPACE}");

        tempText = replaceNode(tempText, "targets", builder.toString());
        tempText = replaceNode(tempText, "mavenName", mavenName);
        tempText = replaceNode(tempText, "spec", spec);
        tempText = replaceNode(tempText, "jdk", jdk);
        tempText = replaceNode(tempText, "description", description);

        JenkinsActionConfig conf = (JenkinsActionConfig) pers.getNewForName("jenkinsAction");
        util.setUser(conf.getJenkinsUser());
        util.setPassword(conf.getEncPassword());

        JenkinsJobThread th = new JenkinsJobThread(this.newJob, "Cause", conf.getJenkinsUrl(), "build", util, null);

        if (this.isJobAvailable()) {
            th.writeConfig(tempText, this.newJob);
        } else {
            th.writeNewConfig(tempText, this.newJob);
        }
        th.enable(this.newJob);
    }

    private void disableJob() {
        JenkinsActionConfig conf = (JenkinsActionConfig) pers.getNewForName("jenkinsAction");
        util.setUser(conf.getJenkinsUser());
        util.setPassword(conf.getEncPassword());
        JenkinsJobThread th = new JenkinsJobThread(this.newJob, "Cause", conf.getJenkinsUrl(), "build", util, null);
        th.disable(this.newJob);

    }

    private void enableJob() {
        JenkinsActionConfig conf = (JenkinsActionConfig) pers.getNewForName("jenkinsAction");
        util.setUser(conf.getJenkinsUser());
        util.setPassword(conf.getEncPassword());
        JenkinsJobThread th = new JenkinsJobThread(this.newJob, "Cause", conf.getJenkinsUrl(), "build", util, null);
        th.enable(this.newJob);
    }

    private String replaceNode(String in, String node, String value) {
        return in.replaceFirst("<" + node + ">(.*)</" + node + ">", "<" + node + ">" + value + "</" + node + ">");
    }

}