码迷,mamicode.com
首页 > 其他好文 > 详细

Jenkins插件hyper slaves源码分析

时间:2016-11-11 14:01:10      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:docker   any   standard   dso   函数   its   instance   acea   efi   

1、public class HyperSlaves extends Plugin implements Describable<HyperSlaves>

  (1)、init():初始化containerDriverFactory,其中的containerDriverFactory是一个抽象类ContainerDriverFactory类型的变量,我们可以调用它的forJob(Job context)方法获得一个ContainerDriver类型的抽象类。

  (2)、createStandardJobProvisionerFactory(job): 其中调用return new HyperProvisionerFactory.StandardJob(...)来返回一个HyperProvisionerFactory的抽象类

 

2、public abstract class HyperProvisionerFactory

作用:准备workspace,再返回一个HyperProvisioner实例

  (1)、其中包含一个抽象方法public abstract HyperProvisioner createProvisioner(TaskListener slaveListener),其中内部类StandardJob实现了本抽象类,其中的createProvisioner函数先创建一个JobBuildsContainersContext类的实例context,再调用prepareWorkspace(job, context),最后调用return new HyperProvisioner(context, slaveListener, driver, job, spec)

 

3、public abstract class OneShotSlave extends Slave implements EphemeralNode

作用:A slave that is designed to be used only once, for a specific hudson.model.Run,and as such has a life cycle to fully match 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 is created, so we can have a 1:1 match between Run and Executor lifecycle:

  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

变量:private transient Queue.Executable executable

private final ComputerLauncher realLauncher;

private boolean provisioningFailed = false;

  (1)、public Launcher createLauncher(TaskListener listener):该函数先调用provision(listener),再调用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 to be 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 have a strong 1:1 relation between a Run and its Executor

  (2)、首先调用创建 Executor类:executor = Executor.currentExecutor(),再调用realLauncher.launch(this.getComputer(), listener)启动一个slave容器

     如果之后调用getComputer().isActuallyOffline()为true,则调用provisionFailed(new IllegalStateException("Computer is offline after launch"))

     最后,如果没有异常发生,则调用executable = executor.getCurrentExecutable()

  

 

3、public class HyperSlave extends OneShotSlave

// EphemeralNode使用hyper container来运行build process,Slave只专注于一个特定的Job,最好专注于一个特定的build,但是在本类刚刚创建的时候,因为jenkins的生存周期,build还不存在。

private final HyperProvisionerFactory provisionerFactory

  (1)、构造函数为:public HyperSlave(String name, String nodeDescription, String labelString, HyperProvisionerFactory provisionerFactory):

      先调用super(name.replaceAll("/", ">>"), nodeDescription, SLAVE_ROOT, labelString, new HyperComputerLauncher())初始化父类

  (2)、public HyperComputer createComputer():该函数只是简单地调用new HyperComputer(this, provisionerFactory)

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

  (3)、public Launcher createLauncher(TaskListener listener):该方法先调用c = getComputer()获取HyperComputer类,再调用super.createLauncher(listener),最后调用launcher = new HyperLauncher(listener, c.getChannel(), c.isUnix(), c.getProvisioner()).decorateFor(this),生成一个launcher并返回。

 

4、public class HyperComputer extends OneShotComputer

private final HyperSlave slave;

private final HyperProvisionerFactory provisionerFactory;

private HyperProvisioner

  (1)、构造函数为:public HyperComputer(HyperSlave slave, HyperProvisionerFactory provisionerFactory),利用slave初始化父类,再分别给slave和provisionerFactory赋值

   // Create a container provisioner to setup this Jenkins "computer" (aka executor)

  (2)、public HyperProvisioner createProvisioner():该函数调用 provisioner = provisionerFactory.createProvisioner(getListener()),并返回provisioner

  (3)、public ComputerLauncher createComputerLauncher():该函数仅仅new并返回一个HyperComputerLauncher()

 

5、public class HyperProvisioner

作用:创建slave容器,并且之后的exec操作也是通过本类进行

protected final JobBuildsContainersContext context;

protected final TaskListener slaveListener;

protected final ContainerDriver driver;

protected final Launcher launcher;

protected final ContainerSetDefinition spec;

  (1)、构造函数为:public HyperProvisioner(JobBuildsContainersContext context, TaskListener slaveListenerm, ContainerDriver driver, Job job, ContainerSetDefinition spec)

     该类中的多数变量都直接赋值,其中this.launcher = new Launcher.LocalLauncher(slaveListener)

  (2)、public void prepareAndLaunchSlaveContainer(final SlaveComputer computer, TaskListener listener):该函数先判断slave container是否存在,如果存在则重用。

     否则,首先获取buildImage和containerSize,再调用final ContainerInstance slaveContainer = driver.createAndLaunchSlaveContainer(computer, launcher, buildImage,  containerSize)生成一个容器实例,最后调用context.setSlaveContainer(slaveContainer)将slave container加入上下文。

  (3)、public Proc launchBuildProcess(Launcher.ProcStarter procStarter, TaskListener):该函数仅仅调用return driver.execInSlaveContainer(launcher, context.getSlaveContainer().getId(), procStarter)

 

6、public class HyperComputerLauncher extends ComputerLauncher

作用:启动slave容器

  (1)、public void launch(final SlaveComputer computer, TaskListener listener):launch容器的之前之后,添加一些log,再调用launch((HyperComputer) computer, listener)

  (2)、public void launch(final HyperComputer computer, TaskListener listener):该函数先调用provisioner=computer.createProvisioner()的HyperProvisioner类的实例,再调用provisioner.prepareAndLaunchSlaveContainer(computer, listener)来生成slave container

 

7、public class HyperLauncher extends Launcher.DecoratedLauncher

作用: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 be used to run a process remotely .

Jenkins插件hyper slaves源码分析

标签:docker   any   standard   dso   函数   its   instance   acea   efi   

原文地址:http://www.cnblogs.com/YaoDD/p/6053792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!