vk-ssl-auto-deploy 0.6.1 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -53
- package/app.js +9 -0
- package/config.json +1 -1
- package/package.json +4 -4
- package/routes/cert.js +30 -11
- package/bin/www.js +0 -92
package/README.md
CHANGED
|
@@ -40,9 +40,9 @@ npm root -g
|
|
|
40
40
|
|
|
41
41
|
假设返回 `/usr/lib/node_modules`
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
则拼接上 `vk-ssl-auto-deploy`,即执行命令 `cd /usr/lib/node_modules/vk-ssl-auto-deploy` 进入工具目录
|
|
44
44
|
|
|
45
|
-
### 5.
|
|
45
|
+
### 5. 修改证书保存目录
|
|
46
46
|
|
|
47
47
|
查看配置文件 [查看配置说明](#配置说明)
|
|
48
48
|
|
|
@@ -60,50 +60,20 @@ vim config.json
|
|
|
60
60
|
- 输入 :q! - 不保存退出
|
|
61
61
|
- 输入 :w - 仅保存
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
```bash
|
|
66
|
-
sudo nginx -t && sudo service nginx reload
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
Windows系统下重载nginx配置(注意:地址需要用 / 不能用 \)
|
|
70
|
-
|
|
71
|
-
```bash
|
|
72
|
-
cd C:/Users/Administrator/Desktop/nginx-openresty-1.15.8.1-win64&nginx.exe -s reload
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### 6. 启动程序
|
|
76
|
-
|
|
77
|
-
安装后可以直接使用命令启动:
|
|
78
|
-
|
|
79
|
-
```bash
|
|
80
|
-
# 调试启动(关闭命令窗口后程序会停止运行)
|
|
81
|
-
vk-ssl
|
|
82
|
-
|
|
83
|
-
# 【推荐】mp2方式运行(关闭命令窗口后程序依然会运行)
|
|
84
|
-
cd <安装目录>,如 cd /usr/lib/node_modules/vk-ssl-auto-deploy
|
|
85
|
-
npm run start
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
**注意服务器需要开放端口:6001**
|
|
89
|
-
|
|
90
|
-
### 7. 开机自动启动服务
|
|
63
|
+
### 6. 启动服务,并设置开机自启
|
|
91
64
|
|
|
92
65
|
#### Linux系统
|
|
93
66
|
|
|
94
67
|
使用PM2的startup功能来实现开机自动启动:
|
|
95
68
|
|
|
96
|
-
**步骤1
|
|
69
|
+
**步骤1:启动服务并保存进程列表**
|
|
97
70
|
|
|
98
71
|
```bash
|
|
72
|
+
# 进入安装目录(需改成你的目录)
|
|
73
|
+
cd /usr/lib/node_modules/vk-ssl-auto-deploy
|
|
74
|
+
|
|
75
|
+
# 生成开机启动脚本
|
|
99
76
|
pm2 startup
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
**步骤2:启动服务并保存进程列表**
|
|
103
|
-
|
|
104
|
-
```bash
|
|
105
|
-
# 进入安装目录
|
|
106
|
-
cd /usr/lib/node_modules/vk-ssl-auto-deploy
|
|
107
77
|
|
|
108
78
|
# 启动服务
|
|
109
79
|
npm run start
|
|
@@ -112,7 +82,7 @@ npm run start
|
|
|
112
82
|
pm2 save
|
|
113
83
|
```
|
|
114
84
|
|
|
115
|
-
**步骤
|
|
85
|
+
**步骤2:验证开机启动**
|
|
116
86
|
|
|
117
87
|
```bash
|
|
118
88
|
# 重启服务器
|
|
@@ -135,7 +105,7 @@ pm2 save --force
|
|
|
135
105
|
|
|
136
106
|
#### Windows系统
|
|
137
107
|
|
|
138
|
-
|
|
108
|
+
使用pm2-windows-startup
|
|
139
109
|
|
|
140
110
|
```bash
|
|
141
111
|
# 安装pm2-windows-startup
|
|
@@ -152,19 +122,6 @@ npm run start
|
|
|
152
122
|
pm2 save
|
|
153
123
|
```
|
|
154
124
|
|
|
155
|
-
**方法二:使用任务计划程序**
|
|
156
|
-
|
|
157
|
-
1. 打开"任务计划程序"(Win+R,输入 `taskschd.msc`)
|
|
158
|
-
2. 点击"创建基本任务"
|
|
159
|
-
3. 名称:`vk-ssl-auto-deploy`
|
|
160
|
-
4. 触发器:选择"计算机启动时"
|
|
161
|
-
5. 操作:选择"启动程序"
|
|
162
|
-
6. 程序或脚本:`npm`
|
|
163
|
-
7. 添加参数:`--prefix <安装目录> run start`
|
|
164
|
-
- 例如:`--prefix C:\Users\Administrator\AppData\Roaming\npm\node_modules\vk-ssl-auto-deploy run start`
|
|
165
|
-
8. 起始于:填写安装目录路径
|
|
166
|
-
9. 完成创建
|
|
167
|
-
|
|
168
125
|
**验证开机启动**
|
|
169
126
|
|
|
170
127
|
```bash
|
package/app.js
CHANGED
|
@@ -38,6 +38,15 @@ app.use((err, req, res, next) => {
|
|
|
38
38
|
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
|
39
39
|
res.status(err.status || 500);
|
|
40
40
|
res.render('error');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
// 启动服务器
|
|
45
|
+
const port = process.env.PORT || config.port;
|
|
46
|
+
const host = process.env.HOST || '0.0.0.0';
|
|
47
|
+
|
|
48
|
+
app.listen(port, host, () => {
|
|
49
|
+
console.log('listenPort:', port)
|
|
41
50
|
});
|
|
42
51
|
|
|
43
52
|
module.exports = app;
|
package/config.json
CHANGED
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vk-ssl-auto-deploy",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.3",
|
|
4
4
|
"description": "SSL证书自动部署工具 - 提供HTTP API接口,支持证书文件自动上传和部署",
|
|
5
5
|
"main": "app.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"vk-ssl": "./
|
|
7
|
+
"vk-ssl": "./app.js"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"dev": "node ./
|
|
11
|
-
"start": "pm2 start
|
|
10
|
+
"dev": "node ./app.js",
|
|
11
|
+
"start": "pm2 start ecosystem.config.js --name vk-ssl-auto-deploy",
|
|
12
12
|
"stop": "pm2 stop vk-ssl-auto-deploy",
|
|
13
13
|
"restart": "pm2 restart vk-ssl-auto-deploy",
|
|
14
14
|
"delete": "pm2 delete vk-ssl-auto-deploy",
|
package/routes/cert.js
CHANGED
|
@@ -44,6 +44,8 @@ let scheduledTaskTimer = null;
|
|
|
44
44
|
let lastExecutionDate = null;
|
|
45
45
|
let isTaskRunning = false;
|
|
46
46
|
let scheduledExecutionTime = null; // 固定的执行时间 { hour, minute }
|
|
47
|
+
let lastCountdownText = ''; // 上次打印的倒计时文本
|
|
48
|
+
let needWrite = false;
|
|
47
49
|
|
|
48
50
|
// 文件锁路径
|
|
49
51
|
const LOCK_FILE = path.join(__dirname, '..', '.cert-deploy.lock');
|
|
@@ -585,8 +587,8 @@ async function executeDeployTask() {
|
|
|
585
587
|
* 服务启动时随机确定一个执行时间(9:00-10:59之间),每天在该固定时间执行
|
|
586
588
|
*/
|
|
587
589
|
function initScheduledTask() {
|
|
588
|
-
logger.info('[定时任务] 初始化定时任务...');
|
|
589
|
-
|
|
590
|
+
logger.info('[定时任务] 初始化定时任务...');
|
|
591
|
+
needWrite = true;
|
|
590
592
|
// 清除旧的定时器
|
|
591
593
|
if (scheduledTaskTimer) {
|
|
592
594
|
clearInterval(scheduledTaskTimer);
|
|
@@ -602,8 +604,8 @@ function initScheduledTask() {
|
|
|
602
604
|
};
|
|
603
605
|
|
|
604
606
|
// scheduledExecutionTime = {
|
|
605
|
-
// hour:
|
|
606
|
-
// minute:
|
|
607
|
+
// hour: 14,
|
|
608
|
+
// minute: 13
|
|
607
609
|
// }
|
|
608
610
|
|
|
609
611
|
logger.success(
|
|
@@ -633,7 +635,7 @@ function initScheduledTask() {
|
|
|
633
635
|
|
|
634
636
|
// 计算剩余时间(毫秒)
|
|
635
637
|
const remainingMs = targetTime - now;
|
|
636
|
-
const remainingSeconds = Math.floor(remainingMs / 1000);
|
|
638
|
+
const remainingSeconds = Math.floor(remainingMs / 1000);
|
|
637
639
|
const hours = Math.floor(remainingSeconds / 3600);
|
|
638
640
|
const minutes = Math.floor((remainingSeconds % 3600) / 60);
|
|
639
641
|
const seconds = remainingSeconds % 60;
|
|
@@ -641,32 +643,49 @@ function initScheduledTask() {
|
|
|
641
643
|
// 格式化倒计时文本
|
|
642
644
|
let countdownText = '';
|
|
643
645
|
if (hours > 0) {
|
|
644
|
-
|
|
646
|
+
// 超过1小时:只显示小时数
|
|
647
|
+
countdownText = `${hours}小时+ 后执行`;
|
|
645
648
|
} else if (minutes > 0) {
|
|
646
|
-
|
|
649
|
+
// 超过1分钟:只显示分钟数
|
|
650
|
+
countdownText = `${minutes}分钟+ 后执行`;
|
|
651
|
+
} else if (seconds === 0){
|
|
652
|
+
countdownText = `即将执行`;
|
|
647
653
|
} else {
|
|
654
|
+
// 1分钟内:进行秒级倒计时
|
|
648
655
|
countdownText = `${seconds}秒 后执行`;
|
|
649
656
|
}
|
|
650
657
|
|
|
651
|
-
|
|
658
|
+
// 只有当倒计时文本发生变化时才打印
|
|
659
|
+
if (countdownText !== lastCountdownText && needWrite) {
|
|
660
|
+
if (countdownText === "即将执行") {
|
|
661
|
+
needWrite = false;
|
|
662
|
+
process.stdout.write(`\r[定时任务] ${countdownText}${' '.repeat(10)}`);
|
|
663
|
+
} else {
|
|
664
|
+
process.stdout.write(`\r[定时任务] 将在 ${countdownText}${' '.repeat(5)}`);
|
|
665
|
+
}
|
|
666
|
+
lastCountdownText = countdownText;
|
|
667
|
+
}
|
|
652
668
|
}
|
|
653
669
|
|
|
654
670
|
// 只在秒数为 0 时检查是否到达执行时间
|
|
655
671
|
if (currentSecond === 0) {
|
|
656
672
|
if (currentHour === scheduledExecutionTime.hour && currentMinute === scheduledExecutionTime.minute) {
|
|
657
673
|
// 检查今天是否已经执行过
|
|
658
|
-
if (lastExecutionDate !== currentDate) {
|
|
674
|
+
if (lastExecutionDate !== currentDate) {
|
|
659
675
|
// 换行后输出触发信息
|
|
660
676
|
console.log('');
|
|
661
677
|
logger.success(
|
|
662
678
|
`[定时任务] 触发执行 - 当前时间: ${currentHour}:${currentMinute.toString().padStart(2, '0')}`
|
|
663
679
|
);
|
|
664
|
-
lastExecutionDate = currentDate;
|
|
680
|
+
lastExecutionDate = currentDate;
|
|
665
681
|
|
|
666
682
|
// 异步执行任务,使用try-catch确保异常不会影响定时器
|
|
667
683
|
executeDeployTask().catch(error => {
|
|
668
684
|
logger.error('[定时任务] 执行失败:', error);
|
|
669
|
-
})
|
|
685
|
+
}).finally(()=> {
|
|
686
|
+
needWrite = true;
|
|
687
|
+
lastCountdownText = "";
|
|
688
|
+
})
|
|
670
689
|
}
|
|
671
690
|
}
|
|
672
691
|
}
|
package/bin/www.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Module dependencies.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const app = require('../app');
|
|
8
|
-
const debug = require('debug')('express-template:server');
|
|
9
|
-
const http = require('http');
|
|
10
|
-
const config = require('../config');
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Get port from environment and store in Express.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
const port = normalizePort(process.env.PORT || config.port);
|
|
17
|
-
app.set('port', port);
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Create HTTP server.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
const server = http.createServer(app);
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Listen on provided port, on all network interfaces.
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
server.listen(port);
|
|
30
|
-
console.log('listenPort:', port)
|
|
31
|
-
server.on('error', onError);
|
|
32
|
-
server.on('listening', onListening);
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Normalize a port into a number, string, or false.
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
function normalizePort(val) {
|
|
39
|
-
const port = parseInt(val, 10);
|
|
40
|
-
|
|
41
|
-
if (isNaN(port)) {
|
|
42
|
-
// named pipe
|
|
43
|
-
return val;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (port >= 0) {
|
|
47
|
-
// port number
|
|
48
|
-
return port;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Event listener for HTTP server "error" event.
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
function onError(error) {
|
|
59
|
-
if (error.syscall !== 'listen') {
|
|
60
|
-
throw error;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const bind = typeof port === 'string' ?
|
|
64
|
-
'Pipe ' + port :
|
|
65
|
-
'Port ' + port;
|
|
66
|
-
|
|
67
|
-
// handle specific listen errors with friendly messages
|
|
68
|
-
switch (error.code) {
|
|
69
|
-
case 'EACCES':
|
|
70
|
-
console.error(bind + ' requires elevated privileges');
|
|
71
|
-
process.exit(1);
|
|
72
|
-
break;
|
|
73
|
-
case 'EADDRINUSE':
|
|
74
|
-
console.error(bind + ' is already in use');
|
|
75
|
-
process.exit(1);
|
|
76
|
-
break;
|
|
77
|
-
default:
|
|
78
|
-
throw error;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Event listener for HTTP server "listening" event.
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
|
-
function onListening() {
|
|
87
|
-
const addr = server.address();
|
|
88
|
-
const bind = typeof addr === 'string' ?
|
|
89
|
-
'pipe ' + addr :
|
|
90
|
-
'port ' + addr.port;
|
|
91
|
-
debug('Listening on ' + bind);
|
|
92
|
-
}
|