Skip to content

Commit 8109f53

Browse files
committed
Move utils to separate file
1 parent 901b785 commit 8109f53

File tree

3 files changed

+53
-56
lines changed

3 files changed

+53
-56
lines changed

ansible/ansible.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
3+
# Utilities for creating the Ansible environment we use.
4+
5+
import subprocess
6+
import pathlib
7+
import sys
8+
import shutil
9+
10+
BASE_PATH = pathlib.Path(__file__).resolve().parent
11+
VENV_PATH = BASE_PATH / ".venv"
12+
13+
# Ansible changes a lot between releases and deprecates a lot of stuff each of
14+
# them. Using a pinned ansible identical between all team members should
15+
# reduce the churn.
16+
def install_ansible(venv_path = VENV_PATH):
17+
requirements = BASE_PATH / "requirements.txt"
18+
venv_requirements = venv_path / "installed-requirements.txt"
19+
20+
# Avoid installing ansible in the virtualenv multiple times
21+
if venv_requirements.exists() and \
22+
venv_requirements.read_bytes() == requirements.read_bytes():
23+
return
24+
25+
print("creating a new virtual environment and install ansible in it...")
26+
shutil.rmtree(venv_path, ignore_errors=True)
27+
subprocess.run([sys.executable, "-m", "venv", str(venv_path)], check=True)
28+
subprocess.run([
29+
str(venv_path / "bin" / "pip"), "install", "-r", str(requirements),
30+
], check=True)
31+
shutil.copy(str(requirements), str(venv_requirements))
32+
33+
def create_workspace(dir, env, playbook):
34+
env_dir = BASE_PATH / "envs" / env
35+
# Create a temporary directory merging together the chosen
36+
# environment, the chosen playbook and the shared files.
37+
(dir / "play").mkdir()
38+
(dir / "play" / "roles").symlink_to(BASE_PATH / "roles")
39+
(dir / "play" / "group_vars").symlink_to(BASE_PATH / "group_vars")
40+
(dir / "play" / "playbook.yml").symlink_to(
41+
BASE_PATH / "playbooks" / (playbook + ".yml")
42+
)
43+
(dir / "env").symlink_to(env_dir)
44+
(dir / "ansible.cfg").symlink_to(BASE_PATH / "ansible.cfg")

ansible/apply

+4-41
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,18 @@
11
#!/usr/bin/env python3
22
import subprocess
33
import pathlib
4-
import sys
54
import shutil
65
import tempfile
76
import argparse
8-
import os
9-
10-
BASE_PATH = pathlib.Path(__file__).resolve().parent
11-
VENV_PATH = BASE_PATH / ".venv"
12-
13-
# Ansible changes a lot between releases and deprecates a lot of stuff each of
14-
# them. Using a pinned ansible identical between all team members should
15-
# reduce the churn.
16-
def install_ansible(venv_path = VENV_PATH):
17-
requirements = BASE_PATH / "requirements.txt"
18-
venv_requirements = venv_path / "installed-requirements.txt"
19-
20-
# Avoid installing ansible in the virtualenv multiple times
21-
if venv_requirements.exists() and \
22-
venv_requirements.read_bytes() == requirements.read_bytes():
23-
return
24-
25-
print("creating a new virtual environment and install ansible in it...")
26-
shutil.rmtree(venv_path, ignore_errors=True)
27-
subprocess.run([sys.executable, "-m", "venv", str(venv_path)], check=True)
28-
subprocess.run([
29-
str(venv_path / "bin" / "pip"), "install", "-r", str(requirements),
30-
], check=True)
31-
shutil.copy(str(requirements), str(venv_requirements))
32-
33-
def create_workspace(dir, env, playbook):
34-
env_dir = BASE_PATH / "envs" / env
35-
# Create a temporary directory merging together the chosen
36-
# environment, the chosen playbook and the shared files.
37-
(dir / "play").mkdir()
38-
(dir / "play" / "roles").symlink_to(BASE_PATH / "roles")
39-
(dir / "play" / "group_vars").symlink_to(BASE_PATH / "group_vars")
40-
(dir / "play" / "playbook.yml").symlink_to(
41-
BASE_PATH / "playbooks" / (playbook + ".yml")
42-
)
43-
(dir / "env").symlink_to(env_dir)
44-
(dir / "ansible.cfg").symlink_to(BASE_PATH / "ansible.cfg")
7+
import ansible
458

469
def run_playbook(args):
4710
tempdir = pathlib.Path(tempfile.mkdtemp())
48-
create_workspace(tempdir, args.env, args.playbook)
11+
ansible.create_workspace(tempdir, args.env, args.playbook)
4912
try:
5013
# Invoke the ansible binary installed in the virtualenv
5114
ansible_args = [
52-
str(VENV_PATH / "bin" / "ansible-playbook"),
15+
str(ansible.VENV_PATH / "bin" / "ansible-playbook"),
5316
"-i", str(tempdir / "env" / "hosts"),
5417
str(tempdir / "play" / "playbook.yml"),
5518
]
@@ -76,5 +39,5 @@ if __name__ == "__main__":
7639
)
7740
args = parser.parse_args()
7841

79-
install_ansible()
42+
ansible.install_ansible()
8043
run_playbook(args)

packer/packer

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
#!/usr/bin/env python3
22

33
import os
4-
import sys
54
import subprocess
65
import pathlib
76
import shutil
87
import argparse
9-
10-
# Python makes us jump through hoops to import module from
11-
# a file that does not end in .py
12-
def load_module():
13-
from importlib.util import spec_from_loader, module_from_spec
14-
from importlib.machinery import SourceFileLoader
15-
apply = os.path.join(os.path.dirname(__file__), '../ansible/apply')
16-
spec = spec_from_loader("apply", SourceFileLoader("apply", apply))
17-
apply = module_from_spec(spec)
18-
spec.loader.exec_module(apply)
19-
return apply
8+
import sys
9+
sys.path.append("../ansible")
10+
import ansible
2011

2112
# Utility for removing everything in a directory except one thing
2213
def remove_all_except_one(directory, exception):
@@ -46,9 +37,8 @@ def create_workspace_dir():
4637
# Create the workspace environment
4738
def create_workspace(env, playbook):
4839
workspace = create_workspace_dir()
49-
apply = load_module()
50-
apply.install_ansible(workspace / ".venv")
51-
apply.create_workspace(workspace, env, playbook)
40+
ansible.install_ansible(workspace / ".venv")
41+
ansible.create_workspace(workspace, env, playbook)
5242

5343
# Link the template into the workspace
5444
template_path = pathlib.Path(playbook).resolve()

0 commit comments

Comments
 (0)