in agent/session/plugin/sendpublickey/sendpublickey.go [54:166]
func doSession(ctx *cli.Context, instance_id, public_key, user_name string) error {
// 判断public_key 是公钥内容还是公钥文件路径
isfile := false
s, err := os.Stat(public_key)
if err != nil {
if os.IsExist(err) {
isfile = !s.IsDir()
}
} else {
isfile = !s.IsDir()
}
publicKeyContent := ""
if isfile {
publicKeyContent, err = readFile(public_key)
if err != nil {
fmt.Errorf("read public_key file failed %s", err.Error())
return err
}
} else {
publicKeyContent = public_key
}
if user_name == "" {
user_name = "root"
}
profile, err := config.LoadProfileWithContext(ctx)
if err != nil {
fmt.Errorf("load configuration failed %s", err)
return err
}
client, err := session.GetEcsClient(ctx)
if err != nil {
return err
}
// 执行agent命令下发,安装config_ecs_instance_connect插件
runcommandRequest := ecs.CreateRunCommandRequest()
runcommandRequest.Scheme = "https"
runcommandRequest.Type = "RunShellScript"
runcommandRequest.CommandContent = installPluginCommandBase64
runcommandRequest.ContentEncoding = "Base64"
runcommandRequest.Timeout = "5"
runcommandRequest.RegionId = profile.RegionId
runcommandRequest.InstanceId = &[]string{instance_id}
runcommandResponse, err := client.RunCommand(runcommandRequest)
if err != nil {
fmt.Errorf("install config_ecs_instance_connect failed %s", err)
return err
}
invokedId := runcommandResponse.InvokeId
time.Sleep(time.Duration(3) * time.Second)
// 检查安装插件的命令是否执行成功
describeInvocationRequest := ecs.CreateDescribeInvocationResultsRequest()
describeInvocationRequest.Scheme = "https"
describeInvocationRequest.RegionId = profile.RegionId
describeInvocationRequest.InvokeId = invokedId
describeInvocationRequest.InstanceId = instance_id
describeInvocationResponse, err := client.DescribeInvocationResults(describeInvocationRequest)
if err != nil {
fmt.Errorf("query 'install config_ecs_instance_connect' command result failed %s", err.Error())
return err
}
for describeInvocationResponse.Invocation.InvocationResults.InvocationResult[0].InvocationStatus == "Running" || describeInvocationResponse.Invocation.InvocationResults.InvocationResult[0].InvocationStatus == "Pending" {
describeInvocationResponse, err = client.DescribeInvocationResults(describeInvocationRequest)
if err != nil {
fmt.Errorf("query 'install config_ecs_instance_connect' command result failed %s", err.Error())
return err
}
time.Sleep(time.Duration(3) * time.Second)
}
if describeInvocationResponse.Invocation.InvocationResults.InvocationResult[0].InvocationStatus != "Success" {
fmt.Errorf("'install config_ecs_instance_connect' command failed, InvocationStatus: %s", describeInvocationResponse.Invocation.InvocationResults.InvocationResult[0].InvocationStatus)
return errors.New("'install config_ecs_instance_connect' command failed")
}
// 调用公共命令注册临时公钥
invokecommandRequest := ecs.CreateInvokeCommandRequest()
invokecommandRequest.Scheme = "https"
invokecommandRequest.CommandId = SENDSSHPUBLICKEY_PUBLICCOMMANDID
invokecommandRequest.InstanceId = &[]string{instance_id}
invokecommandRequest.Parameters = map[string]interface{}{
"username": user_name,
"sshPublicKey": publicKeyContent,
}
invokecommandResponse, err := client.InvokeCommand(invokecommandRequest)
if err != nil {
fmt.Errorf("run public command ACS-ECS-SendSshPublicKey-linux failed %s", err.Error())
return err
}
invokedId = invokecommandResponse.InvokeId
describeInvocationRequest = ecs.CreateDescribeInvocationResultsRequest()
describeInvocationRequest.Scheme = "https"
describeInvocationRequest.RegionId = profile.RegionId
describeInvocationRequest.InvokeId = invokedId
describeInvocationResponse, err = client.DescribeInvocationResults(describeInvocationRequest)
if err != nil {
fmt.Errorf("query 'ACS-ECS-SendSshPublicKey-linux' command result failed %s", err.Error())
return err
}
for describeInvocationResponse.Invocation.InvocationResults.InvocationResult[0].InvocationStatus == "Running" || describeInvocationResponse.Invocation.InvocationResults.InvocationResult[0].InvocationStatus == "Pending" {
describeInvocationResponse, err = client.DescribeInvocationResults(describeInvocationRequest)
if err != nil {
fmt.Errorf("query 'ACS-ECS-SendSshPublicKey-linux' command result failed %s", err.Error())
return err
}
time.Sleep(time.Duration(2) * time.Second)
}
if describeInvocationResponse.Invocation.InvocationResults.InvocationResult[0].InvocationStatus != "Success" {
fmt.Errorf("'ACS-ECS-SendSshPublicKey-linux' command failed, InvocationStatus: %s", describeInvocationResponse.Invocation.InvocationResults.InvocationResult[0].InvocationStatus)
return errors.New("'ACS-ECS-SendSshPublicKey-linux' command failed")
}
fmt.Println("The temporary ssh_public_key has been registered")
return nil
}