reghao 2 лет назад
Родитель
Сommit
d5c931b920

+ 0 - 7
agent/bin/agent.json

@@ -1,7 +0,0 @@
-{
-  "manager": {
-    "host": "localhost",
-    "port": 4020
-  },
-  "heartbeatInterval": 5
-}

+ 0 - 5
agent/bin/shutdown.sh

@@ -1,5 +0,0 @@
-#!/bin/bash
-
-app='devops-agent.jar'
-pid=`jps | grep ${app} | awk '{print $1}'`
-kill -15 ${pid}

+ 0 - 5
agent/bin/start.sh

@@ -1,5 +0,0 @@
-#!/bin/bash
-
-app_dir=`pwd`
-app_name='devops-agent.jar'
-nohup java -jar ${app_dir}"/"${app_name} ${app_dir}/agent.json > console.log 2>&1 &

+ 0 - 5
manager/bin/shutdown.sh

@@ -1,5 +0,0 @@
-#!/bin/bash
-
-app='devops-manager.jar'
-pid=`jps | grep ${app} | awk '{print $1}'`
-kill -15 ${pid}

+ 0 - 6
manager/bin/start.sh

@@ -1,6 +0,0 @@
-#!/bin/bash
-
-app_dir=`pwd`
-app_name='devops-manager.jar'
-# 加载 application.yml 和 application-test.yml 两个配置文件, 分别位于 classpath 和文件系统路径
-java -jar ${app_dir}"/"${app_name} > console.log 2>&1 &

+ 3 - 2
manager/src/main/java/cn/reghao/devops/manager/notification/service/NotifyService.java

