All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.jenkins.results.parser.JenkinsSlave Maven / Gradle / Ivy

The newest version!
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.jenkins.results.parser;

import java.io.IOException;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @author Michael Hashimoto
 */
public class JenkinsSlave implements JenkinsNode {

	public JenkinsSlave() {
		this(
			JenkinsResultsParserUtil.getHostName(
				JenkinsResultsParserUtil.getHostIPAddress()));
	}

	public JenkinsSlave(String hostname) {
		hostname = hostname.replaceAll("([^\\.]+).*", "$1");

		String jenkinsMasterName =
			JenkinsResultsParserUtil.getJenkinsMasterName(hostname);

		if (jenkinsMasterName == null) {
			throw new RuntimeException(
				JenkinsResultsParserUtil.combine(
					"Unable to find Jenkins master name for Jenkins slave ",
					"hostname ", hostname));
		}

		_jenkinsMaster = JenkinsMaster.getInstance(jenkinsMasterName);

		if (hostname.equals(jenkinsMasterName)) {
			_name = "master";
		}
		else {
			_name = hostname;
		}

		JSONObject jenkinsSlaveJSONObject = JenkinsAPIUtil.getAPIJSONObject(
			getComputerURL(), "displayName,idle,offline,offlineCauseReason");

		update(jenkinsSlaveJSONObject);
	}

	@Override
	public int compareTo(JenkinsSlave jenkinsSlave) {
		return _name.compareTo(jenkinsSlave.getName());
	}

	@Override
	public boolean equals(Object object) {
		if (object instanceof JenkinsSlave) {
			JenkinsSlave jenkinsSlave = (JenkinsSlave)object;

			if (compareTo(jenkinsSlave) == 0) {
				return true;
			}

			return false;
		}

		return super.equals(object);
	}

	public String getComputerURL() {
		String name = getName();

		if (name.equals("master")) {
			name = "(" + name + ")";
		}

		return JenkinsResultsParserUtil.combine(
			_jenkinsMaster.getURL(), "/computer/", name);
	}

	public Build getCurrentBuild() {
		JSONObject jsonObject = JenkinsAPIUtil.getAPIJSONObject(
			getComputerURL(), "executors[currentExecutable[url]]");

		JSONArray jsonArray = jsonObject.getJSONArray("executors");

		jsonObject = jsonArray.getJSONObject(0);

		JSONObject currentExecutableJSONObject = jsonObject.getJSONObject(
			"currentExecutable");

		String buildURL = currentExecutableJSONObject.optString("url");

		if (buildURL == null) {
			return null;
		}

		return BuildFactory.newBuild(buildURL, null);
	}

	@Override
	public JenkinsCohort getJenkinsCohort() {
		return _jenkinsMaster.getJenkinsCohort();
	}

	@Override
	public JenkinsMaster getJenkinsMaster() {
		return _jenkinsMaster;
	}

	@Override
	public String getName() {
		return _name;
	}

	public String getNamePrefix() {
		Matcher matcher = _namePattern.matcher(getName());

		if (!matcher.matches()) {
			return null;
		}

		return matcher.group("prefix");
	}

	public Integer getNumber() {
		Matcher matcher = _namePattern.matcher(getName());

		if (!matcher.matches()) {
			return null;
		}

		return Integer.parseInt(matcher.group("number"));
	}

	public String getOfflineCauseReason() {
		return _offlineCauseReason;
	}

	public Set getSiblings() {
		JenkinsMaster jenkinsMaster = getJenkinsMaster();

		if (jenkinsMaster.getSlavesPerHost() < 2) {
			return Collections.emptySet();
		}

		Integer slaveNumber = getNumber();

		if (slaveNumber == null) {
			return Collections.emptySet();
		}

		Set siblings = new HashSet<>();

		int siblingSlaveNumber = slaveNumber + 1;

		if ((slaveNumber % 2) == 0) {
			siblingSlaveNumber = slaveNumber - 1;
		}

		siblings.add(
			jenkinsMaster.getJenkinsSlave(
				getNamePrefix() + siblingSlaveNumber));

		return siblings;
	}

	@Override
	public int hashCode() {
		String hashCodeString = _jenkinsMaster.getName() + "_" + _name;

		return hashCodeString.hashCode();
	}

	public boolean isIdle() {
		return _idle;
	}

	public boolean isOffline() {
		return _offline;
	}

	public boolean isReachable() {
		return JenkinsResultsParserUtil.isServerPortReachable(getName(), 22);
	}

	public void takeSlavesOffline(String offlineReason) {
		_setSlaveStatus(offlineReason, true);
	}

	public void takeSlavesOnline(String offlineReason) {
		_setSlaveStatus(offlineReason, false);
	}

	@Override
	public String toString() {
		return getName();
	}

	public void update() {
		_jenkinsMaster.update();
	}

	protected JenkinsSlave(
		JenkinsMaster jenkinsMaster, JSONObject jenkinsSlaveJSONObject) {

		_jenkinsMaster = jenkinsMaster;

		_name = jenkinsSlaveJSONObject.getString("displayName");

		update(jenkinsSlaveJSONObject);
	}

	protected void update(JSONObject jenkinsSlaveJSONObject) {
		_idle = jenkinsSlaveJSONObject.getBoolean("idle");
		_offline = jenkinsSlaveJSONObject.getBoolean("offline");
		_offlineCauseReason = jenkinsSlaveJSONObject.optString(
			"offlineCauseReason");
	}

	private void _setSlaveStatus(String offlineReason, boolean offlineStatus) {
		try {
			Class clazz = JenkinsSlave.class;

			String script = JenkinsResultsParserUtil.readInputStream(
				clazz.getResourceAsStream(
					"dependencies/set-slave-status.groovy"));

			script = script.replace("${slaves}", _name);
			script = script.replace(
				"${offline.reason}",
				offlineReason.replaceAll("\n", "
\\\\n")); script = script.replace( "${offline.status}", String.valueOf(offlineStatus)); System.out.println( JenkinsResultsParserUtil.executeJenkinsScript( _jenkinsMaster.getName(), script)); } catch (IOException ioException) { System.out.println("Unable to set the status for slaves: " + _name); ioException.printStackTrace(); } } private static final Pattern _namePattern = Pattern.compile( "(?.*[^\\d]+)(?\\d+)"); private boolean _idle; private final JenkinsMaster _jenkinsMaster; private final String _name; private boolean _offline; private String _offlineCauseReason; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy