apply-account.rb apply-account-component.rb 两者分开的依据是什么?也就是说,怎么判断什么代码进这个文件,什么代码进那个文件?
每个类都跑在哪里? 哪个服务和哪个机器?
Thanks, Fengguang
On Mon, Nov 02, 2020 at 07:09:37PM +0800, Luan Shengde wrote:
ParseApplyAccountEmail parse commit url and pub_key check out commit url check commit url exists check base url in upstream-repos check commit available gitee.com clone the repo and check the commit exists non-gitee.com check the feedback of curl command to check the commit url available check out pub_key extract the attachment of the pub_key
ApplyAccount apply account with apply_account_info check account
SendMail send uuid email build email message send email send error email build error email message send error email
Signed-off-by: Luan Shengde luanshengde2@huawei.com
.../mail-robot/apply-account-component.rb | 232 ++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100755 container/mail-robot/apply-account-component.rb
diff --git a/container/mail-robot/apply-account-component.rb b/container/mail-robot/apply-account-component.rb new file mode 100755 index 0000000..98272e7 --- /dev/null +++ b/container/mail-robot/apply-account-component.rb @@ -0,0 +1,232 @@ +#!/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'
+# check whether there is a commit url and ssh pub_key in the email +# apply account on junper server +class ParseApplyAccountEmail
- def initialize(mail_content)
- @mail_content = mail_content
- @my_email = mail_content.from[0]
- @my_name = mail_content.From.unparsed_value.gsub(/ <[^<>]*>/, '')
- end
- def parse_commit_url_pub_key
- commit_url = check_out_commit_url
- pub_key = check_out_pub_key
- return commit_url, pub_key
- end
- def send_error_email(error_message)
- send_error_email_info = {
'my_email' => @my_email,
'my_name' => @my_name,
'my_info' => '',
'error_message' => error_message
- }
- send_err_mail = SendMail.new(send_error_email_info)
- send_err_mail.send_error_email
- end
- def extract_commit_url
- mail_content_body = @mail_content.part[0].part[0].body.decoded || @mail_content.part[0].body.decoded
- mail_content_line = mail_content_body.gsub(/\n/, '')
- no_commit_url unless mail_content_line.match?(%r{my oss commit:\s*https?://[^/]*/[^/]*/[^/]*/commit/[\w\d]{40}})
- mail_content_body.match(%r{https?://[^/]*/[^/]*/[^/]*/commit/[\w\d]{40}})[0]
- end
- def no_commit_url
- error_message = 'No matched commit url found'
- send_error_email(error_message)
- raise error_message
- end
- def check_out_commit_url
- url = extract_commit_url
- base_url = url.gsub(%r{/commit/[\w\d]{40}$}, '')
- check_base_url_in_upstream_repos('/c/upstream-repos', base_url)
- check_commit_available(url, base_url)
- return url
- end
- def check_base_url_in_upstream_repos(upstream_dir, base_url)
- Dir.chdir(upstream_dir)
- match_out = %x(grep -rn #{base_url})
- return unless match_out.empty?
- error_message = 'The url is not in upstream-repo list'
- send_error_email(error_message)
- raise error_message
- end
- def check_commit_available(url, base_url)
- hub_name = url.split('/')[2]
- # it requires authentication when execute curl to get the commit infomation
- # clone the repo and then validate the commit for the email address
- if hub_name.eql? 'gitee.com'
check_gitee_commit(url, base_url)
- else
check_non_gitee_commit(url)
- end
- end
- def check_non_gitee_commit(url)
- url_fdback = %x(curl #{url})
- email_index = url_fdback.index @my_email
- return unless email_index.nil?
- error_message = 'Your commit url is not a valid commit url.'
- send_error_email(error_message)
- raise error_message
- end
- def check_gitee_commit(url, base_url)
- repo_dir = url.split('/')[-3]
- repo_url = [base_url, 'git'].join('.')
- commit_id = url.split('/')[-1]
- Dir.chdir ENV['ROBOT_TMP_REPOS_DIR']
- %x(/usr/bin/git clone #{repo_url} #{repo_dir})
- email_index = %x(/usr/bin/git -C #{repo_dir} show #{commit_id}).index @my_email
- FileUtils.rm_rf repo_dir
- check_gitee_commit_exist(email_index)
- end
- def check_gitee_commit_exist(email_index)
- return unless email_index.nil?
- error_message = 'Your commit url is not a valid commit url.'
- send_error_email(error_message)
- raise error_message
- end
- def check_out_pub_key
- pub_key = @mail_content.part[1].body.decoded if @mail_content.part[1].filename == 'id_rsa.pub'
- check_pub_key(pub_key)
- return pub_key
- end
- def check_pub_key(pub_key)
- return unless pub_key.nil?
- error_message = 'No pub_key found'
- send_error_email(error_message)
- raise error_message
- end
+end
+# apply jumper account for user +class ApplyAccount
- def initialize(apply_account_info)
- @apply_account_info = apply_account_info
- end
- def apply_jumper_account
- account_info_str = %x(curl -XGET "#{ENV['JUMPER_HOST']}:#{ENV['JUMPER_PORT']}/assign_account" \
-d '#{@apply_account_info.to_json}')
- account_info = JSON.parse account_info_str
- check_account_info(account_info)
- return account_info
- end
- def check_account_info(account_info)
- return unless account_info['account'].nil?
- error_message = 'No more available jumper account.'
- send_error_email(error_message)
- raise error_message
- end
+end
+# build mail data and send mail +class SendMail
- def initialize(send_mail_info)
- @send_mail_host = %x(/sbin/ip route |awk '/default/ {print $3}').chomp
- @my_email = send_mail_info['my_email']
- @my_name = send_mail_info['my_name']
- @error_message = send_mail_info['error_message']
- @my_info = send_mail_info['my_info']
- end
- def send_uuid_email
- email_msg = <<~EMAIL_MESSAGE
To: #{@my_email}
Subject: [compass-ci] Account Ready
Dear #{@my_name},
Thank you for joining us.
You can use the following info to submit jobs:
1) setup default config
run the following command to add the below setup to default config file
mkdir -p ~/.config/compass-ci/defaults/
cat >> ~/.config/compass-ci/defaults/\\${USER}.yaml <<-EOF
my_uuid: #{@my_info['my_uuid']}
my_email: #{@my_info['my_email']}
my_name: #{@my_info['my_name']}
EOF
2) download lkp-tests and dependencies
run the following command to install the lkp-test and effect the configuration
git clone https://gitee.com/wu_fengguang/lkp-tests.git
cd lkp-tests
make install
source ${HOME}/.bashrc && source ${HOME}/.bash_profile
3) submit job
reference: https://gitee.com/wu_fengguang/compass-ci/blob/master/doc/tutorial.md
find how to write job yaml
submit jobs/netperf.yaml
regards
compass-ci
- EMAIL_MESSAGE
- send_mail(email_msg)
- end
- def send_error_email
- email_msg = <<~EMAIL_MESSAGE
To: #{@my_email}
Subject: apply account failed
Dear #{@my_name}
Your application for account failed with following errors
#{@error_message}
regards
compass-ci
- EMAIL_MESSAGE
- send_mail(email_msg)
- end
- def send_mail(email_msg)
- %x(curl -XPOST "#{@send_mail_host}:#{ENV['SEND_MAIL_PORT']}/send_mail_text" -d "#{email_msg}")
- end
+end
2.23.0