@mikarinneoracle/oci-cdk-code-only-preview 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/bin/deploy-code-only.js +12 -3
- package/bin/ocdk.js +34 -1
- package/bin/write-log-config.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -133,7 +133,9 @@ export OCI_FUNCTION_APP_NAME='my-existing-function-app'
|
|
|
133
133
|
|
|
134
134
|
These settings are optional: `OCI_CODE_ONLY_SOURCE_DIR` (defaults to the current directory), `OCI_FUNCTION_MEMORY_MB` (from `func.yaml`, otherwise `256`), and `OCI_FUNCTION_TIMEOUT_SECONDS` (from `func.yaml`, otherwise `30`). `OCI_TENANCY_ID`, `OCI_REGION`, and `OCI_NAMESPACE` are also optional when they can be resolved from the active OCI CLI profile.
|
|
135
135
|
|
|
136
|
-
|
|
136
|
+
Each deploy re-builds `<function-name>.zip` in the project root and uploads that file. The ZIP has the required `function/` directory at its root, includes source files, and excludes `node_modules`, `.git`, `.tools`, `.terraform`, and `cdktf.out`. A successful code-only destroy removes this ZIP after Terraform has destroyed the Function App and infrastructure. The current preview path is function-only: it does not create an API Gateway because the CLI-managed function OCID is not in Terraform state.
|
|
137
|
+
|
|
138
|
+
After a successful code-only deploy, OCDK writes the log IDs for `npx ocdk tail:execution-log` automatically.
|
|
137
139
|
|
|
138
140
|
Use the same flag or environment variable for deletion. OCDK deletes the CLI-managed function first, then lets Terraform destroy the Function App and its infrastructure:
|
|
139
141
|
|
package/bin/deploy-code-only.js
CHANGED
|
@@ -67,12 +67,18 @@ function readFunctionMetadata() {
|
|
|
67
67
|
return { functionName, appName, handler, runtimeName, memory, timeout };
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
function
|
|
70
|
+
function codeOnlyArchiveFileName(functionName) {
|
|
71
|
+
const safeName = functionName.replace(/[^A-Za-z0-9._-]/g, '-');
|
|
72
|
+
return `${safeName || 'function'}.zip`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function createArchive(functionName) {
|
|
71
76
|
if (!fs.existsSync(projectDir) || !fs.statSync(projectDir).isDirectory()) {
|
|
72
77
|
fail(`source directory does not exist: ${projectDir}`);
|
|
73
78
|
}
|
|
74
79
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ocdk-code-only-'));
|
|
75
|
-
const
|
|
80
|
+
const archiveFileName = codeOnlyArchiveFileName(functionName);
|
|
81
|
+
const archivePath = path.join(projectDir, archiveFileName);
|
|
76
82
|
const archiveRoot = path.join(tempDir, 'function');
|
|
77
83
|
const excludedTopLevel = new Set(['node_modules', '.git', '.tools', '.terraform', 'cdktf.out']);
|
|
78
84
|
fs.cpSync(projectDir, archiveRoot, {
|
|
@@ -80,12 +86,15 @@ function createArchive() {
|
|
|
80
86
|
filter: (sourcePath) => {
|
|
81
87
|
const relativePath = path.relative(projectDir, sourcePath);
|
|
82
88
|
if (!relativePath) return true;
|
|
89
|
+
if (relativePath === archiveFileName) return false;
|
|
83
90
|
return !excludedTopLevel.has(relativePath.split(path.sep)[0]);
|
|
84
91
|
},
|
|
85
92
|
});
|
|
93
|
+
fs.rmSync(archivePath, { force: true });
|
|
86
94
|
const zip = spawnSync('zip', ['-q', '-r', archivePath, 'function'], { cwd: tempDir, encoding: 'utf8', shell: false });
|
|
87
95
|
if (zip.error || zip.status !== 0) {
|
|
88
96
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
97
|
+
fs.rmSync(archivePath, { force: true });
|
|
89
98
|
fail(`could not create source archive with zip: ${zip.stderr?.trim() || zip.error?.message || 'unknown error'}`);
|
|
90
99
|
}
|
|
91
100
|
return { tempDir, archivePath };
|
|
@@ -133,7 +142,7 @@ function main() {
|
|
|
133
142
|
const metadata = readFunctionMetadata();
|
|
134
143
|
const applicationId = (process.env.OCI_FUNCTION_APP_ID || '').trim();
|
|
135
144
|
if (!applicationId) fail('Terraform output OCI_FUNCTION_APP_ID is missing. Run through "ocdk deploy --code-only".');
|
|
136
|
-
const archive = createArchive();
|
|
145
|
+
const archive = createArchive(metadata.functionName);
|
|
137
146
|
try {
|
|
138
147
|
const functionId = findFunctionId(applicationId, metadata.functionName);
|
|
139
148
|
const commonArgs = [
|
package/bin/ocdk.js
CHANGED
|
@@ -57,6 +57,22 @@ function resolveCodeOnlyFunctionAppId(env) {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
function resolveCodeOnlyArchivePath(projectDir, env) {
|
|
61
|
+
let functionName = env.OCI_FUNCTION_NAME?.trim();
|
|
62
|
+
if (!functionName) {
|
|
63
|
+
try {
|
|
64
|
+
const funcYaml = fs.readFileSync(path.join(projectDir, 'func.yaml'), 'utf8');
|
|
65
|
+
const match = funcYaml.match(/^\s*name\s*:\s*(?:["']([^"']*)["']|([^#\r\n]*))/m);
|
|
66
|
+
functionName = (match?.[1] || match?.[2] || '').trim();
|
|
67
|
+
} catch {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (!functionName) return undefined;
|
|
72
|
+
const safeName = functionName.replace(/[^A-Za-z0-9._-]/g, '-');
|
|
73
|
+
return path.join(projectDir, `${safeName || 'function'}.zip`);
|
|
74
|
+
}
|
|
75
|
+
|
|
60
76
|
const npmRunCommands = ['deploy', 'diff', 'synth', 'destroy', 'list', 'get'];
|
|
61
77
|
|
|
62
78
|
if (!command || command.startsWith('-')) {
|
|
@@ -164,7 +180,17 @@ if (command === 'deploy' && codeOnlyEnabled) {
|
|
|
164
180
|
shell: false,
|
|
165
181
|
env: { ...env, OCI_FUNCTION_APP_ID: functionAppId },
|
|
166
182
|
});
|
|
167
|
-
process.exit(result.status ?? 1);
|
|
183
|
+
if (result.status !== 0) process.exit(result.status ?? 1);
|
|
184
|
+
const logConfig = spawnSync('node', [path.join(root, 'bin', 'write-log-config.js')], {
|
|
185
|
+
stdio: 'inherit',
|
|
186
|
+
cwd: projectDir,
|
|
187
|
+
shell: false,
|
|
188
|
+
env,
|
|
189
|
+
});
|
|
190
|
+
if (logConfig.status !== 0) {
|
|
191
|
+
console.warn('Code-only function deployed, but log-tail configuration could not be written. Run: npx ocdk write-log-config');
|
|
192
|
+
}
|
|
193
|
+
process.exit(0);
|
|
168
194
|
}
|
|
169
195
|
|
|
170
196
|
// Destroy the CLI-managed archive function before Terraform destroys its
|
|
@@ -203,6 +229,13 @@ if (command === 'destroy' && codeOnlyEnabled) {
|
|
|
203
229
|
shell: false,
|
|
204
230
|
env,
|
|
205
231
|
});
|
|
232
|
+
if (infrastructure.status === 0) {
|
|
233
|
+
const archivePath = resolveCodeOnlyArchivePath(projectDir, env);
|
|
234
|
+
if (archivePath && fs.existsSync(archivePath)) {
|
|
235
|
+
fs.rmSync(archivePath, { force: true });
|
|
236
|
+
console.log(`Removed code-only source archive: ${archivePath}`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
206
239
|
process.exit(infrastructure.status ?? 1);
|
|
207
240
|
}
|
|
208
241
|
|
package/bin/write-log-config.js
CHANGED
|
@@ -10,7 +10,7 @@ const fs = require('fs');
|
|
|
10
10
|
const packageRoot = path.join(__dirname, '..');
|
|
11
11
|
const projectRoot = process.cwd();
|
|
12
12
|
const stackName = process.env.OCI_STACK_NAME || 'oci-stack';
|
|
13
|
-
const stackDir = path.join(
|
|
13
|
+
const stackDir = path.join(packageRoot, 'cdktf.out', 'stacks', stackName);
|
|
14
14
|
|
|
15
15
|
if (!fs.existsSync(stackDir)) {
|
|
16
16
|
console.error('Stack directory not found:', stackDir);
|