Skip to content

Commit

Permalink
[samp-local] fix mac-address bug + .NET improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Aug 17, 2023
1 parent b05c63a commit 6339388
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ Lets you quickly build IAM polices, find SAM policy templates or generate SAM Co
```
Usage: samp policy|p [options]
Opens a wizard thet help you to create and attach a new IAM policy to a resource in your template
Opens a wizard that helps you to create and attach a new IAM policy to a resource in your template
Options:
-t, --template <filename> Template file name (default: "template.yaml")
Expand Down
101 changes: 93 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "samp-cli",
"version": "1.0.40",
"version": "1.0.41",
"description": "CLI tool for extended productivity with AWS Serverless Application Model (SAM)",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -68,6 +68,7 @@
"rimraf": "^5.0.1",
"toml": "^3.0.0",
"tsc-watch": "^6.0.4",
"uuid": "^9.0.0",
"yaml": "^2.3.1",
"yaml-cfn": "^0.3.2"
},
Expand Down
37 changes: 29 additions & 8 deletions src/commands/local/lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,31 @@ import { fork, spawnSync } from 'child_process';
import samConfigParser from '../../../shared/samConfigParser.js';
import { locateHandler } from "./handler-finder.js";
import { findSAMTemplateFile, parse } from "../../../shared/parser.js";
import { v4 as uuidv4 } from "uuid";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const mac = getMac();


let uuid;
if (fs.existsSync(`${homedir()}/.lambda-debug/uuid`)) {
uuid = fs.readFileSync(`${homedir()}/.lambda-debug/uuid`).toString();
} else {
uuid = uuidv4();
try {
fs.mkdirSync(`${homedir()}/.lambda-debug`, { recursive: true });
} catch (e) {
}
fs.writeFileSync(`${homedir()}/.lambda-debug/uuid`, uuid);
}

let targetConfig = {};
if (fs.existsSync(`samconfig.toml`)) {
targetConfig = samConfigParser.parse();
}
targetConfig = {
stack_name: targetConfig.stack_name || process.env.SAMP_STACKNAME,
region: targetConfig.region || process.env.SAMP_REGION,
profile: targetConfig.profile || process.env.SAMP_PROFILE
stack_name: valueOrNull(process.env.SAMP_STACKNAME) || targetConfig.stack_name,
region: valueOrNull(process.env.SAMP_REGION) || targetConfig.region,
profile: valueOrNull(process.env.SAMP_PROFILE) || targetConfig.profile
}
console.log(`Using profile: ${targetConfig.profile || 'default'}`);

Expand Down Expand Up @@ -165,7 +179,7 @@ if (!fs.existsSync(".lambda-debug")) {
});
if (!fs.existsSync(zipFilePath)) {
console.log(`Creating Lambda artifact zip`);
fs.writeFileSync(path.join(__dirname, 'relay', 'config.json'), JSON.stringify({ mac: mac, endpoint: endpoint }));
fs.writeFileSync(path.join(__dirname, 'relay', 'config.json'), JSON.stringify({ uuid, endpoint: endpoint }));
// install npm package sin relay folder
const npm = os.platform() === 'win32' ? 'npm.cmd' : 'npm';
console.log(`Installing npm packages in relay lambda function folder`);
Expand Down Expand Up @@ -214,6 +228,7 @@ const functionSources = functions.map(key => { return { uri: template.Resources[
baseDir = process.env.outDir + "/" + obj.uri;
}
}

const { module, handlerMethod, runtime } = locateHandler(template, obj, baseDir);
map[obj.name] = {
module,
Expand Down Expand Up @@ -253,7 +268,7 @@ client.on('error', function (err) {
client.on('connect', async function () {
console.log('Ready for requests...')
fs.writeFileSync(".lambda-debug", JSON.stringify({ stack, endpoint, certData, functions, template, envConfig: targetConfig, accountId }));
client.subscribe('lambda-debug/event/' + mac);
client.subscribe('lambda-debug/event/' + uuid);
});

const eventQueue = {};
Expand All @@ -277,7 +292,7 @@ client.on('message', async function (topic, message) {

frk.on('message', (result) => {
console.log(`Result: ${result}`);
client.publish(`lambda-debug/callback/${mac}/${obj.sessionId}`, result);
client.publish(`lambda-debug/callback/${uuid}/${obj.sessionId}`, result);
});

//
Expand All @@ -296,7 +311,7 @@ if (!targetConfig.childProcess) {
}
process.on("exit", async (x) => {
// delete certificates
client.publish('lambda-debug/callback/' + mac, JSON.stringify({ event: 'lambda-debug-exit', context: {} }));
client.publish('lambda-debug/callback/' + uuid, JSON.stringify({ event: 'lambda-debug-exit', context: {} }));
client.end();
await iotClient.send(new DetachPolicyCommand({
policyName: policyName,
Expand Down Expand Up @@ -365,3 +380,9 @@ function debugInProgress() {
return fs.existsSync(".lambda-debug");
}

function valueOrNull(str) {
if (!str) {
return null;
}
return str.length > 0 ? str : null;
}
4 changes: 2 additions & 2 deletions src/commands/local/lib/relay/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function publishEvent(event, context) {
}

function publish(payload, sessionId) {
client.subscribe(`lambda-debug/callback/${config.mac}/${sessionId}`);
client.publish('lambda-debug/event/' + config.mac, payload);
client.subscribe(`lambda-debug/callback/${config.uuid}/${sessionId}`);
client.publish('lambda-debug/event/' + config.uuid, payload);
}

10 changes: 5 additions & 5 deletions src/commands/local/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const samConfigParser = require('../../shared/samConfigParser');
const runtimeEnvFinder = require('./runtime-env-finder');
let env;
function setEnvVars(cmd) {
process.env.SAMP_PROFILE = cmd.profile || process.env.AWS_PROFILE || "default";
process.env.SAMP_REGION = cmd.region || process.env.AWS_REGION;
process.env.SAMP_STACKNAME = process.env.SAMP_STACKNAME || cmd.stackName || process.env.stackName;
process.env.SAMP_CDK_STACK_PATH = cmd.construct || process.env.SAMP_CDK_STACK_PATH;
process.env.SAMP_PROFILE = cmd.profile || process.env.AWS_PROFILE || '';
process.env.SAMP_REGION = cmd.region || process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || '';
process.env.SAMP_STACKNAME = process.env.SAMP_STACKNAME || cmd.stackName || '';
process.env.SAMP_CDK_STACK_PATH = cmd.construct || process.env.SAMP_CDK_STACK_PATH || '';
}

async function run(cmd) {
Expand All @@ -38,7 +38,7 @@ async function run(cmd) {

if (cmd.debug) {
await setupDebug(cmd);
if (env.iac === "cdk") {
if (env.isNodeJS) {
process.exit(0);
}
} else if (env.iac === "cdk" && (!cmd.stackName || !cmd.construct)) {
Expand Down
1 change: 1 addition & 0 deletions src/commands/local/runtime-support/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ static void InvokeFunctionThread(object? data)
Console.WriteLine("Method not found.");
return;
}
// do error handling!!!
var response = methodInfo.Invoke(instance, parameterList.ToArray());
var responsesDir = e?.FullPath.Replace("samp-requests", "samp-responses");

Expand Down
10 changes: 5 additions & 5 deletions src/commands/local/runtime-support/dotnet/iac-support/sam.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ async function run(initialised, cmd) {
//process.env.outDir = ".samp-out";
await copyAppsettings();

const dotnetProcess = exec(`dotnet build .samp-out/dotnet.csproj`, {});
const dotnetProcess = exec(`dotnet watch --project dotnet.csproj`, {cwd: `.samp-out`});
dotnetProcess.stderr.on('data', (data) => print(data));
dotnetProcess.stdout.on('data', (data) => {
console.log("dotnet: ", data.toString().replace(/\n$/, ''));
if (data.toString().includes("Time Elapsed") && !initialised) {
if (data.toString().includes("dotnet watch 🚀 Started") && !initialised) {
initialised = true;
const childProcess = exec(`node ${__dirname}../../../../runner.js run`, {});
childProcess.stdout.on('data', (data) => print(data));
childProcess.stderr.on('data', (data) => print(data));
if (!cmd.debug) {
const pythonProcess = exec(`dotnet run`, {cwd: `${process.cwd()}/.samp-out`});
pythonProcess.stderr.on('data', (data) => print(data));
pythonProcess.stdout.on('data', (data) => print(data));
const runProcess = exec(`dotnet run`, {cwd: `${process.cwd()}/.samp-out`});
runProcess.stderr.on('data', (data) => print(data));
runProcess.stdout.on('data', (data) => print(data));
} else {
console.log("You can now select '[SAMP] Debug Lambda functions' and start debugging");
}
Expand Down
Loading

0 comments on commit 6339388

Please sign in to comment.