1
1
from __future__ import absolute_import , division , print_function
2
2
3
3
import invoke
4
- import fabric .api as fabric
4
+ import fabric .api
5
+ import fabric .contrib .files
5
6
6
7
from .utils import cd , ssh_host
7
8
8
9
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
+
9
71
@invoke .task (name = "sync-changes" )
10
72
def sync_changes ():
11
73
# Push our changes to GitHub
@@ -14,11 +76,11 @@ def sync_changes():
14
76
15
77
# SSH into the salt master and pull our changes from GitHub
16
78
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" )
18
80
19
81
with cd ("pillar/secrets" ):
20
82
# 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 )
22
84
23
85
24
86
@invoke .task (default = True , pre = [sync_changes ])
@@ -38,4 +100,4 @@ def highstate(hosts):
38
100
# Loop over all the hosts and call salt-call state.highstate on them.
39
101
for host in hosts :
40
102
with ssh_host (host ):
41
- fabric .sudo ("salt-call state.highstate" )
103
+ fabric .api . sudo ("salt-call state.highstate" )
0 commit comments