forked from gbagnoli/devmachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
223 lines (185 loc) · 6.66 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
from __future__ import print_function
import json
import os
import socket
import sys
from io import StringIO
from typing import Optional, Tuple
import yaml
from fabric.api import env, get, hide, local, put, settings, sudo, task
from fabric.contrib.files import contains as remote_contains
from fabric.contrib.files import exists as remote_exists
from fabric.contrib.project import rsync_project
env.use_ssh_config = True
chef_command = "chef-client -N {host} -z -c chef-client.rb -o 'role[{host}]'{secrets}"
chef_script = """
#!/bin/bash
cd {remote}
{chef_command} "$@"
"""
script = "/usr/local/bin/run-chef"
wrapper_script = """#!/bin/bash
sudo {} "$@"
""".format(
script
)
sudoers = "/etc/sudoers.d/chef"
def vendor() -> None:
with settings(hide("stdout")):
try:
local("berks vendor")
except Exception:
local("bundle exec berks vendor")
def validate_secrets(
secrets_local: str, secrets: str, remote: str, local: bool
) -> bool:
remote_path = os.path.join(remote, secrets)
try:
with open(secrets_local) as f:
json.load(f)
return False
except Exception as e:
print(f"Invalid secrets file at {secrets_local}: {e}.", file=sys.stderr)
if not local and remote_exists(remote_path):
print("File is not valid locally, but exists remotely.", file=sys.stderr)
print("Using remote version.", file=sys.stderr)
return True
else:
print("aborting", file=sys.stderr)
sys.exit(1)
def chef(host: str, remote: str, secrets: Optional[str] = None) -> None:
secrets_opts = ""
if secrets:
secrets_opts = f" -j {secrets}"
cmd = chef_command.format(host=host, secrets=secrets_opts)
wrapper = "/usr/local/bin/run-chef-{}".format(env.user)
if not remote_exists(script) or not remote_contains(script, cmd):
put(
StringIO(chef_script.format(remote=remote, chef_command=cmd)),
script,
use_sudo=True,
mode="0750",
)
if remote_exists(sudoers):
sudo("rm -f {}".format(sudoers))
if not remote_exists(wrapper):
put(StringIO(wrapper_script), wrapper, mode="0750", use_sudo=True)
sudo(script)
def local_chef(localhost: str, secrets: Optional[str] = None) -> None:
secrets_opts = ""
if secrets:
secrets_opts = f" -j {secrets}"
wrapper = "/usr/local/bin/run-chef-{}".format(env.user)
cmd = chef_command.format(host=localhost, secrets=secrets_opts)
if not os.path.exists(script):
tmp = os.path.join("/tmp", os.path.basename(script))
with open(tmp, "w") as f:
f.write(chef_script.format(remote=os.getcwd(), chef_command=cmd))
local("sudo install -T -m 755 {} {}".format(tmp, script))
os.unlink(tmp)
local("sudo rm -f {}".format(sudoers))
if not os.path.exists(wrapper):
tmp = os.path.join("/tmp", os.path.basename(wrapper))
with open(tmp, "w") as f:
f.write(wrapper_script)
local("sudo install -T -m 755 -o {} {} {}".format(env.user, tmp, wrapper))
os.unlink(tmp)
local(wrapper)
def rsync(
remote: str, secrets: Optional[str] = None, skip_secrets_upload: bool = False
) -> None:
if not remote_exists(remote):
sudo("mkdir -p {}".format(remote))
sudo("chown {} {}".format(env.user, remote))
rsync_project(
local_dir="./",
remote_dir=remote,
exclude=(
"data",
"boostrap",
"local-mode-cache",
".git",
"nodes",
"secrets",
"ohai/plugins",
".gnupg",
".cache",
),
extra_opts="-q",
delete=True,
)
if secrets and not skip_secrets_upload:
sudo(f"mkdir -p {remote}/secrets")
put(secrets, os.path.join(remote, secrets), mode="0640", use_sudo=True)
elif secrets: # secrets exists remotely but not here
print("Copying secrets file from remote host")
get(os.path.join(remote, secrets), secrets, use_sudo=True)
def install_git_hooks(here: str) -> None:
print("Installing git hooks")
pre_commit_src = os.path.join(here, "hooks", "pre-commit.sh")
pre_push_src = os.path.join(here, "hooks", "pre-push.sh")
pre_commit = os.path.join(here, ".git", "hooks", "pre-commit")
pre_push = os.path.join(here, ".git", "hooks", "pre-push")
hooks = {pre_commit: pre_commit_src, pre_push: pre_push_src}
for dest, src in hooks.items():
try:
os.symlink(src, dest)
print(" - {} -> {}".format(dest, src))
except OSError as e:
if e.errno == 17:
continue
raise e
def check_node(host: str, remote: str, local: bool) -> Tuple[Optional[str], bool]:
here = os.path.dirname(os.path.abspath(__file__))
try:
with open(os.path.join(here, "nodes.yaml")) as f:
conf = yaml.safe_load(f)
except Exception as e:
print(f"Cannot parse nodes.yaml: {e}", file=sys.stderr)
sys.exit(1)
if host not in conf["nodes"]:
print(f"Cannot find host '{host}' in nodes.yaml", file=sys.stderr)
sys.exit(1)
if host in conf["require_secrets"]:
secrets_file = os.path.join("secrets", f"{host}.json")
secrets = os.path.join(here, secrets_file)
skip_upload = validate_secrets(secrets, secrets_file, remote, local)
return secrets_file, skip_upload
rolefile = os.path.join(here, "roles", "{}.rb".format(host))
if not os.path.isfile(rolefile):
print("Cannot find file {rolefile}, aborting", file=sys.stderr)
sys.exit(1)
return None, False
def resolve_host() -> Tuple[str, bool]:
if env.host_string is None:
host = socket.gethostname()
local = True
elif env.host_string.startswith("localhost_"):
host = env.host_string.replace("localhost_", "")
local = True
else:
host = env.host_string
local = False
return host, local
@task
def sync(remote: str = "/usr/local/src/chefrepo/") -> None:
here = os.path.dirname(os.path.abspath(__file__))
install_git_hooks(here)
host, local = resolve_host()
secrets, skip_secrets_upload = check_node(host, remote, local)
os.chdir(here)
vendor()
if not local:
rsync(remote, secrets, skip_secrets_upload)
@task
def run(remote: str = "/usr/local/src/chefrepo/") -> None:
sync(remote)
here = os.path.dirname(os.path.abspath(__file__))
os.chdir(here)
host, local = resolve_host()
secrets, skip_secrets_upload = check_node(host, remote, local)
if local:
local_chef(host, secrets)
else:
chef(host, remote, secrets)