[PATCH v2 compass-ci 1/4] container/mail-robot: add handle forwarded my_name/my_email(s)

[why] for forwarded email from account issuer, the email content will contain my_name/my_email(s). a forwarded email may contain multi my_name/my_email(s), just loop them. the forwarded email content may like: --- my_name: name_1 my_email: email_1 account_vm: true/false/yes/no --- my_name: name_2 my_email: email_2 --- the replied 'apply account' email will return login command for account VM server if account_vm has a value: yes/true add store lab to ES Signed-off-by: Luan Shengde <shdluan@163.com> --- container/mail-robot/lib/apply-account.rb | 65 ++++++++++++++++++++--- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/container/mail-robot/lib/apply-account.rb b/container/mail-robot/lib/apply-account.rb index e8288d7..818f900 100755 --- a/container/mail-robot/lib/apply-account.rb +++ b/container/mail-robot/lib/apply-account.rb @@ -55,6 +55,9 @@ class ApplyAccount @es_host = @send_mail_host @es_port = ES_PORT + # email info file for account issuers. + @account_issuer = YAML.load_file '/etc/compass-ci/account-issuer.yaml' + @my_info = {} end @@ -63,11 +66,35 @@ class ApplyAccount # in order to successfully send email for failed parsing # firstly get my_email before execute parse_mail_content is needed @my_info['my_email'] = @mail_content.from[0] - parse_mail_content - apply_my_account - store_account_info - send_mail('') + # for the forwarded email, it contains my_email/my_name .etc. + # all emails from account issuer will be treated as forwarded emails. + # the mail_content may like: + # --- + # my_name: name_1 + # my_name: email_1 + # account_vm: true/false/yes/no + # --- + # my_name: name_2 + # my_email: email_2 + # --- + # the forwarded email allowed to contain multi my_email/my_name(s) + # in this case, we will loop them + if @account_issuer.include? @my_info['my_email'] + users_info = forward_users + users_info.each do |user_info| + # for forwarded email for multi user, avoid rezidual information from the last. + @my_info.clear + assign_account_vm = user_info.delete('account_vm') + @my_info.update user_info + + applying_account(assign_account_vm) + sleep 5 + end + else + parse_mail_content + applying_account(false) + end rescue StandardError => e puts e.message puts e.backtrace @@ -75,6 +102,24 @@ class ApplyAccount send_mail(e.message) end + def forward_users + forward_email_content = ParseApplyAccountEmail.new(@mail_content) + users_info = forward_email_content.extract_users + + users_info.clone.each_index do |i| + users_info[i]['my_ssh_pubkey'] = [] + users_info[i]['lab'] = ENV['lab'] + end + + users_info + end + + def applying_account(assign_account_vm) + account_info = apply_my_account + store_account_info + send_mail('', account_info, assign_account_vm) + end + def parse_mail_content parse_apply_account_email = ParseApplyAccountEmail.new(@mail_content) @@ -93,7 +138,9 @@ class ApplyAccount apply_info['my_token'] = my_account_es['my_uuid'] if my_account_es['my_token'].nil? apply_info.update my_account_es apply_info.update @my_info - apply_info['my_ssh_pubkey'] = (apply_info['my_ssh_pubkey'] + my_ssh_pubkey_new).uniq + if my_ssh_pubkey_new + apply_info['my_ssh_pubkey'] = (apply_info['my_ssh_pubkey'] + my_ssh_pubkey_new).uniq + end @my_info.update apply_info apply_info['is_update_account'] = true apply_info @@ -102,6 +149,8 @@ class ApplyAccount def apply_my_account my_account_es = read_my_account_es apply_info = {} + apply_info['enable_login'] = true + if my_account_es build_apply_info(apply_info, my_account_es) else @@ -109,7 +158,6 @@ class ApplyAccount @my_info['my_token'] = my_token apply_info.update @my_info end - apply_info['lab'] = ENV['lab'] apply_new_account(apply_info, my_account_es) end @@ -118,6 +166,7 @@ class ApplyAccount acct_info = apply_account.apply_jumper_account @my_info['my_login_name'] = acct_info['my_login_name'] unless my_account_es + acct_info end def store_account_info @@ -125,9 +174,9 @@ class ApplyAccount es.put_source_by_id(@my_info['my_email'], @my_info) end - def send_mail(error_message) + def send_mail(error_message, account_info, assign_account_vm) email_message = if error_message.empty? - build_apply_account_email(@my_info) + build_apply_account_email(@my_info, account_info, assign_account_vm) else build_apply_account_fail_email(@my_info, error_message) end -- 2.23.0

