-
Notifications
You must be signed in to change notification settings - Fork 31
/
logs.js
163 lines (148 loc) · 5.27 KB
/
logs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Copyright 2019 Adobe Inc. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const { flags } = require('@oclif/command')
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const rtLib = require('@adobe/aio-lib-runtime')
const printLogs = rtLib.utils.printLogs
const chalk = require('chalk')
class ActivationLogs extends RuntimeBaseCommand {
async run () {
const { args, flags } = this.parse(ActivationLogs)
const ow = await this.wsk()
const configuredLogForwarding = await ow.logForwarding.get()
if (configuredLogForwarding !== undefined && configuredLogForwarding !== null && !Array.isArray(configuredLogForwarding) && typeof configuredLogForwarding === 'object') {
const destinations = Object.keys(configuredLogForwarding)
if (destinations.length === 1 && destinations[0] !== 'adobe_io_runtime') {
this.log(`Namespace is configured with custom log forwarding destination: '${destinations[0]}'. ` +
'Please use corresponding logging platform to view logs.')
return
}
}
let limit = 1
if (flags.last) {
limit = 1
} else if (flags.limit) {
limit = flags.limit
}
if (!args.activationId) {
const owOptions = await this.getOptions()
owOptions.auth = owOptions.api_key
delete owOptions.api_key
let components
const filterActions = []
const filterPackageActions = (pkgName, actions) => {
// TODO: Following line is a temporary workaround till we figure out how to deal with __APP_PACKAGE__
pkgName = pkgName.replace(/__APP_PACKAGE__/g, '')
Object.keys(actions).forEach((actionName) => {
filterActions.push(pkgName + '/' + actionName)
})
}
if (flags.package) {
if (flags.deployed) {
filterActions.push(flags.package + '/')
} else { // Check in the manifest
components = await rtLib.utils.setPaths()
if (!Object.keys(components.manifestContent.packages).includes(flags.package)) {
this.handleError(`Could not find package ${flags.package} in manifest`)
}
filterPackageActions(flags.package, components.manifestContent.packages[flags.package].actions)
}
}
if (flags.manifest) {
components = await rtLib.utils.setPaths()
Object.entries(components.manifestContent.packages).forEach((packageTuple) => {
filterPackageActions(packageTuple[0], packageTuple[1].actions)
})
} else if (flags.action) {
filterActions.push(flags.action)
}
await rtLib.printActionLogs({ ow: owOptions }, this.log, limit, filterActions, flags.strip, flags.poll || flags.tail || flags.watch)
} else {
const logger = this.log
return ow.activations.logs(args.activationId).then((result) => {
logger(chalk.dim('=== ') + chalk.bold('activation logs %s'), args.activationId)
printLogs(result, flags.strip, logger)
}, (err) => {
this.handleError('failed to retrieve logs for activation', err)
})
}
}
}
ActivationLogs.args = [
{
name: 'activationId'
}
]
ActivationLogs.flags = {
...RuntimeBaseCommand.flags,
action: flags.string({
description: 'Fetch logs for a specific action',
exclusive: ['manifest', 'package'],
char: 'a'
}),
manifest: flags.boolean({
description: 'Fetch logs for all actions in the manifest',
exclusive: ['package', 'action'],
char: 'm'
}),
package: flags.string({
description: 'Fetch logs for a specific package in the manifest',
exclusive: ['manifest', 'action'],
char: 'p'
}),
deployed: flags.boolean({
description: 'Fetch logs for all actions deployed under a specific package',
exclusive: ['manifest', 'action'],
char: 'd'
}),
last: flags.boolean({
char: 'l',
description: 'retrieves the most recent activation logs'
}),
strip: flags.boolean({
char: 'r',
description: 'strip timestamp information and output first line only',
default: false
}),
limit: flags.integer({
description: 'return logs only from last LIMIT number of activations',
exclusive: ['last']
}),
tail: flags.boolean({
description: 'Fetch logs continuously',
default: false,
char: 't',
exclusive: ['watch', 'poll']
}),
watch: flags.boolean({
description: 'Fetch logs continuously',
default: false,
char: 'w',
exclusive: ['tail', 'poll']
}),
poll: flags.boolean({
description: 'Fetch logs continuously',
default: false,
char: 'o',
exclusive: ['watch', 'tail']
})
}
ActivationLogs.description = 'Retrieves the Logs for an Activation'
ActivationLogs.aliases = [
'runtime:activation:log',
'runtime:log',
'runtime:logs',
'rt:activation:logs',
'rt:activation:log',
'rt:log',
'rt:logs'
]
module.exports = ActivationLogs