ApplyAccount check to apply account for user when listened new email with subject 'apply account' input: mail_content
send_account parse email_content parse my_commit_url parse my_ssh_pubkey apply_my_account generate uuid apply account with my_info and my_ssh_pubkey my_info: - my_email - my_name - my_uuid 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 --- container/mail-robot/lib/apply-account.rb | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100755 container/mail-robot/lib/apply-account.rb
diff --git a/container/mail-robot/lib/apply-account.rb b/container/mail-robot/lib/apply-account.rb new file mode 100755 index 0000000..1b0f2ad --- /dev/null +++ b/container/mail-robot/lib/apply-account.rb @@ -0,0 +1,113 @@ +#!/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 '../../../lib/es_client' +require_relative 'assign-account-email' +require_relative 'assign-account-fail-eamil' +require_relative 'apply-jumper-account' +require_relative 'parse-apply-account-email' + +SEND_MAIL_PORT = ENV['SEND_MAIL_PORT'] || 49000 + +# assign uuid/account for user +# when mail-robot listened new email, and the email's subject +# exactly equal 'apply account', mail-robot will call this class +# entry point: send_account +# input: email's content +# +# send_account +# parse_commit_url_pub_key +# call ParseApplyAccountEmail to parse: +# - my_commit_url +# - my_ssh_pubkey +# apply_my_account +# call ApplyJumperAccount to apply new account +# required data: +# my_info: +# - my_email +# - my_name +# - my_uuid +# my_ssh_pubkey +# store_account_info +# call ESClient to store my_info +# my_info: +# - my_email +# - my_name +# - my_uuid +# - my_commit_url +# - my_login_name +# - my_ssh_pubkey +# send_mail +# when successfully applied an account +# call build_apply_account_email to send a successful email +# when rescued error message +# call build_apply_account_fail_email to send fail email +class ApplyAccount + 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(/ <[^<>]*>/, ''), + 'my_ssh_pubkey' => [] + } + end + + def check_to_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'] + @my_info['my_ssh_pubkey'] << my_ssh_pubkey + + 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(error_message) + email_message = if error_message.empty? + build_apply_account_email(@my_info) + else + build_apply_account_fail_email(@my_info, error_message) + end + + %x(curl -XPOST "#{@send_mail_host}:#{@send_mail_port}/send_mail_text" -d "#{email_message}") + end +end
On Fri, Nov 13, 2020 at 06:25:00PM +0800, Luan Shengde wrote:
ApplyAccount check to apply account for user when listened new email with subject 'apply account' input: mail_content
send_account parse email_content parse my_commit_url parse my_ssh_pubkey apply_my_account generate uuid apply account with my_info and my_ssh_pubkey my_info: - my_email
- my_name
- my_uuid
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
container/mail-robot/lib/apply-account.rb | 113 ++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100755 container/mail-robot/lib/apply-account.rb
diff --git a/container/mail-robot/lib/apply-account.rb b/container/mail-robot/lib/apply-account.rb new file mode 100755 index 0000000..1b0f2ad --- /dev/null +++ b/container/mail-robot/lib/apply-account.rb @@ -0,0 +1,113 @@ +#!/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 '../../../lib/es_client' +require_relative 'assign-account-email' +require_relative 'assign-account-fail-eamil' +require_relative 'apply-jumper-account' +require_relative 'parse-apply-account-email'
+SEND_MAIL_PORT = ENV['SEND_MAIL_PORT'] || 49000
+# assign uuid/account for user +# when mail-robot listened new email, and the email's subject +# exactly equal 'apply account', mail-robot will call this class +# entry point: send_account +# input: email's content +# +# send_account +# parse_commit_url_pub_key +# call ParseApplyAccountEmail to parse: +# - my_commit_url +# - my_ssh_pubkey +# apply_my_account +# call ApplyJumperAccount to apply new account +# required data: +# my_info: +# - my_email +# - my_name +# - my_uuid +# my_ssh_pubkey +# store_account_info +# call ESClient to store my_info +# my_info: +# - my_email +# - my_name +# - my_uuid +# - my_commit_url +# - my_login_name +# - my_ssh_pubkey +# send_mail +# when successfully applied an account +# call build_apply_account_email to send a successful email +# when rescued error message +# call build_apply_account_fail_email to send fail email +class ApplyAccount
- 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(/ <[^<>]*>/, ''),
Better put all mail_content related code to ParseApplyAccountEmail And let ParseApplyAccountEmail have a single function to return my_info hash. Including the below my_commit_url, my_ssh_pubkey fields.
'my_ssh_pubkey' => []
- }
- end
- def check_to_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']
- @my_info['my_ssh_pubkey'] << my_ssh_pubkey
Thanks, Fengguang
- @my_info = {
'my_email' => mail_content.from[0],
'my_name' => mail_content.From.unparsed_value.gsub(/ <[^<>]*>/, ''),
Better put all mail_content related code to ParseApplyAccountEmail And let ParseApplyAccountEmail have a single function to return my_info hash. Including the below my_commit_url, my_ssh_pubkey fields.
OK, I will optimize it
Thanks, Luan Shengde
'my_ssh_pubkey' => []
- }
- end
- def check_to_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']
- @my_info['my_ssh_pubkey'] << my_ssh_pubkey
Thanks, Fengguang