-
Notifications
You must be signed in to change notification settings - Fork 30
/
user-data.erb
241 lines (235 loc) · 8.63 KB
/
user-data.erb
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#cloud-config
coreos:
update:
reboot-strategy: off
etcd2:
#generate a new token for each unique cluster from https://discovery.etcd.io/new
<%= @etcd_discovery %>
addr: $public_ipv4:4001
peer-addr: $public_ipv4:7001
advertise-client-urls: http://$public_ipv4:2379
initial-advertise-peer-urls: http://$public_ipv4:2380
listen-client-urls: http://0.0.0.0:2379,http://0.0.0.0:4001
listen-peer-urls: http://$public_ipv4:2380,http://$public_ipv4:7001
data-dir: /var/lib/etcd2
units:
- name: etcd2.service
command: start
- name: fleet.service
command: start
<% if @flannel %>
- name: flannel.service
command: start
content: |
[Unit]
After=network-online.target
Wants=network-online.target
After=etcd.service
Wants=etcd.service
Description=flannel is an etcd backed overlay network for containers
[Service]
Type=notify
ExecStartPre=-/usr/bin/mkdir -p /opt/bin
ExecStartPre=/usr/bin/wget -N -P /opt/bin http://storage.googleapis.com/k8s/flanneld
ExecStartPre=/usr/bin/chmod +x /opt/bin/flanneld
ExecStartPre=/usr/bin/etcdctl set /coreos.com/network/config '{ "Network": "<%= @flannel_network %>" }'
ExecStart=/opt/bin/flanneld -etcd-endpoints http://127.0.0.1:4001 -iface eth1
- name: docker.service
command: start
content: |
[Unit]
After=flannel.service
Wants=flannel.service
Description=Docker Application Container Engine
Documentation=http://docs.docker.io
[Service]
EnvironmentFile=/run/flannel/subnet.env
ExecStartPre=/bin/mount --make-rprivate /
ExecStart=/usr/bin/docker -d --bip=${FLANNEL_SUBNET} --mtu=${FLANNEL_MTU} -s=overlay -H fd:// $DOCKER_OPTS
[Install]
WantedBy=multi-user.target
- name: setup-network-environment.service
command: start
content: |
[Unit]
Description=Setup Network Environment
Documentation=https://github.com/kelseyhightower/setup-network-environment
Requires=network-online.target
After=network-online.target
[Service]
ExecStartPre=-/usr/bin/mkdir -p /opt/bin
ExecStartPre=/usr/bin/wget -N -P /opt/bin http://storage.googleapis.com/k8s/setup-network-environment
ExecStartPre=/usr/bin/chmod +x /opt/bin/setup-network-environment
ExecStart=/opt/bin/setup-network-environment
RemainAfterExit=yes
Type=oneshot
<% end %>
<% @applications.each do |name,app| %>
- name: <%= name %>.service
command: start
content: |
[Unit]
Description=<%= name %>
Requires=registry.service
After=registry.service
[Service]
EnvironmentFile=/etc/environment
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill <%= name %>
ExecStartPre=-/usr/bin/docker rm <%= name %>
<% if app.has_key?('dockerfile') && app['dockerfile'] %>
ExecStartPre=-/run/factorish/build_image <%= app['dockerfile'] %> <%= app['repository'] %> <%= name %>
<% else %>
ExecStartPre=-/run/factorish/fetch_image <%= name %> <%= app['repository'] %>
<% end %>
ExecStart=/bin/sh -c "/usr/bin/docker run --rm <%= app['docker_options'].join(' ') %> -e BOOTSTRAP=<%= @bootstrap %> --name <%= name %> <%= app['repository']%> <%= app['command'] %>"
ExecStop=/usr/bin/docker stop <%= name %>
<% end %>
write_files:
- path: /etc/systemd/system/docker.service.d/50-insecure-registry.conf
content: |
[Service]
Environment=DOCKER_OPTS='--insecure-registry="127.0.0.1:5000"'
- path: /etc/motd
content: "Example Demo\n"
- path: /run/factorish/fetch_image
permissions: '0755'
content: |
#!/bin/bash
set -x
if [[ -z $1 || -z $2 ]]; then
echo 'Usage: fetch_service <name> <repository>'
exit 1
fi
if [[ -e /run/factorish/fetch_$1.lock ]]; then
echo -n 'waiting my turn'
while [[ -e /run/factorish/fetch_$1.lock ]]; do
sleep 5
echo -n .
done
echo .
else
echo $1 > /run/factorish/fetch_$1.lock
fi
echo Initializing service - $1
while ! etcdctl ls /; do
sleep 1
done
etcdctl ls /factorish/services/$1 >/dev/null 2>&1 || etcdctl mkdir /factorish/services/$1 >/dev/null 2>&1
if etcdctl mk /factorish/services/$1/fetching 1 >/dev/null 2>&1; then
if [[ -e /home/core/share/registry/$1.tgz ]]; then
echo - Loading $1 from host cache...
docker images registry | grep registry > /dev/null || \
docker load < /home/core/share/registry/$1.tgz > /dev/null 2>&1
else
echo - Pulling $2 from docker hub...
docker pull $2 > /dev/null
docker save $2 > /home/core/share/registry/$1.tgz
fi
etcdctl rm /factorish/services/$1/fetching
etcdctl mk /factorish/services/$1/cached
rm -f /run/factorish/fetch_$1.lock
exit 0
else
if ! etcdctl get /factorish/services/$1/cached; then
while etcdctl get /factorish/services/$1/fetching >/dev/null 2>&1; do
sleep 10
done
fi
echo - Loading $1 from host cache...
docker images registry | grep registry > /dev/null || \
docker load < /home/core/share/registry/$1.tgz > /dev/null 2>&1
fi
rm -f /run/factorish/fetch_$1.lock
- path: /run/factorish/build_image
permissions: '0755'
content: |
#!/bin/bash
set -x
if [[ -z $1 || -z $2 ]]; then
echo 'Usage: factorish_image <dockerfile> <repository> <name>'
exit 1
fi
echo -n waiting for registry to come online
while ! curl -s http://127.0.0.1:5000/v2/ >/dev/null; do
sleep 1
echo -n .
done
echo .
if [[ -e /run/factorish/build_$3.lock ]]; then
echo -n 'waiting my turn'
while [[ -e /run/factorish/build_$3.lock ]]; do
sleep 5
echo -n .
done
echo .
else
echo $1 > /run/factorish/build_$3.lock
fi
while etcdctl get /factorish/builds/$3/building >/dev/null 2>&1; do
sleep 10
done
echo Building $2 image
echo fetching 127.0.0.1:5000/$2 if it already exists
if ! docker pull 127.0.0.1:5000/$2; then
echo not found, lets build it.
etcdctl ls /factorish/builds >/dev/null 2>&1 || etcdctl mkdir /factorish/builds/$3 >/dev/null 2>&1
if etcdctl mk /factorish/builds/$3/building 1 >/dev/null 2>&1; then
docker build -t 127.0.0.1:5000/$2 $1
docker tag -f 127.0.0.1:5000/$2 $2
docker push 127.0.0.1:5000/$2
etcdctl rm /factorish/builds/$3/building >/dev/null 2>&1
etcdctl mk /factorish/builds/$3/built 1 >/dev/null 2>&1
rm -f /run/factorish/build_$3.lock
echo finished.
exit 0
fi
fi
if ! etcdctl get /factorish/builds/$3/built; then
while etcdctl get /factorish/builds/$3/building >/dev/null 2>&1; do
sleep 10
done
fi
if docker pull 127.0.0.1:5000/$2; then
docker tag -f 127.0.0.1:5000/$2 $2
echo - $2 pulled from private registry.
echo - run ./clean_registry if you expected this to rebuild.
fi
rm -f /run/factorish/build_$3.lock
- path: /etc/profile.d/functions.sh
permissions: '0755'
content: |
<% @applications.each do |name, app| %>
function run_<%= name %>() {
eval `cat /etc/environment | sed "s/^/export /"`
echo "Running <%= name %>"
docker run -d <%= app['docker_options'].join(' ') %> -e BOOTSTRAP=<%= @bootstrap %> --name <%= name %> <%= app['repository']%> <%= app['command'] %>
}
function kill_<%= name %>() {
/usr/bin/docker kill <%= name %>
/usr/bin/docker rm <%= name %>
}
function build_<%= name %>() {
docker build -t <%= app['repository'] %> <%=app['dockerfile'] %>
}
function log_<%= name %>() {
docker logs -f <%= name %>
}
function journal_<%= name %>() {
journalctl -f -u <%= name %>
}
function push_<%= name %>() {
docker tag -f <%= app['repository'] %> 127.0.0.1:5000/<%= app['repository'] %>
docker push 127.0.0.1:5000/<%= app['repository'] %>
}
function <%= name %>() {
/usr/bin/docker exec -it <%= name %> bash
}
<% end %>
function cleanup() {
etcdctl rm --recursive /services
}
function mysql() {
eval `cat /etc/environment | sed 's/^/export /'`
docker run -ti database mysql -h $COREOS_PRIVATE_IPV4 -P 4008 -uadmin -padmin
}