dsmt 0.1.0 → 0.2.0

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.
@@ -0,0 +1,99 @@
1
+ # Contributing to Docker Storage Migration Tool (DSMT)
2
+
3
+ Thank you for considering contributing to DSMT! This document provides guidelines and instructions to help you contribute effectively.
4
+
5
+ ## Code of Conduct
6
+
7
+ This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.
8
+
9
+ ## How Can I Contribute?
10
+
11
+ ### Reporting Bugs
12
+
13
+ Before submitting a bug report:
14
+
15
+ - Check the [issue tracker](https://github.com/itskdhere/dsmt/issues) to see if the issue has already been reported
16
+ - If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/itskdhere/dsmt/issues/new?template=bug_report.md)
17
+
18
+ When filing a bug report, please include:
19
+
20
+ - A clear and descriptive title
21
+ - Steps to reproduce the issue
22
+ - Expected and actual behavior
23
+ - System information (OS, Docker version, Node.js version)
24
+ - Any relevant logs or error messages
25
+
26
+ ### Suggesting Features
27
+
28
+ We welcome feature suggestions! [Open an issue](https://github.com/itskdhere/dsmt/issues/new?template=feature_request.md) with your idea, providing as much context and detail as possible.
29
+
30
+ ### Pull Requests
31
+
32
+ 1. Fork the repository
33
+ 2. Create a new branch for your feature or bugfix
34
+ 3. Make your changes
35
+ 4. Ensure your code follows the project style and passes all tests
36
+ 5. Submit a pull request
37
+
38
+ ## Development Setup
39
+
40
+ ```bash
41
+ # Clone your fork of the repo
42
+ git clone https://github.com/YOUR-USERNAME/dsmt.git
43
+ cd dsmt
44
+
45
+ # Install dependencies
46
+ npm install
47
+
48
+ # Start development mode (watches for changes)
49
+ npm run dev
50
+
51
+ # Link the package globally for testing
52
+ npm link
53
+ ```
54
+
55
+ ## Project Structure
56
+
57
+ - cli.ts - CLI entry point
58
+ - cmd - Command implementations
59
+ - docker - Docker API wrapper
60
+ - types - TypeScript type definitions
61
+
62
+ ## Coding Guidelines
63
+
64
+ - Use TypeScript for all new code
65
+ - Follow existing code style (2 spaces for indentation)
66
+ - Add appropriate error handling and logging
67
+ - Document new functions and methods
68
+
69
+ ## Commit Messages
70
+
71
+ - Use the present tense ("Add feature" not "Added feature")
72
+ - Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
73
+ - Limit the first line to 72 characters
74
+ - Reference issues and pull requests liberally after the first line
75
+
76
+ ## Testing Your Changes
77
+
78
+ Please ensure your changes work properly with:
79
+
80
+ - Different types of Docker volumes
81
+ - Bind mounts with various path structures
82
+ - Different operating systems (if possible)
83
+
84
+ ## Submitting Changes
85
+
86
+ 1. Push your changes to your fork
87
+ 2. Submit a pull request to the main repository
88
+ 3. The title of your PR should clearly describe the change
89
+ 4. Link any relevant issues in the PR description
90
+
91
+ ## License
92
+
93
+ By contributing to DSMT, you agree that your contributions will be licensed under the project's MIT License.
94
+
95
+ ## Questions?
96
+
97
+ Feel free to open an issue if you have any questions about contributing.
98
+
99
+ Thank you for contributing to DSMT!
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # 🐳 Docker Storage Migration Tool (DSMT) 📦
1
+ # 🐳 Docker Storage Migration Tool 📦
2
2
 
3
- A command-line utility for seamlessly exporting and importing Docker volumes and bind mounts.
3
+ `dsmt` is a command-line utility for seamlessly exporting and importing Docker volumes and bind mounts.
4
4
 
5
5
  ## 🔍 Overview
6
6
 
@@ -52,7 +52,7 @@ dsmt import /path/to/tarball.tar.gz /path/to/bind/mount
52
52
  Both commands support the following options:
53
53
 
54
54
  - `-v, --volume`: Explicitly specify source/destination as a Docker volume
55
- - `-m, --mount`: Explicitly specify source/destination as a bind mount
55
+ - `-b, --bind`: Explicitly specify source/destination as a bind mount
56
56
 
57
57
  The tool will automatically detect the source/destination type in most cases, but you can use these flags to be explicit.
58
58
 
@@ -72,19 +72,13 @@ dsmt export /var/www/html /backups
72
72
  dsmt import ./html.tar.gz /var/www/html
73
73
  ```
74
74
 
75
- ## 🛠️ Development
75
+ ## 🛠️ Contribution
76
76
 
77
- ```bash
78
- # Clone the repository
79
- git clone https://github.com/itskdhere/dsmt.git
80
- cd dsmt
77
+ Please refer to the [CONTRIBUTING.md](CONTRIBUTING.md) file for guidelines on contributing to this project.
81
78
 
82
- # Install dependencies
83
- npm install
79
+ ## 🔒 Security
84
80
 
85
- # Run the development build
86
- npm run dev
87
- ```
81
+ Please refer to the [SECURITY.md](SECURITY.md) file for security-related issues and reporting.
88
82
 
89
83
  ## 📄 License
90
84
 
package/SECURITY.md ADDED
@@ -0,0 +1,12 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ | Version | Supported |
6
+ | ------- | ------------------ |
7
+ | > 0.2.0 | :white_check_mark: |
8
+
9
+ ## Reporting a Vulnerability
10
+
11
+ :octocat: https://github.com/itskdhere/dsmt/security/advisories/new
12
+ 📧 [support@itskdhere.eu.org](mailto:support@itskdhere.eu.org)
package/dist/cli.js CHANGED
@@ -6,13 +6,13 @@ const program = new Command();
6
6
  program
7
7
  .name("dsmt")
8
8
  .description("Docker Storage Migration Tool")
9
- .version("0.1.0");
9
+ .version("0.2.0");
10
10
  program
11
11
  .command("export")
12
12
  .argument("<src>", "volume name or bind mount path")
13
13
  .argument("<dst>", "path to export to")
14
14
  .option("-v, --volume", "Volume")
15
- .option("-m, --mount", "Mount")
15
+ .option("-b, --bind", "Bind Mount")
16
16
  .description("Export a Docker Volume or Bind Mount to a Tarball")
17
17
  .action(async (src, dst, options) => await exportCmd(src, dst, options));
18
18
  program
@@ -20,7 +20,7 @@ program
20
20
  .argument("<src>", "path to import from")
21
21
  .argument("<dst>", "volume name or bind mount path")
22
22
  .option("-v, --volume", "Volume")
23
- .option("-m, --mount", "Mount")
23
+ .option("-b, --bind", "Bind Mount")
24
24
  .description("Import a Docker Volume or Bind Mount from a Tarball")
25
25
  .action(async (src, dst, options) => await importCmd(src, dst, options));
26
26
  program.parse(process.argv);
@@ -3,14 +3,15 @@ import chalk from "chalk";
3
3
  import ora from "ora";
4
4
  import path from "path";
5
5
  import { ensureAbsolutePath } from "../lib/etc/utils.js";
6
+ const image = "busybox:latest";
6
7
  export default async function exportCmd(src, dst, options) {
7
- if (options.volume && options.mount) {
8
- console.error(chalk.red("Cannot use both --volume and --mount options at the same time."));
8
+ if (options.volume && options.bind) {
9
+ console.error(chalk.red("Cannot use both --volume and --bind options at the same time."));
9
10
  process.exit(1);
10
11
  }
11
- if (!options.volume && !options.mount) {
12
- if (src.startsWith("/") || src.startsWith("./") || src.startsWith("../")) {
13
- options.mount = true;
12
+ if (!options.volume && !options.bind) {
13
+ if (src.includes("/") || src.includes("\\")) {
14
+ options.bind = true;
14
15
  console.log(chalk.blue(`Auto-detected bind mount path: ${src}`));
15
16
  }
16
17
  else {
@@ -19,13 +20,13 @@ export default async function exportCmd(src, dst, options) {
19
20
  }
20
21
  }
21
22
  if (options.volume) {
22
- await exportVolume(src, dst, options);
23
+ await exportVolume(src, dst);
23
24
  }
24
- else if (options.mount) {
25
- await exportMount(src, dst, options);
25
+ else if (options.bind) {
26
+ await exportBind(src, dst);
26
27
  }
27
28
  }
28
- async function exportMount(src, dst, options) {
29
+ async function exportBind(src, dst) {
29
30
  const spinner = ora("Preparing to export Docker bind mount...").start();
30
31
  try {
31
32
  const srcPath = ensureAbsolutePath(src);
@@ -33,11 +34,22 @@ async function exportMount(src, dst, options) {
33
34
  const name = path.basename(srcPath);
34
35
  spinner.text = `Exporting bind mount from ${chalk.blue(srcPath)} to ${chalk.green(dst)}`;
35
36
  await docker.run({
36
- name: name,
37
- src: srcPath,
38
- dst: dstPath,
39
- cmd: ["tar", "czf", `/dst/${name}.tar.gz`, "-C", "/src", "."],
40
- options,
37
+ name: `dsmt-export-${name}`,
38
+ rm: true,
39
+ mounts: [
40
+ {
41
+ Type: "bind",
42
+ Source: srcPath,
43
+ Target: "/src",
44
+ },
45
+ {
46
+ Type: "bind",
47
+ Source: dstPath,
48
+ Target: "/dst",
49
+ },
50
+ ],
51
+ image: image,
52
+ cmdArgs: ["tar", "czf", `/dst/${name}.tar.gz`, "-C", "/src", "."],
41
53
  });
42
54
  spinner.succeed(`Successfully exported bind mount to ${chalk.green(dstPath)}`);
43
55
  }
@@ -46,7 +58,7 @@ async function exportMount(src, dst, options) {
46
58
  process.exit(1);
47
59
  }
48
60
  }
49
- async function exportVolume(src, dst, options) {
61
+ async function exportVolume(src, dst) {
50
62
  const spinner = ora("Preparing to export Docker volume...").start();
51
63
  try {
52
64
  const volumeName = src;
@@ -54,11 +66,22 @@ async function exportVolume(src, dst, options) {
54
66
  const name = path.basename(src);
55
67
  spinner.text = `Exporting volume ${chalk.blue(src)} to ${chalk.green(dstPath)}`;
56
68
  await docker.run({
57
- name: name,
58
- src: volumeName,
59
- dst: dstPath,
60
- cmd: ["tar", "czf", `/dst/${name}.tar.gz`, "-C", "/src", "."],
61
- options,
69
+ name: `dsmt-export-${name}`,
70
+ rm: true,
71
+ mounts: [
72
+ {
73
+ Type: "volume",
74
+ Source: volumeName,
75
+ Target: "/src",
76
+ },
77
+ {
78
+ Type: "bind",
79
+ Source: dstPath,
80
+ Target: "/dst",
81
+ },
82
+ ],
83
+ image,
84
+ cmdArgs: ["tar", "czf", `/dst/${name}.tar.gz`, "-C", "/src", "."],
62
85
  });
63
86
  spinner.succeed(`Successfully exported volume to ${chalk.green(dstPath)}`);
64
87
  }
@@ -4,14 +4,15 @@ import ora from "ora";
4
4
  import path from "path";
5
5
  import fs from "fs";
6
6
  import { ensureAbsolutePath } from "../lib/etc/utils.js";
7
+ const image = "busybox:latest";
7
8
  export default async function importCmd(src, dst, options) {
8
- if (options.volume && options.mount) {
9
- console.error(chalk.red("Cannot use both --volume and --mount options at the same time."));
9
+ if (options.volume && options.bind) {
10
+ console.error(chalk.red("Cannot use both --volume and --bind options at the same time."));
10
11
  process.exit(1);
11
12
  }
12
- if (!options.volume && !options.mount) {
13
- if (dst.startsWith("/") || dst.startsWith("./") || dst.startsWith("../")) {
14
- options.mount = true;
13
+ if (!options.volume && !options.bind) {
14
+ if (dst.includes("/") || dst.includes("\\")) {
15
+ options.bind = true;
15
16
  console.log(chalk.blue(`Auto-detected bind mount path: ${dst}`));
16
17
  }
17
18
  else {
@@ -20,13 +21,13 @@ export default async function importCmd(src, dst, options) {
20
21
  }
21
22
  }
22
23
  if (options.volume) {
23
- await importVolume(src, dst, options);
24
+ await importVolume(src, dst);
24
25
  }
25
- else if (options.mount) {
26
- await importMount(src, dst, options);
26
+ else if (options.bind) {
27
+ await importBind(src, dst);
27
28
  }
28
29
  }
29
- async function importMount(src, dst, options) {
30
+ async function importBind(src, dst) {
30
31
  const spinner = ora("Preparing to import to Docker bind mount...").start();
31
32
  try {
32
33
  const srcPath = ensureAbsolutePath(src);
@@ -35,13 +36,25 @@ async function importMount(src, dst, options) {
35
36
  throw new Error(`Source file ${srcPath} does not exist`);
36
37
  }
37
38
  const name = path.basename(srcPath);
39
+ const srcDir = path.dirname(srcPath);
38
40
  spinner.text = `Importing from ${chalk.blue(srcPath)} to bind mount ${chalk.green(dstPath)}`;
39
41
  await docker.run({
40
- name: name,
41
- src: srcPath,
42
- dst: dstPath,
43
- cmd: ["tar", "xzf", `/src/${name}`, "-C", "/dst"],
44
- options,
42
+ name: `dsmt-import-${name}`,
43
+ rm: true,
44
+ mounts: [
45
+ {
46
+ Type: "bind",
47
+ Source: srcDir,
48
+ Target: "/src",
49
+ },
50
+ {
51
+ Type: "bind",
52
+ Source: dstPath,
53
+ Target: "/dst",
54
+ },
55
+ ],
56
+ image: image,
57
+ cmdArgs: ["tar", "xzf", `/src/${name}`, "-C", "/dst"],
45
58
  });
46
59
  spinner.succeed(`Successfully imported to bind mount at ${chalk.green(dstPath)}`);
47
60
  }
@@ -50,7 +63,7 @@ async function importMount(src, dst, options) {
50
63
  process.exit(1);
51
64
  }
52
65
  }
53
- async function importVolume(src, dst, options) {
66
+ async function importVolume(src, dst) {
54
67
  const spinner = ora("Preparing to import to Docker volume...").start();
55
68
  try {
56
69
  const srcPath = ensureAbsolutePath(src);
@@ -59,13 +72,25 @@ async function importVolume(src, dst, options) {
59
72
  throw new Error(`Source file ${srcPath} does not exist`);
60
73
  }
61
74
  const name = path.basename(srcPath);
75
+ const srcDir = path.dirname(srcPath);
62
76
  spinner.text = `Importing from ${chalk.blue(srcPath)} to volume ${chalk.green(volumeName)}`;
63
77
  await docker.run({
64
- name: name,
65
- src: srcPath,
66
- dst: volumeName,
67
- cmd: ["tar", "xzf", `/src/${name}`, "-C", "/dst"],
68
- options,
78
+ name: `dsmt-import-${name}`,
79
+ rm: true,
80
+ mounts: [
81
+ {
82
+ Type: "bind",
83
+ Source: srcDir,
84
+ Target: "/src",
85
+ },
86
+ {
87
+ Type: "volume",
88
+ Source: volumeName,
89
+ Target: "/dst",
90
+ },
91
+ ],
92
+ image: image,
93
+ cmdArgs: ["tar", "xzf", `/src/${name}`, "-C", "/dst"],
69
94
  });
70
95
  spinner.succeed(`Successfully imported to volume ${chalk.green(volumeName)}`);
71
96
  }
@@ -0,0 +1,23 @@
1
+ import fs from "fs";
2
+ import os from "os";
3
+ export function isSocketAccessible(socketPath) {
4
+ try {
5
+ const stats = fs.statSync(socketPath);
6
+ return stats.isSocket();
7
+ }
8
+ catch {
9
+ return false;
10
+ }
11
+ }
12
+ export function isNamedPipeAccessible(pipePath) {
13
+ if (os.platform() !== "win32") {
14
+ return false;
15
+ }
16
+ try {
17
+ const stats = fs.statSync(pipePath);
18
+ return stats.isFIFO();
19
+ }
20
+ catch {
21
+ return false;
22
+ }
23
+ }
@@ -0,0 +1,48 @@
1
+ import os from "os";
2
+ import chalk from "chalk";
3
+ import axios from "axios";
4
+ import { createNamedPipeAgent } from "./npipe.js";
5
+ import { getBestDockerConnection } from "./optimal.js";
6
+ export async function dockerClient(endpoint, options = {}) {
7
+ const dockerConfig = await initializeDockerConnection();
8
+ const config = createDockerAxiosConfig(dockerConfig, endpoint, options);
9
+ return axios(config);
10
+ }
11
+ export async function initializeDockerConnection() {
12
+ try {
13
+ const dockerConfig = await getBestDockerConnection();
14
+ return dockerConfig;
15
+ }
16
+ catch (error) {
17
+ console.error(chalk.red("Failed to connect to Docker:"), error.message);
18
+ console.error(chalk.yellow("Make sure Docker is running and accessible."));
19
+ process.exit(1);
20
+ }
21
+ }
22
+ function createDockerAxiosConfig(dockerConfig, endpoint, options = {}) {
23
+ // const dockerConfig = getDockerConfig();
24
+ const config = {
25
+ ...options,
26
+ url: `http://localhost${endpoint}`,
27
+ };
28
+ if (dockerConfig.socketPath) {
29
+ // Handle Windows named pipes differently
30
+ if (os.platform() === "win32" && dockerConfig.socketPath.includes("pipe")) {
31
+ // For Windows named pipes, we need to use a custom HTTP agent
32
+ config.httpAgent = createNamedPipeAgent(dockerConfig.socketPath);
33
+ config.baseURL = "http://localhost";
34
+ }
35
+ else {
36
+ // Unix socket
37
+ config.socketPath = dockerConfig.socketPath;
38
+ }
39
+ }
40
+ else if (dockerConfig.host && dockerConfig.port) {
41
+ // TCP connection
42
+ config.baseURL = `http://${dockerConfig.host}:${dockerConfig.port}`;
43
+ }
44
+ else {
45
+ throw new Error("Invalid Docker connection configuration");
46
+ }
47
+ return config;
48
+ }
@@ -0,0 +1,23 @@
1
+ import os from "os";
2
+ import path from "path";
3
+ import { parseDockerHost } from "./parser.js";
4
+ import { isSocketAccessible } from "./checks.js";
5
+ export function detectLinuxDockerConnection() {
6
+ const socketPaths = [
7
+ "/var/run/docker.sock", // Standard Docker socket
8
+ path.join(os.homedir(), ".docker", "desktop", "docker.sock"), // Rootless Docker
9
+ // Podman
10
+ `/run/user/${process.getuid?.() || 1000}/podman/podman.sock`,
11
+ path.join(os.homedir(), ".local", "share", "containers", "podman", "machine", "podman.sock"),
12
+ ];
13
+ for (const socketPath of socketPaths) {
14
+ if (isSocketAccessible(socketPath)) {
15
+ return { socketPath };
16
+ }
17
+ }
18
+ const dockerHost = process.env.DOCKER_HOST;
19
+ if (dockerHost) {
20
+ return parseDockerHost(dockerHost);
21
+ }
22
+ return { socketPath: "/var/run/docker.sock" };
23
+ }
@@ -0,0 +1,27 @@
1
+ import os from "os";
2
+ import path from "path";
3
+ import { parseDockerHost } from "./parser.js";
4
+ import { isSocketAccessible } from "./checks.js";
5
+ export function detectMacDockerConnection() {
6
+ const socketPaths = [
7
+ // Docker Desktop for Mac
8
+ path.join(os.homedir(), ".docker", "desktop", "docker.sock"),
9
+ path.join(os.homedir(), ".docker", "run", "docker.sock"),
10
+ // Standard Unix socket
11
+ "/var/run/docker.sock",
12
+ // Colima
13
+ path.join(os.homedir(), ".colima", "default", "docker.sock"),
14
+ // Podman Desktop
15
+ path.join(os.homedir(), ".local", "share", "containers", "podman", "machine", "podman.sock"),
16
+ ];
17
+ for (const socketPath of socketPaths) {
18
+ if (isSocketAccessible(socketPath)) {
19
+ return { socketPath };
20
+ }
21
+ }
22
+ const dockerHost = process.env.DOCKER_HOST;
23
+ if (dockerHost) {
24
+ return parseDockerHost(dockerHost);
25
+ }
26
+ return { socketPath: "/var/run/docker.sock" };
27
+ }
@@ -0,0 +1,19 @@
1
+ import net from "net";
2
+ import http from "http";
3
+ function createNamedPipeAgent(pipePath) {
4
+ const agent = new http.Agent();
5
+ agent.createConnection = function (options, callback) {
6
+ const socket = net.createConnection(pipePath);
7
+ socket.on("connect", () => {
8
+ if (callback)
9
+ callback(null, socket);
10
+ });
11
+ socket.on("error", (err) => {
12
+ if (callback)
13
+ callback(err);
14
+ });
15
+ return socket;
16
+ };
17
+ return agent;
18
+ }
19
+ export { createNamedPipeAgent };
@@ -0,0 +1,55 @@
1
+ import os from "os";
2
+ import path from "path";
3
+ import { detectWindowsDockerConnection } from "./windows.js";
4
+ import { detectMacDockerConnection } from "./macos.js";
5
+ import { detectLinuxDockerConnection } from "./linux.js";
6
+ import { testDockerConnection } from "./test.js";
7
+ export function detectDockerConnection() {
8
+ const platform = os.platform();
9
+ switch (platform) {
10
+ case "win32":
11
+ return detectWindowsDockerConnection();
12
+ case "darwin":
13
+ return detectMacDockerConnection();
14
+ case "linux":
15
+ return detectLinuxDockerConnection();
16
+ default:
17
+ return detectLinuxDockerConnection();
18
+ }
19
+ }
20
+ export async function getBestDockerConnection() {
21
+ const primaryConfig = detectDockerConnection();
22
+ if (await testDockerConnection(primaryConfig)) {
23
+ return primaryConfig;
24
+ }
25
+ const fallbackConfigs = [];
26
+ // Platform-specific fallbacks
27
+ if (os.platform() === "win32") {
28
+ // Windows Docker Desktop named pipes
29
+ fallbackConfigs.push({ socketPath: "\\\\.\\pipe\\dockerDesktopLinuxEngine" }, { socketPath: "\\\\.\\pipe\\docker_engine" }, { socketPath: "\\\\.\\pipe\\dockerWindowsEngine" }, { host: "localhost", port: 2375 }, { host: "localhost", port: 2376 });
30
+ }
31
+ else if (os.platform() === "darwin") {
32
+ // macOS specific paths
33
+ fallbackConfigs.push({
34
+ socketPath: path.join(os.homedir(), ".docker", "desktop", "docker.sock"),
35
+ }, { socketPath: path.join(os.homedir(), ".docker", "run", "docker.sock") }, {
36
+ socketPath: path.join(os.homedir(), ".colima", "default", "docker.sock"),
37
+ }, { socketPath: "/var/run/docker.sock" }, { host: "localhost", port: 2375 }, { host: "localhost", port: 2376 });
38
+ }
39
+ else {
40
+ // Linux and other Unix-like systems
41
+ fallbackConfigs.push({ socketPath: "/var/run/docker.sock" }, {
42
+ socketPath: path.join(os.homedir(), ".docker", "desktop", "docker.sock"),
43
+ }, {
44
+ socketPath: `/run/user/${process.getuid?.() || 1000}/podman/podman.sock`,
45
+ }, { host: "localhost", port: 2375 }, { host: "localhost", port: 2376 });
46
+ }
47
+ for (const config of fallbackConfigs) {
48
+ if (await testDockerConnection(config)) {
49
+ return config;
50
+ }
51
+ }
52
+ throw new Error(`Failed to connect to Docker. Tried primary config: ${JSON.stringify(primaryConfig)} ` +
53
+ `and ${fallbackConfigs.length} fallback configurations. ` +
54
+ `Make sure Docker is running and accessible.`);
55
+ }
@@ -0,0 +1,26 @@
1
+ export function parseDockerHost(dockerHost) {
2
+ try {
3
+ if (dockerHost.startsWith("npipe:")) {
4
+ const pipePath = dockerHost.replace("npipe:////./pipe/", "\\\\.\\pipe\\");
5
+ return { socketPath: pipePath };
6
+ }
7
+ const url = new URL(dockerHost);
8
+ switch (url.protocol) {
9
+ case "unix:":
10
+ return { socketPath: url.pathname };
11
+ case "tcp:":
12
+ return {
13
+ host: url.hostname || "localhost",
14
+ port: parseInt(url.port) || 2376,
15
+ };
16
+ case "npipe:":
17
+ return { socketPath: url.pathname };
18
+ default:
19
+ throw new Error(`Unsupported protocol: ${url.protocol}`);
20
+ }
21
+ }
22
+ catch (error) {
23
+ console.warn(`Failed to parse DOCKER_HOST "${dockerHost}":`, error);
24
+ return { socketPath: "/var/run/docker.sock" };
25
+ }
26
+ }
@@ -0,0 +1,77 @@
1
+ import os from "os";
2
+ import http from "http";
3
+ import { createNamedPipeAgent } from "./npipe.js";
4
+ export async function testDockerConnection(config) {
5
+ try {
6
+ if (config.socketPath) {
7
+ // Handle Windows named pipes specially
8
+ if (os.platform() === "win32" && config.socketPath.includes("pipe")) {
9
+ return await testWindowsNamedPipe(config.socketPath);
10
+ }
11
+ else {
12
+ // Unix socket connection
13
+ const axios = (await import("axios")).default;
14
+ const axiosConfig = {
15
+ method: "GET",
16
+ url: "http://localhost/_ping",
17
+ socketPath: config.socketPath,
18
+ timeout: 5000,
19
+ };
20
+ const response = await axios(axiosConfig);
21
+ return response.status === 200 && response.data === "OK";
22
+ }
23
+ }
24
+ else if (config.host && config.port) {
25
+ // TCP connection
26
+ const axios = (await import("axios")).default;
27
+ const axiosConfig = {
28
+ method: "GET",
29
+ baseURL: `http://${config.host}:${config.port}`,
30
+ url: "/_ping",
31
+ timeout: 5000,
32
+ };
33
+ const response = await axios(axiosConfig);
34
+ return response.status === 200 && response.data === "OK";
35
+ }
36
+ else {
37
+ return false;
38
+ }
39
+ }
40
+ catch {
41
+ return false;
42
+ }
43
+ }
44
+ export async function testWindowsNamedPipe(pipePath) {
45
+ if (os.platform() !== "win32") {
46
+ return false;
47
+ }
48
+ return new Promise((resolve) => {
49
+ const agent = createNamedPipeAgent(pipePath);
50
+ const options = {
51
+ agent: agent,
52
+ hostname: "localhost",
53
+ port: 80,
54
+ path: "/_ping",
55
+ method: "GET",
56
+ timeout: 5000,
57
+ };
58
+ const req = http.request(options, (res) => {
59
+ let data = "";
60
+ res.on("data", (chunk) => {
61
+ data += chunk;
62
+ });
63
+ res.on("end", () => {
64
+ resolve(res.statusCode === 200 && data.trim() === "OK");
65
+ });
66
+ });
67
+ req.on("error", () => {
68
+ resolve(false);
69
+ });
70
+ req.on("timeout", () => {
71
+ req.destroy();
72
+ resolve(false);
73
+ });
74
+ req.setTimeout(5000);
75
+ req.end();
76
+ });
77
+ }
@@ -0,0 +1,66 @@
1
+ import fs from "fs";
2
+ import os from "os";
3
+ import path from "path";
4
+ import { execSync } from "child_process";
5
+ import { parseDockerHost } from "./parser.js";
6
+ import { isNamedPipeAccessible } from "./checks.js";
7
+ export function detectWindowsDockerConnection() {
8
+ // Check if DOCKER_HOST environment variable is set to a named pipe
9
+ const dockerHost = process.env.DOCKER_HOST;
10
+ if (dockerHost && dockerHost.startsWith("npipe:")) {
11
+ return parseDockerHost(dockerHost);
12
+ }
13
+ // Check if Docker context is set to a named pipe
14
+ try {
15
+ const contextName = execSync("docker context show", {
16
+ encoding: "utf8",
17
+ }).trim();
18
+ const contextInfo = JSON.parse(execSync(`docker context inspect ${contextName}`, { encoding: "utf8" }));
19
+ if (contextInfo &&
20
+ contextInfo[0] &&
21
+ contextInfo[0].Endpoints &&
22
+ contextInfo[0].Endpoints.docker) {
23
+ const dockerEndpoint = contextInfo[0].Endpoints.docker.Host;
24
+ if (dockerEndpoint && dockerEndpoint.startsWith("npipe:")) {
25
+ return parseDockerHost(dockerEndpoint);
26
+ }
27
+ }
28
+ }
29
+ catch (error) {
30
+ console.debug("Could not detect Docker context:", error);
31
+ }
32
+ // Common Docker Desktop named pipe paths
33
+ const namedPipePaths = [
34
+ "\\\\.\\pipe\\dockerDesktopLinuxEngine", // Docker Desktop Linux containers
35
+ "\\\\.\\pipe\\docker_engine", // Default Docker engine
36
+ "\\\\.\\pipe\\dockerWindowsEngine", // Docker Desktop Windows containers
37
+ ];
38
+ // Check if any named pipe exists
39
+ for (const pipePath of namedPipePaths) {
40
+ if (isNamedPipeAccessible(pipePath)) {
41
+ return { socketPath: pipePath };
42
+ }
43
+ }
44
+ // Check if Docker Desktop is installed
45
+ const dockerDesktopPaths = [
46
+ path.join(os.homedir(), "AppData", "Roaming", "Docker", "settings.json"),
47
+ path.join(os.homedir(), "AppData", "Local", "Docker", "settings.json"),
48
+ ];
49
+ const isDockerDesktopInstalled = dockerDesktopPaths.some((p) => {
50
+ try {
51
+ return fs.existsSync(p);
52
+ }
53
+ catch {
54
+ return false;
55
+ }
56
+ });
57
+ if (isDockerDesktopInstalled) {
58
+ // Return the most common Docker Desktop pipe
59
+ return { socketPath: "\\\\.\\pipe\\dockerDesktopLinuxEngine" };
60
+ }
61
+ // Fallback to TCP for other Docker implementations on Windows
62
+ return {
63
+ host: "localhost",
64
+ port: 2375,
65
+ };
66
+ }
@@ -1,19 +1,13 @@
1
- import axios from "axios";
2
- export async function containerCreate({ name, src, dst, cmd, image, }) {
3
- const binds = [`${src}:/src`, `${dst}:/dst`];
1
+ import { dockerClient } from "../connection/client.js";
2
+ export async function containerCreate(name, { Image, Cmd, Tty = false, HostConfig }) {
4
3
  try {
5
- const createResponse = await axios({
4
+ const createResponse = await dockerClient(`/containers/create${name ? `?name=${name}` : ""}`, {
6
5
  method: "POST",
7
- socketPath: "/var/run/docker.sock",
8
- url: `http://localhost/containers/create`,
9
6
  data: {
10
- Image: image,
11
- Tty: false,
12
- Cmd: cmd,
13
- HostConfig: {
14
- AutoRemove: true,
15
- Binds: binds,
16
- },
7
+ Image,
8
+ Cmd,
9
+ Tty,
10
+ HostConfig,
17
11
  },
18
12
  });
19
13
  if (!createResponse.data || !createResponse.data.Id) {
@@ -28,10 +22,8 @@ export async function containerCreate({ name, src, dst, cmd, image, }) {
28
22
  }
29
23
  export async function containerStart(containerId) {
30
24
  try {
31
- await axios({
25
+ await dockerClient(`/containers/${containerId}/start`, {
32
26
  method: "POST",
33
- socketPath: "/var/run/docker.sock",
34
- url: `http://localhost/containers/${containerId}/start`,
35
27
  });
36
28
  }
37
29
  catch (error) {
@@ -42,10 +34,8 @@ export async function containerStart(containerId) {
42
34
  }
43
35
  export async function containerWait(containerId) {
44
36
  try {
45
- const response = await axios({
37
+ const response = await dockerClient(`/containers/${containerId}/wait`, {
46
38
  method: "POST",
47
- socketPath: "/var/run/docker.sock",
48
- url: `http://localhost/containers/${containerId}/wait`,
49
39
  });
50
40
  if (response.data.StatusCode !== 0) {
51
41
  throw new Error(`Container ${containerId} exited with status code ${response.data.StatusCode}`);
@@ -59,10 +49,8 @@ export async function containerWait(containerId) {
59
49
  }
60
50
  export async function containerRemove(containerId) {
61
51
  try {
62
- await axios({
52
+ await dockerClient(`/containers/${containerId}`, {
63
53
  method: "DELETE",
64
- socketPath: "/var/run/docker.sock",
65
- url: `http://localhost/containers/${containerId}`,
66
54
  });
67
55
  }
68
56
  catch (error) {
@@ -1,10 +1,8 @@
1
- import axios from "axios";
1
+ import { dockerClient } from "../connection/client.js";
2
2
  export async function imageList() {
3
3
  try {
4
- const response = await axios({
4
+ const response = await dockerClient("/images/json", {
5
5
  method: "GET",
6
- socketPath: "/var/run/docker.sock",
7
- url: `http://localhost/images/json`,
8
6
  });
9
7
  return response.data
10
8
  .map((image) => image.RepoTags)
@@ -18,10 +16,8 @@ export async function imageList() {
18
16
  }
19
17
  export async function imagePull(image) {
20
18
  try {
21
- const response = await axios({
19
+ const response = await dockerClient(`/images/create?fromImage=${image}`, {
22
20
  method: "POST",
23
- socketPath: "/var/run/docker.sock",
24
- url: `http://localhost/images/create?fromImage=${image}`,
25
21
  });
26
22
  return response.data;
27
23
  }
@@ -1,13 +1,29 @@
1
- const image = "busybox:latest";
2
1
  import { containerCreate, containerStart, containerWait, containerRemove, } from "./container.js";
3
2
  import { imageList, imagePull } from "./image.js";
4
- export async function run({ name, src, dst, cmd }) {
5
- const localImages = await imageList();
6
- const isImageFound = localImages.some((localImage) => localImage === image);
7
- if (!isImageFound) {
8
- await imagePull(image);
9
- }
10
- const containerId = await containerCreate({ name, src, dst, cmd, image });
3
+ import { volumeCreate, volumeList } from "./volume.js";
4
+ /**
5
+ * @description Create and run a new container from an image
6
+ * @param options - Options for running the container
7
+ */
8
+ export async function run(options) {
9
+ const { name, rm, mounts, volumes, tty, image, cmdArgs } = options;
10
+ let hostConfig = {
11
+ AutoRemove: rm,
12
+ Binds: volumes?.map((v) => `${v.Name}:${v.Mountpoint}`),
13
+ Mounts: mounts?.map((m) => ({
14
+ Type: m.Type,
15
+ Source: m.Source,
16
+ Target: m.Target,
17
+ ReadOnly: m.ReadOnly,
18
+ Consistency: m.Consistency,
19
+ })),
20
+ };
21
+ const containerId = await containerCreate(name, {
22
+ HostConfig: hostConfig,
23
+ Tty: tty || false,
24
+ Image: image,
25
+ Cmd: cmdArgs,
26
+ });
11
27
  await containerStart(containerId);
12
28
  await containerWait(containerId);
13
29
  }
@@ -19,5 +35,7 @@ const docker = {
19
35
  containerRemove,
20
36
  imageList,
21
37
  imagePull,
38
+ volumeList,
39
+ volumeCreate,
22
40
  };
23
41
  export default docker;
@@ -0,0 +1,44 @@
1
+ import { dockerClient } from "../connection/client.js";
2
+ export async function volumeList() {
3
+ try {
4
+ const response = await dockerClient("/volumes", {
5
+ method: "GET",
6
+ });
7
+ const data = response.data;
8
+ const volumes = data.Volumes.map((vol) => {
9
+ return {
10
+ Name: vol.Name,
11
+ Driver: vol.Driver,
12
+ Mountpoint: vol.Mountpoint,
13
+ };
14
+ });
15
+ return volumes;
16
+ }
17
+ catch (error) {
18
+ console.error("Failed to list Docker volumes:", error);
19
+ throw error;
20
+ }
21
+ }
22
+ export async function volumeCreate({ Name, Driver }) {
23
+ if (!Name) {
24
+ throw new Error("Volume name is required for creation.");
25
+ }
26
+ if (!Driver) {
27
+ Driver = "local";
28
+ }
29
+ try {
30
+ const response = await dockerClient("/volumes/create", {
31
+ method: "POST",
32
+ data: {
33
+ Name,
34
+ Driver,
35
+ },
36
+ });
37
+ const data = response.data;
38
+ return data;
39
+ }
40
+ catch (error) {
41
+ console.error(`Failed to create Docker volume ${Name}:`, error);
42
+ throw error;
43
+ }
44
+ }
@@ -1,4 +1,14 @@
1
1
  import path from "path";
2
+ import { volumeList } from "../docker/volume.js";
2
3
  export function ensureAbsolutePath(p) {
3
4
  return path.isAbsolute(p) ? p : path.resolve(process.cwd(), p);
4
5
  }
6
+ export async function volumeExists(name) {
7
+ try {
8
+ const volumes = await volumeList();
9
+ return volumes.map((v) => v.Name).includes(name);
10
+ }
11
+ catch (error) {
12
+ return false;
13
+ }
14
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsmt",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Docker Storage Migration Tool",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,128 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our
6
- community a harassment-free experience for everyone, regardless of age, body
7
- size, visible or invisible disability, ethnicity, sex characteristics, gender
8
- identity and expression, level of experience, education, socio-economic status,
9
- nationality, personal appearance, race, religion, or sexual identity
10
- and orientation.
11
-
12
- We pledge to act and interact in ways that contribute to an open, welcoming,
13
- diverse, inclusive, and healthy community.
14
-
15
- ## Our Standards
16
-
17
- Examples of behavior that contributes to a positive environment for our
18
- community include:
19
-
20
- * Demonstrating empathy and kindness toward other people
21
- * Being respectful of differing opinions, viewpoints, and experiences
22
- * Giving and gracefully accepting constructive feedback
23
- * Accepting responsibility and apologizing to those affected by our mistakes,
24
- and learning from the experience
25
- * Focusing on what is best not just for us as individuals, but for the
26
- overall community
27
-
28
- Examples of unacceptable behavior include:
29
-
30
- * The use of sexualized language or imagery, and sexual attention or
31
- advances of any kind
32
- * Trolling, insulting or derogatory comments, and personal or political attacks
33
- * Public or private harassment
34
- * Publishing others' private information, such as a physical or email
35
- address, without their explicit permission
36
- * Other conduct which could reasonably be considered inappropriate in a
37
- professional setting
38
-
39
- ## Enforcement Responsibilities
40
-
41
- Community leaders are responsible for clarifying and enforcing our standards of
42
- acceptable behavior and will take appropriate and fair corrective action in
43
- response to any behavior that they deem inappropriate, threatening, offensive,
44
- or harmful.
45
-
46
- Community leaders have the right and responsibility to remove, edit, or reject
47
- comments, commits, code, wiki edits, issues, and other contributions that are
48
- not aligned to this Code of Conduct, and will communicate reasons for moderation
49
- decisions when appropriate.
50
-
51
- ## Scope
52
-
53
- This Code of Conduct applies within all community spaces, and also applies when
54
- an individual is officially representing the community in public spaces.
55
- Examples of representing our community include using an official e-mail address,
56
- posting via an official social media account, or acting as an appointed
57
- representative at an online or offline event.
58
-
59
- ## Enforcement
60
-
61
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
- reported to the community leaders responsible for enforcement at
63
- kd@itskdhere.eu.org.
64
- All complaints will be reviewed and investigated promptly and fairly.
65
-
66
- All community leaders are obligated to respect the privacy and security of the
67
- reporter of any incident.
68
-
69
- ## Enforcement Guidelines
70
-
71
- Community leaders will follow these Community Impact Guidelines in determining
72
- the consequences for any action they deem in violation of this Code of Conduct:
73
-
74
- ### 1. Correction
75
-
76
- **Community Impact**: Use of inappropriate language or other behavior deemed
77
- unprofessional or unwelcome in the community.
78
-
79
- **Consequence**: A private, written warning from community leaders, providing
80
- clarity around the nature of the violation and an explanation of why the
81
- behavior was inappropriate. A public apology may be requested.
82
-
83
- ### 2. Warning
84
-
85
- **Community Impact**: A violation through a single incident or series
86
- of actions.
87
-
88
- **Consequence**: A warning with consequences for continued behavior. No
89
- interaction with the people involved, including unsolicited interaction with
90
- those enforcing the Code of Conduct, for a specified period of time. This
91
- includes avoiding interactions in community spaces as well as external channels
92
- like social media. Violating these terms may lead to a temporary or
93
- permanent ban.
94
-
95
- ### 3. Temporary Ban
96
-
97
- **Community Impact**: A serious violation of community standards, including
98
- sustained inappropriate behavior.
99
-
100
- **Consequence**: A temporary ban from any sort of interaction or public
101
- communication with the community for a specified period of time. No public or
102
- private interaction with the people involved, including unsolicited interaction
103
- with those enforcing the Code of Conduct, is allowed during this period.
104
- Violating these terms may lead to a permanent ban.
105
-
106
- ### 4. Permanent Ban
107
-
108
- **Community Impact**: Demonstrating a pattern of violation of community
109
- standards, including sustained inappropriate behavior, harassment of an
110
- individual, or aggression toward or disparagement of classes of individuals.
111
-
112
- **Consequence**: A permanent ban from any sort of public interaction within
113
- the community.
114
-
115
- ## Attribution
116
-
117
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
- version 2.0, available at
119
- https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120
-
121
- Community Impact Guidelines were inspired by [Mozilla's code of conduct
122
- enforcement ladder](https://github.com/mozilla/diversity).
123
-
124
- [homepage]: https://www.contributor-covenant.org
125
-
126
- For answers to common questions about this code of conduct, see the FAQ at
127
- https://www.contributor-covenant.org/faq. Translations are available at
128
- https://www.contributor-covenant.org/translations.