vagrant-sandbox 0.1.0__tar.gz
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.
- vagrant_sandbox-0.1.0/LICENSE +22 -0
- vagrant_sandbox-0.1.0/PKG-INFO +96 -0
- vagrant_sandbox-0.1.0/README.md +70 -0
- vagrant_sandbox-0.1.0/pyproject.toml +45 -0
- vagrant_sandbox-0.1.0/setup.cfg +4 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox/__init__.py +4 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox/cli.py +97 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox/generator.py +100 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox/profiles/base.sh +9 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox/profiles/docker.sh +10 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox/profiles/nodejs.sh +9 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox/profiles/python.sh +6 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox/profiles/user-setup.sh +15 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox/settings.py +193 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox/templates/Vagrantfile.template +48 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox.egg-info/PKG-INFO +96 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox.egg-info/SOURCES.txt +21 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox.egg-info/dependency_links.txt +1 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox.egg-info/entry_points.txt +2 -0
- vagrant_sandbox-0.1.0/src/vagrant_sandbox.egg-info/top_level.txt +1 -0
- vagrant_sandbox-0.1.0/tests/test_cli.py +27 -0
- vagrant_sandbox-0.1.0/tests/test_generator.py +67 -0
- vagrant_sandbox-0.1.0/tests/test_settings.py +50 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 vagrant-sandbox
|
|
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.
|
|
22
|
+
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vagrant-sandbox
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Generate and provision isolated Vagrant development environments
|
|
5
|
+
Author: actocodes
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/actocodes/vagrant-sandbox
|
|
8
|
+
Project-URL: Repository, https://github.com/actocodes/vagrant-sandbox
|
|
9
|
+
Project-URL: Issues, https://github.com/actocodes/vagrant-sandbox/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/actocodes/vagrant-sandbox/releases
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: System :: Systems Administration
|
|
21
|
+
Classifier: Topic :: Utilities
|
|
22
|
+
Requires-Python: >=3.11
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# Vagrant Sandbox
|
|
28
|
+
|
|
29
|
+
`vagrant-sandbox` generates an isolated Ubuntu Vagrant environment, copies the selected provisioning scripts into the project, and can start the VM immediately.
|
|
30
|
+
|
|
31
|
+
## Requirements
|
|
32
|
+
|
|
33
|
+
- Python 3.11 or newer
|
|
34
|
+
- Vagrant installed and available on `PATH`
|
|
35
|
+
- A Vagrant provider such as VirtualBox, VMware, or Parallels
|
|
36
|
+
|
|
37
|
+
## Install and run
|
|
38
|
+
|
|
39
|
+
Install the command with `pipx`:
|
|
40
|
+
|
|
41
|
+
```console
|
|
42
|
+
pipx install vagrant-sandbox
|
|
43
|
+
vagrant-sandbox create
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
For a one-off run without a persistent installation:
|
|
47
|
+
|
|
48
|
+
```console
|
|
49
|
+
pipx run vagrant-sandbox create
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
For local development from a checkout:
|
|
53
|
+
|
|
54
|
+
```console
|
|
55
|
+
python -m pip install .
|
|
56
|
+
vagrant-sandbox create
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`create` prompts for VM settings in an interactive terminal, writes a `Vagrantfile` and `.vagrant-sandbox/provision/` scripts in the current directory, then runs `vagrant up`. To create the files without starting the VM:
|
|
60
|
+
|
|
61
|
+
```console
|
|
62
|
+
vagrant-sandbox create --no-up
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Use `--directory` to choose another output directory. Existing generated files are never replaced unless `--force` is provided.
|
|
66
|
+
|
|
67
|
+
Choose a distinct private IP for each VM that runs on the same host.
|
|
68
|
+
|
|
69
|
+
## Customize
|
|
70
|
+
|
|
71
|
+
Configuration precedence is command-line options over JSON config values over built-in defaults. An example config:
|
|
72
|
+
|
|
73
|
+
```json
|
|
74
|
+
{
|
|
75
|
+
"vm_name": "web-dev",
|
|
76
|
+
"vm_ip": "192.168.56.20",
|
|
77
|
+
"cpus": 4,
|
|
78
|
+
"memory": 4096,
|
|
79
|
+
"box": "bento/ubuntu-24.04",
|
|
80
|
+
"provider": "virtualbox",
|
|
81
|
+
"profiles": ["base", "docker", "nodejs"],
|
|
82
|
+
"custom_scripts": ["scripts/install-tools.sh"]
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Custom script paths in a config file are relative to that config file. Scripts are copied into the generated project and run as privileged Vagrant shell provisioners, after the built-in profiles. Only use scripts you trust.
|
|
87
|
+
|
|
88
|
+
```console
|
|
89
|
+
vagrant-sandbox create --config sandbox.json --vm-name web-dev --cpus 4
|
|
90
|
+
vagrant-sandbox create --profile python --profile docker --provision ./scripts/extra.sh
|
|
91
|
+
vagrant-sandbox profiles
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Supported profiles are `base`, `docker`, `nodejs`, and `python`. The `base` profile is always included. `--profile` can be repeated to choose profiles, when omitted, config profiles are used, then the default `base`.
|
|
95
|
+
|
|
96
|
+
The generated VM does not share the project directory with the host and disables SSH agent forwarding. It uses a host-only private network and does not forward application ports to localhost. Run `vagrant destroy` from the generated project directory to remove the VM.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Vagrant Sandbox
|
|
2
|
+
|
|
3
|
+
`vagrant-sandbox` generates an isolated Ubuntu Vagrant environment, copies the selected provisioning scripts into the project, and can start the VM immediately.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- Python 3.11 or newer
|
|
8
|
+
- Vagrant installed and available on `PATH`
|
|
9
|
+
- A Vagrant provider such as VirtualBox, VMware, or Parallels
|
|
10
|
+
|
|
11
|
+
## Install and run
|
|
12
|
+
|
|
13
|
+
Install the command with `pipx`:
|
|
14
|
+
|
|
15
|
+
```console
|
|
16
|
+
pipx install vagrant-sandbox
|
|
17
|
+
vagrant-sandbox create
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
For a one-off run without a persistent installation:
|
|
21
|
+
|
|
22
|
+
```console
|
|
23
|
+
pipx run vagrant-sandbox create
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
For local development from a checkout:
|
|
27
|
+
|
|
28
|
+
```console
|
|
29
|
+
python -m pip install .
|
|
30
|
+
vagrant-sandbox create
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`create` prompts for VM settings in an interactive terminal, writes a `Vagrantfile` and `.vagrant-sandbox/provision/` scripts in the current directory, then runs `vagrant up`. To create the files without starting the VM:
|
|
34
|
+
|
|
35
|
+
```console
|
|
36
|
+
vagrant-sandbox create --no-up
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Use `--directory` to choose another output directory. Existing generated files are never replaced unless `--force` is provided.
|
|
40
|
+
|
|
41
|
+
Choose a distinct private IP for each VM that runs on the same host.
|
|
42
|
+
|
|
43
|
+
## Customize
|
|
44
|
+
|
|
45
|
+
Configuration precedence is command-line options over JSON config values over built-in defaults. An example config:
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{
|
|
49
|
+
"vm_name": "web-dev",
|
|
50
|
+
"vm_ip": "192.168.56.20",
|
|
51
|
+
"cpus": 4,
|
|
52
|
+
"memory": 4096,
|
|
53
|
+
"box": "bento/ubuntu-24.04",
|
|
54
|
+
"provider": "virtualbox",
|
|
55
|
+
"profiles": ["base", "docker", "nodejs"],
|
|
56
|
+
"custom_scripts": ["scripts/install-tools.sh"]
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Custom script paths in a config file are relative to that config file. Scripts are copied into the generated project and run as privileged Vagrant shell provisioners, after the built-in profiles. Only use scripts you trust.
|
|
61
|
+
|
|
62
|
+
```console
|
|
63
|
+
vagrant-sandbox create --config sandbox.json --vm-name web-dev --cpus 4
|
|
64
|
+
vagrant-sandbox create --profile python --profile docker --provision ./scripts/extra.sh
|
|
65
|
+
vagrant-sandbox profiles
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Supported profiles are `base`, `docker`, `nodejs`, and `python`. The `base` profile is always included. `--profile` can be repeated to choose profiles, when omitted, config profiles are used, then the default `base`.
|
|
69
|
+
|
|
70
|
+
The generated VM does not share the project directory with the host and disables SSH agent forwarding. It uses a host-only private network and does not forward application ports to localhost. Run `vagrant destroy` from the generated project directory to remove the VM.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0.3"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "vagrant-sandbox"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Generate and provision isolated Vagrant development environments"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "actocodes" }]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Environment :: Console",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: 3.14",
|
|
24
|
+
"Topic :: System :: Systems Administration",
|
|
25
|
+
"Topic :: Utilities",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/actocodes/vagrant-sandbox"
|
|
30
|
+
Repository = "https://github.com/actocodes/vagrant-sandbox"
|
|
31
|
+
Issues = "https://github.com/actocodes/vagrant-sandbox/issues"
|
|
32
|
+
Changelog = "https://github.com/actocodes/vagrant-sandbox/releases"
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
vagrant-sandbox = "vagrant_sandbox.cli:main"
|
|
36
|
+
|
|
37
|
+
[tool.setuptools]
|
|
38
|
+
package-dir = { "" = "src" }
|
|
39
|
+
include-package-data = true
|
|
40
|
+
|
|
41
|
+
[tool.setuptools.packages.find]
|
|
42
|
+
where = ["src"]
|
|
43
|
+
|
|
44
|
+
[tool.setuptools.package-data]
|
|
45
|
+
vagrant_sandbox = ["templates/*", "profiles/*.sh"]
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from .generator import generate_project, start_vm
|
|
6
|
+
from .settings import AVAILABLE_PROFILES, load_config, resolve_settings
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
10
|
+
parser = argparse.ArgumentParser(
|
|
11
|
+
prog="vagrant-sandbox",
|
|
12
|
+
description="Generate and provision an isolated Vagrant development VM.",
|
|
13
|
+
)
|
|
14
|
+
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
15
|
+
|
|
16
|
+
create = subparsers.add_parser("create", help="Generate a Vagrantfile and start the VM.")
|
|
17
|
+
create.add_argument("--config", type=Path, help="JSON configuration file.")
|
|
18
|
+
create.add_argument(
|
|
19
|
+
"--directory",
|
|
20
|
+
type=Path,
|
|
21
|
+
default=Path.cwd(),
|
|
22
|
+
help="Project directory (default: current directory).",
|
|
23
|
+
)
|
|
24
|
+
create.add_argument("--vm-name")
|
|
25
|
+
create.add_argument("--vm-ip")
|
|
26
|
+
create.add_argument("--cpus", type=int)
|
|
27
|
+
create.add_argument("--memory", type=int, help="VM memory in MB.")
|
|
28
|
+
create.add_argument("--box")
|
|
29
|
+
create.add_argument("--provider", help="Vagrant provider (for example virtualbox).")
|
|
30
|
+
create.add_argument(
|
|
31
|
+
"--profile",
|
|
32
|
+
action="append",
|
|
33
|
+
choices=AVAILABLE_PROFILES,
|
|
34
|
+
dest="profiles",
|
|
35
|
+
help="Provisioning profile; repeat to select multiple (default: base).",
|
|
36
|
+
)
|
|
37
|
+
create.add_argument(
|
|
38
|
+
"--provision",
|
|
39
|
+
action="append",
|
|
40
|
+
type=Path,
|
|
41
|
+
dest="custom_scripts",
|
|
42
|
+
help="Custom shell provisioning script; repeat to add multiple.",
|
|
43
|
+
)
|
|
44
|
+
create.add_argument("--force", action="store_true", help="Replace generated files that already exist.")
|
|
45
|
+
create.add_argument("--no-up", action="store_true", help="Generate files without running vagrant up.")
|
|
46
|
+
create.add_argument("--non-interactive", action="store_true", help="Do not prompt; use config and defaults.")
|
|
47
|
+
|
|
48
|
+
subparsers.add_parser("profiles", help="List built-in provisioning profiles.")
|
|
49
|
+
return parser
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def main(argv: list[str] | None = None) -> int:
|
|
53
|
+
parser = build_parser()
|
|
54
|
+
args = parser.parse_args(argv)
|
|
55
|
+
if args.command == "profiles":
|
|
56
|
+
print("Available profiles:")
|
|
57
|
+
print(" base Common CLI tools, build essentials, Git, and workspace setup")
|
|
58
|
+
print(" docker Docker Engine")
|
|
59
|
+
print(" nodejs Node.js LTS")
|
|
60
|
+
print(" python Python 3, pip, and venv")
|
|
61
|
+
return 0
|
|
62
|
+
|
|
63
|
+
try:
|
|
64
|
+
config = load_config(args.config)
|
|
65
|
+
cli_values = {
|
|
66
|
+
key: getattr(args, key)
|
|
67
|
+
for key in (
|
|
68
|
+
"vm_name",
|
|
69
|
+
"vm_ip",
|
|
70
|
+
"cpus",
|
|
71
|
+
"memory",
|
|
72
|
+
"box",
|
|
73
|
+
"provider",
|
|
74
|
+
"profiles",
|
|
75
|
+
"custom_scripts",
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
settings = resolve_settings(
|
|
79
|
+
config,
|
|
80
|
+
cli_values,
|
|
81
|
+
interactive=not args.non_interactive and sys.stdin.isatty(),
|
|
82
|
+
)
|
|
83
|
+
vagrantfile = generate_project(settings, args.directory, force=args.force)
|
|
84
|
+
print(f"Generated {vagrantfile}")
|
|
85
|
+
if args.no_up:
|
|
86
|
+
return 0
|
|
87
|
+
result = start_vm(settings, args.directory.resolve())
|
|
88
|
+
if result:
|
|
89
|
+
print(f"vagrant up failed with exit code {result}.", file=sys.stderr)
|
|
90
|
+
return result
|
|
91
|
+
except (ValueError, FileExistsError, OSError, RuntimeError) as error:
|
|
92
|
+
print(f"Error: {error}", file=sys.stderr)
|
|
93
|
+
return 1
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
if __name__ == "__main__":
|
|
97
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import shutil
|
|
3
|
+
import subprocess
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .settings import Settings
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
PACKAGE_DIR = Path(__file__).parent
|
|
10
|
+
TEMPLATE_PATH = PACKAGE_DIR / "templates" / "Vagrantfile.template"
|
|
11
|
+
PROFILES_DIR = PACKAGE_DIR / "profiles"
|
|
12
|
+
PROVISION_DIR = Path(".vagrant-sandbox") / "provision"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def render_vagrantfile(settings: Settings) -> str:
|
|
16
|
+
blocks = [
|
|
17
|
+
_provision_block(f"profile-{profile}", PROVISION_DIR / f"{profile}.sh")
|
|
18
|
+
for profile in settings.profiles
|
|
19
|
+
]
|
|
20
|
+
blocks.append(
|
|
21
|
+
_provision_block(
|
|
22
|
+
"user-setup",
|
|
23
|
+
PROVISION_DIR / "user-setup.sh",
|
|
24
|
+
privileged=False,
|
|
25
|
+
)
|
|
26
|
+
)
|
|
27
|
+
blocks.extend(
|
|
28
|
+
_provision_block(
|
|
29
|
+
f"custom-{index + 1}",
|
|
30
|
+
PROVISION_DIR / f"custom-{index + 1}-{script.name}",
|
|
31
|
+
)
|
|
32
|
+
for index, script in enumerate(settings.custom_scripts)
|
|
33
|
+
)
|
|
34
|
+
template = TEMPLATE_PATH.read_text(encoding="utf-8")
|
|
35
|
+
replacements = {
|
|
36
|
+
"@@VM_NAME@@": json.dumps(settings.vm_name),
|
|
37
|
+
"@@VM_IP@@": json.dumps(settings.vm_ip),
|
|
38
|
+
"@@VM_CPUS@@": str(settings.cpus),
|
|
39
|
+
"@@VM_MEMORY@@": str(settings.memory),
|
|
40
|
+
"@@BOX@@": json.dumps(settings.box),
|
|
41
|
+
"@@PROVISIONERS@@": "\n".join(blocks),
|
|
42
|
+
}
|
|
43
|
+
for marker, value in replacements.items():
|
|
44
|
+
template = template.replace(marker, value)
|
|
45
|
+
return template
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def generate_project(settings: Settings, output_dir: Path, force: bool = False) -> Path:
|
|
49
|
+
output_dir = output_dir.resolve()
|
|
50
|
+
vagrantfile = output_dir / "Vagrantfile"
|
|
51
|
+
provision_dir = output_dir / PROVISION_DIR
|
|
52
|
+
generated_paths = [vagrantfile]
|
|
53
|
+
generated_paths.extend(
|
|
54
|
+
provision_dir / f"{profile}.sh" for profile in settings.profiles
|
|
55
|
+
)
|
|
56
|
+
generated_paths.append(provision_dir / "user-setup.sh")
|
|
57
|
+
generated_paths.extend(
|
|
58
|
+
provision_dir / f"custom-{index + 1}-{script.name}"
|
|
59
|
+
for index, script in enumerate(settings.custom_scripts)
|
|
60
|
+
)
|
|
61
|
+
existing = [path for path in generated_paths if path.exists()]
|
|
62
|
+
if existing and not force:
|
|
63
|
+
names = ", ".join(str(path.relative_to(output_dir)) for path in existing)
|
|
64
|
+
raise FileExistsError(
|
|
65
|
+
f"Generated file(s) already exist: {names}. Use --force to replace them."
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
provision_dir.mkdir(parents=True, exist_ok=True)
|
|
69
|
+
for profile in settings.profiles:
|
|
70
|
+
source = PROFILES_DIR / f"{profile}.sh"
|
|
71
|
+
shutil.copyfile(source, provision_dir / f"{profile}.sh")
|
|
72
|
+
shutil.copyfile(PROFILES_DIR / "user-setup.sh", provision_dir / "user-setup.sh")
|
|
73
|
+
for index, script in enumerate(settings.custom_scripts):
|
|
74
|
+
shutil.copyfile(script, provision_dir / f"custom-{index + 1}-{script.name}")
|
|
75
|
+
vagrantfile.write_text(render_vagrantfile(settings), encoding="utf-8", newline="\n")
|
|
76
|
+
return vagrantfile
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def start_vm(settings: Settings, output_dir: Path) -> int:
|
|
80
|
+
command = ["vagrant", "up"]
|
|
81
|
+
if settings.provider:
|
|
82
|
+
command.extend(["--provider", settings.provider])
|
|
83
|
+
try:
|
|
84
|
+
result = subprocess.run(command, cwd=output_dir, check=False)
|
|
85
|
+
except FileNotFoundError as error:
|
|
86
|
+
raise RuntimeError(
|
|
87
|
+
"Vagrant was not found. Install Vagrant and ensure it is on PATH."
|
|
88
|
+
) from error
|
|
89
|
+
return result.returncode
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _provision_block(name: str, script_path: Path, privileged: bool = True) -> str:
|
|
93
|
+
relative = script_path.as_posix()
|
|
94
|
+
options = (
|
|
95
|
+
f"name: {json.dumps(name)}, "
|
|
96
|
+
f"path: File.expand_path({json.dumps(relative)}, __dir__)"
|
|
97
|
+
)
|
|
98
|
+
if not privileged:
|
|
99
|
+
options += ", privileged: false"
|
|
100
|
+
return f' config.vm.provision "shell", {options}\n'
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
set -euo pipefail
|
|
3
|
+
export DEBIAN_FRONTEND=noninteractive
|
|
4
|
+
|
|
5
|
+
apt-get update -y
|
|
6
|
+
apt-get upgrade -y
|
|
7
|
+
apt-get install -y build-essential git curl wget unzip jq vim tmux htop tree ripgrep ca-certificates gnupg ufw
|
|
8
|
+
timedatectl set-timezone UTC
|
|
9
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
set -euo pipefail
|
|
3
|
+
|
|
4
|
+
mkdir -p "$HOME/workspace"
|
|
5
|
+
if ! grep -Fq "alias ll=" "$HOME/.bashrc"; then
|
|
6
|
+
cat >> "$HOME/.bashrc" <<'EOF'
|
|
7
|
+
|
|
8
|
+
# --- dev sandbox ---
|
|
9
|
+
alias ll='ls -alF'
|
|
10
|
+
alias gs='git status'
|
|
11
|
+
cd ~/workspace 2>/dev/null || true
|
|
12
|
+
EOF
|
|
13
|
+
fi
|
|
14
|
+
git config --global init.defaultBranch main
|
|
15
|
+
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import ipaddress
|
|
2
|
+
import json
|
|
3
|
+
import re
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any, Callable
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
DEFAULTS: dict[str, Any] = {
|
|
10
|
+
"vm_name": "sandbox",
|
|
11
|
+
"vm_ip": "192.168.56.10",
|
|
12
|
+
"cpus": 2,
|
|
13
|
+
"memory": 2048,
|
|
14
|
+
"box": "bento/ubuntu-24.04",
|
|
15
|
+
"provider": None,
|
|
16
|
+
"profiles": ["base"],
|
|
17
|
+
"custom_scripts": [],
|
|
18
|
+
}
|
|
19
|
+
ALLOWED_CONFIG_KEYS = frozenset(DEFAULTS)
|
|
20
|
+
AVAILABLE_PROFILES = ("base", "docker", "nodejs", "python")
|
|
21
|
+
PRIVATE_NETWORKS = tuple(
|
|
22
|
+
ipaddress.ip_network(network)
|
|
23
|
+
for network in ("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16")
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class Settings:
|
|
29
|
+
vm_name: str
|
|
30
|
+
vm_ip: str
|
|
31
|
+
cpus: int
|
|
32
|
+
memory: int
|
|
33
|
+
box: str
|
|
34
|
+
provider: str | None
|
|
35
|
+
profiles: tuple[str, ...]
|
|
36
|
+
custom_scripts: tuple[Path, ...]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def load_config(path: Path | None) -> dict[str, Any]:
|
|
40
|
+
if path is None:
|
|
41
|
+
return {}
|
|
42
|
+
try:
|
|
43
|
+
with path.open(encoding="utf-8") as config_file:
|
|
44
|
+
data = json.load(config_file)
|
|
45
|
+
except (OSError, json.JSONDecodeError) as error:
|
|
46
|
+
raise ValueError(f"Could not read config file '{path}': {error}") from error
|
|
47
|
+
if not isinstance(data, dict):
|
|
48
|
+
raise ValueError("Config file must contain a JSON object.")
|
|
49
|
+
unknown = set(data) - ALLOWED_CONFIG_KEYS
|
|
50
|
+
if unknown:
|
|
51
|
+
raise ValueError(f"Unknown config option(s): {', '.join(sorted(unknown))}")
|
|
52
|
+
if "custom_scripts" in data:
|
|
53
|
+
scripts = data["custom_scripts"]
|
|
54
|
+
if not isinstance(scripts, list) or not all(isinstance(item, str) for item in scripts):
|
|
55
|
+
raise ValueError("'custom_scripts' must be a list of file paths.")
|
|
56
|
+
data["custom_scripts"] = [
|
|
57
|
+
(path.parent / item).resolve() if not Path(item).is_absolute() else Path(item)
|
|
58
|
+
for item in scripts
|
|
59
|
+
]
|
|
60
|
+
return data
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def resolve_settings(
|
|
64
|
+
config: dict[str, Any],
|
|
65
|
+
cli: dict[str, Any],
|
|
66
|
+
interactive: bool = False,
|
|
67
|
+
input_fn: Callable[[str], str] = input,
|
|
68
|
+
) -> Settings:
|
|
69
|
+
values = {**DEFAULTS, **config}
|
|
70
|
+
for key, value in cli.items():
|
|
71
|
+
if value is not None:
|
|
72
|
+
if key == "custom_scripts":
|
|
73
|
+
values[key] = [*values["custom_scripts"], *value]
|
|
74
|
+
else:
|
|
75
|
+
values[key] = value
|
|
76
|
+
|
|
77
|
+
if interactive:
|
|
78
|
+
prompts = (
|
|
79
|
+
("vm_name", "VM name"),
|
|
80
|
+
("vm_ip", "Private IPv4 address"),
|
|
81
|
+
("cpus", "CPU cores"),
|
|
82
|
+
("memory", "Memory in MB"),
|
|
83
|
+
("box", "Vagrant box"),
|
|
84
|
+
("provider", "Provider (blank for Vagrant default)"),
|
|
85
|
+
("profiles", "Provisioning profiles (comma-separated)"),
|
|
86
|
+
("custom_scripts", "Custom shell scripts (comma-separated paths)"),
|
|
87
|
+
)
|
|
88
|
+
for key, label in prompts:
|
|
89
|
+
if cli.get(key) is not None:
|
|
90
|
+
continue
|
|
91
|
+
current = values[key]
|
|
92
|
+
if key in ("profiles", "custom_scripts"):
|
|
93
|
+
shown = ", ".join(str(item) for item in current)
|
|
94
|
+
else:
|
|
95
|
+
shown = "" if current is None else str(current)
|
|
96
|
+
answer = input_fn(f"{label} [{shown}]: ").strip()
|
|
97
|
+
if not answer:
|
|
98
|
+
if key == "provider":
|
|
99
|
+
values[key] = None
|
|
100
|
+
continue
|
|
101
|
+
if key in ("profiles", "custom_scripts"):
|
|
102
|
+
items = [item.strip() for item in answer.split(",") if item.strip()]
|
|
103
|
+
values[key] = items if key == "profiles" else [
|
|
104
|
+
Path(item).expanduser().resolve() for item in items
|
|
105
|
+
]
|
|
106
|
+
elif key in ("cpus", "memory"):
|
|
107
|
+
try:
|
|
108
|
+
values[key] = int(answer)
|
|
109
|
+
except ValueError as error:
|
|
110
|
+
raise ValueError(f"{label} must be an integer.") from error
|
|
111
|
+
else:
|
|
112
|
+
values[key] = answer or None
|
|
113
|
+
|
|
114
|
+
return validate_settings(values)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def validate_settings(values: dict[str, Any]) -> Settings:
|
|
118
|
+
vm_name = values["vm_name"]
|
|
119
|
+
if not isinstance(vm_name, str) or not re.fullmatch(
|
|
120
|
+
r"[A-Za-z0-9][A-Za-z0-9_-]{0,62}", vm_name
|
|
121
|
+
):
|
|
122
|
+
raise ValueError(
|
|
123
|
+
"VM name must be 1-63 letters, digits, hyphens, or underscores and start with a letter or digit."
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
vm_ip = values["vm_ip"]
|
|
127
|
+
if not isinstance(vm_ip, str):
|
|
128
|
+
raise ValueError("VM IP must be a valid private IPv4 address.")
|
|
129
|
+
try:
|
|
130
|
+
ip = ipaddress.ip_address(vm_ip)
|
|
131
|
+
except (TypeError, ValueError) as error:
|
|
132
|
+
raise ValueError("VM IP must be a valid IPv4 or IPv6 address.") from error
|
|
133
|
+
if not isinstance(ip, ipaddress.IPv4Address):
|
|
134
|
+
raise ValueError("VM IP must be IPv4 for Vagrant's host-only network.")
|
|
135
|
+
if not any(ip in network for network in PRIVATE_NETWORKS):
|
|
136
|
+
raise ValueError(
|
|
137
|
+
"VM IP must belong to a private IPv4 range (10/8, 172.16/12, or 192.168/16)."
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
cpus = _bounded_integer(values["cpus"], "CPU count", 1, 256)
|
|
141
|
+
memory = _bounded_integer(values["memory"], "Memory", 512, 1_048_576)
|
|
142
|
+
box = values["box"]
|
|
143
|
+
if not isinstance(box, str) or not box.strip() or any(char in box for char in "\r\n"):
|
|
144
|
+
raise ValueError("Vagrant box must be a non-empty single-line string.")
|
|
145
|
+
|
|
146
|
+
provider = values["provider"]
|
|
147
|
+
if provider is not None and (
|
|
148
|
+
not isinstance(provider, str) or not re.fullmatch(r"[A-Za-z0-9_-]+", provider)
|
|
149
|
+
):
|
|
150
|
+
raise ValueError("Provider must contain only letters, digits, hyphens, or underscores.")
|
|
151
|
+
|
|
152
|
+
profiles_value = values["profiles"]
|
|
153
|
+
if not isinstance(profiles_value, (list, tuple)) or not all(
|
|
154
|
+
isinstance(item, str) for item in profiles_value
|
|
155
|
+
):
|
|
156
|
+
raise ValueError("'profiles' must be a list of profile names.")
|
|
157
|
+
profiles = list(dict.fromkeys(profiles_value))
|
|
158
|
+
unknown_profiles = set(profiles) - set(AVAILABLE_PROFILES)
|
|
159
|
+
if unknown_profiles:
|
|
160
|
+
raise ValueError(f"Unknown profile(s): {', '.join(sorted(unknown_profiles))}")
|
|
161
|
+
if not profiles:
|
|
162
|
+
profiles = ["base"]
|
|
163
|
+
if "base" not in profiles:
|
|
164
|
+
profiles.insert(0, "base")
|
|
165
|
+
|
|
166
|
+
scripts_value = values["custom_scripts"]
|
|
167
|
+
if not isinstance(scripts_value, (list, tuple)):
|
|
168
|
+
raise ValueError("'custom_scripts' must be a list of file paths.")
|
|
169
|
+
scripts: list[Path] = []
|
|
170
|
+
for script in scripts_value:
|
|
171
|
+
if not isinstance(script, (str, Path)):
|
|
172
|
+
raise ValueError("Each custom provisioning script must be a file path.")
|
|
173
|
+
path = Path(script).expanduser().resolve()
|
|
174
|
+
if not path.is_file():
|
|
175
|
+
raise ValueError(f"Custom provisioning script does not exist: {path}")
|
|
176
|
+
scripts.append(path)
|
|
177
|
+
|
|
178
|
+
return Settings(
|
|
179
|
+
vm_name=vm_name,
|
|
180
|
+
vm_ip=str(ip),
|
|
181
|
+
cpus=cpus,
|
|
182
|
+
memory=memory,
|
|
183
|
+
box=box.strip(),
|
|
184
|
+
provider=provider,
|
|
185
|
+
profiles=tuple(profiles),
|
|
186
|
+
custom_scripts=tuple(scripts),
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _bounded_integer(value: Any, label: str, minimum: int, maximum: int) -> int:
|
|
191
|
+
if isinstance(value, bool) or not isinstance(value, int) or not minimum <= value <= maximum:
|
|
192
|
+
raise ValueError(f"{label} must be between {minimum} and {maximum}.")
|
|
193
|
+
return value
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Generated by vagrant-sandbox. Edit settings through the config or CLI.
|
|
2
|
+
VM_NAME = @@VM_NAME@@
|
|
3
|
+
VM_IP = @@VM_IP@@
|
|
4
|
+
VM_CPUS = @@VM_CPUS@@
|
|
5
|
+
VM_MEMORY = @@VM_MEMORY@@
|
|
6
|
+
|
|
7
|
+
Vagrant.configure("2") do |config|
|
|
8
|
+
config.vm.box = @@BOX@@
|
|
9
|
+
config.vm.hostname = VM_NAME
|
|
10
|
+
|
|
11
|
+
# Keep project files on the host isolated from the guest.
|
|
12
|
+
config.vm.synced_folder ".", "/vagrant", disabled: true
|
|
13
|
+
config.ssh.forward_agent = false
|
|
14
|
+
config.ssh.forward_x11 = false
|
|
15
|
+
config.vm.network "private_network", ip: VM_IP
|
|
16
|
+
|
|
17
|
+
config.vm.provider "virtualbox" do |vb|
|
|
18
|
+
vb.name = VM_NAME
|
|
19
|
+
vb.cpus = VM_CPUS
|
|
20
|
+
vb.memory = VM_MEMORY
|
|
21
|
+
vb.linked_clone = true
|
|
22
|
+
vb.customize ["modifyvm", :id, "--clipboard-mode", "disabled"]
|
|
23
|
+
vb.customize ["modifyvm", :id, "--draganddrop", "disabled"]
|
|
24
|
+
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
config.vm.provider "vmware_desktop" do |vmware|
|
|
28
|
+
vmware.vmx["numvcpus"] = VM_CPUS.to_s
|
|
29
|
+
vmware.vmx["memsize"] = VM_MEMORY.to_s
|
|
30
|
+
vmware.vmx["isolation.tools.copy.disable"] = "TRUE"
|
|
31
|
+
vmware.vmx["isolation.tools.paste.disable"] = "TRUE"
|
|
32
|
+
vmware.vmx["isolation.tools.dnd.disable"] = "TRUE"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
config.vm.provider "parallels" do |parallels|
|
|
36
|
+
parallels.cpus = VM_CPUS
|
|
37
|
+
parallels.memory = VM_MEMORY
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
@@PROVISIONERS@@
|
|
41
|
+
|
|
42
|
+
config.vm.post_up_message = <<-MSG
|
|
43
|
+
Sandbox '#{VM_NAME}' is up at #{VM_IP}.
|
|
44
|
+
Clone repositories into ~/workspace inside the VM; host folders are not shared.
|
|
45
|
+
Add SSH config with: vagrant ssh-config --host sandbox-#{VM_NAME}
|
|
46
|
+
MSG
|
|
47
|
+
end
|
|
48
|
+
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vagrant-sandbox
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Generate and provision isolated Vagrant development environments
|
|
5
|
+
Author: actocodes
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/actocodes/vagrant-sandbox
|
|
8
|
+
Project-URL: Repository, https://github.com/actocodes/vagrant-sandbox
|
|
9
|
+
Project-URL: Issues, https://github.com/actocodes/vagrant-sandbox/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/actocodes/vagrant-sandbox/releases
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: System :: Systems Administration
|
|
21
|
+
Classifier: Topic :: Utilities
|
|
22
|
+
Requires-Python: >=3.11
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# Vagrant Sandbox
|
|
28
|
+
|
|
29
|
+
`vagrant-sandbox` generates an isolated Ubuntu Vagrant environment, copies the selected provisioning scripts into the project, and can start the VM immediately.
|
|
30
|
+
|
|
31
|
+
## Requirements
|
|
32
|
+
|
|
33
|
+
- Python 3.11 or newer
|
|
34
|
+
- Vagrant installed and available on `PATH`
|
|
35
|
+
- A Vagrant provider such as VirtualBox, VMware, or Parallels
|
|
36
|
+
|
|
37
|
+
## Install and run
|
|
38
|
+
|
|
39
|
+
Install the command with `pipx`:
|
|
40
|
+
|
|
41
|
+
```console
|
|
42
|
+
pipx install vagrant-sandbox
|
|
43
|
+
vagrant-sandbox create
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
For a one-off run without a persistent installation:
|
|
47
|
+
|
|
48
|
+
```console
|
|
49
|
+
pipx run vagrant-sandbox create
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
For local development from a checkout:
|
|
53
|
+
|
|
54
|
+
```console
|
|
55
|
+
python -m pip install .
|
|
56
|
+
vagrant-sandbox create
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`create` prompts for VM settings in an interactive terminal, writes a `Vagrantfile` and `.vagrant-sandbox/provision/` scripts in the current directory, then runs `vagrant up`. To create the files without starting the VM:
|
|
60
|
+
|
|
61
|
+
```console
|
|
62
|
+
vagrant-sandbox create --no-up
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Use `--directory` to choose another output directory. Existing generated files are never replaced unless `--force` is provided.
|
|
66
|
+
|
|
67
|
+
Choose a distinct private IP for each VM that runs on the same host.
|
|
68
|
+
|
|
69
|
+
## Customize
|
|
70
|
+
|
|
71
|
+
Configuration precedence is command-line options over JSON config values over built-in defaults. An example config:
|
|
72
|
+
|
|
73
|
+
```json
|
|
74
|
+
{
|
|
75
|
+
"vm_name": "web-dev",
|
|
76
|
+
"vm_ip": "192.168.56.20",
|
|
77
|
+
"cpus": 4,
|
|
78
|
+
"memory": 4096,
|
|
79
|
+
"box": "bento/ubuntu-24.04",
|
|
80
|
+
"provider": "virtualbox",
|
|
81
|
+
"profiles": ["base", "docker", "nodejs"],
|
|
82
|
+
"custom_scripts": ["scripts/install-tools.sh"]
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Custom script paths in a config file are relative to that config file. Scripts are copied into the generated project and run as privileged Vagrant shell provisioners, after the built-in profiles. Only use scripts you trust.
|
|
87
|
+
|
|
88
|
+
```console
|
|
89
|
+
vagrant-sandbox create --config sandbox.json --vm-name web-dev --cpus 4
|
|
90
|
+
vagrant-sandbox create --profile python --profile docker --provision ./scripts/extra.sh
|
|
91
|
+
vagrant-sandbox profiles
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Supported profiles are `base`, `docker`, `nodejs`, and `python`. The `base` profile is always included. `--profile` can be repeated to choose profiles, when omitted, config profiles are used, then the default `base`.
|
|
95
|
+
|
|
96
|
+
The generated VM does not share the project directory with the host and disables SSH agent forwarding. It uses a host-only private network and does not forward application ports to localhost. Run `vagrant destroy` from the generated project directory to remove the VM.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/vagrant_sandbox/__init__.py
|
|
5
|
+
src/vagrant_sandbox/cli.py
|
|
6
|
+
src/vagrant_sandbox/generator.py
|
|
7
|
+
src/vagrant_sandbox/settings.py
|
|
8
|
+
src/vagrant_sandbox.egg-info/PKG-INFO
|
|
9
|
+
src/vagrant_sandbox.egg-info/SOURCES.txt
|
|
10
|
+
src/vagrant_sandbox.egg-info/dependency_links.txt
|
|
11
|
+
src/vagrant_sandbox.egg-info/entry_points.txt
|
|
12
|
+
src/vagrant_sandbox.egg-info/top_level.txt
|
|
13
|
+
src/vagrant_sandbox/profiles/base.sh
|
|
14
|
+
src/vagrant_sandbox/profiles/docker.sh
|
|
15
|
+
src/vagrant_sandbox/profiles/nodejs.sh
|
|
16
|
+
src/vagrant_sandbox/profiles/python.sh
|
|
17
|
+
src/vagrant_sandbox/profiles/user-setup.sh
|
|
18
|
+
src/vagrant_sandbox/templates/Vagrantfile.template
|
|
19
|
+
tests/test_cli.py
|
|
20
|
+
tests/test_generator.py
|
|
21
|
+
tests/test_settings.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
vagrant_sandbox
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import tempfile
|
|
2
|
+
import unittest
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from vagrant_sandbox.cli import main
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class CliTests(unittest.TestCase):
|
|
9
|
+
def test_no_up_generates_project_files(self):
|
|
10
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
11
|
+
result = main(
|
|
12
|
+
[
|
|
13
|
+
"create",
|
|
14
|
+
"--directory",
|
|
15
|
+
directory,
|
|
16
|
+
"--non-interactive",
|
|
17
|
+
"--no-up",
|
|
18
|
+
]
|
|
19
|
+
)
|
|
20
|
+
root = Path(directory)
|
|
21
|
+
self.assertEqual(result, 0)
|
|
22
|
+
self.assertTrue((root / "Vagrantfile").is_file())
|
|
23
|
+
self.assertTrue((root / ".vagrant-sandbox" / "provision" / "user-setup.sh").is_file())
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
if __name__ == "__main__":
|
|
27
|
+
unittest.main()
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import tempfile
|
|
2
|
+
import unittest
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from unittest.mock import patch
|
|
5
|
+
|
|
6
|
+
from vagrant_sandbox.generator import generate_project, render_vagrantfile, start_vm
|
|
7
|
+
from vagrant_sandbox.settings import resolve_settings
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class GeneratorTests(unittest.TestCase):
|
|
11
|
+
def test_render_preserves_isolation_and_selected_provisioners(self):
|
|
12
|
+
settings = resolve_settings({}, {"vm_name": "testbox", "profiles": ["nodejs"]})
|
|
13
|
+
content = render_vagrantfile(settings)
|
|
14
|
+
self.assertIn('config.vm.synced_folder ".", "/vagrant", disabled: true', content)
|
|
15
|
+
self.assertIn("config.ssh.forward_agent = false", content)
|
|
16
|
+
self.assertIn('name: "profile-base"', content)
|
|
17
|
+
self.assertIn('name: "profile-nodejs"', content)
|
|
18
|
+
self.assertIn('name: "user-setup"', content)
|
|
19
|
+
self.assertIn("privileged: false", content)
|
|
20
|
+
self.assertIn('VM_NAME = "testbox"', content)
|
|
21
|
+
|
|
22
|
+
def test_generation_refuses_to_replace_without_force(self):
|
|
23
|
+
settings = resolve_settings({}, {})
|
|
24
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
25
|
+
output = Path(directory)
|
|
26
|
+
(output / "Vagrantfile").write_text("keep me", encoding="utf-8")
|
|
27
|
+
with self.assertRaises(FileExistsError):
|
|
28
|
+
generate_project(settings, output)
|
|
29
|
+
self.assertEqual((output / "Vagrantfile").read_text(encoding="utf-8"), "keep me")
|
|
30
|
+
|
|
31
|
+
def test_custom_script_is_copied_and_referenced(self):
|
|
32
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
33
|
+
root = Path(directory)
|
|
34
|
+
custom_script = root / "custom.sh"
|
|
35
|
+
custom_script.write_text("echo custom\n", encoding="utf-8")
|
|
36
|
+
settings = resolve_settings({}, {"custom_scripts": [custom_script]})
|
|
37
|
+
output = root / "project"
|
|
38
|
+
generate_project(settings, output)
|
|
39
|
+
self.assertEqual(
|
|
40
|
+
(output / ".vagrant-sandbox" / "provision" / "custom-1-custom.sh").read_text(encoding="utf-8"),
|
|
41
|
+
"echo custom\n",
|
|
42
|
+
)
|
|
43
|
+
generated = (output / "Vagrantfile").read_text(encoding="utf-8")
|
|
44
|
+
self.assertIn("custom-1-custom.sh", generated)
|
|
45
|
+
|
|
46
|
+
def test_interactive_blank_provider_clears_configured_provider(self):
|
|
47
|
+
answers = iter(("", "", "", "", "", "", "", ""))
|
|
48
|
+
settings = resolve_settings(
|
|
49
|
+
{"provider": "virtualbox"},
|
|
50
|
+
{},
|
|
51
|
+
interactive=True,
|
|
52
|
+
input_fn=lambda _prompt: next(answers),
|
|
53
|
+
)
|
|
54
|
+
self.assertIsNone(settings.provider)
|
|
55
|
+
|
|
56
|
+
def test_start_vm_passes_provider_without_shell(self):
|
|
57
|
+
settings = resolve_settings({}, {"provider": "virtualbox"})
|
|
58
|
+
with patch("vagrant_sandbox.generator.subprocess.run") as run:
|
|
59
|
+
run.return_value.returncode = 0
|
|
60
|
+
self.assertEqual(start_vm(settings, Path(".")), 0)
|
|
61
|
+
run.assert_called_once_with(
|
|
62
|
+
["vagrant", "up", "--provider", "virtualbox"], cwd=Path("."), check=False
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if __name__ == "__main__":
|
|
67
|
+
unittest.main()
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import tempfile
|
|
2
|
+
import unittest
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from vagrant_sandbox.settings import load_config, resolve_settings
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class SettingsTests(unittest.TestCase):
|
|
9
|
+
def test_base_profile_is_always_included(self):
|
|
10
|
+
settings = resolve_settings({}, {"profiles": ["docker"]})
|
|
11
|
+
self.assertEqual(settings.profiles, ("base", "docker"))
|
|
12
|
+
|
|
13
|
+
def test_cli_overrides_config(self):
|
|
14
|
+
settings = resolve_settings(
|
|
15
|
+
{"vm_name": "from-config", "cpus": 4},
|
|
16
|
+
{"vm_name": "from-cli", "cpus": None},
|
|
17
|
+
)
|
|
18
|
+
self.assertEqual(settings.vm_name, "from-cli")
|
|
19
|
+
self.assertEqual(settings.cpus, 4)
|
|
20
|
+
|
|
21
|
+
def test_rejects_invalid_ip_and_unknown_profile(self):
|
|
22
|
+
for values in (
|
|
23
|
+
{"vm_ip": "not-an-ip"},
|
|
24
|
+
{"vm_ip": "8.8.8.8"},
|
|
25
|
+
{"profiles": ["not-a-profile"]},
|
|
26
|
+
):
|
|
27
|
+
with self.subTest(values=values), self.assertRaises(ValueError):
|
|
28
|
+
resolve_settings(values, {})
|
|
29
|
+
|
|
30
|
+
def test_custom_script_paths_are_relative_to_config(self):
|
|
31
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
32
|
+
root = Path(directory)
|
|
33
|
+
script = root / "install.sh"
|
|
34
|
+
script.write_text("echo ready\n", encoding="utf-8")
|
|
35
|
+
config_file = root / "config.json"
|
|
36
|
+
config_file.write_text('{"custom_scripts": ["install.sh"]}', encoding="utf-8")
|
|
37
|
+
config = load_config(config_file)
|
|
38
|
+
settings = resolve_settings(config, {})
|
|
39
|
+
self.assertEqual(settings.custom_scripts, (script.resolve(),))
|
|
40
|
+
|
|
41
|
+
def test_interactive_values_override_defaults(self):
|
|
42
|
+
answers = iter(("devbox", "192.168.56.22", "4", "4096", "ubuntu/jammy64", "", "base,python", ""))
|
|
43
|
+
settings = resolve_settings({}, {}, interactive=True, input_fn=lambda _prompt: next(answers))
|
|
44
|
+
self.assertEqual(settings.vm_name, "devbox")
|
|
45
|
+
self.assertEqual(settings.cpus, 4)
|
|
46
|
+
self.assertEqual(settings.profiles, ("base", "python"))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
if __name__ == "__main__":
|
|
50
|
+
unittest.main()
|