1import { getAppCfg} from '../ctxF/appCfgCtxF.mts'2import { getKlusterCtx } from '../ctxF/klusterCtxF.mts'3import { getAllPods } from '../getSinglePodF.mts'4import { betLog } from '../../../sharedF/betterConsLogF.mts'5import { ret1PodLogs, retMultiPodLogs } from '../logsF/retPodLogsF.mts'6import { shouldSkipStreamLogs } from '../logsF/watchFlogsF.mts'7import { assertDefined } from '../../../sharedF/assertsF/assertDefinedF.mts'8import { assertValidIdx } from '../../../sharedF/assertsF/assertValidIdxAI.mts'9import { cliArg, cliFlag } from '../../../serverF/cliArgAI.mts'10import { assertNonEmpty } from '../../../sharedF/assertsF/assertNonEmptyF.mts'11import { chalkGray } from '../../../serverF/libChalkF.mts'12import { appDeployDiff } from '../../libF/gitF/appDeployDiffF.mts'13import { getRakeEpsPaths } from '../../codeStatsF/deployPathsF.mts'14import { epScopes } from '../../dockerF/getEpCfgF.mts'15import * as _ from 'lodash-es'17type GetLogsProps = {18 name: string19 follow?: boolean20 tailLines?: number21}23export const getLogs = async ({name, follow=cliFlag('--follow', '-f') ?? !shouldSkipStreamLogs(), tailLines}: GetLogsProps) => {24 const {cluster_name} = getKlusterCtx()25 assertDefined(cluster_name)27 const podH: {labels: {name: string, git_sha?: string}} = {labels: {name}}28 if (git_sha) podH.labels.git_sha = git_sha30 let allPods = await getAllPods({...podH, cluster_name})31 if (!allPods.length && git_sha) {32 delete podH.labels.git_sha33 allPods = await getAllPods({...podH, cluster_name})34 const deployedGitSha = allPods[0]?.git_sha35 if (deployedGitSha && deployedGitSha !== git_sha) {36 const {pathsA} = await getRakeEpsPaths({defaultEpScope: epScopes.dockerjs})37 const diffRes = await appDeployDiff({39 diffcmd: 'git log', logLimit: 10, noExtraGitArgs: true,40 })42 existingGitSha: diffRes.existingGitSha, newGitSha: diffRes.newGitSha,43 lastDeployCommitsAgo: diffRes.lastDeployCommitsAgo, lastDeployDurAgo: diffRes.lastDeployDurAgo,44 })45 console.log(diffRes.stdout)46 }47 }48 assertNonEmpty(allPods)51 const podsLen = allPods.length53 if (podArg === 'all') {55 await retMultiPodLogs({cluster_name, pods: allPods, follow, tailLines: tailLines ?? 100})56 return57 }59 const podIdx = podArg ? parseInt(podArg, 10) - 1 : 060 const selectedPod = assertValidIdx(allPods, podIdx, {61 podArg, validRange: `1-${podsLen}`, podNames: _.map(allPods, 'podName'),62 })65 await ret1PodLogs({66 cluster_name, podName: selectedPod.podName, containerName: selectedPod.containerName,67 streamStdout: true, follow, tailLines, timestamps: false, pretty: true68 })69}