dsmt 0.1.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.
- package/CODE_OF_CONDUCT.md +128 -0
- package/LICENSE +21 -0
- package/README.md +91 -0
- package/dist/cli.js +26 -0
- package/dist/cmd/export.js +69 -0
- package/dist/cmd/import.js +76 -0
- package/dist/lib/docker/container.js +71 -0
- package/dist/lib/docker/image.js +32 -0
- package/dist/lib/docker/sdk.js +23 -0
- package/dist/lib/etc/utils.js +4 -0
- package/dist/types/docker.js +1 -0
- package/package.json +33 -0
|
@@ -0,0 +1,128 @@
|
|
|
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.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Krishnendu Das
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# 🐳 Docker Storage Migration Tool (DSMT) 📦
|
|
2
|
+
|
|
3
|
+
A command-line utility for seamlessly exporting and importing Docker volumes and bind mounts.
|
|
4
|
+
|
|
5
|
+
## 🔍 Overview
|
|
6
|
+
|
|
7
|
+
DSMT (Docker Storage Migration Tool) provides a simple way to:
|
|
8
|
+
|
|
9
|
+
- 📤 Export Docker volumes or bind mounts to compressed tarballs.
|
|
10
|
+
- 📥 Import compressed tarballs into Docker volumes or bind mounts.
|
|
11
|
+
|
|
12
|
+
🔄 This tool makes it easy to backup, restore, or migrate Docker storage across systems.
|
|
13
|
+
|
|
14
|
+
## 💻 Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install -g dsmt
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 🚀 Usage
|
|
21
|
+
|
|
22
|
+
### 📤 Exporting Docker Storage
|
|
23
|
+
|
|
24
|
+
Export a Docker volume:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
dsmt export volume_name /path/to/export/directory
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Export a bind mount:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
dsmt export /path/to/bind/mount /path/to/export/directory
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 📥 Importing Docker Storage
|
|
37
|
+
|
|
38
|
+
Import to a Docker volume:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
dsmt import /path/to/tarball.tar.gz volume_name
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Import to a bind mount:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
dsmt import /path/to/tarball.tar.gz /path/to/bind/mount
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## ⚙️ Options
|
|
51
|
+
|
|
52
|
+
Both commands support the following options:
|
|
53
|
+
|
|
54
|
+
- `-v, --volume`: Explicitly specify source/destination as a Docker volume
|
|
55
|
+
- `-m, --mount`: Explicitly specify source/destination as a bind mount
|
|
56
|
+
|
|
57
|
+
The tool will automatically detect the source/destination type in most cases, but you can use these flags to be explicit.
|
|
58
|
+
|
|
59
|
+
## 📋 Examples
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
# Export a volume named 'mongodb_data' to the current directory
|
|
63
|
+
dsmt export mongodb_data ./
|
|
64
|
+
|
|
65
|
+
# Import a tarball to a new volume
|
|
66
|
+
dsmt import ./mongodb_data.tar.gz new_mongodb_data
|
|
67
|
+
|
|
68
|
+
# Export a bind mount to the /backups directory
|
|
69
|
+
dsmt export /var/www/html /backups
|
|
70
|
+
|
|
71
|
+
# Import a tarball to a bind mount
|
|
72
|
+
dsmt import ./html.tar.gz /var/www/html
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 🛠️ Development
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# Clone the repository
|
|
79
|
+
git clone https://github.com/itskdhere/dsmt.git
|
|
80
|
+
cd dsmt
|
|
81
|
+
|
|
82
|
+
# Install dependencies
|
|
83
|
+
npm install
|
|
84
|
+
|
|
85
|
+
# Run the development build
|
|
86
|
+
npm run dev
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## 📄 License
|
|
90
|
+
|
|
91
|
+
MIT © [itskdhere](https://github.com/itskdhere)
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
import exportCmd from "./cmd/export.js";
|
|
3
|
+
import importCmd from "./cmd/import.js";
|
|
4
|
+
import { Command } from "commander";
|
|
5
|
+
const program = new Command();
|
|
6
|
+
program
|
|
7
|
+
.name("dsmt")
|
|
8
|
+
.description("Docker Storage Migration Tool")
|
|
9
|
+
.version("0.1.0");
|
|
10
|
+
program
|
|
11
|
+
.command("export")
|
|
12
|
+
.argument("<src>", "volume name or bind mount path")
|
|
13
|
+
.argument("<dst>", "path to export to")
|
|
14
|
+
.option("-v, --volume", "Volume")
|
|
15
|
+
.option("-m, --mount", "Mount")
|
|
16
|
+
.description("Export a Docker Volume or Bind Mount to a Tarball")
|
|
17
|
+
.action(async (src, dst, options) => await exportCmd(src, dst, options));
|
|
18
|
+
program
|
|
19
|
+
.command("import")
|
|
20
|
+
.argument("<src>", "path to import from")
|
|
21
|
+
.argument("<dst>", "volume name or bind mount path")
|
|
22
|
+
.option("-v, --volume", "Volume")
|
|
23
|
+
.option("-m, --mount", "Mount")
|
|
24
|
+
.description("Import a Docker Volume or Bind Mount from a Tarball")
|
|
25
|
+
.action(async (src, dst, options) => await importCmd(src, dst, options));
|
|
26
|
+
program.parse(process.argv);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import docker from "../lib/docker/sdk.js";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
import ora from "ora";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { ensureAbsolutePath } from "../lib/etc/utils.js";
|
|
6
|
+
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."));
|
|
9
|
+
process.exit(1);
|
|
10
|
+
}
|
|
11
|
+
if (!options.volume && !options.mount) {
|
|
12
|
+
if (src.startsWith("/") || src.startsWith("./") || src.startsWith("../")) {
|
|
13
|
+
options.mount = true;
|
|
14
|
+
console.log(chalk.blue(`Auto-detected bind mount path: ${src}`));
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
options.volume = true;
|
|
18
|
+
console.log(chalk.blue(`Auto-detected volume name: ${src}`));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (options.volume) {
|
|
22
|
+
await exportVolume(src, dst, options);
|
|
23
|
+
}
|
|
24
|
+
else if (options.mount) {
|
|
25
|
+
await exportMount(src, dst, options);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async function exportMount(src, dst, options) {
|
|
29
|
+
const spinner = ora("Preparing to export Docker bind mount...").start();
|
|
30
|
+
try {
|
|
31
|
+
const srcPath = ensureAbsolutePath(src);
|
|
32
|
+
const dstPath = ensureAbsolutePath(dst);
|
|
33
|
+
const name = path.basename(srcPath);
|
|
34
|
+
spinner.text = `Exporting bind mount from ${chalk.blue(srcPath)} to ${chalk.green(dst)}`;
|
|
35
|
+
await docker.run({
|
|
36
|
+
name: name,
|
|
37
|
+
src: srcPath,
|
|
38
|
+
dst: dstPath,
|
|
39
|
+
cmd: ["tar", "czf", `/dst/${name}.tar.gz`, "-C", "/src", "."],
|
|
40
|
+
options,
|
|
41
|
+
});
|
|
42
|
+
spinner.succeed(`Successfully exported bind mount to ${chalk.green(dstPath)}`);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
spinner.fail(`Failed to export bind mount: ${error.message}`);
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async function exportVolume(src, dst, options) {
|
|
50
|
+
const spinner = ora("Preparing to export Docker volume...").start();
|
|
51
|
+
try {
|
|
52
|
+
const volumeName = src;
|
|
53
|
+
const dstPath = ensureAbsolutePath(dst);
|
|
54
|
+
const name = path.basename(src);
|
|
55
|
+
spinner.text = `Exporting volume ${chalk.blue(src)} to ${chalk.green(dstPath)}`;
|
|
56
|
+
await docker.run({
|
|
57
|
+
name: name,
|
|
58
|
+
src: volumeName,
|
|
59
|
+
dst: dstPath,
|
|
60
|
+
cmd: ["tar", "czf", `/dst/${name}.tar.gz`, "-C", "/src", "."],
|
|
61
|
+
options,
|
|
62
|
+
});
|
|
63
|
+
spinner.succeed(`Successfully exported volume to ${chalk.green(dstPath)}`);
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
spinner.fail(`Failed to export volume: ${error.message}`);
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import docker from "../lib/docker/sdk.js";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
import ora from "ora";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import { ensureAbsolutePath } from "../lib/etc/utils.js";
|
|
7
|
+
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."));
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
if (!options.volume && !options.mount) {
|
|
13
|
+
if (dst.startsWith("/") || dst.startsWith("./") || dst.startsWith("../")) {
|
|
14
|
+
options.mount = true;
|
|
15
|
+
console.log(chalk.blue(`Auto-detected bind mount path: ${dst}`));
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
options.volume = true;
|
|
19
|
+
console.log(chalk.blue(`Auto-detected volume name: ${dst}`));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (options.volume) {
|
|
23
|
+
await importVolume(src, dst, options);
|
|
24
|
+
}
|
|
25
|
+
else if (options.mount) {
|
|
26
|
+
await importMount(src, dst, options);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async function importMount(src, dst, options) {
|
|
30
|
+
const spinner = ora("Preparing to import to Docker bind mount...").start();
|
|
31
|
+
try {
|
|
32
|
+
const srcPath = ensureAbsolutePath(src);
|
|
33
|
+
const dstPath = ensureAbsolutePath(dst);
|
|
34
|
+
if (!fs.existsSync(srcPath)) {
|
|
35
|
+
throw new Error(`Source file ${srcPath} does not exist`);
|
|
36
|
+
}
|
|
37
|
+
const name = path.basename(srcPath);
|
|
38
|
+
spinner.text = `Importing from ${chalk.blue(srcPath)} to bind mount ${chalk.green(dstPath)}`;
|
|
39
|
+
await docker.run({
|
|
40
|
+
name: name,
|
|
41
|
+
src: srcPath,
|
|
42
|
+
dst: dstPath,
|
|
43
|
+
cmd: ["tar", "xzf", `/src/${name}`, "-C", "/dst"],
|
|
44
|
+
options,
|
|
45
|
+
});
|
|
46
|
+
spinner.succeed(`Successfully imported to bind mount at ${chalk.green(dstPath)}`);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
spinner.fail(`Failed to import to bind mount: ${error.message}`);
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function importVolume(src, dst, options) {
|
|
54
|
+
const spinner = ora("Preparing to import to Docker volume...").start();
|
|
55
|
+
try {
|
|
56
|
+
const srcPath = ensureAbsolutePath(src);
|
|
57
|
+
const volumeName = dst;
|
|
58
|
+
if (!fs.existsSync(srcPath)) {
|
|
59
|
+
throw new Error(`Source file ${srcPath} does not exist`);
|
|
60
|
+
}
|
|
61
|
+
const name = path.basename(srcPath);
|
|
62
|
+
spinner.text = `Importing from ${chalk.blue(srcPath)} to volume ${chalk.green(volumeName)}`;
|
|
63
|
+
await docker.run({
|
|
64
|
+
name: name,
|
|
65
|
+
src: srcPath,
|
|
66
|
+
dst: volumeName,
|
|
67
|
+
cmd: ["tar", "xzf", `/src/${name}`, "-C", "/dst"],
|
|
68
|
+
options,
|
|
69
|
+
});
|
|
70
|
+
spinner.succeed(`Successfully imported to volume ${chalk.green(volumeName)}`);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
spinner.fail(`Failed to import to volume: ${error.message}`);
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
export async function containerCreate({ name, src, dst, cmd, image, }) {
|
|
3
|
+
const binds = [`${src}:/src`, `${dst}:/dst`];
|
|
4
|
+
try {
|
|
5
|
+
const createResponse = await axios({
|
|
6
|
+
method: "POST",
|
|
7
|
+
socketPath: "/var/run/docker.sock",
|
|
8
|
+
url: `http://localhost/containers/create`,
|
|
9
|
+
data: {
|
|
10
|
+
Image: image,
|
|
11
|
+
Tty: false,
|
|
12
|
+
Cmd: cmd,
|
|
13
|
+
HostConfig: {
|
|
14
|
+
AutoRemove: true,
|
|
15
|
+
Binds: binds,
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
if (!createResponse.data || !createResponse.data.Id) {
|
|
20
|
+
throw new Error("Failed to create container: Invalid response from Docker API");
|
|
21
|
+
}
|
|
22
|
+
return createResponse.data.Id;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
console.error("Failed to create Docker container:", error);
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export async function containerStart(containerId) {
|
|
30
|
+
try {
|
|
31
|
+
await axios({
|
|
32
|
+
method: "POST",
|
|
33
|
+
socketPath: "/var/run/docker.sock",
|
|
34
|
+
url: `http://localhost/containers/${containerId}/start`,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.error(`Failed to start Docker container ${containerId}:`, error);
|
|
39
|
+
await containerRemove(containerId).catch((removeError) => console.error(`Failed to remove container ${containerId} after start failure:`, removeError));
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export async function containerWait(containerId) {
|
|
44
|
+
try {
|
|
45
|
+
const response = await axios({
|
|
46
|
+
method: "POST",
|
|
47
|
+
socketPath: "/var/run/docker.sock",
|
|
48
|
+
url: `http://localhost/containers/${containerId}/wait`,
|
|
49
|
+
});
|
|
50
|
+
if (response.data.StatusCode !== 0) {
|
|
51
|
+
throw new Error(`Container ${containerId} exited with status code ${response.data.StatusCode}`);
|
|
52
|
+
}
|
|
53
|
+
return response.data.StatusCode;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.error(`Failed to wait for Docker container ${containerId}:`, error);
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export async function containerRemove(containerId) {
|
|
61
|
+
try {
|
|
62
|
+
await axios({
|
|
63
|
+
method: "DELETE",
|
|
64
|
+
socketPath: "/var/run/docker.sock",
|
|
65
|
+
url: `http://localhost/containers/${containerId}`,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.warn(`Failed to remove Docker container ${containerId}:`, error);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
export async function imageList() {
|
|
3
|
+
try {
|
|
4
|
+
const response = await axios({
|
|
5
|
+
method: "GET",
|
|
6
|
+
socketPath: "/var/run/docker.sock",
|
|
7
|
+
url: `http://localhost/images/json`,
|
|
8
|
+
});
|
|
9
|
+
return response.data
|
|
10
|
+
.map((image) => image.RepoTags)
|
|
11
|
+
.filter((tags) => tags)
|
|
12
|
+
.flat();
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
console.error("Failed to list Docker images:", error);
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export async function imagePull(image) {
|
|
20
|
+
try {
|
|
21
|
+
const response = await axios({
|
|
22
|
+
method: "POST",
|
|
23
|
+
socketPath: "/var/run/docker.sock",
|
|
24
|
+
url: `http://localhost/images/create?fromImage=${image}`,
|
|
25
|
+
});
|
|
26
|
+
return response.data;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.error(`Failed to pull Docker image ${image}:`, error);
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const image = "busybox:latest";
|
|
2
|
+
import { containerCreate, containerStart, containerWait, containerRemove, } from "./container.js";
|
|
3
|
+
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 });
|
|
11
|
+
await containerStart(containerId);
|
|
12
|
+
await containerWait(containerId);
|
|
13
|
+
}
|
|
14
|
+
const docker = {
|
|
15
|
+
run,
|
|
16
|
+
containerCreate,
|
|
17
|
+
containerStart,
|
|
18
|
+
containerWait,
|
|
19
|
+
containerRemove,
|
|
20
|
+
imageList,
|
|
21
|
+
imagePull,
|
|
22
|
+
};
|
|
23
|
+
export default docker;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dsmt",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Docker Storage Migration Tool",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"dsmt": "./dist/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"dev": "tsc -w",
|
|
11
|
+
"build": "tsc"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/itskdhere/dsmt.git"
|
|
16
|
+
},
|
|
17
|
+
"author": "itskdhere",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/itskdhere/dsmt/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/itskdhere/dsmt#readme",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"axios": "^1.8.4",
|
|
25
|
+
"chalk": "^5.4.1",
|
|
26
|
+
"commander": "^13.1.0",
|
|
27
|
+
"ora": "^8.2.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^22.14.1",
|
|
31
|
+
"typescript": "^5.8.3"
|
|
32
|
+
}
|
|
33
|
+
}
|