Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
getVirtualMachineCount counts only the VMs deployed from the current …
…Jenkins instance [FIXED JENKINS-41568]
  • Loading branch information
clguiman committed Feb 13, 2017
1 parent 6e9fe30 commit 0d342f1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
Expand Up @@ -761,7 +761,17 @@ public static boolean isVMAliveOrHealthy(final AzureVMAgent agent) throws Except
public static int getVirtualMachineCount(final ServicePrincipal servicePrincipal, final String resourceGroupName) {
try {
final Azure azureClient = TokenCache.getInstance(servicePrincipal).getAzureClient();
return azureClient.virtualMachines().listByGroup(resourceGroupName).size();
final PagedList<VirtualMachine> vms = azureClient.virtualMachines().listByGroup(resourceGroupName);
int count = 0;
final AzureUtil.DeploymentTag deployTag = new AzureUtil.DeploymentTag();
for (VirtualMachine vm : vms) {
final Map<String,String> tags = vm.tags();
if ( tags.containsKey(Constants.AZURE_RESOURCES_TAG_NAME) &&
deployTag.isFromSameInstance(new AzureUtil.DeploymentTag(tags.get(Constants.AZURE_RESOURCES_TAG_NAME)))) {
count++;
}
}
return count;
} catch (Exception e) {
LOGGER.log(Level.INFO,
"AzureVMManagementServiceDelegate: getVirtualMachineCount: Got exception while getting hosted "
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/microsoft/azure/vmagent/util/AzureUtil.java
Expand Up @@ -443,6 +443,10 @@ public boolean matches(final DeploymentTag rhs, long timeout) {
return Math.abs(timestamp - rhs.timestamp) > timeout;
}

public boolean isFromSameInstance(final DeploymentTag rhs) {
return instanceId.equals(rhs.instanceId);
}

protected DeploymentTag(long timestamp) {
String id = "";
try {
Expand Down
Expand Up @@ -4,7 +4,7 @@ Subscription_ID=Subscription ID
Client_Id=Client ID
Client_Secret=Client Secret
OAuth2_Token_Endpoint=OAuth 2.0 Token Endpoint
Max_Virtual_Machines_Limit=Max Virtual Machines Limit
Max_Virtual_Machines_Limit=Max Jenkins Agents Limit
Deployment_Timeout=Deployment Timeout (seconds)
Service_Management_URL=Management Service URL
Verify_Configuration=Verify Configuration
Expand Down
Expand Up @@ -289,7 +289,9 @@ public void getVirtualMachineCountTest() {
try {
Random rand = new Random();
final int numberOfAgents = rand.nextInt(4) + 1;
final String vmName = "vmnotcounted";
createDefaultDeployment(numberOfAgents, null);
createAzureVM(vmName);

Assert.assertEquals(numberOfAgents, AzureVMManagementServiceDelegate.getVirtualMachineCount(servicePrincipal, testEnv.azureResourceGroup));
Assert.assertEquals(0, AzureVMManagementServiceDelegate.getVirtualMachineCount(servicePrincipal, testEnv.azureResourceGroup + "-missing"));
Expand Down
Expand Up @@ -200,8 +200,7 @@ protected AzureVMDeploymentInfo createDefaultDeployment(final int numberOfAgents
when(vmCredentials.getPassword()).thenReturn(vmPassword);

if (deploymentRegistrar == null) {
deploymentRegistrar = mock(AzureVMAgentCleanUpTask.DeploymentRegistrar.class);
when(deploymentRegistrar.getDeploymentTag()).thenReturn(new AzureUtil.DeploymentTag("some_tag/123"));
deploymentRegistrar = new AzureVMAgentCleanUpTask.DeploymentRegistrar();
}

AzureVMAgentTemplate templateMock = mock(AzureVMAgentTemplate.class);
Expand Down

0 comments on commit 0d342f1

Please sign in to comment.