🌳
pt0/deployF/k8sF/resActionsF/getLogsF.mts
15import * as _ from 'lodash-es'
17type GetLogsProps = {
18 name: string
19 follow?: boolean
20 tailLines?: number
23export const getLogs = async ({name, follow=cliFlag('--follow', '-f') ?? !shouldSkipStreamLogs(), tailLines}: GetLogsProps) => {
24 const {cluster_name} = getKlusterCtx()
25 assertDefined(cluster_name)
26 const {git_sha} = getAppCfg()
27 const podH: {labels: {name: string, git_sha?: string}} = {labels: {name}}
28 if (git_sha) podH.labels.git_sha = git_sha
30 let allPods = await getAllPods({...podH, cluster_name})
31 if (!allPods.length && git_sha) {
32 delete podH.labels.git_sha
33 allPods = await getAllPods({...podH, cluster_name})
34 const deployedGitSha = allPods[0]?.git_sha
35 if (deployedGitSha && deployedGitSha !== git_sha) {
36 const {pathsA} = await getRakeEpsPaths({defaultEpScope: epScopes.dockerjs})
37 const diffRes = await appDeployDiff({
38 ...getAppCfg(), git_sha, existingGitSha: deployedGitSha, filterPathList: pathsA,
39 diffcmd: 'git log', logLimit: 10, noExtraGitArgs: true,
40 })
41 betLog('WARN outdated git_sha', {
42 existingGitSha: diffRes.existingGitSha, newGitSha: diffRes.newGitSha,
43 lastDeployCommitsAgo: diffRes.lastDeployCommitsAgo, lastDeployDurAgo: diffRes.lastDeployDurAgo,
44 })
45 console.log(diffRes.stdout)
46 }
47 }
50 const podArg = cliArg('--pod')
51 const podsLen = allPods.length
53 if (podArg === 'all') {
54 betLog(`streaming ${podsLen} pods`)
55 await retMultiPodLogs({cluster_name, pods: allPods, follow, tailLines: tailLines ?? 100})
56 return
57 }
59 const podIdx = podArg ? parseInt(podArg, 10) - 1 : 0
60 const selectedPod = assertValidIdx(allPods, podIdx, {
61 podArg, validRange: `1-${podsLen}`, podNames: _.map(allPods, 'podName'),
62 })
63 if (podsLen > 1) betLog(chalkGray(`[${podIdx + 1}/${podsLen}] ${selectedPod.podName}`))
64 betLog({containerNames: selectedPod.containerNames})
65 await ret1PodLogs({
66 cluster_name, podName: selectedPod.podName, containerName: selectedPod.containerName,
67 streamStdout: true, follow, tailLines, timestamps: false, pretty: true
68 })