On Wed, Nov 04, 2020 at 04:27:28PM +0800, Yu Chuan wrote:
This patch looks have three functions, it's too large to review, so how about split this big patch to three small patch?
yeah, I will separate it later.
Thanks, Luan Shengde
Thanks Yu Chuan
On Wed, Nov 04, 2020 at 03:25:36PM +0800, Luan Shengde wrote:
disable use golbal variable apply_info [why]: rubocop: Style/GlobalVars: Do not introduce global variables. [how]: use local variables instead of global variables
fix OptionParser argument: [why]: when write this: opts.on('-e|--email email_address', 'appoint email address') will lead to could not identify the options ./answerback-email.rb --email xxx@163.com ./answerback-email.rb:72:in `<main>': invalid option: --email (OptionParser::InvalidOption) [how]: write seprately like: opts.on('-e email_address', '--email email_address', 'appoint email address')
add transfer user info when execute apply account command [why]: when applying account, the assign-account service need to write the my info to user's default config file ~/.config/compass-ci/defaults/account.yaml my info:
- my_email
- my_name
- my_uuid
[how]: transfer the user info along with the pub_key when sending apply account request
Signed-off-by: Luan Shengde shdluan@163.com
container/assign-account/answerback-email.rb | 92 ++++++++++++-------- 1 file changed, 55 insertions(+), 37 deletions(-)
diff --git a/container/assign-account/answerback-email.rb b/container/assign-account/answerback-email.rb index bb8e809..bda627d 100755 --- a/container/assign-account/answerback-email.rb +++ b/container/assign-account/answerback-email.rb @@ -12,58 +12,65 @@ require 'mail' require 'set' require 'optparse' require_relative '../defconfig' +require_relative '../../lib/es_client'
names = Set.new %w[ JUMPER_HOST JUMPER_PORT
- SEND_MAIL_HOST_INTERNET
- SEND_MAIL_PORT_INTERNET
- SEND_MAIL_HOST
- SEND_MAIL_PORT
]
defaults = relevant_defaults(names)
JUMPER_HOST = defaults['JUMPER_HOST'] || 'api.compass-ci.openeuler.org' JUMPER_PORT = defaults['JUMPER_PORT'] || 29999 -SEND_MAIL_HOST = defaults['SEND_MAIL_HOST_INTERNET'] || 'localhost' -SEND_MAIL_PORT = defaults['SEND_MAIL_PORT_INTERNET'] || 11312 +SEND_MAIL_HOST = defaults['SEND_MAIL_HOST'] || 'localhost' +SEND_MAIL_PORT = defaults['SEND_MAIL_PORT'] || 49000
-$apply_info = { +apply_info = { 'my_email' => nil,
- 'my_name' => nil,
- 'my_uuid' => %x(uuidgen).chomp, 'my_ssh_pubkey' => nil
}
-def init_info(email_file) +def init_info(email_file, apply_info) mail_content = Mail.read(email_file)
- apply_info['my_email'] = mail_content.from[0]
- apply_info['my_name'] = mail_content.From.unparsed_value.gsub(/ <[^<>]*>/, '')
- apply_info['my_ssh_pubkey'] = if mail_content.part[1].filename == 'id_rsa.pub'
mail_content.part[1].body.decoded
end
- $apply_info['my_email'] = mail_content.from[0]
- $apply_info['my_ssh_pubkey'] = if mail_content.part[1].filename == 'id_rsa.pub'
mail_content.part[1].body.decoded.gsub(/\r|\n/, '')
end
- $apply_info
- apply_info
end
options = OptionParser.new do |opts|
- opts.banner = "Usage: answerback-mail.rb [--email email] [--ssh-pubkey pub_key_file] [--raw-email email_file]\n"
opts.banner = 'Usage: answerback-mail.rb [-e|--email email] '
opts.banner += "[-s|--ssh-pubkey pub_key_file] [-f|--raw-email email_file]\n" opts.banner += " -e or -f is required\n" opts.banner += ' -s is optional when use -e'
opts.separator '' opts.separator 'options:'
- opts.on('-e|--email email_address', 'appoint email address') do |email_address|
- $apply_info['my_email'] = email_address
- opts.on('-e email_address', '--email email_address', 'appoint email address') do |email_address|
- apply_info['my_email'] = email_address
- # when apply account with email address, will get no user name
- apply_info['my_name'] = '' end
- opts.on('-s|--ssh-pubkey pub_key_file', 'ssh pub_key file, enable password-less login') do |pub_key_file|
- $apply_info['my_ssh_pubkey'] = File.read(pub_key_file)
- opts.on('-s pub_key_file', '--ssh-pubkey pub_key_file', \
'ssh pub_key file, enable password-less login') do |pub_key_file|
- apply_info['my_ssh_pubkey'] = File.read(pub_key_file) end
- opts.on('-f|--raw-email email_file', 'email file') do |email_file|
- init_info(email_file)
- opts.on('-f email_file', '--raw-email email_file', 'email file') do |email_file|
- init_info(email_file, apply_info) end
- opts.on_tail('-h|--help', 'show this message') do
- opts.on_tail('-h', '--help', 'show this message') do puts opts exit end
@@ -71,10 +78,10 @@ end
options.parse!(ARGV)
-def build_message(email, acct_infos) +def build_message(email, account_info) message = <<~EMAIL_MESSAGE To: #{email}
- Subject: jumper account is ready
Subject: [compass-ci] jumper account is ready
Dear user:
@@ -82,10 +89,10 @@ def build_message(email, acct_infos) You can use the following command to login the jumper server:
login command:
ssh -p #{acct_infos['jumper_port']} #{acct_infos['account']}@#{acct_infos['jumper_ip']}
ssh -p #{account_info['jumper_port']} #{account_info['my_login_name']}@#{account_info['jumper_host']} account password:
#{acct_infos['passwd']}
#{account_info['my_password']}
regards compass-ci
@@ -94,26 +101,37 @@ def build_message(email, acct_infos) return message end
-def account_info(pub_key)
- account_info_str = if pub_key.nil?
%x(curl -XGET '#{JUMPER_HOST}:#{JUMPER_PORT}/assign_account')
else
%x(curl -XGET '#{JUMPER_HOST}:#{JUMPER_PORT}/assign_account' -d "pub_key: #{pub_key}")
end
+def apply_account(apply_info)
- account_info_str = %x(curl -XGET '#{JUMPER_HOST}:#{JUMPER_PORT}/assign_account' -d '#{apply_info.to_json}') JSON.parse account_info_str
end
-def send_account +def send_account(apply_info) message = "No email address specified\n" message += "use -e email_address add a email address\n" message += 'or use -f to add a email file'
- raise message if $apply_info['my_email'].nil?
- acct_info = account_info($apply_info['my_ssh_pubkey'])
- message = build_message($apply_info['my_email'], acct_info)
raise message if apply_info['my_email'].nil?
account_info = apply_account(apply_info)
my_info = {
'my_email' => apply_info['my_email'],
'my_name' => apply_info['my_name'],
# there is no need to add a commit url for this tool
# the es has the key: my_commit_url
'my_commit_url' => '',
'my_login_name' => account_info['my_login_name'],
'my_uuid' => apply_info['my_uuid']
}
store_account_info(my_info)
message = build_message(apply_info['my_email'], account_info)
%x(curl -XPOST '#{SEND_MAIL_HOST}:#{SEND_MAIL_PORT}/send_mail_text' -d "#{message}")
end
-send_account +def store_account_info(my_info)
- es = ESClient.new(index: 'accounts')
- es.put_source_by_id(my_info['my_email'], my_info)
+end
+send_account(apply_info)
2.23.0