Skip to content

Commit

Permalink
add more config options
Browse files Browse the repository at this point in the history
  • Loading branch information
cioddi committed Dec 28, 2023
1 parent 34ef292 commit bf0c030
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
Binary file modified examples/config_test/assets/config_test_0.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/config_test/assets/config_test_2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 0 additions & 12 deletions examples/config_test/config_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ sleep 1
echo "Hello, AutoBashCraft"
sleep 1
echo "Hello, AutoBashCraft"
sleep 1
echo "Hello, AutoBashCraft"
sleep 1
echo "Hello, AutoBashCraft"
sleep 1
echo "Hello, AutoBashCraft"
```

<!--@abc: config({"asciinema":{"speed":0.5},"blub":"Bla:"}) -->
Expand All @@ -22,10 +16,4 @@ sleep 1
echo "Hello, AutoBashCraft"
sleep 1
echo "Hello, AutoBashCraft"
sleep 1
echo "Hello, AutoBashCraft"
sleep 1
echo "Hello, AutoBashCraft"
sleep 1
echo "Hello, AutoBashCraft"
```
45 changes: 29 additions & 16 deletions src/executeCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ export interface ConfigType {
cols: number;
rows: number;
resolution: [number, number];
typingPause: number;
promptPause: number;
};
withDocker: boolean;
}

// default config
const defaultConfig: ConfigType = {
asciinema: {
speed: 2,
cols: 120,
rows: 30,
cols: 100,
rows: 20,
resolution: [120, 30],
typingPause: 0.001,
promptPause: 1,
},
withDocker: false,
}
let config: ConfigType = {...defaultConfig};

Expand All @@ -39,6 +45,24 @@ const hostRecordingPath = "/tmp/autobashcraft/recordings";
const hostWorkspacePath = "/tmp/autobashcraft/workspace";
const containerWorkspacePath = hostWorkspacePath;

const initializeRuntime = async () => {

let containerId;
let dockerSockVolume = "";
if (config.withDocker) {
dockerSockVolume = "-v /var/run/docker.sock:/var/run/docker.sock:z";
gid = "$(getent group docker | cut -d: -f3)";
}
const containerStartCmd = `docker run ${dockerSockVolume} --group-add docker --group-add sudo --network host -dit --rm --user ${uid}:${gid} -v ${hostRecordingPath}:${hostRecordingPath} -v ${hostWorkspacePath}:${containerWorkspacePath} cioddi/autobashcraft:latest`;
const startResult = await execProm(containerStartCmd);
containerId = startResult.stdout.trim();

const cleanupWorkspace = await execProm(
`docker exec -u root ${containerId} bash -c 'rm -rf ${containerWorkspacePath}/* && rm -rf /tmp/autobashcraft/recordings/* && chown ${uid}:${gid} /tmp/autobashcraft -R && chmod 777 -R ${containerWorkspacePath} && ls -alh ${containerWorkspacePath}/'`
);

return containerId;
}

// Function to execute parsedCommands inside a new Docker container
export async function executeCommands({
Expand All @@ -52,20 +76,9 @@ export async function executeCommands({
assetPath: string;
withDocker: boolean;
}) {
let containerId;
config.withDocker = withDocker;
let containerId = await initializeRuntime();
try {
let dockerSockVolume = "";
if (withDocker) {
dockerSockVolume = "-v /var/run/docker.sock:/var/run/docker.sock:z";
gid = "$(getent group docker | cut -d: -f3)";
}
const containerStartCmd = `docker run ${dockerSockVolume} --group-add docker --group-add sudo --network host -dit --rm --user ${uid}:${gid} -v ${hostRecordingPath}:${hostRecordingPath} -v ${hostWorkspacePath}:${containerWorkspacePath} cioddi/autobashcraft:latest`;
const startResult = await execProm(containerStartCmd);
containerId = startResult.stdout.trim();

const cleanupWorkspace = await execProm(
`docker exec -u root ${containerId} bash -c 'rm -rf ${containerWorkspacePath}/* && rm -rf /tmp/autobashcraft/recordings/* && chown ${uid}:${gid} /tmp/autobashcraft -R && chmod 777 -R ${containerWorkspacePath} && ls -alh ${containerWorkspacePath}/'`
);
//console.log(cleanupWorkspace);

let castFilename = "";
Expand All @@ -83,7 +96,7 @@ export async function executeCommands({
console.log("script /app/script created with contents:", commands);
const result = await execProm(execCommandCmd);
// execute the script using a custom version of asciinema-rec_script
const execScriptCmd = `docker exec --user ${uid}:${gid} --privileged ${containerId} bash -c 'asciinema-rec_script ${containerWorkspacePath}/script && ls -al ${containerWorkspacePath} && cp ${containerWorkspacePath}/script.cast ${hostRecordingPath}/${castFilename}.cast && rm ${containerWorkspacePath}/script.cast'`;
const execScriptCmd = `docker exec -t --user ${uid}:${gid} --privileged ${containerId} bash -c 'stty rows ${config.asciinema.rows} cols ${config.asciinema.cols} && TYPING_PAUSE=0.01 asciinema-rec_script ${containerWorkspacePath}/script && ls -al ${containerWorkspacePath} && cp ${containerWorkspacePath}/script.cast ${hostRecordingPath}/${castFilename}.cast && rm ${containerWorkspacePath}/script.cast'`;
const result2 = await execProm(execScriptCmd);
console.log(result2.stdout);
console.log(result2.stderr);
Expand Down

0 comments on commit bf0c030

Please sign in to comment.