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