get email content for mail-robot.rb check email available check base url in upstream-repos check commit url valid check pub_key return if no pub_key assign account generate uuid write account/uuid to es send uuid to user
Signed-off-by: Luan Shengde luanshengde2@huawei.com --- container/mail-robot/answerback-email.rb | 201 +++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100755 container/mail-robot/answerback-email.rb
diff --git a/container/mail-robot/answerback-email.rb b/container/mail-robot/answerback-email.rb new file mode 100755 index 0000000..6f21358 --- /dev/null +++ b/container/mail-robot/answerback-email.rb @@ -0,0 +1,201 @@ +#!/usr/bin/env ruby +# SPDX-License-Identifier: MulanPSL-2.0+ +# frozen_string_literal: true + +=begin + +repo_list: + all repos url list from upstream-repos.git + +API: +call graph: +read_mail_content +send_account_request + email_addr + get email address + email_message_id + get email message_id + check_email_available + check email available + pub_key_value + get pub key + uuid + get jumper server account + generate uuid + mail_message + uuid + submit steps + store account_info + call es_client + send_mail + send mail with mail_message + +the returned data for apply ssh account: +{ + "account" => "guest", + "passwd" => "Use pub_key to login", + "jumper_ip" => "10.10.10.10", + "jumper_port" => "10000" +} + +=end + +require 'json' +require 'mail' +require_relative '/compass-ci/lib/es_client.rb' + +def build_message(to, message_id, account_uuid) + message = <<~EMAIL_MESSAGE + To: #{to} + Message-ID: #{message_id} + Subject: Account Ready + + Dear #{to.split('@')[0]} + + Thank you for joining us. + You can use the following info to submit jobs: + + First add the info to: ~/.config/compass-ci/defaults/{USER}.yaml + + uuid: #{account_uuid} + lab: z9 + + Second: download lkp-tests and dependencies + git clone https://gitee.com_/wu_fengguang/lkp-tests.git + cd lkp-tests + make install + source {HOME}/.bashrc && source {HOME}/.bash_profile + + Third: submit job + reference: https://gitee.com/wu_fengguang/compass-ci/blob/master/doc/tutorial.md + how to write job yaml + submit jobs/netperf.yaml + + Repalce the fields {...} with your local environment variable. + + regards + compass-ci + EMAIL_MESSAGE + + return message +end + +def email_addr(mail_content) + email = mail_content.from[0] + + return email +end + +def url_addr(mail_content) + url_line = mail_content.part[0].body.decoded.split(/\n/).find { |line| line =~ %r{https?://} } + message = 'No commit url found' + + raise message unless url_line + + url = url_line.split[-1] + + return url +end + +def base_url_addr(url) + url_a = url.split('/') + url_a.pop 2 + base_url = url_a.join('/') + + return base_url +end + +def check_email_available(mail_content, email) + url = url_addr(mail_content) + + base_url = base_url_addr(url) + + upstream_dir = '/upstream-repos' + check_url_in_upstream_repos(upstream_dir, base_url) + check_url_commit(url, email) + + return url +end + +def check_url_in_upstream_repos(upstream_dir, base_url) + Dir.chdir(upstream_dir) + match_out = %x(grep -rn #{base_url}) + + message = 'The url is not in upstream-repo list' + + raise StandardError, message, base_url if match_out.empty? +end + +def check_url_commit(url, email) + url_fdback = %x(curl #{url}) + email_index = url_fdback.index email + + message = 'No email info found from the url feedback.' + raise StandardError, message, url if email_index.nil? +end + +def email_message_id(mail_content) + message_id = mail_content.message_id + return message_id +end + +def pub_key_value(mail_content) + pub_key = mail_content.part[1].body.decoded.split(/\r|\n/).join if mail_content.part[1].filename == 'id_rsa.pub' + + message = 'No pub_key fount' + + raise message unless pub_key + + return pub_key +end + +def account_info(pub_key) + account_info_str = %x(curl -XGET "#{ENV['JUMPER_IP']}:#{ENV['JUMPER_PORT']}/assign_account" -d "pub_key: #{pub_key}") + account_info = JSON.parse account_info_str + + message = 'No available jumper account returned.' + raise StandardError, message, account_info if account_info['account'].empty? + + return account_info +end + +def setup_account_uuid + account_uuid = %x(uuidgen).chomp + + return account_uuid +end + +def send_account(mail_content) + email = email_addr(mail_content) + message_id = email_message_id(mail_content) + url = check_email_available(mail_content, email) + pub_key = pub_key_value(mail_content) + acct_infos = account_info(pub_key) + account_uuid = setup_account_uuid + + store_account_info(email, account_uuid, acct_infos, url) + + message = build_message(email, message_id, account_uuid) + send_mail_server = `/sbin/ip route |awk '/default/ {print $3}'`.chomp + + %x(curl -XPOST "#{send_mail_server}:#{ENV['SEND_MAIL_PORT']}/send_mail_text" -d "#{message}") +end + +def store_account_info(to, account_uuid, acct_infos, url) + account_uuid_info = { + 'email' => to, + 'name' => to.split('@')[0], + 'uuid' => account_uuid, + 'user' => acct_infos['account'], + 'my_commit_url' => url + } + + es = ESClient.new(index: 'accounts') + es.put_source_by_id(to, account_uuid_info) +end + +def read_mail_content(mail_file) + mail_content = Mail.read(mail_file) + + return mail_content +end
+# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
+=begin +read_mail_content +send_account_request
- email_addr
- get email address
- email_message_id
- get email message_id
- check_email_available
- check email available
- pub_key_value
- get pub key
- uuid
- get jumper server account
- generate uuid
- mail_message
- uuid
- submit steps
- store account_info
- call es_client
- send_mail
- send mail with mail_message
+=end
why list a bunch of function names in comment.
- message = 'No pub_key fount'
fount -> found
Thanks, Yinsi