1
|
|
|
import Dnsmasq from '../services/dnsmasq' |
2
|
|
|
import Elasticsearch from '../services/elasticsearch' |
3
|
|
|
import Mailhog from '../services/mailhog' |
4
|
|
|
import Mariadb from '../services/mariadb' |
5
|
|
|
import Mysql from '../services/mysql' |
6
|
|
|
import Mysql57 from '../services/mysql57' |
7
|
|
|
import Mysql80 from '../services/mysql80' |
8
|
|
|
import Nginx from '../services/nginx' |
9
|
|
|
import PhpFpm from '../services/phpFpm' |
10
|
|
|
import PhpFpm72 from '../services/phpFpm72' |
11
|
|
|
import PhpFpm73 from '../services/phpFpm73' |
12
|
|
|
import PhpFpm74 from '../services/phpFpm74' |
13
|
|
|
import PhpFpm80 from '../services/phpFpm80' |
14
|
|
|
import Redis from '../services/redis' |
15
|
|
|
import Service from '../services/service' |
16
|
|
|
import {getLinkedDatabase} from '../utils/database' |
17
|
|
|
import {getLinkedPhpVersion} from '../utils/phpFpm' |
18
|
|
|
|
19
|
|
|
class ServiceController { |
20
|
|
|
allServices: Service[] = [ |
21
|
|
|
new Dnsmasq(), |
22
|
|
|
new Elasticsearch(), |
23
|
|
|
new Mailhog(), |
24
|
|
|
new Nginx(), |
25
|
|
|
new Mariadb(), |
26
|
|
|
new Mysql80(), |
27
|
|
|
new Mysql57(), |
28
|
|
|
new PhpFpm80(), |
29
|
|
|
new PhpFpm74(), |
30
|
|
|
new PhpFpm73(), |
31
|
|
|
new PhpFpm72(), |
32
|
|
|
new Redis() |
33
|
|
|
] |
34
|
|
|
|
35
|
|
|
executeStart = async (serviceName: string | undefined): Promise<boolean> => { |
36
|
|
|
if (!serviceName) { |
37
|
|
|
for (const service of this.allServices) { |
38
|
|
|
try { |
39
|
|
|
await this.controlService(service, 'start') |
40
|
|
|
} catch (e) { |
41
|
|
|
console.log(`Failed to start ${service.service}: ${e.message}`) |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
console.log(`Successfully started all Jale services`) |
45
|
|
|
return true |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
for (const service of this.allServices) { |
49
|
|
|
if (service.service.includes(serviceName)) { |
50
|
|
|
try { |
51
|
|
|
if (!(await this.controlService(service, 'start'))) { |
52
|
|
|
continue |
53
|
|
|
} |
54
|
|
|
console.log(`Successfully started ${serviceName}`) |
55
|
|
|
return true |
56
|
|
|
} catch (e) { |
57
|
|
|
console.log(`Failed to start ${service.service}: ${e.message}`) |
58
|
|
|
return false |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
console.warn(`Invalid service: ${serviceName}`) |
64
|
|
|
return false |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
executeStop = async (serviceName: string | undefined): Promise<boolean> => { |
68
|
|
|
if (!serviceName) { |
69
|
|
|
for (const service of this.allServices) { |
70
|
|
|
try { |
71
|
|
|
await this.controlService(service, 'stop') |
72
|
|
|
} catch (e) { |
73
|
|
|
console.log(`Failed to stop ${service.service}: ${e.message}`) |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
console.log(`Successfully stopped all Jale services`) |
78
|
|
|
return true |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
for (const service of this.allServices) { |
82
|
|
|
if (service.service.includes(serviceName)) { |
83
|
|
|
try { |
84
|
|
|
if (!(await this.controlService(service, 'stop'))) { |
85
|
|
|
continue |
86
|
|
|
} |
87
|
|
|
console.log(`Successfully stopped ${serviceName}`) |
88
|
|
|
return true |
89
|
|
|
} catch (e) { |
90
|
|
|
console.log(`Failed to stop ${service.service}: ${e.message}`) |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
console.warn(`Invalid service: ${serviceName}`) |
96
|
|
|
|
97
|
|
|
return false |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
executeRestart = async (serviceName: string | undefined): Promise<boolean> => { |
101
|
|
|
if (!serviceName) { |
102
|
|
|
for (const service of this.allServices) { |
103
|
|
|
try { |
104
|
|
|
await this.controlService(service, 'restart') |
105
|
|
|
} catch (e) { |
106
|
|
|
console.log(`Failed to restarted ${service.service}: ${e.message}`) |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
console.log(`Successfully restarted all Jale services`) |
110
|
|
|
return true |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
for (const service of this.allServices) { |
114
|
|
|
if (service.service.includes('restart')) { |
115
|
|
|
try { |
116
|
|
|
if (!(await this.controlService(service, 'restart'))) { |
117
|
|
|
continue |
118
|
|
|
} |
119
|
|
|
console.log(`Successfully restarted ${serviceName}`) |
120
|
|
|
return true |
121
|
|
|
} catch (e) { |
122
|
|
|
console.log(`Failed to restarted ${service.service}: ${e.message}`) |
123
|
|
|
return false |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
console.warn(`Invalid service: ${serviceName}`) |
129
|
|
|
return false |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Convenience method to start, stop or restart a service. It also checks if you are restarting PHP or MySQL. |
134
|
|
|
* @param service |
135
|
|
|
* @param action |
136
|
|
|
*/ |
137
|
|
|
controlService = async (service: Service, action: 'start' | 'stop' | 'restart'): Promise<boolean> => { |
138
|
|
|
if (service instanceof Mysql) { |
139
|
|
|
const linkedDatabase = await getLinkedDatabase() |
140
|
|
|
if (linkedDatabase.service !== service.service) { |
141
|
|
|
return false |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (service instanceof PhpFpm) { |
146
|
|
|
const linkedPhpVersion = await getLinkedPhpVersion() |
147
|
|
|
if (linkedPhpVersion.service !== service.service) { |
148
|
|
|
return false |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
switch (action) { |
153
|
|
|
case 'start': |
154
|
|
|
console.log(`Starting ${service.service}...`) |
155
|
|
|
await service.start() |
156
|
|
|
break |
157
|
|
|
case 'stop': |
158
|
|
|
console.log(`Stopping ${service.service}...`) |
159
|
|
|
await service.stop() |
160
|
|
|
break |
161
|
|
|
case 'restart': |
162
|
|
|
console.log(`Retarting ${service.service}...`) |
163
|
|
|
await service.restart() |
164
|
|
|
break |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return true |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
export default ServiceController |