@Override public Collection<NodeProvisioner.PlannedNode> provision(Label label, int excessWorkload) { Queue<JCloudsSlaveTemplate> templateProvider = getAvailableTemplateProvider(label); List<PlannedNode> plannedNodeList = new ArrayList<>(); while (excessWorkload > 0 && !Jenkins.getActiveInstance().isQuietingDown() && !Jenkins.getActiveInstance().isTerminating()) { final JCloudsSlaveTemplate template = templateProvider.poll(); if (template == null) { LOGGER.info("Instance cap exceeded on all available templates"); break; } LOGGER.fine("Provisioning slave for " + label + " from template " + template.name); int numExecutors = template.getEffectiveSlaveOptions().getNumExecutors(); ProvisioningActivity.Id id = new ProvisioningActivity.Id(this.name, template.name); Future<Node> task = Computer.threadPoolForRemoting.submit(new NodeCallable(this, template, id)); plannedNodeList.add(new TrackedPlannedNode(id, numExecutors, task)); excessWorkload -= numExecutors; } return plannedNodeList; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; RecordDisposal that = (RecordDisposal) o; if (!inner.equals(that.inner)) return false; return provisioningId.equals(that.provisioningId); }
JCloudsComputer computer = entry.getValue(); String id = entry.getKey(); String cloudName = computer.getId().getCloudName(); JCloudsCloud cloud; try {
final int index = i; final ProvisioningActivity.Id provisioningId = new ProvisioningActivity.Id(this.name, template.getTemplateName());
EnvVars initialEnvironment) throws IOException, InterruptedException { final ProvisioningActivity.Id activityId = new ProvisioningActivity.Id( run.getDisplayName(), getConfig().getDockerContainerLifecycle().getImage() final String futureName = "yadp" + Integer.toString(activityId.getFingerprint());
final ProvisioningActivity.Id id = new ProvisioningActivity.Id(getDisplayName(), t.getDockerContainerLifecycle().getImage());
final ProvisioningActivity.Id provisioningId = new ProvisioningActivity.Id(this.name, template.name);
public JCloudsSlave( @Nonnull ProvisioningActivity.Id id, @Nonnull Server metadata, @Nonnull String labelString, @Nonnull SlaveOptions slaveOptions ) throws IOException, Descriptor.FormException { super( metadata.getName(), null, slaveOptions.getFsRoot(), slaveOptions.getNumExecutors(), Mode.NORMAL, labelString, null, new JCloudsRetentionStrategy(), Collections.singletonList(new EnvironmentVariablesNodeProperty( new EnvironmentVariablesNodeProperty.Entry("OPENSTACK_PUBLIC_IP", Openstack.getPublicAddress(metadata)) )) ); this.cloudName = id.getCloudName(); // TODO deprecate field this.provisioningId = id; this.options = slaveOptions; this.nodeId = metadata.getId(); this.cache = makeCache(); setLauncher(new JCloudsLauncher(getLauncherFactory().createLauncher(this))); }
/** * Should a slave be retained to meet the minimum instances constraint? */ public static boolean shouldSlaveBeRetained(JCloudsSlave slave) { String templateName = slave.getId().getTemplateName(); String cloudName = slave.getId().getCloudName(); if (templateName != null && cloudName != null) { JCloudsCloud cloud = JCloudsCloud.getByName(cloudName); if (cloud != null) { JCloudsSlaveTemplate template = cloud.getTemplate(templateName); if (template != null) { SlaveOptions slaveOptions = template.getEffectiveSlaveOptions(); Integer instancesMin = slaveOptions.getInstancesMin(); JCloudsComputer computer = slave.getComputer(); Integer retentionTime = slaveOptions.getRetentionTime(); if (instancesMin > 0 && computer != null) { if (retentionTime != 0 && (template.getActiveNodesTotal(false) - 1) < instancesMin) { return true; } if (retentionTime == 0 && !computer.isUsed() && (template.getActiveNodesTotal(true) - 1) < instancesMin) { return true; } } } } } return false; }
/** * Return the number of active nodes provisioned using this template. * * @param onlyNewComputers Whether or not to limit the count to unused machines. */ public int getActiveNodesTotal(boolean onlyNewComputers) { int totalServers = 0; for (Node node : Jenkins.getActiveInstance().getNodes()) { if (node instanceof JCloudsSlave) { JCloudsSlave slave = (JCloudsSlave) node; if (slave.getId().getCloudName().equals(cloud.name) && slave.getId().getTemplateName().equals(name)) { JCloudsComputer computer = slave.getComputer(); if (computer != null && !computer.isPendingDelete() && !(computer.getOfflineCause() instanceof OfflineCause.UserCause)) { if (onlyNewComputers) { if (!computer.isUsed()) { totalServers++; } } else { totalServers++; } } } } } return totalServers; }
private String getServerName() { CloudStatistics cs = CloudStatistics.get(); next_number: for (;;) { // Using static counter to ensure colliding template names (between clouds) will not cause a clash String nameCandidate = name + "-" + nodeCounter.getAndIncrement(); // Collide with existing node - quite likely from this cloud if (Jenkins.getInstance().getNode(nameCandidate) != null) continue; // Collide with node being provisioned (at this point this plugin does not assign final name before launch // is completed) or recently used name (just to avoid confusion). for (ProvisioningActivity provisioningActivity : cs.getActivities()) { if (nameCandidate.equals(provisioningActivity.getId().getNodeName())) { continue next_number; } } return nameCandidate; } }
@Restricted(NoExternalUse.class) public @Nonnull JCloudsSlave provisionSlave(JCloudsSlaveTemplate template) throws IOException, Openstack.ActionFailed{ CloudStatistics.ProvisioningListener provisioningListener = CloudStatistics.ProvisioningListener.get(); ProvisioningActivity.Id id = new ProvisioningActivity.Id(this.name, template.name); JCloudsSlave node; try { provisioningListener.onStarted(id); node = template.provisionSlave(this, id); provisioningListener.onComplete(id, node); } catch (Throwable ex) { provisioningListener.onFailure(id, ex); throw ex; } Jenkins.getActiveInstance().addNode(node); return node; }
@Override public int hashCode() { return Objects.hashCode(inner.hashCode(), provisioningId.hashCode()); } }
JCloudsSlave doProvisionFromTemplate(final JCloudsSlaveTemplate t) throws IOException { final StringWriter sw = new StringWriter(); final StreamTaskListener listener = new StreamTaskListener(sw); final ProvisioningActivity.Id provisioningId = new ProvisioningActivity.Id(this.name, t.name); JCloudsSlave node = t.provisionSlave(listener, provisioningId); Jenkins.getInstance().addNode(node); return node; }