1 | import { getConfigurationNames, renderRealmFile } from './realm';
|
2 | import { Realm } from './realm';
|
3 | import { asNames, prompt } from './utils';
|
4 | import { spawn } from 'p-spawn';
|
5 |
|
6 |
|
7 | export async function kcreate(realm: Realm, resourceNames?: string | string[]) {
|
8 | const names = await getConfigurationNames(realm, resourceNames);
|
9 |
|
10 | for (let name of names) {
|
11 | const fileName = await renderRealmFile(realm, name);
|
12 | try {
|
13 | const args = ['create', '-f', fileName];
|
14 | await spawn('kubectl', args);
|
15 | } catch (ex) {
|
16 | console.log(`Can't kcreate ${fileName}, skipping`);
|
17 | }
|
18 | console.log();
|
19 | }
|
20 |
|
21 | }
|
22 |
|
23 | export async function kapply(realm: Realm, resourceNames?: string | string[]) {
|
24 | const names = await getConfigurationNames(realm, resourceNames);
|
25 |
|
26 | for (let name of names) {
|
27 | const fileName = await renderRealmFile(realm, name);
|
28 | try {
|
29 | const args = ['apply', '-f', fileName];
|
30 | addNamespaceIfDefined(realm, args);
|
31 | await spawn('kubectl', args);
|
32 | } catch (ex) {
|
33 | console.log(`Can't kapply ${fileName}, skipping`);
|
34 | }
|
35 | console.log();
|
36 | }
|
37 | }
|
38 |
|
39 |
|
40 | export async function kdel(realm: Realm, resourceNames?: string | string[]) {
|
41 | const names = await getConfigurationNames(realm, resourceNames);
|
42 |
|
43 |
|
44 | if (realm.confirmOnDelete) {
|
45 | const answer = await prompt("Do you really want to delete? (YES to continue)");
|
46 | if (answer !== "YES") {
|
47 | console.log(`Operation not confirmed. Exiting (nothing done).`);
|
48 | return;
|
49 | }
|
50 | }
|
51 |
|
52 | for (let kName of names) {
|
53 | const fileName = await renderRealmFile(realm, kName);
|
54 | const args = ['delete', '-f', fileName];
|
55 | addNamespaceIfDefined(realm, args);
|
56 | try {
|
57 | await spawn('kubectl', args);
|
58 | } catch (ex) {
|
59 | console.log(`Can't kdelete ${kName}, skipping`);
|
60 | }
|
61 | console.log();
|
62 | }
|
63 | }
|
64 |
|
65 | let currentLogPodName: string | null = null;
|
66 |
|
67 | export async function klogs(realm: Realm, resourceNames?: string | string[]) {
|
68 | const names = await getConfigurationNames(realm, resourceNames);
|
69 | const pods = await fetchK8sObjectsByType(realm, 'pods');
|
70 |
|
71 | for (let serviceName of names) {
|
72 | const podNames = await getPodNames(pods, { labels: { run: `${realm.system}-${serviceName}` } });
|
73 |
|
74 | for (let podName of podNames) {
|
75 | console.log(`Will show log for pod: ${podName}`);
|
76 | const args = ['logs', '-f', podName];
|
77 | addNamespaceIfDefined(realm, args);
|
78 |
|
79 | spawn('kubectl', args, {
|
80 | detached: true,
|
81 | onStdout: function (data) {
|
82 |
|
83 | if (currentLogPodName != null && currentLogPodName !== podName) {
|
84 | console.log('-----------\n');
|
85 | }
|
86 |
|
87 |
|
88 | if (currentLogPodName !== podName) {
|
89 | console.log(`----- LOG for: ${serviceName} / ${podName} `);
|
90 | currentLogPodName = podName;
|
91 | }
|
92 |
|
93 |
|
94 | process.stdout.write(data);
|
95 | }
|
96 | });
|
97 | }
|
98 | }
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | export async function kshRestart(realm: Realm, serviceNamesStr: string) {
|
111 | const serviceNames = asNames(serviceNamesStr);
|
112 |
|
113 | const pods = await fetchK8sObjectsByType(realm, 'pods');
|
114 |
|
115 | for (let serviceName of serviceNames) {
|
116 | const podNames = await getPodNames(pods, { labels: { run: `${realm.system}-${serviceName}` } });
|
117 |
|
118 | for (let podName of podNames) {
|
119 |
|
120 |
|
121 | try {
|
122 | const args = ['exec', podName];
|
123 | addNamespaceIfDefined(realm, args);
|
124 | args.push('--', 'test', '-e', '/service/restart.sh');
|
125 | await spawn('kubectl', args, { toConsole: false });
|
126 | } catch (ex) {
|
127 | console.log(`Skipping service ${serviceName} - '/service/restart.sh' not found.`);
|
128 | continue;
|
129 | }
|
130 | console.log(`\n--- Restarting: ${serviceName} (pod: ${podName})`);
|
131 | const args = ['exec', podName];
|
132 | addNamespaceIfDefined(realm, args);
|
133 | args.push('--', '/service/restart.sh');
|
134 | await spawn('kubectl', args);
|
135 | console.log(`--- DONE: ${serviceName} : ${podName}`);
|
136 | }
|
137 | }
|
138 |
|
139 |
|
140 | }
|
141 |
|
142 | export async function kexec(realm: Realm, serviceNamesStr: string, commandAndArgs: string[]) {
|
143 | const serviceNames = asNames(serviceNamesStr);
|
144 | const pods = await fetchK8sObjectsByType(realm, 'pods');
|
145 |
|
146 | for (let serviceName of serviceNames) {
|
147 | const podNames = await getPodNames(pods, { labels: { run: `${realm.system}-${serviceName}` } });
|
148 |
|
149 | for (let podName of podNames) {
|
150 |
|
151 | try {
|
152 | let args = ['exec', podName]
|
153 | addNamespaceIfDefined(realm, args);
|
154 |
|
155 | args.push('--');
|
156 | args = args.concat(commandAndArgs);
|
157 | await spawn('kubectl', args);
|
158 | } catch (ex) {
|
159 | console.log(`Cannot run ${commandAndArgs} on pod ${podName} because ${ex}`);
|
160 | continue;
|
161 | }
|
162 | }
|
163 | }
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | export async function getCurrentContext(): Promise<string | null> {
|
170 |
|
171 | const psResult = await spawn('kubectl', ['config', 'current-context'], { capture: ['stdout', 'stderr'], ignoreFail: true });
|
172 | if (psResult.stderr) {
|
173 |
|
174 | return null;
|
175 | } else {
|
176 | return psResult.stdout.toString().trim() as string;
|
177 | }
|
178 |
|
179 | }
|
180 |
|
181 | export async function setCurrentContext(name: string) {
|
182 |
|
183 |
|
184 | await spawn('kubectl', ['config', 'use-context', name]);
|
185 | }
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | function addNamespaceIfDefined(realm: Realm, args: string[]) {
|
191 | const namespace = realm.namespace;
|
192 | if (namespace) {
|
193 | args.push('--namespace', namespace);
|
194 | }
|
195 | }
|
196 |
|
197 |
|
198 | async function fetchK8sObjectsByType(realm: Realm, type: string) {
|
199 | const args = ['get', type, '-o', 'json'];
|
200 | addNamespaceIfDefined(realm, args);
|
201 | const psResult = await spawn('kubectl', args, { capture: 'stdout' });
|
202 | const podsJsonStr = psResult.stdout.toString();
|
203 | const result = JSON.parse(podsJsonStr);
|
204 | return result.items || [];
|
205 | }
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | function getPodNames(items: any[], filter?: { imageName?: string, labels?: { [key: string]: string } }) {
|
211 | const names = [];
|
212 | if (items) {
|
213 | for (let item of items) {
|
214 | let pass = false;
|
215 | const itemName = item.metadata.name;
|
216 |
|
217 |
|
218 | if (filter && filter.imageName) {
|
219 |
|
220 | const itemImageName = (item.spec && item.spec.containers) ? item.spec.containers[0].image : null;
|
221 | if (itemImageName != null && itemImageName.startsWith(filter.imageName)) {
|
222 | pass = true;
|
223 | } else {
|
224 | pass = false;
|
225 | continue;
|
226 | }
|
227 | }
|
228 |
|
229 |
|
230 | if (filter && filter.labels) {
|
231 | for (let labelName in filter.labels) {
|
232 | if (item.metadata.labels[labelName] === filter.labels[labelName]) {
|
233 | pass = true;
|
234 | } else {
|
235 | pass = false;
|
236 | continue;
|
237 | }
|
238 | }
|
239 | }
|
240 |
|
241 |
|
242 | if (pass) {
|
243 | names.push(itemName);
|
244 | }
|
245 | }
|
246 | }
|
247 | return names;
|
248 | }
|
249 |
|
250 |
|
\ | No newline at end of file |