Class CloudProvisioningState

java.lang.Object
org.jenkinsci.plugins.vsphere.tools.CloudProvisioningState

public class CloudProvisioningState extends Object
Utility class that works out what agents we should start up in response to Jenkins asking us to start things.

We do this by keeping a record of every agent we start, and every agent we have active. That way, we can avoid over-provisioning.

The idea is that we are told what agents that the cloud is going to create, when the cloud has created them (or failed to) and when those agents have died. This way we can keep track of everything, in order to allow the cloud to make accurate decisions regarding what to create next.

Note: This is not thread-safe. Callers must do their own synchronization.
  • Constructor Details

    • CloudProvisioningState

      public CloudProvisioningState(vSphereCloud parent)
  • Method Details

    • provisioningStarted

      public void provisioningStarted(CloudProvisioningRecord provisionable, String nodeName)
      To be called when we've decided to create a new node. Callers MUST ensure that provisionedSlaveNowActive(CloudProvisioningRecord, String) or provisioningEndedInError(CloudProvisioningRecord, String) gets called later.
      Parameters:
      provisionable - Our record for the template for the named node.
      nodeName - The name of the VM.
    • provisionedSlaveNowActive

      public void provisionedSlaveNowActive(CloudProvisioningRecord provisionable, String nodeName)
      To be called when a newly created node (previously promised to provisioningStarted(CloudProvisioningRecord, String)) comes up. Callers MUST ensure that provisionedSlaveNowUnwanted(String, boolean) gets called later when we do not want it anymore.
      Parameters:
      provisionable - Our record for the template for the named node.
      nodeName - The name of the VM.
    • provisionedSlaveNowUnwanted

      public void provisionedSlaveNowUnwanted(String nodeName, boolean willAttemptImmediateDeletion)
      To be called when a node we created (previously told to provisionedSlaveNowActive(CloudProvisioningRecord, String)) is no longer wanted and should be deleted.
      Parameters:
      nodeName - The name of the VM.
      willAttemptImmediateDeletion - If true then the caller must attempt to delete the agent and guarantee that they will call unwantedSlaveNotDeleted(String) or unwantedSlaveNowDeleted(String) as appropriate (just as if they'd called isOkToDeleteUnwantedVM(String) and been told True). If false then the caller is under no such obligation.
    • isOkToDeleteUnwantedVM

      public Boolean isOkToDeleteUnwantedVM(String nodeName)
      To be called before commencing the deletion of a VM.
      Parameters:
      nodeName - The name of the VM being deleted.
      Returns:
      null if the VM is not unwanted (it may have recently been deleted). false if another thread is currently trying to delete it. true if deletion should be attempted, in which case the caller MUST later call unwantedSlaveNowDeleted(String) or unwantedSlaveNotDeleted(String).
    • unwantedSlaveNowDeleted

      public void unwantedSlaveNowDeleted(String nodeName)
      MUST be called when a node previously declared to be unwanted (previously told to provisionedSlaveNowUnwanted(String, boolean)) and that we were given clearance to delete (isOkToDeleteUnwantedVM(String) returned true) has been successfully removed.
      Parameters:
      nodeName - The name of the VM that was successfully deleted.
    • unwantedSlaveNotDeleted

      public void unwantedSlaveNotDeleted(String nodeName)
      MUST be called when a node previously declared to be unwanted (previously told to provisionedSlaveNowUnwanted(String, boolean)) and that we were given clearance to delete (isOkToDeleteUnwantedVM(String) returned true) failed to be removed.
      Parameters:
      nodeName - The name of the VM that failed to delete
    • recordExistingUnwantedVM

      public void recordExistingUnwantedVM(vSphereCloudSlaveTemplate template, String nodeName)
      To be called if we become aware that there is a VM that exist in vSphere (that we created) which we don't want anymore.
      Parameters:
      template - The template to which the node belonged.
      nodeName - The name of the node that exists (despite our wishes).
    • provisioningEndedInError

      public void provisioningEndedInError(CloudProvisioningRecord provisionable, String nodeName)
      To be called when a node that we previously promised to create (by calling provisioningStarted(CloudProvisioningRecord, String)) failed to start.
      Parameters:
      provisionable - Our record for the template for the named node.
      nodeName - The name of the VM.
    • pruneUnwantedRecords

      public void pruneUnwantedRecords()
      To be called every now and again to ensure that we're not caching records that will never be valid again.
    • calculateProvisionableTemplates

      public List<CloudProvisioningRecord> calculateProvisionableTemplates(Iterable<vSphereCloudSlaveTemplate> templates)
      Given a set of templates, returns the equivalent records.
      Parameters:
      templates - The templates we are interested in.
      Returns:
      A list of CloudProvisioningRecord.
    • countNodes

      public int countNodes()
      Counts all the known nodes, active, in-progress and being-deleted, across all templates.
      Returns:
      The number of nodes that exist (or will do).
    • getOrCreateRecord

      public CloudProvisioningRecord getOrCreateRecord(vSphereCloudSlaveTemplate template)
      Gets the record for the given template. If we didn't have one before, we create one.
      Parameters:
      template - The template in question.
      Returns:
      The one-and-only record for this template.
    • getUnwantedVMsThatNeedDeleting

      public List<String> getUnwantedVMsThatNeedDeleting()
      Calculates the current list of "existing but unwanted" VMs, in priority order. Note: The returned data is not "live", it's a copy, so callers are free to edit the List they are given.
      Returns:
      A copy of the list of VMs that we know exist but no longer want, and which aren't in the process of being deleted by anyone.