check to assign account
invoke ParseApplyAccountEmail
parse commit url
parse pubkey
invoke AssignJumperAccount
generate uuid
apply account
store account info to es
send email
send success email
send fail email
Signed-off-by: Luan Shengde <shdluan(a)163.com>
---
lib/assign-account.rb | 73 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 73 insertions(+)
create mode 100755 lib/assign-account.rb
diff --git a/lib/assign-account.rb b/lib/assign-account.rb
new file mode 100755
index 0000000..880de97
--- /dev/null
+++ b/lib/assign-account.rb
@@ -0,0 +1,73 @@
+#!/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'
+require_relative 'es_client'
+
+# assign uuid/account
+class AssignAccount
+ def initialize(mail_content)
+ @send_mail_host = %x(/sbin/ip route | awk '/default/ {print $3}').chomp
+ @send_mail_port = SEND_MAIL_PORT
+ @mail_content = mail_content
+
+ @my_info = {
+ 'my_email' => mail_content.from[0],
+ 'my_name' => mail_content.From.unparsed_value.gsub(/ <[^<>]*>/, '')
+ }
+ end
+
+ def send_account
+ my_commit_url, my_ssh_pubkey = parse_commit_url_pub_key
+ acct_info = apply_my_account(my_ssh_pubkey)
+
+ @my_info['my_commit_url'] = my_commit_url
+ @my_info['my_login_name'] = acct_info['my_login_name']
+
+ store_account_info
+ send_mail('')
+ rescue StandardError => e
+ puts e.message
+ puts e.backtrace
+
+ send_mail(e.message)
+ end
+
+ def parse_commit_url_pub_key
+ parse_apply_account_email = ParseApplyAccountEmail.new(@mail_content)
+
+ my_commit_url = parse_apply_account_email.parse_commit_url
+ my_ssh_pubkey = parse_apply_account_email.parse_pub_key
+
+ return my_commit_url, my_ssh_pubkey
+ end
+
+ def apply_my_account(my_ssh_pubkey)
+ my_uuid = %x(uuidgen).chomp
+
+ @my_info['my_uuid'] = my_uuid
+
+ apply_account = ApplyJumperAccount.new(@my_info, my_ssh_pubkey)
+ acct_info = apply_account.apply_jumper_account
+
+ return acct_info
+ end
+
+ def store_account_info
+ es = ESClient.new(index: 'accounts')
+ es.put_source_by_id(@my_info['my_email'], @my_info)
+ end
+
+ def send_mail(message)
+ email_message = if message.empty?
+ build_apply_account_email(@my_info)
+ else
+ build_apply_account_fail_email(@my_info, message)
+ end
+
+ %x(curl -XPOST "#{@send_mail_host}:#{@send_mail_port}/send_mail_text" -d "#{email_message}")
+ end
+end
--
2.23.0