Finalized lf-networking Vagrant updates for Ubuntu
[releng/builder.git] / vagrant / lf-networking / configure_lf_infra.sh
1 #!/bin/bash
2
3 # script requires information about subdomain
4 if [ -z "$1" ]; then
5     >&2 echo "Please provide the subdomain to Vagrant"
6     exit 1
7 else
8     SUBDOM=$1
9 fi
10
11
12 all_systems() {
13     # install specific versions of puppet modules
14     puppet module install puppetlabs-stdlib -v 4.5.1
15     puppet module install puppetlabs-concat -v 1.2.0
16     puppet module install lex-dnsmasq -v 2.6.1
17
18     # write the subdomain information into a custom facter fact
19     mkdir -p /etc/facter/facts.d/
20     echo "subdomain=${SUBDOM}" > /etc/facter/facts.d/subdomain.txt
21
22     # final bits
23     puppet apply /vagrant/confignetwork.pp
24
25 }
26
27 rh_systems_init() {
28     # for some reason not all systems have @base installed
29     yum install -y -q @base
30
31     # also make sure that a few other utilities are definitely installed
32     yum install -y -q unzip xz
33
34     # install some needed internal networking configurations
35     yum install -y dnsmasq puppet
36
37     # remove current networking configurations
38     rm -f /etc/sysconfig/network-scripts/{ifcfg,route}-{eth,docker}*
39 }
40
41 rh_systems_post() {
42     # don't let cloud-init do funny things to our routing
43     chattr +i /etc/sysconfig/network-scripts/route-eth0
44
45     # setup the needed routing
46     cat <<EOL >> /etc/rc.d/post-cloud-init
47 #!/bin/bash
48
49 # always force puppet to rerun
50 /usr/bin/puppet apply /vagrant/confignetwork.pp
51 EOL
52
53     chmod +x /etc/rc.d/post-cloud-init
54
55     # so that the network stack doesn't futz with our resolv config
56     # after we've configured it
57     chattr +i /etc/resolv.conf
58 }
59
60 ubuntu_systems_post() {
61     # don't let cloud-init destroy our routing
62 #    chattr +i /etc/network/if-up.d/0000routing
63     echo "---> do nothing for now"
64 }
65
66 systemd_init() {
67     # create a post-cloud-init.service and enable it
68     cat <<EOL > /etc/systemd/system/post-cloud-init.service
69 [Unit]
70 Description=Post cloud-init script (overwrites some cloud-init config)
71 After=cloud-init-local.service
72 Requires=cloud-init-local.service
73
74 [Service]
75 Type=oneshot
76 ExecStart=/etc/rc.d/post-cloud-init
77 TimeoutSec=0
78 #RemainAfterExit=yes
79 #SysVStartPriority=99
80
81 # Output needs to appear in instance console output
82 StandardOutput=journal+console
83
84 [Install]
85 WantedBy=multi-user.target
86 EOL
87
88     /usr/bin/systemctl enable post-cloud-init.service
89     chattr +i /etc/systemd/system/post-cloud-init.service
90 }
91
92 sysv_init() {
93     # create the SysV init and enable it
94     cat <<EOL > /etc/init.d/post-cloud-init
95 #!/bin/bash
96
97 ### BEGIN INIT INFO
98 # Provides:         post-cloud-init
99 # Required-Start:   $local_fs $network $named $remote_fs cloud-init-local
100 # Should-Start:     $time
101 # Required-Stop:
102 # Should-Stop:
103 # Default-Start:    2 3 4 5
104 # Default-Stop:     0 1 6
105 # Short-Description:    Setup dnsmasq for LF Rackspace environment
106 # Description:      Setup dnsmasq for LF Rackspace environment after
107 #   cloud-init-local
108
109 # Return values acc. to LSB for all commands but status:
110 # 0       - success
111 # 1       - generic or unspecified error
112 # 2       - invalid or excess argument(s)
113 # 3       - unimplemented feature (e.g. "reload")
114 # 4       - user had insufficient privileges
115 # 5       - program is not installed
116 # 6       - program is not configured
117 # 7       - program is not running
118 # 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
119 #
120 # Note that starting an already running service, stopping
121 # or restarting a not-running service as well as the restart
122 # with force-reload (in case signaling is not supported) are
123 # considered a success.
124
125 RETVAL=0
126
127 prog="post-cloud-init"
128
129 start() {
130     echo -n $"Starting $prog: "
131     /etc/rc.d/post-cloud-init
132     RETVAL=$?
133     return $RETVAL
134 }
135
136 stop() {
137     echo -n $"Shutting down $prog:"
138     # No-op
139     RETVAL=7
140     return $RETVAL
141 }
142
143 case "$1" in
144     start)
145         start
146         RETVAL=$?
147         ;;
148     stop)
149         stop
150         RETVAL=$?
151         ;;
152     restart|try-restart|condrestart)
153         start
154         RETVAL=$?
155         ;;
156     status)
157         echo -n $"Checking for service $prog: "
158         # Return value is slightly different for the status command:
159         # 0 - service up and running
160         # 1 - service dead, but /var/run pid file exists
161         # 2 - service dead, but /var/lock file exists
162         # 3 - service not running (unused)
163         # 4 - service status unknown :-(
164         # 5--199 reserved (5-99 LSB, 100-149 distro, 150-199 appl.)
165         RETVAL=4
166         ;;
167     *)
168         echo "Usage: $0 {start|stop|status|try-restart|condrestart|restart|force-reload|reload}"
169         RETVAL=3
170         ;;
171 esac
172
173 exit $RETVAL
174 EOL
175     chmod +x /etc/init.d/post-cloud-init
176
177     /sbin/chkconfig --add post-cloud-init
178     /sbin/chkconfig post-cloud-init on
179 }
180
181 # Execute setup that all systems need
182 all_systems
183
184 echo "---> Checking distribution"
185 FACTER_OSFAMILY=`/usr/bin/facter osfamily`
186 FACTER_OS=`/usr/bin/facter operatingsystem`
187 case "$FACTER_OSFAMILY" in
188     RedHat)
189         rh_systems_init
190         case "$FACTER_OS" in
191             Fedora)
192                 echo "---> Fedora found"
193                 systemd_init
194             ;;
195             RedHat|CentOS)
196                 if [ `/usr/bin/facter operatingsystemrelease | /bin/cut -d '.' -f1` = "7" ]; then
197                     echo "---> CentOS 7"
198                     systemd_init
199                 else
200                     echo "---> CentOS 6"
201                     sysv_init
202                 fi
203             ;;
204             *)
205                 echo "---> Unknown RH Family OS: ${FACTER_OS}"
206             ;;
207         esac
208         rh_systems_post
209     ;;
210     Debian)
211         case "$FACTER_OS" in
212             Ubuntu)
213                 echo "---> Ubuntu found"
214                 ubuntu_systems_post
215             ;;
216             *)
217                 "---> Nothing to do for ${FACTER_OS}"
218             ;;
219         esac
220     ;;
221     *)
222         echo "---> Unknown OS: ${FACTER_OSFAMILY}"
223     ;;
224 esac
225
226 # vim: sw=4 ts=4 sts=4 et :