Skip to content

Commit b8e349f

Browse files
committed
Add a task to bootstrap a new node
1 parent 7b25bee commit b8e349f

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

Diff for: conf/minion.conf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
master: {{ master }}
2+
3+
{% if roles %}
4+
grains:
5+
roles:
6+
{% for role in roles -%}
7+
- {{ role }}
8+
{% endfor %}
9+
{% endif %}

Diff for: requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
invoke
22
fabric
3+
jinja2

Diff for: tasks/salt.py

+66-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,73 @@
11
from __future__ import absolute_import, division, print_function
22

33
import invoke
4-
import fabric.api as fabric
4+
import fabric.api
5+
import fabric.contrib.files
56

67
from .utils import cd, ssh_host
78

89

10+
SALT_MASTER = "192.168.5.1"
11+
12+
13+
@invoke.task
14+
def bootstrap(host, roles=None):
15+
# If the host does not have a . in it's address, then we'll assume it's the
16+
# short for of host.psf.io and add the .psf.io onto it.
17+
if "." not in host:
18+
host += ".psf.io"
19+
20+
# SSH into the root user of this server and bootstrap the server.
21+
with ssh_host("root@" + host):
22+
# Make sure this host hasn't already been bootstrapped.
23+
if fabric.contrib.files.exists("/etc/salt/minion.d/local.conf"):
24+
raise RuntimeError("{} is already bootstrapped.".format(host))
25+
26+
# Ok, we're going to bootstrap, first we need to add the Salt PPA
27+
fabric.api.run("apt-add-repository -y ppa:saltstack/salt")
28+
29+
# Then we need to update our local apt
30+
fabric.api.run("apt-get update -y")
31+
32+
# Then, upgrade all of the packages that are currently on this
33+
# machine.
34+
fabric.api.run("apt-get upgrade -y")
35+
fabric.api.run("apt-get dist-upgrade -y")
36+
37+
# Reboot the server to make sure any upgrades have been loaded.
38+
fabric.api.reboot()
39+
40+
# Install salt-minion and python-apt so we can manage things with
41+
# salt.
42+
fabric.api.run("apt-get install -y salt-minion python-apt")
43+
44+
# Drop the /etc/salt/minion.d/local.conf onto the server so that it
45+
# can connect with our salt master.
46+
fabric.contrib.files.upload_template(
47+
"conf/minion.conf",
48+
"/etc/salt/minion.d/local.conf",
49+
context={
50+
"master": SALT_MASTER,
51+
"roles": [r.strip() for r in roles.split(",") if r.strip()],
52+
},
53+
use_jinja=True,
54+
mode=0o0644,
55+
)
56+
57+
# Run salt-call state.highstate, this will fail the first time because
58+
# the Master hasn't accepted our key yet.
59+
fabric.api.run("salt-call state.highstate", warn_only=True)
60+
61+
# SSH into our salt master and accept the key for this server.
62+
with ssh_host("salt-master.psf.io"):
63+
fabric.api.sudo("salt-key -a {}".format(host))
64+
65+
# Finally SSH into our server one more time to run salt-call
66+
# state.highstate for real this time.
67+
with ssh_host("root@" + host):
68+
fabric.api.run("salt-call state.highstate")
69+
70+
971
@invoke.task(name="sync-changes")
1072
def sync_changes():
1173
# Push our changes to GitHub
@@ -14,11 +76,11 @@ def sync_changes():
1476

1577
# SSH into the salt master and pull our changes from GitHub
1678
with ssh_host("salt-master.psf.io"), fabric.cd("/srv/salt"):
17-
fabric.sudo("git pull --ff-only origin master")
79+
fabric.api.sudo("git pull --ff-only origin master")
1880

1981
with cd("pillar/secrets"):
2082
# Push our changes into the secret repository
21-
invoke.run("git push origin master", echo=True)
83+
invoke.api.run("git push origin master", echo=True)
2284

2385

2486
@invoke.task(default=True, pre=[sync_changes])
@@ -38,4 +100,4 @@ def highstate(hosts):
38100
# Loop over all the hosts and call salt-call state.highstate on them.
39101
for host in hosts:
40102
with ssh_host(host):
41-
fabric.sudo("salt-call state.highstate")
103+
fabric.api.sudo("salt-call state.highstate")

0 commit comments

Comments
 (0)