From 039841c0ea3e768ed9b682f826487a8135067197 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Tue, 27 Feb 2024 15:45:33 +0300 Subject: [PATCH] #1877 wait for running state --- src/main/java/com/rultor/agents/Agents.java | 33 ++++++++++--------- .../com/rultor/agents/aws/StartsInstance.java | 28 +++++++++++----- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/rultor/agents/Agents.java b/src/main/java/com/rultor/agents/Agents.java index b8415470cc..8d4170c752 100644 --- a/src/main/java/com/rultor/agents/Agents.java +++ b/src/main/java/com/rultor/agents/Agents.java @@ -266,22 +266,25 @@ public Agent agent(final Talk talk, final Profile profile) ), new StartsRequest(profile), new Agent.Quiet( - new StartsInstance( - new AwsEc2( - Manifests.read("Rultor-EC2Key"), - Manifests.read("Rultor-EC2Secret") - ), - new PfShell( - profile, - "none", - Agents.PORT, - "ubuntu", - Agents.priv() + new Agent.Disabled( + new StartsInstance( + new AwsEc2( + Manifests.read("Rultor-EC2Key"), + Manifests.read("Rultor-EC2Secret") + ), + new PfShell( + profile, + "none", + Agents.PORT, + "ubuntu", + Agents.priv() + ), + Manifests.read("Rultor-EC2Image"), + Manifests.read("Rultor-EC2Type"), + Manifests.read("Rultor-EC2Group"), + Manifests.read("Rultor-EC2Subnet") ), - Manifests.read("Rultor-EC2Image"), - Manifests.read("Rultor-EC2Type"), - Manifests.read("Rultor-EC2Group"), - Manifests.read("Rultor-EC2Subnet") + false ) ), new RegistersShell( diff --git a/src/main/java/com/rultor/agents/aws/StartsInstance.java b/src/main/java/com/rultor/agents/aws/StartsInstance.java index e8d8e0682f..8c7a9bd0c1 100644 --- a/src/main/java/com/rultor/agents/aws/StartsInstance.java +++ b/src/main/java/com/rultor/agents/aws/StartsInstance.java @@ -30,7 +30,10 @@ package com.rultor.agents.aws; import com.amazonaws.services.ec2.model.CreateTagsRequest; +import com.amazonaws.services.ec2.model.DescribeInstanceStatusRequest; +import com.amazonaws.services.ec2.model.DescribeInstanceStatusResult; import com.amazonaws.services.ec2.model.Instance; +import com.amazonaws.services.ec2.model.InstanceState; import com.amazonaws.services.ec2.model.RunInstancesRequest; import com.amazonaws.services.ec2.model.RunInstancesResult; import com.amazonaws.services.ec2.model.Tag; @@ -167,14 +170,23 @@ private Instance run() { final RunInstancesResult response = this.api.aws().runInstances(request); final Instance instance = response.getReservation().getInstances().get(0); - final Tag awstag = new Tag() - .withKey("name") - .withValue("rultor"); - final CreateTagsRequest req = new CreateTagsRequest() - .withResources(instance.getInstanceId()) - .withTags(awstag); - this.api.aws().createTags(req); - Logger.info(this, "AWS instance %s launched", instance.getInstanceId()); + final String iid = instance.getInstanceId(); + this.api.aws().createTags( + new CreateTagsRequest() + .withResources(iid) + .withTags(new Tag().withKey("name").withValue("rultor")) + ); + while (true) { + final DescribeInstanceStatusResult res = this.api.aws().describeInstanceStatus( + new DescribeInstanceStatusRequest().withInstanceIds(iid) + ); + final InstanceState state = res.getInstanceStatuses().get(0).getInstanceState(); + Logger.info(this, "AWS instance %s state: %s", state.getName()); + if ("running".equals(state.getName())) { + break; + } + } + Logger.info(this, "AWS instance %s launched and running", iid); return instance; } }