<commit_before>package com.openshift.openshiftjenkinsbuildutils;
import hudson.Launcher;
import hudson.Extension;
import hudson.util.FormValidation;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.AbstractProject;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.QueryParameter;

import com.openshift.restclient.ClientFactory;
import com.openshift.restclient.IClient;
import com.openshift.restclient.ISSLCertificateCallback;
import com.openshift.restclient.ResourceKind;
import com.openshift.restclient.authorization.TokenAuthorizationStrategy;
import com.openshift.restclient.capability.ICapability;
import com.openshift.restclient.model.IReplicationController;

import javax.net.ssl.SSLSession;
import javax.servlet.ServletException;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.cert.X509Certificate;
import java.util.Map;

/**
 * OpenShift {@link Builder}.
 *
 * <p>
 * When the user configures the project and enables this builder,
 * {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked
 * and a new {@link OpenShiftScaler} is created. The created
 * instance is persisted to the project configuration XML by using
 * XStream, so this allows you to use instance fields (like {@link #name})
 * to remember the configuration.
 *
 * <p>
 * When a build is performed, the {@link #perform(AbstractBuild, Launcher, BuildListener)}
 * method will be invoked. 
 *
 * @author <NAME>
 */
public class OpenShiftScaler extends Builder implements ISSLCertificateCallback {

    private String apiURL = "https://openshift.default.svc.cluster.local";
    private String depCfg = "frontend";
    private String nameSpace = "test";
    private String replicaCount = "0";
    private String authToken = "";
    
    
    // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
    @DataBoundConstructor
    public OpenShiftScaler(String apiURL, String depCfg, String nameSpace, String replicaCount, String authToken) {
        this.apiURL = apiURL;
        this.depCfg = depCfg;
        this.nameSpace = nameSpace;
        this.replicaCount = replicaCount;
        this.authToken = authToken;
    }

    /**
     * We'll use this from the <tt>config.jelly</tt>.
     */
    public String getApiURL() {
		return apiURL;
	}

	public String getDepCfg() {
		return depCfg;
	}

	public String getNameSpace() {
		return nameSpace;
	}
	
	public String getReplicaCount() {
		return replicaCount;
	}
	
	public String getAuthToken() {
		return authToken;
	}

    @Override
    public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
    	System.setProperty(ICapability.OPENSHIFT_BINARY_LOCATION, Constants.OC_LOCATION);
    	listener.getLogger().println("OpenShiftScaler in perform");
    	
    	// obtain auth token from defined spot in OpenShift Jenkins image
    	authToken = Auth.deriveAuth(authToken, listener);
    	    	
    	// get oc client (sometime REST, sometimes Exec of oc command
    	IClient client = new ClientFactory().create(apiURL, this);
    	
