[cfg01] Use ssh config to set default user & key
[fuel.git] / mcp / scripts / docker-compose / files / entrypoint.sh
1 #!/bin/bash -e
2 ##############################################################################
3 # Copyright (c) 2018 Mirantis Inc., Enea AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 mkdir -p /etc/salt/{master.d,minion.d,proxy.d}
11
12 if [ ! -z "$SALT_EXT_PILLAR" ]; then
13     cp -avr "/tmp/${SALT_EXT_PILLAR}.conf" /etc/salt/master.d/
14 fi
15
16 if [ ! -f /home/ubuntu/.ssh/authorized_keys ]; then
17     install -D -o ubuntu /root/fuel/mcp/scripts/mcp.rsa.pub \
18                          /home/ubuntu/.ssh/authorized_keys
19     mkdir -p /root/.ssh/
20     echo 'User ubuntu' > /root/.ssh/config
21     echo 'IdentityFile /root/fuel/mcp/scripts/mcp.rsa' >> /root/.ssh/config
22 fi
23
24 if ! grep -q localhost /etc/hosts; then
25     # overwrite hosts only on first container up, to preserve cluster nodes
26     cp -a /root/fuel/mcp/scripts/docker-compose/files/hosts /etc/hosts
27 fi
28
29 # salt state does not properly configure file_roots in master.conf, hard set it
30 cp -a /root/fuel/mcp/scripts/docker-compose/files/opnfv_master.conf \
31       /etc/salt/master.d/opnfv_master.conf
32 echo 'master: localhost' > /etc/salt/minion.d/opnfv_slave.conf
33
34 # NOTE: Most Salt and/or reclass tools have issues traversing Docker mounts
35 # or detecting them as directories inside the container.
36 # For now, let's do a lot of copy operations to bypass this.
37 # Later, we will inject the OPNFV patched reclass model during image build.
38 rm -rf /srv/salt/reclass/classes/*
39 cp -ar /root/fuel/mcp/reclass/classes/* /srv/salt/reclass/classes
40 cp -ar /root/fuel/mcp/reclass/nodes/* /srv/salt/reclass/nodes
41 # Sensitive data should stay out of /root/fuel, which is exposed via Jenkins WS
42 cp -a /root/pod_config.yml \
43       /srv/salt/reclass/classes/cluster/all-mcp-arch-common/opnfv/pod_config.yml
44
45 # OPNFV formulas
46 prefix=/srv/salt/formula/salt-formulas
47 rm -f /root/fuel/mcp/salt-formulas/*/.git
48 cp -ar /root/fuel/mcp/salt-formulas/* ${prefix}/
49 for formula in 'armband' 'opendaylight' 'tacker' 'quagga'; do
50     ln -sf /root/fuel/mcp/salt-formulas/salt-formula-${formula}/* \
51            /srv/salt/env/prd/
52 done
53
54 # Re-create classes.service links that we destroyed above
55 for formula in ${prefix}/*; do
56     if [ -e "${formula}/metadata/service" ] && [[ ! $formula =~ \. ]]; then
57         ln -sf "${formula}/metadata/service" \
58                "/srv/salt/reclass/classes/service/${formula#${prefix}/salt-formula-}"
59     fi
60 done
61
62 # Create links for salt-formula-* packages to mimic git-style salt-formulas
63 for artifact in /usr/share/salt-formulas/env/_*/*; do
64     ln -sf "${artifact}" "/srv/salt/env/prd/${artifact#/usr/share/salt-formulas/env/}"
65 done
66 for artifact in /usr/share/salt-formulas/env/*; do
67     if [[ ! ${artifact} =~ ^_ ]]; then
68         ln -sf "${artifact}" "/srv/salt/env/prd/$(basename ${artifact})"
69     fi
70 done
71 for formula in /usr/share/salt-formulas/reclass/service/*; do
72     ln -sf "${formula}" "/srv/salt/reclass/classes/service/$(basename ${formula})"
73 done
74
75 # Temporary link queens configs to rocky
76 for f in /srv/salt/env/prd/*/files/queens; do
77     if [ ! -d "$f/../rocky" ]; then
78         ln -sf "$f" "$f/../rocky"
79     fi
80 done
81
82 # Tini init system resembles upstart very much, but needs a little adjustment
83 sed -i -e "s|return 'start/running' in |return 'is running' in |" \
84        -e "s|ret = _default_runlevel|return _default_runlevel|" \
85     /usr/lib/python2.7/dist-packages/salt/modules/upstart.py
86
87 # Workaround for: https://github.com/salt-formulas/reclass/issues/77
88 sed -i -e 's|\(ignore_overwritten_missing_references\)defaults.|\1|' \
89     /usr/local/lib/python2.7/dist-packages/reclass/settings.py
90
91 # Remove broken symlinks in /srv/salt, silences recurring warnings
92 find -L /srv/salt /srv/salt/env/prd/_* -maxdepth 1 -type l -delete
93
94 # Fix up any permissions after above file shuffling
95 chown root:root -R /srv/salt
96
97 service ssh start
98 service salt-minion start
99
100 if [[ $# -lt 1 ]] || [[ "$1" == "--"* ]]; then
101     exec /usr/bin/salt-master --log-file-level=quiet --log-level=info "$@"
102 else
103     exec "$@"
104 fi