On Sat, Feb 20, 2021 at 04:32:33PM +0800, Luan Shengde wrote:
[why] for forwarded email from account issuer, the email content will contain my_name/my_email(s). a forwarded email may contain multi my_name/my_email(s), just loop them.
the forwarded email content may like: --- my_name: name_1 my_email: email_1 account_vm: true/false/yes/no --- my_name: name_2 my_email: email_2
my_email: => my email: 把下划线改成空格,会不会更友好一点? 如果保持_的话,也行,建议把 my oss commit: 也改成下划线的形式 my_oss_commit: 全做成变量名形式,保持一致性。 Thanks, Fengguang

On Sat, Feb 20, 2021 at 04:43:57PM +0800, Wu Fengguang wrote:
On Sat, Feb 20, 2021 at 04:32:33PM +0800, Luan Shengde wrote:
[why] for forwarded email from account issuer, the email content will contain my_name/my_email(s). a forwarded email may contain multi my_name/my_email(s), just loop them.
the forwarded email content may like: --- my_name: name_1 my_email: email_1 account_vm: true/false/yes/no --- my_name: name_2 my_email: email_2
my_email: => my email:
把下划线改成空格,会不会更友好一点?
好的, 可以使用空格。 这样的话,在解析的时候加一层转换: my email =》 my_email my name =》 my_name 这样直接update到my_info中。 修改‘my oss commit’ 的话, 文档也需要进行修改。 同意使用空格吧。 我稍后提交新的版本 Thanks, Luan Shengde
如果保持_的话,也行,建议把 my oss commit: 也改成下划线的形式 my_oss_commit: 全做成变量名形式,保持一致性。
Thanks, Fengguang

On Sat, Feb 20, 2021 at 05:34:45PM +0800, Luan Shengde wrote:
On Sat, Feb 20, 2021 at 04:43:57PM +0800, Wu Fengguang wrote:
On Sat, Feb 20, 2021 at 04:32:33PM +0800, Luan Shengde wrote:
[why] for forwarded email from account issuer, the email content will contain my_name/my_email(s). a forwarded email may contain multi my_name/my_email(s), just loop them.
the forwarded email content may like: --- my_name: name_1 my_email: email_1 account_vm: true/false/yes/no --- my_name: name_2 my_email: email_2
my_email: => my email:
把下划线改成空格,会不会更友好一点?
好的, 可以使用空格。 这样的话,在解析的时候加一层转换: my email =》 my_email my name =》 my_name
噢多了一层转换。 如果全用_的形式,就在更广泛的意义上一致了。 那样用 my_name: xxx my_email: xxx my_oss_commit: xxx 也是蛮好的。 Thanks, Fengguang
这样直接update到my_info中。
修改‘my oss commit’ 的话, 文档也需要进行修改。
同意使用空格吧。 我稍后提交新的版本
Thanks, Luan Shengde
如果保持_的话,也行,建议把 my oss commit: 也改成下划线的形式 my_oss_commit: 全做成变量名形式,保持一致性。
Thanks, Fengguang

好的, 可以使用空格。 这样的话,在解析的时候加一层转换: my email =》 my_email my name =》 my_name
噢多了一层转换。 如果全用_的形式,就在更广泛的意义上一致了。 那样用
my_name: xxx my_email: xxx my_oss_commit: xxx
也是蛮好的。
OK. I will optimize it. my oss commit => my_oss_commit Thanks, Luan Shengde
Thanks, Fengguang
这样直接update到my_info中。
修改‘my oss commit’ 的话, 文档也需要进行修改。
同意使用空格吧。 我稍后提交新的版本
Thanks, Luan Shengde
如果保持_的话,也行,建议把 my oss commit: 也改成下划线的形式 my_oss_commit: 全做成变量名形式,保持一致性。
Thanks, Fengguang
participants (2)
-
Luan Shengde
-
Wu Fengguang