[PATCH v6 compass-ci 1/7] container/send-internet-mail: mail-post.rb

get request for send mail analysis email data send mail default use local defined smtp setup when required internet-smtp, will override the local setup and use internet-smtp instead Signed-off-by: Luan Shengde <luanshengde2@huawei.com> --- lib/mail-post.rb | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 lib/mail-post.rb diff --git a/lib/mail-post.rb b/lib/mail-post.rb new file mode 100755 index 0000000..9fcf1fe --- /dev/null +++ b/lib/mail-post.rb @@ -0,0 +1,64 @@ +#!/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 'sinatra' +require 'json' +require 'yaml' +require 'open3' +require 'mail' + +set :bind, '0.0.0.0' +set :port, ENV['SEND_MAIL_PORT'] + +mail_server = %x(/sbin/ip route | awk '/default/ {print $3}').chomp + +smtp = { + address: mail_server, + enable_starttls_auto: false +} + +Mail.defaults { delivery_method :smtp, smtp } + +post '/send_mail_yaml' do + data = YAML.safe_load request.body.read + raise TypeError, data, 'request data type error' unless data.is_a? Hash + + mail_info = { + 'subject' => data['subject'], + 'to' => data['to'], + 'body' => data['body'] + } + + check_send_mail(mail_info) +end + +post '/send_mail_text' do + data = Mail.read_from_string(request.body.read) + + mail_info = { + 'subject' => data.subject, + 'to' => data.to, + 'body' => data.body.decoded + } + + send_mail(mail_info) +end + +def check_send_mail(mail_info) + raise 'no/empty subject.' if mail_info['subject'].nil? || mail_info['subject'].empty? + raise 'no/empty email_to address.' if mail_info['to'].nil? || mail_info['to'].empty? + raise 'no/empty email content.' if mail_info['body'].nil? || mail_info['body'].empty? +end + +def send_mail(mail_info) + check_send_mail(mail_info) + mail = Mail.new do + from ENV['ROBOT_EMAIL_ADDRESS'] + subject mail_info['subject'] + to mail_info['to'] + body mail_info['body'] + end + mail.deliver! +end -- 2.23.0

On Mon, Oct 26, 2020 at 11:54:16AM +0800, Wu Fengguang wrote:
TO ALL:
请把cryptic过程 包装为一个底层函数,起一个描述性的函数名。 必要的话加comment.
+mail_server = %x(/sbin/ip route | awk '/default/ {print $3}').chomp
Please encode the above %x() into a function. The function name should reflect what it does.
I got it Thanks Luan Shengde
Thanks, Fengguang

On Fri, Oct 23, 2020 at 04:33:13PM +0800, Luan Shengde wrote:
get request for send mail analysis email data send mail
default use local defined smtp setup when required internet-smtp, will override the local setup and use internet-smtp instead
Signed-off-by: Luan Shengde <luanshengde2@huawei.com> --- lib/mail-post.rb | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 lib/mail-post.rb
diff --git a/lib/mail-post.rb b/lib/mail-post.rb new file mode 100755 index 0000000..9fcf1fe --- /dev/null +++ b/lib/mail-post.rb @@ -0,0 +1,64 @@ +#!/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 'sinatra' +require 'json' +require 'yaml' +require 'open3' +require 'mail' + +set :bind, '0.0.0.0' +set :port, ENV['SEND_MAIL_PORT'] + +mail_server = %x(/sbin/ip route | awk '/default/ {print $3}').chomp + +smtp = { + address: mail_server, + enable_starttls_auto: false +} + +Mail.defaults { delivery_method :smtp, smtp }
Can move the above to the send-mail container run.rb Avoid running things in lib/ code. Thanks, Fengguang

On Mon, Oct 26, 2020 at 11:55:20AM +0800, Wu Fengguang wrote:
On Fri, Oct 23, 2020 at 04:33:13PM +0800, Luan Shengde wrote:
get request for send mail analysis email data send mail
default use local defined smtp setup when required internet-smtp, will override the local setup and use internet-smtp instead
Signed-off-by: Luan Shengde <luanshengde2@huawei.com> --- lib/mail-post.rb | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 lib/mail-post.rb
diff --git a/lib/mail-post.rb b/lib/mail-post.rb new file mode 100755 index 0000000..9fcf1fe --- /dev/null +++ b/lib/mail-post.rb @@ -0,0 +1,64 @@ +#!/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 'sinatra' +require 'json' +require 'yaml' +require 'open3' +require 'mail' + +set :bind, '0.0.0.0' +set :port, ENV['SEND_MAIL_PORT'] + +mail_server = %x(/sbin/ip route | awk '/default/ {print $3}').chomp + +smtp = { + address: mail_server, + enable_starttls_auto: false +} + +Mail.defaults { delivery_method :smtp, smtp }
Can move the above to the send-mail container run.rb Avoid running things in lib/ code.
I got it
Thanks, Fengguang
participants (2)
-
Luan Shengde
-
Wu Fengguang