@@ -22,10 +22,11 @@ import java.util.concurrent.ExecutorService;
 public class NotifyService {
 public class NotifyService {
     private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("notify-service");
     private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("notify-service");
     private final DingNotify dingNotify;
     private final DingNotify dingNotify;
-    private EmailNotify emailNotify;
+    private final EmailNotify emailNotify;
 
 
-    public NotifyService(DingNotify dingNotify) {
+    public NotifyService(DingNotify dingNotify, EmailNotify emailNotify) {
         this.dingNotify = dingNotify;
         this.dingNotify = dingNotify;
+        this.emailNotify = emailNotify;
     }
     }
 
 
     public <T> void notify(T msg) {
     public <T> void notify(T msg) {

+ 35 - 29
manager/src/main/java/cn/reghao/devops/manager/notification/service/notifier/email/EmailNotify.java

@@ -1,8 +1,12 @@
 package cn.reghao.devops.manager.notification.service.notifier.email;
 package cn.reghao.devops.manager.notification.service.notifier.email;
 
 
+import cn.reghao.devops.manager.notification.db.repository.EmailAccountRepository;
 import cn.reghao.devops.manager.notification.model.po.EmailAccount;
 import cn.reghao.devops.manager.notification.model.po.EmailAccount;
 import cn.reghao.devops.manager.notification.service.notifier.Notify;
 import cn.reghao.devops.manager.notification.service.notifier.Notify;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
 
 
+import javax.mail.Address;
 import javax.mail.MessagingException;
 import javax.mail.MessagingException;
 import javax.mail.Session;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.Transport;
@@ -10,6 +14,7 @@ import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMessage;
 import java.io.UnsupportedEncodingException;
 import java.io.UnsupportedEncodingException;
 import java.util.Date;
 import java.util.Date;
+import java.util.List;
 import java.util.Properties;
 import java.util.Properties;
 
 
 /**
 /**
@@ -18,46 +23,46 @@ import java.util.Properties;
  * @author reghao
  * @author reghao
  * @date 2021-02-25 19:23:16
  * @date 2021-02-25 19:23:16
  */
  */
+@Slf4j
+@Service
 public class EmailNotify implements Notify<EmailMsg> {
 public class EmailNotify implements Notify<EmailMsg> {
-    private final String account;
-    private final String password;
-    private final InternetAddress from;
-    private Session session;
+    private final EmailAccountRepository emailAccountRepository;
 
 
-    public EmailNotify(EmailAccount emailAccount) throws UnsupportedEncodingException {
-        this.account = emailAccount.getUsername();
-        this.password = emailAccount.getPassword();
-        this.from = new InternetAddress(account, emailAccount.getPersonal(), "UTF-8");
-        initSessioin(emailAccount.getSmtp());
+    public EmailNotify(EmailAccountRepository emailAccountRepository) {
+        this.emailAccountRepository = emailAccountRepository;
     }
     }
 
 
-    private void initSessioin(String smtp) {
+    @Override
+    public void send(String receiver, EmailMsg msg) throws Exception {
+        List<EmailAccount> list = emailAccountRepository.findAll();
+        if (list.isEmpty()) {
+            log.info("没有可用于发送通知的邮箱配置");
+            return;
+        }
+
+        EmailAccount emailAccount = list.get(0);
+        String myEmail = emailAccount.getUsername();
+        String password = emailAccount.getPassword();
+        String personal = emailAccount.getPersonal();
+
+        String smtp = emailAccount.getSmtp();
         Properties props = new Properties();
         Properties props = new Properties();
         props.setProperty("mail.transport.protocol", "smtp");
         props.setProperty("mail.transport.protocol", "smtp");
         props.setProperty("mail.smtp.host", smtp);
         props.setProperty("mail.smtp.host", smtp);
         props.setProperty("mail.smtp.auth", "true");
         props.setProperty("mail.smtp.auth", "true");
-        session = Session.getInstance(props);
-        session.setDebug(true);
-    }
+        Session session = Session.getInstance(props);
 
 
-    @Override
-    public void send(String receiver, EmailMsg msg) throws Exception {
-        MimeMessage message = message(emailDestinations(receiver), msg.getSubject(), msg.getContent());
-        Transport transport = session.getTransport();
-        transport.connect(account, password);
-        transport.sendMessage(message, message.getAllRecipients());
-        transport.close();
-    }
+        InternetAddress from = new InternetAddress(myEmail, personal, "UTF-8");
+        InternetAddress[] dest = getDestinations(receiver);
+        MimeMessage message = getMessage(session, from, dest, msg);
 
 
-    public void send(String receiver, String subject, String msg) throws Exception {
-        MimeMessage message = message(emailDestinations(receiver), subject, msg);
         Transport transport = session.getTransport();
         Transport transport = session.getTransport();
-        transport.connect(account, password);
+        transport.connect(myEmail, password);
         transport.sendMessage(message, message.getAllRecipients());
         transport.sendMessage(message, message.getAllRecipients());
         transport.close();
         transport.close();
     }
     }
 
 
-    private InternetAddress[] emailDestinations(String receiver) throws UnsupportedEncodingException {
+    private InternetAddress[] getDestinations(String receiver) throws UnsupportedEncodingException {
         String[] strs = receiver.split(",");
         String[] strs = receiver.split(",");
         int len = strs.length;
         int len = strs.length;
         InternetAddress[] addresses = new InternetAddress[len];
         InternetAddress[] addresses = new InternetAddress[len];
@@ -67,12 +72,13 @@ public class EmailNotify implements Notify<EmailMsg> {
         return addresses;
         return addresses;
     }
     }
 
 
-    private MimeMessage message(InternetAddress[] addresses, String subject, String content) throws MessagingException {
+    private MimeMessage getMessage(Session session, Address from, InternetAddress[] dest, EmailMsg msg)
+            throws MessagingException {
         MimeMessage message = new MimeMessage(session);
         MimeMessage message = new MimeMessage(session);
         message.setFrom(from);
         message.setFrom(from);
-        message.setRecipients(MimeMessage.RecipientType.TO, addresses);
-        message.setSubject(subject, "UTF-8");
-        message.setContent(content, "text/html;charset=UTF-8");
+        message.setRecipients(MimeMessage.RecipientType.TO, dest);
+        message.setSubject(msg.getSubject(), "UTF-8");
+        message.setContent(msg.getContent(), "text/html;charset=UTF-8");
         message.setSentDate(new Date());
         message.setSentDate(new Date());
         return message;
         return message;
     }
     }

+ 3 - 3
manager/src/main/resources/application-dev.yml

@@ -1,5 +1,5 @@
 spring:
 spring:
   datasource:
   datasource:
-    url: jdbc:mysql://192.168.0.210:3306/reghao_devops_tdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
-    username: test
-    password: Test@123456
+    url: jdbc:mysql://localhost/reghao_devops_rdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
+    username: dev
+    password: Dev@123456

+ 0 - 28
manager/src/test/java/AppConfigTest.java

@@ -7,10 +7,6 @@ import cn.reghao.devops.manager.app.model.po.AppBuilding;
 import cn.reghao.devops.manager.notification.service.NotifyService;
 import cn.reghao.devops.manager.notification.service.NotifyService;
 import cn.reghao.devops.manager.notification.db.repository.EmailAccountRepository;
 import cn.reghao.devops.manager.notification.db.repository.EmailAccountRepository;
 import cn.reghao.devops.manager.notification.db.repository.NotifyReceiverRepository;
 import cn.reghao.devops.manager.notification.db.repository.NotifyReceiverRepository;
-import cn.reghao.devops.manager.notification.model.po.EmailAccount;
-import cn.reghao.devops.manager.notification.model.po.NotifyReceiver;
-import cn.reghao.devops.manager.notification.service.notifier.ding.DingMsg;
-import cn.reghao.devops.manager.notification.service.notifier.email.EmailMsg;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.Test;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runner.RunWith;
@@ -35,36 +31,12 @@ public class AppConfigTest {
 
 
     @Test
     @Test
     public void initNotifyTest() throws InterruptedException {
     public void initNotifyTest() throws InterruptedException {
-        NotifyReceiver notifyReceiver = new NotifyReceiver();
-        notifyReceiver.setName("ding-msgbot");
-        notifyReceiver.setType("webhook");
-        notifyReceiver.setUrl("https://oapi.dingtalk.com/robot/send?access_token=2ede844511f6a12a0429a25585222ef1f0eb99094421ea4b3155f17fda0f4662");
-        notifyReceiverRepository.save(notifyReceiver);
-
-        EmailAccount emailAccount = new EmailAccount();
-        emailAccount.setSmtp("smtp.163.com");
-        emailAccount.setUsername("gisgit@163.com");
-        emailAccount.setPassword("sirHdeKYn4PHcMP9");
-        emailAccount.setPersonal("admin@devops");
-        emailAccountRepository.save(emailAccount);
-
     }
     }
 
 
-
     @Autowired
     @Autowired
     NotifyService notifyService;
     NotifyService notifyService;
     @Test
     @Test
     public void notifyTest() throws InterruptedException {
     public void notifyTest() throws InterruptedException {
-        String title = "通知测试";
-        String content = "通知测试内容";
-
-        EmailMsg emailMsg = new EmailMsg(title, content);
-        String receiver = "haosg@outlook.com";
-        notifyService.notify(receiver, emailMsg);
-
-        DingMsg dingMsg = new DingMsg(title, content);
-        //notifyService.notify(dingMsg);
-        Thread.sleep(3600_000);
     }
     }
 
 
     @Autowired
     @Autowired