Jenkins plugin Hyper Slaves Source analysis

Source: Internet
Author: User
Tags docker run

1. public class Hyperslaves extends Plugin implements Describable

(1), Init (): Initializes the containerdriverfactory, where Containerdriverfactory is an abstract class containerdriverfactory variable, and we can call its forjob The (Job context) method obtains an abstract class of type Containerdriver.

(2), createstandardjobprovisionerfactory (Job): Where Call return new Hyperprovisionerfactory.standardjob (...) To return an abstract class of hyperprovisionerfactory

2, public abstract class Hyperprovisionerfactory

Function: Prepare workspace and return a Hyperprovisioner instance

(1), which contains an abstract method public abstract Hyperprovisioner Createprovisioner (Tasklistener slavelistener), The inner class Standardjob implements this abstract class, where the Createprovisioner function first creates an instance of the Jobbuildscontainerscontext class context, and then calls Prepareworkspace ( Job, context), and finally call return new Hyperprovisioner (context, Slavelistener, driver, job, spec)

3, public abstract class Oneshotslave extends Slave implements Ephemeralnode

function: A slave that's designed to being used only once, for A specific hudson.model.run,and as such have A life cycle to fully MA Tch the Run ' s one

Provisioning such a slave should be a lightweight process, so one can provision them at any time and concurrently to match Hudson.model.Queue load.

Typically usage is Docker container based Jenkins agents

Actual launch of the Slave is postponed until a Run are created, so we can have a 1:1 match between Run and Executor Lifecy Cle

Dump the launch log in build log

Mark the build as Not_build on launch failed

Shut down and remove the Executor on build completion

Variable: private transient queue.executable executable

Private final Computerlauncher Reallauncher;

Private Boolean provisioningfailed = false;

(1), Public Launcher createlauncher (Tasklistener listener): This function first calls provision (listener), and then calls return Super.createlauncher (listener)

Assign a queue.executable to this oneshotslave.by design, only one queue.executable can be assigned, then slave is shut Down.

This method has the called just as the Run as been created. It run the actual launch of the executor

and use Run ' s hudson.model.BuildListener as computer Launcher listener to collect the startup log as part of the build

Delaying launch of the executor until the Run is actually started allows to fail the build on launch failure

So we had a strong 1:1 relation between a Run and its Executor

(2), first call to create Executor class: Executor = Executor.currentexecutor (), then call Reallauncher.launch (This.getcomputer (), listener) Start a Slave container

If you later call Getcomputer (). Isactuallyoffline () is true, then call provisionfailed (new IllegalStateException ("Computer is offline After launch "))

Finally, if no exception occurs, call executable = Executor.getcurrentexecutable ()

  

3. public class Hyperslave extends Oneshotslave

Ephemeralnode use Hyper container to run build Process,slave focus only on a specific job, preferably on a specific build, but when this class was just created, because of the life cycle of Jenkins, Build does not yet exist.

Private Final Hyperprovisionerfactory Provisionerfactory

(1), constructor: Public hyperslave (string name, String nodedescription, String labelstring, Hyperprovisionerfactory Provisionerfactory):

First Call Super (Name.replaceall ("/", ">>"), Nodedescription, Slave_root, labelstring, new Hypercomputerlauncher () ) initializes the parent class

(2), Public Hypercomputer Createcomputer (): The function simply calls the new Hypercomputer (this, provisionerfactory)

Create a custom Launcher which relies on "Docker run" to start a new process

(3), public Launcher createlauncher (Tasklistener Listener): This method first calls C = Getcomputer () to get the Hypercomputer class, Call Super.createlauncher again (listener), and finally call launcher = new Hyperlauncher (Listener, C.getchannel (), C.isunix (), C.getprovisioner ()). Decoratefor (this), generates a launcher and returns.

4. public class Hypercomputer extends Oneshotcomputer

Private final hyperslave slave;

Private final hyperprovisionerfactory provisionerfactory;

Private Hyperprovisioner

(1), the constructor is: public Hypercomputer (Hyperslave slave, hyperprovisionerfactory provisionerfactory), the use of slave to initialize the parent class, Assign values to slave and provisionerfactory, respectively.

Create a container provisioner to setup this Jenkins "Computer" (aka Executor)

(2), Public Hyperprovisioner Createprovisioner (): the function calls Provisioner = Provisionerfactory.createprovisioner ( Getlistener ()) and return to Provisioner

(3), Public Computerlauncher Createcomputerlauncher (): The function simply new and returns a Hypercomputerlauncher ()

5. Public class Hyperprovisioner

Function: Creates a slave container, and the subsequent exec operation is done through this class

Protected final Jobbuildscontainerscontext context;

Protected final Tasklistener Slavelistener;

Protected final Containerdriver driver;

Protected final Launcher Launcher;

Protected final Containersetdefinition spec;

(1), the constructor is: public Hyperprovisioner (Jobbuildscontainerscontext context, Tasklistener slavelistenerm, Containerdriver Driver, job job, Containersetdefinition spec)

Most variables in the class are assigned directly, where This.launcher = new Launcher.locallauncher (Slavelistener)

(2), public void Prepareandlaunchslavecontainer (final slavecomputer computer, Tasklistener Listener): This function first Judge slave Whether the container exists and is reused if it exists.

Otherwise, first get buildimage and Containersize, then call final containerinstance Slavecontainer = Driver.createandlaunchslavecontainer ( Computer, launcher, buildimage, containersize) generates a container instance and finally calls Context.setslavecontainer (Slavecontainer) slave Container joins the context.

(3), public Proc launchbuildprocess (Launcher.procstarter procstarter, Tasklistener): This function simply calls return Driver.execinslavecontainer (Launcher, Context.getslavecontainer (). GetId (), Procstarter)

6. Public class Hypercomputerlauncher extends Computerlauncher

Function: Start slave container

(1), public void launch (final slavecomputer computer, Tasklistener Listener): After the launch container, add some log, and then call launch ( Hypercomputer) computer, listener)

(2), public void launch (final hypercomputer computer, Tasklistener Listener): This function first calls provisioner= Computer.createprovisioner () An instance of the Hyperprovisioner class, and then calls Provisioner.prepareandlaunchslavecontainer (computer, Listener) to generate slave container

7. Public class Hyperlauncher extends Launcher.decoratedlauncher

Role: Process launcher which uses Docker exec instead of EXECVE

Jenkins relies on remoting channel to run commands/process on executor. As Docker can as well is used to run a process remotely.

Jenkins plugin Hyper Slaves Source analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.