    	if (client != null) {
    		// seed the auth
        	client.setAuthorizationStrategy(new TokenAuthorizationStrategy(this.authToken));
        	
        	// get ReplicationController ref
        	Map<String, IReplicationController> rcs = Deployment.getDeployments(client, nameSpace, listener);
        	
        	String depId = null;
        	IReplicationController rc = null;
        	// find corresponding rep ctrl and scale it to the right value
        	for (String key : rcs.keySet()) {
        		rc = rcs.get(key);
        		if (key.startsWith(depCfg)) {
        			depId = key;
        			listener.getLogger().println("OpenShiftScaler key into oc scale is " + depId);
            		listener.getLogger().println("OpenShiftScaler rep ctrl " + rc);
        			
					//TODO assume there is only 1 dep cfg per build
					break;
        		}
        	}
        	
        	if (depId == null) {
        		listener.getLogger().println("OpenShiftSclaer did not find any replication controllers for " + depCfg);
        		//TODO if not found, and we are scaling down to zero, don't consider an error - this may be safety
        		// measure to scale down if exits ... perhaps we make this behavior configurable over time, but for now.
        		// we refrain from adding yet 1 more config option
        		if (replicaCount.equals("0"))
        			return true;
        		else
        			return false;
        	}
        	
        	// do the oc scale ... may need to retry
        	long currTime = System.currentTimeMillis();
        	boolean scaleDone = false;
        	while (System.currentTimeMillis() < (currTime + 60000)) {
    			BinaryScaleInvocation runner = new BinaryScaleInvocation(replicaCount, depId, nameSpace, client);
    			InputStream logs = null;
				// create stream and copy bytes
				try {
					logs = new BufferedInputStream(runner.getLogs(true));<commit_msg>allow for retry on locating rc when scaling up; fix typo in trace<commit_after>package com.openshift.openshiftjenkinsbuildutils;
import hudson.Launcher;
import hudson.Extension;
import hudson.util.FormValidation;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.AbstractProject;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.QueryParameter;

import com.openshift.restclient.ClientFactory;
import com.openshift.restclient.IClient;
import com.openshift.restclient.ISSLCertificateCallback;
import com.openshift.restclient.ResourceKind;
import com.openshift.restclient.authorization.TokenAuthorizationStrategy;
import com.openshift.restclient.capability.ICapability;
import com.openshift.restclient.model.IReplicationController;

import javax.net.ssl.SSLSession;
import javax.servlet.ServletException;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.cert.X509Certificate;
import java.util.Map;

/**
 * OpenShift {@link Builder}.
 *
 * <p>
 * When the user configures the project and enables this builder,
 * {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked
 * and a new {@link OpenShiftScaler} is created. The created
 * instance is persisted to the project configuration XML by using
 * XStream, so this allows you to use instance fields (like {@link #name})
 * to remember the configuration.
 *
 * <p>
 * When a build is performed, the {@link #perform(AbstractBuild, Launcher, BuildListener)}
 * method will be invoked. 
 *
 * @author <NAME>
 */
public class OpenShiftScaler extends Builder implements ISSLCertificateCallback {

    private String apiURL = "https://openshift.default.svc.cluster.local";
    private String depCfg = "frontend";
    private String nameSpace = "test";
    private String replicaCount = "0";
    private String authToken = "";
    
    
    // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
    @DataBoundConstructor
    public OpenShiftScaler(String apiURL, String depCfg, String nameSpace, String replicaCount, String authToken) {
        this.apiURL = apiURL;
        this.depCfg = depCfg;
        this.nameSpace = nameSpace;
        this.replicaCount = replicaCount;
        this.authToken = authToken;
    }

    /**
     * We'll use this from the <tt>config.jelly</tt>.
     */
    public String getApiURL() {
		return apiURL;
	}

	public String getDepCfg() {
		return depCfg;
	}

	public String getNameSpace() {
		return nameSpace;
	}
	
	public String getReplicaCount() {
		return replicaCount;
	}
	
	public String getAuthToken() {
		return authToken;
	}

    @Override
    public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
    	System.setProperty(ICapability.OPENSHIFT_BINARY_LOCATION, Constants.OC_LOCATION);
    	listener.getLogger().println("OpenShiftScaler in perform");
    	
    	// obtain auth token from defined spot in OpenShift Jenkins image
    	authToken = Auth.deriveAuth(authToken, listener);
    	    	
    	// get oc client (sometime REST, sometimes Exec of oc command
    	IClient client = new ClientFactory().create(apiURL, this);
    	
    	if (client != null) {
    		// seed the auth
        	client.setAuthorizationStrategy(new TokenAuthorizationStrategy(this.authToken));
        	
        	String depId = null;
        	IReplicationController rc = null;
        	// find corresponding rep ctrl and scale it to the right value
        	long currTime = System.currentTimeMillis();
        	// in testing with the jenkins-ci sample, the initial deploy after
        	// a build is kinda slow ... gotta wait more than one minute
        	while (System.currentTimeMillis() < (currTime + 180000)) {
            	// get ReplicationController ref
            	Map<String, IReplicationController> rcs = Deployment.getDeployments(client, nameSpace, listener);
            	
            	for (String key : rcs.keySet()) {
            		rc = rcs.get(key);
            		if (key.startsWith(depCfg)) {
            			depId = key;
            			listener.getLogger().println("OpenShiftScaler key into oc scale is " + depId);
                		listener.getLogger().println("OpenShiftScaler rep ctrl " + rc);
            			
    					//TODO assume there is only 1 dep cfg per build
    					break;
            		}
            	}
            	
            	// if they want to scale down to 0 and there is not rc to begin with, just punt / consider a no-op
            	if (depId != null || replicaCount.equals("0"))
            		break;
            	else {
					listener.getLogger().println("OpenShiftScaler wait 10 seconds, then look for rep ctrl again");
					try {
						Thread.sleep(10000);
					} catch (InterruptedException e) {
					}
            	}
        	}
        	
        	if (depId == null) {
        		listener.getLogger().println("OpenShiftScaler did not find any replication controllers for " + depCfg);
        		//TODO if not found, and we are scaling down to zero, don't consider an error - this may be safety
        		// measure to scale down if exits ... perhaps we make this behavior configurable over time, but for now.
        		// we refrain from adding yet 1 more config option
        		if (replicaCount.equals("0"))
        			return true;
        		else
        			return false;
        	}
        	
        	// do the oc scale ... may need to retry
        	currTime = System.currentTimeMillis();
        	boolean scaleDone = false;
        	while (System.currentTimeMillis() < (currTime + 60000)) {
    			BinaryScaleInvocation runner = new BinaryScaleInvocation(replicaCount, depId, nameSpace, client);
    			InputStream logs = null;
				// create stream and copy bytes
				try {
					logs = new BufferedInputStream(runner.getLogs(true));
{'fraction_non_alphanumeric': 0.08713953071825184, 'fraction_numerical': 0.003492745835572273, 'mean_word_length': 2.961333806314296, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4232689', 'n_tokens_mistral': 3331, 'n_tokens_neox': 3144, 'n_words': 927}