add -r|--gen-sshkey option for answerback-email
[why]
enable one click login testbox from jumper server
[how]
add -r|--rsa-key option to enable generate private/public key on jumper server
register the public key to es
Signed-off-by: Luan Shengde <shdluan(a)163.com>
---
container/assign-account/answerback-email.rb | 77 ++++++++++----------
1 file changed, 38 insertions(+), 39 deletions(-)
diff --git a/container/assign-account/answerback-email.rb b/container/assign-account/answerback-email.rb
index 686a327..39091c7 100755
--- a/container/assign-account/answerback-email.rb
+++ b/container/assign-account/answerback-email.rb
@@ -7,12 +7,23 @@
# samba mount
# ssh logshn (huawei, ) (install pubkey / send password)
+# 1) external user: email apply => mail robot
+# 2) ./answerback-email: huawei user: cli apply
+# => params
+# => REST api (params different)
+# => robot email notify
+
+# user_type:
+# apply_from: mail/cli
+# allow_login:
+
require 'json'
require 'mail'
require 'set'
require 'optparse'
require_relative '../defconfig'
require_relative '../../lib/es_client'
+require_relative 'send-account-email'
names = Set.new %w[
JUMPER_HOST
@@ -31,17 +42,18 @@ SEND_MAIL_PORT = defaults['SEND_MAIL_PORT'] || 49000
my_info = {
'my_email' => nil,
'my_name' => nil,
+ 'my_commit_url' => nil,
'my_uuid' => %x(uuidgen).chomp,
- 'my_ssh_pubkey' => nil
+ 'my_ssh_pubkey' => [],
+ 'gen_sshkey' => false
}
-def init_info(email_file, my_info)
- mail_content = Mail.read(email_file)
+def init_info(mail_content, my_info)
my_info['my_email'] = mail_content.from[0]
my_info['my_name'] = mail_content.From.unparsed_value.gsub(/ <[^<>]*>/, '')
- my_info['my_ssh_pubkey'] = if mail_content.part[1].filename == 'id_rsa.pub'
- mail_content.part[1].body.decoded
- end
+ my_info['my_ssh_pubkey'] << if mail_content.part[1].filename == 'id_rsa.pub'
+ mail_content.part[1].body.decoded
+ end
end
options = OptionParser.new do |opts|
@@ -55,17 +67,23 @@ options = OptionParser.new do |opts|
opts.on('-e email_address', '--email email_address', 'appoint email address') do |email_address|
my_info['my_email'] = email_address
- # when apply account with email address, will get no user name
+ # when apply account with email address, will get no user name and commit url
my_info['my_name'] = ''
+ my_info['my_commit_url'] = ''
end
opts.on('-s pub_key_file', '--ssh-pubkey pub_key_file', \
'ssh pub_key file, enable password-less login') do |pub_key_file|
- my_info['my_ssh_pubkey'] = File.read(pub_key_file)
+ my_info['my_ssh_pubkey'] << File.read(pub_key_file).chomp
end
opts.on('-f email_file', '--raw-email email_file', 'email file') do |email_file|
- init_info(email_file, my_info)
+ mail_content = Mail.read(email_file)
+ init_info(mail_content, my_info)
+ end
+
+ opts.on('-g', '--gen-sshkey', 'generate jumper ras public/private key and return pubkey') do
+ my_info['gen_sshkey'] = true
end
opts.on_tail('-h', '--help', 'show this message') do
@@ -76,32 +94,6 @@ end
options.parse!(ARGV)
-def build_message(email, account_info)
- message = <<~EMAIL_MESSAGE
- To: #{email}
- Subject: [compass-ci] jumper account is ready
-
- Dear user:
-
- Thank you for joining us.
- You can use the following command to login the jumper server:
-
- Login command:
- ssh -p #{account_info['jumper_port']} #{account_info['my_login_name']}@#{account_info['jumper_host']}
-
- Account password:
- #{account_info['my_password']}
-
- Suggest:
- If you use the password to login, change it in time.
-
- regards
- compass-ci
- EMAIL_MESSAGE
-
- return message
-end
-
def apply_account(my_info)
account_info_str = %x(curl -XGET '#{JUMPER_HOST}:#{JUMPER_PORT}/assign_account' -d '#{my_info.to_json}')
JSON.parse account_info_str
@@ -114,12 +106,19 @@ def send_account(my_info)
raise message if my_info['my_email'].nil?
account_info = apply_account(my_info)
- # for manually assign account, there will be no my_commit_url
- # but the key my_commit_url is required for es
- my_info['my_commit_url'] = ''
my_info['my_login_name'] = account_info['my_login_name']
- my_info.delete 'my_ssh_pubkey'
+
+ unless account_info['my_jumper_pubkey'].nil?
+ my_info['my_ssh_pubkey'] << account_info['my_jumper_pubkey'].chomp
+ end
+
+ my_info.delete 'gen_sshkey'
store_account_info(my_info)
+
+ send_mail(my_info, account_info)
+end
+
+def send_mail(my_info, account_info)
message = build_message(my_info['my_email'], account_info)
%x(curl -XPOST '#{SEND_MAIL_HOST}:#{SEND_MAIL_PORT}/send_mail_text' -d "#{message}")
--
2.23.0