This commit is contained in:
Raven Scott 2024-09-19 01:06:05 -04:00
parent 04f0d02a97
commit 2609ac9e3f

View File

@ -1272,6 +1272,102 @@ module.exports = {
This command interacts with Docker and your Holesail setup to ensure all active connections are listed. It provides a quick snapshot of your current port connections without needing to dive into manual configurations. This command interacts with Docker and your Holesail setup to ensure all active connections are listed. It provides a quick snapshot of your current port connections without needing to dive into manual configurations.
#### The docker_exec.js script
The ability to execute commands inside Docker containers programmatically can be extremely useful when managing containers dynamically. The following script leverages the `cmd-promise` module and the Docker CLI to execute commands inside a Docker container, while handling both output and errors efficiently. Let's walk through the script to see how it works.
```javascript
const cmd = require('cmd-promise')
var args = process.argv
let code = args.slice(4).join(" ")
cmd(`docker exec -i `+ args[2] + ` /bin/bash -c ` + "' cd " + args[3] + " && " + code + "'")
.then(out => {
if (!out.stdout) {
console.log("Done")
}
console.log(out.stdout)
})
.catch(err => {
console.log(err.message.toString().replace("Command failed: " + `docker exec -i `+ args[2] + ` /bin/bash -c ` + "' cd " + args[3] + " && \"" + code + "\"'", ""))
})
```
This simple yet powerful Node.js script facilitates the execution of commands inside Docker containers. The main purpose is to automate the process of running shell commands inside a container's environment, making it a vital tool for platform administrators, especially when automating containerized environments.
### Importing the `cmd-promise` Module
The first step is to import the `cmd-promise` module, which is a handy utility for executing shell commands. It provides a promise-based structure that helps manage asynchronous operations.
```javascript
const cmd = require('cmd-promise')
```
By using promises, this module allows us to wait for the execution of a command to complete and handle the result or any errors that occur.
### Parsing Command-Line Arguments
The script captures the command-line arguments passed to it when executed. The `process.argv` array holds these arguments. In this case, the script expects a few arguments: the Docker container ID, the directory to navigate to, and the command to execute.
```javascript
var args = process.argv
let code = args.slice(4).join(" ")
```
- **`args[2]`**: Represents the Docker container ID.
- **`args[3]`**: Represents the directory inside the Docker container.
- **`args.slice(4)`**: Grabs the command to be executed inside the container and joins it into a single string.
### Building and Executing the Docker Command
Next, the script constructs the command that will be executed inside the container. The `docker exec` command is used to run a command inside a running container.
```javascript
cmd(`docker exec -i `+ args[2] + ` /bin/bash -c ` + "' cd " + args[3] + " && " + code + "'")
```
Heres what the constructed command looks like:
```bash
docker exec -i <container_id> /bin/bash -c 'cd <directory> && <command>'
```
This command does the following:
- **`docker exec -i`**: Runs the command inside the specified Docker container interactively.
- **`/bin/bash -c`**: Runs the command inside a Bash shell within the container.
- **`cd <directory>`**: Changes the current directory to the one specified by the user.
- **`&& <command>`**: Executes the command passed by the user after changing to the specified directory.
### Handling Command Output
Once the command is executed, the script handles the output using promises. It checks if there is any output from the command, and logs it to the console. If there is no output, it simply prints "Done."
```javascript
.then(out => {
if (!out.stdout) {
console.log("Done")
}
console.log(out.stdout)
})
```
If the command runs successfully but produces no output, the script logs "Done" to indicate that it has completed. Otherwise, it logs the output of the command.
### Handling Errors Gracefully
In the event of an error, the script catches it and displays a cleaned-up error message to the user. This is particularly helpful when dealing with Docker or shell command failures.
```javascript
.catch(err => {
console.log(err.message.toString().replace("Command failed: " + `docker exec -i `+ args[2] + ` /bin/bash -c ` + "' cd " + args[3] + " && \"" + code + "\"'", ""))
})
```
The error handler strips out any unnecessary details from the error message, showing only the relevant parts. This improves the readability of error logs, making it easier to diagnose problems.
This script is a great example of how to programmatically interact with Docker containers using Node.js. It dynamically executes commands inside a container by building a `docker exec` command, handling output, and catching errors along the way. Whether it's navigating directories, running system commands, or processing results, this approach simplifies the management of containerized environments in automated workflows.
### Creating a New Port via Holesail ### Creating a New Port via Holesail
With **`/create-port`**, you can create a new Holesail connector port. You provide a connection name and a connection hash, and this command will register the connection within our JUMP infrastructure. The request is sent to the API that handles the creation of ports and returns the necessary connection details. With **`/create-port`**, you can create a new Holesail connector port. You provide a connection name and a connection hash, and this command will register the connection within our JUMP infrastructure. The request is sent to the API that handles the creation of ports and returns the necessary connection details.