Signed-off-by: Cao Xueliang caoxl78320@163.com --- container/assistant/routes.rb | 12 +++++++++++ .../assistant/views/send_account_mail.rb | 20 +++++++++++++++++++ lib/assign_account_client.rb | 13 +++++++----- lib/assistant_client.rb | 8 ++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 container/assistant/views/send_account_mail.rb
diff --git a/container/assistant/routes.rb b/container/assistant/routes.rb index 232ec8c..9a96fc0 100755 --- a/container/assistant/routes.rb +++ b/container/assistant/routes.rb @@ -9,6 +9,7 @@ require 'open3'
require_relative './views/locate_files' require_relative './views/get_mail_list' +require_relative './views/send_account_mail'
configure do set :bind, '0.0.0.0' @@ -37,3 +38,14 @@ get '/get_mail_list/:type' do
return [200, result.to_json] end + +post '/send_account_mail' do + begin + data = JSON.parse(request.body.read) + result = send_account_mail(data) + rescue StandardError => e + return [400, e.backtrace.inspect] + end + + return [200, result.to_json] +end diff --git a/container/assistant/views/send_account_mail.rb b/container/assistant/views/send_account_mail.rb new file mode 100644 index 0000000..b96d082 --- /dev/null +++ b/container/assistant/views/send_account_mail.rb @@ -0,0 +1,20 @@ +# SPDX-License-Identifier: MulanPSL-2.0+ +# # Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. +# # frozen_string_literal: true +# +require_relative "#{ENV['CCI_SRC']}/lib/es_query" +require_relative "#{ENV['CCI_SRC']}/lib/assign_account_client" + +def send_account_mail(user_info) + account_info = ESQuery.new(index: 'accounts').query_by_id(user_info['my_email']) + return {'status'=> true} if account_info + + begin + apply_account = AutoAssignAccount.new(user_info) + apply_account.send_account + rescue + return {'status' => false} + end + + return {'status' => true} +end diff --git a/lib/assign_account_client.rb b/lib/assign_account_client.rb index f551a9d..5aad955 100644 --- a/lib/assign_account_client.rb +++ b/lib/assign_account_client.rb @@ -4,13 +4,13 @@ # frozen_string_literal: true
require 'json' -require 'mail' require 'set' require 'optparse' require 'rest-client' require_relative '../container/defconfig' require_relative 'es_client' require_relative '../container/mail-robot/lib/assign-account-email' +require_relative "#{ENV['CCI_SRC']}/lib/parse_mail_list"
names = Set.new %w[ JUMPER_HOST @@ -32,6 +32,7 @@ class AutoAssignAccount @my_info = user_info
@my_info_es = {} + @account_info = {} end
def update_from_es @@ -59,10 +60,10 @@ class AutoAssignAccount end
def update_my_info_from_account_info - account_info = apply_account + @account_info = apply_account
- @my_info['my_login_name'] = account_info['my_login_name'] - @my_info['my_ssh_pubkey'] << account_info['my_jumper_pubkey'] unless account_info['my_jumper_pubkey'].nil? + @my_info['my_login_name'] = @account_info['my_login_name'] + @my_info['my_ssh_pubkey'] << @account_info['my_jumper_pubkey'] unless @account_info['my_jumper_pubkey'].nil? end
def store_account_info @@ -72,7 +73,9 @@ class AutoAssignAccount
def send_mail @my_info['bisect'] = true - message = build_apply_account_email(@my_info) + default_email = parse_mail_list('delimiter') + @my_info['my_email'] = default_email['to'] unless default_email.empty? + message = build_apply_account_email(@my_info, @account_info, false) %x(curl -XPOST "#{SEND_MAIL_HOST}:#{SEND_MAIL_PORT}/send_mail_text" -d "#{message}") end
diff --git a/lib/assistant_client.rb b/lib/assistant_client.rb index c951f2b..6774e9e 100644 --- a/lib/assistant_client.rb +++ b/lib/assistant_client.rb @@ -21,4 +21,12 @@ class AssistantClient
return JSON.parse(response.body) end + + def send_account_mail(user_info) + resource = RestClient::Resource.new("http://#%7B@host%7D:#%7B@port%7D/send_account_mail") + response = resource.post(user_info.to_json) + return nil unless response.code == 200 + + return JSON.parse(response.body) + end end