|
1 | 1 | import debug from 'debug'; |
2 | 2 | import { Server } from "socket.io"; |
3 | | -import { IApp, IPipeline, IPipelineList, IKubectlAppList, IDeployKeyPair, IKubectlPipelineList, IKubectlApp, IPodSize, IKuberoConfig} from './types'; |
| 3 | +import { IApp, IPipeline, IPipelineList, IKubectlAppList, IDeployKeyPair, IKubectlPipelineList, IKubectlApp, ILoglines, IKuberoConfig} from './types'; |
4 | 4 | import { IPullrequest } from './git/types'; |
5 | 5 | import { App } from './modules/application'; |
6 | 6 | import { Buildpack } from './modules/config'; |
@@ -842,65 +842,78 @@ export class Kubero { |
842 | 842 | } |
843 | 843 | } |
844 | 844 |
|
845 | | - public async getLogsHistory(pipelineName: string, phaseName: string, appName: string) { |
| 845 | + public async getLogsHistory(pipelineName: string, phaseName: string, appName: string, container: string) { |
846 | 846 | const contextName = this.getContext(pipelineName, phaseName); |
847 | 847 | const namespace = pipelineName+'-'+phaseName; |
848 | 848 |
|
849 | | - const logStream = new Stream.PassThrough(); |
850 | | - let logs: String = ''; |
851 | | - logStream.on('data', (chunk: any) => { |
852 | | - //console.log(chunk.toString()); |
853 | | - logs += chunk.toString(); |
854 | | - }); |
855 | | - |
856 | | - let loglines: any[] = []; |
| 849 | + let loglines: ILoglines[] = []; |
857 | 850 | if (contextName) { |
858 | 851 | const pods = await this.kubectl.getPods(namespace, contextName); |
859 | 852 | for (const pod of pods) { |
860 | 853 |
|
861 | 854 | if (pod.metadata?.name?.startsWith(appName)) { |
862 | | - for (const container of pod.spec?.containers || []) { |
863 | | - console.log('getting logs for '+pod.metadata.name+' '+container.name); |
864 | | - try { |
865 | | - await this.kubectl.log.log(namespace, pod.metadata.name, container.name, logStream, {follow: false, tailLines: 80, pretty: false, timestamps: true}) |
866 | | - } catch (error) { |
867 | | - console.log("error getting logs for "+pod.metadata.name+" "+container.name); |
868 | | - return loglines; |
869 | | - } |
870 | | - |
871 | | - |
872 | | - // sleep for 1 second to wait for all logs to be collected |
873 | | - await new Promise(r => setTimeout(r, 1000)); |
874 | | - |
875 | | - // split loglines into array |
876 | | - const loglinesArray = logs.split('\n').reverse(); |
877 | | - for (const logline of loglinesArray) { |
878 | | - if (logline.length > 0) { |
879 | | - // split after first whitespace |
880 | | - const loglineArray = logline.split(/(?<=^\S+)\s/); |
881 | | - const loglineDate = new Date(loglineArray[0]); |
882 | | - const loglineText = loglineArray[1]; |
883 | | - |
884 | | - |
885 | | - |
886 | | - loglines.push({ |
887 | | - id: uuidv4(), |
888 | | - time: loglineDate.getTime(), |
889 | | - pipeline: pipelineName, |
890 | | - phase: phaseName, |
891 | | - app: appName, |
892 | | - pod: pod.metadata.name, |
893 | | - podID: pod.metadata.name.split('-')[3]+'-'+pod.metadata.name.split('-')[4], |
894 | | - container: container.name, |
895 | | - color: this.logcolor(pod.metadata.name), |
896 | | - log: loglineText |
897 | | - }); |
898 | | - } |
| 855 | + if (container == 'web') { |
| 856 | + for (const container of pod.spec?.containers || []) { |
| 857 | + const ll = await this.fetchLogs(namespace, pod.metadata.name, container.name, pipelineName, phaseName, appName) |
| 858 | + loglines = loglines.concat(ll); |
899 | 859 | } |
| 860 | + } else if (container == 'builder' || container == 'fetcher') { |
| 861 | + const ll = await this.fetchLogs(namespace, pod.metadata.name, "kuberoapp-"+container, pipelineName, phaseName, appName) |
| 862 | + loglines = loglines.concat(ll); |
| 863 | + } else { |
| 864 | + // leace the loglines empty |
| 865 | + console.log('unknown container: '+container); |
900 | 866 | } |
901 | 867 | } |
902 | 868 | } |
903 | 869 | } |
| 870 | + return loglines; |
| 871 | + } |
| 872 | + |
| 873 | + private async fetchLogs(namespace: string, podName: string, containerName: string, pipelineName: string, phaseName: string, appName: string): Promise<ILoglines[]> { |
| 874 | + let loglines: ILoglines[] = []; |
| 875 | + |
| 876 | + const logStream = new Stream.PassThrough(); |
| 877 | + let logs: String = ''; |
| 878 | + logStream.on('data', (chunk: any) => { |
| 879 | + //console.log(chunk.toString()); |
| 880 | + logs += chunk.toString(); |
| 881 | + }); |
| 882 | + |
| 883 | + console.log('getting logs for '+podName+' '+containerName); |
| 884 | + try { |
| 885 | + await this.kubectl.log.log(namespace, podName, containerName, logStream, {follow: false, tailLines: 80, pretty: false, timestamps: true}) |
| 886 | + } catch (error) { |
| 887 | + console.log("error getting logs for "+podName+" "+containerName); |
| 888 | + return []; |
| 889 | + } |
| 890 | + |
| 891 | + // sleep for 1 second to wait for all logs to be collected |
| 892 | + await new Promise(r => setTimeout(r, 1000)); |
| 893 | + |
| 894 | + // split loglines into array |
| 895 | + const loglinesArray = logs.split('\n').reverse(); |
| 896 | + for (const logline of loglinesArray) { |
| 897 | + if (logline.length > 0) { |
| 898 | + // split after first whitespace |
| 899 | + const loglineArray = logline.split(/(?<=^\S+)\s/); |
| 900 | + const loglineDate = new Date(loglineArray[0]); |
| 901 | + const loglineText = loglineArray[1]; |
| 902 | + |
| 903 | + loglines.push({ |
| 904 | + id: uuidv4(), |
| 905 | + time: loglineDate.getTime(), |
| 906 | + pipeline: pipelineName, |
| 907 | + phase: phaseName, |
| 908 | + app: appName, |
| 909 | + pod: podName, |
| 910 | + podID: podName.split('-')[3]+'-'+podName.split('-')[4], |
| 911 | + container: containerName, |
| 912 | + color: this.logcolor(podName), |
| 913 | + log: loglineText |
| 914 | + }); |
| 915 | + } |
| 916 | + } |
904 | 917 |
|
905 | 918 | return loglines; |
906 | 919 | } |
|
0 commit comments