You can use Cloud Assistant to simultaneously run scripts on multiple ECS instances. The scripts can be shell, bat, or PowerShell scripts. Compared with SSH or RDP, you can use Cloud Assistant to perform O&M operations on ECS instances in Java.

Prerequisites

  • The Cloud Assistant client is installed on the target instances. For more information, see Configure the Cloud Assistant client.
  • The aliyun-java-sdk-ecs SDK dependency in Java is updated to V4.18.3 or later. For more information, see MavenRepository for the latest versions.

Procedure

  1. Compile the shell, bat, or PowerShell scripts based on the instance configurations and the operations that you want to perform.
    For example, you can run the following shell scripts to upgrade the Python development environment in an ECS instance:
    wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
    rm -rf /usr/bin/python3
    tar -xzvf Python-3.7.0.tgz
    cd Python-3.7.0
    /configure && make && make install
    ln -s /usr/local/bin/python3.7 /usr/bin/python3
    ln -s /usr/local/bin/python3.7-config /usr/bin/python-config
    python3 -V

    For more information about example scripts, see View instance configurations and Modify instance configurations and install applications.

  2. Find ECS instances that meet the specified requirements.
    The target instances must be in the Running (Running) state. For more information about examples of SDK for Java, see Query an ECS instance.
  3. Obtain the AccessKey pair of the account and query the region ID.
    For more information, see Regions and zones and Create an AccessKey.
  4. Create a RunCommandSample.java class to configure the same Cloud Assistant script for one or more ECS instances.
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.ecs.model.v20140526.RunCommandRequest;
    import com.aliyuncs.ecs.model.v20140526.RunCommandResponse;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import java.util.ArrayList;
    import java.util.List;
    
    public class RunCommandsSmaple {
        public static void main(String[] args) {
            // Initialize the profile object and configure the region ID (for example, cn-hangzhou) and the AccessKey pair (AccessKey ID and AccessKey secret).        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
    
            IAcsClient client = new DefaultAcsClient(profile);
            RunCommandRequest request = new RunCommandRequest();
    
            // request.setRegionId("cn-hangzhou");
            request.setName("upgradePython");
            request.setType("RunShellScript");
             // The Cloud Assistant script that you compiled in the shell, bat, or PowerShell format.        request.setCommandContent("wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz && rm -rf /usr/bin/python3 \n tar -xzvf Python-3.7.0.tgz  \n cd Python-3.7.0 \n ./configure && make && make install\nln -s /usr/local/bin/python3.7 /usr/bin/python3\nln -s /usr/local/bin/python3.7-config /usr/bin/python-config\npython3 -V");
             // If the CommandContent is in plaintext, set the ContentEncoding parameter to PlainText.        request.setContentEncoding("PlainText");
             // Because it may take an extended period of time to download the installation package, we recommend that you set a long period to avoid failure of remote scripts due to timeout. The default timeout period is 60s.        request.setTimeout(600L);
    
             // The list of one or more instance IDs.        List<String> instanceIdList = new ArrayList<String>();
            instanceIdList.add("i-bp155lkyo1ut0d******");
            request.setInstanceIds(instanceIdList);
    
             // Handle exceptions and responses.        try {
                RunCommandResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }
    The following result is returned. Record InvokeId.
    {
        "RequestId": "473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E",
        "InvokeId": "t-hz0b22o6******",
        "CommandId": "c-b224dc5072f3460fbb10fc2912******"
    }
  5. Create a DescribeInvocationsSample.java class to check whether the script succeeds.
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.ecs.model.v20140526.DescribeInvocationsRequest;
    import com.aliyuncs.ecs.model.v20140526.DescribeInvocationsResponse;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
     public class DescribeInvocationsSample {
        public static void main(String[] args) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
             DescribeInvocationsRequest request = new DescribeInvocationsRequest();
            // request.setRegionId("cn-hangzhou");
            // Enter the execution ID of the Cloud Assistant script.        request.setInvokeId("t-hz0b22o6******");
             try {
                DescribeInvocationsResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }
    The following result is returned. You can call the InvokeInstances operation to view the running status and result of the script.
    {
        "RequestId": "9A281A3E-0CE1-4D26-83F2-20F52EAC567D",
        "TotalCount": 56,
        "PageNumber": 1,
        "PageSize": 1,
        "Invocations": [{
            "InvokeId": "t-hz0b22o6******",
            "CreationTime": "2020-02-17T11:28:01Z",
            "CommandId": "c-b224dc5072f3460fbb10fc2912******",
            "CommandType": "RunShellScript",
            "CommandName": "upgradePython",
            "CommandContent": "******",
            "Frequency": "",
            "Timed": false,
            "InvokeStatus": "Finished",
            "Parameters": "{}",
            "InvokeInstances": [{
                "InstanceId": "i-bp155lkyo1ut0d******",
                "Repeats": 1,
                "InstanceInvokeStatus": "Finished",
                "InvocationStatus": "Success",
                "Output": "",
                "ExitCode": 0,
                "Dropped": 0,
                "ErrorCode": "",
                "ErrorInfo": "",
                "CreationTime": "2020-02-17T11:28:01Z",
                "StartTime": "2020-02-17T11:28:02Z",
                "StopTime": "",
                "FinishTime": "2020-02-17T11:28:02Z",
                "UpdateTime": "2020-02-17T11:28:02Z"
            }]
        }]
    }