Skip to content

Commit

Permalink
Parallels Desktop Cloud for Jenkins plugin
Browse files Browse the repository at this point in the history
This is an initial commit with initial implementation of a plugin. It is
mostly a proof-of-concept, with lots of bugs and kludges. But basic
functionality works.
  • Loading branch information
romankulikov committed Aug 18, 2015
0 parents commit fcf24d4
Show file tree
Hide file tree
Showing 13 changed files with 1,064 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
work
65 changes: 65 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
(c) 2004-2015. Parallels IP Holdings GmbH. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.580.1</version>
</parent>
<groupId>com.parallels</groupId>
<artifactId>parallels-desktop</artifactId>
<version>0.0</version>
<packaging>hpi</packaging>

<name>Parallels Desktop Cloud</name>
<url>http://parallels.com</url>
<licenses>
<license>
<name>MIT License</name>
<url>http://opensource.org/licenses/MIT</url>
</license>
</licenses>
<!-- Assuming you want to host on @jenkinsci:
<scm>
<connection>scm:git:git://github.com/jenkinsci/${project.artifactId}-plugin.git</connection>
<developerConnection>scm:git:[email protected]:jenkinsci/${project.artifactId}-plugin.git</developerConnection>
<url>http://github.com/jenkinsci/${project.artifactId}-plugin</url>
</scm>
-->
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</project>
162 changes: 162 additions & 0 deletions src/main/java/com/parallels/desktopcould/ParallelsDesktopCloud.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* The MIT License
*
* (c) 2004-2015. Parallels IP Holdings GmbH. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.parallels.desktopcloud;

import hudson.Extension;
import hudson.model.Hudson;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Label;
import hudson.model.Node;
import hudson.slaves.Cloud;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.NodeProvisioner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.stapler.DataBoundConstructor;


public final class ParallelsDesktopCloud extends Cloud
{
private static final Logger LOGGER = Logger.getLogger(ParallelsDesktopCloud.class.getName());
private static final AtomicInteger counter = new AtomicInteger();

private final List<ParallelsDesktopVM> vms;
private final ComputerLauncher pdLauncher;
private final String remoteFS;

@DataBoundConstructor
public ParallelsDesktopCloud(String name, String remoteFS, ComputerLauncher pdLauncher,
List<ParallelsDesktopVM> vms)
{
super(name);
this.remoteFS = remoteFS;
if (vms == null)
this.vms = Collections.emptyList();
else
this.vms = vms;
this.pdLauncher = pdLauncher;
}

@Override
public Collection<NodeProvisioner.PlannedNode> provision(Label label, int excessWorkload)
{
LOGGER.log(Level.SEVERE, "Going to provision " + excessWorkload + " executors");
Collection<NodeProvisioner.PlannedNode> result = new ArrayList<NodeProvisioner.PlannedNode>();
final ParallelsDesktopConnectorSlaveComputer connector = getConnector();
for (int i = 0; (i < vms.size()) && (excessWorkload > 0); i++)
{
final ParallelsDesktopVM vm = vms.get(i);
if (label.matches(Label.parse(vm.getLabels())))
{
final int count = counter.incrementAndGet();
final String vmId = vm.getVmid();
final String slaveName = "PD Slave #" + count;
vm.setSlaveName(slaveName);
--excessWorkload;
result.add(new NodeProvisioner.PlannedNode(slaveName,
Computer.threadPoolForRemoting.submit(new Callable<Node>()
{
@Override
public Node call() throws Exception
{
connector.checkVmExists(vmId);
return connector.createSlaveOnVM(vm);
}
}), 1));
}
}
return result;
}

private ParallelsDesktopConnectorSlaveComputer getConnector()
{
try
{
ParallelsDesktopConnectorSlave slave = new ParallelsDesktopConnectorSlave("PDConnectorSlave", remoteFS, pdLauncher);
Hudson.getInstance().addNode(slave);
return (ParallelsDesktopConnectorSlaveComputer)slave.toComputer();
}
catch (Descriptor.FormException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
LOGGER.log(Level.SEVERE, null, ex);
}
catch(Exception ex)
{
LOGGER.log(Level.SEVERE, null, ex);
}
return null;
}

@Override
public boolean canProvision(Label label)
{
if (label != null)
{
for (ParallelsDesktopVM vm : vms)
{
if (label.matches(Label.parse(vm.getLabels())))
return true;
}
}
return false;
}

public List<ParallelsDesktopVM> getVms()
{
return vms;
}

public ComputerLauncher getPdLauncher()
{
return pdLauncher;
}

public String getRemoteFS()
{
return remoteFS;
}

@Extension
public static final class DescriptorImpl extends Descriptor<Cloud>
{
@Override
public String getDisplayName()
{
return "Parallels Desktop Cloud";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* The MIT License
*
* (c) 2004-2015. Parallels IP Holdings GmbH. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.parallels.desktopcloud;

import hudson.slaves.RetentionStrategy;
import hudson.model.Hudson;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;


public class ParallelsDesktopCloudRetentionStrategy extends RetentionStrategy<ParallelsDesktopVMSlaveComputer>
{
private static final Logger LOGGER = Logger.getLogger("PDCloudRetentionStrategy");

@DataBoundConstructor
public ParallelsDesktopCloudRetentionStrategy()
{
super();
}

@Override
public long check(ParallelsDesktopVMSlaveComputer c)
{
LOGGER.log(Level.SEVERE, "Check VM computer " + c.getName());
if (c.isOffline())
return 1;
if (c.isIdle())
{
LOGGER.log(Level.SEVERE, "Stopping node...");
c.getNode().stop();
try
{
LOGGER.log(Level.SEVERE, "Disconnecting computer...");
c.disconnect(null).get();
LOGGER.log(Level.SEVERE, "Removing node...");
Hudson.getInstance().removeNode(c.getNode());
LOGGER.log(Level.SEVERE, "Node was removed.");
}
catch (Exception e)
{
LOGGER.log(Level.SEVERE, e.toString());
}
}
return 1;
}

@Override
public void start(ParallelsDesktopVMSlaveComputer c)
{
LOGGER.log(Level.SEVERE, "Starting VM computer " + c.getName());
c.connect(false);
}

@Override
public DescriptorImpl getDescriptor()
{
return DESCRIPTOR;
}

@Restricted(NoExternalUse.class)
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
public static final class DescriptorImpl extends hudson.model.Descriptor<RetentionStrategy<?>>
{
@Override
public String getDisplayName()
{
return "ParallelsDesktop Cloud Retention Strategy";
}
}
}
Loading

0 comments on commit fcf24d4

Please sign in to comment.