AssignAccount assign account for user required data: mail_content - send_account parse email_content parse commit url parse pubkey apply_my_account generate uuid apply account store account info to es send email send success email send fail email
usage: reference mail-robot.rb
Signed-off-by: Luan Shengde shdluan@163.com --- lib/assign-account.rb | 78 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100755 lib/assign-account.rb
diff --git a/lib/assign-account.rb b/lib/assign-account.rb new file mode 100755 index 0000000..5521ae6 --- /dev/null +++ b/lib/assign-account.rb @@ -0,0 +1,78 @@ +#!/usr/bin/env ruby +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. +# frozen_string_literal: true + +require 'json' +require 'mail' +require_relative 'es_client' + +# assign uuid/account for user +# send_account +# parse my_commit_url and my_ssh_pubkey +# apply account +# store account info +# send mail +class AssignAccount + def initialize(mail_content) + @send_mail_host = %x(/sbin/ip route | awk '/default/ {print $3}').chomp + @send_mail_port = SEND_MAIL_PORT + @mail_content = mail_content + + @my_info = { + 'my_email' => mail_content.from[0], + 'my_name' => mail_content.From.unparsed_value.gsub(/ <[^<>]*>/, '') + } + end + + def send_account + my_commit_url, my_ssh_pubkey = parse_commit_url_pub_key + acct_info = apply_my_account(my_ssh_pubkey) + + @my_info['my_commit_url'] = my_commit_url + @my_info['my_login_name'] = acct_info['my_login_name'] + + store_account_info + send_mail('') + rescue StandardError => e + puts e.message + puts e.backtrace + + send_mail(e.message) + end + + def parse_commit_url_pub_key + parse_apply_account_email = ParseApplyAccountEmail.new(@mail_content) + + my_commit_url = parse_apply_account_email.parse_commit_url + my_ssh_pubkey = parse_apply_account_email.parse_pub_key + + return my_commit_url, my_ssh_pubkey + end + + def apply_my_account(my_ssh_pubkey) + my_uuid = %x(uuidgen).chomp + + @my_info['my_uuid'] = my_uuid + + apply_account = ApplyJumperAccount.new(@my_info, my_ssh_pubkey) + acct_info = apply_account.apply_jumper_account + + return acct_info + end + + def store_account_info + es = ESClient.new(index: 'accounts') + es.put_source_by_id(@my_info['my_email'], @my_info) + end + + def send_mail(message) + email_message = if message.empty? + build_apply_account_email(@my_info) + else + build_apply_account_fail_email(@my_info, message) + end + + %x(curl -XPOST "#{@send_mail_host}:#{@send_mail_port}/send_mail_text" -d "#{email_message}") + end +end
- def send_mail(message)
- email_message = if message.empty?
build_apply_account_email(@my_info)
else
build_apply_account_fail_email(@my_info, message)
end
This style is strange.
You can define a method
def get_email_message(message) return build_apply_account_email(@my_info) if message.empty?
return build_apply_account_fail_email(@my_info, message) end
email_message = get_email_message(message)
Thanks, Yuanchao
- %x(curl -XPOST "#{@send_mail_host}:#{@send_mail_port}/send_mail_text" -d "#{email_message}")
- end
+end
2.23.0
On Wed, Nov 11, 2020 at 05:11:59PM +0800, Li Yuanchao wrote:
- def send_mail(message)
- email_message = if message.empty?
build_apply_account_email(@my_info)
else
build_apply_account_fail_email(@my_info, message)
end
This style is strange.
I think this still works well or
if error_message.empty? email_message = build_apply_account_email(@my_info) else email_message = build_apply_account_fail_email(@my_info, error_message) end
works
Thanks, Luan Shengde
You can define a method
def get_email_message(message) return build_apply_account_email(@my_info) if message.empty?
return build_apply_account_fail_email(@my_info, message) end
email_message = get_email_message(message)
Thanks, Yuanchao
- %x(curl -XPOST "#{@send_mail_host}:#{@send_mail_port}/send_mail_text" -d "#{email_message}")
- end
+end
2.23.0
On Wed, Nov 11, 2020 at 05:11:59PM +0800, Li Yuanchao wrote:
- def send_mail(message)
- email_message = if message.empty?
build_apply_account_email(@my_info)
else
build_apply_account_fail_email(@my_info, message)
end
This style is strange.
this style is suggested format for rubocop.
Thanks, Luan Shengde
You can define a method
def get_email_message(message) return build_apply_account_email(@my_info) if message.empty?
return build_apply_account_fail_email(@my_info, message) end
email_message = get_email_message(message)
Thanks, Yuanchao
- %x(curl -XPOST "#{@send_mail_host}:#{@send_mail_port}/send_mail_text" -d "#{email_message}")
- end
+end
2.23.0