Skip to content

Commit 7f1568e

Browse files
authored
Merge pull request #46 from nitrictech/feature/deployment-logging
Add basic deployment logging
2 parents 3bd9ebf + 7e6da8f commit 7f1568e

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed

packages/common/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
"eventemitter3": "^4.0.7",
2020
"multimatch": "^5.0.0",
2121
"rxjs": "^6.6.3",
22+
"stream-to-promise": "^3.0.0",
2223
"tslib": "^1",
2324
"yaml": "^1.10.0"
2425
},
2526
"devDependencies": {
2627
"@types/jest": "^26.0.15",
2728
"@types/listr": "latest",
2829
"@types/node": "^10",
30+
"@types/stream-to-promise": "^2.2.1",
2931
"jest": "^26.6.1",
3032
"openapi-types": "^7.2.3",
3133
"rimraf": "3.0.2",

packages/common/src/stack/stack.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import rimraf from 'rimraf';
1818
import { Function } from './function';
1919
import { Site } from './site';
2020
import { findFileRead } from '../utils';
21-
//import multimatch from 'multimatch';
21+
22+
const NITRIC_DIRECTORY = '.nitric';
2223

2324
export class Stack {
2425
private file: string;
@@ -107,6 +108,43 @@ export class Stack {
107108
return `${STAGING_DIR}/${this.name}`;
108109
}
109110

111+
private async makeRelativeDirectory(directory: string): Promise<string> {
112+
const dir = path.join(this.getDirectory(), directory);
113+
114+
if (!fs.existsSync(dir)) {
115+
await fs.promises.mkdir(dir, { recursive: true })
116+
}
117+
118+
return dir;
119+
}
120+
121+
/**
122+
* Returns the nitric directory and creates it if it doesn't exist
123+
*/
124+
async makeNitricDirectory(): Promise<string> {
125+
return await this.makeRelativeDirectory(`./${NITRIC_DIRECTORY}/`);
126+
}
127+
128+
/**
129+
* Returns the nitric log directory and creates it if it doesn't exist
130+
*/
131+
async makeLoggingDirectory(): Promise<string> {
132+
return await this.makeRelativeDirectory(`./${NITRIC_DIRECTORY}/logs/`)
133+
}
134+
135+
/**
136+
* Returns a new log file location. If the log directories are missing, they will be created.
137+
* @param prefix used along with the current time to generate a unique log filename.
138+
*/
139+
async getLoggingFile(prefix: string): Promise<string> {
140+
const currentTime = (new Date().getTime());
141+
const logFileName = `${prefix}-${currentTime}`;
142+
143+
const loggingDirectory = await this.makeLoggingDirectory();
144+
145+
return path.join(loggingDirectory, `./${logFileName}.log`);
146+
}
147+
110148
/**
111149
* Parse a Nitric Stack definition from the given file.
112150
*

packages/plugins/aws/src/tasks/deploy/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { LocalWorkspace } from '@pulumi/pulumi/x/automation';
1111
import { ecr } from '@pulumi/aws';
1212
import { createSite } from './site';
1313
import { createEntrypoints } from './entrypoint';
14+
import fs from 'fs';
1415

1516
/**
1617
* Common Task Options
@@ -31,7 +32,7 @@ interface DeployOptions extends CommonOptions {
3132
}
3233

3334
/**
34-
* Deploys the given Nitric Stack to AWS as a CloudFormation Stack
35+
* Deploys the given Nitric Stack
3536
*/
3637
export class Deploy extends Task<void> {
3738
private stack: Stack;
@@ -57,7 +58,9 @@ export class Deploy extends Task<void> {
5758
this.update('Defining functions');
5859

5960
try {
60-
// Upload the stack to AWS
61+
// Upload the stack
62+
const logFile = await stack.getLoggingFile('deploy:aws');
63+
6164
const pulumiStack = await LocalWorkspace.createOrSelectStack({
6265
// TODO: Incorporate additional stack detail. E.g. dev/test/prod
6366
stackName: 'aws',
@@ -93,8 +96,14 @@ export class Deploy extends Task<void> {
9396
},
9497
});
9598
await pulumiStack.setConfig('aws:region', { value: region });
99+
const update = this.update.bind(this);
96100
// deploy the stack, tailing the logs to console
97-
const upRes = await pulumiStack.up({ onOutput: this.update.bind(this) });
101+
const upRes = await pulumiStack.up({
102+
onOutput: (out: string) => {
103+
update(out);
104+
fs.appendFileSync(logFile, out);
105+
},
106+
});
98107

99108
console.log(upRes);
100109
} catch (e) {

packages/plugins/azure/src/tasks/deploy/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createFunctionAsApp } from './function';
77
import { createQueue } from './queue';
88
import { createAPI } from './api';
99
import * as pulumi from '@pulumi/pulumi';
10+
import fs from 'fs';
1011
//import { createSchedule } from './schedule';
1112

1213
interface DeployOptions {
@@ -34,10 +35,9 @@ export class Deploy extends Task<void> {
3435
const { stack, orgName, adminEmail, region } = this;
3536
const { buckets = [], apis = [], topics = [], schedules = [], queues = [] } = stack.asNitricStack();
3637

37-
const messages: string[] = [];
38-
3938
try {
40-
// Upload the stack to AWS
39+
// Upload the stack
40+
const logFile = await stack.getLoggingFile('deploy:azure');
4141
const pulumiStack = await LocalWorkspace.createOrSelectStack({
4242
// TODO: Incorporate additional stack detail. E.g. dev/test/prod
4343
stackName: 'azure',
@@ -130,13 +130,12 @@ export class Deploy extends Task<void> {
130130
const upRes = await pulumiStack.up({
131131
onOutput: (out) => {
132132
update(out);
133-
messages.push(out);
133+
fs.appendFileSync(logFile, out);
134134
},
135135
});
136136
console.log(upRes);
137137
} catch (e) {
138138
console.log(e);
139-
console.log(messages);
140139
}
141140
}
142141
}

packages/plugins/gcp/src/tasks/deploy/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { LocalWorkspace } from '@pulumi/pulumi/x/automation';
99
import { createSite } from './site';
1010
import { createEntrypoints } from './entrypoints';
1111
import * as pulumi from '@pulumi/pulumi';
12+
import fs from 'fs';
1213

1314
interface CommonOptions {
1415
gcpProject: string;
@@ -40,7 +41,8 @@ export class Deploy extends Task<void> {
4041
const authClient = await auth.getClient();
4142

4243
try {
43-
// Upload the stack to AWS
44+
// Upload the stack
45+
const logFile = await stack.getLoggingFile('deploy:gcp');
4446
const pulumiStack = await LocalWorkspace.createOrSelectStack({
4547
// TODO: Incorporate additional stack detail. E.g. dev/test/prod
4648
stackName: 'gcp',
@@ -79,8 +81,14 @@ export class Deploy extends Task<void> {
7981
});
8082
await pulumiStack.setConfig('gcp:project', { value: gcpProject });
8183
await pulumiStack.setConfig('gcp:region', { value: region });
84+
const update = this.update.bind(this);
8285
// deploy the stack, tailing the logs to console
83-
const upRes = await pulumiStack.up({ onOutput: this.update.bind(this) });
86+
const upRes = await pulumiStack.up({
87+
onOutput: (out: string) => {
88+
update(out);
89+
fs.appendFileSync(logFile, out);
90+
},
91+
});
8492

8593
console.log(upRes);
8694
} catch (e) {

yarn.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,12 +658,14 @@ __metadata:
658658
"@types/jest": ^26.0.15
659659
"@types/listr": latest
660660
"@types/node": ^10
661+
"@types/stream-to-promise": ^2.2.1
661662
eventemitter3: ^4.0.7
662663
jest: ^26.6.1
663664
multimatch: ^5.0.0
664665
openapi-types: ^7.2.3
665666
rimraf: 3.0.2
666667
rxjs: ^6.6.3
668+
stream-to-promise: ^3.0.0
667669
ts-jest: ^26.4.3
668670
tslib: ^1
669671
typescript: ^3.3

0 commit comments

Comments
 (0)